blob: 29a82ec6c5486c19912bfeb38fe70865d40e60ec [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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/Attributes.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000018#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000020#include "llvm/IR/DerivedTypes.h"
Tom Stellard1580dc72014-04-16 17:45:04 +000021#include "llvm/IR/DiagnosticInfo.h"
22#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/GlobalAlias.h"
24#include "llvm/IR/GlobalVariable.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000025#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/InlineAsm.h"
27#include "llvm/IR/IntrinsicInst.h"
28#include "llvm/IR/LLVMContext.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000029#include "llvm/IR/LegacyPassManager.h"
Filip Pizlodec20e42013-05-01 20:59:00 +000030#include "llvm/IR/Module.h"
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000031#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000032#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000033#include "llvm/Support/FileSystem.h"
Duncan Sands1cba0a82013-02-17 16:35:51 +000034#include "llvm/Support/ManagedStatic.h"
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000035#include "llvm/Support/MemoryBuffer.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000036#include "llvm/Support/Threading.h"
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000037#include "llvm/Support/raw_ostream.h"
Gordon Henriksen76a03742007-09-18 03:18:57 +000038#include <cassert>
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000039#include <cstdlib>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000040#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000041#include <system_error>
Gordon Henriksen76a03742007-09-18 03:18:57 +000042
43using namespace llvm;
44
Chandler Carruthe96dd892014-04-21 22:55:11 +000045#define DEBUG_TYPE "ir"
46
Owen Anderson44621e42010-10-07 19:51:21 +000047void llvm::initializeCore(PassRegistry &Registry) {
Chandler Carruth73523022014-01-13 13:07:17 +000048 initializeDominatorTreeWrapperPassPass(Registry);
Chandler Carruth52eef882014-01-12 12:15:39 +000049 initializePrintModulePassWrapperPass(Registry);
50 initializePrintFunctionPassWrapperPass(Registry);
Sergei Larincd1201b2013-02-08 23:37:41 +000051 initializePrintBasicBlockPassPass(Registry);
Anna Thomas740f5292017-07-05 01:16:29 +000052 initializeSafepointIRVerifierPass(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) {
Vivek Pandyab5ab8952017-09-15 20:10:09 +000088 unwrap(C)->setDiagnosticHandlerCallBack(
89 LLVM_EXTENSION reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy>(
Jeroen Ketemaad659c32016-04-08 09:19:02 +000090 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>(
Vivek Pandyab5ab8952017-09-15 20:10:09 +000096 unwrap(C)->getDiagnosticHandlerCallBack());
Jeroen Ketemaad659c32016-04-08 09:19:02 +000097}
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
Robert Widmann490a5802018-01-30 21:34:29 +0000237const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
238 auto &Str = unwrap(M)->getSourceFileName();
239 *Len = Str.length();
240 return Str.c_str();
241}
242
243void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
244 unwrap(M)->setSourceFileName(StringRef(Name, Len));
245}
Peter Zotov0a2fa0a2016-04-05 13:56:59 +0000246
Gordon Henriksen05568bb2007-12-27 20:13:47 +0000247/*--.. Data layout .........................................................--*/
Amaury Sechetf3549c42016-02-16 00:23:52 +0000248const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000249 return unwrap(M)->getDataLayoutStr().c_str();
Gordon Henriksen05568bb2007-12-27 20:13:47 +0000250}
251
Amaury Sechetf3549c42016-02-16 00:23:52 +0000252const char *LLVMGetDataLayout(LLVMModuleRef M) {
253 return LLVMGetDataLayoutStr(M);
254}
255
Amaury Sechet6ada31c2016-02-15 23:40:06 +0000256void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
257 unwrap(M)->setDataLayout(DataLayoutStr);
Gordon Henriksen05568bb2007-12-27 20:13:47 +0000258}
259
260/*--.. Target triple .......................................................--*/
261const char * LLVMGetTarget(LLVMModuleRef M) {
262 return unwrap(M)->getTargetTriple().c_str();
263}
264
265void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
266 unwrap(M)->setTargetTriple(Triple);
267}
268
Matthias Braunde58b612017-01-29 17:52:03 +0000269void LLVMDumpModule(LLVMModuleRef M) {
270 unwrap(M)->print(errs(), nullptr,
271 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
Gordon Henriksen6c6075e2008-03-14 23:58:56 +0000272}
273
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000274LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
275 char **ErrorMessage) {
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000276 std::error_code EC;
277 raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
278 if (EC) {
279 *ErrorMessage = strdup(EC.message().c_str());
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000280 return true;
281 }
282
Craig Topperc6207612014-04-09 06:08:46 +0000283 unwrap(M)->print(dest, nullptr);
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000284
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000285 dest.close();
286
287 if (dest.has_error()) {
Bob Haarman9ce2d032017-10-24 01:26:22 +0000288 std::string E = "Error printing to file: " + dest.error().message();
289 *ErrorMessage = strdup(E.c_str());
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000290 return true;
291 }
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000292
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000293 return false;
294}
295
Anders Waldenborg84355db2013-10-16 18:00:54 +0000296char *LLVMPrintModuleToString(LLVMModuleRef M) {
Alp Tokere69170a2014-06-26 22:52:05 +0000297 std::string buf;
298 raw_string_ostream os(buf);
299
Craig Topperc6207612014-04-09 06:08:46 +0000300 unwrap(M)->print(os, nullptr);
Alp Tokere69170a2014-06-26 22:52:05 +0000301 os.flush();
302
303 return strdup(buf.c_str());
Anders Waldenborg84355db2013-10-16 18:00:54 +0000304}
305
Chris Lattner26941452010-04-10 17:52:58 +0000306/*--.. Operations on inline assembler ......................................--*/
307void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
308 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
309}
310
Gordon Henriksen76a03742007-09-18 03:18:57 +0000311
Chris Lattnera7e04b02010-11-28 20:03:44 +0000312/*--.. Operations on module contexts ......................................--*/
313LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
314 return wrap(&unwrap(M)->getContext());
315}
316
317
Gordon Henriksen76a03742007-09-18 03:18:57 +0000318/*===-- Operations on types -----------------------------------------------===*/
319
320/*--.. Operations on all types (mostly) ....................................--*/
321
322LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000323 switch (unwrap(Ty)->getTypeID()) {
324 case Type::VoidTyID:
325 return LLVMVoidTypeKind;
Dan Gohman518cda42011-12-17 00:04:22 +0000326 case Type::HalfTyID:
327 return LLVMHalfTypeKind;
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000328 case Type::FloatTyID:
329 return LLVMFloatTypeKind;
330 case Type::DoubleTyID:
331 return LLVMDoubleTypeKind;
332 case Type::X86_FP80TyID:
333 return LLVMX86_FP80TypeKind;
334 case Type::FP128TyID:
335 return LLVMFP128TypeKind;
336 case Type::PPC_FP128TyID:
337 return LLVMPPC_FP128TypeKind;
338 case Type::LabelTyID:
339 return LLVMLabelTypeKind;
340 case Type::MetadataTyID:
341 return LLVMMetadataTypeKind;
342 case Type::IntegerTyID:
343 return LLVMIntegerTypeKind;
344 case Type::FunctionTyID:
345 return LLVMFunctionTypeKind;
346 case Type::StructTyID:
347 return LLVMStructTypeKind;
348 case Type::ArrayTyID:
349 return LLVMArrayTypeKind;
350 case Type::PointerTyID:
351 return LLVMPointerTypeKind;
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000352 case Type::VectorTyID:
353 return LLVMVectorTypeKind;
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000354 case Type::X86_MMXTyID:
355 return LLVMX86_MMXTypeKind;
David Majnemerb611e3f2015-08-14 05:09:07 +0000356 case Type::TokenTyID:
357 return LLVMTokenTypeKind;
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000358 }
Rafael Espindolaba7df702013-12-07 02:27:52 +0000359 llvm_unreachable("Unhandled TypeID.");
Gordon Henriksen76a03742007-09-18 03:18:57 +0000360}
361
Torok Edwin1cd9ade2011-10-06 12:13:28 +0000362LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
363{
364 return unwrap(Ty)->isSized();
365}
366
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000367LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
368 return wrap(&unwrap(Ty)->getContext());
369}
370
Aaron Ballman615eb472017-10-15 14:32:27 +0000371#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun8c209aa2017-01-28 02:02:38 +0000372LLVM_DUMP_METHOD void LLVMDumpType(LLVMTypeRef Ty) {
Anders Waldenborgb822cff2013-10-16 21:30:25 +0000373 return unwrap(Ty)->dump();
374}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000375#endif
Anders Waldenborgb822cff2013-10-16 21:30:25 +0000376
Anders Waldenborg47b3bd32013-10-22 06:58:34 +0000377char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
Alp Tokere69170a2014-06-26 22:52:05 +0000378 std::string buf;
379 raw_string_ostream os(buf);
Anders Waldenborg47b3bd32013-10-22 06:58:34 +0000380
Richard Trieuc1485222014-06-21 02:43:02 +0000381 if (unwrap(Ty))
382 unwrap(Ty)->print(os);
383 else
384 os << "Printing <null> Type";
385
Alp Tokere69170a2014-06-26 22:52:05 +0000386 os.flush();
387
388 return strdup(buf.c_str());
Anders Waldenborg47b3bd32013-10-22 06:58:34 +0000389}
390
Gordon Henriksen76a03742007-09-18 03:18:57 +0000391/*--.. Operations on integer types .........................................--*/
392
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000393LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
394 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000395}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000396LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
397 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000398}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000399LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
400 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000401}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000402LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
403 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000404}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000405LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
406 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
407}
Kit Barton72918022015-04-17 15:32:15 +0000408LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
409 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
410}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000411LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
412 return wrap(IntegerType::get(*unwrap(C), NumBits));
Owen Anderson55f1c092009-08-13 21:58:54 +0000413}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000414
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000415LLVMTypeRef LLVMInt1Type(void) {
416 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
417}
418LLVMTypeRef LLVMInt8Type(void) {
419 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
420}
421LLVMTypeRef LLVMInt16Type(void) {
422 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
423}
424LLVMTypeRef LLVMInt32Type(void) {
425 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
426}
427LLVMTypeRef LLVMInt64Type(void) {
428 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
429}
Kit Barton72918022015-04-17 15:32:15 +0000430LLVMTypeRef LLVMInt128Type(void) {
431 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
432}
Gordon Henriksened7beaa2007-10-06 16:05:20 +0000433LLVMTypeRef LLVMIntType(unsigned NumBits) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000434 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
Gordon Henriksen76a03742007-09-18 03:18:57 +0000435}
436
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000437unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000438 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
439}
440
441/*--.. Operations on real types ............................................--*/
442
Dan Gohman518cda42011-12-17 00:04:22 +0000443LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
444 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
445}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000446LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
447 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
448}
449LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
450 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
451}
452LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
453 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
454}
455LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
456 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
457}
458LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
459 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
460}
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000461LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
462 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
463}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000464
Dan Gohman518cda42011-12-17 00:04:22 +0000465LLVMTypeRef LLVMHalfType(void) {
466 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
467}
Owen Anderson55f1c092009-08-13 21:58:54 +0000468LLVMTypeRef LLVMFloatType(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000469 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000470}
471LLVMTypeRef LLVMDoubleType(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000472 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000473}
474LLVMTypeRef LLVMX86FP80Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000475 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000476}
477LLVMTypeRef LLVMFP128Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000478 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000479}
480LLVMTypeRef LLVMPPCFP128Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000481 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000482}
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000483LLVMTypeRef LLVMX86MMXType(void) {
484 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
485}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000486
487/*--.. Operations on function types ........................................--*/
488
Gordon Henriksened7beaa2007-10-06 16:05:20 +0000489LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
490 LLVMTypeRef *ParamTypes, unsigned ParamCount,
Chris Lattner25963c62010-01-09 22:27:07 +0000491 LLVMBool IsVarArg) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000492 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
Owen Anderson4056ca92009-07-29 22:17:13 +0000493 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000494}
495
Chris Lattner25963c62010-01-09 22:27:07 +0000496LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000497 return unwrap<FunctionType>(FunctionTy)->isVarArg();
498}
499
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000500LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000501 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
502}
503
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000504unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000505 return unwrap<FunctionType>(FunctionTy)->getNumParams();
506}
507
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000508void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000509 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
510 for (FunctionType::param_iterator I = Ty->param_begin(),
511 E = Ty->param_end(); I != E; ++I)
512 *Dest++ = wrap(*I);
513}
514
515/*--.. Operations on struct types ..........................................--*/
516
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000517LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
Chris Lattner25963c62010-01-09 22:27:07 +0000518 unsigned ElementCount, LLVMBool Packed) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000519 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000520 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000521}
522
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000523LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
Chris Lattner25963c62010-01-09 22:27:07 +0000524 unsigned ElementCount, LLVMBool Packed) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000525 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
526 ElementCount, Packed);
527}
528
Chris Lattnere71ccde2011-07-14 05:53:17 +0000529LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
530{
Chris Lattner01beceb2011-08-12 18:07:07 +0000531 return wrap(StructType::create(*unwrap(C), Name));
Chris Lattnere71ccde2011-07-14 05:53:17 +0000532}
533
Torok Edwin0d5f6ae2011-10-06 12:12:50 +0000534const char *LLVMGetStructName(LLVMTypeRef Ty)
535{
Torok Edwin2e9affe2011-10-14 20:37:42 +0000536 StructType *Type = unwrap<StructType>(Ty);
537 if (!Type->hasName())
Craig Topperc6207612014-04-09 06:08:46 +0000538 return nullptr;
Torok Edwin2e9affe2011-10-14 20:37:42 +0000539 return Type->getName().data();
Torok Edwin0d5f6ae2011-10-06 12:12:50 +0000540}
541
Chris Lattnere71ccde2011-07-14 05:53:17 +0000542void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
543 unsigned ElementCount, LLVMBool Packed) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000544 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
Chris Lattnere71ccde2011-07-14 05:53:17 +0000545 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
546}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000547
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000548unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000549 return unwrap<StructType>(StructTy)->getNumElements();
550}
551
552void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
553 StructType *Ty = unwrap<StructType>(StructTy);
Nick Lewyckyf735b7b2011-04-20 22:52:37 +0000554 for (StructType::element_iterator I = Ty->element_begin(),
Gordon Henriksen76a03742007-09-18 03:18:57 +0000555 E = Ty->element_end(); I != E; ++I)
556 *Dest++ = wrap(*I);
557}
558
Peter Zotovc164a3f2015-06-04 09:09:53 +0000559LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
560 StructType *Ty = unwrap<StructType>(StructTy);
561 return wrap(Ty->getTypeAtIndex(i));
562}
563
Chris Lattner25963c62010-01-09 22:27:07 +0000564LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000565 return unwrap<StructType>(StructTy)->isPacked();
566}
567
Chris Lattner17cf05b2011-07-14 16:20:28 +0000568LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
569 return unwrap<StructType>(StructTy)->isOpaque();
570}
571
572LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
573 return wrap(unwrap(M)->getTypeByName(Name));
574}
575
Gordon Henriksen76a03742007-09-18 03:18:57 +0000576/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
577
whitequarkf6059fd2017-06-05 11:49:52 +0000578void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
579 int i = 0;
580 for (auto *T : unwrap(Tp)->subtypes()) {
581 Arr[i] = wrap(T);
582 i++;
583 }
584}
585
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000586LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000587 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000588}
589
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000590LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000591 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000592}
593
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000594LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000595 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000596}
597
Peter Collingbourne45681582016-12-02 03:05:41 +0000598LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
599 auto *Ty = unwrap<Type>(WrappedTy);
600 if (auto *PTy = dyn_cast<PointerType>(Ty))
601 return wrap(PTy->getElementType());
602 return wrap(cast<SequentialType>(Ty)->getElementType());
Gordon Henriksen76a03742007-09-18 03:18:57 +0000603}
604
whitequarkf6059fd2017-06-05 11:49:52 +0000605unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
606 return unwrap(Tp)->getNumContainedTypes();
607}
608
Gordon Henriksen76a03742007-09-18 03:18:57 +0000609unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
610 return unwrap<ArrayType>(ArrayTy)->getNumElements();
611}
612
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000613unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
614 return unwrap<PointerType>(PointerTy)->getAddressSpace();
615}
616
Gordon Henriksen76a03742007-09-18 03:18:57 +0000617unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
618 return unwrap<VectorType>(VectorTy)->getNumElements();
619}
620
621/*--.. Operations on other types ...........................................--*/
622
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000623LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
624 return wrap(Type::getVoidTy(*unwrap(C)));
Owen Anderson55f1c092009-08-13 21:58:54 +0000625}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000626LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
627 return wrap(Type::getLabelTy(*unwrap(C)));
628}
whitequark131f98f2017-10-27 11:51:40 +0000629LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
630 return wrap(Type::getTokenTy(*unwrap(C)));
631}
632LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) {
633 return wrap(Type::getMetadataTy(*unwrap(C)));
634}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000635
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000636LLVMTypeRef LLVMVoidType(void) {
637 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
638}
639LLVMTypeRef LLVMLabelType(void) {
640 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
641}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000642
643/*===-- Operations on values ----------------------------------------------===*/
644
645/*--.. Operations on all values ............................................--*/
646
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000647LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000648 return wrap(unwrap(Val)->getType());
649}
650
Peter Zotov3e4561c2016-04-06 22:21:29 +0000651LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
652 switch(unwrap(Val)->getValueID()) {
653#define HANDLE_VALUE(Name) \
654 case Value::Name##Val: \
655 return LLVM##Name##ValueKind;
656#include "llvm/IR/Value.def"
657 default:
658 return LLVMInstructionValueKind;
659 }
660}
661
Gordon Henriksen76a03742007-09-18 03:18:57 +0000662const char *LLVMGetValueName(LLVMValueRef Val) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000663 return unwrap(Val)->getName().data();
Gordon Henriksen76a03742007-09-18 03:18:57 +0000664}
665
666void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
667 unwrap(Val)->setName(Name);
668}
669
Matthias Braun8c209aa2017-01-28 02:02:38 +0000670LLVM_DUMP_METHOD void LLVMDumpValue(LLVMValueRef Val) {
Sam McCalla682dfb2017-01-30 05:40:52 +0000671 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
Gordon Henriksen1d0d24c2007-10-06 00:08:49 +0000672}
673
Peter Zotovcd93b372013-11-06 09:21:01 +0000674char* LLVMPrintValueToString(LLVMValueRef Val) {
Alp Tokere69170a2014-06-26 22:52:05 +0000675 std::string buf;
676 raw_string_ostream os(buf);
Peter Zotovcd93b372013-11-06 09:21:01 +0000677
Richard Trieuc1485222014-06-21 02:43:02 +0000678 if (unwrap(Val))
679 unwrap(Val)->print(os);
680 else
681 os << "Printing <null> Value";
682
Alp Tokere69170a2014-06-26 22:52:05 +0000683 os.flush();
684
685 return strdup(buf.c_str());
Peter Zotovcd93b372013-11-06 09:21:01 +0000686}
687
Chris Lattner40cf28d2009-10-12 04:01:02 +0000688void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
689 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
690}
Gordon Henriksen29e38942008-12-19 18:39:45 +0000691
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000692int LLVMHasMetadata(LLVMValueRef Inst) {
693 return unwrap<Instruction>(Inst)->hasMetadata();
694}
695
696LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000697 auto *I = unwrap<Instruction>(Inst);
698 assert(I && "Expected instruction");
699 if (auto *MD = I->getMetadata(KindID))
700 return wrap(MetadataAsValue::get(I->getContext(), MD));
701 return nullptr;
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000702}
703
Bjorn Steinbrinka09ac002015-01-28 16:35:59 +0000704// MetadataAsValue uses a canonical format which strips the actual MDNode for
705// MDNode with just a single constant value, storing just a ConstantAsMetadata
706// This undoes this canonicalization, reconstructing the MDNode.
707static MDNode *extractMDNode(MetadataAsValue *MAV) {
708 Metadata *MD = MAV->getMetadata();
709 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
710 "Expected a metadata node or a canonicalized constant");
711
712 if (MDNode *N = dyn_cast<MDNode>(MD))
713 return N;
714
715 return MDNode::get(MAV->getContext(), MD);
716}
717
718void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
719 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
720
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000721 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000722}
723
Gordon Henriksen29e38942008-12-19 18:39:45 +0000724/*--.. Conversion functions ................................................--*/
725
726#define LLVM_DEFINE_VALUE_CAST(name) \
727 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
728 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
729 }
730
731LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
732
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000733LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
734 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
735 if (isa<MDNode>(MD->getMetadata()) ||
736 isa<ValueAsMetadata>(MD->getMetadata()))
737 return Val;
738 return nullptr;
739}
740
741LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
742 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
743 if (isa<MDString>(MD->getMetadata()))
744 return Val;
745 return nullptr;
746}
747
Chris Lattner40cf28d2009-10-12 04:01:02 +0000748/*--.. Operations on Uses ..................................................--*/
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000749LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
Chris Lattner40cf28d2009-10-12 04:01:02 +0000750 Value *V = unwrap(Val);
751 Value::use_iterator I = V->use_begin();
752 if (I == V->use_end())
Craig Topperc6207612014-04-09 06:08:46 +0000753 return nullptr;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000754 return wrap(&*I);
Chris Lattner40cf28d2009-10-12 04:01:02 +0000755}
756
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000757LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
758 Use *Next = unwrap(U)->getNext();
759 if (Next)
760 return wrap(Next);
Craig Topperc6207612014-04-09 06:08:46 +0000761 return nullptr;
Chris Lattner40cf28d2009-10-12 04:01:02 +0000762}
763
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000764LLVMValueRef LLVMGetUser(LLVMUseRef U) {
765 return wrap(unwrap(U)->getUser());
Chris Lattner40cf28d2009-10-12 04:01:02 +0000766}
767
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000768LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
769 return wrap(unwrap(U)->get());
Chris Lattner40cf28d2009-10-12 04:01:02 +0000770}
771
772/*--.. Operations on Users .................................................--*/
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000773
774static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
775 unsigned Index) {
776 Metadata *Op = N->getOperand(Index);
777 if (!Op)
778 return nullptr;
779 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
780 return wrap(C->getValue());
781 return wrap(MetadataAsValue::get(Context, Op));
782}
783
Chris Lattner40cf28d2009-10-12 04:01:02 +0000784LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
Torok Edwinfec812e2011-10-06 12:13:11 +0000785 Value *V = unwrap(Val);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000786 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
787 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
788 assert(Index == 0 && "Function-local metadata can only have one operand");
789 return wrap(L->getValue());
790 }
791 return getMDNodeOperandImpl(V->getContext(),
792 cast<MDNode>(MD->getMetadata()), Index);
793 }
794
Torok Edwinfec812e2011-10-06 12:13:11 +0000795 return wrap(cast<User>(V)->getOperand(Index));
Chris Lattner40cf28d2009-10-12 04:01:02 +0000796}
Gordon Henriksen29e38942008-12-19 18:39:45 +0000797
Peter Zotovb19f78f2014-08-12 02:55:40 +0000798LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
799 Value *V = unwrap(Val);
800 return wrap(&cast<User>(V)->getOperandUse(Index));
801}
802
Erick Tryzelaarb4d48702010-08-20 14:51:22 +0000803void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
804 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
805}
806
807int LLVMGetNumOperands(LLVMValueRef Val) {
Torok Edwinfec812e2011-10-06 12:13:11 +0000808 Value *V = unwrap(Val);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000809 if (isa<MetadataAsValue>(V))
810 return LLVMGetMDNodeNumOperands(Val);
811
Torok Edwinfec812e2011-10-06 12:13:11 +0000812 return cast<User>(V)->getNumOperands();
Erick Tryzelaarb4d48702010-08-20 14:51:22 +0000813}
814
Gordon Henriksen76a03742007-09-18 03:18:57 +0000815/*--.. Operations on constants of any type .................................--*/
816
Gordon Henriksen1046c732007-10-06 15:11:06 +0000817LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000818 return wrap(Constant::getNullValue(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000819}
820
Gordon Henriksen1046c732007-10-06 15:11:06 +0000821LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000822 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000823}
824
825LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
Owen Andersonb292b8c2009-07-30 23:03:37 +0000826 return wrap(UndefValue::get(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000827}
828
Chris Lattner25963c62010-01-09 22:27:07 +0000829LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
Gordon Henriksendc88c062007-09-18 18:07:51 +0000830 return isa<Constant>(unwrap(Ty));
831}
832
Chris Lattner25963c62010-01-09 22:27:07 +0000833LLVMBool LLVMIsNull(LLVMValueRef Val) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000834 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
835 return C->isNullValue();
836 return false;
837}
838
Chris Lattner25963c62010-01-09 22:27:07 +0000839LLVMBool LLVMIsUndef(LLVMValueRef Val) {
Gordon Henriksendc88c062007-09-18 18:07:51 +0000840 return isa<UndefValue>(unwrap(Val));
841}
842
Chris Lattner7f318242009-07-06 17:29:59 +0000843LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
Amaury Sechet9bbda192016-04-25 22:23:35 +0000844 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
Chris Lattner7f318242009-07-06 17:29:59 +0000845}
846
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000847/*--.. Operations on metadata nodes ........................................--*/
848
849LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
850 unsigned SLen) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000851 LLVMContext &Context = *unwrap(C);
852 return wrap(MetadataAsValue::get(
853 Context, MDString::get(Context, StringRef(Str, SLen))));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000854}
855
856LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
857 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
858}
859
860LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
861 unsigned Count) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000862 LLVMContext &Context = *unwrap(C);
863 SmallVector<Metadata *, 8> MDs;
864 for (auto *OV : makeArrayRef(Vals, Count)) {
865 Value *V = unwrap(OV);
866 Metadata *MD;
867 if (!V)
868 MD = nullptr;
869 else if (auto *C = dyn_cast<Constant>(V))
870 MD = ConstantAsMetadata::get(C);
871 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
872 MD = MDV->getMetadata();
873 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
874 "outside of direct argument to call");
875 } else {
876 // This is function-local metadata. Pretend to make an MDNode.
877 assert(Count == 1 &&
878 "Expected only one operand to function-local metadata");
879 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
880 }
881
882 MDs.push_back(MD);
883 }
884 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000885}
886
887LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
888 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
889}
890
Amaury Sechetf8429752017-04-17 11:52:54 +0000891LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
892 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
893}
894
895LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {
896 auto *V = unwrap(Val);
897 if (auto *C = dyn_cast<Constant>(V))
898 return wrap(ConstantAsMetadata::get(C));
899 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
900 return wrap(MAV->getMetadata());
901 return wrap(ValueAsMetadata::get(V));
902}
903
Amaury Sechet7c2883c2016-04-03 21:06:04 +0000904const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000905 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
906 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
Amaury Sechet7c2883c2016-04-03 21:06:04 +0000907 *Length = S->getString().size();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000908 return S->getString().data();
909 }
Amaury Sechet7c2883c2016-04-03 21:06:04 +0000910 *Length = 0;
Craig Topperc6207612014-04-09 06:08:46 +0000911 return nullptr;
Torok Edwinfec812e2011-10-06 12:13:11 +0000912}
913
Amaury Sechetb130f432016-04-23 00:12:45 +0000914unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000915 auto *MD = cast<MetadataAsValue>(unwrap(V));
916 if (isa<ValueAsMetadata>(MD->getMetadata()))
917 return 1;
918 return cast<MDNode>(MD->getMetadata())->getNumOperands();
Duncan Sands12ccbe72012-09-19 20:29:39 +0000919}
920
Amaury Sechetb130f432016-04-23 00:12:45 +0000921void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000922 auto *MD = cast<MetadataAsValue>(unwrap(V));
923 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
924 *Dest = wrap(MDV->getValue());
925 return;
926 }
927 const auto *N = cast<MDNode>(MD->getMetadata());
Duncan Sands12ccbe72012-09-19 20:29:39 +0000928 const unsigned numOperands = N->getNumOperands();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000929 LLVMContext &Context = unwrap(V)->getContext();
Duncan Sands12ccbe72012-09-19 20:29:39 +0000930 for (unsigned i = 0; i < numOperands; i++)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000931 Dest[i] = getMDNodeOperandImpl(Context, N, i);
Duncan Sands12ccbe72012-09-19 20:29:39 +0000932}
933
Amaury Sechetb130f432016-04-23 00:12:45 +0000934unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
935 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
Torok Edwin2e9affe2011-10-14 20:37:42 +0000936 return N->getNumOperands();
937 }
938 return 0;
Torok Edwinfec812e2011-10-06 12:13:11 +0000939}
940
Amaury Sechetb130f432016-04-23 00:12:45 +0000941void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
942 LLVMValueRef *Dest) {
943 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
Torok Edwin2e9affe2011-10-14 20:37:42 +0000944 if (!N)
945 return;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000946 LLVMContext &Context = unwrap(M)->getContext();
Torok Edwin2e9affe2011-10-14 20:37:42 +0000947 for (unsigned i=0;i<N->getNumOperands();i++)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000948 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
Torok Edwinfec812e2011-10-06 12:13:11 +0000949}
950
Amaury Sechetb130f432016-04-23 00:12:45 +0000951void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
952 LLVMValueRef Val) {
953 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
Devang Patel92245402011-12-20 19:29:36 +0000954 if (!N)
955 return;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000956 if (!Val)
957 return;
Bjorn Steinbrinka09ac002015-01-28 16:35:59 +0000958 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
Devang Patel92245402011-12-20 19:29:36 +0000959}
960
Gordon Henriksen76a03742007-09-18 03:18:57 +0000961/*--.. Operations on scalar constants ......................................--*/
962
Gordon Henriksen1046c732007-10-06 15:11:06 +0000963LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
Chris Lattner25963c62010-01-09 22:27:07 +0000964 LLVMBool SignExtend) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000965 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000966}
967
Chris Lattner4329e072010-11-23 02:47:22 +0000968LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
969 unsigned NumWords,
970 const uint64_t Words[]) {
971 IntegerType *Ty = unwrap<IntegerType>(IntTy);
972 return wrap(ConstantInt::get(Ty->getContext(),
Jeffrey Yasskin7a162882011-07-18 21:45:40 +0000973 APInt(Ty->getBitWidth(),
974 makeArrayRef(Words, NumWords))));
Chris Lattner4329e072010-11-23 02:47:22 +0000975}
976
Erick Tryzelaardd991352009-08-16 23:36:46 +0000977LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
978 uint8_t Radix) {
979 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
980 Radix));
981}
982
983LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
984 unsigned SLen, uint8_t Radix) {
985 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
986 Radix));
Gordon Henriksen931e1212008-02-02 01:07:50 +0000987}
988
Gordon Henriksen1046c732007-10-06 15:11:06 +0000989LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
Erick Tryzelaardd991352009-08-16 23:36:46 +0000990 return wrap(ConstantFP::get(unwrap(RealTy), N));
Gordon Henriksen931e1212008-02-02 01:07:50 +0000991}
992
993LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
Erick Tryzelaardd991352009-08-16 23:36:46 +0000994 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
995}
996
997LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
998 unsigned SLen) {
999 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001000}
1001
Chris Lattner40cf28d2009-10-12 04:01:02 +00001002unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1003 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1004}
1005
1006long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1007 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1008}
1009
Peter Zotov1d98e6d2014-10-28 19:46:44 +00001010double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1011 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1012 Type *Ty = cFP->getType();
1013
1014 if (Ty->isFloatTy()) {
1015 *LosesInfo = false;
1016 return cFP->getValueAPF().convertToFloat();
1017 }
1018
1019 if (Ty->isDoubleTy()) {
1020 *LosesInfo = false;
1021 return cFP->getValueAPF().convertToDouble();
1022 }
1023
1024 bool APFLosesInfo;
1025 APFloat APF = cFP->getValueAPF();
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001026 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
Peter Zotov1d98e6d2014-10-28 19:46:44 +00001027 *LosesInfo = APFLosesInfo;
1028 return APF.convertToDouble();
1029}
1030
Gordon Henriksen76a03742007-09-18 03:18:57 +00001031/*--.. Operations on composite constants ...................................--*/
1032
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001033LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
Chris Lattner25963c62010-01-09 22:27:07 +00001034 unsigned Length,
1035 LLVMBool DontNullTerminate) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001036 /* Inverted the sense of AddNull because ', 0)' is a
1037 better mnemonic for null termination than ', 1)'. */
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001038 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1039 DontNullTerminate == 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001040}
Amaury Sechet006ce632016-03-13 00:54:40 +00001041
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001042LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
Chris Lattner25963c62010-01-09 22:27:07 +00001043 LLVMBool DontNullTerminate) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001044 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1045 DontNullTerminate);
1046}
Peter Zotovf9aa8822014-08-03 23:54:16 +00001047
Amaury Sechet006ce632016-03-13 00:54:40 +00001048LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1049 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
Peter Zotovf9aa8822014-08-03 23:54:16 +00001050}
1051
Amaury Sechet006ce632016-03-13 00:54:40 +00001052LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1053 return unwrap<ConstantDataSequential>(C)->isString();
Peter Zotovf9aa8822014-08-03 23:54:16 +00001054}
1055
Amaury Sechet7c2883c2016-04-03 21:06:04 +00001056const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1057 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1058 *Length = Str.size();
1059 return Str.data();
Peter Zotovf9aa8822014-08-03 23:54:16 +00001060}
1061
Gordon Henriksen1046c732007-10-06 15:11:06 +00001062LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1063 LLVMValueRef *ConstantVals, unsigned Length) {
Jay Foad83be3612011-06-22 09:24:39 +00001064 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1065 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001066}
Peter Zotovf9aa8822014-08-03 23:54:16 +00001067
Amaury Sechetc78768f2016-03-13 00:40:12 +00001068LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1069 LLVMValueRef *ConstantVals,
1070 unsigned Count, LLVMBool Packed) {
1071 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1072 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1073 Packed != 0));
1074}
1075
Gordon Henriksen1046c732007-10-06 15:11:06 +00001076LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
Chris Lattner25963c62010-01-09 22:27:07 +00001077 LLVMBool Packed) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001078 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1079 Packed);
Gordon Henriksen76a03742007-09-18 03:18:57 +00001080}
Rafael Espindola784ad242011-07-14 19:09:08 +00001081
1082LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1083 LLVMValueRef *ConstantVals,
1084 unsigned Count) {
1085 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
Chris Lattner229907c2011-07-18 04:54:35 +00001086 StructType *Ty = cast<StructType>(unwrap(StructTy));
Rafael Espindola784ad242011-07-14 19:09:08 +00001087
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001088 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
Rafael Espindola784ad242011-07-14 19:09:08 +00001089}
1090
Gordon Henriksen1046c732007-10-06 15:11:06 +00001091LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001092 return wrap(ConstantVector::get(makeArrayRef(
Chris Lattner69229312011-02-15 00:14:00 +00001093 unwrap<Constant>(ScalarConstantVals, Size), Size)));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001094}
Torok Edwin05dc9d62011-10-06 12:39:34 +00001095
1096/*-- Opcode mapping */
1097
1098static LLVMOpcode map_to_llvmopcode(int opcode)
1099{
1100 switch (opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00001101 default: llvm_unreachable("Unhandled Opcode.");
Torok Edwin05dc9d62011-10-06 12:39:34 +00001102#define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
Chandler Carruth9fb823b2013-01-02 11:36:10 +00001103#include "llvm/IR/Instruction.def"
Torok Edwin05dc9d62011-10-06 12:39:34 +00001104#undef HANDLE_INST
Torok Edwin05dc9d62011-10-06 12:39:34 +00001105 }
1106}
1107
1108static int map_from_llvmopcode(LLVMOpcode code)
1109{
1110 switch (code) {
1111#define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
Chandler Carruth9fb823b2013-01-02 11:36:10 +00001112#include "llvm/IR/Instruction.def"
Torok Edwin05dc9d62011-10-06 12:39:34 +00001113#undef HANDLE_INST
Torok Edwin05dc9d62011-10-06 12:39:34 +00001114 }
David Blaikie486df732012-01-16 23:24:27 +00001115 llvm_unreachable("Unhandled Opcode.");
Torok Edwin05dc9d62011-10-06 12:39:34 +00001116}
1117
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001118/*--.. Constant expressions ................................................--*/
1119
Chris Lattner40cf28d2009-10-12 04:01:02 +00001120LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00001121 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
Chris Lattner40cf28d2009-10-12 04:01:02 +00001122}
1123
Duncan Sandsd334aca2009-05-21 15:52:21 +00001124LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001125 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
Duncan Sandsd334aca2009-05-21 15:52:21 +00001126}
1127
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001128LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001129 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001130}
1131
1132LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001133 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001134}
1135
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001136LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001137 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001138}
1139
1140LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001141 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001142}
1143
1144
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001145LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001146 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001147}
1148
1149LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001150 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001151}
1152
1153LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001154 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001155 unwrap<Constant>(RHSConstant)));
1156}
1157
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001158LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1159 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001160 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001161 unwrap<Constant>(RHSConstant)));
1162}
1163
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001164LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1165 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001166 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001167 unwrap<Constant>(RHSConstant)));
1168}
1169
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001170LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001171 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001172 unwrap<Constant>(RHSConstant)));
1173}
1174
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001175LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001176 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001177 unwrap<Constant>(RHSConstant)));
1178}
1179
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001180LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1181 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001182 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001183 unwrap<Constant>(RHSConstant)));
1184}
1185
1186LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1187 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001188 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001189 unwrap<Constant>(RHSConstant)));
1190}
1191
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001192LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1193 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1194 unwrap<Constant>(RHSConstant)));
1195}
1196
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001197LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001198 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001199 unwrap<Constant>(RHSConstant)));
1200}
1201
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001202LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1203 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001204 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001205 unwrap<Constant>(RHSConstant)));
1206}
1207
1208LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1209 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001210 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001211 unwrap<Constant>(RHSConstant)));
1212}
1213
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001214LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001215 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001216 unwrap<Constant>(RHSConstant)));
1217}
1218
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001219LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001220 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001221 unwrap<Constant>(RHSConstant)));
1222}
1223
Manuel Jacob49fafb12016-10-04 23:32:42 +00001224LLVMValueRef LLVMConstExactUDiv(LLVMValueRef LHSConstant,
1225 LLVMValueRef RHSConstant) {
1226 return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1227 unwrap<Constant>(RHSConstant)));
1228}
1229
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001230LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001231 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001232 unwrap<Constant>(RHSConstant)));
1233}
1234
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001235LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
1236 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001237 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001238 unwrap<Constant>(RHSConstant)));
1239}
1240
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001241LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001242 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001243 unwrap<Constant>(RHSConstant)));
1244}
1245
1246LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001247 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001248 unwrap<Constant>(RHSConstant)));
1249}
1250
1251LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001252 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001253 unwrap<Constant>(RHSConstant)));
1254}
1255
1256LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001257 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001258 unwrap<Constant>(RHSConstant)));
1259}
1260
1261LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001262 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001263 unwrap<Constant>(RHSConstant)));
1264}
1265
1266LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001267 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001268 unwrap<Constant>(RHSConstant)));
1269}
1270
1271LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001272 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001273 unwrap<Constant>(RHSConstant)));
1274}
1275
1276LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1277 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Owen Anderson487375e2009-07-29 18:55:55 +00001278 return wrap(ConstantExpr::getICmp(Predicate,
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001279 unwrap<Constant>(LHSConstant),
1280 unwrap<Constant>(RHSConstant)));
1281}
1282
1283LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1284 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Owen Anderson487375e2009-07-29 18:55:55 +00001285 return wrap(ConstantExpr::getFCmp(Predicate,
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001286 unwrap<Constant>(LHSConstant),
1287 unwrap<Constant>(RHSConstant)));
1288}
1289
1290LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001291 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1292 unwrap<Constant>(RHSConstant)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001293}
1294
1295LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001296 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001297 unwrap<Constant>(RHSConstant)));
1298}
1299
1300LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001301 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001302 unwrap<Constant>(RHSConstant)));
1303}
1304
1305LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1306 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
Jay Foaded8db7d2011-07-21 14:31:17 +00001307 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1308 NumIndices);
David Blaikie4a2e73b2015-04-02 18:55:32 +00001309 return wrap(ConstantExpr::getGetElementPtr(
1310 nullptr, unwrap<Constant>(ConstantVal), IdxList));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001311}
1312
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001313LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1314 LLVMValueRef *ConstantIndices,
1315 unsigned NumIndices) {
1316 Constant* Val = unwrap<Constant>(ConstantVal);
Jay Foaded8db7d2011-07-21 14:31:17 +00001317 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1318 NumIndices);
David Blaikie4a2e73b2015-04-02 18:55:32 +00001319 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList));
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001320}
1321
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001322LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001323 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001324 unwrap(ToType)));
1325}
1326
1327LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001328 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001329 unwrap(ToType)));
1330}
1331
1332LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001333 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001334 unwrap(ToType)));
1335}
1336
1337LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001338 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001339 unwrap(ToType)));
1340}
1341
1342LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001343 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001344 unwrap(ToType)));
1345}
1346
1347LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001348 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001349 unwrap(ToType)));
1350}
1351
1352LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Owen Anderson487375e2009-07-29 18:55:55 +00001353 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001354 unwrap(ToType)));
1355}
1356
1357LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Owen Anderson487375e2009-07-29 18:55:55 +00001358 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001359 unwrap(ToType)));
1360}
1361
1362LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001363 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001364 unwrap(ToType)));
1365}
1366
1367LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001368 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001369 unwrap(ToType)));
1370}
1371
1372LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001373 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001374 unwrap(ToType)));
1375}
1376
1377LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001378 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001379 unwrap(ToType)));
1380}
1381
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001382LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1383 LLVMTypeRef ToType) {
1384 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1385 unwrap(ToType)));
1386}
1387
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001388LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1389 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001390 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001391 unwrap(ToType)));
1392}
1393
1394LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1395 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001396 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001397 unwrap(ToType)));
1398}
1399
1400LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1401 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001402 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001403 unwrap(ToType)));
1404}
1405
1406LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1407 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001408 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001409 unwrap(ToType)));
1410}
1411
1412LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
Chris Lattner25963c62010-01-09 22:27:07 +00001413 LLVMBool isSigned) {
Chris Lattner69229312011-02-15 00:14:00 +00001414 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1415 unwrap(ToType), isSigned));
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001416}
1417
1418LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001419 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001420 unwrap(ToType)));
1421}
1422
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001423LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1424 LLVMValueRef ConstantIfTrue,
1425 LLVMValueRef ConstantIfFalse) {
Chris Lattner69229312011-02-15 00:14:00 +00001426 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001427 unwrap<Constant>(ConstantIfTrue),
1428 unwrap<Constant>(ConstantIfFalse)));
1429}
1430
1431LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1432 LLVMValueRef IndexConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001433 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001434 unwrap<Constant>(IndexConstant)));
1435}
1436
1437LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1438 LLVMValueRef ElementValueConstant,
1439 LLVMValueRef IndexConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001440 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001441 unwrap<Constant>(ElementValueConstant),
1442 unwrap<Constant>(IndexConstant)));
1443}
1444
1445LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1446 LLVMValueRef VectorBConstant,
1447 LLVMValueRef MaskConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001448 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001449 unwrap<Constant>(VectorBConstant),
1450 unwrap<Constant>(MaskConstant)));
1451}
1452
Dan Gohmand5104a52008-11-03 22:55:43 +00001453LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1454 unsigned NumIdx) {
Chris Lattner69229312011-02-15 00:14:00 +00001455 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001456 makeArrayRef(IdxList, NumIdx)));
Dan Gohmand5104a52008-11-03 22:55:43 +00001457}
1458
1459LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1460 LLVMValueRef ElementValueConstant,
1461 unsigned *IdxList, unsigned NumIdx) {
Chris Lattner69229312011-02-15 00:14:00 +00001462 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
Dan Gohmand5104a52008-11-03 22:55:43 +00001463 unwrap<Constant>(ElementValueConstant),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001464 makeArrayRef(IdxList, NumIdx)));
Dan Gohmand5104a52008-11-03 22:55:43 +00001465}
1466
Chris Lattner25963c62010-01-09 22:27:07 +00001467LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1468 const char *Constraints,
1469 LLVMBool HasSideEffects,
Benjamin Kramer8dd0edc2012-09-10 11:52:00 +00001470 LLVMBool IsAlignStack) {
Chris Lattner25963c62010-01-09 22:27:07 +00001471 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
Benjamin Kramer8dd0edc2012-09-10 11:52:00 +00001472 Constraints, HasSideEffects, IsAlignStack));
Chris Lattner3d1f5522008-12-17 21:39:50 +00001473}
1474
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00001475LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1476 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1477}
1478
Gordon Henriksen76a03742007-09-18 03:18:57 +00001479/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1480
Gordon Henriksen265f7802008-03-19 01:11:35 +00001481LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1482 return wrap(unwrap<GlobalValue>(Global)->getParent());
1483}
1484
Chris Lattner25963c62010-01-09 22:27:07 +00001485LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001486 return unwrap<GlobalValue>(Global)->isDeclaration();
1487}
1488
1489LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001490 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001491 case GlobalValue::ExternalLinkage:
1492 return LLVMExternalLinkage;
1493 case GlobalValue::AvailableExternallyLinkage:
1494 return LLVMAvailableExternallyLinkage;
1495 case GlobalValue::LinkOnceAnyLinkage:
1496 return LLVMLinkOnceAnyLinkage;
1497 case GlobalValue::LinkOnceODRLinkage:
1498 return LLVMLinkOnceODRLinkage;
1499 case GlobalValue::WeakAnyLinkage:
1500 return LLVMWeakAnyLinkage;
1501 case GlobalValue::WeakODRLinkage:
1502 return LLVMWeakODRLinkage;
1503 case GlobalValue::AppendingLinkage:
1504 return LLVMAppendingLinkage;
1505 case GlobalValue::InternalLinkage:
1506 return LLVMInternalLinkage;
1507 case GlobalValue::PrivateLinkage:
1508 return LLVMPrivateLinkage;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001509 case GlobalValue::ExternalWeakLinkage:
1510 return LLVMExternalWeakLinkage;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001511 case GlobalValue::CommonLinkage:
1512 return LLVMCommonLinkage;
1513 }
1514
David Blaikiea5708dc2012-01-17 07:00:13 +00001515 llvm_unreachable("Invalid GlobalValue linkage!");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001516}
1517
1518void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001519 GlobalValue *GV = unwrap<GlobalValue>(Global);
1520
1521 switch (Linkage) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001522 case LLVMExternalLinkage:
1523 GV->setLinkage(GlobalValue::ExternalLinkage);
1524 break;
1525 case LLVMAvailableExternallyLinkage:
1526 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1527 break;
1528 case LLVMLinkOnceAnyLinkage:
1529 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1530 break;
1531 case LLVMLinkOnceODRLinkage:
1532 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1533 break;
Bill Wendling34bc34e2012-08-17 18:33:14 +00001534 case LLVMLinkOnceODRAutoHideLinkage:
Rafael Espindola716e7402013-11-01 17:09:14 +00001535 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1536 "longer supported.");
Bill Wendling34bc34e2012-08-17 18:33:14 +00001537 break;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001538 case LLVMWeakAnyLinkage:
1539 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1540 break;
1541 case LLVMWeakODRLinkage:
1542 GV->setLinkage(GlobalValue::WeakODRLinkage);
1543 break;
1544 case LLVMAppendingLinkage:
1545 GV->setLinkage(GlobalValue::AppendingLinkage);
1546 break;
1547 case LLVMInternalLinkage:
1548 GV->setLinkage(GlobalValue::InternalLinkage);
1549 break;
1550 case LLVMPrivateLinkage:
1551 GV->setLinkage(GlobalValue::PrivateLinkage);
1552 break;
1553 case LLVMLinkerPrivateLinkage:
Rafael Espindola2fb5bc32014-03-13 23:18:37 +00001554 GV->setLinkage(GlobalValue::PrivateLinkage);
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001555 break;
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001556 case LLVMLinkerPrivateWeakLinkage:
Rafael Espindola2fb5bc32014-03-13 23:18:37 +00001557 GV->setLinkage(GlobalValue::PrivateLinkage);
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001558 break;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001559 case LLVMDLLImportLinkage:
Nico Rieck7157bb72014-01-14 15:22:47 +00001560 DEBUG(errs()
1561 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001562 break;
1563 case LLVMDLLExportLinkage:
Nico Rieck7157bb72014-01-14 15:22:47 +00001564 DEBUG(errs()
1565 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001566 break;
1567 case LLVMExternalWeakLinkage:
1568 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1569 break;
1570 case LLVMGhostLinkage:
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001571 DEBUG(errs()
1572 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001573 break;
1574 case LLVMCommonLinkage:
1575 GV->setLinkage(GlobalValue::CommonLinkage);
1576 break;
1577 }
Gordon Henriksen76a03742007-09-18 03:18:57 +00001578}
1579
1580const char *LLVMGetSection(LLVMValueRef Global) {
Rafael Espindola83658d62016-05-11 18:21:59 +00001581 // Using .data() is safe because of how GlobalObject::setSection is
1582 // implemented.
1583 return unwrap<GlobalValue>(Global)->getSection().data();
Gordon Henriksen76a03742007-09-18 03:18:57 +00001584}
1585
1586void LLVMSetSection(LLVMValueRef Global, const char *Section) {
Rafael Espindola99e05cf2014-05-13 18:45:48 +00001587 unwrap<GlobalObject>(Global)->setSection(Section);
Gordon Henriksen76a03742007-09-18 03:18:57 +00001588}
1589
1590LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1591 return static_cast<LLVMVisibility>(
1592 unwrap<GlobalValue>(Global)->getVisibility());
1593}
1594
1595void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1596 unwrap<GlobalValue>(Global)
1597 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1598}
1599
Reid Kleckner2fae26f2014-03-05 02:34:23 +00001600LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1601 return static_cast<LLVMDLLStorageClass>(
1602 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1603}
1604
1605void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1606 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1607 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1608}
1609
Robert Widmann4bb481b2018-03-14 06:45:51 +00001610LLVMUnnamedAddr LLVMGetUnnamedAddress(LLVMValueRef Global) {
1611 switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
1612 case GlobalVariable::UnnamedAddr::None:
1613 return LLVMNoUnnamedAddr;
1614 case GlobalVariable::UnnamedAddr::Local:
1615 return LLVMLocalUnnamedAddr;
1616 case GlobalVariable::UnnamedAddr::Global:
1617 return LLVMGlobalUnnamedAddr;
1618 }
Simon Pilgrimf0ccaae2018-03-14 12:04:51 +00001619 llvm_unreachable("Unknown UnnamedAddr kind!");
Robert Widmann4bb481b2018-03-14 06:45:51 +00001620}
1621
1622void LLVMSetUnnamedAddress(LLVMValueRef Global, LLVMUnnamedAddr UnnamedAddr) {
1623 GlobalValue *GV = unwrap<GlobalValue>(Global);
1624
1625 switch (UnnamedAddr) {
1626 case LLVMNoUnnamedAddr:
1627 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);
1628 case LLVMLocalUnnamedAddr:
1629 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);
1630 case LLVMGlobalUnnamedAddr:
1631 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);
1632 }
1633}
1634
Tim Northoverad96d012014-03-10 19:24:35 +00001635LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001636 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
Tim Northoverad96d012014-03-10 19:24:35 +00001637}
1638
1639void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001640 unwrap<GlobalValue>(Global)->setUnnamedAddr(
1641 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1642 : GlobalValue::UnnamedAddr::None);
Tim Northoverad96d012014-03-10 19:24:35 +00001643}
1644
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001645/*--.. Operations on global variables, load and store instructions .........--*/
1646
1647unsigned LLVMGetAlignment(LLVMValueRef V) {
1648 Value *P = unwrap<Value>(V);
1649 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1650 return GV->getAlignment();
Peter Zotov9f584e62014-03-05 05:05:34 +00001651 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1652 return AI->getAlignment();
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001653 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1654 return LI->getAlignment();
1655 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1656 return SI->getAlignment();
1657
Peter Zotov9f584e62014-03-05 05:05:34 +00001658 llvm_unreachable(
1659 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001660}
1661
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001662void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1663 Value *P = unwrap<Value>(V);
Rafael Espindola99e05cf2014-05-13 18:45:48 +00001664 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001665 GV->setAlignment(Bytes);
Peter Zotov9f584e62014-03-05 05:05:34 +00001666 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1667 AI->setAlignment(Bytes);
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001668 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1669 LI->setAlignment(Bytes);
1670 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1671 SI->setAlignment(Bytes);
Anders Waldenborga36a7822013-10-29 09:37:28 +00001672 else
Peter Zotov9f584e62014-03-05 05:05:34 +00001673 llvm_unreachable(
1674 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001675}
1676
1677/*--.. Operations on global variables ......................................--*/
1678
1679LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
Owen Andersonb17f3292009-07-08 19:03:57 +00001680 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
Craig Topperc6207612014-04-09 06:08:46 +00001681 GlobalValue::ExternalLinkage, nullptr, Name));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001682}
1683
Erick Tryzelaar06894b32010-02-28 09:46:13 +00001684LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1685 const char *Name,
1686 unsigned AddressSpace) {
1687 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
Craig Topperc6207612014-04-09 06:08:46 +00001688 GlobalValue::ExternalLinkage, nullptr, Name,
1689 nullptr, GlobalVariable::NotThreadLocal,
1690 AddressSpace));
Erick Tryzelaar06894b32010-02-28 09:46:13 +00001691}
1692
Gordon Henriksen783f7bb2007-10-08 03:45:09 +00001693LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1694 return wrap(unwrap(M)->getNamedGlobal(Name));
1695}
1696
Gordon Henriksen054817c2008-03-19 03:47:18 +00001697LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1698 Module *Mod = unwrap(M);
1699 Module::global_iterator I = Mod->global_begin();
1700 if (I == Mod->global_end())
Craig Topperc6207612014-04-09 06:08:46 +00001701 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001702 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001703}
1704
1705LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1706 Module *Mod = unwrap(M);
1707 Module::global_iterator I = Mod->global_end();
1708 if (I == Mod->global_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001709 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001710 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001711}
1712
1713LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1714 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001715 Module::global_iterator I(GV);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001716 if (++I == GV->getParent()->global_end())
Craig Topperc6207612014-04-09 06:08:46 +00001717 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001718 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001719}
1720
1721LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1722 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001723 Module::global_iterator I(GV);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001724 if (I == GV->getParent()->global_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001725 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001726 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001727}
1728
Gordon Henriksen76a03742007-09-18 03:18:57 +00001729void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1730 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1731}
1732
Gordon Henriksen76a03742007-09-18 03:18:57 +00001733LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
Chris Lattner40cf28d2009-10-12 04:01:02 +00001734 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1735 if ( !GV->hasInitializer() )
Craig Topperc6207612014-04-09 06:08:46 +00001736 return nullptr;
Chris Lattner40cf28d2009-10-12 04:01:02 +00001737 return wrap(GV->getInitializer());
Gordon Henriksen76a03742007-09-18 03:18:57 +00001738}
1739
1740void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1741 unwrap<GlobalVariable>(GlobalVar)
1742 ->setInitializer(unwrap<Constant>(ConstantVal));
1743}
1744
Chris Lattner25963c62010-01-09 22:27:07 +00001745LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001746 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1747}
1748
Chris Lattner25963c62010-01-09 22:27:07 +00001749void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001750 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1751}
1752
Chris Lattner25963c62010-01-09 22:27:07 +00001753LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
Gordon Henriksen751ebf72007-10-07 17:31:42 +00001754 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1755}
1756
Chris Lattner25963c62010-01-09 22:27:07 +00001757void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
Gordon Henriksen751ebf72007-10-07 17:31:42 +00001758 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1759}
1760
Hans Wennborg5ff71202013-04-16 08:58:59 +00001761LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1762 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1763 case GlobalVariable::NotThreadLocal:
1764 return LLVMNotThreadLocal;
1765 case GlobalVariable::GeneralDynamicTLSModel:
1766 return LLVMGeneralDynamicTLSModel;
1767 case GlobalVariable::LocalDynamicTLSModel:
1768 return LLVMLocalDynamicTLSModel;
1769 case GlobalVariable::InitialExecTLSModel:
1770 return LLVMInitialExecTLSModel;
1771 case GlobalVariable::LocalExecTLSModel:
1772 return LLVMLocalExecTLSModel;
1773 }
1774
1775 llvm_unreachable("Invalid GlobalVariable thread local mode");
1776}
1777
1778void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1779 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1780
1781 switch (Mode) {
1782 case LLVMNotThreadLocal:
1783 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1784 break;
1785 case LLVMGeneralDynamicTLSModel:
1786 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1787 break;
1788 case LLVMLocalDynamicTLSModel:
1789 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1790 break;
1791 case LLVMInitialExecTLSModel:
1792 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1793 break;
1794 case LLVMLocalExecTLSModel:
1795 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1796 break;
1797 }
1798}
1799
1800LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1801 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1802}
1803
1804void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1805 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1806}
1807
Chris Lattner3d1f5522008-12-17 21:39:50 +00001808/*--.. Operations on aliases ......................................--*/
1809
1810LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1811 const char *Name) {
Rafael Espindola4fe00942014-05-16 13:34:04 +00001812 auto *PTy = cast<PointerType>(unwrap(Ty));
David Blaikie16a2f3e2015-09-14 18:01:59 +00001813 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1814 GlobalValue::ExternalLinkage, Name,
Rafael Espindola993502e2015-02-23 21:51:06 +00001815 unwrap<Constant>(Aliasee), unwrap(M)));
Chris Lattner3d1f5522008-12-17 21:39:50 +00001816}
1817
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001818/*--.. Operations on functions .............................................--*/
1819
1820LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1821 LLVMTypeRef FunctionTy) {
Gabor Greife9ecc682008-04-06 20:25:17 +00001822 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1823 GlobalValue::ExternalLinkage, Name, unwrap(M)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001824}
1825
Gordon Henriksen783f7bb2007-10-08 03:45:09 +00001826LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1827 return wrap(unwrap(M)->getFunction(Name));
1828}
1829
Gordon Henriksen054817c2008-03-19 03:47:18 +00001830LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1831 Module *Mod = unwrap(M);
1832 Module::iterator I = Mod->begin();
1833 if (I == Mod->end())
Craig Topperc6207612014-04-09 06:08:46 +00001834 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001835 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001836}
1837
1838LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1839 Module *Mod = unwrap(M);
1840 Module::iterator I = Mod->end();
1841 if (I == Mod->begin())
Craig Topperc6207612014-04-09 06:08:46 +00001842 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001843 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001844}
1845
1846LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1847 Function *Func = unwrap<Function>(Fn);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001848 Module::iterator I(Func);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001849 if (++I == Func->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00001850 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001851 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001852}
1853
1854LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1855 Function *Func = unwrap<Function>(Fn);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001856 Module::iterator I(Func);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001857 if (I == Func->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00001858 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001859 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001860}
1861
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001862void LLVMDeleteFunction(LLVMValueRef Fn) {
1863 unwrap<Function>(Fn)->eraseFromParent();
1864}
1865
Amaury Sechete39e8532016-02-18 20:38:32 +00001866LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
1867 return unwrap<Function>(Fn)->hasPersonalityFn();
1868}
1869
Andrew Wilkins3bdfc1c2015-07-14 01:23:06 +00001870LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
1871 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
1872}
1873
1874void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
1875 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
1876}
1877
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001878unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1879 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1880 return F->getIntrinsicID();
1881 return 0;
1882}
1883
1884unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1885 return unwrap<Function>(Fn)->getCallingConv();
1886}
1887
1888void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
Sandeep Patel68c5f472009-09-02 08:44:58 +00001889 return unwrap<Function>(Fn)->setCallingConv(
1890 static_cast<CallingConv::ID>(CC));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001891}
1892
Gordon Henriksend930f912008-08-17 18:44:35 +00001893const char *LLVMGetGC(LLVMValueRef Fn) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00001894 Function *F = unwrap<Function>(Fn);
Mehdi Amini599ebf22016-01-08 02:28:20 +00001895 return F->hasGC()? F->getGC().c_str() : nullptr;
Gordon Henriksen71183b62007-12-10 03:18:06 +00001896}
1897
Gordon Henriksend930f912008-08-17 18:44:35 +00001898void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00001899 Function *F = unwrap<Function>(Fn);
Gordon Henriksend930f912008-08-17 18:44:35 +00001900 if (GC)
1901 F->setGC(GC);
Gordon Henriksen71183b62007-12-10 03:18:06 +00001902 else
Gordon Henriksend930f912008-08-17 18:44:35 +00001903 F->clearGC();
Gordon Henriksen71183b62007-12-10 03:18:06 +00001904}
1905
Amaury Sechet5db224e2016-06-12 06:17:24 +00001906void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1907 LLVMAttributeRef A) {
1908 unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
1909}
1910
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001911unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001912 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
1913 return AS.getNumAttributes();
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001914}
1915
1916void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1917 LLVMAttributeRef *Attrs) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001918 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
1919 for (auto A : AS)
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001920 *Attrs++ = wrap(A);
1921}
1922
Amaury Sechet5db224e2016-06-12 06:17:24 +00001923LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
1924 LLVMAttributeIndex Idx,
1925 unsigned KindID) {
1926 return wrap(unwrap<Function>(F)->getAttribute(Idx,
1927 (Attribute::AttrKind)KindID));
1928}
1929
Amaury Sechet6100adf2016-06-15 17:50:39 +00001930LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
1931 LLVMAttributeIndex Idx,
1932 const char *K, unsigned KLen) {
1933 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
1934}
1935
Amaury Sechet5db224e2016-06-12 06:17:24 +00001936void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1937 unsigned KindID) {
1938 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
1939}
1940
Amaury Sechet6100adf2016-06-15 17:50:39 +00001941void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1942 const char *K, unsigned KLen) {
1943 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
1944}
1945
Tom Stellarde8f35e12013-04-16 23:12:43 +00001946void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1947 const char *V) {
1948 Function *Func = unwrap<Function>(Fn);
Reid Kleckner9d16fa02017-04-19 17:28:52 +00001949 Attribute Attr = Attribute::get(Func->getContext(), A, V);
1950 Func->addAttribute(AttributeList::FunctionIndex, Attr);
Tom Stellarde8f35e12013-04-16 23:12:43 +00001951}
1952
Gordon Henriksen265f7802008-03-19 01:11:35 +00001953/*--.. Operations on parameters ............................................--*/
1954
1955unsigned LLVMCountParams(LLVMValueRef FnRef) {
1956 // This function is strictly redundant to
1957 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
Dan Gohmanc7076912008-06-21 22:06:54 +00001958 return unwrap<Function>(FnRef)->arg_size();
Gordon Henriksen265f7802008-03-19 01:11:35 +00001959}
1960
1961void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1962 Function *Fn = unwrap<Function>(FnRef);
1963 for (Function::arg_iterator I = Fn->arg_begin(),
1964 E = Fn->arg_end(); I != E; I++)
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001965 *ParamRefs++ = wrap(&*I);
Gordon Henriksen265f7802008-03-19 01:11:35 +00001966}
1967
1968LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
Reid Kleckner56d028d2017-03-17 17:16:39 +00001969 Function *Fn = unwrap<Function>(FnRef);
1970 return wrap(&Fn->arg_begin()[index]);
Gordon Henriksen265f7802008-03-19 01:11:35 +00001971}
1972
1973LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1974 return wrap(unwrap<Argument>(V)->getParent());
1975}
1976
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001977LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1978 Function *Func = unwrap<Function>(Fn);
1979 Function::arg_iterator I = Func->arg_begin();
1980 if (I == Func->arg_end())
Craig Topperc6207612014-04-09 06:08:46 +00001981 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001982 return wrap(&*I);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001983}
1984
1985LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1986 Function *Func = unwrap<Function>(Fn);
1987 Function::arg_iterator I = Func->arg_end();
1988 if (I == Func->arg_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001989 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001990 return wrap(&*--I);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001991}
1992
1993LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1994 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner56d028d2017-03-17 17:16:39 +00001995 Function *Fn = A->getParent();
1996 if (A->getArgNo() + 1 >= Fn->arg_size())
Craig Topperc6207612014-04-09 06:08:46 +00001997 return nullptr;
Reid Kleckner56d028d2017-03-17 17:16:39 +00001998 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001999}
2000
2001LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
2002 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner56d028d2017-03-17 17:16:39 +00002003 if (A->getArgNo() == 0)
Craig Topperc6207612014-04-09 06:08:46 +00002004 return nullptr;
Reid Kleckner56d028d2017-03-17 17:16:39 +00002005 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00002006}
2007
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002008void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
Bill Wendling49bc76c2013-01-23 06:14:59 +00002009 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner9d16fa02017-04-19 17:28:52 +00002010 A->addAttr(Attribute::getWithAlignment(A->getContext(), align));
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002011}
2012
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002013/*--.. Operations on basic blocks ..........................................--*/
2014
Gordon Henriksen265f7802008-03-19 01:11:35 +00002015LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
2016 return wrap(static_cast<Value*>(unwrap(BB)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002017}
2018
Chris Lattner25963c62010-01-09 22:27:07 +00002019LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002020 return isa<BasicBlock>(unwrap(Val));
2021}
2022
2023LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
2024 return wrap(unwrap<BasicBlock>(Val));
2025}
2026
Amaury Secheta82042e2016-02-09 22:36:41 +00002027const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
2028 return unwrap(BB)->getName().data();
2029}
2030
Gordon Henriksen07a45f42008-03-23 22:21:29 +00002031LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
2032 return wrap(unwrap(BB)->getParent());
Gordon Henriksen265f7802008-03-19 01:11:35 +00002033}
2034
Nate Begeman43c322b2011-08-23 20:27:46 +00002035LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
2036 return wrap(unwrap(BB)->getTerminator());
2037}
2038
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002039unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
Dan Gohmanc7076912008-06-21 22:06:54 +00002040 return unwrap<Function>(FnRef)->size();
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002041}
2042
2043void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2044 Function *Fn = unwrap<Function>(FnRef);
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00002045 for (BasicBlock &BB : *Fn)
2046 *BasicBlocksRefs++ = wrap(&BB);
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002047}
2048
2049LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2050 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2051}
2052
Gordon Henriksen054817c2008-03-19 03:47:18 +00002053LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2054 Function *Func = unwrap<Function>(Fn);
2055 Function::iterator I = Func->begin();
2056 if (I == Func->end())
Craig Topperc6207612014-04-09 06:08:46 +00002057 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002058 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002059}
2060
2061LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2062 Function *Func = unwrap<Function>(Fn);
2063 Function::iterator I = Func->end();
2064 if (I == Func->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002065 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002066 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002067}
2068
2069LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2070 BasicBlock *Block = unwrap(BB);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002071 Function::iterator I(Block);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002072 if (++I == Block->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00002073 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002074 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002075}
2076
2077LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2078 BasicBlock *Block = unwrap(BB);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002079 Function::iterator I(Block);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002080 if (I == Block->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002081 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002082 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002083}
2084
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002085LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2086 LLVMValueRef FnRef,
2087 const char *Name) {
2088 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002089}
2090
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002091LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2092 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2093}
2094
2095LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2096 LLVMBasicBlockRef BBRef,
2097 const char *Name) {
2098 BasicBlock *BB = unwrap(BBRef);
2099 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2100}
2101
2102LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002103 const char *Name) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002104 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002105}
2106
2107void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2108 unwrap(BBRef)->eraseFromParent();
2109}
2110
Nate Begeman43c322b2011-08-23 20:27:46 +00002111void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2112 unwrap(BBRef)->removeFromParent();
2113}
2114
Duncan Sandsb1d61aa2010-07-19 15:31:07 +00002115void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2116 unwrap(BB)->moveBefore(unwrap(MovePos));
2117}
2118
2119void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2120 unwrap(BB)->moveAfter(unwrap(MovePos));
2121}
2122
Gordon Henriksen265f7802008-03-19 01:11:35 +00002123/*--.. Operations on instructions ..........................................--*/
2124
2125LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2126 return wrap(unwrap<Instruction>(Inst)->getParent());
2127}
2128
Gordon Henriksen054817c2008-03-19 03:47:18 +00002129LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2130 BasicBlock *Block = unwrap(BB);
2131 BasicBlock::iterator I = Block->begin();
2132 if (I == Block->end())
Craig Topperc6207612014-04-09 06:08:46 +00002133 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002134 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002135}
2136
2137LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2138 BasicBlock *Block = unwrap(BB);
2139 BasicBlock::iterator I = Block->end();
2140 if (I == Block->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002141 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002142 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002143}
2144
2145LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2146 Instruction *Instr = unwrap<Instruction>(Inst);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002147 BasicBlock::iterator I(Instr);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002148 if (++I == Instr->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00002149 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002150 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002151}
2152
2153LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2154 Instruction *Instr = unwrap<Instruction>(Inst);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002155 BasicBlock::iterator I(Instr);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002156 if (I == Instr->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002157 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002158 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002159}
2160
Amaury Sechet2f432082016-02-11 21:37:54 +00002161void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2162 unwrap<Instruction>(Inst)->removeFromParent();
2163}
2164
Devang Pateldbebc6f2011-10-03 20:59:18 +00002165void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2166 unwrap<Instruction>(Inst)->eraseFromParent();
2167}
2168
Torok Edwin60c40de2011-10-06 12:13:20 +00002169LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
Torok Edwin2e9affe2011-10-14 20:37:42 +00002170 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2171 return (LLVMIntPredicate)I->getPredicate();
2172 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2173 if (CE->getOpcode() == Instruction::ICmp)
2174 return (LLVMIntPredicate)CE->getPredicate();
2175 return (LLVMIntPredicate)0;
Torok Edwin60c40de2011-10-06 12:13:20 +00002176}
2177
Peter Zotov1d98e6d2014-10-28 19:46:44 +00002178LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2179 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2180 return (LLVMRealPredicate)I->getPredicate();
2181 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2182 if (CE->getOpcode() == Instruction::FCmp)
2183 return (LLVMRealPredicate)CE->getPredicate();
2184 return (LLVMRealPredicate)0;
2185}
2186
Torok Edwinab6158e2011-10-14 20:37:49 +00002187LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2188 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2189 return map_to_llvmopcode(C->getOpcode());
2190 return (LLVMOpcode)0;
2191}
2192
Peter Zotovaff492c2014-10-17 01:02:34 +00002193LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2194 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2195 return wrap(C->clone());
2196 return nullptr;
2197}
2198
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002199unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
Robert Widmann478fce92018-03-30 17:49:53 +00002200 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2201 return FPI->getNumArgOperands();
2202 }
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002203 return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands();
2204}
2205
Robert Widmann478fce92018-03-30 17:49:53 +00002206/*--.. Call and invoke instructions ........................................--*/
2207
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002208unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002209 return CallSite(unwrap<Instruction>(Instr)).getCallingConv();
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002210}
2211
2212void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002213 return CallSite(unwrap<Instruction>(Instr))
2214 .setCallingConv(static_cast<CallingConv::ID>(CC));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002215}
2216
Andrew Trickdc073ad2013-09-18 23:31:10 +00002217void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002218 unsigned align) {
2219 CallSite Call = CallSite(unwrap<Instruction>(Instr));
Reid Kleckner9d16fa02017-04-19 17:28:52 +00002220 Attribute AlignAttr = Attribute::getWithAlignment(Call->getContext(), align);
2221 Call.addAttribute(index, AlignAttr);
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002222}
2223
Amaury Secheta65a2372016-06-15 05:14:29 +00002224void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2225 LLVMAttributeRef A) {
2226 CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A));
2227}
2228
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002229unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2230 LLVMAttributeIndex Idx) {
2231 auto CS = CallSite(unwrap<Instruction>(C));
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002232 auto AS = CS.getAttributes().getAttributes(Idx);
2233 return AS.getNumAttributes();
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002234}
2235
2236void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2237 LLVMAttributeRef *Attrs) {
2238 auto CS = CallSite(unwrap<Instruction>(C));
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002239 auto AS = CS.getAttributes().getAttributes(Idx);
2240 for (auto A : AS)
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002241 *Attrs++ = wrap(A);
2242}
2243
Amaury Secheta65a2372016-06-15 05:14:29 +00002244LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
2245 LLVMAttributeIndex Idx,
2246 unsigned KindID) {
2247 return wrap(CallSite(unwrap<Instruction>(C))
2248 .getAttribute(Idx, (Attribute::AttrKind)KindID));
2249}
2250
Amaury Sechet6100adf2016-06-15 17:50:39 +00002251LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
2252 LLVMAttributeIndex Idx,
2253 const char *K, unsigned KLen) {
2254 return wrap(CallSite(unwrap<Instruction>(C))
2255 .getAttribute(Idx, StringRef(K, KLen)));
2256}
2257
Amaury Secheta65a2372016-06-15 05:14:29 +00002258void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2259 unsigned KindID) {
2260 CallSite(unwrap<Instruction>(C))
2261 .removeAttribute(Idx, (Attribute::AttrKind)KindID);
2262}
2263
Amaury Sechet6100adf2016-06-15 17:50:39 +00002264void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2265 const char *K, unsigned KLen) {
2266 CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen));
2267}
2268
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002269LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2270 return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue());
2271}
2272
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002273/*--.. Operations on call instructions (only) ..............................--*/
2274
Chris Lattner25963c62010-01-09 22:27:07 +00002275LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002276 return unwrap<CallInst>(Call)->isTailCall();
2277}
2278
Chris Lattner25963c62010-01-09 22:27:07 +00002279void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002280 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2281}
2282
Amaury Sechete39e8532016-02-18 20:38:32 +00002283/*--.. Operations on invoke instructions (only) ............................--*/
2284
2285LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2286 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2287}
2288
2289LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
Robert Widmann478fce92018-03-30 17:49:53 +00002290 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2291 return wrap(CRI->getUnwindDest());
2292 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2293 return wrap(CSI->getUnwindDest());
2294 }
Amaury Sechete39e8532016-02-18 20:38:32 +00002295 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2296}
2297
2298void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2299 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2300}
2301
2302void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
Robert Widmann478fce92018-03-30 17:49:53 +00002303 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2304 return CRI->setUnwindDest(unwrap(B));
2305 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2306 return CSI->setUnwindDest(unwrap(B));
2307 }
Amaury Sechete39e8532016-02-18 20:38:32 +00002308 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2309}
2310
Peter Zotov2481c752014-10-28 19:46:56 +00002311/*--.. Operations on terminators ...........................................--*/
2312
2313unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2314 return unwrap<TerminatorInst>(Term)->getNumSuccessors();
2315}
2316
2317LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2318 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
2319}
2320
2321void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2322 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
2323}
2324
2325/*--.. Operations on branch instructions (only) ............................--*/
2326
2327LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2328 return unwrap<BranchInst>(Branch)->isConditional();
2329}
2330
2331LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2332 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2333}
2334
2335void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2336 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2337}
2338
Nate Begeman43c322b2011-08-23 20:27:46 +00002339/*--.. Operations on switch instructions (only) ............................--*/
2340
2341LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2342 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2343}
2344
Amaury Sechet1dcf5772016-02-09 22:50:53 +00002345/*--.. Operations on alloca instructions (only) ............................--*/
2346
2347LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
2348 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2349}
2350
Amaury Sechet053ac452016-02-17 22:51:03 +00002351/*--.. Operations on gep instructions (only) ...............................--*/
2352
2353LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
2354 return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2355}
2356
Amaury Sechet8a367d42016-05-01 02:23:14 +00002357void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
2358 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
Amaury Sechet053ac452016-02-17 22:51:03 +00002359}
2360
Gordon Henriksen44dd8fb2007-10-08 18:14:39 +00002361/*--.. Operations on phi nodes .............................................--*/
2362
2363void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2364 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2365 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2366 for (unsigned I = 0; I != Count; ++I)
2367 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2368}
2369
2370unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2371 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2372}
2373
2374LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2375 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2376}
2377
2378LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2379 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2380}
2381
Amaury Sechetaad93532016-02-10 00:38:50 +00002382/*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2383
2384unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
2385 auto *I = unwrap(Inst);
Amaury Sechet053ac452016-02-17 22:51:03 +00002386 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
2387 return GEP->getNumIndices();
Amaury Sechetaad93532016-02-10 00:38:50 +00002388 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2389 return EV->getNumIndices();
2390 if (auto *IV = dyn_cast<InsertValueInst>(I))
2391 return IV->getNumIndices();
2392 llvm_unreachable(
2393 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2394}
2395
2396const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2397 auto *I = unwrap(Inst);
2398 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2399 return EV->getIndices().data();
2400 if (auto *IV = dyn_cast<InsertValueInst>(I))
2401 return IV->getIndices().data();
2402 llvm_unreachable(
2403 "LLVMGetIndices applies only to extractvalue and insertvalue!");
2404}
2405
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002406
2407/*===-- Instruction builders ----------------------------------------------===*/
2408
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002409LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2410 return wrap(new IRBuilder<>(*unwrap(C)));
2411}
2412
Gordon Henriksena735a9c2008-05-04 12:55:34 +00002413LLVMBuilderRef LLVMCreateBuilder(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002414 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002415}
2416
Gordon Henriksen054817c2008-03-19 03:47:18 +00002417void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2418 LLVMValueRef Instr) {
2419 BasicBlock *BB = unwrap(Block);
Duncan P. N. Exon Smith43724642016-08-11 15:45:04 +00002420 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
2421 unwrap(Builder)->SetInsertPoint(BB, I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002422}
2423
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002424void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2425 Instruction *I = unwrap<Instruction>(Instr);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002426 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002427}
2428
2429void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2430 BasicBlock *BB = unwrap(Block);
2431 unwrap(Builder)->SetInsertPoint(BB);
2432}
2433
Gordon Henriksen265f7802008-03-19 01:11:35 +00002434LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2435 return wrap(unwrap(Builder)->GetInsertBlock());
2436}
2437
Chris Lattner3d1f5522008-12-17 21:39:50 +00002438void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
Chris Lattnere4bcde82010-04-01 06:31:45 +00002439 unwrap(Builder)->ClearInsertionPoint();
Chris Lattner3d1f5522008-12-17 21:39:50 +00002440}
2441
2442void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2443 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2444}
2445
Erick Tryzelaar9813bea2009-08-16 02:20:57 +00002446void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2447 const char *Name) {
2448 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2449}
2450
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002451void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2452 delete unwrap(Builder);
2453}
2454
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002455/*--.. Metadata builders ...................................................--*/
2456
2457void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002458 MDNode *Loc =
2459 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002460 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002461}
2462
2463LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002464 LLVMContext &Context = unwrap(Builder)->getContext();
2465 return wrap(MetadataAsValue::get(
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002466 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002467}
2468
2469void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2470 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2471}
2472
2473
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002474/*--.. Instruction builders ................................................--*/
2475
2476LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2477 return wrap(unwrap(B)->CreateRetVoid());
2478}
2479
2480LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2481 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2482}
2483
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002484LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2485 unsigned N) {
2486 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2487}
2488
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002489LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2490 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2491}
2492
2493LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2494 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2495 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2496}
2497
2498LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2499 LLVMBasicBlockRef Else, unsigned NumCases) {
2500 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2501}
2502
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00002503LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2504 unsigned NumDests) {
2505 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2506}
2507
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002508LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2509 LLVMValueRef *Args, unsigned NumArgs,
2510 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2511 const char *Name) {
2512 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002513 makeArrayRef(unwrap(Args), NumArgs),
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002514 Name));
2515}
2516
Bill Wendlingfae14752011-08-12 20:24:12 +00002517LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
Reid Kleckneref9828f2015-07-16 01:16:39 +00002518 LLVMValueRef PersFn, unsigned NumClauses,
2519 const char *Name) {
2520 // The personality used to live on the landingpad instruction, but now it
2521 // lives on the parent function. For compatibility, take the provided
2522 // personality and put it on the parent function.
2523 if (PersFn)
2524 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
2525 cast<Function>(unwrap(PersFn)));
David Majnemer7fddecc2015-06-17 20:52:32 +00002526 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
Bill Wendlingfae14752011-08-12 20:24:12 +00002527}
2528
Robert Widmann478fce92018-03-30 17:49:53 +00002529LLVMValueRef LLVMBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
2530 LLVMValueRef *Args, unsigned NumArgs,
2531 const char *Name) {
2532 return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
2533 makeArrayRef(unwrap(Args), NumArgs),
2534 Name));
2535}
2536
2537LLVMValueRef LLVMBuildCleanupPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
2538 LLVMValueRef *Args, unsigned NumArgs,
2539 const char *Name) {
2540 if (ParentPad == nullptr) {
2541 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
2542 ParentPad = wrap(Constant::getNullValue(Ty));
2543 }
2544 return wrap(unwrap(B)->CreateCleanupPad(unwrap(ParentPad),
2545 makeArrayRef(unwrap(Args), NumArgs),
2546 Name));
2547}
2548
Bill Wendlingf891bf82011-07-31 06:30:59 +00002549LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2550 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2551}
2552
Robert Widmann478fce92018-03-30 17:49:53 +00002553LLVMValueRef LLVMBuildCatchSwitch(LLVMBuilderRef B, LLVMValueRef ParentPad,
2554 LLVMBasicBlockRef UnwindBB,
2555 unsigned NumHandlers, const char *Name) {
2556 if (ParentPad == nullptr) {
2557 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
2558 ParentPad = wrap(Constant::getNullValue(Ty));
2559 }
2560 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
2561 NumHandlers, Name));
2562}
2563
2564LLVMValueRef LLVMBuildCatchRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
2565 LLVMBasicBlockRef BB) {
2566 return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
2567 unwrap(BB)));
2568}
2569
2570LLVMValueRef LLVMBuildCleanupRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
2571 LLVMBasicBlockRef BB) {
2572 return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
2573 unwrap(BB)));
2574}
2575
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002576LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2577 return wrap(unwrap(B)->CreateUnreachable());
2578}
2579
Gordon Henriksen097102c2008-01-01 05:50:53 +00002580void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2581 LLVMBasicBlockRef Dest) {
2582 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2583}
2584
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00002585void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2586 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2587}
2588
Amaury Sechete39e8532016-02-18 20:38:32 +00002589unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
2590 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
2591}
2592
2593LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
2594 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
2595}
2596
Bill Wendlingfae14752011-08-12 20:24:12 +00002597void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2598 unwrap<LandingPadInst>(LandingPad)->
2599 addClause(cast<Constant>(unwrap(ClauseVal)));
2600}
2601
Amaury Sechete39e8532016-02-18 20:38:32 +00002602LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
2603 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
2604}
2605
Bill Wendlingfae14752011-08-12 20:24:12 +00002606void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2607 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2608}
2609
Robert Widmann478fce92018-03-30 17:49:53 +00002610void LLVMAddHandler(LLVMValueRef CatchSwitch, LLVMBasicBlockRef Dest) {
2611 unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
2612}
2613
2614unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
2615 return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
2616}
2617
2618void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
2619 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
2620 for (CatchSwitchInst::handler_iterator I = CSI->handler_begin(),
2621 E = CSI->handler_end(); I != E; ++I)
2622 *Handlers++ = wrap(*I);
2623}
2624
2625LLVMValueRef LLVMGetParentCatchSwitch(LLVMValueRef CatchPad) {
2626 return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
2627}
2628
2629void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad, LLVMValueRef CatchSwitch) {
2630 unwrap<CatchPadInst>(CatchPad)
2631 ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
2632}
2633
2634/*--.. Funclets ...........................................................--*/
2635
2636LLVMValueRef LLVMGetArgOperand(LLVMValueRef Funclet, unsigned i) {
2637 return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
2638}
2639
2640void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
2641 unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
2642}
2643
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002644/*--.. Arithmetic ..........................................................--*/
2645
2646LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2647 const char *Name) {
2648 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2649}
2650
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002651LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2652 const char *Name) {
2653 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2654}
2655
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002656LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2657 const char *Name) {
2658 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2659}
2660
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002661LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2662 const char *Name) {
2663 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2664}
2665
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002666LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2667 const char *Name) {
2668 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2669}
2670
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002671LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2672 const char *Name) {
2673 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2674}
2675
2676LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2677 const char *Name) {
2678 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2679}
2680
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002681LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2682 const char *Name) {
2683 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2684}
2685
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002686LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2687 const char *Name) {
2688 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2689}
2690
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002691LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2692 const char *Name) {
2693 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2694}
2695
2696LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2697 const char *Name) {
2698 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2699}
2700
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002701LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2702 const char *Name) {
2703 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2704}
2705
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002706LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2707 const char *Name) {
2708 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2709}
2710
Manuel Jacob49fafb12016-10-04 23:32:42 +00002711LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2712 LLVMValueRef RHS, const char *Name) {
2713 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
2714}
2715
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002716LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2717 const char *Name) {
2718 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2719}
2720
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002721LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2722 LLVMValueRef RHS, const char *Name) {
2723 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2724}
2725
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002726LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2727 const char *Name) {
2728 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2729}
2730
2731LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2732 const char *Name) {
2733 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2734}
2735
2736LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2737 const char *Name) {
2738 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2739}
2740
2741LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2742 const char *Name) {
2743 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2744}
2745
2746LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2747 const char *Name) {
2748 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2749}
2750
2751LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2752 const char *Name) {
2753 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2754}
2755
2756LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2757 const char *Name) {
2758 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2759}
2760
2761LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2762 const char *Name) {
2763 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2764}
2765
2766LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2767 const char *Name) {
2768 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2769}
2770
2771LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2772 const char *Name) {
2773 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2774}
2775
Erick Tryzelaar31831792010-02-28 05:51:27 +00002776LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2777 LLVMValueRef LHS, LLVMValueRef RHS,
2778 const char *Name) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00002779 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
Erick Tryzelaar31831792010-02-28 05:51:27 +00002780 unwrap(RHS), Name));
2781}
2782
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002783LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2784 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2785}
2786
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002787LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2788 const char *Name) {
2789 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2790}
2791
2792LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2793 const char *Name) {
2794 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2795}
2796
Dan Gohmanf919bd62009-09-28 21:51:41 +00002797LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2798 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2799}
2800
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002801LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2802 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2803}
2804
2805/*--.. Memory ..............................................................--*/
2806
2807LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2808 const char *Name) {
Chris Lattner229907c2011-07-18 04:54:35 +00002809 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
Victor Hernandezf3db9152009-11-07 00:16:28 +00002810 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2811 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
Andrew Trickdc073ad2013-09-18 23:31:10 +00002812 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2813 ITy, unwrap(Ty), AllocSize,
Craig Topperc6207612014-04-09 06:08:46 +00002814 nullptr, nullptr, "");
Victor Hernandezf3db9152009-11-07 00:16:28 +00002815 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002816}
2817
2818LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2819 LLVMValueRef Val, const char *Name) {
Chris Lattner229907c2011-07-18 04:54:35 +00002820 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
Victor Hernandezf3db9152009-11-07 00:16:28 +00002821 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2822 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
Andrew Trickdc073ad2013-09-18 23:31:10 +00002823 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2824 ITy, unwrap(Ty), AllocSize,
Craig Topperc6207612014-04-09 06:08:46 +00002825 unwrap(Val), nullptr, "");
Victor Hernandezf3db9152009-11-07 00:16:28 +00002826 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002827}
2828
2829LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2830 const char *Name) {
Craig Topperc6207612014-04-09 06:08:46 +00002831 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002832}
2833
2834LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2835 LLVMValueRef Val, const char *Name) {
2836 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2837}
2838
2839LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
Victor Hernandezde5ad422009-10-26 23:43:48 +00002840 return wrap(unwrap(B)->Insert(
2841 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002842}
2843
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002844LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2845 const char *Name) {
2846 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2847}
2848
Andrew Trickdc073ad2013-09-18 23:31:10 +00002849LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002850 LLVMValueRef PointerVal) {
2851 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2852}
2853
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002854static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2855 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +00002856 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
2857 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
2858 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
2859 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
2860 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
2861 case LLVMAtomicOrderingAcquireRelease:
2862 return AtomicOrdering::AcquireRelease;
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002863 case LLVMAtomicOrderingSequentiallyConsistent:
JF Bastien800f87a2016-04-06 21:19:33 +00002864 return AtomicOrdering::SequentiallyConsistent;
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002865 }
Peter Zotov1d98e6d2014-10-28 19:46:44 +00002866
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002867 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2868}
2869
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00002870static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
2871 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +00002872 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
2873 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
2874 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
2875 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
2876 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
2877 case AtomicOrdering::AcquireRelease:
2878 return LLVMAtomicOrderingAcquireRelease;
2879 case AtomicOrdering::SequentiallyConsistent:
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00002880 return LLVMAtomicOrderingSequentiallyConsistent;
2881 }
2882
2883 llvm_unreachable("Invalid AtomicOrdering value!");
2884}
2885
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002886// TODO: Should this and other atomic instructions support building with
2887// "syncscope"?
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002888LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2889 LLVMBool isSingleThread, const char *Name) {
2890 return wrap(
2891 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002892 isSingleThread ? SyncScope::SingleThread
2893 : SyncScope::System,
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002894 Name));
2895}
2896
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002897LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2898 LLVMValueRef *Indices, unsigned NumIndices,
2899 const char *Name) {
Jay Foad040dd822011-07-22 08:16:57 +00002900 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
David Blaikie86ecb1b2015-03-15 01:03:19 +00002901 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002902}
2903
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002904LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2905 LLVMValueRef *Indices, unsigned NumIndices,
2906 const char *Name) {
Jay Foad040dd822011-07-22 08:16:57 +00002907 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
David Blaikieaa41cd52015-04-03 21:33:42 +00002908 return wrap(
2909 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002910}
2911
2912LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2913 unsigned Idx, const char *Name) {
David Blaikie64646022015-04-05 22:41:44 +00002914 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002915}
2916
2917LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2918 const char *Name) {
2919 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2920}
2921
2922LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2923 const char *Name) {
2924 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2925}
2926
Chris Lattner2cc6f9d2012-03-22 03:54:15 +00002927LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2928 Value *P = unwrap<Value>(MemAccessInst);
2929 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2930 return LI->isVolatile();
2931 return cast<StoreInst>(P)->isVolatile();
2932}
2933
2934void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2935 Value *P = unwrap<Value>(MemAccessInst);
2936 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2937 return LI->setVolatile(isVolatile);
2938 return cast<StoreInst>(P)->setVolatile(isVolatile);
2939}
2940
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00002941LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
2942 Value *P = unwrap<Value>(MemAccessInst);
2943 AtomicOrdering O;
2944 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2945 O = LI->getOrdering();
2946 else
2947 O = cast<StoreInst>(P)->getOrdering();
2948 return mapToLLVMOrdering(O);
2949}
2950
2951void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
2952 Value *P = unwrap<Value>(MemAccessInst);
2953 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
2954
2955 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2956 return LI->setOrdering(O);
2957 return cast<StoreInst>(P)->setOrdering(O);
2958}
2959
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002960/*--.. Casts ...............................................................--*/
2961
2962LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2963 LLVMTypeRef DestTy, const char *Name) {
2964 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2965}
2966
2967LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2968 LLVMTypeRef DestTy, const char *Name) {
2969 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2970}
2971
2972LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2973 LLVMTypeRef DestTy, const char *Name) {
2974 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2975}
2976
2977LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2978 LLVMTypeRef DestTy, const char *Name) {
2979 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2980}
2981
2982LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2983 LLVMTypeRef DestTy, const char *Name) {
2984 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2985}
2986
2987LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2988 LLVMTypeRef DestTy, const char *Name) {
2989 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2990}
2991
2992LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2993 LLVMTypeRef DestTy, const char *Name) {
2994 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2995}
2996
2997LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2998 LLVMTypeRef DestTy, const char *Name) {
2999 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
3000}
3001
3002LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
3003 LLVMTypeRef DestTy, const char *Name) {
3004 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
3005}
3006
3007LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
3008 LLVMTypeRef DestTy, const char *Name) {
3009 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
3010}
3011
3012LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
3013 LLVMTypeRef DestTy, const char *Name) {
3014 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
3015}
3016
3017LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3018 LLVMTypeRef DestTy, const char *Name) {
3019 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
3020}
3021
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003022LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
3023 LLVMTypeRef DestTy, const char *Name) {
3024 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
3025}
3026
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00003027LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3028 LLVMTypeRef DestTy, const char *Name) {
3029 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
3030 Name));
3031}
3032
3033LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3034 LLVMTypeRef DestTy, const char *Name) {
3035 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
3036 Name));
3037}
3038
3039LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3040 LLVMTypeRef DestTy, const char *Name) {
3041 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
3042 Name));
3043}
3044
Erick Tryzelaar31831792010-02-28 05:51:27 +00003045LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
3046 LLVMTypeRef DestTy, const char *Name) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00003047 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
Erick Tryzelaar31831792010-02-28 05:51:27 +00003048 unwrap(DestTy), Name));
3049}
3050
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00003051LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
3052 LLVMTypeRef DestTy, const char *Name) {
3053 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
3054}
3055
3056LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
Duncan Sands9d786d72009-11-23 10:49:03 +00003057 LLVMTypeRef DestTy, const char *Name) {
3058 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
3059 /*isSigned*/true, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00003060}
3061
3062LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
3063 LLVMTypeRef DestTy, const char *Name) {
3064 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
3065}
3066
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00003067/*--.. Comparisons .........................................................--*/
3068
3069LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
3070 LLVMValueRef LHS, LLVMValueRef RHS,
3071 const char *Name) {
3072 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
3073 unwrap(LHS), unwrap(RHS), Name));
3074}
3075
3076LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
3077 LLVMValueRef LHS, LLVMValueRef RHS,
3078 const char *Name) {
3079 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
3080 unwrap(LHS), unwrap(RHS), Name));
3081}
3082
3083/*--.. Miscellaneous instructions ..........................................--*/
3084
3085LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
Jay Foad52131342011-03-30 11:28:46 +00003086 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00003087}
3088
3089LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
3090 LLVMValueRef *Args, unsigned NumArgs,
3091 const char *Name) {
Jay Foad5bd375a2011-07-15 08:37:34 +00003092 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00003093 makeArrayRef(unwrap(Args), NumArgs),
Jay Foad5bd375a2011-07-15 08:37:34 +00003094 Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00003095}
3096
3097LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
3098 LLVMValueRef Then, LLVMValueRef Else,
3099 const char *Name) {
3100 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
3101 Name));
3102}
3103
3104LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
3105 LLVMTypeRef Ty, const char *Name) {
3106 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
3107}
3108
3109LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
3110 LLVMValueRef Index, const char *Name) {
3111 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
3112 Name));
3113}
3114
3115LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
3116 LLVMValueRef EltVal, LLVMValueRef Index,
3117 const char *Name) {
3118 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
3119 unwrap(Index), Name));
3120}
3121
3122LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
3123 LLVMValueRef V2, LLVMValueRef Mask,
3124 const char *Name) {
3125 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
3126 unwrap(Mask), Name));
3127}
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003128
Dan Gohmand5104a52008-11-03 22:55:43 +00003129LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3130 unsigned Index, const char *Name) {
3131 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
3132}
3133
3134LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3135 LLVMValueRef EltVal, unsigned Index,
3136 const char *Name) {
3137 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3138 Index, Name));
3139}
3140
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00003141LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
3142 const char *Name) {
3143 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
3144}
3145
3146LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
3147 const char *Name) {
3148 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3149}
3150
3151LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
3152 LLVMValueRef RHS, const char *Name) {
3153 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
3154}
3155
Andrew Trickdc073ad2013-09-18 23:31:10 +00003156LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
3157 LLVMValueRef PTR, LLVMValueRef Val,
3158 LLVMAtomicOrdering ordering,
Carlo Kok8c6719b2013-04-23 13:21:19 +00003159 LLVMBool singleThread) {
3160 AtomicRMWInst::BinOp intop;
3161 switch (op) {
3162 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
3163 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
3164 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
3165 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
3166 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
3167 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
3168 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
3169 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
3170 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
3171 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
3172 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
3173 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00003174 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003175 mapFromLLVMOrdering(ordering), singleThread ? SyncScope::SingleThread
3176 : SyncScope::System));
Carlo Kok8c6719b2013-04-23 13:21:19 +00003177}
3178
Mehdi Amini43165d92016-03-19 21:28:28 +00003179LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
3180 LLVMValueRef Cmp, LLVMValueRef New,
3181 LLVMAtomicOrdering SuccessOrdering,
3182 LLVMAtomicOrdering FailureOrdering,
3183 LLVMBool singleThread) {
3184
3185 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
3186 unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
3187 mapFromLLVMOrdering(FailureOrdering),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003188 singleThread ? SyncScope::SingleThread : SyncScope::System));
Mehdi Amini43165d92016-03-19 21:28:28 +00003189}
3190
3191
3192LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
3193 Value *P = unwrap<Value>(AtomicInst);
3194
3195 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003196 return I->getSyncScopeID() == SyncScope::SingleThread;
3197 return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() ==
3198 SyncScope::SingleThread;
Mehdi Amini43165d92016-03-19 21:28:28 +00003199}
3200
3201void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
3202 Value *P = unwrap<Value>(AtomicInst);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003203 SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
Mehdi Amini43165d92016-03-19 21:28:28 +00003204
3205 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003206 return I->setSyncScopeID(SSID);
3207 return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID);
Mehdi Amini43165d92016-03-19 21:28:28 +00003208}
3209
3210LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
3211 Value *P = unwrap<Value>(CmpXchgInst);
3212 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
3213}
3214
3215void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
3216 LLVMAtomicOrdering Ordering) {
3217 Value *P = unwrap<Value>(CmpXchgInst);
3218 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3219
3220 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
3221}
3222
3223LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
3224 Value *P = unwrap<Value>(CmpXchgInst);
3225 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
3226}
3227
3228void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
3229 LLVMAtomicOrdering Ordering) {
3230 Value *P = unwrap<Value>(CmpXchgInst);
3231 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3232
3233 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
3234}
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003235
3236/*===-- Module providers --------------------------------------------------===*/
3237
3238LLVMModuleProviderRef
3239LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003240 return reinterpret_cast<LLVMModuleProviderRef>(M);
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003241}
3242
3243void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
3244 delete unwrap(MP);
3245}
3246
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003247
3248/*===-- Memory buffers ----------------------------------------------------===*/
3249
Chris Lattner25963c62010-01-09 22:27:07 +00003250LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
3251 const char *Path,
3252 LLVMMemoryBufferRef *OutMemBuf,
3253 char **OutMessage) {
3254
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003255 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
3256 if (std::error_code EC = MBOrErr.getError()) {
3257 *OutMessage = strdup(EC.message().c_str());
3258 return 1;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003259 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003260 *OutMemBuf = wrap(MBOrErr.get().release());
3261 return 0;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003262}
3263
Chris Lattner25963c62010-01-09 22:27:07 +00003264LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
3265 char **OutMessage) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003266 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
3267 if (std::error_code EC = MBOrErr.getError()) {
3268 *OutMessage = strdup(EC.message().c_str());
3269 return 1;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003270 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003271 *OutMemBuf = wrap(MBOrErr.get().release());
3272 return 0;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003273}
3274
Bill Wendling526276a2013-02-14 19:11:28 +00003275LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
3276 const char *InputData,
3277 size_t InputDataLength,
3278 const char *BufferName,
Bill Wendlingff61da92013-02-14 19:40:27 +00003279 LLVMBool RequiresNullTerminator) {
Bill Wendling526276a2013-02-14 19:11:28 +00003280
Rafael Espindola3560ff22014-08-27 20:03:13 +00003281 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
3282 StringRef(BufferName),
3283 RequiresNullTerminator).release());
Bill Wendling526276a2013-02-14 19:11:28 +00003284}
3285
3286LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
3287 const char *InputData,
3288 size_t InputDataLength,
3289 const char *BufferName) {
3290
Rafael Espindola3560ff22014-08-27 20:03:13 +00003291 return wrap(
3292 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
3293 StringRef(BufferName)).release());
Bill Wendling526276a2013-02-14 19:11:28 +00003294}
3295
Tom Stellard62c03202013-04-18 19:50:53 +00003296const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
Tom Stellard385fa262013-04-16 23:12:47 +00003297 return unwrap(MemBuf)->getBufferStart();
3298}
Bill Wendling526276a2013-02-14 19:11:28 +00003299
Tom Stellardb7fb7242013-04-16 23:12:51 +00003300size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
3301 return unwrap(MemBuf)->getBufferSize();
3302}
3303
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003304void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
3305 delete unwrap(MemBuf);
3306}
Dan Gohman093b42f2010-08-07 00:43:20 +00003307
Owen Anderson4698c5d2010-10-07 17:55:47 +00003308/*===-- Pass Registry -----------------------------------------------------===*/
3309
3310LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
3311 return wrap(PassRegistry::getPassRegistry());
3312}
Dan Gohman093b42f2010-08-07 00:43:20 +00003313
3314/*===-- Pass Manager ------------------------------------------------------===*/
3315
3316LLVMPassManagerRef LLVMCreatePassManager() {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003317 return wrap(new legacy::PassManager());
Dan Gohman093b42f2010-08-07 00:43:20 +00003318}
3319
3320LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003321 return wrap(new legacy::FunctionPassManager(unwrap(M)));
Dan Gohman093b42f2010-08-07 00:43:20 +00003322}
3323
3324LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
3325 return LLVMCreateFunctionPassManagerForModule(
3326 reinterpret_cast<LLVMModuleRef>(P));
3327}
3328
3329LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003330 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
Dan Gohman093b42f2010-08-07 00:43:20 +00003331}
3332
3333LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003334 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
Dan Gohman093b42f2010-08-07 00:43:20 +00003335}
3336
3337LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003338 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
Dan Gohman093b42f2010-08-07 00:43:20 +00003339}
3340
3341LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003342 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
Dan Gohman093b42f2010-08-07 00:43:20 +00003343}
3344
3345void LLVMDisposePassManager(LLVMPassManagerRef PM) {
3346 delete unwrap(PM);
3347}
Duncan Sands1cba0a82013-02-17 16:35:51 +00003348
3349/*===-- Threading ------------------------------------------------------===*/
3350
3351LLVMBool LLVMStartMultithreaded() {
Chandler Carruth39cd2162014-06-27 15:13:01 +00003352 return LLVMIsMultithreaded();
Duncan Sands1cba0a82013-02-17 16:35:51 +00003353}
3354
3355void LLVMStopMultithreaded() {
Duncan Sands1cba0a82013-02-17 16:35:51 +00003356}
3357
3358LLVMBool LLVMIsMultithreaded() {
3359 return llvm_is_multithreaded();
3360}