blob: d3c33edec18678ed71afabbb48c58ea7cf935cd9 [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}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000455
Dan Gohman518cda42011-12-17 00:04:22 +0000456LLVMTypeRef LLVMHalfType(void) {
457 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
458}
Owen Anderson55f1c092009-08-13 21:58:54 +0000459LLVMTypeRef LLVMFloatType(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000460 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000461}
462LLVMTypeRef LLVMDoubleType(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000463 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000464}
465LLVMTypeRef LLVMX86FP80Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000466 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000467}
468LLVMTypeRef LLVMFP128Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000469 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000470}
471LLVMTypeRef LLVMPPCFP128Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000472 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000473}
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000474LLVMTypeRef LLVMX86MMXType(void) {
475 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
476}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000477
478/*--.. Operations on function types ........................................--*/
479
Gordon Henriksened7beaa2007-10-06 16:05:20 +0000480LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
481 LLVMTypeRef *ParamTypes, unsigned ParamCount,
Chris Lattner25963c62010-01-09 22:27:07 +0000482 LLVMBool IsVarArg) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000483 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
Owen Anderson4056ca92009-07-29 22:17:13 +0000484 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000485}
486
Chris Lattner25963c62010-01-09 22:27:07 +0000487LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000488 return unwrap<FunctionType>(FunctionTy)->isVarArg();
489}
490
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000491LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000492 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
493}
494
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000495unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000496 return unwrap<FunctionType>(FunctionTy)->getNumParams();
497}
498
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000499void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000500 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
501 for (FunctionType::param_iterator I = Ty->param_begin(),
502 E = Ty->param_end(); I != E; ++I)
503 *Dest++ = wrap(*I);
504}
505
506/*--.. Operations on struct types ..........................................--*/
507
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000508LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
Chris Lattner25963c62010-01-09 22:27:07 +0000509 unsigned ElementCount, LLVMBool Packed) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000510 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000511 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000512}
513
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000514LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
Chris Lattner25963c62010-01-09 22:27:07 +0000515 unsigned ElementCount, LLVMBool Packed) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000516 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
517 ElementCount, Packed);
518}
519
Chris Lattnere71ccde2011-07-14 05:53:17 +0000520LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
521{
Chris Lattner01beceb2011-08-12 18:07:07 +0000522 return wrap(StructType::create(*unwrap(C), Name));
Chris Lattnere71ccde2011-07-14 05:53:17 +0000523}
524
Torok Edwin0d5f6ae2011-10-06 12:12:50 +0000525const char *LLVMGetStructName(LLVMTypeRef Ty)
526{
Torok Edwin2e9affe2011-10-14 20:37:42 +0000527 StructType *Type = unwrap<StructType>(Ty);
528 if (!Type->hasName())
Craig Topperc6207612014-04-09 06:08:46 +0000529 return nullptr;
Torok Edwin2e9affe2011-10-14 20:37:42 +0000530 return Type->getName().data();
Torok Edwin0d5f6ae2011-10-06 12:12:50 +0000531}
532
Chris Lattnere71ccde2011-07-14 05:53:17 +0000533void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
534 unsigned ElementCount, LLVMBool Packed) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000535 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
Chris Lattnere71ccde2011-07-14 05:53:17 +0000536 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
537}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000538
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000539unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000540 return unwrap<StructType>(StructTy)->getNumElements();
541}
542
543void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
544 StructType *Ty = unwrap<StructType>(StructTy);
Nick Lewyckyf735b7b2011-04-20 22:52:37 +0000545 for (StructType::element_iterator I = Ty->element_begin(),
Gordon Henriksen76a03742007-09-18 03:18:57 +0000546 E = Ty->element_end(); I != E; ++I)
547 *Dest++ = wrap(*I);
548}
549
Peter Zotovc164a3f2015-06-04 09:09:53 +0000550LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
551 StructType *Ty = unwrap<StructType>(StructTy);
552 return wrap(Ty->getTypeAtIndex(i));
553}
554
Chris Lattner25963c62010-01-09 22:27:07 +0000555LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000556 return unwrap<StructType>(StructTy)->isPacked();
557}
558
Chris Lattner17cf05b2011-07-14 16:20:28 +0000559LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
560 return unwrap<StructType>(StructTy)->isOpaque();
561}
562
563LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
564 return wrap(unwrap(M)->getTypeByName(Name));
565}
566
Gordon Henriksen76a03742007-09-18 03:18:57 +0000567/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
568
whitequarkf6059fd2017-06-05 11:49:52 +0000569void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
570 int i = 0;
571 for (auto *T : unwrap(Tp)->subtypes()) {
572 Arr[i] = wrap(T);
573 i++;
574 }
575}
576
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000577LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000578 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000579}
580
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000581LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000582 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000583}
584
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000585LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000586 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000587}
588
Peter Collingbourne45681582016-12-02 03:05:41 +0000589LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
590 auto *Ty = unwrap<Type>(WrappedTy);
591 if (auto *PTy = dyn_cast<PointerType>(Ty))
592 return wrap(PTy->getElementType());
593 return wrap(cast<SequentialType>(Ty)->getElementType());
Gordon Henriksen76a03742007-09-18 03:18:57 +0000594}
595
whitequarkf6059fd2017-06-05 11:49:52 +0000596unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
597 return unwrap(Tp)->getNumContainedTypes();
598}
599
Gordon Henriksen76a03742007-09-18 03:18:57 +0000600unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
601 return unwrap<ArrayType>(ArrayTy)->getNumElements();
602}
603
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000604unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
605 return unwrap<PointerType>(PointerTy)->getAddressSpace();
606}
607
Gordon Henriksen76a03742007-09-18 03:18:57 +0000608unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
609 return unwrap<VectorType>(VectorTy)->getNumElements();
610}
611
612/*--.. Operations on other types ...........................................--*/
613
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000614LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
615 return wrap(Type::getVoidTy(*unwrap(C)));
Owen Anderson55f1c092009-08-13 21:58:54 +0000616}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000617LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
618 return wrap(Type::getLabelTy(*unwrap(C)));
619}
whitequark131f98f2017-10-27 11:51:40 +0000620LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
621 return wrap(Type::getTokenTy(*unwrap(C)));
622}
623LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) {
624 return wrap(Type::getMetadataTy(*unwrap(C)));
625}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000626
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000627LLVMTypeRef LLVMVoidType(void) {
628 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
629}
630LLVMTypeRef LLVMLabelType(void) {
631 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
632}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000633
634/*===-- Operations on values ----------------------------------------------===*/
635
636/*--.. Operations on all values ............................................--*/
637
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000638LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000639 return wrap(unwrap(Val)->getType());
640}
641
Peter Zotov3e4561c2016-04-06 22:21:29 +0000642LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
643 switch(unwrap(Val)->getValueID()) {
644#define HANDLE_VALUE(Name) \
645 case Value::Name##Val: \
646 return LLVM##Name##ValueKind;
647#include "llvm/IR/Value.def"
648 default:
649 return LLVMInstructionValueKind;
650 }
651}
652
Gordon Henriksen76a03742007-09-18 03:18:57 +0000653const char *LLVMGetValueName(LLVMValueRef Val) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000654 return unwrap(Val)->getName().data();
Gordon Henriksen76a03742007-09-18 03:18:57 +0000655}
656
657void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
658 unwrap(Val)->setName(Name);
659}
660
Matthias Braun8c209aa2017-01-28 02:02:38 +0000661LLVM_DUMP_METHOD void LLVMDumpValue(LLVMValueRef Val) {
Sam McCalla682dfb2017-01-30 05:40:52 +0000662 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
Gordon Henriksen1d0d24c2007-10-06 00:08:49 +0000663}
664
Peter Zotovcd93b372013-11-06 09:21:01 +0000665char* LLVMPrintValueToString(LLVMValueRef Val) {
Alp Tokere69170a2014-06-26 22:52:05 +0000666 std::string buf;
667 raw_string_ostream os(buf);
Peter Zotovcd93b372013-11-06 09:21:01 +0000668
Richard Trieuc1485222014-06-21 02:43:02 +0000669 if (unwrap(Val))
670 unwrap(Val)->print(os);
671 else
672 os << "Printing <null> Value";
673
Alp Tokere69170a2014-06-26 22:52:05 +0000674 os.flush();
675
676 return strdup(buf.c_str());
Peter Zotovcd93b372013-11-06 09:21:01 +0000677}
678
Chris Lattner40cf28d2009-10-12 04:01:02 +0000679void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
680 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
681}
Gordon Henriksen29e38942008-12-19 18:39:45 +0000682
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000683int LLVMHasMetadata(LLVMValueRef Inst) {
684 return unwrap<Instruction>(Inst)->hasMetadata();
685}
686
687LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000688 auto *I = unwrap<Instruction>(Inst);
689 assert(I && "Expected instruction");
690 if (auto *MD = I->getMetadata(KindID))
691 return wrap(MetadataAsValue::get(I->getContext(), MD));
692 return nullptr;
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000693}
694
Bjorn Steinbrinka09ac002015-01-28 16:35:59 +0000695// MetadataAsValue uses a canonical format which strips the actual MDNode for
696// MDNode with just a single constant value, storing just a ConstantAsMetadata
697// This undoes this canonicalization, reconstructing the MDNode.
698static MDNode *extractMDNode(MetadataAsValue *MAV) {
699 Metadata *MD = MAV->getMetadata();
700 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
701 "Expected a metadata node or a canonicalized constant");
702
703 if (MDNode *N = dyn_cast<MDNode>(MD))
704 return N;
705
706 return MDNode::get(MAV->getContext(), MD);
707}
708
709void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
710 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
711
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000712 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000713}
714
Gordon Henriksen29e38942008-12-19 18:39:45 +0000715/*--.. Conversion functions ................................................--*/
716
717#define LLVM_DEFINE_VALUE_CAST(name) \
718 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
719 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
720 }
721
722LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
723
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000724LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
725 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
726 if (isa<MDNode>(MD->getMetadata()) ||
727 isa<ValueAsMetadata>(MD->getMetadata()))
728 return Val;
729 return nullptr;
730}
731
732LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
733 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
734 if (isa<MDString>(MD->getMetadata()))
735 return Val;
736 return nullptr;
737}
738
Chris Lattner40cf28d2009-10-12 04:01:02 +0000739/*--.. Operations on Uses ..................................................--*/
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000740LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
Chris Lattner40cf28d2009-10-12 04:01:02 +0000741 Value *V = unwrap(Val);
742 Value::use_iterator I = V->use_begin();
743 if (I == V->use_end())
Craig Topperc6207612014-04-09 06:08:46 +0000744 return nullptr;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000745 return wrap(&*I);
Chris Lattner40cf28d2009-10-12 04:01:02 +0000746}
747
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000748LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
749 Use *Next = unwrap(U)->getNext();
750 if (Next)
751 return wrap(Next);
Craig Topperc6207612014-04-09 06:08:46 +0000752 return nullptr;
Chris Lattner40cf28d2009-10-12 04:01:02 +0000753}
754
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000755LLVMValueRef LLVMGetUser(LLVMUseRef U) {
756 return wrap(unwrap(U)->getUser());
Chris Lattner40cf28d2009-10-12 04:01:02 +0000757}
758
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000759LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
760 return wrap(unwrap(U)->get());
Chris Lattner40cf28d2009-10-12 04:01:02 +0000761}
762
763/*--.. Operations on Users .................................................--*/
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000764
765static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
766 unsigned Index) {
767 Metadata *Op = N->getOperand(Index);
768 if (!Op)
769 return nullptr;
770 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
771 return wrap(C->getValue());
772 return wrap(MetadataAsValue::get(Context, Op));
773}
774
Chris Lattner40cf28d2009-10-12 04:01:02 +0000775LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
Torok Edwinfec812e2011-10-06 12:13:11 +0000776 Value *V = unwrap(Val);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000777 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
778 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
779 assert(Index == 0 && "Function-local metadata can only have one operand");
780 return wrap(L->getValue());
781 }
782 return getMDNodeOperandImpl(V->getContext(),
783 cast<MDNode>(MD->getMetadata()), Index);
784 }
785
Torok Edwinfec812e2011-10-06 12:13:11 +0000786 return wrap(cast<User>(V)->getOperand(Index));
Chris Lattner40cf28d2009-10-12 04:01:02 +0000787}
Gordon Henriksen29e38942008-12-19 18:39:45 +0000788
Peter Zotovb19f78f2014-08-12 02:55:40 +0000789LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
790 Value *V = unwrap(Val);
791 return wrap(&cast<User>(V)->getOperandUse(Index));
792}
793
Erick Tryzelaarb4d48702010-08-20 14:51:22 +0000794void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
795 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
796}
797
798int LLVMGetNumOperands(LLVMValueRef Val) {
Torok Edwinfec812e2011-10-06 12:13:11 +0000799 Value *V = unwrap(Val);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000800 if (isa<MetadataAsValue>(V))
801 return LLVMGetMDNodeNumOperands(Val);
802
Torok Edwinfec812e2011-10-06 12:13:11 +0000803 return cast<User>(V)->getNumOperands();
Erick Tryzelaarb4d48702010-08-20 14:51:22 +0000804}
805
Gordon Henriksen76a03742007-09-18 03:18:57 +0000806/*--.. Operations on constants of any type .................................--*/
807
Gordon Henriksen1046c732007-10-06 15:11:06 +0000808LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000809 return wrap(Constant::getNullValue(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000810}
811
Gordon Henriksen1046c732007-10-06 15:11:06 +0000812LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000813 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000814}
815
816LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
Owen Andersonb292b8c2009-07-30 23:03:37 +0000817 return wrap(UndefValue::get(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000818}
819
Chris Lattner25963c62010-01-09 22:27:07 +0000820LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
Gordon Henriksendc88c062007-09-18 18:07:51 +0000821 return isa<Constant>(unwrap(Ty));
822}
823
Chris Lattner25963c62010-01-09 22:27:07 +0000824LLVMBool LLVMIsNull(LLVMValueRef Val) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000825 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
826 return C->isNullValue();
827 return false;
828}
829
Chris Lattner25963c62010-01-09 22:27:07 +0000830LLVMBool LLVMIsUndef(LLVMValueRef Val) {
Gordon Henriksendc88c062007-09-18 18:07:51 +0000831 return isa<UndefValue>(unwrap(Val));
832}
833
Chris Lattner7f318242009-07-06 17:29:59 +0000834LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
Amaury Sechet9bbda192016-04-25 22:23:35 +0000835 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
Chris Lattner7f318242009-07-06 17:29:59 +0000836}
837
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000838/*--.. Operations on metadata nodes ........................................--*/
839
840LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
841 unsigned SLen) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000842 LLVMContext &Context = *unwrap(C);
843 return wrap(MetadataAsValue::get(
844 Context, MDString::get(Context, StringRef(Str, SLen))));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000845}
846
847LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
848 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
849}
850
851LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
852 unsigned Count) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000853 LLVMContext &Context = *unwrap(C);
854 SmallVector<Metadata *, 8> MDs;
855 for (auto *OV : makeArrayRef(Vals, Count)) {
856 Value *V = unwrap(OV);
857 Metadata *MD;
858 if (!V)
859 MD = nullptr;
860 else if (auto *C = dyn_cast<Constant>(V))
861 MD = ConstantAsMetadata::get(C);
862 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
863 MD = MDV->getMetadata();
864 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
865 "outside of direct argument to call");
866 } else {
867 // This is function-local metadata. Pretend to make an MDNode.
868 assert(Count == 1 &&
869 "Expected only one operand to function-local metadata");
870 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
871 }
872
873 MDs.push_back(MD);
874 }
875 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000876}
877
878LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
879 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
880}
881
Amaury Sechetf8429752017-04-17 11:52:54 +0000882LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
883 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
884}
885
886LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {
887 auto *V = unwrap(Val);
888 if (auto *C = dyn_cast<Constant>(V))
889 return wrap(ConstantAsMetadata::get(C));
890 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
891 return wrap(MAV->getMetadata());
892 return wrap(ValueAsMetadata::get(V));
893}
894
Amaury Sechet7c2883c2016-04-03 21:06:04 +0000895const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000896 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
897 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
Amaury Sechet7c2883c2016-04-03 21:06:04 +0000898 *Length = S->getString().size();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000899 return S->getString().data();
900 }
Amaury Sechet7c2883c2016-04-03 21:06:04 +0000901 *Length = 0;
Craig Topperc6207612014-04-09 06:08:46 +0000902 return nullptr;
Torok Edwinfec812e2011-10-06 12:13:11 +0000903}
904
Amaury Sechetb130f432016-04-23 00:12:45 +0000905unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000906 auto *MD = cast<MetadataAsValue>(unwrap(V));
907 if (isa<ValueAsMetadata>(MD->getMetadata()))
908 return 1;
909 return cast<MDNode>(MD->getMetadata())->getNumOperands();
Duncan Sands12ccbe72012-09-19 20:29:39 +0000910}
911
Amaury Sechetb130f432016-04-23 00:12:45 +0000912void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000913 auto *MD = cast<MetadataAsValue>(unwrap(V));
914 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
915 *Dest = wrap(MDV->getValue());
916 return;
917 }
918 const auto *N = cast<MDNode>(MD->getMetadata());
Duncan Sands12ccbe72012-09-19 20:29:39 +0000919 const unsigned numOperands = N->getNumOperands();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000920 LLVMContext &Context = unwrap(V)->getContext();
Duncan Sands12ccbe72012-09-19 20:29:39 +0000921 for (unsigned i = 0; i < numOperands; i++)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000922 Dest[i] = getMDNodeOperandImpl(Context, N, i);
Duncan Sands12ccbe72012-09-19 20:29:39 +0000923}
924
Amaury Sechetb130f432016-04-23 00:12:45 +0000925unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
926 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
Torok Edwin2e9affe2011-10-14 20:37:42 +0000927 return N->getNumOperands();
928 }
929 return 0;
Torok Edwinfec812e2011-10-06 12:13:11 +0000930}
931
Amaury Sechetb130f432016-04-23 00:12:45 +0000932void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
933 LLVMValueRef *Dest) {
934 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
Torok Edwin2e9affe2011-10-14 20:37:42 +0000935 if (!N)
936 return;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000937 LLVMContext &Context = unwrap(M)->getContext();
Torok Edwin2e9affe2011-10-14 20:37:42 +0000938 for (unsigned i=0;i<N->getNumOperands();i++)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000939 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
Torok Edwinfec812e2011-10-06 12:13:11 +0000940}
941
Amaury Sechetb130f432016-04-23 00:12:45 +0000942void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
943 LLVMValueRef Val) {
944 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
Devang Patel92245402011-12-20 19:29:36 +0000945 if (!N)
946 return;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000947 if (!Val)
948 return;
Bjorn Steinbrinka09ac002015-01-28 16:35:59 +0000949 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
Devang Patel92245402011-12-20 19:29:36 +0000950}
951
Gordon Henriksen76a03742007-09-18 03:18:57 +0000952/*--.. Operations on scalar constants ......................................--*/
953
Gordon Henriksen1046c732007-10-06 15:11:06 +0000954LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
Chris Lattner25963c62010-01-09 22:27:07 +0000955 LLVMBool SignExtend) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000956 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000957}
958
Chris Lattner4329e072010-11-23 02:47:22 +0000959LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
960 unsigned NumWords,
961 const uint64_t Words[]) {
962 IntegerType *Ty = unwrap<IntegerType>(IntTy);
963 return wrap(ConstantInt::get(Ty->getContext(),
Jeffrey Yasskin7a162882011-07-18 21:45:40 +0000964 APInt(Ty->getBitWidth(),
965 makeArrayRef(Words, NumWords))));
Chris Lattner4329e072010-11-23 02:47:22 +0000966}
967
Erick Tryzelaardd991352009-08-16 23:36:46 +0000968LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
969 uint8_t Radix) {
970 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
971 Radix));
972}
973
974LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
975 unsigned SLen, uint8_t Radix) {
976 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
977 Radix));
Gordon Henriksen931e1212008-02-02 01:07:50 +0000978}
979
Gordon Henriksen1046c732007-10-06 15:11:06 +0000980LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
Erick Tryzelaardd991352009-08-16 23:36:46 +0000981 return wrap(ConstantFP::get(unwrap(RealTy), N));
Gordon Henriksen931e1212008-02-02 01:07:50 +0000982}
983
984LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
Erick Tryzelaardd991352009-08-16 23:36:46 +0000985 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
986}
987
988LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
989 unsigned SLen) {
990 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000991}
992
Chris Lattner40cf28d2009-10-12 04:01:02 +0000993unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
994 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
995}
996
997long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
998 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
999}
1000
Peter Zotov1d98e6d2014-10-28 19:46:44 +00001001double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1002 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1003 Type *Ty = cFP->getType();
1004
1005 if (Ty->isFloatTy()) {
1006 *LosesInfo = false;
1007 return cFP->getValueAPF().convertToFloat();
1008 }
1009
1010 if (Ty->isDoubleTy()) {
1011 *LosesInfo = false;
1012 return cFP->getValueAPF().convertToDouble();
1013 }
1014
1015 bool APFLosesInfo;
1016 APFloat APF = cFP->getValueAPF();
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001017 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
Peter Zotov1d98e6d2014-10-28 19:46:44 +00001018 *LosesInfo = APFLosesInfo;
1019 return APF.convertToDouble();
1020}
1021
Gordon Henriksen76a03742007-09-18 03:18:57 +00001022/*--.. Operations on composite constants ...................................--*/
1023
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001024LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
Chris Lattner25963c62010-01-09 22:27:07 +00001025 unsigned Length,
1026 LLVMBool DontNullTerminate) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001027 /* Inverted the sense of AddNull because ', 0)' is a
1028 better mnemonic for null termination than ', 1)'. */
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001029 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1030 DontNullTerminate == 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001031}
Amaury Sechet006ce632016-03-13 00:54:40 +00001032
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001033LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
Chris Lattner25963c62010-01-09 22:27:07 +00001034 LLVMBool DontNullTerminate) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001035 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1036 DontNullTerminate);
1037}
Peter Zotovf9aa8822014-08-03 23:54:16 +00001038
Amaury Sechet006ce632016-03-13 00:54:40 +00001039LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1040 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
Peter Zotovf9aa8822014-08-03 23:54:16 +00001041}
1042
Amaury Sechet006ce632016-03-13 00:54:40 +00001043LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1044 return unwrap<ConstantDataSequential>(C)->isString();
Peter Zotovf9aa8822014-08-03 23:54:16 +00001045}
1046
Amaury Sechet7c2883c2016-04-03 21:06:04 +00001047const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1048 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1049 *Length = Str.size();
1050 return Str.data();
Peter Zotovf9aa8822014-08-03 23:54:16 +00001051}
1052
Gordon Henriksen1046c732007-10-06 15:11:06 +00001053LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1054 LLVMValueRef *ConstantVals, unsigned Length) {
Jay Foad83be3612011-06-22 09:24:39 +00001055 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1056 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001057}
Peter Zotovf9aa8822014-08-03 23:54:16 +00001058
Amaury Sechetc78768f2016-03-13 00:40:12 +00001059LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1060 LLVMValueRef *ConstantVals,
1061 unsigned Count, LLVMBool Packed) {
1062 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1063 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1064 Packed != 0));
1065}
1066
Gordon Henriksen1046c732007-10-06 15:11:06 +00001067LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
Chris Lattner25963c62010-01-09 22:27:07 +00001068 LLVMBool Packed) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001069 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1070 Packed);
Gordon Henriksen76a03742007-09-18 03:18:57 +00001071}
Rafael Espindola784ad242011-07-14 19:09:08 +00001072
1073LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1074 LLVMValueRef *ConstantVals,
1075 unsigned Count) {
1076 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
Chris Lattner229907c2011-07-18 04:54:35 +00001077 StructType *Ty = cast<StructType>(unwrap(StructTy));
Rafael Espindola784ad242011-07-14 19:09:08 +00001078
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001079 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
Rafael Espindola784ad242011-07-14 19:09:08 +00001080}
1081
Gordon Henriksen1046c732007-10-06 15:11:06 +00001082LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001083 return wrap(ConstantVector::get(makeArrayRef(
Chris Lattner69229312011-02-15 00:14:00 +00001084 unwrap<Constant>(ScalarConstantVals, Size), Size)));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001085}
Torok Edwin05dc9d62011-10-06 12:39:34 +00001086
1087/*-- Opcode mapping */
1088
1089static LLVMOpcode map_to_llvmopcode(int opcode)
1090{
1091 switch (opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00001092 default: llvm_unreachable("Unhandled Opcode.");
Torok Edwin05dc9d62011-10-06 12:39:34 +00001093#define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
Chandler Carruth9fb823b2013-01-02 11:36:10 +00001094#include "llvm/IR/Instruction.def"
Torok Edwin05dc9d62011-10-06 12:39:34 +00001095#undef HANDLE_INST
Torok Edwin05dc9d62011-10-06 12:39:34 +00001096 }
1097}
1098
1099static int map_from_llvmopcode(LLVMOpcode code)
1100{
1101 switch (code) {
1102#define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
Chandler Carruth9fb823b2013-01-02 11:36:10 +00001103#include "llvm/IR/Instruction.def"
Torok Edwin05dc9d62011-10-06 12:39:34 +00001104#undef HANDLE_INST
Torok Edwin05dc9d62011-10-06 12:39:34 +00001105 }
David Blaikie486df732012-01-16 23:24:27 +00001106 llvm_unreachable("Unhandled Opcode.");
Torok Edwin05dc9d62011-10-06 12:39:34 +00001107}
1108
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001109/*--.. Constant expressions ................................................--*/
1110
Chris Lattner40cf28d2009-10-12 04:01:02 +00001111LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00001112 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
Chris Lattner40cf28d2009-10-12 04:01:02 +00001113}
1114
Duncan Sandsd334aca2009-05-21 15:52:21 +00001115LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001116 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
Duncan Sandsd334aca2009-05-21 15:52:21 +00001117}
1118
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001119LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001120 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001121}
1122
1123LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001124 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001125}
1126
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001127LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001128 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001129}
1130
1131LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001132 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001133}
1134
1135
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001136LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001137 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001138}
1139
1140LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001141 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001142}
1143
1144LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001145 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001146 unwrap<Constant>(RHSConstant)));
1147}
1148
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001149LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1150 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001151 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001152 unwrap<Constant>(RHSConstant)));
1153}
1154
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001155LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1156 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001157 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001158 unwrap<Constant>(RHSConstant)));
1159}
1160
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001161LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001162 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001163 unwrap<Constant>(RHSConstant)));
1164}
1165
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001166LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001167 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001168 unwrap<Constant>(RHSConstant)));
1169}
1170
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001171LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1172 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001173 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001174 unwrap<Constant>(RHSConstant)));
1175}
1176
1177LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1178 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001179 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001180 unwrap<Constant>(RHSConstant)));
1181}
1182
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001183LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1184 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1185 unwrap<Constant>(RHSConstant)));
1186}
1187
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001188LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001189 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001190 unwrap<Constant>(RHSConstant)));
1191}
1192
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001193LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1194 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001195 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001196 unwrap<Constant>(RHSConstant)));
1197}
1198
1199LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1200 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001201 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001202 unwrap<Constant>(RHSConstant)));
1203}
1204
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001205LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001206 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001207 unwrap<Constant>(RHSConstant)));
1208}
1209
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001210LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001211 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001212 unwrap<Constant>(RHSConstant)));
1213}
1214
Manuel Jacob49fafb12016-10-04 23:32:42 +00001215LLVMValueRef LLVMConstExactUDiv(LLVMValueRef LHSConstant,
1216 LLVMValueRef RHSConstant) {
1217 return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1218 unwrap<Constant>(RHSConstant)));
1219}
1220
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001221LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001222 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001223 unwrap<Constant>(RHSConstant)));
1224}
1225
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001226LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
1227 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001228 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001229 unwrap<Constant>(RHSConstant)));
1230}
1231
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001232LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001233 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001234 unwrap<Constant>(RHSConstant)));
1235}
1236
1237LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001238 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001239 unwrap<Constant>(RHSConstant)));
1240}
1241
1242LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001243 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001244 unwrap<Constant>(RHSConstant)));
1245}
1246
1247LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001248 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001249 unwrap<Constant>(RHSConstant)));
1250}
1251
1252LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001253 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001254 unwrap<Constant>(RHSConstant)));
1255}
1256
1257LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001258 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001259 unwrap<Constant>(RHSConstant)));
1260}
1261
1262LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001263 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001264 unwrap<Constant>(RHSConstant)));
1265}
1266
1267LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1268 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Owen Anderson487375e2009-07-29 18:55:55 +00001269 return wrap(ConstantExpr::getICmp(Predicate,
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001270 unwrap<Constant>(LHSConstant),
1271 unwrap<Constant>(RHSConstant)));
1272}
1273
1274LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1275 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Owen Anderson487375e2009-07-29 18:55:55 +00001276 return wrap(ConstantExpr::getFCmp(Predicate,
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001277 unwrap<Constant>(LHSConstant),
1278 unwrap<Constant>(RHSConstant)));
1279}
1280
1281LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001282 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1283 unwrap<Constant>(RHSConstant)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001284}
1285
1286LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001287 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001288 unwrap<Constant>(RHSConstant)));
1289}
1290
1291LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001292 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001293 unwrap<Constant>(RHSConstant)));
1294}
1295
1296LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1297 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
Jay Foaded8db7d2011-07-21 14:31:17 +00001298 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1299 NumIndices);
David Blaikie4a2e73b2015-04-02 18:55:32 +00001300 return wrap(ConstantExpr::getGetElementPtr(
1301 nullptr, unwrap<Constant>(ConstantVal), IdxList));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001302}
1303
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001304LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1305 LLVMValueRef *ConstantIndices,
1306 unsigned NumIndices) {
1307 Constant* Val = unwrap<Constant>(ConstantVal);
Jay Foaded8db7d2011-07-21 14:31:17 +00001308 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1309 NumIndices);
David Blaikie4a2e73b2015-04-02 18:55:32 +00001310 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList));
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001311}
1312
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001313LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001314 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001315 unwrap(ToType)));
1316}
1317
1318LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001319 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001320 unwrap(ToType)));
1321}
1322
1323LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001324 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001325 unwrap(ToType)));
1326}
1327
1328LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001329 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001330 unwrap(ToType)));
1331}
1332
1333LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001334 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001335 unwrap(ToType)));
1336}
1337
1338LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001339 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001340 unwrap(ToType)));
1341}
1342
1343LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Owen Anderson487375e2009-07-29 18:55:55 +00001344 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001345 unwrap(ToType)));
1346}
1347
1348LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Owen Anderson487375e2009-07-29 18:55:55 +00001349 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001350 unwrap(ToType)));
1351}
1352
1353LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001354 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001355 unwrap(ToType)));
1356}
1357
1358LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001359 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001360 unwrap(ToType)));
1361}
1362
1363LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001364 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001365 unwrap(ToType)));
1366}
1367
1368LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001369 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001370 unwrap(ToType)));
1371}
1372
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001373LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1374 LLVMTypeRef ToType) {
1375 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1376 unwrap(ToType)));
1377}
1378
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001379LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1380 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001381 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001382 unwrap(ToType)));
1383}
1384
1385LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1386 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001387 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001388 unwrap(ToType)));
1389}
1390
1391LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1392 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001393 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001394 unwrap(ToType)));
1395}
1396
1397LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1398 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001399 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001400 unwrap(ToType)));
1401}
1402
1403LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
Chris Lattner25963c62010-01-09 22:27:07 +00001404 LLVMBool isSigned) {
Chris Lattner69229312011-02-15 00:14:00 +00001405 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1406 unwrap(ToType), isSigned));
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001407}
1408
1409LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001410 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001411 unwrap(ToType)));
1412}
1413
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001414LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1415 LLVMValueRef ConstantIfTrue,
1416 LLVMValueRef ConstantIfFalse) {
Chris Lattner69229312011-02-15 00:14:00 +00001417 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001418 unwrap<Constant>(ConstantIfTrue),
1419 unwrap<Constant>(ConstantIfFalse)));
1420}
1421
1422LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1423 LLVMValueRef IndexConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001424 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001425 unwrap<Constant>(IndexConstant)));
1426}
1427
1428LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1429 LLVMValueRef ElementValueConstant,
1430 LLVMValueRef IndexConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001431 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001432 unwrap<Constant>(ElementValueConstant),
1433 unwrap<Constant>(IndexConstant)));
1434}
1435
1436LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1437 LLVMValueRef VectorBConstant,
1438 LLVMValueRef MaskConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001439 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001440 unwrap<Constant>(VectorBConstant),
1441 unwrap<Constant>(MaskConstant)));
1442}
1443
Dan Gohmand5104a52008-11-03 22:55:43 +00001444LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1445 unsigned NumIdx) {
Chris Lattner69229312011-02-15 00:14:00 +00001446 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001447 makeArrayRef(IdxList, NumIdx)));
Dan Gohmand5104a52008-11-03 22:55:43 +00001448}
1449
1450LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1451 LLVMValueRef ElementValueConstant,
1452 unsigned *IdxList, unsigned NumIdx) {
Chris Lattner69229312011-02-15 00:14:00 +00001453 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
Dan Gohmand5104a52008-11-03 22:55:43 +00001454 unwrap<Constant>(ElementValueConstant),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001455 makeArrayRef(IdxList, NumIdx)));
Dan Gohmand5104a52008-11-03 22:55:43 +00001456}
1457
Chris Lattner25963c62010-01-09 22:27:07 +00001458LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1459 const char *Constraints,
1460 LLVMBool HasSideEffects,
Benjamin Kramer8dd0edc2012-09-10 11:52:00 +00001461 LLVMBool IsAlignStack) {
Chris Lattner25963c62010-01-09 22:27:07 +00001462 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
Benjamin Kramer8dd0edc2012-09-10 11:52:00 +00001463 Constraints, HasSideEffects, IsAlignStack));
Chris Lattner3d1f5522008-12-17 21:39:50 +00001464}
1465
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00001466LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1467 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1468}
1469
Gordon Henriksen76a03742007-09-18 03:18:57 +00001470/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1471
Gordon Henriksen265f7802008-03-19 01:11:35 +00001472LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1473 return wrap(unwrap<GlobalValue>(Global)->getParent());
1474}
1475
Chris Lattner25963c62010-01-09 22:27:07 +00001476LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001477 return unwrap<GlobalValue>(Global)->isDeclaration();
1478}
1479
1480LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001481 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001482 case GlobalValue::ExternalLinkage:
1483 return LLVMExternalLinkage;
1484 case GlobalValue::AvailableExternallyLinkage:
1485 return LLVMAvailableExternallyLinkage;
1486 case GlobalValue::LinkOnceAnyLinkage:
1487 return LLVMLinkOnceAnyLinkage;
1488 case GlobalValue::LinkOnceODRLinkage:
1489 return LLVMLinkOnceODRLinkage;
1490 case GlobalValue::WeakAnyLinkage:
1491 return LLVMWeakAnyLinkage;
1492 case GlobalValue::WeakODRLinkage:
1493 return LLVMWeakODRLinkage;
1494 case GlobalValue::AppendingLinkage:
1495 return LLVMAppendingLinkage;
1496 case GlobalValue::InternalLinkage:
1497 return LLVMInternalLinkage;
1498 case GlobalValue::PrivateLinkage:
1499 return LLVMPrivateLinkage;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001500 case GlobalValue::ExternalWeakLinkage:
1501 return LLVMExternalWeakLinkage;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001502 case GlobalValue::CommonLinkage:
1503 return LLVMCommonLinkage;
1504 }
1505
David Blaikiea5708dc2012-01-17 07:00:13 +00001506 llvm_unreachable("Invalid GlobalValue linkage!");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001507}
1508
1509void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001510 GlobalValue *GV = unwrap<GlobalValue>(Global);
1511
1512 switch (Linkage) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001513 case LLVMExternalLinkage:
1514 GV->setLinkage(GlobalValue::ExternalLinkage);
1515 break;
1516 case LLVMAvailableExternallyLinkage:
1517 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1518 break;
1519 case LLVMLinkOnceAnyLinkage:
1520 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1521 break;
1522 case LLVMLinkOnceODRLinkage:
1523 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1524 break;
Bill Wendling34bc34e2012-08-17 18:33:14 +00001525 case LLVMLinkOnceODRAutoHideLinkage:
Rafael Espindola716e7402013-11-01 17:09:14 +00001526 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1527 "longer supported.");
Bill Wendling34bc34e2012-08-17 18:33:14 +00001528 break;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001529 case LLVMWeakAnyLinkage:
1530 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1531 break;
1532 case LLVMWeakODRLinkage:
1533 GV->setLinkage(GlobalValue::WeakODRLinkage);
1534 break;
1535 case LLVMAppendingLinkage:
1536 GV->setLinkage(GlobalValue::AppendingLinkage);
1537 break;
1538 case LLVMInternalLinkage:
1539 GV->setLinkage(GlobalValue::InternalLinkage);
1540 break;
1541 case LLVMPrivateLinkage:
1542 GV->setLinkage(GlobalValue::PrivateLinkage);
1543 break;
1544 case LLVMLinkerPrivateLinkage:
Rafael Espindola2fb5bc32014-03-13 23:18:37 +00001545 GV->setLinkage(GlobalValue::PrivateLinkage);
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001546 break;
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001547 case LLVMLinkerPrivateWeakLinkage:
Rafael Espindola2fb5bc32014-03-13 23:18:37 +00001548 GV->setLinkage(GlobalValue::PrivateLinkage);
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001549 break;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001550 case LLVMDLLImportLinkage:
Nico Rieck7157bb72014-01-14 15:22:47 +00001551 DEBUG(errs()
1552 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001553 break;
1554 case LLVMDLLExportLinkage:
Nico Rieck7157bb72014-01-14 15:22:47 +00001555 DEBUG(errs()
1556 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001557 break;
1558 case LLVMExternalWeakLinkage:
1559 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1560 break;
1561 case LLVMGhostLinkage:
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001562 DEBUG(errs()
1563 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001564 break;
1565 case LLVMCommonLinkage:
1566 GV->setLinkage(GlobalValue::CommonLinkage);
1567 break;
1568 }
Gordon Henriksen76a03742007-09-18 03:18:57 +00001569}
1570
1571const char *LLVMGetSection(LLVMValueRef Global) {
Rafael Espindola83658d62016-05-11 18:21:59 +00001572 // Using .data() is safe because of how GlobalObject::setSection is
1573 // implemented.
1574 return unwrap<GlobalValue>(Global)->getSection().data();
Gordon Henriksen76a03742007-09-18 03:18:57 +00001575}
1576
1577void LLVMSetSection(LLVMValueRef Global, const char *Section) {
Rafael Espindola99e05cf2014-05-13 18:45:48 +00001578 unwrap<GlobalObject>(Global)->setSection(Section);
Gordon Henriksen76a03742007-09-18 03:18:57 +00001579}
1580
1581LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1582 return static_cast<LLVMVisibility>(
1583 unwrap<GlobalValue>(Global)->getVisibility());
1584}
1585
1586void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1587 unwrap<GlobalValue>(Global)
1588 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1589}
1590
Reid Kleckner2fae26f2014-03-05 02:34:23 +00001591LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1592 return static_cast<LLVMDLLStorageClass>(
1593 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1594}
1595
1596void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1597 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1598 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1599}
1600
Tim Northoverad96d012014-03-10 19:24:35 +00001601LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001602 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
Tim Northoverad96d012014-03-10 19:24:35 +00001603}
1604
1605void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001606 unwrap<GlobalValue>(Global)->setUnnamedAddr(
1607 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1608 : GlobalValue::UnnamedAddr::None);
Tim Northoverad96d012014-03-10 19:24:35 +00001609}
1610
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001611/*--.. Operations on global variables, load and store instructions .........--*/
1612
1613unsigned LLVMGetAlignment(LLVMValueRef V) {
1614 Value *P = unwrap<Value>(V);
1615 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1616 return GV->getAlignment();
Peter Zotov9f584e62014-03-05 05:05:34 +00001617 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1618 return AI->getAlignment();
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001619 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1620 return LI->getAlignment();
1621 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1622 return SI->getAlignment();
1623
Peter Zotov9f584e62014-03-05 05:05:34 +00001624 llvm_unreachable(
1625 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001626}
1627
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001628void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1629 Value *P = unwrap<Value>(V);
Rafael Espindola99e05cf2014-05-13 18:45:48 +00001630 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001631 GV->setAlignment(Bytes);
Peter Zotov9f584e62014-03-05 05:05:34 +00001632 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1633 AI->setAlignment(Bytes);
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001634 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1635 LI->setAlignment(Bytes);
1636 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1637 SI->setAlignment(Bytes);
Anders Waldenborga36a7822013-10-29 09:37:28 +00001638 else
Peter Zotov9f584e62014-03-05 05:05:34 +00001639 llvm_unreachable(
1640 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001641}
1642
1643/*--.. Operations on global variables ......................................--*/
1644
1645LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
Owen Andersonb17f3292009-07-08 19:03:57 +00001646 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
Craig Topperc6207612014-04-09 06:08:46 +00001647 GlobalValue::ExternalLinkage, nullptr, Name));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001648}
1649
Erick Tryzelaar06894b32010-02-28 09:46:13 +00001650LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1651 const char *Name,
1652 unsigned AddressSpace) {
1653 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
Craig Topperc6207612014-04-09 06:08:46 +00001654 GlobalValue::ExternalLinkage, nullptr, Name,
1655 nullptr, GlobalVariable::NotThreadLocal,
1656 AddressSpace));
Erick Tryzelaar06894b32010-02-28 09:46:13 +00001657}
1658
Gordon Henriksen783f7bb2007-10-08 03:45:09 +00001659LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1660 return wrap(unwrap(M)->getNamedGlobal(Name));
1661}
1662
Gordon Henriksen054817c2008-03-19 03:47:18 +00001663LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1664 Module *Mod = unwrap(M);
1665 Module::global_iterator I = Mod->global_begin();
1666 if (I == Mod->global_end())
Craig Topperc6207612014-04-09 06:08:46 +00001667 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001668 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001669}
1670
1671LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1672 Module *Mod = unwrap(M);
1673 Module::global_iterator I = Mod->global_end();
1674 if (I == Mod->global_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001675 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001676 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001677}
1678
1679LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1680 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001681 Module::global_iterator I(GV);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001682 if (++I == GV->getParent()->global_end())
Craig Topperc6207612014-04-09 06:08:46 +00001683 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001684 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001685}
1686
1687LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1688 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001689 Module::global_iterator I(GV);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001690 if (I == GV->getParent()->global_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001691 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001692 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001693}
1694
Gordon Henriksen76a03742007-09-18 03:18:57 +00001695void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1696 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1697}
1698
Gordon Henriksen76a03742007-09-18 03:18:57 +00001699LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
Chris Lattner40cf28d2009-10-12 04:01:02 +00001700 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1701 if ( !GV->hasInitializer() )
Craig Topperc6207612014-04-09 06:08:46 +00001702 return nullptr;
Chris Lattner40cf28d2009-10-12 04:01:02 +00001703 return wrap(GV->getInitializer());
Gordon Henriksen76a03742007-09-18 03:18:57 +00001704}
1705
1706void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1707 unwrap<GlobalVariable>(GlobalVar)
1708 ->setInitializer(unwrap<Constant>(ConstantVal));
1709}
1710
Chris Lattner25963c62010-01-09 22:27:07 +00001711LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001712 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1713}
1714
Chris Lattner25963c62010-01-09 22:27:07 +00001715void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001716 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1717}
1718
Chris Lattner25963c62010-01-09 22:27:07 +00001719LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
Gordon Henriksen751ebf72007-10-07 17:31:42 +00001720 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1721}
1722
Chris Lattner25963c62010-01-09 22:27:07 +00001723void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
Gordon Henriksen751ebf72007-10-07 17:31:42 +00001724 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1725}
1726
Hans Wennborg5ff71202013-04-16 08:58:59 +00001727LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1728 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1729 case GlobalVariable::NotThreadLocal:
1730 return LLVMNotThreadLocal;
1731 case GlobalVariable::GeneralDynamicTLSModel:
1732 return LLVMGeneralDynamicTLSModel;
1733 case GlobalVariable::LocalDynamicTLSModel:
1734 return LLVMLocalDynamicTLSModel;
1735 case GlobalVariable::InitialExecTLSModel:
1736 return LLVMInitialExecTLSModel;
1737 case GlobalVariable::LocalExecTLSModel:
1738 return LLVMLocalExecTLSModel;
1739 }
1740
1741 llvm_unreachable("Invalid GlobalVariable thread local mode");
1742}
1743
1744void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1745 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1746
1747 switch (Mode) {
1748 case LLVMNotThreadLocal:
1749 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1750 break;
1751 case LLVMGeneralDynamicTLSModel:
1752 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1753 break;
1754 case LLVMLocalDynamicTLSModel:
1755 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1756 break;
1757 case LLVMInitialExecTLSModel:
1758 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1759 break;
1760 case LLVMLocalExecTLSModel:
1761 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1762 break;
1763 }
1764}
1765
1766LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1767 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1768}
1769
1770void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1771 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1772}
1773
Chris Lattner3d1f5522008-12-17 21:39:50 +00001774/*--.. Operations on aliases ......................................--*/
1775
1776LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1777 const char *Name) {
Rafael Espindola4fe00942014-05-16 13:34:04 +00001778 auto *PTy = cast<PointerType>(unwrap(Ty));
David Blaikie16a2f3e2015-09-14 18:01:59 +00001779 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1780 GlobalValue::ExternalLinkage, Name,
Rafael Espindola993502e2015-02-23 21:51:06 +00001781 unwrap<Constant>(Aliasee), unwrap(M)));
Chris Lattner3d1f5522008-12-17 21:39:50 +00001782}
1783
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001784/*--.. Operations on functions .............................................--*/
1785
1786LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1787 LLVMTypeRef FunctionTy) {
Gabor Greife9ecc682008-04-06 20:25:17 +00001788 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1789 GlobalValue::ExternalLinkage, Name, unwrap(M)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001790}
1791
Gordon Henriksen783f7bb2007-10-08 03:45:09 +00001792LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1793 return wrap(unwrap(M)->getFunction(Name));
1794}
1795
Gordon Henriksen054817c2008-03-19 03:47:18 +00001796LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1797 Module *Mod = unwrap(M);
1798 Module::iterator I = Mod->begin();
1799 if (I == Mod->end())
Craig Topperc6207612014-04-09 06:08:46 +00001800 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001801 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001802}
1803
1804LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1805 Module *Mod = unwrap(M);
1806 Module::iterator I = Mod->end();
1807 if (I == Mod->begin())
Craig Topperc6207612014-04-09 06:08:46 +00001808 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001809 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001810}
1811
1812LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1813 Function *Func = unwrap<Function>(Fn);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001814 Module::iterator I(Func);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001815 if (++I == Func->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00001816 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001817 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001818}
1819
1820LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1821 Function *Func = unwrap<Function>(Fn);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001822 Module::iterator I(Func);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001823 if (I == Func->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00001824 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001825 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001826}
1827
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001828void LLVMDeleteFunction(LLVMValueRef Fn) {
1829 unwrap<Function>(Fn)->eraseFromParent();
1830}
1831
Amaury Sechete39e8532016-02-18 20:38:32 +00001832LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
1833 return unwrap<Function>(Fn)->hasPersonalityFn();
1834}
1835
Andrew Wilkins3bdfc1c2015-07-14 01:23:06 +00001836LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
1837 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
1838}
1839
1840void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
1841 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
1842}
1843
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001844unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1845 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1846 return F->getIntrinsicID();
1847 return 0;
1848}
1849
1850unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1851 return unwrap<Function>(Fn)->getCallingConv();
1852}
1853
1854void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
Sandeep Patel68c5f472009-09-02 08:44:58 +00001855 return unwrap<Function>(Fn)->setCallingConv(
1856 static_cast<CallingConv::ID>(CC));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001857}
1858
Gordon Henriksend930f912008-08-17 18:44:35 +00001859const char *LLVMGetGC(LLVMValueRef Fn) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00001860 Function *F = unwrap<Function>(Fn);
Mehdi Amini599ebf22016-01-08 02:28:20 +00001861 return F->hasGC()? F->getGC().c_str() : nullptr;
Gordon Henriksen71183b62007-12-10 03:18:06 +00001862}
1863
Gordon Henriksend930f912008-08-17 18:44:35 +00001864void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00001865 Function *F = unwrap<Function>(Fn);
Gordon Henriksend930f912008-08-17 18:44:35 +00001866 if (GC)
1867 F->setGC(GC);
Gordon Henriksen71183b62007-12-10 03:18:06 +00001868 else
Gordon Henriksend930f912008-08-17 18:44:35 +00001869 F->clearGC();
Gordon Henriksen71183b62007-12-10 03:18:06 +00001870}
1871
Amaury Sechet5db224e2016-06-12 06:17:24 +00001872void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1873 LLVMAttributeRef A) {
1874 unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
1875}
1876
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001877unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001878 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
1879 return AS.getNumAttributes();
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001880}
1881
1882void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1883 LLVMAttributeRef *Attrs) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001884 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
1885 for (auto A : AS)
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001886 *Attrs++ = wrap(A);
1887}
1888
Amaury Sechet5db224e2016-06-12 06:17:24 +00001889LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
1890 LLVMAttributeIndex Idx,
1891 unsigned KindID) {
1892 return wrap(unwrap<Function>(F)->getAttribute(Idx,
1893 (Attribute::AttrKind)KindID));
1894}
1895
Amaury Sechet6100adf2016-06-15 17:50:39 +00001896LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
1897 LLVMAttributeIndex Idx,
1898 const char *K, unsigned KLen) {
1899 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
1900}
1901
Amaury Sechet5db224e2016-06-12 06:17:24 +00001902void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1903 unsigned KindID) {
1904 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
1905}
1906
Amaury Sechet6100adf2016-06-15 17:50:39 +00001907void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1908 const char *K, unsigned KLen) {
1909 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
1910}
1911
Tom Stellarde8f35e12013-04-16 23:12:43 +00001912void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1913 const char *V) {
1914 Function *Func = unwrap<Function>(Fn);
Reid Kleckner9d16fa02017-04-19 17:28:52 +00001915 Attribute Attr = Attribute::get(Func->getContext(), A, V);
1916 Func->addAttribute(AttributeList::FunctionIndex, Attr);
Tom Stellarde8f35e12013-04-16 23:12:43 +00001917}
1918
Gordon Henriksen265f7802008-03-19 01:11:35 +00001919/*--.. Operations on parameters ............................................--*/
1920
1921unsigned LLVMCountParams(LLVMValueRef FnRef) {
1922 // This function is strictly redundant to
1923 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
Dan Gohmanc7076912008-06-21 22:06:54 +00001924 return unwrap<Function>(FnRef)->arg_size();
Gordon Henriksen265f7802008-03-19 01:11:35 +00001925}
1926
1927void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1928 Function *Fn = unwrap<Function>(FnRef);
1929 for (Function::arg_iterator I = Fn->arg_begin(),
1930 E = Fn->arg_end(); I != E; I++)
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001931 *ParamRefs++ = wrap(&*I);
Gordon Henriksen265f7802008-03-19 01:11:35 +00001932}
1933
1934LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
Reid Kleckner56d028d2017-03-17 17:16:39 +00001935 Function *Fn = unwrap<Function>(FnRef);
1936 return wrap(&Fn->arg_begin()[index]);
Gordon Henriksen265f7802008-03-19 01:11:35 +00001937}
1938
1939LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1940 return wrap(unwrap<Argument>(V)->getParent());
1941}
1942
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001943LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1944 Function *Func = unwrap<Function>(Fn);
1945 Function::arg_iterator I = Func->arg_begin();
1946 if (I == Func->arg_end())
Craig Topperc6207612014-04-09 06:08:46 +00001947 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001948 return wrap(&*I);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001949}
1950
1951LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1952 Function *Func = unwrap<Function>(Fn);
1953 Function::arg_iterator I = Func->arg_end();
1954 if (I == Func->arg_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001955 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001956 return wrap(&*--I);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001957}
1958
1959LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1960 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner56d028d2017-03-17 17:16:39 +00001961 Function *Fn = A->getParent();
1962 if (A->getArgNo() + 1 >= Fn->arg_size())
Craig Topperc6207612014-04-09 06:08:46 +00001963 return nullptr;
Reid Kleckner56d028d2017-03-17 17:16:39 +00001964 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001965}
1966
1967LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1968 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner56d028d2017-03-17 17:16:39 +00001969 if (A->getArgNo() == 0)
Craig Topperc6207612014-04-09 06:08:46 +00001970 return nullptr;
Reid Kleckner56d028d2017-03-17 17:16:39 +00001971 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001972}
1973
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00001974void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
Bill Wendling49bc76c2013-01-23 06:14:59 +00001975 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner9d16fa02017-04-19 17:28:52 +00001976 A->addAttr(Attribute::getWithAlignment(A->getContext(), align));
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00001977}
1978
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001979/*--.. Operations on basic blocks ..........................................--*/
1980
Gordon Henriksen265f7802008-03-19 01:11:35 +00001981LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1982 return wrap(static_cast<Value*>(unwrap(BB)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001983}
1984
Chris Lattner25963c62010-01-09 22:27:07 +00001985LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001986 return isa<BasicBlock>(unwrap(Val));
1987}
1988
1989LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1990 return wrap(unwrap<BasicBlock>(Val));
1991}
1992
Amaury Secheta82042e2016-02-09 22:36:41 +00001993const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
1994 return unwrap(BB)->getName().data();
1995}
1996
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001997LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1998 return wrap(unwrap(BB)->getParent());
Gordon Henriksen265f7802008-03-19 01:11:35 +00001999}
2000
Nate Begeman43c322b2011-08-23 20:27:46 +00002001LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
2002 return wrap(unwrap(BB)->getTerminator());
2003}
2004
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002005unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
Dan Gohmanc7076912008-06-21 22:06:54 +00002006 return unwrap<Function>(FnRef)->size();
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002007}
2008
2009void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2010 Function *Fn = unwrap<Function>(FnRef);
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00002011 for (BasicBlock &BB : *Fn)
2012 *BasicBlocksRefs++ = wrap(&BB);
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002013}
2014
2015LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2016 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2017}
2018
Gordon Henriksen054817c2008-03-19 03:47:18 +00002019LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2020 Function *Func = unwrap<Function>(Fn);
2021 Function::iterator I = Func->begin();
2022 if (I == Func->end())
Craig Topperc6207612014-04-09 06:08:46 +00002023 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002024 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002025}
2026
2027LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2028 Function *Func = unwrap<Function>(Fn);
2029 Function::iterator I = Func->end();
2030 if (I == Func->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002031 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002032 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002033}
2034
2035LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2036 BasicBlock *Block = unwrap(BB);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002037 Function::iterator I(Block);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002038 if (++I == Block->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00002039 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002040 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002041}
2042
2043LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2044 BasicBlock *Block = unwrap(BB);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002045 Function::iterator I(Block);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002046 if (I == Block->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002047 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002048 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002049}
2050
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002051LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2052 LLVMValueRef FnRef,
2053 const char *Name) {
2054 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002055}
2056
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002057LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2058 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2059}
2060
2061LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2062 LLVMBasicBlockRef BBRef,
2063 const char *Name) {
2064 BasicBlock *BB = unwrap(BBRef);
2065 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2066}
2067
2068LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002069 const char *Name) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002070 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002071}
2072
2073void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2074 unwrap(BBRef)->eraseFromParent();
2075}
2076
Nate Begeman43c322b2011-08-23 20:27:46 +00002077void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2078 unwrap(BBRef)->removeFromParent();
2079}
2080
Duncan Sandsb1d61aa2010-07-19 15:31:07 +00002081void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2082 unwrap(BB)->moveBefore(unwrap(MovePos));
2083}
2084
2085void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2086 unwrap(BB)->moveAfter(unwrap(MovePos));
2087}
2088
Gordon Henriksen265f7802008-03-19 01:11:35 +00002089/*--.. Operations on instructions ..........................................--*/
2090
2091LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2092 return wrap(unwrap<Instruction>(Inst)->getParent());
2093}
2094
Gordon Henriksen054817c2008-03-19 03:47:18 +00002095LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2096 BasicBlock *Block = unwrap(BB);
2097 BasicBlock::iterator I = Block->begin();
2098 if (I == Block->end())
Craig Topperc6207612014-04-09 06:08:46 +00002099 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002100 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002101}
2102
2103LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2104 BasicBlock *Block = unwrap(BB);
2105 BasicBlock::iterator I = Block->end();
2106 if (I == Block->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002107 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002108 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002109}
2110
2111LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2112 Instruction *Instr = unwrap<Instruction>(Inst);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002113 BasicBlock::iterator I(Instr);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002114 if (++I == Instr->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00002115 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002116 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002117}
2118
2119LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2120 Instruction *Instr = unwrap<Instruction>(Inst);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002121 BasicBlock::iterator I(Instr);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002122 if (I == Instr->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002123 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002124 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002125}
2126
Amaury Sechet2f432082016-02-11 21:37:54 +00002127void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2128 unwrap<Instruction>(Inst)->removeFromParent();
2129}
2130
Devang Pateldbebc6f2011-10-03 20:59:18 +00002131void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2132 unwrap<Instruction>(Inst)->eraseFromParent();
2133}
2134
Torok Edwin60c40de2011-10-06 12:13:20 +00002135LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
Torok Edwin2e9affe2011-10-14 20:37:42 +00002136 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2137 return (LLVMIntPredicate)I->getPredicate();
2138 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2139 if (CE->getOpcode() == Instruction::ICmp)
2140 return (LLVMIntPredicate)CE->getPredicate();
2141 return (LLVMIntPredicate)0;
Torok Edwin60c40de2011-10-06 12:13:20 +00002142}
2143
Peter Zotov1d98e6d2014-10-28 19:46:44 +00002144LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2145 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2146 return (LLVMRealPredicate)I->getPredicate();
2147 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2148 if (CE->getOpcode() == Instruction::FCmp)
2149 return (LLVMRealPredicate)CE->getPredicate();
2150 return (LLVMRealPredicate)0;
2151}
2152
Torok Edwinab6158e2011-10-14 20:37:49 +00002153LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2154 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2155 return map_to_llvmopcode(C->getOpcode());
2156 return (LLVMOpcode)0;
2157}
2158
Peter Zotovaff492c2014-10-17 01:02:34 +00002159LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2160 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2161 return wrap(C->clone());
2162 return nullptr;
2163}
2164
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002165/*--.. Call and invoke instructions ........................................--*/
2166
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002167unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
2168 return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands();
2169}
2170
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002171unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002172 return CallSite(unwrap<Instruction>(Instr)).getCallingConv();
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002173}
2174
2175void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002176 return CallSite(unwrap<Instruction>(Instr))
2177 .setCallingConv(static_cast<CallingConv::ID>(CC));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002178}
2179
Andrew Trickdc073ad2013-09-18 23:31:10 +00002180void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002181 unsigned align) {
2182 CallSite Call = CallSite(unwrap<Instruction>(Instr));
Reid Kleckner9d16fa02017-04-19 17:28:52 +00002183 Attribute AlignAttr = Attribute::getWithAlignment(Call->getContext(), align);
2184 Call.addAttribute(index, AlignAttr);
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002185}
2186
Amaury Secheta65a2372016-06-15 05:14:29 +00002187void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2188 LLVMAttributeRef A) {
2189 CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A));
2190}
2191
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002192unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2193 LLVMAttributeIndex Idx) {
2194 auto CS = CallSite(unwrap<Instruction>(C));
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002195 auto AS = CS.getAttributes().getAttributes(Idx);
2196 return AS.getNumAttributes();
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002197}
2198
2199void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2200 LLVMAttributeRef *Attrs) {
2201 auto CS = CallSite(unwrap<Instruction>(C));
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002202 auto AS = CS.getAttributes().getAttributes(Idx);
2203 for (auto A : AS)
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002204 *Attrs++ = wrap(A);
2205}
2206
Amaury Secheta65a2372016-06-15 05:14:29 +00002207LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
2208 LLVMAttributeIndex Idx,
2209 unsigned KindID) {
2210 return wrap(CallSite(unwrap<Instruction>(C))
2211 .getAttribute(Idx, (Attribute::AttrKind)KindID));
2212}
2213
Amaury Sechet6100adf2016-06-15 17:50:39 +00002214LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
2215 LLVMAttributeIndex Idx,
2216 const char *K, unsigned KLen) {
2217 return wrap(CallSite(unwrap<Instruction>(C))
2218 .getAttribute(Idx, StringRef(K, KLen)));
2219}
2220
Amaury Secheta65a2372016-06-15 05:14:29 +00002221void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2222 unsigned KindID) {
2223 CallSite(unwrap<Instruction>(C))
2224 .removeAttribute(Idx, (Attribute::AttrKind)KindID);
2225}
2226
Amaury Sechet6100adf2016-06-15 17:50:39 +00002227void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2228 const char *K, unsigned KLen) {
2229 CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen));
2230}
2231
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002232LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2233 return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue());
2234}
2235
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002236/*--.. Operations on call instructions (only) ..............................--*/
2237
Chris Lattner25963c62010-01-09 22:27:07 +00002238LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002239 return unwrap<CallInst>(Call)->isTailCall();
2240}
2241
Chris Lattner25963c62010-01-09 22:27:07 +00002242void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002243 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2244}
2245
Amaury Sechete39e8532016-02-18 20:38:32 +00002246/*--.. Operations on invoke instructions (only) ............................--*/
2247
2248LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2249 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2250}
2251
2252LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
2253 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2254}
2255
2256void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2257 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2258}
2259
2260void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2261 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2262}
2263
Peter Zotov2481c752014-10-28 19:46:56 +00002264/*--.. Operations on terminators ...........................................--*/
2265
2266unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2267 return unwrap<TerminatorInst>(Term)->getNumSuccessors();
2268}
2269
2270LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2271 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
2272}
2273
2274void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2275 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
2276}
2277
2278/*--.. Operations on branch instructions (only) ............................--*/
2279
2280LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2281 return unwrap<BranchInst>(Branch)->isConditional();
2282}
2283
2284LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2285 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2286}
2287
2288void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2289 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2290}
2291
Nate Begeman43c322b2011-08-23 20:27:46 +00002292/*--.. Operations on switch instructions (only) ............................--*/
2293
2294LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2295 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2296}
2297
Amaury Sechet1dcf5772016-02-09 22:50:53 +00002298/*--.. Operations on alloca instructions (only) ............................--*/
2299
2300LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
2301 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2302}
2303
Amaury Sechet053ac452016-02-17 22:51:03 +00002304/*--.. Operations on gep instructions (only) ...............................--*/
2305
2306LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
2307 return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2308}
2309
Amaury Sechet8a367d42016-05-01 02:23:14 +00002310void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
2311 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
Amaury Sechet053ac452016-02-17 22:51:03 +00002312}
2313
Gordon Henriksen44dd8fb2007-10-08 18:14:39 +00002314/*--.. Operations on phi nodes .............................................--*/
2315
2316void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2317 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2318 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2319 for (unsigned I = 0; I != Count; ++I)
2320 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2321}
2322
2323unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2324 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2325}
2326
2327LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2328 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2329}
2330
2331LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2332 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2333}
2334
Amaury Sechetaad93532016-02-10 00:38:50 +00002335/*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2336
2337unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
2338 auto *I = unwrap(Inst);
Amaury Sechet053ac452016-02-17 22:51:03 +00002339 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
2340 return GEP->getNumIndices();
Amaury Sechetaad93532016-02-10 00:38:50 +00002341 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2342 return EV->getNumIndices();
2343 if (auto *IV = dyn_cast<InsertValueInst>(I))
2344 return IV->getNumIndices();
2345 llvm_unreachable(
2346 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2347}
2348
2349const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2350 auto *I = unwrap(Inst);
2351 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2352 return EV->getIndices().data();
2353 if (auto *IV = dyn_cast<InsertValueInst>(I))
2354 return IV->getIndices().data();
2355 llvm_unreachable(
2356 "LLVMGetIndices applies only to extractvalue and insertvalue!");
2357}
2358
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002359
2360/*===-- Instruction builders ----------------------------------------------===*/
2361
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002362LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2363 return wrap(new IRBuilder<>(*unwrap(C)));
2364}
2365
Gordon Henriksena735a9c2008-05-04 12:55:34 +00002366LLVMBuilderRef LLVMCreateBuilder(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002367 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002368}
2369
Gordon Henriksen054817c2008-03-19 03:47:18 +00002370void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2371 LLVMValueRef Instr) {
2372 BasicBlock *BB = unwrap(Block);
Duncan P. N. Exon Smith43724642016-08-11 15:45:04 +00002373 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
2374 unwrap(Builder)->SetInsertPoint(BB, I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002375}
2376
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002377void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2378 Instruction *I = unwrap<Instruction>(Instr);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002379 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002380}
2381
2382void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2383 BasicBlock *BB = unwrap(Block);
2384 unwrap(Builder)->SetInsertPoint(BB);
2385}
2386
Gordon Henriksen265f7802008-03-19 01:11:35 +00002387LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2388 return wrap(unwrap(Builder)->GetInsertBlock());
2389}
2390
Chris Lattner3d1f5522008-12-17 21:39:50 +00002391void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
Chris Lattnere4bcde82010-04-01 06:31:45 +00002392 unwrap(Builder)->ClearInsertionPoint();
Chris Lattner3d1f5522008-12-17 21:39:50 +00002393}
2394
2395void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2396 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2397}
2398
Erick Tryzelaar9813bea2009-08-16 02:20:57 +00002399void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2400 const char *Name) {
2401 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2402}
2403
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002404void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2405 delete unwrap(Builder);
2406}
2407
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002408/*--.. Metadata builders ...................................................--*/
2409
2410void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002411 MDNode *Loc =
2412 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002413 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002414}
2415
2416LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002417 LLVMContext &Context = unwrap(Builder)->getContext();
2418 return wrap(MetadataAsValue::get(
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002419 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002420}
2421
2422void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2423 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2424}
2425
2426
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002427/*--.. Instruction builders ................................................--*/
2428
2429LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2430 return wrap(unwrap(B)->CreateRetVoid());
2431}
2432
2433LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2434 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2435}
2436
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002437LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2438 unsigned N) {
2439 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2440}
2441
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002442LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2443 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2444}
2445
2446LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2447 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2448 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2449}
2450
2451LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2452 LLVMBasicBlockRef Else, unsigned NumCases) {
2453 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2454}
2455
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00002456LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2457 unsigned NumDests) {
2458 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2459}
2460
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002461LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2462 LLVMValueRef *Args, unsigned NumArgs,
2463 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2464 const char *Name) {
2465 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002466 makeArrayRef(unwrap(Args), NumArgs),
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002467 Name));
2468}
2469
Bill Wendlingfae14752011-08-12 20:24:12 +00002470LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
Reid Kleckneref9828f2015-07-16 01:16:39 +00002471 LLVMValueRef PersFn, unsigned NumClauses,
2472 const char *Name) {
2473 // The personality used to live on the landingpad instruction, but now it
2474 // lives on the parent function. For compatibility, take the provided
2475 // personality and put it on the parent function.
2476 if (PersFn)
2477 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
2478 cast<Function>(unwrap(PersFn)));
David Majnemer7fddecc2015-06-17 20:52:32 +00002479 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
Bill Wendlingfae14752011-08-12 20:24:12 +00002480}
2481
Bill Wendlingf891bf82011-07-31 06:30:59 +00002482LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2483 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2484}
2485
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002486LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2487 return wrap(unwrap(B)->CreateUnreachable());
2488}
2489
Gordon Henriksen097102c2008-01-01 05:50:53 +00002490void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2491 LLVMBasicBlockRef Dest) {
2492 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2493}
2494
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00002495void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2496 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2497}
2498
Amaury Sechete39e8532016-02-18 20:38:32 +00002499unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
2500 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
2501}
2502
2503LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
2504 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
2505}
2506
Bill Wendlingfae14752011-08-12 20:24:12 +00002507void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2508 unwrap<LandingPadInst>(LandingPad)->
2509 addClause(cast<Constant>(unwrap(ClauseVal)));
2510}
2511
Amaury Sechete39e8532016-02-18 20:38:32 +00002512LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
2513 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
2514}
2515
Bill Wendlingfae14752011-08-12 20:24:12 +00002516void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2517 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2518}
2519
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002520/*--.. Arithmetic ..........................................................--*/
2521
2522LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2523 const char *Name) {
2524 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2525}
2526
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002527LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2528 const char *Name) {
2529 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2530}
2531
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002532LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2533 const char *Name) {
2534 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2535}
2536
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002537LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2538 const char *Name) {
2539 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2540}
2541
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002542LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2543 const char *Name) {
2544 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2545}
2546
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002547LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2548 const char *Name) {
2549 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2550}
2551
2552LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2553 const char *Name) {
2554 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2555}
2556
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002557LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2558 const char *Name) {
2559 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2560}
2561
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002562LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2563 const char *Name) {
2564 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2565}
2566
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002567LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2568 const char *Name) {
2569 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2570}
2571
2572LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2573 const char *Name) {
2574 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2575}
2576
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002577LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2578 const char *Name) {
2579 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2580}
2581
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002582LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2583 const char *Name) {
2584 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2585}
2586
Manuel Jacob49fafb12016-10-04 23:32:42 +00002587LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2588 LLVMValueRef RHS, const char *Name) {
2589 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
2590}
2591
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002592LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2593 const char *Name) {
2594 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2595}
2596
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002597LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2598 LLVMValueRef RHS, const char *Name) {
2599 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2600}
2601
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002602LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2603 const char *Name) {
2604 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2605}
2606
2607LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2608 const char *Name) {
2609 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2610}
2611
2612LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2613 const char *Name) {
2614 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2615}
2616
2617LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2618 const char *Name) {
2619 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2620}
2621
2622LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2623 const char *Name) {
2624 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2625}
2626
2627LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2628 const char *Name) {
2629 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2630}
2631
2632LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2633 const char *Name) {
2634 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2635}
2636
2637LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2638 const char *Name) {
2639 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2640}
2641
2642LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2643 const char *Name) {
2644 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2645}
2646
2647LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2648 const char *Name) {
2649 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2650}
2651
Erick Tryzelaar31831792010-02-28 05:51:27 +00002652LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2653 LLVMValueRef LHS, LLVMValueRef RHS,
2654 const char *Name) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00002655 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
Erick Tryzelaar31831792010-02-28 05:51:27 +00002656 unwrap(RHS), Name));
2657}
2658
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002659LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2660 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2661}
2662
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002663LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2664 const char *Name) {
2665 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2666}
2667
2668LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2669 const char *Name) {
2670 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2671}
2672
Dan Gohmanf919bd62009-09-28 21:51:41 +00002673LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2674 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2675}
2676
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002677LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2678 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2679}
2680
2681/*--.. Memory ..............................................................--*/
2682
2683LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2684 const char *Name) {
Chris Lattner229907c2011-07-18 04:54:35 +00002685 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
Victor Hernandezf3db9152009-11-07 00:16:28 +00002686 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2687 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
Andrew Trickdc073ad2013-09-18 23:31:10 +00002688 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2689 ITy, unwrap(Ty), AllocSize,
Craig Topperc6207612014-04-09 06:08:46 +00002690 nullptr, nullptr, "");
Victor Hernandezf3db9152009-11-07 00:16:28 +00002691 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002692}
2693
2694LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2695 LLVMValueRef Val, const char *Name) {
Chris Lattner229907c2011-07-18 04:54:35 +00002696 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
Victor Hernandezf3db9152009-11-07 00:16:28 +00002697 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2698 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
Andrew Trickdc073ad2013-09-18 23:31:10 +00002699 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2700 ITy, unwrap(Ty), AllocSize,
Craig Topperc6207612014-04-09 06:08:46 +00002701 unwrap(Val), nullptr, "");
Victor Hernandezf3db9152009-11-07 00:16:28 +00002702 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002703}
2704
2705LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2706 const char *Name) {
Craig Topperc6207612014-04-09 06:08:46 +00002707 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002708}
2709
2710LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2711 LLVMValueRef Val, const char *Name) {
2712 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2713}
2714
2715LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
Victor Hernandezde5ad422009-10-26 23:43:48 +00002716 return wrap(unwrap(B)->Insert(
2717 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002718}
2719
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002720LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2721 const char *Name) {
2722 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2723}
2724
Andrew Trickdc073ad2013-09-18 23:31:10 +00002725LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002726 LLVMValueRef PointerVal) {
2727 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2728}
2729
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002730static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2731 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +00002732 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
2733 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
2734 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
2735 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
2736 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
2737 case LLVMAtomicOrderingAcquireRelease:
2738 return AtomicOrdering::AcquireRelease;
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002739 case LLVMAtomicOrderingSequentiallyConsistent:
JF Bastien800f87a2016-04-06 21:19:33 +00002740 return AtomicOrdering::SequentiallyConsistent;
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002741 }
Peter Zotov1d98e6d2014-10-28 19:46:44 +00002742
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002743 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2744}
2745
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00002746static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
2747 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +00002748 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
2749 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
2750 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
2751 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
2752 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
2753 case AtomicOrdering::AcquireRelease:
2754 return LLVMAtomicOrderingAcquireRelease;
2755 case AtomicOrdering::SequentiallyConsistent:
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00002756 return LLVMAtomicOrderingSequentiallyConsistent;
2757 }
2758
2759 llvm_unreachable("Invalid AtomicOrdering value!");
2760}
2761
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002762// TODO: Should this and other atomic instructions support building with
2763// "syncscope"?
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002764LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2765 LLVMBool isSingleThread, const char *Name) {
2766 return wrap(
2767 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002768 isSingleThread ? SyncScope::SingleThread
2769 : SyncScope::System,
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002770 Name));
2771}
2772
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002773LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2774 LLVMValueRef *Indices, unsigned NumIndices,
2775 const char *Name) {
Jay Foad040dd822011-07-22 08:16:57 +00002776 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
David Blaikie86ecb1b2015-03-15 01:03:19 +00002777 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002778}
2779
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002780LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2781 LLVMValueRef *Indices, unsigned NumIndices,
2782 const char *Name) {
Jay Foad040dd822011-07-22 08:16:57 +00002783 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
David Blaikieaa41cd52015-04-03 21:33:42 +00002784 return wrap(
2785 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002786}
2787
2788LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2789 unsigned Idx, const char *Name) {
David Blaikie64646022015-04-05 22:41:44 +00002790 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002791}
2792
2793LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2794 const char *Name) {
2795 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2796}
2797
2798LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2799 const char *Name) {
2800 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2801}
2802
Chris Lattner2cc6f9d2012-03-22 03:54:15 +00002803LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2804 Value *P = unwrap<Value>(MemAccessInst);
2805 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2806 return LI->isVolatile();
2807 return cast<StoreInst>(P)->isVolatile();
2808}
2809
2810void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2811 Value *P = unwrap<Value>(MemAccessInst);
2812 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2813 return LI->setVolatile(isVolatile);
2814 return cast<StoreInst>(P)->setVolatile(isVolatile);
2815}
2816
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00002817LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
2818 Value *P = unwrap<Value>(MemAccessInst);
2819 AtomicOrdering O;
2820 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2821 O = LI->getOrdering();
2822 else
2823 O = cast<StoreInst>(P)->getOrdering();
2824 return mapToLLVMOrdering(O);
2825}
2826
2827void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
2828 Value *P = unwrap<Value>(MemAccessInst);
2829 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
2830
2831 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2832 return LI->setOrdering(O);
2833 return cast<StoreInst>(P)->setOrdering(O);
2834}
2835
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002836/*--.. Casts ...............................................................--*/
2837
2838LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2839 LLVMTypeRef DestTy, const char *Name) {
2840 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2841}
2842
2843LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2844 LLVMTypeRef DestTy, const char *Name) {
2845 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2846}
2847
2848LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2849 LLVMTypeRef DestTy, const char *Name) {
2850 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2851}
2852
2853LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2854 LLVMTypeRef DestTy, const char *Name) {
2855 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2856}
2857
2858LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2859 LLVMTypeRef DestTy, const char *Name) {
2860 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2861}
2862
2863LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2864 LLVMTypeRef DestTy, const char *Name) {
2865 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2866}
2867
2868LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2869 LLVMTypeRef DestTy, const char *Name) {
2870 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2871}
2872
2873LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2874 LLVMTypeRef DestTy, const char *Name) {
2875 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2876}
2877
2878LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2879 LLVMTypeRef DestTy, const char *Name) {
2880 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2881}
2882
2883LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2884 LLVMTypeRef DestTy, const char *Name) {
2885 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2886}
2887
2888LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2889 LLVMTypeRef DestTy, const char *Name) {
2890 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2891}
2892
2893LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2894 LLVMTypeRef DestTy, const char *Name) {
2895 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2896}
2897
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002898LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2899 LLVMTypeRef DestTy, const char *Name) {
2900 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2901}
2902
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002903LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2904 LLVMTypeRef DestTy, const char *Name) {
2905 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2906 Name));
2907}
2908
2909LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2910 LLVMTypeRef DestTy, const char *Name) {
2911 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2912 Name));
2913}
2914
2915LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2916 LLVMTypeRef DestTy, const char *Name) {
2917 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2918 Name));
2919}
2920
Erick Tryzelaar31831792010-02-28 05:51:27 +00002921LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2922 LLVMTypeRef DestTy, const char *Name) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00002923 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
Erick Tryzelaar31831792010-02-28 05:51:27 +00002924 unwrap(DestTy), Name));
2925}
2926
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002927LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2928 LLVMTypeRef DestTy, const char *Name) {
2929 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2930}
2931
2932LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
Duncan Sands9d786d72009-11-23 10:49:03 +00002933 LLVMTypeRef DestTy, const char *Name) {
2934 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2935 /*isSigned*/true, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002936}
2937
2938LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2939 LLVMTypeRef DestTy, const char *Name) {
2940 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2941}
2942
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002943/*--.. Comparisons .........................................................--*/
2944
2945LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2946 LLVMValueRef LHS, LLVMValueRef RHS,
2947 const char *Name) {
2948 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2949 unwrap(LHS), unwrap(RHS), Name));
2950}
2951
2952LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2953 LLVMValueRef LHS, LLVMValueRef RHS,
2954 const char *Name) {
2955 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2956 unwrap(LHS), unwrap(RHS), Name));
2957}
2958
2959/*--.. Miscellaneous instructions ..........................................--*/
2960
2961LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
Jay Foad52131342011-03-30 11:28:46 +00002962 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002963}
2964
2965LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2966 LLVMValueRef *Args, unsigned NumArgs,
2967 const char *Name) {
Jay Foad5bd375a2011-07-15 08:37:34 +00002968 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002969 makeArrayRef(unwrap(Args), NumArgs),
Jay Foad5bd375a2011-07-15 08:37:34 +00002970 Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002971}
2972
2973LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2974 LLVMValueRef Then, LLVMValueRef Else,
2975 const char *Name) {
2976 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2977 Name));
2978}
2979
2980LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2981 LLVMTypeRef Ty, const char *Name) {
2982 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2983}
2984
2985LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2986 LLVMValueRef Index, const char *Name) {
2987 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2988 Name));
2989}
2990
2991LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2992 LLVMValueRef EltVal, LLVMValueRef Index,
2993 const char *Name) {
2994 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2995 unwrap(Index), Name));
2996}
2997
2998LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2999 LLVMValueRef V2, LLVMValueRef Mask,
3000 const char *Name) {
3001 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
3002 unwrap(Mask), Name));
3003}
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003004
Dan Gohmand5104a52008-11-03 22:55:43 +00003005LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3006 unsigned Index, const char *Name) {
3007 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
3008}
3009
3010LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3011 LLVMValueRef EltVal, unsigned Index,
3012 const char *Name) {
3013 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3014 Index, Name));
3015}
3016
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00003017LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
3018 const char *Name) {
3019 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
3020}
3021
3022LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
3023 const char *Name) {
3024 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3025}
3026
3027LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
3028 LLVMValueRef RHS, const char *Name) {
3029 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
3030}
3031
Andrew Trickdc073ad2013-09-18 23:31:10 +00003032LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
3033 LLVMValueRef PTR, LLVMValueRef Val,
3034 LLVMAtomicOrdering ordering,
Carlo Kok8c6719b2013-04-23 13:21:19 +00003035 LLVMBool singleThread) {
3036 AtomicRMWInst::BinOp intop;
3037 switch (op) {
3038 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
3039 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
3040 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
3041 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
3042 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
3043 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
3044 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
3045 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
3046 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
3047 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
3048 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
3049 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00003050 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003051 mapFromLLVMOrdering(ordering), singleThread ? SyncScope::SingleThread
3052 : SyncScope::System));
Carlo Kok8c6719b2013-04-23 13:21:19 +00003053}
3054
Mehdi Amini43165d92016-03-19 21:28:28 +00003055LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
3056 LLVMValueRef Cmp, LLVMValueRef New,
3057 LLVMAtomicOrdering SuccessOrdering,
3058 LLVMAtomicOrdering FailureOrdering,
3059 LLVMBool singleThread) {
3060
3061 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
3062 unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
3063 mapFromLLVMOrdering(FailureOrdering),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003064 singleThread ? SyncScope::SingleThread : SyncScope::System));
Mehdi Amini43165d92016-03-19 21:28:28 +00003065}
3066
3067
3068LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
3069 Value *P = unwrap<Value>(AtomicInst);
3070
3071 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003072 return I->getSyncScopeID() == SyncScope::SingleThread;
3073 return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() ==
3074 SyncScope::SingleThread;
Mehdi Amini43165d92016-03-19 21:28:28 +00003075}
3076
3077void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
3078 Value *P = unwrap<Value>(AtomicInst);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003079 SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
Mehdi Amini43165d92016-03-19 21:28:28 +00003080
3081 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003082 return I->setSyncScopeID(SSID);
3083 return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID);
Mehdi Amini43165d92016-03-19 21:28:28 +00003084}
3085
3086LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
3087 Value *P = unwrap<Value>(CmpXchgInst);
3088 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
3089}
3090
3091void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
3092 LLVMAtomicOrdering Ordering) {
3093 Value *P = unwrap<Value>(CmpXchgInst);
3094 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3095
3096 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
3097}
3098
3099LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
3100 Value *P = unwrap<Value>(CmpXchgInst);
3101 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
3102}
3103
3104void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
3105 LLVMAtomicOrdering Ordering) {
3106 Value *P = unwrap<Value>(CmpXchgInst);
3107 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3108
3109 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
3110}
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003111
3112/*===-- Module providers --------------------------------------------------===*/
3113
3114LLVMModuleProviderRef
3115LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003116 return reinterpret_cast<LLVMModuleProviderRef>(M);
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003117}
3118
3119void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
3120 delete unwrap(MP);
3121}
3122
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003123
3124/*===-- Memory buffers ----------------------------------------------------===*/
3125
Chris Lattner25963c62010-01-09 22:27:07 +00003126LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
3127 const char *Path,
3128 LLVMMemoryBufferRef *OutMemBuf,
3129 char **OutMessage) {
3130
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003131 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
3132 if (std::error_code EC = MBOrErr.getError()) {
3133 *OutMessage = strdup(EC.message().c_str());
3134 return 1;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003135 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003136 *OutMemBuf = wrap(MBOrErr.get().release());
3137 return 0;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003138}
3139
Chris Lattner25963c62010-01-09 22:27:07 +00003140LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
3141 char **OutMessage) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003142 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
3143 if (std::error_code EC = MBOrErr.getError()) {
3144 *OutMessage = strdup(EC.message().c_str());
3145 return 1;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003146 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003147 *OutMemBuf = wrap(MBOrErr.get().release());
3148 return 0;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003149}
3150
Bill Wendling526276a2013-02-14 19:11:28 +00003151LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
3152 const char *InputData,
3153 size_t InputDataLength,
3154 const char *BufferName,
Bill Wendlingff61da92013-02-14 19:40:27 +00003155 LLVMBool RequiresNullTerminator) {
Bill Wendling526276a2013-02-14 19:11:28 +00003156
Rafael Espindola3560ff22014-08-27 20:03:13 +00003157 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
3158 StringRef(BufferName),
3159 RequiresNullTerminator).release());
Bill Wendling526276a2013-02-14 19:11:28 +00003160}
3161
3162LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
3163 const char *InputData,
3164 size_t InputDataLength,
3165 const char *BufferName) {
3166
Rafael Espindola3560ff22014-08-27 20:03:13 +00003167 return wrap(
3168 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
3169 StringRef(BufferName)).release());
Bill Wendling526276a2013-02-14 19:11:28 +00003170}
3171
Tom Stellard62c03202013-04-18 19:50:53 +00003172const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
Tom Stellard385fa262013-04-16 23:12:47 +00003173 return unwrap(MemBuf)->getBufferStart();
3174}
Bill Wendling526276a2013-02-14 19:11:28 +00003175
Tom Stellardb7fb7242013-04-16 23:12:51 +00003176size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
3177 return unwrap(MemBuf)->getBufferSize();
3178}
3179
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003180void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
3181 delete unwrap(MemBuf);
3182}
Dan Gohman093b42f2010-08-07 00:43:20 +00003183
Owen Anderson4698c5d2010-10-07 17:55:47 +00003184/*===-- Pass Registry -----------------------------------------------------===*/
3185
3186LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
3187 return wrap(PassRegistry::getPassRegistry());
3188}
Dan Gohman093b42f2010-08-07 00:43:20 +00003189
3190/*===-- Pass Manager ------------------------------------------------------===*/
3191
3192LLVMPassManagerRef LLVMCreatePassManager() {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003193 return wrap(new legacy::PassManager());
Dan Gohman093b42f2010-08-07 00:43:20 +00003194}
3195
3196LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003197 return wrap(new legacy::FunctionPassManager(unwrap(M)));
Dan Gohman093b42f2010-08-07 00:43:20 +00003198}
3199
3200LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
3201 return LLVMCreateFunctionPassManagerForModule(
3202 reinterpret_cast<LLVMModuleRef>(P));
3203}
3204
3205LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003206 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
Dan Gohman093b42f2010-08-07 00:43:20 +00003207}
3208
3209LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003210 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
Dan Gohman093b42f2010-08-07 00:43:20 +00003211}
3212
3213LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003214 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
Dan Gohman093b42f2010-08-07 00:43:20 +00003215}
3216
3217LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003218 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
Dan Gohman093b42f2010-08-07 00:43:20 +00003219}
3220
3221void LLVMDisposePassManager(LLVMPassManagerRef PM) {
3222 delete unwrap(PM);
3223}
Duncan Sands1cba0a82013-02-17 16:35:51 +00003224
3225/*===-- Threading ------------------------------------------------------===*/
3226
3227LLVMBool LLVMStartMultithreaded() {
Chandler Carruth39cd2162014-06-27 15:13:01 +00003228 return LLVMIsMultithreaded();
Duncan Sands1cba0a82013-02-17 16:35:51 +00003229}
3230
3231void LLVMStopMultithreaded() {
Duncan Sands1cba0a82013-02-17 16:35:51 +00003232}
3233
3234LLVMBool LLVMIsMultithreaded() {
3235 return llvm_is_multithreaded();
3236}