blob: 54c73b01acf4dd94d2a43c683095b71d9e7c7abf [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
237
Gordon Henriksen05568bb2007-12-27 20:13:47 +0000238/*--.. Data layout .........................................................--*/
Amaury Sechetf3549c42016-02-16 00:23:52 +0000239const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000240 return unwrap(M)->getDataLayoutStr().c_str();
Gordon Henriksen05568bb2007-12-27 20:13:47 +0000241}
242
Amaury Sechetf3549c42016-02-16 00:23:52 +0000243const char *LLVMGetDataLayout(LLVMModuleRef M) {
244 return LLVMGetDataLayoutStr(M);
245}
246
Amaury Sechet6ada31c2016-02-15 23:40:06 +0000247void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
248 unwrap(M)->setDataLayout(DataLayoutStr);
Gordon Henriksen05568bb2007-12-27 20:13:47 +0000249}
250
251/*--.. Target triple .......................................................--*/
252const char * LLVMGetTarget(LLVMModuleRef M) {
253 return unwrap(M)->getTargetTriple().c_str();
254}
255
256void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
257 unwrap(M)->setTargetTriple(Triple);
258}
259
Matthias Braunde58b612017-01-29 17:52:03 +0000260void LLVMDumpModule(LLVMModuleRef M) {
261 unwrap(M)->print(errs(), nullptr,
262 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
Gordon Henriksen6c6075e2008-03-14 23:58:56 +0000263}
264
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000265LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
266 char **ErrorMessage) {
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000267 std::error_code EC;
268 raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
269 if (EC) {
270 *ErrorMessage = strdup(EC.message().c_str());
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000271 return true;
272 }
273
Craig Topperc6207612014-04-09 06:08:46 +0000274 unwrap(M)->print(dest, nullptr);
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000275
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000276 dest.close();
277
278 if (dest.has_error()) {
Bob Haarman9ce2d032017-10-24 01:26:22 +0000279 std::string E = "Error printing to file: " + dest.error().message();
280 *ErrorMessage = strdup(E.c_str());
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000281 return true;
282 }
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000283
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000284 return false;
285}
286
Anders Waldenborg84355db2013-10-16 18:00:54 +0000287char *LLVMPrintModuleToString(LLVMModuleRef M) {
Alp Tokere69170a2014-06-26 22:52:05 +0000288 std::string buf;
289 raw_string_ostream os(buf);
290
Craig Topperc6207612014-04-09 06:08:46 +0000291 unwrap(M)->print(os, nullptr);
Alp Tokere69170a2014-06-26 22:52:05 +0000292 os.flush();
293
294 return strdup(buf.c_str());
Anders Waldenborg84355db2013-10-16 18:00:54 +0000295}
296
Chris Lattner26941452010-04-10 17:52:58 +0000297/*--.. Operations on inline assembler ......................................--*/
298void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
299 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
300}
301
Gordon Henriksen76a03742007-09-18 03:18:57 +0000302
Chris Lattnera7e04b02010-11-28 20:03:44 +0000303/*--.. Operations on module contexts ......................................--*/
304LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
305 return wrap(&unwrap(M)->getContext());
306}
307
308
Gordon Henriksen76a03742007-09-18 03:18:57 +0000309/*===-- Operations on types -----------------------------------------------===*/
310
311/*--.. Operations on all types (mostly) ....................................--*/
312
313LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000314 switch (unwrap(Ty)->getTypeID()) {
315 case Type::VoidTyID:
316 return LLVMVoidTypeKind;
Dan Gohman518cda42011-12-17 00:04:22 +0000317 case Type::HalfTyID:
318 return LLVMHalfTypeKind;
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000319 case Type::FloatTyID:
320 return LLVMFloatTypeKind;
321 case Type::DoubleTyID:
322 return LLVMDoubleTypeKind;
323 case Type::X86_FP80TyID:
324 return LLVMX86_FP80TypeKind;
325 case Type::FP128TyID:
326 return LLVMFP128TypeKind;
327 case Type::PPC_FP128TyID:
328 return LLVMPPC_FP128TypeKind;
329 case Type::LabelTyID:
330 return LLVMLabelTypeKind;
331 case Type::MetadataTyID:
332 return LLVMMetadataTypeKind;
333 case Type::IntegerTyID:
334 return LLVMIntegerTypeKind;
335 case Type::FunctionTyID:
336 return LLVMFunctionTypeKind;
337 case Type::StructTyID:
338 return LLVMStructTypeKind;
339 case Type::ArrayTyID:
340 return LLVMArrayTypeKind;
341 case Type::PointerTyID:
342 return LLVMPointerTypeKind;
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000343 case Type::VectorTyID:
344 return LLVMVectorTypeKind;
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000345 case Type::X86_MMXTyID:
346 return LLVMX86_MMXTypeKind;
David Majnemerb611e3f2015-08-14 05:09:07 +0000347 case Type::TokenTyID:
348 return LLVMTokenTypeKind;
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000349 }
Rafael Espindolaba7df702013-12-07 02:27:52 +0000350 llvm_unreachable("Unhandled TypeID.");
Gordon Henriksen76a03742007-09-18 03:18:57 +0000351}
352
Torok Edwin1cd9ade2011-10-06 12:13:28 +0000353LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
354{
355 return unwrap(Ty)->isSized();
356}
357
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000358LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
359 return wrap(&unwrap(Ty)->getContext());
360}
361
Aaron Ballman615eb472017-10-15 14:32:27 +0000362#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun8c209aa2017-01-28 02:02:38 +0000363LLVM_DUMP_METHOD void LLVMDumpType(LLVMTypeRef Ty) {
Anders Waldenborgb822cff2013-10-16 21:30:25 +0000364 return unwrap(Ty)->dump();
365}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000366#endif
Anders Waldenborgb822cff2013-10-16 21:30:25 +0000367
Anders Waldenborg47b3bd32013-10-22 06:58:34 +0000368char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
Alp Tokere69170a2014-06-26 22:52:05 +0000369 std::string buf;
370 raw_string_ostream os(buf);
Anders Waldenborg47b3bd32013-10-22 06:58:34 +0000371
Richard Trieuc1485222014-06-21 02:43:02 +0000372 if (unwrap(Ty))
373 unwrap(Ty)->print(os);
374 else
375 os << "Printing <null> Type";
376
Alp Tokere69170a2014-06-26 22:52:05 +0000377 os.flush();
378
379 return strdup(buf.c_str());
Anders Waldenborg47b3bd32013-10-22 06:58:34 +0000380}
381
Gordon Henriksen76a03742007-09-18 03:18:57 +0000382/*--.. Operations on integer types .........................................--*/
383
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000384LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
385 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000386}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000387LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
388 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000389}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000390LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
391 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000392}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000393LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
394 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000395}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000396LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
397 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
398}
Kit Barton72918022015-04-17 15:32:15 +0000399LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
400 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
401}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000402LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
403 return wrap(IntegerType::get(*unwrap(C), NumBits));
Owen Anderson55f1c092009-08-13 21:58:54 +0000404}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000405
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000406LLVMTypeRef LLVMInt1Type(void) {
407 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
408}
409LLVMTypeRef LLVMInt8Type(void) {
410 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
411}
412LLVMTypeRef LLVMInt16Type(void) {
413 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
414}
415LLVMTypeRef LLVMInt32Type(void) {
416 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
417}
418LLVMTypeRef LLVMInt64Type(void) {
419 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
420}
Kit Barton72918022015-04-17 15:32:15 +0000421LLVMTypeRef LLVMInt128Type(void) {
422 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
423}
Gordon Henriksened7beaa2007-10-06 16:05:20 +0000424LLVMTypeRef LLVMIntType(unsigned NumBits) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000425 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
Gordon Henriksen76a03742007-09-18 03:18:57 +0000426}
427
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000428unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000429 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
430}
431
432/*--.. Operations on real types ............................................--*/
433
Dan Gohman518cda42011-12-17 00:04:22 +0000434LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
435 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
436}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000437LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
438 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
439}
440LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
441 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
442}
443LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
444 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
445}
446LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
447 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
448}
449LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
450 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
451}
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000452LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
453 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
454}
David Majnemerb611e3f2015-08-14 05:09:07 +0000455LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
456 return (LLVMTypeRef) Type::getTokenTy(*unwrap(C));
457}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000458
Dan Gohman518cda42011-12-17 00:04:22 +0000459LLVMTypeRef LLVMHalfType(void) {
460 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
461}
Owen Anderson55f1c092009-08-13 21:58:54 +0000462LLVMTypeRef LLVMFloatType(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000463 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000464}
465LLVMTypeRef LLVMDoubleType(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000466 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000467}
468LLVMTypeRef LLVMX86FP80Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000469 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000470}
471LLVMTypeRef LLVMFP128Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000472 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000473}
474LLVMTypeRef LLVMPPCFP128Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000475 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000476}
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000477LLVMTypeRef LLVMX86MMXType(void) {
478 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
479}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000480
481/*--.. Operations on function types ........................................--*/
482
Gordon Henriksened7beaa2007-10-06 16:05:20 +0000483LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
484 LLVMTypeRef *ParamTypes, unsigned ParamCount,
Chris Lattner25963c62010-01-09 22:27:07 +0000485 LLVMBool IsVarArg) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000486 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
Owen Anderson4056ca92009-07-29 22:17:13 +0000487 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000488}
489
Chris Lattner25963c62010-01-09 22:27:07 +0000490LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000491 return unwrap<FunctionType>(FunctionTy)->isVarArg();
492}
493
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000494LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000495 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
496}
497
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000498unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000499 return unwrap<FunctionType>(FunctionTy)->getNumParams();
500}
501
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000502void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000503 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
504 for (FunctionType::param_iterator I = Ty->param_begin(),
505 E = Ty->param_end(); I != E; ++I)
506 *Dest++ = wrap(*I);
507}
508
509/*--.. Operations on struct types ..........................................--*/
510
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000511LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
Chris Lattner25963c62010-01-09 22:27:07 +0000512 unsigned ElementCount, LLVMBool Packed) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000513 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000514 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000515}
516
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000517LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
Chris Lattner25963c62010-01-09 22:27:07 +0000518 unsigned ElementCount, LLVMBool Packed) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000519 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
520 ElementCount, Packed);
521}
522
Chris Lattnere71ccde2011-07-14 05:53:17 +0000523LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
524{
Chris Lattner01beceb2011-08-12 18:07:07 +0000525 return wrap(StructType::create(*unwrap(C), Name));
Chris Lattnere71ccde2011-07-14 05:53:17 +0000526}
527
Torok Edwin0d5f6ae2011-10-06 12:12:50 +0000528const char *LLVMGetStructName(LLVMTypeRef Ty)
529{
Torok Edwin2e9affe2011-10-14 20:37:42 +0000530 StructType *Type = unwrap<StructType>(Ty);
531 if (!Type->hasName())
Craig Topperc6207612014-04-09 06:08:46 +0000532 return nullptr;
Torok Edwin2e9affe2011-10-14 20:37:42 +0000533 return Type->getName().data();
Torok Edwin0d5f6ae2011-10-06 12:12:50 +0000534}
535
Chris Lattnere71ccde2011-07-14 05:53:17 +0000536void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
537 unsigned ElementCount, LLVMBool Packed) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000538 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
Chris Lattnere71ccde2011-07-14 05:53:17 +0000539 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
540}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000541
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000542unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000543 return unwrap<StructType>(StructTy)->getNumElements();
544}
545
546void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
547 StructType *Ty = unwrap<StructType>(StructTy);
Nick Lewyckyf735b7b2011-04-20 22:52:37 +0000548 for (StructType::element_iterator I = Ty->element_begin(),
Gordon Henriksen76a03742007-09-18 03:18:57 +0000549 E = Ty->element_end(); I != E; ++I)
550 *Dest++ = wrap(*I);
551}
552
Peter Zotovc164a3f2015-06-04 09:09:53 +0000553LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
554 StructType *Ty = unwrap<StructType>(StructTy);
555 return wrap(Ty->getTypeAtIndex(i));
556}
557
Chris Lattner25963c62010-01-09 22:27:07 +0000558LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000559 return unwrap<StructType>(StructTy)->isPacked();
560}
561
Chris Lattner17cf05b2011-07-14 16:20:28 +0000562LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
563 return unwrap<StructType>(StructTy)->isOpaque();
564}
565
566LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
567 return wrap(unwrap(M)->getTypeByName(Name));
568}
569
Gordon Henriksen76a03742007-09-18 03:18:57 +0000570/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
571
whitequarkf6059fd2017-06-05 11:49:52 +0000572void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
573 int i = 0;
574 for (auto *T : unwrap(Tp)->subtypes()) {
575 Arr[i] = wrap(T);
576 i++;
577 }
578}
579
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000580LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000581 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000582}
583
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000584LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000585 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000586}
587
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000588LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000589 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000590}
591
Peter Collingbourne45681582016-12-02 03:05:41 +0000592LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
593 auto *Ty = unwrap<Type>(WrappedTy);
594 if (auto *PTy = dyn_cast<PointerType>(Ty))
595 return wrap(PTy->getElementType());
596 return wrap(cast<SequentialType>(Ty)->getElementType());
Gordon Henriksen76a03742007-09-18 03:18:57 +0000597}
598
whitequarkf6059fd2017-06-05 11:49:52 +0000599unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
600 return unwrap(Tp)->getNumContainedTypes();
601}
602
Gordon Henriksen76a03742007-09-18 03:18:57 +0000603unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
604 return unwrap<ArrayType>(ArrayTy)->getNumElements();
605}
606
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000607unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
608 return unwrap<PointerType>(PointerTy)->getAddressSpace();
609}
610
Gordon Henriksen76a03742007-09-18 03:18:57 +0000611unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
612 return unwrap<VectorType>(VectorTy)->getNumElements();
613}
614
615/*--.. Operations on other types ...........................................--*/
616
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000617LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
618 return wrap(Type::getVoidTy(*unwrap(C)));
Owen Anderson55f1c092009-08-13 21:58:54 +0000619}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000620LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
621 return wrap(Type::getLabelTy(*unwrap(C)));
622}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000623
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000624LLVMTypeRef LLVMVoidType(void) {
625 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
626}
627LLVMTypeRef LLVMLabelType(void) {
628 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
629}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000630
631/*===-- Operations on values ----------------------------------------------===*/
632
633/*--.. Operations on all values ............................................--*/
634
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000635LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000636 return wrap(unwrap(Val)->getType());
637}
638
Peter Zotov3e4561c2016-04-06 22:21:29 +0000639LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
640 switch(unwrap(Val)->getValueID()) {
641#define HANDLE_VALUE(Name) \
642 case Value::Name##Val: \
643 return LLVM##Name##ValueKind;
644#include "llvm/IR/Value.def"
645 default:
646 return LLVMInstructionValueKind;
647 }
648}
649
Gordon Henriksen76a03742007-09-18 03:18:57 +0000650const char *LLVMGetValueName(LLVMValueRef Val) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000651 return unwrap(Val)->getName().data();
Gordon Henriksen76a03742007-09-18 03:18:57 +0000652}
653
654void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
655 unwrap(Val)->setName(Name);
656}
657
Matthias Braun8c209aa2017-01-28 02:02:38 +0000658LLVM_DUMP_METHOD void LLVMDumpValue(LLVMValueRef Val) {
Sam McCalla682dfb2017-01-30 05:40:52 +0000659 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
Gordon Henriksen1d0d24c2007-10-06 00:08:49 +0000660}
661
Peter Zotovcd93b372013-11-06 09:21:01 +0000662char* LLVMPrintValueToString(LLVMValueRef Val) {
Alp Tokere69170a2014-06-26 22:52:05 +0000663 std::string buf;
664 raw_string_ostream os(buf);
Peter Zotovcd93b372013-11-06 09:21:01 +0000665
Richard Trieuc1485222014-06-21 02:43:02 +0000666 if (unwrap(Val))
667 unwrap(Val)->print(os);
668 else
669 os << "Printing <null> Value";
670
Alp Tokere69170a2014-06-26 22:52:05 +0000671 os.flush();
672
673 return strdup(buf.c_str());
Peter Zotovcd93b372013-11-06 09:21:01 +0000674}
675
Chris Lattner40cf28d2009-10-12 04:01:02 +0000676void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
677 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
678}
Gordon Henriksen29e38942008-12-19 18:39:45 +0000679
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000680int LLVMHasMetadata(LLVMValueRef Inst) {
681 return unwrap<Instruction>(Inst)->hasMetadata();
682}
683
684LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000685 auto *I = unwrap<Instruction>(Inst);
686 assert(I && "Expected instruction");
687 if (auto *MD = I->getMetadata(KindID))
688 return wrap(MetadataAsValue::get(I->getContext(), MD));
689 return nullptr;
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000690}
691
Bjorn Steinbrinka09ac002015-01-28 16:35:59 +0000692// MetadataAsValue uses a canonical format which strips the actual MDNode for
693// MDNode with just a single constant value, storing just a ConstantAsMetadata
694// This undoes this canonicalization, reconstructing the MDNode.
695static MDNode *extractMDNode(MetadataAsValue *MAV) {
696 Metadata *MD = MAV->getMetadata();
697 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
698 "Expected a metadata node or a canonicalized constant");
699
700 if (MDNode *N = dyn_cast<MDNode>(MD))
701 return N;
702
703 return MDNode::get(MAV->getContext(), MD);
704}
705
706void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
707 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
708
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000709 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000710}
711
Gordon Henriksen29e38942008-12-19 18:39:45 +0000712/*--.. Conversion functions ................................................--*/
713
714#define LLVM_DEFINE_VALUE_CAST(name) \
715 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
716 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
717 }
718
719LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
720
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000721LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
722 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
723 if (isa<MDNode>(MD->getMetadata()) ||
724 isa<ValueAsMetadata>(MD->getMetadata()))
725 return Val;
726 return nullptr;
727}
728
729LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
730 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
731 if (isa<MDString>(MD->getMetadata()))
732 return Val;
733 return nullptr;
734}
735
Chris Lattner40cf28d2009-10-12 04:01:02 +0000736/*--.. Operations on Uses ..................................................--*/
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000737LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
Chris Lattner40cf28d2009-10-12 04:01:02 +0000738 Value *V = unwrap(Val);
739 Value::use_iterator I = V->use_begin();
740 if (I == V->use_end())
Craig Topperc6207612014-04-09 06:08:46 +0000741 return nullptr;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000742 return wrap(&*I);
Chris Lattner40cf28d2009-10-12 04:01:02 +0000743}
744
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000745LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
746 Use *Next = unwrap(U)->getNext();
747 if (Next)
748 return wrap(Next);
Craig Topperc6207612014-04-09 06:08:46 +0000749 return nullptr;
Chris Lattner40cf28d2009-10-12 04:01:02 +0000750}
751
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000752LLVMValueRef LLVMGetUser(LLVMUseRef U) {
753 return wrap(unwrap(U)->getUser());
Chris Lattner40cf28d2009-10-12 04:01:02 +0000754}
755
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000756LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
757 return wrap(unwrap(U)->get());
Chris Lattner40cf28d2009-10-12 04:01:02 +0000758}
759
760/*--.. Operations on Users .................................................--*/
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000761
762static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
763 unsigned Index) {
764 Metadata *Op = N->getOperand(Index);
765 if (!Op)
766 return nullptr;
767 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
768 return wrap(C->getValue());
769 return wrap(MetadataAsValue::get(Context, Op));
770}
771
Chris Lattner40cf28d2009-10-12 04:01:02 +0000772LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
Torok Edwinfec812e2011-10-06 12:13:11 +0000773 Value *V = unwrap(Val);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000774 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
775 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
776 assert(Index == 0 && "Function-local metadata can only have one operand");
777 return wrap(L->getValue());
778 }
779 return getMDNodeOperandImpl(V->getContext(),
780 cast<MDNode>(MD->getMetadata()), Index);
781 }
782
Torok Edwinfec812e2011-10-06 12:13:11 +0000783 return wrap(cast<User>(V)->getOperand(Index));
Chris Lattner40cf28d2009-10-12 04:01:02 +0000784}
Gordon Henriksen29e38942008-12-19 18:39:45 +0000785
Peter Zotovb19f78f2014-08-12 02:55:40 +0000786LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
787 Value *V = unwrap(Val);
788 return wrap(&cast<User>(V)->getOperandUse(Index));
789}
790
Erick Tryzelaarb4d48702010-08-20 14:51:22 +0000791void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
792 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
793}
794
795int LLVMGetNumOperands(LLVMValueRef Val) {
Torok Edwinfec812e2011-10-06 12:13:11 +0000796 Value *V = unwrap(Val);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000797 if (isa<MetadataAsValue>(V))
798 return LLVMGetMDNodeNumOperands(Val);
799
Torok Edwinfec812e2011-10-06 12:13:11 +0000800 return cast<User>(V)->getNumOperands();
Erick Tryzelaarb4d48702010-08-20 14:51:22 +0000801}
802
Gordon Henriksen76a03742007-09-18 03:18:57 +0000803/*--.. Operations on constants of any type .................................--*/
804
Gordon Henriksen1046c732007-10-06 15:11:06 +0000805LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000806 return wrap(Constant::getNullValue(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000807}
808
Gordon Henriksen1046c732007-10-06 15:11:06 +0000809LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000810 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000811}
812
813LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
Owen Andersonb292b8c2009-07-30 23:03:37 +0000814 return wrap(UndefValue::get(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000815}
816
Chris Lattner25963c62010-01-09 22:27:07 +0000817LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
Gordon Henriksendc88c062007-09-18 18:07:51 +0000818 return isa<Constant>(unwrap(Ty));
819}
820
Chris Lattner25963c62010-01-09 22:27:07 +0000821LLVMBool LLVMIsNull(LLVMValueRef Val) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000822 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
823 return C->isNullValue();
824 return false;
825}
826
Chris Lattner25963c62010-01-09 22:27:07 +0000827LLVMBool LLVMIsUndef(LLVMValueRef Val) {
Gordon Henriksendc88c062007-09-18 18:07:51 +0000828 return isa<UndefValue>(unwrap(Val));
829}
830
Chris Lattner7f318242009-07-06 17:29:59 +0000831LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
Amaury Sechet9bbda192016-04-25 22:23:35 +0000832 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
Chris Lattner7f318242009-07-06 17:29:59 +0000833}
834
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000835/*--.. Operations on metadata nodes ........................................--*/
836
837LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
838 unsigned SLen) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000839 LLVMContext &Context = *unwrap(C);
840 return wrap(MetadataAsValue::get(
841 Context, MDString::get(Context, StringRef(Str, SLen))));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000842}
843
844LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
845 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
846}
847
848LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
849 unsigned Count) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000850 LLVMContext &Context = *unwrap(C);
851 SmallVector<Metadata *, 8> MDs;
852 for (auto *OV : makeArrayRef(Vals, Count)) {
853 Value *V = unwrap(OV);
854 Metadata *MD;
855 if (!V)
856 MD = nullptr;
857 else if (auto *C = dyn_cast<Constant>(V))
858 MD = ConstantAsMetadata::get(C);
859 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
860 MD = MDV->getMetadata();
861 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
862 "outside of direct argument to call");
863 } else {
864 // This is function-local metadata. Pretend to make an MDNode.
865 assert(Count == 1 &&
866 "Expected only one operand to function-local metadata");
867 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
868 }
869
870 MDs.push_back(MD);
871 }
872 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000873}
874
875LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
876 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
877}
878
Amaury Sechetf8429752017-04-17 11:52:54 +0000879LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
880 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
881}
882
883LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {
884 auto *V = unwrap(Val);
885 if (auto *C = dyn_cast<Constant>(V))
886 return wrap(ConstantAsMetadata::get(C));
887 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
888 return wrap(MAV->getMetadata());
889 return wrap(ValueAsMetadata::get(V));
890}
891
Amaury Sechet7c2883c2016-04-03 21:06:04 +0000892const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000893 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
894 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
Amaury Sechet7c2883c2016-04-03 21:06:04 +0000895 *Length = S->getString().size();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000896 return S->getString().data();
897 }
Amaury Sechet7c2883c2016-04-03 21:06:04 +0000898 *Length = 0;
Craig Topperc6207612014-04-09 06:08:46 +0000899 return nullptr;
Torok Edwinfec812e2011-10-06 12:13:11 +0000900}
901
Amaury Sechetb130f432016-04-23 00:12:45 +0000902unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000903 auto *MD = cast<MetadataAsValue>(unwrap(V));
904 if (isa<ValueAsMetadata>(MD->getMetadata()))
905 return 1;
906 return cast<MDNode>(MD->getMetadata())->getNumOperands();
Duncan Sands12ccbe72012-09-19 20:29:39 +0000907}
908
Amaury Sechetb130f432016-04-23 00:12:45 +0000909void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000910 auto *MD = cast<MetadataAsValue>(unwrap(V));
911 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
912 *Dest = wrap(MDV->getValue());
913 return;
914 }
915 const auto *N = cast<MDNode>(MD->getMetadata());
Duncan Sands12ccbe72012-09-19 20:29:39 +0000916 const unsigned numOperands = N->getNumOperands();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000917 LLVMContext &Context = unwrap(V)->getContext();
Duncan Sands12ccbe72012-09-19 20:29:39 +0000918 for (unsigned i = 0; i < numOperands; i++)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000919 Dest[i] = getMDNodeOperandImpl(Context, N, i);
Duncan Sands12ccbe72012-09-19 20:29:39 +0000920}
921
Amaury Sechetb130f432016-04-23 00:12:45 +0000922unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
923 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
Torok Edwin2e9affe2011-10-14 20:37:42 +0000924 return N->getNumOperands();
925 }
926 return 0;
Torok Edwinfec812e2011-10-06 12:13:11 +0000927}
928
Amaury Sechetb130f432016-04-23 00:12:45 +0000929void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
930 LLVMValueRef *Dest) {
931 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
Torok Edwin2e9affe2011-10-14 20:37:42 +0000932 if (!N)
933 return;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000934 LLVMContext &Context = unwrap(M)->getContext();
Torok Edwin2e9affe2011-10-14 20:37:42 +0000935 for (unsigned i=0;i<N->getNumOperands();i++)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000936 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
Torok Edwinfec812e2011-10-06 12:13:11 +0000937}
938
Amaury Sechetb130f432016-04-23 00:12:45 +0000939void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
940 LLVMValueRef Val) {
941 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
Devang Patel92245402011-12-20 19:29:36 +0000942 if (!N)
943 return;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000944 if (!Val)
945 return;
Bjorn Steinbrinka09ac002015-01-28 16:35:59 +0000946 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
Devang Patel92245402011-12-20 19:29:36 +0000947}
948
Gordon Henriksen76a03742007-09-18 03:18:57 +0000949/*--.. Operations on scalar constants ......................................--*/
950
Gordon Henriksen1046c732007-10-06 15:11:06 +0000951LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
Chris Lattner25963c62010-01-09 22:27:07 +0000952 LLVMBool SignExtend) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000953 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000954}
955
Chris Lattner4329e072010-11-23 02:47:22 +0000956LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
957 unsigned NumWords,
958 const uint64_t Words[]) {
959 IntegerType *Ty = unwrap<IntegerType>(IntTy);
960 return wrap(ConstantInt::get(Ty->getContext(),
Jeffrey Yasskin7a162882011-07-18 21:45:40 +0000961 APInt(Ty->getBitWidth(),
962 makeArrayRef(Words, NumWords))));
Chris Lattner4329e072010-11-23 02:47:22 +0000963}
964
Erick Tryzelaardd991352009-08-16 23:36:46 +0000965LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
966 uint8_t Radix) {
967 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
968 Radix));
969}
970
971LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
972 unsigned SLen, uint8_t Radix) {
973 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
974 Radix));
Gordon Henriksen931e1212008-02-02 01:07:50 +0000975}
976
Gordon Henriksen1046c732007-10-06 15:11:06 +0000977LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
Erick Tryzelaardd991352009-08-16 23:36:46 +0000978 return wrap(ConstantFP::get(unwrap(RealTy), N));
Gordon Henriksen931e1212008-02-02 01:07:50 +0000979}
980
981LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
Erick Tryzelaardd991352009-08-16 23:36:46 +0000982 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
983}
984
985LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
986 unsigned SLen) {
987 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000988}
989
Chris Lattner40cf28d2009-10-12 04:01:02 +0000990unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
991 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
992}
993
994long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
995 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
996}
997
Peter Zotov1d98e6d2014-10-28 19:46:44 +0000998double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
999 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1000 Type *Ty = cFP->getType();
1001
1002 if (Ty->isFloatTy()) {
1003 *LosesInfo = false;
1004 return cFP->getValueAPF().convertToFloat();
1005 }
1006
1007 if (Ty->isDoubleTy()) {
1008 *LosesInfo = false;
1009 return cFP->getValueAPF().convertToDouble();
1010 }
1011
1012 bool APFLosesInfo;
1013 APFloat APF = cFP->getValueAPF();
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001014 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
Peter Zotov1d98e6d2014-10-28 19:46:44 +00001015 *LosesInfo = APFLosesInfo;
1016 return APF.convertToDouble();
1017}
1018
Gordon Henriksen76a03742007-09-18 03:18:57 +00001019/*--.. Operations on composite constants ...................................--*/
1020
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001021LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
Chris Lattner25963c62010-01-09 22:27:07 +00001022 unsigned Length,
1023 LLVMBool DontNullTerminate) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001024 /* Inverted the sense of AddNull because ', 0)' is a
1025 better mnemonic for null termination than ', 1)'. */
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001026 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1027 DontNullTerminate == 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001028}
Amaury Sechet006ce632016-03-13 00:54:40 +00001029
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001030LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
Chris Lattner25963c62010-01-09 22:27:07 +00001031 LLVMBool DontNullTerminate) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001032 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1033 DontNullTerminate);
1034}
Peter Zotovf9aa8822014-08-03 23:54:16 +00001035
Amaury Sechet006ce632016-03-13 00:54:40 +00001036LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1037 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
Peter Zotovf9aa8822014-08-03 23:54:16 +00001038}
1039
Amaury Sechet006ce632016-03-13 00:54:40 +00001040LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1041 return unwrap<ConstantDataSequential>(C)->isString();
Peter Zotovf9aa8822014-08-03 23:54:16 +00001042}
1043
Amaury Sechet7c2883c2016-04-03 21:06:04 +00001044const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1045 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1046 *Length = Str.size();
1047 return Str.data();
Peter Zotovf9aa8822014-08-03 23:54:16 +00001048}
1049
Gordon Henriksen1046c732007-10-06 15:11:06 +00001050LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1051 LLVMValueRef *ConstantVals, unsigned Length) {
Jay Foad83be3612011-06-22 09:24:39 +00001052 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1053 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001054}
Peter Zotovf9aa8822014-08-03 23:54:16 +00001055
Amaury Sechetc78768f2016-03-13 00:40:12 +00001056LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1057 LLVMValueRef *ConstantVals,
1058 unsigned Count, LLVMBool Packed) {
1059 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1060 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1061 Packed != 0));
1062}
1063
Gordon Henriksen1046c732007-10-06 15:11:06 +00001064LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
Chris Lattner25963c62010-01-09 22:27:07 +00001065 LLVMBool Packed) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001066 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1067 Packed);
Gordon Henriksen76a03742007-09-18 03:18:57 +00001068}
Rafael Espindola784ad242011-07-14 19:09:08 +00001069
1070LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1071 LLVMValueRef *ConstantVals,
1072 unsigned Count) {
1073 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
Chris Lattner229907c2011-07-18 04:54:35 +00001074 StructType *Ty = cast<StructType>(unwrap(StructTy));
Rafael Espindola784ad242011-07-14 19:09:08 +00001075
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001076 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
Rafael Espindola784ad242011-07-14 19:09:08 +00001077}
1078
Gordon Henriksen1046c732007-10-06 15:11:06 +00001079LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001080 return wrap(ConstantVector::get(makeArrayRef(
Chris Lattner69229312011-02-15 00:14:00 +00001081 unwrap<Constant>(ScalarConstantVals, Size), Size)));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001082}
Torok Edwin05dc9d62011-10-06 12:39:34 +00001083
1084/*-- Opcode mapping */
1085
1086static LLVMOpcode map_to_llvmopcode(int opcode)
1087{
1088 switch (opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00001089 default: llvm_unreachable("Unhandled Opcode.");
Torok Edwin05dc9d62011-10-06 12:39:34 +00001090#define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
Chandler Carruth9fb823b2013-01-02 11:36:10 +00001091#include "llvm/IR/Instruction.def"
Torok Edwin05dc9d62011-10-06 12:39:34 +00001092#undef HANDLE_INST
Torok Edwin05dc9d62011-10-06 12:39:34 +00001093 }
1094}
1095
1096static int map_from_llvmopcode(LLVMOpcode code)
1097{
1098 switch (code) {
1099#define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
Chandler Carruth9fb823b2013-01-02 11:36:10 +00001100#include "llvm/IR/Instruction.def"
Torok Edwin05dc9d62011-10-06 12:39:34 +00001101#undef HANDLE_INST
Torok Edwin05dc9d62011-10-06 12:39:34 +00001102 }
David Blaikie486df732012-01-16 23:24:27 +00001103 llvm_unreachable("Unhandled Opcode.");
Torok Edwin05dc9d62011-10-06 12:39:34 +00001104}
1105
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001106/*--.. Constant expressions ................................................--*/
1107
Chris Lattner40cf28d2009-10-12 04:01:02 +00001108LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00001109 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
Chris Lattner40cf28d2009-10-12 04:01:02 +00001110}
1111
Duncan Sandsd334aca2009-05-21 15:52:21 +00001112LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001113 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
Duncan Sandsd334aca2009-05-21 15:52:21 +00001114}
1115
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001116LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001117 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001118}
1119
1120LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001121 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001122}
1123
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001124LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001125 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001126}
1127
1128LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001129 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001130}
1131
1132
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001133LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001134 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001135}
1136
1137LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001138 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001139}
1140
1141LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001142 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001143 unwrap<Constant>(RHSConstant)));
1144}
1145
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001146LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1147 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001148 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001149 unwrap<Constant>(RHSConstant)));
1150}
1151
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001152LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1153 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001154 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001155 unwrap<Constant>(RHSConstant)));
1156}
1157
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001158LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001159 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001160 unwrap<Constant>(RHSConstant)));
1161}
1162
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001163LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001164 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001165 unwrap<Constant>(RHSConstant)));
1166}
1167
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001168LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1169 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001170 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001171 unwrap<Constant>(RHSConstant)));
1172}
1173
1174LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1175 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001176 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001177 unwrap<Constant>(RHSConstant)));
1178}
1179
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001180LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1181 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1182 unwrap<Constant>(RHSConstant)));
1183}
1184
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001185LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001186 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001187 unwrap<Constant>(RHSConstant)));
1188}
1189
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001190LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1191 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001192 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001193 unwrap<Constant>(RHSConstant)));
1194}
1195
1196LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1197 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001198 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001199 unwrap<Constant>(RHSConstant)));
1200}
1201
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001202LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001203 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001204 unwrap<Constant>(RHSConstant)));
1205}
1206
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001207LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001208 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001209 unwrap<Constant>(RHSConstant)));
1210}
1211
Manuel Jacob49fafb12016-10-04 23:32:42 +00001212LLVMValueRef LLVMConstExactUDiv(LLVMValueRef LHSConstant,
1213 LLVMValueRef RHSConstant) {
1214 return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1215 unwrap<Constant>(RHSConstant)));
1216}
1217
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001218LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001219 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001220 unwrap<Constant>(RHSConstant)));
1221}
1222
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001223LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
1224 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001225 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001226 unwrap<Constant>(RHSConstant)));
1227}
1228
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001229LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001230 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001231 unwrap<Constant>(RHSConstant)));
1232}
1233
1234LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001235 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001236 unwrap<Constant>(RHSConstant)));
1237}
1238
1239LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001240 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001241 unwrap<Constant>(RHSConstant)));
1242}
1243
1244LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001245 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001246 unwrap<Constant>(RHSConstant)));
1247}
1248
1249LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001250 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001251 unwrap<Constant>(RHSConstant)));
1252}
1253
1254LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001255 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001256 unwrap<Constant>(RHSConstant)));
1257}
1258
1259LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001260 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001261 unwrap<Constant>(RHSConstant)));
1262}
1263
1264LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1265 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Owen Anderson487375e2009-07-29 18:55:55 +00001266 return wrap(ConstantExpr::getICmp(Predicate,
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001267 unwrap<Constant>(LHSConstant),
1268 unwrap<Constant>(RHSConstant)));
1269}
1270
1271LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1272 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Owen Anderson487375e2009-07-29 18:55:55 +00001273 return wrap(ConstantExpr::getFCmp(Predicate,
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001274 unwrap<Constant>(LHSConstant),
1275 unwrap<Constant>(RHSConstant)));
1276}
1277
1278LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001279 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1280 unwrap<Constant>(RHSConstant)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001281}
1282
1283LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001284 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001285 unwrap<Constant>(RHSConstant)));
1286}
1287
1288LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001289 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001290 unwrap<Constant>(RHSConstant)));
1291}
1292
1293LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1294 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
Jay Foaded8db7d2011-07-21 14:31:17 +00001295 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1296 NumIndices);
David Blaikie4a2e73b2015-04-02 18:55:32 +00001297 return wrap(ConstantExpr::getGetElementPtr(
1298 nullptr, unwrap<Constant>(ConstantVal), IdxList));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001299}
1300
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001301LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1302 LLVMValueRef *ConstantIndices,
1303 unsigned NumIndices) {
1304 Constant* Val = unwrap<Constant>(ConstantVal);
Jay Foaded8db7d2011-07-21 14:31:17 +00001305 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1306 NumIndices);
David Blaikie4a2e73b2015-04-02 18:55:32 +00001307 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList));
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001308}
1309
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001310LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001311 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001312 unwrap(ToType)));
1313}
1314
1315LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001316 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001317 unwrap(ToType)));
1318}
1319
1320LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001321 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001322 unwrap(ToType)));
1323}
1324
1325LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001326 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001327 unwrap(ToType)));
1328}
1329
1330LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001331 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001332 unwrap(ToType)));
1333}
1334
1335LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001336 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001337 unwrap(ToType)));
1338}
1339
1340LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Owen Anderson487375e2009-07-29 18:55:55 +00001341 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001342 unwrap(ToType)));
1343}
1344
1345LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Owen Anderson487375e2009-07-29 18:55:55 +00001346 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001347 unwrap(ToType)));
1348}
1349
1350LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001351 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001352 unwrap(ToType)));
1353}
1354
1355LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001356 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001357 unwrap(ToType)));
1358}
1359
1360LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001361 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001362 unwrap(ToType)));
1363}
1364
1365LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001366 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001367 unwrap(ToType)));
1368}
1369
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001370LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1371 LLVMTypeRef ToType) {
1372 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1373 unwrap(ToType)));
1374}
1375
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001376LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1377 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001378 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001379 unwrap(ToType)));
1380}
1381
1382LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1383 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001384 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001385 unwrap(ToType)));
1386}
1387
1388LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1389 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001390 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001391 unwrap(ToType)));
1392}
1393
1394LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1395 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001396 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001397 unwrap(ToType)));
1398}
1399
1400LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
Chris Lattner25963c62010-01-09 22:27:07 +00001401 LLVMBool isSigned) {
Chris Lattner69229312011-02-15 00:14:00 +00001402 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1403 unwrap(ToType), isSigned));
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001404}
1405
1406LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001407 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001408 unwrap(ToType)));
1409}
1410
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001411LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1412 LLVMValueRef ConstantIfTrue,
1413 LLVMValueRef ConstantIfFalse) {
Chris Lattner69229312011-02-15 00:14:00 +00001414 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001415 unwrap<Constant>(ConstantIfTrue),
1416 unwrap<Constant>(ConstantIfFalse)));
1417}
1418
1419LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1420 LLVMValueRef IndexConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001421 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001422 unwrap<Constant>(IndexConstant)));
1423}
1424
1425LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1426 LLVMValueRef ElementValueConstant,
1427 LLVMValueRef IndexConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001428 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001429 unwrap<Constant>(ElementValueConstant),
1430 unwrap<Constant>(IndexConstant)));
1431}
1432
1433LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1434 LLVMValueRef VectorBConstant,
1435 LLVMValueRef MaskConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001436 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001437 unwrap<Constant>(VectorBConstant),
1438 unwrap<Constant>(MaskConstant)));
1439}
1440
Dan Gohmand5104a52008-11-03 22:55:43 +00001441LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1442 unsigned NumIdx) {
Chris Lattner69229312011-02-15 00:14:00 +00001443 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001444 makeArrayRef(IdxList, NumIdx)));
Dan Gohmand5104a52008-11-03 22:55:43 +00001445}
1446
1447LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1448 LLVMValueRef ElementValueConstant,
1449 unsigned *IdxList, unsigned NumIdx) {
Chris Lattner69229312011-02-15 00:14:00 +00001450 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
Dan Gohmand5104a52008-11-03 22:55:43 +00001451 unwrap<Constant>(ElementValueConstant),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001452 makeArrayRef(IdxList, NumIdx)));
Dan Gohmand5104a52008-11-03 22:55:43 +00001453}
1454
Chris Lattner25963c62010-01-09 22:27:07 +00001455LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1456 const char *Constraints,
1457 LLVMBool HasSideEffects,
Benjamin Kramer8dd0edc2012-09-10 11:52:00 +00001458 LLVMBool IsAlignStack) {
Chris Lattner25963c62010-01-09 22:27:07 +00001459 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
Benjamin Kramer8dd0edc2012-09-10 11:52:00 +00001460 Constraints, HasSideEffects, IsAlignStack));
Chris Lattner3d1f5522008-12-17 21:39:50 +00001461}
1462
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00001463LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1464 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1465}
1466
Gordon Henriksen76a03742007-09-18 03:18:57 +00001467/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1468
Gordon Henriksen265f7802008-03-19 01:11:35 +00001469LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1470 return wrap(unwrap<GlobalValue>(Global)->getParent());
1471}
1472
Chris Lattner25963c62010-01-09 22:27:07 +00001473LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001474 return unwrap<GlobalValue>(Global)->isDeclaration();
1475}
1476
1477LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001478 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001479 case GlobalValue::ExternalLinkage:
1480 return LLVMExternalLinkage;
1481 case GlobalValue::AvailableExternallyLinkage:
1482 return LLVMAvailableExternallyLinkage;
1483 case GlobalValue::LinkOnceAnyLinkage:
1484 return LLVMLinkOnceAnyLinkage;
1485 case GlobalValue::LinkOnceODRLinkage:
1486 return LLVMLinkOnceODRLinkage;
1487 case GlobalValue::WeakAnyLinkage:
1488 return LLVMWeakAnyLinkage;
1489 case GlobalValue::WeakODRLinkage:
1490 return LLVMWeakODRLinkage;
1491 case GlobalValue::AppendingLinkage:
1492 return LLVMAppendingLinkage;
1493 case GlobalValue::InternalLinkage:
1494 return LLVMInternalLinkage;
1495 case GlobalValue::PrivateLinkage:
1496 return LLVMPrivateLinkage;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001497 case GlobalValue::ExternalWeakLinkage:
1498 return LLVMExternalWeakLinkage;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001499 case GlobalValue::CommonLinkage:
1500 return LLVMCommonLinkage;
1501 }
1502
David Blaikiea5708dc2012-01-17 07:00:13 +00001503 llvm_unreachable("Invalid GlobalValue linkage!");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001504}
1505
1506void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001507 GlobalValue *GV = unwrap<GlobalValue>(Global);
1508
1509 switch (Linkage) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001510 case LLVMExternalLinkage:
1511 GV->setLinkage(GlobalValue::ExternalLinkage);
1512 break;
1513 case LLVMAvailableExternallyLinkage:
1514 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1515 break;
1516 case LLVMLinkOnceAnyLinkage:
1517 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1518 break;
1519 case LLVMLinkOnceODRLinkage:
1520 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1521 break;
Bill Wendling34bc34e2012-08-17 18:33:14 +00001522 case LLVMLinkOnceODRAutoHideLinkage:
Rafael Espindola716e7402013-11-01 17:09:14 +00001523 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1524 "longer supported.");
Bill Wendling34bc34e2012-08-17 18:33:14 +00001525 break;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001526 case LLVMWeakAnyLinkage:
1527 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1528 break;
1529 case LLVMWeakODRLinkage:
1530 GV->setLinkage(GlobalValue::WeakODRLinkage);
1531 break;
1532 case LLVMAppendingLinkage:
1533 GV->setLinkage(GlobalValue::AppendingLinkage);
1534 break;
1535 case LLVMInternalLinkage:
1536 GV->setLinkage(GlobalValue::InternalLinkage);
1537 break;
1538 case LLVMPrivateLinkage:
1539 GV->setLinkage(GlobalValue::PrivateLinkage);
1540 break;
1541 case LLVMLinkerPrivateLinkage:
Rafael Espindola2fb5bc32014-03-13 23:18:37 +00001542 GV->setLinkage(GlobalValue::PrivateLinkage);
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001543 break;
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001544 case LLVMLinkerPrivateWeakLinkage:
Rafael Espindola2fb5bc32014-03-13 23:18:37 +00001545 GV->setLinkage(GlobalValue::PrivateLinkage);
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001546 break;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001547 case LLVMDLLImportLinkage:
Nico Rieck7157bb72014-01-14 15:22:47 +00001548 DEBUG(errs()
1549 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001550 break;
1551 case LLVMDLLExportLinkage:
Nico Rieck7157bb72014-01-14 15:22:47 +00001552 DEBUG(errs()
1553 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001554 break;
1555 case LLVMExternalWeakLinkage:
1556 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1557 break;
1558 case LLVMGhostLinkage:
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001559 DEBUG(errs()
1560 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001561 break;
1562 case LLVMCommonLinkage:
1563 GV->setLinkage(GlobalValue::CommonLinkage);
1564 break;
1565 }
Gordon Henriksen76a03742007-09-18 03:18:57 +00001566}
1567
1568const char *LLVMGetSection(LLVMValueRef Global) {
Rafael Espindola83658d62016-05-11 18:21:59 +00001569 // Using .data() is safe because of how GlobalObject::setSection is
1570 // implemented.
1571 return unwrap<GlobalValue>(Global)->getSection().data();
Gordon Henriksen76a03742007-09-18 03:18:57 +00001572}
1573
1574void LLVMSetSection(LLVMValueRef Global, const char *Section) {
Rafael Espindola99e05cf2014-05-13 18:45:48 +00001575 unwrap<GlobalObject>(Global)->setSection(Section);
Gordon Henriksen76a03742007-09-18 03:18:57 +00001576}
1577
1578LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1579 return static_cast<LLVMVisibility>(
1580 unwrap<GlobalValue>(Global)->getVisibility());
1581}
1582
1583void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1584 unwrap<GlobalValue>(Global)
1585 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1586}
1587
Reid Kleckner2fae26f2014-03-05 02:34:23 +00001588LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1589 return static_cast<LLVMDLLStorageClass>(
1590 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1591}
1592
1593void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1594 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1595 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1596}
1597
Tim Northoverad96d012014-03-10 19:24:35 +00001598LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001599 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
Tim Northoverad96d012014-03-10 19:24:35 +00001600}
1601
1602void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001603 unwrap<GlobalValue>(Global)->setUnnamedAddr(
1604 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1605 : GlobalValue::UnnamedAddr::None);
Tim Northoverad96d012014-03-10 19:24:35 +00001606}
1607
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001608/*--.. Operations on global variables, load and store instructions .........--*/
1609
1610unsigned LLVMGetAlignment(LLVMValueRef V) {
1611 Value *P = unwrap<Value>(V);
1612 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1613 return GV->getAlignment();
Peter Zotov9f584e62014-03-05 05:05:34 +00001614 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1615 return AI->getAlignment();
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001616 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1617 return LI->getAlignment();
1618 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1619 return SI->getAlignment();
1620
Peter Zotov9f584e62014-03-05 05:05:34 +00001621 llvm_unreachable(
1622 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001623}
1624
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001625void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1626 Value *P = unwrap<Value>(V);
Rafael Espindola99e05cf2014-05-13 18:45:48 +00001627 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001628 GV->setAlignment(Bytes);
Peter Zotov9f584e62014-03-05 05:05:34 +00001629 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1630 AI->setAlignment(Bytes);
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001631 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1632 LI->setAlignment(Bytes);
1633 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1634 SI->setAlignment(Bytes);
Anders Waldenborga36a7822013-10-29 09:37:28 +00001635 else
Peter Zotov9f584e62014-03-05 05:05:34 +00001636 llvm_unreachable(
1637 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001638}
1639
1640/*--.. Operations on global variables ......................................--*/
1641
1642LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
Owen Andersonb17f3292009-07-08 19:03:57 +00001643 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
Craig Topperc6207612014-04-09 06:08:46 +00001644 GlobalValue::ExternalLinkage, nullptr, Name));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001645}
1646
Erick Tryzelaar06894b32010-02-28 09:46:13 +00001647LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1648 const char *Name,
1649 unsigned AddressSpace) {
1650 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
Craig Topperc6207612014-04-09 06:08:46 +00001651 GlobalValue::ExternalLinkage, nullptr, Name,
1652 nullptr, GlobalVariable::NotThreadLocal,
1653 AddressSpace));
Erick Tryzelaar06894b32010-02-28 09:46:13 +00001654}
1655
Gordon Henriksen783f7bb2007-10-08 03:45:09 +00001656LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1657 return wrap(unwrap(M)->getNamedGlobal(Name));
1658}
1659
Gordon Henriksen054817c2008-03-19 03:47:18 +00001660LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1661 Module *Mod = unwrap(M);
1662 Module::global_iterator I = Mod->global_begin();
1663 if (I == Mod->global_end())
Craig Topperc6207612014-04-09 06:08:46 +00001664 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001665 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001666}
1667
1668LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1669 Module *Mod = unwrap(M);
1670 Module::global_iterator I = Mod->global_end();
1671 if (I == Mod->global_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001672 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001673 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001674}
1675
1676LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1677 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001678 Module::global_iterator I(GV);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001679 if (++I == GV->getParent()->global_end())
Craig Topperc6207612014-04-09 06:08:46 +00001680 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001681 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001682}
1683
1684LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1685 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001686 Module::global_iterator I(GV);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001687 if (I == GV->getParent()->global_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001688 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001689 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001690}
1691
Gordon Henriksen76a03742007-09-18 03:18:57 +00001692void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1693 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1694}
1695
Gordon Henriksen76a03742007-09-18 03:18:57 +00001696LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
Chris Lattner40cf28d2009-10-12 04:01:02 +00001697 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1698 if ( !GV->hasInitializer() )
Craig Topperc6207612014-04-09 06:08:46 +00001699 return nullptr;
Chris Lattner40cf28d2009-10-12 04:01:02 +00001700 return wrap(GV->getInitializer());
Gordon Henriksen76a03742007-09-18 03:18:57 +00001701}
1702
1703void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1704 unwrap<GlobalVariable>(GlobalVar)
1705 ->setInitializer(unwrap<Constant>(ConstantVal));
1706}
1707
Chris Lattner25963c62010-01-09 22:27:07 +00001708LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001709 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1710}
1711
Chris Lattner25963c62010-01-09 22:27:07 +00001712void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001713 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1714}
1715
Chris Lattner25963c62010-01-09 22:27:07 +00001716LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
Gordon Henriksen751ebf72007-10-07 17:31:42 +00001717 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1718}
1719
Chris Lattner25963c62010-01-09 22:27:07 +00001720void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
Gordon Henriksen751ebf72007-10-07 17:31:42 +00001721 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1722}
1723
Hans Wennborg5ff71202013-04-16 08:58:59 +00001724LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1725 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1726 case GlobalVariable::NotThreadLocal:
1727 return LLVMNotThreadLocal;
1728 case GlobalVariable::GeneralDynamicTLSModel:
1729 return LLVMGeneralDynamicTLSModel;
1730 case GlobalVariable::LocalDynamicTLSModel:
1731 return LLVMLocalDynamicTLSModel;
1732 case GlobalVariable::InitialExecTLSModel:
1733 return LLVMInitialExecTLSModel;
1734 case GlobalVariable::LocalExecTLSModel:
1735 return LLVMLocalExecTLSModel;
1736 }
1737
1738 llvm_unreachable("Invalid GlobalVariable thread local mode");
1739}
1740
1741void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1742 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1743
1744 switch (Mode) {
1745 case LLVMNotThreadLocal:
1746 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1747 break;
1748 case LLVMGeneralDynamicTLSModel:
1749 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1750 break;
1751 case LLVMLocalDynamicTLSModel:
1752 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1753 break;
1754 case LLVMInitialExecTLSModel:
1755 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1756 break;
1757 case LLVMLocalExecTLSModel:
1758 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1759 break;
1760 }
1761}
1762
1763LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1764 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1765}
1766
1767void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1768 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1769}
1770
Chris Lattner3d1f5522008-12-17 21:39:50 +00001771/*--.. Operations on aliases ......................................--*/
1772
1773LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1774 const char *Name) {
Rafael Espindola4fe00942014-05-16 13:34:04 +00001775 auto *PTy = cast<PointerType>(unwrap(Ty));
David Blaikie16a2f3e2015-09-14 18:01:59 +00001776 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1777 GlobalValue::ExternalLinkage, Name,
Rafael Espindola993502e2015-02-23 21:51:06 +00001778 unwrap<Constant>(Aliasee), unwrap(M)));
Chris Lattner3d1f5522008-12-17 21:39:50 +00001779}
1780
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001781/*--.. Operations on functions .............................................--*/
1782
1783LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1784 LLVMTypeRef FunctionTy) {
Gabor Greife9ecc682008-04-06 20:25:17 +00001785 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1786 GlobalValue::ExternalLinkage, Name, unwrap(M)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001787}
1788
Gordon Henriksen783f7bb2007-10-08 03:45:09 +00001789LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1790 return wrap(unwrap(M)->getFunction(Name));
1791}
1792
Gordon Henriksen054817c2008-03-19 03:47:18 +00001793LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1794 Module *Mod = unwrap(M);
1795 Module::iterator I = Mod->begin();
1796 if (I == Mod->end())
Craig Topperc6207612014-04-09 06:08:46 +00001797 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001798 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001799}
1800
1801LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1802 Module *Mod = unwrap(M);
1803 Module::iterator I = Mod->end();
1804 if (I == Mod->begin())
Craig Topperc6207612014-04-09 06:08:46 +00001805 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001806 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001807}
1808
1809LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1810 Function *Func = unwrap<Function>(Fn);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001811 Module::iterator I(Func);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001812 if (++I == Func->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00001813 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001814 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001815}
1816
1817LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1818 Function *Func = unwrap<Function>(Fn);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001819 Module::iterator I(Func);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001820 if (I == Func->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00001821 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001822 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001823}
1824
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001825void LLVMDeleteFunction(LLVMValueRef Fn) {
1826 unwrap<Function>(Fn)->eraseFromParent();
1827}
1828
Amaury Sechete39e8532016-02-18 20:38:32 +00001829LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
1830 return unwrap<Function>(Fn)->hasPersonalityFn();
1831}
1832
Andrew Wilkins3bdfc1c2015-07-14 01:23:06 +00001833LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
1834 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
1835}
1836
1837void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
1838 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
1839}
1840
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001841unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1842 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1843 return F->getIntrinsicID();
1844 return 0;
1845}
1846
1847unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1848 return unwrap<Function>(Fn)->getCallingConv();
1849}
1850
1851void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
Sandeep Patel68c5f472009-09-02 08:44:58 +00001852 return unwrap<Function>(Fn)->setCallingConv(
1853 static_cast<CallingConv::ID>(CC));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001854}
1855
Gordon Henriksend930f912008-08-17 18:44:35 +00001856const char *LLVMGetGC(LLVMValueRef Fn) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00001857 Function *F = unwrap<Function>(Fn);
Mehdi Amini599ebf22016-01-08 02:28:20 +00001858 return F->hasGC()? F->getGC().c_str() : nullptr;
Gordon Henriksen71183b62007-12-10 03:18:06 +00001859}
1860
Gordon Henriksend930f912008-08-17 18:44:35 +00001861void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00001862 Function *F = unwrap<Function>(Fn);
Gordon Henriksend930f912008-08-17 18:44:35 +00001863 if (GC)
1864 F->setGC(GC);
Gordon Henriksen71183b62007-12-10 03:18:06 +00001865 else
Gordon Henriksend930f912008-08-17 18:44:35 +00001866 F->clearGC();
Gordon Henriksen71183b62007-12-10 03:18:06 +00001867}
1868
Amaury Sechet5db224e2016-06-12 06:17:24 +00001869void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1870 LLVMAttributeRef A) {
1871 unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
1872}
1873
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001874unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001875 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
1876 return AS.getNumAttributes();
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001877}
1878
1879void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1880 LLVMAttributeRef *Attrs) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001881 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
1882 for (auto A : AS)
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001883 *Attrs++ = wrap(A);
1884}
1885
Amaury Sechet5db224e2016-06-12 06:17:24 +00001886LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
1887 LLVMAttributeIndex Idx,
1888 unsigned KindID) {
1889 return wrap(unwrap<Function>(F)->getAttribute(Idx,
1890 (Attribute::AttrKind)KindID));
1891}
1892
Amaury Sechet6100adf2016-06-15 17:50:39 +00001893LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
1894 LLVMAttributeIndex Idx,
1895 const char *K, unsigned KLen) {
1896 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
1897}
1898
Amaury Sechet5db224e2016-06-12 06:17:24 +00001899void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1900 unsigned KindID) {
1901 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
1902}
1903
Amaury Sechet6100adf2016-06-15 17:50:39 +00001904void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1905 const char *K, unsigned KLen) {
1906 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
1907}
1908
Tom Stellarde8f35e12013-04-16 23:12:43 +00001909void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1910 const char *V) {
1911 Function *Func = unwrap<Function>(Fn);
Reid Kleckner9d16fa02017-04-19 17:28:52 +00001912 Attribute Attr = Attribute::get(Func->getContext(), A, V);
1913 Func->addAttribute(AttributeList::FunctionIndex, Attr);
Tom Stellarde8f35e12013-04-16 23:12:43 +00001914}
1915
Gordon Henriksen265f7802008-03-19 01:11:35 +00001916/*--.. Operations on parameters ............................................--*/
1917
1918unsigned LLVMCountParams(LLVMValueRef FnRef) {
1919 // This function is strictly redundant to
1920 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
Dan Gohmanc7076912008-06-21 22:06:54 +00001921 return unwrap<Function>(FnRef)->arg_size();
Gordon Henriksen265f7802008-03-19 01:11:35 +00001922}
1923
1924void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1925 Function *Fn = unwrap<Function>(FnRef);
1926 for (Function::arg_iterator I = Fn->arg_begin(),
1927 E = Fn->arg_end(); I != E; I++)
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001928 *ParamRefs++ = wrap(&*I);
Gordon Henriksen265f7802008-03-19 01:11:35 +00001929}
1930
1931LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
Reid Kleckner56d028d2017-03-17 17:16:39 +00001932 Function *Fn = unwrap<Function>(FnRef);
1933 return wrap(&Fn->arg_begin()[index]);
Gordon Henriksen265f7802008-03-19 01:11:35 +00001934}
1935
1936LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1937 return wrap(unwrap<Argument>(V)->getParent());
1938}
1939
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001940LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1941 Function *Func = unwrap<Function>(Fn);
1942 Function::arg_iterator I = Func->arg_begin();
1943 if (I == Func->arg_end())
Craig Topperc6207612014-04-09 06:08:46 +00001944 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001945 return wrap(&*I);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001946}
1947
1948LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1949 Function *Func = unwrap<Function>(Fn);
1950 Function::arg_iterator I = Func->arg_end();
1951 if (I == Func->arg_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001952 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001953 return wrap(&*--I);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001954}
1955
1956LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1957 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner56d028d2017-03-17 17:16:39 +00001958 Function *Fn = A->getParent();
1959 if (A->getArgNo() + 1 >= Fn->arg_size())
Craig Topperc6207612014-04-09 06:08:46 +00001960 return nullptr;
Reid Kleckner56d028d2017-03-17 17:16:39 +00001961 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001962}
1963
1964LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1965 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner56d028d2017-03-17 17:16:39 +00001966 if (A->getArgNo() == 0)
Craig Topperc6207612014-04-09 06:08:46 +00001967 return nullptr;
Reid Kleckner56d028d2017-03-17 17:16:39 +00001968 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001969}
1970
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00001971void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
Bill Wendling49bc76c2013-01-23 06:14:59 +00001972 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner9d16fa02017-04-19 17:28:52 +00001973 A->addAttr(Attribute::getWithAlignment(A->getContext(), align));
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00001974}
1975
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001976/*--.. Operations on basic blocks ..........................................--*/
1977
Gordon Henriksen265f7802008-03-19 01:11:35 +00001978LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1979 return wrap(static_cast<Value*>(unwrap(BB)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001980}
1981
Chris Lattner25963c62010-01-09 22:27:07 +00001982LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001983 return isa<BasicBlock>(unwrap(Val));
1984}
1985
1986LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1987 return wrap(unwrap<BasicBlock>(Val));
1988}
1989
Amaury Secheta82042e2016-02-09 22:36:41 +00001990const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
1991 return unwrap(BB)->getName().data();
1992}
1993
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001994LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1995 return wrap(unwrap(BB)->getParent());
Gordon Henriksen265f7802008-03-19 01:11:35 +00001996}
1997
Nate Begeman43c322b2011-08-23 20:27:46 +00001998LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1999 return wrap(unwrap(BB)->getTerminator());
2000}
2001
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002002unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
Dan Gohmanc7076912008-06-21 22:06:54 +00002003 return unwrap<Function>(FnRef)->size();
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002004}
2005
2006void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2007 Function *Fn = unwrap<Function>(FnRef);
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00002008 for (BasicBlock &BB : *Fn)
2009 *BasicBlocksRefs++ = wrap(&BB);
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002010}
2011
2012LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2013 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2014}
2015
Gordon Henriksen054817c2008-03-19 03:47:18 +00002016LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2017 Function *Func = unwrap<Function>(Fn);
2018 Function::iterator I = Func->begin();
2019 if (I == Func->end())
Craig Topperc6207612014-04-09 06:08:46 +00002020 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002021 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002022}
2023
2024LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2025 Function *Func = unwrap<Function>(Fn);
2026 Function::iterator I = Func->end();
2027 if (I == Func->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002028 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002029 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002030}
2031
2032LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2033 BasicBlock *Block = unwrap(BB);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002034 Function::iterator I(Block);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002035 if (++I == Block->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00002036 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002037 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002038}
2039
2040LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2041 BasicBlock *Block = unwrap(BB);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002042 Function::iterator I(Block);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002043 if (I == Block->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002044 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002045 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002046}
2047
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002048LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2049 LLVMValueRef FnRef,
2050 const char *Name) {
2051 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002052}
2053
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002054LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2055 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2056}
2057
2058LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2059 LLVMBasicBlockRef BBRef,
2060 const char *Name) {
2061 BasicBlock *BB = unwrap(BBRef);
2062 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2063}
2064
2065LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002066 const char *Name) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002067 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002068}
2069
2070void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2071 unwrap(BBRef)->eraseFromParent();
2072}
2073
Nate Begeman43c322b2011-08-23 20:27:46 +00002074void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2075 unwrap(BBRef)->removeFromParent();
2076}
2077
Duncan Sandsb1d61aa2010-07-19 15:31:07 +00002078void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2079 unwrap(BB)->moveBefore(unwrap(MovePos));
2080}
2081
2082void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2083 unwrap(BB)->moveAfter(unwrap(MovePos));
2084}
2085
Gordon Henriksen265f7802008-03-19 01:11:35 +00002086/*--.. Operations on instructions ..........................................--*/
2087
2088LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2089 return wrap(unwrap<Instruction>(Inst)->getParent());
2090}
2091
Gordon Henriksen054817c2008-03-19 03:47:18 +00002092LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2093 BasicBlock *Block = unwrap(BB);
2094 BasicBlock::iterator I = Block->begin();
2095 if (I == Block->end())
Craig Topperc6207612014-04-09 06:08:46 +00002096 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002097 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002098}
2099
2100LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2101 BasicBlock *Block = unwrap(BB);
2102 BasicBlock::iterator I = Block->end();
2103 if (I == Block->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002104 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002105 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002106}
2107
2108LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2109 Instruction *Instr = unwrap<Instruction>(Inst);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002110 BasicBlock::iterator I(Instr);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002111 if (++I == Instr->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00002112 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002113 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002114}
2115
2116LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2117 Instruction *Instr = unwrap<Instruction>(Inst);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002118 BasicBlock::iterator I(Instr);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002119 if (I == Instr->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002120 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002121 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002122}
2123
Amaury Sechet2f432082016-02-11 21:37:54 +00002124void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2125 unwrap<Instruction>(Inst)->removeFromParent();
2126}
2127
Devang Pateldbebc6f2011-10-03 20:59:18 +00002128void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2129 unwrap<Instruction>(Inst)->eraseFromParent();
2130}
2131
Torok Edwin60c40de2011-10-06 12:13:20 +00002132LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
Torok Edwin2e9affe2011-10-14 20:37:42 +00002133 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2134 return (LLVMIntPredicate)I->getPredicate();
2135 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2136 if (CE->getOpcode() == Instruction::ICmp)
2137 return (LLVMIntPredicate)CE->getPredicate();
2138 return (LLVMIntPredicate)0;
Torok Edwin60c40de2011-10-06 12:13:20 +00002139}
2140
Peter Zotov1d98e6d2014-10-28 19:46:44 +00002141LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2142 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2143 return (LLVMRealPredicate)I->getPredicate();
2144 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2145 if (CE->getOpcode() == Instruction::FCmp)
2146 return (LLVMRealPredicate)CE->getPredicate();
2147 return (LLVMRealPredicate)0;
2148}
2149
Torok Edwinab6158e2011-10-14 20:37:49 +00002150LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2151 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2152 return map_to_llvmopcode(C->getOpcode());
2153 return (LLVMOpcode)0;
2154}
2155
Peter Zotovaff492c2014-10-17 01:02:34 +00002156LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2157 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2158 return wrap(C->clone());
2159 return nullptr;
2160}
2161
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002162/*--.. Call and invoke instructions ........................................--*/
2163
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002164unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
2165 return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands();
2166}
2167
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002168unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002169 return CallSite(unwrap<Instruction>(Instr)).getCallingConv();
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002170}
2171
2172void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002173 return CallSite(unwrap<Instruction>(Instr))
2174 .setCallingConv(static_cast<CallingConv::ID>(CC));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002175}
2176
Andrew Trickdc073ad2013-09-18 23:31:10 +00002177void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002178 unsigned align) {
2179 CallSite Call = CallSite(unwrap<Instruction>(Instr));
Reid Kleckner9d16fa02017-04-19 17:28:52 +00002180 Attribute AlignAttr = Attribute::getWithAlignment(Call->getContext(), align);
2181 Call.addAttribute(index, AlignAttr);
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002182}
2183
Amaury Secheta65a2372016-06-15 05:14:29 +00002184void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2185 LLVMAttributeRef A) {
2186 CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A));
2187}
2188
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002189unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2190 LLVMAttributeIndex Idx) {
2191 auto CS = CallSite(unwrap<Instruction>(C));
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002192 auto AS = CS.getAttributes().getAttributes(Idx);
2193 return AS.getNumAttributes();
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002194}
2195
2196void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2197 LLVMAttributeRef *Attrs) {
2198 auto CS = CallSite(unwrap<Instruction>(C));
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002199 auto AS = CS.getAttributes().getAttributes(Idx);
2200 for (auto A : AS)
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002201 *Attrs++ = wrap(A);
2202}
2203
Amaury Secheta65a2372016-06-15 05:14:29 +00002204LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
2205 LLVMAttributeIndex Idx,
2206 unsigned KindID) {
2207 return wrap(CallSite(unwrap<Instruction>(C))
2208 .getAttribute(Idx, (Attribute::AttrKind)KindID));
2209}
2210
Amaury Sechet6100adf2016-06-15 17:50:39 +00002211LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
2212 LLVMAttributeIndex Idx,
2213 const char *K, unsigned KLen) {
2214 return wrap(CallSite(unwrap<Instruction>(C))
2215 .getAttribute(Idx, StringRef(K, KLen)));
2216}
2217
Amaury Secheta65a2372016-06-15 05:14:29 +00002218void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2219 unsigned KindID) {
2220 CallSite(unwrap<Instruction>(C))
2221 .removeAttribute(Idx, (Attribute::AttrKind)KindID);
2222}
2223
Amaury Sechet6100adf2016-06-15 17:50:39 +00002224void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2225 const char *K, unsigned KLen) {
2226 CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen));
2227}
2228
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002229LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2230 return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue());
2231}
2232
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002233/*--.. Operations on call instructions (only) ..............................--*/
2234
Chris Lattner25963c62010-01-09 22:27:07 +00002235LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002236 return unwrap<CallInst>(Call)->isTailCall();
2237}
2238
Chris Lattner25963c62010-01-09 22:27:07 +00002239void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002240 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2241}
2242
Amaury Sechete39e8532016-02-18 20:38:32 +00002243/*--.. Operations on invoke instructions (only) ............................--*/
2244
2245LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2246 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2247}
2248
2249LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
2250 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2251}
2252
2253void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2254 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2255}
2256
2257void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2258 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2259}
2260
Peter Zotov2481c752014-10-28 19:46:56 +00002261/*--.. Operations on terminators ...........................................--*/
2262
2263unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2264 return unwrap<TerminatorInst>(Term)->getNumSuccessors();
2265}
2266
2267LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2268 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
2269}
2270
2271void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2272 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
2273}
2274
2275/*--.. Operations on branch instructions (only) ............................--*/
2276
2277LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2278 return unwrap<BranchInst>(Branch)->isConditional();
2279}
2280
2281LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2282 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2283}
2284
2285void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2286 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2287}
2288
Nate Begeman43c322b2011-08-23 20:27:46 +00002289/*--.. Operations on switch instructions (only) ............................--*/
2290
2291LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2292 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2293}
2294
Amaury Sechet1dcf5772016-02-09 22:50:53 +00002295/*--.. Operations on alloca instructions (only) ............................--*/
2296
2297LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
2298 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2299}
2300
Amaury Sechet053ac452016-02-17 22:51:03 +00002301/*--.. Operations on gep instructions (only) ...............................--*/
2302
2303LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
2304 return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2305}
2306
Amaury Sechet8a367d42016-05-01 02:23:14 +00002307void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
2308 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
Amaury Sechet053ac452016-02-17 22:51:03 +00002309}
2310
Gordon Henriksen44dd8fb2007-10-08 18:14:39 +00002311/*--.. Operations on phi nodes .............................................--*/
2312
2313void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2314 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2315 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2316 for (unsigned I = 0; I != Count; ++I)
2317 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2318}
2319
2320unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2321 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2322}
2323
2324LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2325 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2326}
2327
2328LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2329 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2330}
2331
Amaury Sechetaad93532016-02-10 00:38:50 +00002332/*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2333
2334unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
2335 auto *I = unwrap(Inst);
Amaury Sechet053ac452016-02-17 22:51:03 +00002336 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
2337 return GEP->getNumIndices();
Amaury Sechetaad93532016-02-10 00:38:50 +00002338 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2339 return EV->getNumIndices();
2340 if (auto *IV = dyn_cast<InsertValueInst>(I))
2341 return IV->getNumIndices();
2342 llvm_unreachable(
2343 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2344}
2345
2346const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2347 auto *I = unwrap(Inst);
2348 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2349 return EV->getIndices().data();
2350 if (auto *IV = dyn_cast<InsertValueInst>(I))
2351 return IV->getIndices().data();
2352 llvm_unreachable(
2353 "LLVMGetIndices applies only to extractvalue and insertvalue!");
2354}
2355
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002356
2357/*===-- Instruction builders ----------------------------------------------===*/
2358
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002359LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2360 return wrap(new IRBuilder<>(*unwrap(C)));
2361}
2362
Gordon Henriksena735a9c2008-05-04 12:55:34 +00002363LLVMBuilderRef LLVMCreateBuilder(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002364 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002365}
2366
Gordon Henriksen054817c2008-03-19 03:47:18 +00002367void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2368 LLVMValueRef Instr) {
2369 BasicBlock *BB = unwrap(Block);
Duncan P. N. Exon Smith43724642016-08-11 15:45:04 +00002370 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
2371 unwrap(Builder)->SetInsertPoint(BB, I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002372}
2373
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002374void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2375 Instruction *I = unwrap<Instruction>(Instr);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002376 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002377}
2378
2379void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2380 BasicBlock *BB = unwrap(Block);
2381 unwrap(Builder)->SetInsertPoint(BB);
2382}
2383
Gordon Henriksen265f7802008-03-19 01:11:35 +00002384LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2385 return wrap(unwrap(Builder)->GetInsertBlock());
2386}
2387
Chris Lattner3d1f5522008-12-17 21:39:50 +00002388void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
Chris Lattnere4bcde82010-04-01 06:31:45 +00002389 unwrap(Builder)->ClearInsertionPoint();
Chris Lattner3d1f5522008-12-17 21:39:50 +00002390}
2391
2392void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2393 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2394}
2395
Erick Tryzelaar9813bea2009-08-16 02:20:57 +00002396void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2397 const char *Name) {
2398 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2399}
2400
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002401void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2402 delete unwrap(Builder);
2403}
2404
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002405/*--.. Metadata builders ...................................................--*/
2406
2407void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002408 MDNode *Loc =
2409 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002410 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002411}
2412
2413LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002414 LLVMContext &Context = unwrap(Builder)->getContext();
2415 return wrap(MetadataAsValue::get(
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002416 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002417}
2418
2419void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2420 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2421}
2422
2423
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002424/*--.. Instruction builders ................................................--*/
2425
2426LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2427 return wrap(unwrap(B)->CreateRetVoid());
2428}
2429
2430LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2431 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2432}
2433
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002434LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2435 unsigned N) {
2436 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2437}
2438
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002439LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2440 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2441}
2442
2443LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2444 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2445 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2446}
2447
2448LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2449 LLVMBasicBlockRef Else, unsigned NumCases) {
2450 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2451}
2452
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00002453LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2454 unsigned NumDests) {
2455 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2456}
2457
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002458LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2459 LLVMValueRef *Args, unsigned NumArgs,
2460 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2461 const char *Name) {
2462 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002463 makeArrayRef(unwrap(Args), NumArgs),
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002464 Name));
2465}
2466
Bill Wendlingfae14752011-08-12 20:24:12 +00002467LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
Reid Kleckneref9828f2015-07-16 01:16:39 +00002468 LLVMValueRef PersFn, unsigned NumClauses,
2469 const char *Name) {
2470 // The personality used to live on the landingpad instruction, but now it
2471 // lives on the parent function. For compatibility, take the provided
2472 // personality and put it on the parent function.
2473 if (PersFn)
2474 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
2475 cast<Function>(unwrap(PersFn)));
David Majnemer7fddecc2015-06-17 20:52:32 +00002476 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
Bill Wendlingfae14752011-08-12 20:24:12 +00002477}
2478
Bill Wendlingf891bf82011-07-31 06:30:59 +00002479LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2480 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2481}
2482
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002483LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2484 return wrap(unwrap(B)->CreateUnreachable());
2485}
2486
Gordon Henriksen097102c2008-01-01 05:50:53 +00002487void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2488 LLVMBasicBlockRef Dest) {
2489 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2490}
2491
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00002492void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2493 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2494}
2495
Amaury Sechete39e8532016-02-18 20:38:32 +00002496unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
2497 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
2498}
2499
2500LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
2501 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
2502}
2503
Bill Wendlingfae14752011-08-12 20:24:12 +00002504void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2505 unwrap<LandingPadInst>(LandingPad)->
2506 addClause(cast<Constant>(unwrap(ClauseVal)));
2507}
2508
Amaury Sechete39e8532016-02-18 20:38:32 +00002509LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
2510 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
2511}
2512
Bill Wendlingfae14752011-08-12 20:24:12 +00002513void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2514 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2515}
2516
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002517/*--.. Arithmetic ..........................................................--*/
2518
2519LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2520 const char *Name) {
2521 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2522}
2523
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002524LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2525 const char *Name) {
2526 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2527}
2528
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002529LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2530 const char *Name) {
2531 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2532}
2533
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002534LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2535 const char *Name) {
2536 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2537}
2538
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002539LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2540 const char *Name) {
2541 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2542}
2543
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002544LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2545 const char *Name) {
2546 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2547}
2548
2549LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2550 const char *Name) {
2551 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2552}
2553
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002554LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2555 const char *Name) {
2556 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2557}
2558
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002559LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2560 const char *Name) {
2561 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2562}
2563
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002564LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2565 const char *Name) {
2566 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2567}
2568
2569LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2570 const char *Name) {
2571 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2572}
2573
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002574LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2575 const char *Name) {
2576 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2577}
2578
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002579LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2580 const char *Name) {
2581 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2582}
2583
Manuel Jacob49fafb12016-10-04 23:32:42 +00002584LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2585 LLVMValueRef RHS, const char *Name) {
2586 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
2587}
2588
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002589LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2590 const char *Name) {
2591 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2592}
2593
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002594LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2595 LLVMValueRef RHS, const char *Name) {
2596 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2597}
2598
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002599LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2600 const char *Name) {
2601 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2602}
2603
2604LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2605 const char *Name) {
2606 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2607}
2608
2609LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2610 const char *Name) {
2611 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2612}
2613
2614LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2615 const char *Name) {
2616 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2617}
2618
2619LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2620 const char *Name) {
2621 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2622}
2623
2624LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2625 const char *Name) {
2626 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2627}
2628
2629LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2630 const char *Name) {
2631 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2632}
2633
2634LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2635 const char *Name) {
2636 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2637}
2638
2639LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2640 const char *Name) {
2641 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2642}
2643
2644LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2645 const char *Name) {
2646 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2647}
2648
Erick Tryzelaar31831792010-02-28 05:51:27 +00002649LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2650 LLVMValueRef LHS, LLVMValueRef RHS,
2651 const char *Name) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00002652 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
Erick Tryzelaar31831792010-02-28 05:51:27 +00002653 unwrap(RHS), Name));
2654}
2655
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002656LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2657 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2658}
2659
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002660LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2661 const char *Name) {
2662 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2663}
2664
2665LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2666 const char *Name) {
2667 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2668}
2669
Dan Gohmanf919bd62009-09-28 21:51:41 +00002670LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2671 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2672}
2673
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002674LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2675 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2676}
2677
2678/*--.. Memory ..............................................................--*/
2679
2680LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2681 const char *Name) {
Chris Lattner229907c2011-07-18 04:54:35 +00002682 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
Victor Hernandezf3db9152009-11-07 00:16:28 +00002683 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2684 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
Andrew Trickdc073ad2013-09-18 23:31:10 +00002685 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2686 ITy, unwrap(Ty), AllocSize,
Craig Topperc6207612014-04-09 06:08:46 +00002687 nullptr, nullptr, "");
Victor Hernandezf3db9152009-11-07 00:16:28 +00002688 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002689}
2690
2691LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2692 LLVMValueRef Val, const char *Name) {
Chris Lattner229907c2011-07-18 04:54:35 +00002693 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
Victor Hernandezf3db9152009-11-07 00:16:28 +00002694 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2695 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
Andrew Trickdc073ad2013-09-18 23:31:10 +00002696 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2697 ITy, unwrap(Ty), AllocSize,
Craig Topperc6207612014-04-09 06:08:46 +00002698 unwrap(Val), nullptr, "");
Victor Hernandezf3db9152009-11-07 00:16:28 +00002699 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002700}
2701
2702LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2703 const char *Name) {
Craig Topperc6207612014-04-09 06:08:46 +00002704 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002705}
2706
2707LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2708 LLVMValueRef Val, const char *Name) {
2709 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2710}
2711
2712LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
Victor Hernandezde5ad422009-10-26 23:43:48 +00002713 return wrap(unwrap(B)->Insert(
2714 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002715}
2716
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002717LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2718 const char *Name) {
2719 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2720}
2721
Andrew Trickdc073ad2013-09-18 23:31:10 +00002722LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002723 LLVMValueRef PointerVal) {
2724 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2725}
2726
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002727static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2728 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +00002729 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
2730 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
2731 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
2732 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
2733 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
2734 case LLVMAtomicOrderingAcquireRelease:
2735 return AtomicOrdering::AcquireRelease;
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002736 case LLVMAtomicOrderingSequentiallyConsistent:
JF Bastien800f87a2016-04-06 21:19:33 +00002737 return AtomicOrdering::SequentiallyConsistent;
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002738 }
Peter Zotov1d98e6d2014-10-28 19:46:44 +00002739
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002740 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2741}
2742
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00002743static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
2744 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +00002745 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
2746 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
2747 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
2748 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
2749 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
2750 case AtomicOrdering::AcquireRelease:
2751 return LLVMAtomicOrderingAcquireRelease;
2752 case AtomicOrdering::SequentiallyConsistent:
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00002753 return LLVMAtomicOrderingSequentiallyConsistent;
2754 }
2755
2756 llvm_unreachable("Invalid AtomicOrdering value!");
2757}
2758
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002759// TODO: Should this and other atomic instructions support building with
2760// "syncscope"?
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002761LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2762 LLVMBool isSingleThread, const char *Name) {
2763 return wrap(
2764 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002765 isSingleThread ? SyncScope::SingleThread
2766 : SyncScope::System,
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002767 Name));
2768}
2769
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002770LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2771 LLVMValueRef *Indices, unsigned NumIndices,
2772 const char *Name) {
Jay Foad040dd822011-07-22 08:16:57 +00002773 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
David Blaikie86ecb1b2015-03-15 01:03:19 +00002774 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002775}
2776
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002777LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2778 LLVMValueRef *Indices, unsigned NumIndices,
2779 const char *Name) {
Jay Foad040dd822011-07-22 08:16:57 +00002780 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
David Blaikieaa41cd52015-04-03 21:33:42 +00002781 return wrap(
2782 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002783}
2784
2785LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2786 unsigned Idx, const char *Name) {
David Blaikie64646022015-04-05 22:41:44 +00002787 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002788}
2789
2790LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2791 const char *Name) {
2792 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2793}
2794
2795LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2796 const char *Name) {
2797 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2798}
2799
Chris Lattner2cc6f9d2012-03-22 03:54:15 +00002800LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2801 Value *P = unwrap<Value>(MemAccessInst);
2802 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2803 return LI->isVolatile();
2804 return cast<StoreInst>(P)->isVolatile();
2805}
2806
2807void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2808 Value *P = unwrap<Value>(MemAccessInst);
2809 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2810 return LI->setVolatile(isVolatile);
2811 return cast<StoreInst>(P)->setVolatile(isVolatile);
2812}
2813
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00002814LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
2815 Value *P = unwrap<Value>(MemAccessInst);
2816 AtomicOrdering O;
2817 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2818 O = LI->getOrdering();
2819 else
2820 O = cast<StoreInst>(P)->getOrdering();
2821 return mapToLLVMOrdering(O);
2822}
2823
2824void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
2825 Value *P = unwrap<Value>(MemAccessInst);
2826 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
2827
2828 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2829 return LI->setOrdering(O);
2830 return cast<StoreInst>(P)->setOrdering(O);
2831}
2832
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002833/*--.. Casts ...............................................................--*/
2834
2835LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2836 LLVMTypeRef DestTy, const char *Name) {
2837 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2838}
2839
2840LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2841 LLVMTypeRef DestTy, const char *Name) {
2842 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2843}
2844
2845LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2846 LLVMTypeRef DestTy, const char *Name) {
2847 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2848}
2849
2850LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2851 LLVMTypeRef DestTy, const char *Name) {
2852 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2853}
2854
2855LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2856 LLVMTypeRef DestTy, const char *Name) {
2857 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2858}
2859
2860LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2861 LLVMTypeRef DestTy, const char *Name) {
2862 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2863}
2864
2865LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2866 LLVMTypeRef DestTy, const char *Name) {
2867 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2868}
2869
2870LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2871 LLVMTypeRef DestTy, const char *Name) {
2872 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2873}
2874
2875LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2876 LLVMTypeRef DestTy, const char *Name) {
2877 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2878}
2879
2880LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2881 LLVMTypeRef DestTy, const char *Name) {
2882 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2883}
2884
2885LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2886 LLVMTypeRef DestTy, const char *Name) {
2887 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2888}
2889
2890LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2891 LLVMTypeRef DestTy, const char *Name) {
2892 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2893}
2894
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002895LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2896 LLVMTypeRef DestTy, const char *Name) {
2897 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2898}
2899
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002900LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2901 LLVMTypeRef DestTy, const char *Name) {
2902 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2903 Name));
2904}
2905
2906LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2907 LLVMTypeRef DestTy, const char *Name) {
2908 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2909 Name));
2910}
2911
2912LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2913 LLVMTypeRef DestTy, const char *Name) {
2914 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2915 Name));
2916}
2917
Erick Tryzelaar31831792010-02-28 05:51:27 +00002918LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2919 LLVMTypeRef DestTy, const char *Name) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00002920 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
Erick Tryzelaar31831792010-02-28 05:51:27 +00002921 unwrap(DestTy), Name));
2922}
2923
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002924LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2925 LLVMTypeRef DestTy, const char *Name) {
2926 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2927}
2928
2929LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
Duncan Sands9d786d72009-11-23 10:49:03 +00002930 LLVMTypeRef DestTy, const char *Name) {
2931 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2932 /*isSigned*/true, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002933}
2934
2935LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2936 LLVMTypeRef DestTy, const char *Name) {
2937 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2938}
2939
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002940/*--.. Comparisons .........................................................--*/
2941
2942LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2943 LLVMValueRef LHS, LLVMValueRef RHS,
2944 const char *Name) {
2945 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2946 unwrap(LHS), unwrap(RHS), Name));
2947}
2948
2949LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2950 LLVMValueRef LHS, LLVMValueRef RHS,
2951 const char *Name) {
2952 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2953 unwrap(LHS), unwrap(RHS), Name));
2954}
2955
2956/*--.. Miscellaneous instructions ..........................................--*/
2957
2958LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
Jay Foad52131342011-03-30 11:28:46 +00002959 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002960}
2961
2962LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2963 LLVMValueRef *Args, unsigned NumArgs,
2964 const char *Name) {
Jay Foad5bd375a2011-07-15 08:37:34 +00002965 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002966 makeArrayRef(unwrap(Args), NumArgs),
Jay Foad5bd375a2011-07-15 08:37:34 +00002967 Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002968}
2969
2970LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2971 LLVMValueRef Then, LLVMValueRef Else,
2972 const char *Name) {
2973 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2974 Name));
2975}
2976
2977LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2978 LLVMTypeRef Ty, const char *Name) {
2979 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2980}
2981
2982LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2983 LLVMValueRef Index, const char *Name) {
2984 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2985 Name));
2986}
2987
2988LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2989 LLVMValueRef EltVal, LLVMValueRef Index,
2990 const char *Name) {
2991 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2992 unwrap(Index), Name));
2993}
2994
2995LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2996 LLVMValueRef V2, LLVMValueRef Mask,
2997 const char *Name) {
2998 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2999 unwrap(Mask), Name));
3000}
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003001
Dan Gohmand5104a52008-11-03 22:55:43 +00003002LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3003 unsigned Index, const char *Name) {
3004 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
3005}
3006
3007LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3008 LLVMValueRef EltVal, unsigned Index,
3009 const char *Name) {
3010 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3011 Index, Name));
3012}
3013
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00003014LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
3015 const char *Name) {
3016 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
3017}
3018
3019LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
3020 const char *Name) {
3021 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3022}
3023
3024LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
3025 LLVMValueRef RHS, const char *Name) {
3026 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
3027}
3028
Andrew Trickdc073ad2013-09-18 23:31:10 +00003029LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
3030 LLVMValueRef PTR, LLVMValueRef Val,
3031 LLVMAtomicOrdering ordering,
Carlo Kok8c6719b2013-04-23 13:21:19 +00003032 LLVMBool singleThread) {
3033 AtomicRMWInst::BinOp intop;
3034 switch (op) {
3035 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
3036 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
3037 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
3038 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
3039 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
3040 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
3041 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
3042 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
3043 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
3044 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
3045 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
3046 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00003047 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003048 mapFromLLVMOrdering(ordering), singleThread ? SyncScope::SingleThread
3049 : SyncScope::System));
Carlo Kok8c6719b2013-04-23 13:21:19 +00003050}
3051
Mehdi Amini43165d92016-03-19 21:28:28 +00003052LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
3053 LLVMValueRef Cmp, LLVMValueRef New,
3054 LLVMAtomicOrdering SuccessOrdering,
3055 LLVMAtomicOrdering FailureOrdering,
3056 LLVMBool singleThread) {
3057
3058 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
3059 unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
3060 mapFromLLVMOrdering(FailureOrdering),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003061 singleThread ? SyncScope::SingleThread : SyncScope::System));
Mehdi Amini43165d92016-03-19 21:28:28 +00003062}
3063
3064
3065LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
3066 Value *P = unwrap<Value>(AtomicInst);
3067
3068 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003069 return I->getSyncScopeID() == SyncScope::SingleThread;
3070 return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() ==
3071 SyncScope::SingleThread;
Mehdi Amini43165d92016-03-19 21:28:28 +00003072}
3073
3074void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
3075 Value *P = unwrap<Value>(AtomicInst);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003076 SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
Mehdi Amini43165d92016-03-19 21:28:28 +00003077
3078 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003079 return I->setSyncScopeID(SSID);
3080 return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID);
Mehdi Amini43165d92016-03-19 21:28:28 +00003081}
3082
3083LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
3084 Value *P = unwrap<Value>(CmpXchgInst);
3085 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
3086}
3087
3088void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
3089 LLVMAtomicOrdering Ordering) {
3090 Value *P = unwrap<Value>(CmpXchgInst);
3091 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3092
3093 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
3094}
3095
3096LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
3097 Value *P = unwrap<Value>(CmpXchgInst);
3098 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
3099}
3100
3101void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
3102 LLVMAtomicOrdering Ordering) {
3103 Value *P = unwrap<Value>(CmpXchgInst);
3104 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3105
3106 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
3107}
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003108
3109/*===-- Module providers --------------------------------------------------===*/
3110
3111LLVMModuleProviderRef
3112LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003113 return reinterpret_cast<LLVMModuleProviderRef>(M);
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003114}
3115
3116void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
3117 delete unwrap(MP);
3118}
3119
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003120
3121/*===-- Memory buffers ----------------------------------------------------===*/
3122
Chris Lattner25963c62010-01-09 22:27:07 +00003123LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
3124 const char *Path,
3125 LLVMMemoryBufferRef *OutMemBuf,
3126 char **OutMessage) {
3127
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003128 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
3129 if (std::error_code EC = MBOrErr.getError()) {
3130 *OutMessage = strdup(EC.message().c_str());
3131 return 1;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003132 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003133 *OutMemBuf = wrap(MBOrErr.get().release());
3134 return 0;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003135}
3136
Chris Lattner25963c62010-01-09 22:27:07 +00003137LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
3138 char **OutMessage) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003139 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
3140 if (std::error_code EC = MBOrErr.getError()) {
3141 *OutMessage = strdup(EC.message().c_str());
3142 return 1;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003143 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003144 *OutMemBuf = wrap(MBOrErr.get().release());
3145 return 0;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003146}
3147
Bill Wendling526276a2013-02-14 19:11:28 +00003148LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
3149 const char *InputData,
3150 size_t InputDataLength,
3151 const char *BufferName,
Bill Wendlingff61da92013-02-14 19:40:27 +00003152 LLVMBool RequiresNullTerminator) {
Bill Wendling526276a2013-02-14 19:11:28 +00003153
Rafael Espindola3560ff22014-08-27 20:03:13 +00003154 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
3155 StringRef(BufferName),
3156 RequiresNullTerminator).release());
Bill Wendling526276a2013-02-14 19:11:28 +00003157}
3158
3159LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
3160 const char *InputData,
3161 size_t InputDataLength,
3162 const char *BufferName) {
3163
Rafael Espindola3560ff22014-08-27 20:03:13 +00003164 return wrap(
3165 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
3166 StringRef(BufferName)).release());
Bill Wendling526276a2013-02-14 19:11:28 +00003167}
3168
Tom Stellard62c03202013-04-18 19:50:53 +00003169const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
Tom Stellard385fa262013-04-16 23:12:47 +00003170 return unwrap(MemBuf)->getBufferStart();
3171}
Bill Wendling526276a2013-02-14 19:11:28 +00003172
Tom Stellardb7fb7242013-04-16 23:12:51 +00003173size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
3174 return unwrap(MemBuf)->getBufferSize();
3175}
3176
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003177void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
3178 delete unwrap(MemBuf);
3179}
Dan Gohman093b42f2010-08-07 00:43:20 +00003180
Owen Anderson4698c5d2010-10-07 17:55:47 +00003181/*===-- Pass Registry -----------------------------------------------------===*/
3182
3183LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
3184 return wrap(PassRegistry::getPassRegistry());
3185}
Dan Gohman093b42f2010-08-07 00:43:20 +00003186
3187/*===-- Pass Manager ------------------------------------------------------===*/
3188
3189LLVMPassManagerRef LLVMCreatePassManager() {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003190 return wrap(new legacy::PassManager());
Dan Gohman093b42f2010-08-07 00:43:20 +00003191}
3192
3193LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003194 return wrap(new legacy::FunctionPassManager(unwrap(M)));
Dan Gohman093b42f2010-08-07 00:43:20 +00003195}
3196
3197LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
3198 return LLVMCreateFunctionPassManagerForModule(
3199 reinterpret_cast<LLVMModuleRef>(P));
3200}
3201
3202LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003203 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
Dan Gohman093b42f2010-08-07 00:43:20 +00003204}
3205
3206LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003207 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
Dan Gohman093b42f2010-08-07 00:43:20 +00003208}
3209
3210LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003211 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
Dan Gohman093b42f2010-08-07 00:43:20 +00003212}
3213
3214LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003215 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
Dan Gohman093b42f2010-08-07 00:43:20 +00003216}
3217
3218void LLVMDisposePassManager(LLVMPassManagerRef PM) {
3219 delete unwrap(PM);
3220}
Duncan Sands1cba0a82013-02-17 16:35:51 +00003221
3222/*===-- Threading ------------------------------------------------------===*/
3223
3224LLVMBool LLVMStartMultithreaded() {
Chandler Carruth39cd2162014-06-27 15:13:01 +00003225 return LLVMIsMultithreaded();
Duncan Sands1cba0a82013-02-17 16:35:51 +00003226}
3227
3228void LLVMStopMultithreaded() {
Duncan Sands1cba0a82013-02-17 16:35:51 +00003229}
3230
3231LLVMBool LLVMIsMultithreaded() {
3232 return llvm_is_multithreaded();
3233}