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