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