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