blob: 02311db66e3531a414635e5408a408dc7d4fa99e [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
Robert Widmannbce367702018-05-14 08:09:00 +0000269/*--.. Module flags ........................................................--*/
270struct LLVMOpaqueModuleFlagEntry {
271 LLVMModuleFlagBehavior Behavior;
272 const char *Key;
273 size_t KeyLen;
274 LLVMMetadataRef Metadata;
275};
276
277static Module::ModFlagBehavior
278map_to_llvmModFlagBehavior(LLVMModuleFlagBehavior Behavior) {
279 switch (Behavior) {
280 case LLVMModuleFlagBehaviorError:
281 return Module::ModFlagBehavior::Error;
282 case LLVMModuleFlagBehaviorWarning:
283 return Module::ModFlagBehavior::Warning;
284 case LLVMModuleFlagBehaviorRequire:
285 return Module::ModFlagBehavior::Require;
286 case LLVMModuleFlagBehaviorOverride:
287 return Module::ModFlagBehavior::Override;
288 case LLVMModuleFlagBehaviorAppend:
289 return Module::ModFlagBehavior::Append;
290 case LLVMModuleFlagBehaviorAppendUnique:
291 return Module::ModFlagBehavior::AppendUnique;
292 }
Simon Pilgrime91a6312018-05-14 12:20:19 +0000293 llvm_unreachable("Unknown LLVMModuleFlagBehavior");
Robert Widmannbce367702018-05-14 08:09:00 +0000294}
295
296static LLVMModuleFlagBehavior
297map_from_llvmModFlagBehavior(Module::ModFlagBehavior Behavior) {
298 switch (Behavior) {
299 case Module::ModFlagBehavior::Error:
300 return LLVMModuleFlagBehaviorError;
301 case Module::ModFlagBehavior::Warning:
302 return LLVMModuleFlagBehaviorWarning;
303 case Module::ModFlagBehavior::Require:
304 return LLVMModuleFlagBehaviorRequire;
305 case Module::ModFlagBehavior::Override:
306 return LLVMModuleFlagBehaviorOverride;
307 case Module::ModFlagBehavior::Append:
308 return LLVMModuleFlagBehaviorAppend;
309 case Module::ModFlagBehavior::AppendUnique:
310 return LLVMModuleFlagBehaviorAppendUnique;
311 default:
312 llvm_unreachable("Unhandled Flag Behavior");
313 }
314}
315
316LLVMModuleFlagEntry *LLVMCopyModuleFlagsMetadata(LLVMModuleRef M, size_t *Len) {
317 SmallVector<Module::ModuleFlagEntry, 8> MFEs;
318 unwrap(M)->getModuleFlagsMetadata(MFEs);
319
320 LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>(
321 safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
322 for (unsigned i = 0; i < MFEs.size(); ++i) {
323 const auto &ModuleFlag = MFEs[i];
324 Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
325 Result[i].Key = ModuleFlag.Key->getString().data();
326 Result[i].KeyLen = ModuleFlag.Key->getString().size();
327 Result[i].Metadata = wrap(ModuleFlag.Val);
328 }
329 *Len = MFEs.size();
330 return Result;
331}
332
333void LLVMDisposeModuleFlagsMetadata(LLVMModuleFlagEntry *Entries) {
334 free(Entries);
335}
336
337LLVMModuleFlagBehavior
338LLVMModuleFlagEntriesGetFlagBehavior(LLVMModuleFlagEntry *Entries,
339 unsigned Index) {
340 LLVMOpaqueModuleFlagEntry MFE =
341 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
342 return MFE.Behavior;
343}
344
345const char *LLVMModuleFlagEntriesGetKey(LLVMModuleFlagEntry *Entries,
346 unsigned Index, size_t *Len) {
347 LLVMOpaqueModuleFlagEntry MFE =
348 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
349 *Len = MFE.KeyLen;
350 return MFE.Key;
351}
352
353LLVMMetadataRef LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry *Entries,
354 unsigned Index) {
355 LLVMOpaqueModuleFlagEntry MFE =
356 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
357 return MFE.Metadata;
358}
359
360LLVMMetadataRef LLVMGetModuleFlag(LLVMModuleRef M,
361 const char *Key, size_t KeyLen) {
362 return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
363}
364
365void LLVMAddModuleFlag(LLVMModuleRef M, LLVMModuleFlagBehavior Behavior,
366 const char *Key, size_t KeyLen,
367 LLVMMetadataRef Val) {
368 unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
369 {Key, KeyLen}, unwrap(Val));
370}
371
372/*--.. Printing modules ....................................................--*/
373
Matthias Braunde58b612017-01-29 17:52:03 +0000374void LLVMDumpModule(LLVMModuleRef M) {
375 unwrap(M)->print(errs(), nullptr,
376 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
Gordon Henriksen6c6075e2008-03-14 23:58:56 +0000377}
378
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000379LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
380 char **ErrorMessage) {
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000381 std::error_code EC;
382 raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
383 if (EC) {
384 *ErrorMessage = strdup(EC.message().c_str());
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000385 return true;
386 }
387
Craig Topperc6207612014-04-09 06:08:46 +0000388 unwrap(M)->print(dest, nullptr);
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000389
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000390 dest.close();
391
392 if (dest.has_error()) {
Bob Haarman9ce2d032017-10-24 01:26:22 +0000393 std::string E = "Error printing to file: " + dest.error().message();
394 *ErrorMessage = strdup(E.c_str());
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000395 return true;
396 }
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000397
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000398 return false;
399}
400
Anders Waldenborg84355db2013-10-16 18:00:54 +0000401char *LLVMPrintModuleToString(LLVMModuleRef M) {
Alp Tokere69170a2014-06-26 22:52:05 +0000402 std::string buf;
403 raw_string_ostream os(buf);
404
Craig Topperc6207612014-04-09 06:08:46 +0000405 unwrap(M)->print(os, nullptr);
Alp Tokere69170a2014-06-26 22:52:05 +0000406 os.flush();
407
408 return strdup(buf.c_str());
Anders Waldenborg84355db2013-10-16 18:00:54 +0000409}
410
Chris Lattner26941452010-04-10 17:52:58 +0000411/*--.. Operations on inline assembler ......................................--*/
Robert Widmannf108d572018-04-06 02:31:29 +0000412void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
413 unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
414}
415
Chris Lattner26941452010-04-10 17:52:58 +0000416void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
417 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
418}
419
Robert Widmannf108d572018-04-06 02:31:29 +0000420void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
421 unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
422}
423
424const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
425 auto &Str = unwrap(M)->getModuleInlineAsm();
426 *Len = Str.length();
427 return Str.c_str();
428}
429
430LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty,
431 char *AsmString, size_t AsmStringSize,
432 char *Constraints, size_t ConstraintsSize,
433 LLVMBool HasSideEffects, LLVMBool IsAlignStack,
434 LLVMInlineAsmDialect Dialect) {
435 InlineAsm::AsmDialect AD;
436 switch (Dialect) {
437 case LLVMInlineAsmDialectATT:
438 AD = InlineAsm::AD_ATT;
Robert Widmann58568252018-04-10 18:10:10 +0000439 break;
Robert Widmannf108d572018-04-06 02:31:29 +0000440 case LLVMInlineAsmDialectIntel:
441 AD = InlineAsm::AD_Intel;
Robert Widmann58568252018-04-10 18:10:10 +0000442 break;
Robert Widmannf108d572018-04-06 02:31:29 +0000443 }
444 return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
445 StringRef(AsmString, AsmStringSize),
446 StringRef(Constraints, ConstraintsSize),
447 HasSideEffects, IsAlignStack, AD));
448}
449
Gordon Henriksen76a03742007-09-18 03:18:57 +0000450
Chris Lattnera7e04b02010-11-28 20:03:44 +0000451/*--.. Operations on module contexts ......................................--*/
452LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
453 return wrap(&unwrap(M)->getContext());
454}
455
456
Gordon Henriksen76a03742007-09-18 03:18:57 +0000457/*===-- Operations on types -----------------------------------------------===*/
458
459/*--.. Operations on all types (mostly) ....................................--*/
460
461LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000462 switch (unwrap(Ty)->getTypeID()) {
463 case Type::VoidTyID:
464 return LLVMVoidTypeKind;
Dan Gohman518cda42011-12-17 00:04:22 +0000465 case Type::HalfTyID:
466 return LLVMHalfTypeKind;
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000467 case Type::FloatTyID:
468 return LLVMFloatTypeKind;
469 case Type::DoubleTyID:
470 return LLVMDoubleTypeKind;
471 case Type::X86_FP80TyID:
472 return LLVMX86_FP80TypeKind;
473 case Type::FP128TyID:
474 return LLVMFP128TypeKind;
475 case Type::PPC_FP128TyID:
476 return LLVMPPC_FP128TypeKind;
477 case Type::LabelTyID:
478 return LLVMLabelTypeKind;
479 case Type::MetadataTyID:
480 return LLVMMetadataTypeKind;
481 case Type::IntegerTyID:
482 return LLVMIntegerTypeKind;
483 case Type::FunctionTyID:
484 return LLVMFunctionTypeKind;
485 case Type::StructTyID:
486 return LLVMStructTypeKind;
487 case Type::ArrayTyID:
488 return LLVMArrayTypeKind;
489 case Type::PointerTyID:
490 return LLVMPointerTypeKind;
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000491 case Type::VectorTyID:
492 return LLVMVectorTypeKind;
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000493 case Type::X86_MMXTyID:
494 return LLVMX86_MMXTypeKind;
David Majnemerb611e3f2015-08-14 05:09:07 +0000495 case Type::TokenTyID:
496 return LLVMTokenTypeKind;
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000497 }
Rafael Espindolaba7df702013-12-07 02:27:52 +0000498 llvm_unreachable("Unhandled TypeID.");
Gordon Henriksen76a03742007-09-18 03:18:57 +0000499}
500
Torok Edwin1cd9ade2011-10-06 12:13:28 +0000501LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
502{
503 return unwrap(Ty)->isSized();
504}
505
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000506LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
507 return wrap(&unwrap(Ty)->getContext());
508}
509
whitequark31dff532018-04-17 14:52:43 +0000510void LLVMDumpType(LLVMTypeRef Ty) {
511 return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
Anders Waldenborgb822cff2013-10-16 21:30:25 +0000512}
513
Anders Waldenborg47b3bd32013-10-22 06:58:34 +0000514char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
Alp Tokere69170a2014-06-26 22:52:05 +0000515 std::string buf;
516 raw_string_ostream os(buf);
Anders Waldenborg47b3bd32013-10-22 06:58:34 +0000517
Richard Trieuc1485222014-06-21 02:43:02 +0000518 if (unwrap(Ty))
519 unwrap(Ty)->print(os);
520 else
521 os << "Printing <null> Type";
522
Alp Tokere69170a2014-06-26 22:52:05 +0000523 os.flush();
524
525 return strdup(buf.c_str());
Anders Waldenborg47b3bd32013-10-22 06:58:34 +0000526}
527
Gordon Henriksen76a03742007-09-18 03:18:57 +0000528/*--.. Operations on integer types .........................................--*/
529
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000530LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
531 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000532}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000533LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
534 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000535}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000536LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
537 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000538}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000539LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
540 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000541}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000542LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
543 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
544}
Kit Barton72918022015-04-17 15:32:15 +0000545LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
546 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
547}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000548LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
549 return wrap(IntegerType::get(*unwrap(C), NumBits));
Owen Anderson55f1c092009-08-13 21:58:54 +0000550}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000551
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000552LLVMTypeRef LLVMInt1Type(void) {
553 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
554}
555LLVMTypeRef LLVMInt8Type(void) {
556 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
557}
558LLVMTypeRef LLVMInt16Type(void) {
559 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
560}
561LLVMTypeRef LLVMInt32Type(void) {
562 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
563}
564LLVMTypeRef LLVMInt64Type(void) {
565 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
566}
Kit Barton72918022015-04-17 15:32:15 +0000567LLVMTypeRef LLVMInt128Type(void) {
568 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
569}
Gordon Henriksened7beaa2007-10-06 16:05:20 +0000570LLVMTypeRef LLVMIntType(unsigned NumBits) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000571 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
Gordon Henriksen76a03742007-09-18 03:18:57 +0000572}
573
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000574unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000575 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
576}
577
578/*--.. Operations on real types ............................................--*/
579
Dan Gohman518cda42011-12-17 00:04:22 +0000580LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
581 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
582}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000583LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
584 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
585}
586LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
587 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
588}
589LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
590 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
591}
592LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
593 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
594}
595LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
596 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
597}
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000598LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
599 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
600}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000601
Dan Gohman518cda42011-12-17 00:04:22 +0000602LLVMTypeRef LLVMHalfType(void) {
603 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
604}
Owen Anderson55f1c092009-08-13 21:58:54 +0000605LLVMTypeRef LLVMFloatType(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000606 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000607}
608LLVMTypeRef LLVMDoubleType(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000609 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000610}
611LLVMTypeRef LLVMX86FP80Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000612 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000613}
614LLVMTypeRef LLVMFP128Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000615 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000616}
617LLVMTypeRef LLVMPPCFP128Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000618 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000619}
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000620LLVMTypeRef LLVMX86MMXType(void) {
621 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
622}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000623
624/*--.. Operations on function types ........................................--*/
625
Gordon Henriksened7beaa2007-10-06 16:05:20 +0000626LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
627 LLVMTypeRef *ParamTypes, unsigned ParamCount,
Chris Lattner25963c62010-01-09 22:27:07 +0000628 LLVMBool IsVarArg) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000629 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
Owen Anderson4056ca92009-07-29 22:17:13 +0000630 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000631}
632
Chris Lattner25963c62010-01-09 22:27:07 +0000633LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000634 return unwrap<FunctionType>(FunctionTy)->isVarArg();
635}
636
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000637LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000638 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
639}
640
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000641unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000642 return unwrap<FunctionType>(FunctionTy)->getNumParams();
643}
644
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000645void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000646 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
647 for (FunctionType::param_iterator I = Ty->param_begin(),
648 E = Ty->param_end(); I != E; ++I)
649 *Dest++ = wrap(*I);
650}
651
652/*--.. Operations on struct types ..........................................--*/
653
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000654LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
Chris Lattner25963c62010-01-09 22:27:07 +0000655 unsigned ElementCount, LLVMBool Packed) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000656 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000657 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000658}
659
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000660LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
Chris Lattner25963c62010-01-09 22:27:07 +0000661 unsigned ElementCount, LLVMBool Packed) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000662 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
663 ElementCount, Packed);
664}
665
Chris Lattnere71ccde2011-07-14 05:53:17 +0000666LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
667{
Chris Lattner01beceb2011-08-12 18:07:07 +0000668 return wrap(StructType::create(*unwrap(C), Name));
Chris Lattnere71ccde2011-07-14 05:53:17 +0000669}
670
Torok Edwin0d5f6ae2011-10-06 12:12:50 +0000671const char *LLVMGetStructName(LLVMTypeRef Ty)
672{
Torok Edwin2e9affe2011-10-14 20:37:42 +0000673 StructType *Type = unwrap<StructType>(Ty);
674 if (!Type->hasName())
Craig Topperc6207612014-04-09 06:08:46 +0000675 return nullptr;
Torok Edwin2e9affe2011-10-14 20:37:42 +0000676 return Type->getName().data();
Torok Edwin0d5f6ae2011-10-06 12:12:50 +0000677}
678
Chris Lattnere71ccde2011-07-14 05:53:17 +0000679void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
680 unsigned ElementCount, LLVMBool Packed) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000681 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
Chris Lattnere71ccde2011-07-14 05:53:17 +0000682 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
683}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000684
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000685unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000686 return unwrap<StructType>(StructTy)->getNumElements();
687}
688
689void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
690 StructType *Ty = unwrap<StructType>(StructTy);
Nick Lewyckyf735b7b2011-04-20 22:52:37 +0000691 for (StructType::element_iterator I = Ty->element_begin(),
Gordon Henriksen76a03742007-09-18 03:18:57 +0000692 E = Ty->element_end(); I != E; ++I)
693 *Dest++ = wrap(*I);
694}
695
Peter Zotovc164a3f2015-06-04 09:09:53 +0000696LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
697 StructType *Ty = unwrap<StructType>(StructTy);
698 return wrap(Ty->getTypeAtIndex(i));
699}
700
Chris Lattner25963c62010-01-09 22:27:07 +0000701LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000702 return unwrap<StructType>(StructTy)->isPacked();
703}
704
Chris Lattner17cf05b2011-07-14 16:20:28 +0000705LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
706 return unwrap<StructType>(StructTy)->isOpaque();
707}
708
709LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
710 return wrap(unwrap(M)->getTypeByName(Name));
711}
712
Gordon Henriksen76a03742007-09-18 03:18:57 +0000713/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
714
whitequarkf6059fd2017-06-05 11:49:52 +0000715void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
716 int i = 0;
717 for (auto *T : unwrap(Tp)->subtypes()) {
718 Arr[i] = wrap(T);
719 i++;
720 }
721}
722
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000723LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000724 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000725}
726
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000727LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000728 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000729}
730
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000731LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000732 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000733}
734
Peter Collingbourne45681582016-12-02 03:05:41 +0000735LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
736 auto *Ty = unwrap<Type>(WrappedTy);
737 if (auto *PTy = dyn_cast<PointerType>(Ty))
738 return wrap(PTy->getElementType());
739 return wrap(cast<SequentialType>(Ty)->getElementType());
Gordon Henriksen76a03742007-09-18 03:18:57 +0000740}
741
whitequarkf6059fd2017-06-05 11:49:52 +0000742unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
743 return unwrap(Tp)->getNumContainedTypes();
744}
745
Gordon Henriksen76a03742007-09-18 03:18:57 +0000746unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
747 return unwrap<ArrayType>(ArrayTy)->getNumElements();
748}
749
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000750unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
751 return unwrap<PointerType>(PointerTy)->getAddressSpace();
752}
753
Gordon Henriksen76a03742007-09-18 03:18:57 +0000754unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
755 return unwrap<VectorType>(VectorTy)->getNumElements();
756}
757
758/*--.. Operations on other types ...........................................--*/
759
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000760LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
761 return wrap(Type::getVoidTy(*unwrap(C)));
Owen Anderson55f1c092009-08-13 21:58:54 +0000762}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000763LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
764 return wrap(Type::getLabelTy(*unwrap(C)));
765}
whitequark131f98f2017-10-27 11:51:40 +0000766LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
767 return wrap(Type::getTokenTy(*unwrap(C)));
768}
769LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) {
770 return wrap(Type::getMetadataTy(*unwrap(C)));
771}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000772
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000773LLVMTypeRef LLVMVoidType(void) {
774 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
775}
776LLVMTypeRef LLVMLabelType(void) {
777 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
778}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000779
780/*===-- Operations on values ----------------------------------------------===*/
781
782/*--.. Operations on all values ............................................--*/
783
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000784LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000785 return wrap(unwrap(Val)->getType());
786}
787
Peter Zotov3e4561c2016-04-06 22:21:29 +0000788LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
789 switch(unwrap(Val)->getValueID()) {
790#define HANDLE_VALUE(Name) \
791 case Value::Name##Val: \
792 return LLVM##Name##ValueKind;
793#include "llvm/IR/Value.def"
794 default:
795 return LLVMInstructionValueKind;
796 }
797}
798
Gordon Henriksen76a03742007-09-18 03:18:57 +0000799const char *LLVMGetValueName(LLVMValueRef Val) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000800 return unwrap(Val)->getName().data();
Gordon Henriksen76a03742007-09-18 03:18:57 +0000801}
802
803void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
804 unwrap(Val)->setName(Name);
805}
806
whitequark31dff532018-04-17 14:52:43 +0000807void LLVMDumpValue(LLVMValueRef Val) {
Sam McCalla682dfb2017-01-30 05:40:52 +0000808 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
Gordon Henriksen1d0d24c2007-10-06 00:08:49 +0000809}
810
Peter Zotovcd93b372013-11-06 09:21:01 +0000811char* LLVMPrintValueToString(LLVMValueRef Val) {
Alp Tokere69170a2014-06-26 22:52:05 +0000812 std::string buf;
813 raw_string_ostream os(buf);
Peter Zotovcd93b372013-11-06 09:21:01 +0000814
Richard Trieuc1485222014-06-21 02:43:02 +0000815 if (unwrap(Val))
816 unwrap(Val)->print(os);
817 else
818 os << "Printing <null> Value";
819
Alp Tokere69170a2014-06-26 22:52:05 +0000820 os.flush();
821
822 return strdup(buf.c_str());
Peter Zotovcd93b372013-11-06 09:21:01 +0000823}
824
Chris Lattner40cf28d2009-10-12 04:01:02 +0000825void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
826 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
827}
Gordon Henriksen29e38942008-12-19 18:39:45 +0000828
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000829int LLVMHasMetadata(LLVMValueRef Inst) {
830 return unwrap<Instruction>(Inst)->hasMetadata();
831}
832
833LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000834 auto *I = unwrap<Instruction>(Inst);
835 assert(I && "Expected instruction");
836 if (auto *MD = I->getMetadata(KindID))
837 return wrap(MetadataAsValue::get(I->getContext(), MD));
838 return nullptr;
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000839}
840
Bjorn Steinbrinka09ac002015-01-28 16:35:59 +0000841// MetadataAsValue uses a canonical format which strips the actual MDNode for
842// MDNode with just a single constant value, storing just a ConstantAsMetadata
843// This undoes this canonicalization, reconstructing the MDNode.
844static MDNode *extractMDNode(MetadataAsValue *MAV) {
845 Metadata *MD = MAV->getMetadata();
846 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
847 "Expected a metadata node or a canonicalized constant");
848
849 if (MDNode *N = dyn_cast<MDNode>(MD))
850 return N;
851
852 return MDNode::get(MAV->getContext(), MD);
853}
854
855void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
856 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
857
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000858 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000859}
860
Gordon Henriksen29e38942008-12-19 18:39:45 +0000861/*--.. Conversion functions ................................................--*/
862
863#define LLVM_DEFINE_VALUE_CAST(name) \
864 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
865 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
866 }
867
868LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
869
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000870LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
871 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
872 if (isa<MDNode>(MD->getMetadata()) ||
873 isa<ValueAsMetadata>(MD->getMetadata()))
874 return Val;
875 return nullptr;
876}
877
878LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
879 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
880 if (isa<MDString>(MD->getMetadata()))
881 return Val;
882 return nullptr;
883}
884
Chris Lattner40cf28d2009-10-12 04:01:02 +0000885/*--.. Operations on Uses ..................................................--*/
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000886LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
Chris Lattner40cf28d2009-10-12 04:01:02 +0000887 Value *V = unwrap(Val);
888 Value::use_iterator I = V->use_begin();
889 if (I == V->use_end())
Craig Topperc6207612014-04-09 06:08:46 +0000890 return nullptr;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000891 return wrap(&*I);
Chris Lattner40cf28d2009-10-12 04:01:02 +0000892}
893
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000894LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
895 Use *Next = unwrap(U)->getNext();
896 if (Next)
897 return wrap(Next);
Craig Topperc6207612014-04-09 06:08:46 +0000898 return nullptr;
Chris Lattner40cf28d2009-10-12 04:01:02 +0000899}
900
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000901LLVMValueRef LLVMGetUser(LLVMUseRef U) {
902 return wrap(unwrap(U)->getUser());
Chris Lattner40cf28d2009-10-12 04:01:02 +0000903}
904
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000905LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
906 return wrap(unwrap(U)->get());
Chris Lattner40cf28d2009-10-12 04:01:02 +0000907}
908
909/*--.. Operations on Users .................................................--*/
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000910
911static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
912 unsigned Index) {
913 Metadata *Op = N->getOperand(Index);
914 if (!Op)
915 return nullptr;
916 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
917 return wrap(C->getValue());
918 return wrap(MetadataAsValue::get(Context, Op));
919}
920
Chris Lattner40cf28d2009-10-12 04:01:02 +0000921LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
Torok Edwinfec812e2011-10-06 12:13:11 +0000922 Value *V = unwrap(Val);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000923 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
924 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
925 assert(Index == 0 && "Function-local metadata can only have one operand");
926 return wrap(L->getValue());
927 }
928 return getMDNodeOperandImpl(V->getContext(),
929 cast<MDNode>(MD->getMetadata()), Index);
930 }
931
Torok Edwinfec812e2011-10-06 12:13:11 +0000932 return wrap(cast<User>(V)->getOperand(Index));
Chris Lattner40cf28d2009-10-12 04:01:02 +0000933}
Gordon Henriksen29e38942008-12-19 18:39:45 +0000934
Peter Zotovb19f78f2014-08-12 02:55:40 +0000935LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
936 Value *V = unwrap(Val);
937 return wrap(&cast<User>(V)->getOperandUse(Index));
938}
939
Erick Tryzelaarb4d48702010-08-20 14:51:22 +0000940void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
941 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
942}
943
944int LLVMGetNumOperands(LLVMValueRef Val) {
Torok Edwinfec812e2011-10-06 12:13:11 +0000945 Value *V = unwrap(Val);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000946 if (isa<MetadataAsValue>(V))
947 return LLVMGetMDNodeNumOperands(Val);
948
Torok Edwinfec812e2011-10-06 12:13:11 +0000949 return cast<User>(V)->getNumOperands();
Erick Tryzelaarb4d48702010-08-20 14:51:22 +0000950}
951
Gordon Henriksen76a03742007-09-18 03:18:57 +0000952/*--.. Operations on constants of any type .................................--*/
953
Gordon Henriksen1046c732007-10-06 15:11:06 +0000954LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000955 return wrap(Constant::getNullValue(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000956}
957
Gordon Henriksen1046c732007-10-06 15:11:06 +0000958LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000959 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000960}
961
962LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
Owen Andersonb292b8c2009-07-30 23:03:37 +0000963 return wrap(UndefValue::get(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000964}
965
Chris Lattner25963c62010-01-09 22:27:07 +0000966LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
Gordon Henriksendc88c062007-09-18 18:07:51 +0000967 return isa<Constant>(unwrap(Ty));
968}
969
Chris Lattner25963c62010-01-09 22:27:07 +0000970LLVMBool LLVMIsNull(LLVMValueRef Val) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000971 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
972 return C->isNullValue();
973 return false;
974}
975
Chris Lattner25963c62010-01-09 22:27:07 +0000976LLVMBool LLVMIsUndef(LLVMValueRef Val) {
Gordon Henriksendc88c062007-09-18 18:07:51 +0000977 return isa<UndefValue>(unwrap(Val));
978}
979
Chris Lattner7f318242009-07-06 17:29:59 +0000980LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
Amaury Sechet9bbda192016-04-25 22:23:35 +0000981 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
Chris Lattner7f318242009-07-06 17:29:59 +0000982}
983
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000984/*--.. Operations on metadata nodes ........................................--*/
985
986LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
987 unsigned SLen) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000988 LLVMContext &Context = *unwrap(C);
989 return wrap(MetadataAsValue::get(
990 Context, MDString::get(Context, StringRef(Str, SLen))));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000991}
992
993LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
994 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
995}
996
997LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
998 unsigned Count) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000999 LLVMContext &Context = *unwrap(C);
1000 SmallVector<Metadata *, 8> MDs;
1001 for (auto *OV : makeArrayRef(Vals, Count)) {
1002 Value *V = unwrap(OV);
1003 Metadata *MD;
1004 if (!V)
1005 MD = nullptr;
1006 else if (auto *C = dyn_cast<Constant>(V))
1007 MD = ConstantAsMetadata::get(C);
1008 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1009 MD = MDV->getMetadata();
1010 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1011 "outside of direct argument to call");
1012 } else {
1013 // This is function-local metadata. Pretend to make an MDNode.
1014 assert(Count == 1 &&
1015 "Expected only one operand to function-local metadata");
1016 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
1017 }
1018
1019 MDs.push_back(MD);
1020 }
1021 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00001022}
1023
1024LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1025 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1026}
1027
Amaury Sechetf8429752017-04-17 11:52:54 +00001028LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
1029 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1030}
1031
1032LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {
1033 auto *V = unwrap(Val);
1034 if (auto *C = dyn_cast<Constant>(V))
1035 return wrap(ConstantAsMetadata::get(C));
1036 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1037 return wrap(MAV->getMetadata());
1038 return wrap(ValueAsMetadata::get(V));
1039}
1040
Amaury Sechet7c2883c2016-04-03 21:06:04 +00001041const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001042 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1043 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
Amaury Sechet7c2883c2016-04-03 21:06:04 +00001044 *Length = S->getString().size();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001045 return S->getString().data();
1046 }
Amaury Sechet7c2883c2016-04-03 21:06:04 +00001047 *Length = 0;
Craig Topperc6207612014-04-09 06:08:46 +00001048 return nullptr;
Torok Edwinfec812e2011-10-06 12:13:11 +00001049}
1050
Amaury Sechetb130f432016-04-23 00:12:45 +00001051unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001052 auto *MD = cast<MetadataAsValue>(unwrap(V));
1053 if (isa<ValueAsMetadata>(MD->getMetadata()))
1054 return 1;
1055 return cast<MDNode>(MD->getMetadata())->getNumOperands();
Duncan Sands12ccbe72012-09-19 20:29:39 +00001056}
1057
Amaury Sechetb130f432016-04-23 00:12:45 +00001058void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001059 auto *MD = cast<MetadataAsValue>(unwrap(V));
1060 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1061 *Dest = wrap(MDV->getValue());
1062 return;
1063 }
1064 const auto *N = cast<MDNode>(MD->getMetadata());
Duncan Sands12ccbe72012-09-19 20:29:39 +00001065 const unsigned numOperands = N->getNumOperands();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001066 LLVMContext &Context = unwrap(V)->getContext();
Duncan Sands12ccbe72012-09-19 20:29:39 +00001067 for (unsigned i = 0; i < numOperands; i++)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001068 Dest[i] = getMDNodeOperandImpl(Context, N, i);
Duncan Sands12ccbe72012-09-19 20:29:39 +00001069}
1070
Amaury Sechetb130f432016-04-23 00:12:45 +00001071unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
1072 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
Torok Edwin2e9affe2011-10-14 20:37:42 +00001073 return N->getNumOperands();
1074 }
1075 return 0;
Torok Edwinfec812e2011-10-06 12:13:11 +00001076}
1077
Amaury Sechetb130f432016-04-23 00:12:45 +00001078void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
1079 LLVMValueRef *Dest) {
1080 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
Torok Edwin2e9affe2011-10-14 20:37:42 +00001081 if (!N)
1082 return;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001083 LLVMContext &Context = unwrap(M)->getContext();
Torok Edwin2e9affe2011-10-14 20:37:42 +00001084 for (unsigned i=0;i<N->getNumOperands();i++)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001085 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
Torok Edwinfec812e2011-10-06 12:13:11 +00001086}
1087
Amaury Sechetb130f432016-04-23 00:12:45 +00001088void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
1089 LLVMValueRef Val) {
1090 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
Devang Patel92245402011-12-20 19:29:36 +00001091 if (!N)
1092 return;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001093 if (!Val)
1094 return;
Bjorn Steinbrinka09ac002015-01-28 16:35:59 +00001095 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
Devang Patel92245402011-12-20 19:29:36 +00001096}
1097
Gordon Henriksen76a03742007-09-18 03:18:57 +00001098/*--.. Operations on scalar constants ......................................--*/
1099
Gordon Henriksen1046c732007-10-06 15:11:06 +00001100LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
Chris Lattner25963c62010-01-09 22:27:07 +00001101 LLVMBool SignExtend) {
Owen Andersonedb4a702009-07-24 23:12:02 +00001102 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001103}
1104
Chris Lattner4329e072010-11-23 02:47:22 +00001105LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
1106 unsigned NumWords,
1107 const uint64_t Words[]) {
1108 IntegerType *Ty = unwrap<IntegerType>(IntTy);
1109 return wrap(ConstantInt::get(Ty->getContext(),
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00001110 APInt(Ty->getBitWidth(),
1111 makeArrayRef(Words, NumWords))));
Chris Lattner4329e072010-11-23 02:47:22 +00001112}
1113
Erick Tryzelaardd991352009-08-16 23:36:46 +00001114LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
1115 uint8_t Radix) {
1116 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1117 Radix));
1118}
1119
1120LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
1121 unsigned SLen, uint8_t Radix) {
1122 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1123 Radix));
Gordon Henriksen931e1212008-02-02 01:07:50 +00001124}
1125
Gordon Henriksen1046c732007-10-06 15:11:06 +00001126LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
Erick Tryzelaardd991352009-08-16 23:36:46 +00001127 return wrap(ConstantFP::get(unwrap(RealTy), N));
Gordon Henriksen931e1212008-02-02 01:07:50 +00001128}
1129
1130LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
Erick Tryzelaardd991352009-08-16 23:36:46 +00001131 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1132}
1133
1134LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
1135 unsigned SLen) {
1136 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001137}
1138
Chris Lattner40cf28d2009-10-12 04:01:02 +00001139unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1140 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1141}
1142
1143long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1144 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1145}
1146
Peter Zotov1d98e6d2014-10-28 19:46:44 +00001147double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1148 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1149 Type *Ty = cFP->getType();
1150
1151 if (Ty->isFloatTy()) {
1152 *LosesInfo = false;
1153 return cFP->getValueAPF().convertToFloat();
1154 }
1155
1156 if (Ty->isDoubleTy()) {
1157 *LosesInfo = false;
1158 return cFP->getValueAPF().convertToDouble();
1159 }
1160
1161 bool APFLosesInfo;
1162 APFloat APF = cFP->getValueAPF();
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001163 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
Peter Zotov1d98e6d2014-10-28 19:46:44 +00001164 *LosesInfo = APFLosesInfo;
1165 return APF.convertToDouble();
1166}
1167
Gordon Henriksen76a03742007-09-18 03:18:57 +00001168/*--.. Operations on composite constants ...................................--*/
1169
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001170LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
Chris Lattner25963c62010-01-09 22:27:07 +00001171 unsigned Length,
1172 LLVMBool DontNullTerminate) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001173 /* Inverted the sense of AddNull because ', 0)' is a
1174 better mnemonic for null termination than ', 1)'. */
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001175 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1176 DontNullTerminate == 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001177}
Amaury Sechet006ce632016-03-13 00:54:40 +00001178
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001179LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
Chris Lattner25963c62010-01-09 22:27:07 +00001180 LLVMBool DontNullTerminate) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001181 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1182 DontNullTerminate);
1183}
Peter Zotovf9aa8822014-08-03 23:54:16 +00001184
Amaury Sechet006ce632016-03-13 00:54:40 +00001185LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1186 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
Peter Zotovf9aa8822014-08-03 23:54:16 +00001187}
1188
Amaury Sechet006ce632016-03-13 00:54:40 +00001189LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1190 return unwrap<ConstantDataSequential>(C)->isString();
Peter Zotovf9aa8822014-08-03 23:54:16 +00001191}
1192
Amaury Sechet7c2883c2016-04-03 21:06:04 +00001193const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1194 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1195 *Length = Str.size();
1196 return Str.data();
Peter Zotovf9aa8822014-08-03 23:54:16 +00001197}
1198
Gordon Henriksen1046c732007-10-06 15:11:06 +00001199LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1200 LLVMValueRef *ConstantVals, unsigned Length) {
Jay Foad83be3612011-06-22 09:24:39 +00001201 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1202 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001203}
Peter Zotovf9aa8822014-08-03 23:54:16 +00001204
Amaury Sechetc78768f2016-03-13 00:40:12 +00001205LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1206 LLVMValueRef *ConstantVals,
1207 unsigned Count, LLVMBool Packed) {
1208 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1209 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1210 Packed != 0));
1211}
1212
Gordon Henriksen1046c732007-10-06 15:11:06 +00001213LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
Chris Lattner25963c62010-01-09 22:27:07 +00001214 LLVMBool Packed) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001215 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1216 Packed);
Gordon Henriksen76a03742007-09-18 03:18:57 +00001217}
Rafael Espindola784ad242011-07-14 19:09:08 +00001218
1219LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1220 LLVMValueRef *ConstantVals,
1221 unsigned Count) {
1222 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
Chris Lattner229907c2011-07-18 04:54:35 +00001223 StructType *Ty = cast<StructType>(unwrap(StructTy));
Rafael Espindola784ad242011-07-14 19:09:08 +00001224
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001225 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
Rafael Espindola784ad242011-07-14 19:09:08 +00001226}
1227
Gordon Henriksen1046c732007-10-06 15:11:06 +00001228LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001229 return wrap(ConstantVector::get(makeArrayRef(
Chris Lattner69229312011-02-15 00:14:00 +00001230 unwrap<Constant>(ScalarConstantVals, Size), Size)));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001231}
Torok Edwin05dc9d62011-10-06 12:39:34 +00001232
1233/*-- Opcode mapping */
1234
1235static LLVMOpcode map_to_llvmopcode(int opcode)
1236{
1237 switch (opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00001238 default: llvm_unreachable("Unhandled Opcode.");
Torok Edwin05dc9d62011-10-06 12:39:34 +00001239#define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
Chandler Carruth9fb823b2013-01-02 11:36:10 +00001240#include "llvm/IR/Instruction.def"
Torok Edwin05dc9d62011-10-06 12:39:34 +00001241#undef HANDLE_INST
Torok Edwin05dc9d62011-10-06 12:39:34 +00001242 }
1243}
1244
1245static int map_from_llvmopcode(LLVMOpcode code)
1246{
1247 switch (code) {
1248#define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
Chandler Carruth9fb823b2013-01-02 11:36:10 +00001249#include "llvm/IR/Instruction.def"
Torok Edwin05dc9d62011-10-06 12:39:34 +00001250#undef HANDLE_INST
Torok Edwin05dc9d62011-10-06 12:39:34 +00001251 }
David Blaikie486df732012-01-16 23:24:27 +00001252 llvm_unreachable("Unhandled Opcode.");
Torok Edwin05dc9d62011-10-06 12:39:34 +00001253}
1254
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001255/*--.. Constant expressions ................................................--*/
1256
Chris Lattner40cf28d2009-10-12 04:01:02 +00001257LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00001258 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
Chris Lattner40cf28d2009-10-12 04:01:02 +00001259}
1260
Duncan Sandsd334aca2009-05-21 15:52:21 +00001261LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001262 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
Duncan Sandsd334aca2009-05-21 15:52:21 +00001263}
1264
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001265LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001266 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001267}
1268
1269LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001270 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001271}
1272
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001273LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001274 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001275}
1276
1277LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001278 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001279}
1280
1281
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001282LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001283 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001284}
1285
1286LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001287 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001288}
1289
1290LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001291 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001292 unwrap<Constant>(RHSConstant)));
1293}
1294
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001295LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1296 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001297 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001298 unwrap<Constant>(RHSConstant)));
1299}
1300
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001301LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1302 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001303 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001304 unwrap<Constant>(RHSConstant)));
1305}
1306
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001307LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001308 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001309 unwrap<Constant>(RHSConstant)));
1310}
1311
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001312LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001313 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001314 unwrap<Constant>(RHSConstant)));
1315}
1316
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001317LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1318 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001319 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001320 unwrap<Constant>(RHSConstant)));
1321}
1322
1323LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1324 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001325 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001326 unwrap<Constant>(RHSConstant)));
1327}
1328
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001329LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1330 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1331 unwrap<Constant>(RHSConstant)));
1332}
1333
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001334LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001335 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001336 unwrap<Constant>(RHSConstant)));
1337}
1338
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001339LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1340 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001341 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001342 unwrap<Constant>(RHSConstant)));
1343}
1344
1345LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1346 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001347 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001348 unwrap<Constant>(RHSConstant)));
1349}
1350
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001351LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001352 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001353 unwrap<Constant>(RHSConstant)));
1354}
1355
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001356LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001357 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001358 unwrap<Constant>(RHSConstant)));
1359}
1360
Manuel Jacob49fafb12016-10-04 23:32:42 +00001361LLVMValueRef LLVMConstExactUDiv(LLVMValueRef LHSConstant,
1362 LLVMValueRef RHSConstant) {
1363 return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1364 unwrap<Constant>(RHSConstant)));
1365}
1366
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001367LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001368 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001369 unwrap<Constant>(RHSConstant)));
1370}
1371
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001372LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
1373 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001374 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001375 unwrap<Constant>(RHSConstant)));
1376}
1377
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001378LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001379 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001380 unwrap<Constant>(RHSConstant)));
1381}
1382
1383LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001384 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001385 unwrap<Constant>(RHSConstant)));
1386}
1387
1388LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001389 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001390 unwrap<Constant>(RHSConstant)));
1391}
1392
1393LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001394 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001395 unwrap<Constant>(RHSConstant)));
1396}
1397
1398LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001399 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001400 unwrap<Constant>(RHSConstant)));
1401}
1402
1403LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001404 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001405 unwrap<Constant>(RHSConstant)));
1406}
1407
1408LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001409 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001410 unwrap<Constant>(RHSConstant)));
1411}
1412
1413LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1414 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Owen Anderson487375e2009-07-29 18:55:55 +00001415 return wrap(ConstantExpr::getICmp(Predicate,
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001416 unwrap<Constant>(LHSConstant),
1417 unwrap<Constant>(RHSConstant)));
1418}
1419
1420LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1421 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Owen Anderson487375e2009-07-29 18:55:55 +00001422 return wrap(ConstantExpr::getFCmp(Predicate,
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001423 unwrap<Constant>(LHSConstant),
1424 unwrap<Constant>(RHSConstant)));
1425}
1426
1427LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001428 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1429 unwrap<Constant>(RHSConstant)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001430}
1431
1432LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001433 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001434 unwrap<Constant>(RHSConstant)));
1435}
1436
1437LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001438 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001439 unwrap<Constant>(RHSConstant)));
1440}
1441
1442LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1443 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
Jay Foaded8db7d2011-07-21 14:31:17 +00001444 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1445 NumIndices);
David Blaikie4a2e73b2015-04-02 18:55:32 +00001446 return wrap(ConstantExpr::getGetElementPtr(
1447 nullptr, unwrap<Constant>(ConstantVal), IdxList));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001448}
1449
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001450LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1451 LLVMValueRef *ConstantIndices,
1452 unsigned NumIndices) {
1453 Constant* Val = unwrap<Constant>(ConstantVal);
Jay Foaded8db7d2011-07-21 14:31:17 +00001454 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1455 NumIndices);
David Blaikie4a2e73b2015-04-02 18:55:32 +00001456 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList));
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001457}
1458
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001459LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001460 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001461 unwrap(ToType)));
1462}
1463
1464LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001465 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001466 unwrap(ToType)));
1467}
1468
1469LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001470 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001471 unwrap(ToType)));
1472}
1473
1474LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001475 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001476 unwrap(ToType)));
1477}
1478
1479LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001480 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001481 unwrap(ToType)));
1482}
1483
1484LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001485 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001486 unwrap(ToType)));
1487}
1488
1489LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Owen Anderson487375e2009-07-29 18:55:55 +00001490 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001491 unwrap(ToType)));
1492}
1493
1494LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Owen Anderson487375e2009-07-29 18:55:55 +00001495 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001496 unwrap(ToType)));
1497}
1498
1499LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001500 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001501 unwrap(ToType)));
1502}
1503
1504LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001505 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001506 unwrap(ToType)));
1507}
1508
1509LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001510 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001511 unwrap(ToType)));
1512}
1513
1514LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001515 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001516 unwrap(ToType)));
1517}
1518
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001519LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1520 LLVMTypeRef ToType) {
1521 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1522 unwrap(ToType)));
1523}
1524
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001525LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1526 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001527 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001528 unwrap(ToType)));
1529}
1530
1531LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1532 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001533 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001534 unwrap(ToType)));
1535}
1536
1537LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1538 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001539 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001540 unwrap(ToType)));
1541}
1542
1543LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1544 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001545 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001546 unwrap(ToType)));
1547}
1548
1549LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
Chris Lattner25963c62010-01-09 22:27:07 +00001550 LLVMBool isSigned) {
Chris Lattner69229312011-02-15 00:14:00 +00001551 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1552 unwrap(ToType), isSigned));
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001553}
1554
1555LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001556 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001557 unwrap(ToType)));
1558}
1559
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001560LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1561 LLVMValueRef ConstantIfTrue,
1562 LLVMValueRef ConstantIfFalse) {
Chris Lattner69229312011-02-15 00:14:00 +00001563 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001564 unwrap<Constant>(ConstantIfTrue),
1565 unwrap<Constant>(ConstantIfFalse)));
1566}
1567
1568LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1569 LLVMValueRef IndexConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001570 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001571 unwrap<Constant>(IndexConstant)));
1572}
1573
1574LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1575 LLVMValueRef ElementValueConstant,
1576 LLVMValueRef IndexConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001577 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001578 unwrap<Constant>(ElementValueConstant),
1579 unwrap<Constant>(IndexConstant)));
1580}
1581
1582LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1583 LLVMValueRef VectorBConstant,
1584 LLVMValueRef MaskConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001585 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001586 unwrap<Constant>(VectorBConstant),
1587 unwrap<Constant>(MaskConstant)));
1588}
1589
Dan Gohmand5104a52008-11-03 22:55:43 +00001590LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1591 unsigned NumIdx) {
Chris Lattner69229312011-02-15 00:14:00 +00001592 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001593 makeArrayRef(IdxList, NumIdx)));
Dan Gohmand5104a52008-11-03 22:55:43 +00001594}
1595
1596LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1597 LLVMValueRef ElementValueConstant,
1598 unsigned *IdxList, unsigned NumIdx) {
Chris Lattner69229312011-02-15 00:14:00 +00001599 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
Dan Gohmand5104a52008-11-03 22:55:43 +00001600 unwrap<Constant>(ElementValueConstant),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001601 makeArrayRef(IdxList, NumIdx)));
Dan Gohmand5104a52008-11-03 22:55:43 +00001602}
1603
Chris Lattner25963c62010-01-09 22:27:07 +00001604LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1605 const char *Constraints,
1606 LLVMBool HasSideEffects,
Benjamin Kramer8dd0edc2012-09-10 11:52:00 +00001607 LLVMBool IsAlignStack) {
Chris Lattner25963c62010-01-09 22:27:07 +00001608 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
Benjamin Kramer8dd0edc2012-09-10 11:52:00 +00001609 Constraints, HasSideEffects, IsAlignStack));
Chris Lattner3d1f5522008-12-17 21:39:50 +00001610}
1611
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00001612LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1613 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1614}
1615
Gordon Henriksen76a03742007-09-18 03:18:57 +00001616/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1617
Gordon Henriksen265f7802008-03-19 01:11:35 +00001618LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1619 return wrap(unwrap<GlobalValue>(Global)->getParent());
1620}
1621
Chris Lattner25963c62010-01-09 22:27:07 +00001622LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001623 return unwrap<GlobalValue>(Global)->isDeclaration();
1624}
1625
1626LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001627 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001628 case GlobalValue::ExternalLinkage:
1629 return LLVMExternalLinkage;
1630 case GlobalValue::AvailableExternallyLinkage:
1631 return LLVMAvailableExternallyLinkage;
1632 case GlobalValue::LinkOnceAnyLinkage:
1633 return LLVMLinkOnceAnyLinkage;
1634 case GlobalValue::LinkOnceODRLinkage:
1635 return LLVMLinkOnceODRLinkage;
1636 case GlobalValue::WeakAnyLinkage:
1637 return LLVMWeakAnyLinkage;
1638 case GlobalValue::WeakODRLinkage:
1639 return LLVMWeakODRLinkage;
1640 case GlobalValue::AppendingLinkage:
1641 return LLVMAppendingLinkage;
1642 case GlobalValue::InternalLinkage:
1643 return LLVMInternalLinkage;
1644 case GlobalValue::PrivateLinkage:
1645 return LLVMPrivateLinkage;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001646 case GlobalValue::ExternalWeakLinkage:
1647 return LLVMExternalWeakLinkage;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001648 case GlobalValue::CommonLinkage:
1649 return LLVMCommonLinkage;
1650 }
1651
David Blaikiea5708dc2012-01-17 07:00:13 +00001652 llvm_unreachable("Invalid GlobalValue linkage!");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001653}
1654
1655void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001656 GlobalValue *GV = unwrap<GlobalValue>(Global);
1657
1658 switch (Linkage) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001659 case LLVMExternalLinkage:
1660 GV->setLinkage(GlobalValue::ExternalLinkage);
1661 break;
1662 case LLVMAvailableExternallyLinkage:
1663 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1664 break;
1665 case LLVMLinkOnceAnyLinkage:
1666 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1667 break;
1668 case LLVMLinkOnceODRLinkage:
1669 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1670 break;
Bill Wendling34bc34e2012-08-17 18:33:14 +00001671 case LLVMLinkOnceODRAutoHideLinkage:
Rafael Espindola716e7402013-11-01 17:09:14 +00001672 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1673 "longer supported.");
Bill Wendling34bc34e2012-08-17 18:33:14 +00001674 break;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001675 case LLVMWeakAnyLinkage:
1676 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1677 break;
1678 case LLVMWeakODRLinkage:
1679 GV->setLinkage(GlobalValue::WeakODRLinkage);
1680 break;
1681 case LLVMAppendingLinkage:
1682 GV->setLinkage(GlobalValue::AppendingLinkage);
1683 break;
1684 case LLVMInternalLinkage:
1685 GV->setLinkage(GlobalValue::InternalLinkage);
1686 break;
1687 case LLVMPrivateLinkage:
1688 GV->setLinkage(GlobalValue::PrivateLinkage);
1689 break;
1690 case LLVMLinkerPrivateLinkage:
Rafael Espindola2fb5bc32014-03-13 23:18:37 +00001691 GV->setLinkage(GlobalValue::PrivateLinkage);
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001692 break;
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001693 case LLVMLinkerPrivateWeakLinkage:
Rafael Espindola2fb5bc32014-03-13 23:18:37 +00001694 GV->setLinkage(GlobalValue::PrivateLinkage);
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001695 break;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001696 case LLVMDLLImportLinkage:
Nico Rieck7157bb72014-01-14 15:22:47 +00001697 DEBUG(errs()
1698 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001699 break;
1700 case LLVMDLLExportLinkage:
Nico Rieck7157bb72014-01-14 15:22:47 +00001701 DEBUG(errs()
1702 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001703 break;
1704 case LLVMExternalWeakLinkage:
1705 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1706 break;
1707 case LLVMGhostLinkage:
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001708 DEBUG(errs()
1709 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001710 break;
1711 case LLVMCommonLinkage:
1712 GV->setLinkage(GlobalValue::CommonLinkage);
1713 break;
1714 }
Gordon Henriksen76a03742007-09-18 03:18:57 +00001715}
1716
1717const char *LLVMGetSection(LLVMValueRef Global) {
Rafael Espindola83658d62016-05-11 18:21:59 +00001718 // Using .data() is safe because of how GlobalObject::setSection is
1719 // implemented.
1720 return unwrap<GlobalValue>(Global)->getSection().data();
Gordon Henriksen76a03742007-09-18 03:18:57 +00001721}
1722
1723void LLVMSetSection(LLVMValueRef Global, const char *Section) {
Rafael Espindola99e05cf2014-05-13 18:45:48 +00001724 unwrap<GlobalObject>(Global)->setSection(Section);
Gordon Henriksen76a03742007-09-18 03:18:57 +00001725}
1726
1727LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1728 return static_cast<LLVMVisibility>(
1729 unwrap<GlobalValue>(Global)->getVisibility());
1730}
1731
1732void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1733 unwrap<GlobalValue>(Global)
1734 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1735}
1736
Reid Kleckner2fae26f2014-03-05 02:34:23 +00001737LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1738 return static_cast<LLVMDLLStorageClass>(
1739 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1740}
1741
1742void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1743 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1744 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1745}
1746
Robert Widmann4bb481b2018-03-14 06:45:51 +00001747LLVMUnnamedAddr LLVMGetUnnamedAddress(LLVMValueRef Global) {
1748 switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
1749 case GlobalVariable::UnnamedAddr::None:
1750 return LLVMNoUnnamedAddr;
1751 case GlobalVariable::UnnamedAddr::Local:
1752 return LLVMLocalUnnamedAddr;
1753 case GlobalVariable::UnnamedAddr::Global:
1754 return LLVMGlobalUnnamedAddr;
1755 }
Simon Pilgrimf0ccaae2018-03-14 12:04:51 +00001756 llvm_unreachable("Unknown UnnamedAddr kind!");
Robert Widmann4bb481b2018-03-14 06:45:51 +00001757}
1758
1759void LLVMSetUnnamedAddress(LLVMValueRef Global, LLVMUnnamedAddr UnnamedAddr) {
1760 GlobalValue *GV = unwrap<GlobalValue>(Global);
1761
1762 switch (UnnamedAddr) {
1763 case LLVMNoUnnamedAddr:
1764 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);
1765 case LLVMLocalUnnamedAddr:
1766 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);
1767 case LLVMGlobalUnnamedAddr:
1768 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);
1769 }
1770}
1771
Tim Northoverad96d012014-03-10 19:24:35 +00001772LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001773 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
Tim Northoverad96d012014-03-10 19:24:35 +00001774}
1775
1776void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001777 unwrap<GlobalValue>(Global)->setUnnamedAddr(
1778 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1779 : GlobalValue::UnnamedAddr::None);
Tim Northoverad96d012014-03-10 19:24:35 +00001780}
1781
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001782/*--.. Operations on global variables, load and store instructions .........--*/
1783
1784unsigned LLVMGetAlignment(LLVMValueRef V) {
1785 Value *P = unwrap<Value>(V);
1786 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1787 return GV->getAlignment();
Peter Zotov9f584e62014-03-05 05:05:34 +00001788 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1789 return AI->getAlignment();
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001790 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1791 return LI->getAlignment();
1792 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1793 return SI->getAlignment();
1794
Peter Zotov9f584e62014-03-05 05:05:34 +00001795 llvm_unreachable(
1796 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001797}
1798
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001799void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1800 Value *P = unwrap<Value>(V);
Rafael Espindola99e05cf2014-05-13 18:45:48 +00001801 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001802 GV->setAlignment(Bytes);
Peter Zotov9f584e62014-03-05 05:05:34 +00001803 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1804 AI->setAlignment(Bytes);
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001805 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1806 LI->setAlignment(Bytes);
1807 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1808 SI->setAlignment(Bytes);
Anders Waldenborga36a7822013-10-29 09:37:28 +00001809 else
Peter Zotov9f584e62014-03-05 05:05:34 +00001810 llvm_unreachable(
1811 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001812}
1813
1814/*--.. Operations on global variables ......................................--*/
1815
1816LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
Owen Andersonb17f3292009-07-08 19:03:57 +00001817 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
Craig Topperc6207612014-04-09 06:08:46 +00001818 GlobalValue::ExternalLinkage, nullptr, Name));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001819}
1820
Erick Tryzelaar06894b32010-02-28 09:46:13 +00001821LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1822 const char *Name,
1823 unsigned AddressSpace) {
1824 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
Craig Topperc6207612014-04-09 06:08:46 +00001825 GlobalValue::ExternalLinkage, nullptr, Name,
1826 nullptr, GlobalVariable::NotThreadLocal,
1827 AddressSpace));
Erick Tryzelaar06894b32010-02-28 09:46:13 +00001828}
1829
Gordon Henriksen783f7bb2007-10-08 03:45:09 +00001830LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1831 return wrap(unwrap(M)->getNamedGlobal(Name));
1832}
1833
Gordon Henriksen054817c2008-03-19 03:47:18 +00001834LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1835 Module *Mod = unwrap(M);
1836 Module::global_iterator I = Mod->global_begin();
1837 if (I == Mod->global_end())
Craig Topperc6207612014-04-09 06:08:46 +00001838 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001839 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001840}
1841
1842LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1843 Module *Mod = unwrap(M);
1844 Module::global_iterator I = Mod->global_end();
1845 if (I == Mod->global_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001846 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001847 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001848}
1849
1850LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1851 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001852 Module::global_iterator I(GV);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001853 if (++I == GV->getParent()->global_end())
Craig Topperc6207612014-04-09 06:08:46 +00001854 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001855 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001856}
1857
1858LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1859 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001860 Module::global_iterator I(GV);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001861 if (I == GV->getParent()->global_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001862 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001863 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001864}
1865
Gordon Henriksen76a03742007-09-18 03:18:57 +00001866void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1867 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1868}
1869
Gordon Henriksen76a03742007-09-18 03:18:57 +00001870LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
Chris Lattner40cf28d2009-10-12 04:01:02 +00001871 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1872 if ( !GV->hasInitializer() )
Craig Topperc6207612014-04-09 06:08:46 +00001873 return nullptr;
Chris Lattner40cf28d2009-10-12 04:01:02 +00001874 return wrap(GV->getInitializer());
Gordon Henriksen76a03742007-09-18 03:18:57 +00001875}
1876
1877void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1878 unwrap<GlobalVariable>(GlobalVar)
1879 ->setInitializer(unwrap<Constant>(ConstantVal));
1880}
1881
Chris Lattner25963c62010-01-09 22:27:07 +00001882LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001883 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1884}
1885
Chris Lattner25963c62010-01-09 22:27:07 +00001886void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001887 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1888}
1889
Chris Lattner25963c62010-01-09 22:27:07 +00001890LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
Gordon Henriksen751ebf72007-10-07 17:31:42 +00001891 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1892}
1893
Chris Lattner25963c62010-01-09 22:27:07 +00001894void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
Gordon Henriksen751ebf72007-10-07 17:31:42 +00001895 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1896}
1897
Hans Wennborg5ff71202013-04-16 08:58:59 +00001898LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1899 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1900 case GlobalVariable::NotThreadLocal:
1901 return LLVMNotThreadLocal;
1902 case GlobalVariable::GeneralDynamicTLSModel:
1903 return LLVMGeneralDynamicTLSModel;
1904 case GlobalVariable::LocalDynamicTLSModel:
1905 return LLVMLocalDynamicTLSModel;
1906 case GlobalVariable::InitialExecTLSModel:
1907 return LLVMInitialExecTLSModel;
1908 case GlobalVariable::LocalExecTLSModel:
1909 return LLVMLocalExecTLSModel;
1910 }
1911
1912 llvm_unreachable("Invalid GlobalVariable thread local mode");
1913}
1914
1915void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1916 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1917
1918 switch (Mode) {
1919 case LLVMNotThreadLocal:
1920 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1921 break;
1922 case LLVMGeneralDynamicTLSModel:
1923 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1924 break;
1925 case LLVMLocalDynamicTLSModel:
1926 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1927 break;
1928 case LLVMInitialExecTLSModel:
1929 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1930 break;
1931 case LLVMLocalExecTLSModel:
1932 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1933 break;
1934 }
1935}
1936
1937LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1938 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1939}
1940
1941void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1942 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1943}
1944
Chris Lattner3d1f5522008-12-17 21:39:50 +00001945/*--.. Operations on aliases ......................................--*/
1946
1947LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1948 const char *Name) {
Rafael Espindola4fe00942014-05-16 13:34:04 +00001949 auto *PTy = cast<PointerType>(unwrap(Ty));
David Blaikie16a2f3e2015-09-14 18:01:59 +00001950 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1951 GlobalValue::ExternalLinkage, Name,
Rafael Espindola993502e2015-02-23 21:51:06 +00001952 unwrap<Constant>(Aliasee), unwrap(M)));
Chris Lattner3d1f5522008-12-17 21:39:50 +00001953}
1954
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001955/*--.. Operations on functions .............................................--*/
1956
1957LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1958 LLVMTypeRef FunctionTy) {
Gabor Greife9ecc682008-04-06 20:25:17 +00001959 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1960 GlobalValue::ExternalLinkage, Name, unwrap(M)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001961}
1962
Gordon Henriksen783f7bb2007-10-08 03:45:09 +00001963LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1964 return wrap(unwrap(M)->getFunction(Name));
1965}
1966
Gordon Henriksen054817c2008-03-19 03:47:18 +00001967LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1968 Module *Mod = unwrap(M);
1969 Module::iterator I = Mod->begin();
1970 if (I == Mod->end())
Craig Topperc6207612014-04-09 06:08:46 +00001971 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001972 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001973}
1974
1975LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1976 Module *Mod = unwrap(M);
1977 Module::iterator I = Mod->end();
1978 if (I == Mod->begin())
Craig Topperc6207612014-04-09 06:08:46 +00001979 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001980 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001981}
1982
1983LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1984 Function *Func = unwrap<Function>(Fn);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001985 Module::iterator I(Func);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001986 if (++I == Func->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00001987 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001988 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001989}
1990
1991LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1992 Function *Func = unwrap<Function>(Fn);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001993 Module::iterator I(Func);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001994 if (I == Func->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00001995 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001996 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001997}
1998
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001999void LLVMDeleteFunction(LLVMValueRef Fn) {
2000 unwrap<Function>(Fn)->eraseFromParent();
2001}
2002
Amaury Sechete39e8532016-02-18 20:38:32 +00002003LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
2004 return unwrap<Function>(Fn)->hasPersonalityFn();
2005}
2006
Andrew Wilkins3bdfc1c2015-07-14 01:23:06 +00002007LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
2008 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2009}
2010
2011void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
2012 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2013}
2014
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002015unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
2016 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2017 return F->getIntrinsicID();
2018 return 0;
2019}
2020
2021unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
2022 return unwrap<Function>(Fn)->getCallingConv();
2023}
2024
2025void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
Sandeep Patel68c5f472009-09-02 08:44:58 +00002026 return unwrap<Function>(Fn)->setCallingConv(
2027 static_cast<CallingConv::ID>(CC));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002028}
2029
Gordon Henriksend930f912008-08-17 18:44:35 +00002030const char *LLVMGetGC(LLVMValueRef Fn) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00002031 Function *F = unwrap<Function>(Fn);
Mehdi Amini599ebf22016-01-08 02:28:20 +00002032 return F->hasGC()? F->getGC().c_str() : nullptr;
Gordon Henriksen71183b62007-12-10 03:18:06 +00002033}
2034
Gordon Henriksend930f912008-08-17 18:44:35 +00002035void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00002036 Function *F = unwrap<Function>(Fn);
Gordon Henriksend930f912008-08-17 18:44:35 +00002037 if (GC)
2038 F->setGC(GC);
Gordon Henriksen71183b62007-12-10 03:18:06 +00002039 else
Gordon Henriksend930f912008-08-17 18:44:35 +00002040 F->clearGC();
Gordon Henriksen71183b62007-12-10 03:18:06 +00002041}
2042
Amaury Sechet5db224e2016-06-12 06:17:24 +00002043void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2044 LLVMAttributeRef A) {
2045 unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
2046}
2047
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002048unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002049 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2050 return AS.getNumAttributes();
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002051}
2052
2053void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2054 LLVMAttributeRef *Attrs) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002055 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2056 for (auto A : AS)
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002057 *Attrs++ = wrap(A);
2058}
2059
Amaury Sechet5db224e2016-06-12 06:17:24 +00002060LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
2061 LLVMAttributeIndex Idx,
2062 unsigned KindID) {
2063 return wrap(unwrap<Function>(F)->getAttribute(Idx,
2064 (Attribute::AttrKind)KindID));
2065}
2066
Amaury Sechet6100adf2016-06-15 17:50:39 +00002067LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
2068 LLVMAttributeIndex Idx,
2069 const char *K, unsigned KLen) {
2070 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
2071}
2072
Amaury Sechet5db224e2016-06-12 06:17:24 +00002073void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2074 unsigned KindID) {
2075 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
2076}
2077
Amaury Sechet6100adf2016-06-15 17:50:39 +00002078void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2079 const char *K, unsigned KLen) {
2080 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
2081}
2082
Tom Stellarde8f35e12013-04-16 23:12:43 +00002083void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
2084 const char *V) {
2085 Function *Func = unwrap<Function>(Fn);
Reid Kleckner9d16fa02017-04-19 17:28:52 +00002086 Attribute Attr = Attribute::get(Func->getContext(), A, V);
2087 Func->addAttribute(AttributeList::FunctionIndex, Attr);
Tom Stellarde8f35e12013-04-16 23:12:43 +00002088}
2089
Gordon Henriksen265f7802008-03-19 01:11:35 +00002090/*--.. Operations on parameters ............................................--*/
2091
2092unsigned LLVMCountParams(LLVMValueRef FnRef) {
2093 // This function is strictly redundant to
2094 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
Dan Gohmanc7076912008-06-21 22:06:54 +00002095 return unwrap<Function>(FnRef)->arg_size();
Gordon Henriksen265f7802008-03-19 01:11:35 +00002096}
2097
2098void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2099 Function *Fn = unwrap<Function>(FnRef);
2100 for (Function::arg_iterator I = Fn->arg_begin(),
2101 E = Fn->arg_end(); I != E; I++)
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002102 *ParamRefs++ = wrap(&*I);
Gordon Henriksen265f7802008-03-19 01:11:35 +00002103}
2104
2105LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
Reid Kleckner56d028d2017-03-17 17:16:39 +00002106 Function *Fn = unwrap<Function>(FnRef);
2107 return wrap(&Fn->arg_begin()[index]);
Gordon Henriksen265f7802008-03-19 01:11:35 +00002108}
2109
2110LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
2111 return wrap(unwrap<Argument>(V)->getParent());
2112}
2113
Gordon Henriksen07a45f42008-03-23 22:21:29 +00002114LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
2115 Function *Func = unwrap<Function>(Fn);
2116 Function::arg_iterator I = Func->arg_begin();
2117 if (I == Func->arg_end())
Craig Topperc6207612014-04-09 06:08:46 +00002118 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002119 return wrap(&*I);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00002120}
2121
2122LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
2123 Function *Func = unwrap<Function>(Fn);
2124 Function::arg_iterator I = Func->arg_end();
2125 if (I == Func->arg_begin())
Craig Topperc6207612014-04-09 06:08:46 +00002126 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002127 return wrap(&*--I);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00002128}
2129
2130LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
2131 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner56d028d2017-03-17 17:16:39 +00002132 Function *Fn = A->getParent();
2133 if (A->getArgNo() + 1 >= Fn->arg_size())
Craig Topperc6207612014-04-09 06:08:46 +00002134 return nullptr;
Reid Kleckner56d028d2017-03-17 17:16:39 +00002135 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00002136}
2137
2138LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
2139 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner56d028d2017-03-17 17:16:39 +00002140 if (A->getArgNo() == 0)
Craig Topperc6207612014-04-09 06:08:46 +00002141 return nullptr;
Reid Kleckner56d028d2017-03-17 17:16:39 +00002142 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00002143}
2144
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002145void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
Bill Wendling49bc76c2013-01-23 06:14:59 +00002146 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner9d16fa02017-04-19 17:28:52 +00002147 A->addAttr(Attribute::getWithAlignment(A->getContext(), align));
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002148}
2149
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002150/*--.. Operations on basic blocks ..........................................--*/
2151
Gordon Henriksen265f7802008-03-19 01:11:35 +00002152LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
2153 return wrap(static_cast<Value*>(unwrap(BB)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002154}
2155
Chris Lattner25963c62010-01-09 22:27:07 +00002156LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002157 return isa<BasicBlock>(unwrap(Val));
2158}
2159
2160LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
2161 return wrap(unwrap<BasicBlock>(Val));
2162}
2163
Amaury Secheta82042e2016-02-09 22:36:41 +00002164const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
2165 return unwrap(BB)->getName().data();
2166}
2167
Gordon Henriksen07a45f42008-03-23 22:21:29 +00002168LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
2169 return wrap(unwrap(BB)->getParent());
Gordon Henriksen265f7802008-03-19 01:11:35 +00002170}
2171
Nate Begeman43c322b2011-08-23 20:27:46 +00002172LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
2173 return wrap(unwrap(BB)->getTerminator());
2174}
2175
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002176unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
Dan Gohmanc7076912008-06-21 22:06:54 +00002177 return unwrap<Function>(FnRef)->size();
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002178}
2179
2180void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2181 Function *Fn = unwrap<Function>(FnRef);
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00002182 for (BasicBlock &BB : *Fn)
2183 *BasicBlocksRefs++ = wrap(&BB);
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002184}
2185
2186LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2187 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2188}
2189
Gordon Henriksen054817c2008-03-19 03:47:18 +00002190LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2191 Function *Func = unwrap<Function>(Fn);
2192 Function::iterator I = Func->begin();
2193 if (I == Func->end())
Craig Topperc6207612014-04-09 06:08:46 +00002194 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002195 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002196}
2197
2198LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2199 Function *Func = unwrap<Function>(Fn);
2200 Function::iterator I = Func->end();
2201 if (I == Func->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002202 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002203 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002204}
2205
2206LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2207 BasicBlock *Block = unwrap(BB);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002208 Function::iterator I(Block);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002209 if (++I == Block->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00002210 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002211 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002212}
2213
2214LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2215 BasicBlock *Block = unwrap(BB);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002216 Function::iterator I(Block);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002217 if (I == Block->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002218 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002219 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002220}
2221
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002222LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2223 LLVMValueRef FnRef,
2224 const char *Name) {
2225 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002226}
2227
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002228LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2229 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2230}
2231
2232LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2233 LLVMBasicBlockRef BBRef,
2234 const char *Name) {
2235 BasicBlock *BB = unwrap(BBRef);
2236 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2237}
2238
2239LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002240 const char *Name) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002241 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002242}
2243
2244void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2245 unwrap(BBRef)->eraseFromParent();
2246}
2247
Nate Begeman43c322b2011-08-23 20:27:46 +00002248void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2249 unwrap(BBRef)->removeFromParent();
2250}
2251
Duncan Sandsb1d61aa2010-07-19 15:31:07 +00002252void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2253 unwrap(BB)->moveBefore(unwrap(MovePos));
2254}
2255
2256void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2257 unwrap(BB)->moveAfter(unwrap(MovePos));
2258}
2259
Gordon Henriksen265f7802008-03-19 01:11:35 +00002260/*--.. Operations on instructions ..........................................--*/
2261
2262LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2263 return wrap(unwrap<Instruction>(Inst)->getParent());
2264}
2265
Gordon Henriksen054817c2008-03-19 03:47:18 +00002266LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2267 BasicBlock *Block = unwrap(BB);
2268 BasicBlock::iterator I = Block->begin();
2269 if (I == Block->end())
Craig Topperc6207612014-04-09 06:08:46 +00002270 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002271 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002272}
2273
2274LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2275 BasicBlock *Block = unwrap(BB);
2276 BasicBlock::iterator I = Block->end();
2277 if (I == Block->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002278 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002279 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002280}
2281
2282LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2283 Instruction *Instr = unwrap<Instruction>(Inst);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002284 BasicBlock::iterator I(Instr);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002285 if (++I == Instr->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00002286 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002287 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002288}
2289
2290LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2291 Instruction *Instr = unwrap<Instruction>(Inst);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002292 BasicBlock::iterator I(Instr);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002293 if (I == Instr->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002294 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002295 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002296}
2297
Amaury Sechet2f432082016-02-11 21:37:54 +00002298void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2299 unwrap<Instruction>(Inst)->removeFromParent();
2300}
2301
Devang Pateldbebc6f2011-10-03 20:59:18 +00002302void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2303 unwrap<Instruction>(Inst)->eraseFromParent();
2304}
2305
Torok Edwin60c40de2011-10-06 12:13:20 +00002306LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
Torok Edwin2e9affe2011-10-14 20:37:42 +00002307 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2308 return (LLVMIntPredicate)I->getPredicate();
2309 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2310 if (CE->getOpcode() == Instruction::ICmp)
2311 return (LLVMIntPredicate)CE->getPredicate();
2312 return (LLVMIntPredicate)0;
Torok Edwin60c40de2011-10-06 12:13:20 +00002313}
2314
Peter Zotov1d98e6d2014-10-28 19:46:44 +00002315LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2316 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2317 return (LLVMRealPredicate)I->getPredicate();
2318 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2319 if (CE->getOpcode() == Instruction::FCmp)
2320 return (LLVMRealPredicate)CE->getPredicate();
2321 return (LLVMRealPredicate)0;
2322}
2323
Torok Edwinab6158e2011-10-14 20:37:49 +00002324LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2325 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2326 return map_to_llvmopcode(C->getOpcode());
2327 return (LLVMOpcode)0;
2328}
2329
Peter Zotovaff492c2014-10-17 01:02:34 +00002330LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2331 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2332 return wrap(C->clone());
2333 return nullptr;
2334}
2335
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002336unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
Robert Widmann478fce92018-03-30 17:49:53 +00002337 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2338 return FPI->getNumArgOperands();
2339 }
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002340 return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands();
2341}
2342
Robert Widmann478fce92018-03-30 17:49:53 +00002343/*--.. Call and invoke instructions ........................................--*/
2344
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002345unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002346 return CallSite(unwrap<Instruction>(Instr)).getCallingConv();
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002347}
2348
2349void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002350 return CallSite(unwrap<Instruction>(Instr))
2351 .setCallingConv(static_cast<CallingConv::ID>(CC));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002352}
2353
Andrew Trickdc073ad2013-09-18 23:31:10 +00002354void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002355 unsigned align) {
2356 CallSite Call = CallSite(unwrap<Instruction>(Instr));
Reid Kleckner9d16fa02017-04-19 17:28:52 +00002357 Attribute AlignAttr = Attribute::getWithAlignment(Call->getContext(), align);
2358 Call.addAttribute(index, AlignAttr);
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002359}
2360
Amaury Secheta65a2372016-06-15 05:14:29 +00002361void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2362 LLVMAttributeRef A) {
2363 CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A));
2364}
2365
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002366unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2367 LLVMAttributeIndex Idx) {
2368 auto CS = CallSite(unwrap<Instruction>(C));
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002369 auto AS = CS.getAttributes().getAttributes(Idx);
2370 return AS.getNumAttributes();
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002371}
2372
2373void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2374 LLVMAttributeRef *Attrs) {
2375 auto CS = CallSite(unwrap<Instruction>(C));
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002376 auto AS = CS.getAttributes().getAttributes(Idx);
2377 for (auto A : AS)
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002378 *Attrs++ = wrap(A);
2379}
2380
Amaury Secheta65a2372016-06-15 05:14:29 +00002381LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
2382 LLVMAttributeIndex Idx,
2383 unsigned KindID) {
2384 return wrap(CallSite(unwrap<Instruction>(C))
2385 .getAttribute(Idx, (Attribute::AttrKind)KindID));
2386}
2387
Amaury Sechet6100adf2016-06-15 17:50:39 +00002388LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
2389 LLVMAttributeIndex Idx,
2390 const char *K, unsigned KLen) {
2391 return wrap(CallSite(unwrap<Instruction>(C))
2392 .getAttribute(Idx, StringRef(K, KLen)));
2393}
2394
Amaury Secheta65a2372016-06-15 05:14:29 +00002395void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2396 unsigned KindID) {
2397 CallSite(unwrap<Instruction>(C))
2398 .removeAttribute(Idx, (Attribute::AttrKind)KindID);
2399}
2400
Amaury Sechet6100adf2016-06-15 17:50:39 +00002401void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2402 const char *K, unsigned KLen) {
2403 CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen));
2404}
2405
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002406LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2407 return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue());
2408}
2409
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002410/*--.. Operations on call instructions (only) ..............................--*/
2411
Chris Lattner25963c62010-01-09 22:27:07 +00002412LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002413 return unwrap<CallInst>(Call)->isTailCall();
2414}
2415
Chris Lattner25963c62010-01-09 22:27:07 +00002416void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002417 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2418}
2419
Amaury Sechete39e8532016-02-18 20:38:32 +00002420/*--.. Operations on invoke instructions (only) ............................--*/
2421
2422LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2423 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2424}
2425
2426LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
Robert Widmann478fce92018-03-30 17:49:53 +00002427 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2428 return wrap(CRI->getUnwindDest());
2429 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2430 return wrap(CSI->getUnwindDest());
2431 }
Amaury Sechete39e8532016-02-18 20:38:32 +00002432 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2433}
2434
2435void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2436 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2437}
2438
2439void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
Robert Widmann478fce92018-03-30 17:49:53 +00002440 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2441 return CRI->setUnwindDest(unwrap(B));
2442 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2443 return CSI->setUnwindDest(unwrap(B));
2444 }
Amaury Sechete39e8532016-02-18 20:38:32 +00002445 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2446}
2447
Peter Zotov2481c752014-10-28 19:46:56 +00002448/*--.. Operations on terminators ...........................................--*/
2449
2450unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2451 return unwrap<TerminatorInst>(Term)->getNumSuccessors();
2452}
2453
2454LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2455 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
2456}
2457
2458void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2459 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
2460}
2461
2462/*--.. Operations on branch instructions (only) ............................--*/
2463
2464LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2465 return unwrap<BranchInst>(Branch)->isConditional();
2466}
2467
2468LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2469 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2470}
2471
2472void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2473 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2474}
2475
Nate Begeman43c322b2011-08-23 20:27:46 +00002476/*--.. Operations on switch instructions (only) ............................--*/
2477
2478LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2479 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2480}
2481
Amaury Sechet1dcf5772016-02-09 22:50:53 +00002482/*--.. Operations on alloca instructions (only) ............................--*/
2483
2484LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
2485 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2486}
2487
Amaury Sechet053ac452016-02-17 22:51:03 +00002488/*--.. Operations on gep instructions (only) ...............................--*/
2489
2490LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
2491 return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2492}
2493
Amaury Sechet8a367d42016-05-01 02:23:14 +00002494void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
2495 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
Amaury Sechet053ac452016-02-17 22:51:03 +00002496}
2497
Gordon Henriksen44dd8fb2007-10-08 18:14:39 +00002498/*--.. Operations on phi nodes .............................................--*/
2499
2500void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2501 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2502 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2503 for (unsigned I = 0; I != Count; ++I)
2504 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2505}
2506
2507unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2508 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2509}
2510
2511LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2512 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2513}
2514
2515LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2516 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2517}
2518
Amaury Sechetaad93532016-02-10 00:38:50 +00002519/*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2520
2521unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
2522 auto *I = unwrap(Inst);
Amaury Sechet053ac452016-02-17 22:51:03 +00002523 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
2524 return GEP->getNumIndices();
Amaury Sechetaad93532016-02-10 00:38:50 +00002525 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2526 return EV->getNumIndices();
2527 if (auto *IV = dyn_cast<InsertValueInst>(I))
2528 return IV->getNumIndices();
2529 llvm_unreachable(
2530 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2531}
2532
2533const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2534 auto *I = unwrap(Inst);
2535 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2536 return EV->getIndices().data();
2537 if (auto *IV = dyn_cast<InsertValueInst>(I))
2538 return IV->getIndices().data();
2539 llvm_unreachable(
2540 "LLVMGetIndices applies only to extractvalue and insertvalue!");
2541}
2542
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002543
2544/*===-- Instruction builders ----------------------------------------------===*/
2545
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002546LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2547 return wrap(new IRBuilder<>(*unwrap(C)));
2548}
2549
Gordon Henriksena735a9c2008-05-04 12:55:34 +00002550LLVMBuilderRef LLVMCreateBuilder(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002551 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002552}
2553
Gordon Henriksen054817c2008-03-19 03:47:18 +00002554void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2555 LLVMValueRef Instr) {
2556 BasicBlock *BB = unwrap(Block);
Duncan P. N. Exon Smith43724642016-08-11 15:45:04 +00002557 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
2558 unwrap(Builder)->SetInsertPoint(BB, I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002559}
2560
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002561void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2562 Instruction *I = unwrap<Instruction>(Instr);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002563 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002564}
2565
2566void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2567 BasicBlock *BB = unwrap(Block);
2568 unwrap(Builder)->SetInsertPoint(BB);
2569}
2570
Gordon Henriksen265f7802008-03-19 01:11:35 +00002571LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2572 return wrap(unwrap(Builder)->GetInsertBlock());
2573}
2574
Chris Lattner3d1f5522008-12-17 21:39:50 +00002575void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
Chris Lattnere4bcde82010-04-01 06:31:45 +00002576 unwrap(Builder)->ClearInsertionPoint();
Chris Lattner3d1f5522008-12-17 21:39:50 +00002577}
2578
2579void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2580 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2581}
2582
Erick Tryzelaar9813bea2009-08-16 02:20:57 +00002583void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2584 const char *Name) {
2585 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2586}
2587
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002588void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2589 delete unwrap(Builder);
2590}
2591
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002592/*--.. Metadata builders ...................................................--*/
2593
2594void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002595 MDNode *Loc =
2596 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002597 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002598}
2599
2600LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002601 LLVMContext &Context = unwrap(Builder)->getContext();
2602 return wrap(MetadataAsValue::get(
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002603 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002604}
2605
2606void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2607 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2608}
2609
2610
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002611/*--.. Instruction builders ................................................--*/
2612
2613LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2614 return wrap(unwrap(B)->CreateRetVoid());
2615}
2616
2617LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2618 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2619}
2620
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002621LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2622 unsigned N) {
2623 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2624}
2625
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002626LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2627 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2628}
2629
2630LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2631 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2632 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2633}
2634
2635LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2636 LLVMBasicBlockRef Else, unsigned NumCases) {
2637 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2638}
2639
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00002640LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2641 unsigned NumDests) {
2642 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2643}
2644
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002645LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2646 LLVMValueRef *Args, unsigned NumArgs,
2647 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2648 const char *Name) {
2649 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002650 makeArrayRef(unwrap(Args), NumArgs),
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002651 Name));
2652}
2653
Bill Wendlingfae14752011-08-12 20:24:12 +00002654LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
Reid Kleckneref9828f2015-07-16 01:16:39 +00002655 LLVMValueRef PersFn, unsigned NumClauses,
2656 const char *Name) {
2657 // The personality used to live on the landingpad instruction, but now it
2658 // lives on the parent function. For compatibility, take the provided
2659 // personality and put it on the parent function.
2660 if (PersFn)
2661 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
2662 cast<Function>(unwrap(PersFn)));
David Majnemer7fddecc2015-06-17 20:52:32 +00002663 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
Bill Wendlingfae14752011-08-12 20:24:12 +00002664}
2665
Robert Widmann478fce92018-03-30 17:49:53 +00002666LLVMValueRef LLVMBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
2667 LLVMValueRef *Args, unsigned NumArgs,
2668 const char *Name) {
2669 return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
2670 makeArrayRef(unwrap(Args), NumArgs),
2671 Name));
2672}
2673
2674LLVMValueRef LLVMBuildCleanupPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
2675 LLVMValueRef *Args, unsigned NumArgs,
2676 const char *Name) {
2677 if (ParentPad == nullptr) {
2678 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
2679 ParentPad = wrap(Constant::getNullValue(Ty));
2680 }
2681 return wrap(unwrap(B)->CreateCleanupPad(unwrap(ParentPad),
2682 makeArrayRef(unwrap(Args), NumArgs),
2683 Name));
2684}
2685
Bill Wendlingf891bf82011-07-31 06:30:59 +00002686LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2687 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2688}
2689
Robert Widmann478fce92018-03-30 17:49:53 +00002690LLVMValueRef LLVMBuildCatchSwitch(LLVMBuilderRef B, LLVMValueRef ParentPad,
2691 LLVMBasicBlockRef UnwindBB,
2692 unsigned NumHandlers, const char *Name) {
2693 if (ParentPad == nullptr) {
2694 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
2695 ParentPad = wrap(Constant::getNullValue(Ty));
2696 }
2697 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
2698 NumHandlers, Name));
2699}
2700
2701LLVMValueRef LLVMBuildCatchRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
2702 LLVMBasicBlockRef BB) {
2703 return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
2704 unwrap(BB)));
2705}
2706
2707LLVMValueRef LLVMBuildCleanupRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
2708 LLVMBasicBlockRef BB) {
2709 return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
2710 unwrap(BB)));
2711}
2712
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002713LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2714 return wrap(unwrap(B)->CreateUnreachable());
2715}
2716
Gordon Henriksen097102c2008-01-01 05:50:53 +00002717void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2718 LLVMBasicBlockRef Dest) {
2719 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2720}
2721
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00002722void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2723 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2724}
2725
Amaury Sechete39e8532016-02-18 20:38:32 +00002726unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
2727 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
2728}
2729
2730LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
2731 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
2732}
2733
Bill Wendlingfae14752011-08-12 20:24:12 +00002734void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2735 unwrap<LandingPadInst>(LandingPad)->
2736 addClause(cast<Constant>(unwrap(ClauseVal)));
2737}
2738
Amaury Sechete39e8532016-02-18 20:38:32 +00002739LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
2740 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
2741}
2742
Bill Wendlingfae14752011-08-12 20:24:12 +00002743void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2744 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2745}
2746
Robert Widmann478fce92018-03-30 17:49:53 +00002747void LLVMAddHandler(LLVMValueRef CatchSwitch, LLVMBasicBlockRef Dest) {
2748 unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
2749}
2750
2751unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
2752 return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
2753}
2754
2755void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
2756 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
2757 for (CatchSwitchInst::handler_iterator I = CSI->handler_begin(),
2758 E = CSI->handler_end(); I != E; ++I)
2759 *Handlers++ = wrap(*I);
2760}
2761
2762LLVMValueRef LLVMGetParentCatchSwitch(LLVMValueRef CatchPad) {
2763 return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
2764}
2765
2766void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad, LLVMValueRef CatchSwitch) {
2767 unwrap<CatchPadInst>(CatchPad)
2768 ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
2769}
2770
2771/*--.. Funclets ...........................................................--*/
2772
2773LLVMValueRef LLVMGetArgOperand(LLVMValueRef Funclet, unsigned i) {
2774 return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
2775}
2776
2777void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
2778 unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
2779}
2780
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002781/*--.. Arithmetic ..........................................................--*/
2782
2783LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2784 const char *Name) {
2785 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2786}
2787
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002788LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2789 const char *Name) {
2790 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2791}
2792
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002793LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2794 const char *Name) {
2795 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2796}
2797
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002798LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2799 const char *Name) {
2800 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2801}
2802
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002803LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2804 const char *Name) {
2805 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2806}
2807
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002808LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2809 const char *Name) {
2810 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2811}
2812
2813LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2814 const char *Name) {
2815 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2816}
2817
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002818LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2819 const char *Name) {
2820 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2821}
2822
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002823LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2824 const char *Name) {
2825 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2826}
2827
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002828LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2829 const char *Name) {
2830 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2831}
2832
2833LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2834 const char *Name) {
2835 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2836}
2837
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002838LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2839 const char *Name) {
2840 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2841}
2842
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002843LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2844 const char *Name) {
2845 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2846}
2847
Manuel Jacob49fafb12016-10-04 23:32:42 +00002848LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2849 LLVMValueRef RHS, const char *Name) {
2850 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
2851}
2852
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002853LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2854 const char *Name) {
2855 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2856}
2857
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002858LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2859 LLVMValueRef RHS, const char *Name) {
2860 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2861}
2862
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002863LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2864 const char *Name) {
2865 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2866}
2867
2868LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2869 const char *Name) {
2870 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2871}
2872
2873LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2874 const char *Name) {
2875 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2876}
2877
2878LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2879 const char *Name) {
2880 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2881}
2882
2883LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2884 const char *Name) {
2885 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2886}
2887
2888LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2889 const char *Name) {
2890 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2891}
2892
2893LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2894 const char *Name) {
2895 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2896}
2897
2898LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2899 const char *Name) {
2900 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2901}
2902
2903LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2904 const char *Name) {
2905 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2906}
2907
2908LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2909 const char *Name) {
2910 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2911}
2912
Erick Tryzelaar31831792010-02-28 05:51:27 +00002913LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2914 LLVMValueRef LHS, LLVMValueRef RHS,
2915 const char *Name) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00002916 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
Erick Tryzelaar31831792010-02-28 05:51:27 +00002917 unwrap(RHS), Name));
2918}
2919
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002920LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2921 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2922}
2923
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002924LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2925 const char *Name) {
2926 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2927}
2928
2929LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2930 const char *Name) {
2931 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2932}
2933
Dan Gohmanf919bd62009-09-28 21:51:41 +00002934LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2935 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2936}
2937
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002938LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2939 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2940}
2941
2942/*--.. Memory ..............................................................--*/
2943
2944LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2945 const char *Name) {
Chris Lattner229907c2011-07-18 04:54:35 +00002946 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
Victor Hernandezf3db9152009-11-07 00:16:28 +00002947 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2948 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
Andrew Trickdc073ad2013-09-18 23:31:10 +00002949 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2950 ITy, unwrap(Ty), AllocSize,
Craig Topperc6207612014-04-09 06:08:46 +00002951 nullptr, nullptr, "");
Victor Hernandezf3db9152009-11-07 00:16:28 +00002952 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002953}
2954
2955LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2956 LLVMValueRef Val, const char *Name) {
Chris Lattner229907c2011-07-18 04:54:35 +00002957 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
Victor Hernandezf3db9152009-11-07 00:16:28 +00002958 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2959 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
Andrew Trickdc073ad2013-09-18 23:31:10 +00002960 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2961 ITy, unwrap(Ty), AllocSize,
Craig Topperc6207612014-04-09 06:08:46 +00002962 unwrap(Val), nullptr, "");
Victor Hernandezf3db9152009-11-07 00:16:28 +00002963 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002964}
2965
2966LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2967 const char *Name) {
Craig Topperc6207612014-04-09 06:08:46 +00002968 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002969}
2970
2971LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2972 LLVMValueRef Val, const char *Name) {
2973 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2974}
2975
2976LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
Victor Hernandezde5ad422009-10-26 23:43:48 +00002977 return wrap(unwrap(B)->Insert(
2978 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002979}
2980
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002981LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2982 const char *Name) {
2983 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2984}
2985
Andrew Trickdc073ad2013-09-18 23:31:10 +00002986LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002987 LLVMValueRef PointerVal) {
2988 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2989}
2990
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002991static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2992 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +00002993 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
2994 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
2995 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
2996 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
2997 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
2998 case LLVMAtomicOrderingAcquireRelease:
2999 return AtomicOrdering::AcquireRelease;
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00003000 case LLVMAtomicOrderingSequentiallyConsistent:
JF Bastien800f87a2016-04-06 21:19:33 +00003001 return AtomicOrdering::SequentiallyConsistent;
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00003002 }
Peter Zotov1d98e6d2014-10-28 19:46:44 +00003003
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00003004 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3005}
3006
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00003007static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
3008 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +00003009 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
3010 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
3011 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
3012 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
3013 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
3014 case AtomicOrdering::AcquireRelease:
3015 return LLVMAtomicOrderingAcquireRelease;
3016 case AtomicOrdering::SequentiallyConsistent:
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00003017 return LLVMAtomicOrderingSequentiallyConsistent;
3018 }
3019
3020 llvm_unreachable("Invalid AtomicOrdering value!");
3021}
3022
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003023// TODO: Should this and other atomic instructions support building with
3024// "syncscope"?
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00003025LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
3026 LLVMBool isSingleThread, const char *Name) {
3027 return wrap(
3028 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003029 isSingleThread ? SyncScope::SingleThread
3030 : SyncScope::System,
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00003031 Name));
3032}
3033
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00003034LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
3035 LLVMValueRef *Indices, unsigned NumIndices,
3036 const char *Name) {
Jay Foad040dd822011-07-22 08:16:57 +00003037 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
David Blaikie86ecb1b2015-03-15 01:03:19 +00003038 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00003039}
3040
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00003041LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
3042 LLVMValueRef *Indices, unsigned NumIndices,
3043 const char *Name) {
Jay Foad040dd822011-07-22 08:16:57 +00003044 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
David Blaikieaa41cd52015-04-03 21:33:42 +00003045 return wrap(
3046 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00003047}
3048
3049LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
3050 unsigned Idx, const char *Name) {
David Blaikie64646022015-04-05 22:41:44 +00003051 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00003052}
3053
3054LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
3055 const char *Name) {
3056 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3057}
3058
3059LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
3060 const char *Name) {
3061 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
3062}
3063
Chris Lattner2cc6f9d2012-03-22 03:54:15 +00003064LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
3065 Value *P = unwrap<Value>(MemAccessInst);
3066 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3067 return LI->isVolatile();
3068 return cast<StoreInst>(P)->isVolatile();
3069}
3070
3071void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
3072 Value *P = unwrap<Value>(MemAccessInst);
3073 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3074 return LI->setVolatile(isVolatile);
3075 return cast<StoreInst>(P)->setVolatile(isVolatile);
3076}
3077
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00003078LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
3079 Value *P = unwrap<Value>(MemAccessInst);
3080 AtomicOrdering O;
3081 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3082 O = LI->getOrdering();
3083 else
3084 O = cast<StoreInst>(P)->getOrdering();
3085 return mapToLLVMOrdering(O);
3086}
3087
3088void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
3089 Value *P = unwrap<Value>(MemAccessInst);
3090 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3091
3092 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3093 return LI->setOrdering(O);
3094 return cast<StoreInst>(P)->setOrdering(O);
3095}
3096
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00003097/*--.. Casts ...............................................................--*/
3098
3099LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
3100 LLVMTypeRef DestTy, const char *Name) {
3101 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
3102}
3103
3104LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
3105 LLVMTypeRef DestTy, const char *Name) {
3106 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
3107}
3108
3109LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
3110 LLVMTypeRef DestTy, const char *Name) {
3111 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
3112}
3113
3114LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
3115 LLVMTypeRef DestTy, const char *Name) {
3116 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
3117}
3118
3119LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
3120 LLVMTypeRef DestTy, const char *Name) {
3121 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
3122}
3123
3124LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
3125 LLVMTypeRef DestTy, const char *Name) {
3126 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
3127}
3128
3129LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
3130 LLVMTypeRef DestTy, const char *Name) {
3131 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
3132}
3133
3134LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
3135 LLVMTypeRef DestTy, const char *Name) {
3136 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
3137}
3138
3139LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
3140 LLVMTypeRef DestTy, const char *Name) {
3141 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
3142}
3143
3144LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
3145 LLVMTypeRef DestTy, const char *Name) {
3146 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
3147}
3148
3149LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
3150 LLVMTypeRef DestTy, const char *Name) {
3151 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
3152}
3153
3154LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3155 LLVMTypeRef DestTy, const char *Name) {
3156 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
3157}
3158
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003159LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
3160 LLVMTypeRef DestTy, const char *Name) {
3161 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
3162}
3163
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00003164LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3165 LLVMTypeRef DestTy, const char *Name) {
3166 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
3167 Name));
3168}
3169
3170LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3171 LLVMTypeRef DestTy, const char *Name) {
3172 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
3173 Name));
3174}
3175
3176LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3177 LLVMTypeRef DestTy, const char *Name) {
3178 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
3179 Name));
3180}
3181
Erick Tryzelaar31831792010-02-28 05:51:27 +00003182LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
3183 LLVMTypeRef DestTy, const char *Name) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00003184 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
Erick Tryzelaar31831792010-02-28 05:51:27 +00003185 unwrap(DestTy), Name));
3186}
3187
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00003188LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
3189 LLVMTypeRef DestTy, const char *Name) {
3190 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
3191}
3192
3193LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
Duncan Sands9d786d72009-11-23 10:49:03 +00003194 LLVMTypeRef DestTy, const char *Name) {
3195 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
3196 /*isSigned*/true, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00003197}
3198
3199LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
3200 LLVMTypeRef DestTy, const char *Name) {
3201 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
3202}
3203
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00003204/*--.. Comparisons .........................................................--*/
3205
3206LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
3207 LLVMValueRef LHS, LLVMValueRef RHS,
3208 const char *Name) {
3209 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
3210 unwrap(LHS), unwrap(RHS), Name));
3211}
3212
3213LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
3214 LLVMValueRef LHS, LLVMValueRef RHS,
3215 const char *Name) {
3216 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
3217 unwrap(LHS), unwrap(RHS), Name));
3218}
3219
3220/*--.. Miscellaneous instructions ..........................................--*/
3221
3222LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
Jay Foad52131342011-03-30 11:28:46 +00003223 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00003224}
3225
3226LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
3227 LLVMValueRef *Args, unsigned NumArgs,
3228 const char *Name) {
Jay Foad5bd375a2011-07-15 08:37:34 +00003229 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00003230 makeArrayRef(unwrap(Args), NumArgs),
Jay Foad5bd375a2011-07-15 08:37:34 +00003231 Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00003232}
3233
3234LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
3235 LLVMValueRef Then, LLVMValueRef Else,
3236 const char *Name) {
3237 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
3238 Name));
3239}
3240
3241LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
3242 LLVMTypeRef Ty, const char *Name) {
3243 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
3244}
3245
3246LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
3247 LLVMValueRef Index, const char *Name) {
3248 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
3249 Name));
3250}
3251
3252LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
3253 LLVMValueRef EltVal, LLVMValueRef Index,
3254 const char *Name) {
3255 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
3256 unwrap(Index), Name));
3257}
3258
3259LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
3260 LLVMValueRef V2, LLVMValueRef Mask,
3261 const char *Name) {
3262 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
3263 unwrap(Mask), Name));
3264}
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003265
Dan Gohmand5104a52008-11-03 22:55:43 +00003266LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3267 unsigned Index, const char *Name) {
3268 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
3269}
3270
3271LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3272 LLVMValueRef EltVal, unsigned Index,
3273 const char *Name) {
3274 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3275 Index, Name));
3276}
3277
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00003278LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
3279 const char *Name) {
3280 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
3281}
3282
3283LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
3284 const char *Name) {
3285 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3286}
3287
3288LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
3289 LLVMValueRef RHS, const char *Name) {
3290 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
3291}
3292
Andrew Trickdc073ad2013-09-18 23:31:10 +00003293LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
3294 LLVMValueRef PTR, LLVMValueRef Val,
3295 LLVMAtomicOrdering ordering,
Carlo Kok8c6719b2013-04-23 13:21:19 +00003296 LLVMBool singleThread) {
3297 AtomicRMWInst::BinOp intop;
3298 switch (op) {
3299 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
3300 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
3301 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
3302 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
3303 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
3304 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
3305 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
3306 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
3307 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
3308 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
3309 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
3310 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00003311 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003312 mapFromLLVMOrdering(ordering), singleThread ? SyncScope::SingleThread
3313 : SyncScope::System));
Carlo Kok8c6719b2013-04-23 13:21:19 +00003314}
3315
Mehdi Amini43165d92016-03-19 21:28:28 +00003316LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
3317 LLVMValueRef Cmp, LLVMValueRef New,
3318 LLVMAtomicOrdering SuccessOrdering,
3319 LLVMAtomicOrdering FailureOrdering,
3320 LLVMBool singleThread) {
3321
3322 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
3323 unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
3324 mapFromLLVMOrdering(FailureOrdering),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003325 singleThread ? SyncScope::SingleThread : SyncScope::System));
Mehdi Amini43165d92016-03-19 21:28:28 +00003326}
3327
3328
3329LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
3330 Value *P = unwrap<Value>(AtomicInst);
3331
3332 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003333 return I->getSyncScopeID() == SyncScope::SingleThread;
3334 return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() ==
3335 SyncScope::SingleThread;
Mehdi Amini43165d92016-03-19 21:28:28 +00003336}
3337
3338void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
3339 Value *P = unwrap<Value>(AtomicInst);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003340 SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
Mehdi Amini43165d92016-03-19 21:28:28 +00003341
3342 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003343 return I->setSyncScopeID(SSID);
3344 return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID);
Mehdi Amini43165d92016-03-19 21:28:28 +00003345}
3346
3347LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
3348 Value *P = unwrap<Value>(CmpXchgInst);
3349 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
3350}
3351
3352void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
3353 LLVMAtomicOrdering Ordering) {
3354 Value *P = unwrap<Value>(CmpXchgInst);
3355 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3356
3357 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
3358}
3359
3360LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
3361 Value *P = unwrap<Value>(CmpXchgInst);
3362 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
3363}
3364
3365void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
3366 LLVMAtomicOrdering Ordering) {
3367 Value *P = unwrap<Value>(CmpXchgInst);
3368 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3369
3370 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
3371}
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003372
3373/*===-- Module providers --------------------------------------------------===*/
3374
3375LLVMModuleProviderRef
3376LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003377 return reinterpret_cast<LLVMModuleProviderRef>(M);
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003378}
3379
3380void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
3381 delete unwrap(MP);
3382}
3383
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003384
3385/*===-- Memory buffers ----------------------------------------------------===*/
3386
Chris Lattner25963c62010-01-09 22:27:07 +00003387LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
3388 const char *Path,
3389 LLVMMemoryBufferRef *OutMemBuf,
3390 char **OutMessage) {
3391
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003392 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
3393 if (std::error_code EC = MBOrErr.getError()) {
3394 *OutMessage = strdup(EC.message().c_str());
3395 return 1;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003396 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003397 *OutMemBuf = wrap(MBOrErr.get().release());
3398 return 0;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003399}
3400
Chris Lattner25963c62010-01-09 22:27:07 +00003401LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
3402 char **OutMessage) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003403 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
3404 if (std::error_code EC = MBOrErr.getError()) {
3405 *OutMessage = strdup(EC.message().c_str());
3406 return 1;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003407 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003408 *OutMemBuf = wrap(MBOrErr.get().release());
3409 return 0;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003410}
3411
Bill Wendling526276a2013-02-14 19:11:28 +00003412LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
3413 const char *InputData,
3414 size_t InputDataLength,
3415 const char *BufferName,
Bill Wendlingff61da92013-02-14 19:40:27 +00003416 LLVMBool RequiresNullTerminator) {
Bill Wendling526276a2013-02-14 19:11:28 +00003417
Rafael Espindola3560ff22014-08-27 20:03:13 +00003418 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
3419 StringRef(BufferName),
3420 RequiresNullTerminator).release());
Bill Wendling526276a2013-02-14 19:11:28 +00003421}
3422
3423LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
3424 const char *InputData,
3425 size_t InputDataLength,
3426 const char *BufferName) {
3427
Rafael Espindola3560ff22014-08-27 20:03:13 +00003428 return wrap(
3429 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
3430 StringRef(BufferName)).release());
Bill Wendling526276a2013-02-14 19:11:28 +00003431}
3432
Tom Stellard62c03202013-04-18 19:50:53 +00003433const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
Tom Stellard385fa262013-04-16 23:12:47 +00003434 return unwrap(MemBuf)->getBufferStart();
3435}
Bill Wendling526276a2013-02-14 19:11:28 +00003436
Tom Stellardb7fb7242013-04-16 23:12:51 +00003437size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
3438 return unwrap(MemBuf)->getBufferSize();
3439}
3440
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003441void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
3442 delete unwrap(MemBuf);
3443}
Dan Gohman093b42f2010-08-07 00:43:20 +00003444
Owen Anderson4698c5d2010-10-07 17:55:47 +00003445/*===-- Pass Registry -----------------------------------------------------===*/
3446
3447LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
3448 return wrap(PassRegistry::getPassRegistry());
3449}
Dan Gohman093b42f2010-08-07 00:43:20 +00003450
3451/*===-- Pass Manager ------------------------------------------------------===*/
3452
3453LLVMPassManagerRef LLVMCreatePassManager() {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003454 return wrap(new legacy::PassManager());
Dan Gohman093b42f2010-08-07 00:43:20 +00003455}
3456
3457LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003458 return wrap(new legacy::FunctionPassManager(unwrap(M)));
Dan Gohman093b42f2010-08-07 00:43:20 +00003459}
3460
3461LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
3462 return LLVMCreateFunctionPassManagerForModule(
3463 reinterpret_cast<LLVMModuleRef>(P));
3464}
3465
3466LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003467 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
Dan Gohman093b42f2010-08-07 00:43:20 +00003468}
3469
3470LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003471 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
Dan Gohman093b42f2010-08-07 00:43:20 +00003472}
3473
3474LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003475 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
Dan Gohman093b42f2010-08-07 00:43:20 +00003476}
3477
3478LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003479 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
Dan Gohman093b42f2010-08-07 00:43:20 +00003480}
3481
3482void LLVMDisposePassManager(LLVMPassManagerRef PM) {
3483 delete unwrap(PM);
3484}
Duncan Sands1cba0a82013-02-17 16:35:51 +00003485
3486/*===-- Threading ------------------------------------------------------===*/
3487
3488LLVMBool LLVMStartMultithreaded() {
Chandler Carruth39cd2162014-06-27 15:13:01 +00003489 return LLVMIsMultithreaded();
Duncan Sands1cba0a82013-02-17 16:35:51 +00003490}
3491
3492void LLVMStopMultithreaded() {
Duncan Sands1cba0a82013-02-17 16:35:51 +00003493}
3494
3495LLVMBool LLVMIsMultithreaded() {
3496 return llvm_is_multithreaded();
3497}