1 //===-- Core.cpp ----------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the common infrastructure (including the C bindings)
10 // for libLLVMCore.a, which implements the LLVM intermediate representation.
12 //===----------------------------------------------------------------------===//
14 #include "llvm-c/Core.h"
15 #include "llvm-c/Types.h"
16 #include "llvm/IR/Attributes.h"
17 #include "llvm/IR/BasicBlock.h"
18 #include "llvm/IR/ConstantRange.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DebugInfoMetadata.h"
21 #include "llvm/IR/DebugProgramInstruction.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/DiagnosticInfo.h"
24 #include "llvm/IR/DiagnosticPrinter.h"
25 #include "llvm/IR/GlobalAlias.h"
26 #include "llvm/IR/GlobalVariable.h"
27 #include "llvm/IR/IRBuilder.h"
28 #include "llvm/IR/InlineAsm.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/IntrinsicInst.h"
31 #include "llvm/IR/LLVMContext.h"
32 #include "llvm/IR/LegacyPassManager.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/InitializePasses.h"
35 #include "llvm/PassRegistry.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/FileSystem.h"
39 #include "llvm/Support/ManagedStatic.h"
40 #include "llvm/Support/MathExtras.h"
41 #include "llvm/Support/MemoryBuffer.h"
42 #include "llvm/Support/Threading.h"
43 #include "llvm/Support/raw_ostream.h"
47 #include <system_error>
51 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OperandBundleDef
, LLVMOperandBundleRef
)
53 inline BasicBlock
**unwrap(LLVMBasicBlockRef
*BBs
) {
54 return reinterpret_cast<BasicBlock
**>(BBs
);
57 #define DEBUG_TYPE "ir"
59 void llvm::initializeCore(PassRegistry
&Registry
) {
60 initializeDominatorTreeWrapperPassPass(Registry
);
61 initializePrintModulePassWrapperPass(Registry
);
62 initializePrintFunctionPassWrapperPass(Registry
);
63 initializeSafepointIRVerifierPass(Registry
);
64 initializeVerifierLegacyPassPass(Registry
);
71 /*===-- Version query -----------------------------------------------------===*/
73 void LLVMGetVersion(unsigned *Major
, unsigned *Minor
, unsigned *Patch
) {
75 *Major
= LLVM_VERSION_MAJOR
;
77 *Minor
= LLVM_VERSION_MINOR
;
79 *Patch
= LLVM_VERSION_PATCH
;
82 /*===-- Error handling ----------------------------------------------------===*/
84 char *LLVMCreateMessage(const char *Message
) {
85 return strdup(Message
);
88 void LLVMDisposeMessage(char *Message
) {
93 /*===-- Operations on contexts --------------------------------------------===*/
95 static LLVMContext
&getGlobalContext() {
96 static LLVMContext GlobalContext
;
100 LLVMContextRef
LLVMContextCreate() {
101 return wrap(new LLVMContext());
104 LLVMContextRef
LLVMGetGlobalContext() { return wrap(&getGlobalContext()); }
106 void LLVMContextSetDiagnosticHandler(LLVMContextRef C
,
107 LLVMDiagnosticHandler Handler
,
108 void *DiagnosticContext
) {
109 unwrap(C
)->setDiagnosticHandlerCallBack(
110 LLVM_EXTENSION
reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy
>(
115 LLVMDiagnosticHandler
LLVMContextGetDiagnosticHandler(LLVMContextRef C
) {
116 return LLVM_EXTENSION
reinterpret_cast<LLVMDiagnosticHandler
>(
117 unwrap(C
)->getDiagnosticHandlerCallBack());
120 void *LLVMContextGetDiagnosticContext(LLVMContextRef C
) {
121 return unwrap(C
)->getDiagnosticContext();
124 void LLVMContextSetYieldCallback(LLVMContextRef C
, LLVMYieldCallback Callback
,
125 void *OpaqueHandle
) {
127 LLVM_EXTENSION
reinterpret_cast<LLVMContext::YieldCallbackTy
>(Callback
);
128 unwrap(C
)->setYieldCallback(YieldCallback
, OpaqueHandle
);
131 LLVMBool
LLVMContextShouldDiscardValueNames(LLVMContextRef C
) {
132 return unwrap(C
)->shouldDiscardValueNames();
135 void LLVMContextSetDiscardValueNames(LLVMContextRef C
, LLVMBool Discard
) {
136 unwrap(C
)->setDiscardValueNames(Discard
);
139 void LLVMContextDispose(LLVMContextRef C
) {
143 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C
, const char *Name
,
145 return unwrap(C
)->getMDKindID(StringRef(Name
, SLen
));
148 unsigned LLVMGetMDKindID(const char *Name
, unsigned SLen
) {
149 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name
, SLen
);
152 unsigned LLVMGetSyncScopeID(LLVMContextRef C
, const char *Name
, size_t SLen
) {
153 return unwrap(C
)->getOrInsertSyncScopeID(StringRef(Name
, SLen
));
156 unsigned LLVMGetEnumAttributeKindForName(const char *Name
, size_t SLen
) {
157 return Attribute::getAttrKindFromName(StringRef(Name
, SLen
));
160 unsigned LLVMGetLastEnumAttributeKind(void) {
161 return Attribute::AttrKind::EndAttrKinds
;
164 LLVMAttributeRef
LLVMCreateEnumAttribute(LLVMContextRef C
, unsigned KindID
,
166 auto &Ctx
= *unwrap(C
);
167 auto AttrKind
= (Attribute::AttrKind
)KindID
;
168 return wrap(Attribute::get(Ctx
, AttrKind
, Val
));
171 unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A
) {
172 return unwrap(A
).getKindAsEnum();
175 uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A
) {
176 auto Attr
= unwrap(A
);
177 if (Attr
.isEnumAttribute())
179 return Attr
.getValueAsInt();
182 LLVMAttributeRef
LLVMCreateTypeAttribute(LLVMContextRef C
, unsigned KindID
,
183 LLVMTypeRef type_ref
) {
184 auto &Ctx
= *unwrap(C
);
185 auto AttrKind
= (Attribute::AttrKind
)KindID
;
186 return wrap(Attribute::get(Ctx
, AttrKind
, unwrap(type_ref
)));
189 LLVMTypeRef
LLVMGetTypeAttributeValue(LLVMAttributeRef A
) {
190 auto Attr
= unwrap(A
);
191 return wrap(Attr
.getValueAsType());
194 LLVMAttributeRef
LLVMCreateConstantRangeAttribute(LLVMContextRef C
,
197 const uint64_t LowerWords
[],
198 const uint64_t UpperWords
[]) {
199 auto &Ctx
= *unwrap(C
);
200 auto AttrKind
= (Attribute::AttrKind
)KindID
;
201 unsigned NumWords
= divideCeil(NumBits
, 64);
202 return wrap(Attribute::get(
204 ConstantRange(APInt(NumBits
, ArrayRef(LowerWords
, NumWords
)),
205 APInt(NumBits
, ArrayRef(UpperWords
, NumWords
)))));
208 LLVMAttributeRef
LLVMCreateStringAttribute(LLVMContextRef C
,
209 const char *K
, unsigned KLength
,
210 const char *V
, unsigned VLength
) {
211 return wrap(Attribute::get(*unwrap(C
), StringRef(K
, KLength
),
212 StringRef(V
, VLength
)));
215 const char *LLVMGetStringAttributeKind(LLVMAttributeRef A
,
217 auto S
= unwrap(A
).getKindAsString();
222 const char *LLVMGetStringAttributeValue(LLVMAttributeRef A
,
224 auto S
= unwrap(A
).getValueAsString();
229 LLVMBool
LLVMIsEnumAttribute(LLVMAttributeRef A
) {
230 auto Attr
= unwrap(A
);
231 return Attr
.isEnumAttribute() || Attr
.isIntAttribute();
234 LLVMBool
LLVMIsStringAttribute(LLVMAttributeRef A
) {
235 return unwrap(A
).isStringAttribute();
238 LLVMBool
LLVMIsTypeAttribute(LLVMAttributeRef A
) {
239 return unwrap(A
).isTypeAttribute();
242 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI
) {
243 std::string MsgStorage
;
244 raw_string_ostream
Stream(MsgStorage
);
245 DiagnosticPrinterRawOStream
DP(Stream
);
247 unwrap(DI
)->print(DP
);
250 return LLVMCreateMessage(MsgStorage
.c_str());
253 LLVMDiagnosticSeverity
LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI
) {
254 LLVMDiagnosticSeverity severity
;
256 switch(unwrap(DI
)->getSeverity()) {
258 severity
= LLVMDSError
;
261 severity
= LLVMDSWarning
;
264 severity
= LLVMDSRemark
;
267 severity
= LLVMDSNote
;
274 /*===-- Operations on modules ---------------------------------------------===*/
276 LLVMModuleRef
LLVMModuleCreateWithName(const char *ModuleID
) {
277 return wrap(new Module(ModuleID
, getGlobalContext()));
280 LLVMModuleRef
LLVMModuleCreateWithNameInContext(const char *ModuleID
,
282 return wrap(new Module(ModuleID
, *unwrap(C
)));
285 void LLVMDisposeModule(LLVMModuleRef M
) {
289 const char *LLVMGetModuleIdentifier(LLVMModuleRef M
, size_t *Len
) {
290 auto &Str
= unwrap(M
)->getModuleIdentifier();
295 void LLVMSetModuleIdentifier(LLVMModuleRef M
, const char *Ident
, size_t Len
) {
296 unwrap(M
)->setModuleIdentifier(StringRef(Ident
, Len
));
299 const char *LLVMGetSourceFileName(LLVMModuleRef M
, size_t *Len
) {
300 auto &Str
= unwrap(M
)->getSourceFileName();
305 void LLVMSetSourceFileName(LLVMModuleRef M
, const char *Name
, size_t Len
) {
306 unwrap(M
)->setSourceFileName(StringRef(Name
, Len
));
309 /*--.. Data layout .........................................................--*/
310 const char *LLVMGetDataLayoutStr(LLVMModuleRef M
) {
311 return unwrap(M
)->getDataLayoutStr().c_str();
314 const char *LLVMGetDataLayout(LLVMModuleRef M
) {
315 return LLVMGetDataLayoutStr(M
);
318 void LLVMSetDataLayout(LLVMModuleRef M
, const char *DataLayoutStr
) {
319 unwrap(M
)->setDataLayout(DataLayoutStr
);
322 /*--.. Target triple .......................................................--*/
323 const char * LLVMGetTarget(LLVMModuleRef M
) {
324 return unwrap(M
)->getTargetTriple().c_str();
327 void LLVMSetTarget(LLVMModuleRef M
, const char *Triple
) {
328 unwrap(M
)->setTargetTriple(Triple
);
331 /*--.. Module flags ........................................................--*/
332 struct LLVMOpaqueModuleFlagEntry
{
333 LLVMModuleFlagBehavior Behavior
;
336 LLVMMetadataRef Metadata
;
339 static Module::ModFlagBehavior
340 map_to_llvmModFlagBehavior(LLVMModuleFlagBehavior Behavior
) {
342 case LLVMModuleFlagBehaviorError
:
343 return Module::ModFlagBehavior::Error
;
344 case LLVMModuleFlagBehaviorWarning
:
345 return Module::ModFlagBehavior::Warning
;
346 case LLVMModuleFlagBehaviorRequire
:
347 return Module::ModFlagBehavior::Require
;
348 case LLVMModuleFlagBehaviorOverride
:
349 return Module::ModFlagBehavior::Override
;
350 case LLVMModuleFlagBehaviorAppend
:
351 return Module::ModFlagBehavior::Append
;
352 case LLVMModuleFlagBehaviorAppendUnique
:
353 return Module::ModFlagBehavior::AppendUnique
;
355 llvm_unreachable("Unknown LLVMModuleFlagBehavior");
358 static LLVMModuleFlagBehavior
359 map_from_llvmModFlagBehavior(Module::ModFlagBehavior Behavior
) {
361 case Module::ModFlagBehavior::Error
:
362 return LLVMModuleFlagBehaviorError
;
363 case Module::ModFlagBehavior::Warning
:
364 return LLVMModuleFlagBehaviorWarning
;
365 case Module::ModFlagBehavior::Require
:
366 return LLVMModuleFlagBehaviorRequire
;
367 case Module::ModFlagBehavior::Override
:
368 return LLVMModuleFlagBehaviorOverride
;
369 case Module::ModFlagBehavior::Append
:
370 return LLVMModuleFlagBehaviorAppend
;
371 case Module::ModFlagBehavior::AppendUnique
:
372 return LLVMModuleFlagBehaviorAppendUnique
;
374 llvm_unreachable("Unhandled Flag Behavior");
378 LLVMModuleFlagEntry
*LLVMCopyModuleFlagsMetadata(LLVMModuleRef M
, size_t *Len
) {
379 SmallVector
<Module::ModuleFlagEntry
, 8> MFEs
;
380 unwrap(M
)->getModuleFlagsMetadata(MFEs
);
382 LLVMOpaqueModuleFlagEntry
*Result
= static_cast<LLVMOpaqueModuleFlagEntry
*>(
383 safe_malloc(MFEs
.size() * sizeof(LLVMOpaqueModuleFlagEntry
)));
384 for (unsigned i
= 0; i
< MFEs
.size(); ++i
) {
385 const auto &ModuleFlag
= MFEs
[i
];
386 Result
[i
].Behavior
= map_from_llvmModFlagBehavior(ModuleFlag
.Behavior
);
387 Result
[i
].Key
= ModuleFlag
.Key
->getString().data();
388 Result
[i
].KeyLen
= ModuleFlag
.Key
->getString().size();
389 Result
[i
].Metadata
= wrap(ModuleFlag
.Val
);
395 void LLVMDisposeModuleFlagsMetadata(LLVMModuleFlagEntry
*Entries
) {
399 LLVMModuleFlagBehavior
400 LLVMModuleFlagEntriesGetFlagBehavior(LLVMModuleFlagEntry
*Entries
,
402 LLVMOpaqueModuleFlagEntry MFE
=
403 static_cast<LLVMOpaqueModuleFlagEntry
>(Entries
[Index
]);
407 const char *LLVMModuleFlagEntriesGetKey(LLVMModuleFlagEntry
*Entries
,
408 unsigned Index
, size_t *Len
) {
409 LLVMOpaqueModuleFlagEntry MFE
=
410 static_cast<LLVMOpaqueModuleFlagEntry
>(Entries
[Index
]);
415 LLVMMetadataRef
LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry
*Entries
,
417 LLVMOpaqueModuleFlagEntry MFE
=
418 static_cast<LLVMOpaqueModuleFlagEntry
>(Entries
[Index
]);
422 LLVMMetadataRef
LLVMGetModuleFlag(LLVMModuleRef M
,
423 const char *Key
, size_t KeyLen
) {
424 return wrap(unwrap(M
)->getModuleFlag({Key
, KeyLen
}));
427 void LLVMAddModuleFlag(LLVMModuleRef M
, LLVMModuleFlagBehavior Behavior
,
428 const char *Key
, size_t KeyLen
,
429 LLVMMetadataRef Val
) {
430 unwrap(M
)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior
),
431 {Key
, KeyLen
}, unwrap(Val
));
434 LLVMBool
LLVMIsNewDbgInfoFormat(LLVMModuleRef M
) {
435 return unwrap(M
)->IsNewDbgInfoFormat
;
438 void LLVMSetIsNewDbgInfoFormat(LLVMModuleRef M
, LLVMBool UseNewFormat
) {
439 unwrap(M
)->setIsNewDbgInfoFormat(UseNewFormat
);
442 /*--.. Printing modules ....................................................--*/
444 void LLVMDumpModule(LLVMModuleRef M
) {
445 unwrap(M
)->print(errs(), nullptr,
446 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
449 LLVMBool
LLVMPrintModuleToFile(LLVMModuleRef M
, const char *Filename
,
450 char **ErrorMessage
) {
452 raw_fd_ostream
dest(Filename
, EC
, sys::fs::OF_TextWithCRLF
);
454 *ErrorMessage
= strdup(EC
.message().c_str());
458 unwrap(M
)->print(dest
, nullptr);
462 if (dest
.has_error()) {
463 std::string E
= "Error printing to file: " + dest
.error().message();
464 *ErrorMessage
= strdup(E
.c_str());
471 char *LLVMPrintModuleToString(LLVMModuleRef M
) {
473 raw_string_ostream
os(buf
);
475 unwrap(M
)->print(os
, nullptr);
478 return strdup(buf
.c_str());
481 /*--.. Operations on inline assembler ......................................--*/
482 void LLVMSetModuleInlineAsm2(LLVMModuleRef M
, const char *Asm
, size_t Len
) {
483 unwrap(M
)->setModuleInlineAsm(StringRef(Asm
, Len
));
486 void LLVMSetModuleInlineAsm(LLVMModuleRef M
, const char *Asm
) {
487 unwrap(M
)->setModuleInlineAsm(StringRef(Asm
));
490 void LLVMAppendModuleInlineAsm(LLVMModuleRef M
, const char *Asm
, size_t Len
) {
491 unwrap(M
)->appendModuleInlineAsm(StringRef(Asm
, Len
));
494 const char *LLVMGetModuleInlineAsm(LLVMModuleRef M
, size_t *Len
) {
495 auto &Str
= unwrap(M
)->getModuleInlineAsm();
500 LLVMValueRef
LLVMGetInlineAsm(LLVMTypeRef Ty
, const char *AsmString
,
501 size_t AsmStringSize
, const char *Constraints
,
502 size_t ConstraintsSize
, LLVMBool HasSideEffects
,
503 LLVMBool IsAlignStack
,
504 LLVMInlineAsmDialect Dialect
, LLVMBool CanThrow
) {
505 InlineAsm::AsmDialect AD
;
507 case LLVMInlineAsmDialectATT
:
508 AD
= InlineAsm::AD_ATT
;
510 case LLVMInlineAsmDialectIntel
:
511 AD
= InlineAsm::AD_Intel
;
514 return wrap(InlineAsm::get(unwrap
<FunctionType
>(Ty
),
515 StringRef(AsmString
, AsmStringSize
),
516 StringRef(Constraints
, ConstraintsSize
),
517 HasSideEffects
, IsAlignStack
, AD
, CanThrow
));
520 const char *LLVMGetInlineAsmAsmString(LLVMValueRef InlineAsmVal
, size_t *Len
) {
522 Value
*Val
= unwrap
<Value
>(InlineAsmVal
);
523 const std::string
&AsmString
= cast
<InlineAsm
>(Val
)->getAsmString();
525 *Len
= AsmString
.length();
526 return AsmString
.c_str();
529 const char *LLVMGetInlineAsmConstraintString(LLVMValueRef InlineAsmVal
,
531 Value
*Val
= unwrap
<Value
>(InlineAsmVal
);
532 const std::string
&ConstraintString
=
533 cast
<InlineAsm
>(Val
)->getConstraintString();
535 *Len
= ConstraintString
.length();
536 return ConstraintString
.c_str();
539 LLVMInlineAsmDialect
LLVMGetInlineAsmDialect(LLVMValueRef InlineAsmVal
) {
541 Value
*Val
= unwrap
<Value
>(InlineAsmVal
);
542 InlineAsm::AsmDialect Dialect
= cast
<InlineAsm
>(Val
)->getDialect();
545 case InlineAsm::AD_ATT
:
546 return LLVMInlineAsmDialectATT
;
547 case InlineAsm::AD_Intel
:
548 return LLVMInlineAsmDialectIntel
;
551 llvm_unreachable("Unrecognized inline assembly dialect");
552 return LLVMInlineAsmDialectATT
;
555 LLVMTypeRef
LLVMGetInlineAsmFunctionType(LLVMValueRef InlineAsmVal
) {
556 Value
*Val
= unwrap
<Value
>(InlineAsmVal
);
557 return (LLVMTypeRef
)cast
<InlineAsm
>(Val
)->getFunctionType();
560 LLVMBool
LLVMGetInlineAsmHasSideEffects(LLVMValueRef InlineAsmVal
) {
561 Value
*Val
= unwrap
<Value
>(InlineAsmVal
);
562 return cast
<InlineAsm
>(Val
)->hasSideEffects();
565 LLVMBool
LLVMGetInlineAsmNeedsAlignedStack(LLVMValueRef InlineAsmVal
) {
566 Value
*Val
= unwrap
<Value
>(InlineAsmVal
);
567 return cast
<InlineAsm
>(Val
)->isAlignStack();
570 LLVMBool
LLVMGetInlineAsmCanUnwind(LLVMValueRef InlineAsmVal
) {
571 Value
*Val
= unwrap
<Value
>(InlineAsmVal
);
572 return cast
<InlineAsm
>(Val
)->canThrow();
575 /*--.. Operations on module contexts ......................................--*/
576 LLVMContextRef
LLVMGetModuleContext(LLVMModuleRef M
) {
577 return wrap(&unwrap(M
)->getContext());
581 /*===-- Operations on types -----------------------------------------------===*/
583 /*--.. Operations on all types (mostly) ....................................--*/
585 LLVMTypeKind
LLVMGetTypeKind(LLVMTypeRef Ty
) {
586 switch (unwrap(Ty
)->getTypeID()) {
588 return LLVMVoidTypeKind
;
590 return LLVMHalfTypeKind
;
591 case Type::BFloatTyID
:
592 return LLVMBFloatTypeKind
;
593 case Type::FloatTyID
:
594 return LLVMFloatTypeKind
;
595 case Type::DoubleTyID
:
596 return LLVMDoubleTypeKind
;
597 case Type::X86_FP80TyID
:
598 return LLVMX86_FP80TypeKind
;
599 case Type::FP128TyID
:
600 return LLVMFP128TypeKind
;
601 case Type::PPC_FP128TyID
:
602 return LLVMPPC_FP128TypeKind
;
603 case Type::LabelTyID
:
604 return LLVMLabelTypeKind
;
605 case Type::MetadataTyID
:
606 return LLVMMetadataTypeKind
;
607 case Type::IntegerTyID
:
608 return LLVMIntegerTypeKind
;
609 case Type::FunctionTyID
:
610 return LLVMFunctionTypeKind
;
611 case Type::StructTyID
:
612 return LLVMStructTypeKind
;
613 case Type::ArrayTyID
:
614 return LLVMArrayTypeKind
;
615 case Type::PointerTyID
:
616 return LLVMPointerTypeKind
;
617 case Type::FixedVectorTyID
:
618 return LLVMVectorTypeKind
;
619 case Type::X86_AMXTyID
:
620 return LLVMX86_AMXTypeKind
;
621 case Type::TokenTyID
:
622 return LLVMTokenTypeKind
;
623 case Type::ScalableVectorTyID
:
624 return LLVMScalableVectorTypeKind
;
625 case Type::TargetExtTyID
:
626 return LLVMTargetExtTypeKind
;
627 case Type::TypedPointerTyID
:
628 llvm_unreachable("Typed pointers are unsupported via the C API");
630 llvm_unreachable("Unhandled TypeID.");
633 LLVMBool
LLVMTypeIsSized(LLVMTypeRef Ty
)
635 return unwrap(Ty
)->isSized();
638 LLVMContextRef
LLVMGetTypeContext(LLVMTypeRef Ty
) {
639 return wrap(&unwrap(Ty
)->getContext());
642 void LLVMDumpType(LLVMTypeRef Ty
) {
643 return unwrap(Ty
)->print(errs(), /*IsForDebug=*/true);
646 char *LLVMPrintTypeToString(LLVMTypeRef Ty
) {
648 raw_string_ostream
os(buf
);
651 unwrap(Ty
)->print(os
);
653 os
<< "Printing <null> Type";
657 return strdup(buf
.c_str());
660 /*--.. Operations on integer types .........................................--*/
662 LLVMTypeRef
LLVMInt1TypeInContext(LLVMContextRef C
) {
663 return (LLVMTypeRef
) Type::getInt1Ty(*unwrap(C
));
665 LLVMTypeRef
LLVMInt8TypeInContext(LLVMContextRef C
) {
666 return (LLVMTypeRef
) Type::getInt8Ty(*unwrap(C
));
668 LLVMTypeRef
LLVMInt16TypeInContext(LLVMContextRef C
) {
669 return (LLVMTypeRef
) Type::getInt16Ty(*unwrap(C
));
671 LLVMTypeRef
LLVMInt32TypeInContext(LLVMContextRef C
) {
672 return (LLVMTypeRef
) Type::getInt32Ty(*unwrap(C
));
674 LLVMTypeRef
LLVMInt64TypeInContext(LLVMContextRef C
) {
675 return (LLVMTypeRef
) Type::getInt64Ty(*unwrap(C
));
677 LLVMTypeRef
LLVMInt128TypeInContext(LLVMContextRef C
) {
678 return (LLVMTypeRef
) Type::getInt128Ty(*unwrap(C
));
680 LLVMTypeRef
LLVMIntTypeInContext(LLVMContextRef C
, unsigned NumBits
) {
681 return wrap(IntegerType::get(*unwrap(C
), NumBits
));
684 LLVMTypeRef
LLVMInt1Type(void) {
685 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
687 LLVMTypeRef
LLVMInt8Type(void) {
688 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
690 LLVMTypeRef
LLVMInt16Type(void) {
691 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
693 LLVMTypeRef
LLVMInt32Type(void) {
694 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
696 LLVMTypeRef
LLVMInt64Type(void) {
697 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
699 LLVMTypeRef
LLVMInt128Type(void) {
700 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
702 LLVMTypeRef
LLVMIntType(unsigned NumBits
) {
703 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits
);
706 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy
) {
707 return unwrap
<IntegerType
>(IntegerTy
)->getBitWidth();
710 /*--.. Operations on real types ............................................--*/
712 LLVMTypeRef
LLVMHalfTypeInContext(LLVMContextRef C
) {
713 return (LLVMTypeRef
) Type::getHalfTy(*unwrap(C
));
715 LLVMTypeRef
LLVMBFloatTypeInContext(LLVMContextRef C
) {
716 return (LLVMTypeRef
) Type::getBFloatTy(*unwrap(C
));
718 LLVMTypeRef
LLVMFloatTypeInContext(LLVMContextRef C
) {
719 return (LLVMTypeRef
) Type::getFloatTy(*unwrap(C
));
721 LLVMTypeRef
LLVMDoubleTypeInContext(LLVMContextRef C
) {
722 return (LLVMTypeRef
) Type::getDoubleTy(*unwrap(C
));
724 LLVMTypeRef
LLVMX86FP80TypeInContext(LLVMContextRef C
) {
725 return (LLVMTypeRef
) Type::getX86_FP80Ty(*unwrap(C
));
727 LLVMTypeRef
LLVMFP128TypeInContext(LLVMContextRef C
) {
728 return (LLVMTypeRef
) Type::getFP128Ty(*unwrap(C
));
730 LLVMTypeRef
LLVMPPCFP128TypeInContext(LLVMContextRef C
) {
731 return (LLVMTypeRef
) Type::getPPC_FP128Ty(*unwrap(C
));
733 LLVMTypeRef
LLVMX86AMXTypeInContext(LLVMContextRef C
) {
734 return (LLVMTypeRef
) Type::getX86_AMXTy(*unwrap(C
));
737 LLVMTypeRef
LLVMHalfType(void) {
738 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
740 LLVMTypeRef
LLVMBFloatType(void) {
741 return LLVMBFloatTypeInContext(LLVMGetGlobalContext());
743 LLVMTypeRef
LLVMFloatType(void) {
744 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
746 LLVMTypeRef
LLVMDoubleType(void) {
747 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
749 LLVMTypeRef
LLVMX86FP80Type(void) {
750 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
752 LLVMTypeRef
LLVMFP128Type(void) {
753 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
755 LLVMTypeRef
LLVMPPCFP128Type(void) {
756 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
758 LLVMTypeRef
LLVMX86AMXType(void) {
759 return LLVMX86AMXTypeInContext(LLVMGetGlobalContext());
762 /*--.. Operations on function types ........................................--*/
764 LLVMTypeRef
LLVMFunctionType(LLVMTypeRef ReturnType
,
765 LLVMTypeRef
*ParamTypes
, unsigned ParamCount
,
767 ArrayRef
<Type
*> Tys(unwrap(ParamTypes
), ParamCount
);
768 return wrap(FunctionType::get(unwrap(ReturnType
), Tys
, IsVarArg
!= 0));
771 LLVMBool
LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy
) {
772 return unwrap
<FunctionType
>(FunctionTy
)->isVarArg();
775 LLVMTypeRef
LLVMGetReturnType(LLVMTypeRef FunctionTy
) {
776 return wrap(unwrap
<FunctionType
>(FunctionTy
)->getReturnType());
779 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy
) {
780 return unwrap
<FunctionType
>(FunctionTy
)->getNumParams();
783 void LLVMGetParamTypes(LLVMTypeRef FunctionTy
, LLVMTypeRef
*Dest
) {
784 FunctionType
*Ty
= unwrap
<FunctionType
>(FunctionTy
);
785 for (Type
*T
: Ty
->params())
789 /*--.. Operations on struct types ..........................................--*/
791 LLVMTypeRef
LLVMStructTypeInContext(LLVMContextRef C
, LLVMTypeRef
*ElementTypes
,
792 unsigned ElementCount
, LLVMBool Packed
) {
793 ArrayRef
<Type
*> Tys(unwrap(ElementTypes
), ElementCount
);
794 return wrap(StructType::get(*unwrap(C
), Tys
, Packed
!= 0));
797 LLVMTypeRef
LLVMStructType(LLVMTypeRef
*ElementTypes
,
798 unsigned ElementCount
, LLVMBool Packed
) {
799 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes
,
800 ElementCount
, Packed
);
803 LLVMTypeRef
LLVMStructCreateNamed(LLVMContextRef C
, const char *Name
)
805 return wrap(StructType::create(*unwrap(C
), Name
));
808 const char *LLVMGetStructName(LLVMTypeRef Ty
)
810 StructType
*Type
= unwrap
<StructType
>(Ty
);
811 if (!Type
->hasName())
813 return Type
->getName().data();
816 void LLVMStructSetBody(LLVMTypeRef StructTy
, LLVMTypeRef
*ElementTypes
,
817 unsigned ElementCount
, LLVMBool Packed
) {
818 ArrayRef
<Type
*> Tys(unwrap(ElementTypes
), ElementCount
);
819 unwrap
<StructType
>(StructTy
)->setBody(Tys
, Packed
!= 0);
822 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy
) {
823 return unwrap
<StructType
>(StructTy
)->getNumElements();
826 void LLVMGetStructElementTypes(LLVMTypeRef StructTy
, LLVMTypeRef
*Dest
) {
827 StructType
*Ty
= unwrap
<StructType
>(StructTy
);
828 for (Type
*T
: Ty
->elements())
832 LLVMTypeRef
LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy
, unsigned i
) {
833 StructType
*Ty
= unwrap
<StructType
>(StructTy
);
834 return wrap(Ty
->getTypeAtIndex(i
));
837 LLVMBool
LLVMIsPackedStruct(LLVMTypeRef StructTy
) {
838 return unwrap
<StructType
>(StructTy
)->isPacked();
841 LLVMBool
LLVMIsOpaqueStruct(LLVMTypeRef StructTy
) {
842 return unwrap
<StructType
>(StructTy
)->isOpaque();
845 LLVMBool
LLVMIsLiteralStruct(LLVMTypeRef StructTy
) {
846 return unwrap
<StructType
>(StructTy
)->isLiteral();
849 LLVMTypeRef
LLVMGetTypeByName(LLVMModuleRef M
, const char *Name
) {
850 return wrap(StructType::getTypeByName(unwrap(M
)->getContext(), Name
));
853 LLVMTypeRef
LLVMGetTypeByName2(LLVMContextRef C
, const char *Name
) {
854 return wrap(StructType::getTypeByName(*unwrap(C
), Name
));
857 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
859 void LLVMGetSubtypes(LLVMTypeRef Tp
, LLVMTypeRef
*Arr
) {
861 for (auto *T
: unwrap(Tp
)->subtypes()) {
867 LLVMTypeRef
LLVMArrayType(LLVMTypeRef ElementType
, unsigned ElementCount
) {
868 return wrap(ArrayType::get(unwrap(ElementType
), ElementCount
));
871 LLVMTypeRef
LLVMArrayType2(LLVMTypeRef ElementType
, uint64_t ElementCount
) {
872 return wrap(ArrayType::get(unwrap(ElementType
), ElementCount
));
875 LLVMTypeRef
LLVMPointerType(LLVMTypeRef ElementType
, unsigned AddressSpace
) {
877 PointerType::get(unwrap(ElementType
)->getContext(), AddressSpace
));
880 LLVMBool
LLVMPointerTypeIsOpaque(LLVMTypeRef Ty
) {
884 LLVMTypeRef
LLVMVectorType(LLVMTypeRef ElementType
, unsigned ElementCount
) {
885 return wrap(FixedVectorType::get(unwrap(ElementType
), ElementCount
));
888 LLVMTypeRef
LLVMScalableVectorType(LLVMTypeRef ElementType
,
889 unsigned ElementCount
) {
890 return wrap(ScalableVectorType::get(unwrap(ElementType
), ElementCount
));
893 LLVMTypeRef
LLVMGetElementType(LLVMTypeRef WrappedTy
) {
894 auto *Ty
= unwrap(WrappedTy
);
895 if (auto *ATy
= dyn_cast
<ArrayType
>(Ty
))
896 return wrap(ATy
->getElementType());
897 return wrap(cast
<VectorType
>(Ty
)->getElementType());
900 unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp
) {
901 return unwrap(Tp
)->getNumContainedTypes();
904 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy
) {
905 return unwrap
<ArrayType
>(ArrayTy
)->getNumElements();
908 uint64_t LLVMGetArrayLength2(LLVMTypeRef ArrayTy
) {
909 return unwrap
<ArrayType
>(ArrayTy
)->getNumElements();
912 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy
) {
913 return unwrap
<PointerType
>(PointerTy
)->getAddressSpace();
916 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy
) {
917 return unwrap
<VectorType
>(VectorTy
)->getElementCount().getKnownMinValue();
920 LLVMValueRef
LLVMGetConstantPtrAuthPointer(LLVMValueRef PtrAuth
) {
921 return wrap(unwrap
<ConstantPtrAuth
>(PtrAuth
)->getPointer());
924 LLVMValueRef
LLVMGetConstantPtrAuthKey(LLVMValueRef PtrAuth
) {
925 return wrap(unwrap
<ConstantPtrAuth
>(PtrAuth
)->getKey());
928 LLVMValueRef
LLVMGetConstantPtrAuthDiscriminator(LLVMValueRef PtrAuth
) {
929 return wrap(unwrap
<ConstantPtrAuth
>(PtrAuth
)->getDiscriminator());
932 LLVMValueRef
LLVMGetConstantPtrAuthAddrDiscriminator(LLVMValueRef PtrAuth
) {
933 return wrap(unwrap
<ConstantPtrAuth
>(PtrAuth
)->getAddrDiscriminator());
936 /*--.. Operations on other types ...........................................--*/
938 LLVMTypeRef
LLVMPointerTypeInContext(LLVMContextRef C
, unsigned AddressSpace
) {
939 return wrap(PointerType::get(*unwrap(C
), AddressSpace
));
942 LLVMTypeRef
LLVMVoidTypeInContext(LLVMContextRef C
) {
943 return wrap(Type::getVoidTy(*unwrap(C
)));
945 LLVMTypeRef
LLVMLabelTypeInContext(LLVMContextRef C
) {
946 return wrap(Type::getLabelTy(*unwrap(C
)));
948 LLVMTypeRef
LLVMTokenTypeInContext(LLVMContextRef C
) {
949 return wrap(Type::getTokenTy(*unwrap(C
)));
951 LLVMTypeRef
LLVMMetadataTypeInContext(LLVMContextRef C
) {
952 return wrap(Type::getMetadataTy(*unwrap(C
)));
955 LLVMTypeRef
LLVMVoidType(void) {
956 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
958 LLVMTypeRef
LLVMLabelType(void) {
959 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
962 LLVMTypeRef
LLVMTargetExtTypeInContext(LLVMContextRef C
, const char *Name
,
963 LLVMTypeRef
*TypeParams
,
964 unsigned TypeParamCount
,
966 unsigned IntParamCount
) {
967 ArrayRef
<Type
*> TypeParamArray(unwrap(TypeParams
), TypeParamCount
);
968 ArrayRef
<unsigned> IntParamArray(IntParams
, IntParamCount
);
970 TargetExtType::get(*unwrap(C
), Name
, TypeParamArray
, IntParamArray
));
973 const char *LLVMGetTargetExtTypeName(LLVMTypeRef TargetExtTy
) {
974 TargetExtType
*Type
= unwrap
<TargetExtType
>(TargetExtTy
);
975 return Type
->getName().data();
978 unsigned LLVMGetTargetExtTypeNumTypeParams(LLVMTypeRef TargetExtTy
) {
979 TargetExtType
*Type
= unwrap
<TargetExtType
>(TargetExtTy
);
980 return Type
->getNumTypeParameters();
983 LLVMTypeRef
LLVMGetTargetExtTypeTypeParam(LLVMTypeRef TargetExtTy
,
985 TargetExtType
*Type
= unwrap
<TargetExtType
>(TargetExtTy
);
986 return wrap(Type
->getTypeParameter(Idx
));
989 unsigned LLVMGetTargetExtTypeNumIntParams(LLVMTypeRef TargetExtTy
) {
990 TargetExtType
*Type
= unwrap
<TargetExtType
>(TargetExtTy
);
991 return Type
->getNumIntParameters();
994 unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy
, unsigned Idx
) {
995 TargetExtType
*Type
= unwrap
<TargetExtType
>(TargetExtTy
);
996 return Type
->getIntParameter(Idx
);
999 /*===-- Operations on values ----------------------------------------------===*/
1001 /*--.. Operations on all values ............................................--*/
1003 LLVMTypeRef
LLVMTypeOf(LLVMValueRef Val
) {
1004 return wrap(unwrap(Val
)->getType());
1007 LLVMValueKind
LLVMGetValueKind(LLVMValueRef Val
) {
1008 switch(unwrap(Val
)->getValueID()) {
1009 #define LLVM_C_API 1
1010 #define HANDLE_VALUE(Name) \
1011 case Value::Name##Val: \
1012 return LLVM##Name##ValueKind;
1013 #include "llvm/IR/Value.def"
1015 return LLVMInstructionValueKind
;
1019 const char *LLVMGetValueName2(LLVMValueRef Val
, size_t *Length
) {
1020 auto *V
= unwrap(Val
);
1021 *Length
= V
->getName().size();
1022 return V
->getName().data();
1025 void LLVMSetValueName2(LLVMValueRef Val
, const char *Name
, size_t NameLen
) {
1026 unwrap(Val
)->setName(StringRef(Name
, NameLen
));
1029 const char *LLVMGetValueName(LLVMValueRef Val
) {
1030 return unwrap(Val
)->getName().data();
1033 void LLVMSetValueName(LLVMValueRef Val
, const char *Name
) {
1034 unwrap(Val
)->setName(Name
);
1037 void LLVMDumpValue(LLVMValueRef Val
) {
1038 unwrap(Val
)->print(errs(), /*IsForDebug=*/true);
1041 char* LLVMPrintValueToString(LLVMValueRef Val
) {
1043 raw_string_ostream
os(buf
);
1046 unwrap(Val
)->print(os
);
1048 os
<< "Printing <null> Value";
1052 return strdup(buf
.c_str());
1055 LLVMContextRef
LLVMGetValueContext(LLVMValueRef Val
) {
1056 return wrap(&unwrap(Val
)->getContext());
1059 char *LLVMPrintDbgRecordToString(LLVMDbgRecordRef Record
) {
1061 raw_string_ostream
os(buf
);
1064 unwrap(Record
)->print(os
);
1066 os
<< "Printing <null> DbgRecord";
1070 return strdup(buf
.c_str());
1073 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal
, LLVMValueRef NewVal
) {
1074 unwrap(OldVal
)->replaceAllUsesWith(unwrap(NewVal
));
1077 int LLVMHasMetadata(LLVMValueRef Inst
) {
1078 return unwrap
<Instruction
>(Inst
)->hasMetadata();
1081 LLVMValueRef
LLVMGetMetadata(LLVMValueRef Inst
, unsigned KindID
) {
1082 auto *I
= unwrap
<Instruction
>(Inst
);
1083 assert(I
&& "Expected instruction");
1084 if (auto *MD
= I
->getMetadata(KindID
))
1085 return wrap(MetadataAsValue::get(I
->getContext(), MD
));
1089 // MetadataAsValue uses a canonical format which strips the actual MDNode for
1090 // MDNode with just a single constant value, storing just a ConstantAsMetadata
1091 // This undoes this canonicalization, reconstructing the MDNode.
1092 static MDNode
*extractMDNode(MetadataAsValue
*MAV
) {
1093 Metadata
*MD
= MAV
->getMetadata();
1094 assert((isa
<MDNode
>(MD
) || isa
<ConstantAsMetadata
>(MD
)) &&
1095 "Expected a metadata node or a canonicalized constant");
1097 if (MDNode
*N
= dyn_cast
<MDNode
>(MD
))
1100 return MDNode::get(MAV
->getContext(), MD
);
1103 void LLVMSetMetadata(LLVMValueRef Inst
, unsigned KindID
, LLVMValueRef Val
) {
1104 MDNode
*N
= Val
? extractMDNode(unwrap
<MetadataAsValue
>(Val
)) : nullptr;
1106 unwrap
<Instruction
>(Inst
)->setMetadata(KindID
, N
);
1109 struct LLVMOpaqueValueMetadataEntry
{
1111 LLVMMetadataRef Metadata
;
1114 using MetadataEntries
= SmallVectorImpl
<std::pair
<unsigned, MDNode
*>>;
1115 static LLVMValueMetadataEntry
*
1116 llvm_getMetadata(size_t *NumEntries
,
1117 llvm::function_ref
<void(MetadataEntries
&)> AccessMD
) {
1118 SmallVector
<std::pair
<unsigned, MDNode
*>, 8> MVEs
;
1121 LLVMOpaqueValueMetadataEntry
*Result
=
1122 static_cast<LLVMOpaqueValueMetadataEntry
*>(
1123 safe_malloc(MVEs
.size() * sizeof(LLVMOpaqueValueMetadataEntry
)));
1124 for (unsigned i
= 0; i
< MVEs
.size(); ++i
) {
1125 const auto &ModuleFlag
= MVEs
[i
];
1126 Result
[i
].Kind
= ModuleFlag
.first
;
1127 Result
[i
].Metadata
= wrap(ModuleFlag
.second
);
1129 *NumEntries
= MVEs
.size();
1133 LLVMValueMetadataEntry
*
1134 LLVMInstructionGetAllMetadataOtherThanDebugLoc(LLVMValueRef Value
,
1135 size_t *NumEntries
) {
1136 return llvm_getMetadata(NumEntries
, [&Value
](MetadataEntries
&Entries
) {
1138 unwrap
<Instruction
>(Value
)->getAllMetadata(Entries
);
1142 /*--.. Conversion functions ................................................--*/
1144 #define LLVM_DEFINE_VALUE_CAST(name) \
1145 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
1146 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
1149 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST
)
1151 LLVMValueRef
LLVMIsAMDNode(LLVMValueRef Val
) {
1152 if (auto *MD
= dyn_cast_or_null
<MetadataAsValue
>(unwrap(Val
)))
1153 if (isa
<MDNode
>(MD
->getMetadata()) ||
1154 isa
<ValueAsMetadata
>(MD
->getMetadata()))
1159 LLVMValueRef
LLVMIsAValueAsMetadata(LLVMValueRef Val
) {
1160 if (auto *MD
= dyn_cast_or_null
<MetadataAsValue
>(unwrap(Val
)))
1161 if (isa
<ValueAsMetadata
>(MD
->getMetadata()))
1166 LLVMValueRef
LLVMIsAMDString(LLVMValueRef Val
) {
1167 if (auto *MD
= dyn_cast_or_null
<MetadataAsValue
>(unwrap(Val
)))
1168 if (isa
<MDString
>(MD
->getMetadata()))
1173 /*--.. Operations on Uses ..................................................--*/
1174 LLVMUseRef
LLVMGetFirstUse(LLVMValueRef Val
) {
1175 Value
*V
= unwrap(Val
);
1176 Value::use_iterator I
= V
->use_begin();
1177 if (I
== V
->use_end())
1182 LLVMUseRef
LLVMGetNextUse(LLVMUseRef U
) {
1183 Use
*Next
= unwrap(U
)->getNext();
1189 LLVMValueRef
LLVMGetUser(LLVMUseRef U
) {
1190 return wrap(unwrap(U
)->getUser());
1193 LLVMValueRef
LLVMGetUsedValue(LLVMUseRef U
) {
1194 return wrap(unwrap(U
)->get());
1197 /*--.. Operations on Users .................................................--*/
1199 static LLVMValueRef
getMDNodeOperandImpl(LLVMContext
&Context
, const MDNode
*N
,
1201 Metadata
*Op
= N
->getOperand(Index
);
1204 if (auto *C
= dyn_cast
<ConstantAsMetadata
>(Op
))
1205 return wrap(C
->getValue());
1206 return wrap(MetadataAsValue::get(Context
, Op
));
1209 LLVMValueRef
LLVMGetOperand(LLVMValueRef Val
, unsigned Index
) {
1210 Value
*V
= unwrap(Val
);
1211 if (auto *MD
= dyn_cast
<MetadataAsValue
>(V
)) {
1212 if (auto *L
= dyn_cast
<ValueAsMetadata
>(MD
->getMetadata())) {
1213 assert(Index
== 0 && "Function-local metadata can only have one operand");
1214 return wrap(L
->getValue());
1216 return getMDNodeOperandImpl(V
->getContext(),
1217 cast
<MDNode
>(MD
->getMetadata()), Index
);
1220 return wrap(cast
<User
>(V
)->getOperand(Index
));
1223 LLVMUseRef
LLVMGetOperandUse(LLVMValueRef Val
, unsigned Index
) {
1224 Value
*V
= unwrap(Val
);
1225 return wrap(&cast
<User
>(V
)->getOperandUse(Index
));
1228 void LLVMSetOperand(LLVMValueRef Val
, unsigned Index
, LLVMValueRef Op
) {
1229 unwrap
<User
>(Val
)->setOperand(Index
, unwrap(Op
));
1232 int LLVMGetNumOperands(LLVMValueRef Val
) {
1233 Value
*V
= unwrap(Val
);
1234 if (isa
<MetadataAsValue
>(V
))
1235 return LLVMGetMDNodeNumOperands(Val
);
1237 return cast
<User
>(V
)->getNumOperands();
1240 /*--.. Operations on constants of any type .................................--*/
1242 LLVMValueRef
LLVMConstNull(LLVMTypeRef Ty
) {
1243 return wrap(Constant::getNullValue(unwrap(Ty
)));
1246 LLVMValueRef
LLVMConstAllOnes(LLVMTypeRef Ty
) {
1247 return wrap(Constant::getAllOnesValue(unwrap(Ty
)));
1250 LLVMValueRef
LLVMGetUndef(LLVMTypeRef Ty
) {
1251 return wrap(UndefValue::get(unwrap(Ty
)));
1254 LLVMValueRef
LLVMGetPoison(LLVMTypeRef Ty
) {
1255 return wrap(PoisonValue::get(unwrap(Ty
)));
1258 LLVMBool
LLVMIsConstant(LLVMValueRef Ty
) {
1259 return isa
<Constant
>(unwrap(Ty
));
1262 LLVMBool
LLVMIsNull(LLVMValueRef Val
) {
1263 if (Constant
*C
= dyn_cast
<Constant
>(unwrap(Val
)))
1264 return C
->isNullValue();
1268 LLVMBool
LLVMIsUndef(LLVMValueRef Val
) {
1269 return isa
<UndefValue
>(unwrap(Val
));
1272 LLVMBool
LLVMIsPoison(LLVMValueRef Val
) {
1273 return isa
<PoisonValue
>(unwrap(Val
));
1276 LLVMValueRef
LLVMConstPointerNull(LLVMTypeRef Ty
) {
1277 return wrap(ConstantPointerNull::get(unwrap
<PointerType
>(Ty
)));
1280 /*--.. Operations on metadata nodes ........................................--*/
1282 LLVMMetadataRef
LLVMMDStringInContext2(LLVMContextRef C
, const char *Str
,
1284 return wrap(MDString::get(*unwrap(C
), StringRef(Str
, SLen
)));
1287 LLVMMetadataRef
LLVMMDNodeInContext2(LLVMContextRef C
, LLVMMetadataRef
*MDs
,
1289 return wrap(MDNode::get(*unwrap(C
), ArrayRef
<Metadata
*>(unwrap(MDs
), Count
)));
1292 LLVMValueRef
LLVMMDStringInContext(LLVMContextRef C
, const char *Str
,
1294 LLVMContext
&Context
= *unwrap(C
);
1295 return wrap(MetadataAsValue::get(
1296 Context
, MDString::get(Context
, StringRef(Str
, SLen
))));
1299 LLVMValueRef
LLVMMDString(const char *Str
, unsigned SLen
) {
1300 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str
, SLen
);
1303 LLVMValueRef
LLVMMDNodeInContext(LLVMContextRef C
, LLVMValueRef
*Vals
,
1305 LLVMContext
&Context
= *unwrap(C
);
1306 SmallVector
<Metadata
*, 8> MDs
;
1307 for (auto *OV
: ArrayRef(Vals
, Count
)) {
1308 Value
*V
= unwrap(OV
);
1312 else if (auto *C
= dyn_cast
<Constant
>(V
))
1313 MD
= ConstantAsMetadata::get(C
);
1314 else if (auto *MDV
= dyn_cast
<MetadataAsValue
>(V
)) {
1315 MD
= MDV
->getMetadata();
1316 assert(!isa
<LocalAsMetadata
>(MD
) && "Unexpected function-local metadata "
1317 "outside of direct argument to call");
1319 // This is function-local metadata. Pretend to make an MDNode.
1320 assert(Count
== 1 &&
1321 "Expected only one operand to function-local metadata");
1322 return wrap(MetadataAsValue::get(Context
, LocalAsMetadata::get(V
)));
1327 return wrap(MetadataAsValue::get(Context
, MDNode::get(Context
, MDs
)));
1330 LLVMValueRef
LLVMMDNode(LLVMValueRef
*Vals
, unsigned Count
) {
1331 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals
, Count
);
1334 LLVMValueRef
LLVMMetadataAsValue(LLVMContextRef C
, LLVMMetadataRef MD
) {
1335 return wrap(MetadataAsValue::get(*unwrap(C
), unwrap(MD
)));
1338 LLVMMetadataRef
LLVMValueAsMetadata(LLVMValueRef Val
) {
1339 auto *V
= unwrap(Val
);
1340 if (auto *C
= dyn_cast
<Constant
>(V
))
1341 return wrap(ConstantAsMetadata::get(C
));
1342 if (auto *MAV
= dyn_cast
<MetadataAsValue
>(V
))
1343 return wrap(MAV
->getMetadata());
1344 return wrap(ValueAsMetadata::get(V
));
1347 const char *LLVMGetMDString(LLVMValueRef V
, unsigned *Length
) {
1348 if (const auto *MD
= dyn_cast
<MetadataAsValue
>(unwrap(V
)))
1349 if (const MDString
*S
= dyn_cast
<MDString
>(MD
->getMetadata())) {
1350 *Length
= S
->getString().size();
1351 return S
->getString().data();
1357 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V
) {
1358 auto *MD
= unwrap
<MetadataAsValue
>(V
);
1359 if (isa
<ValueAsMetadata
>(MD
->getMetadata()))
1361 return cast
<MDNode
>(MD
->getMetadata())->getNumOperands();
1364 LLVMNamedMDNodeRef
LLVMGetFirstNamedMetadata(LLVMModuleRef M
) {
1365 Module
*Mod
= unwrap(M
);
1366 Module::named_metadata_iterator I
= Mod
->named_metadata_begin();
1367 if (I
== Mod
->named_metadata_end())
1372 LLVMNamedMDNodeRef
LLVMGetLastNamedMetadata(LLVMModuleRef M
) {
1373 Module
*Mod
= unwrap(M
);
1374 Module::named_metadata_iterator I
= Mod
->named_metadata_end();
1375 if (I
== Mod
->named_metadata_begin())
1380 LLVMNamedMDNodeRef
LLVMGetNextNamedMetadata(LLVMNamedMDNodeRef NMD
) {
1381 NamedMDNode
*NamedNode
= unwrap(NMD
);
1382 Module::named_metadata_iterator
I(NamedNode
);
1383 if (++I
== NamedNode
->getParent()->named_metadata_end())
1388 LLVMNamedMDNodeRef
LLVMGetPreviousNamedMetadata(LLVMNamedMDNodeRef NMD
) {
1389 NamedMDNode
*NamedNode
= unwrap(NMD
);
1390 Module::named_metadata_iterator
I(NamedNode
);
1391 if (I
== NamedNode
->getParent()->named_metadata_begin())
1396 LLVMNamedMDNodeRef
LLVMGetNamedMetadata(LLVMModuleRef M
,
1397 const char *Name
, size_t NameLen
) {
1398 return wrap(unwrap(M
)->getNamedMetadata(StringRef(Name
, NameLen
)));
1401 LLVMNamedMDNodeRef
LLVMGetOrInsertNamedMetadata(LLVMModuleRef M
,
1402 const char *Name
, size_t NameLen
) {
1403 return wrap(unwrap(M
)->getOrInsertNamedMetadata({Name
, NameLen
}));
1406 const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD
, size_t *NameLen
) {
1407 NamedMDNode
*NamedNode
= unwrap(NMD
);
1408 *NameLen
= NamedNode
->getName().size();
1409 return NamedNode
->getName().data();
1412 void LLVMGetMDNodeOperands(LLVMValueRef V
, LLVMValueRef
*Dest
) {
1413 auto *MD
= unwrap
<MetadataAsValue
>(V
);
1414 if (auto *MDV
= dyn_cast
<ValueAsMetadata
>(MD
->getMetadata())) {
1415 *Dest
= wrap(MDV
->getValue());
1418 const auto *N
= cast
<MDNode
>(MD
->getMetadata());
1419 const unsigned numOperands
= N
->getNumOperands();
1420 LLVMContext
&Context
= unwrap(V
)->getContext();
1421 for (unsigned i
= 0; i
< numOperands
; i
++)
1422 Dest
[i
] = getMDNodeOperandImpl(Context
, N
, i
);
1425 void LLVMReplaceMDNodeOperandWith(LLVMValueRef V
, unsigned Index
,
1426 LLVMMetadataRef Replacement
) {
1427 auto *MD
= cast
<MetadataAsValue
>(unwrap(V
));
1428 auto *N
= cast
<MDNode
>(MD
->getMetadata());
1429 N
->replaceOperandWith(Index
, unwrap
<Metadata
>(Replacement
));
1432 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M
, const char *Name
) {
1433 if (NamedMDNode
*N
= unwrap(M
)->getNamedMetadata(Name
)) {
1434 return N
->getNumOperands();
1439 void LLVMGetNamedMetadataOperands(LLVMModuleRef M
, const char *Name
,
1440 LLVMValueRef
*Dest
) {
1441 NamedMDNode
*N
= unwrap(M
)->getNamedMetadata(Name
);
1444 LLVMContext
&Context
= unwrap(M
)->getContext();
1445 for (unsigned i
=0;i
<N
->getNumOperands();i
++)
1446 Dest
[i
] = wrap(MetadataAsValue::get(Context
, N
->getOperand(i
)));
1449 void LLVMAddNamedMetadataOperand(LLVMModuleRef M
, const char *Name
,
1451 NamedMDNode
*N
= unwrap(M
)->getOrInsertNamedMetadata(Name
);
1456 N
->addOperand(extractMDNode(unwrap
<MetadataAsValue
>(Val
)));
1459 const char *LLVMGetDebugLocDirectory(LLVMValueRef Val
, unsigned *Length
) {
1460 if (!Length
) return nullptr;
1462 if (const auto *I
= dyn_cast
<Instruction
>(unwrap(Val
))) {
1463 if (const auto &DL
= I
->getDebugLoc()) {
1464 S
= DL
->getDirectory();
1466 } else if (const auto *GV
= dyn_cast
<GlobalVariable
>(unwrap(Val
))) {
1467 SmallVector
<DIGlobalVariableExpression
*, 1> GVEs
;
1468 GV
->getDebugInfo(GVEs
);
1470 if (const DIGlobalVariable
*DGV
= GVEs
[0]->getVariable())
1471 S
= DGV
->getDirectory();
1472 } else if (const auto *F
= dyn_cast
<Function
>(unwrap(Val
))) {
1473 if (const DISubprogram
*DSP
= F
->getSubprogram())
1474 S
= DSP
->getDirectory();
1476 assert(0 && "Expected Instruction, GlobalVariable or Function");
1483 const char *LLVMGetDebugLocFilename(LLVMValueRef Val
, unsigned *Length
) {
1484 if (!Length
) return nullptr;
1486 if (const auto *I
= dyn_cast
<Instruction
>(unwrap(Val
))) {
1487 if (const auto &DL
= I
->getDebugLoc()) {
1488 S
= DL
->getFilename();
1490 } else if (const auto *GV
= dyn_cast
<GlobalVariable
>(unwrap(Val
))) {
1491 SmallVector
<DIGlobalVariableExpression
*, 1> GVEs
;
1492 GV
->getDebugInfo(GVEs
);
1494 if (const DIGlobalVariable
*DGV
= GVEs
[0]->getVariable())
1495 S
= DGV
->getFilename();
1496 } else if (const auto *F
= dyn_cast
<Function
>(unwrap(Val
))) {
1497 if (const DISubprogram
*DSP
= F
->getSubprogram())
1498 S
= DSP
->getFilename();
1500 assert(0 && "Expected Instruction, GlobalVariable or Function");
1507 unsigned LLVMGetDebugLocLine(LLVMValueRef Val
) {
1509 if (const auto *I
= dyn_cast
<Instruction
>(unwrap(Val
))) {
1510 if (const auto &DL
= I
->getDebugLoc()) {
1513 } else if (const auto *GV
= dyn_cast
<GlobalVariable
>(unwrap(Val
))) {
1514 SmallVector
<DIGlobalVariableExpression
*, 1> GVEs
;
1515 GV
->getDebugInfo(GVEs
);
1517 if (const DIGlobalVariable
*DGV
= GVEs
[0]->getVariable())
1519 } else if (const auto *F
= dyn_cast
<Function
>(unwrap(Val
))) {
1520 if (const DISubprogram
*DSP
= F
->getSubprogram())
1523 assert(0 && "Expected Instruction, GlobalVariable or Function");
1529 unsigned LLVMGetDebugLocColumn(LLVMValueRef Val
) {
1531 if (const auto *I
= dyn_cast
<Instruction
>(unwrap(Val
)))
1532 if (const auto &DL
= I
->getDebugLoc())
1533 C
= DL
->getColumn();
1537 /*--.. Operations on scalar constants ......................................--*/
1539 LLVMValueRef
LLVMConstInt(LLVMTypeRef IntTy
, unsigned long long N
,
1540 LLVMBool SignExtend
) {
1541 return wrap(ConstantInt::get(unwrap
<IntegerType
>(IntTy
), N
, SignExtend
!= 0));
1544 LLVMValueRef
LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy
,
1546 const uint64_t Words
[]) {
1547 IntegerType
*Ty
= unwrap
<IntegerType
>(IntTy
);
1548 return wrap(ConstantInt::get(
1549 Ty
->getContext(), APInt(Ty
->getBitWidth(), ArrayRef(Words
, NumWords
))));
1552 LLVMValueRef
LLVMConstIntOfString(LLVMTypeRef IntTy
, const char Str
[],
1554 return wrap(ConstantInt::get(unwrap
<IntegerType
>(IntTy
), StringRef(Str
),
1558 LLVMValueRef
LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy
, const char Str
[],
1559 unsigned SLen
, uint8_t Radix
) {
1560 return wrap(ConstantInt::get(unwrap
<IntegerType
>(IntTy
), StringRef(Str
, SLen
),
1564 LLVMValueRef
LLVMConstReal(LLVMTypeRef RealTy
, double N
) {
1565 return wrap(ConstantFP::get(unwrap(RealTy
), N
));
1568 LLVMValueRef
LLVMConstRealOfString(LLVMTypeRef RealTy
, const char *Text
) {
1569 return wrap(ConstantFP::get(unwrap(RealTy
), StringRef(Text
)));
1572 LLVMValueRef
LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy
, const char Str
[],
1574 return wrap(ConstantFP::get(unwrap(RealTy
), StringRef(Str
, SLen
)));
1577 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal
) {
1578 return unwrap
<ConstantInt
>(ConstantVal
)->getZExtValue();
1581 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal
) {
1582 return unwrap
<ConstantInt
>(ConstantVal
)->getSExtValue();
1585 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal
, LLVMBool
*LosesInfo
) {
1586 ConstantFP
*cFP
= unwrap
<ConstantFP
>(ConstantVal
) ;
1587 Type
*Ty
= cFP
->getType();
1589 if (Ty
->isHalfTy() || Ty
->isBFloatTy() || Ty
->isFloatTy() ||
1592 return cFP
->getValueAPF().convertToDouble();
1596 APFloat APF
= cFP
->getValueAPF();
1597 APF
.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven
, &APFLosesInfo
);
1598 *LosesInfo
= APFLosesInfo
;
1599 return APF
.convertToDouble();
1602 /*--.. Operations on composite constants ...................................--*/
1604 LLVMValueRef
LLVMConstStringInContext(LLVMContextRef C
, const char *Str
,
1606 LLVMBool DontNullTerminate
) {
1607 /* Inverted the sense of AddNull because ', 0)' is a
1608 better mnemonic for null termination than ', 1)'. */
1609 return wrap(ConstantDataArray::getString(*unwrap(C
), StringRef(Str
, Length
),
1610 DontNullTerminate
== 0));
1613 LLVMValueRef
LLVMConstStringInContext2(LLVMContextRef C
, const char *Str
,
1615 LLVMBool DontNullTerminate
) {
1616 /* Inverted the sense of AddNull because ', 0)' is a
1617 better mnemonic for null termination than ', 1)'. */
1618 return wrap(ConstantDataArray::getString(*unwrap(C
), StringRef(Str
, Length
),
1619 DontNullTerminate
== 0));
1622 LLVMValueRef
LLVMConstString(const char *Str
, unsigned Length
,
1623 LLVMBool DontNullTerminate
) {
1624 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str
, Length
,
1628 LLVMValueRef
LLVMGetAggregateElement(LLVMValueRef C
, unsigned Idx
) {
1629 return wrap(unwrap
<Constant
>(C
)->getAggregateElement(Idx
));
1632 LLVMValueRef
LLVMGetElementAsConstant(LLVMValueRef C
, unsigned idx
) {
1633 return wrap(unwrap
<ConstantDataSequential
>(C
)->getElementAsConstant(idx
));
1636 LLVMBool
LLVMIsConstantString(LLVMValueRef C
) {
1637 return unwrap
<ConstantDataSequential
>(C
)->isString();
1640 const char *LLVMGetAsString(LLVMValueRef C
, size_t *Length
) {
1641 StringRef Str
= unwrap
<ConstantDataSequential
>(C
)->getAsString();
1642 *Length
= Str
.size();
1646 LLVMValueRef
LLVMConstArray(LLVMTypeRef ElementTy
,
1647 LLVMValueRef
*ConstantVals
, unsigned Length
) {
1648 ArrayRef
<Constant
*> V(unwrap
<Constant
>(ConstantVals
, Length
), Length
);
1649 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy
), Length
), V
));
1652 LLVMValueRef
LLVMConstArray2(LLVMTypeRef ElementTy
, LLVMValueRef
*ConstantVals
,
1654 ArrayRef
<Constant
*> V(unwrap
<Constant
>(ConstantVals
, Length
), Length
);
1655 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy
), Length
), V
));
1658 LLVMValueRef
LLVMConstStructInContext(LLVMContextRef C
,
1659 LLVMValueRef
*ConstantVals
,
1660 unsigned Count
, LLVMBool Packed
) {
1661 Constant
**Elements
= unwrap
<Constant
>(ConstantVals
, Count
);
1662 return wrap(ConstantStruct::getAnon(*unwrap(C
), ArrayRef(Elements
, Count
),
1666 LLVMValueRef
LLVMConstStruct(LLVMValueRef
*ConstantVals
, unsigned Count
,
1668 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals
, Count
,
1672 LLVMValueRef
LLVMConstNamedStruct(LLVMTypeRef StructTy
,
1673 LLVMValueRef
*ConstantVals
,
1675 Constant
**Elements
= unwrap
<Constant
>(ConstantVals
, Count
);
1676 StructType
*Ty
= unwrap
<StructType
>(StructTy
);
1678 return wrap(ConstantStruct::get(Ty
, ArrayRef(Elements
, Count
)));
1681 LLVMValueRef
LLVMConstVector(LLVMValueRef
*ScalarConstantVals
, unsigned Size
) {
1682 return wrap(ConstantVector::get(
1683 ArrayRef(unwrap
<Constant
>(ScalarConstantVals
, Size
), Size
)));
1686 LLVMValueRef
LLVMConstantPtrAuth(LLVMValueRef Ptr
, LLVMValueRef Key
,
1687 LLVMValueRef Disc
, LLVMValueRef AddrDisc
) {
1688 return wrap(ConstantPtrAuth::get(
1689 unwrap
<Constant
>(Ptr
), unwrap
<ConstantInt
>(Key
),
1690 unwrap
<ConstantInt
>(Disc
), unwrap
<Constant
>(AddrDisc
)));
1693 /*-- Opcode mapping */
1695 static LLVMOpcode
map_to_llvmopcode(int opcode
)
1698 default: llvm_unreachable("Unhandled Opcode.");
1699 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1700 #include "llvm/IR/Instruction.def"
1705 static int map_from_llvmopcode(LLVMOpcode code
)
1708 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1709 #include "llvm/IR/Instruction.def"
1712 llvm_unreachable("Unhandled Opcode.");
1715 /*-- GEP wrap flag conversions */
1717 static GEPNoWrapFlags
mapFromLLVMGEPNoWrapFlags(LLVMGEPNoWrapFlags GEPFlags
) {
1718 GEPNoWrapFlags NewGEPFlags
;
1719 if ((GEPFlags
& LLVMGEPFlagInBounds
) != 0)
1720 NewGEPFlags
|= GEPNoWrapFlags::inBounds();
1721 if ((GEPFlags
& LLVMGEPFlagNUSW
) != 0)
1722 NewGEPFlags
|= GEPNoWrapFlags::noUnsignedSignedWrap();
1723 if ((GEPFlags
& LLVMGEPFlagNUW
) != 0)
1724 NewGEPFlags
|= GEPNoWrapFlags::noUnsignedWrap();
1729 static LLVMGEPNoWrapFlags
mapToLLVMGEPNoWrapFlags(GEPNoWrapFlags GEPFlags
) {
1730 LLVMGEPNoWrapFlags NewGEPFlags
= 0;
1731 if (GEPFlags
.isInBounds())
1732 NewGEPFlags
|= LLVMGEPFlagInBounds
;
1733 if (GEPFlags
.hasNoUnsignedSignedWrap())
1734 NewGEPFlags
|= LLVMGEPFlagNUSW
;
1735 if (GEPFlags
.hasNoUnsignedWrap())
1736 NewGEPFlags
|= LLVMGEPFlagNUW
;
1741 /*--.. Constant expressions ................................................--*/
1743 LLVMOpcode
LLVMGetConstOpcode(LLVMValueRef ConstantVal
) {
1744 return map_to_llvmopcode(unwrap
<ConstantExpr
>(ConstantVal
)->getOpcode());
1747 LLVMValueRef
LLVMAlignOf(LLVMTypeRef Ty
) {
1748 return wrap(ConstantExpr::getAlignOf(unwrap(Ty
)));
1751 LLVMValueRef
LLVMSizeOf(LLVMTypeRef Ty
) {
1752 return wrap(ConstantExpr::getSizeOf(unwrap(Ty
)));
1755 LLVMValueRef
LLVMConstNeg(LLVMValueRef ConstantVal
) {
1756 return wrap(ConstantExpr::getNeg(unwrap
<Constant
>(ConstantVal
)));
1759 LLVMValueRef
LLVMConstNSWNeg(LLVMValueRef ConstantVal
) {
1760 return wrap(ConstantExpr::getNSWNeg(unwrap
<Constant
>(ConstantVal
)));
1763 LLVMValueRef
LLVMConstNUWNeg(LLVMValueRef ConstantVal
) {
1764 return wrap(ConstantExpr::getNeg(unwrap
<Constant
>(ConstantVal
)));
1768 LLVMValueRef
LLVMConstNot(LLVMValueRef ConstantVal
) {
1769 return wrap(ConstantExpr::getNot(unwrap
<Constant
>(ConstantVal
)));
1772 LLVMValueRef
LLVMConstAdd(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1773 return wrap(ConstantExpr::getAdd(unwrap
<Constant
>(LHSConstant
),
1774 unwrap
<Constant
>(RHSConstant
)));
1777 LLVMValueRef
LLVMConstNSWAdd(LLVMValueRef LHSConstant
,
1778 LLVMValueRef RHSConstant
) {
1779 return wrap(ConstantExpr::getNSWAdd(unwrap
<Constant
>(LHSConstant
),
1780 unwrap
<Constant
>(RHSConstant
)));
1783 LLVMValueRef
LLVMConstNUWAdd(LLVMValueRef LHSConstant
,
1784 LLVMValueRef RHSConstant
) {
1785 return wrap(ConstantExpr::getNUWAdd(unwrap
<Constant
>(LHSConstant
),
1786 unwrap
<Constant
>(RHSConstant
)));
1789 LLVMValueRef
LLVMConstSub(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1790 return wrap(ConstantExpr::getSub(unwrap
<Constant
>(LHSConstant
),
1791 unwrap
<Constant
>(RHSConstant
)));
1794 LLVMValueRef
LLVMConstNSWSub(LLVMValueRef LHSConstant
,
1795 LLVMValueRef RHSConstant
) {
1796 return wrap(ConstantExpr::getNSWSub(unwrap
<Constant
>(LHSConstant
),
1797 unwrap
<Constant
>(RHSConstant
)));
1800 LLVMValueRef
LLVMConstNUWSub(LLVMValueRef LHSConstant
,
1801 LLVMValueRef RHSConstant
) {
1802 return wrap(ConstantExpr::getNUWSub(unwrap
<Constant
>(LHSConstant
),
1803 unwrap
<Constant
>(RHSConstant
)));
1806 LLVMValueRef
LLVMConstMul(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1807 return wrap(ConstantExpr::getMul(unwrap
<Constant
>(LHSConstant
),
1808 unwrap
<Constant
>(RHSConstant
)));
1811 LLVMValueRef
LLVMConstNSWMul(LLVMValueRef LHSConstant
,
1812 LLVMValueRef RHSConstant
) {
1813 return wrap(ConstantExpr::getNSWMul(unwrap
<Constant
>(LHSConstant
),
1814 unwrap
<Constant
>(RHSConstant
)));
1817 LLVMValueRef
LLVMConstNUWMul(LLVMValueRef LHSConstant
,
1818 LLVMValueRef RHSConstant
) {
1819 return wrap(ConstantExpr::getNUWMul(unwrap
<Constant
>(LHSConstant
),
1820 unwrap
<Constant
>(RHSConstant
)));
1823 LLVMValueRef
LLVMConstXor(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1824 return wrap(ConstantExpr::getXor(unwrap
<Constant
>(LHSConstant
),
1825 unwrap
<Constant
>(RHSConstant
)));
1828 LLVMValueRef
LLVMConstGEP2(LLVMTypeRef Ty
, LLVMValueRef ConstantVal
,
1829 LLVMValueRef
*ConstantIndices
, unsigned NumIndices
) {
1830 ArrayRef
<Constant
*> IdxList(unwrap
<Constant
>(ConstantIndices
, NumIndices
),
1832 Constant
*Val
= unwrap
<Constant
>(ConstantVal
);
1833 return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty
), Val
, IdxList
));
1836 LLVMValueRef
LLVMConstInBoundsGEP2(LLVMTypeRef Ty
, LLVMValueRef ConstantVal
,
1837 LLVMValueRef
*ConstantIndices
,
1838 unsigned NumIndices
) {
1839 ArrayRef
<Constant
*> IdxList(unwrap
<Constant
>(ConstantIndices
, NumIndices
),
1841 Constant
*Val
= unwrap
<Constant
>(ConstantVal
);
1842 return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty
), Val
, IdxList
));
1845 LLVMValueRef
LLVMConstGEPWithNoWrapFlags(LLVMTypeRef Ty
,
1846 LLVMValueRef ConstantVal
,
1847 LLVMValueRef
*ConstantIndices
,
1848 unsigned NumIndices
,
1849 LLVMGEPNoWrapFlags NoWrapFlags
) {
1850 ArrayRef
<Constant
*> IdxList(unwrap
<Constant
>(ConstantIndices
, NumIndices
),
1852 Constant
*Val
= unwrap
<Constant
>(ConstantVal
);
1853 return wrap(ConstantExpr::getGetElementPtr(
1854 unwrap(Ty
), Val
, IdxList
, mapFromLLVMGEPNoWrapFlags(NoWrapFlags
)));
1857 LLVMValueRef
LLVMConstTrunc(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1858 return wrap(ConstantExpr::getTrunc(unwrap
<Constant
>(ConstantVal
),
1862 LLVMValueRef
LLVMConstPtrToInt(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1863 return wrap(ConstantExpr::getPtrToInt(unwrap
<Constant
>(ConstantVal
),
1867 LLVMValueRef
LLVMConstIntToPtr(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1868 return wrap(ConstantExpr::getIntToPtr(unwrap
<Constant
>(ConstantVal
),
1872 LLVMValueRef
LLVMConstBitCast(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1873 return wrap(ConstantExpr::getBitCast(unwrap
<Constant
>(ConstantVal
),
1877 LLVMValueRef
LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal
,
1878 LLVMTypeRef ToType
) {
1879 return wrap(ConstantExpr::getAddrSpaceCast(unwrap
<Constant
>(ConstantVal
),
1883 LLVMValueRef
LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal
,
1884 LLVMTypeRef ToType
) {
1885 return wrap(ConstantExpr::getTruncOrBitCast(unwrap
<Constant
>(ConstantVal
),
1889 LLVMValueRef
LLVMConstPointerCast(LLVMValueRef ConstantVal
,
1890 LLVMTypeRef ToType
) {
1891 return wrap(ConstantExpr::getPointerCast(unwrap
<Constant
>(ConstantVal
),
1895 LLVMValueRef
LLVMConstExtractElement(LLVMValueRef VectorConstant
,
1896 LLVMValueRef IndexConstant
) {
1897 return wrap(ConstantExpr::getExtractElement(unwrap
<Constant
>(VectorConstant
),
1898 unwrap
<Constant
>(IndexConstant
)));
1901 LLVMValueRef
LLVMConstInsertElement(LLVMValueRef VectorConstant
,
1902 LLVMValueRef ElementValueConstant
,
1903 LLVMValueRef IndexConstant
) {
1904 return wrap(ConstantExpr::getInsertElement(unwrap
<Constant
>(VectorConstant
),
1905 unwrap
<Constant
>(ElementValueConstant
),
1906 unwrap
<Constant
>(IndexConstant
)));
1909 LLVMValueRef
LLVMConstShuffleVector(LLVMValueRef VectorAConstant
,
1910 LLVMValueRef VectorBConstant
,
1911 LLVMValueRef MaskConstant
) {
1912 SmallVector
<int, 16> IntMask
;
1913 ShuffleVectorInst::getShuffleMask(unwrap
<Constant
>(MaskConstant
), IntMask
);
1914 return wrap(ConstantExpr::getShuffleVector(unwrap
<Constant
>(VectorAConstant
),
1915 unwrap
<Constant
>(VectorBConstant
),
1919 LLVMValueRef
LLVMConstInlineAsm(LLVMTypeRef Ty
, const char *AsmString
,
1920 const char *Constraints
,
1921 LLVMBool HasSideEffects
,
1922 LLVMBool IsAlignStack
) {
1923 return wrap(InlineAsm::get(dyn_cast
<FunctionType
>(unwrap(Ty
)), AsmString
,
1924 Constraints
, HasSideEffects
, IsAlignStack
));
1927 LLVMValueRef
LLVMBlockAddress(LLVMValueRef F
, LLVMBasicBlockRef BB
) {
1928 return wrap(BlockAddress::get(unwrap
<Function
>(F
), unwrap(BB
)));
1931 LLVMValueRef
LLVMGetBlockAddressFunction(LLVMValueRef BlockAddr
) {
1932 return wrap(unwrap
<BlockAddress
>(BlockAddr
)->getFunction());
1935 LLVMBasicBlockRef
LLVMGetBlockAddressBasicBlock(LLVMValueRef BlockAddr
) {
1936 return wrap(unwrap
<BlockAddress
>(BlockAddr
)->getBasicBlock());
1939 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1941 LLVMModuleRef
LLVMGetGlobalParent(LLVMValueRef Global
) {
1942 return wrap(unwrap
<GlobalValue
>(Global
)->getParent());
1945 LLVMBool
LLVMIsDeclaration(LLVMValueRef Global
) {
1946 return unwrap
<GlobalValue
>(Global
)->isDeclaration();
1949 LLVMLinkage
LLVMGetLinkage(LLVMValueRef Global
) {
1950 switch (unwrap
<GlobalValue
>(Global
)->getLinkage()) {
1951 case GlobalValue::ExternalLinkage
:
1952 return LLVMExternalLinkage
;
1953 case GlobalValue::AvailableExternallyLinkage
:
1954 return LLVMAvailableExternallyLinkage
;
1955 case GlobalValue::LinkOnceAnyLinkage
:
1956 return LLVMLinkOnceAnyLinkage
;
1957 case GlobalValue::LinkOnceODRLinkage
:
1958 return LLVMLinkOnceODRLinkage
;
1959 case GlobalValue::WeakAnyLinkage
:
1960 return LLVMWeakAnyLinkage
;
1961 case GlobalValue::WeakODRLinkage
:
1962 return LLVMWeakODRLinkage
;
1963 case GlobalValue::AppendingLinkage
:
1964 return LLVMAppendingLinkage
;
1965 case GlobalValue::InternalLinkage
:
1966 return LLVMInternalLinkage
;
1967 case GlobalValue::PrivateLinkage
:
1968 return LLVMPrivateLinkage
;
1969 case GlobalValue::ExternalWeakLinkage
:
1970 return LLVMExternalWeakLinkage
;
1971 case GlobalValue::CommonLinkage
:
1972 return LLVMCommonLinkage
;
1975 llvm_unreachable("Invalid GlobalValue linkage!");
1978 void LLVMSetLinkage(LLVMValueRef Global
, LLVMLinkage Linkage
) {
1979 GlobalValue
*GV
= unwrap
<GlobalValue
>(Global
);
1982 case LLVMExternalLinkage
:
1983 GV
->setLinkage(GlobalValue::ExternalLinkage
);
1985 case LLVMAvailableExternallyLinkage
:
1986 GV
->setLinkage(GlobalValue::AvailableExternallyLinkage
);
1988 case LLVMLinkOnceAnyLinkage
:
1989 GV
->setLinkage(GlobalValue::LinkOnceAnyLinkage
);
1991 case LLVMLinkOnceODRLinkage
:
1992 GV
->setLinkage(GlobalValue::LinkOnceODRLinkage
);
1994 case LLVMLinkOnceODRAutoHideLinkage
:
1996 errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1997 "longer supported.");
1999 case LLVMWeakAnyLinkage
:
2000 GV
->setLinkage(GlobalValue::WeakAnyLinkage
);
2002 case LLVMWeakODRLinkage
:
2003 GV
->setLinkage(GlobalValue::WeakODRLinkage
);
2005 case LLVMAppendingLinkage
:
2006 GV
->setLinkage(GlobalValue::AppendingLinkage
);
2008 case LLVMInternalLinkage
:
2009 GV
->setLinkage(GlobalValue::InternalLinkage
);
2011 case LLVMPrivateLinkage
:
2012 GV
->setLinkage(GlobalValue::PrivateLinkage
);
2014 case LLVMLinkerPrivateLinkage
:
2015 GV
->setLinkage(GlobalValue::PrivateLinkage
);
2017 case LLVMLinkerPrivateWeakLinkage
:
2018 GV
->setLinkage(GlobalValue::PrivateLinkage
);
2020 case LLVMDLLImportLinkage
:
2023 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
2025 case LLVMDLLExportLinkage
:
2028 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
2030 case LLVMExternalWeakLinkage
:
2031 GV
->setLinkage(GlobalValue::ExternalWeakLinkage
);
2033 case LLVMGhostLinkage
:
2035 errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
2037 case LLVMCommonLinkage
:
2038 GV
->setLinkage(GlobalValue::CommonLinkage
);
2043 const char *LLVMGetSection(LLVMValueRef Global
) {
2044 // Using .data() is safe because of how GlobalObject::setSection is
2046 return unwrap
<GlobalValue
>(Global
)->getSection().data();
2049 void LLVMSetSection(LLVMValueRef Global
, const char *Section
) {
2050 unwrap
<GlobalObject
>(Global
)->setSection(Section
);
2053 LLVMVisibility
LLVMGetVisibility(LLVMValueRef Global
) {
2054 return static_cast<LLVMVisibility
>(
2055 unwrap
<GlobalValue
>(Global
)->getVisibility());
2058 void LLVMSetVisibility(LLVMValueRef Global
, LLVMVisibility Viz
) {
2059 unwrap
<GlobalValue
>(Global
)
2060 ->setVisibility(static_cast<GlobalValue::VisibilityTypes
>(Viz
));
2063 LLVMDLLStorageClass
LLVMGetDLLStorageClass(LLVMValueRef Global
) {
2064 return static_cast<LLVMDLLStorageClass
>(
2065 unwrap
<GlobalValue
>(Global
)->getDLLStorageClass());
2068 void LLVMSetDLLStorageClass(LLVMValueRef Global
, LLVMDLLStorageClass Class
) {
2069 unwrap
<GlobalValue
>(Global
)->setDLLStorageClass(
2070 static_cast<GlobalValue::DLLStorageClassTypes
>(Class
));
2073 LLVMUnnamedAddr
LLVMGetUnnamedAddress(LLVMValueRef Global
) {
2074 switch (unwrap
<GlobalValue
>(Global
)->getUnnamedAddr()) {
2075 case GlobalVariable::UnnamedAddr::None
:
2076 return LLVMNoUnnamedAddr
;
2077 case GlobalVariable::UnnamedAddr::Local
:
2078 return LLVMLocalUnnamedAddr
;
2079 case GlobalVariable::UnnamedAddr::Global
:
2080 return LLVMGlobalUnnamedAddr
;
2082 llvm_unreachable("Unknown UnnamedAddr kind!");
2085 void LLVMSetUnnamedAddress(LLVMValueRef Global
, LLVMUnnamedAddr UnnamedAddr
) {
2086 GlobalValue
*GV
= unwrap
<GlobalValue
>(Global
);
2088 switch (UnnamedAddr
) {
2089 case LLVMNoUnnamedAddr
:
2090 return GV
->setUnnamedAddr(GlobalVariable::UnnamedAddr::None
);
2091 case LLVMLocalUnnamedAddr
:
2092 return GV
->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local
);
2093 case LLVMGlobalUnnamedAddr
:
2094 return GV
->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global
);
2098 LLVMBool
LLVMHasUnnamedAddr(LLVMValueRef Global
) {
2099 return unwrap
<GlobalValue
>(Global
)->hasGlobalUnnamedAddr();
2102 void LLVMSetUnnamedAddr(LLVMValueRef Global
, LLVMBool HasUnnamedAddr
) {
2103 unwrap
<GlobalValue
>(Global
)->setUnnamedAddr(
2104 HasUnnamedAddr
? GlobalValue::UnnamedAddr::Global
2105 : GlobalValue::UnnamedAddr::None
);
2108 LLVMTypeRef
LLVMGlobalGetValueType(LLVMValueRef Global
) {
2109 return wrap(unwrap
<GlobalValue
>(Global
)->getValueType());
2112 /*--.. Operations on global variables, load and store instructions .........--*/
2114 unsigned LLVMGetAlignment(LLVMValueRef V
) {
2115 Value
*P
= unwrap(V
);
2116 if (GlobalObject
*GV
= dyn_cast
<GlobalObject
>(P
))
2117 return GV
->getAlign() ? GV
->getAlign()->value() : 0;
2118 if (AllocaInst
*AI
= dyn_cast
<AllocaInst
>(P
))
2119 return AI
->getAlign().value();
2120 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
2121 return LI
->getAlign().value();
2122 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(P
))
2123 return SI
->getAlign().value();
2124 if (AtomicRMWInst
*RMWI
= dyn_cast
<AtomicRMWInst
>(P
))
2125 return RMWI
->getAlign().value();
2126 if (AtomicCmpXchgInst
*CXI
= dyn_cast
<AtomicCmpXchgInst
>(P
))
2127 return CXI
->getAlign().value();
2130 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2131 "and AtomicCmpXchgInst have alignment");
2134 void LLVMSetAlignment(LLVMValueRef V
, unsigned Bytes
) {
2135 Value
*P
= unwrap(V
);
2136 if (GlobalObject
*GV
= dyn_cast
<GlobalObject
>(P
))
2137 GV
->setAlignment(MaybeAlign(Bytes
));
2138 else if (AllocaInst
*AI
= dyn_cast
<AllocaInst
>(P
))
2139 AI
->setAlignment(Align(Bytes
));
2140 else if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
2141 LI
->setAlignment(Align(Bytes
));
2142 else if (StoreInst
*SI
= dyn_cast
<StoreInst
>(P
))
2143 SI
->setAlignment(Align(Bytes
));
2144 else if (AtomicRMWInst
*RMWI
= dyn_cast
<AtomicRMWInst
>(P
))
2145 RMWI
->setAlignment(Align(Bytes
));
2146 else if (AtomicCmpXchgInst
*CXI
= dyn_cast
<AtomicCmpXchgInst
>(P
))
2147 CXI
->setAlignment(Align(Bytes
));
2150 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2151 "and AtomicCmpXchgInst have alignment");
2154 LLVMValueMetadataEntry
*LLVMGlobalCopyAllMetadata(LLVMValueRef Value
,
2155 size_t *NumEntries
) {
2156 return llvm_getMetadata(NumEntries
, [&Value
](MetadataEntries
&Entries
) {
2158 if (Instruction
*Instr
= dyn_cast
<Instruction
>(unwrap(Value
))) {
2159 Instr
->getAllMetadata(Entries
);
2161 unwrap
<GlobalObject
>(Value
)->getAllMetadata(Entries
);
2166 unsigned LLVMValueMetadataEntriesGetKind(LLVMValueMetadataEntry
*Entries
,
2168 LLVMOpaqueValueMetadataEntry MVE
=
2169 static_cast<LLVMOpaqueValueMetadataEntry
>(Entries
[Index
]);
2174 LLVMValueMetadataEntriesGetMetadata(LLVMValueMetadataEntry
*Entries
,
2176 LLVMOpaqueValueMetadataEntry MVE
=
2177 static_cast<LLVMOpaqueValueMetadataEntry
>(Entries
[Index
]);
2178 return MVE
.Metadata
;
2181 void LLVMDisposeValueMetadataEntries(LLVMValueMetadataEntry
*Entries
) {
2185 void LLVMGlobalSetMetadata(LLVMValueRef Global
, unsigned Kind
,
2186 LLVMMetadataRef MD
) {
2187 unwrap
<GlobalObject
>(Global
)->setMetadata(Kind
, unwrap
<MDNode
>(MD
));
2190 void LLVMGlobalEraseMetadata(LLVMValueRef Global
, unsigned Kind
) {
2191 unwrap
<GlobalObject
>(Global
)->eraseMetadata(Kind
);
2194 void LLVMGlobalClearMetadata(LLVMValueRef Global
) {
2195 unwrap
<GlobalObject
>(Global
)->clearMetadata();
2198 /*--.. Operations on global variables ......................................--*/
2200 LLVMValueRef
LLVMAddGlobal(LLVMModuleRef M
, LLVMTypeRef Ty
, const char *Name
) {
2201 return wrap(new GlobalVariable(*unwrap(M
), unwrap(Ty
), false,
2202 GlobalValue::ExternalLinkage
, nullptr, Name
));
2205 LLVMValueRef
LLVMAddGlobalInAddressSpace(LLVMModuleRef M
, LLVMTypeRef Ty
,
2207 unsigned AddressSpace
) {
2208 return wrap(new GlobalVariable(*unwrap(M
), unwrap(Ty
), false,
2209 GlobalValue::ExternalLinkage
, nullptr, Name
,
2210 nullptr, GlobalVariable::NotThreadLocal
,
2214 LLVMValueRef
LLVMGetNamedGlobal(LLVMModuleRef M
, const char *Name
) {
2215 return wrap(unwrap(M
)->getNamedGlobal(Name
));
2218 LLVMValueRef
LLVMGetNamedGlobalWithLength(LLVMModuleRef M
, const char *Name
,
2220 return wrap(unwrap(M
)->getNamedGlobal(StringRef(Name
, Length
)));
2223 LLVMValueRef
LLVMGetFirstGlobal(LLVMModuleRef M
) {
2224 Module
*Mod
= unwrap(M
);
2225 Module::global_iterator I
= Mod
->global_begin();
2226 if (I
== Mod
->global_end())
2231 LLVMValueRef
LLVMGetLastGlobal(LLVMModuleRef M
) {
2232 Module
*Mod
= unwrap(M
);
2233 Module::global_iterator I
= Mod
->global_end();
2234 if (I
== Mod
->global_begin())
2239 LLVMValueRef
LLVMGetNextGlobal(LLVMValueRef GlobalVar
) {
2240 GlobalVariable
*GV
= unwrap
<GlobalVariable
>(GlobalVar
);
2241 Module::global_iterator
I(GV
);
2242 if (++I
== GV
->getParent()->global_end())
2247 LLVMValueRef
LLVMGetPreviousGlobal(LLVMValueRef GlobalVar
) {
2248 GlobalVariable
*GV
= unwrap
<GlobalVariable
>(GlobalVar
);
2249 Module::global_iterator
I(GV
);
2250 if (I
== GV
->getParent()->global_begin())
2255 void LLVMDeleteGlobal(LLVMValueRef GlobalVar
) {
2256 unwrap
<GlobalVariable
>(GlobalVar
)->eraseFromParent();
2259 LLVMValueRef
LLVMGetInitializer(LLVMValueRef GlobalVar
) {
2260 GlobalVariable
* GV
= unwrap
<GlobalVariable
>(GlobalVar
);
2261 if ( !GV
->hasInitializer() )
2263 return wrap(GV
->getInitializer());
2266 void LLVMSetInitializer(LLVMValueRef GlobalVar
, LLVMValueRef ConstantVal
) {
2267 unwrap
<GlobalVariable
>(GlobalVar
)->setInitializer(
2268 ConstantVal
? unwrap
<Constant
>(ConstantVal
) : nullptr);
2271 LLVMBool
LLVMIsThreadLocal(LLVMValueRef GlobalVar
) {
2272 return unwrap
<GlobalVariable
>(GlobalVar
)->isThreadLocal();
2275 void LLVMSetThreadLocal(LLVMValueRef GlobalVar
, LLVMBool IsThreadLocal
) {
2276 unwrap
<GlobalVariable
>(GlobalVar
)->setThreadLocal(IsThreadLocal
!= 0);
2279 LLVMBool
LLVMIsGlobalConstant(LLVMValueRef GlobalVar
) {
2280 return unwrap
<GlobalVariable
>(GlobalVar
)->isConstant();
2283 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar
, LLVMBool IsConstant
) {
2284 unwrap
<GlobalVariable
>(GlobalVar
)->setConstant(IsConstant
!= 0);
2287 LLVMThreadLocalMode
LLVMGetThreadLocalMode(LLVMValueRef GlobalVar
) {
2288 switch (unwrap
<GlobalVariable
>(GlobalVar
)->getThreadLocalMode()) {
2289 case GlobalVariable::NotThreadLocal
:
2290 return LLVMNotThreadLocal
;
2291 case GlobalVariable::GeneralDynamicTLSModel
:
2292 return LLVMGeneralDynamicTLSModel
;
2293 case GlobalVariable::LocalDynamicTLSModel
:
2294 return LLVMLocalDynamicTLSModel
;
2295 case GlobalVariable::InitialExecTLSModel
:
2296 return LLVMInitialExecTLSModel
;
2297 case GlobalVariable::LocalExecTLSModel
:
2298 return LLVMLocalExecTLSModel
;
2301 llvm_unreachable("Invalid GlobalVariable thread local mode");
2304 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar
, LLVMThreadLocalMode Mode
) {
2305 GlobalVariable
*GV
= unwrap
<GlobalVariable
>(GlobalVar
);
2308 case LLVMNotThreadLocal
:
2309 GV
->setThreadLocalMode(GlobalVariable::NotThreadLocal
);
2311 case LLVMGeneralDynamicTLSModel
:
2312 GV
->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel
);
2314 case LLVMLocalDynamicTLSModel
:
2315 GV
->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel
);
2317 case LLVMInitialExecTLSModel
:
2318 GV
->setThreadLocalMode(GlobalVariable::InitialExecTLSModel
);
2320 case LLVMLocalExecTLSModel
:
2321 GV
->setThreadLocalMode(GlobalVariable::LocalExecTLSModel
);
2326 LLVMBool
LLVMIsExternallyInitialized(LLVMValueRef GlobalVar
) {
2327 return unwrap
<GlobalVariable
>(GlobalVar
)->isExternallyInitialized();
2330 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar
, LLVMBool IsExtInit
) {
2331 unwrap
<GlobalVariable
>(GlobalVar
)->setExternallyInitialized(IsExtInit
);
2334 /*--.. Operations on aliases ......................................--*/
2336 LLVMValueRef
LLVMAddAlias2(LLVMModuleRef M
, LLVMTypeRef ValueTy
,
2337 unsigned AddrSpace
, LLVMValueRef Aliasee
,
2339 return wrap(GlobalAlias::create(unwrap(ValueTy
), AddrSpace
,
2340 GlobalValue::ExternalLinkage
, Name
,
2341 unwrap
<Constant
>(Aliasee
), unwrap(M
)));
2344 LLVMValueRef
LLVMGetNamedGlobalAlias(LLVMModuleRef M
,
2345 const char *Name
, size_t NameLen
) {
2346 return wrap(unwrap(M
)->getNamedAlias(StringRef(Name
, NameLen
)));
2349 LLVMValueRef
LLVMGetFirstGlobalAlias(LLVMModuleRef M
) {
2350 Module
*Mod
= unwrap(M
);
2351 Module::alias_iterator I
= Mod
->alias_begin();
2352 if (I
== Mod
->alias_end())
2357 LLVMValueRef
LLVMGetLastGlobalAlias(LLVMModuleRef M
) {
2358 Module
*Mod
= unwrap(M
);
2359 Module::alias_iterator I
= Mod
->alias_end();
2360 if (I
== Mod
->alias_begin())
2365 LLVMValueRef
LLVMGetNextGlobalAlias(LLVMValueRef GA
) {
2366 GlobalAlias
*Alias
= unwrap
<GlobalAlias
>(GA
);
2367 Module::alias_iterator
I(Alias
);
2368 if (++I
== Alias
->getParent()->alias_end())
2373 LLVMValueRef
LLVMGetPreviousGlobalAlias(LLVMValueRef GA
) {
2374 GlobalAlias
*Alias
= unwrap
<GlobalAlias
>(GA
);
2375 Module::alias_iterator
I(Alias
);
2376 if (I
== Alias
->getParent()->alias_begin())
2381 LLVMValueRef
LLVMAliasGetAliasee(LLVMValueRef Alias
) {
2382 return wrap(unwrap
<GlobalAlias
>(Alias
)->getAliasee());
2385 void LLVMAliasSetAliasee(LLVMValueRef Alias
, LLVMValueRef Aliasee
) {
2386 unwrap
<GlobalAlias
>(Alias
)->setAliasee(unwrap
<Constant
>(Aliasee
));
2389 /*--.. Operations on functions .............................................--*/
2391 LLVMValueRef
LLVMAddFunction(LLVMModuleRef M
, const char *Name
,
2392 LLVMTypeRef FunctionTy
) {
2393 return wrap(Function::Create(unwrap
<FunctionType
>(FunctionTy
),
2394 GlobalValue::ExternalLinkage
, Name
, unwrap(M
)));
2397 LLVMValueRef
LLVMGetNamedFunction(LLVMModuleRef M
, const char *Name
) {
2398 return wrap(unwrap(M
)->getFunction(Name
));
2401 LLVMValueRef
LLVMGetNamedFunctionWithLength(LLVMModuleRef M
, const char *Name
,
2403 return wrap(unwrap(M
)->getFunction(StringRef(Name
, Length
)));
2406 LLVMValueRef
LLVMGetFirstFunction(LLVMModuleRef M
) {
2407 Module
*Mod
= unwrap(M
);
2408 Module::iterator I
= Mod
->begin();
2409 if (I
== Mod
->end())
2414 LLVMValueRef
LLVMGetLastFunction(LLVMModuleRef M
) {
2415 Module
*Mod
= unwrap(M
);
2416 Module::iterator I
= Mod
->end();
2417 if (I
== Mod
->begin())
2422 LLVMValueRef
LLVMGetNextFunction(LLVMValueRef Fn
) {
2423 Function
*Func
= unwrap
<Function
>(Fn
);
2424 Module::iterator
I(Func
);
2425 if (++I
== Func
->getParent()->end())
2430 LLVMValueRef
LLVMGetPreviousFunction(LLVMValueRef Fn
) {
2431 Function
*Func
= unwrap
<Function
>(Fn
);
2432 Module::iterator
I(Func
);
2433 if (I
== Func
->getParent()->begin())
2438 void LLVMDeleteFunction(LLVMValueRef Fn
) {
2439 unwrap
<Function
>(Fn
)->eraseFromParent();
2442 LLVMBool
LLVMHasPersonalityFn(LLVMValueRef Fn
) {
2443 return unwrap
<Function
>(Fn
)->hasPersonalityFn();
2446 LLVMValueRef
LLVMGetPersonalityFn(LLVMValueRef Fn
) {
2447 return wrap(unwrap
<Function
>(Fn
)->getPersonalityFn());
2450 void LLVMSetPersonalityFn(LLVMValueRef Fn
, LLVMValueRef PersonalityFn
) {
2451 unwrap
<Function
>(Fn
)->setPersonalityFn(
2452 PersonalityFn
? unwrap
<Constant
>(PersonalityFn
) : nullptr);
2455 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn
) {
2456 if (Function
*F
= dyn_cast
<Function
>(unwrap(Fn
)))
2457 return F
->getIntrinsicID();
2461 static Intrinsic::ID
llvm_map_to_intrinsic_id(unsigned ID
) {
2462 assert(ID
< llvm::Intrinsic::num_intrinsics
&& "Intrinsic ID out of range");
2463 return llvm::Intrinsic::ID(ID
);
2466 LLVMValueRef
LLVMGetIntrinsicDeclaration(LLVMModuleRef Mod
,
2468 LLVMTypeRef
*ParamTypes
,
2469 size_t ParamCount
) {
2470 ArrayRef
<Type
*> Tys(unwrap(ParamTypes
), ParamCount
);
2471 auto IID
= llvm_map_to_intrinsic_id(ID
);
2472 return wrap(llvm::Intrinsic::getOrInsertDeclaration(unwrap(Mod
), IID
, Tys
));
2475 const char *LLVMIntrinsicGetName(unsigned ID
, size_t *NameLength
) {
2476 auto IID
= llvm_map_to_intrinsic_id(ID
);
2477 auto Str
= llvm::Intrinsic::getName(IID
);
2478 *NameLength
= Str
.size();
2482 LLVMTypeRef
LLVMIntrinsicGetType(LLVMContextRef Ctx
, unsigned ID
,
2483 LLVMTypeRef
*ParamTypes
, size_t ParamCount
) {
2484 auto IID
= llvm_map_to_intrinsic_id(ID
);
2485 ArrayRef
<Type
*> Tys(unwrap(ParamTypes
), ParamCount
);
2486 return wrap(llvm::Intrinsic::getType(*unwrap(Ctx
), IID
, Tys
));
2489 char *LLVMIntrinsicCopyOverloadedName(unsigned ID
, LLVMTypeRef
*ParamTypes
,
2490 size_t ParamCount
, size_t *NameLength
) {
2491 auto IID
= llvm_map_to_intrinsic_id(ID
);
2492 ArrayRef
<Type
*> Tys(unwrap(ParamTypes
), ParamCount
);
2493 auto Str
= llvm::Intrinsic::getNameNoUnnamedTypes(IID
, Tys
);
2494 *NameLength
= Str
.length();
2495 return strdup(Str
.c_str());
2498 char *LLVMIntrinsicCopyOverloadedName2(LLVMModuleRef Mod
, unsigned ID
,
2499 LLVMTypeRef
*ParamTypes
,
2500 size_t ParamCount
, size_t *NameLength
) {
2501 auto IID
= llvm_map_to_intrinsic_id(ID
);
2502 ArrayRef
<Type
*> Tys(unwrap(ParamTypes
), ParamCount
);
2503 auto Str
= llvm::Intrinsic::getName(IID
, Tys
, unwrap(Mod
));
2504 *NameLength
= Str
.length();
2505 return strdup(Str
.c_str());
2508 unsigned LLVMLookupIntrinsicID(const char *Name
, size_t NameLen
) {
2509 return Intrinsic::lookupIntrinsicID({Name
, NameLen
});
2512 LLVMBool
LLVMIntrinsicIsOverloaded(unsigned ID
) {
2513 auto IID
= llvm_map_to_intrinsic_id(ID
);
2514 return llvm::Intrinsic::isOverloaded(IID
);
2517 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn
) {
2518 return unwrap
<Function
>(Fn
)->getCallingConv();
2521 void LLVMSetFunctionCallConv(LLVMValueRef Fn
, unsigned CC
) {
2522 return unwrap
<Function
>(Fn
)->setCallingConv(
2523 static_cast<CallingConv::ID
>(CC
));
2526 const char *LLVMGetGC(LLVMValueRef Fn
) {
2527 Function
*F
= unwrap
<Function
>(Fn
);
2528 return F
->hasGC()? F
->getGC().c_str() : nullptr;
2531 void LLVMSetGC(LLVMValueRef Fn
, const char *GC
) {
2532 Function
*F
= unwrap
<Function
>(Fn
);
2539 LLVMValueRef
LLVMGetPrefixData(LLVMValueRef Fn
) {
2540 Function
*F
= unwrap
<Function
>(Fn
);
2541 return wrap(F
->getPrefixData());
2544 LLVMBool
LLVMHasPrefixData(LLVMValueRef Fn
) {
2545 Function
*F
= unwrap
<Function
>(Fn
);
2546 return F
->hasPrefixData();
2549 void LLVMSetPrefixData(LLVMValueRef Fn
, LLVMValueRef prefixData
) {
2550 Function
*F
= unwrap
<Function
>(Fn
);
2551 Constant
*prefix
= unwrap
<Constant
>(prefixData
);
2552 F
->setPrefixData(prefix
);
2555 LLVMValueRef
LLVMGetPrologueData(LLVMValueRef Fn
) {
2556 Function
*F
= unwrap
<Function
>(Fn
);
2557 return wrap(F
->getPrologueData());
2560 LLVMBool
LLVMHasPrologueData(LLVMValueRef Fn
) {
2561 Function
*F
= unwrap
<Function
>(Fn
);
2562 return F
->hasPrologueData();
2565 void LLVMSetPrologueData(LLVMValueRef Fn
, LLVMValueRef prologueData
) {
2566 Function
*F
= unwrap
<Function
>(Fn
);
2567 Constant
*prologue
= unwrap
<Constant
>(prologueData
);
2568 F
->setPrologueData(prologue
);
2571 void LLVMAddAttributeAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
,
2572 LLVMAttributeRef A
) {
2573 unwrap
<Function
>(F
)->addAttributeAtIndex(Idx
, unwrap(A
));
2576 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
) {
2577 auto AS
= unwrap
<Function
>(F
)->getAttributes().getAttributes(Idx
);
2578 return AS
.getNumAttributes();
2581 void LLVMGetAttributesAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
,
2582 LLVMAttributeRef
*Attrs
) {
2583 auto AS
= unwrap
<Function
>(F
)->getAttributes().getAttributes(Idx
);
2588 LLVMAttributeRef
LLVMGetEnumAttributeAtIndex(LLVMValueRef F
,
2589 LLVMAttributeIndex Idx
,
2591 return wrap(unwrap
<Function
>(F
)->getAttributeAtIndex(
2592 Idx
, (Attribute::AttrKind
)KindID
));
2595 LLVMAttributeRef
LLVMGetStringAttributeAtIndex(LLVMValueRef F
,
2596 LLVMAttributeIndex Idx
,
2597 const char *K
, unsigned KLen
) {
2599 unwrap
<Function
>(F
)->getAttributeAtIndex(Idx
, StringRef(K
, KLen
)));
2602 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
,
2604 unwrap
<Function
>(F
)->removeAttributeAtIndex(Idx
, (Attribute::AttrKind
)KindID
);
2607 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
,
2608 const char *K
, unsigned KLen
) {
2609 unwrap
<Function
>(F
)->removeAttributeAtIndex(Idx
, StringRef(K
, KLen
));
2612 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn
, const char *A
,
2614 Function
*Func
= unwrap
<Function
>(Fn
);
2615 Attribute Attr
= Attribute::get(Func
->getContext(), A
, V
);
2616 Func
->addFnAttr(Attr
);
2619 /*--.. Operations on parameters ............................................--*/
2621 unsigned LLVMCountParams(LLVMValueRef FnRef
) {
2622 // This function is strictly redundant to
2623 // LLVMCountParamTypes(LLVMGlobalGetValueType(FnRef))
2624 return unwrap
<Function
>(FnRef
)->arg_size();
2627 void LLVMGetParams(LLVMValueRef FnRef
, LLVMValueRef
*ParamRefs
) {
2628 Function
*Fn
= unwrap
<Function
>(FnRef
);
2629 for (Argument
&A
: Fn
->args())
2630 *ParamRefs
++ = wrap(&A
);
2633 LLVMValueRef
LLVMGetParam(LLVMValueRef FnRef
, unsigned index
) {
2634 Function
*Fn
= unwrap
<Function
>(FnRef
);
2635 return wrap(&Fn
->arg_begin()[index
]);
2638 LLVMValueRef
LLVMGetParamParent(LLVMValueRef V
) {
2639 return wrap(unwrap
<Argument
>(V
)->getParent());
2642 LLVMValueRef
LLVMGetFirstParam(LLVMValueRef Fn
) {
2643 Function
*Func
= unwrap
<Function
>(Fn
);
2644 Function::arg_iterator I
= Func
->arg_begin();
2645 if (I
== Func
->arg_end())
2650 LLVMValueRef
LLVMGetLastParam(LLVMValueRef Fn
) {
2651 Function
*Func
= unwrap
<Function
>(Fn
);
2652 Function::arg_iterator I
= Func
->arg_end();
2653 if (I
== Func
->arg_begin())
2658 LLVMValueRef
LLVMGetNextParam(LLVMValueRef Arg
) {
2659 Argument
*A
= unwrap
<Argument
>(Arg
);
2660 Function
*Fn
= A
->getParent();
2661 if (A
->getArgNo() + 1 >= Fn
->arg_size())
2663 return wrap(&Fn
->arg_begin()[A
->getArgNo() + 1]);
2666 LLVMValueRef
LLVMGetPreviousParam(LLVMValueRef Arg
) {
2667 Argument
*A
= unwrap
<Argument
>(Arg
);
2668 if (A
->getArgNo() == 0)
2670 return wrap(&A
->getParent()->arg_begin()[A
->getArgNo() - 1]);
2673 void LLVMSetParamAlignment(LLVMValueRef Arg
, unsigned align
) {
2674 Argument
*A
= unwrap
<Argument
>(Arg
);
2675 A
->addAttr(Attribute::getWithAlignment(A
->getContext(), Align(align
)));
2678 /*--.. Operations on ifuncs ................................................--*/
2680 LLVMValueRef
LLVMAddGlobalIFunc(LLVMModuleRef M
,
2681 const char *Name
, size_t NameLen
,
2682 LLVMTypeRef Ty
, unsigned AddrSpace
,
2683 LLVMValueRef Resolver
) {
2684 return wrap(GlobalIFunc::create(unwrap(Ty
), AddrSpace
,
2685 GlobalValue::ExternalLinkage
,
2686 StringRef(Name
, NameLen
),
2687 unwrap
<Constant
>(Resolver
), unwrap(M
)));
2690 LLVMValueRef
LLVMGetNamedGlobalIFunc(LLVMModuleRef M
,
2691 const char *Name
, size_t NameLen
) {
2692 return wrap(unwrap(M
)->getNamedIFunc(StringRef(Name
, NameLen
)));
2695 LLVMValueRef
LLVMGetFirstGlobalIFunc(LLVMModuleRef M
) {
2696 Module
*Mod
= unwrap(M
);
2697 Module::ifunc_iterator I
= Mod
->ifunc_begin();
2698 if (I
== Mod
->ifunc_end())
2703 LLVMValueRef
LLVMGetLastGlobalIFunc(LLVMModuleRef M
) {
2704 Module
*Mod
= unwrap(M
);
2705 Module::ifunc_iterator I
= Mod
->ifunc_end();
2706 if (I
== Mod
->ifunc_begin())
2711 LLVMValueRef
LLVMGetNextGlobalIFunc(LLVMValueRef IFunc
) {
2712 GlobalIFunc
*GIF
= unwrap
<GlobalIFunc
>(IFunc
);
2713 Module::ifunc_iterator
I(GIF
);
2714 if (++I
== GIF
->getParent()->ifunc_end())
2719 LLVMValueRef
LLVMGetPreviousGlobalIFunc(LLVMValueRef IFunc
) {
2720 GlobalIFunc
*GIF
= unwrap
<GlobalIFunc
>(IFunc
);
2721 Module::ifunc_iterator
I(GIF
);
2722 if (I
== GIF
->getParent()->ifunc_begin())
2727 LLVMValueRef
LLVMGetGlobalIFuncResolver(LLVMValueRef IFunc
) {
2728 return wrap(unwrap
<GlobalIFunc
>(IFunc
)->getResolver());
2731 void LLVMSetGlobalIFuncResolver(LLVMValueRef IFunc
, LLVMValueRef Resolver
) {
2732 unwrap
<GlobalIFunc
>(IFunc
)->setResolver(unwrap
<Constant
>(Resolver
));
2735 void LLVMEraseGlobalIFunc(LLVMValueRef IFunc
) {
2736 unwrap
<GlobalIFunc
>(IFunc
)->eraseFromParent();
2739 void LLVMRemoveGlobalIFunc(LLVMValueRef IFunc
) {
2740 unwrap
<GlobalIFunc
>(IFunc
)->removeFromParent();
2743 /*--.. Operations on operand bundles........................................--*/
2745 LLVMOperandBundleRef
LLVMCreateOperandBundle(const char *Tag
, size_t TagLen
,
2748 return wrap(new OperandBundleDef(std::string(Tag
, TagLen
),
2749 ArrayRef(unwrap(Args
), NumArgs
)));
2752 void LLVMDisposeOperandBundle(LLVMOperandBundleRef Bundle
) {
2753 delete unwrap(Bundle
);
2756 const char *LLVMGetOperandBundleTag(LLVMOperandBundleRef Bundle
, size_t *Len
) {
2757 StringRef Str
= unwrap(Bundle
)->getTag();
2762 unsigned LLVMGetNumOperandBundleArgs(LLVMOperandBundleRef Bundle
) {
2763 return unwrap(Bundle
)->inputs().size();
2766 LLVMValueRef
LLVMGetOperandBundleArgAtIndex(LLVMOperandBundleRef Bundle
,
2768 return wrap(unwrap(Bundle
)->inputs()[Index
]);
2771 /*--.. Operations on basic blocks ..........................................--*/
2773 LLVMValueRef
LLVMBasicBlockAsValue(LLVMBasicBlockRef BB
) {
2774 return wrap(static_cast<Value
*>(unwrap(BB
)));
2777 LLVMBool
LLVMValueIsBasicBlock(LLVMValueRef Val
) {
2778 return isa
<BasicBlock
>(unwrap(Val
));
2781 LLVMBasicBlockRef
LLVMValueAsBasicBlock(LLVMValueRef Val
) {
2782 return wrap(unwrap
<BasicBlock
>(Val
));
2785 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB
) {
2786 return unwrap(BB
)->getName().data();
2789 LLVMValueRef
LLVMGetBasicBlockParent(LLVMBasicBlockRef BB
) {
2790 return wrap(unwrap(BB
)->getParent());
2793 LLVMValueRef
LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB
) {
2794 return wrap(unwrap(BB
)->getTerminator());
2797 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef
) {
2798 return unwrap
<Function
>(FnRef
)->size();
2801 void LLVMGetBasicBlocks(LLVMValueRef FnRef
, LLVMBasicBlockRef
*BasicBlocksRefs
){
2802 Function
*Fn
= unwrap
<Function
>(FnRef
);
2803 for (BasicBlock
&BB
: *Fn
)
2804 *BasicBlocksRefs
++ = wrap(&BB
);
2807 LLVMBasicBlockRef
LLVMGetEntryBasicBlock(LLVMValueRef Fn
) {
2808 return wrap(&unwrap
<Function
>(Fn
)->getEntryBlock());
2811 LLVMBasicBlockRef
LLVMGetFirstBasicBlock(LLVMValueRef Fn
) {
2812 Function
*Func
= unwrap
<Function
>(Fn
);
2813 Function::iterator I
= Func
->begin();
2814 if (I
== Func
->end())
2819 LLVMBasicBlockRef
LLVMGetLastBasicBlock(LLVMValueRef Fn
) {
2820 Function
*Func
= unwrap
<Function
>(Fn
);
2821 Function::iterator I
= Func
->end();
2822 if (I
== Func
->begin())
2827 LLVMBasicBlockRef
LLVMGetNextBasicBlock(LLVMBasicBlockRef BB
) {
2828 BasicBlock
*Block
= unwrap(BB
);
2829 Function::iterator
I(Block
);
2830 if (++I
== Block
->getParent()->end())
2835 LLVMBasicBlockRef
LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB
) {
2836 BasicBlock
*Block
= unwrap(BB
);
2837 Function::iterator
I(Block
);
2838 if (I
== Block
->getParent()->begin())
2843 LLVMBasicBlockRef
LLVMCreateBasicBlockInContext(LLVMContextRef C
,
2845 return wrap(llvm::BasicBlock::Create(*unwrap(C
), Name
));
2848 void LLVMInsertExistingBasicBlockAfterInsertBlock(LLVMBuilderRef Builder
,
2849 LLVMBasicBlockRef BB
) {
2850 BasicBlock
*ToInsert
= unwrap(BB
);
2851 BasicBlock
*CurBB
= unwrap(Builder
)->GetInsertBlock();
2852 assert(CurBB
&& "current insertion point is invalid!");
2853 CurBB
->getParent()->insert(std::next(CurBB
->getIterator()), ToInsert
);
2856 void LLVMAppendExistingBasicBlock(LLVMValueRef Fn
,
2857 LLVMBasicBlockRef BB
) {
2858 unwrap
<Function
>(Fn
)->insert(unwrap
<Function
>(Fn
)->end(), unwrap(BB
));
2861 LLVMBasicBlockRef
LLVMAppendBasicBlockInContext(LLVMContextRef C
,
2864 return wrap(BasicBlock::Create(*unwrap(C
), Name
, unwrap
<Function
>(FnRef
)));
2867 LLVMBasicBlockRef
LLVMAppendBasicBlock(LLVMValueRef FnRef
, const char *Name
) {
2868 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef
, Name
);
2871 LLVMBasicBlockRef
LLVMInsertBasicBlockInContext(LLVMContextRef C
,
2872 LLVMBasicBlockRef BBRef
,
2874 BasicBlock
*BB
= unwrap(BBRef
);
2875 return wrap(BasicBlock::Create(*unwrap(C
), Name
, BB
->getParent(), BB
));
2878 LLVMBasicBlockRef
LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef
,
2880 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef
, Name
);
2883 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef
) {
2884 unwrap(BBRef
)->eraseFromParent();
2887 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef
) {
2888 unwrap(BBRef
)->removeFromParent();
2891 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB
, LLVMBasicBlockRef MovePos
) {
2892 unwrap(BB
)->moveBefore(unwrap(MovePos
));
2895 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB
, LLVMBasicBlockRef MovePos
) {
2896 unwrap(BB
)->moveAfter(unwrap(MovePos
));
2899 /*--.. Operations on instructions ..........................................--*/
2901 LLVMBasicBlockRef
LLVMGetInstructionParent(LLVMValueRef Inst
) {
2902 return wrap(unwrap
<Instruction
>(Inst
)->getParent());
2905 LLVMValueRef
LLVMGetFirstInstruction(LLVMBasicBlockRef BB
) {
2906 BasicBlock
*Block
= unwrap(BB
);
2907 BasicBlock::iterator I
= Block
->begin();
2908 if (I
== Block
->end())
2913 LLVMValueRef
LLVMGetLastInstruction(LLVMBasicBlockRef BB
) {
2914 BasicBlock
*Block
= unwrap(BB
);
2915 BasicBlock::iterator I
= Block
->end();
2916 if (I
== Block
->begin())
2921 LLVMValueRef
LLVMGetNextInstruction(LLVMValueRef Inst
) {
2922 Instruction
*Instr
= unwrap
<Instruction
>(Inst
);
2923 BasicBlock::iterator
I(Instr
);
2924 if (++I
== Instr
->getParent()->end())
2929 LLVMValueRef
LLVMGetPreviousInstruction(LLVMValueRef Inst
) {
2930 Instruction
*Instr
= unwrap
<Instruction
>(Inst
);
2931 BasicBlock::iterator
I(Instr
);
2932 if (I
== Instr
->getParent()->begin())
2937 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst
) {
2938 unwrap
<Instruction
>(Inst
)->removeFromParent();
2941 void LLVMInstructionEraseFromParent(LLVMValueRef Inst
) {
2942 unwrap
<Instruction
>(Inst
)->eraseFromParent();
2945 void LLVMDeleteInstruction(LLVMValueRef Inst
) {
2946 unwrap
<Instruction
>(Inst
)->deleteValue();
2949 LLVMIntPredicate
LLVMGetICmpPredicate(LLVMValueRef Inst
) {
2950 if (ICmpInst
*I
= dyn_cast
<ICmpInst
>(unwrap(Inst
)))
2951 return (LLVMIntPredicate
)I
->getPredicate();
2952 return (LLVMIntPredicate
)0;
2955 LLVMRealPredicate
LLVMGetFCmpPredicate(LLVMValueRef Inst
) {
2956 if (FCmpInst
*I
= dyn_cast
<FCmpInst
>(unwrap(Inst
)))
2957 return (LLVMRealPredicate
)I
->getPredicate();
2958 return (LLVMRealPredicate
)0;
2961 LLVMOpcode
LLVMGetInstructionOpcode(LLVMValueRef Inst
) {
2962 if (Instruction
*C
= dyn_cast
<Instruction
>(unwrap(Inst
)))
2963 return map_to_llvmopcode(C
->getOpcode());
2964 return (LLVMOpcode
)0;
2967 LLVMValueRef
LLVMInstructionClone(LLVMValueRef Inst
) {
2968 if (Instruction
*C
= dyn_cast
<Instruction
>(unwrap(Inst
)))
2969 return wrap(C
->clone());
2973 LLVMValueRef
LLVMIsATerminatorInst(LLVMValueRef Inst
) {
2974 Instruction
*I
= dyn_cast
<Instruction
>(unwrap(Inst
));
2975 return (I
&& I
->isTerminator()) ? wrap(I
) : nullptr;
2978 LLVMDbgRecordRef
LLVMGetFirstDbgRecord(LLVMValueRef Inst
) {
2979 Instruction
*Instr
= unwrap
<Instruction
>(Inst
);
2980 auto I
= Instr
->DebugMarker
->StoredDbgRecords
.begin();
2981 if (I
== Instr
->DebugMarker
->StoredDbgRecords
.end())
2986 LLVMDbgRecordRef
LLVMGetLastDbgRecord(LLVMValueRef Inst
) {
2987 Instruction
*Instr
= unwrap
<Instruction
>(Inst
);
2988 auto I
= Instr
->DebugMarker
->StoredDbgRecords
.rbegin();
2989 if (I
== Instr
->DebugMarker
->StoredDbgRecords
.rend())
2994 LLVMDbgRecordRef
LLVMGetNextDbgRecord(LLVMDbgRecordRef Rec
) {
2995 DbgRecord
*Record
= unwrap
<DbgRecord
>(Rec
);
2996 simple_ilist
<DbgRecord
>::iterator
I(Record
);
2997 if (++I
== Record
->getInstruction()->DebugMarker
->StoredDbgRecords
.end())
3002 LLVMDbgRecordRef
LLVMGetPreviousDbgRecord(LLVMDbgRecordRef Rec
) {
3003 DbgRecord
*Record
= unwrap
<DbgRecord
>(Rec
);
3004 simple_ilist
<DbgRecord
>::iterator
I(Record
);
3005 if (I
== Record
->getInstruction()->DebugMarker
->StoredDbgRecords
.begin())
3010 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr
) {
3011 if (FuncletPadInst
*FPI
= dyn_cast
<FuncletPadInst
>(unwrap(Instr
))) {
3012 return FPI
->arg_size();
3014 return unwrap
<CallBase
>(Instr
)->arg_size();
3017 /*--.. Call and invoke instructions ........................................--*/
3019 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr
) {
3020 return unwrap
<CallBase
>(Instr
)->getCallingConv();
3023 void LLVMSetInstructionCallConv(LLVMValueRef Instr
, unsigned CC
) {
3024 return unwrap
<CallBase
>(Instr
)->setCallingConv(
3025 static_cast<CallingConv::ID
>(CC
));
3028 void LLVMSetInstrParamAlignment(LLVMValueRef Instr
, LLVMAttributeIndex Idx
,
3030 auto *Call
= unwrap
<CallBase
>(Instr
);
3031 Attribute AlignAttr
=
3032 Attribute::getWithAlignment(Call
->getContext(), Align(align
));
3033 Call
->addAttributeAtIndex(Idx
, AlignAttr
);
3036 void LLVMAddCallSiteAttribute(LLVMValueRef C
, LLVMAttributeIndex Idx
,
3037 LLVMAttributeRef A
) {
3038 unwrap
<CallBase
>(C
)->addAttributeAtIndex(Idx
, unwrap(A
));
3041 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C
,
3042 LLVMAttributeIndex Idx
) {
3043 auto *Call
= unwrap
<CallBase
>(C
);
3044 auto AS
= Call
->getAttributes().getAttributes(Idx
);
3045 return AS
.getNumAttributes();
3048 void LLVMGetCallSiteAttributes(LLVMValueRef C
, LLVMAttributeIndex Idx
,
3049 LLVMAttributeRef
*Attrs
) {
3050 auto *Call
= unwrap
<CallBase
>(C
);
3051 auto AS
= Call
->getAttributes().getAttributes(Idx
);
3056 LLVMAttributeRef
LLVMGetCallSiteEnumAttribute(LLVMValueRef C
,
3057 LLVMAttributeIndex Idx
,
3059 return wrap(unwrap
<CallBase
>(C
)->getAttributeAtIndex(
3060 Idx
, (Attribute::AttrKind
)KindID
));
3063 LLVMAttributeRef
LLVMGetCallSiteStringAttribute(LLVMValueRef C
,
3064 LLVMAttributeIndex Idx
,
3065 const char *K
, unsigned KLen
) {
3067 unwrap
<CallBase
>(C
)->getAttributeAtIndex(Idx
, StringRef(K
, KLen
)));
3070 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C
, LLVMAttributeIndex Idx
,
3072 unwrap
<CallBase
>(C
)->removeAttributeAtIndex(Idx
, (Attribute::AttrKind
)KindID
);
3075 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C
, LLVMAttributeIndex Idx
,
3076 const char *K
, unsigned KLen
) {
3077 unwrap
<CallBase
>(C
)->removeAttributeAtIndex(Idx
, StringRef(K
, KLen
));
3080 LLVMValueRef
LLVMGetCalledValue(LLVMValueRef Instr
) {
3081 return wrap(unwrap
<CallBase
>(Instr
)->getCalledOperand());
3084 LLVMTypeRef
LLVMGetCalledFunctionType(LLVMValueRef Instr
) {
3085 return wrap(unwrap
<CallBase
>(Instr
)->getFunctionType());
3088 unsigned LLVMGetNumOperandBundles(LLVMValueRef C
) {
3089 return unwrap
<CallBase
>(C
)->getNumOperandBundles();
3092 LLVMOperandBundleRef
LLVMGetOperandBundleAtIndex(LLVMValueRef C
,
3095 new OperandBundleDef(unwrap
<CallBase
>(C
)->getOperandBundleAt(Index
)));
3098 /*--.. Operations on call instructions (only) ..............................--*/
3100 LLVMBool
LLVMIsTailCall(LLVMValueRef Call
) {
3101 return unwrap
<CallInst
>(Call
)->isTailCall();
3104 void LLVMSetTailCall(LLVMValueRef Call
, LLVMBool isTailCall
) {
3105 unwrap
<CallInst
>(Call
)->setTailCall(isTailCall
);
3108 LLVMTailCallKind
LLVMGetTailCallKind(LLVMValueRef Call
) {
3109 return (LLVMTailCallKind
)unwrap
<CallInst
>(Call
)->getTailCallKind();
3112 void LLVMSetTailCallKind(LLVMValueRef Call
, LLVMTailCallKind kind
) {
3113 unwrap
<CallInst
>(Call
)->setTailCallKind((CallInst::TailCallKind
)kind
);
3116 /*--.. Operations on invoke instructions (only) ............................--*/
3118 LLVMBasicBlockRef
LLVMGetNormalDest(LLVMValueRef Invoke
) {
3119 return wrap(unwrap
<InvokeInst
>(Invoke
)->getNormalDest());
3122 LLVMBasicBlockRef
LLVMGetUnwindDest(LLVMValueRef Invoke
) {
3123 if (CleanupReturnInst
*CRI
= dyn_cast
<CleanupReturnInst
>(unwrap(Invoke
))) {
3124 return wrap(CRI
->getUnwindDest());
3125 } else if (CatchSwitchInst
*CSI
= dyn_cast
<CatchSwitchInst
>(unwrap(Invoke
))) {
3126 return wrap(CSI
->getUnwindDest());
3128 return wrap(unwrap
<InvokeInst
>(Invoke
)->getUnwindDest());
3131 void LLVMSetNormalDest(LLVMValueRef Invoke
, LLVMBasicBlockRef B
) {
3132 unwrap
<InvokeInst
>(Invoke
)->setNormalDest(unwrap(B
));
3135 void LLVMSetUnwindDest(LLVMValueRef Invoke
, LLVMBasicBlockRef B
) {
3136 if (CleanupReturnInst
*CRI
= dyn_cast
<CleanupReturnInst
>(unwrap(Invoke
))) {
3137 return CRI
->setUnwindDest(unwrap(B
));
3138 } else if (CatchSwitchInst
*CSI
= dyn_cast
<CatchSwitchInst
>(unwrap(Invoke
))) {
3139 return CSI
->setUnwindDest(unwrap(B
));
3141 unwrap
<InvokeInst
>(Invoke
)->setUnwindDest(unwrap(B
));
3144 LLVMBasicBlockRef
LLVMGetCallBrDefaultDest(LLVMValueRef CallBr
) {
3145 return wrap(unwrap
<CallBrInst
>(CallBr
)->getDefaultDest());
3148 unsigned LLVMGetCallBrNumIndirectDests(LLVMValueRef CallBr
) {
3149 return unwrap
<CallBrInst
>(CallBr
)->getNumIndirectDests();
3152 LLVMBasicBlockRef
LLVMGetCallBrIndirectDest(LLVMValueRef CallBr
, unsigned Idx
) {
3153 return wrap(unwrap
<CallBrInst
>(CallBr
)->getIndirectDest(Idx
));
3156 /*--.. Operations on terminators ...........................................--*/
3158 unsigned LLVMGetNumSuccessors(LLVMValueRef Term
) {
3159 return unwrap
<Instruction
>(Term
)->getNumSuccessors();
3162 LLVMBasicBlockRef
LLVMGetSuccessor(LLVMValueRef Term
, unsigned i
) {
3163 return wrap(unwrap
<Instruction
>(Term
)->getSuccessor(i
));
3166 void LLVMSetSuccessor(LLVMValueRef Term
, unsigned i
, LLVMBasicBlockRef block
) {
3167 return unwrap
<Instruction
>(Term
)->setSuccessor(i
, unwrap(block
));
3170 /*--.. Operations on branch instructions (only) ............................--*/
3172 LLVMBool
LLVMIsConditional(LLVMValueRef Branch
) {
3173 return unwrap
<BranchInst
>(Branch
)->isConditional();
3176 LLVMValueRef
LLVMGetCondition(LLVMValueRef Branch
) {
3177 return wrap(unwrap
<BranchInst
>(Branch
)->getCondition());
3180 void LLVMSetCondition(LLVMValueRef Branch
, LLVMValueRef Cond
) {
3181 return unwrap
<BranchInst
>(Branch
)->setCondition(unwrap(Cond
));
3184 /*--.. Operations on switch instructions (only) ............................--*/
3186 LLVMBasicBlockRef
LLVMGetSwitchDefaultDest(LLVMValueRef Switch
) {
3187 return wrap(unwrap
<SwitchInst
>(Switch
)->getDefaultDest());
3190 /*--.. Operations on alloca instructions (only) ............................--*/
3192 LLVMTypeRef
LLVMGetAllocatedType(LLVMValueRef Alloca
) {
3193 return wrap(unwrap
<AllocaInst
>(Alloca
)->getAllocatedType());
3196 /*--.. Operations on gep instructions (only) ...............................--*/
3198 LLVMBool
LLVMIsInBounds(LLVMValueRef GEP
) {
3199 return unwrap
<GEPOperator
>(GEP
)->isInBounds();
3202 void LLVMSetIsInBounds(LLVMValueRef GEP
, LLVMBool InBounds
) {
3203 return unwrap
<GetElementPtrInst
>(GEP
)->setIsInBounds(InBounds
);
3206 LLVMTypeRef
LLVMGetGEPSourceElementType(LLVMValueRef GEP
) {
3207 return wrap(unwrap
<GEPOperator
>(GEP
)->getSourceElementType());
3210 LLVMGEPNoWrapFlags
LLVMGEPGetNoWrapFlags(LLVMValueRef GEP
) {
3211 GEPOperator
*GEPOp
= unwrap
<GEPOperator
>(GEP
);
3212 return mapToLLVMGEPNoWrapFlags(GEPOp
->getNoWrapFlags());
3215 void LLVMGEPSetNoWrapFlags(LLVMValueRef GEP
, LLVMGEPNoWrapFlags NoWrapFlags
) {
3216 GetElementPtrInst
*GEPInst
= unwrap
<GetElementPtrInst
>(GEP
);
3217 GEPInst
->setNoWrapFlags(mapFromLLVMGEPNoWrapFlags(NoWrapFlags
));
3220 /*--.. Operations on phi nodes .............................................--*/
3222 void LLVMAddIncoming(LLVMValueRef PhiNode
, LLVMValueRef
*IncomingValues
,
3223 LLVMBasicBlockRef
*IncomingBlocks
, unsigned Count
) {
3224 PHINode
*PhiVal
= unwrap
<PHINode
>(PhiNode
);
3225 for (unsigned I
= 0; I
!= Count
; ++I
)
3226 PhiVal
->addIncoming(unwrap(IncomingValues
[I
]), unwrap(IncomingBlocks
[I
]));
3229 unsigned LLVMCountIncoming(LLVMValueRef PhiNode
) {
3230 return unwrap
<PHINode
>(PhiNode
)->getNumIncomingValues();
3233 LLVMValueRef
LLVMGetIncomingValue(LLVMValueRef PhiNode
, unsigned Index
) {
3234 return wrap(unwrap
<PHINode
>(PhiNode
)->getIncomingValue(Index
));
3237 LLVMBasicBlockRef
LLVMGetIncomingBlock(LLVMValueRef PhiNode
, unsigned Index
) {
3238 return wrap(unwrap
<PHINode
>(PhiNode
)->getIncomingBlock(Index
));
3241 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
3243 unsigned LLVMGetNumIndices(LLVMValueRef Inst
) {
3244 auto *I
= unwrap(Inst
);
3245 if (auto *GEP
= dyn_cast
<GEPOperator
>(I
))
3246 return GEP
->getNumIndices();
3247 if (auto *EV
= dyn_cast
<ExtractValueInst
>(I
))
3248 return EV
->getNumIndices();
3249 if (auto *IV
= dyn_cast
<InsertValueInst
>(I
))
3250 return IV
->getNumIndices();
3252 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3255 const unsigned *LLVMGetIndices(LLVMValueRef Inst
) {
3256 auto *I
= unwrap(Inst
);
3257 if (auto *EV
= dyn_cast
<ExtractValueInst
>(I
))
3258 return EV
->getIndices().data();
3259 if (auto *IV
= dyn_cast
<InsertValueInst
>(I
))
3260 return IV
->getIndices().data();
3262 "LLVMGetIndices applies only to extractvalue and insertvalue!");
3266 /*===-- Instruction builders ----------------------------------------------===*/
3268 LLVMBuilderRef
LLVMCreateBuilderInContext(LLVMContextRef C
) {
3269 return wrap(new IRBuilder
<>(*unwrap(C
)));
3272 LLVMBuilderRef
LLVMCreateBuilder(void) {
3273 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
3276 static void LLVMPositionBuilderImpl(IRBuilder
<> *Builder
, BasicBlock
*Block
,
3277 Instruction
*Instr
, bool BeforeDbgRecords
) {
3278 BasicBlock::iterator I
= Instr
? Instr
->getIterator() : Block
->end();
3279 I
.setHeadBit(BeforeDbgRecords
);
3280 Builder
->SetInsertPoint(Block
, I
);
3283 void LLVMPositionBuilder(LLVMBuilderRef Builder
, LLVMBasicBlockRef Block
,
3284 LLVMValueRef Instr
) {
3285 return LLVMPositionBuilderImpl(unwrap(Builder
), unwrap(Block
),
3286 unwrap
<Instruction
>(Instr
), false);
3289 void LLVMPositionBuilderBeforeDbgRecords(LLVMBuilderRef Builder
,
3290 LLVMBasicBlockRef Block
,
3291 LLVMValueRef Instr
) {
3292 return LLVMPositionBuilderImpl(unwrap(Builder
), unwrap(Block
),
3293 unwrap
<Instruction
>(Instr
), true);
3296 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder
, LLVMValueRef Instr
) {
3297 Instruction
*I
= unwrap
<Instruction
>(Instr
);
3298 return LLVMPositionBuilderImpl(unwrap(Builder
), I
->getParent(), I
, false);
3301 void LLVMPositionBuilderBeforeInstrAndDbgRecords(LLVMBuilderRef Builder
,
3302 LLVMValueRef Instr
) {
3303 Instruction
*I
= unwrap
<Instruction
>(Instr
);
3304 return LLVMPositionBuilderImpl(unwrap(Builder
), I
->getParent(), I
, true);
3307 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder
, LLVMBasicBlockRef Block
) {
3308 BasicBlock
*BB
= unwrap(Block
);
3309 unwrap(Builder
)->SetInsertPoint(BB
);
3312 LLVMBasicBlockRef
LLVMGetInsertBlock(LLVMBuilderRef Builder
) {
3313 return wrap(unwrap(Builder
)->GetInsertBlock());
3316 void LLVMClearInsertionPosition(LLVMBuilderRef Builder
) {
3317 unwrap(Builder
)->ClearInsertionPoint();
3320 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder
, LLVMValueRef Instr
) {
3321 unwrap(Builder
)->Insert(unwrap
<Instruction
>(Instr
));
3324 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder
, LLVMValueRef Instr
,
3326 unwrap(Builder
)->Insert(unwrap
<Instruction
>(Instr
), Name
);
3329 void LLVMDisposeBuilder(LLVMBuilderRef Builder
) {
3330 delete unwrap(Builder
);
3333 /*--.. Metadata builders ...................................................--*/
3335 LLVMMetadataRef
LLVMGetCurrentDebugLocation2(LLVMBuilderRef Builder
) {
3336 return wrap(unwrap(Builder
)->getCurrentDebugLocation().getAsMDNode());
3339 void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Builder
, LLVMMetadataRef Loc
) {
3341 unwrap(Builder
)->SetCurrentDebugLocation(DebugLoc(unwrap
<MDNode
>(Loc
)));
3343 unwrap(Builder
)->SetCurrentDebugLocation(DebugLoc());
3346 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder
, LLVMValueRef L
) {
3348 L
? cast
<MDNode
>(unwrap
<MetadataAsValue
>(L
)->getMetadata()) : nullptr;
3349 unwrap(Builder
)->SetCurrentDebugLocation(DebugLoc(Loc
));
3352 LLVMValueRef
LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder
) {
3353 LLVMContext
&Context
= unwrap(Builder
)->getContext();
3354 return wrap(MetadataAsValue::get(
3355 Context
, unwrap(Builder
)->getCurrentDebugLocation().getAsMDNode()));
3358 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder
, LLVMValueRef Inst
) {
3359 unwrap(Builder
)->SetInstDebugLocation(unwrap
<Instruction
>(Inst
));
3362 void LLVMAddMetadataToInst(LLVMBuilderRef Builder
, LLVMValueRef Inst
) {
3363 unwrap(Builder
)->AddMetadataToInst(unwrap
<Instruction
>(Inst
));
3366 void LLVMBuilderSetDefaultFPMathTag(LLVMBuilderRef Builder
,
3367 LLVMMetadataRef FPMathTag
) {
3369 unwrap(Builder
)->setDefaultFPMathTag(FPMathTag
3370 ? unwrap
<MDNode
>(FPMathTag
)
3374 LLVMContextRef
LLVMGetBuilderContext(LLVMBuilderRef Builder
) {
3375 return wrap(&unwrap(Builder
)->getContext());
3378 LLVMMetadataRef
LLVMBuilderGetDefaultFPMathTag(LLVMBuilderRef Builder
) {
3379 return wrap(unwrap(Builder
)->getDefaultFPMathTag());
3382 /*--.. Instruction builders ................................................--*/
3384 LLVMValueRef
LLVMBuildRetVoid(LLVMBuilderRef B
) {
3385 return wrap(unwrap(B
)->CreateRetVoid());
3388 LLVMValueRef
LLVMBuildRet(LLVMBuilderRef B
, LLVMValueRef V
) {
3389 return wrap(unwrap(B
)->CreateRet(unwrap(V
)));
3392 LLVMValueRef
LLVMBuildAggregateRet(LLVMBuilderRef B
, LLVMValueRef
*RetVals
,
3394 return wrap(unwrap(B
)->CreateAggregateRet(unwrap(RetVals
), N
));
3397 LLVMValueRef
LLVMBuildBr(LLVMBuilderRef B
, LLVMBasicBlockRef Dest
) {
3398 return wrap(unwrap(B
)->CreateBr(unwrap(Dest
)));
3401 LLVMValueRef
LLVMBuildCondBr(LLVMBuilderRef B
, LLVMValueRef If
,
3402 LLVMBasicBlockRef Then
, LLVMBasicBlockRef Else
) {
3403 return wrap(unwrap(B
)->CreateCondBr(unwrap(If
), unwrap(Then
), unwrap(Else
)));
3406 LLVMValueRef
LLVMBuildSwitch(LLVMBuilderRef B
, LLVMValueRef V
,
3407 LLVMBasicBlockRef Else
, unsigned NumCases
) {
3408 return wrap(unwrap(B
)->CreateSwitch(unwrap(V
), unwrap(Else
), NumCases
));
3411 LLVMValueRef
LLVMBuildIndirectBr(LLVMBuilderRef B
, LLVMValueRef Addr
,
3412 unsigned NumDests
) {
3413 return wrap(unwrap(B
)->CreateIndirectBr(unwrap(Addr
), NumDests
));
3416 LLVMValueRef
LLVMBuildCallBr(LLVMBuilderRef B
, LLVMTypeRef Ty
, LLVMValueRef Fn
,
3417 LLVMBasicBlockRef DefaultDest
,
3418 LLVMBasicBlockRef
*IndirectDests
,
3419 unsigned NumIndirectDests
, LLVMValueRef
*Args
,
3420 unsigned NumArgs
, LLVMOperandBundleRef
*Bundles
,
3421 unsigned NumBundles
, const char *Name
) {
3423 SmallVector
<OperandBundleDef
, 8> OBs
;
3424 for (auto *Bundle
: ArrayRef(Bundles
, NumBundles
)) {
3425 OperandBundleDef
*OB
= unwrap(Bundle
);
3429 return wrap(unwrap(B
)->CreateCallBr(
3430 unwrap
<FunctionType
>(Ty
), unwrap(Fn
), unwrap(DefaultDest
),
3431 ArrayRef(unwrap(IndirectDests
), NumIndirectDests
),
3432 ArrayRef
<Value
*>(unwrap(Args
), NumArgs
), OBs
, Name
));
3435 LLVMValueRef
LLVMBuildInvoke2(LLVMBuilderRef B
, LLVMTypeRef Ty
, LLVMValueRef Fn
,
3436 LLVMValueRef
*Args
, unsigned NumArgs
,
3437 LLVMBasicBlockRef Then
, LLVMBasicBlockRef Catch
,
3439 return wrap(unwrap(B
)->CreateInvoke(unwrap
<FunctionType
>(Ty
), unwrap(Fn
),
3440 unwrap(Then
), unwrap(Catch
),
3441 ArrayRef(unwrap(Args
), NumArgs
), Name
));
3444 LLVMValueRef
LLVMBuildInvokeWithOperandBundles(
3445 LLVMBuilderRef B
, LLVMTypeRef Ty
, LLVMValueRef Fn
, LLVMValueRef
*Args
,
3446 unsigned NumArgs
, LLVMBasicBlockRef Then
, LLVMBasicBlockRef Catch
,
3447 LLVMOperandBundleRef
*Bundles
, unsigned NumBundles
, const char *Name
) {
3448 SmallVector
<OperandBundleDef
, 8> OBs
;
3449 for (auto *Bundle
: ArrayRef(Bundles
, NumBundles
)) {
3450 OperandBundleDef
*OB
= unwrap(Bundle
);
3453 return wrap(unwrap(B
)->CreateInvoke(
3454 unwrap
<FunctionType
>(Ty
), unwrap(Fn
), unwrap(Then
), unwrap(Catch
),
3455 ArrayRef(unwrap(Args
), NumArgs
), OBs
, Name
));
3458 LLVMValueRef
LLVMBuildLandingPad(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3459 LLVMValueRef PersFn
, unsigned NumClauses
,
3461 // The personality used to live on the landingpad instruction, but now it
3462 // lives on the parent function. For compatibility, take the provided
3463 // personality and put it on the parent function.
3465 unwrap(B
)->GetInsertBlock()->getParent()->setPersonalityFn(
3466 unwrap
<Function
>(PersFn
));
3467 return wrap(unwrap(B
)->CreateLandingPad(unwrap(Ty
), NumClauses
, Name
));
3470 LLVMValueRef
LLVMBuildCatchPad(LLVMBuilderRef B
, LLVMValueRef ParentPad
,
3471 LLVMValueRef
*Args
, unsigned NumArgs
,
3473 return wrap(unwrap(B
)->CreateCatchPad(unwrap(ParentPad
),
3474 ArrayRef(unwrap(Args
), NumArgs
), Name
));
3477 LLVMValueRef
LLVMBuildCleanupPad(LLVMBuilderRef B
, LLVMValueRef ParentPad
,
3478 LLVMValueRef
*Args
, unsigned NumArgs
,
3480 if (ParentPad
== nullptr) {
3481 Type
*Ty
= Type::getTokenTy(unwrap(B
)->getContext());
3482 ParentPad
= wrap(Constant::getNullValue(Ty
));
3484 return wrap(unwrap(B
)->CreateCleanupPad(
3485 unwrap(ParentPad
), ArrayRef(unwrap(Args
), NumArgs
), Name
));
3488 LLVMValueRef
LLVMBuildResume(LLVMBuilderRef B
, LLVMValueRef Exn
) {
3489 return wrap(unwrap(B
)->CreateResume(unwrap(Exn
)));
3492 LLVMValueRef
LLVMBuildCatchSwitch(LLVMBuilderRef B
, LLVMValueRef ParentPad
,
3493 LLVMBasicBlockRef UnwindBB
,
3494 unsigned NumHandlers
, const char *Name
) {
3495 if (ParentPad
== nullptr) {
3496 Type
*Ty
= Type::getTokenTy(unwrap(B
)->getContext());
3497 ParentPad
= wrap(Constant::getNullValue(Ty
));
3499 return wrap(unwrap(B
)->CreateCatchSwitch(unwrap(ParentPad
), unwrap(UnwindBB
),
3500 NumHandlers
, Name
));
3503 LLVMValueRef
LLVMBuildCatchRet(LLVMBuilderRef B
, LLVMValueRef CatchPad
,
3504 LLVMBasicBlockRef BB
) {
3505 return wrap(unwrap(B
)->CreateCatchRet(unwrap
<CatchPadInst
>(CatchPad
),
3509 LLVMValueRef
LLVMBuildCleanupRet(LLVMBuilderRef B
, LLVMValueRef CatchPad
,
3510 LLVMBasicBlockRef BB
) {
3511 return wrap(unwrap(B
)->CreateCleanupRet(unwrap
<CleanupPadInst
>(CatchPad
),
3515 LLVMValueRef
LLVMBuildUnreachable(LLVMBuilderRef B
) {
3516 return wrap(unwrap(B
)->CreateUnreachable());
3519 void LLVMAddCase(LLVMValueRef Switch
, LLVMValueRef OnVal
,
3520 LLVMBasicBlockRef Dest
) {
3521 unwrap
<SwitchInst
>(Switch
)->addCase(unwrap
<ConstantInt
>(OnVal
), unwrap(Dest
));
3524 void LLVMAddDestination(LLVMValueRef IndirectBr
, LLVMBasicBlockRef Dest
) {
3525 unwrap
<IndirectBrInst
>(IndirectBr
)->addDestination(unwrap(Dest
));
3528 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad
) {
3529 return unwrap
<LandingPadInst
>(LandingPad
)->getNumClauses();
3532 LLVMValueRef
LLVMGetClause(LLVMValueRef LandingPad
, unsigned Idx
) {
3533 return wrap(unwrap
<LandingPadInst
>(LandingPad
)->getClause(Idx
));
3536 void LLVMAddClause(LLVMValueRef LandingPad
, LLVMValueRef ClauseVal
) {
3537 unwrap
<LandingPadInst
>(LandingPad
)->addClause(unwrap
<Constant
>(ClauseVal
));
3540 LLVMBool
LLVMIsCleanup(LLVMValueRef LandingPad
) {
3541 return unwrap
<LandingPadInst
>(LandingPad
)->isCleanup();
3544 void LLVMSetCleanup(LLVMValueRef LandingPad
, LLVMBool Val
) {
3545 unwrap
<LandingPadInst
>(LandingPad
)->setCleanup(Val
);
3548 void LLVMAddHandler(LLVMValueRef CatchSwitch
, LLVMBasicBlockRef Dest
) {
3549 unwrap
<CatchSwitchInst
>(CatchSwitch
)->addHandler(unwrap(Dest
));
3552 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch
) {
3553 return unwrap
<CatchSwitchInst
>(CatchSwitch
)->getNumHandlers();
3556 void LLVMGetHandlers(LLVMValueRef CatchSwitch
, LLVMBasicBlockRef
*Handlers
) {
3557 CatchSwitchInst
*CSI
= unwrap
<CatchSwitchInst
>(CatchSwitch
);
3558 for (const BasicBlock
*H
: CSI
->handlers())
3559 *Handlers
++ = wrap(H
);
3562 LLVMValueRef
LLVMGetParentCatchSwitch(LLVMValueRef CatchPad
) {
3563 return wrap(unwrap
<CatchPadInst
>(CatchPad
)->getCatchSwitch());
3566 void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad
, LLVMValueRef CatchSwitch
) {
3567 unwrap
<CatchPadInst
>(CatchPad
)
3568 ->setCatchSwitch(unwrap
<CatchSwitchInst
>(CatchSwitch
));
3571 /*--.. Funclets ...........................................................--*/
3573 LLVMValueRef
LLVMGetArgOperand(LLVMValueRef Funclet
, unsigned i
) {
3574 return wrap(unwrap
<FuncletPadInst
>(Funclet
)->getArgOperand(i
));
3577 void LLVMSetArgOperand(LLVMValueRef Funclet
, unsigned i
, LLVMValueRef value
) {
3578 unwrap
<FuncletPadInst
>(Funclet
)->setArgOperand(i
, unwrap(value
));
3581 /*--.. Arithmetic ..........................................................--*/
3583 static FastMathFlags
mapFromLLVMFastMathFlags(LLVMFastMathFlags FMF
) {
3584 FastMathFlags NewFMF
;
3585 NewFMF
.setAllowReassoc((FMF
& LLVMFastMathAllowReassoc
) != 0);
3586 NewFMF
.setNoNaNs((FMF
& LLVMFastMathNoNaNs
) != 0);
3587 NewFMF
.setNoInfs((FMF
& LLVMFastMathNoInfs
) != 0);
3588 NewFMF
.setNoSignedZeros((FMF
& LLVMFastMathNoSignedZeros
) != 0);
3589 NewFMF
.setAllowReciprocal((FMF
& LLVMFastMathAllowReciprocal
) != 0);
3590 NewFMF
.setAllowContract((FMF
& LLVMFastMathAllowContract
) != 0);
3591 NewFMF
.setApproxFunc((FMF
& LLVMFastMathApproxFunc
) != 0);
3596 static LLVMFastMathFlags
mapToLLVMFastMathFlags(FastMathFlags FMF
) {
3597 LLVMFastMathFlags NewFMF
= LLVMFastMathNone
;
3598 if (FMF
.allowReassoc())
3599 NewFMF
|= LLVMFastMathAllowReassoc
;
3601 NewFMF
|= LLVMFastMathNoNaNs
;
3603 NewFMF
|= LLVMFastMathNoInfs
;
3604 if (FMF
.noSignedZeros())
3605 NewFMF
|= LLVMFastMathNoSignedZeros
;
3606 if (FMF
.allowReciprocal())
3607 NewFMF
|= LLVMFastMathAllowReciprocal
;
3608 if (FMF
.allowContract())
3609 NewFMF
|= LLVMFastMathAllowContract
;
3610 if (FMF
.approxFunc())
3611 NewFMF
|= LLVMFastMathApproxFunc
;
3616 LLVMValueRef
LLVMBuildAdd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3618 return wrap(unwrap(B
)->CreateAdd(unwrap(LHS
), unwrap(RHS
), Name
));
3621 LLVMValueRef
LLVMBuildNSWAdd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3623 return wrap(unwrap(B
)->CreateNSWAdd(unwrap(LHS
), unwrap(RHS
), Name
));
3626 LLVMValueRef
LLVMBuildNUWAdd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3628 return wrap(unwrap(B
)->CreateNUWAdd(unwrap(LHS
), unwrap(RHS
), Name
));
3631 LLVMValueRef
LLVMBuildFAdd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3633 return wrap(unwrap(B
)->CreateFAdd(unwrap(LHS
), unwrap(RHS
), Name
));
3636 LLVMValueRef
LLVMBuildSub(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3638 return wrap(unwrap(B
)->CreateSub(unwrap(LHS
), unwrap(RHS
), Name
));
3641 LLVMValueRef
LLVMBuildNSWSub(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3643 return wrap(unwrap(B
)->CreateNSWSub(unwrap(LHS
), unwrap(RHS
), Name
));
3646 LLVMValueRef
LLVMBuildNUWSub(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3648 return wrap(unwrap(B
)->CreateNUWSub(unwrap(LHS
), unwrap(RHS
), Name
));
3651 LLVMValueRef
LLVMBuildFSub(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3653 return wrap(unwrap(B
)->CreateFSub(unwrap(LHS
), unwrap(RHS
), Name
));
3656 LLVMValueRef
LLVMBuildMul(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3658 return wrap(unwrap(B
)->CreateMul(unwrap(LHS
), unwrap(RHS
), Name
));
3661 LLVMValueRef
LLVMBuildNSWMul(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3663 return wrap(unwrap(B
)->CreateNSWMul(unwrap(LHS
), unwrap(RHS
), Name
));
3666 LLVMValueRef
LLVMBuildNUWMul(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3668 return wrap(unwrap(B
)->CreateNUWMul(unwrap(LHS
), unwrap(RHS
), Name
));
3671 LLVMValueRef
LLVMBuildFMul(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3673 return wrap(unwrap(B
)->CreateFMul(unwrap(LHS
), unwrap(RHS
), Name
));
3676 LLVMValueRef
LLVMBuildUDiv(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3678 return wrap(unwrap(B
)->CreateUDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3681 LLVMValueRef
LLVMBuildExactUDiv(LLVMBuilderRef B
, LLVMValueRef LHS
,
3682 LLVMValueRef RHS
, const char *Name
) {
3683 return wrap(unwrap(B
)->CreateExactUDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3686 LLVMValueRef
LLVMBuildSDiv(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3688 return wrap(unwrap(B
)->CreateSDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3691 LLVMValueRef
LLVMBuildExactSDiv(LLVMBuilderRef B
, LLVMValueRef LHS
,
3692 LLVMValueRef RHS
, const char *Name
) {
3693 return wrap(unwrap(B
)->CreateExactSDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3696 LLVMValueRef
LLVMBuildFDiv(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3698 return wrap(unwrap(B
)->CreateFDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3701 LLVMValueRef
LLVMBuildURem(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3703 return wrap(unwrap(B
)->CreateURem(unwrap(LHS
), unwrap(RHS
), Name
));
3706 LLVMValueRef
LLVMBuildSRem(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3708 return wrap(unwrap(B
)->CreateSRem(unwrap(LHS
), unwrap(RHS
), Name
));
3711 LLVMValueRef
LLVMBuildFRem(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3713 return wrap(unwrap(B
)->CreateFRem(unwrap(LHS
), unwrap(RHS
), Name
));
3716 LLVMValueRef
LLVMBuildShl(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3718 return wrap(unwrap(B
)->CreateShl(unwrap(LHS
), unwrap(RHS
), Name
));
3721 LLVMValueRef
LLVMBuildLShr(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3723 return wrap(unwrap(B
)->CreateLShr(unwrap(LHS
), unwrap(RHS
), Name
));
3726 LLVMValueRef
LLVMBuildAShr(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3728 return wrap(unwrap(B
)->CreateAShr(unwrap(LHS
), unwrap(RHS
), Name
));
3731 LLVMValueRef
LLVMBuildAnd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3733 return wrap(unwrap(B
)->CreateAnd(unwrap(LHS
), unwrap(RHS
), Name
));
3736 LLVMValueRef
LLVMBuildOr(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3738 return wrap(unwrap(B
)->CreateOr(unwrap(LHS
), unwrap(RHS
), Name
));
3741 LLVMValueRef
LLVMBuildXor(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3743 return wrap(unwrap(B
)->CreateXor(unwrap(LHS
), unwrap(RHS
), Name
));
3746 LLVMValueRef
LLVMBuildBinOp(LLVMBuilderRef B
, LLVMOpcode Op
,
3747 LLVMValueRef LHS
, LLVMValueRef RHS
,
3749 return wrap(unwrap(B
)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op
)), unwrap(LHS
),
3750 unwrap(RHS
), Name
));
3753 LLVMValueRef
LLVMBuildNeg(LLVMBuilderRef B
, LLVMValueRef V
, const char *Name
) {
3754 return wrap(unwrap(B
)->CreateNeg(unwrap(V
), Name
));
3757 LLVMValueRef
LLVMBuildNSWNeg(LLVMBuilderRef B
, LLVMValueRef V
,
3759 return wrap(unwrap(B
)->CreateNSWNeg(unwrap(V
), Name
));
3762 LLVMValueRef
LLVMBuildNUWNeg(LLVMBuilderRef B
, LLVMValueRef V
,
3764 Value
*Neg
= unwrap(B
)->CreateNeg(unwrap(V
), Name
);
3765 if (auto *I
= dyn_cast
<BinaryOperator
>(Neg
))
3766 I
->setHasNoUnsignedWrap();
3770 LLVMValueRef
LLVMBuildFNeg(LLVMBuilderRef B
, LLVMValueRef V
, const char *Name
) {
3771 return wrap(unwrap(B
)->CreateFNeg(unwrap(V
), Name
));
3774 LLVMValueRef
LLVMBuildNot(LLVMBuilderRef B
, LLVMValueRef V
, const char *Name
) {
3775 return wrap(unwrap(B
)->CreateNot(unwrap(V
), Name
));
3778 LLVMBool
LLVMGetNUW(LLVMValueRef ArithInst
) {
3779 Value
*P
= unwrap
<Value
>(ArithInst
);
3780 return cast
<Instruction
>(P
)->hasNoUnsignedWrap();
3783 void LLVMSetNUW(LLVMValueRef ArithInst
, LLVMBool HasNUW
) {
3784 Value
*P
= unwrap
<Value
>(ArithInst
);
3785 cast
<Instruction
>(P
)->setHasNoUnsignedWrap(HasNUW
);
3788 LLVMBool
LLVMGetNSW(LLVMValueRef ArithInst
) {
3789 Value
*P
= unwrap
<Value
>(ArithInst
);
3790 return cast
<Instruction
>(P
)->hasNoSignedWrap();
3793 void LLVMSetNSW(LLVMValueRef ArithInst
, LLVMBool HasNSW
) {
3794 Value
*P
= unwrap
<Value
>(ArithInst
);
3795 cast
<Instruction
>(P
)->setHasNoSignedWrap(HasNSW
);
3798 LLVMBool
LLVMGetExact(LLVMValueRef DivOrShrInst
) {
3799 Value
*P
= unwrap
<Value
>(DivOrShrInst
);
3800 return cast
<Instruction
>(P
)->isExact();
3803 void LLVMSetExact(LLVMValueRef DivOrShrInst
, LLVMBool IsExact
) {
3804 Value
*P
= unwrap
<Value
>(DivOrShrInst
);
3805 cast
<Instruction
>(P
)->setIsExact(IsExact
);
3808 LLVMBool
LLVMGetNNeg(LLVMValueRef NonNegInst
) {
3809 Value
*P
= unwrap
<Value
>(NonNegInst
);
3810 return cast
<Instruction
>(P
)->hasNonNeg();
3813 void LLVMSetNNeg(LLVMValueRef NonNegInst
, LLVMBool IsNonNeg
) {
3814 Value
*P
= unwrap
<Value
>(NonNegInst
);
3815 cast
<Instruction
>(P
)->setNonNeg(IsNonNeg
);
3818 LLVMFastMathFlags
LLVMGetFastMathFlags(LLVMValueRef FPMathInst
) {
3819 Value
*P
= unwrap
<Value
>(FPMathInst
);
3820 FastMathFlags FMF
= cast
<Instruction
>(P
)->getFastMathFlags();
3821 return mapToLLVMFastMathFlags(FMF
);
3824 void LLVMSetFastMathFlags(LLVMValueRef FPMathInst
, LLVMFastMathFlags FMF
) {
3825 Value
*P
= unwrap
<Value
>(FPMathInst
);
3826 cast
<Instruction
>(P
)->setFastMathFlags(mapFromLLVMFastMathFlags(FMF
));
3829 LLVMBool
LLVMCanValueUseFastMathFlags(LLVMValueRef V
) {
3830 Value
*Val
= unwrap
<Value
>(V
);
3831 return isa
<FPMathOperator
>(Val
);
3834 LLVMBool
LLVMGetIsDisjoint(LLVMValueRef Inst
) {
3835 Value
*P
= unwrap
<Value
>(Inst
);
3836 return cast
<PossiblyDisjointInst
>(P
)->isDisjoint();
3839 void LLVMSetIsDisjoint(LLVMValueRef Inst
, LLVMBool IsDisjoint
) {
3840 Value
*P
= unwrap
<Value
>(Inst
);
3841 cast
<PossiblyDisjointInst
>(P
)->setIsDisjoint(IsDisjoint
);
3844 /*--.. Memory ..............................................................--*/
3846 LLVMValueRef
LLVMBuildMalloc(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3848 Type
* ITy
= Type::getInt32Ty(unwrap(B
)->GetInsertBlock()->getContext());
3849 Constant
* AllocSize
= ConstantExpr::getSizeOf(unwrap(Ty
));
3850 AllocSize
= ConstantExpr::getTruncOrBitCast(AllocSize
, ITy
);
3851 return wrap(unwrap(B
)->CreateMalloc(ITy
, unwrap(Ty
), AllocSize
, nullptr,
3855 LLVMValueRef
LLVMBuildArrayMalloc(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3856 LLVMValueRef Val
, const char *Name
) {
3857 Type
* ITy
= Type::getInt32Ty(unwrap(B
)->GetInsertBlock()->getContext());
3858 Constant
* AllocSize
= ConstantExpr::getSizeOf(unwrap(Ty
));
3859 AllocSize
= ConstantExpr::getTruncOrBitCast(AllocSize
, ITy
);
3860 return wrap(unwrap(B
)->CreateMalloc(ITy
, unwrap(Ty
), AllocSize
, unwrap(Val
),
3864 LLVMValueRef
LLVMBuildMemSet(LLVMBuilderRef B
, LLVMValueRef Ptr
,
3865 LLVMValueRef Val
, LLVMValueRef Len
,
3867 return wrap(unwrap(B
)->CreateMemSet(unwrap(Ptr
), unwrap(Val
), unwrap(Len
),
3868 MaybeAlign(Align
)));
3871 LLVMValueRef
LLVMBuildMemCpy(LLVMBuilderRef B
,
3872 LLVMValueRef Dst
, unsigned DstAlign
,
3873 LLVMValueRef Src
, unsigned SrcAlign
,
3874 LLVMValueRef Size
) {
3875 return wrap(unwrap(B
)->CreateMemCpy(unwrap(Dst
), MaybeAlign(DstAlign
),
3876 unwrap(Src
), MaybeAlign(SrcAlign
),
3880 LLVMValueRef
LLVMBuildMemMove(LLVMBuilderRef B
,
3881 LLVMValueRef Dst
, unsigned DstAlign
,
3882 LLVMValueRef Src
, unsigned SrcAlign
,
3883 LLVMValueRef Size
) {
3884 return wrap(unwrap(B
)->CreateMemMove(unwrap(Dst
), MaybeAlign(DstAlign
),
3885 unwrap(Src
), MaybeAlign(SrcAlign
),
3889 LLVMValueRef
LLVMBuildAlloca(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3891 return wrap(unwrap(B
)->CreateAlloca(unwrap(Ty
), nullptr, Name
));
3894 LLVMValueRef
LLVMBuildArrayAlloca(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3895 LLVMValueRef Val
, const char *Name
) {
3896 return wrap(unwrap(B
)->CreateAlloca(unwrap(Ty
), unwrap(Val
), Name
));
3899 LLVMValueRef
LLVMBuildFree(LLVMBuilderRef B
, LLVMValueRef PointerVal
) {
3900 return wrap(unwrap(B
)->CreateFree(unwrap(PointerVal
)));
3903 LLVMValueRef
LLVMBuildLoad2(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3904 LLVMValueRef PointerVal
, const char *Name
) {
3905 return wrap(unwrap(B
)->CreateLoad(unwrap(Ty
), unwrap(PointerVal
), Name
));
3908 LLVMValueRef
LLVMBuildStore(LLVMBuilderRef B
, LLVMValueRef Val
,
3909 LLVMValueRef PointerVal
) {
3910 return wrap(unwrap(B
)->CreateStore(unwrap(Val
), unwrap(PointerVal
)));
3913 static AtomicOrdering
mapFromLLVMOrdering(LLVMAtomicOrdering Ordering
) {
3915 case LLVMAtomicOrderingNotAtomic
: return AtomicOrdering::NotAtomic
;
3916 case LLVMAtomicOrderingUnordered
: return AtomicOrdering::Unordered
;
3917 case LLVMAtomicOrderingMonotonic
: return AtomicOrdering::Monotonic
;
3918 case LLVMAtomicOrderingAcquire
: return AtomicOrdering::Acquire
;
3919 case LLVMAtomicOrderingRelease
: return AtomicOrdering::Release
;
3920 case LLVMAtomicOrderingAcquireRelease
:
3921 return AtomicOrdering::AcquireRelease
;
3922 case LLVMAtomicOrderingSequentiallyConsistent
:
3923 return AtomicOrdering::SequentiallyConsistent
;
3926 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3929 static LLVMAtomicOrdering
mapToLLVMOrdering(AtomicOrdering Ordering
) {
3931 case AtomicOrdering::NotAtomic
: return LLVMAtomicOrderingNotAtomic
;
3932 case AtomicOrdering::Unordered
: return LLVMAtomicOrderingUnordered
;
3933 case AtomicOrdering::Monotonic
: return LLVMAtomicOrderingMonotonic
;
3934 case AtomicOrdering::Acquire
: return LLVMAtomicOrderingAcquire
;
3935 case AtomicOrdering::Release
: return LLVMAtomicOrderingRelease
;
3936 case AtomicOrdering::AcquireRelease
:
3937 return LLVMAtomicOrderingAcquireRelease
;
3938 case AtomicOrdering::SequentiallyConsistent
:
3939 return LLVMAtomicOrderingSequentiallyConsistent
;
3942 llvm_unreachable("Invalid AtomicOrdering value!");
3945 static AtomicRMWInst::BinOp
mapFromLLVMRMWBinOp(LLVMAtomicRMWBinOp BinOp
) {
3947 case LLVMAtomicRMWBinOpXchg
: return AtomicRMWInst::Xchg
;
3948 case LLVMAtomicRMWBinOpAdd
: return AtomicRMWInst::Add
;
3949 case LLVMAtomicRMWBinOpSub
: return AtomicRMWInst::Sub
;
3950 case LLVMAtomicRMWBinOpAnd
: return AtomicRMWInst::And
;
3951 case LLVMAtomicRMWBinOpNand
: return AtomicRMWInst::Nand
;
3952 case LLVMAtomicRMWBinOpOr
: return AtomicRMWInst::Or
;
3953 case LLVMAtomicRMWBinOpXor
: return AtomicRMWInst::Xor
;
3954 case LLVMAtomicRMWBinOpMax
: return AtomicRMWInst::Max
;
3955 case LLVMAtomicRMWBinOpMin
: return AtomicRMWInst::Min
;
3956 case LLVMAtomicRMWBinOpUMax
: return AtomicRMWInst::UMax
;
3957 case LLVMAtomicRMWBinOpUMin
: return AtomicRMWInst::UMin
;
3958 case LLVMAtomicRMWBinOpFAdd
: return AtomicRMWInst::FAdd
;
3959 case LLVMAtomicRMWBinOpFSub
: return AtomicRMWInst::FSub
;
3960 case LLVMAtomicRMWBinOpFMax
: return AtomicRMWInst::FMax
;
3961 case LLVMAtomicRMWBinOpFMin
: return AtomicRMWInst::FMin
;
3962 case LLVMAtomicRMWBinOpUIncWrap
:
3963 return AtomicRMWInst::UIncWrap
;
3964 case LLVMAtomicRMWBinOpUDecWrap
:
3965 return AtomicRMWInst::UDecWrap
;
3966 case LLVMAtomicRMWBinOpUSubCond
:
3967 return AtomicRMWInst::USubCond
;
3968 case LLVMAtomicRMWBinOpUSubSat
:
3969 return AtomicRMWInst::USubSat
;
3972 llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3975 static LLVMAtomicRMWBinOp
mapToLLVMRMWBinOp(AtomicRMWInst::BinOp BinOp
) {
3977 case AtomicRMWInst::Xchg
: return LLVMAtomicRMWBinOpXchg
;
3978 case AtomicRMWInst::Add
: return LLVMAtomicRMWBinOpAdd
;
3979 case AtomicRMWInst::Sub
: return LLVMAtomicRMWBinOpSub
;
3980 case AtomicRMWInst::And
: return LLVMAtomicRMWBinOpAnd
;
3981 case AtomicRMWInst::Nand
: return LLVMAtomicRMWBinOpNand
;
3982 case AtomicRMWInst::Or
: return LLVMAtomicRMWBinOpOr
;
3983 case AtomicRMWInst::Xor
: return LLVMAtomicRMWBinOpXor
;
3984 case AtomicRMWInst::Max
: return LLVMAtomicRMWBinOpMax
;
3985 case AtomicRMWInst::Min
: return LLVMAtomicRMWBinOpMin
;
3986 case AtomicRMWInst::UMax
: return LLVMAtomicRMWBinOpUMax
;
3987 case AtomicRMWInst::UMin
: return LLVMAtomicRMWBinOpUMin
;
3988 case AtomicRMWInst::FAdd
: return LLVMAtomicRMWBinOpFAdd
;
3989 case AtomicRMWInst::FSub
: return LLVMAtomicRMWBinOpFSub
;
3990 case AtomicRMWInst::FMax
: return LLVMAtomicRMWBinOpFMax
;
3991 case AtomicRMWInst::FMin
: return LLVMAtomicRMWBinOpFMin
;
3992 case AtomicRMWInst::UIncWrap
:
3993 return LLVMAtomicRMWBinOpUIncWrap
;
3994 case AtomicRMWInst::UDecWrap
:
3995 return LLVMAtomicRMWBinOpUDecWrap
;
3996 case AtomicRMWInst::USubCond
:
3997 return LLVMAtomicRMWBinOpUSubCond
;
3998 case AtomicRMWInst::USubSat
:
3999 return LLVMAtomicRMWBinOpUSubSat
;
4003 llvm_unreachable("Invalid AtomicRMWBinOp value!");
4006 LLVMValueRef
LLVMBuildFence(LLVMBuilderRef B
, LLVMAtomicOrdering Ordering
,
4007 LLVMBool isSingleThread
, const char *Name
) {
4009 unwrap(B
)->CreateFence(mapFromLLVMOrdering(Ordering
),
4010 isSingleThread
? SyncScope::SingleThread
4011 : SyncScope::System
,
4015 LLVMValueRef
LLVMBuildFenceSyncScope(LLVMBuilderRef B
,
4016 LLVMAtomicOrdering Ordering
, unsigned SSID
,
4019 unwrap(B
)->CreateFence(mapFromLLVMOrdering(Ordering
), SSID
, Name
));
4022 LLVMValueRef
LLVMBuildGEP2(LLVMBuilderRef B
, LLVMTypeRef Ty
,
4023 LLVMValueRef Pointer
, LLVMValueRef
*Indices
,
4024 unsigned NumIndices
, const char *Name
) {
4025 ArrayRef
<Value
*> IdxList(unwrap(Indices
), NumIndices
);
4026 return wrap(unwrap(B
)->CreateGEP(unwrap(Ty
), unwrap(Pointer
), IdxList
, Name
));
4029 LLVMValueRef
LLVMBuildInBoundsGEP2(LLVMBuilderRef B
, LLVMTypeRef Ty
,
4030 LLVMValueRef Pointer
, LLVMValueRef
*Indices
,
4031 unsigned NumIndices
, const char *Name
) {
4032 ArrayRef
<Value
*> IdxList(unwrap(Indices
), NumIndices
);
4034 unwrap(B
)->CreateInBoundsGEP(unwrap(Ty
), unwrap(Pointer
), IdxList
, Name
));
4037 LLVMValueRef
LLVMBuildGEPWithNoWrapFlags(LLVMBuilderRef B
, LLVMTypeRef Ty
,
4038 LLVMValueRef Pointer
,
4039 LLVMValueRef
*Indices
,
4040 unsigned NumIndices
, const char *Name
,
4041 LLVMGEPNoWrapFlags NoWrapFlags
) {
4042 ArrayRef
<Value
*> IdxList(unwrap(Indices
), NumIndices
);
4043 return wrap(unwrap(B
)->CreateGEP(unwrap(Ty
), unwrap(Pointer
), IdxList
, Name
,
4044 mapFromLLVMGEPNoWrapFlags(NoWrapFlags
)));
4047 LLVMValueRef
LLVMBuildStructGEP2(LLVMBuilderRef B
, LLVMTypeRef Ty
,
4048 LLVMValueRef Pointer
, unsigned Idx
,
4051 unwrap(B
)->CreateStructGEP(unwrap(Ty
), unwrap(Pointer
), Idx
, Name
));
4054 LLVMValueRef
LLVMBuildGlobalString(LLVMBuilderRef B
, const char *Str
,
4056 return wrap(unwrap(B
)->CreateGlobalString(Str
, Name
));
4059 LLVMValueRef
LLVMBuildGlobalStringPtr(LLVMBuilderRef B
, const char *Str
,
4061 return wrap(unwrap(B
)->CreateGlobalString(Str
, Name
));
4064 LLVMBool
LLVMGetVolatile(LLVMValueRef MemAccessInst
) {
4065 Value
*P
= unwrap(MemAccessInst
);
4066 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
4067 return LI
->isVolatile();
4068 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(P
))
4069 return SI
->isVolatile();
4070 if (AtomicRMWInst
*AI
= dyn_cast
<AtomicRMWInst
>(P
))
4071 return AI
->isVolatile();
4072 return cast
<AtomicCmpXchgInst
>(P
)->isVolatile();
4075 void LLVMSetVolatile(LLVMValueRef MemAccessInst
, LLVMBool isVolatile
) {
4076 Value
*P
= unwrap(MemAccessInst
);
4077 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
4078 return LI
->setVolatile(isVolatile
);
4079 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(P
))
4080 return SI
->setVolatile(isVolatile
);
4081 if (AtomicRMWInst
*AI
= dyn_cast
<AtomicRMWInst
>(P
))
4082 return AI
->setVolatile(isVolatile
);
4083 return cast
<AtomicCmpXchgInst
>(P
)->setVolatile(isVolatile
);
4086 LLVMBool
LLVMGetWeak(LLVMValueRef CmpXchgInst
) {
4087 return unwrap
<AtomicCmpXchgInst
>(CmpXchgInst
)->isWeak();
4090 void LLVMSetWeak(LLVMValueRef CmpXchgInst
, LLVMBool isWeak
) {
4091 return unwrap
<AtomicCmpXchgInst
>(CmpXchgInst
)->setWeak(isWeak
);
4094 LLVMAtomicOrdering
LLVMGetOrdering(LLVMValueRef MemAccessInst
) {
4095 Value
*P
= unwrap(MemAccessInst
);
4097 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
4098 O
= LI
->getOrdering();
4099 else if (StoreInst
*SI
= dyn_cast
<StoreInst
>(P
))
4100 O
= SI
->getOrdering();
4101 else if (FenceInst
*FI
= dyn_cast
<FenceInst
>(P
))
4102 O
= FI
->getOrdering();
4104 O
= cast
<AtomicRMWInst
>(P
)->getOrdering();
4105 return mapToLLVMOrdering(O
);
4108 void LLVMSetOrdering(LLVMValueRef MemAccessInst
, LLVMAtomicOrdering Ordering
) {
4109 Value
*P
= unwrap(MemAccessInst
);
4110 AtomicOrdering O
= mapFromLLVMOrdering(Ordering
);
4112 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
4113 return LI
->setOrdering(O
);
4114 else if (FenceInst
*FI
= dyn_cast
<FenceInst
>(P
))
4115 return FI
->setOrdering(O
);
4116 else if (AtomicRMWInst
*ARWI
= dyn_cast
<AtomicRMWInst
>(P
))
4117 return ARWI
->setOrdering(O
);
4118 return cast
<StoreInst
>(P
)->setOrdering(O
);
4121 LLVMAtomicRMWBinOp
LLVMGetAtomicRMWBinOp(LLVMValueRef Inst
) {
4122 return mapToLLVMRMWBinOp(unwrap
<AtomicRMWInst
>(Inst
)->getOperation());
4125 void LLVMSetAtomicRMWBinOp(LLVMValueRef Inst
, LLVMAtomicRMWBinOp BinOp
) {
4126 unwrap
<AtomicRMWInst
>(Inst
)->setOperation(mapFromLLVMRMWBinOp(BinOp
));
4129 /*--.. Casts ...............................................................--*/
4131 LLVMValueRef
LLVMBuildTrunc(LLVMBuilderRef B
, LLVMValueRef Val
,
4132 LLVMTypeRef DestTy
, const char *Name
) {
4133 return wrap(unwrap(B
)->CreateTrunc(unwrap(Val
), unwrap(DestTy
), Name
));
4136 LLVMValueRef
LLVMBuildZExt(LLVMBuilderRef B
, LLVMValueRef Val
,
4137 LLVMTypeRef DestTy
, const char *Name
) {
4138 return wrap(unwrap(B
)->CreateZExt(unwrap(Val
), unwrap(DestTy
), Name
));
4141 LLVMValueRef
LLVMBuildSExt(LLVMBuilderRef B
, LLVMValueRef Val
,
4142 LLVMTypeRef DestTy
, const char *Name
) {
4143 return wrap(unwrap(B
)->CreateSExt(unwrap(Val
), unwrap(DestTy
), Name
));
4146 LLVMValueRef
LLVMBuildFPToUI(LLVMBuilderRef B
, LLVMValueRef Val
,
4147 LLVMTypeRef DestTy
, const char *Name
) {
4148 return wrap(unwrap(B
)->CreateFPToUI(unwrap(Val
), unwrap(DestTy
), Name
));
4151 LLVMValueRef
LLVMBuildFPToSI(LLVMBuilderRef B
, LLVMValueRef Val
,
4152 LLVMTypeRef DestTy
, const char *Name
) {
4153 return wrap(unwrap(B
)->CreateFPToSI(unwrap(Val
), unwrap(DestTy
), Name
));
4156 LLVMValueRef
LLVMBuildUIToFP(LLVMBuilderRef B
, LLVMValueRef Val
,
4157 LLVMTypeRef DestTy
, const char *Name
) {
4158 return wrap(unwrap(B
)->CreateUIToFP(unwrap(Val
), unwrap(DestTy
), Name
));
4161 LLVMValueRef
LLVMBuildSIToFP(LLVMBuilderRef B
, LLVMValueRef Val
,
4162 LLVMTypeRef DestTy
, const char *Name
) {
4163 return wrap(unwrap(B
)->CreateSIToFP(unwrap(Val
), unwrap(DestTy
), Name
));
4166 LLVMValueRef
LLVMBuildFPTrunc(LLVMBuilderRef B
, LLVMValueRef Val
,
4167 LLVMTypeRef DestTy
, const char *Name
) {
4168 return wrap(unwrap(B
)->CreateFPTrunc(unwrap(Val
), unwrap(DestTy
), Name
));
4171 LLVMValueRef
LLVMBuildFPExt(LLVMBuilderRef B
, LLVMValueRef Val
,
4172 LLVMTypeRef DestTy
, const char *Name
) {
4173 return wrap(unwrap(B
)->CreateFPExt(unwrap(Val
), unwrap(DestTy
), Name
));
4176 LLVMValueRef
LLVMBuildPtrToInt(LLVMBuilderRef B
, LLVMValueRef Val
,
4177 LLVMTypeRef DestTy
, const char *Name
) {
4178 return wrap(unwrap(B
)->CreatePtrToInt(unwrap(Val
), unwrap(DestTy
), Name
));
4181 LLVMValueRef
LLVMBuildIntToPtr(LLVMBuilderRef B
, LLVMValueRef Val
,
4182 LLVMTypeRef DestTy
, const char *Name
) {
4183 return wrap(unwrap(B
)->CreateIntToPtr(unwrap(Val
), unwrap(DestTy
), Name
));
4186 LLVMValueRef
LLVMBuildBitCast(LLVMBuilderRef B
, LLVMValueRef Val
,
4187 LLVMTypeRef DestTy
, const char *Name
) {
4188 return wrap(unwrap(B
)->CreateBitCast(unwrap(Val
), unwrap(DestTy
), Name
));
4191 LLVMValueRef
LLVMBuildAddrSpaceCast(LLVMBuilderRef B
, LLVMValueRef Val
,
4192 LLVMTypeRef DestTy
, const char *Name
) {
4193 return wrap(unwrap(B
)->CreateAddrSpaceCast(unwrap(Val
), unwrap(DestTy
), Name
));
4196 LLVMValueRef
LLVMBuildZExtOrBitCast(LLVMBuilderRef B
, LLVMValueRef Val
,
4197 LLVMTypeRef DestTy
, const char *Name
) {
4198 return wrap(unwrap(B
)->CreateZExtOrBitCast(unwrap(Val
), unwrap(DestTy
),
4202 LLVMValueRef
LLVMBuildSExtOrBitCast(LLVMBuilderRef B
, LLVMValueRef Val
,
4203 LLVMTypeRef DestTy
, const char *Name
) {
4204 return wrap(unwrap(B
)->CreateSExtOrBitCast(unwrap(Val
), unwrap(DestTy
),
4208 LLVMValueRef
LLVMBuildTruncOrBitCast(LLVMBuilderRef B
, LLVMValueRef Val
,
4209 LLVMTypeRef DestTy
, const char *Name
) {
4210 return wrap(unwrap(B
)->CreateTruncOrBitCast(unwrap(Val
), unwrap(DestTy
),
4214 LLVMValueRef
LLVMBuildCast(LLVMBuilderRef B
, LLVMOpcode Op
, LLVMValueRef Val
,
4215 LLVMTypeRef DestTy
, const char *Name
) {
4216 return wrap(unwrap(B
)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op
)), unwrap(Val
),
4217 unwrap(DestTy
), Name
));
4220 LLVMValueRef
LLVMBuildPointerCast(LLVMBuilderRef B
, LLVMValueRef Val
,
4221 LLVMTypeRef DestTy
, const char *Name
) {
4222 return wrap(unwrap(B
)->CreatePointerCast(unwrap(Val
), unwrap(DestTy
), Name
));
4225 LLVMValueRef
LLVMBuildIntCast2(LLVMBuilderRef B
, LLVMValueRef Val
,
4226 LLVMTypeRef DestTy
, LLVMBool IsSigned
,
4229 unwrap(B
)->CreateIntCast(unwrap(Val
), unwrap(DestTy
), IsSigned
, Name
));
4232 LLVMValueRef
LLVMBuildIntCast(LLVMBuilderRef B
, LLVMValueRef Val
,
4233 LLVMTypeRef DestTy
, const char *Name
) {
4234 return wrap(unwrap(B
)->CreateIntCast(unwrap(Val
), unwrap(DestTy
),
4235 /*isSigned*/true, Name
));
4238 LLVMValueRef
LLVMBuildFPCast(LLVMBuilderRef B
, LLVMValueRef Val
,
4239 LLVMTypeRef DestTy
, const char *Name
) {
4240 return wrap(unwrap(B
)->CreateFPCast(unwrap(Val
), unwrap(DestTy
), Name
));
4243 LLVMOpcode
LLVMGetCastOpcode(LLVMValueRef Src
, LLVMBool SrcIsSigned
,
4244 LLVMTypeRef DestTy
, LLVMBool DestIsSigned
) {
4245 return map_to_llvmopcode(CastInst::getCastOpcode(
4246 unwrap(Src
), SrcIsSigned
, unwrap(DestTy
), DestIsSigned
));
4249 /*--.. Comparisons .........................................................--*/
4251 LLVMValueRef
LLVMBuildICmp(LLVMBuilderRef B
, LLVMIntPredicate Op
,
4252 LLVMValueRef LHS
, LLVMValueRef RHS
,
4254 return wrap(unwrap(B
)->CreateICmp(static_cast<ICmpInst::Predicate
>(Op
),
4255 unwrap(LHS
), unwrap(RHS
), Name
));
4258 LLVMValueRef
LLVMBuildFCmp(LLVMBuilderRef B
, LLVMRealPredicate Op
,
4259 LLVMValueRef LHS
, LLVMValueRef RHS
,
4261 return wrap(unwrap(B
)->CreateFCmp(static_cast<FCmpInst::Predicate
>(Op
),
4262 unwrap(LHS
), unwrap(RHS
), Name
));
4265 /*--.. Miscellaneous instructions ..........................................--*/
4267 LLVMValueRef
LLVMBuildPhi(LLVMBuilderRef B
, LLVMTypeRef Ty
, const char *Name
) {
4268 return wrap(unwrap(B
)->CreatePHI(unwrap(Ty
), 0, Name
));
4271 LLVMValueRef
LLVMBuildCall2(LLVMBuilderRef B
, LLVMTypeRef Ty
, LLVMValueRef Fn
,
4272 LLVMValueRef
*Args
, unsigned NumArgs
,
4274 FunctionType
*FTy
= unwrap
<FunctionType
>(Ty
);
4275 return wrap(unwrap(B
)->CreateCall(FTy
, unwrap(Fn
),
4276 ArrayRef(unwrap(Args
), NumArgs
), Name
));
4280 LLVMBuildCallWithOperandBundles(LLVMBuilderRef B
, LLVMTypeRef Ty
,
4281 LLVMValueRef Fn
, LLVMValueRef
*Args
,
4282 unsigned NumArgs
, LLVMOperandBundleRef
*Bundles
,
4283 unsigned NumBundles
, const char *Name
) {
4284 FunctionType
*FTy
= unwrap
<FunctionType
>(Ty
);
4285 SmallVector
<OperandBundleDef
, 8> OBs
;
4286 for (auto *Bundle
: ArrayRef(Bundles
, NumBundles
)) {
4287 OperandBundleDef
*OB
= unwrap(Bundle
);
4290 return wrap(unwrap(B
)->CreateCall(
4291 FTy
, unwrap(Fn
), ArrayRef(unwrap(Args
), NumArgs
), OBs
, Name
));
4294 LLVMValueRef
LLVMBuildSelect(LLVMBuilderRef B
, LLVMValueRef If
,
4295 LLVMValueRef Then
, LLVMValueRef Else
,
4297 return wrap(unwrap(B
)->CreateSelect(unwrap(If
), unwrap(Then
), unwrap(Else
),
4301 LLVMValueRef
LLVMBuildVAArg(LLVMBuilderRef B
, LLVMValueRef List
,
4302 LLVMTypeRef Ty
, const char *Name
) {
4303 return wrap(unwrap(B
)->CreateVAArg(unwrap(List
), unwrap(Ty
), Name
));
4306 LLVMValueRef
LLVMBuildExtractElement(LLVMBuilderRef B
, LLVMValueRef VecVal
,
4307 LLVMValueRef Index
, const char *Name
) {
4308 return wrap(unwrap(B
)->CreateExtractElement(unwrap(VecVal
), unwrap(Index
),
4312 LLVMValueRef
LLVMBuildInsertElement(LLVMBuilderRef B
, LLVMValueRef VecVal
,
4313 LLVMValueRef EltVal
, LLVMValueRef Index
,
4315 return wrap(unwrap(B
)->CreateInsertElement(unwrap(VecVal
), unwrap(EltVal
),
4316 unwrap(Index
), Name
));
4319 LLVMValueRef
LLVMBuildShuffleVector(LLVMBuilderRef B
, LLVMValueRef V1
,
4320 LLVMValueRef V2
, LLVMValueRef Mask
,
4322 return wrap(unwrap(B
)->CreateShuffleVector(unwrap(V1
), unwrap(V2
),
4323 unwrap(Mask
), Name
));
4326 LLVMValueRef
LLVMBuildExtractValue(LLVMBuilderRef B
, LLVMValueRef AggVal
,
4327 unsigned Index
, const char *Name
) {
4328 return wrap(unwrap(B
)->CreateExtractValue(unwrap(AggVal
), Index
, Name
));
4331 LLVMValueRef
LLVMBuildInsertValue(LLVMBuilderRef B
, LLVMValueRef AggVal
,
4332 LLVMValueRef EltVal
, unsigned Index
,
4334 return wrap(unwrap(B
)->CreateInsertValue(unwrap(AggVal
), unwrap(EltVal
),
4338 LLVMValueRef
LLVMBuildFreeze(LLVMBuilderRef B
, LLVMValueRef Val
,
4340 return wrap(unwrap(B
)->CreateFreeze(unwrap(Val
), Name
));
4343 LLVMValueRef
LLVMBuildIsNull(LLVMBuilderRef B
, LLVMValueRef Val
,
4345 return wrap(unwrap(B
)->CreateIsNull(unwrap(Val
), Name
));
4348 LLVMValueRef
LLVMBuildIsNotNull(LLVMBuilderRef B
, LLVMValueRef Val
,
4350 return wrap(unwrap(B
)->CreateIsNotNull(unwrap(Val
), Name
));
4353 LLVMValueRef
LLVMBuildPtrDiff2(LLVMBuilderRef B
, LLVMTypeRef ElemTy
,
4354 LLVMValueRef LHS
, LLVMValueRef RHS
,
4356 return wrap(unwrap(B
)->CreatePtrDiff(unwrap(ElemTy
), unwrap(LHS
),
4357 unwrap(RHS
), Name
));
4360 LLVMValueRef
LLVMBuildAtomicRMW(LLVMBuilderRef B
,LLVMAtomicRMWBinOp op
,
4361 LLVMValueRef PTR
, LLVMValueRef Val
,
4362 LLVMAtomicOrdering ordering
,
4363 LLVMBool singleThread
) {
4364 AtomicRMWInst::BinOp intop
= mapFromLLVMRMWBinOp(op
);
4365 return wrap(unwrap(B
)->CreateAtomicRMW(
4366 intop
, unwrap(PTR
), unwrap(Val
), MaybeAlign(),
4367 mapFromLLVMOrdering(ordering
),
4368 singleThread
? SyncScope::SingleThread
: SyncScope::System
));
4371 LLVMValueRef
LLVMBuildAtomicRMWSyncScope(LLVMBuilderRef B
,
4372 LLVMAtomicRMWBinOp op
,
4373 LLVMValueRef PTR
, LLVMValueRef Val
,
4374 LLVMAtomicOrdering ordering
,
4376 AtomicRMWInst::BinOp intop
= mapFromLLVMRMWBinOp(op
);
4377 return wrap(unwrap(B
)->CreateAtomicRMW(intop
, unwrap(PTR
), unwrap(Val
),
4379 mapFromLLVMOrdering(ordering
), SSID
));
4382 LLVMValueRef
LLVMBuildAtomicCmpXchg(LLVMBuilderRef B
, LLVMValueRef Ptr
,
4383 LLVMValueRef Cmp
, LLVMValueRef New
,
4384 LLVMAtomicOrdering SuccessOrdering
,
4385 LLVMAtomicOrdering FailureOrdering
,
4386 LLVMBool singleThread
) {
4388 return wrap(unwrap(B
)->CreateAtomicCmpXchg(
4389 unwrap(Ptr
), unwrap(Cmp
), unwrap(New
), MaybeAlign(),
4390 mapFromLLVMOrdering(SuccessOrdering
),
4391 mapFromLLVMOrdering(FailureOrdering
),
4392 singleThread
? SyncScope::SingleThread
: SyncScope::System
));
4395 LLVMValueRef
LLVMBuildAtomicCmpXchgSyncScope(LLVMBuilderRef B
, LLVMValueRef Ptr
,
4396 LLVMValueRef Cmp
, LLVMValueRef New
,
4397 LLVMAtomicOrdering SuccessOrdering
,
4398 LLVMAtomicOrdering FailureOrdering
,
4400 return wrap(unwrap(B
)->CreateAtomicCmpXchg(
4401 unwrap(Ptr
), unwrap(Cmp
), unwrap(New
), MaybeAlign(),
4402 mapFromLLVMOrdering(SuccessOrdering
),
4403 mapFromLLVMOrdering(FailureOrdering
), SSID
));
4406 unsigned LLVMGetNumMaskElements(LLVMValueRef SVInst
) {
4407 Value
*P
= unwrap(SVInst
);
4408 ShuffleVectorInst
*I
= cast
<ShuffleVectorInst
>(P
);
4409 return I
->getShuffleMask().size();
4412 int LLVMGetMaskValue(LLVMValueRef SVInst
, unsigned Elt
) {
4413 Value
*P
= unwrap(SVInst
);
4414 ShuffleVectorInst
*I
= cast
<ShuffleVectorInst
>(P
);
4415 return I
->getMaskValue(Elt
);
4418 int LLVMGetUndefMaskElem(void) { return PoisonMaskElem
; }
4420 LLVMBool
LLVMIsAtomic(LLVMValueRef Inst
) {
4421 return unwrap
<Instruction
>(Inst
)->isAtomic();
4424 LLVMBool
LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst
) {
4425 // Backwards compatibility: return false for non-atomic instructions
4426 Instruction
*I
= unwrap
<Instruction
>(AtomicInst
);
4430 return *getAtomicSyncScopeID(I
) == SyncScope::SingleThread
;
4433 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst
, LLVMBool NewValue
) {
4434 // Backwards compatibility: ignore non-atomic instructions
4435 Instruction
*I
= unwrap
<Instruction
>(AtomicInst
);
4439 SyncScope::ID SSID
= NewValue
? SyncScope::SingleThread
: SyncScope::System
;
4440 setAtomicSyncScopeID(I
, SSID
);
4443 unsigned LLVMGetAtomicSyncScopeID(LLVMValueRef AtomicInst
) {
4444 Instruction
*I
= unwrap
<Instruction
>(AtomicInst
);
4445 assert(I
->isAtomic() && "Expected an atomic instruction");
4446 return *getAtomicSyncScopeID(I
);
4449 void LLVMSetAtomicSyncScopeID(LLVMValueRef AtomicInst
, unsigned SSID
) {
4450 Instruction
*I
= unwrap
<Instruction
>(AtomicInst
);
4451 assert(I
->isAtomic() && "Expected an atomic instruction");
4452 setAtomicSyncScopeID(I
, SSID
);
4455 LLVMAtomicOrdering
LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst
) {
4456 Value
*P
= unwrap(CmpXchgInst
);
4457 return mapToLLVMOrdering(cast
<AtomicCmpXchgInst
>(P
)->getSuccessOrdering());
4460 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst
,
4461 LLVMAtomicOrdering Ordering
) {
4462 Value
*P
= unwrap(CmpXchgInst
);
4463 AtomicOrdering O
= mapFromLLVMOrdering(Ordering
);
4465 return cast
<AtomicCmpXchgInst
>(P
)->setSuccessOrdering(O
);
4468 LLVMAtomicOrdering
LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst
) {
4469 Value
*P
= unwrap(CmpXchgInst
);
4470 return mapToLLVMOrdering(cast
<AtomicCmpXchgInst
>(P
)->getFailureOrdering());
4473 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst
,
4474 LLVMAtomicOrdering Ordering
) {
4475 Value
*P
= unwrap(CmpXchgInst
);
4476 AtomicOrdering O
= mapFromLLVMOrdering(Ordering
);
4478 return cast
<AtomicCmpXchgInst
>(P
)->setFailureOrdering(O
);
4481 /*===-- Module providers --------------------------------------------------===*/
4483 LLVMModuleProviderRef
4484 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M
) {
4485 return reinterpret_cast<LLVMModuleProviderRef
>(M
);
4488 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP
) {
4493 /*===-- Memory buffers ----------------------------------------------------===*/
4495 LLVMBool
LLVMCreateMemoryBufferWithContentsOfFile(
4497 LLVMMemoryBufferRef
*OutMemBuf
,
4498 char **OutMessage
) {
4500 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MBOrErr
= MemoryBuffer::getFile(Path
);
4501 if (std::error_code EC
= MBOrErr
.getError()) {
4502 *OutMessage
= strdup(EC
.message().c_str());
4505 *OutMemBuf
= wrap(MBOrErr
.get().release());
4509 LLVMBool
LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef
*OutMemBuf
,
4510 char **OutMessage
) {
4511 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MBOrErr
= MemoryBuffer::getSTDIN();
4512 if (std::error_code EC
= MBOrErr
.getError()) {
4513 *OutMessage
= strdup(EC
.message().c_str());
4516 *OutMemBuf
= wrap(MBOrErr
.get().release());
4520 LLVMMemoryBufferRef
LLVMCreateMemoryBufferWithMemoryRange(
4521 const char *InputData
,
4522 size_t InputDataLength
,
4523 const char *BufferName
,
4524 LLVMBool RequiresNullTerminator
) {
4526 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData
, InputDataLength
),
4527 StringRef(BufferName
),
4528 RequiresNullTerminator
).release());
4531 LLVMMemoryBufferRef
LLVMCreateMemoryBufferWithMemoryRangeCopy(
4532 const char *InputData
,
4533 size_t InputDataLength
,
4534 const char *BufferName
) {
4537 MemoryBuffer::getMemBufferCopy(StringRef(InputData
, InputDataLength
),
4538 StringRef(BufferName
)).release());
4541 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf
) {
4542 return unwrap(MemBuf
)->getBufferStart();
4545 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf
) {
4546 return unwrap(MemBuf
)->getBufferSize();
4549 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf
) {
4550 delete unwrap(MemBuf
);
4553 /*===-- Pass Manager ------------------------------------------------------===*/
4555 LLVMPassManagerRef
LLVMCreatePassManager() {
4556 return wrap(new legacy::PassManager());
4559 LLVMPassManagerRef
LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M
) {
4560 return wrap(new legacy::FunctionPassManager(unwrap(M
)));
4563 LLVMPassManagerRef
LLVMCreateFunctionPassManager(LLVMModuleProviderRef P
) {
4564 return LLVMCreateFunctionPassManagerForModule(
4565 reinterpret_cast<LLVMModuleRef
>(P
));
4568 LLVMBool
LLVMRunPassManager(LLVMPassManagerRef PM
, LLVMModuleRef M
) {
4569 return unwrap
<legacy::PassManager
>(PM
)->run(*unwrap(M
));
4572 LLVMBool
LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM
) {
4573 return unwrap
<legacy::FunctionPassManager
>(FPM
)->doInitialization();
4576 LLVMBool
LLVMRunFunctionPassManager(LLVMPassManagerRef FPM
, LLVMValueRef F
) {
4577 return unwrap
<legacy::FunctionPassManager
>(FPM
)->run(*unwrap
<Function
>(F
));
4580 LLVMBool
LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM
) {
4581 return unwrap
<legacy::FunctionPassManager
>(FPM
)->doFinalization();
4584 void LLVMDisposePassManager(LLVMPassManagerRef PM
) {
4588 /*===-- Threading ------------------------------------------------------===*/
4590 LLVMBool
LLVMStartMultithreaded() {
4591 return LLVMIsMultithreaded();
4594 void LLVMStopMultithreaded() {
4597 LLVMBool
LLVMIsMultithreaded() {
4598 return llvm_is_multithreaded();