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 LLVMIsDSOLocal(LLVMValueRef Global) {
2210 return unwrap<GlobalValue>(P: Global)->isDSOLocal();
2211}
2212
2213void LLVMSetDSOLocal(LLVMValueRef Global, LLVMBool IsDSOLocal) {
2214 unwrap<GlobalValue>(P: Global)->setDSOLocal(IsDSOLocal);
2215}
2216
2217LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
2218 return unwrap<GlobalValue>(P: Global)->hasGlobalUnnamedAddr();
2219}
2220
2221void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
2222 unwrap<GlobalValue>(P: Global)->setUnnamedAddr(
2223 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
2224 : GlobalValue::UnnamedAddr::None);
2225}
2226
2227LLVMTypeRef LLVMGlobalGetValueType(LLVMValueRef Global) {
2228 return wrap(P: unwrap<GlobalValue>(P: Global)->getValueType());
2229}
2230
2231/*--.. Operations on global variables, load and store instructions .........--*/
2232
2233unsigned LLVMGetAlignment(LLVMValueRef V) {
2234 Value *P = unwrap(P: V);
2235 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Val: P))
2236 return GV->getAlign() ? GV->getAlign()->value() : 0;
2237 if (Function *F = dyn_cast<Function>(Val: P))
2238 return F->getAlign() ? F->getAlign()->value() : 0;
2239 if (AllocaInst *AI = dyn_cast<AllocaInst>(Val: P))
2240 return AI->getAlign().value();
2241 if (LoadInst *LI = dyn_cast<LoadInst>(Val: P))
2242 return LI->getAlign().value();
2243 if (StoreInst *SI = dyn_cast<StoreInst>(Val: P))
2244 return SI->getAlign().value();
2245 if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(Val: P))
2246 return RMWI->getAlign().value();
2247 if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(Val: P))
2248 return CXI->getAlign().value();
2249
2250 llvm_unreachable(
2251 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2252 "and AtomicCmpXchgInst have alignment");
2253}
2254
2255void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2256 Value *P = unwrap(P: V);
2257 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Val: P))
2258 GV->setAlignment(MaybeAlign(Bytes));
2259 else if (Function *F = dyn_cast<Function>(Val: P))
2260 F->setAlignment(MaybeAlign(Bytes));
2261 else if (AllocaInst *AI = dyn_cast<AllocaInst>(Val: P))
2262 AI->setAlignment(Align(Bytes));
2263 else if (LoadInst *LI = dyn_cast<LoadInst>(Val: P))
2264 LI->setAlignment(Align(Bytes));
2265 else if (StoreInst *SI = dyn_cast<StoreInst>(Val: P))
2266 SI->setAlignment(Align(Bytes));
2267 else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(Val: P))
2268 RMWI->setAlignment(Align(Bytes));
2269 else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(Val: P))
2270 CXI->setAlignment(Align(Bytes));
2271 else
2272 llvm_unreachable(
2273 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2274 "and AtomicCmpXchgInst have alignment");
2275}
2276
2277LLVMValueMetadataEntry *LLVMGlobalCopyAllMetadata(LLVMValueRef Value,
2278 size_t *NumEntries) {
2279 return llvm_getMetadata(NumEntries, AccessMD: [&Value](MetadataEntries &Entries) {
2280 Entries.clear();
2281 if (Instruction *Instr = dyn_cast<Instruction>(Val: unwrap(P: Value))) {
2282 Instr->getAllMetadata(MDs&: Entries);
2283 } else {
2284 unwrap<GlobalObject>(P: Value)->getAllMetadata(MDs&: Entries);
2285 }
2286 });
2287}
2288
2289unsigned LLVMValueMetadataEntriesGetKind(LLVMValueMetadataEntry *Entries,
2290 unsigned Index) {
2291 LLVMOpaqueValueMetadataEntry MVE =
2292 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2293 return MVE.Kind;
2294}
2295
2296LLVMMetadataRef
2297LLVMValueMetadataEntriesGetMetadata(LLVMValueMetadataEntry *Entries,
2298 unsigned Index) {
2299 LLVMOpaqueValueMetadataEntry MVE =
2300 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2301 return MVE.Metadata;
2302}
2303
2304void LLVMDisposeValueMetadataEntries(LLVMValueMetadataEntry *Entries) {
2305 free(ptr: Entries);
2306}
2307
2308void LLVMGlobalSetMetadata(LLVMValueRef Global, unsigned Kind,
2309 LLVMMetadataRef MD) {
2310 unwrap<GlobalObject>(P: Global)->setMetadata(KindID: Kind, Node: unwrap<MDNode>(P: MD));
2311}
2312
2313void LLVMGlobalAddMetadata(LLVMValueRef Global, unsigned Kind,
2314 LLVMMetadataRef MD) {
2315 unwrap<GlobalObject>(P: Global)->addMetadata(KindID: Kind, MD&: *unwrap<MDNode>(P: MD));
2316}
2317
2318void LLVMGlobalEraseMetadata(LLVMValueRef Global, unsigned Kind) {
2319 unwrap<GlobalObject>(P: Global)->eraseMetadata(KindID: Kind);
2320}
2321
2322void LLVMGlobalClearMetadata(LLVMValueRef Global) {
2323 unwrap<GlobalObject>(P: Global)->clearMetadata();
2324}
2325
2326void LLVMGlobalAddDebugInfo(LLVMValueRef Global, LLVMMetadataRef GVE) {
2327 unwrap<GlobalVariable>(P: Global)->addDebugInfo(
2328 GV: unwrap<DIGlobalVariableExpression>(P: GVE));
2329}
2330
2331/*--.. Operations on global variables ......................................--*/
2332
2333LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
2334 return wrap(P: new GlobalVariable(*unwrap(P: M), unwrap(P: Ty), false,
2335 GlobalValue::ExternalLinkage, nullptr, Name));
2336}
2337
2338LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
2339 const char *Name,
2340 unsigned AddressSpace) {
2341 return wrap(P: new GlobalVariable(*unwrap(P: M), unwrap(P: Ty), false,
2342 GlobalValue::ExternalLinkage, nullptr, Name,
2343 nullptr, GlobalVariable::NotThreadLocal,
2344 AddressSpace));
2345}
2346
2347LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
2348 return wrap(P: unwrap(P: M)->getNamedGlobal(Name));
2349}
2350
2351LLVMValueRef LLVMGetNamedGlobalWithLength(LLVMModuleRef M, const char *Name,
2352 size_t Length) {
2353 return wrap(P: unwrap(P: M)->getNamedGlobal(Name: StringRef(Name, Length)));
2354}
2355
2356LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
2357 Module *Mod = unwrap(P: M);
2358 Module::global_iterator I = Mod->global_begin();
2359 if (I == Mod->global_end())
2360 return nullptr;
2361 return wrap(P: &*I);
2362}
2363
2364LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
2365 Module *Mod = unwrap(P: M);
2366 Module::global_iterator I = Mod->global_end();
2367 if (I == Mod->global_begin())
2368 return nullptr;
2369 return wrap(P: &*--I);
2370}
2371
2372LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
2373 GlobalVariable *GV = unwrap<GlobalVariable>(P: GlobalVar);
2374 Module::global_iterator I(GV);
2375 if (++I == GV->getParent()->global_end())
2376 return nullptr;
2377 return wrap(P: &*I);
2378}
2379
2380LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
2381 GlobalVariable *GV = unwrap<GlobalVariable>(P: GlobalVar);
2382 Module::global_iterator I(GV);
2383 if (I == GV->getParent()->global_begin())
2384 return nullptr;
2385 return wrap(P: &*--I);
2386}
2387
2388void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
2389 unwrap<GlobalVariable>(P: GlobalVar)->eraseFromParent();
2390}
2391
2392LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
2393 GlobalVariable* GV = unwrap<GlobalVariable>(P: GlobalVar);
2394 if ( !GV->hasInitializer() )
2395 return nullptr;
2396 return wrap(P: GV->getInitializer());
2397}
2398
2399void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
2400 unwrap<GlobalVariable>(P: GlobalVar)->setInitializer(
2401 ConstantVal ? unwrap<Constant>(P: ConstantVal) : nullptr);
2402}
2403
2404LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
2405 return unwrap<GlobalVariable>(P: GlobalVar)->isThreadLocal();
2406}
2407
2408void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
2409 unwrap<GlobalVariable>(P: GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2410}
2411
2412LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
2413 return unwrap<GlobalVariable>(P: GlobalVar)->isConstant();
2414}
2415
2416void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
2417 unwrap<GlobalVariable>(P: GlobalVar)->setConstant(IsConstant != 0);
2418}
2419
2420LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
2421 switch (unwrap<GlobalVariable>(P: GlobalVar)->getThreadLocalMode()) {
2422 case GlobalVariable::NotThreadLocal:
2423 return LLVMNotThreadLocal;
2424 case GlobalVariable::GeneralDynamicTLSModel:
2425 return LLVMGeneralDynamicTLSModel;
2426 case GlobalVariable::LocalDynamicTLSModel:
2427 return LLVMLocalDynamicTLSModel;
2428 case GlobalVariable::InitialExecTLSModel:
2429 return LLVMInitialExecTLSModel;
2430 case GlobalVariable::LocalExecTLSModel:
2431 return LLVMLocalExecTLSModel;
2432 }
2433
2434 llvm_unreachable("Invalid GlobalVariable thread local mode");
2435}
2436
2437void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
2438 GlobalVariable *GV = unwrap<GlobalVariable>(P: GlobalVar);
2439
2440 switch (Mode) {
2441 case LLVMNotThreadLocal:
2442 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
2443 break;
2444 case LLVMGeneralDynamicTLSModel:
2445 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
2446 break;
2447 case LLVMLocalDynamicTLSModel:
2448 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
2449 break;
2450 case LLVMInitialExecTLSModel:
2451 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
2452 break;
2453 case LLVMLocalExecTLSModel:
2454 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
2455 break;
2456 }
2457}
2458
2459LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
2460 return unwrap<GlobalVariable>(P: GlobalVar)->isExternallyInitialized();
2461}
2462
2463void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
2464 unwrap<GlobalVariable>(P: GlobalVar)->setExternallyInitialized(IsExtInit);
2465}
2466
2467/*--.. Operations on aliases ......................................--*/
2468
2469LLVMValueRef LLVMAddAlias2(LLVMModuleRef M, LLVMTypeRef ValueTy,
2470 unsigned AddrSpace, LLVMValueRef Aliasee,
2471 const char *Name) {
2472 return wrap(P: GlobalAlias::create(Ty: unwrap(P: ValueTy), AddressSpace: AddrSpace,
2473 Linkage: GlobalValue::ExternalLinkage, Name,
2474 Aliasee: unwrap<Constant>(P: Aliasee), Parent: unwrap(P: M)));
2475}
2476
2477LLVMValueRef LLVMGetNamedGlobalAlias(LLVMModuleRef M,
2478 const char *Name, size_t NameLen) {
2479 return wrap(P: unwrap(P: M)->getNamedAlias(Name: StringRef(Name, NameLen)));
2480}
2481
2482LLVMValueRef LLVMGetFirstGlobalAlias(LLVMModuleRef M) {
2483 Module *Mod = unwrap(P: M);
2484 Module::alias_iterator I = Mod->alias_begin();
2485 if (I == Mod->alias_end())
2486 return nullptr;
2487 return wrap(P: &*I);
2488}
2489
2490LLVMValueRef LLVMGetLastGlobalAlias(LLVMModuleRef M) {
2491 Module *Mod = unwrap(P: M);
2492 Module::alias_iterator I = Mod->alias_end();
2493 if (I == Mod->alias_begin())
2494 return nullptr;
2495 return wrap(P: &*--I);
2496}
2497
2498LLVMValueRef LLVMGetNextGlobalAlias(LLVMValueRef GA) {
2499 GlobalAlias *Alias = unwrap<GlobalAlias>(P: GA);
2500 Module::alias_iterator I(Alias);
2501 if (++I == Alias->getParent()->alias_end())
2502 return nullptr;
2503 return wrap(P: &*I);
2504}
2505
2506LLVMValueRef LLVMGetPreviousGlobalAlias(LLVMValueRef GA) {
2507 GlobalAlias *Alias = unwrap<GlobalAlias>(P: GA);
2508 Module::alias_iterator I(Alias);
2509 if (I == Alias->getParent()->alias_begin())
2510 return nullptr;
2511 return wrap(P: &*--I);
2512}
2513
2514LLVMValueRef LLVMAliasGetAliasee(LLVMValueRef Alias) {
2515 return wrap(P: unwrap<GlobalAlias>(P: Alias)->getAliasee());
2516}
2517
2518void LLVMAliasSetAliasee(LLVMValueRef Alias, LLVMValueRef Aliasee) {
2519 unwrap<GlobalAlias>(P: Alias)->setAliasee(unwrap<Constant>(P: Aliasee));
2520}
2521
2522/*--.. Operations on functions .............................................--*/
2523
2524LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
2525 LLVMTypeRef FunctionTy) {
2526 return wrap(P: Function::Create(Ty: unwrap<FunctionType>(P: FunctionTy),
2527 Linkage: GlobalValue::ExternalLinkage, N: Name, M: unwrap(P: M)));
2528}
2529
2530LLVMValueRef LLVMGetOrInsertFunction(LLVMModuleRef M, const char *Name,
2531 size_t NameLen, LLVMTypeRef FunctionTy) {
2532 return wrap(P: unwrap(P: M)
2533 ->getOrInsertFunction(Name: StringRef(Name, NameLen),
2534 T: unwrap<FunctionType>(P: FunctionTy))
2535 .getCallee());
2536}
2537
2538LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
2539 return wrap(P: unwrap(P: M)->getFunction(Name));
2540}
2541
2542LLVMValueRef LLVMGetNamedFunctionWithLength(LLVMModuleRef M, const char *Name,
2543 size_t Length) {
2544 return wrap(P: unwrap(P: M)->getFunction(Name: StringRef(Name, Length)));
2545}
2546
2547LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
2548 Module *Mod = unwrap(P: M);
2549 Module::iterator I = Mod->begin();
2550 if (I == Mod->end())
2551 return nullptr;
2552 return wrap(P: &*I);
2553}
2554
2555LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
2556 Module *Mod = unwrap(P: M);
2557 Module::iterator I = Mod->end();
2558 if (I == Mod->begin())
2559 return nullptr;
2560 return wrap(P: &*--I);
2561}
2562
2563LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
2564 Function *Func = unwrap<Function>(P: Fn);
2565 Module::iterator I(Func);
2566 if (++I == Func->getParent()->end())
2567 return nullptr;
2568 return wrap(P: &*I);
2569}
2570
2571LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
2572 Function *Func = unwrap<Function>(P: Fn);
2573 Module::iterator I(Func);
2574 if (I == Func->getParent()->begin())
2575 return nullptr;
2576 return wrap(P: &*--I);
2577}
2578
2579void LLVMDeleteFunction(LLVMValueRef Fn) {
2580 unwrap<Function>(P: Fn)->eraseFromParent();
2581}
2582
2583LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
2584 return unwrap<Function>(P: Fn)->hasPersonalityFn();
2585}
2586
2587LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
2588 return wrap(P: unwrap<Function>(P: Fn)->getPersonalityFn());
2589}
2590
2591void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
2592 unwrap<Function>(P: Fn)->setPersonalityFn(
2593 PersonalityFn ? unwrap<Constant>(P: PersonalityFn) : nullptr);
2594}
2595
2596unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
2597 if (Function *F = dyn_cast<Function>(Val: unwrap(P: Fn)))
2598 return F->getIntrinsicID();
2599 return 0;
2600}
2601
2602static Intrinsic::ID llvm_map_to_intrinsic_id(unsigned ID) {
2603 assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2604 return llvm::Intrinsic::ID(ID);
2605}
2606
2607LLVMValueRef LLVMGetIntrinsicDeclaration(LLVMModuleRef Mod, unsigned ID,
2608 LLVMTypeRef *OverloadTypes,
2609 size_t OverloadCount) {
2610 ArrayRef<Type *> OverloadTys(unwrap(Tys: OverloadTypes), OverloadCount);
2611 auto IID = llvm_map_to_intrinsic_id(ID);
2612 return wrap(
2613 P: llvm::Intrinsic::getOrInsertDeclaration(M: unwrap(P: Mod), id: IID, OverloadTys));
2614}
2615
2616const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2617 auto IID = llvm_map_to_intrinsic_id(ID);
2618 auto Str = llvm::Intrinsic::getName(id: IID);
2619 *NameLength = Str.size();
2620 return Str.data();
2621}
2622
2623LLVMTypeRef LLVMIntrinsicGetType(LLVMContextRef Ctx, unsigned ID,
2624 LLVMTypeRef *OverloadTypes,
2625 size_t OverloadCount) {
2626 auto IID = llvm_map_to_intrinsic_id(ID);
2627 ArrayRef<Type *> OverloadTys(unwrap(Tys: OverloadTypes), OverloadCount);
2628 return wrap(P: llvm::Intrinsic::getType(Context&: *unwrap(P: Ctx), id: IID, OverloadTys));
2629}
2630
2631char *LLVMIntrinsicCopyOverloadedName(unsigned ID, LLVMTypeRef *OverloadTypes,
2632 size_t OverloadCount,
2633 size_t *NameLength) {
2634 auto IID = llvm_map_to_intrinsic_id(ID);
2635 ArrayRef<Type *> OverloadTys(unwrap(Tys: OverloadTypes), OverloadCount);
2636 auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(Id: IID, OverloadTys);
2637 *NameLength = Str.length();
2638 return strdup(s: Str.c_str());
2639}
2640
2641char *LLVMIntrinsicCopyOverloadedName2(LLVMModuleRef Mod, unsigned ID,
2642 LLVMTypeRef *OverloadTypes,
2643 size_t OverloadCount,
2644 size_t *NameLength) {
2645 auto IID = llvm_map_to_intrinsic_id(ID);
2646 ArrayRef<Type *> OverloadTys(unwrap(Tys: OverloadTypes), OverloadCount);
2647 auto Str = llvm::Intrinsic::getName(Id: IID, OverloadTys, M: unwrap(P: Mod));
2648 *NameLength = Str.length();
2649 return strdup(s: Str.c_str());
2650}
2651
2652unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2653 return Intrinsic::lookupIntrinsicID(Name: {Name, NameLen});
2654}
2655
2656LLVMBool LLVMIntrinsicIsOverloaded(unsigned ID) {
2657 auto IID = llvm_map_to_intrinsic_id(ID);
2658 return llvm::Intrinsic::isOverloaded(id: IID);
2659}
2660
2661unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
2662 return unwrap<Function>(P: Fn)->getCallingConv();
2663}
2664
2665void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
2666 return unwrap<Function>(P: Fn)->setCallingConv(
2667 static_cast<CallingConv::ID>(CC));
2668}
2669
2670const char *LLVMGetGC(LLVMValueRef Fn) {
2671 Function *F = unwrap<Function>(P: Fn);
2672 return F->hasGC()? F->getGC().c_str() : nullptr;
2673}
2674
2675void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2676 Function *F = unwrap<Function>(P: Fn);
2677 if (GC)
2678 F->setGC(GC);
2679 else
2680 F->clearGC();
2681}
2682
2683LLVMValueRef LLVMGetPrefixData(LLVMValueRef Fn) {
2684 Function *F = unwrap<Function>(P: Fn);
2685 return wrap(P: F->getPrefixData());
2686}
2687
2688LLVMBool LLVMHasPrefixData(LLVMValueRef Fn) {
2689 Function *F = unwrap<Function>(P: Fn);
2690 return F->hasPrefixData();
2691}
2692
2693void LLVMSetPrefixData(LLVMValueRef Fn, LLVMValueRef prefixData) {
2694 Function *F = unwrap<Function>(P: Fn);
2695 Constant *prefix = unwrap<Constant>(P: prefixData);
2696 F->setPrefixData(prefix);
2697}
2698
2699LLVMValueRef LLVMGetPrologueData(LLVMValueRef Fn) {
2700 Function *F = unwrap<Function>(P: Fn);
2701 return wrap(P: F->getPrologueData());
2702}
2703
2704LLVMBool LLVMHasPrologueData(LLVMValueRef Fn) {
2705 Function *F = unwrap<Function>(P: Fn);
2706 return F->hasPrologueData();
2707}
2708
2709void LLVMSetPrologueData(LLVMValueRef Fn, LLVMValueRef prologueData) {
2710 Function *F = unwrap<Function>(P: Fn);
2711 Constant *prologue = unwrap<Constant>(P: prologueData);
2712 F->setPrologueData(prologue);
2713}
2714
2715void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2716 LLVMAttributeRef A) {
2717 unwrap<Function>(P: F)->addAttributeAtIndex(i: Idx, Attr: unwrap(Attr: A));
2718}
2719
2720unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
2721 auto AS = unwrap<Function>(P: F)->getAttributes().getAttributes(Index: Idx);
2722 return AS.getNumAttributes();
2723}
2724
2725void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2726 LLVMAttributeRef *Attrs) {
2727 auto AS = unwrap<Function>(P: F)->getAttributes().getAttributes(Index: Idx);
2728 for (auto A : AS)
2729 *Attrs++ = wrap(Attr: A);
2730}
2731
2732LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
2733 LLVMAttributeIndex Idx,
2734 unsigned KindID) {
2735 return wrap(Attr: unwrap<Function>(P: F)->getAttributeAtIndex(
2736 i: Idx, Kind: (Attribute::AttrKind)KindID));
2737}
2738
2739LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
2740 LLVMAttributeIndex Idx,
2741 const char *K, unsigned KLen) {
2742 return wrap(
2743 Attr: unwrap<Function>(P: F)->getAttributeAtIndex(i: Idx, Kind: StringRef(K, KLen)));
2744}
2745
2746void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2747 unsigned KindID) {
2748 unwrap<Function>(P: F)->removeAttributeAtIndex(i: Idx, Kind: (Attribute::AttrKind)KindID);
2749}
2750
2751void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2752 const char *K, unsigned KLen) {
2753 unwrap<Function>(P: F)->removeAttributeAtIndex(i: Idx, Kind: StringRef(K, KLen));
2754}
2755
2756void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
2757 const char *V) {
2758 Function *Func = unwrap<Function>(P: Fn);
2759 Attribute Attr = Attribute::get(Context&: Func->getContext(), Kind: A, Val: V);
2760 Func->addFnAttr(Attr);
2761}
2762
2763/*--.. Operations on parameters ............................................--*/
2764
2765unsigned LLVMCountParams(LLVMValueRef FnRef) {
2766 // This function is strictly redundant to
2767 // LLVMCountParamTypes(LLVMGlobalGetValueType(FnRef))
2768 return unwrap<Function>(P: FnRef)->arg_size();
2769}
2770
2771void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2772 Function *Fn = unwrap<Function>(P: FnRef);
2773 for (Argument &A : Fn->args())
2774 *ParamRefs++ = wrap(P: &A);
2775}
2776
2777LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
2778 Function *Fn = unwrap<Function>(P: FnRef);
2779 return wrap(P: &Fn->arg_begin()[index]);
2780}
2781
2782LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
2783 return wrap(P: unwrap<Argument>(P: V)->getParent());
2784}
2785
2786LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
2787 Function *Func = unwrap<Function>(P: Fn);
2788 Function::arg_iterator I = Func->arg_begin();
2789 if (I == Func->arg_end())
2790 return nullptr;
2791 return wrap(P: &*I);
2792}
2793
2794LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
2795 Function *Func = unwrap<Function>(P: Fn);
2796 Function::arg_iterator I = Func->arg_end();
2797 if (I == Func->arg_begin())
2798 return nullptr;
2799 return wrap(P: &*--I);
2800}
2801
2802LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
2803 Argument *A = unwrap<Argument>(P: Arg);
2804 Function *Fn = A->getParent();
2805 if (A->getArgNo() + 1 >= Fn->arg_size())
2806 return nullptr;
2807 return wrap(P: &Fn->arg_begin()[A->getArgNo() + 1]);
2808}
2809
2810LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
2811 Argument *A = unwrap<Argument>(P: Arg);
2812 if (A->getArgNo() == 0)
2813 return nullptr;
2814 return wrap(P: &A->getParent()->arg_begin()[A->getArgNo() - 1]);
2815}
2816
2817void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
2818 Argument *A = unwrap<Argument>(P: Arg);
2819 A->addAttr(Attr: Attribute::getWithAlignment(Context&: A->getContext(), Alignment: Align(align)));
2820}
2821
2822/*--.. Operations on ifuncs ................................................--*/
2823
2824LLVMValueRef LLVMAddGlobalIFunc(LLVMModuleRef M,
2825 const char *Name, size_t NameLen,
2826 LLVMTypeRef Ty, unsigned AddrSpace,
2827 LLVMValueRef Resolver) {
2828 return wrap(P: GlobalIFunc::create(Ty: unwrap(P: Ty), AddressSpace: AddrSpace,
2829 Linkage: GlobalValue::ExternalLinkage,
2830 Name: StringRef(Name, NameLen),
2831 Resolver: unwrap<Constant>(P: Resolver), Parent: unwrap(P: M)));
2832}
2833
2834LLVMValueRef LLVMGetNamedGlobalIFunc(LLVMModuleRef M,
2835 const char *Name, size_t NameLen) {
2836 return wrap(P: unwrap(P: M)->getNamedIFunc(Name: StringRef(Name, NameLen)));
2837}
2838
2839LLVMValueRef LLVMGetFirstGlobalIFunc(LLVMModuleRef M) {
2840 Module *Mod = unwrap(P: M);
2841 Module::ifunc_iterator I = Mod->ifunc_begin();
2842 if (I == Mod->ifunc_end())
2843 return nullptr;
2844 return wrap(P: &*I);
2845}
2846
2847LLVMValueRef LLVMGetLastGlobalIFunc(LLVMModuleRef M) {
2848 Module *Mod = unwrap(P: M);
2849 Module::ifunc_iterator I = Mod->ifunc_end();
2850 if (I == Mod->ifunc_begin())
2851 return nullptr;
2852 return wrap(P: &*--I);
2853}
2854
2855LLVMValueRef LLVMGetNextGlobalIFunc(LLVMValueRef IFunc) {
2856 GlobalIFunc *GIF = unwrap<GlobalIFunc>(P: IFunc);
2857 Module::ifunc_iterator I(GIF);
2858 if (++I == GIF->getParent()->ifunc_end())
2859 return nullptr;
2860 return wrap(P: &*I);
2861}
2862
2863LLVMValueRef LLVMGetPreviousGlobalIFunc(LLVMValueRef IFunc) {
2864 GlobalIFunc *GIF = unwrap<GlobalIFunc>(P: IFunc);
2865 Module::ifunc_iterator I(GIF);
2866 if (I == GIF->getParent()->ifunc_begin())
2867 return nullptr;
2868 return wrap(P: &*--I);
2869}
2870
2871LLVMValueRef LLVMGetGlobalIFuncResolver(LLVMValueRef IFunc) {
2872 return wrap(P: unwrap<GlobalIFunc>(P: IFunc)->getResolver());
2873}
2874
2875void LLVMSetGlobalIFuncResolver(LLVMValueRef IFunc, LLVMValueRef Resolver) {
2876 unwrap<GlobalIFunc>(P: IFunc)->setResolver(unwrap<Constant>(P: Resolver));
2877}
2878
2879void LLVMEraseGlobalIFunc(LLVMValueRef IFunc) {
2880 unwrap<GlobalIFunc>(P: IFunc)->eraseFromParent();
2881}
2882
2883void LLVMRemoveGlobalIFunc(LLVMValueRef IFunc) {
2884 unwrap<GlobalIFunc>(P: IFunc)->removeFromParent();
2885}
2886
2887/*--.. Operations on operand bundles........................................--*/
2888
2889LLVMOperandBundleRef LLVMCreateOperandBundle(const char *Tag, size_t TagLen,
2890 LLVMValueRef *Args,
2891 unsigned NumArgs) {
2892 return wrap(P: new OperandBundleDef(std::string(Tag, TagLen),
2893 ArrayRef(unwrap(Vals: Args), NumArgs)));
2894}
2895
2896void LLVMDisposeOperandBundle(LLVMOperandBundleRef Bundle) {
2897 delete unwrap(P: Bundle);
2898}
2899
2900const char *LLVMGetOperandBundleTag(LLVMOperandBundleRef Bundle, size_t *Len) {
2901 StringRef Str = unwrap(P: Bundle)->getTag();
2902 *Len = Str.size();
2903 return Str.data();
2904}
2905
2906unsigned LLVMGetNumOperandBundleArgs(LLVMOperandBundleRef Bundle) {
2907 return unwrap(P: Bundle)->inputs().size();
2908}
2909
2910LLVMValueRef LLVMGetOperandBundleArgAtIndex(LLVMOperandBundleRef Bundle,
2911 unsigned Index) {
2912 return wrap(P: unwrap(P: Bundle)->inputs()[Index]);
2913}
2914
2915/*--.. Operations on basic blocks ..........................................--*/
2916
2917LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
2918 return wrap(P: static_cast<Value*>(unwrap(P: BB)));
2919}
2920
2921LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
2922 return isa<BasicBlock>(Val: unwrap(P: Val));
2923}
2924
2925LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
2926 return wrap(P: unwrap<BasicBlock>(P: Val));
2927}
2928
2929const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
2930 return unwrap(P: BB)->getName().data();
2931}
2932
2933LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
2934 return wrap(P: unwrap(P: BB)->getParent());
2935}
2936
2937LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
2938 return wrap(P: unwrap(P: BB)->getTerminatorOrNull());
2939}
2940
2941unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
2942 return unwrap<Function>(P: FnRef)->size();
2943}
2944
2945void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2946 Function *Fn = unwrap<Function>(P: FnRef);
2947 for (BasicBlock &BB : *Fn)
2948 *BasicBlocksRefs++ = wrap(P: &BB);
2949}
2950
2951LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2952 return wrap(P: &unwrap<Function>(P: Fn)->getEntryBlock());
2953}
2954
2955LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2956 Function *Func = unwrap<Function>(P: Fn);
2957 Function::iterator I = Func->begin();
2958 if (I == Func->end())
2959 return nullptr;
2960 return wrap(P: &*I);
2961}
2962
2963LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2964 Function *Func = unwrap<Function>(P: Fn);
2965 Function::iterator I = Func->end();
2966 if (I == Func->begin())
2967 return nullptr;
2968 return wrap(P: &*--I);
2969}
2970
2971LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2972 BasicBlock *Block = unwrap(P: BB);
2973 Function::iterator I(Block);
2974 if (++I == Block->getParent()->end())
2975 return nullptr;
2976 return wrap(P: &*I);
2977}
2978
2979LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2980 BasicBlock *Block = unwrap(P: BB);
2981 Function::iterator I(Block);
2982 if (I == Block->getParent()->begin())
2983 return nullptr;
2984 return wrap(P: &*--I);
2985}
2986
2987LLVMBasicBlockRef LLVMCreateBasicBlockInContext(LLVMContextRef C,
2988 const char *Name) {
2989 return wrap(P: llvm::BasicBlock::Create(Context&: *unwrap(P: C), Name));
2990}
2991
2992void LLVMInsertExistingBasicBlockAfterInsertBlock(LLVMBuilderRef Builder,
2993 LLVMBasicBlockRef BB) {
2994 BasicBlock *ToInsert = unwrap(P: BB);
2995 BasicBlock *CurBB = unwrap(P: Builder)->GetInsertBlock();
2996 assert(CurBB && "current insertion point is invalid!");
2997 CurBB->getParent()->insert(Position: std::next(x: CurBB->getIterator()), BB: ToInsert);
2998}
2999
3000void LLVMAppendExistingBasicBlock(LLVMValueRef Fn,
3001 LLVMBasicBlockRef BB) {
3002 unwrap<Function>(P: Fn)->insert(Position: unwrap<Function>(P: Fn)->end(), BB: unwrap(P: BB));
3003}
3004
3005LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
3006 LLVMValueRef FnRef,
3007 const char *Name) {
3008 return wrap(P: BasicBlock::Create(Context&: *unwrap(P: C), Name, Parent: unwrap<Function>(P: FnRef)));
3009}
3010
3011LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
3012 return LLVMAppendBasicBlockInContext(C: getGlobalContextForCAPI(), FnRef, Name);
3013}
3014
3015LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
3016 LLVMBasicBlockRef BBRef,
3017 const char *Name) {
3018 BasicBlock *BB = unwrap(P: BBRef);
3019 return wrap(P: BasicBlock::Create(Context&: *unwrap(P: C), Name, Parent: BB->getParent(), InsertBefore: BB));
3020}
3021
3022LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
3023 const char *Name) {
3024 return LLVMInsertBasicBlockInContext(C: getGlobalContextForCAPI(), BBRef, Name);
3025}
3026
3027void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
3028 unwrap(P: BBRef)->eraseFromParent();
3029}
3030
3031void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
3032 unwrap(P: BBRef)->removeFromParent();
3033}
3034
3035void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
3036 unwrap(P: BB)->moveBefore(MovePos: unwrap(P: MovePos));
3037}
3038
3039void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
3040 unwrap(P: BB)->moveAfter(MovePos: unwrap(P: MovePos));
3041}
3042
3043/*--.. Operations on instructions ..........................................--*/
3044
3045LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
3046 return wrap(P: unwrap<Instruction>(P: Inst)->getParent());
3047}
3048
3049LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
3050 BasicBlock *Block = unwrap(P: BB);
3051 BasicBlock::iterator I = Block->begin();
3052 if (I == Block->end())
3053 return nullptr;
3054 return wrap(P: &*I);
3055}
3056
3057LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
3058 BasicBlock *Block = unwrap(P: BB);
3059 BasicBlock::iterator I = Block->end();
3060 if (I == Block->begin())
3061 return nullptr;
3062 return wrap(P: &*--I);
3063}
3064
3065LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
3066 Instruction *Instr = unwrap<Instruction>(P: Inst);
3067 BasicBlock::iterator I(Instr);
3068 if (++I == Instr->getParent()->end())
3069 return nullptr;
3070 return wrap(P: &*I);
3071}
3072
3073LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
3074 Instruction *Instr = unwrap<Instruction>(P: Inst);
3075 BasicBlock::iterator I(Instr);
3076 if (I == Instr->getParent()->begin())
3077 return nullptr;
3078 return wrap(P: &*--I);
3079}
3080
3081void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
3082 unwrap<Instruction>(P: Inst)->removeFromParent();
3083}
3084
3085void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
3086 unwrap<Instruction>(P: Inst)->eraseFromParent();
3087}
3088
3089void LLVMDeleteInstruction(LLVMValueRef Inst) {
3090 unwrap<Instruction>(P: Inst)->deleteValue();
3091}
3092
3093LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
3094 if (ICmpInst *I = dyn_cast<ICmpInst>(Val: unwrap(P: Inst)))
3095 return (LLVMIntPredicate)I->getPredicate();
3096 return (LLVMIntPredicate)0;
3097}
3098
3099LLVMBool LLVMGetICmpSameSign(LLVMValueRef Inst) {
3100 return unwrap<ICmpInst>(P: Inst)->hasSameSign();
3101}
3102
3103void LLVMSetICmpSameSign(LLVMValueRef Inst, LLVMBool SameSign) {
3104 unwrap<ICmpInst>(P: Inst)->setSameSign(SameSign);
3105}
3106
3107LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
3108 if (FCmpInst *I = dyn_cast<FCmpInst>(Val: unwrap(P: Inst)))
3109 return (LLVMRealPredicate)I->getPredicate();
3110 return (LLVMRealPredicate)0;
3111}
3112
3113LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
3114 if (Instruction *C = dyn_cast<Instruction>(Val: unwrap(P: Inst)))
3115 return map_to_llvmopcode(opcode: C->getOpcode());
3116 return (LLVMOpcode)0;
3117}
3118
3119LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
3120 if (Instruction *C = dyn_cast<Instruction>(Val: unwrap(P: Inst)))
3121 return wrap(P: C->clone());
3122 return nullptr;
3123}
3124
3125LLVMValueRef LLVMIsATerminatorInst(LLVMValueRef Inst) {
3126 Instruction *I = dyn_cast<Instruction>(Val: unwrap(P: Inst));
3127 return (I && I->isTerminator()) ? wrap(P: I) : nullptr;
3128}
3129
3130LLVMDbgRecordRef LLVMGetFirstDbgRecord(LLVMValueRef Inst) {
3131 Instruction *Instr = unwrap<Instruction>(P: Inst);
3132 if (!Instr->getDbgMarker())
3133 return nullptr;
3134 auto I = Instr->getDbgMarker()->StoredDbgRecords.begin();
3135 if (I == Instr->getDbgMarker()->StoredDbgRecords.end())
3136 return nullptr;
3137 return wrap(P: &*I);
3138}
3139
3140LLVMDbgRecordRef LLVMGetLastDbgRecord(LLVMValueRef Inst) {
3141 Instruction *Instr = unwrap<Instruction>(P: Inst);
3142 if (!Instr->getDbgMarker())
3143 return nullptr;
3144 auto I = Instr->getDbgMarker()->StoredDbgRecords.rbegin();
3145 if (I == Instr->getDbgMarker()->StoredDbgRecords.rend())
3146 return nullptr;
3147 return wrap(P: &*I);
3148}
3149
3150LLVMDbgRecordRef LLVMGetNextDbgRecord(LLVMDbgRecordRef Rec) {
3151 DbgRecord *Record = unwrap<DbgRecord>(P: Rec);
3152 simple_ilist<DbgRecord>::iterator I(Record);
3153 if (++I == Record->getInstruction()->getDbgMarker()->StoredDbgRecords.end())
3154 return nullptr;
3155 return wrap(P: &*I);
3156}
3157
3158LLVMDbgRecordRef LLVMGetPreviousDbgRecord(LLVMDbgRecordRef Rec) {
3159 DbgRecord *Record = unwrap<DbgRecord>(P: Rec);
3160 simple_ilist<DbgRecord>::iterator I(Record);
3161 if (I == Record->getInstruction()->getDbgMarker()->StoredDbgRecords.begin())
3162 return nullptr;
3163 return wrap(P: &*--I);
3164}
3165
3166LLVMMetadataRef LLVMDbgRecordGetDebugLoc(LLVMDbgRecordRef Rec) {
3167 return wrap(P: unwrap<DbgRecord>(P: Rec)->getDebugLoc().getAsMDNode());
3168}
3169
3170LLVMDbgRecordKind LLVMDbgRecordGetKind(LLVMDbgRecordRef Rec) {
3171 DbgRecord *Record = unwrap<DbgRecord>(P: Rec);
3172 if (isa<DbgLabelRecord>(Val: Record))
3173 return LLVMDbgRecordLabel;
3174 DbgVariableRecord *VariableRecord = dyn_cast<DbgVariableRecord>(Val: Record);
3175 assert(VariableRecord && "unexpected record");
3176 if (VariableRecord->isDbgDeclare())
3177 return LLVMDbgRecordDeclare;
3178 if (VariableRecord->isDbgValue())
3179 return LLVMDbgRecordValue;
3180 assert(VariableRecord->isDbgAssign() && "unexpected record");
3181 return LLVMDbgRecordAssign;
3182}
3183
3184LLVMValueRef LLVMDbgVariableRecordGetValue(LLVMDbgRecordRef Rec,
3185 unsigned OpIdx) {
3186 return wrap(P: unwrap<DbgVariableRecord>(P: Rec)->getValue(OpIdx));
3187}
3188
3189LLVMMetadataRef LLVMDbgVariableRecordGetVariable(LLVMDbgRecordRef Rec) {
3190 return wrap(P: unwrap<DbgVariableRecord>(P: Rec)->getRawVariable());
3191}
3192
3193LLVMMetadataRef LLVMDbgVariableRecordGetExpression(LLVMDbgRecordRef Rec) {
3194 return wrap(P: unwrap<DbgVariableRecord>(P: Rec)->getRawExpression());
3195}
3196
3197unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
3198 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(Val: unwrap(P: Instr))) {
3199 return FPI->arg_size();
3200 }
3201 return unwrap<CallBase>(P: Instr)->arg_size();
3202}
3203
3204/*--.. Call and invoke instructions ........................................--*/
3205
3206unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
3207 return unwrap<CallBase>(P: Instr)->getCallingConv();
3208}
3209
3210void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
3211 return unwrap<CallBase>(P: Instr)->setCallingConv(
3212 static_cast<CallingConv::ID>(CC));
3213}
3214
3215void LLVMSetInstrParamAlignment(LLVMValueRef Instr, LLVMAttributeIndex Idx,
3216 unsigned align) {
3217 auto *Call = unwrap<CallBase>(P: Instr);
3218 Attribute AlignAttr =
3219 Attribute::getWithAlignment(Context&: Call->getContext(), Alignment: Align(align));
3220 Call->addAttributeAtIndex(i: Idx, Attr: AlignAttr);
3221}
3222
3223void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
3224 LLVMAttributeRef A) {
3225 unwrap<CallBase>(P: C)->addAttributeAtIndex(i: Idx, Attr: unwrap(Attr: A));
3226}
3227
3228unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
3229 LLVMAttributeIndex Idx) {
3230 auto *Call = unwrap<CallBase>(P: C);
3231 auto AS = Call->getAttributes().getAttributes(Index: Idx);
3232 return AS.getNumAttributes();
3233}
3234
3235void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
3236 LLVMAttributeRef *Attrs) {
3237 auto *Call = unwrap<CallBase>(P: C);
3238 auto AS = Call->getAttributes().getAttributes(Index: Idx);
3239 for (auto A : AS)
3240 *Attrs++ = wrap(Attr: A);
3241}
3242
3243LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
3244 LLVMAttributeIndex Idx,
3245 unsigned KindID) {
3246 return wrap(Attr: unwrap<CallBase>(P: C)->getAttributeAtIndex(
3247 i: Idx, Kind: (Attribute::AttrKind)KindID));
3248}
3249
3250LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
3251 LLVMAttributeIndex Idx,
3252 const char *K, unsigned KLen) {
3253 return wrap(
3254 Attr: unwrap<CallBase>(P: C)->getAttributeAtIndex(i: Idx, Kind: StringRef(K, KLen)));
3255}
3256
3257void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
3258 unsigned KindID) {
3259 unwrap<CallBase>(P: C)->removeAttributeAtIndex(i: Idx, Kind: (Attribute::AttrKind)KindID);
3260}
3261
3262void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
3263 const char *K, unsigned KLen) {
3264 unwrap<CallBase>(P: C)->removeAttributeAtIndex(i: Idx, Kind: StringRef(K, KLen));
3265}
3266
3267LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
3268 return wrap(P: unwrap<CallBase>(P: Instr)->getCalledOperand());
3269}
3270
3271LLVMTypeRef LLVMGetCalledFunctionType(LLVMValueRef Instr) {
3272 return wrap(P: unwrap<CallBase>(P: Instr)->getFunctionType());
3273}
3274
3275unsigned LLVMGetNumOperandBundles(LLVMValueRef C) {
3276 return unwrap<CallBase>(P: C)->getNumOperandBundles();
3277}
3278
3279LLVMOperandBundleRef LLVMGetOperandBundleAtIndex(LLVMValueRef C,
3280 unsigned Index) {
3281 return wrap(
3282 P: new OperandBundleDef(unwrap<CallBase>(P: C)->getOperandBundleAt(Index)));
3283}
3284
3285/*--.. Operations on call instructions (only) ..............................--*/
3286
3287LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
3288 return unwrap<CallInst>(P: Call)->isTailCall();
3289}
3290
3291void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
3292 unwrap<CallInst>(P: Call)->setTailCall(isTailCall);
3293}
3294
3295LLVMTailCallKind LLVMGetTailCallKind(LLVMValueRef Call) {
3296 return (LLVMTailCallKind)unwrap<CallInst>(P: Call)->getTailCallKind();
3297}
3298
3299void LLVMSetTailCallKind(LLVMValueRef Call, LLVMTailCallKind kind) {
3300 unwrap<CallInst>(P: Call)->setTailCallKind((CallInst::TailCallKind)kind);
3301}
3302
3303/*--.. Operations on invoke instructions (only) ............................--*/
3304
3305LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
3306 return wrap(P: unwrap<InvokeInst>(P: Invoke)->getNormalDest());
3307}
3308
3309LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
3310 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(Val: unwrap(P: Invoke))) {
3311 return wrap(P: CRI->getUnwindDest());
3312 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(Val: unwrap(P: Invoke))) {
3313 return wrap(P: CSI->getUnwindDest());
3314 }
3315 return wrap(P: unwrap<InvokeInst>(P: Invoke)->getUnwindDest());
3316}
3317
3318void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
3319 unwrap<InvokeInst>(P: Invoke)->setNormalDest(unwrap(P: B));
3320}
3321
3322void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
3323 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(Val: unwrap(P: Invoke))) {
3324 return CRI->setUnwindDest(unwrap(P: B));
3325 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(Val: unwrap(P: Invoke))) {
3326 return CSI->setUnwindDest(unwrap(P: B));
3327 }
3328 unwrap<InvokeInst>(P: Invoke)->setUnwindDest(unwrap(P: B));
3329}
3330
3331LLVMBasicBlockRef LLVMGetCallBrDefaultDest(LLVMValueRef CallBr) {
3332 return wrap(P: unwrap<CallBrInst>(P: CallBr)->getDefaultDest());
3333}
3334
3335unsigned LLVMGetCallBrNumIndirectDests(LLVMValueRef CallBr) {
3336 return unwrap<CallBrInst>(P: CallBr)->getNumIndirectDests();
3337}
3338
3339LLVMBasicBlockRef LLVMGetCallBrIndirectDest(LLVMValueRef CallBr, unsigned Idx) {
3340 return wrap(P: unwrap<CallBrInst>(P: CallBr)->getIndirectDest(i: Idx));
3341}
3342
3343/*--.. Operations on terminators ...........................................--*/
3344
3345unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
3346 return unwrap<Instruction>(P: Term)->getNumSuccessors();
3347}
3348
3349LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
3350 return wrap(P: unwrap<Instruction>(P: Term)->getSuccessor(Idx: i));
3351}
3352
3353void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
3354 return unwrap<Instruction>(P: Term)->setSuccessor(Idx: i, BB: unwrap(P: block));
3355}
3356
3357/*--.. Operations on branch instructions (only) ............................--*/
3358
3359LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
3360 return isa<CondBrInst>(Val: unwrap<Instruction>(P: Branch));
3361}
3362
3363LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
3364 return wrap(P: unwrap<CondBrInst>(P: Branch)->getCondition());
3365}
3366
3367void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
3368 return unwrap<CondBrInst>(P: Branch)->setCondition(unwrap(P: Cond));
3369}
3370
3371/*--.. Operations on switch instructions (only) ............................--*/
3372
3373LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
3374 return wrap(P: unwrap<SwitchInst>(P: Switch)->getDefaultDest());
3375}
3376
3377LLVMValueRef LLVMGetSwitchCaseValue(LLVMValueRef Switch, unsigned i) {
3378 assert(i > 0 && i <= unwrap<SwitchInst>(Switch)->getNumCases());
3379 auto It = unwrap<SwitchInst>(P: Switch)->case_begin() + (i - 1);
3380 return wrap(P: It->getCaseValue());
3381}
3382
3383void LLVMSetSwitchCaseValue(LLVMValueRef Switch, unsigned i,
3384 LLVMValueRef CaseValue) {
3385 assert(i > 0 && i <= unwrap<SwitchInst>(Switch)->getNumCases());
3386 auto It = unwrap<SwitchInst>(P: Switch)->case_begin() + (i - 1);
3387 It->setValue(unwrap<ConstantInt>(P: CaseValue));
3388}
3389
3390/*--.. Operations on alloca instructions (only) ............................--*/
3391
3392LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
3393 return wrap(P: unwrap<AllocaInst>(P: Alloca)->getAllocatedType());
3394}
3395
3396/*--.. Operations on gep instructions (only) ...............................--*/
3397
3398LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
3399 return unwrap<GEPOperator>(P: GEP)->isInBounds();
3400}
3401
3402void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
3403 return unwrap<GetElementPtrInst>(P: GEP)->setIsInBounds(InBounds);
3404}
3405
3406LLVMTypeRef LLVMGetGEPSourceElementType(LLVMValueRef GEP) {
3407 return wrap(P: unwrap<GEPOperator>(P: GEP)->getSourceElementType());
3408}
3409
3410LLVMGEPNoWrapFlags LLVMGEPGetNoWrapFlags(LLVMValueRef GEP) {
3411 GEPOperator *GEPOp = unwrap<GEPOperator>(P: GEP);
3412 return mapToLLVMGEPNoWrapFlags(GEPFlags: GEPOp->getNoWrapFlags());
3413}
3414
3415void LLVMGEPSetNoWrapFlags(LLVMValueRef GEP, LLVMGEPNoWrapFlags NoWrapFlags) {
3416 GetElementPtrInst *GEPInst = unwrap<GetElementPtrInst>(P: GEP);
3417 GEPInst->setNoWrapFlags(mapFromLLVMGEPNoWrapFlags(GEPFlags: NoWrapFlags));
3418}
3419
3420/*--.. Operations on phi nodes .............................................--*/
3421
3422void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
3423 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
3424 PHINode *PhiVal = unwrap<PHINode>(P: PhiNode);
3425 for (unsigned I = 0; I != Count; ++I)
3426 PhiVal->addIncoming(V: unwrap(P: IncomingValues[I]), BB: unwrap(P: IncomingBlocks[I]));
3427}
3428
3429unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
3430 return unwrap<PHINode>(P: PhiNode)->getNumIncomingValues();
3431}
3432
3433LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
3434 return wrap(P: unwrap<PHINode>(P: PhiNode)->getIncomingValue(i: Index));
3435}
3436
3437LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
3438 return wrap(P: unwrap<PHINode>(P: PhiNode)->getIncomingBlock(i: Index));
3439}
3440
3441/*--.. Operations on extractvalue and insertvalue nodes ....................--*/
3442
3443unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
3444 auto *I = unwrap(P: Inst);
3445 if (auto *GEP = dyn_cast<GEPOperator>(Val: I))
3446 return GEP->getNumIndices();
3447 if (auto *EV = dyn_cast<ExtractValueInst>(Val: I))
3448 return EV->getNumIndices();
3449 if (auto *IV = dyn_cast<InsertValueInst>(Val: I))
3450 return IV->getNumIndices();
3451 llvm_unreachable(
3452 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3453}
3454
3455const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
3456 auto *I = unwrap(P: Inst);
3457 if (auto *EV = dyn_cast<ExtractValueInst>(Val: I))
3458 return EV->getIndices().data();
3459 if (auto *IV = dyn_cast<InsertValueInst>(Val: I))
3460 return IV->getIndices().data();
3461 llvm_unreachable(
3462 "LLVMGetIndices applies only to extractvalue and insertvalue!");
3463}
3464
3465
3466/*===-- Instruction builders ----------------------------------------------===*/
3467
3468LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
3469 return wrap(P: new IRBuilder<>(*unwrap(P: C)));
3470}
3471
3472LLVMBuilderRef LLVMCreateBuilder(void) {
3473 return LLVMCreateBuilderInContext(C: getGlobalContextForCAPI());
3474}
3475
3476static void LLVMPositionBuilderImpl(IRBuilder<> *Builder, BasicBlock *Block,
3477 Instruction *Instr, bool BeforeDbgRecords) {
3478 BasicBlock::iterator I = Instr ? Instr->getIterator() : Block->end();
3479 I.setHeadBit(BeforeDbgRecords);
3480 Builder->SetInsertPoint(I);
3481}
3482
3483void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
3484 LLVMValueRef Instr) {
3485 return LLVMPositionBuilderImpl(Builder: unwrap(P: Builder), Block: unwrap(P: Block),
3486 Instr: unwrap<Instruction>(P: Instr), BeforeDbgRecords: false);
3487}
3488
3489void LLVMPositionBuilderBeforeDbgRecords(LLVMBuilderRef Builder,
3490 LLVMBasicBlockRef Block,
3491 LLVMValueRef Instr) {
3492 return LLVMPositionBuilderImpl(Builder: unwrap(P: Builder), Block: unwrap(P: Block),
3493 Instr: unwrap<Instruction>(P: Instr), BeforeDbgRecords: true);
3494}
3495
3496void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
3497 Instruction *I = unwrap<Instruction>(P: Instr);
3498 return LLVMPositionBuilderImpl(Builder: unwrap(P: Builder), Block: I->getParent(), Instr: I, BeforeDbgRecords: false);
3499}
3500
3501void LLVMPositionBuilderBeforeInstrAndDbgRecords(LLVMBuilderRef Builder,
3502 LLVMValueRef Instr) {
3503 Instruction *I = unwrap<Instruction>(P: Instr);
3504 return LLVMPositionBuilderImpl(Builder: unwrap(P: Builder), Block: I->getParent(), Instr: I, BeforeDbgRecords: true);
3505}
3506
3507void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
3508 BasicBlock *BB = unwrap(P: Block);
3509 unwrap(P: Builder)->SetInsertPoint(BB);
3510}
3511
3512LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
3513 return wrap(P: unwrap(P: Builder)->GetInsertBlock());
3514}
3515
3516void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
3517 unwrap(P: Builder)->ClearInsertionPoint();
3518}
3519
3520void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
3521 unwrap(P: Builder)->Insert(I: unwrap<Instruction>(P: Instr));
3522}
3523
3524void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
3525 const char *Name) {
3526 unwrap(P: Builder)->Insert(I: unwrap<Instruction>(P: Instr), Name);
3527}
3528
3529void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
3530 delete unwrap(P: Builder);
3531}
3532
3533/*--.. Metadata builders ...................................................--*/
3534
3535LLVMMetadataRef LLVMGetCurrentDebugLocation2(LLVMBuilderRef Builder) {
3536 return wrap(P: unwrap(P: Builder)->getCurrentDebugLocation().getAsMDNode());
3537}
3538
3539void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Builder, LLVMMetadataRef Loc) {
3540 if (Loc)
3541 unwrap(P: Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<DILocation>(P: Loc)));
3542 else
3543 unwrap(P: Builder)->SetCurrentDebugLocation(DebugLoc());
3544}
3545
3546void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
3547 DILocation *Loc =
3548 L ? cast<DILocation>(Val: unwrap<MetadataAsValue>(P: L)->getMetadata()) : nullptr;
3549 unwrap(P: Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3550}
3551
3552LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
3553 LLVMContext &Context = unwrap(P: Builder)->getContext();
3554 return wrap(P: MetadataAsValue::get(
3555 Context, MD: unwrap(P: Builder)->getCurrentDebugLocation().getAsMDNode()));
3556}
3557
3558void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
3559 unwrap(P: Builder)->SetInstDebugLocation(unwrap<Instruction>(P: Inst));
3560}
3561
3562void LLVMAddMetadataToInst(LLVMBuilderRef Builder, LLVMValueRef Inst) {
3563 unwrap(P: Builder)->SetInstDebugLocation(unwrap<Instruction>(P: Inst));
3564}
3565
3566void LLVMBuilderSetDefaultFPMathTag(LLVMBuilderRef Builder,
3567 LLVMMetadataRef FPMathTag) {
3568
3569 unwrap(P: Builder)->setDefaultFPMathTag(FPMathTag
3570 ? unwrap<MDNode>(P: FPMathTag)
3571 : nullptr);
3572}
3573
3574LLVMContextRef LLVMGetBuilderContext(LLVMBuilderRef Builder) {
3575 return wrap(P: &unwrap(P: Builder)->getContext());
3576}
3577
3578LLVMMetadataRef LLVMBuilderGetDefaultFPMathTag(LLVMBuilderRef Builder) {
3579 return wrap(P: unwrap(P: Builder)->getDefaultFPMathTag());
3580}
3581
3582/*--.. Instruction builders ................................................--*/
3583
3584LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
3585 return wrap(P: unwrap(P: B)->CreateRetVoid());
3586}
3587
3588LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
3589 return wrap(P: unwrap(P: B)->CreateRet(V: unwrap(P: V)));
3590}
3591
3592LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
3593 unsigned N) {
3594 return wrap(P: unwrap(P: B)->CreateAggregateRet(RetVals: {unwrap(Vals: RetVals), N}));
3595}
3596
3597LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
3598 return wrap(P: unwrap(P: B)->CreateBr(Dest: unwrap(P: Dest)));
3599}
3600
3601LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
3602 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
3603 return wrap(P: unwrap(P: B)->CreateCondBr(Cond: unwrap(P: If), True: unwrap(P: Then), False: unwrap(P: Else)));
3604}
3605
3606LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
3607 LLVMBasicBlockRef Else, unsigned NumCases) {
3608 return wrap(P: unwrap(P: B)->CreateSwitch(V: unwrap(P: V), Dest: unwrap(P: Else), NumCases));
3609}
3610
3611LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
3612 unsigned NumDests) {
3613 return wrap(P: unwrap(P: B)->CreateIndirectBr(Addr: unwrap(P: Addr), NumDests));
3614}
3615
3616LLVMValueRef LLVMBuildCallBr(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
3617 LLVMBasicBlockRef DefaultDest,
3618 LLVMBasicBlockRef *IndirectDests,
3619 unsigned NumIndirectDests, LLVMValueRef *Args,
3620 unsigned NumArgs, LLVMOperandBundleRef *Bundles,
3621 unsigned NumBundles, const char *Name) {
3622
3623 SmallVector<OperandBundleDef, 8> OBs;
3624 for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
3625 OperandBundleDef *OB = unwrap(P: Bundle);
3626 OBs.push_back(Elt: *OB);
3627 }
3628
3629 return wrap(P: unwrap(P: B)->CreateCallBr(
3630 Ty: unwrap<FunctionType>(P: Ty), Callee: unwrap(P: Fn), DefaultDest: unwrap(P: DefaultDest),
3631 IndirectDests: ArrayRef(unwrap(BBs: IndirectDests), NumIndirectDests),
3632 Args: ArrayRef<Value *>(unwrap(Vals: Args), NumArgs), OpBundles: OBs, Name));
3633}
3634
3635LLVMValueRef LLVMBuildInvoke2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
3636 LLVMValueRef *Args, unsigned NumArgs,
3637 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
3638 const char *Name) {
3639 return wrap(P: unwrap(P: B)->CreateInvoke(Ty: unwrap<FunctionType>(P: Ty), Callee: unwrap(P: Fn),
3640 NormalDest: unwrap(P: Then), UnwindDest: unwrap(P: Catch),
3641 Args: ArrayRef(unwrap(Vals: Args), NumArgs), Name));
3642}
3643
3644LLVMValueRef LLVMBuildInvokeWithOperandBundles(
3645 LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, LLVMValueRef *Args,
3646 unsigned NumArgs, LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
3647 LLVMOperandBundleRef *Bundles, unsigned NumBundles, const char *Name) {
3648 SmallVector<OperandBundleDef, 8> OBs;
3649 for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
3650 OperandBundleDef *OB = unwrap(P: Bundle);
3651 OBs.push_back(Elt: *OB);
3652 }
3653 return wrap(P: unwrap(P: B)->CreateInvoke(
3654 Ty: unwrap<FunctionType>(P: Ty), Callee: unwrap(P: Fn), NormalDest: unwrap(P: Then), UnwindDest: unwrap(P: Catch),
3655 Args: ArrayRef(unwrap(Vals: Args), NumArgs), OpBundles: OBs, Name));
3656}
3657
3658LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
3659 LLVMValueRef PersFn, unsigned NumClauses,
3660 const char *Name) {
3661 // The personality used to live on the landingpad instruction, but now it
3662 // lives on the parent function. For compatibility, take the provided
3663 // personality and put it on the parent function.
3664 if (PersFn)
3665 unwrap(P: B)->GetInsertBlock()->getParent()->setPersonalityFn(
3666 unwrap<Function>(P: PersFn));
3667 return wrap(P: unwrap(P: B)->CreateLandingPad(Ty: unwrap(P: Ty), NumClauses, Name));
3668}
3669
3670LLVMValueRef LLVMBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
3671 LLVMValueRef *Args, unsigned NumArgs,
3672 const char *Name) {
3673 return wrap(P: unwrap(P: B)->CreateCatchPad(ParentPad: unwrap(P: ParentPad),
3674 Args: ArrayRef(unwrap(Vals: Args), NumArgs), Name));
3675}
3676
3677LLVMValueRef LLVMBuildCleanupPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
3678 LLVMValueRef *Args, unsigned NumArgs,
3679 const char *Name) {
3680 if (ParentPad == nullptr) {
3681 Type *Ty = Type::getTokenTy(C&: unwrap(P: B)->getContext());
3682 ParentPad = wrap(P: Constant::getNullValue(Ty));
3683 }
3684 return wrap(P: unwrap(P: B)->CreateCleanupPad(
3685 ParentPad: unwrap(P: ParentPad), Args: ArrayRef(unwrap(Vals: Args), NumArgs), Name));
3686}
3687
3688LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
3689 return wrap(P: unwrap(P: B)->CreateResume(Exn: unwrap(P: Exn)));
3690}
3691
3692LLVMValueRef LLVMBuildCatchSwitch(LLVMBuilderRef B, LLVMValueRef ParentPad,
3693 LLVMBasicBlockRef UnwindBB,
3694 unsigned NumHandlers, const char *Name) {
3695 if (ParentPad == nullptr) {
3696 Type *Ty = Type::getTokenTy(C&: unwrap(P: B)->getContext());
3697 ParentPad = wrap(P: Constant::getNullValue(Ty));
3698 }
3699 return wrap(P: unwrap(P: B)->CreateCatchSwitch(ParentPad: unwrap(P: ParentPad), UnwindBB: unwrap(P: UnwindBB),
3700 NumHandlers, Name));
3701}
3702
3703LLVMValueRef LLVMBuildCatchRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
3704 LLVMBasicBlockRef BB) {
3705 return wrap(P: unwrap(P: B)->CreateCatchRet(CatchPad: unwrap<CatchPadInst>(P: CatchPad),
3706 BB: unwrap(P: BB)));
3707}
3708
3709LLVMValueRef LLVMBuildCleanupRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
3710 LLVMBasicBlockRef BB) {
3711 return wrap(P: unwrap(P: B)->CreateCleanupRet(CleanupPad: unwrap<CleanupPadInst>(P: CatchPad),
3712 UnwindBB: unwrap(P: BB)));
3713}
3714
3715LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
3716 return wrap(P: unwrap(P: B)->CreateUnreachable());
3717}
3718
3719void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
3720 LLVMBasicBlockRef Dest) {
3721 unwrap<SwitchInst>(P: Switch)->addCase(OnVal: unwrap<ConstantInt>(P: OnVal), Dest: unwrap(P: Dest));
3722}
3723
3724void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
3725 unwrap<IndirectBrInst>(P: IndirectBr)->addDestination(Dest: unwrap(P: Dest));
3726}
3727
3728unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3729 return unwrap<LandingPadInst>(P: LandingPad)->getNumClauses();
3730}
3731
3732LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
3733 return wrap(P: unwrap<LandingPadInst>(P: LandingPad)->getClause(Idx));
3734}
3735
3736void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
3737 unwrap<LandingPadInst>(P: LandingPad)->addClause(ClauseVal: unwrap<Constant>(P: ClauseVal));
3738}
3739
3740LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
3741 return unwrap<LandingPadInst>(P: LandingPad)->isCleanup();
3742}
3743
3744void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3745 unwrap<LandingPadInst>(P: LandingPad)->setCleanup(Val);
3746}
3747
3748void LLVMAddHandler(LLVMValueRef CatchSwitch, LLVMBasicBlockRef Dest) {
3749 unwrap<CatchSwitchInst>(P: CatchSwitch)->addHandler(Dest: unwrap(P: Dest));
3750}
3751
3752unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3753 return unwrap<CatchSwitchInst>(P: CatchSwitch)->getNumHandlers();
3754}
3755
3756void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3757 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(P: CatchSwitch);
3758 for (const BasicBlock *H : CSI->handlers())
3759 *Handlers++ = wrap(P: H);
3760}
3761
3762LLVMValueRef LLVMGetParentCatchSwitch(LLVMValueRef CatchPad) {
3763 return wrap(P: unwrap<CatchPadInst>(P: CatchPad)->getCatchSwitch());
3764}
3765
3766void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad, LLVMValueRef CatchSwitch) {
3767 unwrap<CatchPadInst>(P: CatchPad)
3768 ->setCatchSwitch(unwrap<CatchSwitchInst>(P: CatchSwitch));
3769}
3770
3771/*--.. Funclets ...........................................................--*/
3772
3773LLVMValueRef LLVMGetArgOperand(LLVMValueRef Funclet, unsigned i) {
3774 return wrap(P: unwrap<FuncletPadInst>(P: Funclet)->getArgOperand(i));
3775}
3776
3777void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
3778 unwrap<FuncletPadInst>(P: Funclet)->setArgOperand(i, v: unwrap(P: value));
3779}
3780
3781/*--.. Arithmetic ..........................................................--*/
3782
3783static FastMathFlags mapFromLLVMFastMathFlags(LLVMFastMathFlags FMF) {
3784 FastMathFlags NewFMF;
3785 NewFMF.setAllowReassoc((FMF & LLVMFastMathAllowReassoc) != 0);
3786 NewFMF.setNoNaNs((FMF & LLVMFastMathNoNaNs) != 0);
3787 NewFMF.setNoInfs((FMF & LLVMFastMathNoInfs) != 0);
3788 NewFMF.setNoSignedZeros((FMF & LLVMFastMathNoSignedZeros) != 0);
3789 NewFMF.setAllowReciprocal((FMF & LLVMFastMathAllowReciprocal) != 0);
3790 NewFMF.setAllowContract((FMF & LLVMFastMathAllowContract) != 0);
3791 NewFMF.setApproxFunc((FMF & LLVMFastMathApproxFunc) != 0);
3792
3793 return NewFMF;
3794}
3795
3796static LLVMFastMathFlags mapToLLVMFastMathFlags(FastMathFlags FMF) {
3797 LLVMFastMathFlags NewFMF = LLVMFastMathNone;
3798 if (FMF.allowReassoc())
3799 NewFMF |= LLVMFastMathAllowReassoc;
3800 if (FMF.noNaNs())
3801 NewFMF |= LLVMFastMathNoNaNs;
3802 if (FMF.noInfs())
3803 NewFMF |= LLVMFastMathNoInfs;
3804 if (FMF.noSignedZeros())
3805 NewFMF |= LLVMFastMathNoSignedZeros;
3806 if (FMF.allowReciprocal())
3807 NewFMF |= LLVMFastMathAllowReciprocal;
3808 if (FMF.allowContract())
3809 NewFMF |= LLVMFastMathAllowContract;
3810 if (FMF.approxFunc())
3811 NewFMF |= LLVMFastMathApproxFunc;
3812
3813 return NewFMF;
3814}
3815
3816LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3817 const char *Name) {
3818 return wrap(P: unwrap(P: B)->CreateAdd(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3819}
3820
3821LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3822 const char *Name) {
3823 return wrap(P: unwrap(P: B)->CreateNSWAdd(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3824}
3825
3826LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3827 const char *Name) {
3828 return wrap(P: unwrap(P: B)->CreateNUWAdd(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3829}
3830
3831LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3832 const char *Name) {
3833 return wrap(P: unwrap(P: B)->CreateFAdd(L: unwrap(P: LHS), R: unwrap(P: RHS), Name));
3834}
3835
3836LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3837 const char *Name) {
3838 return wrap(P: unwrap(P: B)->CreateSub(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3839}
3840
3841LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3842 const char *Name) {
3843 return wrap(P: unwrap(P: B)->CreateNSWSub(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3844}
3845
3846LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3847 const char *Name) {
3848 return wrap(P: unwrap(P: B)->CreateNUWSub(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3849}
3850
3851LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3852 const char *Name) {
3853 return wrap(P: unwrap(P: B)->CreateFSub(L: unwrap(P: LHS), R: unwrap(P: RHS), Name));
3854}
3855
3856LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3857 const char *Name) {
3858 return wrap(P: unwrap(P: B)->CreateMul(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3859}
3860
3861LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3862 const char *Name) {
3863 return wrap(P: unwrap(P: B)->CreateNSWMul(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3864}
3865
3866LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3867 const char *Name) {
3868 return wrap(P: unwrap(P: B)->CreateNUWMul(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3869}
3870
3871LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3872 const char *Name) {
3873 return wrap(P: unwrap(P: B)->CreateFMul(L: unwrap(P: LHS), R: unwrap(P: RHS), Name));
3874}
3875
3876LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3877 const char *Name) {
3878 return wrap(P: unwrap(P: B)->CreateUDiv(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3879}
3880
3881LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
3882 LLVMValueRef RHS, const char *Name) {
3883 return wrap(P: unwrap(P: B)->CreateExactUDiv(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3884}
3885
3886LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3887 const char *Name) {
3888 return wrap(P: unwrap(P: B)->CreateSDiv(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3889}
3890
3891LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
3892 LLVMValueRef RHS, const char *Name) {
3893 return wrap(P: unwrap(P: B)->CreateExactSDiv(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3894}
3895
3896LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3897 const char *Name) {
3898 return wrap(P: unwrap(P: B)->CreateFDiv(L: unwrap(P: LHS), R: unwrap(P: RHS), Name));
3899}
3900
3901LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3902 const char *Name) {
3903 return wrap(P: unwrap(P: B)->CreateURem(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3904}
3905
3906LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3907 const char *Name) {
3908 return wrap(P: unwrap(P: B)->CreateSRem(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3909}
3910
3911LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3912 const char *Name) {
3913 return wrap(P: unwrap(P: B)->CreateFRem(L: unwrap(P: LHS), R: unwrap(P: RHS), Name));
3914}
3915
3916LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3917 const char *Name) {
3918 return wrap(P: unwrap(P: B)->CreateShl(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3919}
3920
3921LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3922 const char *Name) {
3923 return wrap(P: unwrap(P: B)->CreateLShr(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3924}
3925
3926LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3927 const char *Name) {
3928 return wrap(P: unwrap(P: B)->CreateAShr(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3929}
3930
3931LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3932 const char *Name) {
3933 return wrap(P: unwrap(P: B)->CreateAnd(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3934}
3935
3936LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3937 const char *Name) {
3938 return wrap(P: unwrap(P: B)->CreateOr(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3939}
3940
3941LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3942 const char *Name) {
3943 return wrap(P: unwrap(P: B)->CreateXor(LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
3944}
3945
3946LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
3947 LLVMValueRef LHS, LLVMValueRef RHS,
3948 const char *Name) {
3949 return wrap(P: unwrap(P: B)->CreateBinOp(Opc: Instruction::BinaryOps(map_from_llvmopcode(code: Op)), LHS: unwrap(P: LHS),
3950 RHS: unwrap(P: RHS), Name));
3951}
3952
3953LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3954 return wrap(P: unwrap(P: B)->CreateNeg(V: unwrap(P: V), Name));
3955}
3956
3957LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
3958 const char *Name) {
3959 return wrap(P: unwrap(P: B)->CreateNSWNeg(V: unwrap(P: V), Name));
3960}
3961
3962LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
3963 const char *Name) {
3964 Value *Neg = unwrap(P: B)->CreateNeg(V: unwrap(P: V), Name);
3965 if (auto *I = dyn_cast<BinaryOperator>(Val: Neg))
3966 I->setHasNoUnsignedWrap();
3967 return wrap(P: Neg);
3968}
3969
3970LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3971 return wrap(P: unwrap(P: B)->CreateFNeg(V: unwrap(P: V), Name));
3972}
3973
3974LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3975 return wrap(P: unwrap(P: B)->CreateNot(V: unwrap(P: V), Name));
3976}
3977
3978LLVMBool LLVMGetNUW(LLVMValueRef ArithInst) {
3979 Value *P = unwrap<Value>(P: ArithInst);
3980 return cast<Instruction>(Val: P)->hasNoUnsignedWrap();
3981}
3982
3983void LLVMSetNUW(LLVMValueRef ArithInst, LLVMBool HasNUW) {
3984 Value *P = unwrap<Value>(P: ArithInst);
3985 cast<Instruction>(Val: P)->setHasNoUnsignedWrap(HasNUW);
3986}
3987
3988LLVMBool LLVMGetNSW(LLVMValueRef ArithInst) {
3989 Value *P = unwrap<Value>(P: ArithInst);
3990 return cast<Instruction>(Val: P)->hasNoSignedWrap();
3991}
3992
3993void LLVMSetNSW(LLVMValueRef ArithInst, LLVMBool HasNSW) {
3994 Value *P = unwrap<Value>(P: ArithInst);
3995 cast<Instruction>(Val: P)->setHasNoSignedWrap(HasNSW);
3996}
3997
3998LLVMBool LLVMGetExact(LLVMValueRef DivOrShrInst) {
3999 Value *P = unwrap<Value>(P: DivOrShrInst);
4000 return cast<Instruction>(Val: P)->isExact();
4001}
4002
4003void LLVMSetExact(LLVMValueRef DivOrShrInst, LLVMBool IsExact) {
4004 Value *P = unwrap<Value>(P: DivOrShrInst);
4005 cast<Instruction>(Val: P)->setIsExact(IsExact);
4006}
4007
4008LLVMBool LLVMGetNNeg(LLVMValueRef NonNegInst) {
4009 Value *P = unwrap<Value>(P: NonNegInst);
4010 return cast<Instruction>(Val: P)->hasNonNeg();
4011}
4012
4013void LLVMSetNNeg(LLVMValueRef NonNegInst, LLVMBool IsNonNeg) {
4014 Value *P = unwrap<Value>(P: NonNegInst);
4015 cast<Instruction>(Val: P)->setNonNeg(IsNonNeg);
4016}
4017
4018LLVMFastMathFlags LLVMGetFastMathFlags(LLVMValueRef FPMathInst) {
4019 Value *P = unwrap<Value>(P: FPMathInst);
4020 FastMathFlags FMF = cast<Instruction>(Val: P)->getFastMathFlags();
4021 return mapToLLVMFastMathFlags(FMF);
4022}
4023
4024void LLVMSetFastMathFlags(LLVMValueRef FPMathInst, LLVMFastMathFlags FMF) {
4025 Value *P = unwrap<Value>(P: FPMathInst);
4026 cast<Instruction>(Val: P)->setFastMathFlags(mapFromLLVMFastMathFlags(FMF));
4027}
4028
4029LLVMBool LLVMCanValueUseFastMathFlags(LLVMValueRef V) {
4030 Value *Val = unwrap<Value>(P: V);
4031 return isa<FPMathOperator>(Val);
4032}
4033
4034LLVMBool LLVMGetIsDisjoint(LLVMValueRef Inst) {
4035 Value *P = unwrap<Value>(P: Inst);
4036 return cast<PossiblyDisjointInst>(Val: P)->isDisjoint();
4037}
4038
4039void LLVMSetIsDisjoint(LLVMValueRef Inst, LLVMBool IsDisjoint) {
4040 Value *P = unwrap<Value>(P: Inst);
4041 cast<PossiblyDisjointInst>(Val: P)->setIsDisjoint(IsDisjoint);
4042}
4043
4044/*--.. Memory ..............................................................--*/
4045
4046LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
4047 const char *Name) {
4048 BasicBlock *BB = unwrap(P: B)->GetInsertBlock();
4049 const DataLayout &DL = BB->getDataLayout();
4050 Type *ITy = Type::getInt32Ty(C&: BB->getContext());
4051 Value *AllocSize =
4052 unwrap(P: B)->CreateTypeSize(Ty: ITy, Size: DL.getTypeAllocSize(Ty: unwrap(P: Ty)));
4053 return wrap(P: unwrap(P: B)->CreateMalloc(IntPtrTy: ITy, AllocSize, ArraySize: nullptr, MallocF: nullptr, Name));
4054}
4055
4056LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
4057 LLVMValueRef Val, const char *Name) {
4058 BasicBlock *BB = unwrap(P: B)->GetInsertBlock();
4059 const DataLayout &DL = BB->getDataLayout();
4060 Type *ITy = Type::getInt32Ty(C&: BB->getContext());
4061 Value *AllocSize =
4062 unwrap(P: B)->CreateTypeSize(Ty: ITy, Size: DL.getTypeAllocSize(Ty: unwrap(P: Ty)));
4063 return wrap(
4064 P: unwrap(P: B)->CreateMalloc(IntPtrTy: ITy, AllocSize, ArraySize: unwrap(P: Val), MallocF: nullptr, Name));
4065}
4066
4067LLVMValueRef LLVMBuildMemSet(LLVMBuilderRef B, LLVMValueRef Ptr,
4068 LLVMValueRef Val, LLVMValueRef Len,
4069 unsigned Align) {
4070 return wrap(P: unwrap(P: B)->CreateMemSet(Ptr: unwrap(P: Ptr), Val: unwrap(P: Val), Size: unwrap(P: Len),
4071 Align: MaybeAlign(Align)));
4072}
4073
4074LLVMValueRef LLVMBuildMemCpy(LLVMBuilderRef B,
4075 LLVMValueRef Dst, unsigned DstAlign,
4076 LLVMValueRef Src, unsigned SrcAlign,
4077 LLVMValueRef Size) {
4078 return wrap(P: unwrap(P: B)->CreateMemCpy(Dst: unwrap(P: Dst), DstAlign: MaybeAlign(DstAlign),
4079 Src: unwrap(P: Src), SrcAlign: MaybeAlign(SrcAlign),
4080 Size: unwrap(P: Size)));
4081}
4082
4083LLVMValueRef LLVMBuildMemMove(LLVMBuilderRef B,
4084 LLVMValueRef Dst, unsigned DstAlign,
4085 LLVMValueRef Src, unsigned SrcAlign,
4086 LLVMValueRef Size) {
4087 return wrap(P: unwrap(P: B)->CreateMemMove(Dst: unwrap(P: Dst), DstAlign: MaybeAlign(DstAlign),
4088 Src: unwrap(P: Src), SrcAlign: MaybeAlign(SrcAlign),
4089 Size: unwrap(P: Size)));
4090}
4091
4092LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
4093 const char *Name) {
4094 return wrap(P: unwrap(P: B)->CreateAlloca(Ty: unwrap(P: Ty), ArraySize: nullptr, Name));
4095}
4096
4097LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
4098 LLVMValueRef Val, const char *Name) {
4099 return wrap(P: unwrap(P: B)->CreateAlloca(Ty: unwrap(P: Ty), ArraySize: unwrap(P: Val), Name));
4100}
4101
4102LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
4103 return wrap(P: unwrap(P: B)->CreateFree(Source: unwrap(P: PointerVal)));
4104}
4105
4106LLVMValueRef LLVMBuildLoad2(LLVMBuilderRef B, LLVMTypeRef Ty,
4107 LLVMValueRef PointerVal, const char *Name) {
4108 return wrap(P: unwrap(P: B)->CreateLoad(Ty: unwrap(P: Ty), Ptr: unwrap(P: PointerVal), Name));
4109}
4110
4111LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
4112 LLVMValueRef PointerVal) {
4113 return wrap(P: unwrap(P: B)->CreateStore(Val: unwrap(P: Val), Ptr: unwrap(P: PointerVal)));
4114}
4115
4116static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
4117 switch (Ordering) {
4118 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
4119 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
4120 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
4121 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
4122 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
4123 case LLVMAtomicOrderingAcquireRelease:
4124 return AtomicOrdering::AcquireRelease;
4125 case LLVMAtomicOrderingSequentiallyConsistent:
4126 return AtomicOrdering::SequentiallyConsistent;
4127 }
4128
4129 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
4130}
4131
4132static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
4133 switch (Ordering) {
4134 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
4135 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
4136 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
4137 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
4138 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
4139 case AtomicOrdering::AcquireRelease:
4140 return LLVMAtomicOrderingAcquireRelease;
4141 case AtomicOrdering::SequentiallyConsistent:
4142 return LLVMAtomicOrderingSequentiallyConsistent;
4143 }
4144
4145 llvm_unreachable("Invalid AtomicOrdering value!");
4146}
4147
4148static AtomicRMWInst::BinOp mapFromLLVMRMWBinOp(LLVMAtomicRMWBinOp BinOp) {
4149 switch (BinOp) {
4150 case LLVMAtomicRMWBinOpXchg: return AtomicRMWInst::Xchg;
4151 case LLVMAtomicRMWBinOpAdd: return AtomicRMWInst::Add;
4152 case LLVMAtomicRMWBinOpSub: return AtomicRMWInst::Sub;
4153 case LLVMAtomicRMWBinOpAnd: return AtomicRMWInst::And;
4154 case LLVMAtomicRMWBinOpNand: return AtomicRMWInst::Nand;
4155 case LLVMAtomicRMWBinOpOr: return AtomicRMWInst::Or;
4156 case LLVMAtomicRMWBinOpXor: return AtomicRMWInst::Xor;
4157 case LLVMAtomicRMWBinOpMax: return AtomicRMWInst::Max;
4158 case LLVMAtomicRMWBinOpMin: return AtomicRMWInst::Min;
4159 case LLVMAtomicRMWBinOpUMax: return AtomicRMWInst::UMax;
4160 case LLVMAtomicRMWBinOpUMin: return AtomicRMWInst::UMin;
4161 case LLVMAtomicRMWBinOpFAdd: return AtomicRMWInst::FAdd;
4162 case LLVMAtomicRMWBinOpFSub: return AtomicRMWInst::FSub;
4163 case LLVMAtomicRMWBinOpFMax: return AtomicRMWInst::FMax;
4164 case LLVMAtomicRMWBinOpFMin: return AtomicRMWInst::FMin;
4165 case LLVMAtomicRMWBinOpFMaximum:
4166 return AtomicRMWInst::FMaximum;
4167 case LLVMAtomicRMWBinOpFMinimum:
4168 return AtomicRMWInst::FMinimum;
4169 case LLVMAtomicRMWBinOpFMaximumNum:
4170 return AtomicRMWInst::FMaximumNum;
4171 case LLVMAtomicRMWBinOpFMinimumNum:
4172 return AtomicRMWInst::FMinimumNum;
4173 case LLVMAtomicRMWBinOpUIncWrap:
4174 return AtomicRMWInst::UIncWrap;
4175 case LLVMAtomicRMWBinOpUDecWrap:
4176 return AtomicRMWInst::UDecWrap;
4177 case LLVMAtomicRMWBinOpUSubCond:
4178 return AtomicRMWInst::USubCond;
4179 case LLVMAtomicRMWBinOpUSubSat:
4180 return AtomicRMWInst::USubSat;
4181 }
4182
4183 llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
4184}
4185
4186static LLVMAtomicRMWBinOp mapToLLVMRMWBinOp(AtomicRMWInst::BinOp BinOp) {
4187 switch (BinOp) {
4188 case AtomicRMWInst::Xchg: return LLVMAtomicRMWBinOpXchg;
4189 case AtomicRMWInst::Add: return LLVMAtomicRMWBinOpAdd;
4190 case AtomicRMWInst::Sub: return LLVMAtomicRMWBinOpSub;
4191 case AtomicRMWInst::And: return LLVMAtomicRMWBinOpAnd;
4192 case AtomicRMWInst::Nand: return LLVMAtomicRMWBinOpNand;
4193 case AtomicRMWInst::Or: return LLVMAtomicRMWBinOpOr;
4194 case AtomicRMWInst::Xor: return LLVMAtomicRMWBinOpXor;
4195 case AtomicRMWInst::Max: return LLVMAtomicRMWBinOpMax;
4196 case AtomicRMWInst::Min: return LLVMAtomicRMWBinOpMin;
4197 case AtomicRMWInst::UMax: return LLVMAtomicRMWBinOpUMax;
4198 case AtomicRMWInst::UMin: return LLVMAtomicRMWBinOpUMin;
4199 case AtomicRMWInst::FAdd: return LLVMAtomicRMWBinOpFAdd;
4200 case AtomicRMWInst::FSub: return LLVMAtomicRMWBinOpFSub;
4201 case AtomicRMWInst::FMax: return LLVMAtomicRMWBinOpFMax;
4202 case AtomicRMWInst::FMin: return LLVMAtomicRMWBinOpFMin;
4203 case AtomicRMWInst::FMaximum:
4204 return LLVMAtomicRMWBinOpFMaximum;
4205 case AtomicRMWInst::FMinimum:
4206 return LLVMAtomicRMWBinOpFMinimum;
4207 case AtomicRMWInst::FMaximumNum:
4208 return LLVMAtomicRMWBinOpFMaximumNum;
4209 case AtomicRMWInst::FMinimumNum:
4210 return LLVMAtomicRMWBinOpFMinimumNum;
4211 case AtomicRMWInst::UIncWrap:
4212 return LLVMAtomicRMWBinOpUIncWrap;
4213 case AtomicRMWInst::UDecWrap:
4214 return LLVMAtomicRMWBinOpUDecWrap;
4215 case AtomicRMWInst::USubCond:
4216 return LLVMAtomicRMWBinOpUSubCond;
4217 case AtomicRMWInst::USubSat:
4218 return LLVMAtomicRMWBinOpUSubSat;
4219 default: break;
4220 }
4221
4222 llvm_unreachable("Invalid AtomicRMWBinOp value!");
4223}
4224
4225LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
4226 LLVMBool isSingleThread, const char *Name) {
4227 return wrap(
4228 P: unwrap(P: B)->CreateFence(Ordering: mapFromLLVMOrdering(Ordering),
4229 SSID: isSingleThread ? SyncScope::SingleThread
4230 : SyncScope::System,
4231 Name));
4232}
4233
4234LLVMValueRef LLVMBuildFenceSyncScope(LLVMBuilderRef B,
4235 LLVMAtomicOrdering Ordering, unsigned SSID,
4236 const char *Name) {
4237 return wrap(
4238 P: unwrap(P: B)->CreateFence(Ordering: mapFromLLVMOrdering(Ordering), SSID, Name));
4239}
4240
4241LLVMValueRef LLVMBuildGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
4242 LLVMValueRef Pointer, LLVMValueRef *Indices,
4243 unsigned NumIndices, const char *Name) {
4244 ArrayRef<Value *> IdxList(unwrap(Vals: Indices), NumIndices);
4245 return wrap(P: unwrap(P: B)->CreateGEP(Ty: unwrap(P: Ty), Ptr: unwrap(P: Pointer), IdxList, Name));
4246}
4247
4248LLVMValueRef LLVMBuildInBoundsGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
4249 LLVMValueRef Pointer, LLVMValueRef *Indices,
4250 unsigned NumIndices, const char *Name) {
4251 ArrayRef<Value *> IdxList(unwrap(Vals: Indices), NumIndices);
4252 return wrap(
4253 P: unwrap(P: B)->CreateInBoundsGEP(Ty: unwrap(P: Ty), Ptr: unwrap(P: Pointer), IdxList, Name));
4254}
4255
4256LLVMValueRef LLVMBuildGEPWithNoWrapFlags(LLVMBuilderRef B, LLVMTypeRef Ty,
4257 LLVMValueRef Pointer,
4258 LLVMValueRef *Indices,
4259 unsigned NumIndices, const char *Name,
4260 LLVMGEPNoWrapFlags NoWrapFlags) {
4261 ArrayRef<Value *> IdxList(unwrap(Vals: Indices), NumIndices);
4262 return wrap(P: unwrap(P: B)->CreateGEP(Ty: unwrap(P: Ty), Ptr: unwrap(P: Pointer), IdxList, Name,
4263 NW: mapFromLLVMGEPNoWrapFlags(GEPFlags: NoWrapFlags)));
4264}
4265
4266LLVMValueRef LLVMBuildStructGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
4267 LLVMValueRef Pointer, unsigned Idx,
4268 const char *Name) {
4269 return wrap(
4270 P: unwrap(P: B)->CreateStructGEP(Ty: unwrap(P: Ty), Ptr: unwrap(P: Pointer), Idx, Name));
4271}
4272
4273LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
4274 const char *Name) {
4275 return wrap(P: unwrap(P: B)->CreateGlobalString(Str, Name));
4276}
4277
4278LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
4279 const char *Name) {
4280 return wrap(P: unwrap(P: B)->CreateGlobalString(Str, Name));
4281}
4282
4283LLVMBool LLVMGetVolatile(LLVMValueRef Inst) {
4284 return cast<Instruction>(Val: unwrap(P: Inst))->isVolatile();
4285}
4286
4287void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
4288 Value *P = unwrap(P: MemAccessInst);
4289 if (LoadInst *LI = dyn_cast<LoadInst>(Val: P))
4290 return LI->setVolatile(isVolatile);
4291 if (StoreInst *SI = dyn_cast<StoreInst>(Val: P))
4292 return SI->setVolatile(isVolatile);
4293 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(Val: P))
4294 return AI->setVolatile(isVolatile);
4295 return cast<AtomicCmpXchgInst>(Val: P)->setVolatile(isVolatile);
4296}
4297
4298LLVMBool LLVMGetWeak(LLVMValueRef CmpXchgInst) {
4299 return unwrap<AtomicCmpXchgInst>(P: CmpXchgInst)->isWeak();
4300}
4301
4302void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) {
4303 return unwrap<AtomicCmpXchgInst>(P: CmpXchgInst)->setWeak(isWeak);
4304}
4305
4306LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
4307 Value *P = unwrap(P: MemAccessInst);
4308 AtomicOrdering O;
4309 if (LoadInst *LI = dyn_cast<LoadInst>(Val: P))
4310 O = LI->getOrdering();
4311 else if (StoreInst *SI = dyn_cast<StoreInst>(Val: P))
4312 O = SI->getOrdering();
4313 else if (FenceInst *FI = dyn_cast<FenceInst>(Val: P))
4314 O = FI->getOrdering();
4315 else
4316 O = cast<AtomicRMWInst>(Val: P)->getOrdering();
4317 return mapToLLVMOrdering(Ordering: O);
4318}
4319
4320void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
4321 Value *P = unwrap(P: MemAccessInst);
4322 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
4323
4324 if (LoadInst *LI = dyn_cast<LoadInst>(Val: P))
4325 return LI->setOrdering(O);
4326 else if (FenceInst *FI = dyn_cast<FenceInst>(Val: P))
4327 return FI->setOrdering(O);
4328 else if (AtomicRMWInst *ARWI = dyn_cast<AtomicRMWInst>(Val: P))
4329 return ARWI->setOrdering(O);
4330 return cast<StoreInst>(Val: P)->setOrdering(O);
4331}
4332
4333LLVMAtomicRMWBinOp LLVMGetAtomicRMWBinOp(LLVMValueRef Inst) {
4334 return mapToLLVMRMWBinOp(BinOp: unwrap<AtomicRMWInst>(P: Inst)->getOperation());
4335}
4336
4337void LLVMSetAtomicRMWBinOp(LLVMValueRef Inst, LLVMAtomicRMWBinOp BinOp) {
4338 unwrap<AtomicRMWInst>(P: Inst)->setOperation(mapFromLLVMRMWBinOp(BinOp));
4339}
4340
4341/*--.. Casts ...............................................................--*/
4342
4343LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
4344 LLVMTypeRef DestTy, const char *Name) {
4345 return wrap(P: unwrap(P: B)->CreateTrunc(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4346}
4347
4348LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
4349 LLVMTypeRef DestTy, const char *Name) {
4350 return wrap(P: unwrap(P: B)->CreateZExt(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4351}
4352
4353LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
4354 LLVMTypeRef DestTy, const char *Name) {
4355 return wrap(P: unwrap(P: B)->CreateSExt(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4356}
4357
4358LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
4359 LLVMTypeRef DestTy, const char *Name) {
4360 return wrap(P: unwrap(P: B)->CreateFPToUI(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4361}
4362
4363LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
4364 LLVMTypeRef DestTy, const char *Name) {
4365 return wrap(P: unwrap(P: B)->CreateFPToSI(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4366}
4367
4368LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
4369 LLVMTypeRef DestTy, const char *Name) {
4370 return wrap(P: unwrap(P: B)->CreateUIToFP(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4371}
4372
4373LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
4374 LLVMTypeRef DestTy, const char *Name) {
4375 return wrap(P: unwrap(P: B)->CreateSIToFP(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4376}
4377
4378LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
4379 LLVMTypeRef DestTy, const char *Name) {
4380 return wrap(P: unwrap(P: B)->CreateFPTrunc(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4381}
4382
4383LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
4384 LLVMTypeRef DestTy, const char *Name) {
4385 return wrap(P: unwrap(P: B)->CreateFPExt(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4386}
4387
4388LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
4389 LLVMTypeRef DestTy, const char *Name) {
4390 return wrap(P: unwrap(P: B)->CreatePtrToInt(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4391}
4392
4393LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
4394 LLVMTypeRef DestTy, const char *Name) {
4395 return wrap(P: unwrap(P: B)->CreateIntToPtr(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4396}
4397
4398LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
4399 LLVMTypeRef DestTy, const char *Name) {
4400 return wrap(P: unwrap(P: B)->CreateBitCast(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4401}
4402
4403LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
4404 LLVMTypeRef DestTy, const char *Name) {
4405 return wrap(P: unwrap(P: B)->CreateAddrSpaceCast(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4406}
4407
4408LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
4409 LLVMTypeRef DestTy, const char *Name) {
4410 return wrap(P: unwrap(P: B)->CreateZExtOrBitCast(V: unwrap(P: Val), DestTy: unwrap(P: DestTy),
4411 Name));
4412}
4413
4414LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
4415 LLVMTypeRef DestTy, const char *Name) {
4416 return wrap(P: unwrap(P: B)->CreateSExtOrBitCast(V: unwrap(P: Val), DestTy: unwrap(P: DestTy),
4417 Name));
4418}
4419
4420LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
4421 LLVMTypeRef DestTy, const char *Name) {
4422 return wrap(P: unwrap(P: B)->CreateTruncOrBitCast(V: unwrap(P: Val), DestTy: unwrap(P: DestTy),
4423 Name));
4424}
4425
4426LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
4427 LLVMTypeRef DestTy, const char *Name) {
4428 return wrap(P: unwrap(P: B)->CreateCast(Op: Instruction::CastOps(map_from_llvmopcode(code: Op)), V: unwrap(P: Val),
4429 DestTy: unwrap(P: DestTy), Name));
4430}
4431
4432LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
4433 LLVMTypeRef DestTy, const char *Name) {
4434 return wrap(P: unwrap(P: B)->CreatePointerCast(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4435}
4436
4437LLVMValueRef LLVMBuildIntCast2(LLVMBuilderRef B, LLVMValueRef Val,
4438 LLVMTypeRef DestTy, LLVMBool IsSigned,
4439 const char *Name) {
4440 return wrap(
4441 P: unwrap(P: B)->CreateIntCast(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), isSigned: IsSigned, Name));
4442}
4443
4444LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
4445 LLVMTypeRef DestTy, const char *Name) {
4446 return wrap(P: unwrap(P: B)->CreateIntCast(V: unwrap(P: Val), DestTy: unwrap(P: DestTy),
4447 /*isSigned*/true, Name));
4448}
4449
4450LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
4451 LLVMTypeRef DestTy, const char *Name) {
4452 return wrap(P: unwrap(P: B)->CreateFPCast(V: unwrap(P: Val), DestTy: unwrap(P: DestTy), Name));
4453}
4454
4455LLVMOpcode LLVMGetCastOpcode(LLVMValueRef Src, LLVMBool SrcIsSigned,
4456 LLVMTypeRef DestTy, LLVMBool DestIsSigned) {
4457 return map_to_llvmopcode(opcode: CastInst::getCastOpcode(
4458 Val: unwrap(P: Src), SrcIsSigned, Ty: unwrap(P: DestTy), DstIsSigned: DestIsSigned));
4459}
4460
4461/*--.. Comparisons .........................................................--*/
4462
4463LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
4464 LLVMValueRef LHS, LLVMValueRef RHS,
4465 const char *Name) {
4466 return wrap(P: unwrap(P: B)->CreateICmp(P: static_cast<ICmpInst::Predicate>(Op),
4467 LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
4468}
4469
4470LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
4471 LLVMValueRef LHS, LLVMValueRef RHS,
4472 const char *Name) {
4473 return wrap(P: unwrap(P: B)->CreateFCmp(P: static_cast<FCmpInst::Predicate>(Op),
4474 LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name));
4475}
4476
4477/*--.. Miscellaneous instructions ..........................................--*/
4478
4479LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
4480 return wrap(P: unwrap(P: B)->CreatePHI(Ty: unwrap(P: Ty), NumReservedValues: 0, Name));
4481}
4482
4483LLVMValueRef LLVMBuildCall2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
4484 LLVMValueRef *Args, unsigned NumArgs,
4485 const char *Name) {
4486 FunctionType *FTy = unwrap<FunctionType>(P: Ty);
4487 return wrap(P: unwrap(P: B)->CreateCall(FTy, Callee: unwrap(P: Fn),
4488 Args: ArrayRef(unwrap(Vals: Args), NumArgs), Name));
4489}
4490
4491LLVMValueRef
4492LLVMBuildCallWithOperandBundles(LLVMBuilderRef B, LLVMTypeRef Ty,
4493 LLVMValueRef Fn, LLVMValueRef *Args,
4494 unsigned NumArgs, LLVMOperandBundleRef *Bundles,
4495 unsigned NumBundles, const char *Name) {
4496 FunctionType *FTy = unwrap<FunctionType>(P: Ty);
4497 SmallVector<OperandBundleDef, 8> OBs;
4498 for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
4499 OperandBundleDef *OB = unwrap(P: Bundle);
4500 OBs.push_back(Elt: *OB);
4501 }
4502 return wrap(P: unwrap(P: B)->CreateCall(
4503 FTy, Callee: unwrap(P: Fn), Args: ArrayRef(unwrap(Vals: Args), NumArgs), OpBundles: OBs, Name));
4504}
4505
4506LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
4507 LLVMValueRef Then, LLVMValueRef Else,
4508 const char *Name) {
4509 return wrap(P: unwrap(P: B)->CreateSelect(C: unwrap(P: If), True: unwrap(P: Then), False: unwrap(P: Else),
4510 Name));
4511}
4512
4513LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
4514 LLVMTypeRef Ty, const char *Name) {
4515 return wrap(P: unwrap(P: B)->CreateVAArg(List: unwrap(P: List), Ty: unwrap(P: Ty), Name));
4516}
4517
4518LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
4519 LLVMValueRef Index, const char *Name) {
4520 return wrap(P: unwrap(P: B)->CreateExtractElement(Vec: unwrap(P: VecVal), Idx: unwrap(P: Index),
4521 Name));
4522}
4523
4524LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
4525 LLVMValueRef EltVal, LLVMValueRef Index,
4526 const char *Name) {
4527 return wrap(P: unwrap(P: B)->CreateInsertElement(Vec: unwrap(P: VecVal), NewElt: unwrap(P: EltVal),
4528 Idx: unwrap(P: Index), Name));
4529}
4530
4531LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
4532 LLVMValueRef V2, LLVMValueRef Mask,
4533 const char *Name) {
4534 return wrap(P: unwrap(P: B)->CreateShuffleVector(V1: unwrap(P: V1), V2: unwrap(P: V2),
4535 Mask: unwrap(P: Mask), Name));
4536}
4537
4538LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
4539 unsigned Index, const char *Name) {
4540 return wrap(P: unwrap(P: B)->CreateExtractValue(Agg: unwrap(P: AggVal), Idxs: Index, Name));
4541}
4542
4543LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
4544 LLVMValueRef EltVal, unsigned Index,
4545 const char *Name) {
4546 return wrap(P: unwrap(P: B)->CreateInsertValue(Agg: unwrap(P: AggVal), Val: unwrap(P: EltVal),
4547 Idxs: Index, Name));
4548}
4549
4550LLVMValueRef LLVMBuildFreeze(LLVMBuilderRef B, LLVMValueRef Val,
4551 const char *Name) {
4552 return wrap(P: unwrap(P: B)->CreateFreeze(V: unwrap(P: Val), Name));
4553}
4554
4555LLVMValueRef LLVMBuildBitInsert(LLVMBuilderRef B, LLVMValueRef Base,
4556 LLVMValueRef Val, LLVMValueRef Offset,
4557 const char *Name) {
4558 return wrap(P: unwrap(P: B)->CreateBitInsert(Base: unwrap(P: Base), Val: unwrap(P: Val),
4559 Offset: unwrap(P: Offset), Name));
4560}
4561
4562LLVMValueRef LLVMBuildBitExtract(LLVMBuilderRef B, LLVMTypeRef Ty,
4563 LLVMValueRef Src, LLVMValueRef Offset,
4564 const char *Name) {
4565 return wrap(P: unwrap(P: B)->CreateBitExtract(Ty: unwrap(P: Ty), Src: unwrap(P: Src),
4566 Offset: unwrap(P: Offset), Name));
4567}
4568
4569LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
4570 const char *Name) {
4571 return wrap(P: unwrap(P: B)->CreateIsNull(Arg: unwrap(P: Val), Name));
4572}
4573
4574LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
4575 const char *Name) {
4576 return wrap(P: unwrap(P: B)->CreateIsNotNull(Arg: unwrap(P: Val), Name));
4577}
4578
4579LLVMValueRef LLVMBuildPtrDiff2(LLVMBuilderRef B, LLVMTypeRef ElemTy,
4580 LLVMValueRef LHS, LLVMValueRef RHS,
4581 const char *Name) {
4582 IRBuilderBase *Builder = unwrap(P: B);
4583 Value *Diff =
4584 Builder->CreatePtrDiff(ElemTy: unwrap(P: ElemTy), LHS: unwrap(P: LHS), RHS: unwrap(P: RHS), Name);
4585 return wrap(P: Builder->CreateSExtOrTrunc(V: Diff, DestTy: Builder->getInt64Ty()));
4586}
4587
4588LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
4589 LLVMValueRef PTR, LLVMValueRef Val,
4590 LLVMAtomicOrdering ordering,
4591 LLVMBool singleThread) {
4592 AtomicRMWInst::BinOp intop = mapFromLLVMRMWBinOp(BinOp: op);
4593 return wrap(P: unwrap(P: B)->CreateAtomicRMW(
4594 Op: intop, Ptr: unwrap(P: PTR), Val: unwrap(P: Val), Align: MaybeAlign(),
4595 Ordering: mapFromLLVMOrdering(Ordering: ordering),
4596 SSID: singleThread ? SyncScope::SingleThread : SyncScope::System));
4597}
4598
4599LLVMValueRef LLVMBuildAtomicRMWSyncScope(LLVMBuilderRef B,
4600 LLVMAtomicRMWBinOp op,
4601 LLVMValueRef PTR, LLVMValueRef Val,
4602 LLVMAtomicOrdering ordering,
4603 unsigned SSID) {
4604 AtomicRMWInst::BinOp intop = mapFromLLVMRMWBinOp(BinOp: op);
4605 return wrap(P: unwrap(P: B)->CreateAtomicRMW(Op: intop, Ptr: unwrap(P: PTR), Val: unwrap(P: Val),
4606 Align: MaybeAlign(),
4607 Ordering: mapFromLLVMOrdering(Ordering: ordering), SSID));
4608}
4609
4610LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
4611 LLVMValueRef Cmp, LLVMValueRef New,
4612 LLVMAtomicOrdering SuccessOrdering,
4613 LLVMAtomicOrdering FailureOrdering,
4614 LLVMBool singleThread) {
4615
4616 return wrap(P: unwrap(P: B)->CreateAtomicCmpXchg(
4617 Ptr: unwrap(P: Ptr), Cmp: unwrap(P: Cmp), New: unwrap(P: New), Align: MaybeAlign(),
4618 SuccessOrdering: mapFromLLVMOrdering(Ordering: SuccessOrdering),
4619 FailureOrdering: mapFromLLVMOrdering(Ordering: FailureOrdering),
4620 SSID: singleThread ? SyncScope::SingleThread : SyncScope::System));
4621}
4622
4623LLVMValueRef LLVMBuildAtomicCmpXchgSyncScope(LLVMBuilderRef B, LLVMValueRef Ptr,
4624 LLVMValueRef Cmp, LLVMValueRef New,
4625 LLVMAtomicOrdering SuccessOrdering,
4626 LLVMAtomicOrdering FailureOrdering,
4627 unsigned SSID) {
4628 return wrap(P: unwrap(P: B)->CreateAtomicCmpXchg(
4629 Ptr: unwrap(P: Ptr), Cmp: unwrap(P: Cmp), New: unwrap(P: New), Align: MaybeAlign(),
4630 SuccessOrdering: mapFromLLVMOrdering(Ordering: SuccessOrdering),
4631 FailureOrdering: mapFromLLVMOrdering(Ordering: FailureOrdering), SSID));
4632}
4633
4634unsigned LLVMGetNumMaskElements(LLVMValueRef SVInst) {
4635 Value *P = unwrap(P: SVInst);
4636 ShuffleVectorInst *I = cast<ShuffleVectorInst>(Val: P);
4637 return I->getShuffleMask().size();
4638}
4639
4640int LLVMGetMaskValue(LLVMValueRef SVInst, unsigned Elt) {
4641 Value *P = unwrap(P: SVInst);
4642 ShuffleVectorInst *I = cast<ShuffleVectorInst>(Val: P);
4643 return I->getMaskValue(Elt);
4644}
4645
4646int LLVMGetUndefMaskElem(void) { return PoisonMaskElem; }
4647
4648LLVMBool LLVMIsAtomic(LLVMValueRef Inst) {
4649 return unwrap<Instruction>(P: Inst)->isAtomic();
4650}
4651
4652LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
4653 // Backwards compatibility: return false for non-atomic instructions
4654 Instruction *I = unwrap<Instruction>(P: AtomicInst);
4655 if (!I->isAtomic())
4656 return 0;
4657
4658 return *getAtomicSyncScopeID(I) == SyncScope::SingleThread;
4659}
4660
4661void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
4662 // Backwards compatibility: ignore non-atomic instructions
4663 Instruction *I = unwrap<Instruction>(P: AtomicInst);
4664 if (!I->isAtomic())
4665 return;
4666
4667 SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
4668 setAtomicSyncScopeID(I, SSID);
4669}
4670
4671unsigned LLVMGetAtomicSyncScopeID(LLVMValueRef AtomicInst) {
4672 Instruction *I = unwrap<Instruction>(P: AtomicInst);
4673 assert(I->isAtomic() && "Expected an atomic instruction");
4674 return *getAtomicSyncScopeID(I);
4675}
4676
4677void LLVMSetAtomicSyncScopeID(LLVMValueRef AtomicInst, unsigned SSID) {
4678 Instruction *I = unwrap<Instruction>(P: AtomicInst);
4679 assert(I->isAtomic() && "Expected an atomic instruction");
4680 setAtomicSyncScopeID(I, SSID);
4681}
4682
4683LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
4684 Value *P = unwrap(P: CmpXchgInst);
4685 return mapToLLVMOrdering(Ordering: cast<AtomicCmpXchgInst>(Val: P)->getSuccessOrdering());
4686}
4687
4688void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
4689 LLVMAtomicOrdering Ordering) {
4690 Value *P = unwrap(P: CmpXchgInst);
4691 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
4692
4693 return cast<AtomicCmpXchgInst>(Val: P)->setSuccessOrdering(O);
4694}
4695
4696LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
4697 Value *P = unwrap(P: CmpXchgInst);
4698 return mapToLLVMOrdering(Ordering: cast<AtomicCmpXchgInst>(Val: P)->getFailureOrdering());
4699}
4700
4701void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
4702 LLVMAtomicOrdering Ordering) {
4703 Value *P = unwrap(P: CmpXchgInst);
4704 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
4705
4706 return cast<AtomicCmpXchgInst>(Val: P)->setFailureOrdering(O);
4707}
4708
4709/*===-- Module providers --------------------------------------------------===*/
4710
4711LLVMModuleProviderRef
4712LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
4713 return reinterpret_cast<LLVMModuleProviderRef>(M);
4714}
4715
4716void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
4717 delete unwrap(MP);
4718}
4719
4720
4721/*===-- Memory buffers ----------------------------------------------------===*/
4722
4723LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
4724 const char *Path,
4725 LLVMMemoryBufferRef *OutMemBuf,
4726 char **OutMessage) {
4727
4728 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Filename: Path);
4729 if (std::error_code EC = MBOrErr.getError()) {
4730 *OutMessage = strdup(s: EC.message().c_str());
4731 return 1;
4732 }
4733 *OutMemBuf = wrap(P: MBOrErr.get().release());
4734 return 0;
4735}
4736
4737LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
4738 char **OutMessage) {
4739 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
4740 if (std::error_code EC = MBOrErr.getError()) {
4741 *OutMessage = strdup(s: EC.message().c_str());
4742 return 1;
4743 }
4744 *OutMemBuf = wrap(P: MBOrErr.get().release());
4745 return 0;
4746}
4747
4748LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
4749 const char *InputData,
4750 size_t InputDataLength,
4751 const char *BufferName,
4752 LLVMBool RequiresNullTerminator) {
4753
4754 return wrap(P: MemoryBuffer::getMemBuffer(InputData: StringRef(InputData, InputDataLength),
4755 BufferName: StringRef(BufferName),
4756 RequiresNullTerminator).release());
4757}
4758
4759LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
4760 const char *InputData,
4761 size_t InputDataLength,
4762 const char *BufferName) {
4763
4764 return wrap(
4765 P: MemoryBuffer::getMemBufferCopy(InputData: StringRef(InputData, InputDataLength),
4766 BufferName: StringRef(BufferName)).release());
4767}
4768
4769const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
4770 return unwrap(P: MemBuf)->getBufferStart();
4771}
4772
4773size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
4774 return unwrap(P: MemBuf)->getBufferSize();
4775}
4776
4777void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
4778 delete unwrap(P: MemBuf);
4779}
4780
4781/*===-- Pass Manager ------------------------------------------------------===*/
4782
4783LLVMPassManagerRef LLVMCreatePassManager() {
4784 return wrap(P: new legacy::PassManager());
4785}
4786
4787LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
4788 return wrap(P: new legacy::FunctionPassManager(unwrap(P: M)));
4789}
4790
4791LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
4792 return LLVMCreateFunctionPassManagerForModule(
4793 M: reinterpret_cast<LLVMModuleRef>(P));
4794}
4795
4796LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
4797 return unwrap<legacy::PassManager>(P: PM)->run(M&: *unwrap(P: M));
4798}
4799
4800LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
4801 return unwrap<legacy::FunctionPassManager>(P: FPM)->doInitialization();
4802}
4803
4804LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
4805 return unwrap<legacy::FunctionPassManager>(P: FPM)->run(F&: *unwrap<Function>(P: F));
4806}
4807
4808LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
4809 return unwrap<legacy::FunctionPassManager>(P: FPM)->doFinalization();
4810}
4811
4812void LLVMDisposePassManager(LLVMPassManagerRef PM) {
4813 delete unwrap(P: PM);
4814}
4815
4816/*===-- Threading ------------------------------------------------------===*/
4817
4818LLVMBool LLVMStartMultithreaded() {
4819 return LLVMIsMultithreaded();
4820}
4821
4822void LLVMStopMultithreaded() {
4823}
4824
4825LLVMBool LLVMIsMultithreaded() {
4826 return llvm_is_multithreaded();
4827}
4828
4829/*===-- Data Layout ----------------------------------------------------===*/
4830
4831LLVMTargetDataRef LLVMGetModuleDataLayout(LLVMModuleRef M) {
4832 return wrap(P: &unwrap(P: M)->getDataLayout());
4833}
4834
4835void LLVMSetModuleDataLayout(LLVMModuleRef M, LLVMTargetDataRef DL) {
4836 unwrap(P: M)->setDataLayout(*unwrap(P: DL));
4837}
4838
4839LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep) {
4840 return wrap(P: new DataLayout(StringRep));
4841}
4842
4843void LLVMDisposeTargetData(LLVMTargetDataRef TD) { delete unwrap(P: TD); }
4844
4845char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD) {
4846 std::string StringRep = unwrap(P: TD)->getStringRepresentation();
4847 return strdup(s: StringRep.c_str());
4848}
4849
4850LLVMByteOrdering LLVMByteOrder(LLVMTargetDataRef TD) {
4851 return unwrap(P: TD)->isLittleEndian() ? LLVMLittleEndian : LLVMBigEndian;
4852}
4853
4854unsigned LLVMPointerSize(LLVMTargetDataRef TD) {
4855 return unwrap(P: TD)->getPointerSize(AS: 0);
4856}
4857
4858unsigned LLVMPointerSizeForAS(LLVMTargetDataRef TD, unsigned AS) {
4859 return unwrap(P: TD)->getPointerSize(AS);
4860}
4861
4862LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD) {
4863 return wrap(P: unwrap(P: TD)->getIntPtrType(C&: *unwrap(P: getGlobalContextForCAPI())));
4864}
4865
4866LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, unsigned AS) {
4867 return wrap(
4868 P: unwrap(P: TD)->getIntPtrType(C&: *unwrap(P: getGlobalContextForCAPI()), AddressSpace: AS));
4869}
4870
4871LLVMTypeRef LLVMIntPtrTypeInContext(LLVMContextRef C, LLVMTargetDataRef TD) {
4872 return wrap(P: unwrap(P: TD)->getIntPtrType(C&: *unwrap(P: C)));
4873}
4874
4875LLVMTypeRef LLVMIntPtrTypeForASInContext(LLVMContextRef C, LLVMTargetDataRef TD,
4876 unsigned AS) {
4877 return wrap(P: unwrap(P: TD)->getIntPtrType(C&: *unwrap(P: C), AddressSpace: AS));
4878}
4879
4880unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
4881 return unwrap(P: TD)->getTypeSizeInBits(Ty: unwrap(P: Ty));
4882}
4883
4884unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
4885 return unwrap(P: TD)->getTypeStoreSize(Ty: unwrap(P: Ty));
4886}
4887
4888unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
4889 return unwrap(P: TD)->getTypeAllocSize(Ty: unwrap(P: Ty));
4890}
4891
4892unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
4893 return unwrap(P: TD)->getABITypeAlign(Ty: unwrap(P: Ty)).value();
4894}
4895
4896unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
4897 return unwrap(P: TD)->getABITypeAlign(Ty: unwrap(P: Ty)).value();
4898}
4899
4900unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
4901 return unwrap(P: TD)->getPrefTypeAlign(Ty: unwrap(P: Ty)).value();
4902}
4903
4904unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,
4905 LLVMValueRef GlobalVar) {
4906 return unwrap(P: TD)
4907 ->getPreferredAlign(GV: unwrap<GlobalVariable>(P: GlobalVar))
4908 .value();
4909}
4910
4911unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
4912 unsigned long long Offset) {
4913 StructType *STy = unwrap<StructType>(P: StructTy);
4914 return unwrap(P: TD)->getStructLayout(Ty: STy)->getElementContainingOffset(FixedOffset: Offset);
4915}
4916
4917unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD,
4918 LLVMTypeRef StructTy, unsigned Element) {
4919 StructType *STy = unwrap<StructType>(P: StructTy);
4920 return unwrap(P: TD)->getStructLayout(Ty: STy)->getElementOffset(Idx: Element);
4921}
4922