blob: b5ed30b85c8a13a1e06730db83a0a247ce5438f5 [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
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000571LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000572 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000573}
574
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000575LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000576 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000577}
578
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000579LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000580 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000581}
582
Peter Collingbourne45681582016-12-02 03:05:41 +0000583LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
584 auto *Ty = unwrap<Type>(WrappedTy);
585 if (auto *PTy = dyn_cast<PointerType>(Ty))
586 return wrap(PTy->getElementType());
587 return wrap(cast<SequentialType>(Ty)->getElementType());
Gordon Henriksen76a03742007-09-18 03:18:57 +0000588}
589
590unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
591 return unwrap<ArrayType>(ArrayTy)->getNumElements();
592}
593
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000594unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
595 return unwrap<PointerType>(PointerTy)->getAddressSpace();
596}
597
Gordon Henriksen76a03742007-09-18 03:18:57 +0000598unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
599 return unwrap<VectorType>(VectorTy)->getNumElements();
600}
601
602/*--.. Operations on other types ...........................................--*/
603
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000604LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
605 return wrap(Type::getVoidTy(*unwrap(C)));
Owen Anderson55f1c092009-08-13 21:58:54 +0000606}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000607LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
608 return wrap(Type::getLabelTy(*unwrap(C)));
609}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000610
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000611LLVMTypeRef LLVMVoidType(void) {
612 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
613}
614LLVMTypeRef LLVMLabelType(void) {
615 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
616}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000617
618/*===-- Operations on values ----------------------------------------------===*/
619
620/*--.. Operations on all values ............................................--*/
621
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000622LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000623 return wrap(unwrap(Val)->getType());
624}
625
Peter Zotov3e4561c2016-04-06 22:21:29 +0000626LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
627 switch(unwrap(Val)->getValueID()) {
628#define HANDLE_VALUE(Name) \
629 case Value::Name##Val: \
630 return LLVM##Name##ValueKind;
631#include "llvm/IR/Value.def"
632 default:
633 return LLVMInstructionValueKind;
634 }
635}
636
Gordon Henriksen76a03742007-09-18 03:18:57 +0000637const char *LLVMGetValueName(LLVMValueRef Val) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000638 return unwrap(Val)->getName().data();
Gordon Henriksen76a03742007-09-18 03:18:57 +0000639}
640
641void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
642 unwrap(Val)->setName(Name);
643}
644
Matthias Braun8c209aa2017-01-28 02:02:38 +0000645LLVM_DUMP_METHOD void LLVMDumpValue(LLVMValueRef Val) {
Sam McCalla682dfb2017-01-30 05:40:52 +0000646 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
Gordon Henriksen1d0d24c2007-10-06 00:08:49 +0000647}
648
Peter Zotovcd93b372013-11-06 09:21:01 +0000649char* LLVMPrintValueToString(LLVMValueRef Val) {
Alp Tokere69170a2014-06-26 22:52:05 +0000650 std::string buf;
651 raw_string_ostream os(buf);
Peter Zotovcd93b372013-11-06 09:21:01 +0000652
Richard Trieuc1485222014-06-21 02:43:02 +0000653 if (unwrap(Val))
654 unwrap(Val)->print(os);
655 else
656 os << "Printing <null> Value";
657
Alp Tokere69170a2014-06-26 22:52:05 +0000658 os.flush();
659
660 return strdup(buf.c_str());
Peter Zotovcd93b372013-11-06 09:21:01 +0000661}
662
Chris Lattner40cf28d2009-10-12 04:01:02 +0000663void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
664 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
665}
Gordon Henriksen29e38942008-12-19 18:39:45 +0000666
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000667int LLVMHasMetadata(LLVMValueRef Inst) {
668 return unwrap<Instruction>(Inst)->hasMetadata();
669}
670
671LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000672 auto *I = unwrap<Instruction>(Inst);
673 assert(I && "Expected instruction");
674 if (auto *MD = I->getMetadata(KindID))
675 return wrap(MetadataAsValue::get(I->getContext(), MD));
676 return nullptr;
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000677}
678
Bjorn Steinbrinka09ac002015-01-28 16:35:59 +0000679// MetadataAsValue uses a canonical format which strips the actual MDNode for
680// MDNode with just a single constant value, storing just a ConstantAsMetadata
681// This undoes this canonicalization, reconstructing the MDNode.
682static MDNode *extractMDNode(MetadataAsValue *MAV) {
683 Metadata *MD = MAV->getMetadata();
684 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
685 "Expected a metadata node or a canonicalized constant");
686
687 if (MDNode *N = dyn_cast<MDNode>(MD))
688 return N;
689
690 return MDNode::get(MAV->getContext(), MD);
691}
692
693void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
694 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
695
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000696 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000697}
698
Gordon Henriksen29e38942008-12-19 18:39:45 +0000699/*--.. Conversion functions ................................................--*/
700
701#define LLVM_DEFINE_VALUE_CAST(name) \
702 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
703 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
704 }
705
706LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
707
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000708LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
709 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
710 if (isa<MDNode>(MD->getMetadata()) ||
711 isa<ValueAsMetadata>(MD->getMetadata()))
712 return Val;
713 return nullptr;
714}
715
716LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
717 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
718 if (isa<MDString>(MD->getMetadata()))
719 return Val;
720 return nullptr;
721}
722
Chris Lattner40cf28d2009-10-12 04:01:02 +0000723/*--.. Operations on Uses ..................................................--*/
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000724LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
Chris Lattner40cf28d2009-10-12 04:01:02 +0000725 Value *V = unwrap(Val);
726 Value::use_iterator I = V->use_begin();
727 if (I == V->use_end())
Craig Topperc6207612014-04-09 06:08:46 +0000728 return nullptr;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000729 return wrap(&*I);
Chris Lattner40cf28d2009-10-12 04:01:02 +0000730}
731
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000732LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
733 Use *Next = unwrap(U)->getNext();
734 if (Next)
735 return wrap(Next);
Craig Topperc6207612014-04-09 06:08:46 +0000736 return nullptr;
Chris Lattner40cf28d2009-10-12 04:01:02 +0000737}
738
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000739LLVMValueRef LLVMGetUser(LLVMUseRef U) {
740 return wrap(unwrap(U)->getUser());
Chris Lattner40cf28d2009-10-12 04:01:02 +0000741}
742
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000743LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
744 return wrap(unwrap(U)->get());
Chris Lattner40cf28d2009-10-12 04:01:02 +0000745}
746
747/*--.. Operations on Users .................................................--*/
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000748
749static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
750 unsigned Index) {
751 Metadata *Op = N->getOperand(Index);
752 if (!Op)
753 return nullptr;
754 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
755 return wrap(C->getValue());
756 return wrap(MetadataAsValue::get(Context, Op));
757}
758
Chris Lattner40cf28d2009-10-12 04:01:02 +0000759LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
Torok Edwinfec812e2011-10-06 12:13:11 +0000760 Value *V = unwrap(Val);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000761 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
762 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
763 assert(Index == 0 && "Function-local metadata can only have one operand");
764 return wrap(L->getValue());
765 }
766 return getMDNodeOperandImpl(V->getContext(),
767 cast<MDNode>(MD->getMetadata()), Index);
768 }
769
Torok Edwinfec812e2011-10-06 12:13:11 +0000770 return wrap(cast<User>(V)->getOperand(Index));
Chris Lattner40cf28d2009-10-12 04:01:02 +0000771}
Gordon Henriksen29e38942008-12-19 18:39:45 +0000772
Peter Zotovb19f78f2014-08-12 02:55:40 +0000773LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
774 Value *V = unwrap(Val);
775 return wrap(&cast<User>(V)->getOperandUse(Index));
776}
777
Erick Tryzelaarb4d48702010-08-20 14:51:22 +0000778void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
779 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
780}
781
782int LLVMGetNumOperands(LLVMValueRef Val) {
Torok Edwinfec812e2011-10-06 12:13:11 +0000783 Value *V = unwrap(Val);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000784 if (isa<MetadataAsValue>(V))
785 return LLVMGetMDNodeNumOperands(Val);
786
Torok Edwinfec812e2011-10-06 12:13:11 +0000787 return cast<User>(V)->getNumOperands();
Erick Tryzelaarb4d48702010-08-20 14:51:22 +0000788}
789
Gordon Henriksen76a03742007-09-18 03:18:57 +0000790/*--.. Operations on constants of any type .................................--*/
791
Gordon Henriksen1046c732007-10-06 15:11:06 +0000792LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000793 return wrap(Constant::getNullValue(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000794}
795
Gordon Henriksen1046c732007-10-06 15:11:06 +0000796LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000797 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000798}
799
800LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
Owen Andersonb292b8c2009-07-30 23:03:37 +0000801 return wrap(UndefValue::get(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000802}
803
Chris Lattner25963c62010-01-09 22:27:07 +0000804LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
Gordon Henriksendc88c062007-09-18 18:07:51 +0000805 return isa<Constant>(unwrap(Ty));
806}
807
Chris Lattner25963c62010-01-09 22:27:07 +0000808LLVMBool LLVMIsNull(LLVMValueRef Val) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000809 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
810 return C->isNullValue();
811 return false;
812}
813
Chris Lattner25963c62010-01-09 22:27:07 +0000814LLVMBool LLVMIsUndef(LLVMValueRef Val) {
Gordon Henriksendc88c062007-09-18 18:07:51 +0000815 return isa<UndefValue>(unwrap(Val));
816}
817
Chris Lattner7f318242009-07-06 17:29:59 +0000818LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
Amaury Sechet9bbda192016-04-25 22:23:35 +0000819 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
Chris Lattner7f318242009-07-06 17:29:59 +0000820}
821
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000822/*--.. Operations on metadata nodes ........................................--*/
823
824LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
825 unsigned SLen) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000826 LLVMContext &Context = *unwrap(C);
827 return wrap(MetadataAsValue::get(
828 Context, MDString::get(Context, StringRef(Str, SLen))));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000829}
830
831LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
832 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
833}
834
835LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
836 unsigned Count) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000837 LLVMContext &Context = *unwrap(C);
838 SmallVector<Metadata *, 8> MDs;
839 for (auto *OV : makeArrayRef(Vals, Count)) {
840 Value *V = unwrap(OV);
841 Metadata *MD;
842 if (!V)
843 MD = nullptr;
844 else if (auto *C = dyn_cast<Constant>(V))
845 MD = ConstantAsMetadata::get(C);
846 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
847 MD = MDV->getMetadata();
848 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
849 "outside of direct argument to call");
850 } else {
851 // This is function-local metadata. Pretend to make an MDNode.
852 assert(Count == 1 &&
853 "Expected only one operand to function-local metadata");
854 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
855 }
856
857 MDs.push_back(MD);
858 }
859 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000860}
861
862LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
863 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
864}
865
Amaury Sechet7c2883c2016-04-03 21:06:04 +0000866const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000867 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
868 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
Amaury Sechet7c2883c2016-04-03 21:06:04 +0000869 *Length = S->getString().size();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000870 return S->getString().data();
871 }
Amaury Sechet7c2883c2016-04-03 21:06:04 +0000872 *Length = 0;
Craig Topperc6207612014-04-09 06:08:46 +0000873 return nullptr;
Torok Edwinfec812e2011-10-06 12:13:11 +0000874}
875
Amaury Sechetb130f432016-04-23 00:12:45 +0000876unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000877 auto *MD = cast<MetadataAsValue>(unwrap(V));
878 if (isa<ValueAsMetadata>(MD->getMetadata()))
879 return 1;
880 return cast<MDNode>(MD->getMetadata())->getNumOperands();
Duncan Sands12ccbe72012-09-19 20:29:39 +0000881}
882
Amaury Sechetb130f432016-04-23 00:12:45 +0000883void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000884 auto *MD = cast<MetadataAsValue>(unwrap(V));
885 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
886 *Dest = wrap(MDV->getValue());
887 return;
888 }
889 const auto *N = cast<MDNode>(MD->getMetadata());
Duncan Sands12ccbe72012-09-19 20:29:39 +0000890 const unsigned numOperands = N->getNumOperands();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000891 LLVMContext &Context = unwrap(V)->getContext();
Duncan Sands12ccbe72012-09-19 20:29:39 +0000892 for (unsigned i = 0; i < numOperands; i++)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000893 Dest[i] = getMDNodeOperandImpl(Context, N, i);
Duncan Sands12ccbe72012-09-19 20:29:39 +0000894}
895
Amaury Sechetb130f432016-04-23 00:12:45 +0000896unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
897 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
Torok Edwin2e9affe2011-10-14 20:37:42 +0000898 return N->getNumOperands();
899 }
900 return 0;
Torok Edwinfec812e2011-10-06 12:13:11 +0000901}
902
Amaury Sechetb130f432016-04-23 00:12:45 +0000903void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
904 LLVMValueRef *Dest) {
905 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
Torok Edwin2e9affe2011-10-14 20:37:42 +0000906 if (!N)
907 return;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000908 LLVMContext &Context = unwrap(M)->getContext();
Torok Edwin2e9affe2011-10-14 20:37:42 +0000909 for (unsigned i=0;i<N->getNumOperands();i++)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000910 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
Torok Edwinfec812e2011-10-06 12:13:11 +0000911}
912
Amaury Sechetb130f432016-04-23 00:12:45 +0000913void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
914 LLVMValueRef Val) {
915 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
Devang Patel92245402011-12-20 19:29:36 +0000916 if (!N)
917 return;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000918 if (!Val)
919 return;
Bjorn Steinbrinka09ac002015-01-28 16:35:59 +0000920 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
Devang Patel92245402011-12-20 19:29:36 +0000921}
922
Gordon Henriksen76a03742007-09-18 03:18:57 +0000923/*--.. Operations on scalar constants ......................................--*/
924
Gordon Henriksen1046c732007-10-06 15:11:06 +0000925LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
Chris Lattner25963c62010-01-09 22:27:07 +0000926 LLVMBool SignExtend) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000927 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000928}
929
Chris Lattner4329e072010-11-23 02:47:22 +0000930LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
931 unsigned NumWords,
932 const uint64_t Words[]) {
933 IntegerType *Ty = unwrap<IntegerType>(IntTy);
934 return wrap(ConstantInt::get(Ty->getContext(),
Jeffrey Yasskin7a162882011-07-18 21:45:40 +0000935 APInt(Ty->getBitWidth(),
936 makeArrayRef(Words, NumWords))));
Chris Lattner4329e072010-11-23 02:47:22 +0000937}
938
Erick Tryzelaardd991352009-08-16 23:36:46 +0000939LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
940 uint8_t Radix) {
941 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
942 Radix));
943}
944
945LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
946 unsigned SLen, uint8_t Radix) {
947 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
948 Radix));
Gordon Henriksen931e1212008-02-02 01:07:50 +0000949}
950
Gordon Henriksen1046c732007-10-06 15:11:06 +0000951LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
Erick Tryzelaardd991352009-08-16 23:36:46 +0000952 return wrap(ConstantFP::get(unwrap(RealTy), N));
Gordon Henriksen931e1212008-02-02 01:07:50 +0000953}
954
955LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
Erick Tryzelaardd991352009-08-16 23:36:46 +0000956 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
957}
958
959LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
960 unsigned SLen) {
961 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000962}
963
Chris Lattner40cf28d2009-10-12 04:01:02 +0000964unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
965 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
966}
967
968long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
969 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
970}
971
Peter Zotov1d98e6d2014-10-28 19:46:44 +0000972double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
973 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
974 Type *Ty = cFP->getType();
975
976 if (Ty->isFloatTy()) {
977 *LosesInfo = false;
978 return cFP->getValueAPF().convertToFloat();
979 }
980
981 if (Ty->isDoubleTy()) {
982 *LosesInfo = false;
983 return cFP->getValueAPF().convertToDouble();
984 }
985
986 bool APFLosesInfo;
987 APFloat APF = cFP->getValueAPF();
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000988 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
Peter Zotov1d98e6d2014-10-28 19:46:44 +0000989 *LosesInfo = APFLosesInfo;
990 return APF.convertToDouble();
991}
992
Gordon Henriksen76a03742007-09-18 03:18:57 +0000993/*--.. Operations on composite constants ...................................--*/
994
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000995LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
Chris Lattner25963c62010-01-09 22:27:07 +0000996 unsigned Length,
997 LLVMBool DontNullTerminate) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000998 /* Inverted the sense of AddNull because ', 0)' is a
999 better mnemonic for null termination than ', 1)'. */
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001000 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1001 DontNullTerminate == 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001002}
Amaury Sechet006ce632016-03-13 00:54:40 +00001003
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001004LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
Chris Lattner25963c62010-01-09 22:27:07 +00001005 LLVMBool DontNullTerminate) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001006 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1007 DontNullTerminate);
1008}
Peter Zotovf9aa8822014-08-03 23:54:16 +00001009
Amaury Sechet006ce632016-03-13 00:54:40 +00001010LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1011 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
Peter Zotovf9aa8822014-08-03 23:54:16 +00001012}
1013
Amaury Sechet006ce632016-03-13 00:54:40 +00001014LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1015 return unwrap<ConstantDataSequential>(C)->isString();
Peter Zotovf9aa8822014-08-03 23:54:16 +00001016}
1017
Amaury Sechet7c2883c2016-04-03 21:06:04 +00001018const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1019 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1020 *Length = Str.size();
1021 return Str.data();
Peter Zotovf9aa8822014-08-03 23:54:16 +00001022}
1023
Gordon Henriksen1046c732007-10-06 15:11:06 +00001024LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1025 LLVMValueRef *ConstantVals, unsigned Length) {
Jay Foad83be3612011-06-22 09:24:39 +00001026 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1027 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001028}
Peter Zotovf9aa8822014-08-03 23:54:16 +00001029
Amaury Sechetc78768f2016-03-13 00:40:12 +00001030LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1031 LLVMValueRef *ConstantVals,
1032 unsigned Count, LLVMBool Packed) {
1033 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1034 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1035 Packed != 0));
1036}
1037
Gordon Henriksen1046c732007-10-06 15:11:06 +00001038LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
Chris Lattner25963c62010-01-09 22:27:07 +00001039 LLVMBool Packed) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001040 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1041 Packed);
Gordon Henriksen76a03742007-09-18 03:18:57 +00001042}
Rafael Espindola784ad242011-07-14 19:09:08 +00001043
1044LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1045 LLVMValueRef *ConstantVals,
1046 unsigned Count) {
1047 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
Chris Lattner229907c2011-07-18 04:54:35 +00001048 StructType *Ty = cast<StructType>(unwrap(StructTy));
Rafael Espindola784ad242011-07-14 19:09:08 +00001049
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001050 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
Rafael Espindola784ad242011-07-14 19:09:08 +00001051}
1052
Gordon Henriksen1046c732007-10-06 15:11:06 +00001053LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001054 return wrap(ConstantVector::get(makeArrayRef(
Chris Lattner69229312011-02-15 00:14:00 +00001055 unwrap<Constant>(ScalarConstantVals, Size), Size)));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001056}
Torok Edwin05dc9d62011-10-06 12:39:34 +00001057
1058/*-- Opcode mapping */
1059
1060static LLVMOpcode map_to_llvmopcode(int opcode)
1061{
1062 switch (opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00001063 default: llvm_unreachable("Unhandled Opcode.");
Torok Edwin05dc9d62011-10-06 12:39:34 +00001064#define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
Chandler Carruth9fb823b2013-01-02 11:36:10 +00001065#include "llvm/IR/Instruction.def"
Torok Edwin05dc9d62011-10-06 12:39:34 +00001066#undef HANDLE_INST
Torok Edwin05dc9d62011-10-06 12:39:34 +00001067 }
1068}
1069
1070static int map_from_llvmopcode(LLVMOpcode code)
1071{
1072 switch (code) {
1073#define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
Chandler Carruth9fb823b2013-01-02 11:36:10 +00001074#include "llvm/IR/Instruction.def"
Torok Edwin05dc9d62011-10-06 12:39:34 +00001075#undef HANDLE_INST
Torok Edwin05dc9d62011-10-06 12:39:34 +00001076 }
David Blaikie486df732012-01-16 23:24:27 +00001077 llvm_unreachable("Unhandled Opcode.");
Torok Edwin05dc9d62011-10-06 12:39:34 +00001078}
1079
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001080/*--.. Constant expressions ................................................--*/
1081
Chris Lattner40cf28d2009-10-12 04:01:02 +00001082LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00001083 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
Chris Lattner40cf28d2009-10-12 04:01:02 +00001084}
1085
Duncan Sandsd334aca2009-05-21 15:52:21 +00001086LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001087 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
Duncan Sandsd334aca2009-05-21 15:52:21 +00001088}
1089
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001090LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001091 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001092}
1093
1094LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001095 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001096}
1097
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001098LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001099 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001100}
1101
1102LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001103 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001104}
1105
1106
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001107LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001108 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001109}
1110
1111LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001112 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001113}
1114
1115LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001116 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001117 unwrap<Constant>(RHSConstant)));
1118}
1119
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001120LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1121 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001122 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001123 unwrap<Constant>(RHSConstant)));
1124}
1125
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001126LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1127 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001128 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001129 unwrap<Constant>(RHSConstant)));
1130}
1131
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001132LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001133 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001134 unwrap<Constant>(RHSConstant)));
1135}
1136
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001137LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001138 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001139 unwrap<Constant>(RHSConstant)));
1140}
1141
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001142LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1143 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001144 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001145 unwrap<Constant>(RHSConstant)));
1146}
1147
1148LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1149 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001150 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001151 unwrap<Constant>(RHSConstant)));
1152}
1153
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001154LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1155 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1156 unwrap<Constant>(RHSConstant)));
1157}
1158
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001159LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001160 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001161 unwrap<Constant>(RHSConstant)));
1162}
1163
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001164LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1165 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001166 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001167 unwrap<Constant>(RHSConstant)));
1168}
1169
1170LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1171 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001172 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001173 unwrap<Constant>(RHSConstant)));
1174}
1175
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001176LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001177 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001178 unwrap<Constant>(RHSConstant)));
1179}
1180
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001181LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001182 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001183 unwrap<Constant>(RHSConstant)));
1184}
1185
Manuel Jacob49fafb12016-10-04 23:32:42 +00001186LLVMValueRef LLVMConstExactUDiv(LLVMValueRef LHSConstant,
1187 LLVMValueRef RHSConstant) {
1188 return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1189 unwrap<Constant>(RHSConstant)));
1190}
1191
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001192LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001193 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001194 unwrap<Constant>(RHSConstant)));
1195}
1196
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001197LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
1198 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001199 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001200 unwrap<Constant>(RHSConstant)));
1201}
1202
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001203LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001204 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001205 unwrap<Constant>(RHSConstant)));
1206}
1207
1208LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001209 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001210 unwrap<Constant>(RHSConstant)));
1211}
1212
1213LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001214 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001215 unwrap<Constant>(RHSConstant)));
1216}
1217
1218LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001219 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001220 unwrap<Constant>(RHSConstant)));
1221}
1222
1223LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001224 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001225 unwrap<Constant>(RHSConstant)));
1226}
1227
1228LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001229 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001230 unwrap<Constant>(RHSConstant)));
1231}
1232
1233LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001234 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001235 unwrap<Constant>(RHSConstant)));
1236}
1237
1238LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1239 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Owen Anderson487375e2009-07-29 18:55:55 +00001240 return wrap(ConstantExpr::getICmp(Predicate,
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001241 unwrap<Constant>(LHSConstant),
1242 unwrap<Constant>(RHSConstant)));
1243}
1244
1245LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1246 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Owen Anderson487375e2009-07-29 18:55:55 +00001247 return wrap(ConstantExpr::getFCmp(Predicate,
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001248 unwrap<Constant>(LHSConstant),
1249 unwrap<Constant>(RHSConstant)));
1250}
1251
1252LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001253 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1254 unwrap<Constant>(RHSConstant)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001255}
1256
1257LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001258 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001259 unwrap<Constant>(RHSConstant)));
1260}
1261
1262LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001263 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001264 unwrap<Constant>(RHSConstant)));
1265}
1266
1267LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1268 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
Jay Foaded8db7d2011-07-21 14:31:17 +00001269 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1270 NumIndices);
David Blaikie4a2e73b2015-04-02 18:55:32 +00001271 return wrap(ConstantExpr::getGetElementPtr(
1272 nullptr, unwrap<Constant>(ConstantVal), IdxList));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001273}
1274
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001275LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1276 LLVMValueRef *ConstantIndices,
1277 unsigned NumIndices) {
1278 Constant* Val = unwrap<Constant>(ConstantVal);
Jay Foaded8db7d2011-07-21 14:31:17 +00001279 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1280 NumIndices);
David Blaikie4a2e73b2015-04-02 18:55:32 +00001281 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList));
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001282}
1283
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001284LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001285 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001286 unwrap(ToType)));
1287}
1288
1289LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001290 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001291 unwrap(ToType)));
1292}
1293
1294LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001295 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001296 unwrap(ToType)));
1297}
1298
1299LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001300 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001301 unwrap(ToType)));
1302}
1303
1304LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001305 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001306 unwrap(ToType)));
1307}
1308
1309LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001310 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001311 unwrap(ToType)));
1312}
1313
1314LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Owen Anderson487375e2009-07-29 18:55:55 +00001315 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001316 unwrap(ToType)));
1317}
1318
1319LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Owen Anderson487375e2009-07-29 18:55:55 +00001320 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001321 unwrap(ToType)));
1322}
1323
1324LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001325 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001326 unwrap(ToType)));
1327}
1328
1329LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001330 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001331 unwrap(ToType)));
1332}
1333
1334LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001335 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001336 unwrap(ToType)));
1337}
1338
1339LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001340 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001341 unwrap(ToType)));
1342}
1343
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001344LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1345 LLVMTypeRef ToType) {
1346 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1347 unwrap(ToType)));
1348}
1349
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001350LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1351 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001352 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001353 unwrap(ToType)));
1354}
1355
1356LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1357 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001358 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001359 unwrap(ToType)));
1360}
1361
1362LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1363 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001364 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001365 unwrap(ToType)));
1366}
1367
1368LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1369 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001370 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001371 unwrap(ToType)));
1372}
1373
1374LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
Chris Lattner25963c62010-01-09 22:27:07 +00001375 LLVMBool isSigned) {
Chris Lattner69229312011-02-15 00:14:00 +00001376 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1377 unwrap(ToType), isSigned));
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001378}
1379
1380LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001381 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001382 unwrap(ToType)));
1383}
1384
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001385LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1386 LLVMValueRef ConstantIfTrue,
1387 LLVMValueRef ConstantIfFalse) {
Chris Lattner69229312011-02-15 00:14:00 +00001388 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001389 unwrap<Constant>(ConstantIfTrue),
1390 unwrap<Constant>(ConstantIfFalse)));
1391}
1392
1393LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1394 LLVMValueRef IndexConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001395 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001396 unwrap<Constant>(IndexConstant)));
1397}
1398
1399LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1400 LLVMValueRef ElementValueConstant,
1401 LLVMValueRef IndexConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001402 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001403 unwrap<Constant>(ElementValueConstant),
1404 unwrap<Constant>(IndexConstant)));
1405}
1406
1407LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1408 LLVMValueRef VectorBConstant,
1409 LLVMValueRef MaskConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001410 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001411 unwrap<Constant>(VectorBConstant),
1412 unwrap<Constant>(MaskConstant)));
1413}
1414
Dan Gohmand5104a52008-11-03 22:55:43 +00001415LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1416 unsigned NumIdx) {
Chris Lattner69229312011-02-15 00:14:00 +00001417 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001418 makeArrayRef(IdxList, NumIdx)));
Dan Gohmand5104a52008-11-03 22:55:43 +00001419}
1420
1421LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1422 LLVMValueRef ElementValueConstant,
1423 unsigned *IdxList, unsigned NumIdx) {
Chris Lattner69229312011-02-15 00:14:00 +00001424 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
Dan Gohmand5104a52008-11-03 22:55:43 +00001425 unwrap<Constant>(ElementValueConstant),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001426 makeArrayRef(IdxList, NumIdx)));
Dan Gohmand5104a52008-11-03 22:55:43 +00001427}
1428
Chris Lattner25963c62010-01-09 22:27:07 +00001429LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1430 const char *Constraints,
1431 LLVMBool HasSideEffects,
Benjamin Kramer8dd0edc2012-09-10 11:52:00 +00001432 LLVMBool IsAlignStack) {
Chris Lattner25963c62010-01-09 22:27:07 +00001433 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
Benjamin Kramer8dd0edc2012-09-10 11:52:00 +00001434 Constraints, HasSideEffects, IsAlignStack));
Chris Lattner3d1f5522008-12-17 21:39:50 +00001435}
1436
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00001437LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1438 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1439}
1440
Gordon Henriksen76a03742007-09-18 03:18:57 +00001441/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1442
Gordon Henriksen265f7802008-03-19 01:11:35 +00001443LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1444 return wrap(unwrap<GlobalValue>(Global)->getParent());
1445}
1446
Chris Lattner25963c62010-01-09 22:27:07 +00001447LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001448 return unwrap<GlobalValue>(Global)->isDeclaration();
1449}
1450
1451LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001452 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001453 case GlobalValue::ExternalLinkage:
1454 return LLVMExternalLinkage;
1455 case GlobalValue::AvailableExternallyLinkage:
1456 return LLVMAvailableExternallyLinkage;
1457 case GlobalValue::LinkOnceAnyLinkage:
1458 return LLVMLinkOnceAnyLinkage;
1459 case GlobalValue::LinkOnceODRLinkage:
1460 return LLVMLinkOnceODRLinkage;
1461 case GlobalValue::WeakAnyLinkage:
1462 return LLVMWeakAnyLinkage;
1463 case GlobalValue::WeakODRLinkage:
1464 return LLVMWeakODRLinkage;
1465 case GlobalValue::AppendingLinkage:
1466 return LLVMAppendingLinkage;
1467 case GlobalValue::InternalLinkage:
1468 return LLVMInternalLinkage;
1469 case GlobalValue::PrivateLinkage:
1470 return LLVMPrivateLinkage;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001471 case GlobalValue::ExternalWeakLinkage:
1472 return LLVMExternalWeakLinkage;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001473 case GlobalValue::CommonLinkage:
1474 return LLVMCommonLinkage;
1475 }
1476
David Blaikiea5708dc2012-01-17 07:00:13 +00001477 llvm_unreachable("Invalid GlobalValue linkage!");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001478}
1479
1480void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001481 GlobalValue *GV = unwrap<GlobalValue>(Global);
1482
1483 switch (Linkage) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001484 case LLVMExternalLinkage:
1485 GV->setLinkage(GlobalValue::ExternalLinkage);
1486 break;
1487 case LLVMAvailableExternallyLinkage:
1488 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1489 break;
1490 case LLVMLinkOnceAnyLinkage:
1491 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1492 break;
1493 case LLVMLinkOnceODRLinkage:
1494 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1495 break;
Bill Wendling34bc34e2012-08-17 18:33:14 +00001496 case LLVMLinkOnceODRAutoHideLinkage:
Rafael Espindola716e7402013-11-01 17:09:14 +00001497 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1498 "longer supported.");
Bill Wendling34bc34e2012-08-17 18:33:14 +00001499 break;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001500 case LLVMWeakAnyLinkage:
1501 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1502 break;
1503 case LLVMWeakODRLinkage:
1504 GV->setLinkage(GlobalValue::WeakODRLinkage);
1505 break;
1506 case LLVMAppendingLinkage:
1507 GV->setLinkage(GlobalValue::AppendingLinkage);
1508 break;
1509 case LLVMInternalLinkage:
1510 GV->setLinkage(GlobalValue::InternalLinkage);
1511 break;
1512 case LLVMPrivateLinkage:
1513 GV->setLinkage(GlobalValue::PrivateLinkage);
1514 break;
1515 case LLVMLinkerPrivateLinkage:
Rafael Espindola2fb5bc32014-03-13 23:18:37 +00001516 GV->setLinkage(GlobalValue::PrivateLinkage);
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001517 break;
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001518 case LLVMLinkerPrivateWeakLinkage:
Rafael Espindola2fb5bc32014-03-13 23:18:37 +00001519 GV->setLinkage(GlobalValue::PrivateLinkage);
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001520 break;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001521 case LLVMDLLImportLinkage:
Nico Rieck7157bb72014-01-14 15:22:47 +00001522 DEBUG(errs()
1523 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001524 break;
1525 case LLVMDLLExportLinkage:
Nico Rieck7157bb72014-01-14 15:22:47 +00001526 DEBUG(errs()
1527 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001528 break;
1529 case LLVMExternalWeakLinkage:
1530 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1531 break;
1532 case LLVMGhostLinkage:
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001533 DEBUG(errs()
1534 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001535 break;
1536 case LLVMCommonLinkage:
1537 GV->setLinkage(GlobalValue::CommonLinkage);
1538 break;
1539 }
Gordon Henriksen76a03742007-09-18 03:18:57 +00001540}
1541
1542const char *LLVMGetSection(LLVMValueRef Global) {
Rafael Espindola83658d62016-05-11 18:21:59 +00001543 // Using .data() is safe because of how GlobalObject::setSection is
1544 // implemented.
1545 return unwrap<GlobalValue>(Global)->getSection().data();
Gordon Henriksen76a03742007-09-18 03:18:57 +00001546}
1547
1548void LLVMSetSection(LLVMValueRef Global, const char *Section) {
Rafael Espindola99e05cf2014-05-13 18:45:48 +00001549 unwrap<GlobalObject>(Global)->setSection(Section);
Gordon Henriksen76a03742007-09-18 03:18:57 +00001550}
1551
1552LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1553 return static_cast<LLVMVisibility>(
1554 unwrap<GlobalValue>(Global)->getVisibility());
1555}
1556
1557void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1558 unwrap<GlobalValue>(Global)
1559 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1560}
1561
Reid Kleckner2fae26f2014-03-05 02:34:23 +00001562LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1563 return static_cast<LLVMDLLStorageClass>(
1564 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1565}
1566
1567void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1568 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1569 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1570}
1571
Tim Northoverad96d012014-03-10 19:24:35 +00001572LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001573 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
Tim Northoverad96d012014-03-10 19:24:35 +00001574}
1575
1576void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001577 unwrap<GlobalValue>(Global)->setUnnamedAddr(
1578 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1579 : GlobalValue::UnnamedAddr::None);
Tim Northoverad96d012014-03-10 19:24:35 +00001580}
1581
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001582/*--.. Operations on global variables, load and store instructions .........--*/
1583
1584unsigned LLVMGetAlignment(LLVMValueRef V) {
1585 Value *P = unwrap<Value>(V);
1586 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1587 return GV->getAlignment();
Peter Zotov9f584e62014-03-05 05:05:34 +00001588 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1589 return AI->getAlignment();
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001590 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1591 return LI->getAlignment();
1592 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1593 return SI->getAlignment();
1594
Peter Zotov9f584e62014-03-05 05:05:34 +00001595 llvm_unreachable(
1596 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001597}
1598
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001599void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1600 Value *P = unwrap<Value>(V);
Rafael Espindola99e05cf2014-05-13 18:45:48 +00001601 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001602 GV->setAlignment(Bytes);
Peter Zotov9f584e62014-03-05 05:05:34 +00001603 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1604 AI->setAlignment(Bytes);
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001605 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1606 LI->setAlignment(Bytes);
1607 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1608 SI->setAlignment(Bytes);
Anders Waldenborga36a7822013-10-29 09:37:28 +00001609 else
Peter Zotov9f584e62014-03-05 05:05:34 +00001610 llvm_unreachable(
1611 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001612}
1613
1614/*--.. Operations on global variables ......................................--*/
1615
1616LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
Owen Andersonb17f3292009-07-08 19:03:57 +00001617 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
Craig Topperc6207612014-04-09 06:08:46 +00001618 GlobalValue::ExternalLinkage, nullptr, Name));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001619}
1620
Erick Tryzelaar06894b32010-02-28 09:46:13 +00001621LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1622 const char *Name,
1623 unsigned AddressSpace) {
1624 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
Craig Topperc6207612014-04-09 06:08:46 +00001625 GlobalValue::ExternalLinkage, nullptr, Name,
1626 nullptr, GlobalVariable::NotThreadLocal,
1627 AddressSpace));
Erick Tryzelaar06894b32010-02-28 09:46:13 +00001628}
1629
Gordon Henriksen783f7bb2007-10-08 03:45:09 +00001630LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1631 return wrap(unwrap(M)->getNamedGlobal(Name));
1632}
1633
Gordon Henriksen054817c2008-03-19 03:47:18 +00001634LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1635 Module *Mod = unwrap(M);
1636 Module::global_iterator I = Mod->global_begin();
1637 if (I == Mod->global_end())
Craig Topperc6207612014-04-09 06:08:46 +00001638 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001639 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001640}
1641
1642LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1643 Module *Mod = unwrap(M);
1644 Module::global_iterator I = Mod->global_end();
1645 if (I == Mod->global_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001646 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001647 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001648}
1649
1650LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1651 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001652 Module::global_iterator I(GV);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001653 if (++I == GV->getParent()->global_end())
Craig Topperc6207612014-04-09 06:08:46 +00001654 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001655 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001656}
1657
1658LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1659 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001660 Module::global_iterator I(GV);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001661 if (I == GV->getParent()->global_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001662 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001663 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001664}
1665
Gordon Henriksen76a03742007-09-18 03:18:57 +00001666void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1667 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1668}
1669
Gordon Henriksen76a03742007-09-18 03:18:57 +00001670LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
Chris Lattner40cf28d2009-10-12 04:01:02 +00001671 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1672 if ( !GV->hasInitializer() )
Craig Topperc6207612014-04-09 06:08:46 +00001673 return nullptr;
Chris Lattner40cf28d2009-10-12 04:01:02 +00001674 return wrap(GV->getInitializer());
Gordon Henriksen76a03742007-09-18 03:18:57 +00001675}
1676
1677void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1678 unwrap<GlobalVariable>(GlobalVar)
1679 ->setInitializer(unwrap<Constant>(ConstantVal));
1680}
1681
Chris Lattner25963c62010-01-09 22:27:07 +00001682LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001683 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1684}
1685
Chris Lattner25963c62010-01-09 22:27:07 +00001686void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001687 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1688}
1689
Chris Lattner25963c62010-01-09 22:27:07 +00001690LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
Gordon Henriksen751ebf72007-10-07 17:31:42 +00001691 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1692}
1693
Chris Lattner25963c62010-01-09 22:27:07 +00001694void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
Gordon Henriksen751ebf72007-10-07 17:31:42 +00001695 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1696}
1697
Hans Wennborg5ff71202013-04-16 08:58:59 +00001698LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1699 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1700 case GlobalVariable::NotThreadLocal:
1701 return LLVMNotThreadLocal;
1702 case GlobalVariable::GeneralDynamicTLSModel:
1703 return LLVMGeneralDynamicTLSModel;
1704 case GlobalVariable::LocalDynamicTLSModel:
1705 return LLVMLocalDynamicTLSModel;
1706 case GlobalVariable::InitialExecTLSModel:
1707 return LLVMInitialExecTLSModel;
1708 case GlobalVariable::LocalExecTLSModel:
1709 return LLVMLocalExecTLSModel;
1710 }
1711
1712 llvm_unreachable("Invalid GlobalVariable thread local mode");
1713}
1714
1715void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1716 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1717
1718 switch (Mode) {
1719 case LLVMNotThreadLocal:
1720 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1721 break;
1722 case LLVMGeneralDynamicTLSModel:
1723 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1724 break;
1725 case LLVMLocalDynamicTLSModel:
1726 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1727 break;
1728 case LLVMInitialExecTLSModel:
1729 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1730 break;
1731 case LLVMLocalExecTLSModel:
1732 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1733 break;
1734 }
1735}
1736
1737LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1738 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1739}
1740
1741void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1742 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1743}
1744
Chris Lattner3d1f5522008-12-17 21:39:50 +00001745/*--.. Operations on aliases ......................................--*/
1746
1747LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1748 const char *Name) {
Rafael Espindola4fe00942014-05-16 13:34:04 +00001749 auto *PTy = cast<PointerType>(unwrap(Ty));
David Blaikie16a2f3e2015-09-14 18:01:59 +00001750 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1751 GlobalValue::ExternalLinkage, Name,
Rafael Espindola993502e2015-02-23 21:51:06 +00001752 unwrap<Constant>(Aliasee), unwrap(M)));
Chris Lattner3d1f5522008-12-17 21:39:50 +00001753}
1754
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001755/*--.. Operations on functions .............................................--*/
1756
1757LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1758 LLVMTypeRef FunctionTy) {
Gabor Greife9ecc682008-04-06 20:25:17 +00001759 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1760 GlobalValue::ExternalLinkage, Name, unwrap(M)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001761}
1762
Gordon Henriksen783f7bb2007-10-08 03:45:09 +00001763LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1764 return wrap(unwrap(M)->getFunction(Name));
1765}
1766
Gordon Henriksen054817c2008-03-19 03:47:18 +00001767LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1768 Module *Mod = unwrap(M);
1769 Module::iterator I = Mod->begin();
1770 if (I == Mod->end())
Craig Topperc6207612014-04-09 06:08:46 +00001771 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001772 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001773}
1774
1775LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1776 Module *Mod = unwrap(M);
1777 Module::iterator I = Mod->end();
1778 if (I == Mod->begin())
Craig Topperc6207612014-04-09 06:08:46 +00001779 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001780 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001781}
1782
1783LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1784 Function *Func = unwrap<Function>(Fn);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001785 Module::iterator I(Func);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001786 if (++I == Func->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00001787 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001788 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001789}
1790
1791LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1792 Function *Func = unwrap<Function>(Fn);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001793 Module::iterator I(Func);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001794 if (I == Func->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00001795 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001796 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001797}
1798
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001799void LLVMDeleteFunction(LLVMValueRef Fn) {
1800 unwrap<Function>(Fn)->eraseFromParent();
1801}
1802
Amaury Sechete39e8532016-02-18 20:38:32 +00001803LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
1804 return unwrap<Function>(Fn)->hasPersonalityFn();
1805}
1806
Andrew Wilkins3bdfc1c2015-07-14 01:23:06 +00001807LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
1808 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
1809}
1810
1811void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
1812 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
1813}
1814
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001815unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1816 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1817 return F->getIntrinsicID();
1818 return 0;
1819}
1820
1821unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1822 return unwrap<Function>(Fn)->getCallingConv();
1823}
1824
1825void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
Sandeep Patel68c5f472009-09-02 08:44:58 +00001826 return unwrap<Function>(Fn)->setCallingConv(
1827 static_cast<CallingConv::ID>(CC));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001828}
1829
Gordon Henriksend930f912008-08-17 18:44:35 +00001830const char *LLVMGetGC(LLVMValueRef Fn) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00001831 Function *F = unwrap<Function>(Fn);
Mehdi Amini599ebf22016-01-08 02:28:20 +00001832 return F->hasGC()? F->getGC().c_str() : nullptr;
Gordon Henriksen71183b62007-12-10 03:18:06 +00001833}
1834
Gordon Henriksend930f912008-08-17 18:44:35 +00001835void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00001836 Function *F = unwrap<Function>(Fn);
Gordon Henriksend930f912008-08-17 18:44:35 +00001837 if (GC)
1838 F->setGC(GC);
Gordon Henriksen71183b62007-12-10 03:18:06 +00001839 else
Gordon Henriksend930f912008-08-17 18:44:35 +00001840 F->clearGC();
Gordon Henriksen71183b62007-12-10 03:18:06 +00001841}
1842
Amaury Sechet5db224e2016-06-12 06:17:24 +00001843void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1844 LLVMAttributeRef A) {
1845 unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
1846}
1847
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001848unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001849 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
1850 return AS.getNumAttributes();
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001851}
1852
1853void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1854 LLVMAttributeRef *Attrs) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001855 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
1856 for (auto A : AS)
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001857 *Attrs++ = wrap(A);
1858}
1859
Amaury Sechet5db224e2016-06-12 06:17:24 +00001860LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
1861 LLVMAttributeIndex Idx,
1862 unsigned KindID) {
1863 return wrap(unwrap<Function>(F)->getAttribute(Idx,
1864 (Attribute::AttrKind)KindID));
1865}
1866
Amaury Sechet6100adf2016-06-15 17:50:39 +00001867LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
1868 LLVMAttributeIndex Idx,
1869 const char *K, unsigned KLen) {
1870 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
1871}
1872
Amaury Sechet5db224e2016-06-12 06:17:24 +00001873void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1874 unsigned KindID) {
1875 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
1876}
1877
Amaury Sechet6100adf2016-06-15 17:50:39 +00001878void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1879 const char *K, unsigned KLen) {
1880 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
1881}
1882
Tom Stellarde8f35e12013-04-16 23:12:43 +00001883void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1884 const char *V) {
1885 Function *Func = unwrap<Function>(Fn);
Reid Klecknerb5180542017-03-21 16:57:19 +00001886 AttributeList::AttrIndex Idx =
1887 AttributeList::AttrIndex(AttributeList::FunctionIndex);
Tom Stellarde8f35e12013-04-16 23:12:43 +00001888 AttrBuilder B;
1889
1890 B.addAttribute(A, V);
Reid Klecknerb5180542017-03-21 16:57:19 +00001891 AttributeList Set = AttributeList::get(Func->getContext(), Idx, B);
Tom Stellarde8f35e12013-04-16 23:12:43 +00001892 Func->addAttributes(Idx, Set);
1893}
1894
Gordon Henriksen265f7802008-03-19 01:11:35 +00001895/*--.. Operations on parameters ............................................--*/
1896
1897unsigned LLVMCountParams(LLVMValueRef FnRef) {
1898 // This function is strictly redundant to
1899 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
Dan Gohmanc7076912008-06-21 22:06:54 +00001900 return unwrap<Function>(FnRef)->arg_size();
Gordon Henriksen265f7802008-03-19 01:11:35 +00001901}
1902
1903void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1904 Function *Fn = unwrap<Function>(FnRef);
1905 for (Function::arg_iterator I = Fn->arg_begin(),
1906 E = Fn->arg_end(); I != E; I++)
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001907 *ParamRefs++ = wrap(&*I);
Gordon Henriksen265f7802008-03-19 01:11:35 +00001908}
1909
1910LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
Reid Kleckner56d028d2017-03-17 17:16:39 +00001911 Function *Fn = unwrap<Function>(FnRef);
1912 return wrap(&Fn->arg_begin()[index]);
Gordon Henriksen265f7802008-03-19 01:11:35 +00001913}
1914
1915LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1916 return wrap(unwrap<Argument>(V)->getParent());
1917}
1918
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001919LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1920 Function *Func = unwrap<Function>(Fn);
1921 Function::arg_iterator I = Func->arg_begin();
1922 if (I == Func->arg_end())
Craig Topperc6207612014-04-09 06:08:46 +00001923 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001924 return wrap(&*I);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001925}
1926
1927LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1928 Function *Func = unwrap<Function>(Fn);
1929 Function::arg_iterator I = Func->arg_end();
1930 if (I == Func->arg_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001931 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001932 return wrap(&*--I);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001933}
1934
1935LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1936 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner56d028d2017-03-17 17:16:39 +00001937 Function *Fn = A->getParent();
1938 if (A->getArgNo() + 1 >= Fn->arg_size())
Craig Topperc6207612014-04-09 06:08:46 +00001939 return nullptr;
Reid Kleckner56d028d2017-03-17 17:16:39 +00001940 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001941}
1942
1943LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1944 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner56d028d2017-03-17 17:16:39 +00001945 if (A->getArgNo() == 0)
Craig Topperc6207612014-04-09 06:08:46 +00001946 return nullptr;
Reid Kleckner56d028d2017-03-17 17:16:39 +00001947 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001948}
1949
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00001950void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
Bill Wendling49bc76c2013-01-23 06:14:59 +00001951 Argument *A = unwrap<Argument>(Arg);
Bill Wendling50d27842012-10-15 20:35:56 +00001952 AttrBuilder B;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001953 B.addAlignmentAttr(align);
Reid Klecknerb5180542017-03-21 16:57:19 +00001954 A->addAttr(AttributeList::get(A->getContext(), A->getArgNo() + 1, B));
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00001955}
1956
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001957/*--.. Operations on basic blocks ..........................................--*/
1958
Gordon Henriksen265f7802008-03-19 01:11:35 +00001959LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1960 return wrap(static_cast<Value*>(unwrap(BB)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001961}
1962
Chris Lattner25963c62010-01-09 22:27:07 +00001963LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001964 return isa<BasicBlock>(unwrap(Val));
1965}
1966
1967LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1968 return wrap(unwrap<BasicBlock>(Val));
1969}
1970
Amaury Secheta82042e2016-02-09 22:36:41 +00001971const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
1972 return unwrap(BB)->getName().data();
1973}
1974
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001975LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1976 return wrap(unwrap(BB)->getParent());
Gordon Henriksen265f7802008-03-19 01:11:35 +00001977}
1978
Nate Begeman43c322b2011-08-23 20:27:46 +00001979LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1980 return wrap(unwrap(BB)->getTerminator());
1981}
1982
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001983unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
Dan Gohmanc7076912008-06-21 22:06:54 +00001984 return unwrap<Function>(FnRef)->size();
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001985}
1986
1987void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1988 Function *Fn = unwrap<Function>(FnRef);
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00001989 for (BasicBlock &BB : *Fn)
1990 *BasicBlocksRefs++ = wrap(&BB);
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001991}
1992
1993LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1994 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1995}
1996
Gordon Henriksen054817c2008-03-19 03:47:18 +00001997LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1998 Function *Func = unwrap<Function>(Fn);
1999 Function::iterator I = Func->begin();
2000 if (I == Func->end())
Craig Topperc6207612014-04-09 06:08:46 +00002001 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002002 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002003}
2004
2005LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2006 Function *Func = unwrap<Function>(Fn);
2007 Function::iterator I = Func->end();
2008 if (I == Func->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002009 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002010 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002011}
2012
2013LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2014 BasicBlock *Block = unwrap(BB);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002015 Function::iterator I(Block);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002016 if (++I == Block->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00002017 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002018 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002019}
2020
2021LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2022 BasicBlock *Block = unwrap(BB);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002023 Function::iterator I(Block);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002024 if (I == Block->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002025 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002026 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002027}
2028
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002029LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2030 LLVMValueRef FnRef,
2031 const char *Name) {
2032 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002033}
2034
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002035LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2036 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2037}
2038
2039LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2040 LLVMBasicBlockRef BBRef,
2041 const char *Name) {
2042 BasicBlock *BB = unwrap(BBRef);
2043 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2044}
2045
2046LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002047 const char *Name) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002048 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002049}
2050
2051void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2052 unwrap(BBRef)->eraseFromParent();
2053}
2054
Nate Begeman43c322b2011-08-23 20:27:46 +00002055void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2056 unwrap(BBRef)->removeFromParent();
2057}
2058
Duncan Sandsb1d61aa2010-07-19 15:31:07 +00002059void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2060 unwrap(BB)->moveBefore(unwrap(MovePos));
2061}
2062
2063void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2064 unwrap(BB)->moveAfter(unwrap(MovePos));
2065}
2066
Gordon Henriksen265f7802008-03-19 01:11:35 +00002067/*--.. Operations on instructions ..........................................--*/
2068
2069LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2070 return wrap(unwrap<Instruction>(Inst)->getParent());
2071}
2072
Gordon Henriksen054817c2008-03-19 03:47:18 +00002073LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2074 BasicBlock *Block = unwrap(BB);
2075 BasicBlock::iterator I = Block->begin();
2076 if (I == Block->end())
Craig Topperc6207612014-04-09 06:08:46 +00002077 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002078 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002079}
2080
2081LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2082 BasicBlock *Block = unwrap(BB);
2083 BasicBlock::iterator I = Block->end();
2084 if (I == Block->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002085 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002086 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002087}
2088
2089LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2090 Instruction *Instr = unwrap<Instruction>(Inst);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002091 BasicBlock::iterator I(Instr);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002092 if (++I == Instr->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00002093 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002094 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002095}
2096
2097LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2098 Instruction *Instr = unwrap<Instruction>(Inst);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002099 BasicBlock::iterator I(Instr);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002100 if (I == Instr->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002101 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002102 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002103}
2104
Amaury Sechet2f432082016-02-11 21:37:54 +00002105void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2106 unwrap<Instruction>(Inst)->removeFromParent();
2107}
2108
Devang Pateldbebc6f2011-10-03 20:59:18 +00002109void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2110 unwrap<Instruction>(Inst)->eraseFromParent();
2111}
2112
Torok Edwin60c40de2011-10-06 12:13:20 +00002113LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
Torok Edwin2e9affe2011-10-14 20:37:42 +00002114 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2115 return (LLVMIntPredicate)I->getPredicate();
2116 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2117 if (CE->getOpcode() == Instruction::ICmp)
2118 return (LLVMIntPredicate)CE->getPredicate();
2119 return (LLVMIntPredicate)0;
Torok Edwin60c40de2011-10-06 12:13:20 +00002120}
2121
Peter Zotov1d98e6d2014-10-28 19:46:44 +00002122LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2123 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2124 return (LLVMRealPredicate)I->getPredicate();
2125 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2126 if (CE->getOpcode() == Instruction::FCmp)
2127 return (LLVMRealPredicate)CE->getPredicate();
2128 return (LLVMRealPredicate)0;
2129}
2130
Torok Edwinab6158e2011-10-14 20:37:49 +00002131LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2132 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2133 return map_to_llvmopcode(C->getOpcode());
2134 return (LLVMOpcode)0;
2135}
2136
Peter Zotovaff492c2014-10-17 01:02:34 +00002137LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2138 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2139 return wrap(C->clone());
2140 return nullptr;
2141}
2142
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002143/*--.. Call and invoke instructions ........................................--*/
2144
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002145unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
2146 return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands();
2147}
2148
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002149unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002150 return CallSite(unwrap<Instruction>(Instr)).getCallingConv();
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002151}
2152
2153void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002154 return CallSite(unwrap<Instruction>(Instr))
2155 .setCallingConv(static_cast<CallingConv::ID>(CC));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002156}
2157
Andrew Trickdc073ad2013-09-18 23:31:10 +00002158void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002159 unsigned align) {
2160 CallSite Call = CallSite(unwrap<Instruction>(Instr));
Bill Wendling50d27842012-10-15 20:35:56 +00002161 AttrBuilder B;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00002162 B.addAlignmentAttr(align);
Reid Klecknerb5180542017-03-21 16:57:19 +00002163 Call.setAttributes(Call.getAttributes().addAttributes(
2164 Call->getContext(), index,
2165 AttributeList::get(Call->getContext(), index, B)));
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002166}
2167
Amaury Secheta65a2372016-06-15 05:14:29 +00002168void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2169 LLVMAttributeRef A) {
2170 CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A));
2171}
2172
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002173unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2174 LLVMAttributeIndex Idx) {
2175 auto CS = CallSite(unwrap<Instruction>(C));
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002176 auto AS = CS.getAttributes().getAttributes(Idx);
2177 return AS.getNumAttributes();
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002178}
2179
2180void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2181 LLVMAttributeRef *Attrs) {
2182 auto CS = CallSite(unwrap<Instruction>(C));
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002183 auto AS = CS.getAttributes().getAttributes(Idx);
2184 for (auto A : AS)
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002185 *Attrs++ = wrap(A);
2186}
2187
Amaury Secheta65a2372016-06-15 05:14:29 +00002188LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
2189 LLVMAttributeIndex Idx,
2190 unsigned KindID) {
2191 return wrap(CallSite(unwrap<Instruction>(C))
2192 .getAttribute(Idx, (Attribute::AttrKind)KindID));
2193}
2194
Amaury Sechet6100adf2016-06-15 17:50:39 +00002195LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
2196 LLVMAttributeIndex Idx,
2197 const char *K, unsigned KLen) {
2198 return wrap(CallSite(unwrap<Instruction>(C))
2199 .getAttribute(Idx, StringRef(K, KLen)));
2200}
2201
Amaury Secheta65a2372016-06-15 05:14:29 +00002202void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2203 unsigned KindID) {
2204 CallSite(unwrap<Instruction>(C))
2205 .removeAttribute(Idx, (Attribute::AttrKind)KindID);
2206}
2207
Amaury Sechet6100adf2016-06-15 17:50:39 +00002208void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2209 const char *K, unsigned KLen) {
2210 CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen));
2211}
2212
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002213LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2214 return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue());
2215}
2216
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002217/*--.. Operations on call instructions (only) ..............................--*/
2218
Chris Lattner25963c62010-01-09 22:27:07 +00002219LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002220 return unwrap<CallInst>(Call)->isTailCall();
2221}
2222
Chris Lattner25963c62010-01-09 22:27:07 +00002223void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002224 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2225}
2226
Amaury Sechete39e8532016-02-18 20:38:32 +00002227/*--.. Operations on invoke instructions (only) ............................--*/
2228
2229LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2230 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2231}
2232
2233LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
2234 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2235}
2236
2237void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2238 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2239}
2240
2241void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2242 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2243}
2244
Peter Zotov2481c752014-10-28 19:46:56 +00002245/*--.. Operations on terminators ...........................................--*/
2246
2247unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2248 return unwrap<TerminatorInst>(Term)->getNumSuccessors();
2249}
2250
2251LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2252 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
2253}
2254
2255void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2256 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
2257}
2258
2259/*--.. Operations on branch instructions (only) ............................--*/
2260
2261LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2262 return unwrap<BranchInst>(Branch)->isConditional();
2263}
2264
2265LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2266 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2267}
2268
2269void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2270 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2271}
2272
Nate Begeman43c322b2011-08-23 20:27:46 +00002273/*--.. Operations on switch instructions (only) ............................--*/
2274
2275LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2276 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2277}
2278
Amaury Sechet1dcf5772016-02-09 22:50:53 +00002279/*--.. Operations on alloca instructions (only) ............................--*/
2280
2281LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
2282 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2283}
2284
Amaury Sechet053ac452016-02-17 22:51:03 +00002285/*--.. Operations on gep instructions (only) ...............................--*/
2286
2287LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
2288 return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2289}
2290
Amaury Sechet8a367d42016-05-01 02:23:14 +00002291void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
2292 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
Amaury Sechet053ac452016-02-17 22:51:03 +00002293}
2294
Gordon Henriksen44dd8fb2007-10-08 18:14:39 +00002295/*--.. Operations on phi nodes .............................................--*/
2296
2297void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2298 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2299 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2300 for (unsigned I = 0; I != Count; ++I)
2301 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2302}
2303
2304unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2305 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2306}
2307
2308LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2309 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2310}
2311
2312LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2313 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2314}
2315
Amaury Sechetaad93532016-02-10 00:38:50 +00002316/*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2317
2318unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
2319 auto *I = unwrap(Inst);
Amaury Sechet053ac452016-02-17 22:51:03 +00002320 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
2321 return GEP->getNumIndices();
Amaury Sechetaad93532016-02-10 00:38:50 +00002322 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2323 return EV->getNumIndices();
2324 if (auto *IV = dyn_cast<InsertValueInst>(I))
2325 return IV->getNumIndices();
2326 llvm_unreachable(
2327 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2328}
2329
2330const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2331 auto *I = unwrap(Inst);
2332 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2333 return EV->getIndices().data();
2334 if (auto *IV = dyn_cast<InsertValueInst>(I))
2335 return IV->getIndices().data();
2336 llvm_unreachable(
2337 "LLVMGetIndices applies only to extractvalue and insertvalue!");
2338}
2339
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002340
2341/*===-- Instruction builders ----------------------------------------------===*/
2342
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002343LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2344 return wrap(new IRBuilder<>(*unwrap(C)));
2345}
2346
Gordon Henriksena735a9c2008-05-04 12:55:34 +00002347LLVMBuilderRef LLVMCreateBuilder(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002348 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002349}
2350
Gordon Henriksen054817c2008-03-19 03:47:18 +00002351void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2352 LLVMValueRef Instr) {
2353 BasicBlock *BB = unwrap(Block);
Duncan P. N. Exon Smith43724642016-08-11 15:45:04 +00002354 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
2355 unwrap(Builder)->SetInsertPoint(BB, I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002356}
2357
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002358void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2359 Instruction *I = unwrap<Instruction>(Instr);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002360 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002361}
2362
2363void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2364 BasicBlock *BB = unwrap(Block);
2365 unwrap(Builder)->SetInsertPoint(BB);
2366}
2367
Gordon Henriksen265f7802008-03-19 01:11:35 +00002368LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2369 return wrap(unwrap(Builder)->GetInsertBlock());
2370}
2371
Chris Lattner3d1f5522008-12-17 21:39:50 +00002372void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
Chris Lattnere4bcde82010-04-01 06:31:45 +00002373 unwrap(Builder)->ClearInsertionPoint();
Chris Lattner3d1f5522008-12-17 21:39:50 +00002374}
2375
2376void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2377 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2378}
2379
Erick Tryzelaar9813bea2009-08-16 02:20:57 +00002380void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2381 const char *Name) {
2382 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2383}
2384
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002385void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2386 delete unwrap(Builder);
2387}
2388
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002389/*--.. Metadata builders ...................................................--*/
2390
2391void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002392 MDNode *Loc =
2393 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002394 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002395}
2396
2397LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002398 LLVMContext &Context = unwrap(Builder)->getContext();
2399 return wrap(MetadataAsValue::get(
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002400 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002401}
2402
2403void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2404 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2405}
2406
2407
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002408/*--.. Instruction builders ................................................--*/
2409
2410LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2411 return wrap(unwrap(B)->CreateRetVoid());
2412}
2413
2414LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2415 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2416}
2417
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002418LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2419 unsigned N) {
2420 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2421}
2422
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002423LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2424 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2425}
2426
2427LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2428 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2429 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2430}
2431
2432LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2433 LLVMBasicBlockRef Else, unsigned NumCases) {
2434 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2435}
2436
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00002437LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2438 unsigned NumDests) {
2439 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2440}
2441
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002442LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2443 LLVMValueRef *Args, unsigned NumArgs,
2444 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2445 const char *Name) {
2446 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002447 makeArrayRef(unwrap(Args), NumArgs),
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002448 Name));
2449}
2450
Bill Wendlingfae14752011-08-12 20:24:12 +00002451LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
Reid Kleckneref9828f2015-07-16 01:16:39 +00002452 LLVMValueRef PersFn, unsigned NumClauses,
2453 const char *Name) {
2454 // The personality used to live on the landingpad instruction, but now it
2455 // lives on the parent function. For compatibility, take the provided
2456 // personality and put it on the parent function.
2457 if (PersFn)
2458 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
2459 cast<Function>(unwrap(PersFn)));
David Majnemer7fddecc2015-06-17 20:52:32 +00002460 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
Bill Wendlingfae14752011-08-12 20:24:12 +00002461}
2462
Bill Wendlingf891bf82011-07-31 06:30:59 +00002463LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2464 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2465}
2466
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002467LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2468 return wrap(unwrap(B)->CreateUnreachable());
2469}
2470
Gordon Henriksen097102c2008-01-01 05:50:53 +00002471void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2472 LLVMBasicBlockRef Dest) {
2473 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2474}
2475
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00002476void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2477 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2478}
2479
Amaury Sechete39e8532016-02-18 20:38:32 +00002480unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
2481 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
2482}
2483
2484LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
2485 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
2486}
2487
Bill Wendlingfae14752011-08-12 20:24:12 +00002488void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2489 unwrap<LandingPadInst>(LandingPad)->
2490 addClause(cast<Constant>(unwrap(ClauseVal)));
2491}
2492
Amaury Sechete39e8532016-02-18 20:38:32 +00002493LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
2494 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
2495}
2496
Bill Wendlingfae14752011-08-12 20:24:12 +00002497void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2498 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2499}
2500
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002501/*--.. Arithmetic ..........................................................--*/
2502
2503LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2504 const char *Name) {
2505 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2506}
2507
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002508LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2509 const char *Name) {
2510 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2511}
2512
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002513LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2514 const char *Name) {
2515 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2516}
2517
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002518LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2519 const char *Name) {
2520 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2521}
2522
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002523LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2524 const char *Name) {
2525 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2526}
2527
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002528LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2529 const char *Name) {
2530 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2531}
2532
2533LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2534 const char *Name) {
2535 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2536}
2537
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002538LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2539 const char *Name) {
2540 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2541}
2542
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002543LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2544 const char *Name) {
2545 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2546}
2547
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002548LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2549 const char *Name) {
2550 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2551}
2552
2553LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2554 const char *Name) {
2555 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2556}
2557
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002558LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2559 const char *Name) {
2560 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2561}
2562
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002563LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2564 const char *Name) {
2565 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2566}
2567
Manuel Jacob49fafb12016-10-04 23:32:42 +00002568LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2569 LLVMValueRef RHS, const char *Name) {
2570 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
2571}
2572
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002573LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2574 const char *Name) {
2575 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2576}
2577
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002578LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2579 LLVMValueRef RHS, const char *Name) {
2580 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2581}
2582
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002583LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2584 const char *Name) {
2585 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2586}
2587
2588LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2589 const char *Name) {
2590 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2591}
2592
2593LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2594 const char *Name) {
2595 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2596}
2597
2598LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2599 const char *Name) {
2600 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2601}
2602
2603LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2604 const char *Name) {
2605 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2606}
2607
2608LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2609 const char *Name) {
2610 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2611}
2612
2613LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2614 const char *Name) {
2615 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2616}
2617
2618LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2619 const char *Name) {
2620 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2621}
2622
2623LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2624 const char *Name) {
2625 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2626}
2627
2628LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2629 const char *Name) {
2630 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2631}
2632
Erick Tryzelaar31831792010-02-28 05:51:27 +00002633LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2634 LLVMValueRef LHS, LLVMValueRef RHS,
2635 const char *Name) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00002636 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
Erick Tryzelaar31831792010-02-28 05:51:27 +00002637 unwrap(RHS), Name));
2638}
2639
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002640LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2641 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2642}
2643
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002644LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2645 const char *Name) {
2646 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2647}
2648
2649LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2650 const char *Name) {
2651 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2652}
2653
Dan Gohmanf919bd62009-09-28 21:51:41 +00002654LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2655 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2656}
2657
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002658LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2659 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2660}
2661
2662/*--.. Memory ..............................................................--*/
2663
2664LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2665 const char *Name) {
Chris Lattner229907c2011-07-18 04:54:35 +00002666 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
Victor Hernandezf3db9152009-11-07 00:16:28 +00002667 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2668 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
Andrew Trickdc073ad2013-09-18 23:31:10 +00002669 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2670 ITy, unwrap(Ty), AllocSize,
Craig Topperc6207612014-04-09 06:08:46 +00002671 nullptr, nullptr, "");
Victor Hernandezf3db9152009-11-07 00:16:28 +00002672 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002673}
2674
2675LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2676 LLVMValueRef Val, const char *Name) {
Chris Lattner229907c2011-07-18 04:54:35 +00002677 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
Victor Hernandezf3db9152009-11-07 00:16:28 +00002678 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2679 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
Andrew Trickdc073ad2013-09-18 23:31:10 +00002680 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2681 ITy, unwrap(Ty), AllocSize,
Craig Topperc6207612014-04-09 06:08:46 +00002682 unwrap(Val), nullptr, "");
Victor Hernandezf3db9152009-11-07 00:16:28 +00002683 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002684}
2685
2686LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2687 const char *Name) {
Craig Topperc6207612014-04-09 06:08:46 +00002688 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002689}
2690
2691LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2692 LLVMValueRef Val, const char *Name) {
2693 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2694}
2695
2696LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
Victor Hernandezde5ad422009-10-26 23:43:48 +00002697 return wrap(unwrap(B)->Insert(
2698 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002699}
2700
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002701LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2702 const char *Name) {
2703 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2704}
2705
Andrew Trickdc073ad2013-09-18 23:31:10 +00002706LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002707 LLVMValueRef PointerVal) {
2708 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2709}
2710
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002711static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2712 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +00002713 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
2714 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
2715 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
2716 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
2717 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
2718 case LLVMAtomicOrderingAcquireRelease:
2719 return AtomicOrdering::AcquireRelease;
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002720 case LLVMAtomicOrderingSequentiallyConsistent:
JF Bastien800f87a2016-04-06 21:19:33 +00002721 return AtomicOrdering::SequentiallyConsistent;
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002722 }
Peter Zotov1d98e6d2014-10-28 19:46:44 +00002723
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002724 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2725}
2726
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00002727static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
2728 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +00002729 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
2730 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
2731 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
2732 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
2733 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
2734 case AtomicOrdering::AcquireRelease:
2735 return LLVMAtomicOrderingAcquireRelease;
2736 case AtomicOrdering::SequentiallyConsistent:
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00002737 return LLVMAtomicOrderingSequentiallyConsistent;
2738 }
2739
2740 llvm_unreachable("Invalid AtomicOrdering value!");
2741}
2742
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002743LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2744 LLVMBool isSingleThread, const char *Name) {
2745 return wrap(
2746 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2747 isSingleThread ? SingleThread : CrossThread,
2748 Name));
2749}
2750
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002751LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2752 LLVMValueRef *Indices, unsigned NumIndices,
2753 const char *Name) {
Jay Foad040dd822011-07-22 08:16:57 +00002754 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
David Blaikie86ecb1b2015-03-15 01:03:19 +00002755 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002756}
2757
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002758LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2759 LLVMValueRef *Indices, unsigned NumIndices,
2760 const char *Name) {
Jay Foad040dd822011-07-22 08:16:57 +00002761 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
David Blaikieaa41cd52015-04-03 21:33:42 +00002762 return wrap(
2763 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002764}
2765
2766LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2767 unsigned Idx, const char *Name) {
David Blaikie64646022015-04-05 22:41:44 +00002768 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002769}
2770
2771LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2772 const char *Name) {
2773 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2774}
2775
2776LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2777 const char *Name) {
2778 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2779}
2780
Chris Lattner2cc6f9d2012-03-22 03:54:15 +00002781LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2782 Value *P = unwrap<Value>(MemAccessInst);
2783 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2784 return LI->isVolatile();
2785 return cast<StoreInst>(P)->isVolatile();
2786}
2787
2788void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2789 Value *P = unwrap<Value>(MemAccessInst);
2790 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2791 return LI->setVolatile(isVolatile);
2792 return cast<StoreInst>(P)->setVolatile(isVolatile);
2793}
2794
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00002795LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
2796 Value *P = unwrap<Value>(MemAccessInst);
2797 AtomicOrdering O;
2798 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2799 O = LI->getOrdering();
2800 else
2801 O = cast<StoreInst>(P)->getOrdering();
2802 return mapToLLVMOrdering(O);
2803}
2804
2805void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
2806 Value *P = unwrap<Value>(MemAccessInst);
2807 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
2808
2809 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2810 return LI->setOrdering(O);
2811 return cast<StoreInst>(P)->setOrdering(O);
2812}
2813
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002814/*--.. Casts ...............................................................--*/
2815
2816LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2817 LLVMTypeRef DestTy, const char *Name) {
2818 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2819}
2820
2821LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2822 LLVMTypeRef DestTy, const char *Name) {
2823 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2824}
2825
2826LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2827 LLVMTypeRef DestTy, const char *Name) {
2828 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2829}
2830
2831LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2832 LLVMTypeRef DestTy, const char *Name) {
2833 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2834}
2835
2836LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2837 LLVMTypeRef DestTy, const char *Name) {
2838 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2839}
2840
2841LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2842 LLVMTypeRef DestTy, const char *Name) {
2843 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2844}
2845
2846LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2847 LLVMTypeRef DestTy, const char *Name) {
2848 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2849}
2850
2851LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2852 LLVMTypeRef DestTy, const char *Name) {
2853 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2854}
2855
2856LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2857 LLVMTypeRef DestTy, const char *Name) {
2858 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2859}
2860
2861LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2862 LLVMTypeRef DestTy, const char *Name) {
2863 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2864}
2865
2866LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2867 LLVMTypeRef DestTy, const char *Name) {
2868 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2869}
2870
2871LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2872 LLVMTypeRef DestTy, const char *Name) {
2873 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2874}
2875
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002876LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2877 LLVMTypeRef DestTy, const char *Name) {
2878 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2879}
2880
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002881LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2882 LLVMTypeRef DestTy, const char *Name) {
2883 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2884 Name));
2885}
2886
2887LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2888 LLVMTypeRef DestTy, const char *Name) {
2889 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2890 Name));
2891}
2892
2893LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2894 LLVMTypeRef DestTy, const char *Name) {
2895 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2896 Name));
2897}
2898
Erick Tryzelaar31831792010-02-28 05:51:27 +00002899LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2900 LLVMTypeRef DestTy, const char *Name) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00002901 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
Erick Tryzelaar31831792010-02-28 05:51:27 +00002902 unwrap(DestTy), Name));
2903}
2904
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002905LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2906 LLVMTypeRef DestTy, const char *Name) {
2907 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2908}
2909
2910LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
Duncan Sands9d786d72009-11-23 10:49:03 +00002911 LLVMTypeRef DestTy, const char *Name) {
2912 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2913 /*isSigned*/true, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002914}
2915
2916LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2917 LLVMTypeRef DestTy, const char *Name) {
2918 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2919}
2920
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002921/*--.. Comparisons .........................................................--*/
2922
2923LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2924 LLVMValueRef LHS, LLVMValueRef RHS,
2925 const char *Name) {
2926 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2927 unwrap(LHS), unwrap(RHS), Name));
2928}
2929
2930LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2931 LLVMValueRef LHS, LLVMValueRef RHS,
2932 const char *Name) {
2933 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2934 unwrap(LHS), unwrap(RHS), Name));
2935}
2936
2937/*--.. Miscellaneous instructions ..........................................--*/
2938
2939LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
Jay Foad52131342011-03-30 11:28:46 +00002940 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002941}
2942
2943LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2944 LLVMValueRef *Args, unsigned NumArgs,
2945 const char *Name) {
Jay Foad5bd375a2011-07-15 08:37:34 +00002946 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002947 makeArrayRef(unwrap(Args), NumArgs),
Jay Foad5bd375a2011-07-15 08:37:34 +00002948 Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002949}
2950
2951LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2952 LLVMValueRef Then, LLVMValueRef Else,
2953 const char *Name) {
2954 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2955 Name));
2956}
2957
2958LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2959 LLVMTypeRef Ty, const char *Name) {
2960 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2961}
2962
2963LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2964 LLVMValueRef Index, const char *Name) {
2965 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2966 Name));
2967}
2968
2969LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2970 LLVMValueRef EltVal, LLVMValueRef Index,
2971 const char *Name) {
2972 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2973 unwrap(Index), Name));
2974}
2975
2976LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2977 LLVMValueRef V2, LLVMValueRef Mask,
2978 const char *Name) {
2979 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2980 unwrap(Mask), Name));
2981}
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00002982
Dan Gohmand5104a52008-11-03 22:55:43 +00002983LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2984 unsigned Index, const char *Name) {
2985 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2986}
2987
2988LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2989 LLVMValueRef EltVal, unsigned Index,
2990 const char *Name) {
2991 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2992 Index, Name));
2993}
2994
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002995LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2996 const char *Name) {
2997 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2998}
2999
3000LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
3001 const char *Name) {
3002 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3003}
3004
3005LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
3006 LLVMValueRef RHS, const char *Name) {
3007 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
3008}
3009
Andrew Trickdc073ad2013-09-18 23:31:10 +00003010LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
3011 LLVMValueRef PTR, LLVMValueRef Val,
3012 LLVMAtomicOrdering ordering,
Carlo Kok8c6719b2013-04-23 13:21:19 +00003013 LLVMBool singleThread) {
3014 AtomicRMWInst::BinOp intop;
3015 switch (op) {
3016 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
3017 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
3018 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
3019 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
3020 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
3021 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
3022 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
3023 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
3024 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
3025 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
3026 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
3027 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00003028 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00003029 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
Carlo Kok8c6719b2013-04-23 13:21:19 +00003030}
3031
Mehdi Amini43165d92016-03-19 21:28:28 +00003032LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
3033 LLVMValueRef Cmp, LLVMValueRef New,
3034 LLVMAtomicOrdering SuccessOrdering,
3035 LLVMAtomicOrdering FailureOrdering,
3036 LLVMBool singleThread) {
3037
3038 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
3039 unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
3040 mapFromLLVMOrdering(FailureOrdering),
3041 singleThread ? SingleThread : CrossThread));
3042}
3043
3044
3045LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
3046 Value *P = unwrap<Value>(AtomicInst);
3047
3048 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3049 return I->getSynchScope() == SingleThread;
3050 return cast<AtomicCmpXchgInst>(P)->getSynchScope() == SingleThread;
3051}
3052
3053void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
3054 Value *P = unwrap<Value>(AtomicInst);
3055 SynchronizationScope Sync = NewValue ? SingleThread : CrossThread;
3056
3057 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3058 return I->setSynchScope(Sync);
3059 return cast<AtomicCmpXchgInst>(P)->setSynchScope(Sync);
3060}
3061
3062LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
3063 Value *P = unwrap<Value>(CmpXchgInst);
3064 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
3065}
3066
3067void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
3068 LLVMAtomicOrdering Ordering) {
3069 Value *P = unwrap<Value>(CmpXchgInst);
3070 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3071
3072 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
3073}
3074
3075LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
3076 Value *P = unwrap<Value>(CmpXchgInst);
3077 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
3078}
3079
3080void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
3081 LLVMAtomicOrdering Ordering) {
3082 Value *P = unwrap<Value>(CmpXchgInst);
3083 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3084
3085 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
3086}
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003087
3088/*===-- Module providers --------------------------------------------------===*/
3089
3090LLVMModuleProviderRef
3091LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003092 return reinterpret_cast<LLVMModuleProviderRef>(M);
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003093}
3094
3095void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
3096 delete unwrap(MP);
3097}
3098
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003099
3100/*===-- Memory buffers ----------------------------------------------------===*/
3101
Chris Lattner25963c62010-01-09 22:27:07 +00003102LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
3103 const char *Path,
3104 LLVMMemoryBufferRef *OutMemBuf,
3105 char **OutMessage) {
3106
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003107 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
3108 if (std::error_code EC = MBOrErr.getError()) {
3109 *OutMessage = strdup(EC.message().c_str());
3110 return 1;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003111 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003112 *OutMemBuf = wrap(MBOrErr.get().release());
3113 return 0;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003114}
3115
Chris Lattner25963c62010-01-09 22:27:07 +00003116LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
3117 char **OutMessage) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003118 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
3119 if (std::error_code EC = MBOrErr.getError()) {
3120 *OutMessage = strdup(EC.message().c_str());
3121 return 1;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003122 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003123 *OutMemBuf = wrap(MBOrErr.get().release());
3124 return 0;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003125}
3126
Bill Wendling526276a2013-02-14 19:11:28 +00003127LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
3128 const char *InputData,
3129 size_t InputDataLength,
3130 const char *BufferName,
Bill Wendlingff61da92013-02-14 19:40:27 +00003131 LLVMBool RequiresNullTerminator) {
Bill Wendling526276a2013-02-14 19:11:28 +00003132
Rafael Espindola3560ff22014-08-27 20:03:13 +00003133 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
3134 StringRef(BufferName),
3135 RequiresNullTerminator).release());
Bill Wendling526276a2013-02-14 19:11:28 +00003136}
3137
3138LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
3139 const char *InputData,
3140 size_t InputDataLength,
3141 const char *BufferName) {
3142
Rafael Espindola3560ff22014-08-27 20:03:13 +00003143 return wrap(
3144 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
3145 StringRef(BufferName)).release());
Bill Wendling526276a2013-02-14 19:11:28 +00003146}
3147
Tom Stellard62c03202013-04-18 19:50:53 +00003148const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
Tom Stellard385fa262013-04-16 23:12:47 +00003149 return unwrap(MemBuf)->getBufferStart();
3150}
Bill Wendling526276a2013-02-14 19:11:28 +00003151
Tom Stellardb7fb7242013-04-16 23:12:51 +00003152size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
3153 return unwrap(MemBuf)->getBufferSize();
3154}
3155
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003156void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
3157 delete unwrap(MemBuf);
3158}
Dan Gohman093b42f2010-08-07 00:43:20 +00003159
Owen Anderson4698c5d2010-10-07 17:55:47 +00003160/*===-- Pass Registry -----------------------------------------------------===*/
3161
3162LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
3163 return wrap(PassRegistry::getPassRegistry());
3164}
Dan Gohman093b42f2010-08-07 00:43:20 +00003165
3166/*===-- Pass Manager ------------------------------------------------------===*/
3167
3168LLVMPassManagerRef LLVMCreatePassManager() {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003169 return wrap(new legacy::PassManager());
Dan Gohman093b42f2010-08-07 00:43:20 +00003170}
3171
3172LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003173 return wrap(new legacy::FunctionPassManager(unwrap(M)));
Dan Gohman093b42f2010-08-07 00:43:20 +00003174}
3175
3176LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
3177 return LLVMCreateFunctionPassManagerForModule(
3178 reinterpret_cast<LLVMModuleRef>(P));
3179}
3180
3181LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003182 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
Dan Gohman093b42f2010-08-07 00:43:20 +00003183}
3184
3185LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003186 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
Dan Gohman093b42f2010-08-07 00:43:20 +00003187}
3188
3189LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003190 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
Dan Gohman093b42f2010-08-07 00:43:20 +00003191}
3192
3193LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003194 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
Dan Gohman093b42f2010-08-07 00:43:20 +00003195}
3196
3197void LLVMDisposePassManager(LLVMPassManagerRef PM) {
3198 delete unwrap(PM);
3199}
Duncan Sands1cba0a82013-02-17 16:35:51 +00003200
3201/*===-- Threading ------------------------------------------------------===*/
3202
3203LLVMBool LLVMStartMultithreaded() {
Chandler Carruth39cd2162014-06-27 15:13:01 +00003204 return LLVMIsMultithreaded();
Duncan Sands1cba0a82013-02-17 16:35:51 +00003205}
3206
3207void LLVMStopMultithreaded() {
Duncan Sands1cba0a82013-02-17 16:35:51 +00003208}
3209
3210LLVMBool LLVMIsMultithreaded() {
3211 return llvm_is_multithreaded();
3212}