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/IR/Attributes.h"
16 #include "llvm/IR/BasicBlock.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DebugInfoMetadata.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/IR/DiagnosticPrinter.h"
22 #include "llvm/IR/GlobalAlias.h"
23 #include "llvm/IR/GlobalVariable.h"
24 #include "llvm/IR/IRBuilder.h"
25 #include "llvm/IR/InlineAsm.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/LLVMContext.h"
28 #include "llvm/IR/LegacyPassManager.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/InitializePasses.h"
31 #include "llvm/PassRegistry.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/FileSystem.h"
35 #include "llvm/Support/ManagedStatic.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/Threading.h"
38 #include "llvm/Support/raw_ostream.h"
42 #include <system_error>
46 #define DEBUG_TYPE "ir"
48 void llvm::initializeCore(PassRegistry
&Registry
) {
49 initializeDominatorTreeWrapperPassPass(Registry
);
50 initializePrintModulePassWrapperPass(Registry
);
51 initializePrintFunctionPassWrapperPass(Registry
);
52 initializeSafepointIRVerifierPass(Registry
);
53 initializeVerifierLegacyPassPass(Registry
);
60 /*===-- Version query -----------------------------------------------------===*/
62 void LLVMGetVersion(unsigned *Major
, unsigned *Minor
, unsigned *Patch
) {
64 *Major
= LLVM_VERSION_MAJOR
;
66 *Minor
= LLVM_VERSION_MINOR
;
68 *Patch
= LLVM_VERSION_PATCH
;
71 /*===-- Error handling ----------------------------------------------------===*/
73 char *LLVMCreateMessage(const char *Message
) {
74 return strdup(Message
);
77 void LLVMDisposeMessage(char *Message
) {
82 /*===-- Operations on contexts --------------------------------------------===*/
84 static LLVMContext
&getGlobalContext() {
85 static LLVMContext GlobalContext
;
89 LLVMContextRef
LLVMContextCreate() {
90 return wrap(new LLVMContext());
93 LLVMContextRef
LLVMGetGlobalContext() { return wrap(&getGlobalContext()); }
95 void LLVMContextSetDiagnosticHandler(LLVMContextRef C
,
96 LLVMDiagnosticHandler Handler
,
97 void *DiagnosticContext
) {
98 unwrap(C
)->setDiagnosticHandlerCallBack(
99 LLVM_EXTENSION
reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy
>(
104 LLVMDiagnosticHandler
LLVMContextGetDiagnosticHandler(LLVMContextRef C
) {
105 return LLVM_EXTENSION
reinterpret_cast<LLVMDiagnosticHandler
>(
106 unwrap(C
)->getDiagnosticHandlerCallBack());
109 void *LLVMContextGetDiagnosticContext(LLVMContextRef C
) {
110 return unwrap(C
)->getDiagnosticContext();
113 void LLVMContextSetYieldCallback(LLVMContextRef C
, LLVMYieldCallback Callback
,
114 void *OpaqueHandle
) {
116 LLVM_EXTENSION
reinterpret_cast<LLVMContext::YieldCallbackTy
>(Callback
);
117 unwrap(C
)->setYieldCallback(YieldCallback
, OpaqueHandle
);
120 LLVMBool
LLVMContextShouldDiscardValueNames(LLVMContextRef C
) {
121 return unwrap(C
)->shouldDiscardValueNames();
124 void LLVMContextSetDiscardValueNames(LLVMContextRef C
, LLVMBool Discard
) {
125 unwrap(C
)->setDiscardValueNames(Discard
);
128 void LLVMContextDispose(LLVMContextRef C
) {
132 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C
, const char *Name
,
134 return unwrap(C
)->getMDKindID(StringRef(Name
, SLen
));
137 unsigned LLVMGetMDKindID(const char *Name
, unsigned SLen
) {
138 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name
, SLen
);
141 unsigned LLVMGetEnumAttributeKindForName(const char *Name
, size_t SLen
) {
142 return Attribute::getAttrKindFromName(StringRef(Name
, SLen
));
145 unsigned LLVMGetLastEnumAttributeKind(void) {
146 return Attribute::AttrKind::EndAttrKinds
;
149 LLVMAttributeRef
LLVMCreateEnumAttribute(LLVMContextRef C
, unsigned KindID
,
151 auto &Ctx
= *unwrap(C
);
152 auto AttrKind
= (Attribute::AttrKind
)KindID
;
153 return wrap(Attribute::get(Ctx
, AttrKind
, Val
));
156 unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A
) {
157 return unwrap(A
).getKindAsEnum();
160 uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A
) {
161 auto Attr
= unwrap(A
);
162 if (Attr
.isEnumAttribute())
164 return Attr
.getValueAsInt();
167 LLVMAttributeRef
LLVMCreateTypeAttribute(LLVMContextRef C
, unsigned KindID
,
168 LLVMTypeRef type_ref
) {
169 auto &Ctx
= *unwrap(C
);
170 auto AttrKind
= (Attribute::AttrKind
)KindID
;
171 return wrap(Attribute::get(Ctx
, AttrKind
, unwrap(type_ref
)));
174 LLVMTypeRef
LLVMGetTypeAttributeValue(LLVMAttributeRef A
) {
175 auto Attr
= unwrap(A
);
176 return wrap(Attr
.getValueAsType());
179 LLVMAttributeRef
LLVMCreateStringAttribute(LLVMContextRef C
,
180 const char *K
, unsigned KLength
,
181 const char *V
, unsigned VLength
) {
182 return wrap(Attribute::get(*unwrap(C
), StringRef(K
, KLength
),
183 StringRef(V
, VLength
)));
186 const char *LLVMGetStringAttributeKind(LLVMAttributeRef A
,
188 auto S
= unwrap(A
).getKindAsString();
193 const char *LLVMGetStringAttributeValue(LLVMAttributeRef A
,
195 auto S
= unwrap(A
).getValueAsString();
200 LLVMBool
LLVMIsEnumAttribute(LLVMAttributeRef A
) {
201 auto Attr
= unwrap(A
);
202 return Attr
.isEnumAttribute() || Attr
.isIntAttribute();
205 LLVMBool
LLVMIsStringAttribute(LLVMAttributeRef A
) {
206 return unwrap(A
).isStringAttribute();
209 LLVMBool
LLVMIsTypeAttribute(LLVMAttributeRef A
) {
210 return unwrap(A
).isTypeAttribute();
213 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI
) {
214 std::string MsgStorage
;
215 raw_string_ostream
Stream(MsgStorage
);
216 DiagnosticPrinterRawOStream
DP(Stream
);
218 unwrap(DI
)->print(DP
);
221 return LLVMCreateMessage(MsgStorage
.c_str());
224 LLVMDiagnosticSeverity
LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI
) {
225 LLVMDiagnosticSeverity severity
;
227 switch(unwrap(DI
)->getSeverity()) {
229 severity
= LLVMDSError
;
232 severity
= LLVMDSWarning
;
235 severity
= LLVMDSRemark
;
238 severity
= LLVMDSNote
;
245 /*===-- Operations on modules ---------------------------------------------===*/
247 LLVMModuleRef
LLVMModuleCreateWithName(const char *ModuleID
) {
248 return wrap(new Module(ModuleID
, getGlobalContext()));
251 LLVMModuleRef
LLVMModuleCreateWithNameInContext(const char *ModuleID
,
253 return wrap(new Module(ModuleID
, *unwrap(C
)));
256 void LLVMDisposeModule(LLVMModuleRef M
) {
260 const char *LLVMGetModuleIdentifier(LLVMModuleRef M
, size_t *Len
) {
261 auto &Str
= unwrap(M
)->getModuleIdentifier();
266 void LLVMSetModuleIdentifier(LLVMModuleRef M
, const char *Ident
, size_t Len
) {
267 unwrap(M
)->setModuleIdentifier(StringRef(Ident
, Len
));
270 const char *LLVMGetSourceFileName(LLVMModuleRef M
, size_t *Len
) {
271 auto &Str
= unwrap(M
)->getSourceFileName();
276 void LLVMSetSourceFileName(LLVMModuleRef M
, const char *Name
, size_t Len
) {
277 unwrap(M
)->setSourceFileName(StringRef(Name
, Len
));
280 /*--.. Data layout .........................................................--*/
281 const char *LLVMGetDataLayoutStr(LLVMModuleRef M
) {
282 return unwrap(M
)->getDataLayoutStr().c_str();
285 const char *LLVMGetDataLayout(LLVMModuleRef M
) {
286 return LLVMGetDataLayoutStr(M
);
289 void LLVMSetDataLayout(LLVMModuleRef M
, const char *DataLayoutStr
) {
290 unwrap(M
)->setDataLayout(DataLayoutStr
);
293 /*--.. Target triple .......................................................--*/
294 const char * LLVMGetTarget(LLVMModuleRef M
) {
295 return unwrap(M
)->getTargetTriple().c_str();
298 void LLVMSetTarget(LLVMModuleRef M
, const char *Triple
) {
299 unwrap(M
)->setTargetTriple(Triple
);
302 /*--.. Module flags ........................................................--*/
303 struct LLVMOpaqueModuleFlagEntry
{
304 LLVMModuleFlagBehavior Behavior
;
307 LLVMMetadataRef Metadata
;
310 static Module::ModFlagBehavior
311 map_to_llvmModFlagBehavior(LLVMModuleFlagBehavior Behavior
) {
313 case LLVMModuleFlagBehaviorError
:
314 return Module::ModFlagBehavior::Error
;
315 case LLVMModuleFlagBehaviorWarning
:
316 return Module::ModFlagBehavior::Warning
;
317 case LLVMModuleFlagBehaviorRequire
:
318 return Module::ModFlagBehavior::Require
;
319 case LLVMModuleFlagBehaviorOverride
:
320 return Module::ModFlagBehavior::Override
;
321 case LLVMModuleFlagBehaviorAppend
:
322 return Module::ModFlagBehavior::Append
;
323 case LLVMModuleFlagBehaviorAppendUnique
:
324 return Module::ModFlagBehavior::AppendUnique
;
326 llvm_unreachable("Unknown LLVMModuleFlagBehavior");
329 static LLVMModuleFlagBehavior
330 map_from_llvmModFlagBehavior(Module::ModFlagBehavior Behavior
) {
332 case Module::ModFlagBehavior::Error
:
333 return LLVMModuleFlagBehaviorError
;
334 case Module::ModFlagBehavior::Warning
:
335 return LLVMModuleFlagBehaviorWarning
;
336 case Module::ModFlagBehavior::Require
:
337 return LLVMModuleFlagBehaviorRequire
;
338 case Module::ModFlagBehavior::Override
:
339 return LLVMModuleFlagBehaviorOverride
;
340 case Module::ModFlagBehavior::Append
:
341 return LLVMModuleFlagBehaviorAppend
;
342 case Module::ModFlagBehavior::AppendUnique
:
343 return LLVMModuleFlagBehaviorAppendUnique
;
345 llvm_unreachable("Unhandled Flag Behavior");
349 LLVMModuleFlagEntry
*LLVMCopyModuleFlagsMetadata(LLVMModuleRef M
, size_t *Len
) {
350 SmallVector
<Module::ModuleFlagEntry
, 8> MFEs
;
351 unwrap(M
)->getModuleFlagsMetadata(MFEs
);
353 LLVMOpaqueModuleFlagEntry
*Result
= static_cast<LLVMOpaqueModuleFlagEntry
*>(
354 safe_malloc(MFEs
.size() * sizeof(LLVMOpaqueModuleFlagEntry
)));
355 for (unsigned i
= 0; i
< MFEs
.size(); ++i
) {
356 const auto &ModuleFlag
= MFEs
[i
];
357 Result
[i
].Behavior
= map_from_llvmModFlagBehavior(ModuleFlag
.Behavior
);
358 Result
[i
].Key
= ModuleFlag
.Key
->getString().data();
359 Result
[i
].KeyLen
= ModuleFlag
.Key
->getString().size();
360 Result
[i
].Metadata
= wrap(ModuleFlag
.Val
);
366 void LLVMDisposeModuleFlagsMetadata(LLVMModuleFlagEntry
*Entries
) {
370 LLVMModuleFlagBehavior
371 LLVMModuleFlagEntriesGetFlagBehavior(LLVMModuleFlagEntry
*Entries
,
373 LLVMOpaqueModuleFlagEntry MFE
=
374 static_cast<LLVMOpaqueModuleFlagEntry
>(Entries
[Index
]);
378 const char *LLVMModuleFlagEntriesGetKey(LLVMModuleFlagEntry
*Entries
,
379 unsigned Index
, size_t *Len
) {
380 LLVMOpaqueModuleFlagEntry MFE
=
381 static_cast<LLVMOpaqueModuleFlagEntry
>(Entries
[Index
]);
386 LLVMMetadataRef
LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry
*Entries
,
388 LLVMOpaqueModuleFlagEntry MFE
=
389 static_cast<LLVMOpaqueModuleFlagEntry
>(Entries
[Index
]);
393 LLVMMetadataRef
LLVMGetModuleFlag(LLVMModuleRef M
,
394 const char *Key
, size_t KeyLen
) {
395 return wrap(unwrap(M
)->getModuleFlag({Key
, KeyLen
}));
398 void LLVMAddModuleFlag(LLVMModuleRef M
, LLVMModuleFlagBehavior Behavior
,
399 const char *Key
, size_t KeyLen
,
400 LLVMMetadataRef Val
) {
401 unwrap(M
)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior
),
402 {Key
, KeyLen
}, unwrap(Val
));
405 /*--.. Printing modules ....................................................--*/
407 void LLVMDumpModule(LLVMModuleRef M
) {
408 unwrap(M
)->print(errs(), nullptr,
409 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
412 LLVMBool
LLVMPrintModuleToFile(LLVMModuleRef M
, const char *Filename
,
413 char **ErrorMessage
) {
415 raw_fd_ostream
dest(Filename
, EC
, sys::fs::OF_TextWithCRLF
);
417 *ErrorMessage
= strdup(EC
.message().c_str());
421 unwrap(M
)->print(dest
, nullptr);
425 if (dest
.has_error()) {
426 std::string E
= "Error printing to file: " + dest
.error().message();
427 *ErrorMessage
= strdup(E
.c_str());
434 char *LLVMPrintModuleToString(LLVMModuleRef M
) {
436 raw_string_ostream
os(buf
);
438 unwrap(M
)->print(os
, nullptr);
441 return strdup(buf
.c_str());
444 /*--.. Operations on inline assembler ......................................--*/
445 void LLVMSetModuleInlineAsm2(LLVMModuleRef M
, const char *Asm
, size_t Len
) {
446 unwrap(M
)->setModuleInlineAsm(StringRef(Asm
, Len
));
449 void LLVMSetModuleInlineAsm(LLVMModuleRef M
, const char *Asm
) {
450 unwrap(M
)->setModuleInlineAsm(StringRef(Asm
));
453 void LLVMAppendModuleInlineAsm(LLVMModuleRef M
, const char *Asm
, size_t Len
) {
454 unwrap(M
)->appendModuleInlineAsm(StringRef(Asm
, Len
));
457 const char *LLVMGetModuleInlineAsm(LLVMModuleRef M
, size_t *Len
) {
458 auto &Str
= unwrap(M
)->getModuleInlineAsm();
463 LLVMValueRef
LLVMGetInlineAsm(LLVMTypeRef Ty
, const char *AsmString
,
464 size_t AsmStringSize
, const char *Constraints
,
465 size_t ConstraintsSize
, LLVMBool HasSideEffects
,
466 LLVMBool IsAlignStack
,
467 LLVMInlineAsmDialect Dialect
, LLVMBool CanThrow
) {
468 InlineAsm::AsmDialect AD
;
470 case LLVMInlineAsmDialectATT
:
471 AD
= InlineAsm::AD_ATT
;
473 case LLVMInlineAsmDialectIntel
:
474 AD
= InlineAsm::AD_Intel
;
477 return wrap(InlineAsm::get(unwrap
<FunctionType
>(Ty
),
478 StringRef(AsmString
, AsmStringSize
),
479 StringRef(Constraints
, ConstraintsSize
),
480 HasSideEffects
, IsAlignStack
, AD
, CanThrow
));
483 const char *LLVMGetInlineAsmAsmString(LLVMValueRef InlineAsmVal
, size_t *Len
) {
485 Value
*Val
= unwrap
<Value
>(InlineAsmVal
);
486 const std::string
&AsmString
= cast
<InlineAsm
>(Val
)->getAsmString();
488 *Len
= AsmString
.length();
489 return AsmString
.c_str();
492 const char *LLVMGetInlineAsmConstraintString(LLVMValueRef InlineAsmVal
,
494 Value
*Val
= unwrap
<Value
>(InlineAsmVal
);
495 const std::string
&ConstraintString
=
496 cast
<InlineAsm
>(Val
)->getConstraintString();
498 *Len
= ConstraintString
.length();
499 return ConstraintString
.c_str();
502 LLVMInlineAsmDialect
LLVMGetInlineAsmDialect(LLVMValueRef InlineAsmVal
) {
504 Value
*Val
= unwrap
<Value
>(InlineAsmVal
);
505 InlineAsm::AsmDialect Dialect
= cast
<InlineAsm
>(Val
)->getDialect();
508 case InlineAsm::AD_ATT
:
509 return LLVMInlineAsmDialectATT
;
510 case InlineAsm::AD_Intel
:
511 return LLVMInlineAsmDialectIntel
;
514 llvm_unreachable("Unrecognized inline assembly dialect");
515 return LLVMInlineAsmDialectATT
;
518 LLVMTypeRef
LLVMGetInlineAsmFunctionType(LLVMValueRef InlineAsmVal
) {
519 Value
*Val
= unwrap
<Value
>(InlineAsmVal
);
520 return (LLVMTypeRef
)cast
<InlineAsm
>(Val
)->getFunctionType();
523 LLVMBool
LLVMGetInlineAsmHasSideEffects(LLVMValueRef InlineAsmVal
) {
524 Value
*Val
= unwrap
<Value
>(InlineAsmVal
);
525 return cast
<InlineAsm
>(Val
)->hasSideEffects();
528 LLVMBool
LLVMGetInlineAsmNeedsAlignedStack(LLVMValueRef InlineAsmVal
) {
529 Value
*Val
= unwrap
<Value
>(InlineAsmVal
);
530 return cast
<InlineAsm
>(Val
)->isAlignStack();
533 LLVMBool
LLVMGetInlineAsmCanUnwind(LLVMValueRef InlineAsmVal
) {
534 Value
*Val
= unwrap
<Value
>(InlineAsmVal
);
535 return cast
<InlineAsm
>(Val
)->canThrow();
538 /*--.. Operations on module contexts ......................................--*/
539 LLVMContextRef
LLVMGetModuleContext(LLVMModuleRef M
) {
540 return wrap(&unwrap(M
)->getContext());
544 /*===-- Operations on types -----------------------------------------------===*/
546 /*--.. Operations on all types (mostly) ....................................--*/
548 LLVMTypeKind
LLVMGetTypeKind(LLVMTypeRef Ty
) {
549 switch (unwrap(Ty
)->getTypeID()) {
551 return LLVMVoidTypeKind
;
553 return LLVMHalfTypeKind
;
554 case Type::BFloatTyID
:
555 return LLVMBFloatTypeKind
;
556 case Type::FloatTyID
:
557 return LLVMFloatTypeKind
;
558 case Type::DoubleTyID
:
559 return LLVMDoubleTypeKind
;
560 case Type::X86_FP80TyID
:
561 return LLVMX86_FP80TypeKind
;
562 case Type::FP128TyID
:
563 return LLVMFP128TypeKind
;
564 case Type::PPC_FP128TyID
:
565 return LLVMPPC_FP128TypeKind
;
566 case Type::LabelTyID
:
567 return LLVMLabelTypeKind
;
568 case Type::MetadataTyID
:
569 return LLVMMetadataTypeKind
;
570 case Type::IntegerTyID
:
571 return LLVMIntegerTypeKind
;
572 case Type::FunctionTyID
:
573 return LLVMFunctionTypeKind
;
574 case Type::StructTyID
:
575 return LLVMStructTypeKind
;
576 case Type::ArrayTyID
:
577 return LLVMArrayTypeKind
;
578 case Type::PointerTyID
:
579 return LLVMPointerTypeKind
;
580 case Type::FixedVectorTyID
:
581 return LLVMVectorTypeKind
;
582 case Type::X86_MMXTyID
:
583 return LLVMX86_MMXTypeKind
;
584 case Type::X86_AMXTyID
:
585 return LLVMX86_AMXTypeKind
;
586 case Type::TokenTyID
:
587 return LLVMTokenTypeKind
;
588 case Type::ScalableVectorTyID
:
589 return LLVMScalableVectorTypeKind
;
590 case Type::TargetExtTyID
:
591 return LLVMTargetExtTypeKind
;
592 case Type::TypedPointerTyID
:
593 llvm_unreachable("Typed pointers are unsupported via the C API");
595 llvm_unreachable("Unhandled TypeID.");
598 LLVMBool
LLVMTypeIsSized(LLVMTypeRef Ty
)
600 return unwrap(Ty
)->isSized();
603 LLVMContextRef
LLVMGetTypeContext(LLVMTypeRef Ty
) {
604 return wrap(&unwrap(Ty
)->getContext());
607 void LLVMDumpType(LLVMTypeRef Ty
) {
608 return unwrap(Ty
)->print(errs(), /*IsForDebug=*/true);
611 char *LLVMPrintTypeToString(LLVMTypeRef Ty
) {
613 raw_string_ostream
os(buf
);
616 unwrap(Ty
)->print(os
);
618 os
<< "Printing <null> Type";
622 return strdup(buf
.c_str());
625 /*--.. Operations on integer types .........................................--*/
627 LLVMTypeRef
LLVMInt1TypeInContext(LLVMContextRef C
) {
628 return (LLVMTypeRef
) Type::getInt1Ty(*unwrap(C
));
630 LLVMTypeRef
LLVMInt8TypeInContext(LLVMContextRef C
) {
631 return (LLVMTypeRef
) Type::getInt8Ty(*unwrap(C
));
633 LLVMTypeRef
LLVMInt16TypeInContext(LLVMContextRef C
) {
634 return (LLVMTypeRef
) Type::getInt16Ty(*unwrap(C
));
636 LLVMTypeRef
LLVMInt32TypeInContext(LLVMContextRef C
) {
637 return (LLVMTypeRef
) Type::getInt32Ty(*unwrap(C
));
639 LLVMTypeRef
LLVMInt64TypeInContext(LLVMContextRef C
) {
640 return (LLVMTypeRef
) Type::getInt64Ty(*unwrap(C
));
642 LLVMTypeRef
LLVMInt128TypeInContext(LLVMContextRef C
) {
643 return (LLVMTypeRef
) Type::getInt128Ty(*unwrap(C
));
645 LLVMTypeRef
LLVMIntTypeInContext(LLVMContextRef C
, unsigned NumBits
) {
646 return wrap(IntegerType::get(*unwrap(C
), NumBits
));
649 LLVMTypeRef
LLVMInt1Type(void) {
650 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
652 LLVMTypeRef
LLVMInt8Type(void) {
653 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
655 LLVMTypeRef
LLVMInt16Type(void) {
656 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
658 LLVMTypeRef
LLVMInt32Type(void) {
659 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
661 LLVMTypeRef
LLVMInt64Type(void) {
662 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
664 LLVMTypeRef
LLVMInt128Type(void) {
665 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
667 LLVMTypeRef
LLVMIntType(unsigned NumBits
) {
668 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits
);
671 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy
) {
672 return unwrap
<IntegerType
>(IntegerTy
)->getBitWidth();
675 /*--.. Operations on real types ............................................--*/
677 LLVMTypeRef
LLVMHalfTypeInContext(LLVMContextRef C
) {
678 return (LLVMTypeRef
) Type::getHalfTy(*unwrap(C
));
680 LLVMTypeRef
LLVMBFloatTypeInContext(LLVMContextRef C
) {
681 return (LLVMTypeRef
) Type::getBFloatTy(*unwrap(C
));
683 LLVMTypeRef
LLVMFloatTypeInContext(LLVMContextRef C
) {
684 return (LLVMTypeRef
) Type::getFloatTy(*unwrap(C
));
686 LLVMTypeRef
LLVMDoubleTypeInContext(LLVMContextRef C
) {
687 return (LLVMTypeRef
) Type::getDoubleTy(*unwrap(C
));
689 LLVMTypeRef
LLVMX86FP80TypeInContext(LLVMContextRef C
) {
690 return (LLVMTypeRef
) Type::getX86_FP80Ty(*unwrap(C
));
692 LLVMTypeRef
LLVMFP128TypeInContext(LLVMContextRef C
) {
693 return (LLVMTypeRef
) Type::getFP128Ty(*unwrap(C
));
695 LLVMTypeRef
LLVMPPCFP128TypeInContext(LLVMContextRef C
) {
696 return (LLVMTypeRef
) Type::getPPC_FP128Ty(*unwrap(C
));
698 LLVMTypeRef
LLVMX86MMXTypeInContext(LLVMContextRef C
) {
699 return (LLVMTypeRef
) Type::getX86_MMXTy(*unwrap(C
));
701 LLVMTypeRef
LLVMX86AMXTypeInContext(LLVMContextRef C
) {
702 return (LLVMTypeRef
) Type::getX86_AMXTy(*unwrap(C
));
705 LLVMTypeRef
LLVMHalfType(void) {
706 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
708 LLVMTypeRef
LLVMBFloatType(void) {
709 return LLVMBFloatTypeInContext(LLVMGetGlobalContext());
711 LLVMTypeRef
LLVMFloatType(void) {
712 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
714 LLVMTypeRef
LLVMDoubleType(void) {
715 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
717 LLVMTypeRef
LLVMX86FP80Type(void) {
718 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
720 LLVMTypeRef
LLVMFP128Type(void) {
721 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
723 LLVMTypeRef
LLVMPPCFP128Type(void) {
724 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
726 LLVMTypeRef
LLVMX86MMXType(void) {
727 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
729 LLVMTypeRef
LLVMX86AMXType(void) {
730 return LLVMX86AMXTypeInContext(LLVMGetGlobalContext());
733 /*--.. Operations on function types ........................................--*/
735 LLVMTypeRef
LLVMFunctionType(LLVMTypeRef ReturnType
,
736 LLVMTypeRef
*ParamTypes
, unsigned ParamCount
,
738 ArrayRef
<Type
*> Tys(unwrap(ParamTypes
), ParamCount
);
739 return wrap(FunctionType::get(unwrap(ReturnType
), Tys
, IsVarArg
!= 0));
742 LLVMBool
LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy
) {
743 return unwrap
<FunctionType
>(FunctionTy
)->isVarArg();
746 LLVMTypeRef
LLVMGetReturnType(LLVMTypeRef FunctionTy
) {
747 return wrap(unwrap
<FunctionType
>(FunctionTy
)->getReturnType());
750 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy
) {
751 return unwrap
<FunctionType
>(FunctionTy
)->getNumParams();
754 void LLVMGetParamTypes(LLVMTypeRef FunctionTy
, LLVMTypeRef
*Dest
) {
755 FunctionType
*Ty
= unwrap
<FunctionType
>(FunctionTy
);
756 for (Type
*T
: Ty
->params())
760 /*--.. Operations on struct types ..........................................--*/
762 LLVMTypeRef
LLVMStructTypeInContext(LLVMContextRef C
, LLVMTypeRef
*ElementTypes
,
763 unsigned ElementCount
, LLVMBool Packed
) {
764 ArrayRef
<Type
*> Tys(unwrap(ElementTypes
), ElementCount
);
765 return wrap(StructType::get(*unwrap(C
), Tys
, Packed
!= 0));
768 LLVMTypeRef
LLVMStructType(LLVMTypeRef
*ElementTypes
,
769 unsigned ElementCount
, LLVMBool Packed
) {
770 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes
,
771 ElementCount
, Packed
);
774 LLVMTypeRef
LLVMStructCreateNamed(LLVMContextRef C
, const char *Name
)
776 return wrap(StructType::create(*unwrap(C
), Name
));
779 const char *LLVMGetStructName(LLVMTypeRef Ty
)
781 StructType
*Type
= unwrap
<StructType
>(Ty
);
782 if (!Type
->hasName())
784 return Type
->getName().data();
787 void LLVMStructSetBody(LLVMTypeRef StructTy
, LLVMTypeRef
*ElementTypes
,
788 unsigned ElementCount
, LLVMBool Packed
) {
789 ArrayRef
<Type
*> Tys(unwrap(ElementTypes
), ElementCount
);
790 unwrap
<StructType
>(StructTy
)->setBody(Tys
, Packed
!= 0);
793 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy
) {
794 return unwrap
<StructType
>(StructTy
)->getNumElements();
797 void LLVMGetStructElementTypes(LLVMTypeRef StructTy
, LLVMTypeRef
*Dest
) {
798 StructType
*Ty
= unwrap
<StructType
>(StructTy
);
799 for (Type
*T
: Ty
->elements())
803 LLVMTypeRef
LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy
, unsigned i
) {
804 StructType
*Ty
= unwrap
<StructType
>(StructTy
);
805 return wrap(Ty
->getTypeAtIndex(i
));
808 LLVMBool
LLVMIsPackedStruct(LLVMTypeRef StructTy
) {
809 return unwrap
<StructType
>(StructTy
)->isPacked();
812 LLVMBool
LLVMIsOpaqueStruct(LLVMTypeRef StructTy
) {
813 return unwrap
<StructType
>(StructTy
)->isOpaque();
816 LLVMBool
LLVMIsLiteralStruct(LLVMTypeRef StructTy
) {
817 return unwrap
<StructType
>(StructTy
)->isLiteral();
820 LLVMTypeRef
LLVMGetTypeByName(LLVMModuleRef M
, const char *Name
) {
821 return wrap(StructType::getTypeByName(unwrap(M
)->getContext(), Name
));
824 LLVMTypeRef
LLVMGetTypeByName2(LLVMContextRef C
, const char *Name
) {
825 return wrap(StructType::getTypeByName(*unwrap(C
), Name
));
828 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
830 void LLVMGetSubtypes(LLVMTypeRef Tp
, LLVMTypeRef
*Arr
) {
832 for (auto *T
: unwrap(Tp
)->subtypes()) {
838 LLVMTypeRef
LLVMArrayType(LLVMTypeRef ElementType
, unsigned ElementCount
) {
839 return wrap(ArrayType::get(unwrap(ElementType
), ElementCount
));
842 LLVMTypeRef
LLVMArrayType2(LLVMTypeRef ElementType
, uint64_t ElementCount
) {
843 return wrap(ArrayType::get(unwrap(ElementType
), ElementCount
));
846 LLVMTypeRef
LLVMPointerType(LLVMTypeRef ElementType
, unsigned AddressSpace
) {
847 return wrap(PointerType::get(unwrap(ElementType
), AddressSpace
));
850 LLVMBool
LLVMPointerTypeIsOpaque(LLVMTypeRef Ty
) {
854 LLVMTypeRef
LLVMVectorType(LLVMTypeRef ElementType
, unsigned ElementCount
) {
855 return wrap(FixedVectorType::get(unwrap(ElementType
), ElementCount
));
858 LLVMTypeRef
LLVMScalableVectorType(LLVMTypeRef ElementType
,
859 unsigned ElementCount
) {
860 return wrap(ScalableVectorType::get(unwrap(ElementType
), ElementCount
));
863 LLVMTypeRef
LLVMGetElementType(LLVMTypeRef WrappedTy
) {
864 auto *Ty
= unwrap(WrappedTy
);
865 if (auto *ATy
= dyn_cast
<ArrayType
>(Ty
))
866 return wrap(ATy
->getElementType());
867 return wrap(cast
<VectorType
>(Ty
)->getElementType());
870 unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp
) {
871 return unwrap(Tp
)->getNumContainedTypes();
874 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy
) {
875 return unwrap
<ArrayType
>(ArrayTy
)->getNumElements();
878 uint64_t LLVMGetArrayLength2(LLVMTypeRef ArrayTy
) {
879 return unwrap
<ArrayType
>(ArrayTy
)->getNumElements();
882 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy
) {
883 return unwrap
<PointerType
>(PointerTy
)->getAddressSpace();
886 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy
) {
887 return unwrap
<VectorType
>(VectorTy
)->getElementCount().getKnownMinValue();
890 /*--.. Operations on other types ...........................................--*/
892 LLVMTypeRef
LLVMPointerTypeInContext(LLVMContextRef C
, unsigned AddressSpace
) {
893 return wrap(PointerType::get(*unwrap(C
), AddressSpace
));
896 LLVMTypeRef
LLVMVoidTypeInContext(LLVMContextRef C
) {
897 return wrap(Type::getVoidTy(*unwrap(C
)));
899 LLVMTypeRef
LLVMLabelTypeInContext(LLVMContextRef C
) {
900 return wrap(Type::getLabelTy(*unwrap(C
)));
902 LLVMTypeRef
LLVMTokenTypeInContext(LLVMContextRef C
) {
903 return wrap(Type::getTokenTy(*unwrap(C
)));
905 LLVMTypeRef
LLVMMetadataTypeInContext(LLVMContextRef C
) {
906 return wrap(Type::getMetadataTy(*unwrap(C
)));
909 LLVMTypeRef
LLVMVoidType(void) {
910 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
912 LLVMTypeRef
LLVMLabelType(void) {
913 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
916 LLVMTypeRef
LLVMTargetExtTypeInContext(LLVMContextRef C
, const char *Name
,
917 LLVMTypeRef
*TypeParams
,
918 unsigned TypeParamCount
,
920 unsigned IntParamCount
) {
921 ArrayRef
<Type
*> TypeParamArray(unwrap(TypeParams
), TypeParamCount
);
922 ArrayRef
<unsigned> IntParamArray(IntParams
, IntParamCount
);
924 TargetExtType::get(*unwrap(C
), Name
, TypeParamArray
, IntParamArray
));
927 /*===-- Operations on values ----------------------------------------------===*/
929 /*--.. Operations on all values ............................................--*/
931 LLVMTypeRef
LLVMTypeOf(LLVMValueRef Val
) {
932 return wrap(unwrap(Val
)->getType());
935 LLVMValueKind
LLVMGetValueKind(LLVMValueRef Val
) {
936 switch(unwrap(Val
)->getValueID()) {
938 #define HANDLE_VALUE(Name) \
939 case Value::Name##Val: \
940 return LLVM##Name##ValueKind;
941 #include "llvm/IR/Value.def"
943 return LLVMInstructionValueKind
;
947 const char *LLVMGetValueName2(LLVMValueRef Val
, size_t *Length
) {
948 auto *V
= unwrap(Val
);
949 *Length
= V
->getName().size();
950 return V
->getName().data();
953 void LLVMSetValueName2(LLVMValueRef Val
, const char *Name
, size_t NameLen
) {
954 unwrap(Val
)->setName(StringRef(Name
, NameLen
));
957 const char *LLVMGetValueName(LLVMValueRef Val
) {
958 return unwrap(Val
)->getName().data();
961 void LLVMSetValueName(LLVMValueRef Val
, const char *Name
) {
962 unwrap(Val
)->setName(Name
);
965 void LLVMDumpValue(LLVMValueRef Val
) {
966 unwrap(Val
)->print(errs(), /*IsForDebug=*/true);
969 char* LLVMPrintValueToString(LLVMValueRef Val
) {
971 raw_string_ostream
os(buf
);
974 unwrap(Val
)->print(os
);
976 os
<< "Printing <null> Value";
980 return strdup(buf
.c_str());
983 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal
, LLVMValueRef NewVal
) {
984 unwrap(OldVal
)->replaceAllUsesWith(unwrap(NewVal
));
987 int LLVMHasMetadata(LLVMValueRef Inst
) {
988 return unwrap
<Instruction
>(Inst
)->hasMetadata();
991 LLVMValueRef
LLVMGetMetadata(LLVMValueRef Inst
, unsigned KindID
) {
992 auto *I
= unwrap
<Instruction
>(Inst
);
993 assert(I
&& "Expected instruction");
994 if (auto *MD
= I
->getMetadata(KindID
))
995 return wrap(MetadataAsValue::get(I
->getContext(), MD
));
999 // MetadataAsValue uses a canonical format which strips the actual MDNode for
1000 // MDNode with just a single constant value, storing just a ConstantAsMetadata
1001 // This undoes this canonicalization, reconstructing the MDNode.
1002 static MDNode
*extractMDNode(MetadataAsValue
*MAV
) {
1003 Metadata
*MD
= MAV
->getMetadata();
1004 assert((isa
<MDNode
>(MD
) || isa
<ConstantAsMetadata
>(MD
)) &&
1005 "Expected a metadata node or a canonicalized constant");
1007 if (MDNode
*N
= dyn_cast
<MDNode
>(MD
))
1010 return MDNode::get(MAV
->getContext(), MD
);
1013 void LLVMSetMetadata(LLVMValueRef Inst
, unsigned KindID
, LLVMValueRef Val
) {
1014 MDNode
*N
= Val
? extractMDNode(unwrap
<MetadataAsValue
>(Val
)) : nullptr;
1016 unwrap
<Instruction
>(Inst
)->setMetadata(KindID
, N
);
1019 struct LLVMOpaqueValueMetadataEntry
{
1021 LLVMMetadataRef Metadata
;
1024 using MetadataEntries
= SmallVectorImpl
<std::pair
<unsigned, MDNode
*>>;
1025 static LLVMValueMetadataEntry
*
1026 llvm_getMetadata(size_t *NumEntries
,
1027 llvm::function_ref
<void(MetadataEntries
&)> AccessMD
) {
1028 SmallVector
<std::pair
<unsigned, MDNode
*>, 8> MVEs
;
1031 LLVMOpaqueValueMetadataEntry
*Result
=
1032 static_cast<LLVMOpaqueValueMetadataEntry
*>(
1033 safe_malloc(MVEs
.size() * sizeof(LLVMOpaqueValueMetadataEntry
)));
1034 for (unsigned i
= 0; i
< MVEs
.size(); ++i
) {
1035 const auto &ModuleFlag
= MVEs
[i
];
1036 Result
[i
].Kind
= ModuleFlag
.first
;
1037 Result
[i
].Metadata
= wrap(ModuleFlag
.second
);
1039 *NumEntries
= MVEs
.size();
1043 LLVMValueMetadataEntry
*
1044 LLVMInstructionGetAllMetadataOtherThanDebugLoc(LLVMValueRef Value
,
1045 size_t *NumEntries
) {
1046 return llvm_getMetadata(NumEntries
, [&Value
](MetadataEntries
&Entries
) {
1048 unwrap
<Instruction
>(Value
)->getAllMetadata(Entries
);
1052 /*--.. Conversion functions ................................................--*/
1054 #define LLVM_DEFINE_VALUE_CAST(name) \
1055 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
1056 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
1059 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST
)
1061 LLVMValueRef
LLVMIsAMDNode(LLVMValueRef Val
) {
1062 if (auto *MD
= dyn_cast_or_null
<MetadataAsValue
>(unwrap(Val
)))
1063 if (isa
<MDNode
>(MD
->getMetadata()) ||
1064 isa
<ValueAsMetadata
>(MD
->getMetadata()))
1069 LLVMValueRef
LLVMIsAValueAsMetadata(LLVMValueRef Val
) {
1070 if (auto *MD
= dyn_cast_or_null
<MetadataAsValue
>(unwrap(Val
)))
1071 if (isa
<ValueAsMetadata
>(MD
->getMetadata()))
1076 LLVMValueRef
LLVMIsAMDString(LLVMValueRef Val
) {
1077 if (auto *MD
= dyn_cast_or_null
<MetadataAsValue
>(unwrap(Val
)))
1078 if (isa
<MDString
>(MD
->getMetadata()))
1083 /*--.. Operations on Uses ..................................................--*/
1084 LLVMUseRef
LLVMGetFirstUse(LLVMValueRef Val
) {
1085 Value
*V
= unwrap(Val
);
1086 Value::use_iterator I
= V
->use_begin();
1087 if (I
== V
->use_end())
1092 LLVMUseRef
LLVMGetNextUse(LLVMUseRef U
) {
1093 Use
*Next
= unwrap(U
)->getNext();
1099 LLVMValueRef
LLVMGetUser(LLVMUseRef U
) {
1100 return wrap(unwrap(U
)->getUser());
1103 LLVMValueRef
LLVMGetUsedValue(LLVMUseRef U
) {
1104 return wrap(unwrap(U
)->get());
1107 /*--.. Operations on Users .................................................--*/
1109 static LLVMValueRef
getMDNodeOperandImpl(LLVMContext
&Context
, const MDNode
*N
,
1111 Metadata
*Op
= N
->getOperand(Index
);
1114 if (auto *C
= dyn_cast
<ConstantAsMetadata
>(Op
))
1115 return wrap(C
->getValue());
1116 return wrap(MetadataAsValue::get(Context
, Op
));
1119 LLVMValueRef
LLVMGetOperand(LLVMValueRef Val
, unsigned Index
) {
1120 Value
*V
= unwrap(Val
);
1121 if (auto *MD
= dyn_cast
<MetadataAsValue
>(V
)) {
1122 if (auto *L
= dyn_cast
<ValueAsMetadata
>(MD
->getMetadata())) {
1123 assert(Index
== 0 && "Function-local metadata can only have one operand");
1124 return wrap(L
->getValue());
1126 return getMDNodeOperandImpl(V
->getContext(),
1127 cast
<MDNode
>(MD
->getMetadata()), Index
);
1130 return wrap(cast
<User
>(V
)->getOperand(Index
));
1133 LLVMUseRef
LLVMGetOperandUse(LLVMValueRef Val
, unsigned Index
) {
1134 Value
*V
= unwrap(Val
);
1135 return wrap(&cast
<User
>(V
)->getOperandUse(Index
));
1138 void LLVMSetOperand(LLVMValueRef Val
, unsigned Index
, LLVMValueRef Op
) {
1139 unwrap
<User
>(Val
)->setOperand(Index
, unwrap(Op
));
1142 int LLVMGetNumOperands(LLVMValueRef Val
) {
1143 Value
*V
= unwrap(Val
);
1144 if (isa
<MetadataAsValue
>(V
))
1145 return LLVMGetMDNodeNumOperands(Val
);
1147 return cast
<User
>(V
)->getNumOperands();
1150 /*--.. Operations on constants of any type .................................--*/
1152 LLVMValueRef
LLVMConstNull(LLVMTypeRef Ty
) {
1153 return wrap(Constant::getNullValue(unwrap(Ty
)));
1156 LLVMValueRef
LLVMConstAllOnes(LLVMTypeRef Ty
) {
1157 return wrap(Constant::getAllOnesValue(unwrap(Ty
)));
1160 LLVMValueRef
LLVMGetUndef(LLVMTypeRef Ty
) {
1161 return wrap(UndefValue::get(unwrap(Ty
)));
1164 LLVMValueRef
LLVMGetPoison(LLVMTypeRef Ty
) {
1165 return wrap(PoisonValue::get(unwrap(Ty
)));
1168 LLVMBool
LLVMIsConstant(LLVMValueRef Ty
) {
1169 return isa
<Constant
>(unwrap(Ty
));
1172 LLVMBool
LLVMIsNull(LLVMValueRef Val
) {
1173 if (Constant
*C
= dyn_cast
<Constant
>(unwrap(Val
)))
1174 return C
->isNullValue();
1178 LLVMBool
LLVMIsUndef(LLVMValueRef Val
) {
1179 return isa
<UndefValue
>(unwrap(Val
));
1182 LLVMBool
LLVMIsPoison(LLVMValueRef Val
) {
1183 return isa
<PoisonValue
>(unwrap(Val
));
1186 LLVMValueRef
LLVMConstPointerNull(LLVMTypeRef Ty
) {
1187 return wrap(ConstantPointerNull::get(unwrap
<PointerType
>(Ty
)));
1190 /*--.. Operations on metadata nodes ........................................--*/
1192 LLVMMetadataRef
LLVMMDStringInContext2(LLVMContextRef C
, const char *Str
,
1194 return wrap(MDString::get(*unwrap(C
), StringRef(Str
, SLen
)));
1197 LLVMMetadataRef
LLVMMDNodeInContext2(LLVMContextRef C
, LLVMMetadataRef
*MDs
,
1199 return wrap(MDNode::get(*unwrap(C
), ArrayRef
<Metadata
*>(unwrap(MDs
), Count
)));
1202 LLVMValueRef
LLVMMDStringInContext(LLVMContextRef C
, const char *Str
,
1204 LLVMContext
&Context
= *unwrap(C
);
1205 return wrap(MetadataAsValue::get(
1206 Context
, MDString::get(Context
, StringRef(Str
, SLen
))));
1209 LLVMValueRef
LLVMMDString(const char *Str
, unsigned SLen
) {
1210 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str
, SLen
);
1213 LLVMValueRef
LLVMMDNodeInContext(LLVMContextRef C
, LLVMValueRef
*Vals
,
1215 LLVMContext
&Context
= *unwrap(C
);
1216 SmallVector
<Metadata
*, 8> MDs
;
1217 for (auto *OV
: ArrayRef(Vals
, Count
)) {
1218 Value
*V
= unwrap(OV
);
1222 else if (auto *C
= dyn_cast
<Constant
>(V
))
1223 MD
= ConstantAsMetadata::get(C
);
1224 else if (auto *MDV
= dyn_cast
<MetadataAsValue
>(V
)) {
1225 MD
= MDV
->getMetadata();
1226 assert(!isa
<LocalAsMetadata
>(MD
) && "Unexpected function-local metadata "
1227 "outside of direct argument to call");
1229 // This is function-local metadata. Pretend to make an MDNode.
1230 assert(Count
== 1 &&
1231 "Expected only one operand to function-local metadata");
1232 return wrap(MetadataAsValue::get(Context
, LocalAsMetadata::get(V
)));
1237 return wrap(MetadataAsValue::get(Context
, MDNode::get(Context
, MDs
)));
1240 LLVMValueRef
LLVMMDNode(LLVMValueRef
*Vals
, unsigned Count
) {
1241 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals
, Count
);
1244 LLVMValueRef
LLVMMetadataAsValue(LLVMContextRef C
, LLVMMetadataRef MD
) {
1245 return wrap(MetadataAsValue::get(*unwrap(C
), unwrap(MD
)));
1248 LLVMMetadataRef
LLVMValueAsMetadata(LLVMValueRef Val
) {
1249 auto *V
= unwrap(Val
);
1250 if (auto *C
= dyn_cast
<Constant
>(V
))
1251 return wrap(ConstantAsMetadata::get(C
));
1252 if (auto *MAV
= dyn_cast
<MetadataAsValue
>(V
))
1253 return wrap(MAV
->getMetadata());
1254 return wrap(ValueAsMetadata::get(V
));
1257 const char *LLVMGetMDString(LLVMValueRef V
, unsigned *Length
) {
1258 if (const auto *MD
= dyn_cast
<MetadataAsValue
>(unwrap(V
)))
1259 if (const MDString
*S
= dyn_cast
<MDString
>(MD
->getMetadata())) {
1260 *Length
= S
->getString().size();
1261 return S
->getString().data();
1267 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V
) {
1268 auto *MD
= unwrap
<MetadataAsValue
>(V
);
1269 if (isa
<ValueAsMetadata
>(MD
->getMetadata()))
1271 return cast
<MDNode
>(MD
->getMetadata())->getNumOperands();
1274 LLVMNamedMDNodeRef
LLVMGetFirstNamedMetadata(LLVMModuleRef M
) {
1275 Module
*Mod
= unwrap(M
);
1276 Module::named_metadata_iterator I
= Mod
->named_metadata_begin();
1277 if (I
== Mod
->named_metadata_end())
1282 LLVMNamedMDNodeRef
LLVMGetLastNamedMetadata(LLVMModuleRef M
) {
1283 Module
*Mod
= unwrap(M
);
1284 Module::named_metadata_iterator I
= Mod
->named_metadata_end();
1285 if (I
== Mod
->named_metadata_begin())
1290 LLVMNamedMDNodeRef
LLVMGetNextNamedMetadata(LLVMNamedMDNodeRef NMD
) {
1291 NamedMDNode
*NamedNode
= unwrap(NMD
);
1292 Module::named_metadata_iterator
I(NamedNode
);
1293 if (++I
== NamedNode
->getParent()->named_metadata_end())
1298 LLVMNamedMDNodeRef
LLVMGetPreviousNamedMetadata(LLVMNamedMDNodeRef NMD
) {
1299 NamedMDNode
*NamedNode
= unwrap(NMD
);
1300 Module::named_metadata_iterator
I(NamedNode
);
1301 if (I
== NamedNode
->getParent()->named_metadata_begin())
1306 LLVMNamedMDNodeRef
LLVMGetNamedMetadata(LLVMModuleRef M
,
1307 const char *Name
, size_t NameLen
) {
1308 return wrap(unwrap(M
)->getNamedMetadata(StringRef(Name
, NameLen
)));
1311 LLVMNamedMDNodeRef
LLVMGetOrInsertNamedMetadata(LLVMModuleRef M
,
1312 const char *Name
, size_t NameLen
) {
1313 return wrap(unwrap(M
)->getOrInsertNamedMetadata({Name
, NameLen
}));
1316 const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD
, size_t *NameLen
) {
1317 NamedMDNode
*NamedNode
= unwrap(NMD
);
1318 *NameLen
= NamedNode
->getName().size();
1319 return NamedNode
->getName().data();
1322 void LLVMGetMDNodeOperands(LLVMValueRef V
, LLVMValueRef
*Dest
) {
1323 auto *MD
= unwrap
<MetadataAsValue
>(V
);
1324 if (auto *MDV
= dyn_cast
<ValueAsMetadata
>(MD
->getMetadata())) {
1325 *Dest
= wrap(MDV
->getValue());
1328 const auto *N
= cast
<MDNode
>(MD
->getMetadata());
1329 const unsigned numOperands
= N
->getNumOperands();
1330 LLVMContext
&Context
= unwrap(V
)->getContext();
1331 for (unsigned i
= 0; i
< numOperands
; i
++)
1332 Dest
[i
] = getMDNodeOperandImpl(Context
, N
, i
);
1335 void LLVMReplaceMDNodeOperandWith(LLVMValueRef V
, unsigned Index
,
1336 LLVMMetadataRef Replacement
) {
1337 auto *MD
= cast
<MetadataAsValue
>(unwrap(V
));
1338 auto *N
= cast
<MDNode
>(MD
->getMetadata());
1339 N
->replaceOperandWith(Index
, unwrap
<Metadata
>(Replacement
));
1342 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M
, const char *Name
) {
1343 if (NamedMDNode
*N
= unwrap(M
)->getNamedMetadata(Name
)) {
1344 return N
->getNumOperands();
1349 void LLVMGetNamedMetadataOperands(LLVMModuleRef M
, const char *Name
,
1350 LLVMValueRef
*Dest
) {
1351 NamedMDNode
*N
= unwrap(M
)->getNamedMetadata(Name
);
1354 LLVMContext
&Context
= unwrap(M
)->getContext();
1355 for (unsigned i
=0;i
<N
->getNumOperands();i
++)
1356 Dest
[i
] = wrap(MetadataAsValue::get(Context
, N
->getOperand(i
)));
1359 void LLVMAddNamedMetadataOperand(LLVMModuleRef M
, const char *Name
,
1361 NamedMDNode
*N
= unwrap(M
)->getOrInsertNamedMetadata(Name
);
1366 N
->addOperand(extractMDNode(unwrap
<MetadataAsValue
>(Val
)));
1369 const char *LLVMGetDebugLocDirectory(LLVMValueRef Val
, unsigned *Length
) {
1370 if (!Length
) return nullptr;
1372 if (const auto *I
= dyn_cast
<Instruction
>(unwrap(Val
))) {
1373 if (const auto &DL
= I
->getDebugLoc()) {
1374 S
= DL
->getDirectory();
1376 } else if (const auto *GV
= dyn_cast
<GlobalVariable
>(unwrap(Val
))) {
1377 SmallVector
<DIGlobalVariableExpression
*, 1> GVEs
;
1378 GV
->getDebugInfo(GVEs
);
1380 if (const DIGlobalVariable
*DGV
= GVEs
[0]->getVariable())
1381 S
= DGV
->getDirectory();
1382 } else if (const auto *F
= dyn_cast
<Function
>(unwrap(Val
))) {
1383 if (const DISubprogram
*DSP
= F
->getSubprogram())
1384 S
= DSP
->getDirectory();
1386 assert(0 && "Expected Instruction, GlobalVariable or Function");
1393 const char *LLVMGetDebugLocFilename(LLVMValueRef Val
, unsigned *Length
) {
1394 if (!Length
) return nullptr;
1396 if (const auto *I
= dyn_cast
<Instruction
>(unwrap(Val
))) {
1397 if (const auto &DL
= I
->getDebugLoc()) {
1398 S
= DL
->getFilename();
1400 } else if (const auto *GV
= dyn_cast
<GlobalVariable
>(unwrap(Val
))) {
1401 SmallVector
<DIGlobalVariableExpression
*, 1> GVEs
;
1402 GV
->getDebugInfo(GVEs
);
1404 if (const DIGlobalVariable
*DGV
= GVEs
[0]->getVariable())
1405 S
= DGV
->getFilename();
1406 } else if (const auto *F
= dyn_cast
<Function
>(unwrap(Val
))) {
1407 if (const DISubprogram
*DSP
= F
->getSubprogram())
1408 S
= DSP
->getFilename();
1410 assert(0 && "Expected Instruction, GlobalVariable or Function");
1417 unsigned LLVMGetDebugLocLine(LLVMValueRef Val
) {
1419 if (const auto *I
= dyn_cast
<Instruction
>(unwrap(Val
))) {
1420 if (const auto &DL
= I
->getDebugLoc()) {
1423 } else if (const auto *GV
= dyn_cast
<GlobalVariable
>(unwrap(Val
))) {
1424 SmallVector
<DIGlobalVariableExpression
*, 1> GVEs
;
1425 GV
->getDebugInfo(GVEs
);
1427 if (const DIGlobalVariable
*DGV
= GVEs
[0]->getVariable())
1429 } else if (const auto *F
= dyn_cast
<Function
>(unwrap(Val
))) {
1430 if (const DISubprogram
*DSP
= F
->getSubprogram())
1433 assert(0 && "Expected Instruction, GlobalVariable or Function");
1439 unsigned LLVMGetDebugLocColumn(LLVMValueRef Val
) {
1441 if (const auto *I
= dyn_cast
<Instruction
>(unwrap(Val
)))
1442 if (const auto &DL
= I
->getDebugLoc())
1443 C
= DL
->getColumn();
1447 /*--.. Operations on scalar constants ......................................--*/
1449 LLVMValueRef
LLVMConstInt(LLVMTypeRef IntTy
, unsigned long long N
,
1450 LLVMBool SignExtend
) {
1451 return wrap(ConstantInt::get(unwrap
<IntegerType
>(IntTy
), N
, SignExtend
!= 0));
1454 LLVMValueRef
LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy
,
1456 const uint64_t Words
[]) {
1457 IntegerType
*Ty
= unwrap
<IntegerType
>(IntTy
);
1458 return wrap(ConstantInt::get(
1459 Ty
->getContext(), APInt(Ty
->getBitWidth(), ArrayRef(Words
, NumWords
))));
1462 LLVMValueRef
LLVMConstIntOfString(LLVMTypeRef IntTy
, const char Str
[],
1464 return wrap(ConstantInt::get(unwrap
<IntegerType
>(IntTy
), StringRef(Str
),
1468 LLVMValueRef
LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy
, const char Str
[],
1469 unsigned SLen
, uint8_t Radix
) {
1470 return wrap(ConstantInt::get(unwrap
<IntegerType
>(IntTy
), StringRef(Str
, SLen
),
1474 LLVMValueRef
LLVMConstReal(LLVMTypeRef RealTy
, double N
) {
1475 return wrap(ConstantFP::get(unwrap(RealTy
), N
));
1478 LLVMValueRef
LLVMConstRealOfString(LLVMTypeRef RealTy
, const char *Text
) {
1479 return wrap(ConstantFP::get(unwrap(RealTy
), StringRef(Text
)));
1482 LLVMValueRef
LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy
, const char Str
[],
1484 return wrap(ConstantFP::get(unwrap(RealTy
), StringRef(Str
, SLen
)));
1487 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal
) {
1488 return unwrap
<ConstantInt
>(ConstantVal
)->getZExtValue();
1491 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal
) {
1492 return unwrap
<ConstantInt
>(ConstantVal
)->getSExtValue();
1495 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal
, LLVMBool
*LosesInfo
) {
1496 ConstantFP
*cFP
= unwrap
<ConstantFP
>(ConstantVal
) ;
1497 Type
*Ty
= cFP
->getType();
1499 if (Ty
->isHalfTy() || Ty
->isBFloatTy() || Ty
->isFloatTy() ||
1502 return cFP
->getValueAPF().convertToDouble();
1506 APFloat APF
= cFP
->getValueAPF();
1507 APF
.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven
, &APFLosesInfo
);
1508 *LosesInfo
= APFLosesInfo
;
1509 return APF
.convertToDouble();
1512 /*--.. Operations on composite constants ...................................--*/
1514 LLVMValueRef
LLVMConstStringInContext(LLVMContextRef C
, const char *Str
,
1516 LLVMBool DontNullTerminate
) {
1517 /* Inverted the sense of AddNull because ', 0)' is a
1518 better mnemonic for null termination than ', 1)'. */
1519 return wrap(ConstantDataArray::getString(*unwrap(C
), StringRef(Str
, Length
),
1520 DontNullTerminate
== 0));
1523 LLVMValueRef
LLVMConstString(const char *Str
, unsigned Length
,
1524 LLVMBool DontNullTerminate
) {
1525 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str
, Length
,
1529 LLVMValueRef
LLVMGetAggregateElement(LLVMValueRef C
, unsigned Idx
) {
1530 return wrap(unwrap
<Constant
>(C
)->getAggregateElement(Idx
));
1533 LLVMValueRef
LLVMGetElementAsConstant(LLVMValueRef C
, unsigned idx
) {
1534 return wrap(unwrap
<ConstantDataSequential
>(C
)->getElementAsConstant(idx
));
1537 LLVMBool
LLVMIsConstantString(LLVMValueRef C
) {
1538 return unwrap
<ConstantDataSequential
>(C
)->isString();
1541 const char *LLVMGetAsString(LLVMValueRef C
, size_t *Length
) {
1542 StringRef Str
= unwrap
<ConstantDataSequential
>(C
)->getAsString();
1543 *Length
= Str
.size();
1547 LLVMValueRef
LLVMConstArray(LLVMTypeRef ElementTy
,
1548 LLVMValueRef
*ConstantVals
, unsigned Length
) {
1549 ArrayRef
<Constant
*> V(unwrap
<Constant
>(ConstantVals
, Length
), Length
);
1550 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy
), Length
), V
));
1553 LLVMValueRef
LLVMConstArray2(LLVMTypeRef ElementTy
, LLVMValueRef
*ConstantVals
,
1555 ArrayRef
<Constant
*> V(unwrap
<Constant
>(ConstantVals
, Length
), Length
);
1556 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy
), Length
), V
));
1559 LLVMValueRef
LLVMConstStructInContext(LLVMContextRef C
,
1560 LLVMValueRef
*ConstantVals
,
1561 unsigned Count
, LLVMBool Packed
) {
1562 Constant
**Elements
= unwrap
<Constant
>(ConstantVals
, Count
);
1563 return wrap(ConstantStruct::getAnon(*unwrap(C
), ArrayRef(Elements
, Count
),
1567 LLVMValueRef
LLVMConstStruct(LLVMValueRef
*ConstantVals
, unsigned Count
,
1569 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals
, Count
,
1573 LLVMValueRef
LLVMConstNamedStruct(LLVMTypeRef StructTy
,
1574 LLVMValueRef
*ConstantVals
,
1576 Constant
**Elements
= unwrap
<Constant
>(ConstantVals
, Count
);
1577 StructType
*Ty
= unwrap
<StructType
>(StructTy
);
1579 return wrap(ConstantStruct::get(Ty
, ArrayRef(Elements
, Count
)));
1582 LLVMValueRef
LLVMConstVector(LLVMValueRef
*ScalarConstantVals
, unsigned Size
) {
1583 return wrap(ConstantVector::get(
1584 ArrayRef(unwrap
<Constant
>(ScalarConstantVals
, Size
), Size
)));
1587 /*-- Opcode mapping */
1589 static LLVMOpcode
map_to_llvmopcode(int opcode
)
1592 default: llvm_unreachable("Unhandled Opcode.");
1593 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1594 #include "llvm/IR/Instruction.def"
1599 static int map_from_llvmopcode(LLVMOpcode code
)
1602 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1603 #include "llvm/IR/Instruction.def"
1606 llvm_unreachable("Unhandled Opcode.");
1609 /*--.. Constant expressions ................................................--*/
1611 LLVMOpcode
LLVMGetConstOpcode(LLVMValueRef ConstantVal
) {
1612 return map_to_llvmopcode(unwrap
<ConstantExpr
>(ConstantVal
)->getOpcode());
1615 LLVMValueRef
LLVMAlignOf(LLVMTypeRef Ty
) {
1616 return wrap(ConstantExpr::getAlignOf(unwrap(Ty
)));
1619 LLVMValueRef
LLVMSizeOf(LLVMTypeRef Ty
) {
1620 return wrap(ConstantExpr::getSizeOf(unwrap(Ty
)));
1623 LLVMValueRef
LLVMConstNeg(LLVMValueRef ConstantVal
) {
1624 return wrap(ConstantExpr::getNeg(unwrap
<Constant
>(ConstantVal
)));
1627 LLVMValueRef
LLVMConstNSWNeg(LLVMValueRef ConstantVal
) {
1628 return wrap(ConstantExpr::getNSWNeg(unwrap
<Constant
>(ConstantVal
)));
1631 LLVMValueRef
LLVMConstNUWNeg(LLVMValueRef ConstantVal
) {
1632 return wrap(ConstantExpr::getNUWNeg(unwrap
<Constant
>(ConstantVal
)));
1636 LLVMValueRef
LLVMConstNot(LLVMValueRef ConstantVal
) {
1637 return wrap(ConstantExpr::getNot(unwrap
<Constant
>(ConstantVal
)));
1640 LLVMValueRef
LLVMConstAdd(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1641 return wrap(ConstantExpr::getAdd(unwrap
<Constant
>(LHSConstant
),
1642 unwrap
<Constant
>(RHSConstant
)));
1645 LLVMValueRef
LLVMConstNSWAdd(LLVMValueRef LHSConstant
,
1646 LLVMValueRef RHSConstant
) {
1647 return wrap(ConstantExpr::getNSWAdd(unwrap
<Constant
>(LHSConstant
),
1648 unwrap
<Constant
>(RHSConstant
)));
1651 LLVMValueRef
LLVMConstNUWAdd(LLVMValueRef LHSConstant
,
1652 LLVMValueRef RHSConstant
) {
1653 return wrap(ConstantExpr::getNUWAdd(unwrap
<Constant
>(LHSConstant
),
1654 unwrap
<Constant
>(RHSConstant
)));
1657 LLVMValueRef
LLVMConstSub(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1658 return wrap(ConstantExpr::getSub(unwrap
<Constant
>(LHSConstant
),
1659 unwrap
<Constant
>(RHSConstant
)));
1662 LLVMValueRef
LLVMConstNSWSub(LLVMValueRef LHSConstant
,
1663 LLVMValueRef RHSConstant
) {
1664 return wrap(ConstantExpr::getNSWSub(unwrap
<Constant
>(LHSConstant
),
1665 unwrap
<Constant
>(RHSConstant
)));
1668 LLVMValueRef
LLVMConstNUWSub(LLVMValueRef LHSConstant
,
1669 LLVMValueRef RHSConstant
) {
1670 return wrap(ConstantExpr::getNUWSub(unwrap
<Constant
>(LHSConstant
),
1671 unwrap
<Constant
>(RHSConstant
)));
1674 LLVMValueRef
LLVMConstMul(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1675 return wrap(ConstantExpr::getMul(unwrap
<Constant
>(LHSConstant
),
1676 unwrap
<Constant
>(RHSConstant
)));
1679 LLVMValueRef
LLVMConstNSWMul(LLVMValueRef LHSConstant
,
1680 LLVMValueRef RHSConstant
) {
1681 return wrap(ConstantExpr::getNSWMul(unwrap
<Constant
>(LHSConstant
),
1682 unwrap
<Constant
>(RHSConstant
)));
1685 LLVMValueRef
LLVMConstNUWMul(LLVMValueRef LHSConstant
,
1686 LLVMValueRef RHSConstant
) {
1687 return wrap(ConstantExpr::getNUWMul(unwrap
<Constant
>(LHSConstant
),
1688 unwrap
<Constant
>(RHSConstant
)));
1691 LLVMValueRef
LLVMConstXor(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1692 return wrap(ConstantExpr::getXor(unwrap
<Constant
>(LHSConstant
),
1693 unwrap
<Constant
>(RHSConstant
)));
1696 LLVMValueRef
LLVMConstICmp(LLVMIntPredicate Predicate
,
1697 LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1698 return wrap(ConstantExpr::getICmp(Predicate
,
1699 unwrap
<Constant
>(LHSConstant
),
1700 unwrap
<Constant
>(RHSConstant
)));
1703 LLVMValueRef
LLVMConstFCmp(LLVMRealPredicate Predicate
,
1704 LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1705 return wrap(ConstantExpr::getFCmp(Predicate
,
1706 unwrap
<Constant
>(LHSConstant
),
1707 unwrap
<Constant
>(RHSConstant
)));
1710 LLVMValueRef
LLVMConstShl(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1711 return wrap(ConstantExpr::getShl(unwrap
<Constant
>(LHSConstant
),
1712 unwrap
<Constant
>(RHSConstant
)));
1715 LLVMValueRef
LLVMConstLShr(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1716 return wrap(ConstantExpr::getLShr(unwrap
<Constant
>(LHSConstant
),
1717 unwrap
<Constant
>(RHSConstant
)));
1720 LLVMValueRef
LLVMConstAShr(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1721 return wrap(ConstantExpr::getAShr(unwrap
<Constant
>(LHSConstant
),
1722 unwrap
<Constant
>(RHSConstant
)));
1725 LLVMValueRef
LLVMConstGEP2(LLVMTypeRef Ty
, LLVMValueRef ConstantVal
,
1726 LLVMValueRef
*ConstantIndices
, unsigned NumIndices
) {
1727 ArrayRef
<Constant
*> IdxList(unwrap
<Constant
>(ConstantIndices
, NumIndices
),
1729 Constant
*Val
= unwrap
<Constant
>(ConstantVal
);
1730 return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty
), Val
, IdxList
));
1733 LLVMValueRef
LLVMConstInBoundsGEP2(LLVMTypeRef Ty
, LLVMValueRef ConstantVal
,
1734 LLVMValueRef
*ConstantIndices
,
1735 unsigned NumIndices
) {
1736 ArrayRef
<Constant
*> IdxList(unwrap
<Constant
>(ConstantIndices
, NumIndices
),
1738 Constant
*Val
= unwrap
<Constant
>(ConstantVal
);
1739 return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty
), Val
, IdxList
));
1742 LLVMValueRef
LLVMConstTrunc(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1743 return wrap(ConstantExpr::getTrunc(unwrap
<Constant
>(ConstantVal
),
1747 LLVMValueRef
LLVMConstSExt(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1748 return wrap(ConstantExpr::getSExt(unwrap
<Constant
>(ConstantVal
),
1752 LLVMValueRef
LLVMConstZExt(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1753 return wrap(ConstantExpr::getZExt(unwrap
<Constant
>(ConstantVal
),
1757 LLVMValueRef
LLVMConstFPTrunc(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1758 return wrap(ConstantExpr::getFPTrunc(unwrap
<Constant
>(ConstantVal
),
1762 LLVMValueRef
LLVMConstFPExt(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1763 return wrap(ConstantExpr::getFPExtend(unwrap
<Constant
>(ConstantVal
),
1767 LLVMValueRef
LLVMConstUIToFP(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1768 return wrap(ConstantExpr::getUIToFP(unwrap
<Constant
>(ConstantVal
),
1772 LLVMValueRef
LLVMConstSIToFP(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1773 return wrap(ConstantExpr::getSIToFP(unwrap
<Constant
>(ConstantVal
),
1777 LLVMValueRef
LLVMConstFPToUI(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1778 return wrap(ConstantExpr::getFPToUI(unwrap
<Constant
>(ConstantVal
),
1782 LLVMValueRef
LLVMConstFPToSI(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1783 return wrap(ConstantExpr::getFPToSI(unwrap
<Constant
>(ConstantVal
),
1787 LLVMValueRef
LLVMConstPtrToInt(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1788 return wrap(ConstantExpr::getPtrToInt(unwrap
<Constant
>(ConstantVal
),
1792 LLVMValueRef
LLVMConstIntToPtr(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1793 return wrap(ConstantExpr::getIntToPtr(unwrap
<Constant
>(ConstantVal
),
1797 LLVMValueRef
LLVMConstBitCast(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1798 return wrap(ConstantExpr::getBitCast(unwrap
<Constant
>(ConstantVal
),
1802 LLVMValueRef
LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal
,
1803 LLVMTypeRef ToType
) {
1804 return wrap(ConstantExpr::getAddrSpaceCast(unwrap
<Constant
>(ConstantVal
),
1808 LLVMValueRef
LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal
,
1809 LLVMTypeRef ToType
) {
1810 return wrap(ConstantExpr::getZExtOrBitCast(unwrap
<Constant
>(ConstantVal
),
1814 LLVMValueRef
LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal
,
1815 LLVMTypeRef ToType
) {
1816 return wrap(ConstantExpr::getSExtOrBitCast(unwrap
<Constant
>(ConstantVal
),
1820 LLVMValueRef
LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal
,
1821 LLVMTypeRef ToType
) {
1822 return wrap(ConstantExpr::getTruncOrBitCast(unwrap
<Constant
>(ConstantVal
),
1826 LLVMValueRef
LLVMConstPointerCast(LLVMValueRef ConstantVal
,
1827 LLVMTypeRef ToType
) {
1828 return wrap(ConstantExpr::getPointerCast(unwrap
<Constant
>(ConstantVal
),
1832 LLVMValueRef
LLVMConstIntCast(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
,
1833 LLVMBool isSigned
) {
1834 return wrap(ConstantExpr::getIntegerCast(unwrap
<Constant
>(ConstantVal
),
1835 unwrap(ToType
), isSigned
));
1838 LLVMValueRef
LLVMConstFPCast(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1839 return wrap(ConstantExpr::getFPCast(unwrap
<Constant
>(ConstantVal
),
1843 LLVMValueRef
LLVMConstExtractElement(LLVMValueRef VectorConstant
,
1844 LLVMValueRef IndexConstant
) {
1845 return wrap(ConstantExpr::getExtractElement(unwrap
<Constant
>(VectorConstant
),
1846 unwrap
<Constant
>(IndexConstant
)));
1849 LLVMValueRef
LLVMConstInsertElement(LLVMValueRef VectorConstant
,
1850 LLVMValueRef ElementValueConstant
,
1851 LLVMValueRef IndexConstant
) {
1852 return wrap(ConstantExpr::getInsertElement(unwrap
<Constant
>(VectorConstant
),
1853 unwrap
<Constant
>(ElementValueConstant
),
1854 unwrap
<Constant
>(IndexConstant
)));
1857 LLVMValueRef
LLVMConstShuffleVector(LLVMValueRef VectorAConstant
,
1858 LLVMValueRef VectorBConstant
,
1859 LLVMValueRef MaskConstant
) {
1860 SmallVector
<int, 16> IntMask
;
1861 ShuffleVectorInst::getShuffleMask(unwrap
<Constant
>(MaskConstant
), IntMask
);
1862 return wrap(ConstantExpr::getShuffleVector(unwrap
<Constant
>(VectorAConstant
),
1863 unwrap
<Constant
>(VectorBConstant
),
1867 LLVMValueRef
LLVMConstInlineAsm(LLVMTypeRef Ty
, const char *AsmString
,
1868 const char *Constraints
,
1869 LLVMBool HasSideEffects
,
1870 LLVMBool IsAlignStack
) {
1871 return wrap(InlineAsm::get(dyn_cast
<FunctionType
>(unwrap(Ty
)), AsmString
,
1872 Constraints
, HasSideEffects
, IsAlignStack
));
1875 LLVMValueRef
LLVMBlockAddress(LLVMValueRef F
, LLVMBasicBlockRef BB
) {
1876 return wrap(BlockAddress::get(unwrap
<Function
>(F
), unwrap(BB
)));
1879 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1881 LLVMModuleRef
LLVMGetGlobalParent(LLVMValueRef Global
) {
1882 return wrap(unwrap
<GlobalValue
>(Global
)->getParent());
1885 LLVMBool
LLVMIsDeclaration(LLVMValueRef Global
) {
1886 return unwrap
<GlobalValue
>(Global
)->isDeclaration();
1889 LLVMLinkage
LLVMGetLinkage(LLVMValueRef Global
) {
1890 switch (unwrap
<GlobalValue
>(Global
)->getLinkage()) {
1891 case GlobalValue::ExternalLinkage
:
1892 return LLVMExternalLinkage
;
1893 case GlobalValue::AvailableExternallyLinkage
:
1894 return LLVMAvailableExternallyLinkage
;
1895 case GlobalValue::LinkOnceAnyLinkage
:
1896 return LLVMLinkOnceAnyLinkage
;
1897 case GlobalValue::LinkOnceODRLinkage
:
1898 return LLVMLinkOnceODRLinkage
;
1899 case GlobalValue::WeakAnyLinkage
:
1900 return LLVMWeakAnyLinkage
;
1901 case GlobalValue::WeakODRLinkage
:
1902 return LLVMWeakODRLinkage
;
1903 case GlobalValue::AppendingLinkage
:
1904 return LLVMAppendingLinkage
;
1905 case GlobalValue::InternalLinkage
:
1906 return LLVMInternalLinkage
;
1907 case GlobalValue::PrivateLinkage
:
1908 return LLVMPrivateLinkage
;
1909 case GlobalValue::ExternalWeakLinkage
:
1910 return LLVMExternalWeakLinkage
;
1911 case GlobalValue::CommonLinkage
:
1912 return LLVMCommonLinkage
;
1915 llvm_unreachable("Invalid GlobalValue linkage!");
1918 void LLVMSetLinkage(LLVMValueRef Global
, LLVMLinkage Linkage
) {
1919 GlobalValue
*GV
= unwrap
<GlobalValue
>(Global
);
1922 case LLVMExternalLinkage
:
1923 GV
->setLinkage(GlobalValue::ExternalLinkage
);
1925 case LLVMAvailableExternallyLinkage
:
1926 GV
->setLinkage(GlobalValue::AvailableExternallyLinkage
);
1928 case LLVMLinkOnceAnyLinkage
:
1929 GV
->setLinkage(GlobalValue::LinkOnceAnyLinkage
);
1931 case LLVMLinkOnceODRLinkage
:
1932 GV
->setLinkage(GlobalValue::LinkOnceODRLinkage
);
1934 case LLVMLinkOnceODRAutoHideLinkage
:
1936 errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1937 "longer supported.");
1939 case LLVMWeakAnyLinkage
:
1940 GV
->setLinkage(GlobalValue::WeakAnyLinkage
);
1942 case LLVMWeakODRLinkage
:
1943 GV
->setLinkage(GlobalValue::WeakODRLinkage
);
1945 case LLVMAppendingLinkage
:
1946 GV
->setLinkage(GlobalValue::AppendingLinkage
);
1948 case LLVMInternalLinkage
:
1949 GV
->setLinkage(GlobalValue::InternalLinkage
);
1951 case LLVMPrivateLinkage
:
1952 GV
->setLinkage(GlobalValue::PrivateLinkage
);
1954 case LLVMLinkerPrivateLinkage
:
1955 GV
->setLinkage(GlobalValue::PrivateLinkage
);
1957 case LLVMLinkerPrivateWeakLinkage
:
1958 GV
->setLinkage(GlobalValue::PrivateLinkage
);
1960 case LLVMDLLImportLinkage
:
1963 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1965 case LLVMDLLExportLinkage
:
1968 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1970 case LLVMExternalWeakLinkage
:
1971 GV
->setLinkage(GlobalValue::ExternalWeakLinkage
);
1973 case LLVMGhostLinkage
:
1975 errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1977 case LLVMCommonLinkage
:
1978 GV
->setLinkage(GlobalValue::CommonLinkage
);
1983 const char *LLVMGetSection(LLVMValueRef Global
) {
1984 // Using .data() is safe because of how GlobalObject::setSection is
1986 return unwrap
<GlobalValue
>(Global
)->getSection().data();
1989 void LLVMSetSection(LLVMValueRef Global
, const char *Section
) {
1990 unwrap
<GlobalObject
>(Global
)->setSection(Section
);
1993 LLVMVisibility
LLVMGetVisibility(LLVMValueRef Global
) {
1994 return static_cast<LLVMVisibility
>(
1995 unwrap
<GlobalValue
>(Global
)->getVisibility());
1998 void LLVMSetVisibility(LLVMValueRef Global
, LLVMVisibility Viz
) {
1999 unwrap
<GlobalValue
>(Global
)
2000 ->setVisibility(static_cast<GlobalValue::VisibilityTypes
>(Viz
));
2003 LLVMDLLStorageClass
LLVMGetDLLStorageClass(LLVMValueRef Global
) {
2004 return static_cast<LLVMDLLStorageClass
>(
2005 unwrap
<GlobalValue
>(Global
)->getDLLStorageClass());
2008 void LLVMSetDLLStorageClass(LLVMValueRef Global
, LLVMDLLStorageClass Class
) {
2009 unwrap
<GlobalValue
>(Global
)->setDLLStorageClass(
2010 static_cast<GlobalValue::DLLStorageClassTypes
>(Class
));
2013 LLVMUnnamedAddr
LLVMGetUnnamedAddress(LLVMValueRef Global
) {
2014 switch (unwrap
<GlobalValue
>(Global
)->getUnnamedAddr()) {
2015 case GlobalVariable::UnnamedAddr::None
:
2016 return LLVMNoUnnamedAddr
;
2017 case GlobalVariable::UnnamedAddr::Local
:
2018 return LLVMLocalUnnamedAddr
;
2019 case GlobalVariable::UnnamedAddr::Global
:
2020 return LLVMGlobalUnnamedAddr
;
2022 llvm_unreachable("Unknown UnnamedAddr kind!");
2025 void LLVMSetUnnamedAddress(LLVMValueRef Global
, LLVMUnnamedAddr UnnamedAddr
) {
2026 GlobalValue
*GV
= unwrap
<GlobalValue
>(Global
);
2028 switch (UnnamedAddr
) {
2029 case LLVMNoUnnamedAddr
:
2030 return GV
->setUnnamedAddr(GlobalVariable::UnnamedAddr::None
);
2031 case LLVMLocalUnnamedAddr
:
2032 return GV
->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local
);
2033 case LLVMGlobalUnnamedAddr
:
2034 return GV
->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global
);
2038 LLVMBool
LLVMHasUnnamedAddr(LLVMValueRef Global
) {
2039 return unwrap
<GlobalValue
>(Global
)->hasGlobalUnnamedAddr();
2042 void LLVMSetUnnamedAddr(LLVMValueRef Global
, LLVMBool HasUnnamedAddr
) {
2043 unwrap
<GlobalValue
>(Global
)->setUnnamedAddr(
2044 HasUnnamedAddr
? GlobalValue::UnnamedAddr::Global
2045 : GlobalValue::UnnamedAddr::None
);
2048 LLVMTypeRef
LLVMGlobalGetValueType(LLVMValueRef Global
) {
2049 return wrap(unwrap
<GlobalValue
>(Global
)->getValueType());
2052 /*--.. Operations on global variables, load and store instructions .........--*/
2054 unsigned LLVMGetAlignment(LLVMValueRef V
) {
2055 Value
*P
= unwrap(V
);
2056 if (GlobalObject
*GV
= dyn_cast
<GlobalObject
>(P
))
2057 return GV
->getAlign() ? GV
->getAlign()->value() : 0;
2058 if (AllocaInst
*AI
= dyn_cast
<AllocaInst
>(P
))
2059 return AI
->getAlign().value();
2060 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
2061 return LI
->getAlign().value();
2062 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(P
))
2063 return SI
->getAlign().value();
2064 if (AtomicRMWInst
*RMWI
= dyn_cast
<AtomicRMWInst
>(P
))
2065 return RMWI
->getAlign().value();
2066 if (AtomicCmpXchgInst
*CXI
= dyn_cast
<AtomicCmpXchgInst
>(P
))
2067 return CXI
->getAlign().value();
2070 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2071 "and AtomicCmpXchgInst have alignment");
2074 void LLVMSetAlignment(LLVMValueRef V
, unsigned Bytes
) {
2075 Value
*P
= unwrap(V
);
2076 if (GlobalObject
*GV
= dyn_cast
<GlobalObject
>(P
))
2077 GV
->setAlignment(MaybeAlign(Bytes
));
2078 else if (AllocaInst
*AI
= dyn_cast
<AllocaInst
>(P
))
2079 AI
->setAlignment(Align(Bytes
));
2080 else if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
2081 LI
->setAlignment(Align(Bytes
));
2082 else if (StoreInst
*SI
= dyn_cast
<StoreInst
>(P
))
2083 SI
->setAlignment(Align(Bytes
));
2084 else if (AtomicRMWInst
*RMWI
= dyn_cast
<AtomicRMWInst
>(P
))
2085 RMWI
->setAlignment(Align(Bytes
));
2086 else if (AtomicCmpXchgInst
*CXI
= dyn_cast
<AtomicCmpXchgInst
>(P
))
2087 CXI
->setAlignment(Align(Bytes
));
2090 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2091 "and AtomicCmpXchgInst have alignment");
2094 LLVMValueMetadataEntry
*LLVMGlobalCopyAllMetadata(LLVMValueRef Value
,
2095 size_t *NumEntries
) {
2096 return llvm_getMetadata(NumEntries
, [&Value
](MetadataEntries
&Entries
) {
2098 if (Instruction
*Instr
= dyn_cast
<Instruction
>(unwrap(Value
))) {
2099 Instr
->getAllMetadata(Entries
);
2101 unwrap
<GlobalObject
>(Value
)->getAllMetadata(Entries
);
2106 unsigned LLVMValueMetadataEntriesGetKind(LLVMValueMetadataEntry
*Entries
,
2108 LLVMOpaqueValueMetadataEntry MVE
=
2109 static_cast<LLVMOpaqueValueMetadataEntry
>(Entries
[Index
]);
2114 LLVMValueMetadataEntriesGetMetadata(LLVMValueMetadataEntry
*Entries
,
2116 LLVMOpaqueValueMetadataEntry MVE
=
2117 static_cast<LLVMOpaqueValueMetadataEntry
>(Entries
[Index
]);
2118 return MVE
.Metadata
;
2121 void LLVMDisposeValueMetadataEntries(LLVMValueMetadataEntry
*Entries
) {
2125 void LLVMGlobalSetMetadata(LLVMValueRef Global
, unsigned Kind
,
2126 LLVMMetadataRef MD
) {
2127 unwrap
<GlobalObject
>(Global
)->setMetadata(Kind
, unwrap
<MDNode
>(MD
));
2130 void LLVMGlobalEraseMetadata(LLVMValueRef Global
, unsigned Kind
) {
2131 unwrap
<GlobalObject
>(Global
)->eraseMetadata(Kind
);
2134 void LLVMGlobalClearMetadata(LLVMValueRef Global
) {
2135 unwrap
<GlobalObject
>(Global
)->clearMetadata();
2138 /*--.. Operations on global variables ......................................--*/
2140 LLVMValueRef
LLVMAddGlobal(LLVMModuleRef M
, LLVMTypeRef Ty
, const char *Name
) {
2141 return wrap(new GlobalVariable(*unwrap(M
), unwrap(Ty
), false,
2142 GlobalValue::ExternalLinkage
, nullptr, Name
));
2145 LLVMValueRef
LLVMAddGlobalInAddressSpace(LLVMModuleRef M
, LLVMTypeRef Ty
,
2147 unsigned AddressSpace
) {
2148 return wrap(new GlobalVariable(*unwrap(M
), unwrap(Ty
), false,
2149 GlobalValue::ExternalLinkage
, nullptr, Name
,
2150 nullptr, GlobalVariable::NotThreadLocal
,
2154 LLVMValueRef
LLVMGetNamedGlobal(LLVMModuleRef M
, const char *Name
) {
2155 return wrap(unwrap(M
)->getNamedGlobal(Name
));
2158 LLVMValueRef
LLVMGetFirstGlobal(LLVMModuleRef M
) {
2159 Module
*Mod
= unwrap(M
);
2160 Module::global_iterator I
= Mod
->global_begin();
2161 if (I
== Mod
->global_end())
2166 LLVMValueRef
LLVMGetLastGlobal(LLVMModuleRef M
) {
2167 Module
*Mod
= unwrap(M
);
2168 Module::global_iterator I
= Mod
->global_end();
2169 if (I
== Mod
->global_begin())
2174 LLVMValueRef
LLVMGetNextGlobal(LLVMValueRef GlobalVar
) {
2175 GlobalVariable
*GV
= unwrap
<GlobalVariable
>(GlobalVar
);
2176 Module::global_iterator
I(GV
);
2177 if (++I
== GV
->getParent()->global_end())
2182 LLVMValueRef
LLVMGetPreviousGlobal(LLVMValueRef GlobalVar
) {
2183 GlobalVariable
*GV
= unwrap
<GlobalVariable
>(GlobalVar
);
2184 Module::global_iterator
I(GV
);
2185 if (I
== GV
->getParent()->global_begin())
2190 void LLVMDeleteGlobal(LLVMValueRef GlobalVar
) {
2191 unwrap
<GlobalVariable
>(GlobalVar
)->eraseFromParent();
2194 LLVMValueRef
LLVMGetInitializer(LLVMValueRef GlobalVar
) {
2195 GlobalVariable
* GV
= unwrap
<GlobalVariable
>(GlobalVar
);
2196 if ( !GV
->hasInitializer() )
2198 return wrap(GV
->getInitializer());
2201 void LLVMSetInitializer(LLVMValueRef GlobalVar
, LLVMValueRef ConstantVal
) {
2202 unwrap
<GlobalVariable
>(GlobalVar
)
2203 ->setInitializer(unwrap
<Constant
>(ConstantVal
));
2206 LLVMBool
LLVMIsThreadLocal(LLVMValueRef GlobalVar
) {
2207 return unwrap
<GlobalVariable
>(GlobalVar
)->isThreadLocal();
2210 void LLVMSetThreadLocal(LLVMValueRef GlobalVar
, LLVMBool IsThreadLocal
) {
2211 unwrap
<GlobalVariable
>(GlobalVar
)->setThreadLocal(IsThreadLocal
!= 0);
2214 LLVMBool
LLVMIsGlobalConstant(LLVMValueRef GlobalVar
) {
2215 return unwrap
<GlobalVariable
>(GlobalVar
)->isConstant();
2218 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar
, LLVMBool IsConstant
) {
2219 unwrap
<GlobalVariable
>(GlobalVar
)->setConstant(IsConstant
!= 0);
2222 LLVMThreadLocalMode
LLVMGetThreadLocalMode(LLVMValueRef GlobalVar
) {
2223 switch (unwrap
<GlobalVariable
>(GlobalVar
)->getThreadLocalMode()) {
2224 case GlobalVariable::NotThreadLocal
:
2225 return LLVMNotThreadLocal
;
2226 case GlobalVariable::GeneralDynamicTLSModel
:
2227 return LLVMGeneralDynamicTLSModel
;
2228 case GlobalVariable::LocalDynamicTLSModel
:
2229 return LLVMLocalDynamicTLSModel
;
2230 case GlobalVariable::InitialExecTLSModel
:
2231 return LLVMInitialExecTLSModel
;
2232 case GlobalVariable::LocalExecTLSModel
:
2233 return LLVMLocalExecTLSModel
;
2236 llvm_unreachable("Invalid GlobalVariable thread local mode");
2239 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar
, LLVMThreadLocalMode Mode
) {
2240 GlobalVariable
*GV
= unwrap
<GlobalVariable
>(GlobalVar
);
2243 case LLVMNotThreadLocal
:
2244 GV
->setThreadLocalMode(GlobalVariable::NotThreadLocal
);
2246 case LLVMGeneralDynamicTLSModel
:
2247 GV
->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel
);
2249 case LLVMLocalDynamicTLSModel
:
2250 GV
->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel
);
2252 case LLVMInitialExecTLSModel
:
2253 GV
->setThreadLocalMode(GlobalVariable::InitialExecTLSModel
);
2255 case LLVMLocalExecTLSModel
:
2256 GV
->setThreadLocalMode(GlobalVariable::LocalExecTLSModel
);
2261 LLVMBool
LLVMIsExternallyInitialized(LLVMValueRef GlobalVar
) {
2262 return unwrap
<GlobalVariable
>(GlobalVar
)->isExternallyInitialized();
2265 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar
, LLVMBool IsExtInit
) {
2266 unwrap
<GlobalVariable
>(GlobalVar
)->setExternallyInitialized(IsExtInit
);
2269 /*--.. Operations on aliases ......................................--*/
2271 LLVMValueRef
LLVMAddAlias2(LLVMModuleRef M
, LLVMTypeRef ValueTy
,
2272 unsigned AddrSpace
, LLVMValueRef Aliasee
,
2274 return wrap(GlobalAlias::create(unwrap(ValueTy
), AddrSpace
,
2275 GlobalValue::ExternalLinkage
, Name
,
2276 unwrap
<Constant
>(Aliasee
), unwrap(M
)));
2279 LLVMValueRef
LLVMGetNamedGlobalAlias(LLVMModuleRef M
,
2280 const char *Name
, size_t NameLen
) {
2281 return wrap(unwrap(M
)->getNamedAlias(StringRef(Name
, NameLen
)));
2284 LLVMValueRef
LLVMGetFirstGlobalAlias(LLVMModuleRef M
) {
2285 Module
*Mod
= unwrap(M
);
2286 Module::alias_iterator I
= Mod
->alias_begin();
2287 if (I
== Mod
->alias_end())
2292 LLVMValueRef
LLVMGetLastGlobalAlias(LLVMModuleRef M
) {
2293 Module
*Mod
= unwrap(M
);
2294 Module::alias_iterator I
= Mod
->alias_end();
2295 if (I
== Mod
->alias_begin())
2300 LLVMValueRef
LLVMGetNextGlobalAlias(LLVMValueRef GA
) {
2301 GlobalAlias
*Alias
= unwrap
<GlobalAlias
>(GA
);
2302 Module::alias_iterator
I(Alias
);
2303 if (++I
== Alias
->getParent()->alias_end())
2308 LLVMValueRef
LLVMGetPreviousGlobalAlias(LLVMValueRef GA
) {
2309 GlobalAlias
*Alias
= unwrap
<GlobalAlias
>(GA
);
2310 Module::alias_iterator
I(Alias
);
2311 if (I
== Alias
->getParent()->alias_begin())
2316 LLVMValueRef
LLVMAliasGetAliasee(LLVMValueRef Alias
) {
2317 return wrap(unwrap
<GlobalAlias
>(Alias
)->getAliasee());
2320 void LLVMAliasSetAliasee(LLVMValueRef Alias
, LLVMValueRef Aliasee
) {
2321 unwrap
<GlobalAlias
>(Alias
)->setAliasee(unwrap
<Constant
>(Aliasee
));
2324 /*--.. Operations on functions .............................................--*/
2326 LLVMValueRef
LLVMAddFunction(LLVMModuleRef M
, const char *Name
,
2327 LLVMTypeRef FunctionTy
) {
2328 return wrap(Function::Create(unwrap
<FunctionType
>(FunctionTy
),
2329 GlobalValue::ExternalLinkage
, Name
, unwrap(M
)));
2332 LLVMValueRef
LLVMGetNamedFunction(LLVMModuleRef M
, const char *Name
) {
2333 return wrap(unwrap(M
)->getFunction(Name
));
2336 LLVMValueRef
LLVMGetFirstFunction(LLVMModuleRef M
) {
2337 Module
*Mod
= unwrap(M
);
2338 Module::iterator I
= Mod
->begin();
2339 if (I
== Mod
->end())
2344 LLVMValueRef
LLVMGetLastFunction(LLVMModuleRef M
) {
2345 Module
*Mod
= unwrap(M
);
2346 Module::iterator I
= Mod
->end();
2347 if (I
== Mod
->begin())
2352 LLVMValueRef
LLVMGetNextFunction(LLVMValueRef Fn
) {
2353 Function
*Func
= unwrap
<Function
>(Fn
);
2354 Module::iterator
I(Func
);
2355 if (++I
== Func
->getParent()->end())
2360 LLVMValueRef
LLVMGetPreviousFunction(LLVMValueRef Fn
) {
2361 Function
*Func
= unwrap
<Function
>(Fn
);
2362 Module::iterator
I(Func
);
2363 if (I
== Func
->getParent()->begin())
2368 void LLVMDeleteFunction(LLVMValueRef Fn
) {
2369 unwrap
<Function
>(Fn
)->eraseFromParent();
2372 LLVMBool
LLVMHasPersonalityFn(LLVMValueRef Fn
) {
2373 return unwrap
<Function
>(Fn
)->hasPersonalityFn();
2376 LLVMValueRef
LLVMGetPersonalityFn(LLVMValueRef Fn
) {
2377 return wrap(unwrap
<Function
>(Fn
)->getPersonalityFn());
2380 void LLVMSetPersonalityFn(LLVMValueRef Fn
, LLVMValueRef PersonalityFn
) {
2381 unwrap
<Function
>(Fn
)->setPersonalityFn(unwrap
<Constant
>(PersonalityFn
));
2384 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn
) {
2385 if (Function
*F
= dyn_cast
<Function
>(unwrap(Fn
)))
2386 return F
->getIntrinsicID();
2390 static Intrinsic::ID
llvm_map_to_intrinsic_id(unsigned ID
) {
2391 assert(ID
< llvm::Intrinsic::num_intrinsics
&& "Intrinsic ID out of range");
2392 return llvm::Intrinsic::ID(ID
);
2395 LLVMValueRef
LLVMGetIntrinsicDeclaration(LLVMModuleRef Mod
,
2397 LLVMTypeRef
*ParamTypes
,
2398 size_t ParamCount
) {
2399 ArrayRef
<Type
*> Tys(unwrap(ParamTypes
), ParamCount
);
2400 auto IID
= llvm_map_to_intrinsic_id(ID
);
2401 return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod
), IID
, Tys
));
2404 const char *LLVMIntrinsicGetName(unsigned ID
, size_t *NameLength
) {
2405 auto IID
= llvm_map_to_intrinsic_id(ID
);
2406 auto Str
= llvm::Intrinsic::getName(IID
);
2407 *NameLength
= Str
.size();
2411 LLVMTypeRef
LLVMIntrinsicGetType(LLVMContextRef Ctx
, unsigned ID
,
2412 LLVMTypeRef
*ParamTypes
, size_t ParamCount
) {
2413 auto IID
= llvm_map_to_intrinsic_id(ID
);
2414 ArrayRef
<Type
*> Tys(unwrap(ParamTypes
), ParamCount
);
2415 return wrap(llvm::Intrinsic::getType(*unwrap(Ctx
), IID
, Tys
));
2418 const char *LLVMIntrinsicCopyOverloadedName(unsigned ID
,
2419 LLVMTypeRef
*ParamTypes
,
2421 size_t *NameLength
) {
2422 auto IID
= llvm_map_to_intrinsic_id(ID
);
2423 ArrayRef
<Type
*> Tys(unwrap(ParamTypes
), ParamCount
);
2424 auto Str
= llvm::Intrinsic::getNameNoUnnamedTypes(IID
, Tys
);
2425 *NameLength
= Str
.length();
2426 return strdup(Str
.c_str());
2429 const char *LLVMIntrinsicCopyOverloadedName2(LLVMModuleRef Mod
, unsigned ID
,
2430 LLVMTypeRef
*ParamTypes
,
2432 size_t *NameLength
) {
2433 auto IID
= llvm_map_to_intrinsic_id(ID
);
2434 ArrayRef
<Type
*> Tys(unwrap(ParamTypes
), ParamCount
);
2435 auto Str
= llvm::Intrinsic::getName(IID
, Tys
, unwrap(Mod
));
2436 *NameLength
= Str
.length();
2437 return strdup(Str
.c_str());
2440 unsigned LLVMLookupIntrinsicID(const char *Name
, size_t NameLen
) {
2441 return Function::lookupIntrinsicID({Name
, NameLen
});
2444 LLVMBool
LLVMIntrinsicIsOverloaded(unsigned ID
) {
2445 auto IID
= llvm_map_to_intrinsic_id(ID
);
2446 return llvm::Intrinsic::isOverloaded(IID
);
2449 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn
) {
2450 return unwrap
<Function
>(Fn
)->getCallingConv();
2453 void LLVMSetFunctionCallConv(LLVMValueRef Fn
, unsigned CC
) {
2454 return unwrap
<Function
>(Fn
)->setCallingConv(
2455 static_cast<CallingConv::ID
>(CC
));
2458 const char *LLVMGetGC(LLVMValueRef Fn
) {
2459 Function
*F
= unwrap
<Function
>(Fn
);
2460 return F
->hasGC()? F
->getGC().c_str() : nullptr;
2463 void LLVMSetGC(LLVMValueRef Fn
, const char *GC
) {
2464 Function
*F
= unwrap
<Function
>(Fn
);
2471 void LLVMAddAttributeAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
,
2472 LLVMAttributeRef A
) {
2473 unwrap
<Function
>(F
)->addAttributeAtIndex(Idx
, unwrap(A
));
2476 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
) {
2477 auto AS
= unwrap
<Function
>(F
)->getAttributes().getAttributes(Idx
);
2478 return AS
.getNumAttributes();
2481 void LLVMGetAttributesAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
,
2482 LLVMAttributeRef
*Attrs
) {
2483 auto AS
= unwrap
<Function
>(F
)->getAttributes().getAttributes(Idx
);
2488 LLVMAttributeRef
LLVMGetEnumAttributeAtIndex(LLVMValueRef F
,
2489 LLVMAttributeIndex Idx
,
2491 return wrap(unwrap
<Function
>(F
)->getAttributeAtIndex(
2492 Idx
, (Attribute::AttrKind
)KindID
));
2495 LLVMAttributeRef
LLVMGetStringAttributeAtIndex(LLVMValueRef F
,
2496 LLVMAttributeIndex Idx
,
2497 const char *K
, unsigned KLen
) {
2499 unwrap
<Function
>(F
)->getAttributeAtIndex(Idx
, StringRef(K
, KLen
)));
2502 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
,
2504 unwrap
<Function
>(F
)->removeAttributeAtIndex(Idx
, (Attribute::AttrKind
)KindID
);
2507 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
,
2508 const char *K
, unsigned KLen
) {
2509 unwrap
<Function
>(F
)->removeAttributeAtIndex(Idx
, StringRef(K
, KLen
));
2512 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn
, const char *A
,
2514 Function
*Func
= unwrap
<Function
>(Fn
);
2515 Attribute Attr
= Attribute::get(Func
->getContext(), A
, V
);
2516 Func
->addFnAttr(Attr
);
2519 /*--.. Operations on parameters ............................................--*/
2521 unsigned LLVMCountParams(LLVMValueRef FnRef
) {
2522 // This function is strictly redundant to
2523 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2524 return unwrap
<Function
>(FnRef
)->arg_size();
2527 void LLVMGetParams(LLVMValueRef FnRef
, LLVMValueRef
*ParamRefs
) {
2528 Function
*Fn
= unwrap
<Function
>(FnRef
);
2529 for (Argument
&A
: Fn
->args())
2530 *ParamRefs
++ = wrap(&A
);
2533 LLVMValueRef
LLVMGetParam(LLVMValueRef FnRef
, unsigned index
) {
2534 Function
*Fn
= unwrap
<Function
>(FnRef
);
2535 return wrap(&Fn
->arg_begin()[index
]);
2538 LLVMValueRef
LLVMGetParamParent(LLVMValueRef V
) {
2539 return wrap(unwrap
<Argument
>(V
)->getParent());
2542 LLVMValueRef
LLVMGetFirstParam(LLVMValueRef Fn
) {
2543 Function
*Func
= unwrap
<Function
>(Fn
);
2544 Function::arg_iterator I
= Func
->arg_begin();
2545 if (I
== Func
->arg_end())
2550 LLVMValueRef
LLVMGetLastParam(LLVMValueRef Fn
) {
2551 Function
*Func
= unwrap
<Function
>(Fn
);
2552 Function::arg_iterator I
= Func
->arg_end();
2553 if (I
== Func
->arg_begin())
2558 LLVMValueRef
LLVMGetNextParam(LLVMValueRef Arg
) {
2559 Argument
*A
= unwrap
<Argument
>(Arg
);
2560 Function
*Fn
= A
->getParent();
2561 if (A
->getArgNo() + 1 >= Fn
->arg_size())
2563 return wrap(&Fn
->arg_begin()[A
->getArgNo() + 1]);
2566 LLVMValueRef
LLVMGetPreviousParam(LLVMValueRef Arg
) {
2567 Argument
*A
= unwrap
<Argument
>(Arg
);
2568 if (A
->getArgNo() == 0)
2570 return wrap(&A
->getParent()->arg_begin()[A
->getArgNo() - 1]);
2573 void LLVMSetParamAlignment(LLVMValueRef Arg
, unsigned align
) {
2574 Argument
*A
= unwrap
<Argument
>(Arg
);
2575 A
->addAttr(Attribute::getWithAlignment(A
->getContext(), Align(align
)));
2578 /*--.. Operations on ifuncs ................................................--*/
2580 LLVMValueRef
LLVMAddGlobalIFunc(LLVMModuleRef M
,
2581 const char *Name
, size_t NameLen
,
2582 LLVMTypeRef Ty
, unsigned AddrSpace
,
2583 LLVMValueRef Resolver
) {
2584 return wrap(GlobalIFunc::create(unwrap(Ty
), AddrSpace
,
2585 GlobalValue::ExternalLinkage
,
2586 StringRef(Name
, NameLen
),
2587 unwrap
<Constant
>(Resolver
), unwrap(M
)));
2590 LLVMValueRef
LLVMGetNamedGlobalIFunc(LLVMModuleRef M
,
2591 const char *Name
, size_t NameLen
) {
2592 return wrap(unwrap(M
)->getNamedIFunc(StringRef(Name
, NameLen
)));
2595 LLVMValueRef
LLVMGetFirstGlobalIFunc(LLVMModuleRef M
) {
2596 Module
*Mod
= unwrap(M
);
2597 Module::ifunc_iterator I
= Mod
->ifunc_begin();
2598 if (I
== Mod
->ifunc_end())
2603 LLVMValueRef
LLVMGetLastGlobalIFunc(LLVMModuleRef M
) {
2604 Module
*Mod
= unwrap(M
);
2605 Module::ifunc_iterator I
= Mod
->ifunc_end();
2606 if (I
== Mod
->ifunc_begin())
2611 LLVMValueRef
LLVMGetNextGlobalIFunc(LLVMValueRef IFunc
) {
2612 GlobalIFunc
*GIF
= unwrap
<GlobalIFunc
>(IFunc
);
2613 Module::ifunc_iterator
I(GIF
);
2614 if (++I
== GIF
->getParent()->ifunc_end())
2619 LLVMValueRef
LLVMGetPreviousGlobalIFunc(LLVMValueRef IFunc
) {
2620 GlobalIFunc
*GIF
= unwrap
<GlobalIFunc
>(IFunc
);
2621 Module::ifunc_iterator
I(GIF
);
2622 if (I
== GIF
->getParent()->ifunc_begin())
2627 LLVMValueRef
LLVMGetGlobalIFuncResolver(LLVMValueRef IFunc
) {
2628 return wrap(unwrap
<GlobalIFunc
>(IFunc
)->getResolver());
2631 void LLVMSetGlobalIFuncResolver(LLVMValueRef IFunc
, LLVMValueRef Resolver
) {
2632 unwrap
<GlobalIFunc
>(IFunc
)->setResolver(unwrap
<Constant
>(Resolver
));
2635 void LLVMEraseGlobalIFunc(LLVMValueRef IFunc
) {
2636 unwrap
<GlobalIFunc
>(IFunc
)->eraseFromParent();
2639 void LLVMRemoveGlobalIFunc(LLVMValueRef IFunc
) {
2640 unwrap
<GlobalIFunc
>(IFunc
)->removeFromParent();
2643 /*--.. Operations on basic blocks ..........................................--*/
2645 LLVMValueRef
LLVMBasicBlockAsValue(LLVMBasicBlockRef BB
) {
2646 return wrap(static_cast<Value
*>(unwrap(BB
)));
2649 LLVMBool
LLVMValueIsBasicBlock(LLVMValueRef Val
) {
2650 return isa
<BasicBlock
>(unwrap(Val
));
2653 LLVMBasicBlockRef
LLVMValueAsBasicBlock(LLVMValueRef Val
) {
2654 return wrap(unwrap
<BasicBlock
>(Val
));
2657 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB
) {
2658 return unwrap(BB
)->getName().data();
2661 LLVMValueRef
LLVMGetBasicBlockParent(LLVMBasicBlockRef BB
) {
2662 return wrap(unwrap(BB
)->getParent());
2665 LLVMValueRef
LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB
) {
2666 return wrap(unwrap(BB
)->getTerminator());
2669 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef
) {
2670 return unwrap
<Function
>(FnRef
)->size();
2673 void LLVMGetBasicBlocks(LLVMValueRef FnRef
, LLVMBasicBlockRef
*BasicBlocksRefs
){
2674 Function
*Fn
= unwrap
<Function
>(FnRef
);
2675 for (BasicBlock
&BB
: *Fn
)
2676 *BasicBlocksRefs
++ = wrap(&BB
);
2679 LLVMBasicBlockRef
LLVMGetEntryBasicBlock(LLVMValueRef Fn
) {
2680 return wrap(&unwrap
<Function
>(Fn
)->getEntryBlock());
2683 LLVMBasicBlockRef
LLVMGetFirstBasicBlock(LLVMValueRef Fn
) {
2684 Function
*Func
= unwrap
<Function
>(Fn
);
2685 Function::iterator I
= Func
->begin();
2686 if (I
== Func
->end())
2691 LLVMBasicBlockRef
LLVMGetLastBasicBlock(LLVMValueRef Fn
) {
2692 Function
*Func
= unwrap
<Function
>(Fn
);
2693 Function::iterator I
= Func
->end();
2694 if (I
== Func
->begin())
2699 LLVMBasicBlockRef
LLVMGetNextBasicBlock(LLVMBasicBlockRef BB
) {
2700 BasicBlock
*Block
= unwrap(BB
);
2701 Function::iterator
I(Block
);
2702 if (++I
== Block
->getParent()->end())
2707 LLVMBasicBlockRef
LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB
) {
2708 BasicBlock
*Block
= unwrap(BB
);
2709 Function::iterator
I(Block
);
2710 if (I
== Block
->getParent()->begin())
2715 LLVMBasicBlockRef
LLVMCreateBasicBlockInContext(LLVMContextRef C
,
2717 return wrap(llvm::BasicBlock::Create(*unwrap(C
), Name
));
2720 void LLVMInsertExistingBasicBlockAfterInsertBlock(LLVMBuilderRef Builder
,
2721 LLVMBasicBlockRef BB
) {
2722 BasicBlock
*ToInsert
= unwrap(BB
);
2723 BasicBlock
*CurBB
= unwrap(Builder
)->GetInsertBlock();
2724 assert(CurBB
&& "current insertion point is invalid!");
2725 CurBB
->getParent()->insert(std::next(CurBB
->getIterator()), ToInsert
);
2728 void LLVMAppendExistingBasicBlock(LLVMValueRef Fn
,
2729 LLVMBasicBlockRef BB
) {
2730 unwrap
<Function
>(Fn
)->insert(unwrap
<Function
>(Fn
)->end(), unwrap(BB
));
2733 LLVMBasicBlockRef
LLVMAppendBasicBlockInContext(LLVMContextRef C
,
2736 return wrap(BasicBlock::Create(*unwrap(C
), Name
, unwrap
<Function
>(FnRef
)));
2739 LLVMBasicBlockRef
LLVMAppendBasicBlock(LLVMValueRef FnRef
, const char *Name
) {
2740 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef
, Name
);
2743 LLVMBasicBlockRef
LLVMInsertBasicBlockInContext(LLVMContextRef C
,
2744 LLVMBasicBlockRef BBRef
,
2746 BasicBlock
*BB
= unwrap(BBRef
);
2747 return wrap(BasicBlock::Create(*unwrap(C
), Name
, BB
->getParent(), BB
));
2750 LLVMBasicBlockRef
LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef
,
2752 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef
, Name
);
2755 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef
) {
2756 unwrap(BBRef
)->eraseFromParent();
2759 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef
) {
2760 unwrap(BBRef
)->removeFromParent();
2763 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB
, LLVMBasicBlockRef MovePos
) {
2764 unwrap(BB
)->moveBefore(unwrap(MovePos
));
2767 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB
, LLVMBasicBlockRef MovePos
) {
2768 unwrap(BB
)->moveAfter(unwrap(MovePos
));
2771 /*--.. Operations on instructions ..........................................--*/
2773 LLVMBasicBlockRef
LLVMGetInstructionParent(LLVMValueRef Inst
) {
2774 return wrap(unwrap
<Instruction
>(Inst
)->getParent());
2777 LLVMValueRef
LLVMGetFirstInstruction(LLVMBasicBlockRef BB
) {
2778 BasicBlock
*Block
= unwrap(BB
);
2779 BasicBlock::iterator I
= Block
->begin();
2780 if (I
== Block
->end())
2785 LLVMValueRef
LLVMGetLastInstruction(LLVMBasicBlockRef BB
) {
2786 BasicBlock
*Block
= unwrap(BB
);
2787 BasicBlock::iterator I
= Block
->end();
2788 if (I
== Block
->begin())
2793 LLVMValueRef
LLVMGetNextInstruction(LLVMValueRef Inst
) {
2794 Instruction
*Instr
= unwrap
<Instruction
>(Inst
);
2795 BasicBlock::iterator
I(Instr
);
2796 if (++I
== Instr
->getParent()->end())
2801 LLVMValueRef
LLVMGetPreviousInstruction(LLVMValueRef Inst
) {
2802 Instruction
*Instr
= unwrap
<Instruction
>(Inst
);
2803 BasicBlock::iterator
I(Instr
);
2804 if (I
== Instr
->getParent()->begin())
2809 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst
) {
2810 unwrap
<Instruction
>(Inst
)->removeFromParent();
2813 void LLVMInstructionEraseFromParent(LLVMValueRef Inst
) {
2814 unwrap
<Instruction
>(Inst
)->eraseFromParent();
2817 void LLVMDeleteInstruction(LLVMValueRef Inst
) {
2818 unwrap
<Instruction
>(Inst
)->deleteValue();
2821 LLVMIntPredicate
LLVMGetICmpPredicate(LLVMValueRef Inst
) {
2822 if (ICmpInst
*I
= dyn_cast
<ICmpInst
>(unwrap(Inst
)))
2823 return (LLVMIntPredicate
)I
->getPredicate();
2824 if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(unwrap(Inst
)))
2825 if (CE
->getOpcode() == Instruction::ICmp
)
2826 return (LLVMIntPredicate
)CE
->getPredicate();
2827 return (LLVMIntPredicate
)0;
2830 LLVMRealPredicate
LLVMGetFCmpPredicate(LLVMValueRef Inst
) {
2831 if (FCmpInst
*I
= dyn_cast
<FCmpInst
>(unwrap(Inst
)))
2832 return (LLVMRealPredicate
)I
->getPredicate();
2833 if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(unwrap(Inst
)))
2834 if (CE
->getOpcode() == Instruction::FCmp
)
2835 return (LLVMRealPredicate
)CE
->getPredicate();
2836 return (LLVMRealPredicate
)0;
2839 LLVMOpcode
LLVMGetInstructionOpcode(LLVMValueRef Inst
) {
2840 if (Instruction
*C
= dyn_cast
<Instruction
>(unwrap(Inst
)))
2841 return map_to_llvmopcode(C
->getOpcode());
2842 return (LLVMOpcode
)0;
2845 LLVMValueRef
LLVMInstructionClone(LLVMValueRef Inst
) {
2846 if (Instruction
*C
= dyn_cast
<Instruction
>(unwrap(Inst
)))
2847 return wrap(C
->clone());
2851 LLVMValueRef
LLVMIsATerminatorInst(LLVMValueRef Inst
) {
2852 Instruction
*I
= dyn_cast
<Instruction
>(unwrap(Inst
));
2853 return (I
&& I
->isTerminator()) ? wrap(I
) : nullptr;
2856 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr
) {
2857 if (FuncletPadInst
*FPI
= dyn_cast
<FuncletPadInst
>(unwrap(Instr
))) {
2858 return FPI
->arg_size();
2860 return unwrap
<CallBase
>(Instr
)->arg_size();
2863 /*--.. Call and invoke instructions ........................................--*/
2865 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr
) {
2866 return unwrap
<CallBase
>(Instr
)->getCallingConv();
2869 void LLVMSetInstructionCallConv(LLVMValueRef Instr
, unsigned CC
) {
2870 return unwrap
<CallBase
>(Instr
)->setCallingConv(
2871 static_cast<CallingConv::ID
>(CC
));
2874 void LLVMSetInstrParamAlignment(LLVMValueRef Instr
, LLVMAttributeIndex Idx
,
2876 auto *Call
= unwrap
<CallBase
>(Instr
);
2877 Attribute AlignAttr
=
2878 Attribute::getWithAlignment(Call
->getContext(), Align(align
));
2879 Call
->addAttributeAtIndex(Idx
, AlignAttr
);
2882 void LLVMAddCallSiteAttribute(LLVMValueRef C
, LLVMAttributeIndex Idx
,
2883 LLVMAttributeRef A
) {
2884 unwrap
<CallBase
>(C
)->addAttributeAtIndex(Idx
, unwrap(A
));
2887 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C
,
2888 LLVMAttributeIndex Idx
) {
2889 auto *Call
= unwrap
<CallBase
>(C
);
2890 auto AS
= Call
->getAttributes().getAttributes(Idx
);
2891 return AS
.getNumAttributes();
2894 void LLVMGetCallSiteAttributes(LLVMValueRef C
, LLVMAttributeIndex Idx
,
2895 LLVMAttributeRef
*Attrs
) {
2896 auto *Call
= unwrap
<CallBase
>(C
);
2897 auto AS
= Call
->getAttributes().getAttributes(Idx
);
2902 LLVMAttributeRef
LLVMGetCallSiteEnumAttribute(LLVMValueRef C
,
2903 LLVMAttributeIndex Idx
,
2905 return wrap(unwrap
<CallBase
>(C
)->getAttributeAtIndex(
2906 Idx
, (Attribute::AttrKind
)KindID
));
2909 LLVMAttributeRef
LLVMGetCallSiteStringAttribute(LLVMValueRef C
,
2910 LLVMAttributeIndex Idx
,
2911 const char *K
, unsigned KLen
) {
2913 unwrap
<CallBase
>(C
)->getAttributeAtIndex(Idx
, StringRef(K
, KLen
)));
2916 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C
, LLVMAttributeIndex Idx
,
2918 unwrap
<CallBase
>(C
)->removeAttributeAtIndex(Idx
, (Attribute::AttrKind
)KindID
);
2921 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C
, LLVMAttributeIndex Idx
,
2922 const char *K
, unsigned KLen
) {
2923 unwrap
<CallBase
>(C
)->removeAttributeAtIndex(Idx
, StringRef(K
, KLen
));
2926 LLVMValueRef
LLVMGetCalledValue(LLVMValueRef Instr
) {
2927 return wrap(unwrap
<CallBase
>(Instr
)->getCalledOperand());
2930 LLVMTypeRef
LLVMGetCalledFunctionType(LLVMValueRef Instr
) {
2931 return wrap(unwrap
<CallBase
>(Instr
)->getFunctionType());
2934 /*--.. Operations on call instructions (only) ..............................--*/
2936 LLVMBool
LLVMIsTailCall(LLVMValueRef Call
) {
2937 return unwrap
<CallInst
>(Call
)->isTailCall();
2940 void LLVMSetTailCall(LLVMValueRef Call
, LLVMBool isTailCall
) {
2941 unwrap
<CallInst
>(Call
)->setTailCall(isTailCall
);
2944 LLVMTailCallKind
LLVMGetTailCallKind(LLVMValueRef Call
) {
2945 return (LLVMTailCallKind
)unwrap
<CallInst
>(Call
)->getTailCallKind();
2948 void LLVMSetTailCallKind(LLVMValueRef Call
, LLVMTailCallKind kind
) {
2949 unwrap
<CallInst
>(Call
)->setTailCallKind((CallInst::TailCallKind
)kind
);
2952 /*--.. Operations on invoke instructions (only) ............................--*/
2954 LLVMBasicBlockRef
LLVMGetNormalDest(LLVMValueRef Invoke
) {
2955 return wrap(unwrap
<InvokeInst
>(Invoke
)->getNormalDest());
2958 LLVMBasicBlockRef
LLVMGetUnwindDest(LLVMValueRef Invoke
) {
2959 if (CleanupReturnInst
*CRI
= dyn_cast
<CleanupReturnInst
>(unwrap(Invoke
))) {
2960 return wrap(CRI
->getUnwindDest());
2961 } else if (CatchSwitchInst
*CSI
= dyn_cast
<CatchSwitchInst
>(unwrap(Invoke
))) {
2962 return wrap(CSI
->getUnwindDest());
2964 return wrap(unwrap
<InvokeInst
>(Invoke
)->getUnwindDest());
2967 void LLVMSetNormalDest(LLVMValueRef Invoke
, LLVMBasicBlockRef B
) {
2968 unwrap
<InvokeInst
>(Invoke
)->setNormalDest(unwrap(B
));
2971 void LLVMSetUnwindDest(LLVMValueRef Invoke
, LLVMBasicBlockRef B
) {
2972 if (CleanupReturnInst
*CRI
= dyn_cast
<CleanupReturnInst
>(unwrap(Invoke
))) {
2973 return CRI
->setUnwindDest(unwrap(B
));
2974 } else if (CatchSwitchInst
*CSI
= dyn_cast
<CatchSwitchInst
>(unwrap(Invoke
))) {
2975 return CSI
->setUnwindDest(unwrap(B
));
2977 unwrap
<InvokeInst
>(Invoke
)->setUnwindDest(unwrap(B
));
2980 /*--.. Operations on terminators ...........................................--*/
2982 unsigned LLVMGetNumSuccessors(LLVMValueRef Term
) {
2983 return unwrap
<Instruction
>(Term
)->getNumSuccessors();
2986 LLVMBasicBlockRef
LLVMGetSuccessor(LLVMValueRef Term
, unsigned i
) {
2987 return wrap(unwrap
<Instruction
>(Term
)->getSuccessor(i
));
2990 void LLVMSetSuccessor(LLVMValueRef Term
, unsigned i
, LLVMBasicBlockRef block
) {
2991 return unwrap
<Instruction
>(Term
)->setSuccessor(i
, unwrap(block
));
2994 /*--.. Operations on branch instructions (only) ............................--*/
2996 LLVMBool
LLVMIsConditional(LLVMValueRef Branch
) {
2997 return unwrap
<BranchInst
>(Branch
)->isConditional();
3000 LLVMValueRef
LLVMGetCondition(LLVMValueRef Branch
) {
3001 return wrap(unwrap
<BranchInst
>(Branch
)->getCondition());
3004 void LLVMSetCondition(LLVMValueRef Branch
, LLVMValueRef Cond
) {
3005 return unwrap
<BranchInst
>(Branch
)->setCondition(unwrap(Cond
));
3008 /*--.. Operations on switch instructions (only) ............................--*/
3010 LLVMBasicBlockRef
LLVMGetSwitchDefaultDest(LLVMValueRef Switch
) {
3011 return wrap(unwrap
<SwitchInst
>(Switch
)->getDefaultDest());
3014 /*--.. Operations on alloca instructions (only) ............................--*/
3016 LLVMTypeRef
LLVMGetAllocatedType(LLVMValueRef Alloca
) {
3017 return wrap(unwrap
<AllocaInst
>(Alloca
)->getAllocatedType());
3020 /*--.. Operations on gep instructions (only) ...............................--*/
3022 LLVMBool
LLVMIsInBounds(LLVMValueRef GEP
) {
3023 return unwrap
<GEPOperator
>(GEP
)->isInBounds();
3026 void LLVMSetIsInBounds(LLVMValueRef GEP
, LLVMBool InBounds
) {
3027 return unwrap
<GetElementPtrInst
>(GEP
)->setIsInBounds(InBounds
);
3030 LLVMTypeRef
LLVMGetGEPSourceElementType(LLVMValueRef GEP
) {
3031 return wrap(unwrap
<GEPOperator
>(GEP
)->getSourceElementType());
3034 /*--.. Operations on phi nodes .............................................--*/
3036 void LLVMAddIncoming(LLVMValueRef PhiNode
, LLVMValueRef
*IncomingValues
,
3037 LLVMBasicBlockRef
*IncomingBlocks
, unsigned Count
) {
3038 PHINode
*PhiVal
= unwrap
<PHINode
>(PhiNode
);
3039 for (unsigned I
= 0; I
!= Count
; ++I
)
3040 PhiVal
->addIncoming(unwrap(IncomingValues
[I
]), unwrap(IncomingBlocks
[I
]));
3043 unsigned LLVMCountIncoming(LLVMValueRef PhiNode
) {
3044 return unwrap
<PHINode
>(PhiNode
)->getNumIncomingValues();
3047 LLVMValueRef
LLVMGetIncomingValue(LLVMValueRef PhiNode
, unsigned Index
) {
3048 return wrap(unwrap
<PHINode
>(PhiNode
)->getIncomingValue(Index
));
3051 LLVMBasicBlockRef
LLVMGetIncomingBlock(LLVMValueRef PhiNode
, unsigned Index
) {
3052 return wrap(unwrap
<PHINode
>(PhiNode
)->getIncomingBlock(Index
));
3055 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
3057 unsigned LLVMGetNumIndices(LLVMValueRef Inst
) {
3058 auto *I
= unwrap(Inst
);
3059 if (auto *GEP
= dyn_cast
<GEPOperator
>(I
))
3060 return GEP
->getNumIndices();
3061 if (auto *EV
= dyn_cast
<ExtractValueInst
>(I
))
3062 return EV
->getNumIndices();
3063 if (auto *IV
= dyn_cast
<InsertValueInst
>(I
))
3064 return IV
->getNumIndices();
3066 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3069 const unsigned *LLVMGetIndices(LLVMValueRef Inst
) {
3070 auto *I
= unwrap(Inst
);
3071 if (auto *EV
= dyn_cast
<ExtractValueInst
>(I
))
3072 return EV
->getIndices().data();
3073 if (auto *IV
= dyn_cast
<InsertValueInst
>(I
))
3074 return IV
->getIndices().data();
3076 "LLVMGetIndices applies only to extractvalue and insertvalue!");
3080 /*===-- Instruction builders ----------------------------------------------===*/
3082 LLVMBuilderRef
LLVMCreateBuilderInContext(LLVMContextRef C
) {
3083 return wrap(new IRBuilder
<>(*unwrap(C
)));
3086 LLVMBuilderRef
LLVMCreateBuilder(void) {
3087 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
3090 void LLVMPositionBuilder(LLVMBuilderRef Builder
, LLVMBasicBlockRef Block
,
3091 LLVMValueRef Instr
) {
3092 BasicBlock
*BB
= unwrap(Block
);
3093 auto I
= Instr
? unwrap
<Instruction
>(Instr
)->getIterator() : BB
->end();
3094 unwrap(Builder
)->SetInsertPoint(BB
, I
);
3097 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder
, LLVMValueRef Instr
) {
3098 Instruction
*I
= unwrap
<Instruction
>(Instr
);
3099 unwrap(Builder
)->SetInsertPoint(I
->getParent(), I
->getIterator());
3102 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder
, LLVMBasicBlockRef Block
) {
3103 BasicBlock
*BB
= unwrap(Block
);
3104 unwrap(Builder
)->SetInsertPoint(BB
);
3107 LLVMBasicBlockRef
LLVMGetInsertBlock(LLVMBuilderRef Builder
) {
3108 return wrap(unwrap(Builder
)->GetInsertBlock());
3111 void LLVMClearInsertionPosition(LLVMBuilderRef Builder
) {
3112 unwrap(Builder
)->ClearInsertionPoint();
3115 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder
, LLVMValueRef Instr
) {
3116 unwrap(Builder
)->Insert(unwrap
<Instruction
>(Instr
));
3119 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder
, LLVMValueRef Instr
,
3121 unwrap(Builder
)->Insert(unwrap
<Instruction
>(Instr
), Name
);
3124 void LLVMDisposeBuilder(LLVMBuilderRef Builder
) {
3125 delete unwrap(Builder
);
3128 /*--.. Metadata builders ...................................................--*/
3130 LLVMMetadataRef
LLVMGetCurrentDebugLocation2(LLVMBuilderRef Builder
) {
3131 return wrap(unwrap(Builder
)->getCurrentDebugLocation().getAsMDNode());
3134 void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Builder
, LLVMMetadataRef Loc
) {
3136 unwrap(Builder
)->SetCurrentDebugLocation(DebugLoc(unwrap
<MDNode
>(Loc
)));
3138 unwrap(Builder
)->SetCurrentDebugLocation(DebugLoc());
3141 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder
, LLVMValueRef L
) {
3143 L
? cast
<MDNode
>(unwrap
<MetadataAsValue
>(L
)->getMetadata()) : nullptr;
3144 unwrap(Builder
)->SetCurrentDebugLocation(DebugLoc(Loc
));
3147 LLVMValueRef
LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder
) {
3148 LLVMContext
&Context
= unwrap(Builder
)->getContext();
3149 return wrap(MetadataAsValue::get(
3150 Context
, unwrap(Builder
)->getCurrentDebugLocation().getAsMDNode()));
3153 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder
, LLVMValueRef Inst
) {
3154 unwrap(Builder
)->SetInstDebugLocation(unwrap
<Instruction
>(Inst
));
3157 void LLVMAddMetadataToInst(LLVMBuilderRef Builder
, LLVMValueRef Inst
) {
3158 unwrap(Builder
)->AddMetadataToInst(unwrap
<Instruction
>(Inst
));
3161 void LLVMBuilderSetDefaultFPMathTag(LLVMBuilderRef Builder
,
3162 LLVMMetadataRef FPMathTag
) {
3164 unwrap(Builder
)->setDefaultFPMathTag(FPMathTag
3165 ? unwrap
<MDNode
>(FPMathTag
)
3169 LLVMMetadataRef
LLVMBuilderGetDefaultFPMathTag(LLVMBuilderRef Builder
) {
3170 return wrap(unwrap(Builder
)->getDefaultFPMathTag());
3173 /*--.. Instruction builders ................................................--*/
3175 LLVMValueRef
LLVMBuildRetVoid(LLVMBuilderRef B
) {
3176 return wrap(unwrap(B
)->CreateRetVoid());
3179 LLVMValueRef
LLVMBuildRet(LLVMBuilderRef B
, LLVMValueRef V
) {
3180 return wrap(unwrap(B
)->CreateRet(unwrap(V
)));
3183 LLVMValueRef
LLVMBuildAggregateRet(LLVMBuilderRef B
, LLVMValueRef
*RetVals
,
3185 return wrap(unwrap(B
)->CreateAggregateRet(unwrap(RetVals
), N
));
3188 LLVMValueRef
LLVMBuildBr(LLVMBuilderRef B
, LLVMBasicBlockRef Dest
) {
3189 return wrap(unwrap(B
)->CreateBr(unwrap(Dest
)));
3192 LLVMValueRef
LLVMBuildCondBr(LLVMBuilderRef B
, LLVMValueRef If
,
3193 LLVMBasicBlockRef Then
, LLVMBasicBlockRef Else
) {
3194 return wrap(unwrap(B
)->CreateCondBr(unwrap(If
), unwrap(Then
), unwrap(Else
)));
3197 LLVMValueRef
LLVMBuildSwitch(LLVMBuilderRef B
, LLVMValueRef V
,
3198 LLVMBasicBlockRef Else
, unsigned NumCases
) {
3199 return wrap(unwrap(B
)->CreateSwitch(unwrap(V
), unwrap(Else
), NumCases
));
3202 LLVMValueRef
LLVMBuildIndirectBr(LLVMBuilderRef B
, LLVMValueRef Addr
,
3203 unsigned NumDests
) {
3204 return wrap(unwrap(B
)->CreateIndirectBr(unwrap(Addr
), NumDests
));
3207 LLVMValueRef
LLVMBuildInvoke2(LLVMBuilderRef B
, LLVMTypeRef Ty
, LLVMValueRef Fn
,
3208 LLVMValueRef
*Args
, unsigned NumArgs
,
3209 LLVMBasicBlockRef Then
, LLVMBasicBlockRef Catch
,
3211 return wrap(unwrap(B
)->CreateInvoke(unwrap
<FunctionType
>(Ty
), unwrap(Fn
),
3212 unwrap(Then
), unwrap(Catch
),
3213 ArrayRef(unwrap(Args
), NumArgs
), Name
));
3216 LLVMValueRef
LLVMBuildLandingPad(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3217 LLVMValueRef PersFn
, unsigned NumClauses
,
3219 // The personality used to live on the landingpad instruction, but now it
3220 // lives on the parent function. For compatibility, take the provided
3221 // personality and put it on the parent function.
3223 unwrap(B
)->GetInsertBlock()->getParent()->setPersonalityFn(
3224 unwrap
<Function
>(PersFn
));
3225 return wrap(unwrap(B
)->CreateLandingPad(unwrap(Ty
), NumClauses
, Name
));
3228 LLVMValueRef
LLVMBuildCatchPad(LLVMBuilderRef B
, LLVMValueRef ParentPad
,
3229 LLVMValueRef
*Args
, unsigned NumArgs
,
3231 return wrap(unwrap(B
)->CreateCatchPad(unwrap(ParentPad
),
3232 ArrayRef(unwrap(Args
), NumArgs
), Name
));
3235 LLVMValueRef
LLVMBuildCleanupPad(LLVMBuilderRef B
, LLVMValueRef ParentPad
,
3236 LLVMValueRef
*Args
, unsigned NumArgs
,
3238 if (ParentPad
== nullptr) {
3239 Type
*Ty
= Type::getTokenTy(unwrap(B
)->getContext());
3240 ParentPad
= wrap(Constant::getNullValue(Ty
));
3242 return wrap(unwrap(B
)->CreateCleanupPad(
3243 unwrap(ParentPad
), ArrayRef(unwrap(Args
), NumArgs
), Name
));
3246 LLVMValueRef
LLVMBuildResume(LLVMBuilderRef B
, LLVMValueRef Exn
) {
3247 return wrap(unwrap(B
)->CreateResume(unwrap(Exn
)));
3250 LLVMValueRef
LLVMBuildCatchSwitch(LLVMBuilderRef B
, LLVMValueRef ParentPad
,
3251 LLVMBasicBlockRef UnwindBB
,
3252 unsigned NumHandlers
, const char *Name
) {
3253 if (ParentPad
== nullptr) {
3254 Type
*Ty
= Type::getTokenTy(unwrap(B
)->getContext());
3255 ParentPad
= wrap(Constant::getNullValue(Ty
));
3257 return wrap(unwrap(B
)->CreateCatchSwitch(unwrap(ParentPad
), unwrap(UnwindBB
),
3258 NumHandlers
, Name
));
3261 LLVMValueRef
LLVMBuildCatchRet(LLVMBuilderRef B
, LLVMValueRef CatchPad
,
3262 LLVMBasicBlockRef BB
) {
3263 return wrap(unwrap(B
)->CreateCatchRet(unwrap
<CatchPadInst
>(CatchPad
),
3267 LLVMValueRef
LLVMBuildCleanupRet(LLVMBuilderRef B
, LLVMValueRef CatchPad
,
3268 LLVMBasicBlockRef BB
) {
3269 return wrap(unwrap(B
)->CreateCleanupRet(unwrap
<CleanupPadInst
>(CatchPad
),
3273 LLVMValueRef
LLVMBuildUnreachable(LLVMBuilderRef B
) {
3274 return wrap(unwrap(B
)->CreateUnreachable());
3277 void LLVMAddCase(LLVMValueRef Switch
, LLVMValueRef OnVal
,
3278 LLVMBasicBlockRef Dest
) {
3279 unwrap
<SwitchInst
>(Switch
)->addCase(unwrap
<ConstantInt
>(OnVal
), unwrap(Dest
));
3282 void LLVMAddDestination(LLVMValueRef IndirectBr
, LLVMBasicBlockRef Dest
) {
3283 unwrap
<IndirectBrInst
>(IndirectBr
)->addDestination(unwrap(Dest
));
3286 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad
) {
3287 return unwrap
<LandingPadInst
>(LandingPad
)->getNumClauses();
3290 LLVMValueRef
LLVMGetClause(LLVMValueRef LandingPad
, unsigned Idx
) {
3291 return wrap(unwrap
<LandingPadInst
>(LandingPad
)->getClause(Idx
));
3294 void LLVMAddClause(LLVMValueRef LandingPad
, LLVMValueRef ClauseVal
) {
3295 unwrap
<LandingPadInst
>(LandingPad
)->addClause(unwrap
<Constant
>(ClauseVal
));
3298 LLVMBool
LLVMIsCleanup(LLVMValueRef LandingPad
) {
3299 return unwrap
<LandingPadInst
>(LandingPad
)->isCleanup();
3302 void LLVMSetCleanup(LLVMValueRef LandingPad
, LLVMBool Val
) {
3303 unwrap
<LandingPadInst
>(LandingPad
)->setCleanup(Val
);
3306 void LLVMAddHandler(LLVMValueRef CatchSwitch
, LLVMBasicBlockRef Dest
) {
3307 unwrap
<CatchSwitchInst
>(CatchSwitch
)->addHandler(unwrap(Dest
));
3310 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch
) {
3311 return unwrap
<CatchSwitchInst
>(CatchSwitch
)->getNumHandlers();
3314 void LLVMGetHandlers(LLVMValueRef CatchSwitch
, LLVMBasicBlockRef
*Handlers
) {
3315 CatchSwitchInst
*CSI
= unwrap
<CatchSwitchInst
>(CatchSwitch
);
3316 for (const BasicBlock
*H
: CSI
->handlers())
3317 *Handlers
++ = wrap(H
);
3320 LLVMValueRef
LLVMGetParentCatchSwitch(LLVMValueRef CatchPad
) {
3321 return wrap(unwrap
<CatchPadInst
>(CatchPad
)->getCatchSwitch());
3324 void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad
, LLVMValueRef CatchSwitch
) {
3325 unwrap
<CatchPadInst
>(CatchPad
)
3326 ->setCatchSwitch(unwrap
<CatchSwitchInst
>(CatchSwitch
));
3329 /*--.. Funclets ...........................................................--*/
3331 LLVMValueRef
LLVMGetArgOperand(LLVMValueRef Funclet
, unsigned i
) {
3332 return wrap(unwrap
<FuncletPadInst
>(Funclet
)->getArgOperand(i
));
3335 void LLVMSetArgOperand(LLVMValueRef Funclet
, unsigned i
, LLVMValueRef value
) {
3336 unwrap
<FuncletPadInst
>(Funclet
)->setArgOperand(i
, unwrap(value
));
3339 /*--.. Arithmetic ..........................................................--*/
3341 LLVMValueRef
LLVMBuildAdd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3343 return wrap(unwrap(B
)->CreateAdd(unwrap(LHS
), unwrap(RHS
), Name
));
3346 LLVMValueRef
LLVMBuildNSWAdd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3348 return wrap(unwrap(B
)->CreateNSWAdd(unwrap(LHS
), unwrap(RHS
), Name
));
3351 LLVMValueRef
LLVMBuildNUWAdd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3353 return wrap(unwrap(B
)->CreateNUWAdd(unwrap(LHS
), unwrap(RHS
), Name
));
3356 LLVMValueRef
LLVMBuildFAdd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3358 return wrap(unwrap(B
)->CreateFAdd(unwrap(LHS
), unwrap(RHS
), Name
));
3361 LLVMValueRef
LLVMBuildSub(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3363 return wrap(unwrap(B
)->CreateSub(unwrap(LHS
), unwrap(RHS
), Name
));
3366 LLVMValueRef
LLVMBuildNSWSub(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3368 return wrap(unwrap(B
)->CreateNSWSub(unwrap(LHS
), unwrap(RHS
), Name
));
3371 LLVMValueRef
LLVMBuildNUWSub(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3373 return wrap(unwrap(B
)->CreateNUWSub(unwrap(LHS
), unwrap(RHS
), Name
));
3376 LLVMValueRef
LLVMBuildFSub(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3378 return wrap(unwrap(B
)->CreateFSub(unwrap(LHS
), unwrap(RHS
), Name
));
3381 LLVMValueRef
LLVMBuildMul(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3383 return wrap(unwrap(B
)->CreateMul(unwrap(LHS
), unwrap(RHS
), Name
));
3386 LLVMValueRef
LLVMBuildNSWMul(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3388 return wrap(unwrap(B
)->CreateNSWMul(unwrap(LHS
), unwrap(RHS
), Name
));
3391 LLVMValueRef
LLVMBuildNUWMul(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3393 return wrap(unwrap(B
)->CreateNUWMul(unwrap(LHS
), unwrap(RHS
), Name
));
3396 LLVMValueRef
LLVMBuildFMul(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3398 return wrap(unwrap(B
)->CreateFMul(unwrap(LHS
), unwrap(RHS
), Name
));
3401 LLVMValueRef
LLVMBuildUDiv(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3403 return wrap(unwrap(B
)->CreateUDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3406 LLVMValueRef
LLVMBuildExactUDiv(LLVMBuilderRef B
, LLVMValueRef LHS
,
3407 LLVMValueRef RHS
, const char *Name
) {
3408 return wrap(unwrap(B
)->CreateExactUDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3411 LLVMValueRef
LLVMBuildSDiv(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3413 return wrap(unwrap(B
)->CreateSDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3416 LLVMValueRef
LLVMBuildExactSDiv(LLVMBuilderRef B
, LLVMValueRef LHS
,
3417 LLVMValueRef RHS
, const char *Name
) {
3418 return wrap(unwrap(B
)->CreateExactSDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3421 LLVMValueRef
LLVMBuildFDiv(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3423 return wrap(unwrap(B
)->CreateFDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3426 LLVMValueRef
LLVMBuildURem(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3428 return wrap(unwrap(B
)->CreateURem(unwrap(LHS
), unwrap(RHS
), Name
));
3431 LLVMValueRef
LLVMBuildSRem(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3433 return wrap(unwrap(B
)->CreateSRem(unwrap(LHS
), unwrap(RHS
), Name
));
3436 LLVMValueRef
LLVMBuildFRem(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3438 return wrap(unwrap(B
)->CreateFRem(unwrap(LHS
), unwrap(RHS
), Name
));
3441 LLVMValueRef
LLVMBuildShl(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3443 return wrap(unwrap(B
)->CreateShl(unwrap(LHS
), unwrap(RHS
), Name
));
3446 LLVMValueRef
LLVMBuildLShr(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3448 return wrap(unwrap(B
)->CreateLShr(unwrap(LHS
), unwrap(RHS
), Name
));
3451 LLVMValueRef
LLVMBuildAShr(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3453 return wrap(unwrap(B
)->CreateAShr(unwrap(LHS
), unwrap(RHS
), Name
));
3456 LLVMValueRef
LLVMBuildAnd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3458 return wrap(unwrap(B
)->CreateAnd(unwrap(LHS
), unwrap(RHS
), Name
));
3461 LLVMValueRef
LLVMBuildOr(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3463 return wrap(unwrap(B
)->CreateOr(unwrap(LHS
), unwrap(RHS
), Name
));
3466 LLVMValueRef
LLVMBuildXor(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3468 return wrap(unwrap(B
)->CreateXor(unwrap(LHS
), unwrap(RHS
), Name
));
3471 LLVMValueRef
LLVMBuildBinOp(LLVMBuilderRef B
, LLVMOpcode Op
,
3472 LLVMValueRef LHS
, LLVMValueRef RHS
,
3474 return wrap(unwrap(B
)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op
)), unwrap(LHS
),
3475 unwrap(RHS
), Name
));
3478 LLVMValueRef
LLVMBuildNeg(LLVMBuilderRef B
, LLVMValueRef V
, const char *Name
) {
3479 return wrap(unwrap(B
)->CreateNeg(unwrap(V
), Name
));
3482 LLVMValueRef
LLVMBuildNSWNeg(LLVMBuilderRef B
, LLVMValueRef V
,
3484 return wrap(unwrap(B
)->CreateNSWNeg(unwrap(V
), Name
));
3487 LLVMValueRef
LLVMBuildNUWNeg(LLVMBuilderRef B
, LLVMValueRef V
,
3489 return wrap(unwrap(B
)->CreateNUWNeg(unwrap(V
), Name
));
3492 LLVMValueRef
LLVMBuildFNeg(LLVMBuilderRef B
, LLVMValueRef V
, const char *Name
) {
3493 return wrap(unwrap(B
)->CreateFNeg(unwrap(V
), Name
));
3496 LLVMValueRef
LLVMBuildNot(LLVMBuilderRef B
, LLVMValueRef V
, const char *Name
) {
3497 return wrap(unwrap(B
)->CreateNot(unwrap(V
), Name
));
3500 LLVMBool
LLVMGetNUW(LLVMValueRef ArithInst
) {
3501 Value
*P
= unwrap
<Value
>(ArithInst
);
3502 return cast
<Instruction
>(P
)->hasNoUnsignedWrap();
3505 void LLVMSetNUW(LLVMValueRef ArithInst
, LLVMBool HasNUW
) {
3506 Value
*P
= unwrap
<Value
>(ArithInst
);
3507 cast
<Instruction
>(P
)->setHasNoUnsignedWrap(HasNUW
);
3510 LLVMBool
LLVMGetNSW(LLVMValueRef ArithInst
) {
3511 Value
*P
= unwrap
<Value
>(ArithInst
);
3512 return cast
<Instruction
>(P
)->hasNoSignedWrap();
3515 void LLVMSetNSW(LLVMValueRef ArithInst
, LLVMBool HasNSW
) {
3516 Value
*P
= unwrap
<Value
>(ArithInst
);
3517 cast
<Instruction
>(P
)->setHasNoSignedWrap(HasNSW
);
3520 LLVMBool
LLVMGetExact(LLVMValueRef DivOrShrInst
) {
3521 Value
*P
= unwrap
<Value
>(DivOrShrInst
);
3522 return cast
<Instruction
>(P
)->isExact();
3525 void LLVMSetExact(LLVMValueRef DivOrShrInst
, LLVMBool IsExact
) {
3526 Value
*P
= unwrap
<Value
>(DivOrShrInst
);
3527 cast
<Instruction
>(P
)->setIsExact(IsExact
);
3530 /*--.. Memory ..............................................................--*/
3532 LLVMValueRef
LLVMBuildMalloc(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3534 Type
* ITy
= Type::getInt32Ty(unwrap(B
)->GetInsertBlock()->getContext());
3535 Constant
* AllocSize
= ConstantExpr::getSizeOf(unwrap(Ty
));
3536 AllocSize
= ConstantExpr::getTruncOrBitCast(AllocSize
, ITy
);
3537 return wrap(unwrap(B
)->CreateMalloc(ITy
, unwrap(Ty
), AllocSize
, nullptr,
3541 LLVMValueRef
LLVMBuildArrayMalloc(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3542 LLVMValueRef Val
, const char *Name
) {
3543 Type
* ITy
= Type::getInt32Ty(unwrap(B
)->GetInsertBlock()->getContext());
3544 Constant
* AllocSize
= ConstantExpr::getSizeOf(unwrap(Ty
));
3545 AllocSize
= ConstantExpr::getTruncOrBitCast(AllocSize
, ITy
);
3546 return wrap(unwrap(B
)->CreateMalloc(ITy
, unwrap(Ty
), AllocSize
, unwrap(Val
),
3550 LLVMValueRef
LLVMBuildMemSet(LLVMBuilderRef B
, LLVMValueRef Ptr
,
3551 LLVMValueRef Val
, LLVMValueRef Len
,
3553 return wrap(unwrap(B
)->CreateMemSet(unwrap(Ptr
), unwrap(Val
), unwrap(Len
),
3554 MaybeAlign(Align
)));
3557 LLVMValueRef
LLVMBuildMemCpy(LLVMBuilderRef B
,
3558 LLVMValueRef Dst
, unsigned DstAlign
,
3559 LLVMValueRef Src
, unsigned SrcAlign
,
3560 LLVMValueRef Size
) {
3561 return wrap(unwrap(B
)->CreateMemCpy(unwrap(Dst
), MaybeAlign(DstAlign
),
3562 unwrap(Src
), MaybeAlign(SrcAlign
),
3566 LLVMValueRef
LLVMBuildMemMove(LLVMBuilderRef B
,
3567 LLVMValueRef Dst
, unsigned DstAlign
,
3568 LLVMValueRef Src
, unsigned SrcAlign
,
3569 LLVMValueRef Size
) {
3570 return wrap(unwrap(B
)->CreateMemMove(unwrap(Dst
), MaybeAlign(DstAlign
),
3571 unwrap(Src
), MaybeAlign(SrcAlign
),
3575 LLVMValueRef
LLVMBuildAlloca(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3577 return wrap(unwrap(B
)->CreateAlloca(unwrap(Ty
), nullptr, Name
));
3580 LLVMValueRef
LLVMBuildArrayAlloca(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3581 LLVMValueRef Val
, const char *Name
) {
3582 return wrap(unwrap(B
)->CreateAlloca(unwrap(Ty
), unwrap(Val
), Name
));
3585 LLVMValueRef
LLVMBuildFree(LLVMBuilderRef B
, LLVMValueRef PointerVal
) {
3586 return wrap(unwrap(B
)->CreateFree(unwrap(PointerVal
)));
3589 LLVMValueRef
LLVMBuildLoad2(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3590 LLVMValueRef PointerVal
, const char *Name
) {
3591 return wrap(unwrap(B
)->CreateLoad(unwrap(Ty
), unwrap(PointerVal
), Name
));
3594 LLVMValueRef
LLVMBuildStore(LLVMBuilderRef B
, LLVMValueRef Val
,
3595 LLVMValueRef PointerVal
) {
3596 return wrap(unwrap(B
)->CreateStore(unwrap(Val
), unwrap(PointerVal
)));
3599 static AtomicOrdering
mapFromLLVMOrdering(LLVMAtomicOrdering Ordering
) {
3601 case LLVMAtomicOrderingNotAtomic
: return AtomicOrdering::NotAtomic
;
3602 case LLVMAtomicOrderingUnordered
: return AtomicOrdering::Unordered
;
3603 case LLVMAtomicOrderingMonotonic
: return AtomicOrdering::Monotonic
;
3604 case LLVMAtomicOrderingAcquire
: return AtomicOrdering::Acquire
;
3605 case LLVMAtomicOrderingRelease
: return AtomicOrdering::Release
;
3606 case LLVMAtomicOrderingAcquireRelease
:
3607 return AtomicOrdering::AcquireRelease
;
3608 case LLVMAtomicOrderingSequentiallyConsistent
:
3609 return AtomicOrdering::SequentiallyConsistent
;
3612 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3615 static LLVMAtomicOrdering
mapToLLVMOrdering(AtomicOrdering Ordering
) {
3617 case AtomicOrdering::NotAtomic
: return LLVMAtomicOrderingNotAtomic
;
3618 case AtomicOrdering::Unordered
: return LLVMAtomicOrderingUnordered
;
3619 case AtomicOrdering::Monotonic
: return LLVMAtomicOrderingMonotonic
;
3620 case AtomicOrdering::Acquire
: return LLVMAtomicOrderingAcquire
;
3621 case AtomicOrdering::Release
: return LLVMAtomicOrderingRelease
;
3622 case AtomicOrdering::AcquireRelease
:
3623 return LLVMAtomicOrderingAcquireRelease
;
3624 case AtomicOrdering::SequentiallyConsistent
:
3625 return LLVMAtomicOrderingSequentiallyConsistent
;
3628 llvm_unreachable("Invalid AtomicOrdering value!");
3631 static AtomicRMWInst::BinOp
mapFromLLVMRMWBinOp(LLVMAtomicRMWBinOp BinOp
) {
3633 case LLVMAtomicRMWBinOpXchg
: return AtomicRMWInst::Xchg
;
3634 case LLVMAtomicRMWBinOpAdd
: return AtomicRMWInst::Add
;
3635 case LLVMAtomicRMWBinOpSub
: return AtomicRMWInst::Sub
;
3636 case LLVMAtomicRMWBinOpAnd
: return AtomicRMWInst::And
;
3637 case LLVMAtomicRMWBinOpNand
: return AtomicRMWInst::Nand
;
3638 case LLVMAtomicRMWBinOpOr
: return AtomicRMWInst::Or
;
3639 case LLVMAtomicRMWBinOpXor
: return AtomicRMWInst::Xor
;
3640 case LLVMAtomicRMWBinOpMax
: return AtomicRMWInst::Max
;
3641 case LLVMAtomicRMWBinOpMin
: return AtomicRMWInst::Min
;
3642 case LLVMAtomicRMWBinOpUMax
: return AtomicRMWInst::UMax
;
3643 case LLVMAtomicRMWBinOpUMin
: return AtomicRMWInst::UMin
;
3644 case LLVMAtomicRMWBinOpFAdd
: return AtomicRMWInst::FAdd
;
3645 case LLVMAtomicRMWBinOpFSub
: return AtomicRMWInst::FSub
;
3646 case LLVMAtomicRMWBinOpFMax
: return AtomicRMWInst::FMax
;
3647 case LLVMAtomicRMWBinOpFMin
: return AtomicRMWInst::FMin
;
3650 llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3653 static LLVMAtomicRMWBinOp
mapToLLVMRMWBinOp(AtomicRMWInst::BinOp BinOp
) {
3655 case AtomicRMWInst::Xchg
: return LLVMAtomicRMWBinOpXchg
;
3656 case AtomicRMWInst::Add
: return LLVMAtomicRMWBinOpAdd
;
3657 case AtomicRMWInst::Sub
: return LLVMAtomicRMWBinOpSub
;
3658 case AtomicRMWInst::And
: return LLVMAtomicRMWBinOpAnd
;
3659 case AtomicRMWInst::Nand
: return LLVMAtomicRMWBinOpNand
;
3660 case AtomicRMWInst::Or
: return LLVMAtomicRMWBinOpOr
;
3661 case AtomicRMWInst::Xor
: return LLVMAtomicRMWBinOpXor
;
3662 case AtomicRMWInst::Max
: return LLVMAtomicRMWBinOpMax
;
3663 case AtomicRMWInst::Min
: return LLVMAtomicRMWBinOpMin
;
3664 case AtomicRMWInst::UMax
: return LLVMAtomicRMWBinOpUMax
;
3665 case AtomicRMWInst::UMin
: return LLVMAtomicRMWBinOpUMin
;
3666 case AtomicRMWInst::FAdd
: return LLVMAtomicRMWBinOpFAdd
;
3667 case AtomicRMWInst::FSub
: return LLVMAtomicRMWBinOpFSub
;
3668 case AtomicRMWInst::FMax
: return LLVMAtomicRMWBinOpFMax
;
3669 case AtomicRMWInst::FMin
: return LLVMAtomicRMWBinOpFMin
;
3673 llvm_unreachable("Invalid AtomicRMWBinOp value!");
3676 // TODO: Should this and other atomic instructions support building with
3678 LLVMValueRef
LLVMBuildFence(LLVMBuilderRef B
, LLVMAtomicOrdering Ordering
,
3679 LLVMBool isSingleThread
, const char *Name
) {
3681 unwrap(B
)->CreateFence(mapFromLLVMOrdering(Ordering
),
3682 isSingleThread
? SyncScope::SingleThread
3683 : SyncScope::System
,
3687 LLVMValueRef
LLVMBuildGEP2(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3688 LLVMValueRef Pointer
, LLVMValueRef
*Indices
,
3689 unsigned NumIndices
, const char *Name
) {
3690 ArrayRef
<Value
*> IdxList(unwrap(Indices
), NumIndices
);
3691 return wrap(unwrap(B
)->CreateGEP(unwrap(Ty
), unwrap(Pointer
), IdxList
, Name
));
3694 LLVMValueRef
LLVMBuildInBoundsGEP2(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3695 LLVMValueRef Pointer
, LLVMValueRef
*Indices
,
3696 unsigned NumIndices
, const char *Name
) {
3697 ArrayRef
<Value
*> IdxList(unwrap(Indices
), NumIndices
);
3699 unwrap(B
)->CreateInBoundsGEP(unwrap(Ty
), unwrap(Pointer
), IdxList
, Name
));
3702 LLVMValueRef
LLVMBuildStructGEP2(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3703 LLVMValueRef Pointer
, unsigned Idx
,
3706 unwrap(B
)->CreateStructGEP(unwrap(Ty
), unwrap(Pointer
), Idx
, Name
));
3709 LLVMValueRef
LLVMBuildGlobalString(LLVMBuilderRef B
, const char *Str
,
3711 return wrap(unwrap(B
)->CreateGlobalString(Str
, Name
));
3714 LLVMValueRef
LLVMBuildGlobalStringPtr(LLVMBuilderRef B
, const char *Str
,
3716 return wrap(unwrap(B
)->CreateGlobalStringPtr(Str
, Name
));
3719 LLVMBool
LLVMGetVolatile(LLVMValueRef MemAccessInst
) {
3720 Value
*P
= unwrap(MemAccessInst
);
3721 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
3722 return LI
->isVolatile();
3723 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(P
))
3724 return SI
->isVolatile();
3725 if (AtomicRMWInst
*AI
= dyn_cast
<AtomicRMWInst
>(P
))
3726 return AI
->isVolatile();
3727 return cast
<AtomicCmpXchgInst
>(P
)->isVolatile();
3730 void LLVMSetVolatile(LLVMValueRef MemAccessInst
, LLVMBool isVolatile
) {
3731 Value
*P
= unwrap(MemAccessInst
);
3732 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
3733 return LI
->setVolatile(isVolatile
);
3734 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(P
))
3735 return SI
->setVolatile(isVolatile
);
3736 if (AtomicRMWInst
*AI
= dyn_cast
<AtomicRMWInst
>(P
))
3737 return AI
->setVolatile(isVolatile
);
3738 return cast
<AtomicCmpXchgInst
>(P
)->setVolatile(isVolatile
);
3741 LLVMBool
LLVMGetWeak(LLVMValueRef CmpXchgInst
) {
3742 return unwrap
<AtomicCmpXchgInst
>(CmpXchgInst
)->isWeak();
3745 void LLVMSetWeak(LLVMValueRef CmpXchgInst
, LLVMBool isWeak
) {
3746 return unwrap
<AtomicCmpXchgInst
>(CmpXchgInst
)->setWeak(isWeak
);
3749 LLVMAtomicOrdering
LLVMGetOrdering(LLVMValueRef MemAccessInst
) {
3750 Value
*P
= unwrap(MemAccessInst
);
3752 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
3753 O
= LI
->getOrdering();
3754 else if (StoreInst
*SI
= dyn_cast
<StoreInst
>(P
))
3755 O
= SI
->getOrdering();
3756 else if (FenceInst
*FI
= dyn_cast
<FenceInst
>(P
))
3757 O
= FI
->getOrdering();
3759 O
= cast
<AtomicRMWInst
>(P
)->getOrdering();
3760 return mapToLLVMOrdering(O
);
3763 void LLVMSetOrdering(LLVMValueRef MemAccessInst
, LLVMAtomicOrdering Ordering
) {
3764 Value
*P
= unwrap(MemAccessInst
);
3765 AtomicOrdering O
= mapFromLLVMOrdering(Ordering
);
3767 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
3768 return LI
->setOrdering(O
);
3769 else if (FenceInst
*FI
= dyn_cast
<FenceInst
>(P
))
3770 return FI
->setOrdering(O
);
3771 else if (AtomicRMWInst
*ARWI
= dyn_cast
<AtomicRMWInst
>(P
))
3772 return ARWI
->setOrdering(O
);
3773 return cast
<StoreInst
>(P
)->setOrdering(O
);
3776 LLVMAtomicRMWBinOp
LLVMGetAtomicRMWBinOp(LLVMValueRef Inst
) {
3777 return mapToLLVMRMWBinOp(unwrap
<AtomicRMWInst
>(Inst
)->getOperation());
3780 void LLVMSetAtomicRMWBinOp(LLVMValueRef Inst
, LLVMAtomicRMWBinOp BinOp
) {
3781 unwrap
<AtomicRMWInst
>(Inst
)->setOperation(mapFromLLVMRMWBinOp(BinOp
));
3784 /*--.. Casts ...............................................................--*/
3786 LLVMValueRef
LLVMBuildTrunc(LLVMBuilderRef B
, LLVMValueRef Val
,
3787 LLVMTypeRef DestTy
, const char *Name
) {
3788 return wrap(unwrap(B
)->CreateTrunc(unwrap(Val
), unwrap(DestTy
), Name
));
3791 LLVMValueRef
LLVMBuildZExt(LLVMBuilderRef B
, LLVMValueRef Val
,
3792 LLVMTypeRef DestTy
, const char *Name
) {
3793 return wrap(unwrap(B
)->CreateZExt(unwrap(Val
), unwrap(DestTy
), Name
));
3796 LLVMValueRef
LLVMBuildSExt(LLVMBuilderRef B
, LLVMValueRef Val
,
3797 LLVMTypeRef DestTy
, const char *Name
) {
3798 return wrap(unwrap(B
)->CreateSExt(unwrap(Val
), unwrap(DestTy
), Name
));
3801 LLVMValueRef
LLVMBuildFPToUI(LLVMBuilderRef B
, LLVMValueRef Val
,
3802 LLVMTypeRef DestTy
, const char *Name
) {
3803 return wrap(unwrap(B
)->CreateFPToUI(unwrap(Val
), unwrap(DestTy
), Name
));
3806 LLVMValueRef
LLVMBuildFPToSI(LLVMBuilderRef B
, LLVMValueRef Val
,
3807 LLVMTypeRef DestTy
, const char *Name
) {
3808 return wrap(unwrap(B
)->CreateFPToSI(unwrap(Val
), unwrap(DestTy
), Name
));
3811 LLVMValueRef
LLVMBuildUIToFP(LLVMBuilderRef B
, LLVMValueRef Val
,
3812 LLVMTypeRef DestTy
, const char *Name
) {
3813 return wrap(unwrap(B
)->CreateUIToFP(unwrap(Val
), unwrap(DestTy
), Name
));
3816 LLVMValueRef
LLVMBuildSIToFP(LLVMBuilderRef B
, LLVMValueRef Val
,
3817 LLVMTypeRef DestTy
, const char *Name
) {
3818 return wrap(unwrap(B
)->CreateSIToFP(unwrap(Val
), unwrap(DestTy
), Name
));
3821 LLVMValueRef
LLVMBuildFPTrunc(LLVMBuilderRef B
, LLVMValueRef Val
,
3822 LLVMTypeRef DestTy
, const char *Name
) {
3823 return wrap(unwrap(B
)->CreateFPTrunc(unwrap(Val
), unwrap(DestTy
), Name
));
3826 LLVMValueRef
LLVMBuildFPExt(LLVMBuilderRef B
, LLVMValueRef Val
,
3827 LLVMTypeRef DestTy
, const char *Name
) {
3828 return wrap(unwrap(B
)->CreateFPExt(unwrap(Val
), unwrap(DestTy
), Name
));
3831 LLVMValueRef
LLVMBuildPtrToInt(LLVMBuilderRef B
, LLVMValueRef Val
,
3832 LLVMTypeRef DestTy
, const char *Name
) {
3833 return wrap(unwrap(B
)->CreatePtrToInt(unwrap(Val
), unwrap(DestTy
), Name
));
3836 LLVMValueRef
LLVMBuildIntToPtr(LLVMBuilderRef B
, LLVMValueRef Val
,
3837 LLVMTypeRef DestTy
, const char *Name
) {
3838 return wrap(unwrap(B
)->CreateIntToPtr(unwrap(Val
), unwrap(DestTy
), Name
));
3841 LLVMValueRef
LLVMBuildBitCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3842 LLVMTypeRef DestTy
, const char *Name
) {
3843 return wrap(unwrap(B
)->CreateBitCast(unwrap(Val
), unwrap(DestTy
), Name
));
3846 LLVMValueRef
LLVMBuildAddrSpaceCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3847 LLVMTypeRef DestTy
, const char *Name
) {
3848 return wrap(unwrap(B
)->CreateAddrSpaceCast(unwrap(Val
), unwrap(DestTy
), Name
));
3851 LLVMValueRef
LLVMBuildZExtOrBitCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3852 LLVMTypeRef DestTy
, const char *Name
) {
3853 return wrap(unwrap(B
)->CreateZExtOrBitCast(unwrap(Val
), unwrap(DestTy
),
3857 LLVMValueRef
LLVMBuildSExtOrBitCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3858 LLVMTypeRef DestTy
, const char *Name
) {
3859 return wrap(unwrap(B
)->CreateSExtOrBitCast(unwrap(Val
), unwrap(DestTy
),
3863 LLVMValueRef
LLVMBuildTruncOrBitCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3864 LLVMTypeRef DestTy
, const char *Name
) {
3865 return wrap(unwrap(B
)->CreateTruncOrBitCast(unwrap(Val
), unwrap(DestTy
),
3869 LLVMValueRef
LLVMBuildCast(LLVMBuilderRef B
, LLVMOpcode Op
, LLVMValueRef Val
,
3870 LLVMTypeRef DestTy
, const char *Name
) {
3871 return wrap(unwrap(B
)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op
)), unwrap(Val
),
3872 unwrap(DestTy
), Name
));
3875 LLVMValueRef
LLVMBuildPointerCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3876 LLVMTypeRef DestTy
, const char *Name
) {
3877 return wrap(unwrap(B
)->CreatePointerCast(unwrap(Val
), unwrap(DestTy
), Name
));
3880 LLVMValueRef
LLVMBuildIntCast2(LLVMBuilderRef B
, LLVMValueRef Val
,
3881 LLVMTypeRef DestTy
, LLVMBool IsSigned
,
3884 unwrap(B
)->CreateIntCast(unwrap(Val
), unwrap(DestTy
), IsSigned
, Name
));
3887 LLVMValueRef
LLVMBuildIntCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3888 LLVMTypeRef DestTy
, const char *Name
) {
3889 return wrap(unwrap(B
)->CreateIntCast(unwrap(Val
), unwrap(DestTy
),
3890 /*isSigned*/true, Name
));
3893 LLVMValueRef
LLVMBuildFPCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3894 LLVMTypeRef DestTy
, const char *Name
) {
3895 return wrap(unwrap(B
)->CreateFPCast(unwrap(Val
), unwrap(DestTy
), Name
));
3898 LLVMOpcode
LLVMGetCastOpcode(LLVMValueRef Src
, LLVMBool SrcIsSigned
,
3899 LLVMTypeRef DestTy
, LLVMBool DestIsSigned
) {
3900 return map_to_llvmopcode(CastInst::getCastOpcode(
3901 unwrap(Src
), SrcIsSigned
, unwrap(DestTy
), DestIsSigned
));
3904 /*--.. Comparisons .........................................................--*/
3906 LLVMValueRef
LLVMBuildICmp(LLVMBuilderRef B
, LLVMIntPredicate Op
,
3907 LLVMValueRef LHS
, LLVMValueRef RHS
,
3909 return wrap(unwrap(B
)->CreateICmp(static_cast<ICmpInst::Predicate
>(Op
),
3910 unwrap(LHS
), unwrap(RHS
), Name
));
3913 LLVMValueRef
LLVMBuildFCmp(LLVMBuilderRef B
, LLVMRealPredicate Op
,
3914 LLVMValueRef LHS
, LLVMValueRef RHS
,
3916 return wrap(unwrap(B
)->CreateFCmp(static_cast<FCmpInst::Predicate
>(Op
),
3917 unwrap(LHS
), unwrap(RHS
), Name
));
3920 /*--.. Miscellaneous instructions ..........................................--*/
3922 LLVMValueRef
LLVMBuildPhi(LLVMBuilderRef B
, LLVMTypeRef Ty
, const char *Name
) {
3923 return wrap(unwrap(B
)->CreatePHI(unwrap(Ty
), 0, Name
));
3926 LLVMValueRef
LLVMBuildCall2(LLVMBuilderRef B
, LLVMTypeRef Ty
, LLVMValueRef Fn
,
3927 LLVMValueRef
*Args
, unsigned NumArgs
,
3929 FunctionType
*FTy
= unwrap
<FunctionType
>(Ty
);
3930 return wrap(unwrap(B
)->CreateCall(FTy
, unwrap(Fn
),
3931 ArrayRef(unwrap(Args
), NumArgs
), Name
));
3934 LLVMValueRef
LLVMBuildSelect(LLVMBuilderRef B
, LLVMValueRef If
,
3935 LLVMValueRef Then
, LLVMValueRef Else
,
3937 return wrap(unwrap(B
)->CreateSelect(unwrap(If
), unwrap(Then
), unwrap(Else
),
3941 LLVMValueRef
LLVMBuildVAArg(LLVMBuilderRef B
, LLVMValueRef List
,
3942 LLVMTypeRef Ty
, const char *Name
) {
3943 return wrap(unwrap(B
)->CreateVAArg(unwrap(List
), unwrap(Ty
), Name
));
3946 LLVMValueRef
LLVMBuildExtractElement(LLVMBuilderRef B
, LLVMValueRef VecVal
,
3947 LLVMValueRef Index
, const char *Name
) {
3948 return wrap(unwrap(B
)->CreateExtractElement(unwrap(VecVal
), unwrap(Index
),
3952 LLVMValueRef
LLVMBuildInsertElement(LLVMBuilderRef B
, LLVMValueRef VecVal
,
3953 LLVMValueRef EltVal
, LLVMValueRef Index
,
3955 return wrap(unwrap(B
)->CreateInsertElement(unwrap(VecVal
), unwrap(EltVal
),
3956 unwrap(Index
), Name
));
3959 LLVMValueRef
LLVMBuildShuffleVector(LLVMBuilderRef B
, LLVMValueRef V1
,
3960 LLVMValueRef V2
, LLVMValueRef Mask
,
3962 return wrap(unwrap(B
)->CreateShuffleVector(unwrap(V1
), unwrap(V2
),
3963 unwrap(Mask
), Name
));
3966 LLVMValueRef
LLVMBuildExtractValue(LLVMBuilderRef B
, LLVMValueRef AggVal
,
3967 unsigned Index
, const char *Name
) {
3968 return wrap(unwrap(B
)->CreateExtractValue(unwrap(AggVal
), Index
, Name
));
3971 LLVMValueRef
LLVMBuildInsertValue(LLVMBuilderRef B
, LLVMValueRef AggVal
,
3972 LLVMValueRef EltVal
, unsigned Index
,
3974 return wrap(unwrap(B
)->CreateInsertValue(unwrap(AggVal
), unwrap(EltVal
),
3978 LLVMValueRef
LLVMBuildFreeze(LLVMBuilderRef B
, LLVMValueRef Val
,
3980 return wrap(unwrap(B
)->CreateFreeze(unwrap(Val
), Name
));
3983 LLVMValueRef
LLVMBuildIsNull(LLVMBuilderRef B
, LLVMValueRef Val
,
3985 return wrap(unwrap(B
)->CreateIsNull(unwrap(Val
), Name
));
3988 LLVMValueRef
LLVMBuildIsNotNull(LLVMBuilderRef B
, LLVMValueRef Val
,
3990 return wrap(unwrap(B
)->CreateIsNotNull(unwrap(Val
), Name
));
3993 LLVMValueRef
LLVMBuildPtrDiff2(LLVMBuilderRef B
, LLVMTypeRef ElemTy
,
3994 LLVMValueRef LHS
, LLVMValueRef RHS
,
3996 return wrap(unwrap(B
)->CreatePtrDiff(unwrap(ElemTy
), unwrap(LHS
),
3997 unwrap(RHS
), Name
));
4000 LLVMValueRef
LLVMBuildAtomicRMW(LLVMBuilderRef B
,LLVMAtomicRMWBinOp op
,
4001 LLVMValueRef PTR
, LLVMValueRef Val
,
4002 LLVMAtomicOrdering ordering
,
4003 LLVMBool singleThread
) {
4004 AtomicRMWInst::BinOp intop
= mapFromLLVMRMWBinOp(op
);
4005 return wrap(unwrap(B
)->CreateAtomicRMW(
4006 intop
, unwrap(PTR
), unwrap(Val
), MaybeAlign(),
4007 mapFromLLVMOrdering(ordering
),
4008 singleThread
? SyncScope::SingleThread
: SyncScope::System
));
4011 LLVMValueRef
LLVMBuildAtomicCmpXchg(LLVMBuilderRef B
, LLVMValueRef Ptr
,
4012 LLVMValueRef Cmp
, LLVMValueRef New
,
4013 LLVMAtomicOrdering SuccessOrdering
,
4014 LLVMAtomicOrdering FailureOrdering
,
4015 LLVMBool singleThread
) {
4017 return wrap(unwrap(B
)->CreateAtomicCmpXchg(
4018 unwrap(Ptr
), unwrap(Cmp
), unwrap(New
), MaybeAlign(),
4019 mapFromLLVMOrdering(SuccessOrdering
),
4020 mapFromLLVMOrdering(FailureOrdering
),
4021 singleThread
? SyncScope::SingleThread
: SyncScope::System
));
4024 unsigned LLVMGetNumMaskElements(LLVMValueRef SVInst
) {
4025 Value
*P
= unwrap(SVInst
);
4026 ShuffleVectorInst
*I
= cast
<ShuffleVectorInst
>(P
);
4027 return I
->getShuffleMask().size();
4030 int LLVMGetMaskValue(LLVMValueRef SVInst
, unsigned Elt
) {
4031 Value
*P
= unwrap(SVInst
);
4032 ShuffleVectorInst
*I
= cast
<ShuffleVectorInst
>(P
);
4033 return I
->getMaskValue(Elt
);
4036 int LLVMGetUndefMaskElem(void) { return PoisonMaskElem
; }
4038 LLVMBool
LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst
) {
4039 Value
*P
= unwrap(AtomicInst
);
4041 if (AtomicRMWInst
*I
= dyn_cast
<AtomicRMWInst
>(P
))
4042 return I
->getSyncScopeID() == SyncScope::SingleThread
;
4043 else if (FenceInst
*FI
= dyn_cast
<FenceInst
>(P
))
4044 return FI
->getSyncScopeID() == SyncScope::SingleThread
;
4045 else if (StoreInst
*SI
= dyn_cast
<StoreInst
>(P
))
4046 return SI
->getSyncScopeID() == SyncScope::SingleThread
;
4047 else if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
4048 return LI
->getSyncScopeID() == SyncScope::SingleThread
;
4049 return cast
<AtomicCmpXchgInst
>(P
)->getSyncScopeID() ==
4050 SyncScope::SingleThread
;
4053 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst
, LLVMBool NewValue
) {
4054 Value
*P
= unwrap(AtomicInst
);
4055 SyncScope::ID SSID
= NewValue
? SyncScope::SingleThread
: SyncScope::System
;
4057 if (AtomicRMWInst
*I
= dyn_cast
<AtomicRMWInst
>(P
))
4058 return I
->setSyncScopeID(SSID
);
4059 else if (FenceInst
*FI
= dyn_cast
<FenceInst
>(P
))
4060 return FI
->setSyncScopeID(SSID
);
4061 else if (StoreInst
*SI
= dyn_cast
<StoreInst
>(P
))
4062 return SI
->setSyncScopeID(SSID
);
4063 else if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
4064 return LI
->setSyncScopeID(SSID
);
4065 return cast
<AtomicCmpXchgInst
>(P
)->setSyncScopeID(SSID
);
4068 LLVMAtomicOrdering
LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst
) {
4069 Value
*P
= unwrap(CmpXchgInst
);
4070 return mapToLLVMOrdering(cast
<AtomicCmpXchgInst
>(P
)->getSuccessOrdering());
4073 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst
,
4074 LLVMAtomicOrdering Ordering
) {
4075 Value
*P
= unwrap(CmpXchgInst
);
4076 AtomicOrdering O
= mapFromLLVMOrdering(Ordering
);
4078 return cast
<AtomicCmpXchgInst
>(P
)->setSuccessOrdering(O
);
4081 LLVMAtomicOrdering
LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst
) {
4082 Value
*P
= unwrap(CmpXchgInst
);
4083 return mapToLLVMOrdering(cast
<AtomicCmpXchgInst
>(P
)->getFailureOrdering());
4086 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst
,
4087 LLVMAtomicOrdering Ordering
) {
4088 Value
*P
= unwrap(CmpXchgInst
);
4089 AtomicOrdering O
= mapFromLLVMOrdering(Ordering
);
4091 return cast
<AtomicCmpXchgInst
>(P
)->setFailureOrdering(O
);
4094 /*===-- Module providers --------------------------------------------------===*/
4096 LLVMModuleProviderRef
4097 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M
) {
4098 return reinterpret_cast<LLVMModuleProviderRef
>(M
);
4101 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP
) {
4106 /*===-- Memory buffers ----------------------------------------------------===*/
4108 LLVMBool
LLVMCreateMemoryBufferWithContentsOfFile(
4110 LLVMMemoryBufferRef
*OutMemBuf
,
4111 char **OutMessage
) {
4113 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MBOrErr
= MemoryBuffer::getFile(Path
);
4114 if (std::error_code EC
= MBOrErr
.getError()) {
4115 *OutMessage
= strdup(EC
.message().c_str());
4118 *OutMemBuf
= wrap(MBOrErr
.get().release());
4122 LLVMBool
LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef
*OutMemBuf
,
4123 char **OutMessage
) {
4124 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MBOrErr
= MemoryBuffer::getSTDIN();
4125 if (std::error_code EC
= MBOrErr
.getError()) {
4126 *OutMessage
= strdup(EC
.message().c_str());
4129 *OutMemBuf
= wrap(MBOrErr
.get().release());
4133 LLVMMemoryBufferRef
LLVMCreateMemoryBufferWithMemoryRange(
4134 const char *InputData
,
4135 size_t InputDataLength
,
4136 const char *BufferName
,
4137 LLVMBool RequiresNullTerminator
) {
4139 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData
, InputDataLength
),
4140 StringRef(BufferName
),
4141 RequiresNullTerminator
).release());
4144 LLVMMemoryBufferRef
LLVMCreateMemoryBufferWithMemoryRangeCopy(
4145 const char *InputData
,
4146 size_t InputDataLength
,
4147 const char *BufferName
) {
4150 MemoryBuffer::getMemBufferCopy(StringRef(InputData
, InputDataLength
),
4151 StringRef(BufferName
)).release());
4154 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf
) {
4155 return unwrap(MemBuf
)->getBufferStart();
4158 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf
) {
4159 return unwrap(MemBuf
)->getBufferSize();
4162 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf
) {
4163 delete unwrap(MemBuf
);
4166 /*===-- Pass Manager ------------------------------------------------------===*/
4168 LLVMPassManagerRef
LLVMCreatePassManager() {
4169 return wrap(new legacy::PassManager());
4172 LLVMPassManagerRef
LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M
) {
4173 return wrap(new legacy::FunctionPassManager(unwrap(M
)));
4176 LLVMPassManagerRef
LLVMCreateFunctionPassManager(LLVMModuleProviderRef P
) {
4177 return LLVMCreateFunctionPassManagerForModule(
4178 reinterpret_cast<LLVMModuleRef
>(P
));
4181 LLVMBool
LLVMRunPassManager(LLVMPassManagerRef PM
, LLVMModuleRef M
) {
4182 return unwrap
<legacy::PassManager
>(PM
)->run(*unwrap(M
));
4185 LLVMBool
LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM
) {
4186 return unwrap
<legacy::FunctionPassManager
>(FPM
)->doInitialization();
4189 LLVMBool
LLVMRunFunctionPassManager(LLVMPassManagerRef FPM
, LLVMValueRef F
) {
4190 return unwrap
<legacy::FunctionPassManager
>(FPM
)->run(*unwrap
<Function
>(F
));
4193 LLVMBool
LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM
) {
4194 return unwrap
<legacy::FunctionPassManager
>(FPM
)->doFinalization();
4197 void LLVMDisposePassManager(LLVMPassManagerRef PM
) {
4201 /*===-- Threading ------------------------------------------------------===*/
4203 LLVMBool
LLVMStartMultithreaded() {
4204 return LLVMIsMultithreaded();
4207 void LLVMStopMultithreaded() {
4210 LLVMBool
LLVMIsMultithreaded() {
4211 return llvm_is_multithreaded();