1//===-- Core.cpp ----------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the common infrastructure (including the C bindings)
10// for libLLVMCore.a, which implements the LLVM intermediate representation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm-c/Core.h"
15#include "llvm-c/Types.h"
16#include "llvm/IR/Attributes.h"
17#include "llvm/IR/BasicBlock.h"
18#include "llvm/IR/ConstantRange.h"
19#include "llvm/IR/Constants.h"
20#include "llvm/IR/DebugInfoMetadata.h"
21#include "llvm/IR/DebugProgramInstruction.h"
22#include "llvm/IR/DerivedTypes.h"
23#include "llvm/IR/DiagnosticInfo.h"
24#include "llvm/IR/DiagnosticPrinter.h"
25#include "llvm/IR/GlobalAlias.h"
26#include "llvm/IR/GlobalVariable.h"
27#include "llvm/IR/IRBuilder.h"
28#include "llvm/IR/InlineAsm.h"
29#include "llvm/IR/Instructions.h"
30#include "llvm/IR/IntrinsicInst.h"
31#include "llvm/IR/LLVMContext.h"
32#include "llvm/IR/LegacyPassManager.h"
33#include "llvm/IR/Module.h"
34#include "llvm/InitializePasses.h"
35#include "llvm/PassRegistry.h"
36#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/FileSystem.h"
39#include "llvm/Support/ManagedStatic.h"
40#include "llvm/Support/MathExtras.h"
41#include "llvm/Support/MemoryBuffer.h"
42#include "llvm/Support/Threading.h"
43#include "llvm/Support/raw_ostream.h"
44#include <cassert>
45#include <cstdlib>
46#include <cstring>
47#include <system_error>
48
49using namespace llvm;
50
51DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OperandBundleDef, LLVMOperandBundleRef)
52
53inline BasicBlock **unwrap(LLVMBasicBlockRef *BBs) {
54 return reinterpret_cast<BasicBlock **>(BBs);
55}
56
57#define DEBUG_TYPE "ir"
58
59void llvm::initializeCore(PassRegistry &Registry) {
60 initializeDominatorTreeWrapperPassPass(Registry);
61 initializePrintModulePassWrapperPass(Registry);
62 initializePrintFunctionPassWrapperPass(Registry);
63 initializeSafepointIRVerifierPass(Registry);
64 initializeVerifierLegacyPassPass(Registry);
65}
66
67void LLVMShutdown() {
68 llvm_shutdown();
69}
70
71/*===-- Version query -----------------------------------------------------===*/
72
73void LLVMGetVersion(unsigned *Major, unsigned *Minor, unsigned *Patch) {
74 if (Major)
75 *Major = LLVM_VERSION_MAJOR;
76 if (Minor)
77 *Minor = LLVM_VERSION_MINOR;
78 if (Patch)
79 *Patch = LLVM_VERSION_PATCH;
80}
81
82/*===-- Error handling ----------------------------------------------------===*/
83
84char *LLVMCreateMessage(const char *Message) {
85 return strdup(s: Message);
86}
87
88void LLVMDisposeMessage(char *Message) {
89 free(ptr: Message);
90}
91
92
93/*===-- Operations on contexts --------------------------------------------===*/
94
95static LLVMContext &getGlobalContext() {
96 static LLVMContext GlobalContext;
97 return GlobalContext;
98}
99
100LLVMContextRef LLVMContextCreate() {
101 return wrap(P: new LLVMContext());
102}
103
104LLVMContextRef LLVMGetGlobalContext() { return wrap(P: &getGlobalContext()); }
105
106void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
107 LLVMDiagnosticHandler Handler,
108 void *DiagnosticContext) {
109 unwrap(P: C)->setDiagnosticHandlerCallBack(
110 LLVM_EXTENSION reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy>(
111 Handler),
112 DiagContext: DiagnosticContext);
113}
114
115LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) {
116 return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
117 unwrap(P: C)->getDiagnosticHandlerCallBack());
118}
119
120void *LLVMContextGetDiagnosticContext(LLVMContextRef C) {
121 return unwrap(P: C)->getDiagnosticContext();
122}
123
124void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
125 void *OpaqueHandle) {
126 auto YieldCallback =
127 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
128 unwrap(P: C)->setYieldCallback(Callback: YieldCallback, OpaqueHandle);
129}
130
131LLVMBool LLVMContextShouldDiscardValueNames(LLVMContextRef C) {
132 return unwrap(P: C)->shouldDiscardValueNames();
133}
134
135void LLVMContextSetDiscardValueNames(LLVMContextRef C, LLVMBool Discard) {
136 unwrap(P: C)->setDiscardValueNames(Discard);
137}
138
139void LLVMContextDispose(LLVMContextRef C) {
140 delete unwrap(P: C);
141}
142
143unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,
144 unsigned SLen) {
145 return unwrap(P: C)->getMDKindID(Name: StringRef(Name, SLen));
146}
147
148unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
149 return LLVMGetMDKindIDInContext(C: LLVMGetGlobalContext(), Name, SLen);
150}
151
152unsigned LLVMGetSyncScopeID(LLVMContextRef C, const char *Name, size_t SLen) {
153 return unwrap(P: C)->getOrInsertSyncScopeID(SSN: StringRef(Name, SLen));
154}
155
156unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
157 return Attribute::getAttrKindFromName(AttrName: StringRef(Name, SLen));
158}
159
160unsigned LLVMGetLastEnumAttributeKind(void) {
161 return Attribute::AttrKind::EndAttrKinds;
162}
163
164LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID,
165 uint64_t Val) {
166 auto &Ctx = *unwrap(P: C);
167 auto AttrKind = (Attribute::AttrKind)KindID;
168 return wrap(Attr: Attribute::get(Context&: Ctx, Kind: AttrKind, Val));
169}
170
171unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) {
172 return unwrap(Attr: A).getKindAsEnum();
173}
174
175uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) {
176 auto Attr = unwrap(Attr: A);
177 if (Attr.isEnumAttribute())
178 return 0;
179 return Attr.getValueAsInt();
180}
181
182LLVMAttributeRef LLVMCreateTypeAttribute(LLVMContextRef C, unsigned KindID,
183 LLVMTypeRef type_ref) {
184 auto &Ctx = *unwrap(P: C);
185 auto AttrKind = (Attribute::AttrKind)KindID;
186 return wrap(Attr: Attribute::get(Context&: Ctx, Kind: AttrKind, Ty: unwrap(P: type_ref)));
187}
188
189LLVMTypeRef LLVMGetTypeAttributeValue(LLVMAttributeRef A) {
190 auto Attr = unwrap(Attr: A);
191 return wrap(P: Attr.getValueAsType());
192}
193
194LLVMAttributeRef LLVMCreateConstantRangeAttribute(LLVMContextRef C,
195 unsigned KindID,
196 unsigned NumBits,
197 const uint64_t LowerWords[],
198 const uint64_t UpperWords[]) {
199 auto &Ctx = *unwrap(P: C);
200 auto AttrKind = (Attribute::AttrKind)KindID;
201 unsigned NumWords = divideCeil(Numerator: NumBits, Denominator: 64);
202 return wrap(Attr: Attribute::get(
203 Context&: Ctx, Kind: AttrKind,
204 CR: ConstantRange(APInt(NumBits, ArrayRef(LowerWords, NumWords)),
205 APInt(NumBits, ArrayRef(UpperWords, NumWords)))));
206}
207
208LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
209 const char *K, unsigned KLength,
210 const char *V, unsigned VLength) {
211 return wrap(Attr: Attribute::get(Context&: *unwrap(P: C), Kind: StringRef(K, KLength),
212 Val: StringRef(V, VLength)));
213}
214
215const char *LLVMGetStringAttributeKind(LLVMAttributeRef A,
216 unsigned *Length) {
217 auto S = unwrap(Attr: A).getKindAsString();
218 *Length = S.size();
219 return S.data();
220}
221
222const char *LLVMGetStringAttributeValue(LLVMAttributeRef A,
223 unsigned *Length) {
224 auto S = unwrap(Attr: A).getValueAsString();
225 *Length = S.size();
226 return S.data();
227}
228
229LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) {
230 auto Attr = unwrap(Attr: A);
231 return Attr.isEnumAttribute() || Attr.isIntAttribute();
232}
233
234LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) {
235 return unwrap(Attr: A).isStringAttribute();
236}
237
238LLVMBool LLVMIsTypeAttribute(LLVMAttributeRef A) {
239 return unwrap(Attr: A).isTypeAttribute();
240}
241
242char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
243 std::string MsgStorage;
244 raw_string_ostream Stream(MsgStorage);
245 DiagnosticPrinterRawOStream DP(Stream);
246
247 unwrap(P: DI)->print(DP);
248 Stream.flush();
249
250 return LLVMCreateMessage(Message: MsgStorage.c_str());
251}
252
253LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {
254 LLVMDiagnosticSeverity severity;
255
256 switch(unwrap(P: DI)->getSeverity()) {
257 default:
258 severity = LLVMDSError;
259 break;
260 case DS_Warning:
261 severity = LLVMDSWarning;
262 break;
263 case DS_Remark:
264 severity = LLVMDSRemark;
265 break;
266 case DS_Note:
267 severity = LLVMDSNote;
268 break;
269 }
270
271 return severity;
272}
273
274/*===-- Operations on modules ---------------------------------------------===*/
275
276LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
277 return wrap(P: new Module(ModuleID, getGlobalContext()));
278}
279
280LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
281 LLVMContextRef C) {
282 return wrap(P: new Module(ModuleID, *unwrap(P: C)));
283}
284
285void LLVMDisposeModule(LLVMModuleRef M) {
286 delete unwrap(P: M);
287}
288
289const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
290 auto &Str = unwrap(P: M)->getModuleIdentifier();
291 *Len = Str.length();
292 return Str.c_str();
293}
294
295void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
296 unwrap(P: M)->setModuleIdentifier(StringRef(Ident, Len));
297}
298
299const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
300 auto &Str = unwrap(P: M)->getSourceFileName();
301 *Len = Str.length();
302 return Str.c_str();
303}
304
305void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
306 unwrap(P: M)->setSourceFileName(StringRef(Name, Len));
307}
308
309/*--.. Data layout .........................................................--*/
310const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
311 return unwrap(P: M)->getDataLayoutStr().c_str();
312}
313
314const char *LLVMGetDataLayout(LLVMModuleRef M) {
315 return LLVMGetDataLayoutStr(M);
316}
317
318void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
319 unwrap(P: M)->setDataLayout(DataLayoutStr);
320}
321
322/*--.. Target triple .......................................................--*/
323const char * LLVMGetTarget(LLVMModuleRef M) {
324 return unwrap(P: M)->getTargetTriple().str().c_str();
325}
326
327void LLVMSetTarget(LLVMModuleRef M, const char *TripleStr) {
328 unwrap(P: M)->setTargetTriple(Triple(TripleStr));
329}
330
331/*--.. Module flags ........................................................--*/
332struct LLVMOpaqueModuleFlagEntry {
333 LLVMModuleFlagBehavior Behavior;
334 const char *Key;
335 size_t KeyLen;
336 LLVMMetadataRef Metadata;
337};
338
339static Module::ModFlagBehavior
340map_to_llvmModFlagBehavior(LLVMModuleFlagBehavior Behavior) {
341 switch (Behavior) {
342 case LLVMModuleFlagBehaviorError:
343 return Module::ModFlagBehavior::Error;
344 case LLVMModuleFlagBehaviorWarning:
345 return Module::ModFlagBehavior::Warning;
346 case LLVMModuleFlagBehaviorRequire:
347 return Module::ModFlagBehavior::Require;
348 case LLVMModuleFlagBehaviorOverride:
349 return Module::ModFlagBehavior::Override;
350 case LLVMModuleFlagBehaviorAppend:
351 return Module::ModFlagBehavior::Append;
352 case LLVMModuleFlagBehaviorAppendUnique:
353 return Module::ModFlagBehavior::AppendUnique;
354 }
355 llvm_unreachable("Unknown LLVMModuleFlagBehavior");
356}
357
358static LLVMModuleFlagBehavior
359map_from_llvmModFlagBehavior(Module::ModFlagBehavior Behavior) {
360 switch (Behavior) {
361 case Module::ModFlagBehavior::Error:
362 return LLVMModuleFlagBehaviorError;
363 case Module::ModFlagBehavior::Warning:
364 return LLVMModuleFlagBehaviorWarning;
365 case Module::ModFlagBehavior::Require:
366 return LLVMModuleFlagBehaviorRequire;
367 case Module::ModFlagBehavior::Override:
368 return LLVMModuleFlagBehaviorOverride;
369 case Module::ModFlagBehavior::Append:
370 return LLVMModuleFlagBehaviorAppend;
371 case Module::ModFlagBehavior::AppendUnique:
372 return LLVMModuleFlagBehaviorAppendUnique;
373 default:
374 llvm_unreachable("Unhandled Flag Behavior");
375 }
376}
377
378LLVMModuleFlagEntry *LLVMCopyModuleFlagsMetadata(LLVMModuleRef M, size_t *Len) {
379 SmallVector<Module::ModuleFlagEntry, 8> MFEs;
380 unwrap(P: M)->getModuleFlagsMetadata(Flags&: MFEs);
381
382 LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>(
383 safe_malloc(Sz: MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
384 for (unsigned i = 0; i < MFEs.size(); ++i) {
385 const auto &ModuleFlag = MFEs[i];
386 Result[i].Behavior = map_from_llvmModFlagBehavior(Behavior: ModuleFlag.Behavior);
387 Result[i].Key = ModuleFlag.Key->getString().data();
388 Result[i].KeyLen = ModuleFlag.Key->getString().size();
389 Result[i].Metadata = wrap(P: ModuleFlag.Val);
390 }
391 *Len = MFEs.size();
392 return Result;
393}
394
395void LLVMDisposeModuleFlagsMetadata(LLVMModuleFlagEntry *Entries) {
396 free(ptr: Entries);
397}
398
399LLVMModuleFlagBehavior
400LLVMModuleFlagEntriesGetFlagBehavior(LLVMModuleFlagEntry *Entries,
401 unsigned Index) {
402 LLVMOpaqueModuleFlagEntry MFE =
403 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
404 return MFE.Behavior;
405}
406
407const char *LLVMModuleFlagEntriesGetKey(LLVMModuleFlagEntry *Entries,
408 unsigned Index, size_t *Len) {
409 LLVMOpaqueModuleFlagEntry MFE =
410 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
411 *Len = MFE.KeyLen;
412 return MFE.Key;
413}
414
415LLVMMetadataRef LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry *Entries,
416 unsigned Index) {
417 LLVMOpaqueModuleFlagEntry MFE =
418 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
419 return MFE.Metadata;
420}
421
422LLVMMetadataRef LLVMGetModuleFlag(LLVMModuleRef M,
423 const char *Key, size_t KeyLen) {
424 return wrap(P: unwrap(P: M)->getModuleFlag(Key: {Key, KeyLen}));
425}
426
427void LLVMAddModuleFlag(LLVMModuleRef M, LLVMModuleFlagBehavior Behavior,
428 const char *Key, size_t KeyLen,
429 LLVMMetadataRef Val) {
430 unwrap(P: M)->addModuleFlag(Behavior: map_to_llvmModFlagBehavior(Behavior),
431 Key: {Key, KeyLen}, Val: unwrap(P: Val));
432}
433
434LLVMBool LLVMIsNewDbgInfoFormat(LLVMModuleRef M) { return true; }
435
436void LLVMSetIsNewDbgInfoFormat(LLVMModuleRef M, LLVMBool UseNewFormat) {
437 if (!UseNewFormat)
438 llvm_unreachable("LLVM no longer supports intrinsic based debug-info");
439 (void)M;
440}
441
442/*--.. Printing modules ....................................................--*/
443
444void LLVMDumpModule(LLVMModuleRef M) {
445 unwrap(P: M)->print(OS&: errs(), AAW: nullptr,
446 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
447}
448
449LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
450 char **ErrorMessage) {
451 std::error_code EC;
452 raw_fd_ostream dest(Filename, EC, sys::fs::OF_TextWithCRLF);
453 if (EC) {
454 *ErrorMessage = strdup(s: EC.message().c_str());
455 return true;
456 }
457
458 unwrap(P: M)->print(OS&: dest, AAW: nullptr);
459
460 dest.close();
461
462 if (dest.has_error()) {
463 std::string E = "Error printing to file: " + dest.error().message();
464 *ErrorMessage = strdup(s: E.c_str());
465 return true;
466 }
467
468 return false;
469}
470
471char *LLVMPrintModuleToString(LLVMModuleRef M) {
472 std::string buf;
473 raw_string_ostream os(buf);
474
475 unwrap(P: M)->print(OS&: os, AAW: nullptr);
476 os.flush();
477
478 return strdup(s: buf.c_str());
479}
480
481/*--.. Operations on inline assembler ......................................--*/
482void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
483 unwrap(P: M)->setModuleInlineAsm(StringRef(Asm, Len));
484}
485
486void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
487 unwrap(P: M)->setModuleInlineAsm(StringRef(Asm));
488}
489
490void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
491 unwrap(P: M)->appendModuleInlineAsm(Asm: StringRef(Asm, Len));
492}
493
494const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
495 auto &Str = unwrap(P: M)->getModuleInlineAsm();
496 *Len = Str.length();
497 return Str.c_str();
498}
499
500LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty, const char *AsmString,
501 size_t AsmStringSize, const char *Constraints,
502 size_t ConstraintsSize, LLVMBool HasSideEffects,
503 LLVMBool IsAlignStack,
504 LLVMInlineAsmDialect Dialect, LLVMBool CanThrow) {
505 InlineAsm::AsmDialect AD;
506 switch (Dialect) {
507 case LLVMInlineAsmDialectATT:
508 AD = InlineAsm::AD_ATT;
509 break;
510 case LLVMInlineAsmDialectIntel:
511 AD = InlineAsm::AD_Intel;
512 break;
513 }
514 return wrap(P: InlineAsm::get(Ty: unwrap<FunctionType>(P: Ty),
515 AsmString: StringRef(AsmString, AsmStringSize),
516 Constraints: StringRef(Constraints, ConstraintsSize),
517 hasSideEffects: HasSideEffects, isAlignStack: IsAlignStack, asmDialect: AD, canThrow: CanThrow));
518}
519
520const char *LLVMGetInlineAsmAsmString(LLVMValueRef InlineAsmVal, size_t *Len) {
521
522 Value *Val = unwrap<Value>(P: InlineAsmVal);
523 StringRef AsmString = cast<InlineAsm>(Val)->getAsmString();
524
525 *Len = AsmString.size();
526 return AsmString.data();
527}
528
529const char *LLVMGetInlineAsmConstraintString(LLVMValueRef InlineAsmVal,
530 size_t *Len) {
531 Value *Val = unwrap<Value>(P: InlineAsmVal);
532 StringRef ConstraintString = cast<InlineAsm>(Val)->getConstraintString();
533
534 *Len = ConstraintString.size();
535 return ConstraintString.data();
536}
537
538LLVMInlineAsmDialect LLVMGetInlineAsmDialect(LLVMValueRef InlineAsmVal) {
539
540 Value *Val = unwrap<Value>(P: InlineAsmVal);
541 InlineAsm::AsmDialect Dialect = cast<InlineAsm>(Val)->getDialect();
542
543 switch (Dialect) {
544 case InlineAsm::AD_ATT:
545 return LLVMInlineAsmDialectATT;
546 case InlineAsm::AD_Intel:
547 return LLVMInlineAsmDialectIntel;
548 }
549
550 llvm_unreachable("Unrecognized inline assembly dialect");
551 return LLVMInlineAsmDialectATT;
552}
553
554LLVMTypeRef LLVMGetInlineAsmFunctionType(LLVMValueRef InlineAsmVal) {
555 Value *Val = unwrap<Value>(P: InlineAsmVal);
556 return (LLVMTypeRef)cast<InlineAsm>(Val)->getFunctionType();
557}
558
559LLVMBool LLVMGetInlineAsmHasSideEffects(LLVMValueRef InlineAsmVal) {
560 Value *Val = unwrap<Value>(P: InlineAsmVal);
561 return cast<InlineAsm>(Val)->hasSideEffects();
562}
563
564LLVMBool LLVMGetInlineAsmNeedsAlignedStack(LLVMValueRef InlineAsmVal) {
565 Value *Val = unwrap<Value>(P: InlineAsmVal);
566 return cast<InlineAsm>(Val)->isAlignStack();
567}
568
569LLVMBool LLVMGetInlineAsmCanUnwind(LLVMValueRef InlineAsmVal) {
570 Value *Val = unwrap<Value>(P: InlineAsmVal);
571 return cast<InlineAsm>(Val)->canThrow();
572}
573
574/*--.. Operations on module contexts ......................................--*/
575LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
576 return wrap(P: &unwrap(P: M)->getContext());
577}
578
579
580/*===-- Operations on types -----------------------------------------------===*/
581
582/*--.. Operations on all types (mostly) ....................................--*/
583
584LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
585 switch (unwrap(P: Ty)->getTypeID()) {
586 case Type::VoidTyID:
587 return LLVMVoidTypeKind;
588 case Type::HalfTyID:
589 return LLVMHalfTypeKind;
590 case Type::BFloatTyID:
591 return LLVMBFloatTypeKind;
592 case Type::FloatTyID:
593 return LLVMFloatTypeKind;
594 case Type::DoubleTyID:
595 return LLVMDoubleTypeKind;
596 case Type::X86_FP80TyID:
597 return LLVMX86_FP80TypeKind;
598 case Type::FP128TyID:
599 return LLVMFP128TypeKind;
600 case Type::PPC_FP128TyID:
601 return LLVMPPC_FP128TypeKind;
602 case Type::LabelTyID:
603 return LLVMLabelTypeKind;
604 case Type::MetadataTyID:
605 return LLVMMetadataTypeKind;
606 case Type::IntegerTyID:
607 return LLVMIntegerTypeKind;
608 case Type::FunctionTyID:
609 return LLVMFunctionTypeKind;
610 case Type::StructTyID:
611 return LLVMStructTypeKind;
612 case Type::ArrayTyID:
613 return LLVMArrayTypeKind;
614 case Type::PointerTyID:
615 return LLVMPointerTypeKind;
616 case Type::FixedVectorTyID:
617 return LLVMVectorTypeKind;
618 case Type::X86_AMXTyID:
619 return LLVMX86_AMXTypeKind;
620 case Type::TokenTyID:
621 return LLVMTokenTypeKind;
622 case Type::ScalableVectorTyID:
623 return LLVMScalableVectorTypeKind;
624 case Type::TargetExtTyID:
625 return LLVMTargetExtTypeKind;
626 case Type::TypedPointerTyID:
627 llvm_unreachable("Typed pointers are unsupported via the C API");
628 }
629 llvm_unreachable("Unhandled TypeID.");
630}
631
632LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
633{
634 return unwrap(P: Ty)->isSized();
635}
636
637LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
638 return wrap(P: &unwrap(P: Ty)->getContext());
639}
640
641void LLVMDumpType(LLVMTypeRef Ty) {
642 return unwrap(P: Ty)->print(O&: errs(), /*IsForDebug=*/true);
643}
644
645char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
646 std::string buf;
647 raw_string_ostream os(buf);
648
649 if (unwrap(P: Ty))
650 unwrap(P: Ty)->print(O&: os);
651 else
652 os << "Printing <null> Type";
653
654 os.flush();
655
656 return strdup(s: buf.c_str());
657}
658
659/*--.. Operations on integer types .........................................--*/
660
661LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
662 return (LLVMTypeRef) Type::getInt1Ty(C&: *unwrap(P: C));
663}
664LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
665 return (LLVMTypeRef) Type::getInt8Ty(C&: *unwrap(P: C));
666}
667LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
668 return (LLVMTypeRef) Type::getInt16Ty(C&: *unwrap(P: C));
669}
670LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
671 return (LLVMTypeRef) Type::getInt32Ty(C&: *unwrap(P: C));
672}
673LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
674 return (LLVMTypeRef) Type::getInt64Ty(C&: *unwrap(P: C));
675}
676LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
677 return (LLVMTypeRef) Type::getInt128Ty(C&: *unwrap(P: C));
678}
679LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
680 return wrap(P: IntegerType::get(C&: *unwrap(P: C), NumBits));
681}
682
683LLVMTypeRef LLVMInt1Type(void) {
684 return LLVMInt1TypeInContext(C: LLVMGetGlobalContext());
685}
686LLVMTypeRef LLVMInt8Type(void) {
687 return LLVMInt8TypeInContext(C: LLVMGetGlobalContext());
688}
689LLVMTypeRef LLVMInt16Type(void) {
690 return LLVMInt16TypeInContext(C: LLVMGetGlobalContext());
691}
692LLVMTypeRef LLVMInt32Type(void) {
693 return LLVMInt32TypeInContext(C: LLVMGetGlobalContext());
694}
695LLVMTypeRef LLVMInt64Type(void) {
696 return LLVMInt64TypeInContext(C: LLVMGetGlobalContext());
697}
698LLVMTypeRef LLVMInt128Type(void) {
699 return LLVMInt128TypeInContext(C: LLVMGetGlobalContext());
700}
701LLVMTypeRef LLVMIntType(unsigned NumBits) {
702 return LLVMIntTypeInContext(C: LLVMGetGlobalContext(), NumBits);
703}
704
705unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
706 return unwrap<IntegerType>(P: IntegerTy)->getBitWidth();
707}
708
709/*--.. Operations on real types ............................................--*/
710
711LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
712 return (LLVMTypeRef) Type::getHalfTy(C&: *unwrap(P: C));
713}
714LLVMTypeRef LLVMBFloatTypeInContext(LLVMContextRef C) {
715 return (LLVMTypeRef) Type::getBFloatTy(C&: *unwrap(P: C));
716}
717LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
718 return (LLVMTypeRef) Type::getFloatTy(C&: *unwrap(P: C));
719}
720LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
721 return (LLVMTypeRef) Type::getDoubleTy(C&: *unwrap(P: C));
722}
723LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
724 return (LLVMTypeRef) Type::getX86_FP80Ty(C&: *unwrap(P: C));
725}
726LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
727 return (LLVMTypeRef) Type::getFP128Ty(C&: *unwrap(P: C));
728}
729LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
730 return (LLVMTypeRef) Type::getPPC_FP128Ty(C&: *unwrap(P: C));
731}
732LLVMTypeRef LLVMX86AMXTypeInContext(LLVMContextRef C) {
733 return (LLVMTypeRef) Type::getX86_AMXTy(C&: *unwrap(P: C));
734}
735
736LLVMTypeRef LLVMHalfType(void) {
737 return LLVMHalfTypeInContext(C: LLVMGetGlobalContext());
738}
739LLVMTypeRef LLVMBFloatType(void) {
740 return LLVMBFloatTypeInContext(C: LLVMGetGlobalContext());
741}
742LLVMTypeRef LLVMFloatType(void) {
743 return LLVMFloatTypeInContext(C: LLVMGetGlobalContext());
744}
745LLVMTypeRef LLVMDoubleType(void) {
746 return LLVMDoubleTypeInContext(C: LLVMGetGlobalContext());
747}
748LLVMTypeRef LLVMX86FP80Type(void) {
749 return LLVMX86FP80TypeInContext(C: LLVMGetGlobalContext());
750}
751LLVMTypeRef LLVMFP128Type(void) {
752 return LLVMFP128TypeInContext(C: LLVMGetGlobalContext());
753}
754LLVMTypeRef LLVMPPCFP128Type(void) {
755 return LLVMPPCFP128TypeInContext(C: LLVMGetGlobalContext());
756}
757LLVMTypeRef LLVMX86AMXType(void) {
758 return LLVMX86AMXTypeInContext(C: LLVMGetGlobalContext());
759}
760
761/*--.. Operations on function types ........................................--*/
762
763LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
764 LLVMTypeRef *ParamTypes, unsigned ParamCount,
765 LLVMBool IsVarArg) {
766 ArrayRef<Type*> Tys(unwrap(Tys: ParamTypes), ParamCount);
767 return wrap(P: FunctionType::get(Result: unwrap(P: ReturnType), Params: Tys, isVarArg: IsVarArg != 0));
768}
769
770LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
771 return unwrap<FunctionType>(P: FunctionTy)->isVarArg();
772}
773
774LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
775 return wrap(P: unwrap<FunctionType>(P: FunctionTy)->getReturnType());
776}
777
778unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
779 return unwrap<FunctionType>(P: FunctionTy)->getNumParams();
780}
781
782void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
783 FunctionType *Ty = unwrap<FunctionType>(P: FunctionTy);
784 for (Type *T : Ty->params())
785 *Dest++ = wrap(P: T);
786}
787
788/*--.. Operations on struct types ..........................................--*/
789
790LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
791 unsigned ElementCount, LLVMBool Packed) {
792 ArrayRef<Type*> Tys(unwrap(Tys: ElementTypes), ElementCount);
793 return wrap(P: StructType::get(Context&: *unwrap(P: C), Elements: Tys, isPacked: Packed != 0));
794}
795
796LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
797 unsigned ElementCount, LLVMBool Packed) {
798 return LLVMStructTypeInContext(C: LLVMGetGlobalContext(), ElementTypes,
799 ElementCount, Packed);
800}
801
802LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
803{
804 return wrap(P: StructType::create(Context&: *unwrap(P: C), Name));
805}
806
807const char *LLVMGetStructName(LLVMTypeRef Ty)
808{
809 StructType *Type = unwrap<StructType>(P: Ty);
810 if (!Type->hasName())
811 return nullptr;
812 return Type->getName().data();
813}
814
815void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
816 unsigned ElementCount, LLVMBool Packed) {
817 ArrayRef<Type*> Tys(unwrap(Tys: ElementTypes), ElementCount);
818 unwrap<StructType>(P: StructTy)->setBody(Elements: Tys, isPacked: Packed != 0);
819}
820
821unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
822 return unwrap<StructType>(P: StructTy)->getNumElements();
823}
824
825void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
826 StructType *Ty = unwrap<StructType>(P: StructTy);
827 for (Type *T : Ty->elements())
828 *Dest++ = wrap(P: T);
829}
830
831LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
832 StructType *Ty = unwrap<StructType>(P: StructTy);
833 return wrap(P: Ty->getTypeAtIndex(N: i));
834}
835
836LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
837 return unwrap<StructType>(P: StructTy)->isPacked();
838}
839
840LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
841 return unwrap<StructType>(P: StructTy)->isOpaque();
842}
843
844LLVMBool LLVMIsLiteralStruct(LLVMTypeRef StructTy) {
845 return unwrap<StructType>(P: StructTy)->isLiteral();
846}
847
848LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
849 return wrap(P: StructType::getTypeByName(C&: unwrap(P: M)->getContext(), Name));
850}
851
852LLVMTypeRef LLVMGetTypeByName2(LLVMContextRef C, const char *Name) {
853 return wrap(P: StructType::getTypeByName(C&: *unwrap(P: C), Name));
854}
855
856/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
857
858void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
859 int i = 0;
860 for (auto *T : unwrap(P: Tp)->subtypes()) {
861 Arr[i] = wrap(P: T);
862 i++;
863 }
864}
865
866LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
867 return wrap(P: ArrayType::get(ElementType: unwrap(P: ElementType), NumElements: ElementCount));
868}
869
870LLVMTypeRef LLVMArrayType2(LLVMTypeRef ElementType, uint64_t ElementCount) {
871 return wrap(P: ArrayType::get(ElementType: unwrap(P: ElementType), NumElements: ElementCount));
872}
873
874LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
875 return wrap(
876 P: PointerType::get(C&: unwrap(P: ElementType)->getContext(), AddressSpace));
877}
878
879LLVMBool LLVMPointerTypeIsOpaque(LLVMTypeRef Ty) {
880 return true;
881}
882
883LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
884 return wrap(P: FixedVectorType::get(ElementType: unwrap(P: ElementType), NumElts: ElementCount));
885}
886
887LLVMTypeRef LLVMScalableVectorType(LLVMTypeRef ElementType,
888 unsigned ElementCount) {
889 return wrap(P: ScalableVectorType::get(ElementType: unwrap(P: ElementType), MinNumElts: ElementCount));
890}
891
892LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
893 auto *Ty = unwrap(P: WrappedTy);
894 if (auto *ATy = dyn_cast<ArrayType>(Val: Ty))
895 return wrap(P: ATy->getElementType());
896 return wrap(P: cast<VectorType>(Val: Ty)->getElementType());
897}
898
899unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
900 return unwrap(P: Tp)->getNumContainedTypes();
901}
902
903unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
904 return unwrap<ArrayType>(P: ArrayTy)->getNumElements();
905}
906
907uint64_t LLVMGetArrayLength2(LLVMTypeRef ArrayTy) {
908 return unwrap<ArrayType>(P: ArrayTy)->getNumElements();
909}
910
911unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
912 return unwrap<PointerType>(P: PointerTy)->getAddressSpace();
913}
914
915unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
916 return unwrap<VectorType>(P: VectorTy)->getElementCount().getKnownMinValue();
917}
918
919LLVMValueRef LLVMGetConstantPtrAuthPointer(LLVMValueRef PtrAuth) {
920 return wrap(P: unwrap<ConstantPtrAuth>(P: PtrAuth)->getPointer());
921}
922
923LLVMValueRef LLVMGetConstantPtrAuthKey(LLVMValueRef PtrAuth) {
924 return wrap(P: unwrap<ConstantPtrAuth>(P: PtrAuth)->getKey());
925}
926
927LLVMValueRef LLVMGetConstantPtrAuthDiscriminator(LLVMValueRef PtrAuth) {
928 return wrap(P: unwrap<ConstantPtrAuth>(P: PtrAuth)->getDiscriminator());
929}
930
931LLVMValueRef LLVMGetConstantPtrAuthAddrDiscriminator(LLVMValueRef PtrAuth) {
932 return wrap(P: unwrap<ConstantPtrAuth>(P: PtrAuth)->getAddrDiscriminator());
933}
934
935/*--.. Operations on other types ...........................................--*/
936
937LLVMTypeRef LLVMPointerTypeInContext(LLVMContextRef C, unsigned AddressSpace) {
938 return wrap(P: PointerType::get(C&: *unwrap(P: C), AddressSpace));
939}
940
941LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
942 return wrap(P: Type::getVoidTy(C&: *unwrap(P: C)));
943}
944LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
945 return wrap(P: Type::getLabelTy(C&: *unwrap(P: C)));
946}
947LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
948 return wrap(P: Type::getTokenTy(C&: *unwrap(P: C)));
949}
950LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) {
951 return wrap(P: Type::getMetadataTy(C&: *unwrap(P: C)));
952}
953
954LLVMTypeRef LLVMVoidType(void) {
955 return LLVMVoidTypeInContext(C: LLVMGetGlobalContext());
956}
957LLVMTypeRef LLVMLabelType(void) {
958 return LLVMLabelTypeInContext(C: LLVMGetGlobalContext());
959}
960
961LLVMTypeRef LLVMTargetExtTypeInContext(LLVMContextRef C, const char *Name,
962 LLVMTypeRef *TypeParams,
963 unsigned TypeParamCount,
964 unsigned *IntParams,
965 unsigned IntParamCount) {
966 ArrayRef<Type *> TypeParamArray(unwrap(Tys: TypeParams), TypeParamCount);
967 ArrayRef<unsigned> IntParamArray(IntParams, IntParamCount);
968 return wrap(
969 P: TargetExtType::get(Context&: *unwrap(P: C), Name, Types: TypeParamArray, Ints: IntParamArray));
970}
971
972const char *LLVMGetTargetExtTypeName(LLVMTypeRef TargetExtTy) {
973 TargetExtType *Type = unwrap<TargetExtType>(P: TargetExtTy);
974 return Type->getName().data();
975}
976
977unsigned LLVMGetTargetExtTypeNumTypeParams(LLVMTypeRef TargetExtTy) {
978 TargetExtType *Type = unwrap<TargetExtType>(P: TargetExtTy);
979 return Type->getNumTypeParameters();
980}
981
982LLVMTypeRef LLVMGetTargetExtTypeTypeParam(LLVMTypeRef TargetExtTy,
983 unsigned Idx) {
984 TargetExtType *Type = unwrap<TargetExtType>(P: TargetExtTy);
985 return wrap(P: Type->getTypeParameter(i: Idx));
986}
987
988unsigned LLVMGetTargetExtTypeNumIntParams(LLVMTypeRef TargetExtTy) {
989 TargetExtType *Type = unwrap<TargetExtType>(P: TargetExtTy);
990 return Type->getNumIntParameters();
991}
992
993unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy, unsigned Idx) {
994 TargetExtType *Type = unwrap<TargetExtType>(P: TargetExtTy);
995 return Type->getIntParameter(i: Idx);
996}
997
998/*===-- Operations on values ----------------------------------------------===*/
999
1000/*--.. Operations on all values ............................................--*/
1001
1002LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
1003 return wrap(P: unwrap(P: Val)->getType());
1004}
1005
1006LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
1007 switch(unwrap(P: Val)->getValueID()) {
1008#define LLVM_C_API 1
1009#define HANDLE_VALUE(Name) \
1010 case Value::Name##Val: \
1011 return LLVM##Name##ValueKind;
1012#include "llvm/IR/Value.def"
1013 default:
1014 return LLVMInstructionValueKind;
1015 }
1016}
1017
1018const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
1019 auto *V = unwrap(P: Val);
1020 *Length = V->getName().size();
1021 return V->getName().data();
1022}
1023
1024void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
1025 unwrap(P: Val)->setName(StringRef(Name, NameLen));
1026}
1027
1028const char *LLVMGetValueName(LLVMValueRef Val) {
1029 return unwrap(P: Val)->getName().data();
1030}
1031
1032void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
1033 unwrap(P: Val)->setName(Name);
1034}
1035
1036void LLVMDumpValue(LLVMValueRef Val) {
1037 unwrap(P: Val)->print(O&: errs(), /*IsForDebug=*/true);
1038}
1039
1040char* LLVMPrintValueToString(LLVMValueRef Val) {
1041 std::string buf;
1042 raw_string_ostream os(buf);
1043
1044 if (unwrap(P: Val))
1045 unwrap(P: Val)->print(O&: os);
1046 else
1047 os << "Printing <null> Value";
1048
1049 os.flush();
1050
1051 return strdup(s: buf.c_str());
1052}
1053
1054LLVMContextRef LLVMGetValueContext(LLVMValueRef Val) {
1055 return wrap(P: &unwrap(P: Val)->getContext());
1056}
1057
1058char *LLVMPrintDbgRecordToString(LLVMDbgRecordRef Record) {
1059 std::string buf;
1060 raw_string_ostream os(buf);
1061
1062 if (unwrap(P: Record))
1063 unwrap(P: Record)->print(O&: os);
1064 else
1065 os << "Printing <null> DbgRecord";
1066
1067 os.flush();
1068
1069 return strdup(s: buf.c_str());
1070}
1071
1072void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
1073 unwrap(P: OldVal)->replaceAllUsesWith(V: unwrap(P: NewVal));
1074}
1075
1076int LLVMHasMetadata(LLVMValueRef Inst) {
1077 return unwrap<Instruction>(P: Inst)->hasMetadata();
1078}
1079
1080LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
1081 auto *I = unwrap<Instruction>(P: Inst);
1082 assert(I && "Expected instruction");
1083 if (auto *MD = I->getMetadata(KindID))
1084 return wrap(P: MetadataAsValue::get(Context&: I->getContext(), MD));
1085 return nullptr;
1086}
1087
1088// MetadataAsValue uses a canonical format which strips the actual MDNode for
1089// MDNode with just a single constant value, storing just a ConstantAsMetadata
1090// This undoes this canonicalization, reconstructing the MDNode.
1091static MDNode *extractMDNode(MetadataAsValue *MAV) {
1092 Metadata *MD = MAV->getMetadata();
1093 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
1094 "Expected a metadata node or a canonicalized constant");
1095
1096 if (MDNode *N = dyn_cast<MDNode>(Val: MD))
1097 return N;
1098
1099 return MDNode::get(Context&: MAV->getContext(), MDs: MD);
1100}
1101
1102void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
1103 MDNode *N = Val ? extractMDNode(MAV: unwrap<MetadataAsValue>(P: Val)) : nullptr;
1104
1105 unwrap<Instruction>(P: Inst)->setMetadata(KindID, Node: N);
1106}
1107
1108struct LLVMOpaqueValueMetadataEntry {
1109 unsigned Kind;
1110 LLVMMetadataRef Metadata;
1111};
1112
1113using MetadataEntries = SmallVectorImpl<std::pair<unsigned, MDNode *>>;
1114static LLVMValueMetadataEntry *
1115llvm_getMetadata(size_t *NumEntries,
1116 llvm::function_ref<void(MetadataEntries &)> AccessMD) {
1117 SmallVector<std::pair<unsigned, MDNode *>, 8> MVEs;
1118 AccessMD(MVEs);
1119
1120 LLVMOpaqueValueMetadataEntry *Result =
1121 static_cast<LLVMOpaqueValueMetadataEntry *>(
1122 safe_malloc(Sz: MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry)));
1123 for (unsigned i = 0; i < MVEs.size(); ++i) {
1124 const auto &ModuleFlag = MVEs[i];
1125 Result[i].Kind = ModuleFlag.first;
1126 Result[i].Metadata = wrap(P: ModuleFlag.second);
1127 }
1128 *NumEntries = MVEs.size();
1129 return Result;
1130}
1131
1132LLVMValueMetadataEntry *
1133LLVMInstructionGetAllMetadataOtherThanDebugLoc(LLVMValueRef Value,
1134 size_t *NumEntries) {
1135 return llvm_getMetadata(NumEntries, AccessMD: [&Value](MetadataEntries &Entries) {
1136 Entries.clear();
1137 unwrap<Instruction>(P: Value)->getAllMetadata(MDs&: Entries);
1138 });
1139}
1140
1141/*--.. Conversion functions ................................................--*/
1142
1143#define LLVM_DEFINE_VALUE_CAST(name) \
1144 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
1145 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
1146 }
1147
1148LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
1149
1150LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
1151 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(Val: unwrap(P: Val)))
1152 if (isa<MDNode>(Val: MD->getMetadata()) ||
1153 isa<ValueAsMetadata>(Val: MD->getMetadata()))
1154 return Val;
1155 return nullptr;
1156}
1157
1158LLVMValueRef LLVMIsAValueAsMetadata(LLVMValueRef Val) {
1159 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(Val: unwrap(P: Val)))
1160 if (isa<ValueAsMetadata>(Val: MD->getMetadata()))
1161 return Val;
1162 return nullptr;
1163}
1164
1165LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
1166 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(Val: unwrap(P: Val)))
1167 if (isa<MDString>(Val: MD->getMetadata()))
1168 return Val;
1169 return nullptr;
1170}
1171
1172/*--.. Operations on Uses ..................................................--*/
1173LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
1174 Value *V = unwrap(P: Val);
1175 Value::use_iterator I = V->use_begin();
1176 if (I == V->use_end())
1177 return nullptr;
1178 return wrap(P: &*I);
1179}
1180
1181LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
1182 Use *Next = unwrap(P: U)->getNext();
1183 if (Next)
1184 return wrap(P: Next);
1185 return nullptr;
1186}
1187
1188LLVMValueRef LLVMGetUser(LLVMUseRef U) {
1189 return wrap(P: unwrap(P: U)->getUser());
1190}
1191
1192LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
1193 return wrap(P: unwrap(P: U)->get());
1194}
1195
1196/*--.. Operations on Users .................................................--*/
1197
1198static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
1199 unsigned Index) {
1200 Metadata *Op = N->getOperand(I: Index);
1201 if (!Op)
1202 return nullptr;
1203 if (auto *C = dyn_cast<ConstantAsMetadata>(Val: Op))
1204 return wrap(P: C->getValue());
1205 return wrap(P: MetadataAsValue::get(Context, MD: Op));
1206}
1207
1208LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
1209 Value *V = unwrap(P: Val);
1210 if (auto *MD = dyn_cast<MetadataAsValue>(Val: V)) {
1211 if (auto *L = dyn_cast<ValueAsMetadata>(Val: MD->getMetadata())) {
1212 assert(Index == 0 && "Function-local metadata can only have one operand");
1213 return wrap(P: L->getValue());
1214 }
1215 return getMDNodeOperandImpl(Context&: V->getContext(),
1216 N: cast<MDNode>(Val: MD->getMetadata()), Index);
1217 }
1218
1219 return wrap(P: cast<User>(Val: V)->getOperand(i: Index));
1220}
1221
1222LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
1223 Value *V = unwrap(P: Val);
1224 return wrap(P: &cast<User>(Val: V)->getOperandUse(i: Index));
1225}
1226
1227void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
1228 unwrap<User>(P: Val)->setOperand(i: Index, Val: unwrap(P: Op));
1229}
1230
1231int LLVMGetNumOperands(LLVMValueRef Val) {
1232 Value *V = unwrap(P: Val);
1233 if (isa<MetadataAsValue>(Val: V))
1234 return LLVMGetMDNodeNumOperands(V: Val);
1235
1236 return cast<User>(Val: V)->getNumOperands();
1237}
1238
1239/*--.. Operations on constants of any type .................................--*/
1240
1241LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
1242 return wrap(P: Constant::getNullValue(Ty: unwrap(P: Ty)));
1243}
1244
1245LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
1246 return wrap(P: Constant::getAllOnesValue(Ty: unwrap(P: Ty)));
1247}
1248
1249LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
1250 return wrap(P: UndefValue::get(T: unwrap(P: Ty)));
1251}
1252
1253LLVMValueRef LLVMGetPoison(LLVMTypeRef Ty) {
1254 return wrap(P: PoisonValue::get(T: unwrap(P: Ty)));
1255}
1256
1257LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
1258 return isa<Constant>(Val: unwrap(P: Ty));
1259}
1260
1261LLVMBool LLVMIsNull(LLVMValueRef Val) {
1262 if (Constant *C = dyn_cast<Constant>(Val: unwrap(P: Val)))
1263 return C->isNullValue();
1264 return false;
1265}
1266
1267LLVMBool LLVMIsUndef(LLVMValueRef Val) {
1268 return isa<UndefValue>(Val: unwrap(P: Val));
1269}
1270
1271LLVMBool LLVMIsPoison(LLVMValueRef Val) {
1272 return isa<PoisonValue>(Val: unwrap(P: Val));
1273}
1274
1275LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
1276 return wrap(P: ConstantPointerNull::get(T: unwrap<PointerType>(P: Ty)));
1277}
1278
1279/*--.. Operations on metadata nodes ........................................--*/
1280
1281LLVMMetadataRef LLVMMDStringInContext2(LLVMContextRef C, const char *Str,
1282 size_t SLen) {
1283 return wrap(P: MDString::get(Context&: *unwrap(P: C), Str: StringRef(Str, SLen)));
1284}
1285
1286LLVMMetadataRef LLVMMDNodeInContext2(LLVMContextRef C, LLVMMetadataRef *MDs,
1287 size_t Count) {
1288 return wrap(P: MDNode::get(Context&: *unwrap(P: C), MDs: ArrayRef<Metadata*>(unwrap(MDs), Count)));
1289}
1290
1291LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
1292 unsigned SLen) {
1293 LLVMContext &Context = *unwrap(P: C);
1294 return wrap(P: MetadataAsValue::get(
1295 Context, MD: MDString::get(Context, Str: StringRef(Str, SLen))));
1296}
1297
1298LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1299 return LLVMMDStringInContext(C: LLVMGetGlobalContext(), Str, SLen);
1300}
1301
1302LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
1303 unsigned Count) {
1304 LLVMContext &Context = *unwrap(P: C);
1305 SmallVector<Metadata *, 8> MDs;
1306 for (auto *OV : ArrayRef(Vals, Count)) {
1307 Value *V = unwrap(P: OV);
1308 Metadata *MD;
1309 if (!V)
1310 MD = nullptr;
1311 else if (auto *C = dyn_cast<Constant>(Val: V))
1312 MD = ConstantAsMetadata::get(C);
1313 else if (auto *MDV = dyn_cast<MetadataAsValue>(Val: V)) {
1314 MD = MDV->getMetadata();
1315 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1316 "outside of direct argument to call");
1317 } else {
1318 // This is function-local metadata. Pretend to make an MDNode.
1319 assert(Count == 1 &&
1320 "Expected only one operand to function-local metadata");
1321 return wrap(P: MetadataAsValue::get(Context, MD: LocalAsMetadata::get(Local: V)));
1322 }
1323
1324 MDs.push_back(Elt: MD);
1325 }
1326 return wrap(P: MetadataAsValue::get(Context, MD: MDNode::get(Context, MDs)));
1327}
1328
1329LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1330 return LLVMMDNodeInContext(C: LLVMGetGlobalContext(), Vals, Count);
1331}
1332
1333LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
1334 return wrap(P: MetadataAsValue::get(Context&: *unwrap(P: C), MD: unwrap(P: MD)));
1335}
1336
1337LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {
1338 auto *V = unwrap(P: Val);
1339 if (auto *C = dyn_cast<Constant>(Val: V))
1340 return wrap(P: ConstantAsMetadata::get(C));
1341 if (auto *MAV = dyn_cast<MetadataAsValue>(Val: V))
1342 return wrap(P: MAV->getMetadata());
1343 return wrap(P: ValueAsMetadata::get(V));
1344}
1345
1346const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1347 if (const auto *MD = dyn_cast<MetadataAsValue>(Val: unwrap(P: V)))
1348 if (const MDString *S = dyn_cast<MDString>(Val: MD->getMetadata())) {
1349 *Length = S->getString().size();
1350 return S->getString().data();
1351 }
1352 *Length = 0;
1353 return nullptr;
1354}
1355
1356unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
1357 auto *MD = unwrap<MetadataAsValue>(P: V);
1358 if (isa<ValueAsMetadata>(Val: MD->getMetadata()))
1359 return 1;
1360 return cast<MDNode>(Val: MD->getMetadata())->getNumOperands();
1361}
1362
1363LLVMNamedMDNodeRef LLVMGetFirstNamedMetadata(LLVMModuleRef M) {
1364 Module *Mod = unwrap(P: M);
1365 Module::named_metadata_iterator I = Mod->named_metadata_begin();
1366 if (I == Mod->named_metadata_end())
1367 return nullptr;
1368 return wrap(P: &*I);
1369}
1370
1371LLVMNamedMDNodeRef LLVMGetLastNamedMetadata(LLVMModuleRef M) {
1372 Module *Mod = unwrap(P: M);
1373 Module::named_metadata_iterator I = Mod->named_metadata_end();
1374 if (I == Mod->named_metadata_begin())
1375 return nullptr;
1376 return wrap(P: &*--I);
1377}
1378
1379LLVMNamedMDNodeRef LLVMGetNextNamedMetadata(LLVMNamedMDNodeRef NMD) {
1380 NamedMDNode *NamedNode = unwrap(P: NMD);
1381 Module::named_metadata_iterator I(NamedNode);
1382 if (++I == NamedNode->getParent()->named_metadata_end())
1383 return nullptr;
1384 return wrap(P: &*I);
1385}
1386
1387LLVMNamedMDNodeRef LLVMGetPreviousNamedMetadata(LLVMNamedMDNodeRef NMD) {
1388 NamedMDNode *NamedNode = unwrap(P: NMD);
1389 Module::named_metadata_iterator I(NamedNode);
1390 if (I == NamedNode->getParent()->named_metadata_begin())
1391 return nullptr;
1392 return wrap(P: &*--I);
1393}
1394
1395LLVMNamedMDNodeRef LLVMGetNamedMetadata(LLVMModuleRef M,
1396 const char *Name, size_t NameLen) {
1397 return wrap(P: unwrap(P: M)->getNamedMetadata(Name: StringRef(Name, NameLen)));
1398}
1399
1400LLVMNamedMDNodeRef LLVMGetOrInsertNamedMetadata(LLVMModuleRef M,
1401 const char *Name, size_t NameLen) {
1402 return wrap(P: unwrap(P: M)->getOrInsertNamedMetadata(Name: {Name, NameLen}));
1403}
1404
1405const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1406 NamedMDNode *NamedNode = unwrap(P: NMD);
1407 *NameLen = NamedNode->getName().size();
1408 return NamedNode->getName().data();
1409}
1410
1411void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
1412 auto *MD = unwrap<MetadataAsValue>(P: V);
1413 if (auto *MDV = dyn_cast<ValueAsMetadata>(Val: MD->getMetadata())) {
1414 *Dest = wrap(P: MDV->getValue());
1415 return;
1416 }
1417 const auto *N = cast<MDNode>(Val: MD->getMetadata());
1418 const unsigned numOperands = N->getNumOperands();
1419 LLVMContext &Context = unwrap(P: V)->getContext();
1420 for (unsigned i = 0; i < numOperands; i++)
1421 Dest[i] = getMDNodeOperandImpl(Context, N, Index: i);
1422}
1423
1424void LLVMReplaceMDNodeOperandWith(LLVMValueRef V, unsigned Index,
1425 LLVMMetadataRef Replacement) {
1426 auto *MD = cast<MetadataAsValue>(Val: unwrap(P: V));
1427 auto *N = cast<MDNode>(Val: MD->getMetadata());
1428 N->replaceOperandWith(I: Index, New: unwrap<Metadata>(P: Replacement));
1429}
1430
1431unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
1432 if (NamedMDNode *N = unwrap(P: M)->getNamedMetadata(Name)) {
1433 return N->getNumOperands();
1434 }
1435 return 0;
1436}
1437
1438void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
1439 LLVMValueRef *Dest) {
1440 NamedMDNode *N = unwrap(P: M)->getNamedMetadata(Name);
1441 if (!N)
1442 return;
1443 LLVMContext &Context = unwrap(P: M)->getContext();
1444 for (unsigned i=0;i<N->getNumOperands();i++)
1445 Dest[i] = wrap(P: MetadataAsValue::get(Context, MD: N->getOperand(i)));
1446}
1447
1448void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
1449 LLVMValueRef Val) {
1450 NamedMDNode *N = unwrap(P: M)->getOrInsertNamedMetadata(Name);
1451 if (!N)
1452 return;
1453 if (!Val)
1454 return;
1455 N->addOperand(M: extractMDNode(MAV: unwrap<MetadataAsValue>(P: Val)));
1456}
1457
1458const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
1459 if (!Length) return nullptr;
1460 StringRef S;
1461 if (const auto *I = dyn_cast<Instruction>(Val: unwrap(P: Val))) {
1462 if (const auto &DL = I->getDebugLoc()) {
1463 S = DL->getDirectory();
1464 }
1465 } else if (const auto *GV = dyn_cast<GlobalVariable>(Val: unwrap(P: Val))) {
1466 SmallVector<DIGlobalVariableExpression *, 1> GVEs;
1467 GV->getDebugInfo(GVs&: GVEs);
1468 if (GVEs.size())
1469 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1470 S = DGV->getDirectory();
1471 } else if (const auto *F = dyn_cast<Function>(Val: unwrap(P: Val))) {
1472 if (const DISubprogram *DSP = F->getSubprogram())
1473 S = DSP->getDirectory();
1474 } else {
1475 assert(0 && "Expected Instruction, GlobalVariable or Function");
1476 return nullptr;
1477 }
1478 *Length = S.size();
1479 return S.data();
1480}
1481
1482const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
1483 if (!Length) return nullptr;
1484 StringRef S;
1485 if (const auto *I = dyn_cast<Instruction>(Val: unwrap(P: Val))) {
1486 if (const auto &DL = I->getDebugLoc()) {
1487 S = DL->getFilename();
1488 }
1489 } else if (const auto *GV = dyn_cast<GlobalVariable>(Val: unwrap(P: Val))) {
1490 SmallVector<DIGlobalVariableExpression *, 1> GVEs;
1491 GV->getDebugInfo(GVs&: GVEs);
1492 if (GVEs.size())
1493 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1494 S = DGV->getFilename();
1495 } else if (const auto *F = dyn_cast<Function>(Val: unwrap(P: Val))) {
1496 if (const DISubprogram *DSP = F->getSubprogram())
1497 S = DSP->getFilename();
1498 } else {
1499 assert(0 && "Expected Instruction, GlobalVariable or Function");
1500 return nullptr;
1501 }
1502 *Length = S.size();
1503 return S.data();
1504}
1505
1506unsigned LLVMGetDebugLocLine(LLVMValueRef Val) {
1507 unsigned L = 0;
1508 if (const auto *I = dyn_cast<Instruction>(Val: unwrap(P: Val))) {
1509 if (const auto &DL = I->getDebugLoc()) {
1510 L = DL->getLine();
1511 }
1512 } else if (const auto *GV = dyn_cast<GlobalVariable>(Val: unwrap(P: Val))) {
1513 SmallVector<DIGlobalVariableExpression *, 1> GVEs;
1514 GV->getDebugInfo(GVs&: GVEs);
1515 if (GVEs.size())
1516 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1517 L = DGV->getLine();
1518 } else if (const auto *F = dyn_cast<Function>(Val: unwrap(P: Val))) {
1519 if (const DISubprogram *DSP = F->getSubprogram())
1520 L = DSP->getLine();
1521 } else {
1522 assert(0 && "Expected Instruction, GlobalVariable or Function");
1523 return -1;
1524 }
1525 return L;
1526}
1527
1528unsigned LLVMGetDebugLocColumn(LLVMValueRef Val) {
1529 unsigned C = 0;
1530 if (const auto *I = dyn_cast<Instruction>(Val: unwrap(P: Val)))
1531 if (const auto &DL = I->getDebugLoc())
1532 C = DL->getColumn();
1533 return C;
1534}
1535
1536/*--.. Operations on scalar constants ......................................--*/
1537
1538LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1539 LLVMBool SignExtend) {
1540 return wrap(P: ConstantInt::get(Ty: unwrap<IntegerType>(P: IntTy), V: N, IsSigned: SignExtend != 0));
1541}
1542
1543LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
1544 unsigned NumWords,
1545 const uint64_t Words[]) {
1546 IntegerType *Ty = unwrap<IntegerType>(P: IntTy);
1547 return wrap(P: ConstantInt::get(
1548 Context&: Ty->getContext(), V: APInt(Ty->getBitWidth(), ArrayRef(Words, NumWords))));
1549}
1550
1551LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
1552 uint8_t Radix) {
1553 return wrap(P: ConstantInt::get(Ty: unwrap<IntegerType>(P: IntTy), Str: StringRef(Str),
1554 Radix));
1555}
1556
1557LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
1558 unsigned SLen, uint8_t Radix) {
1559 return wrap(P: ConstantInt::get(Ty: unwrap<IntegerType>(P: IntTy), Str: StringRef(Str, SLen),
1560 Radix));
1561}
1562
1563LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
1564 return wrap(P: ConstantFP::get(Ty: unwrap(P: RealTy), V: N));
1565}
1566
1567LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
1568 return wrap(P: ConstantFP::get(Ty: unwrap(P: RealTy), Str: StringRef(Text)));
1569}
1570
1571LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
1572 unsigned SLen) {
1573 return wrap(P: ConstantFP::get(Ty: unwrap(P: RealTy), Str: StringRef(Str, SLen)));
1574}
1575
1576unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1577 return unwrap<ConstantInt>(P: ConstantVal)->getZExtValue();
1578}
1579
1580long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1581 return unwrap<ConstantInt>(P: ConstantVal)->getSExtValue();
1582}
1583
1584double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1585 ConstantFP *cFP = unwrap<ConstantFP>(P: ConstantVal) ;
1586 Type *Ty = cFP->getType();
1587
1588 if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
1589 Ty->isDoubleTy()) {
1590 *LosesInfo = false;
1591 return cFP->getValueAPF().convertToDouble();
1592 }
1593
1594 bool APFLosesInfo;
1595 APFloat APF = cFP->getValueAPF();
1596 APF.convert(ToSemantics: APFloat::IEEEdouble(), RM: APFloat::rmNearestTiesToEven, losesInfo: &APFLosesInfo);
1597 *LosesInfo = APFLosesInfo;
1598 return APF.convertToDouble();
1599}
1600
1601/*--.. Operations on composite constants ...................................--*/
1602
1603LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
1604 unsigned Length,
1605 LLVMBool DontNullTerminate) {
1606 /* Inverted the sense of AddNull because ', 0)' is a
1607 better mnemonic for null termination than ', 1)'. */
1608 return wrap(P: ConstantDataArray::getString(Context&: *unwrap(P: C), Initializer: StringRef(Str, Length),
1609 AddNull: DontNullTerminate == 0));
1610}
1611
1612LLVMValueRef LLVMConstStringInContext2(LLVMContextRef C, const char *Str,
1613 size_t Length,
1614 LLVMBool DontNullTerminate) {
1615 /* Inverted the sense of AddNull because ', 0)' is a
1616 better mnemonic for null termination than ', 1)'. */
1617 return wrap(P: ConstantDataArray::getString(Context&: *unwrap(P: C), Initializer: StringRef(Str, Length),
1618 AddNull: DontNullTerminate == 0));
1619}
1620
1621LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1622 LLVMBool DontNullTerminate) {
1623 return LLVMConstStringInContext(C: LLVMGetGlobalContext(), Str, Length,
1624 DontNullTerminate);
1625}
1626
1627LLVMValueRef LLVMGetAggregateElement(LLVMValueRef C, unsigned Idx) {
1628 return wrap(P: unwrap<Constant>(P: C)->getAggregateElement(Elt: Idx));
1629}
1630
1631LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1632 return wrap(P: unwrap<ConstantDataSequential>(P: C)->getElementAsConstant(i: idx));
1633}
1634
1635LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1636 return unwrap<ConstantDataSequential>(P: C)->isString();
1637}
1638
1639const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1640 StringRef Str = unwrap<ConstantDataSequential>(P: C)->getAsString();
1641 *Length = Str.size();
1642 return Str.data();
1643}
1644
1645const char *LLVMGetRawDataValues(LLVMValueRef C, size_t *SizeInBytes) {
1646 StringRef Str = unwrap<ConstantDataSequential>(P: C)->getRawDataValues();
1647 *SizeInBytes = Str.size();
1648 return Str.data();
1649}
1650
1651LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1652 LLVMValueRef *ConstantVals, unsigned Length) {
1653 ArrayRef<Constant*> V(unwrap<Constant>(Vals: ConstantVals, Length), Length);
1654 return wrap(P: ConstantArray::get(T: ArrayType::get(ElementType: unwrap(P: ElementTy), NumElements: Length), V));
1655}
1656
1657LLVMValueRef LLVMConstArray2(LLVMTypeRef ElementTy, LLVMValueRef *ConstantVals,
1658 uint64_t Length) {
1659 ArrayRef<Constant *> V(unwrap<Constant>(Vals: ConstantVals, Length), Length);
1660 return wrap(P: ConstantArray::get(T: ArrayType::get(ElementType: unwrap(P: ElementTy), NumElements: Length), V));
1661}
1662
1663LLVMValueRef LLVMConstDataArray(LLVMTypeRef ElementTy, const char *Data,
1664 size_t SizeInBytes) {
1665 Type *Ty = unwrap(P: ElementTy);
1666 size_t Len = SizeInBytes / (Ty->getPrimitiveSizeInBits() / 8);
1667 return wrap(P: ConstantDataArray::getRaw(Data: StringRef(Data, SizeInBytes), NumElements: Len, ElementTy: Ty));
1668}
1669
1670LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1671 LLVMValueRef *ConstantVals,
1672 unsigned Count, LLVMBool Packed) {
1673 Constant **Elements = unwrap<Constant>(Vals: ConstantVals, Length: Count);
1674 return wrap(P: ConstantStruct::getAnon(Ctx&: *unwrap(P: C), V: ArrayRef(Elements, Count),
1675 Packed: Packed != 0));
1676}
1677
1678LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1679 LLVMBool Packed) {
1680 return LLVMConstStructInContext(C: LLVMGetGlobalContext(), ConstantVals, Count,
1681 Packed);
1682}
1683
1684LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1685 LLVMValueRef *ConstantVals,
1686 unsigned Count) {
1687 Constant **Elements = unwrap<Constant>(Vals: ConstantVals, Length: Count);
1688 StructType *Ty = unwrap<StructType>(P: StructTy);
1689
1690 return wrap(P: ConstantStruct::get(T: Ty, V: ArrayRef(Elements, Count)));
1691}
1692
1693LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1694 return wrap(P: ConstantVector::get(
1695 V: ArrayRef(unwrap<Constant>(Vals: ScalarConstantVals, Length: Size), Size)));
1696}
1697
1698LLVMValueRef LLVMConstantPtrAuth(LLVMValueRef Ptr, LLVMValueRef Key,
1699 LLVMValueRef Disc, LLVMValueRef AddrDisc) {
1700 return wrap(P: ConstantPtrAuth::get(
1701 Ptr: unwrap<Constant>(P: Ptr), Key: unwrap<ConstantInt>(P: Key),
1702 Disc: unwrap<ConstantInt>(P: Disc), AddrDisc: unwrap<Constant>(P: AddrDisc)));
1703}
1704
1705/*-- Opcode mapping */
1706
1707static LLVMOpcode map_to_llvmopcode(int opcode)
1708{
1709 switch (opcode) {
1710 default: llvm_unreachable("Unhandled Opcode.");
1711#define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1712#include "llvm/IR/Instruction.def"
1713#undef HANDLE_INST
1714 }
1715}
1716
1717static int map_from_llvmopcode(LLVMOpcode code)
1718{
1719 switch (code) {
1720#define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1721#include "llvm/IR/Instruction.def"
1722#undef HANDLE_INST
1723 }
1724 llvm_unreachable("Unhandled Opcode.");
1725}
1726
1727/*-- GEP wrap flag conversions */
1728
1729static GEPNoWrapFlags mapFromLLVMGEPNoWrapFlags(LLVMGEPNoWrapFlags GEPFlags) {
1730 GEPNoWrapFlags NewGEPFlags;
1731 if ((GEPFlags & LLVMGEPFlagInBounds) != 0)
1732 NewGEPFlags |= GEPNoWrapFlags::inBounds();
1733 if ((GEPFlags & LLVMGEPFlagNUSW) != 0)
1734 NewGEPFlags |= GEPNoWrapFlags::noUnsignedSignedWrap();
1735 if ((GEPFlags & LLVMGEPFlagNUW) != 0)
1736 NewGEPFlags |= GEPNoWrapFlags::noUnsignedWrap();
1737
1738 return NewGEPFlags;
1739}
1740
1741static LLVMGEPNoWrapFlags mapToLLVMGEPNoWrapFlags(GEPNoWrapFlags GEPFlags) {
1742 LLVMGEPNoWrapFlags NewGEPFlags = 0;
1743 if (GEPFlags.isInBounds())
1744 NewGEPFlags |= LLVMGEPFlagInBounds;
1745 if (GEPFlags.hasNoUnsignedSignedWrap())
1746 NewGEPFlags |= LLVMGEPFlagNUSW;
1747 if (GEPFlags.hasNoUnsignedWrap())
1748 NewGEPFlags |= LLVMGEPFlagNUW;
1749
1750 return NewGEPFlags;
1751}
1752
1753/*--.. Constant expressions ................................................--*/
1754
1755LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
1756 return map_to_llvmopcode(opcode: unwrap<ConstantExpr>(P: ConstantVal)->getOpcode());
1757}
1758
1759LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
1760 return wrap(P: ConstantExpr::getAlignOf(Ty: unwrap(P: Ty)));
1761}
1762
1763LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
1764 return wrap(P: ConstantExpr::getSizeOf(Ty: unwrap(P: Ty)));
1765}
1766
1767LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
1768 return wrap(P: ConstantExpr::getNeg(C: unwrap<Constant>(P: ConstantVal)));
1769}
1770
1771LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
1772 return wrap(P: ConstantExpr::getNSWNeg(C: unwrap<Constant>(P: ConstantVal)));
1773}
1774
1775LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
1776 return wrap(P: ConstantExpr::getNeg(C: unwrap<Constant>(P: ConstantVal)));
1777}
1778
1779
1780LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
1781 return wrap(P: ConstantExpr::getNot(C: unwrap<Constant>(P: ConstantVal)));
1782}
1783
1784LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1785 return wrap(P: ConstantExpr::getAdd(C1: unwrap<Constant>(P: LHSConstant),
1786 C2: unwrap<Constant>(P: RHSConstant)));
1787}
1788
1789LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1790 LLVMValueRef RHSConstant) {
1791 return wrap(P: ConstantExpr::getNSWAdd(C1: unwrap<Constant>(P: LHSConstant),
1792 C2: unwrap<Constant>(P: RHSConstant)));
1793}
1794
1795LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1796 LLVMValueRef RHSConstant) {
1797 return wrap(P: ConstantExpr::getNUWAdd(C1: unwrap<Constant>(P: LHSConstant),
1798 C2: unwrap<Constant>(P: RHSConstant)));
1799}
1800
1801LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1802 return wrap(P: ConstantExpr::getSub(C1: unwrap<Constant>(P: LHSConstant),
1803 C2: unwrap<Constant>(P: RHSConstant)));
1804}
1805
1806LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1807 LLVMValueRef RHSConstant) {
1808 return wrap(P: ConstantExpr::getNSWSub(C1: unwrap<Constant>(P: LHSConstant),
1809 C2: unwrap<Constant>(P: RHSConstant)));
1810}
1811
1812LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1813 LLVMValueRef RHSConstant) {
1814 return wrap(P: ConstantExpr::getNUWSub(C1: unwrap<Constant>(P: LHSConstant),
1815 C2: unwrap<Constant>(P: RHSConstant)));
1816}
1817
1818LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1819 return wrap(P: ConstantExpr::getXor(C1: unwrap<Constant>(P: LHSConstant),
1820 C2: unwrap<Constant>(P: RHSConstant)));
1821}
1822
1823LLVMValueRef LLVMConstGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
1824 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1825 ArrayRef<Constant *> IdxList(unwrap<Constant>(Vals: ConstantIndices, Length: NumIndices),
1826 NumIndices);
1827 Constant *Val = unwrap<Constant>(P: ConstantVal);
1828 return wrap(P: ConstantExpr::getGetElementPtr(Ty: unwrap(P: Ty), C: Val, IdxList));
1829}
1830
1831LLVMValueRef LLVMConstInBoundsGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
1832 LLVMValueRef *ConstantIndices,
1833 unsigned NumIndices) {
1834 ArrayRef<Constant *> IdxList(unwrap<Constant>(Vals: ConstantIndices, Length: NumIndices),
1835 NumIndices);
1836 Constant *Val = unwrap<Constant>(P: ConstantVal);
1837 return wrap(P: ConstantExpr::getInBoundsGetElementPtr(Ty: unwrap(P: Ty), C: Val, IdxList));
1838}
1839
1840LLVMValueRef LLVMConstGEPWithNoWrapFlags(LLVMTypeRef Ty,
1841 LLVMValueRef ConstantVal,
1842 LLVMValueRef *ConstantIndices,
1843 unsigned NumIndices,
1844 LLVMGEPNoWrapFlags NoWrapFlags) {
1845 ArrayRef<Constant *> IdxList(unwrap<Constant>(Vals: ConstantIndices, Length: NumIndices),
1846 NumIndices);
1847 Constant *Val = unwrap<Constant>(P: ConstantVal);
1848 return wrap(P: ConstantExpr::getGetElementPtr(
1849 Ty: unwrap(P: Ty), C: Val, IdxList, NW: mapFromLLVMGEPNoWrapFlags(GEPFlags: NoWrapFlags)));
1850}
1851
1852LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1853 return wrap(P: ConstantExpr::getTrunc(C: unwrap<Constant>(P: ConstantVal),
1854 Ty: unwrap(P: ToType)));
1855}
1856
1857LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1858 return wrap(P: ConstantExpr::getPtrToInt(C: unwrap<Constant>(P: ConstantVal),
1859 Ty: unwrap(P: ToType)));
1860}
1861
1862LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1863 return wrap(P: ConstantExpr::getIntToPtr(C: unwrap<Constant>(P: ConstantVal),
1864 Ty: unwrap(P: ToType)));
1865}
1866
1867LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1868 return wrap(P: ConstantExpr::getBitCast(C: unwrap<Constant>(P: ConstantVal),
1869 Ty: unwrap(P: ToType)));
1870}
1871
1872LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1873 LLVMTypeRef ToType) {
1874 return wrap(P: ConstantExpr::getAddrSpaceCast(C: unwrap<Constant>(P: ConstantVal),
1875 Ty: unwrap(P: ToType)));
1876}
1877
1878LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1879 LLVMTypeRef ToType) {
1880 return wrap(P: ConstantExpr::getTruncOrBitCast(C: unwrap<Constant>(P: ConstantVal),
1881 Ty: unwrap(P: ToType)));
1882}
1883
1884LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1885 LLVMTypeRef ToType) {
1886 return wrap(P: ConstantExpr::getPointerCast(C: unwrap<Constant>(P: ConstantVal),
1887 Ty: unwrap(P: ToType)));
1888}
1889
1890LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1891 LLVMValueRef IndexConstant) {
1892 return wrap(P: ConstantExpr::getExtractElement(Vec: unwrap<Constant>(P: VectorConstant),
1893 Idx: unwrap<Constant>(P: IndexConstant)));
1894}
1895
1896LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1897 LLVMValueRef ElementValueConstant,
1898 LLVMValueRef IndexConstant) {
1899 return wrap(P: ConstantExpr::getInsertElement(Vec: unwrap<Constant>(P: VectorConstant),
1900 Elt: unwrap<Constant>(P: ElementValueConstant),
1901 Idx: unwrap<Constant>(P: IndexConstant)));
1902}
1903
1904LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1905 LLVMValueRef VectorBConstant,
1906 LLVMValueRef MaskConstant) {
1907 SmallVector<int, 16> IntMask;
1908 ShuffleVectorInst::getShuffleMask(Mask: unwrap<Constant>(P: MaskConstant), Result&: IntMask);
1909 return wrap(P: ConstantExpr::getShuffleVector(V1: unwrap<Constant>(P: VectorAConstant),
1910 V2: unwrap<Constant>(P: VectorBConstant),
1911 Mask: IntMask));
1912}
1913
1914LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1915 const char *Constraints,
1916 LLVMBool HasSideEffects,
1917 LLVMBool IsAlignStack) {
1918 return wrap(P: InlineAsm::get(Ty: dyn_cast<FunctionType>(Val: unwrap(P: Ty)), AsmString,
1919 Constraints, hasSideEffects: HasSideEffects, isAlignStack: IsAlignStack));
1920}
1921
1922LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1923 return wrap(P: BlockAddress::get(F: unwrap<Function>(P: F), BB: unwrap(P: BB)));
1924}
1925
1926LLVMValueRef LLVMGetBlockAddressFunction(LLVMValueRef BlockAddr) {
1927 return wrap(P: unwrap<BlockAddress>(P: BlockAddr)->getFunction());
1928}
1929
1930LLVMBasicBlockRef LLVMGetBlockAddressBasicBlock(LLVMValueRef BlockAddr) {
1931 return wrap(P: unwrap<BlockAddress>(P: BlockAddr)->getBasicBlock());
1932}
1933
1934/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1935
1936LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1937 return wrap(P: unwrap<GlobalValue>(P: Global)->getParent());
1938}
1939
1940LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1941 return unwrap<GlobalValue>(P: Global)->isDeclaration();
1942}
1943
1944LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1945 switch (unwrap<GlobalValue>(P: Global)->getLinkage()) {
1946 case GlobalValue::ExternalLinkage:
1947 return LLVMExternalLinkage;
1948 case GlobalValue::AvailableExternallyLinkage:
1949 return LLVMAvailableExternallyLinkage;
1950 case GlobalValue::LinkOnceAnyLinkage:
1951 return LLVMLinkOnceAnyLinkage;
1952 case GlobalValue::LinkOnceODRLinkage:
1953 return LLVMLinkOnceODRLinkage;
1954 case GlobalValue::WeakAnyLinkage:
1955 return LLVMWeakAnyLinkage;
1956 case GlobalValue::WeakODRLinkage:
1957 return LLVMWeakODRLinkage;
1958 case GlobalValue::AppendingLinkage:
1959 return LLVMAppendingLinkage;
1960 case GlobalValue::InternalLinkage:
1961 return LLVMInternalLinkage;
1962 case GlobalValue::PrivateLinkage:
1963 return LLVMPrivateLinkage;
1964 case GlobalValue::ExternalWeakLinkage:
1965 return LLVMExternalWeakLinkage;
1966 case GlobalValue::CommonLinkage:
1967 return LLVMCommonLinkage;
1968 }
1969
1970 llvm_unreachable("Invalid GlobalValue linkage!");
1971}
1972
1973void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1974 GlobalValue *GV = unwrap<GlobalValue>(P: Global);
1975
1976 switch (Linkage) {
1977 case LLVMExternalLinkage:
1978 GV->setLinkage(GlobalValue::ExternalLinkage);
1979 break;
1980 case LLVMAvailableExternallyLinkage:
1981 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1982 break;
1983 case LLVMLinkOnceAnyLinkage:
1984 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1985 break;
1986 case LLVMLinkOnceODRLinkage:
1987 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1988 break;
1989 case LLVMLinkOnceODRAutoHideLinkage:
1990 LLVM_DEBUG(
1991 errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1992 "longer supported.");
1993 break;
1994 case LLVMWeakAnyLinkage:
1995 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1996 break;
1997 case LLVMWeakODRLinkage:
1998 GV->setLinkage(GlobalValue::WeakODRLinkage);
1999 break;
2000 case LLVMAppendingLinkage:
2001 GV->setLinkage(GlobalValue::AppendingLinkage);
2002 break;
2003 case LLVMInternalLinkage:
2004 GV->setLinkage(GlobalValue::InternalLinkage);
2005 break;
2006 case LLVMPrivateLinkage:
2007 GV->setLinkage(GlobalValue::PrivateLinkage);
2008 break;
2009 case LLVMLinkerPrivateLinkage:
2010 GV->setLinkage(GlobalValue::PrivateLinkage);
2011 break;
2012 case LLVMLinkerPrivateWeakLinkage:
2013 GV->setLinkage(GlobalValue::PrivateLinkage);
2014 break;
2015 case LLVMDLLImportLinkage:
2016 LLVM_DEBUG(
2017 errs()
2018 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
2019 break;
2020 case LLVMDLLExportLinkage:
2021 LLVM_DEBUG(
2022 errs()
2023 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
2024 break;
2025 case LLVMExternalWeakLinkage:
2026 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
2027 break;
2028 case LLVMGhostLinkage:
2029 LLVM_DEBUG(
2030 errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
2031 break;
2032 case LLVMCommonLinkage:
2033 GV->setLinkage(GlobalValue::CommonLinkage);
2034 break;
2035 }
2036}
2037
2038const char *LLVMGetSection(LLVMValueRef Global) {
2039 // Using .data() is safe because of how GlobalObject::setSection is
2040 // implemented.
2041 return unwrap<GlobalValue>(P: Global)->getSection().data();
2042}
2043
2044void LLVMSetSection(LLVMValueRef Global, const char *Section) {
2045 unwrap<GlobalObject>(P: Global)->setSection(Section);
2046}
2047
2048LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
2049 return static_cast<LLVMVisibility>(
2050 unwrap<GlobalValue>(P: Global)->getVisibility());
2051}
2052
2053void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
2054 unwrap<GlobalValue>(P: Global)
2055 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
2056}
2057
2058LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
2059 return static_cast<LLVMDLLStorageClass>(
2060 unwrap<GlobalValue>(P: Global)->getDLLStorageClass());
2061}
2062
2063void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
2064 unwrap<GlobalValue>(P: Global)->setDLLStorageClass(
2065 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
2066}
2067
2068LLVMUnnamedAddr LLVMGetUnnamedAddress(LLVMValueRef Global) {
2069 switch (unwrap<GlobalValue>(P: Global)->getUnnamedAddr()) {
2070 case GlobalVariable::UnnamedAddr::None:
2071 return LLVMNoUnnamedAddr;
2072 case GlobalVariable::UnnamedAddr::Local:
2073 return LLVMLocalUnnamedAddr;
2074 case GlobalVariable::UnnamedAddr::Global:
2075 return LLVMGlobalUnnamedAddr;
2076 }
2077 llvm_unreachable("Unknown UnnamedAddr kind!");
2078}
2079
2080void LLVMSetUnnamedAddress(LLVMValueRef Global, LLVMUnnamedAddr UnnamedAddr) {
2081 GlobalValue *GV = unwrap<GlobalValue>(P: Global);
2082
2083 switch (UnnamedAddr) {
2084 case LLVMNoUnnamedAddr:
2085 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);
2086 case LLVMLocalUnnamedAddr:
2087 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);
2088 case LLVMGlobalUnnamedAddr:
2089 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);
2090 }
2091}
2092
2093LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
2094 return unwrap<GlobalValue>(P: Global)->hasGlobalUnnamedAddr();
2095}
2096
2097void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
2098 unwrap<GlobalValue>(P: Global)->setUnnamedAddr(
2099 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
2100 : GlobalValue::UnnamedAddr::None);
2101}
2102
2103LLVMTypeRef LLVMGlobalGetValueType(LLVMValueRef Global) {
2104 return wrap(P: unwrap<GlobalValue>(P: Global)->getValueType());
2105}
2106
2107/*--.. Operations on global variables, load and store instructions .........--*/
2108
2109unsigned LLVMGetAlignment(LLVMValueRef V) {
2110 Value *P = unwrap(P: V);
2111 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Val: P))
2112 return GV->getAlign() ? GV->getAlign()->value() : 0;
2113 if (Function *F = dyn_cast<Function>(Val: P))
2114 return F->getAlign() ? F->getAlign()->value() : 0;
2115 if (AllocaInst *AI = dyn_cast<AllocaInst>(Val: P))
2116 return AI->getAlign().value();
2117 if (LoadInst *LI = dyn_cast<LoadInst>(Val: P))
2118 return LI->getAlign().value();
2119 if (StoreInst *SI = dyn_cast<StoreInst>(Val: P))
2120 return SI->getAlign().value();
2121 if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(Val: P))
2122 return RMWI->getAlign().value();
2123 if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(Val: P))
2124 return CXI->getAlign().value();
2125
2126 llvm_unreachable(
2127 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2128 "and AtomicCmpXchgInst have alignment");
2129}
2130
2131void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2132 Value *P = unwrap(P: V);
2133 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Val: P))
2134 GV->setAlignment(MaybeAlign(Bytes));
2135 else if (Function *F = dyn_cast<Function>(Val: P))
2136 F->setAlignment(MaybeAlign(Bytes));
2137 else if (AllocaInst *AI = dyn_cast<AllocaInst>(Val: P))
2138 AI->setAlignment(Align(Bytes));
2139 else if (LoadInst *LI = dyn_cast<LoadInst>(Val: P))
2140 LI->setAlignment(Align(Bytes));
2141 else if (StoreInst *SI = dyn_cast<StoreInst>(Val: P))
2142 SI->setAlignment(Align(Bytes));
2143 else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(Val: P))
2144 RMWI->setAlignment(Align(Bytes));
2145 else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(Val: P))
2146 CXI->setAlignment(Align(Bytes));
2147 else
2148 llvm_unreachable(
2149 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2150 "and AtomicCmpXchgInst have alignment");
2151}
2152
2153LLVMValueMetadataEntry *LLVMGlobalCopyAllMetadata(LLVMValueRef Value,
2154 size_t *NumEntries) {
2155 return llvm_getMetadata(NumEntries, AccessMD: [&Value](MetadataEntries &Entries) {
2156 Entries.clear();
2157 if (Instruction *Instr = dyn_cast<Instruction>(Val: unwrap(P: Value))) {
2158 Instr->getAllMetadata(MDs&: Entries);
2159 } else {
2160 unwrap<GlobalObject>(P: Value)->getAllMetadata(MDs&: Entries);
2161 }
2162 });
2163}
2164
2165unsigned LLVMValueMetadataEntriesGetKind(LLVMValueMetadataEntry *Entries,
2166 unsigned Index) {
2167 LLVMOpaqueValueMetadataEntry MVE =
2168 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2169 return MVE.Kind;
2170}
2171
2172LLVMMetadataRef
2173LLVMValueMetadataEntriesGetMetadata(LLVMValueMetadataEntry *Entries,
2174 unsigned Index) {
2175 LLVMOpaqueValueMetadataEntry MVE =
2176 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2177 return MVE.Metadata;
2178}
2179
2180void LLVMDisposeValueMetadataEntries(LLVMValueMetadataEntry *Entries) {
2181 free(ptr: Entries);
2182}
2183
2184void LLVMGlobalSetMetadata(LLVMValueRef Global, unsigned Kind,
2185 LLVMMetadataRef MD) {
2186 unwrap<GlobalObject>(P: Global)->setMetadata(KindID: Kind, Node: unwrap<MDNode>(P: MD));
2187}
2188
2189void LLVMGlobalEraseMetadata(LLVMValueRef Global, unsigned Kind) {
2190 unwrap<GlobalObject>(P: Global)->eraseMetadata(KindID: Kind);
2191}
2192
2193void LLVMGlobalClearMetadata(LLVMValueRef Global) {
2194 unwrap<GlobalObject>(P: Global)->clearMetadata();
2195}
2196
2197/*--.. Operations on global variables ......................................--*/
2198
2199LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
2200 return wrap(P: new GlobalVariable(*unwrap(P: M), unwrap(P: Ty), false,
2201 GlobalValue::ExternalLinkage, nullptr, Name));
2202}
2203
2204LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
2205 const char *Name,
2206 unsigned AddressSpace) {
2207 return wrap(P: new GlobalVariable(*unwrap(P: M), unwrap(P: Ty), false,
2208 GlobalValue::ExternalLinkage, nullptr, Name,
2209 nullptr, GlobalVariable::NotThreadLocal,
2210 AddressSpace));
2211}
2212
2213LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
2214 return wrap(P: unwrap(P: M)->getNamedGlobal(Name));
2215}
2216
2217LLVMValueRef LLVMGetNamedGlobalWithLength(LLVMModuleRef M, const char *Name,
2218 size_t Length) {
2219 return wrap(P: unwrap(P: M)->getNamedGlobal(Name: StringRef(Name, Length)));
2220}
2221
2222LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
2223 Module *Mod = unwrap(P: M);
2224 Module::global_iterator I = Mod->global_begin();
2225 if (I == Mod->global_end())
2226 return nullptr;
2227 return wrap(P: &*I);
2228}
2229
2230LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
2231 Module *Mod = unwrap(P: M);
2232 Module::global_iterator I = Mod->global_end();
2233 if (I == Mod->global_begin())
2234 return nullptr;
2235 return wrap(P: &*--I);
2236}
2237
2238LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
2239 GlobalVariable *GV = unwrap<GlobalVariable>(P: GlobalVar);
2240 Module::global_iterator I(GV);
2241 if (++I == GV->getParent()->global_end())
2242 return nullptr;
2243 return wrap(P: &*I);
2244}
2245
2246LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
2247 GlobalVariable *GV = unwrap<GlobalVariable>(P: GlobalVar);
2248 Module::global_iterator I(GV);
2249 if (I == GV->getParent()->global_begin())
2250 return nullptr;
2251 return wrap(P: &*--I);
2252}
2253
2254void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
2255 unwrap<GlobalVariable>(P: GlobalVar)->eraseFromParent();
2256}
2257
2258LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
2259 GlobalVariable* GV = unwrap<GlobalVariable>(P: GlobalVar);
2260 if ( !GV->hasInitializer() )
2261 return nullptr;
2262 return wrap(P: GV->getInitializer());
2263}
2264
2265void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
2266 unwrap<GlobalVariable>(P: GlobalVar)->setInitializer(
2267 ConstantVal ? unwrap<Constant>(P: ConstantVal) : nullptr);
2268}
2269
2270LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
2271 return unwrap<GlobalVariable>(P: GlobalVar)->isThreadLocal();
2272}
2273
2274void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
2275 unwrap<GlobalVariable>(P: GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2276}
2277
2278LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
2279 return unwrap<GlobalVariable>(P: GlobalVar)->isConstant();
2280}
2281
2282void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
2283 unwrap<GlobalVariable>(P: GlobalVar)->setConstant(IsConstant != 0);
2284}
2285
2286LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
2287 switch (unwrap<GlobalVariable>(P: GlobalVar)->getThreadLocalMode()) {
2288 case GlobalVariable::NotThreadLocal:
2289 return LLVMNotThreadLocal;
2290 case GlobalVariable::GeneralDynamicTLSModel:
2291 return LLVMGeneralDynamicTLSModel;
2292 case GlobalVariable::LocalDynamicTLSModel:
2293 return LLVMLocalDynamicTLSModel;
2294 case GlobalVariable::InitialExecTLSModel:
2295 return LLVMInitialExecTLSModel;
2296 case GlobalVariable::LocalExecTLSModel:
2297 return LLVMLocalExecTLSModel;
2298 }
2299
2300 llvm_unreachable("Invalid GlobalVariable thread local mode");
2301}
2302
2303void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
2304 GlobalVariable *GV = unwrap<GlobalVariable>(P: GlobalVar);
2305
2306 switch (Mode) {
2307 case LLVMNotThreadLocal:
2308 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
2309 break;
2310 case LLVMGeneralDynamicTLSModel:
2311 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
2312 break;
2313 case LLVMLocalDynamicTLSModel:
2314 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
2315 break;
2316 case LLVMInitialExecTLSModel:
2317 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
2318 break;
2319 case LLVMLocalExecTLSModel:
2320 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
2321 break;
2322 }
2323}
2324
2325LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
2326 return unwrap<GlobalVariable>(P: GlobalVar)->isExternallyInitialized();
2327}
2328
2329void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
2330 unwrap<GlobalVariable>(P: GlobalVar)->setExternallyInitialized(IsExtInit);
2331}
2332
2333/*--.. Operations on aliases ......................................--*/
2334
2335LLVMValueRef LLVMAddAlias2(LLVMModuleRef M, LLVMTypeRef ValueTy,
2336 unsigned AddrSpace, LLVMValueRef Aliasee,
2337 const char *Name) {
2338 return wrap(P: GlobalAlias::create(Ty: unwrap(P: ValueTy), AddressSpace: AddrSpace,
2339 Linkage: GlobalValue::ExternalLinkage, Name,
2340 Aliasee: unwrap<Constant>(P: Aliasee), Parent: unwrap(P: M)));
2341}
2342
2343LLVMValueRef LLVMGetNamedGlobalAlias(LLVMModuleRef M,
2344 const char *Name, size_t NameLen) {
2345 return wrap(P: unwrap(P: M)->getNamedAlias(Name: StringRef(Name, NameLen)));
2346}
2347
2348LLVMValueRef LLVMGetFirstGlobalAlias(LLVMModuleRef M) {
2349 Module *Mod = unwrap(P: M);
2350 Module::alias_iterator I = Mod->alias_begin();
2351 if (I == Mod->alias_end())
2352 return nullptr;
2353 return wrap(P: &*I);
2354}
2355
2356LLVMValueRef LLVMGetLastGlobalAlias(LLVMModuleRef M) {
2357 Module *Mod = unwrap(P: M);
2358 Module::alias_iterator I = Mod->alias_end();
2359 if (I == Mod->alias_begin())
2360 return nullptr;
2361 return wrap(P: &*--I);
2362}
2363
2364LLVMValueRef LLVMGetNextGlobalAlias(LLVMValueRef GA) {
2365 GlobalAlias *Alias = unwrap<GlobalAlias>(P: GA);
2366 Module::alias_iterator I(Alias);
2367 if (++I == Alias->getParent()->alias_end())
2368 return nullptr;
2369 return wrap(P: &*I);
2370}
2371
2372LLVMValueRef LLVMGetPreviousGlobalAlias(LLVMValueRef GA) {
2373 GlobalAlias *Alias = unwrap<GlobalAlias>(P: GA);
2374 Module::alias_iterator I(Alias);
2375 if (I == Alias->getParent()->alias_begin())
2376 return nullptr;
2377 return wrap(P: &*--I);
2378}
2379
2380LLVMValueRef LLVMAliasGetAliasee(LLVMValueRef Alias) {
2381 return wrap(P: unwrap<GlobalAlias>(P: Alias)->getAliasee());
2382}
2383
2384void LLVMAliasSetAliasee(LLVMValueRef Alias, LLVMValueRef Aliasee) {
2385 unwrap<GlobalAlias>(P: Alias)->setAliasee(unwrap<Constant>(P: Aliasee));
2386}
2387
2388/*--.. Operations on functions .............................................--*/
2389
2390LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
2391 LLVMTypeRef FunctionTy) {
2392 return wrap(P: Function::Create(Ty: unwrap<FunctionType>(P: FunctionTy),
2393 Linkage: GlobalValue::ExternalLinkage, N: Name, M: unwrap(P: M)));
2394}
2395
2396LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
2397 return wrap(P: unwrap(P: M)->getFunction(Name));
2398}
2399
2400LLVMValueRef LLVMGetNamedFunctionWithLength(LLVMModuleRef M, const char *Name,
2401 size_t Length) {
2402 return wrap(P: unwrap(P: M)->getFunction(Name: StringRef(Name, Length)));
2403}
2404
2405LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
2406 Module *Mod = unwrap(P: M);
2407 Module::iterator I = Mod->begin();
2408 if (I == Mod->end())
2409 return nullptr;
2410 return wrap(P: &*I);
2411}
2412
2413LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
2414 Module *Mod = unwrap(P: M);
2415 Module::iterator I = Mod->end();
2416 if (I == Mod->begin())
2417 return nullptr;
2418 return wrap(P: &*--I);
2419}
2420
2421LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
2422 Function *Func = unwrap<Function>(P: Fn);
2423 Module::iterator I(Func);
2424 if (++I == Func->getParent()->end())
2425 return nullptr;
2426 return wrap(P: &*I);
2427}
2428
2429LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
2430 Function *Func = unwrap<Function>(P: Fn);
2431 Module::iterator I(Func);
2432 if (I == Func->getParent()->begin())
2433 return nullptr;
2434 return wrap(P: &*--I);
2435}
2436
2437void LLVMDeleteFunction(LLVMValueRef Fn) {
2438 unwrap<Function>(P: Fn)->eraseFromParent();
2439}
2440
2441LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
2442 return unwrap<Function>(P: Fn)->hasPersonalityFn();
2443}
2444
2445LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
2446 return wrap(P: unwrap<Function>(P: Fn)->getPersonalityFn());
2447}
2448
2449void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
2450 unwrap<Function>(P: Fn)->setPersonalityFn(
2451 PersonalityFn ? unwrap<Constant>(P: PersonalityFn) : nullptr);
2452}
2453
2454unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
2455 if (Function *F = dyn_cast<Function>(Val: unwrap(P: Fn)))
2456 return F->getIntrinsicID();
2457 return 0;
2458}
2459
2460static Intrinsic::ID llvm_map_to_intrinsic_id(unsigned ID) {
2461 assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2462 return llvm::Intrinsic::ID(ID);
2463}
2464
2465LLVMValueRef LLVMGetIntrinsicDeclaration(LLVMModuleRef Mod,
2466 unsigned ID,
2467 LLVMTypeRef *ParamTypes,
2468 size_t ParamCount) {
2469 ArrayRef<Type*> Tys(unwrap(Tys: ParamTypes), ParamCount);
2470 auto IID = llvm_map_to_intrinsic_id(ID);
2471 return wrap(P: llvm::Intrinsic::getOrInsertDeclaration(M: unwrap(P: Mod), id: IID, Tys));
2472}
2473
2474const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2475 auto IID = llvm_map_to_intrinsic_id(ID);
2476 auto Str = llvm::Intrinsic::getName(id: IID);
2477 *NameLength = Str.size();
2478 return Str.data();
2479}
2480
2481LLVMTypeRef LLVMIntrinsicGetType(LLVMContextRef Ctx, unsigned ID,
2482 LLVMTypeRef *ParamTypes, size_t ParamCount) {
2483 auto IID = llvm_map_to_intrinsic_id(ID);
2484 ArrayRef<Type*> Tys(unwrap(Tys: ParamTypes), ParamCount);
2485 return wrap(P: llvm::Intrinsic::getType(Context&: *unwrap(P: Ctx), id: IID, Tys));
2486}
2487
2488char *LLVMIntrinsicCopyOverloadedName(unsigned ID, LLVMTypeRef *ParamTypes,
2489 size_t ParamCount, size_t *NameLength) {
2490 auto IID = llvm_map_to_intrinsic_id(ID);
2491 ArrayRef<Type*> Tys(unwrap(Tys: ParamTypes), ParamCount);
2492 auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(Id: IID, Tys);
2493 *NameLength = Str.length();
2494 return strdup(s: Str.c_str());
2495}
2496
2497char *LLVMIntrinsicCopyOverloadedName2(LLVMModuleRef Mod, unsigned ID,
2498 LLVMTypeRef *ParamTypes,
2499 size_t ParamCount, size_t *NameLength) {
2500 auto IID = llvm_map_to_intrinsic_id(ID);
2501 ArrayRef<Type *> Tys(unwrap(Tys: ParamTypes), ParamCount);
2502 auto Str = llvm::Intrinsic::getName(Id: IID, Tys, M: unwrap(P: Mod));
2503 *NameLength = Str.length();
2504 return strdup(s: Str.c_str());
2505}
2506
2507unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2508 return Intrinsic::lookupIntrinsicID(Name: {Name, NameLen});
2509}
2510
2511LLVMBool LLVMIntrinsicIsOverloaded(unsigned ID) {
2512 auto IID = llvm_map_to_intrinsic_id(ID);
2513 return llvm::Intrinsic::isOverloaded(id: IID);
2514}
2515
2516unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
2517 return unwrap<Function>(P: Fn)->getCallingConv();
2518}
2519
2520void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
2521 return unwrap<Function>(P: Fn)->setCallingConv(
2522 static_cast<CallingConv::ID>(CC));
2523}
2524
2525const char *LLVMGetGC(LLVMValueRef Fn) {
2526 Function *F = unwrap<Function>(P: Fn);
2527 return F->hasGC()? F->getGC().c_str() : nullptr;
2528}
2529
2530void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2531 Function *F = unwrap<Function>(P: Fn);
2532 if (GC)
2533 F->setGC(GC);
2534 else
2535 F->clearGC();
2536}
2537
2538LLVMValueRef LLVMGetPrefixData(LLVMValueRef Fn) {
2539 Function *F = unwrap<Function>(P: Fn);
2540 return wrap(P: F->getPrefixData());
2541}
2542
2543LLVMBool LLVMHasPrefixData(LLVMValueRef Fn) {
2544 Function *F = unwrap<Function>(P: Fn);
2545 return F->hasPrefixData();
2546}
2547
2548void LLVMSetPrefixData(LLVMValueRef Fn, LLVMValueRef prefixData) {
2549 Function *F = unwrap<Function>(P: Fn);
2550 Constant *prefix = unwrap<Constant>(P: prefixData);
2551 F->setPrefixData(prefix);
2552}
2553
2554LLVMValueRef LLVMGetPrologueData(LLVMValueRef Fn) {
2555 Function *F = unwrap<Function>(P: Fn);
2556 return wrap(P: F->getPrologueData());
2557}
2558
2559LLVMBool LLVMHasPrologueData(LLVMValueRef Fn) {
2560 Function *F = unwrap<Function>(P: Fn);
2561 return F->hasPrologueData();
2562}
2563
2564void LLVMSetPrologueData(LLVMValueRef Fn, LLVMValueRef prologueData) {
2565 Function *F = unwrap<Function>(P: Fn);
2566 Constant *prologue = unwrap<Constant>(P: prologueData);
2567 F->setPrologueData(prologue);
2568}
2569
2570void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2571 LLVMAttributeRef A) {
2572 unwrap<Function>(P: F)->addAttributeAtIndex(i: Idx, Attr: unwrap(Attr: A));
2573}
2574
2575unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
2576 auto AS = unwrap<Function>(P: F)->getAttributes().getAttributes(Index: Idx);
2577 return AS.getNumAttributes();
2578}
2579
2580void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2581 LLVMAttributeRef *Attrs) {
2582 auto AS = unwrap<Function>(P: F)->getAttributes().getAttributes(Index: Idx);
2583 for (auto A : AS)
2584 *Attrs++ = wrap(Attr: A);
2585}
2586
2587LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
2588 LLVMAttributeIndex Idx,
2589 unsigned KindID) {
2590 return wrap(Attr: unwrap<Function>(P: F)->getAttributeAtIndex(
2591 i: Idx, Kind: (Attribute::AttrKind)KindID));
2592}
2593
2594LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
2595 LLVMAttributeIndex Idx,
2596 const char *K, unsigned KLen) {
2597 return wrap(
2598 Attr: unwrap<Function>(P: F)->getAttributeAtIndex(i: Idx, Kind: StringRef(K, KLen)));
2599}
2600
2601void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2602 unsigned KindID) {
2603 unwrap<Function>(P: F)->removeAttributeAtIndex(i: Idx, Kind: (Attribute::AttrKind)KindID);
2604}
2605
2606void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2607 const char *K, unsigned KLen) {
2608 unwrap<Function>(P: F)->removeAttributeAtIndex(i: Idx, Kind: StringRef(K, KLen));
2609}
2610
2611void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
2612 const char *V) {
2613 Function *Func = unwrap<Function>(P: Fn);
2614 Attribute Attr = Attribute::get(Context&: Func->getContext(), Kind: A, Val: V);
2615 Func->addFnAttr(Attr);
2616}
2617
2618/*--.. Operations on parameters ............................................--*/
2619
2620unsigned LLVMCountParams(LLVMValueRef FnRef) {
2621 // This function is strictly redundant to
2622 // LLVMCountParamTypes(LLVMGlobalGetValueType(FnRef))
2623 return unwrap<Function>(P: FnRef)->arg_size();
2624}
2625
2626void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2627 Function *Fn = unwrap<Function>(P: FnRef);
2628 for (Argument &A : Fn->args())
2629 *ParamRefs++ = wrap(P: &A);
2630}
2631
2632LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
2633 Function *Fn = unwrap<Function>(P: FnRef);
2634 return wrap(P: &Fn->arg_begin()[index]);
2635}
2636
2637LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
2638 return wrap(P: unwrap<Argument>(P: V)->getParent());
2639}
2640
2641LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
2642 Function *Func = unwrap<Function>(P: Fn);
2643 Function::arg_iterator I = Func->arg_begin();
2644 if (I == Func->arg_end())
2645 return nullptr;
2646 return wrap(P: &*I);
2647}
2648
2649LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
2650 Function *Func = unwrap<Function>(P: Fn);
2651 Function::arg_iterator I = Func->arg_end();
2652 if (I == Func->arg_begin())
2653 return nullptr;
2654 return wrap(P: &*--I);
2655}
2656
2657LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
2658 Argument *A = unwrap<Argument>(P: Arg);
2659 Function *Fn = A->getParent();
2660 if (A->getArgNo() + 1 >= Fn->arg_size())
2661 return nullptr;
2662 return wrap(P: &Fn->arg_begin()[A->getArgNo() + 1]);
2663}
2664
2665LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
2666 Argument *A = unwrap<Argument>(P: Arg);
2667 if (A->getArgNo() == 0)
2668 return nullptr;
2669 return wrap(P: &A->getParent()->arg_begin()[A->getArgNo() - 1]);
2670}
2671
2672void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
2673 Argument *A = unwrap<Argument>(P: Arg);
2674 A->addAttr(Attr: Attribute::getWithAlignment(Context&: A->getContext(), Alignment: Align(align)));
2675}
2676
2677/*--.. Operations on ifuncs ................................................--*/
2678
2679LLVMValueRef LLVMAddGlobalIFunc(LLVMModuleRef M,
2680 const char *Name, size_t NameLen,
2681 LLVMTypeRef Ty, unsigned AddrSpace,
2682 LLVMValueRef Resolver) {
2683 return wrap(P: GlobalIFunc::create(Ty: unwrap(P: Ty), AddressSpace: AddrSpace,
2684 Linkage: GlobalValue::ExternalLinkage,
2685 Name: StringRef(Name, NameLen),
2686 Resolver: unwrap<Constant>(P: Resolver), Parent: unwrap(P: M)));
2687}
2688
2689LLVMValueRef LLVMGetNamedGlobalIFunc(LLVMModuleRef M,
2690 const char *Name, size_t NameLen) {
2691 return wrap(P: unwrap(P: M)->getNamedIFunc(Name: StringRef(Name, NameLen)));
2692}
2693
2694LLVMValueRef LLVMGetFirstGlobalIFunc(LLVMModuleRef M) {
2695 Module *Mod = unwrap(P: M);
2696 Module::ifunc_iterator I = Mod->ifunc_begin();
2697 if (I == Mod->ifunc_end())
2698 return nullptr;
2699 return wrap(P: &*I);
2700}
2701
2702LLVMValueRef LLVMGetLastGlobalIFunc(LLVMModuleRef M) {
2703 Module *Mod = unwrap(P: M);
2704 Module::ifunc_iterator I = Mod->ifunc_end();
2705 if (I == Mod->ifunc_begin())
2706 return nullptr;
2707 return wrap(P: &*--I);
2708}
2709
2710LLVMValueRef LLVMGetNextGlobalIFunc(LLVMValueRef IFunc) {
2711 GlobalIFunc *GIF = unwrap<GlobalIFunc>(P: IFunc);
2712 Module::ifunc_iterator I(GIF);
2713 if (++I == GIF->getParent()->ifunc_end())
2714 return nullptr;
2715 return wrap(P: &*I);
2716}
2717
2718LLVMValueRef LLVMGetPreviousGlobalIFunc(LLVMValueRef IFunc) {
2719 GlobalIFunc *GIF = unwrap<GlobalIFunc>(P: IFunc);
2720 Module::ifunc_iterator I(GIF);
2721 if (I == GIF->getParent()->ifunc_begin())
2722 return nullptr;
2723 return wrap(P: &*--I);
2724}
2725
2726LLVMValueRef LLVMGetGlobalIFuncResolver(LLVMValueRef IFunc) {
2727 return wrap(P: unwrap<GlobalIFunc>(P: IFunc)->getResolver());
2728}
2729
2730void LLVMSetGlobalIFuncResolver(LLVMValueRef IFunc, LLVMValueRef Resolver) {
2731 unwrap<GlobalIFunc>(P: IFunc)->setResolver(unwrap<Constant>(P: Resolver));
2732}
2733
2734void LLVMEraseGlobalIFunc(LLVMValueRef IFunc) {
2735 unwrap<GlobalIFunc>(P: IFunc)->eraseFromParent();
2736}
2737
2738void LLVMRemoveGlobalIFunc(LLVMValueRef IFunc) {
2739 unwrap<GlobalIFunc>(P: IFunc)->removeFromParent();
2740}
2741
2742/*--.. Operations on operand bundles........................................--*/
2743
2744LLVMOperandBundleRef LLVMCreateOperandBundle(const char *Tag, size_t TagLen,
2745 LLVMValueRef *Args,
2746 unsigned NumArgs) {
2747 return wrap(P: new OperandBundleDef(std::string(Tag, TagLen),
2748 ArrayRef(unwrap(Vals: Args), NumArgs)));
2749}
2750
2751void LLVMDisposeOperandBundle(LLVMOperandBundleRef Bundle) {
2752 delete unwrap(P: Bundle);
2753}
2754
2755const char *LLVMGetOperandBundleTag(LLVMOperandBundleRef Bundle, size_t *Len) {
2756 StringRef Str = unwrap(P: Bundle)->getTag();
2757 *Len = Str.size();
2758 return Str.data();
2759}
2760
2761unsigned LLVMGetNumOperandBundleArgs(LLVMOperandBundleRef Bundle) {
2762 return unwrap(P: Bundle)->inputs().size();
2763}
2764
2765LLVMValueRef LLVMGetOperandBundleArgAtIndex(LLVMOperandBundleRef Bundle,
2766 unsigned Index) {
2767 return wrap(P: unwrap(P: Bundle)->inputs()[Index]);
2768}
2769
2770/*--.. Operations on basic blocks ..........................................--*/
2771
2772LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
2773 return wrap(P: static_cast<Value*>(unwrap(P: BB)));
2774}
2775
2776LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
2777 return isa<BasicBlock>(Val: unwrap(P: Val));
2778}
2779
2780LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
2781 return wrap(P: unwrap<BasicBlock>(P: Val));
2782}
2783
2784const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
2785 return unwrap(P: BB)->getName().data();
2786}
2787
2788LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
2789 return wrap(P: unwrap(P: BB)->getParent());
2790}
2791
2792LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
2793 return wrap(P: unwrap(P: BB)->getTerminator());
2794}
2795
2796unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
2797 return unwrap<Function>(P: FnRef)->size();
2798}
2799
2800void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2801 Function *Fn = unwrap<Function>(P: FnRef);
2802 for (BasicBlock &BB : *Fn)
2803 *BasicBlocksRefs++ = wrap(P: &BB);
2804}
2805
2806LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2807 return wrap(P: &unwrap<Function>(P: Fn)->getEntryBlock());
2808}
2809
2810LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2811 Function *Func = unwrap<Function>(P: Fn);
2812 Function::iterator I = Func->begin();
2813 if (I == Func->end())
2814 return nullptr;
2815 return wrap(P: &*I);
2816}
2817
2818LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2819 Function *Func = unwrap<Function>(P: Fn);
2820 Function::iterator I = Func->end();
2821 if (I == Func->begin())
2822 return nullptr;
2823 return wrap(P: &*--I);
2824}
2825
2826LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2827 BasicBlock *Block = unwrap(P: BB);
2828 Function::iterator I(Block);
2829 if (++I == Block->getParent()->end())
2830 return nullptr;
2831 return wrap(P: &*I);
2832}
2833
2834LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2835 BasicBlock *Block = unwrap(P: BB);
2836 Function::iterator I(Block);
2837 if (I == Block->getParent()->begin())
2838 return nullptr;
2839 return wrap(P: &*--I);
2840}
2841
2842LLVMBasicBlockRef LLVMCreateBasicBlockInContext(LLVMContextRef C,
2843 const char *Name) {
2844 return wrap(P: llvm::BasicBlock::Create(Context&: *unwrap(P: C), Name));
2845}
2846
2847void LLVMInsertExistingBasicBlockAfterInsertBlock(LLVMBuilderRef Builder,
2848 LLVMBasicBlockRef BB) {
2849 BasicBlock *ToInsert = unwrap(P: BB);
2850 BasicBlock *CurBB = unwrap(P: Builder)->GetInsertBlock();
2851 assert(CurBB && "current insertion point is invalid!");
2852 CurBB->getParent()->insert(Position: std::next(x: CurBB->getIterator()), BB: ToInsert);
2853}
2854
2855void LLVMAppendExistingBasicBlock(LLVMValueRef Fn,
2856 LLVMBasicBlockRef BB) {
2857 unwrap<Function>(P: Fn)->insert(Position: unwrap<Function>(P: Fn)->end(), BB: unwrap(P: BB));
2858}
2859
2860LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2861 LLVMValueRef FnRef,
2862 const char *Name) {
2863 return wrap(P: BasicBlock::Create(Context&: *unwrap(P: C), Name, Parent: unwrap<Function>(P: FnRef)));
2864}
2865
2866LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2867 return LLVMAppendBasicBlockInContext(C: LLVMGetGlobalContext(), FnRef, Name);
2868}
2869
2870LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2871 LLVMBasicBlockRef BBRef,
2872 const char *Name) {
2873 BasicBlock *BB = unwrap(P: BBRef);
2874 return wrap(P: BasicBlock::Create(Context&: *unwrap(P: C), Name, Parent: BB->getParent(), InsertBefore: BB));
2875}
2876
2877LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
2878 const char *Name) {
2879 return LLVMInsertBasicBlockInContext(C: LLVMGetGlobalContext(), BBRef, Name);
2880}
2881
2882void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2883 unwrap(P: BBRef)->eraseFromParent();
2884}
2885
2886void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2887 unwrap(P: BBRef)->removeFromParent();
2888}
2889
2890void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2891 unwrap(P: BB)->moveBefore(MovePos: unwrap(P: MovePos));
2892}
2893
2894void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2895 unwrap(P: BB)->moveAfter(MovePos: unwrap(P: MovePos));
2896}
2897
2898/*--.. Operations on instructions ..........................................--*/
2899
2900LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2901 return wrap(P: unwrap<Instruction>(P: Inst)->getParent());
2902}
2903
2904LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2905 BasicBlock *Block = unwrap(P: BB);
2906 BasicBlock::iterator I = Block->begin();
2907 if (I == Block->end())
2908 return nullptr;
2909 return wrap(P: &*I);
2910}
2911
2912LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2913 BasicBlock *Block = unwrap(P: BB);
2914 BasicBlock::iterator I = Block->end();
2915 if (I == Block->begin())
2916 return nullptr;
2917 return wrap(P: &*--I);
2918}
2919
2920LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2921 Instruction *Instr = unwrap<Instruction>(P: Inst);
2922 BasicBlock::iterator I(Instr);
2923 if (++I == Instr->getParent()->end())
2924 return nullptr;
2925 return wrap(P: &*I);
2926}
2927
2928LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2929 Instruction *Instr = unwrap<Instruction>(P: Inst);
2930 BasicBlock::iterator I(Instr);
2931 if (I == Instr->getParent()->begin())
2932 return nullptr;
2933 return wrap(P: &*--I);
2934}
2935
2936void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2937 unwrap<Instruction>(P: Inst)->removeFromParent();
2938}
2939
2940void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2941 unwrap<Instruction>(P: Inst)->eraseFromParent();
2942}
2943
2944void LLVMDeleteInstruction(LLVMValueRef Inst) {
2945 unwrap<Instruction>(P: Inst)->deleteValue();
2946}
2947
2948LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
2949 if (ICmpInst *I = dyn_cast<ICmpInst>(Val: unwrap(P: Inst)))
2950 return (LLVMIntPredicate)I->getPredicate();
2951 return (LLVMIntPredicate)0;
2952}
2953
2954LLVMBool LLVMGetICmpSameSign(LLVMValueRef Inst) {
2955 return unwrap<ICmpInst>(P: Inst)->hasSameSign();
2956}
2957
2958void LLVMSetICmpSameSign(LLVMValueRef Inst, LLVMBool SameSign) {
2959 unwrap<ICmpInst>(P: Inst)->setSameSign(SameSign);
2960}
2961
2962LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2963 if (FCmpInst *I = dyn_cast<FCmpInst>(Val: unwrap(P: Inst)))
2964 return (LLVMRealPredicate)I->getPredicate();
2965 return (LLVMRealPredicate)0;
2966}
2967
2968LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2969 if (Instruction *C = dyn_cast<Instruction>(Val: unwrap(P: Inst)))
2970 return map_to_llvmopcode(opcode: C->getOpcode());
2971 return (LLVMOpcode)0;
2972}
2973
2974LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2975 if (Instruction *C = dyn_cast<Instruction>(Val: unwrap(P: Inst)))
2976 return wrap(P: C->clone());
2977 return nullptr;
2978}
2979
2980LLVMValueRef LLVMIsATerminatorInst(LLVMValueRef Inst) {
2981 Instruction *I = dyn_cast<Instruction>(Val: unwrap(P: Inst));
2982 return (I && I->isTerminator()) ? wrap(P: I) : nullptr;
2983}
2984
2985LLVMDbgRecordRef LLVMGetFirstDbgRecord(LLVMValueRef Inst) {
2986 Instruction *Instr = unwrap<Instruction>(P: Inst);
2987 auto I = Instr->DebugMarker->StoredDbgRecords.begin();
2988 if (I == Instr->DebugMarker->StoredDbgRecords.end())
2989 return nullptr;
2990 return wrap(P: &*I);
2991}
2992
2993LLVMDbgRecordRef LLVMGetLastDbgRecord(LLVMValueRef Inst) {
2994 Instruction *Instr = unwrap<Instruction>(P: Inst);
2995 auto I = Instr->DebugMarker->StoredDbgRecords.rbegin();
2996 if (I == Instr->DebugMarker->StoredDbgRecords.rend())
2997 return nullptr;
2998 return wrap(P: &*I);
2999}
3000
3001LLVMDbgRecordRef LLVMGetNextDbgRecord(LLVMDbgRecordRef Rec) {
3002 DbgRecord *Record = unwrap<DbgRecord>(P: Rec);
3003 simple_ilist<DbgRecord>::iterator I(Record);
3004 if (++I == Record->getInstruction()->DebugMarker->StoredDbgRecords.end())
3005 return nullptr;
3006 return wrap(P: &*I);
3007}
3008
3009LLVMDbgRecordRef LLVMGetPreviousDbgRecord(LLVMDbgRecordRef Rec) {
3010 DbgRecord *Record = unwrap<DbgRecord>(P: Rec);
3011 simple_ilist<DbgRecord>::iterator I(Record);
3012 if (I == Record->getInstruction()->DebugMarker->StoredDbgRecords.begin())
3013 return nullptr;
3014 return wrap(P: &*--I);
3015}
3016
3017unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
3018 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(Val: unwrap(P: Instr))) {
3019 return FPI->arg_size();
3020 }
3021 return unwrap<CallBase>(P: Instr)->arg_size();
3022}
3023
3024/*--.. Call and invoke instructions ........................................--*/
3025
3026unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
3027 return unwrap<CallBase>(P: Instr)->getCallingConv();
3028}
3029
3030void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
3031 return unwrap<CallBase>(P: Instr)->setCallingConv(
3032 static_cast<CallingConv::ID>(CC));
3033}
3034
3035void LLVMSetInstrParamAlignment(LLVMValueRef Instr, LLVMAttributeIndex Idx,
3036 unsigned align) {
3037 auto *Call = unwrap<CallBase>(P: Instr);
3038 Attribute AlignAttr =
3039 Attribute::getWithAlignment(Context&: Call->getContext(), Alignment: Align(align));
3040 Call->addAttributeAtIndex(i: Idx, Attr: AlignAttr);
3041}
3042
3043void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
3044 LLVMAttributeRef A) {
3045 unwrap<CallBase>(P: C)->addAttributeAtIndex(i: Idx, Attr: unwrap(Attr: A));
3046}
3047
3048unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
3049 LLVMAttributeIndex Idx) {
3050 auto *Call = unwrap<CallBase>(P: C);
3051 auto AS = Call->getAttributes().getAttributes(Index: Idx);
3052 return AS.getNumAttributes();
3053}
3054
3055void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
3056 LLVMAttributeRef *Attrs) {
3057 auto *Call = unwrap<CallBase>(P: C);
3058 auto AS = Call->getAttributes().getAttributes(Index: Idx);
3059 for (auto A : AS)
3060 *Attrs++ = wrap(Attr: A);
3061}
3062
3063LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
3064 LLVMAttributeIndex Idx,
3065 unsigned KindID) {
3066 return wrap(Attr: unwrap<CallBase>(P: C)->getAttributeAtIndex(
3067 i: Idx, Kind: (Attribute::AttrKind)KindID));
3068}
3069
3070LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
3071 LLVMAttributeIndex Idx,
3072 const char *K, unsigned KLen) {
3073 return wrap(
3074 Attr: unwrap<CallBase>(P: C)->getAttributeAtIndex(i: Idx, Kind: StringRef(K, KLen)));
3075}
3076
3077void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
3078 unsigned KindID) {
3079 unwrap<CallBase>(P: C)->removeAttributeAtIndex(i: Idx, Kind: (Attribute::AttrKind)KindID);
3080}
3081
3082void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
3083 const char *K, unsigned KLen) {
3084 unwrap<CallBase>(P: C)->removeAttributeAtIndex(i: Idx, Kind: StringRef(K, KLen));
3085}
3086
3087LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
3088 return wrap(P: unwrap<CallBase>(P: Instr)->getCalledOperand());
3089}
3090
3091LLVMTypeRef LLVMGetCalledFunctionType(LLVMValueRef Instr) {
3092 return wrap(P: unwrap<CallBase>(P: Instr)->getFunctionType());
3093}
3094
3095unsigned LLVMGetNumOperandBundles(LLVMValueRef C) {
3096 return unwrap<CallBase>(P: C)->getNumOperandBundles();
3097}
3098
3099LLVMOperandBundleRef LLVMGetOperandBundleAtIndex(LLVMValueRef C,
3100 unsigned Index) {
3101 return wrap(
3102 P: new OperandBundleDef(unwrap<CallBase>(P: C)->getOperandBundleAt(Index)));
3103}
3104
3105/*--.. Operations on call instructions (only) ..............................--*/
3106
3107LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
3108 return unwrap<CallInst>(P: Call)->isTailCall();
3109}
3110
3111void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
3112 unwrap<CallInst>(P: Call)->setTailCall(isTailCall);
3113}
3114
3115LLVMTailCallKind LLVMGetTailCallKind(LLVMValueRef Call) {
3116 return (LLVMTailCallKind)unwrap<CallInst>(P: Call)->getTailCallKind();
3117}
3118
3119void LLVMSetTailCallKind(LLVMValueRef Call, LLVMTailCallKind kind) {
3120 unwrap<CallInst>(P: Call)->setTailCallKind((CallInst::TailCallKind)kind);
3121}
3122
3123/*--.. Operations on invoke instructions (only) ............................--*/
3124
3125LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
3126 return wrap(P: unwrap<InvokeInst>(P: Invoke)->getNormalDest());
3127}
3128
3129LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
3130 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(Val: unwrap(P: Invoke))) {
3131 return wrap(P: CRI->getUnwindDest());
3132 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(Val: unwrap(P: Invoke))) {
3133 return wrap(P: CSI->getUnwindDest());
3134 }
3135 return wrap(P: unwrap<InvokeInst>(P: Invoke)->getUnwindDest());
3136}
3137
3138void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
3139 unwrap<InvokeInst>(P: Invoke)->setNormalDest(unwrap(P: B));
3140}
3141
3142void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
3143 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(Val: unwrap(P: Invoke))) {
3144 return CRI->setUnwindDest(unwrap(P: B));
3145 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(Val: unwrap(P: Invoke))) {
3146 return CSI->setUnwindDest(unwrap(P: B));
3147 }
3148 unwrap<InvokeInst>(P: Invoke)->setUnwindDest(unwrap(P: B));
3149}
3150
3151LLVMBasicBlockRef LLVMGetCallBrDefaultDest(LLVMValueRef CallBr) {
3152 return wrap(P: unwrap<CallBrInst>(P: CallBr)->getDefaultDest());
3153}
3154
3155unsigned LLVMGetCallBrNumIndirectDests(LLVMValueRef CallBr) {
3156 return unwrap<CallBrInst>(P: CallBr)->getNumIndirectDests();
3157}
3158
3159LLVMBasicBlockRef LLVMGetCallBrIndirectDest(LLVMValueRef CallBr, unsigned Idx) {
3160 return wrap(P: unwrap<CallBrInst>(P: CallBr)->getIndirectDest(i: Idx));
3161}
3162
3163/*--.. Operations on terminators ...........................................--*/
3164
3165unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
3166 return unwrap<Instruction>(P: Term)->getNumSuccessors();
3167}
3168
3169LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
3170 return wrap(P: unwrap<Instruction>(P: Term)->getSuccessor(Idx: i));
3171}
3172
3173void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
3174 return unwrap<Instruction>(P: Term)->setSuccessor(Idx: i, BB: unwrap(P: block));
3175}
3176
3177/*--.. Operations on branch instructions (only) ............................--*/
3178
3179LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
3180 return unwrap<BranchInst>(P: Branch)->isConditional();
3181}
3182
3183LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
3184 return wrap(P: unwrap<BranchInst>(P: Branch)->getCondition());
3185}
3186
3187void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
3188 return unwrap<BranchInst>(P: Branch)->setCondition(unwrap(P: Cond));
3189}
3190
3191/*--.. Operations on switch instructions (only) ............................--*/
3192
3193LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
3194 return wrap(P: unwrap<SwitchInst>(P: Switch)->getDefaultDest());
3195}
3196
3197/*--.. Operations on alloca instructions (only) ............................--*/
3198
3199LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
3200 return wrap(P: unwrap<AllocaInst>(P: Alloca)->getAllocatedType());
3201}
3202
3203/*--.. Operations on gep instructions (only) ...............................--*/
3204
3205LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
3206 return unwrap<GEPOperator>(P: GEP)->isInBounds();
3207}
3208
3209void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
3210 return unwrap<GetElementPtrInst>(P: GEP)->setIsInBounds(InBounds);
3211}
3212
3213LLVMTypeRef LLVMGetGEPSourceElementType(LLVMValueRef GEP) {
3214 return wrap(P: unwrap<GEPOperator>(P: GEP)->getSourceElementType());
3215}
3216
3217LLVMGEPNoWrapFlags LLVMGEPGetNoWrapFlags(LLVMValueRef GEP) {
3218 GEPOperator *GEPOp = unwrap<GEPOperator>(P: GEP);
3219 return mapToLLVMGEPNoWrapFlags(GEPFlags: GEPOp->getNoWrapFlags());
3220}
3221
3222void LLVMGEPSetNoWrapFlags(LLVMValueRef GEP, LLVMGEPNoWrapFlags NoWrapFlags) {
3223 GetElementPtrInst *GEPInst = unwrap<GetElementPtrInst>(P: GEP);
3224 GEPInst->setNoWrapFlags(mapFromLLVMGEPNoWrapFlags(GEPFlags: NoWrapFlags));
3225}
3226
3227/*--.. Operations on phi nodes .............................................--*/
3228
3229void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
3230 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
3231 PHINode *PhiVal = unwrap<PHINode>(P: PhiNode);
3232 for (unsigned I = 0; I != Count; ++I)
3233 PhiVal->addIncoming(V: unwrap(P: IncomingValues[I]), BB: unwrap(P: IncomingBlocks[I]));
3234}
3235
3236unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
3237 return unwrap<PHINode>(P: PhiNode)->getNumIncomingValues();
3238}
3239
3240LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
3241 return wrap(P: unwrap<PHINode>(P: PhiNode)->getIncomingValue(i: Index));
3242}
3243
3244LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
3245 return wrap(P: unwrap<PHINode>(P: PhiNode)->getIncomingBlock(i: Index));
3246}
3247
3248/*--.. Operations on extractvalue and insertvalue nodes ....................--*/
3249
3250unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
3251 auto *I = unwrap(P: Inst);
3252 if (auto *GEP = dyn_cast<GEPOperator>(Val: I))
3253 return GEP->getNumIndices();
3254 if (auto *EV = dyn_cast<ExtractValueInst>(Val: I))
3255 return EV->getNumIndices();
3256 if (auto *IV = dyn_cast<InsertValueInst>(Val: I))
3257 return IV->getNumIndices();
3258 llvm_unreachable(
3259 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3260}
3261
3262const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
3263 auto *I = unwrap(P: Inst);
3264 if (auto *EV = dyn_cast<ExtractValueInst>(Val: I))
3265 return EV->getIndices().data();
3266 if (auto *IV = dyn_cast<InsertValueInst>(Val: I))
3267 return IV->getIndices().data();
3268 llvm_unreachable(
3269 "LLVMGetIndices applies only to extractvalue and insertvalue!");
3270}
3271
3272
3273/*===-- Instruction builders ----------------------------------------------===*/
3274
3275LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
3276 return wrap(P: new IRBuilder<>(*unwrap(P: C)));
3277}
3278
3279LLVMBuilderRef LLVMCreateBuilder(void) {
3280 return LLVMCreateBuilderInContext(C: LLVMGetGlobalContext());
3281}
3282
3283static void LLVMPositionBuilderImpl(IRBuilder<> *Builder, BasicBlock *Block,
3284 Instruction *Instr, bool BeforeDbgRecords) {
3285 BasicBlock::iterator I = Instr ? Instr->getIterator() : Block->end();
3286 I.setHeadBit(BeforeDbgRecords);
3287 Builder->SetInsertPoint(TheBB: Block, IP: I);
3288}
3289
3290void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
3291 LLVMValueRef Instr) {
3292 return LLVMPositionBuilderImpl(Builder: unwrap(P: Builder), Block: unwrap(P: Block),
3293 Instr: unwrap<Instruction>(P: Instr), BeforeDbgRecords: false);
3294}
3295
3296void LLVMPositionBuilderBeforeDbgRecords(LLVMBuilderRef Builder,
3297 LLVMBasicBlockRef Block,
3298 LLVMValueRef Instr) {
3299 return LLVMPositionBuilderImpl(Builder: unwrap(P: Builder), Block: unwrap(P: Block),
3300 Instr: unwrap<Instruction>(P: Instr), BeforeDbgRecords: true);
3301}
3302
3303void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
3304 Instruction *I = unwrap<Instruction>(P: Instr);
3305 return LLVMPositionBuilderImpl(Builder: unwrap(P: Builder), Block: I->getParent(), Instr: I, BeforeDbgRecords: false);
3306}
3307
3308void LLVMPositionBuilderBeforeInstrAndDbgRecords(LLVMBuilderRef Builder,
3309 LLVMValueRef Instr) {
3310 Instruction *I = unwrap<Instruction>(P: Instr);
3311 return LLVMPositionBuilderImpl(Builder: unwrap(P: Builder), Block: I->getParent(), Instr: I, BeforeDbgRecords: true);
3312}
3313
3314void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
3315 BasicBlock *BB = unwrap(P: Block);
3316 unwrap(P: Builder)->SetInsertPoint(BB);
3317}
3318
3319LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
3320 return wrap(P: unwrap(P: Builder)->GetInsertBlock());
3321}
3322
3323void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
3324 unwrap(P: Builder)->ClearInsertionPoint();
3325}
3326
3327void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
3328 unwrap(P: Builder)->Insert(I: unwrap<Instruction>(P: Instr));
3329}
3330
3331void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
3332 const char *Name) {
3333 unwrap(P: Builder)->Insert(I: unwrap<Instruction>(P: Instr), Name);
3334}
3335
3336void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
3337 delete unwrap(P: Builder);
3338}
3339
3340/*--.. Metadata builders ...................................................--*/
3341
3342LLVMMetadataRef LLVMGetCurrentDebugLocation2(LLVMBuilderRef Builder) {
3343 return wrap(P: unwrap(P: Builder)->getCurrentDebugLocation().getAsMDNode());
3344}
3345
3346void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Builder, LLVMMetadataRef Loc) {
3347 if (Loc)
3348 unwrap(P: Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(P: Loc)));
3349 else
3350 unwrap(P: Builder)->SetCurrentDebugLocation(DebugLoc());
3351}
3352
3353void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
3354 MDNode *Loc =
3355 L ? cast<MDNode>(Val: unwrap<MetadataAsValue>(P: L)->getMetadata()) : nullptr;
3356 unwrap(P: Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3357}
3358
3359LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
3360 LLVMContext &Context = unwrap(P: Builder)->getContext();
3361 return wrap(P: MetadataAsValue::get(
3362 Context, MD: unwrap(P: Builder)->getCurrentDebugLocation().getAsMDNode()));
3363}
3364
3365void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
3366 unwrap(P: Builder)->SetInstDebugLocation(unwrap<Instruction>(P: Inst));
3367}
3368
3369void LLVMAddMetadataToInst(LLVMBuilderRef Builder, LLVMValueRef Inst) {
3370 unwrap(P: Builder)->AddMetadataToInst(I: unwrap<Instruction>(P: Inst));
3371}
3372
3373void LLVMBuilderSetDefaultFPMathTag(LLVMBuilderRef Builder,
3374 LLVMMetadataRef FPMathTag) {
3375
3376 unwrap(P: Builder)->setDefaultFPMathTag(FPMathTag
3377 ? unwrap<MDNode>(P: FPMathTag)
3378 : nullptr);
3379}
3380
3381LLVMContextRef LLVMGetBuilderContext(LLVMBuilderRef Builder) {
3382 return wrap(P: &unwrap(P: Builder)->getContext());
3383}
3384
3385LLVMMetadataRef LLVMBuilderGetDefaultFPMathTag(LLVMBuilderRef Builder) {
3386 return wrap(P: unwrap(P: Builder)->getDefaultFPMathTag());
3387}
3388
3389/*--.. Instruction builders ................................................--*/
3390
3391LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
3392 return wrap(P: unwrap(P: B)->CreateRetVoid());
3393}
3394
3395LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
3396 return wrap(P: unwrap(P: B)->CreateRet(V: unwrap(P: V)));
3397}
3398
3399LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
3400 unsigned N) {
3401 return wrap(P: unwrap(P: B)->CreateAggregateRet(retVals: unwrap(Vals: RetVals), N));
3402}
3403
3404LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
3405 return wrap(P: unwrap(P: B)->CreateBr(Dest: unwrap(P: Dest)));
3406}
3407
3408LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
3409 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
3410 return wrap(P: unwrap(P: B)->CreateCondBr(Cond: unwrap(P: If), True: unwrap(P: Then), False: unwrap(P: Else)));
3411}
3412
3413LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
3414 LLVMBasicBlockRef Else, unsigned NumCases) {
3415 return wrap(P: unwrap(P: B)->CreateSwitch(V: unwrap(P: V), Dest: unwrap(P: Else), NumCases));
3416}
3417
3418LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
3419 unsigned NumDests) {
3420 return wrap(P: unwrap(P: B)->CreateIndirectBr(Addr: unwrap(P: Addr), NumDests));
3421}
3422
3423LLVMValueRef LLVMBuildCallBr(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
3424 LLVMBasicBlockRef DefaultDest,
3425 LLVMBasicBlockRef *IndirectDests,
3426 unsigned NumIndirectDests, LLVMValueRef *Args,
3427 unsigned NumArgs, LLVMOperandBundleRef *Bundles,
3428 unsigned NumBundles, const char *Name) {
3429
3430 SmallVector<OperandBundleDef, 8> OBs;
3431 for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
3432 OperandBundleDef *OB = unwrap(P: Bundle);
3433 OBs.push_back(Elt: *OB);
3434 }
3435
3436 return wrap(P: unwrap(P: B)->CreateCallBr(
3437 Ty: unwrap<FunctionType>(P: Ty), Callee: unwrap(P: Fn), DefaultDest: unwrap(P: DefaultDest),
3438 IndirectDests: ArrayRef(unwrap(BBs: IndirectDests), NumIndirectDests),
3439 Args: ArrayRef<Value *>(unwrap(Vals: Args), NumArgs), OpBundles: OBs, Name));
3440}
3441
3442LLVMValueRef LLVMBuildInvoke2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
3443 LLVMValueRef *Args, unsigned NumArgs,
3444 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
3445 const char *Name) {
3446 return wrap(P: unwrap(P: B)->CreateInvoke(Ty: unwrap<FunctionType>(P: Ty), Callee: unwrap(P: Fn),
3447 NormalDest: unwrap(P: Then), UnwindDest: unwrap(P: Catch),
3448 Args: ArrayRef(unwrap(Vals: Args), NumArgs), Name));
3449}
3450
3451LLVMValueRef LLVMBuildInvokeWithOperandBundles(
3452 LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, LLVMValueRef *Args,
3453 unsigned NumArgs, LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
3454 LLVMOperandBundleRef *Bundles, unsigned NumBundles, const char *Name) {
3455 SmallVector<OperandBundleDef, 8> OBs;
3456 for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
3457 OperandBundleDef *OB = unwrap(P: Bundle);
3458 OBs.push_back(Elt: *OB);
3459 }
3460 return wrap(P: unwrap(P: B)->CreateInvoke(
3461 Ty: unwrap<FunctionType>(P: Ty), Callee: unwrap(P: Fn), NormalDest: unwrap(P: Then), UnwindDest: unwrap(P: Catch),
3462 Args: ArrayRef(unwrap(Vals: Args), NumArgs), OpBundles: OBs, Name));
3463}
3464
3465LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
3466 LLVMValueRef PersFn, unsigned NumClauses,
3467 const char *Name) {
3468 // The personality used to live on the landingpad instruction, but now it
3469 // lives on the parent function. For compatibility, take the provided
3470 // personality and put it on the parent function.
3471 if (PersFn)
3472 unwrap(P: B)->GetInsertBlock()->getParent()->setPersonalityFn(
3473 unwrap<Function>(P: PersFn));
3474 return wrap(P: unwrap(P: B)->CreateLandingPad(Ty: unwrap(P: Ty), NumClauses, Name));
3475}
3476
3477LLVMValueRef LLVMBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
3478 LLVMValueRef *Args, unsigned NumArgs,
3479 const char *Name) {
3480 return wrap(P: unwrap(P: B)->CreateCatchPad(ParentPad: unwrap(P: ParentPad),
3481 Args: ArrayRef(unwrap(Vals: Args), NumArgs), Name));
3482}
3483
3484LLVMValueRef LLVMBuildCleanupPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
3485 LLVMValueRef *Args, unsigned NumArgs,
3486 const char *Name) {
3487 if (ParentPad == nullptr) {
3488 Type *Ty = Type::getTokenTy(C&: unwrap(P: B)->getContext());
3489 ParentPad = wrap(P: Constant::getNullValue(Ty));
3490 }
3491 return wrap(P: unwrap(P: B)->CreateCleanupPad(
3492 ParentPad: unwrap(P: ParentPad), Args: ArrayRef(unwrap(Vals: Args), NumArgs), Name));
3493}
3494
3495LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
3496 return wrap(P: unwrap(P: B)->CreateResume(Exn: unwrap(P: Exn)));
3497}
3498
3499LLVMValueRef LLVMBuildCatchSwitch(LLVMBuilderRef B, LLVMValueRef ParentPad,
3500 LLVMBasicBlockRef UnwindBB,
3501 unsigned NumHandlers, const char *Name) {
3502 if (ParentPad == nullptr) {
3503 Type *Ty = Type::getTokenTy(C&: unwrap(P: B)->getContext());
3504 ParentPad = wrap(P: Constant::getNullValue(Ty));
3505 }
3506 return wrap(P: unwrap(P: B)->CreateCatchSwitch(ParentPad: unwrap(P: ParentPad), UnwindBB: unwrap(P: UnwindBB),
3507 NumHandlers, Name));
3508}
3509
3510LLVMValueRef LLVMBuildCatchRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
3511 LLVMBasicBlockRef BB) {
3512 return wrap(P: unwrap(P: B)->CreateCatchRet(CatchPad: unwrap<CatchPadInst>(P: CatchPad),
3513 BB: unwrap(P: BB)));
3514}
3515
3516LLVMValueRef LLVMBuildCleanupRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
3517 LLVMBasicBlockRef BB) {
3518 return wrap(P: unwrap(P: B)->CreateCleanupRet(CleanupPad: unwrap<CleanupPadInst>(P: CatchPad),
3519 UnwindBB: unwrap(P: BB)));
3520}
3521
3522LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
3523 return wrap(P: unwrap(P: B)->CreateUnreachable());
3524}
3525
3526void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
3527 LLVMBasicBlockRef Dest) {
3528 unwrap<SwitchInst>(P: Switch)->addCase(OnVal: unwrap<ConstantInt>(P: OnVal), Dest: unwrap(P: Dest));
3529}
3530
3531void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
3532 unwrap<IndirectBrInst>(P: IndirectBr)->addDestination(Dest: unwrap(P: Dest));
3533}
3534
3535unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3536 return unwrap<LandingPadInst>(P: LandingPad)->getNumClauses();
3537}
3538
3539LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
3540 return wrap(P: unwrap<LandingPadInst>(P: LandingPad)->getClause(Idx));
3541}
3542
3543void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
3544 unwrap<LandingPadInst>(P: LandingPad)->addClause(ClauseVal: unwrap<Constant>(P: ClauseVal));
3545}
3546
3547LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
3548 return unwrap<LandingPadInst>(P: LandingPad)->isCleanup();
3549}
3550
3551void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3552 unwrap<LandingPadInst>(P: LandingPad)->setCleanup(Val);
3553}
3554
3555void LLVMAddHandler(LLVMValueRef CatchSwitch, LLVMBasicBlockRef Dest) {
3556 unwrap<CatchSwitchInst>(P: CatchSwitch)->addHandler(Dest: unwrap(P: Dest));
3557}
3558
3559unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3560 return unwrap<CatchSwitchInst>(P: CatchSwitch)->getNumHandlers();
3561}
3562
3563void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3564 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(P: CatchSwitch);
3565 for (const BasicBlock *H : CSI->handlers())
3566 *Handlers++ = wrap(P: H);
3567}
3568
3569LLVMValueRef LLVMGetParentCatchSwitch(LLVMValueRef CatchPad) {
3570 return wrap(P: unwrap<CatchPadInst>(P: CatchPad)->getCatchSwitch());
3571}
3572
3573void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad, LLVMValueRef CatchSwitch) {
3574 unwrap<CatchPadInst>(P: CatchPad)
3575 ->setCatchSwitch(unwrap<CatchSwitchInst>(P: CatchSwitch));
3576}
3577
3578/*--.. Funclets ...........................................................--*/
3579
3580LLVMValueRef LLVMGetArgOperand(LLVMValueRef Funclet, unsigned i) {
3581 return wrap(P: unwrap<FuncletPadInst>(P: Funclet)->getArgOperand(i));
3582}
3583
3584void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
3585 unwrap<FuncletPadInst>(P: Funclet)->setArgOperand(i, v: unwrap(P: value));
3586}
3587
3588/*--.. Arithmetic ..........................................................--*/
3589
3590static FastMathFlags mapFromLLVMFastMathFlags(LLVMFastMathFlags FMF) {
3591 FastMathFlags NewFMF;
3592 NewFMF.setAllowReassoc((FMF & LLVMFastMathAllowReassoc) != 0);
3593 NewFMF.setNoNaNs((FMF & LLVMFastMathNoNaNs) != 0);
3594 NewFMF.setNoInfs((FMF & LLVMFastMathNoInfs) != 0);
3595 NewFMF.setNoSignedZeros((FMF & LLVMFastMathNoSignedZeros) != 0);
3596 NewFMF.setAllowReciprocal((FMF & LLVMFastMathAllowReciprocal) != 0);
3597 NewFMF.setAllowContract((FMF & LLVMFastMathAllowContract) != 0);
3598 NewFMF.setApproxFunc((FMF & LLVMFastMathApproxFunc) != 0);
3599
3600 return NewFMF;
3601}
3602
3603static LLVMFastMathFlags mapToLLVMFastMathFlags(FastMathFlags FMF) {
3604 LLVMFastMathFlags NewFMF = LLVMFastMathNone;
3605 if (FMF.allowReassoc())
3606 NewFMF |= LLVMFastMathAllowReassoc;
3607 if (FMF.noNaNs())
3608 NewFMF |= LLVMFastMathNoNaNs;
3609 if (FMF.noInfs())
3610 NewFMF |= LLVMFastMathNoInfs;
3611 if (FMF.noSignedZeros())
3612 NewFMF |= LLVMFastMathNoSignedZeros;
3613 if (FMF.allowReciprocal())
3614 NewFMF |= LLVMFastMathAllowReciprocal;
3615 if (FMF.allowContract())
3616 NewFMF |= LLVMFastMathAllowContract;
3617 if (FMF.approxFunc())
3618 NewFMF |= LLVMFastMathApproxFunc;
3619
3620 return NewFMF;
3621}
3622
3623LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3624 const char *Name) {
3625 return wrap(P: unwrap(P: B)->CreateAdd(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3626}
3627
3628LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3629 const char *Name) {
3630 return wrap(P: unwrap(P: B)->CreateNSWAdd(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3631}
3632
3633LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3634 const char *Name) {
3635 return wrap(P: unwrap(P: B)->CreateNUWAdd(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3636}
3637
3638LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3639 const char *Name) {
3640 return wrap(P: unwrap(P: B)->CreateFAdd(L: unwrap(P: LHS), R: unwrap(P: RHS), Name));
3641}
3642
3643LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3644 const char *Name) {
3645 return wrap(P: unwrap(P: B)->CreateSub(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3646}
3647
3648LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3649 const char *Name) {
3650 return wrap(P: unwrap(P: B)->CreateNSWSub(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3651}
3652
3653LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3654 const char *Name) {
3655 return wrap(P: unwrap(P: B)->CreateNUWSub(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3656}
3657
3658LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3659 const char *Name) {
3660 return wrap(P: unwrap(P: B)->CreateFSub(L: unwrap(P: LHS), R: unwrap(P: RHS), Name));
3661}
3662
3663LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3664 const char *Name) {
3665 return wrap(P: unwrap(P: B)->CreateMul(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3666}
3667
3668LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3669 const char *Name) {
3670 return wrap(P: unwrap(P: B)->CreateNSWMul(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3671}
3672
3673LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3674 const char *Name) {
3675 return wrap(P: unwrap(P: B)->CreateNUWMul(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3676}
3677
3678LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3679 const char *Name) {
3680 return wrap(P: unwrap(P: B)->CreateFMul(L: unwrap(P: LHS), R: unwrap(P: RHS), Name));
3681}
3682
3683LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3684 const char *Name) {
3685 return wrap(P: unwrap(P: B)->CreateUDiv(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3686}
3687
3688LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
3689 LLVMValueRef RHS, const char *Name) {
3690 return wrap(P: unwrap(P: B)->CreateExactUDiv(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3691}
3692
3693LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3694 const char *Name) {
3695 return wrap(P: unwrap(P: B)->CreateSDiv(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3696}
3697
3698LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
3699 LLVMValueRef RHS, const char *Name) {
3700 return wrap(P: unwrap(P: B)->CreateExactSDiv(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3701}
3702
3703LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3704 const char *Name) {
3705 return wrap(P: unwrap(P: B)->CreateFDiv(L: unwrap(P: LHS), R: unwrap(P: RHS), Name));
3706}
3707
3708LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3709 const char *Name) {
3710 return wrap(P: unwrap(P: B)->CreateURem(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3711}
3712
3713LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3714 const char *Name) {
3715 return wrap(P: unwrap(P: B)->CreateSRem(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3716}
3717
3718LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3719 const char *Name) {
3720 return wrap(P: unwrap(P: B)->CreateFRem(L: unwrap(P: LHS), R: unwrap(P: RHS), Name));
3721}
3722
3723LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3724 const char *Name) {
3725 return wrap(P: unwrap(P: B)->CreateShl(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3726}
3727
3728LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3729 const char *Name) {
3730 return wrap(P: unwrap(P: B)->CreateLShr(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3731}
3732
3733LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3734 const char *Name) {
3735 return wrap(P: unwrap(P: B)->CreateAShr(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3736}
3737
3738LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3739 const char *Name) {
3740 return wrap(P: unwrap(P: B)->CreateAnd(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3741}
3742
3743LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3744 const char *Name) {
3745 return wrap(P: unwrap(P: B)->CreateOr(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3746}
3747
3748LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3749 const char *Name) {
3750 return wrap(P: unwrap(P: B)->CreateXor(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3751}
3752
3753LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
3754 LLVMValueRef LHS, LLVMValueRef RHS,
3755 const char *Name) {
3756 return wrap(P: unwrap(P: B)->CreateBinOp(Opc: Instruction::BinaryOps(map_from_llvmopcode(code: Op)), LHS: unwrap(P: LHS),
3757 RHS: unwrap(P: RHS), Name));
3758}
3759
3760LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3761 return wrap(P: unwrap(P: B)->CreateNeg(V: unwrap(P: V), Name));
3762}
3763
3764LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
3765 const char *Name) {
3766 return wrap(P: unwrap(P: B)->CreateNSWNeg(V: unwrap(P: V), Name));
3767}
3768
3769LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
3770 const char *Name) {
3771 Value *Neg = unwrap(P: B)->CreateNeg(V: unwrap(P: V), Name);
3772 if (auto *I = dyn_cast<BinaryOperator>(Val: Neg))
3773 I->setHasNoUnsignedWrap();
3774 return wrap(P: Neg);
3775}
3776
3777LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3778 return wrap(P: unwrap(P: B)->CreateFNeg(V: unwrap(P: V), Name));
3779}
3780
3781LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3782 return wrap(P: unwrap(P: B)->CreateNot(V: unwrap(P: V), Name));
3783}
3784
3785LLVMBool LLVMGetNUW(LLVMValueRef ArithInst) {
3786 Value *P = unwrap<Value>(P: ArithInst);
3787 return cast<Instruction>(Val: P)->hasNoUnsignedWrap();
3788}
3789
3790void LLVMSetNUW(LLVMValueRef ArithInst, LLVMBool HasNUW) {
3791 Value *P = unwrap<Value>(P: ArithInst);
3792 cast<Instruction>(Val: P)->setHasNoUnsignedWrap(HasNUW);
3793}
3794
3795LLVMBool LLVMGetNSW(LLVMValueRef ArithInst) {
3796 Value *P = unwrap<Value>(P: ArithInst);
3797 return cast<Instruction>(Val: P)->hasNoSignedWrap();
3798}
3799
3800void LLVMSetNSW(LLVMValueRef ArithInst, LLVMBool HasNSW) {
3801 Value *P = unwrap<Value>(P: ArithInst);
3802 cast<Instruction>(Val: P)->setHasNoSignedWrap(HasNSW);
3803}
3804
3805LLVMBool LLVMGetExact(LLVMValueRef DivOrShrInst) {
3806 Value *P = unwrap<Value>(P: DivOrShrInst);
3807 return cast<Instruction>(Val: P)->isExact();
3808}
3809
3810void LLVMSetExact(LLVMValueRef DivOrShrInst, LLVMBool IsExact) {
3811 Value *P = unwrap<Value>(P: DivOrShrInst);
3812 cast<Instruction>(Val: P)->setIsExact(IsExact);
3813}
3814
3815LLVMBool LLVMGetNNeg(LLVMValueRef NonNegInst) {
3816 Value *P = unwrap<Value>(P: NonNegInst);
3817 return cast<Instruction>(Val: P)->hasNonNeg();
3818}
3819
3820void LLVMSetNNeg(LLVMValueRef NonNegInst, LLVMBool IsNonNeg) {
3821 Value *P = unwrap<Value>(P: NonNegInst);
3822 cast<Instruction>(Val: P)->setNonNeg(IsNonNeg);
3823}
3824
3825LLVMFastMathFlags LLVMGetFastMathFlags(LLVMValueRef FPMathInst) {
3826 Value *P = unwrap<Value>(P: FPMathInst);
3827 FastMathFlags FMF = cast<Instruction>(Val: P)->getFastMathFlags();
3828 return mapToLLVMFastMathFlags(FMF);
3829}
3830
3831void LLVMSetFastMathFlags(LLVMValueRef FPMathInst, LLVMFastMathFlags FMF) {
3832 Value *P = unwrap<Value>(P: FPMathInst);
3833 cast<Instruction>(Val: P)->setFastMathFlags(mapFromLLVMFastMathFlags(FMF));
3834}
3835
3836LLVMBool LLVMCanValueUseFastMathFlags(LLVMValueRef V) {
3837 Value *Val = unwrap<Value>(P: V);
3838 return isa<FPMathOperator>(Val);
3839}
3840
3841LLVMBool LLVMGetIsDisjoint(LLVMValueRef Inst) {
3842 Value *P = unwrap<Value>(P: Inst);
3843 return cast<PossiblyDisjointInst>(Val: P)->isDisjoint();
3844}
3845
3846void LLVMSetIsDisjoint(LLVMValueRef Inst, LLVMBool IsDisjoint) {
3847 Value *P = unwrap<Value>(P: Inst);
3848 cast<PossiblyDisjointInst>(Val: P)->setIsDisjoint(IsDisjoint);
3849}
3850
3851/*--.. Memory ..............................................................--*/
3852
3853LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
3854 const char *Name) {
3855 Type* ITy = Type::getInt32Ty(C&: unwrap(P: B)->GetInsertBlock()->getContext());
3856 Constant* AllocSize = ConstantExpr::getSizeOf(Ty: unwrap(P: Ty));
3857 AllocSize = ConstantExpr::getTruncOrBitCast(C: AllocSize, Ty: ITy);
3858 return wrap(P: unwrap(P: B)->CreateMalloc(IntPtrTy: ITy, AllocTy: unwrap(P: Ty), AllocSize, ArraySize: nullptr,
3859 MallocF: nullptr, Name));
3860}
3861
3862LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
3863 LLVMValueRef Val, const char *Name) {
3864 Type* ITy = Type::getInt32Ty(C&: unwrap(P: B)->GetInsertBlock()->getContext());
3865 Constant* AllocSize = ConstantExpr::getSizeOf(Ty: unwrap(P: Ty));
3866 AllocSize = ConstantExpr::getTruncOrBitCast(C: AllocSize, Ty: ITy);
3867 return wrap(P: unwrap(P: B)->CreateMalloc(IntPtrTy: ITy, AllocTy: unwrap(P: Ty), AllocSize, ArraySize: unwrap(P: Val),
3868 MallocF: nullptr, Name));
3869}
3870
3871LLVMValueRef LLVMBuildMemSet(LLVMBuilderRef B, LLVMValueRef Ptr,
3872 LLVMValueRef Val, LLVMValueRef Len,
3873 unsigned Align) {
3874 return wrap(P: unwrap(P: B)->CreateMemSet(Ptr: unwrap(P: Ptr), Val: unwrap(P: Val), Size: unwrap(P: Len),
3875 Align: MaybeAlign(Align)));
3876}
3877
3878LLVMValueRef LLVMBuildMemCpy(LLVMBuilderRef B,
3879 LLVMValueRef Dst, unsigned DstAlign,
3880 LLVMValueRef Src, unsigned SrcAlign,
3881 LLVMValueRef Size) {
3882 return wrap(P: unwrap(P: B)->CreateMemCpy(Dst: unwrap(P: Dst), DstAlign: MaybeAlign(DstAlign),
3883 Src: unwrap(P: Src), SrcAlign: MaybeAlign(SrcAlign),
3884 Size: unwrap(P: Size)));
3885}
3886
3887LLVMValueRef LLVMBuildMemMove(LLVMBuilderRef B,
3888 LLVMValueRef Dst, unsigned DstAlign,
3889 LLVMValueRef Src, unsigned SrcAlign,
3890 LLVMValueRef Size) {
3891 return wrap(P: unwrap(P: B)->CreateMemMove(Dst: unwrap(P: Dst), DstAlign: MaybeAlign(DstAlign),
3892 Src: unwrap(P: Src), SrcAlign: MaybeAlign(SrcAlign),
3893 Size: unwrap(P: Size)));
3894}
3895
3896LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
3897 const char *Name) {
3898 return wrap(P: unwrap(P: B)->CreateAlloca(Ty: unwrap(P: Ty), ArraySize: nullptr, Name));
3899}
3900
3901LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
3902 LLVMValueRef Val, const char *Name) {
3903 return wrap(P: unwrap(P: B)->CreateAlloca(Ty: unwrap(P: Ty), ArraySize: unwrap(P: Val), Name));
3904}
3905
3906LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
3907 return wrap(P: unwrap(P: B)->CreateFree(Source: unwrap(P: PointerVal)));
3908}
3909
3910LLVMValueRef LLVMBuildLoad2(LLVMBuilderRef B, LLVMTypeRef Ty,
3911 LLVMValueRef PointerVal, const char *Name) {
3912 return wrap(P: unwrap(P: B)->CreateLoad(Ty: unwrap(P: Ty), Ptr: unwrap(P: PointerVal), Name));
3913}
3914
3915LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
3916 LLVMValueRef PointerVal) {
3917 return wrap(P: unwrap(P: B)->CreateStore(Val: unwrap(P: Val), Ptr: unwrap(P: PointerVal)));
3918}
3919
3920static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
3921 switch (Ordering) {
3922 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
3923 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
3924 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
3925 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
3926 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
3927 case LLVMAtomicOrderingAcquireRelease:
3928 return AtomicOrdering::AcquireRelease;
3929 case LLVMAtomicOrderingSequentiallyConsistent:
3930 return AtomicOrdering::SequentiallyConsistent;
3931 }
3932
3933 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3934}
3935
3936static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
3937 switch (Ordering) {
3938 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
3939 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
3940 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
3941 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
3942 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
3943 case AtomicOrdering::AcquireRelease:
3944 return LLVMAtomicOrderingAcquireRelease;
3945 case AtomicOrdering::SequentiallyConsistent:
3946 return LLVMAtomicOrderingSequentiallyConsistent;
3947 }
3948
3949 llvm_unreachable("Invalid AtomicOrdering value!");
3950}
3951
3952static AtomicRMWInst::BinOp mapFromLLVMRMWBinOp(LLVMAtomicRMWBinOp BinOp) {
3953 switch (BinOp) {
3954 case LLVMAtomicRMWBinOpXchg: return AtomicRMWInst::Xchg;
3955 case LLVMAtomicRMWBinOpAdd: return AtomicRMWInst::Add;
3956 case LLVMAtomicRMWBinOpSub: return AtomicRMWInst::Sub;
3957 case LLVMAtomicRMWBinOpAnd: return AtomicRMWInst::And;
3958 case LLVMAtomicRMWBinOpNand: return AtomicRMWInst::Nand;
3959 case LLVMAtomicRMWBinOpOr: return AtomicRMWInst::Or;
3960 case LLVMAtomicRMWBinOpXor: return AtomicRMWInst::Xor;
3961 case LLVMAtomicRMWBinOpMax: return AtomicRMWInst::Max;
3962 case LLVMAtomicRMWBinOpMin: return AtomicRMWInst::Min;
3963 case LLVMAtomicRMWBinOpUMax: return AtomicRMWInst::UMax;
3964 case LLVMAtomicRMWBinOpUMin: return AtomicRMWInst::UMin;
3965 case LLVMAtomicRMWBinOpFAdd: return AtomicRMWInst::FAdd;
3966 case LLVMAtomicRMWBinOpFSub: return AtomicRMWInst::FSub;
3967 case LLVMAtomicRMWBinOpFMax: return AtomicRMWInst::FMax;
3968 case LLVMAtomicRMWBinOpFMin: return AtomicRMWInst::FMin;
3969 case LLVMAtomicRMWBinOpFMaximum:
3970 return AtomicRMWInst::FMaximum;
3971 case LLVMAtomicRMWBinOpFMinimum:
3972 return AtomicRMWInst::FMinimum;
3973 case LLVMAtomicRMWBinOpUIncWrap:
3974 return AtomicRMWInst::UIncWrap;
3975 case LLVMAtomicRMWBinOpUDecWrap:
3976 return AtomicRMWInst::UDecWrap;
3977 case LLVMAtomicRMWBinOpUSubCond:
3978 return AtomicRMWInst::USubCond;
3979 case LLVMAtomicRMWBinOpUSubSat:
3980 return AtomicRMWInst::USubSat;
3981 }
3982
3983 llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3984}
3985
3986static LLVMAtomicRMWBinOp mapToLLVMRMWBinOp(AtomicRMWInst::BinOp BinOp) {
3987 switch (BinOp) {
3988 case AtomicRMWInst::Xchg: return LLVMAtomicRMWBinOpXchg;
3989 case AtomicRMWInst::Add: return LLVMAtomicRMWBinOpAdd;
3990 case AtomicRMWInst::Sub: return LLVMAtomicRMWBinOpSub;
3991 case AtomicRMWInst::And: return LLVMAtomicRMWBinOpAnd;
3992 case AtomicRMWInst::Nand: return LLVMAtomicRMWBinOpNand;
3993 case AtomicRMWInst::Or: return LLVMAtomicRMWBinOpOr;
3994 case AtomicRMWInst::Xor: return LLVMAtomicRMWBinOpXor;
3995 case AtomicRMWInst::Max: return LLVMAtomicRMWBinOpMax;
3996 case AtomicRMWInst::Min: return LLVMAtomicRMWBinOpMin;
3997 case AtomicRMWInst::UMax: return LLVMAtomicRMWBinOpUMax;
3998 case AtomicRMWInst::UMin: return LLVMAtomicRMWBinOpUMin;
3999 case AtomicRMWInst::FAdd: return LLVMAtomicRMWBinOpFAdd;
4000 case AtomicRMWInst::FSub: return LLVMAtomicRMWBinOpFSub;
4001 case AtomicRMWInst::FMax: return LLVMAtomicRMWBinOpFMax;
4002 case AtomicRMWInst::FMin: return LLVMAtomicRMWBinOpFMin;
4003 case AtomicRMWInst::FMaximum:
4004 return LLVMAtomicRMWBinOpFMaximum;
4005 case AtomicRMWInst::FMinimum:
4006 return LLVMAtomicRMWBinOpFMinimum;
4007 case AtomicRMWInst::UIncWrap:
4008 return LLVMAtomicRMWBinOpUIncWrap;
4009 case AtomicRMWInst::UDecWrap:
4010 return LLVMAtomicRMWBinOpUDecWrap;
4011 case AtomicRMWInst::USubCond:
4012 return LLVMAtomicRMWBinOpUSubCond;
4013 case AtomicRMWInst::USubSat:
4014 return LLVMAtomicRMWBinOpUSubSat;
4015 default: break;
4016 }
4017
4018 llvm_unreachable("Invalid AtomicRMWBinOp value!");
4019}
4020
4021LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
4022 LLVMBool isSingleThread, const char *Name) {
4023 return wrap(
4024 P: unwrap(P: B)->CreateFence(Ordering: mapFromLLVMOrdering(Ordering),
4025 SSID: isSingleThread ? SyncScope::SingleThread
4026 : SyncScope::System,
4027 Name));
4028}
4029
4030LLVMValueRef LLVMBuildFenceSyncScope(LLVMBuilderRef B,
4031 LLVMAtomicOrdering Ordering, unsigned SSID,
4032 const char *Name) {
4033 return wrap(
4034 P: unwrap(P: B)->CreateFence(Ordering: mapFromLLVMOrdering(Ordering), SSID, Name));
4035}
4036
4037LLVMValueRef LLVMBuildGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
4038 LLVMValueRef Pointer, LLVMValueRef *Indices,
4039 unsigned NumIndices, const char *Name) {
4040 ArrayRef<Value *> IdxList(unwrap(Vals: Indices), NumIndices);
4041 return wrap(P: unwrap(P: B)->CreateGEP(Ty: unwrap(P: Ty), Ptr: unwrap(P: Pointer), IdxList, Name));
4042}
4043
4044LLVMValueRef LLVMBuildInBoundsGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
4045 LLVMValueRef Pointer, LLVMValueRef *Indices,
4046 unsigned NumIndices, const char *Name) {
4047 ArrayRef<Value *> IdxList(unwrap(Vals: Indices), NumIndices);
4048 return wrap(
4049 P: unwrap(P: B)->CreateInBoundsGEP(Ty: unwrap(P: Ty), Ptr: unwrap(P: Pointer), IdxList, Name));
4050}
4051
4052LLVMValueRef LLVMBuildGEPWithNoWrapFlags(LLVMBuilderRef B, LLVMTypeRef Ty,
4053 LLVMValueRef Pointer,
4054 LLVMValueRef *Indices,
4055 unsigned NumIndices, const char *Name,
4056 LLVMGEPNoWrapFlags NoWrapFlags) {
4057 ArrayRef<Value *> IdxList(unwrap(Vals: Indices), NumIndices);
4058 return wrap(P: unwrap(P: B)->CreateGEP(Ty: unwrap(P: Ty), Ptr: unwrap(P: Pointer), IdxList, Name,
4059 NW: mapFromLLVMGEPNoWrapFlags(GEPFlags: NoWrapFlags)));
4060}
4061
4062LLVMValueRef LLVMBuildStructGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
4063 LLVMValueRef Pointer, unsigned Idx,
4064 const char *Name) {
4065 return wrap(
4066 P: unwrap(P: B)->CreateStructGEP(Ty: unwrap(P: Ty), Ptr: unwrap(P: Pointer), Idx, Name));
4067}
4068
4069LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
4070 const char *Name) {
4071 return wrap(P: unwrap(P: B)->CreateGlobalString(Str, Name));
4072}
4073
4074LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
4075 const char *Name) {
4076 return wrap(P: unwrap(P: B)->CreateGlobalString(Str, Name));
4077}
4078
4079LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
4080 Value *P = unwrap(P: MemAccessInst);
4081 if (LoadInst *LI = dyn_cast<LoadInst>(Val: P))
4082 return LI->isVolatile();
4083 if (StoreInst *SI = dyn_cast<StoreInst>(Val: P))
4084 return SI->isVolatile();
4085 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(Val: P))
4086 return AI->isVolatile();
4087 return cast<AtomicCmpXchgInst>(Val: P)->isVolatile();
4088}
4089
4090void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
4091 Value *P = unwrap(P: MemAccessInst);
4092 if (LoadInst *LI = dyn_cast<LoadInst>(Val: P))
4093 return LI->setVolatile(isVolatile);
4094 if (StoreInst *SI = dyn_cast<StoreInst>(Val: P))
4095 return SI->setVolatile(isVolatile);
4096 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(Val: P))
4097 return AI->setVolatile(isVolatile);
4098 return cast<AtomicCmpXchgInst>(Val: P)->setVolatile(isVolatile);
4099}
4100
4101LLVMBool LLVMGetWeak(LLVMValueRef CmpXchgInst) {
4102 return unwrap<AtomicCmpXchgInst>(P: CmpXchgInst)->isWeak();
4103}
4104
4105void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) {
4106 return unwrap<AtomicCmpXchgInst>(P: CmpXchgInst)->setWeak(isWeak);
4107}
4108
4109LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
4110 Value *P = unwrap(P: MemAccessInst);
4111 AtomicOrdering O;
4112 if (LoadInst *LI = dyn_cast<LoadInst>(Val: P))
4113 O = LI->getOrdering();
4114 else if (StoreInst *SI = dyn_cast<StoreInst>(Val: P))
4115 O = SI->getOrdering();
4116 else if (FenceInst *FI = dyn_cast<FenceInst>(Val: P))
4117 O = FI->getOrdering();
4118 else
4119 O = cast<AtomicRMWInst>(Val: P)->getOrdering();
4120 return mapToLLVMOrdering(Ordering: O);
4121}
4122
4123void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
4124 Value *P = unwrap(P: MemAccessInst);
4125 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
4126
4127 if (LoadInst *LI = dyn_cast<LoadInst>(Val: P))
4128 return LI->setOrdering(O);
4129 else if (FenceInst *FI = dyn_cast<FenceInst>(Val: P))
4130 return FI->setOrdering(O);
4131 else if (AtomicRMWInst *ARWI = dyn_cast<AtomicRMWInst>(Val: P))
4132 return ARWI->setOrdering(O);
4133 return cast<StoreInst>(Val: P)->setOrdering(O);
4134}
4135
4136LLVMAtomicRMWBinOp LLVMGetAtomicRMWBinOp(LLVMValueRef Inst) {
4137 return mapToLLVMRMWBinOp(BinOp: unwrap<AtomicRMWInst>(P: Inst)->getOperation());
4138}
4139
4140void LLVMSetAtomicRMWBinOp(LLVMValueRef Inst, LLVMAtomicRMWBinOp BinOp) {
4141 unwrap<AtomicRMWInst>(P: Inst)->setOperation(mapFromLLVMRMWBinOp(BinOp));
4142}
4143
4144/*--.. Casts ...............................................................--*/
4145
4146LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
4147 LLVMTypeRef DestTy, const char *Name) {
4148 return wrap(P: unwrap(P: B)->CreateTrunc(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4149}
4150
4151LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
4152 LLVMTypeRef DestTy, const char *Name) {
4153 return wrap(P: unwrap(P: B)->CreateZExt(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4154}
4155
4156LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
4157 LLVMTypeRef DestTy, const char *Name) {
4158 return wrap(P: unwrap(P: B)->CreateSExt(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4159}
4160
4161LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
4162 LLVMTypeRef DestTy, const char *Name) {
4163 return wrap(P: unwrap(P: B)->CreateFPToUI(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4164}
4165
4166LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
4167 LLVMTypeRef DestTy, const char *Name) {
4168 return wrap(P: unwrap(P: B)->CreateFPToSI(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4169}
4170
4171LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
4172 LLVMTypeRef DestTy, const char *Name) {
4173 return wrap(P: unwrap(P: B)->CreateUIToFP(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4174}
4175
4176LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
4177 LLVMTypeRef DestTy, const char *Name) {
4178 return wrap(P: unwrap(P: B)->CreateSIToFP(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4179}
4180
4181LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
4182 LLVMTypeRef DestTy, const char *Name) {
4183 return wrap(P: unwrap(P: B)->CreateFPTrunc(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4184}
4185
4186LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
4187 LLVMTypeRef DestTy, const char *Name) {
4188 return wrap(P: unwrap(P: B)->CreateFPExt(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4189}
4190
4191LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
4192 LLVMTypeRef DestTy, const char *Name) {
4193 return wrap(P: unwrap(P: B)->CreatePtrToInt(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4194}
4195
4196LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
4197 LLVMTypeRef DestTy, const char *Name) {
4198 return wrap(P: unwrap(P: B)->CreateIntToPtr(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4199}
4200
4201LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
4202 LLVMTypeRef DestTy, const char *Name) {
4203 return wrap(P: unwrap(P: B)->CreateBitCast(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4204}
4205
4206LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
4207 LLVMTypeRef DestTy, const char *Name) {
4208 return wrap(P: unwrap(P: B)->CreateAddrSpaceCast(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4209}
4210
4211LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
4212 LLVMTypeRef DestTy, const char *Name) {
4213 return wrap(P: unwrap(P: B)->CreateZExtOrBitCast(V: unwrap(P: Val), DestTy: unwrap(P: DestTy),
4214 Name));
4215}
4216
4217LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
4218 LLVMTypeRef DestTy, const char *Name) {
4219 return wrap(P: unwrap(P: B)->CreateSExtOrBitCast(V: unwrap(P: Val), DestTy: unwrap(P: DestTy),
4220 Name));
4221}
4222
4223LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
4224 LLVMTypeRef DestTy, const char *Name) {
4225 return wrap(P: unwrap(P: B)->CreateTruncOrBitCast(V: unwrap(P: Val), DestTy: unwrap(P: DestTy),
4226 Name));
4227}
4228
4229LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
4230 LLVMTypeRef DestTy, const char *Name) {
4231 return wrap(P: unwrap(P: B)->CreateCast(Op: Instruction::CastOps(map_from_llvmopcode(code: Op)), V: unwrap(P: Val),
4232 DestTy: unwrap(P: DestTy), Name));
4233}
4234
4235LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
4236 LLVMTypeRef DestTy, const char *Name) {
4237 return wrap(P: unwrap(P: B)->CreatePointerCast(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4238}
4239
4240LLVMValueRef LLVMBuildIntCast2(LLVMBuilderRef B, LLVMValueRef Val,
4241 LLVMTypeRef DestTy, LLVMBool IsSigned,
4242 const char *Name) {
4243 return wrap(
4244 P: unwrap(P: B)->CreateIntCast(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), isSigned: IsSigned, Name));
4245}
4246
4247LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
4248 LLVMTypeRef DestTy, const char *Name) {
4249 return wrap(P: unwrap(P: B)->CreateIntCast(V: unwrap(P: Val), DestTy: unwrap(P: DestTy),
4250 /*isSigned*/true, Name));
4251}
4252
4253LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
4254 LLVMTypeRef DestTy, const char *Name) {
4255 return wrap(P: unwrap(P: B)->CreateFPCast(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4256}
4257
4258LLVMOpcode LLVMGetCastOpcode(LLVMValueRef Src, LLVMBool SrcIsSigned,
4259 LLVMTypeRef DestTy, LLVMBool DestIsSigned) {
4260 return map_to_llvmopcode(opcode: CastInst::getCastOpcode(
4261 Val: unwrap(P: Src), SrcIsSigned, Ty: unwrap(P: DestTy), DstIsSigned: DestIsSigned));
4262}
4263
4264/*--.. Comparisons .........................................................--*/
4265
4266LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
4267 LLVMValueRef LHS, LLVMValueRef RHS,
4268 const char *Name) {
4269 return wrap(P: unwrap(P: B)->CreateICmp(P: static_cast<ICmpInst::Predicate>(Op),
4270 LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
4271}
4272
4273LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
4274 LLVMValueRef LHS, LLVMValueRef RHS,
4275 const char *Name) {
4276 return wrap(P: unwrap(P: B)->CreateFCmp(P: static_cast<FCmpInst::Predicate>(Op),
4277 LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
4278}
4279
4280/*--.. Miscellaneous instructions ..........................................--*/
4281
4282LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
4283 return wrap(P: unwrap(P: B)->CreatePHI(Ty: unwrap(P: Ty), NumReservedValues: 0, Name));
4284}
4285
4286LLVMValueRef LLVMBuildCall2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
4287 LLVMValueRef *Args, unsigned NumArgs,
4288 const char *Name) {
4289 FunctionType *FTy = unwrap<FunctionType>(P: Ty);
4290 return wrap(P: unwrap(P: B)->CreateCall(FTy, Callee: unwrap(P: Fn),
4291 Args: ArrayRef(unwrap(Vals: Args), NumArgs), Name));
4292}
4293
4294LLVMValueRef
4295LLVMBuildCallWithOperandBundles(LLVMBuilderRef B, LLVMTypeRef Ty,
4296 LLVMValueRef Fn, LLVMValueRef *Args,
4297 unsigned NumArgs, LLVMOperandBundleRef *Bundles,
4298 unsigned NumBundles, const char *Name) {
4299 FunctionType *FTy = unwrap<FunctionType>(P: Ty);
4300 SmallVector<OperandBundleDef, 8> OBs;
4301 for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
4302 OperandBundleDef *OB = unwrap(P: Bundle);
4303 OBs.push_back(Elt: *OB);
4304 }
4305 return wrap(P: unwrap(P: B)->CreateCall(
4306 FTy, Callee: unwrap(P: Fn), Args: ArrayRef(unwrap(Vals: Args), NumArgs), OpBundles: OBs, Name));
4307}
4308
4309LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
4310 LLVMValueRef Then, LLVMValueRef Else,
4311 const char *Name) {
4312 return wrap(P: unwrap(P: B)->CreateSelect(C: unwrap(P: If), True: unwrap(P: Then), False: unwrap(P: Else),
4313 Name));
4314}
4315
4316LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
4317 LLVMTypeRef Ty, const char *Name) {
4318 return wrap(P: unwrap(P: B)->CreateVAArg(List: unwrap(P: List), Ty: unwrap(P: Ty), Name));
4319}
4320
4321LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
4322 LLVMValueRef Index, const char *Name) {
4323 return wrap(P: unwrap(P: B)->CreateExtractElement(Vec: unwrap(P: VecVal), Idx: unwrap(P: Index),
4324 Name));
4325}
4326
4327LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
4328 LLVMValueRef EltVal, LLVMValueRef Index,
4329 const char *Name) {
4330 return wrap(P: unwrap(P: B)->CreateInsertElement(Vec: unwrap(P: VecVal), NewElt: unwrap(P: EltVal),
4331 Idx: unwrap(P: Index), Name));
4332}
4333
4334LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
4335 LLVMValueRef V2, LLVMValueRef Mask,
4336 const char *Name) {
4337 return wrap(P: unwrap(P: B)->CreateShuffleVector(V1: unwrap(P: V1), V2: unwrap(P: V2),
4338 Mask: unwrap(P: Mask), Name));
4339}
4340
4341LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
4342 unsigned Index, const char *Name) {
4343 return wrap(P: unwrap(P: B)->CreateExtractValue(Agg: unwrap(P: AggVal), Idxs: Index, Name));
4344}
4345
4346LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
4347 LLVMValueRef EltVal, unsigned Index,
4348 const char *Name) {
4349 return wrap(P: unwrap(P: B)->CreateInsertValue(Agg: unwrap(P: AggVal), Val: unwrap(P: EltVal),
4350 Idxs: Index, Name));
4351}
4352
4353LLVMValueRef LLVMBuildFreeze(LLVMBuilderRef B, LLVMValueRef Val,
4354 const char *Name) {
4355 return wrap(P: unwrap(P: B)->CreateFreeze(V: unwrap(P: Val), Name));
4356}
4357
4358LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
4359 const char *Name) {
4360 return wrap(P: unwrap(P: B)->CreateIsNull(Arg: unwrap(P: Val), Name));
4361}
4362
4363LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
4364 const char *Name) {
4365 return wrap(P: unwrap(P: B)->CreateIsNotNull(Arg: unwrap(P: Val), Name));
4366}
4367
4368LLVMValueRef LLVMBuildPtrDiff2(LLVMBuilderRef B, LLVMTypeRef ElemTy,
4369 LLVMValueRef LHS, LLVMValueRef RHS,
4370 const char *Name) {
4371 return wrap(P: unwrap(P: B)->CreatePtrDiff(ElemTy: unwrap(P: ElemTy), LHS: unwrap(P: LHS),
4372 RHS: unwrap(P: RHS), Name));
4373}
4374
4375LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
4376 LLVMValueRef PTR, LLVMValueRef Val,
4377 LLVMAtomicOrdering ordering,
4378 LLVMBool singleThread) {
4379 AtomicRMWInst::BinOp intop = mapFromLLVMRMWBinOp(BinOp: op);
4380 return wrap(P: unwrap(P: B)->CreateAtomicRMW(
4381 Op: intop, Ptr: unwrap(P: PTR), Val: unwrap(P: Val), Align: MaybeAlign(),
4382 Ordering: mapFromLLVMOrdering(Ordering: ordering),
4383 SSID: singleThread ? SyncScope::SingleThread : SyncScope::System));
4384}
4385
4386LLVMValueRef LLVMBuildAtomicRMWSyncScope(LLVMBuilderRef B,
4387 LLVMAtomicRMWBinOp op,
4388 LLVMValueRef PTR, LLVMValueRef Val,
4389 LLVMAtomicOrdering ordering,
4390 unsigned SSID) {
4391 AtomicRMWInst::BinOp intop = mapFromLLVMRMWBinOp(BinOp: op);
4392 return wrap(P: unwrap(P: B)->CreateAtomicRMW(Op: intop, Ptr: unwrap(P: PTR), Val: unwrap(P: Val),
4393 Align: MaybeAlign(),
4394 Ordering: mapFromLLVMOrdering(Ordering: ordering), SSID));
4395}
4396
4397LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
4398 LLVMValueRef Cmp, LLVMValueRef New,
4399 LLVMAtomicOrdering SuccessOrdering,
4400 LLVMAtomicOrdering FailureOrdering,
4401 LLVMBool singleThread) {
4402
4403 return wrap(P: unwrap(P: B)->CreateAtomicCmpXchg(
4404 Ptr: unwrap(P: Ptr), Cmp: unwrap(P: Cmp), New: unwrap(P: New), Align: MaybeAlign(),
4405 SuccessOrdering: mapFromLLVMOrdering(Ordering: SuccessOrdering),
4406 FailureOrdering: mapFromLLVMOrdering(Ordering: FailureOrdering),
4407 SSID: singleThread ? SyncScope::SingleThread : SyncScope::System));
4408}
4409
4410LLVMValueRef LLVMBuildAtomicCmpXchgSyncScope(LLVMBuilderRef B, LLVMValueRef Ptr,
4411 LLVMValueRef Cmp, LLVMValueRef New,
4412 LLVMAtomicOrdering SuccessOrdering,
4413 LLVMAtomicOrdering FailureOrdering,
4414 unsigned SSID) {
4415 return wrap(P: unwrap(P: B)->CreateAtomicCmpXchg(
4416 Ptr: unwrap(P: Ptr), Cmp: unwrap(P: Cmp), New: unwrap(P: New), Align: MaybeAlign(),
4417 SuccessOrdering: mapFromLLVMOrdering(Ordering: SuccessOrdering),
4418 FailureOrdering: mapFromLLVMOrdering(Ordering: FailureOrdering), SSID));
4419}
4420
4421unsigned LLVMGetNumMaskElements(LLVMValueRef SVInst) {
4422 Value *P = unwrap(P: SVInst);
4423 ShuffleVectorInst *I = cast<ShuffleVectorInst>(Val: P);
4424 return I->getShuffleMask().size();
4425}
4426
4427int LLVMGetMaskValue(LLVMValueRef SVInst, unsigned Elt) {
4428 Value *P = unwrap(P: SVInst);
4429 ShuffleVectorInst *I = cast<ShuffleVectorInst>(Val: P);
4430 return I->getMaskValue(Elt);
4431}
4432
4433int LLVMGetUndefMaskElem(void) { return PoisonMaskElem; }
4434
4435LLVMBool LLVMIsAtomic(LLVMValueRef Inst) {
4436 return unwrap<Instruction>(P: Inst)->isAtomic();
4437}
4438
4439LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
4440 // Backwards compatibility: return false for non-atomic instructions
4441 Instruction *I = unwrap<Instruction>(P: AtomicInst);
4442 if (!I->isAtomic())
4443 return 0;
4444
4445 return *getAtomicSyncScopeID(I) == SyncScope::SingleThread;
4446}
4447
4448void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
4449 // Backwards compatibility: ignore non-atomic instructions
4450 Instruction *I = unwrap<Instruction>(P: AtomicInst);
4451 if (!I->isAtomic())
4452 return;
4453
4454 SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
4455 setAtomicSyncScopeID(I, SSID);
4456}
4457
4458unsigned LLVMGetAtomicSyncScopeID(LLVMValueRef AtomicInst) {
4459 Instruction *I = unwrap<Instruction>(P: AtomicInst);
4460 assert(I->isAtomic() && "Expected an atomic instruction");
4461 return *getAtomicSyncScopeID(I);
4462}
4463
4464void LLVMSetAtomicSyncScopeID(LLVMValueRef AtomicInst, unsigned SSID) {
4465 Instruction *I = unwrap<Instruction>(P: AtomicInst);
4466 assert(I->isAtomic() && "Expected an atomic instruction");
4467 setAtomicSyncScopeID(I, SSID);
4468}
4469
4470LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
4471 Value *P = unwrap(P: CmpXchgInst);
4472 return mapToLLVMOrdering(Ordering: cast<AtomicCmpXchgInst>(Val: P)->getSuccessOrdering());
4473}
4474
4475void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
4476 LLVMAtomicOrdering Ordering) {
4477 Value *P = unwrap(P: CmpXchgInst);
4478 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
4479
4480 return cast<AtomicCmpXchgInst>(Val: P)->setSuccessOrdering(O);
4481}
4482
4483LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
4484 Value *P = unwrap(P: CmpXchgInst);
4485 return mapToLLVMOrdering(Ordering: cast<AtomicCmpXchgInst>(Val: P)->getFailureOrdering());
4486}
4487
4488void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
4489 LLVMAtomicOrdering Ordering) {
4490 Value *P = unwrap(P: CmpXchgInst);
4491 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
4492
4493 return cast<AtomicCmpXchgInst>(Val: P)->setFailureOrdering(O);
4494}
4495
4496/*===-- Module providers --------------------------------------------------===*/
4497
4498LLVMModuleProviderRef
4499LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
4500 return reinterpret_cast<LLVMModuleProviderRef>(M);
4501}
4502
4503void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
4504 delete unwrap(MP);
4505}
4506
4507
4508/*===-- Memory buffers ----------------------------------------------------===*/
4509
4510LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
4511 const char *Path,
4512 LLVMMemoryBufferRef *OutMemBuf,
4513 char **OutMessage) {
4514
4515 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Filename: Path);
4516 if (std::error_code EC = MBOrErr.getError()) {
4517 *OutMessage = strdup(s: EC.message().c_str());
4518 return 1;
4519 }
4520 *OutMemBuf = wrap(P: MBOrErr.get().release());
4521 return 0;
4522}
4523
4524LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
4525 char **OutMessage) {
4526 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
4527 if (std::error_code EC = MBOrErr.getError()) {
4528 *OutMessage = strdup(s: EC.message().c_str());
4529 return 1;
4530 }
4531 *OutMemBuf = wrap(P: MBOrErr.get().release());
4532 return 0;
4533}
4534
4535LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
4536 const char *InputData,
4537 size_t InputDataLength,
4538 const char *BufferName,
4539 LLVMBool RequiresNullTerminator) {
4540
4541 return wrap(P: MemoryBuffer::getMemBuffer(InputData: StringRef(InputData, InputDataLength),
4542 BufferName: StringRef(BufferName),
4543 RequiresNullTerminator).release());
4544}
4545
4546LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
4547 const char *InputData,
4548 size_t InputDataLength,
4549 const char *BufferName) {
4550
4551 return wrap(
4552 P: MemoryBuffer::getMemBufferCopy(InputData: StringRef(InputData, InputDataLength),
4553 BufferName: StringRef(BufferName)).release());
4554}
4555
4556const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
4557 return unwrap(P: MemBuf)->getBufferStart();
4558}
4559
4560size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
4561 return unwrap(P: MemBuf)->getBufferSize();
4562}
4563
4564void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
4565 delete unwrap(P: MemBuf);
4566}
4567
4568/*===-- Pass Manager ------------------------------------------------------===*/
4569
4570LLVMPassManagerRef LLVMCreatePassManager() {
4571 return wrap(P: new legacy::PassManager());
4572}
4573
4574LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
4575 return wrap(P: new legacy::FunctionPassManager(unwrap(P: M)));
4576}
4577
4578LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
4579 return LLVMCreateFunctionPassManagerForModule(
4580 M: reinterpret_cast<LLVMModuleRef>(P));
4581}
4582
4583LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
4584 return unwrap<legacy::PassManager>(P: PM)->run(M&: *unwrap(P: M));
4585}
4586
4587LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
4588 return unwrap<legacy::FunctionPassManager>(P: FPM)->doInitialization();
4589}
4590
4591LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
4592 return unwrap<legacy::FunctionPassManager>(P: FPM)->run(F&: *unwrap<Function>(P: F));
4593}
4594
4595LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
4596 return unwrap<legacy::FunctionPassManager>(P: FPM)->doFinalization();
4597}
4598
4599void LLVMDisposePassManager(LLVMPassManagerRef PM) {
4600 delete unwrap(P: PM);
4601}
4602
4603/*===-- Threading ------------------------------------------------------===*/
4604
4605LLVMBool LLVMStartMultithreaded() {
4606 return LLVMIsMultithreaded();
4607}
4608
4609void LLVMStopMultithreaded() {
4610}
4611
4612LLVMBool LLVMIsMultithreaded() {
4613 return llvm_is_multithreaded();
4614}
4615