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