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

source code of llvm/lib/IR/Core.cpp