blob: cf1cd28a46c043df31c275ab15ebdc6a7d6263c2 [file] [log] [blame]
Gordon Henriksen76a03742007-09-18 03:18:57 +00001//===-- Core.cpp ----------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Gordon Henriksen76a03742007-09-18 03:18:57 +00007//
8//===----------------------------------------------------------------------===//
9//
Owen Anderson44621e42010-10-07 19:51:21 +000010// This file implements the common infrastructure (including the C bindings)
11// for libLLVMCore.a, which implements the LLVM intermediate representation.
Gordon Henriksen76a03742007-09-18 03:18:57 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm-c/Core.h"
Amaury Sechet60b31452016-04-20 01:02:12 +000016#include "llvm/ADT/StringSwitch.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/Attributes.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000018#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000020#include "llvm/IR/DerivedTypes.h"
Tom Stellard1580dc72014-04-16 17:45:04 +000021#include "llvm/IR/DiagnosticInfo.h"
22#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/GlobalAlias.h"
24#include "llvm/IR/GlobalVariable.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000025#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/InlineAsm.h"
27#include "llvm/IR/IntrinsicInst.h"
28#include "llvm/IR/LLVMContext.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000029#include "llvm/IR/LegacyPassManager.h"
Filip Pizlodec20e42013-05-01 20:59:00 +000030#include "llvm/IR/Module.h"
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000031#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000032#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000033#include "llvm/Support/FileSystem.h"
Duncan Sands1cba0a82013-02-17 16:35:51 +000034#include "llvm/Support/ManagedStatic.h"
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000035#include "llvm/Support/MemoryBuffer.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000036#include "llvm/Support/Threading.h"
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000037#include "llvm/Support/raw_ostream.h"
Gordon Henriksen76a03742007-09-18 03:18:57 +000038#include <cassert>
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000039#include <cstdlib>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000040#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000041#include <system_error>
Gordon Henriksen76a03742007-09-18 03:18:57 +000042
43using namespace llvm;
44
Chandler Carruthe96dd892014-04-21 22:55:11 +000045#define DEBUG_TYPE "ir"
46
Owen Anderson44621e42010-10-07 19:51:21 +000047void llvm::initializeCore(PassRegistry &Registry) {
Chandler Carruth73523022014-01-13 13:07:17 +000048 initializeDominatorTreeWrapperPassPass(Registry);
Chandler Carruth52eef882014-01-12 12:15:39 +000049 initializePrintModulePassWrapperPass(Registry);
50 initializePrintFunctionPassWrapperPass(Registry);
Sergei Larincd1201b2013-02-08 23:37:41 +000051 initializePrintBasicBlockPassPass(Registry);
Anna Thomas740f5292017-07-05 01:16:29 +000052 initializeSafepointIRVerifierPass(Registry);
Chandler Carruth4d356312014-01-20 11:34:08 +000053 initializeVerifierLegacyPassPass(Registry);
Owen Anderson44621e42010-10-07 19:51:21 +000054}
55
56void LLVMInitializeCore(LLVMPassRegistryRef R) {
57 initializeCore(*unwrap(R));
58}
Gordon Henriksen76a03742007-09-18 03:18:57 +000059
Duncan Sands1cba0a82013-02-17 16:35:51 +000060void LLVMShutdown() {
61 llvm_shutdown();
62}
63
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000064/*===-- Error handling ----------------------------------------------------===*/
65
Filip Pizlo3fdbaff2013-05-22 02:46:43 +000066char *LLVMCreateMessage(const char *Message) {
67 return strdup(Message);
68}
69
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000070void LLVMDisposeMessage(char *Message) {
71 free(Message);
72}
73
74
Owen Anderson6773d382009-07-01 16:58:40 +000075/*===-- Operations on contexts --------------------------------------------===*/
76
Mehdi Aminidc4c0952016-04-14 21:59:18 +000077static ManagedStatic<LLVMContext> GlobalContext;
78
Owen Anderson6773d382009-07-01 16:58:40 +000079LLVMContextRef LLVMContextCreate() {
80 return wrap(new LLVMContext());
81}
82
Mehdi Aminidc4c0952016-04-14 21:59:18 +000083LLVMContextRef LLVMGetGlobalContext() { return wrap(&*GlobalContext); }
Owen Andersonf7691d32009-07-02 00:16:38 +000084
Tom Stellard1580dc72014-04-16 17:45:04 +000085void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
86 LLVMDiagnosticHandler Handler,
87 void *DiagnosticContext) {
Vivek Pandyab5ab8952017-09-15 20:10:09 +000088 unwrap(C)->setDiagnosticHandlerCallBack(
89 LLVM_EXTENSION reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy>(
Jeroen Ketemaad659c32016-04-08 09:19:02 +000090 Handler),
Tom Stellard1580dc72014-04-16 17:45:04 +000091 DiagnosticContext);
92}
93
Jeroen Ketemaad659c32016-04-08 09:19:02 +000094LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) {
95 return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
Vivek Pandyab5ab8952017-09-15 20:10:09 +000096 unwrap(C)->getDiagnosticHandlerCallBack());
Jeroen Ketemaad659c32016-04-08 09:19:02 +000097}
98
99void *LLVMContextGetDiagnosticContext(LLVMContextRef C) {
100 return unwrap(C)->getDiagnosticContext();
101}
102
Juergen Ributzka34390c72014-05-16 02:33:15 +0000103void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
104 void *OpaqueHandle) {
105 auto YieldCallback =
106 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
107 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
108}
109
Owen Anderson6773d382009-07-01 16:58:40 +0000110void LLVMContextDispose(LLVMContextRef C) {
111 delete unwrap(C);
112}
113
Amaury Sechet56f056c2016-04-04 22:00:25 +0000114unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000115 unsigned SLen) {
116 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
117}
118
Amaury Sechet56f056c2016-04-04 22:00:25 +0000119unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000120 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
121}
122
Amaury Sechet60b31452016-04-20 01:02:12 +0000123#define GET_ATTR_KIND_FROM_NAME
124#include "AttributesCompatFunc.inc"
125
Amaury Sechet5db224e2016-06-12 06:17:24 +0000126unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
Amaury Sechet447831a2016-05-23 16:38:25 +0000127 return getAttrKindFromName(StringRef(Name, SLen));
Amaury Sechet60b31452016-04-20 01:02:12 +0000128}
129
Amaury Sechet48b06652016-06-12 07:56:21 +0000130unsigned LLVMGetLastEnumAttributeKind(void) {
Amaury Sechet5db224e2016-06-12 06:17:24 +0000131 return Attribute::AttrKind::EndAttrKinds;
132}
133
134LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID,
135 uint64_t Val) {
136 return wrap(Attribute::get(*unwrap(C), (Attribute::AttrKind)KindID, Val));
137}
138
139unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) {
140 return unwrap(A).getKindAsEnum();
141}
142
143uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) {
144 auto Attr = unwrap(A);
145 if (Attr.isEnumAttribute())
146 return 0;
147 return Attr.getValueAsInt();
148}
149
150LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
151 const char *K, unsigned KLength,
152 const char *V, unsigned VLength) {
153 return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
154 StringRef(V, VLength)));
155}
156
157const char *LLVMGetStringAttributeKind(LLVMAttributeRef A,
158 unsigned *Length) {
159 auto S = unwrap(A).getKindAsString();
160 *Length = S.size();
161 return S.data();
162}
163
164const char *LLVMGetStringAttributeValue(LLVMAttributeRef A,
165 unsigned *Length) {
166 auto S = unwrap(A).getValueAsString();
167 *Length = S.size();
168 return S.data();
169}
170
171LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) {
172 auto Attr = unwrap(A);
173 return Attr.isEnumAttribute() || Attr.isIntAttribute();
174}
175
176LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) {
177 return unwrap(A).isStringAttribute();
178}
179
Tom Stellard1580dc72014-04-16 17:45:04 +0000180char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
Alp Tokere69170a2014-06-26 22:52:05 +0000181 std::string MsgStorage;
182 raw_string_ostream Stream(MsgStorage);
183 DiagnosticPrinterRawOStream DP(Stream);
184
Tom Stellard1580dc72014-04-16 17:45:04 +0000185 unwrap(DI)->print(DP);
Alp Tokere69170a2014-06-26 22:52:05 +0000186 Stream.flush();
187
188 return LLVMCreateMessage(MsgStorage.c_str());
Tom Stellard1580dc72014-04-16 17:45:04 +0000189}
190
Amaury Sechet5984dfe2016-03-07 21:39:20 +0000191LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {
Tom Stellard1580dc72014-04-16 17:45:04 +0000192 LLVMDiagnosticSeverity severity;
193
194 switch(unwrap(DI)->getSeverity()) {
195 default:
196 severity = LLVMDSError;
197 break;
198 case DS_Warning:
199 severity = LLVMDSWarning;
200 break;
201 case DS_Remark:
202 severity = LLVMDSRemark;
203 break;
204 case DS_Note:
205 severity = LLVMDSNote;
206 break;
207 }
208
209 return severity;
210}
211
Gordon Henriksen76a03742007-09-18 03:18:57 +0000212/*===-- Operations on modules ---------------------------------------------===*/
213
Owen Anderson31d44e42009-07-02 07:17:57 +0000214LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
Mehdi Aminidc4c0952016-04-14 21:59:18 +0000215 return wrap(new Module(ModuleID, *GlobalContext));
Owen Anderson31d44e42009-07-02 07:17:57 +0000216}
217
Andrew Trickdc073ad2013-09-18 23:31:10 +0000218LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
Owen Anderson31d44e42009-07-02 07:17:57 +0000219 LLVMContextRef C) {
Owen Anderson1cf085d2009-07-01 21:22:36 +0000220 return wrap(new Module(ModuleID, *unwrap(C)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000221}
222
223void LLVMDisposeModule(LLVMModuleRef M) {
224 delete unwrap(M);
225}
226
Peter Zotov0a2fa0a2016-04-05 13:56:59 +0000227const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
228 auto &Str = unwrap(M)->getModuleIdentifier();
229 *Len = Str.length();
230 return Str.c_str();
231}
232
233void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
234 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
235}
236
Robert Widmann490a5802018-01-30 21:34:29 +0000237const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
238 auto &Str = unwrap(M)->getSourceFileName();
239 *Len = Str.length();
240 return Str.c_str();
241}
242
243void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
244 unwrap(M)->setSourceFileName(StringRef(Name, Len));
245}
Peter Zotov0a2fa0a2016-04-05 13:56:59 +0000246
Gordon Henriksen05568bb2007-12-27 20:13:47 +0000247/*--.. Data layout .........................................................--*/
Amaury Sechetf3549c42016-02-16 00:23:52 +0000248const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000249 return unwrap(M)->getDataLayoutStr().c_str();
Gordon Henriksen05568bb2007-12-27 20:13:47 +0000250}
251
Amaury Sechetf3549c42016-02-16 00:23:52 +0000252const char *LLVMGetDataLayout(LLVMModuleRef M) {
253 return LLVMGetDataLayoutStr(M);
254}
255
Amaury Sechet6ada31c2016-02-15 23:40:06 +0000256void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
257 unwrap(M)->setDataLayout(DataLayoutStr);
Gordon Henriksen05568bb2007-12-27 20:13:47 +0000258}
259
260/*--.. Target triple .......................................................--*/
261const char * LLVMGetTarget(LLVMModuleRef M) {
262 return unwrap(M)->getTargetTriple().c_str();
263}
264
265void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
266 unwrap(M)->setTargetTriple(Triple);
267}
268
Matthias Braunde58b612017-01-29 17:52:03 +0000269void LLVMDumpModule(LLVMModuleRef M) {
270 unwrap(M)->print(errs(), nullptr,
271 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
Gordon Henriksen6c6075e2008-03-14 23:58:56 +0000272}
273
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000274LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
275 char **ErrorMessage) {
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000276 std::error_code EC;
277 raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
278 if (EC) {
279 *ErrorMessage = strdup(EC.message().c_str());
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000280 return true;
281 }
282
Craig Topperc6207612014-04-09 06:08:46 +0000283 unwrap(M)->print(dest, nullptr);
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000284
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000285 dest.close();
286
287 if (dest.has_error()) {
Bob Haarman9ce2d032017-10-24 01:26:22 +0000288 std::string E = "Error printing to file: " + dest.error().message();
289 *ErrorMessage = strdup(E.c_str());
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000290 return true;
291 }
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000292
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000293 return false;
294}
295
Anders Waldenborg84355db2013-10-16 18:00:54 +0000296char *LLVMPrintModuleToString(LLVMModuleRef M) {
Alp Tokere69170a2014-06-26 22:52:05 +0000297 std::string buf;
298 raw_string_ostream os(buf);
299
Craig Topperc6207612014-04-09 06:08:46 +0000300 unwrap(M)->print(os, nullptr);
Alp Tokere69170a2014-06-26 22:52:05 +0000301 os.flush();
302
303 return strdup(buf.c_str());
Anders Waldenborg84355db2013-10-16 18:00:54 +0000304}
305
Chris Lattner26941452010-04-10 17:52:58 +0000306/*--.. Operations on inline assembler ......................................--*/
Robert Widmannf108d572018-04-06 02:31:29 +0000307void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
308 unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
309}
310
Chris Lattner26941452010-04-10 17:52:58 +0000311void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
312 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
313}
314
Robert Widmannf108d572018-04-06 02:31:29 +0000315void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
316 unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
317}
318
319const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
320 auto &Str = unwrap(M)->getModuleInlineAsm();
321 *Len = Str.length();
322 return Str.c_str();
323}
324
325LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty,
326 char *AsmString, size_t AsmStringSize,
327 char *Constraints, size_t ConstraintsSize,
328 LLVMBool HasSideEffects, LLVMBool IsAlignStack,
329 LLVMInlineAsmDialect Dialect) {
330 InlineAsm::AsmDialect AD;
331 switch (Dialect) {
332 case LLVMInlineAsmDialectATT:
333 AD = InlineAsm::AD_ATT;
Robert Widmann58568252018-04-10 18:10:10 +0000334 break;
Robert Widmannf108d572018-04-06 02:31:29 +0000335 case LLVMInlineAsmDialectIntel:
336 AD = InlineAsm::AD_Intel;
Robert Widmann58568252018-04-10 18:10:10 +0000337 break;
Robert Widmannf108d572018-04-06 02:31:29 +0000338 }
339 return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
340 StringRef(AsmString, AsmStringSize),
341 StringRef(Constraints, ConstraintsSize),
342 HasSideEffects, IsAlignStack, AD));
343}
344
Gordon Henriksen76a03742007-09-18 03:18:57 +0000345
Chris Lattnera7e04b02010-11-28 20:03:44 +0000346/*--.. Operations on module contexts ......................................--*/
347LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
348 return wrap(&unwrap(M)->getContext());
349}
350
351
Gordon Henriksen76a03742007-09-18 03:18:57 +0000352/*===-- Operations on types -----------------------------------------------===*/
353
354/*--.. Operations on all types (mostly) ....................................--*/
355
356LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000357 switch (unwrap(Ty)->getTypeID()) {
358 case Type::VoidTyID:
359 return LLVMVoidTypeKind;
Dan Gohman518cda42011-12-17 00:04:22 +0000360 case Type::HalfTyID:
361 return LLVMHalfTypeKind;
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000362 case Type::FloatTyID:
363 return LLVMFloatTypeKind;
364 case Type::DoubleTyID:
365 return LLVMDoubleTypeKind;
366 case Type::X86_FP80TyID:
367 return LLVMX86_FP80TypeKind;
368 case Type::FP128TyID:
369 return LLVMFP128TypeKind;
370 case Type::PPC_FP128TyID:
371 return LLVMPPC_FP128TypeKind;
372 case Type::LabelTyID:
373 return LLVMLabelTypeKind;
374 case Type::MetadataTyID:
375 return LLVMMetadataTypeKind;
376 case Type::IntegerTyID:
377 return LLVMIntegerTypeKind;
378 case Type::FunctionTyID:
379 return LLVMFunctionTypeKind;
380 case Type::StructTyID:
381 return LLVMStructTypeKind;
382 case Type::ArrayTyID:
383 return LLVMArrayTypeKind;
384 case Type::PointerTyID:
385 return LLVMPointerTypeKind;
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000386 case Type::VectorTyID:
387 return LLVMVectorTypeKind;
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000388 case Type::X86_MMXTyID:
389 return LLVMX86_MMXTypeKind;
David Majnemerb611e3f2015-08-14 05:09:07 +0000390 case Type::TokenTyID:
391 return LLVMTokenTypeKind;
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000392 }
Rafael Espindolaba7df702013-12-07 02:27:52 +0000393 llvm_unreachable("Unhandled TypeID.");
Gordon Henriksen76a03742007-09-18 03:18:57 +0000394}
395
Torok Edwin1cd9ade2011-10-06 12:13:28 +0000396LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
397{
398 return unwrap(Ty)->isSized();
399}
400
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000401LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
402 return wrap(&unwrap(Ty)->getContext());
403}
404
Aaron Ballman615eb472017-10-15 14:32:27 +0000405#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun8c209aa2017-01-28 02:02:38 +0000406LLVM_DUMP_METHOD void LLVMDumpType(LLVMTypeRef Ty) {
Anders Waldenborgb822cff2013-10-16 21:30:25 +0000407 return unwrap(Ty)->dump();
408}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000409#endif
Anders Waldenborgb822cff2013-10-16 21:30:25 +0000410
Anders Waldenborg47b3bd32013-10-22 06:58:34 +0000411char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
Alp Tokere69170a2014-06-26 22:52:05 +0000412 std::string buf;
413 raw_string_ostream os(buf);
Anders Waldenborg47b3bd32013-10-22 06:58:34 +0000414
Richard Trieuc1485222014-06-21 02:43:02 +0000415 if (unwrap(Ty))
416 unwrap(Ty)->print(os);
417 else
418 os << "Printing <null> Type";
419
Alp Tokere69170a2014-06-26 22:52:05 +0000420 os.flush();
421
422 return strdup(buf.c_str());
Anders Waldenborg47b3bd32013-10-22 06:58:34 +0000423}
424
Gordon Henriksen76a03742007-09-18 03:18:57 +0000425/*--.. Operations on integer types .........................................--*/
426
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000427LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
428 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000429}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000430LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
431 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000432}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000433LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
434 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000435}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000436LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
437 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000438}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000439LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
440 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
441}
Kit Barton72918022015-04-17 15:32:15 +0000442LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
443 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
444}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000445LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
446 return wrap(IntegerType::get(*unwrap(C), NumBits));
Owen Anderson55f1c092009-08-13 21:58:54 +0000447}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000448
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000449LLVMTypeRef LLVMInt1Type(void) {
450 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
451}
452LLVMTypeRef LLVMInt8Type(void) {
453 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
454}
455LLVMTypeRef LLVMInt16Type(void) {
456 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
457}
458LLVMTypeRef LLVMInt32Type(void) {
459 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
460}
461LLVMTypeRef LLVMInt64Type(void) {
462 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
463}
Kit Barton72918022015-04-17 15:32:15 +0000464LLVMTypeRef LLVMInt128Type(void) {
465 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
466}
Gordon Henriksened7beaa2007-10-06 16:05:20 +0000467LLVMTypeRef LLVMIntType(unsigned NumBits) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000468 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
Gordon Henriksen76a03742007-09-18 03:18:57 +0000469}
470
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000471unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000472 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
473}
474
475/*--.. Operations on real types ............................................--*/
476
Dan Gohman518cda42011-12-17 00:04:22 +0000477LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
478 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
479}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000480LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
481 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
482}
483LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
484 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
485}
486LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
487 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
488}
489LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
490 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
491}
492LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
493 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
494}
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000495LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
496 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
497}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000498
Dan Gohman518cda42011-12-17 00:04:22 +0000499LLVMTypeRef LLVMHalfType(void) {
500 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
501}
Owen Anderson55f1c092009-08-13 21:58:54 +0000502LLVMTypeRef LLVMFloatType(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000503 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000504}
505LLVMTypeRef LLVMDoubleType(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000506 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000507}
508LLVMTypeRef LLVMX86FP80Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000509 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000510}
511LLVMTypeRef LLVMFP128Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000512 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000513}
514LLVMTypeRef LLVMPPCFP128Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000515 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000516}
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000517LLVMTypeRef LLVMX86MMXType(void) {
518 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
519}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000520
521/*--.. Operations on function types ........................................--*/
522
Gordon Henriksened7beaa2007-10-06 16:05:20 +0000523LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
524 LLVMTypeRef *ParamTypes, unsigned ParamCount,
Chris Lattner25963c62010-01-09 22:27:07 +0000525 LLVMBool IsVarArg) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000526 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
Owen Anderson4056ca92009-07-29 22:17:13 +0000527 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000528}
529
Chris Lattner25963c62010-01-09 22:27:07 +0000530LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000531 return unwrap<FunctionType>(FunctionTy)->isVarArg();
532}
533
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000534LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000535 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
536}
537
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000538unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000539 return unwrap<FunctionType>(FunctionTy)->getNumParams();
540}
541
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000542void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000543 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
544 for (FunctionType::param_iterator I = Ty->param_begin(),
545 E = Ty->param_end(); I != E; ++I)
546 *Dest++ = wrap(*I);
547}
548
549/*--.. Operations on struct types ..........................................--*/
550
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000551LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
Chris Lattner25963c62010-01-09 22:27:07 +0000552 unsigned ElementCount, LLVMBool Packed) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000553 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000554 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000555}
556
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000557LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
Chris Lattner25963c62010-01-09 22:27:07 +0000558 unsigned ElementCount, LLVMBool Packed) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000559 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
560 ElementCount, Packed);
561}
562
Chris Lattnere71ccde2011-07-14 05:53:17 +0000563LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
564{
Chris Lattner01beceb2011-08-12 18:07:07 +0000565 return wrap(StructType::create(*unwrap(C), Name));
Chris Lattnere71ccde2011-07-14 05:53:17 +0000566}
567
Torok Edwin0d5f6ae2011-10-06 12:12:50 +0000568const char *LLVMGetStructName(LLVMTypeRef Ty)
569{
Torok Edwin2e9affe2011-10-14 20:37:42 +0000570 StructType *Type = unwrap<StructType>(Ty);
571 if (!Type->hasName())
Craig Topperc6207612014-04-09 06:08:46 +0000572 return nullptr;
Torok Edwin2e9affe2011-10-14 20:37:42 +0000573 return Type->getName().data();
Torok Edwin0d5f6ae2011-10-06 12:12:50 +0000574}
575
Chris Lattnere71ccde2011-07-14 05:53:17 +0000576void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
577 unsigned ElementCount, LLVMBool Packed) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000578 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
Chris Lattnere71ccde2011-07-14 05:53:17 +0000579 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
580}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000581
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000582unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000583 return unwrap<StructType>(StructTy)->getNumElements();
584}
585
586void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
587 StructType *Ty = unwrap<StructType>(StructTy);
Nick Lewyckyf735b7b2011-04-20 22:52:37 +0000588 for (StructType::element_iterator I = Ty->element_begin(),
Gordon Henriksen76a03742007-09-18 03:18:57 +0000589 E = Ty->element_end(); I != E; ++I)
590 *Dest++ = wrap(*I);
591}
592
Peter Zotovc164a3f2015-06-04 09:09:53 +0000593LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
594 StructType *Ty = unwrap<StructType>(StructTy);
595 return wrap(Ty->getTypeAtIndex(i));
596}
597
Chris Lattner25963c62010-01-09 22:27:07 +0000598LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000599 return unwrap<StructType>(StructTy)->isPacked();
600}
601
Chris Lattner17cf05b2011-07-14 16:20:28 +0000602LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
603 return unwrap<StructType>(StructTy)->isOpaque();
604}
605
606LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
607 return wrap(unwrap(M)->getTypeByName(Name));
608}
609
Gordon Henriksen76a03742007-09-18 03:18:57 +0000610/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
611
whitequarkf6059fd2017-06-05 11:49:52 +0000612void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
613 int i = 0;
614 for (auto *T : unwrap(Tp)->subtypes()) {
615 Arr[i] = wrap(T);
616 i++;
617 }
618}
619
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000620LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000621 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000622}
623
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000624LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000625 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000626}
627
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000628LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000629 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000630}
631
Peter Collingbourne45681582016-12-02 03:05:41 +0000632LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
633 auto *Ty = unwrap<Type>(WrappedTy);
634 if (auto *PTy = dyn_cast<PointerType>(Ty))
635 return wrap(PTy->getElementType());
636 return wrap(cast<SequentialType>(Ty)->getElementType());
Gordon Henriksen76a03742007-09-18 03:18:57 +0000637}
638
whitequarkf6059fd2017-06-05 11:49:52 +0000639unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
640 return unwrap(Tp)->getNumContainedTypes();
641}
642
Gordon Henriksen76a03742007-09-18 03:18:57 +0000643unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
644 return unwrap<ArrayType>(ArrayTy)->getNumElements();
645}
646
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000647unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
648 return unwrap<PointerType>(PointerTy)->getAddressSpace();
649}
650
Gordon Henriksen76a03742007-09-18 03:18:57 +0000651unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
652 return unwrap<VectorType>(VectorTy)->getNumElements();
653}
654
655/*--.. Operations on other types ...........................................--*/
656
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000657LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
658 return wrap(Type::getVoidTy(*unwrap(C)));
Owen Anderson55f1c092009-08-13 21:58:54 +0000659}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000660LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
661 return wrap(Type::getLabelTy(*unwrap(C)));
662}
whitequark131f98f2017-10-27 11:51:40 +0000663LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
664 return wrap(Type::getTokenTy(*unwrap(C)));
665}
666LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) {
667 return wrap(Type::getMetadataTy(*unwrap(C)));
668}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000669
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000670LLVMTypeRef LLVMVoidType(void) {
671 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
672}
673LLVMTypeRef LLVMLabelType(void) {
674 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
675}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000676
677/*===-- Operations on values ----------------------------------------------===*/
678
679/*--.. Operations on all values ............................................--*/
680
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000681LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000682 return wrap(unwrap(Val)->getType());
683}
684
Peter Zotov3e4561c2016-04-06 22:21:29 +0000685LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
686 switch(unwrap(Val)->getValueID()) {
687#define HANDLE_VALUE(Name) \
688 case Value::Name##Val: \
689 return LLVM##Name##ValueKind;
690#include "llvm/IR/Value.def"
691 default:
692 return LLVMInstructionValueKind;
693 }
694}
695
Gordon Henriksen76a03742007-09-18 03:18:57 +0000696const char *LLVMGetValueName(LLVMValueRef Val) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000697 return unwrap(Val)->getName().data();
Gordon Henriksen76a03742007-09-18 03:18:57 +0000698}
699
700void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
701 unwrap(Val)->setName(Name);
702}
703
Matthias Braun8c209aa2017-01-28 02:02:38 +0000704LLVM_DUMP_METHOD void LLVMDumpValue(LLVMValueRef Val) {
Sam McCalla682dfb2017-01-30 05:40:52 +0000705 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
Gordon Henriksen1d0d24c2007-10-06 00:08:49 +0000706}
707
Peter Zotovcd93b372013-11-06 09:21:01 +0000708char* LLVMPrintValueToString(LLVMValueRef Val) {
Alp Tokere69170a2014-06-26 22:52:05 +0000709 std::string buf;
710 raw_string_ostream os(buf);
Peter Zotovcd93b372013-11-06 09:21:01 +0000711
Richard Trieuc1485222014-06-21 02:43:02 +0000712 if (unwrap(Val))
713 unwrap(Val)->print(os);
714 else
715 os << "Printing <null> Value";
716
Alp Tokere69170a2014-06-26 22:52:05 +0000717 os.flush();
718
719 return strdup(buf.c_str());
Peter Zotovcd93b372013-11-06 09:21:01 +0000720}
721
Chris Lattner40cf28d2009-10-12 04:01:02 +0000722void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
723 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
724}
Gordon Henriksen29e38942008-12-19 18:39:45 +0000725
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000726int LLVMHasMetadata(LLVMValueRef Inst) {
727 return unwrap<Instruction>(Inst)->hasMetadata();
728}
729
730LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000731 auto *I = unwrap<Instruction>(Inst);
732 assert(I && "Expected instruction");
733 if (auto *MD = I->getMetadata(KindID))
734 return wrap(MetadataAsValue::get(I->getContext(), MD));
735 return nullptr;
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000736}
737
Bjorn Steinbrinka09ac002015-01-28 16:35:59 +0000738// MetadataAsValue uses a canonical format which strips the actual MDNode for
739// MDNode with just a single constant value, storing just a ConstantAsMetadata
740// This undoes this canonicalization, reconstructing the MDNode.
741static MDNode *extractMDNode(MetadataAsValue *MAV) {
742 Metadata *MD = MAV->getMetadata();
743 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
744 "Expected a metadata node or a canonicalized constant");
745
746 if (MDNode *N = dyn_cast<MDNode>(MD))
747 return N;
748
749 return MDNode::get(MAV->getContext(), MD);
750}
751
752void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
753 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
754
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000755 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000756}
757
Gordon Henriksen29e38942008-12-19 18:39:45 +0000758/*--.. Conversion functions ................................................--*/
759
760#define LLVM_DEFINE_VALUE_CAST(name) \
761 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
762 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
763 }
764
765LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
766
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000767LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
768 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
769 if (isa<MDNode>(MD->getMetadata()) ||
770 isa<ValueAsMetadata>(MD->getMetadata()))
771 return Val;
772 return nullptr;
773}
774
775LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
776 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
777 if (isa<MDString>(MD->getMetadata()))
778 return Val;
779 return nullptr;
780}
781
Chris Lattner40cf28d2009-10-12 04:01:02 +0000782/*--.. Operations on Uses ..................................................--*/
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000783LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
Chris Lattner40cf28d2009-10-12 04:01:02 +0000784 Value *V = unwrap(Val);
785 Value::use_iterator I = V->use_begin();
786 if (I == V->use_end())
Craig Topperc6207612014-04-09 06:08:46 +0000787 return nullptr;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000788 return wrap(&*I);
Chris Lattner40cf28d2009-10-12 04:01:02 +0000789}
790
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000791LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
792 Use *Next = unwrap(U)->getNext();
793 if (Next)
794 return wrap(Next);
Craig Topperc6207612014-04-09 06:08:46 +0000795 return nullptr;
Chris Lattner40cf28d2009-10-12 04:01:02 +0000796}
797
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000798LLVMValueRef LLVMGetUser(LLVMUseRef U) {
799 return wrap(unwrap(U)->getUser());
Chris Lattner40cf28d2009-10-12 04:01:02 +0000800}
801
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000802LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
803 return wrap(unwrap(U)->get());
Chris Lattner40cf28d2009-10-12 04:01:02 +0000804}
805
806/*--.. Operations on Users .................................................--*/
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000807
808static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
809 unsigned Index) {
810 Metadata *Op = N->getOperand(Index);
811 if (!Op)
812 return nullptr;
813 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
814 return wrap(C->getValue());
815 return wrap(MetadataAsValue::get(Context, Op));
816}
817
Chris Lattner40cf28d2009-10-12 04:01:02 +0000818LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
Torok Edwinfec812e2011-10-06 12:13:11 +0000819 Value *V = unwrap(Val);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000820 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
821 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
822 assert(Index == 0 && "Function-local metadata can only have one operand");
823 return wrap(L->getValue());
824 }
825 return getMDNodeOperandImpl(V->getContext(),
826 cast<MDNode>(MD->getMetadata()), Index);
827 }
828
Torok Edwinfec812e2011-10-06 12:13:11 +0000829 return wrap(cast<User>(V)->getOperand(Index));
Chris Lattner40cf28d2009-10-12 04:01:02 +0000830}
Gordon Henriksen29e38942008-12-19 18:39:45 +0000831
Peter Zotovb19f78f2014-08-12 02:55:40 +0000832LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
833 Value *V = unwrap(Val);
834 return wrap(&cast<User>(V)->getOperandUse(Index));
835}
836
Erick Tryzelaarb4d48702010-08-20 14:51:22 +0000837void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
838 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
839}
840
841int LLVMGetNumOperands(LLVMValueRef Val) {
Torok Edwinfec812e2011-10-06 12:13:11 +0000842 Value *V = unwrap(Val);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000843 if (isa<MetadataAsValue>(V))
844 return LLVMGetMDNodeNumOperands(Val);
845
Torok Edwinfec812e2011-10-06 12:13:11 +0000846 return cast<User>(V)->getNumOperands();
Erick Tryzelaarb4d48702010-08-20 14:51:22 +0000847}
848
Gordon Henriksen76a03742007-09-18 03:18:57 +0000849/*--.. Operations on constants of any type .................................--*/
850
Gordon Henriksen1046c732007-10-06 15:11:06 +0000851LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000852 return wrap(Constant::getNullValue(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000853}
854
Gordon Henriksen1046c732007-10-06 15:11:06 +0000855LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000856 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000857}
858
859LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
Owen Andersonb292b8c2009-07-30 23:03:37 +0000860 return wrap(UndefValue::get(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000861}
862
Chris Lattner25963c62010-01-09 22:27:07 +0000863LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
Gordon Henriksendc88c062007-09-18 18:07:51 +0000864 return isa<Constant>(unwrap(Ty));
865}
866
Chris Lattner25963c62010-01-09 22:27:07 +0000867LLVMBool LLVMIsNull(LLVMValueRef Val) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000868 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
869 return C->isNullValue();
870 return false;
871}
872
Chris Lattner25963c62010-01-09 22:27:07 +0000873LLVMBool LLVMIsUndef(LLVMValueRef Val) {
Gordon Henriksendc88c062007-09-18 18:07:51 +0000874 return isa<UndefValue>(unwrap(Val));
875}
876
Chris Lattner7f318242009-07-06 17:29:59 +0000877LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
Amaury Sechet9bbda192016-04-25 22:23:35 +0000878 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
Chris Lattner7f318242009-07-06 17:29:59 +0000879}
880
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000881/*--.. Operations on metadata nodes ........................................--*/
882
883LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
884 unsigned SLen) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000885 LLVMContext &Context = *unwrap(C);
886 return wrap(MetadataAsValue::get(
887 Context, MDString::get(Context, StringRef(Str, SLen))));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000888}
889
890LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
891 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
892}
893
894LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
895 unsigned Count) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000896 LLVMContext &Context = *unwrap(C);
897 SmallVector<Metadata *, 8> MDs;
898 for (auto *OV : makeArrayRef(Vals, Count)) {
899 Value *V = unwrap(OV);
900 Metadata *MD;
901 if (!V)
902 MD = nullptr;
903 else if (auto *C = dyn_cast<Constant>(V))
904 MD = ConstantAsMetadata::get(C);
905 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
906 MD = MDV->getMetadata();
907 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
908 "outside of direct argument to call");
909 } else {
910 // This is function-local metadata. Pretend to make an MDNode.
911 assert(Count == 1 &&
912 "Expected only one operand to function-local metadata");
913 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
914 }
915
916 MDs.push_back(MD);
917 }
918 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000919}
920
921LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
922 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
923}
924
Amaury Sechetf8429752017-04-17 11:52:54 +0000925LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
926 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
927}
928
929LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {
930 auto *V = unwrap(Val);
931 if (auto *C = dyn_cast<Constant>(V))
932 return wrap(ConstantAsMetadata::get(C));
933 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
934 return wrap(MAV->getMetadata());
935 return wrap(ValueAsMetadata::get(V));
936}
937
Amaury Sechet7c2883c2016-04-03 21:06:04 +0000938const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000939 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
940 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
Amaury Sechet7c2883c2016-04-03 21:06:04 +0000941 *Length = S->getString().size();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000942 return S->getString().data();
943 }
Amaury Sechet7c2883c2016-04-03 21:06:04 +0000944 *Length = 0;
Craig Topperc6207612014-04-09 06:08:46 +0000945 return nullptr;
Torok Edwinfec812e2011-10-06 12:13:11 +0000946}
947
Amaury Sechetb130f432016-04-23 00:12:45 +0000948unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000949 auto *MD = cast<MetadataAsValue>(unwrap(V));
950 if (isa<ValueAsMetadata>(MD->getMetadata()))
951 return 1;
952 return cast<MDNode>(MD->getMetadata())->getNumOperands();
Duncan Sands12ccbe72012-09-19 20:29:39 +0000953}
954
Amaury Sechetb130f432016-04-23 00:12:45 +0000955void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000956 auto *MD = cast<MetadataAsValue>(unwrap(V));
957 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
958 *Dest = wrap(MDV->getValue());
959 return;
960 }
961 const auto *N = cast<MDNode>(MD->getMetadata());
Duncan Sands12ccbe72012-09-19 20:29:39 +0000962 const unsigned numOperands = N->getNumOperands();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000963 LLVMContext &Context = unwrap(V)->getContext();
Duncan Sands12ccbe72012-09-19 20:29:39 +0000964 for (unsigned i = 0; i < numOperands; i++)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000965 Dest[i] = getMDNodeOperandImpl(Context, N, i);
Duncan Sands12ccbe72012-09-19 20:29:39 +0000966}
967
Amaury Sechetb130f432016-04-23 00:12:45 +0000968unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
969 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
Torok Edwin2e9affe2011-10-14 20:37:42 +0000970 return N->getNumOperands();
971 }
972 return 0;
Torok Edwinfec812e2011-10-06 12:13:11 +0000973}
974
Amaury Sechetb130f432016-04-23 00:12:45 +0000975void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
976 LLVMValueRef *Dest) {
977 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
Torok Edwin2e9affe2011-10-14 20:37:42 +0000978 if (!N)
979 return;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000980 LLVMContext &Context = unwrap(M)->getContext();
Torok Edwin2e9affe2011-10-14 20:37:42 +0000981 for (unsigned i=0;i<N->getNumOperands();i++)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000982 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
Torok Edwinfec812e2011-10-06 12:13:11 +0000983}
984
Amaury Sechetb130f432016-04-23 00:12:45 +0000985void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
986 LLVMValueRef Val) {
987 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
Devang Patel92245402011-12-20 19:29:36 +0000988 if (!N)
989 return;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000990 if (!Val)
991 return;
Bjorn Steinbrinka09ac002015-01-28 16:35:59 +0000992 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
Devang Patel92245402011-12-20 19:29:36 +0000993}
994
Gordon Henriksen76a03742007-09-18 03:18:57 +0000995/*--.. Operations on scalar constants ......................................--*/
996
Gordon Henriksen1046c732007-10-06 15:11:06 +0000997LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
Chris Lattner25963c62010-01-09 22:27:07 +0000998 LLVMBool SignExtend) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000999 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001000}
1001
Chris Lattner4329e072010-11-23 02:47:22 +00001002LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
1003 unsigned NumWords,
1004 const uint64_t Words[]) {
1005 IntegerType *Ty = unwrap<IntegerType>(IntTy);
1006 return wrap(ConstantInt::get(Ty->getContext(),
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00001007 APInt(Ty->getBitWidth(),
1008 makeArrayRef(Words, NumWords))));
Chris Lattner4329e072010-11-23 02:47:22 +00001009}
1010
Erick Tryzelaardd991352009-08-16 23:36:46 +00001011LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
1012 uint8_t Radix) {
1013 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1014 Radix));
1015}
1016
1017LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
1018 unsigned SLen, uint8_t Radix) {
1019 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1020 Radix));
Gordon Henriksen931e1212008-02-02 01:07:50 +00001021}
1022
Gordon Henriksen1046c732007-10-06 15:11:06 +00001023LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
Erick Tryzelaardd991352009-08-16 23:36:46 +00001024 return wrap(ConstantFP::get(unwrap(RealTy), N));
Gordon Henriksen931e1212008-02-02 01:07:50 +00001025}
1026
1027LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
Erick Tryzelaardd991352009-08-16 23:36:46 +00001028 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1029}
1030
1031LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
1032 unsigned SLen) {
1033 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001034}
1035
Chris Lattner40cf28d2009-10-12 04:01:02 +00001036unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1037 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1038}
1039
1040long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1041 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1042}
1043
Peter Zotov1d98e6d2014-10-28 19:46:44 +00001044double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1045 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1046 Type *Ty = cFP->getType();
1047
1048 if (Ty->isFloatTy()) {
1049 *LosesInfo = false;
1050 return cFP->getValueAPF().convertToFloat();
1051 }
1052
1053 if (Ty->isDoubleTy()) {
1054 *LosesInfo = false;
1055 return cFP->getValueAPF().convertToDouble();
1056 }
1057
1058 bool APFLosesInfo;
1059 APFloat APF = cFP->getValueAPF();
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001060 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
Peter Zotov1d98e6d2014-10-28 19:46:44 +00001061 *LosesInfo = APFLosesInfo;
1062 return APF.convertToDouble();
1063}
1064
Gordon Henriksen76a03742007-09-18 03:18:57 +00001065/*--.. Operations on composite constants ...................................--*/
1066
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001067LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
Chris Lattner25963c62010-01-09 22:27:07 +00001068 unsigned Length,
1069 LLVMBool DontNullTerminate) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001070 /* Inverted the sense of AddNull because ', 0)' is a
1071 better mnemonic for null termination than ', 1)'. */
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001072 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1073 DontNullTerminate == 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001074}
Amaury Sechet006ce632016-03-13 00:54:40 +00001075
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001076LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
Chris Lattner25963c62010-01-09 22:27:07 +00001077 LLVMBool DontNullTerminate) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001078 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1079 DontNullTerminate);
1080}
Peter Zotovf9aa8822014-08-03 23:54:16 +00001081
Amaury Sechet006ce632016-03-13 00:54:40 +00001082LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1083 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
Peter Zotovf9aa8822014-08-03 23:54:16 +00001084}
1085
Amaury Sechet006ce632016-03-13 00:54:40 +00001086LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1087 return unwrap<ConstantDataSequential>(C)->isString();
Peter Zotovf9aa8822014-08-03 23:54:16 +00001088}
1089
Amaury Sechet7c2883c2016-04-03 21:06:04 +00001090const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1091 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1092 *Length = Str.size();
1093 return Str.data();
Peter Zotovf9aa8822014-08-03 23:54:16 +00001094}
1095
Gordon Henriksen1046c732007-10-06 15:11:06 +00001096LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1097 LLVMValueRef *ConstantVals, unsigned Length) {
Jay Foad83be3612011-06-22 09:24:39 +00001098 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1099 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001100}
Peter Zotovf9aa8822014-08-03 23:54:16 +00001101
Amaury Sechetc78768f2016-03-13 00:40:12 +00001102LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1103 LLVMValueRef *ConstantVals,
1104 unsigned Count, LLVMBool Packed) {
1105 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1106 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1107 Packed != 0));
1108}
1109
Gordon Henriksen1046c732007-10-06 15:11:06 +00001110LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
Chris Lattner25963c62010-01-09 22:27:07 +00001111 LLVMBool Packed) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001112 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1113 Packed);
Gordon Henriksen76a03742007-09-18 03:18:57 +00001114}
Rafael Espindola784ad242011-07-14 19:09:08 +00001115
1116LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1117 LLVMValueRef *ConstantVals,
1118 unsigned Count) {
1119 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
Chris Lattner229907c2011-07-18 04:54:35 +00001120 StructType *Ty = cast<StructType>(unwrap(StructTy));
Rafael Espindola784ad242011-07-14 19:09:08 +00001121
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001122 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
Rafael Espindola784ad242011-07-14 19:09:08 +00001123}
1124
Gordon Henriksen1046c732007-10-06 15:11:06 +00001125LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001126 return wrap(ConstantVector::get(makeArrayRef(
Chris Lattner69229312011-02-15 00:14:00 +00001127 unwrap<Constant>(ScalarConstantVals, Size), Size)));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001128}
Torok Edwin05dc9d62011-10-06 12:39:34 +00001129
1130/*-- Opcode mapping */
1131
1132static LLVMOpcode map_to_llvmopcode(int opcode)
1133{
1134 switch (opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00001135 default: llvm_unreachable("Unhandled Opcode.");
Torok Edwin05dc9d62011-10-06 12:39:34 +00001136#define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
Chandler Carruth9fb823b2013-01-02 11:36:10 +00001137#include "llvm/IR/Instruction.def"
Torok Edwin05dc9d62011-10-06 12:39:34 +00001138#undef HANDLE_INST
Torok Edwin05dc9d62011-10-06 12:39:34 +00001139 }
1140}
1141
1142static int map_from_llvmopcode(LLVMOpcode code)
1143{
1144 switch (code) {
1145#define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
Chandler Carruth9fb823b2013-01-02 11:36:10 +00001146#include "llvm/IR/Instruction.def"
Torok Edwin05dc9d62011-10-06 12:39:34 +00001147#undef HANDLE_INST
Torok Edwin05dc9d62011-10-06 12:39:34 +00001148 }
David Blaikie486df732012-01-16 23:24:27 +00001149 llvm_unreachable("Unhandled Opcode.");
Torok Edwin05dc9d62011-10-06 12:39:34 +00001150}
1151
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001152/*--.. Constant expressions ................................................--*/
1153
Chris Lattner40cf28d2009-10-12 04:01:02 +00001154LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00001155 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
Chris Lattner40cf28d2009-10-12 04:01:02 +00001156}
1157
Duncan Sandsd334aca2009-05-21 15:52:21 +00001158LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001159 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
Duncan Sandsd334aca2009-05-21 15:52:21 +00001160}
1161
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001162LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001163 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001164}
1165
1166LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001167 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001168}
1169
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001170LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001171 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001172}
1173
1174LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001175 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001176}
1177
1178
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001179LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001180 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001181}
1182
1183LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001184 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001185}
1186
1187LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001188 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001189 unwrap<Constant>(RHSConstant)));
1190}
1191
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001192LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1193 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001194 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001195 unwrap<Constant>(RHSConstant)));
1196}
1197
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001198LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1199 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001200 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001201 unwrap<Constant>(RHSConstant)));
1202}
1203
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001204LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001205 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001206 unwrap<Constant>(RHSConstant)));
1207}
1208
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001209LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001210 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001211 unwrap<Constant>(RHSConstant)));
1212}
1213
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001214LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1215 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001216 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001217 unwrap<Constant>(RHSConstant)));
1218}
1219
1220LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1221 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001222 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001223 unwrap<Constant>(RHSConstant)));
1224}
1225
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001226LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1227 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1228 unwrap<Constant>(RHSConstant)));
1229}
1230
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001231LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001232 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001233 unwrap<Constant>(RHSConstant)));
1234}
1235
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001236LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1237 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001238 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001239 unwrap<Constant>(RHSConstant)));
1240}
1241
1242LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1243 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001244 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001245 unwrap<Constant>(RHSConstant)));
1246}
1247
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001248LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001249 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001250 unwrap<Constant>(RHSConstant)));
1251}
1252
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001253LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001254 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001255 unwrap<Constant>(RHSConstant)));
1256}
1257
Manuel Jacob49fafb12016-10-04 23:32:42 +00001258LLVMValueRef LLVMConstExactUDiv(LLVMValueRef LHSConstant,
1259 LLVMValueRef RHSConstant) {
1260 return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1261 unwrap<Constant>(RHSConstant)));
1262}
1263
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001264LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001265 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001266 unwrap<Constant>(RHSConstant)));
1267}
1268
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001269LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
1270 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001271 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001272 unwrap<Constant>(RHSConstant)));
1273}
1274
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001275LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001276 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001277 unwrap<Constant>(RHSConstant)));
1278}
1279
1280LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001281 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001282 unwrap<Constant>(RHSConstant)));
1283}
1284
1285LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001286 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001287 unwrap<Constant>(RHSConstant)));
1288}
1289
1290LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001291 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001292 unwrap<Constant>(RHSConstant)));
1293}
1294
1295LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001296 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001297 unwrap<Constant>(RHSConstant)));
1298}
1299
1300LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001301 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001302 unwrap<Constant>(RHSConstant)));
1303}
1304
1305LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001306 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001307 unwrap<Constant>(RHSConstant)));
1308}
1309
1310LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1311 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Owen Anderson487375e2009-07-29 18:55:55 +00001312 return wrap(ConstantExpr::getICmp(Predicate,
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001313 unwrap<Constant>(LHSConstant),
1314 unwrap<Constant>(RHSConstant)));
1315}
1316
1317LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1318 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Owen Anderson487375e2009-07-29 18:55:55 +00001319 return wrap(ConstantExpr::getFCmp(Predicate,
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001320 unwrap<Constant>(LHSConstant),
1321 unwrap<Constant>(RHSConstant)));
1322}
1323
1324LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001325 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1326 unwrap<Constant>(RHSConstant)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001327}
1328
1329LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001330 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001331 unwrap<Constant>(RHSConstant)));
1332}
1333
1334LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001335 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001336 unwrap<Constant>(RHSConstant)));
1337}
1338
1339LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1340 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
Jay Foaded8db7d2011-07-21 14:31:17 +00001341 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1342 NumIndices);
David Blaikie4a2e73b2015-04-02 18:55:32 +00001343 return wrap(ConstantExpr::getGetElementPtr(
1344 nullptr, unwrap<Constant>(ConstantVal), IdxList));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001345}
1346
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001347LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1348 LLVMValueRef *ConstantIndices,
1349 unsigned NumIndices) {
1350 Constant* Val = unwrap<Constant>(ConstantVal);
Jay Foaded8db7d2011-07-21 14:31:17 +00001351 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1352 NumIndices);
David Blaikie4a2e73b2015-04-02 18:55:32 +00001353 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList));
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001354}
1355
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001356LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001357 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001358 unwrap(ToType)));
1359}
1360
1361LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001362 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001363 unwrap(ToType)));
1364}
1365
1366LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001367 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001368 unwrap(ToType)));
1369}
1370
1371LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001372 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001373 unwrap(ToType)));
1374}
1375
1376LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001377 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001378 unwrap(ToType)));
1379}
1380
1381LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001382 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001383 unwrap(ToType)));
1384}
1385
1386LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Owen Anderson487375e2009-07-29 18:55:55 +00001387 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001388 unwrap(ToType)));
1389}
1390
1391LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Owen Anderson487375e2009-07-29 18:55:55 +00001392 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001393 unwrap(ToType)));
1394}
1395
1396LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001397 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001398 unwrap(ToType)));
1399}
1400
1401LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001402 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001403 unwrap(ToType)));
1404}
1405
1406LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001407 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001408 unwrap(ToType)));
1409}
1410
1411LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001412 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001413 unwrap(ToType)));
1414}
1415
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001416LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1417 LLVMTypeRef ToType) {
1418 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1419 unwrap(ToType)));
1420}
1421
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001422LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1423 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001424 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001425 unwrap(ToType)));
1426}
1427
1428LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1429 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001430 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001431 unwrap(ToType)));
1432}
1433
1434LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1435 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001436 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001437 unwrap(ToType)));
1438}
1439
1440LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1441 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001442 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001443 unwrap(ToType)));
1444}
1445
1446LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
Chris Lattner25963c62010-01-09 22:27:07 +00001447 LLVMBool isSigned) {
Chris Lattner69229312011-02-15 00:14:00 +00001448 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1449 unwrap(ToType), isSigned));
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001450}
1451
1452LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001453 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001454 unwrap(ToType)));
1455}
1456
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001457LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1458 LLVMValueRef ConstantIfTrue,
1459 LLVMValueRef ConstantIfFalse) {
Chris Lattner69229312011-02-15 00:14:00 +00001460 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001461 unwrap<Constant>(ConstantIfTrue),
1462 unwrap<Constant>(ConstantIfFalse)));
1463}
1464
1465LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1466 LLVMValueRef IndexConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001467 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001468 unwrap<Constant>(IndexConstant)));
1469}
1470
1471LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1472 LLVMValueRef ElementValueConstant,
1473 LLVMValueRef IndexConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001474 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001475 unwrap<Constant>(ElementValueConstant),
1476 unwrap<Constant>(IndexConstant)));
1477}
1478
1479LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1480 LLVMValueRef VectorBConstant,
1481 LLVMValueRef MaskConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001482 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001483 unwrap<Constant>(VectorBConstant),
1484 unwrap<Constant>(MaskConstant)));
1485}
1486
Dan Gohmand5104a52008-11-03 22:55:43 +00001487LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1488 unsigned NumIdx) {
Chris Lattner69229312011-02-15 00:14:00 +00001489 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001490 makeArrayRef(IdxList, NumIdx)));
Dan Gohmand5104a52008-11-03 22:55:43 +00001491}
1492
1493LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1494 LLVMValueRef ElementValueConstant,
1495 unsigned *IdxList, unsigned NumIdx) {
Chris Lattner69229312011-02-15 00:14:00 +00001496 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
Dan Gohmand5104a52008-11-03 22:55:43 +00001497 unwrap<Constant>(ElementValueConstant),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001498 makeArrayRef(IdxList, NumIdx)));
Dan Gohmand5104a52008-11-03 22:55:43 +00001499}
1500
Chris Lattner25963c62010-01-09 22:27:07 +00001501LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1502 const char *Constraints,
1503 LLVMBool HasSideEffects,
Benjamin Kramer8dd0edc2012-09-10 11:52:00 +00001504 LLVMBool IsAlignStack) {
Chris Lattner25963c62010-01-09 22:27:07 +00001505 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
Benjamin Kramer8dd0edc2012-09-10 11:52:00 +00001506 Constraints, HasSideEffects, IsAlignStack));
Chris Lattner3d1f5522008-12-17 21:39:50 +00001507}
1508
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00001509LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1510 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1511}
1512
Gordon Henriksen76a03742007-09-18 03:18:57 +00001513/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1514
Gordon Henriksen265f7802008-03-19 01:11:35 +00001515LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1516 return wrap(unwrap<GlobalValue>(Global)->getParent());
1517}
1518
Chris Lattner25963c62010-01-09 22:27:07 +00001519LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001520 return unwrap<GlobalValue>(Global)->isDeclaration();
1521}
1522
1523LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001524 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001525 case GlobalValue::ExternalLinkage:
1526 return LLVMExternalLinkage;
1527 case GlobalValue::AvailableExternallyLinkage:
1528 return LLVMAvailableExternallyLinkage;
1529 case GlobalValue::LinkOnceAnyLinkage:
1530 return LLVMLinkOnceAnyLinkage;
1531 case GlobalValue::LinkOnceODRLinkage:
1532 return LLVMLinkOnceODRLinkage;
1533 case GlobalValue::WeakAnyLinkage:
1534 return LLVMWeakAnyLinkage;
1535 case GlobalValue::WeakODRLinkage:
1536 return LLVMWeakODRLinkage;
1537 case GlobalValue::AppendingLinkage:
1538 return LLVMAppendingLinkage;
1539 case GlobalValue::InternalLinkage:
1540 return LLVMInternalLinkage;
1541 case GlobalValue::PrivateLinkage:
1542 return LLVMPrivateLinkage;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001543 case GlobalValue::ExternalWeakLinkage:
1544 return LLVMExternalWeakLinkage;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001545 case GlobalValue::CommonLinkage:
1546 return LLVMCommonLinkage;
1547 }
1548
David Blaikiea5708dc2012-01-17 07:00:13 +00001549 llvm_unreachable("Invalid GlobalValue linkage!");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001550}
1551
1552void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001553 GlobalValue *GV = unwrap<GlobalValue>(Global);
1554
1555 switch (Linkage) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001556 case LLVMExternalLinkage:
1557 GV->setLinkage(GlobalValue::ExternalLinkage);
1558 break;
1559 case LLVMAvailableExternallyLinkage:
1560 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1561 break;
1562 case LLVMLinkOnceAnyLinkage:
1563 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1564 break;
1565 case LLVMLinkOnceODRLinkage:
1566 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1567 break;
Bill Wendling34bc34e2012-08-17 18:33:14 +00001568 case LLVMLinkOnceODRAutoHideLinkage:
Rafael Espindola716e7402013-11-01 17:09:14 +00001569 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1570 "longer supported.");
Bill Wendling34bc34e2012-08-17 18:33:14 +00001571 break;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001572 case LLVMWeakAnyLinkage:
1573 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1574 break;
1575 case LLVMWeakODRLinkage:
1576 GV->setLinkage(GlobalValue::WeakODRLinkage);
1577 break;
1578 case LLVMAppendingLinkage:
1579 GV->setLinkage(GlobalValue::AppendingLinkage);
1580 break;
1581 case LLVMInternalLinkage:
1582 GV->setLinkage(GlobalValue::InternalLinkage);
1583 break;
1584 case LLVMPrivateLinkage:
1585 GV->setLinkage(GlobalValue::PrivateLinkage);
1586 break;
1587 case LLVMLinkerPrivateLinkage:
Rafael Espindola2fb5bc32014-03-13 23:18:37 +00001588 GV->setLinkage(GlobalValue::PrivateLinkage);
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001589 break;
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001590 case LLVMLinkerPrivateWeakLinkage:
Rafael Espindola2fb5bc32014-03-13 23:18:37 +00001591 GV->setLinkage(GlobalValue::PrivateLinkage);
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001592 break;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001593 case LLVMDLLImportLinkage:
Nico Rieck7157bb72014-01-14 15:22:47 +00001594 DEBUG(errs()
1595 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001596 break;
1597 case LLVMDLLExportLinkage:
Nico Rieck7157bb72014-01-14 15:22:47 +00001598 DEBUG(errs()
1599 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001600 break;
1601 case LLVMExternalWeakLinkage:
1602 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1603 break;
1604 case LLVMGhostLinkage:
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001605 DEBUG(errs()
1606 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001607 break;
1608 case LLVMCommonLinkage:
1609 GV->setLinkage(GlobalValue::CommonLinkage);
1610 break;
1611 }
Gordon Henriksen76a03742007-09-18 03:18:57 +00001612}
1613
1614const char *LLVMGetSection(LLVMValueRef Global) {
Rafael Espindola83658d62016-05-11 18:21:59 +00001615 // Using .data() is safe because of how GlobalObject::setSection is
1616 // implemented.
1617 return unwrap<GlobalValue>(Global)->getSection().data();
Gordon Henriksen76a03742007-09-18 03:18:57 +00001618}
1619
1620void LLVMSetSection(LLVMValueRef Global, const char *Section) {
Rafael Espindola99e05cf2014-05-13 18:45:48 +00001621 unwrap<GlobalObject>(Global)->setSection(Section);
Gordon Henriksen76a03742007-09-18 03:18:57 +00001622}
1623
1624LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1625 return static_cast<LLVMVisibility>(
1626 unwrap<GlobalValue>(Global)->getVisibility());
1627}
1628
1629void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1630 unwrap<GlobalValue>(Global)
1631 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1632}
1633
Reid Kleckner2fae26f2014-03-05 02:34:23 +00001634LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1635 return static_cast<LLVMDLLStorageClass>(
1636 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1637}
1638
1639void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1640 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1641 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1642}
1643
Robert Widmann4bb481b2018-03-14 06:45:51 +00001644LLVMUnnamedAddr LLVMGetUnnamedAddress(LLVMValueRef Global) {
1645 switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
1646 case GlobalVariable::UnnamedAddr::None:
1647 return LLVMNoUnnamedAddr;
1648 case GlobalVariable::UnnamedAddr::Local:
1649 return LLVMLocalUnnamedAddr;
1650 case GlobalVariable::UnnamedAddr::Global:
1651 return LLVMGlobalUnnamedAddr;
1652 }
Simon Pilgrimf0ccaae2018-03-14 12:04:51 +00001653 llvm_unreachable("Unknown UnnamedAddr kind!");
Robert Widmann4bb481b2018-03-14 06:45:51 +00001654}
1655
1656void LLVMSetUnnamedAddress(LLVMValueRef Global, LLVMUnnamedAddr UnnamedAddr) {
1657 GlobalValue *GV = unwrap<GlobalValue>(Global);
1658
1659 switch (UnnamedAddr) {
1660 case LLVMNoUnnamedAddr:
1661 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);
1662 case LLVMLocalUnnamedAddr:
1663 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);
1664 case LLVMGlobalUnnamedAddr:
1665 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);
1666 }
1667}
1668
Tim Northoverad96d012014-03-10 19:24:35 +00001669LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001670 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
Tim Northoverad96d012014-03-10 19:24:35 +00001671}
1672
1673void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001674 unwrap<GlobalValue>(Global)->setUnnamedAddr(
1675 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1676 : GlobalValue::UnnamedAddr::None);
Tim Northoverad96d012014-03-10 19:24:35 +00001677}
1678
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001679/*--.. Operations on global variables, load and store instructions .........--*/
1680
1681unsigned LLVMGetAlignment(LLVMValueRef V) {
1682 Value *P = unwrap<Value>(V);
1683 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1684 return GV->getAlignment();
Peter Zotov9f584e62014-03-05 05:05:34 +00001685 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1686 return AI->getAlignment();
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001687 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1688 return LI->getAlignment();
1689 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1690 return SI->getAlignment();
1691
Peter Zotov9f584e62014-03-05 05:05:34 +00001692 llvm_unreachable(
1693 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001694}
1695
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001696void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1697 Value *P = unwrap<Value>(V);
Rafael Espindola99e05cf2014-05-13 18:45:48 +00001698 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001699 GV->setAlignment(Bytes);
Peter Zotov9f584e62014-03-05 05:05:34 +00001700 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1701 AI->setAlignment(Bytes);
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001702 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1703 LI->setAlignment(Bytes);
1704 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1705 SI->setAlignment(Bytes);
Anders Waldenborga36a7822013-10-29 09:37:28 +00001706 else
Peter Zotov9f584e62014-03-05 05:05:34 +00001707 llvm_unreachable(
1708 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001709}
1710
1711/*--.. Operations on global variables ......................................--*/
1712
1713LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
Owen Andersonb17f3292009-07-08 19:03:57 +00001714 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
Craig Topperc6207612014-04-09 06:08:46 +00001715 GlobalValue::ExternalLinkage, nullptr, Name));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001716}
1717
Erick Tryzelaar06894b32010-02-28 09:46:13 +00001718LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1719 const char *Name,
1720 unsigned AddressSpace) {
1721 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
Craig Topperc6207612014-04-09 06:08:46 +00001722 GlobalValue::ExternalLinkage, nullptr, Name,
1723 nullptr, GlobalVariable::NotThreadLocal,
1724 AddressSpace));
Erick Tryzelaar06894b32010-02-28 09:46:13 +00001725}
1726
Gordon Henriksen783f7bb2007-10-08 03:45:09 +00001727LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1728 return wrap(unwrap(M)->getNamedGlobal(Name));
1729}
1730
Gordon Henriksen054817c2008-03-19 03:47:18 +00001731LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1732 Module *Mod = unwrap(M);
1733 Module::global_iterator I = Mod->global_begin();
1734 if (I == Mod->global_end())
Craig Topperc6207612014-04-09 06:08:46 +00001735 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001736 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001737}
1738
1739LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1740 Module *Mod = unwrap(M);
1741 Module::global_iterator I = Mod->global_end();
1742 if (I == Mod->global_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001743 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001744 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001745}
1746
1747LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1748 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001749 Module::global_iterator I(GV);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001750 if (++I == GV->getParent()->global_end())
Craig Topperc6207612014-04-09 06:08:46 +00001751 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001752 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001753}
1754
1755LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1756 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001757 Module::global_iterator I(GV);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001758 if (I == GV->getParent()->global_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001759 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001760 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001761}
1762
Gordon Henriksen76a03742007-09-18 03:18:57 +00001763void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1764 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1765}
1766
Gordon Henriksen76a03742007-09-18 03:18:57 +00001767LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
Chris Lattner40cf28d2009-10-12 04:01:02 +00001768 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1769 if ( !GV->hasInitializer() )
Craig Topperc6207612014-04-09 06:08:46 +00001770 return nullptr;
Chris Lattner40cf28d2009-10-12 04:01:02 +00001771 return wrap(GV->getInitializer());
Gordon Henriksen76a03742007-09-18 03:18:57 +00001772}
1773
1774void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1775 unwrap<GlobalVariable>(GlobalVar)
1776 ->setInitializer(unwrap<Constant>(ConstantVal));
1777}
1778
Chris Lattner25963c62010-01-09 22:27:07 +00001779LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001780 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1781}
1782
Chris Lattner25963c62010-01-09 22:27:07 +00001783void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001784 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1785}
1786
Chris Lattner25963c62010-01-09 22:27:07 +00001787LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
Gordon Henriksen751ebf72007-10-07 17:31:42 +00001788 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1789}
1790
Chris Lattner25963c62010-01-09 22:27:07 +00001791void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
Gordon Henriksen751ebf72007-10-07 17:31:42 +00001792 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1793}
1794
Hans Wennborg5ff71202013-04-16 08:58:59 +00001795LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1796 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1797 case GlobalVariable::NotThreadLocal:
1798 return LLVMNotThreadLocal;
1799 case GlobalVariable::GeneralDynamicTLSModel:
1800 return LLVMGeneralDynamicTLSModel;
1801 case GlobalVariable::LocalDynamicTLSModel:
1802 return LLVMLocalDynamicTLSModel;
1803 case GlobalVariable::InitialExecTLSModel:
1804 return LLVMInitialExecTLSModel;
1805 case GlobalVariable::LocalExecTLSModel:
1806 return LLVMLocalExecTLSModel;
1807 }
1808
1809 llvm_unreachable("Invalid GlobalVariable thread local mode");
1810}
1811
1812void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1813 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1814
1815 switch (Mode) {
1816 case LLVMNotThreadLocal:
1817 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1818 break;
1819 case LLVMGeneralDynamicTLSModel:
1820 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1821 break;
1822 case LLVMLocalDynamicTLSModel:
1823 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1824 break;
1825 case LLVMInitialExecTLSModel:
1826 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1827 break;
1828 case LLVMLocalExecTLSModel:
1829 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1830 break;
1831 }
1832}
1833
1834LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1835 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1836}
1837
1838void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1839 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1840}
1841
Chris Lattner3d1f5522008-12-17 21:39:50 +00001842/*--.. Operations on aliases ......................................--*/
1843
1844LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1845 const char *Name) {
Rafael Espindola4fe00942014-05-16 13:34:04 +00001846 auto *PTy = cast<PointerType>(unwrap(Ty));
David Blaikie16a2f3e2015-09-14 18:01:59 +00001847 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1848 GlobalValue::ExternalLinkage, Name,
Rafael Espindola993502e2015-02-23 21:51:06 +00001849 unwrap<Constant>(Aliasee), unwrap(M)));
Chris Lattner3d1f5522008-12-17 21:39:50 +00001850}
1851
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001852/*--.. Operations on functions .............................................--*/
1853
1854LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1855 LLVMTypeRef FunctionTy) {
Gabor Greife9ecc682008-04-06 20:25:17 +00001856 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1857 GlobalValue::ExternalLinkage, Name, unwrap(M)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001858}
1859
Gordon Henriksen783f7bb2007-10-08 03:45:09 +00001860LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1861 return wrap(unwrap(M)->getFunction(Name));
1862}
1863
Gordon Henriksen054817c2008-03-19 03:47:18 +00001864LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1865 Module *Mod = unwrap(M);
1866 Module::iterator I = Mod->begin();
1867 if (I == Mod->end())
Craig Topperc6207612014-04-09 06:08:46 +00001868 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001869 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001870}
1871
1872LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1873 Module *Mod = unwrap(M);
1874 Module::iterator I = Mod->end();
1875 if (I == Mod->begin())
Craig Topperc6207612014-04-09 06:08:46 +00001876 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001877 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001878}
1879
1880LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1881 Function *Func = unwrap<Function>(Fn);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001882 Module::iterator I(Func);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001883 if (++I == Func->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00001884 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001885 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001886}
1887
1888LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1889 Function *Func = unwrap<Function>(Fn);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001890 Module::iterator I(Func);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001891 if (I == Func->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00001892 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001893 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001894}
1895
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001896void LLVMDeleteFunction(LLVMValueRef Fn) {
1897 unwrap<Function>(Fn)->eraseFromParent();
1898}
1899
Amaury Sechete39e8532016-02-18 20:38:32 +00001900LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
1901 return unwrap<Function>(Fn)->hasPersonalityFn();
1902}
1903
Andrew Wilkins3bdfc1c2015-07-14 01:23:06 +00001904LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
1905 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
1906}
1907
1908void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
1909 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
1910}
1911
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001912unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1913 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1914 return F->getIntrinsicID();
1915 return 0;
1916}
1917
1918unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1919 return unwrap<Function>(Fn)->getCallingConv();
1920}
1921
1922void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
Sandeep Patel68c5f472009-09-02 08:44:58 +00001923 return unwrap<Function>(Fn)->setCallingConv(
1924 static_cast<CallingConv::ID>(CC));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001925}
1926
Gordon Henriksend930f912008-08-17 18:44:35 +00001927const char *LLVMGetGC(LLVMValueRef Fn) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00001928 Function *F = unwrap<Function>(Fn);
Mehdi Amini599ebf22016-01-08 02:28:20 +00001929 return F->hasGC()? F->getGC().c_str() : nullptr;
Gordon Henriksen71183b62007-12-10 03:18:06 +00001930}
1931
Gordon Henriksend930f912008-08-17 18:44:35 +00001932void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00001933 Function *F = unwrap<Function>(Fn);
Gordon Henriksend930f912008-08-17 18:44:35 +00001934 if (GC)
1935 F->setGC(GC);
Gordon Henriksen71183b62007-12-10 03:18:06 +00001936 else
Gordon Henriksend930f912008-08-17 18:44:35 +00001937 F->clearGC();
Gordon Henriksen71183b62007-12-10 03:18:06 +00001938}
1939
Amaury Sechet5db224e2016-06-12 06:17:24 +00001940void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1941 LLVMAttributeRef A) {
1942 unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
1943}
1944
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001945unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001946 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
1947 return AS.getNumAttributes();
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001948}
1949
1950void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1951 LLVMAttributeRef *Attrs) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001952 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
1953 for (auto A : AS)
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001954 *Attrs++ = wrap(A);
1955}
1956
Amaury Sechet5db224e2016-06-12 06:17:24 +00001957LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
1958 LLVMAttributeIndex Idx,
1959 unsigned KindID) {
1960 return wrap(unwrap<Function>(F)->getAttribute(Idx,
1961 (Attribute::AttrKind)KindID));
1962}
1963
Amaury Sechet6100adf2016-06-15 17:50:39 +00001964LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
1965 LLVMAttributeIndex Idx,
1966 const char *K, unsigned KLen) {
1967 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
1968}
1969
Amaury Sechet5db224e2016-06-12 06:17:24 +00001970void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1971 unsigned KindID) {
1972 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
1973}
1974
Amaury Sechet6100adf2016-06-15 17:50:39 +00001975void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1976 const char *K, unsigned KLen) {
1977 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
1978}
1979
Tom Stellarde8f35e12013-04-16 23:12:43 +00001980void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1981 const char *V) {
1982 Function *Func = unwrap<Function>(Fn);
Reid Kleckner9d16fa02017-04-19 17:28:52 +00001983 Attribute Attr = Attribute::get(Func->getContext(), A, V);
1984 Func->addAttribute(AttributeList::FunctionIndex, Attr);
Tom Stellarde8f35e12013-04-16 23:12:43 +00001985}
1986
Gordon Henriksen265f7802008-03-19 01:11:35 +00001987/*--.. Operations on parameters ............................................--*/
1988
1989unsigned LLVMCountParams(LLVMValueRef FnRef) {
1990 // This function is strictly redundant to
1991 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
Dan Gohmanc7076912008-06-21 22:06:54 +00001992 return unwrap<Function>(FnRef)->arg_size();
Gordon Henriksen265f7802008-03-19 01:11:35 +00001993}
1994
1995void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1996 Function *Fn = unwrap<Function>(FnRef);
1997 for (Function::arg_iterator I = Fn->arg_begin(),
1998 E = Fn->arg_end(); I != E; I++)
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001999 *ParamRefs++ = wrap(&*I);
Gordon Henriksen265f7802008-03-19 01:11:35 +00002000}
2001
2002LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
Reid Kleckner56d028d2017-03-17 17:16:39 +00002003 Function *Fn = unwrap<Function>(FnRef);
2004 return wrap(&Fn->arg_begin()[index]);
Gordon Henriksen265f7802008-03-19 01:11:35 +00002005}
2006
2007LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
2008 return wrap(unwrap<Argument>(V)->getParent());
2009}
2010
Gordon Henriksen07a45f42008-03-23 22:21:29 +00002011LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
2012 Function *Func = unwrap<Function>(Fn);
2013 Function::arg_iterator I = Func->arg_begin();
2014 if (I == Func->arg_end())
Craig Topperc6207612014-04-09 06:08:46 +00002015 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002016 return wrap(&*I);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00002017}
2018
2019LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
2020 Function *Func = unwrap<Function>(Fn);
2021 Function::arg_iterator I = Func->arg_end();
2022 if (I == Func->arg_begin())
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 Henriksen07a45f42008-03-23 22:21:29 +00002025}
2026
2027LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
2028 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner56d028d2017-03-17 17:16:39 +00002029 Function *Fn = A->getParent();
2030 if (A->getArgNo() + 1 >= Fn->arg_size())
Craig Topperc6207612014-04-09 06:08:46 +00002031 return nullptr;
Reid Kleckner56d028d2017-03-17 17:16:39 +00002032 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00002033}
2034
2035LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
2036 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner56d028d2017-03-17 17:16:39 +00002037 if (A->getArgNo() == 0)
Craig Topperc6207612014-04-09 06:08:46 +00002038 return nullptr;
Reid Kleckner56d028d2017-03-17 17:16:39 +00002039 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00002040}
2041
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002042void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
Bill Wendling49bc76c2013-01-23 06:14:59 +00002043 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner9d16fa02017-04-19 17:28:52 +00002044 A->addAttr(Attribute::getWithAlignment(A->getContext(), align));
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002045}
2046
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002047/*--.. Operations on basic blocks ..........................................--*/
2048
Gordon Henriksen265f7802008-03-19 01:11:35 +00002049LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
2050 return wrap(static_cast<Value*>(unwrap(BB)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002051}
2052
Chris Lattner25963c62010-01-09 22:27:07 +00002053LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002054 return isa<BasicBlock>(unwrap(Val));
2055}
2056
2057LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
2058 return wrap(unwrap<BasicBlock>(Val));
2059}
2060
Amaury Secheta82042e2016-02-09 22:36:41 +00002061const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
2062 return unwrap(BB)->getName().data();
2063}
2064
Gordon Henriksen07a45f42008-03-23 22:21:29 +00002065LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
2066 return wrap(unwrap(BB)->getParent());
Gordon Henriksen265f7802008-03-19 01:11:35 +00002067}
2068
Nate Begeman43c322b2011-08-23 20:27:46 +00002069LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
2070 return wrap(unwrap(BB)->getTerminator());
2071}
2072
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002073unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
Dan Gohmanc7076912008-06-21 22:06:54 +00002074 return unwrap<Function>(FnRef)->size();
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002075}
2076
2077void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2078 Function *Fn = unwrap<Function>(FnRef);
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00002079 for (BasicBlock &BB : *Fn)
2080 *BasicBlocksRefs++ = wrap(&BB);
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002081}
2082
2083LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2084 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2085}
2086
Gordon Henriksen054817c2008-03-19 03:47:18 +00002087LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2088 Function *Func = unwrap<Function>(Fn);
2089 Function::iterator I = Func->begin();
2090 if (I == Func->end())
Craig Topperc6207612014-04-09 06:08:46 +00002091 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002092 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002093}
2094
2095LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2096 Function *Func = unwrap<Function>(Fn);
2097 Function::iterator I = Func->end();
2098 if (I == Func->begin())
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
2103LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2104 BasicBlock *Block = unwrap(BB);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002105 Function::iterator I(Block);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002106 if (++I == Block->getParent()->end())
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
2111LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2112 BasicBlock *Block = unwrap(BB);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002113 Function::iterator I(Block);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002114 if (I == Block->getParent()->begin())
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
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002119LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2120 LLVMValueRef FnRef,
2121 const char *Name) {
2122 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002123}
2124
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002125LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2126 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2127}
2128
2129LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2130 LLVMBasicBlockRef BBRef,
2131 const char *Name) {
2132 BasicBlock *BB = unwrap(BBRef);
2133 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2134}
2135
2136LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002137 const char *Name) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002138 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002139}
2140
2141void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2142 unwrap(BBRef)->eraseFromParent();
2143}
2144
Nate Begeman43c322b2011-08-23 20:27:46 +00002145void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2146 unwrap(BBRef)->removeFromParent();
2147}
2148
Duncan Sandsb1d61aa2010-07-19 15:31:07 +00002149void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2150 unwrap(BB)->moveBefore(unwrap(MovePos));
2151}
2152
2153void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2154 unwrap(BB)->moveAfter(unwrap(MovePos));
2155}
2156
Gordon Henriksen265f7802008-03-19 01:11:35 +00002157/*--.. Operations on instructions ..........................................--*/
2158
2159LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2160 return wrap(unwrap<Instruction>(Inst)->getParent());
2161}
2162
Gordon Henriksen054817c2008-03-19 03:47:18 +00002163LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2164 BasicBlock *Block = unwrap(BB);
2165 BasicBlock::iterator I = Block->begin();
2166 if (I == Block->end())
Craig Topperc6207612014-04-09 06:08:46 +00002167 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002168 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002169}
2170
2171LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2172 BasicBlock *Block = unwrap(BB);
2173 BasicBlock::iterator I = Block->end();
2174 if (I == Block->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002175 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002176 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002177}
2178
2179LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2180 Instruction *Instr = unwrap<Instruction>(Inst);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002181 BasicBlock::iterator I(Instr);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002182 if (++I == Instr->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00002183 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002184 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002185}
2186
2187LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2188 Instruction *Instr = unwrap<Instruction>(Inst);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002189 BasicBlock::iterator I(Instr);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002190 if (I == Instr->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002191 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002192 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002193}
2194
Amaury Sechet2f432082016-02-11 21:37:54 +00002195void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2196 unwrap<Instruction>(Inst)->removeFromParent();
2197}
2198
Devang Pateldbebc6f2011-10-03 20:59:18 +00002199void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2200 unwrap<Instruction>(Inst)->eraseFromParent();
2201}
2202
Torok Edwin60c40de2011-10-06 12:13:20 +00002203LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
Torok Edwin2e9affe2011-10-14 20:37:42 +00002204 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2205 return (LLVMIntPredicate)I->getPredicate();
2206 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2207 if (CE->getOpcode() == Instruction::ICmp)
2208 return (LLVMIntPredicate)CE->getPredicate();
2209 return (LLVMIntPredicate)0;
Torok Edwin60c40de2011-10-06 12:13:20 +00002210}
2211
Peter Zotov1d98e6d2014-10-28 19:46:44 +00002212LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2213 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2214 return (LLVMRealPredicate)I->getPredicate();
2215 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2216 if (CE->getOpcode() == Instruction::FCmp)
2217 return (LLVMRealPredicate)CE->getPredicate();
2218 return (LLVMRealPredicate)0;
2219}
2220
Torok Edwinab6158e2011-10-14 20:37:49 +00002221LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2222 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2223 return map_to_llvmopcode(C->getOpcode());
2224 return (LLVMOpcode)0;
2225}
2226
Peter Zotovaff492c2014-10-17 01:02:34 +00002227LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2228 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2229 return wrap(C->clone());
2230 return nullptr;
2231}
2232
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002233unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
Robert Widmann478fce92018-03-30 17:49:53 +00002234 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2235 return FPI->getNumArgOperands();
2236 }
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002237 return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands();
2238}
2239
Robert Widmann478fce92018-03-30 17:49:53 +00002240/*--.. Call and invoke instructions ........................................--*/
2241
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002242unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002243 return CallSite(unwrap<Instruction>(Instr)).getCallingConv();
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002244}
2245
2246void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002247 return CallSite(unwrap<Instruction>(Instr))
2248 .setCallingConv(static_cast<CallingConv::ID>(CC));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002249}
2250
Andrew Trickdc073ad2013-09-18 23:31:10 +00002251void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002252 unsigned align) {
2253 CallSite Call = CallSite(unwrap<Instruction>(Instr));
Reid Kleckner9d16fa02017-04-19 17:28:52 +00002254 Attribute AlignAttr = Attribute::getWithAlignment(Call->getContext(), align);
2255 Call.addAttribute(index, AlignAttr);
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002256}
2257
Amaury Secheta65a2372016-06-15 05:14:29 +00002258void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2259 LLVMAttributeRef A) {
2260 CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A));
2261}
2262
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002263unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2264 LLVMAttributeIndex Idx) {
2265 auto CS = CallSite(unwrap<Instruction>(C));
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002266 auto AS = CS.getAttributes().getAttributes(Idx);
2267 return AS.getNumAttributes();
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002268}
2269
2270void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2271 LLVMAttributeRef *Attrs) {
2272 auto CS = CallSite(unwrap<Instruction>(C));
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002273 auto AS = CS.getAttributes().getAttributes(Idx);
2274 for (auto A : AS)
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002275 *Attrs++ = wrap(A);
2276}
2277
Amaury Secheta65a2372016-06-15 05:14:29 +00002278LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
2279 LLVMAttributeIndex Idx,
2280 unsigned KindID) {
2281 return wrap(CallSite(unwrap<Instruction>(C))
2282 .getAttribute(Idx, (Attribute::AttrKind)KindID));
2283}
2284
Amaury Sechet6100adf2016-06-15 17:50:39 +00002285LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
2286 LLVMAttributeIndex Idx,
2287 const char *K, unsigned KLen) {
2288 return wrap(CallSite(unwrap<Instruction>(C))
2289 .getAttribute(Idx, StringRef(K, KLen)));
2290}
2291
Amaury Secheta65a2372016-06-15 05:14:29 +00002292void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2293 unsigned KindID) {
2294 CallSite(unwrap<Instruction>(C))
2295 .removeAttribute(Idx, (Attribute::AttrKind)KindID);
2296}
2297
Amaury Sechet6100adf2016-06-15 17:50:39 +00002298void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2299 const char *K, unsigned KLen) {
2300 CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen));
2301}
2302
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002303LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2304 return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue());
2305}
2306
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002307/*--.. Operations on call instructions (only) ..............................--*/
2308
Chris Lattner25963c62010-01-09 22:27:07 +00002309LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002310 return unwrap<CallInst>(Call)->isTailCall();
2311}
2312
Chris Lattner25963c62010-01-09 22:27:07 +00002313void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002314 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2315}
2316
Amaury Sechete39e8532016-02-18 20:38:32 +00002317/*--.. Operations on invoke instructions (only) ............................--*/
2318
2319LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2320 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2321}
2322
2323LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
Robert Widmann478fce92018-03-30 17:49:53 +00002324 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2325 return wrap(CRI->getUnwindDest());
2326 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2327 return wrap(CSI->getUnwindDest());
2328 }
Amaury Sechete39e8532016-02-18 20:38:32 +00002329 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2330}
2331
2332void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2333 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2334}
2335
2336void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
Robert Widmann478fce92018-03-30 17:49:53 +00002337 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2338 return CRI->setUnwindDest(unwrap(B));
2339 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2340 return CSI->setUnwindDest(unwrap(B));
2341 }
Amaury Sechete39e8532016-02-18 20:38:32 +00002342 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2343}
2344
Peter Zotov2481c752014-10-28 19:46:56 +00002345/*--.. Operations on terminators ...........................................--*/
2346
2347unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2348 return unwrap<TerminatorInst>(Term)->getNumSuccessors();
2349}
2350
2351LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2352 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
2353}
2354
2355void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2356 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
2357}
2358
2359/*--.. Operations on branch instructions (only) ............................--*/
2360
2361LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2362 return unwrap<BranchInst>(Branch)->isConditional();
2363}
2364
2365LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2366 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2367}
2368
2369void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2370 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2371}
2372
Nate Begeman43c322b2011-08-23 20:27:46 +00002373/*--.. Operations on switch instructions (only) ............................--*/
2374
2375LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2376 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2377}
2378
Amaury Sechet1dcf5772016-02-09 22:50:53 +00002379/*--.. Operations on alloca instructions (only) ............................--*/
2380
2381LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
2382 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2383}
2384
Amaury Sechet053ac452016-02-17 22:51:03 +00002385/*--.. Operations on gep instructions (only) ...............................--*/
2386
2387LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
2388 return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2389}
2390
Amaury Sechet8a367d42016-05-01 02:23:14 +00002391void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
2392 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
Amaury Sechet053ac452016-02-17 22:51:03 +00002393}
2394
Gordon Henriksen44dd8fb2007-10-08 18:14:39 +00002395/*--.. Operations on phi nodes .............................................--*/
2396
2397void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2398 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2399 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2400 for (unsigned I = 0; I != Count; ++I)
2401 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2402}
2403
2404unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2405 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2406}
2407
2408LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2409 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2410}
2411
2412LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2413 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2414}
2415
Amaury Sechetaad93532016-02-10 00:38:50 +00002416/*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2417
2418unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
2419 auto *I = unwrap(Inst);
Amaury Sechet053ac452016-02-17 22:51:03 +00002420 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
2421 return GEP->getNumIndices();
Amaury Sechetaad93532016-02-10 00:38:50 +00002422 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2423 return EV->getNumIndices();
2424 if (auto *IV = dyn_cast<InsertValueInst>(I))
2425 return IV->getNumIndices();
2426 llvm_unreachable(
2427 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2428}
2429
2430const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2431 auto *I = unwrap(Inst);
2432 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2433 return EV->getIndices().data();
2434 if (auto *IV = dyn_cast<InsertValueInst>(I))
2435 return IV->getIndices().data();
2436 llvm_unreachable(
2437 "LLVMGetIndices applies only to extractvalue and insertvalue!");
2438}
2439
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002440
2441/*===-- Instruction builders ----------------------------------------------===*/
2442
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002443LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2444 return wrap(new IRBuilder<>(*unwrap(C)));
2445}
2446
Gordon Henriksena735a9c2008-05-04 12:55:34 +00002447LLVMBuilderRef LLVMCreateBuilder(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002448 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002449}
2450
Gordon Henriksen054817c2008-03-19 03:47:18 +00002451void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2452 LLVMValueRef Instr) {
2453 BasicBlock *BB = unwrap(Block);
Duncan P. N. Exon Smith43724642016-08-11 15:45:04 +00002454 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
2455 unwrap(Builder)->SetInsertPoint(BB, I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002456}
2457
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002458void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2459 Instruction *I = unwrap<Instruction>(Instr);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002460 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002461}
2462
2463void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2464 BasicBlock *BB = unwrap(Block);
2465 unwrap(Builder)->SetInsertPoint(BB);
2466}
2467
Gordon Henriksen265f7802008-03-19 01:11:35 +00002468LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2469 return wrap(unwrap(Builder)->GetInsertBlock());
2470}
2471
Chris Lattner3d1f5522008-12-17 21:39:50 +00002472void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
Chris Lattnere4bcde82010-04-01 06:31:45 +00002473 unwrap(Builder)->ClearInsertionPoint();
Chris Lattner3d1f5522008-12-17 21:39:50 +00002474}
2475
2476void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2477 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2478}
2479
Erick Tryzelaar9813bea2009-08-16 02:20:57 +00002480void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2481 const char *Name) {
2482 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2483}
2484
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002485void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2486 delete unwrap(Builder);
2487}
2488
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002489/*--.. Metadata builders ...................................................--*/
2490
2491void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002492 MDNode *Loc =
2493 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002494 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002495}
2496
2497LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002498 LLVMContext &Context = unwrap(Builder)->getContext();
2499 return wrap(MetadataAsValue::get(
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002500 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002501}
2502
2503void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2504 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2505}
2506
2507
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002508/*--.. Instruction builders ................................................--*/
2509
2510LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2511 return wrap(unwrap(B)->CreateRetVoid());
2512}
2513
2514LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2515 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2516}
2517
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002518LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2519 unsigned N) {
2520 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2521}
2522
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002523LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2524 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2525}
2526
2527LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2528 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2529 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2530}
2531
2532LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2533 LLVMBasicBlockRef Else, unsigned NumCases) {
2534 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2535}
2536
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00002537LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2538 unsigned NumDests) {
2539 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2540}
2541
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002542LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2543 LLVMValueRef *Args, unsigned NumArgs,
2544 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2545 const char *Name) {
2546 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002547 makeArrayRef(unwrap(Args), NumArgs),
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002548 Name));
2549}
2550
Bill Wendlingfae14752011-08-12 20:24:12 +00002551LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
Reid Kleckneref9828f2015-07-16 01:16:39 +00002552 LLVMValueRef PersFn, unsigned NumClauses,
2553 const char *Name) {
2554 // The personality used to live on the landingpad instruction, but now it
2555 // lives on the parent function. For compatibility, take the provided
2556 // personality and put it on the parent function.
2557 if (PersFn)
2558 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
2559 cast<Function>(unwrap(PersFn)));
David Majnemer7fddecc2015-06-17 20:52:32 +00002560 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
Bill Wendlingfae14752011-08-12 20:24:12 +00002561}
2562
Robert Widmann478fce92018-03-30 17:49:53 +00002563LLVMValueRef LLVMBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
2564 LLVMValueRef *Args, unsigned NumArgs,
2565 const char *Name) {
2566 return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
2567 makeArrayRef(unwrap(Args), NumArgs),
2568 Name));
2569}
2570
2571LLVMValueRef LLVMBuildCleanupPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
2572 LLVMValueRef *Args, unsigned NumArgs,
2573 const char *Name) {
2574 if (ParentPad == nullptr) {
2575 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
2576 ParentPad = wrap(Constant::getNullValue(Ty));
2577 }
2578 return wrap(unwrap(B)->CreateCleanupPad(unwrap(ParentPad),
2579 makeArrayRef(unwrap(Args), NumArgs),
2580 Name));
2581}
2582
Bill Wendlingf891bf82011-07-31 06:30:59 +00002583LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2584 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2585}
2586
Robert Widmann478fce92018-03-30 17:49:53 +00002587LLVMValueRef LLVMBuildCatchSwitch(LLVMBuilderRef B, LLVMValueRef ParentPad,
2588 LLVMBasicBlockRef UnwindBB,
2589 unsigned NumHandlers, const char *Name) {
2590 if (ParentPad == nullptr) {
2591 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
2592 ParentPad = wrap(Constant::getNullValue(Ty));
2593 }
2594 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
2595 NumHandlers, Name));
2596}
2597
2598LLVMValueRef LLVMBuildCatchRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
2599 LLVMBasicBlockRef BB) {
2600 return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
2601 unwrap(BB)));
2602}
2603
2604LLVMValueRef LLVMBuildCleanupRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
2605 LLVMBasicBlockRef BB) {
2606 return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
2607 unwrap(BB)));
2608}
2609
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002610LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2611 return wrap(unwrap(B)->CreateUnreachable());
2612}
2613
Gordon Henriksen097102c2008-01-01 05:50:53 +00002614void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2615 LLVMBasicBlockRef Dest) {
2616 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2617}
2618
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00002619void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2620 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2621}
2622
Amaury Sechete39e8532016-02-18 20:38:32 +00002623unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
2624 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
2625}
2626
2627LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
2628 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
2629}
2630
Bill Wendlingfae14752011-08-12 20:24:12 +00002631void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2632 unwrap<LandingPadInst>(LandingPad)->
2633 addClause(cast<Constant>(unwrap(ClauseVal)));
2634}
2635
Amaury Sechete39e8532016-02-18 20:38:32 +00002636LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
2637 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
2638}
2639
Bill Wendlingfae14752011-08-12 20:24:12 +00002640void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2641 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2642}
2643
Robert Widmann478fce92018-03-30 17:49:53 +00002644void LLVMAddHandler(LLVMValueRef CatchSwitch, LLVMBasicBlockRef Dest) {
2645 unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
2646}
2647
2648unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
2649 return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
2650}
2651
2652void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
2653 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
2654 for (CatchSwitchInst::handler_iterator I = CSI->handler_begin(),
2655 E = CSI->handler_end(); I != E; ++I)
2656 *Handlers++ = wrap(*I);
2657}
2658
2659LLVMValueRef LLVMGetParentCatchSwitch(LLVMValueRef CatchPad) {
2660 return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
2661}
2662
2663void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad, LLVMValueRef CatchSwitch) {
2664 unwrap<CatchPadInst>(CatchPad)
2665 ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
2666}
2667
2668/*--.. Funclets ...........................................................--*/
2669
2670LLVMValueRef LLVMGetArgOperand(LLVMValueRef Funclet, unsigned i) {
2671 return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
2672}
2673
2674void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
2675 unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
2676}
2677
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002678/*--.. Arithmetic ..........................................................--*/
2679
2680LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2681 const char *Name) {
2682 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2683}
2684
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002685LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2686 const char *Name) {
2687 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2688}
2689
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002690LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2691 const char *Name) {
2692 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2693}
2694
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002695LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2696 const char *Name) {
2697 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2698}
2699
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002700LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2701 const char *Name) {
2702 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2703}
2704
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002705LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2706 const char *Name) {
2707 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2708}
2709
2710LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2711 const char *Name) {
2712 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2713}
2714
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002715LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2716 const char *Name) {
2717 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2718}
2719
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002720LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2721 const char *Name) {
2722 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2723}
2724
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002725LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2726 const char *Name) {
2727 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2728}
2729
2730LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2731 const char *Name) {
2732 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2733}
2734
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002735LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2736 const char *Name) {
2737 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2738}
2739
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002740LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2741 const char *Name) {
2742 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2743}
2744
Manuel Jacob49fafb12016-10-04 23:32:42 +00002745LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2746 LLVMValueRef RHS, const char *Name) {
2747 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
2748}
2749
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002750LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2751 const char *Name) {
2752 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2753}
2754
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002755LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2756 LLVMValueRef RHS, const char *Name) {
2757 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2758}
2759
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002760LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2761 const char *Name) {
2762 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2763}
2764
2765LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2766 const char *Name) {
2767 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2768}
2769
2770LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2771 const char *Name) {
2772 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2773}
2774
2775LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2776 const char *Name) {
2777 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2778}
2779
2780LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2781 const char *Name) {
2782 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2783}
2784
2785LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2786 const char *Name) {
2787 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2788}
2789
2790LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2791 const char *Name) {
2792 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2793}
2794
2795LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2796 const char *Name) {
2797 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2798}
2799
2800LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2801 const char *Name) {
2802 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2803}
2804
2805LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2806 const char *Name) {
2807 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2808}
2809
Erick Tryzelaar31831792010-02-28 05:51:27 +00002810LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2811 LLVMValueRef LHS, LLVMValueRef RHS,
2812 const char *Name) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00002813 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
Erick Tryzelaar31831792010-02-28 05:51:27 +00002814 unwrap(RHS), Name));
2815}
2816
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002817LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2818 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2819}
2820
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002821LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2822 const char *Name) {
2823 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2824}
2825
2826LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2827 const char *Name) {
2828 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2829}
2830
Dan Gohmanf919bd62009-09-28 21:51:41 +00002831LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2832 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2833}
2834
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002835LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2836 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2837}
2838
2839/*--.. Memory ..............................................................--*/
2840
2841LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2842 const char *Name) {
Chris Lattner229907c2011-07-18 04:54:35 +00002843 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
Victor Hernandezf3db9152009-11-07 00:16:28 +00002844 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2845 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
Andrew Trickdc073ad2013-09-18 23:31:10 +00002846 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2847 ITy, unwrap(Ty), AllocSize,
Craig Topperc6207612014-04-09 06:08:46 +00002848 nullptr, nullptr, "");
Victor Hernandezf3db9152009-11-07 00:16:28 +00002849 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002850}
2851
2852LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2853 LLVMValueRef Val, const char *Name) {
Chris Lattner229907c2011-07-18 04:54:35 +00002854 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
Victor Hernandezf3db9152009-11-07 00:16:28 +00002855 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2856 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
Andrew Trickdc073ad2013-09-18 23:31:10 +00002857 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2858 ITy, unwrap(Ty), AllocSize,
Craig Topperc6207612014-04-09 06:08:46 +00002859 unwrap(Val), nullptr, "");
Victor Hernandezf3db9152009-11-07 00:16:28 +00002860 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002861}
2862
2863LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2864 const char *Name) {
Craig Topperc6207612014-04-09 06:08:46 +00002865 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002866}
2867
2868LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2869 LLVMValueRef Val, const char *Name) {
2870 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2871}
2872
2873LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
Victor Hernandezde5ad422009-10-26 23:43:48 +00002874 return wrap(unwrap(B)->Insert(
2875 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002876}
2877
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002878LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2879 const char *Name) {
2880 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2881}
2882
Andrew Trickdc073ad2013-09-18 23:31:10 +00002883LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002884 LLVMValueRef PointerVal) {
2885 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2886}
2887
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002888static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2889 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +00002890 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
2891 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
2892 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
2893 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
2894 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
2895 case LLVMAtomicOrderingAcquireRelease:
2896 return AtomicOrdering::AcquireRelease;
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002897 case LLVMAtomicOrderingSequentiallyConsistent:
JF Bastien800f87a2016-04-06 21:19:33 +00002898 return AtomicOrdering::SequentiallyConsistent;
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002899 }
Peter Zotov1d98e6d2014-10-28 19:46:44 +00002900
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002901 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2902}
2903
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00002904static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
2905 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +00002906 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
2907 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
2908 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
2909 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
2910 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
2911 case AtomicOrdering::AcquireRelease:
2912 return LLVMAtomicOrderingAcquireRelease;
2913 case AtomicOrdering::SequentiallyConsistent:
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00002914 return LLVMAtomicOrderingSequentiallyConsistent;
2915 }
2916
2917 llvm_unreachable("Invalid AtomicOrdering value!");
2918}
2919
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002920// TODO: Should this and other atomic instructions support building with
2921// "syncscope"?
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002922LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2923 LLVMBool isSingleThread, const char *Name) {
2924 return wrap(
2925 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002926 isSingleThread ? SyncScope::SingleThread
2927 : SyncScope::System,
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002928 Name));
2929}
2930
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002931LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2932 LLVMValueRef *Indices, unsigned NumIndices,
2933 const char *Name) {
Jay Foad040dd822011-07-22 08:16:57 +00002934 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
David Blaikie86ecb1b2015-03-15 01:03:19 +00002935 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002936}
2937
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002938LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2939 LLVMValueRef *Indices, unsigned NumIndices,
2940 const char *Name) {
Jay Foad040dd822011-07-22 08:16:57 +00002941 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
David Blaikieaa41cd52015-04-03 21:33:42 +00002942 return wrap(
2943 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002944}
2945
2946LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2947 unsigned Idx, const char *Name) {
David Blaikie64646022015-04-05 22:41:44 +00002948 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002949}
2950
2951LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2952 const char *Name) {
2953 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2954}
2955
2956LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2957 const char *Name) {
2958 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2959}
2960
Chris Lattner2cc6f9d2012-03-22 03:54:15 +00002961LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2962 Value *P = unwrap<Value>(MemAccessInst);
2963 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2964 return LI->isVolatile();
2965 return cast<StoreInst>(P)->isVolatile();
2966}
2967
2968void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2969 Value *P = unwrap<Value>(MemAccessInst);
2970 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2971 return LI->setVolatile(isVolatile);
2972 return cast<StoreInst>(P)->setVolatile(isVolatile);
2973}
2974
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00002975LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
2976 Value *P = unwrap<Value>(MemAccessInst);
2977 AtomicOrdering O;
2978 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2979 O = LI->getOrdering();
2980 else
2981 O = cast<StoreInst>(P)->getOrdering();
2982 return mapToLLVMOrdering(O);
2983}
2984
2985void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
2986 Value *P = unwrap<Value>(MemAccessInst);
2987 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
2988
2989 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2990 return LI->setOrdering(O);
2991 return cast<StoreInst>(P)->setOrdering(O);
2992}
2993
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002994/*--.. Casts ...............................................................--*/
2995
2996LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2997 LLVMTypeRef DestTy, const char *Name) {
2998 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2999}
3000
3001LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
3002 LLVMTypeRef DestTy, const char *Name) {
3003 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
3004}
3005
3006LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
3007 LLVMTypeRef DestTy, const char *Name) {
3008 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
3009}
3010
3011LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
3012 LLVMTypeRef DestTy, const char *Name) {
3013 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
3014}
3015
3016LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
3017 LLVMTypeRef DestTy, const char *Name) {
3018 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
3019}
3020
3021LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
3022 LLVMTypeRef DestTy, const char *Name) {
3023 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
3024}
3025
3026LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
3027 LLVMTypeRef DestTy, const char *Name) {
3028 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
3029}
3030
3031LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
3032 LLVMTypeRef DestTy, const char *Name) {
3033 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
3034}
3035
3036LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
3037 LLVMTypeRef DestTy, const char *Name) {
3038 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
3039}
3040
3041LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
3042 LLVMTypeRef DestTy, const char *Name) {
3043 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
3044}
3045
3046LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
3047 LLVMTypeRef DestTy, const char *Name) {
3048 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
3049}
3050
3051LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3052 LLVMTypeRef DestTy, const char *Name) {
3053 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
3054}
3055
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003056LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
3057 LLVMTypeRef DestTy, const char *Name) {
3058 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
3059}
3060
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00003061LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3062 LLVMTypeRef DestTy, const char *Name) {
3063 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
3064 Name));
3065}
3066
3067LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3068 LLVMTypeRef DestTy, const char *Name) {
3069 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
3070 Name));
3071}
3072
3073LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3074 LLVMTypeRef DestTy, const char *Name) {
3075 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
3076 Name));
3077}
3078
Erick Tryzelaar31831792010-02-28 05:51:27 +00003079LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
3080 LLVMTypeRef DestTy, const char *Name) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00003081 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
Erick Tryzelaar31831792010-02-28 05:51:27 +00003082 unwrap(DestTy), Name));
3083}
3084
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00003085LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
3086 LLVMTypeRef DestTy, const char *Name) {
3087 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
3088}
3089
3090LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
Duncan Sands9d786d72009-11-23 10:49:03 +00003091 LLVMTypeRef DestTy, const char *Name) {
3092 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
3093 /*isSigned*/true, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00003094}
3095
3096LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
3097 LLVMTypeRef DestTy, const char *Name) {
3098 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
3099}
3100
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00003101/*--.. Comparisons .........................................................--*/
3102
3103LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
3104 LLVMValueRef LHS, LLVMValueRef RHS,
3105 const char *Name) {
3106 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
3107 unwrap(LHS), unwrap(RHS), Name));
3108}
3109
3110LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
3111 LLVMValueRef LHS, LLVMValueRef RHS,
3112 const char *Name) {
3113 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
3114 unwrap(LHS), unwrap(RHS), Name));
3115}
3116
3117/*--.. Miscellaneous instructions ..........................................--*/
3118
3119LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
Jay Foad52131342011-03-30 11:28:46 +00003120 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00003121}
3122
3123LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
3124 LLVMValueRef *Args, unsigned NumArgs,
3125 const char *Name) {
Jay Foad5bd375a2011-07-15 08:37:34 +00003126 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00003127 makeArrayRef(unwrap(Args), NumArgs),
Jay Foad5bd375a2011-07-15 08:37:34 +00003128 Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00003129}
3130
3131LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
3132 LLVMValueRef Then, LLVMValueRef Else,
3133 const char *Name) {
3134 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
3135 Name));
3136}
3137
3138LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
3139 LLVMTypeRef Ty, const char *Name) {
3140 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
3141}
3142
3143LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
3144 LLVMValueRef Index, const char *Name) {
3145 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
3146 Name));
3147}
3148
3149LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
3150 LLVMValueRef EltVal, LLVMValueRef Index,
3151 const char *Name) {
3152 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
3153 unwrap(Index), Name));
3154}
3155
3156LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
3157 LLVMValueRef V2, LLVMValueRef Mask,
3158 const char *Name) {
3159 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
3160 unwrap(Mask), Name));
3161}
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003162
Dan Gohmand5104a52008-11-03 22:55:43 +00003163LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3164 unsigned Index, const char *Name) {
3165 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
3166}
3167
3168LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3169 LLVMValueRef EltVal, unsigned Index,
3170 const char *Name) {
3171 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3172 Index, Name));
3173}
3174
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00003175LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
3176 const char *Name) {
3177 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
3178}
3179
3180LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
3181 const char *Name) {
3182 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3183}
3184
3185LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
3186 LLVMValueRef RHS, const char *Name) {
3187 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
3188}
3189
Andrew Trickdc073ad2013-09-18 23:31:10 +00003190LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
3191 LLVMValueRef PTR, LLVMValueRef Val,
3192 LLVMAtomicOrdering ordering,
Carlo Kok8c6719b2013-04-23 13:21:19 +00003193 LLVMBool singleThread) {
3194 AtomicRMWInst::BinOp intop;
3195 switch (op) {
3196 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
3197 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
3198 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
3199 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
3200 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
3201 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
3202 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
3203 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
3204 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
3205 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
3206 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
3207 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00003208 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003209 mapFromLLVMOrdering(ordering), singleThread ? SyncScope::SingleThread
3210 : SyncScope::System));
Carlo Kok8c6719b2013-04-23 13:21:19 +00003211}
3212
Mehdi Amini43165d92016-03-19 21:28:28 +00003213LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
3214 LLVMValueRef Cmp, LLVMValueRef New,
3215 LLVMAtomicOrdering SuccessOrdering,
3216 LLVMAtomicOrdering FailureOrdering,
3217 LLVMBool singleThread) {
3218
3219 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
3220 unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
3221 mapFromLLVMOrdering(FailureOrdering),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003222 singleThread ? SyncScope::SingleThread : SyncScope::System));
Mehdi Amini43165d92016-03-19 21:28:28 +00003223}
3224
3225
3226LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
3227 Value *P = unwrap<Value>(AtomicInst);
3228
3229 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003230 return I->getSyncScopeID() == SyncScope::SingleThread;
3231 return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() ==
3232 SyncScope::SingleThread;
Mehdi Amini43165d92016-03-19 21:28:28 +00003233}
3234
3235void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
3236 Value *P = unwrap<Value>(AtomicInst);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003237 SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
Mehdi Amini43165d92016-03-19 21:28:28 +00003238
3239 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003240 return I->setSyncScopeID(SSID);
3241 return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID);
Mehdi Amini43165d92016-03-19 21:28:28 +00003242}
3243
3244LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
3245 Value *P = unwrap<Value>(CmpXchgInst);
3246 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
3247}
3248
3249void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
3250 LLVMAtomicOrdering Ordering) {
3251 Value *P = unwrap<Value>(CmpXchgInst);
3252 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3253
3254 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
3255}
3256
3257LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
3258 Value *P = unwrap<Value>(CmpXchgInst);
3259 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
3260}
3261
3262void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
3263 LLVMAtomicOrdering Ordering) {
3264 Value *P = unwrap<Value>(CmpXchgInst);
3265 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3266
3267 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
3268}
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003269
3270/*===-- Module providers --------------------------------------------------===*/
3271
3272LLVMModuleProviderRef
3273LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003274 return reinterpret_cast<LLVMModuleProviderRef>(M);
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003275}
3276
3277void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
3278 delete unwrap(MP);
3279}
3280
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003281
3282/*===-- Memory buffers ----------------------------------------------------===*/
3283
Chris Lattner25963c62010-01-09 22:27:07 +00003284LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
3285 const char *Path,
3286 LLVMMemoryBufferRef *OutMemBuf,
3287 char **OutMessage) {
3288
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003289 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
3290 if (std::error_code EC = MBOrErr.getError()) {
3291 *OutMessage = strdup(EC.message().c_str());
3292 return 1;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003293 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003294 *OutMemBuf = wrap(MBOrErr.get().release());
3295 return 0;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003296}
3297
Chris Lattner25963c62010-01-09 22:27:07 +00003298LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
3299 char **OutMessage) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003300 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
3301 if (std::error_code EC = MBOrErr.getError()) {
3302 *OutMessage = strdup(EC.message().c_str());
3303 return 1;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003304 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003305 *OutMemBuf = wrap(MBOrErr.get().release());
3306 return 0;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003307}
3308
Bill Wendling526276a2013-02-14 19:11:28 +00003309LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
3310 const char *InputData,
3311 size_t InputDataLength,
3312 const char *BufferName,
Bill Wendlingff61da92013-02-14 19:40:27 +00003313 LLVMBool RequiresNullTerminator) {
Bill Wendling526276a2013-02-14 19:11:28 +00003314
Rafael Espindola3560ff22014-08-27 20:03:13 +00003315 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
3316 StringRef(BufferName),
3317 RequiresNullTerminator).release());
Bill Wendling526276a2013-02-14 19:11:28 +00003318}
3319
3320LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
3321 const char *InputData,
3322 size_t InputDataLength,
3323 const char *BufferName) {
3324
Rafael Espindola3560ff22014-08-27 20:03:13 +00003325 return wrap(
3326 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
3327 StringRef(BufferName)).release());
Bill Wendling526276a2013-02-14 19:11:28 +00003328}
3329
Tom Stellard62c03202013-04-18 19:50:53 +00003330const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
Tom Stellard385fa262013-04-16 23:12:47 +00003331 return unwrap(MemBuf)->getBufferStart();
3332}
Bill Wendling526276a2013-02-14 19:11:28 +00003333
Tom Stellardb7fb7242013-04-16 23:12:51 +00003334size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
3335 return unwrap(MemBuf)->getBufferSize();
3336}
3337
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003338void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
3339 delete unwrap(MemBuf);
3340}
Dan Gohman093b42f2010-08-07 00:43:20 +00003341
Owen Anderson4698c5d2010-10-07 17:55:47 +00003342/*===-- Pass Registry -----------------------------------------------------===*/
3343
3344LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
3345 return wrap(PassRegistry::getPassRegistry());
3346}
Dan Gohman093b42f2010-08-07 00:43:20 +00003347
3348/*===-- Pass Manager ------------------------------------------------------===*/
3349
3350LLVMPassManagerRef LLVMCreatePassManager() {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003351 return wrap(new legacy::PassManager());
Dan Gohman093b42f2010-08-07 00:43:20 +00003352}
3353
3354LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003355 return wrap(new legacy::FunctionPassManager(unwrap(M)));
Dan Gohman093b42f2010-08-07 00:43:20 +00003356}
3357
3358LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
3359 return LLVMCreateFunctionPassManagerForModule(
3360 reinterpret_cast<LLVMModuleRef>(P));
3361}
3362
3363LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003364 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
Dan Gohman093b42f2010-08-07 00:43:20 +00003365}
3366
3367LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003368 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
Dan Gohman093b42f2010-08-07 00:43:20 +00003369}
3370
3371LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003372 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
Dan Gohman093b42f2010-08-07 00:43:20 +00003373}
3374
3375LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003376 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
Dan Gohman093b42f2010-08-07 00:43:20 +00003377}
3378
3379void LLVMDisposePassManager(LLVMPassManagerRef PM) {
3380 delete unwrap(PM);
3381}
Duncan Sands1cba0a82013-02-17 16:35:51 +00003382
3383/*===-- Threading ------------------------------------------------------===*/
3384
3385LLVMBool LLVMStartMultithreaded() {
Chandler Carruth39cd2162014-06-27 15:13:01 +00003386 return LLVMIsMultithreaded();
Duncan Sands1cba0a82013-02-17 16:35:51 +00003387}
3388
3389void LLVMStopMultithreaded() {
Duncan Sands1cba0a82013-02-17 16:35:51 +00003390}
3391
3392LLVMBool LLVMIsMultithreaded() {
3393 return llvm_is_multithreaded();
3394}