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