1 //===-- Core.cpp ----------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the common infrastructure (including the C bindings)
11 // for libLLVMCore.a, which implements the LLVM intermediate representation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/Core.h"
16 #include "llvm/ADT/StringSwitch.h"
17 #include "llvm/IR/Attributes.h"
18 #include "llvm/IR/CallSite.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DebugInfoMetadata.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/DiagnosticInfo.h"
23 #include "llvm/IR/DiagnosticPrinter.h"
24 #include "llvm/IR/GlobalAlias.h"
25 #include "llvm/IR/GlobalVariable.h"
26 #include "llvm/IR/IRBuilder.h"
27 #include "llvm/IR/InlineAsm.h"
28 #include "llvm/IR/IntrinsicInst.h"
29 #include "llvm/IR/LLVMContext.h"
30 #include "llvm/IR/LegacyPassManager.h"
31 #include "llvm/IR/Module.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 initializePrintBasicBlockPassPass(Registry
);
53 initializeSafepointIRVerifierPass(Registry
);
54 initializeVerifierLegacyPassPass(Registry
);
57 void LLVMInitializeCore(LLVMPassRegistryRef R
) {
58 initializeCore(*unwrap(R
));
65 /*===-- Error handling ----------------------------------------------------===*/
67 char *LLVMCreateMessage(const char *Message
) {
68 return strdup(Message
);
71 void LLVMDisposeMessage(char *Message
) {
76 /*===-- Operations on contexts --------------------------------------------===*/
78 static ManagedStatic
<LLVMContext
> GlobalContext
;
80 LLVMContextRef
LLVMContextCreate() {
81 return wrap(new LLVMContext());
84 LLVMContextRef
LLVMGetGlobalContext() { return wrap(&*GlobalContext
); }
86 void LLVMContextSetDiagnosticHandler(LLVMContextRef C
,
87 LLVMDiagnosticHandler Handler
,
88 void *DiagnosticContext
) {
89 unwrap(C
)->setDiagnosticHandlerCallBack(
90 LLVM_EXTENSION
reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy
>(
95 LLVMDiagnosticHandler
LLVMContextGetDiagnosticHandler(LLVMContextRef C
) {
96 return LLVM_EXTENSION
reinterpret_cast<LLVMDiagnosticHandler
>(
97 unwrap(C
)->getDiagnosticHandlerCallBack());
100 void *LLVMContextGetDiagnosticContext(LLVMContextRef C
) {
101 return unwrap(C
)->getDiagnosticContext();
104 void LLVMContextSetYieldCallback(LLVMContextRef C
, LLVMYieldCallback Callback
,
105 void *OpaqueHandle
) {
107 LLVM_EXTENSION
reinterpret_cast<LLVMContext::YieldCallbackTy
>(Callback
);
108 unwrap(C
)->setYieldCallback(YieldCallback
, OpaqueHandle
);
111 void LLVMContextDispose(LLVMContextRef C
) {
115 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C
, const char *Name
,
117 return unwrap(C
)->getMDKindID(StringRef(Name
, SLen
));
120 unsigned LLVMGetMDKindID(const char *Name
, unsigned SLen
) {
121 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name
, SLen
);
124 #define GET_ATTR_KIND_FROM_NAME
125 #include "AttributesCompatFunc.inc"
127 unsigned LLVMGetEnumAttributeKindForName(const char *Name
, size_t SLen
) {
128 return getAttrKindFromName(StringRef(Name
, SLen
));
131 unsigned LLVMGetLastEnumAttributeKind(void) {
132 return Attribute::AttrKind::EndAttrKinds
;
135 LLVMAttributeRef
LLVMCreateEnumAttribute(LLVMContextRef C
, unsigned KindID
,
137 return wrap(Attribute::get(*unwrap(C
), (Attribute::AttrKind
)KindID
, Val
));
140 unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A
) {
141 return unwrap(A
).getKindAsEnum();
144 uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A
) {
145 auto Attr
= unwrap(A
);
146 if (Attr
.isEnumAttribute())
148 return Attr
.getValueAsInt();
151 LLVMAttributeRef
LLVMCreateStringAttribute(LLVMContextRef C
,
152 const char *K
, unsigned KLength
,
153 const char *V
, unsigned VLength
) {
154 return wrap(Attribute::get(*unwrap(C
), StringRef(K
, KLength
),
155 StringRef(V
, VLength
)));
158 const char *LLVMGetStringAttributeKind(LLVMAttributeRef A
,
160 auto S
= unwrap(A
).getKindAsString();
165 const char *LLVMGetStringAttributeValue(LLVMAttributeRef A
,
167 auto S
= unwrap(A
).getValueAsString();
172 LLVMBool
LLVMIsEnumAttribute(LLVMAttributeRef A
) {
173 auto Attr
= unwrap(A
);
174 return Attr
.isEnumAttribute() || Attr
.isIntAttribute();
177 LLVMBool
LLVMIsStringAttribute(LLVMAttributeRef A
) {
178 return unwrap(A
).isStringAttribute();
181 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI
) {
182 std::string MsgStorage
;
183 raw_string_ostream
Stream(MsgStorage
);
184 DiagnosticPrinterRawOStream
DP(Stream
);
186 unwrap(DI
)->print(DP
);
189 return LLVMCreateMessage(MsgStorage
.c_str());
192 LLVMDiagnosticSeverity
LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI
) {
193 LLVMDiagnosticSeverity severity
;
195 switch(unwrap(DI
)->getSeverity()) {
197 severity
= LLVMDSError
;
200 severity
= LLVMDSWarning
;
203 severity
= LLVMDSRemark
;
206 severity
= LLVMDSNote
;
213 /*===-- Operations on modules ---------------------------------------------===*/
215 LLVMModuleRef
LLVMModuleCreateWithName(const char *ModuleID
) {
216 return wrap(new Module(ModuleID
, *GlobalContext
));
219 LLVMModuleRef
LLVMModuleCreateWithNameInContext(const char *ModuleID
,
221 return wrap(new Module(ModuleID
, *unwrap(C
)));
224 void LLVMDisposeModule(LLVMModuleRef M
) {
228 const char *LLVMGetModuleIdentifier(LLVMModuleRef M
, size_t *Len
) {
229 auto &Str
= unwrap(M
)->getModuleIdentifier();
234 void LLVMSetModuleIdentifier(LLVMModuleRef M
, const char *Ident
, size_t Len
) {
235 unwrap(M
)->setModuleIdentifier(StringRef(Ident
, Len
));
238 const char *LLVMGetSourceFileName(LLVMModuleRef M
, size_t *Len
) {
239 auto &Str
= unwrap(M
)->getSourceFileName();
244 void LLVMSetSourceFileName(LLVMModuleRef M
, const char *Name
, size_t Len
) {
245 unwrap(M
)->setSourceFileName(StringRef(Name
, Len
));
248 /*--.. Data layout .........................................................--*/
249 const char *LLVMGetDataLayoutStr(LLVMModuleRef M
) {
250 return unwrap(M
)->getDataLayoutStr().c_str();
253 const char *LLVMGetDataLayout(LLVMModuleRef M
) {
254 return LLVMGetDataLayoutStr(M
);
257 void LLVMSetDataLayout(LLVMModuleRef M
, const char *DataLayoutStr
) {
258 unwrap(M
)->setDataLayout(DataLayoutStr
);
261 /*--.. Target triple .......................................................--*/
262 const char * LLVMGetTarget(LLVMModuleRef M
) {
263 return unwrap(M
)->getTargetTriple().c_str();
266 void LLVMSetTarget(LLVMModuleRef M
, const char *Triple
) {
267 unwrap(M
)->setTargetTriple(Triple
);
270 /*--.. Module flags ........................................................--*/
271 struct LLVMOpaqueModuleFlagEntry
{
272 LLVMModuleFlagBehavior Behavior
;
275 LLVMMetadataRef Metadata
;
278 static Module::ModFlagBehavior
279 map_to_llvmModFlagBehavior(LLVMModuleFlagBehavior Behavior
) {
281 case LLVMModuleFlagBehaviorError
:
282 return Module::ModFlagBehavior::Error
;
283 case LLVMModuleFlagBehaviorWarning
:
284 return Module::ModFlagBehavior::Warning
;
285 case LLVMModuleFlagBehaviorRequire
:
286 return Module::ModFlagBehavior::Require
;
287 case LLVMModuleFlagBehaviorOverride
:
288 return Module::ModFlagBehavior::Override
;
289 case LLVMModuleFlagBehaviorAppend
:
290 return Module::ModFlagBehavior::Append
;
291 case LLVMModuleFlagBehaviorAppendUnique
:
292 return Module::ModFlagBehavior::AppendUnique
;
294 llvm_unreachable("Unknown LLVMModuleFlagBehavior");
297 static LLVMModuleFlagBehavior
298 map_from_llvmModFlagBehavior(Module::ModFlagBehavior Behavior
) {
300 case Module::ModFlagBehavior::Error
:
301 return LLVMModuleFlagBehaviorError
;
302 case Module::ModFlagBehavior::Warning
:
303 return LLVMModuleFlagBehaviorWarning
;
304 case Module::ModFlagBehavior::Require
:
305 return LLVMModuleFlagBehaviorRequire
;
306 case Module::ModFlagBehavior::Override
:
307 return LLVMModuleFlagBehaviorOverride
;
308 case Module::ModFlagBehavior::Append
:
309 return LLVMModuleFlagBehaviorAppend
;
310 case Module::ModFlagBehavior::AppendUnique
:
311 return LLVMModuleFlagBehaviorAppendUnique
;
313 llvm_unreachable("Unhandled Flag Behavior");
317 LLVMModuleFlagEntry
*LLVMCopyModuleFlagsMetadata(LLVMModuleRef M
, size_t *Len
) {
318 SmallVector
<Module::ModuleFlagEntry
, 8> MFEs
;
319 unwrap(M
)->getModuleFlagsMetadata(MFEs
);
321 LLVMOpaqueModuleFlagEntry
*Result
= static_cast<LLVMOpaqueModuleFlagEntry
*>(
322 safe_malloc(MFEs
.size() * sizeof(LLVMOpaqueModuleFlagEntry
)));
323 for (unsigned i
= 0; i
< MFEs
.size(); ++i
) {
324 const auto &ModuleFlag
= MFEs
[i
];
325 Result
[i
].Behavior
= map_from_llvmModFlagBehavior(ModuleFlag
.Behavior
);
326 Result
[i
].Key
= ModuleFlag
.Key
->getString().data();
327 Result
[i
].KeyLen
= ModuleFlag
.Key
->getString().size();
328 Result
[i
].Metadata
= wrap(ModuleFlag
.Val
);
334 void LLVMDisposeModuleFlagsMetadata(LLVMModuleFlagEntry
*Entries
) {
338 LLVMModuleFlagBehavior
339 LLVMModuleFlagEntriesGetFlagBehavior(LLVMModuleFlagEntry
*Entries
,
341 LLVMOpaqueModuleFlagEntry MFE
=
342 static_cast<LLVMOpaqueModuleFlagEntry
>(Entries
[Index
]);
346 const char *LLVMModuleFlagEntriesGetKey(LLVMModuleFlagEntry
*Entries
,
347 unsigned Index
, size_t *Len
) {
348 LLVMOpaqueModuleFlagEntry MFE
=
349 static_cast<LLVMOpaqueModuleFlagEntry
>(Entries
[Index
]);
354 LLVMMetadataRef
LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry
*Entries
,
356 LLVMOpaqueModuleFlagEntry MFE
=
357 static_cast<LLVMOpaqueModuleFlagEntry
>(Entries
[Index
]);
361 LLVMMetadataRef
LLVMGetModuleFlag(LLVMModuleRef M
,
362 const char *Key
, size_t KeyLen
) {
363 return wrap(unwrap(M
)->getModuleFlag({Key
, KeyLen
}));
366 void LLVMAddModuleFlag(LLVMModuleRef M
, LLVMModuleFlagBehavior Behavior
,
367 const char *Key
, size_t KeyLen
,
368 LLVMMetadataRef Val
) {
369 unwrap(M
)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior
),
370 {Key
, KeyLen
}, unwrap(Val
));
373 /*--.. Printing modules ....................................................--*/
375 void LLVMDumpModule(LLVMModuleRef M
) {
376 unwrap(M
)->print(errs(), nullptr,
377 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
380 LLVMBool
LLVMPrintModuleToFile(LLVMModuleRef M
, const char *Filename
,
381 char **ErrorMessage
) {
383 raw_fd_ostream
dest(Filename
, EC
, sys::fs::F_Text
);
385 *ErrorMessage
= strdup(EC
.message().c_str());
389 unwrap(M
)->print(dest
, nullptr);
393 if (dest
.has_error()) {
394 std::string E
= "Error printing to file: " + dest
.error().message();
395 *ErrorMessage
= strdup(E
.c_str());
402 char *LLVMPrintModuleToString(LLVMModuleRef M
) {
404 raw_string_ostream
os(buf
);
406 unwrap(M
)->print(os
, nullptr);
409 return strdup(buf
.c_str());
412 /*--.. Operations on inline assembler ......................................--*/
413 void LLVMSetModuleInlineAsm2(LLVMModuleRef M
, const char *Asm
, size_t Len
) {
414 unwrap(M
)->setModuleInlineAsm(StringRef(Asm
, Len
));
417 void LLVMSetModuleInlineAsm(LLVMModuleRef M
, const char *Asm
) {
418 unwrap(M
)->setModuleInlineAsm(StringRef(Asm
));
421 void LLVMAppendModuleInlineAsm(LLVMModuleRef M
, const char *Asm
, size_t Len
) {
422 unwrap(M
)->appendModuleInlineAsm(StringRef(Asm
, Len
));
425 const char *LLVMGetModuleInlineAsm(LLVMModuleRef M
, size_t *Len
) {
426 auto &Str
= unwrap(M
)->getModuleInlineAsm();
431 LLVMValueRef
LLVMGetInlineAsm(LLVMTypeRef Ty
,
432 char *AsmString
, size_t AsmStringSize
,
433 char *Constraints
, size_t ConstraintsSize
,
434 LLVMBool HasSideEffects
, LLVMBool IsAlignStack
,
435 LLVMInlineAsmDialect Dialect
) {
436 InlineAsm::AsmDialect AD
;
438 case LLVMInlineAsmDialectATT
:
439 AD
= InlineAsm::AD_ATT
;
441 case LLVMInlineAsmDialectIntel
:
442 AD
= InlineAsm::AD_Intel
;
445 return wrap(InlineAsm::get(unwrap
<FunctionType
>(Ty
),
446 StringRef(AsmString
, AsmStringSize
),
447 StringRef(Constraints
, ConstraintsSize
),
448 HasSideEffects
, IsAlignStack
, AD
));
452 /*--.. Operations on module contexts ......................................--*/
453 LLVMContextRef
LLVMGetModuleContext(LLVMModuleRef M
) {
454 return wrap(&unwrap(M
)->getContext());
458 /*===-- Operations on types -----------------------------------------------===*/
460 /*--.. Operations on all types (mostly) ....................................--*/
462 LLVMTypeKind
LLVMGetTypeKind(LLVMTypeRef Ty
) {
463 switch (unwrap(Ty
)->getTypeID()) {
465 return LLVMVoidTypeKind
;
467 return LLVMHalfTypeKind
;
468 case Type::FloatTyID
:
469 return LLVMFloatTypeKind
;
470 case Type::DoubleTyID
:
471 return LLVMDoubleTypeKind
;
472 case Type::X86_FP80TyID
:
473 return LLVMX86_FP80TypeKind
;
474 case Type::FP128TyID
:
475 return LLVMFP128TypeKind
;
476 case Type::PPC_FP128TyID
:
477 return LLVMPPC_FP128TypeKind
;
478 case Type::LabelTyID
:
479 return LLVMLabelTypeKind
;
480 case Type::MetadataTyID
:
481 return LLVMMetadataTypeKind
;
482 case Type::IntegerTyID
:
483 return LLVMIntegerTypeKind
;
484 case Type::FunctionTyID
:
485 return LLVMFunctionTypeKind
;
486 case Type::StructTyID
:
487 return LLVMStructTypeKind
;
488 case Type::ArrayTyID
:
489 return LLVMArrayTypeKind
;
490 case Type::PointerTyID
:
491 return LLVMPointerTypeKind
;
492 case Type::VectorTyID
:
493 return LLVMVectorTypeKind
;
494 case Type::X86_MMXTyID
:
495 return LLVMX86_MMXTypeKind
;
496 case Type::TokenTyID
:
497 return LLVMTokenTypeKind
;
499 llvm_unreachable("Unhandled TypeID.");
502 LLVMBool
LLVMTypeIsSized(LLVMTypeRef Ty
)
504 return unwrap(Ty
)->isSized();
507 LLVMContextRef
LLVMGetTypeContext(LLVMTypeRef Ty
) {
508 return wrap(&unwrap(Ty
)->getContext());
511 void LLVMDumpType(LLVMTypeRef Ty
) {
512 return unwrap(Ty
)->print(errs(), /*IsForDebug=*/true);
515 char *LLVMPrintTypeToString(LLVMTypeRef Ty
) {
517 raw_string_ostream
os(buf
);
520 unwrap(Ty
)->print(os
);
522 os
<< "Printing <null> Type";
526 return strdup(buf
.c_str());
529 /*--.. Operations on integer types .........................................--*/
531 LLVMTypeRef
LLVMInt1TypeInContext(LLVMContextRef C
) {
532 return (LLVMTypeRef
) Type::getInt1Ty(*unwrap(C
));
534 LLVMTypeRef
LLVMInt8TypeInContext(LLVMContextRef C
) {
535 return (LLVMTypeRef
) Type::getInt8Ty(*unwrap(C
));
537 LLVMTypeRef
LLVMInt16TypeInContext(LLVMContextRef C
) {
538 return (LLVMTypeRef
) Type::getInt16Ty(*unwrap(C
));
540 LLVMTypeRef
LLVMInt32TypeInContext(LLVMContextRef C
) {
541 return (LLVMTypeRef
) Type::getInt32Ty(*unwrap(C
));
543 LLVMTypeRef
LLVMInt64TypeInContext(LLVMContextRef C
) {
544 return (LLVMTypeRef
) Type::getInt64Ty(*unwrap(C
));
546 LLVMTypeRef
LLVMInt128TypeInContext(LLVMContextRef C
) {
547 return (LLVMTypeRef
) Type::getInt128Ty(*unwrap(C
));
549 LLVMTypeRef
LLVMIntTypeInContext(LLVMContextRef C
, unsigned NumBits
) {
550 return wrap(IntegerType::get(*unwrap(C
), NumBits
));
553 LLVMTypeRef
LLVMInt1Type(void) {
554 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
556 LLVMTypeRef
LLVMInt8Type(void) {
557 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
559 LLVMTypeRef
LLVMInt16Type(void) {
560 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
562 LLVMTypeRef
LLVMInt32Type(void) {
563 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
565 LLVMTypeRef
LLVMInt64Type(void) {
566 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
568 LLVMTypeRef
LLVMInt128Type(void) {
569 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
571 LLVMTypeRef
LLVMIntType(unsigned NumBits
) {
572 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits
);
575 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy
) {
576 return unwrap
<IntegerType
>(IntegerTy
)->getBitWidth();
579 /*--.. Operations on real types ............................................--*/
581 LLVMTypeRef
LLVMHalfTypeInContext(LLVMContextRef C
) {
582 return (LLVMTypeRef
) Type::getHalfTy(*unwrap(C
));
584 LLVMTypeRef
LLVMFloatTypeInContext(LLVMContextRef C
) {
585 return (LLVMTypeRef
) Type::getFloatTy(*unwrap(C
));
587 LLVMTypeRef
LLVMDoubleTypeInContext(LLVMContextRef C
) {
588 return (LLVMTypeRef
) Type::getDoubleTy(*unwrap(C
));
590 LLVMTypeRef
LLVMX86FP80TypeInContext(LLVMContextRef C
) {
591 return (LLVMTypeRef
) Type::getX86_FP80Ty(*unwrap(C
));
593 LLVMTypeRef
LLVMFP128TypeInContext(LLVMContextRef C
) {
594 return (LLVMTypeRef
) Type::getFP128Ty(*unwrap(C
));
596 LLVMTypeRef
LLVMPPCFP128TypeInContext(LLVMContextRef C
) {
597 return (LLVMTypeRef
) Type::getPPC_FP128Ty(*unwrap(C
));
599 LLVMTypeRef
LLVMX86MMXTypeInContext(LLVMContextRef C
) {
600 return (LLVMTypeRef
) Type::getX86_MMXTy(*unwrap(C
));
603 LLVMTypeRef
LLVMHalfType(void) {
604 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
606 LLVMTypeRef
LLVMFloatType(void) {
607 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
609 LLVMTypeRef
LLVMDoubleType(void) {
610 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
612 LLVMTypeRef
LLVMX86FP80Type(void) {
613 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
615 LLVMTypeRef
LLVMFP128Type(void) {
616 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
618 LLVMTypeRef
LLVMPPCFP128Type(void) {
619 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
621 LLVMTypeRef
LLVMX86MMXType(void) {
622 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
625 /*--.. Operations on function types ........................................--*/
627 LLVMTypeRef
LLVMFunctionType(LLVMTypeRef ReturnType
,
628 LLVMTypeRef
*ParamTypes
, unsigned ParamCount
,
630 ArrayRef
<Type
*> Tys(unwrap(ParamTypes
), ParamCount
);
631 return wrap(FunctionType::get(unwrap(ReturnType
), Tys
, IsVarArg
!= 0));
634 LLVMBool
LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy
) {
635 return unwrap
<FunctionType
>(FunctionTy
)->isVarArg();
638 LLVMTypeRef
LLVMGetReturnType(LLVMTypeRef FunctionTy
) {
639 return wrap(unwrap
<FunctionType
>(FunctionTy
)->getReturnType());
642 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy
) {
643 return unwrap
<FunctionType
>(FunctionTy
)->getNumParams();
646 void LLVMGetParamTypes(LLVMTypeRef FunctionTy
, LLVMTypeRef
*Dest
) {
647 FunctionType
*Ty
= unwrap
<FunctionType
>(FunctionTy
);
648 for (FunctionType::param_iterator I
= Ty
->param_begin(),
649 E
= Ty
->param_end(); I
!= E
; ++I
)
653 /*--.. Operations on struct types ..........................................--*/
655 LLVMTypeRef
LLVMStructTypeInContext(LLVMContextRef C
, LLVMTypeRef
*ElementTypes
,
656 unsigned ElementCount
, LLVMBool Packed
) {
657 ArrayRef
<Type
*> Tys(unwrap(ElementTypes
), ElementCount
);
658 return wrap(StructType::get(*unwrap(C
), Tys
, Packed
!= 0));
661 LLVMTypeRef
LLVMStructType(LLVMTypeRef
*ElementTypes
,
662 unsigned ElementCount
, LLVMBool Packed
) {
663 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes
,
664 ElementCount
, Packed
);
667 LLVMTypeRef
LLVMStructCreateNamed(LLVMContextRef C
, const char *Name
)
669 return wrap(StructType::create(*unwrap(C
), Name
));
672 const char *LLVMGetStructName(LLVMTypeRef Ty
)
674 StructType
*Type
= unwrap
<StructType
>(Ty
);
675 if (!Type
->hasName())
677 return Type
->getName().data();
680 void LLVMStructSetBody(LLVMTypeRef StructTy
, LLVMTypeRef
*ElementTypes
,
681 unsigned ElementCount
, LLVMBool Packed
) {
682 ArrayRef
<Type
*> Tys(unwrap(ElementTypes
), ElementCount
);
683 unwrap
<StructType
>(StructTy
)->setBody(Tys
, Packed
!= 0);
686 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy
) {
687 return unwrap
<StructType
>(StructTy
)->getNumElements();
690 void LLVMGetStructElementTypes(LLVMTypeRef StructTy
, LLVMTypeRef
*Dest
) {
691 StructType
*Ty
= unwrap
<StructType
>(StructTy
);
692 for (StructType::element_iterator I
= Ty
->element_begin(),
693 E
= Ty
->element_end(); I
!= E
; ++I
)
697 LLVMTypeRef
LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy
, unsigned i
) {
698 StructType
*Ty
= unwrap
<StructType
>(StructTy
);
699 return wrap(Ty
->getTypeAtIndex(i
));
702 LLVMBool
LLVMIsPackedStruct(LLVMTypeRef StructTy
) {
703 return unwrap
<StructType
>(StructTy
)->isPacked();
706 LLVMBool
LLVMIsOpaqueStruct(LLVMTypeRef StructTy
) {
707 return unwrap
<StructType
>(StructTy
)->isOpaque();
710 LLVMBool
LLVMIsLiteralStruct(LLVMTypeRef StructTy
) {
711 return unwrap
<StructType
>(StructTy
)->isLiteral();
714 LLVMTypeRef
LLVMGetTypeByName(LLVMModuleRef M
, const char *Name
) {
715 return wrap(unwrap(M
)->getTypeByName(Name
));
718 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
720 void LLVMGetSubtypes(LLVMTypeRef Tp
, LLVMTypeRef
*Arr
) {
722 for (auto *T
: unwrap(Tp
)->subtypes()) {
728 LLVMTypeRef
LLVMArrayType(LLVMTypeRef ElementType
, unsigned ElementCount
) {
729 return wrap(ArrayType::get(unwrap(ElementType
), ElementCount
));
732 LLVMTypeRef
LLVMPointerType(LLVMTypeRef ElementType
, unsigned AddressSpace
) {
733 return wrap(PointerType::get(unwrap(ElementType
), AddressSpace
));
736 LLVMTypeRef
LLVMVectorType(LLVMTypeRef ElementType
, unsigned ElementCount
) {
737 return wrap(VectorType::get(unwrap(ElementType
), ElementCount
));
740 LLVMTypeRef
LLVMGetElementType(LLVMTypeRef WrappedTy
) {
741 auto *Ty
= unwrap
<Type
>(WrappedTy
);
742 if (auto *PTy
= dyn_cast
<PointerType
>(Ty
))
743 return wrap(PTy
->getElementType());
744 return wrap(cast
<SequentialType
>(Ty
)->getElementType());
747 unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp
) {
748 return unwrap(Tp
)->getNumContainedTypes();
751 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy
) {
752 return unwrap
<ArrayType
>(ArrayTy
)->getNumElements();
755 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy
) {
756 return unwrap
<PointerType
>(PointerTy
)->getAddressSpace();
759 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy
) {
760 return unwrap
<VectorType
>(VectorTy
)->getNumElements();
763 /*--.. Operations on other types ...........................................--*/
765 LLVMTypeRef
LLVMVoidTypeInContext(LLVMContextRef C
) {
766 return wrap(Type::getVoidTy(*unwrap(C
)));
768 LLVMTypeRef
LLVMLabelTypeInContext(LLVMContextRef C
) {
769 return wrap(Type::getLabelTy(*unwrap(C
)));
771 LLVMTypeRef
LLVMTokenTypeInContext(LLVMContextRef C
) {
772 return wrap(Type::getTokenTy(*unwrap(C
)));
774 LLVMTypeRef
LLVMMetadataTypeInContext(LLVMContextRef C
) {
775 return wrap(Type::getMetadataTy(*unwrap(C
)));
778 LLVMTypeRef
LLVMVoidType(void) {
779 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
781 LLVMTypeRef
LLVMLabelType(void) {
782 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
785 /*===-- Operations on values ----------------------------------------------===*/
787 /*--.. Operations on all values ............................................--*/
789 LLVMTypeRef
LLVMTypeOf(LLVMValueRef Val
) {
790 return wrap(unwrap(Val
)->getType());
793 LLVMValueKind
LLVMGetValueKind(LLVMValueRef Val
) {
794 switch(unwrap(Val
)->getValueID()) {
795 #define HANDLE_VALUE(Name) \
796 case Value::Name##Val: \
797 return LLVM##Name##ValueKind;
798 #include "llvm/IR/Value.def"
800 return LLVMInstructionValueKind
;
804 const char *LLVMGetValueName2(LLVMValueRef Val
, size_t *Length
) {
805 auto *V
= unwrap(Val
);
806 *Length
= V
->getName().size();
807 return V
->getName().data();
810 void LLVMSetValueName2(LLVMValueRef Val
, const char *Name
, size_t NameLen
) {
811 unwrap(Val
)->setName(StringRef(Name
, NameLen
));
814 const char *LLVMGetValueName(LLVMValueRef Val
) {
815 return unwrap(Val
)->getName().data();
818 void LLVMSetValueName(LLVMValueRef Val
, const char *Name
) {
819 unwrap(Val
)->setName(Name
);
822 void LLVMDumpValue(LLVMValueRef Val
) {
823 unwrap(Val
)->print(errs(), /*IsForDebug=*/true);
826 char* LLVMPrintValueToString(LLVMValueRef Val
) {
828 raw_string_ostream
os(buf
);
831 unwrap(Val
)->print(os
);
833 os
<< "Printing <null> Value";
837 return strdup(buf
.c_str());
840 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal
, LLVMValueRef NewVal
) {
841 unwrap(OldVal
)->replaceAllUsesWith(unwrap(NewVal
));
844 int LLVMHasMetadata(LLVMValueRef Inst
) {
845 return unwrap
<Instruction
>(Inst
)->hasMetadata();
848 LLVMValueRef
LLVMGetMetadata(LLVMValueRef Inst
, unsigned KindID
) {
849 auto *I
= unwrap
<Instruction
>(Inst
);
850 assert(I
&& "Expected instruction");
851 if (auto *MD
= I
->getMetadata(KindID
))
852 return wrap(MetadataAsValue::get(I
->getContext(), MD
));
856 // MetadataAsValue uses a canonical format which strips the actual MDNode for
857 // MDNode with just a single constant value, storing just a ConstantAsMetadata
858 // This undoes this canonicalization, reconstructing the MDNode.
859 static MDNode
*extractMDNode(MetadataAsValue
*MAV
) {
860 Metadata
*MD
= MAV
->getMetadata();
861 assert((isa
<MDNode
>(MD
) || isa
<ConstantAsMetadata
>(MD
)) &&
862 "Expected a metadata node or a canonicalized constant");
864 if (MDNode
*N
= dyn_cast
<MDNode
>(MD
))
867 return MDNode::get(MAV
->getContext(), MD
);
870 void LLVMSetMetadata(LLVMValueRef Inst
, unsigned KindID
, LLVMValueRef Val
) {
871 MDNode
*N
= Val
? extractMDNode(unwrap
<MetadataAsValue
>(Val
)) : nullptr;
873 unwrap
<Instruction
>(Inst
)->setMetadata(KindID
, N
);
876 struct LLVMOpaqueValueMetadataEntry
{
878 LLVMMetadataRef Metadata
;
881 using MetadataEntries
= SmallVectorImpl
<std::pair
<unsigned, MDNode
*>>;
882 static LLVMValueMetadataEntry
*
883 llvm_getMetadata(size_t *NumEntries
,
884 llvm::function_ref
<void(MetadataEntries
&)> AccessMD
) {
885 SmallVector
<std::pair
<unsigned, MDNode
*>, 8> MVEs
;
888 LLVMOpaqueValueMetadataEntry
*Result
=
889 static_cast<LLVMOpaqueValueMetadataEntry
*>(
890 safe_malloc(MVEs
.size() * sizeof(LLVMOpaqueValueMetadataEntry
)));
891 for (unsigned i
= 0; i
< MVEs
.size(); ++i
) {
892 const auto &ModuleFlag
= MVEs
[i
];
893 Result
[i
].Kind
= ModuleFlag
.first
;
894 Result
[i
].Metadata
= wrap(ModuleFlag
.second
);
896 *NumEntries
= MVEs
.size();
900 LLVMValueMetadataEntry
*
901 LLVMInstructionGetAllMetadataOtherThanDebugLoc(LLVMValueRef Value
,
902 size_t *NumEntries
) {
903 return llvm_getMetadata(NumEntries
, [&Value
](MetadataEntries
&Entries
) {
904 unwrap
<Instruction
>(Value
)->getAllMetadata(Entries
);
908 /*--.. Conversion functions ................................................--*/
910 #define LLVM_DEFINE_VALUE_CAST(name) \
911 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
912 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
915 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST
)
917 LLVMValueRef
LLVMIsAMDNode(LLVMValueRef Val
) {
918 if (auto *MD
= dyn_cast_or_null
<MetadataAsValue
>(unwrap(Val
)))
919 if (isa
<MDNode
>(MD
->getMetadata()) ||
920 isa
<ValueAsMetadata
>(MD
->getMetadata()))
925 LLVMValueRef
LLVMIsAMDString(LLVMValueRef Val
) {
926 if (auto *MD
= dyn_cast_or_null
<MetadataAsValue
>(unwrap(Val
)))
927 if (isa
<MDString
>(MD
->getMetadata()))
932 /*--.. Operations on Uses ..................................................--*/
933 LLVMUseRef
LLVMGetFirstUse(LLVMValueRef Val
) {
934 Value
*V
= unwrap(Val
);
935 Value::use_iterator I
= V
->use_begin();
936 if (I
== V
->use_end())
941 LLVMUseRef
LLVMGetNextUse(LLVMUseRef U
) {
942 Use
*Next
= unwrap(U
)->getNext();
948 LLVMValueRef
LLVMGetUser(LLVMUseRef U
) {
949 return wrap(unwrap(U
)->getUser());
952 LLVMValueRef
LLVMGetUsedValue(LLVMUseRef U
) {
953 return wrap(unwrap(U
)->get());
956 /*--.. Operations on Users .................................................--*/
958 static LLVMValueRef
getMDNodeOperandImpl(LLVMContext
&Context
, const MDNode
*N
,
960 Metadata
*Op
= N
->getOperand(Index
);
963 if (auto *C
= dyn_cast
<ConstantAsMetadata
>(Op
))
964 return wrap(C
->getValue());
965 return wrap(MetadataAsValue::get(Context
, Op
));
968 LLVMValueRef
LLVMGetOperand(LLVMValueRef Val
, unsigned Index
) {
969 Value
*V
= unwrap(Val
);
970 if (auto *MD
= dyn_cast
<MetadataAsValue
>(V
)) {
971 if (auto *L
= dyn_cast
<ValueAsMetadata
>(MD
->getMetadata())) {
972 assert(Index
== 0 && "Function-local metadata can only have one operand");
973 return wrap(L
->getValue());
975 return getMDNodeOperandImpl(V
->getContext(),
976 cast
<MDNode
>(MD
->getMetadata()), Index
);
979 return wrap(cast
<User
>(V
)->getOperand(Index
));
982 LLVMUseRef
LLVMGetOperandUse(LLVMValueRef Val
, unsigned Index
) {
983 Value
*V
= unwrap(Val
);
984 return wrap(&cast
<User
>(V
)->getOperandUse(Index
));
987 void LLVMSetOperand(LLVMValueRef Val
, unsigned Index
, LLVMValueRef Op
) {
988 unwrap
<User
>(Val
)->setOperand(Index
, unwrap(Op
));
991 int LLVMGetNumOperands(LLVMValueRef Val
) {
992 Value
*V
= unwrap(Val
);
993 if (isa
<MetadataAsValue
>(V
))
994 return LLVMGetMDNodeNumOperands(Val
);
996 return cast
<User
>(V
)->getNumOperands();
999 /*--.. Operations on constants of any type .................................--*/
1001 LLVMValueRef
LLVMConstNull(LLVMTypeRef Ty
) {
1002 return wrap(Constant::getNullValue(unwrap(Ty
)));
1005 LLVMValueRef
LLVMConstAllOnes(LLVMTypeRef Ty
) {
1006 return wrap(Constant::getAllOnesValue(unwrap(Ty
)));
1009 LLVMValueRef
LLVMGetUndef(LLVMTypeRef Ty
) {
1010 return wrap(UndefValue::get(unwrap(Ty
)));
1013 LLVMBool
LLVMIsConstant(LLVMValueRef Ty
) {
1014 return isa
<Constant
>(unwrap(Ty
));
1017 LLVMBool
LLVMIsNull(LLVMValueRef Val
) {
1018 if (Constant
*C
= dyn_cast
<Constant
>(unwrap(Val
)))
1019 return C
->isNullValue();
1023 LLVMBool
LLVMIsUndef(LLVMValueRef Val
) {
1024 return isa
<UndefValue
>(unwrap(Val
));
1027 LLVMValueRef
LLVMConstPointerNull(LLVMTypeRef Ty
) {
1028 return wrap(ConstantPointerNull::get(unwrap
<PointerType
>(Ty
)));
1031 /*--.. Operations on metadata nodes ........................................--*/
1033 LLVMValueRef
LLVMMDStringInContext(LLVMContextRef C
, const char *Str
,
1035 LLVMContext
&Context
= *unwrap(C
);
1036 return wrap(MetadataAsValue::get(
1037 Context
, MDString::get(Context
, StringRef(Str
, SLen
))));
1040 LLVMValueRef
LLVMMDString(const char *Str
, unsigned SLen
) {
1041 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str
, SLen
);
1044 LLVMValueRef
LLVMMDNodeInContext(LLVMContextRef C
, LLVMValueRef
*Vals
,
1046 LLVMContext
&Context
= *unwrap(C
);
1047 SmallVector
<Metadata
*, 8> MDs
;
1048 for (auto *OV
: makeArrayRef(Vals
, Count
)) {
1049 Value
*V
= unwrap(OV
);
1053 else if (auto *C
= dyn_cast
<Constant
>(V
))
1054 MD
= ConstantAsMetadata::get(C
);
1055 else if (auto *MDV
= dyn_cast
<MetadataAsValue
>(V
)) {
1056 MD
= MDV
->getMetadata();
1057 assert(!isa
<LocalAsMetadata
>(MD
) && "Unexpected function-local metadata "
1058 "outside of direct argument to call");
1060 // This is function-local metadata. Pretend to make an MDNode.
1061 assert(Count
== 1 &&
1062 "Expected only one operand to function-local metadata");
1063 return wrap(MetadataAsValue::get(Context
, LocalAsMetadata::get(V
)));
1068 return wrap(MetadataAsValue::get(Context
, MDNode::get(Context
, MDs
)));
1071 LLVMValueRef
LLVMMDNode(LLVMValueRef
*Vals
, unsigned Count
) {
1072 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals
, Count
);
1075 LLVMValueRef
LLVMMetadataAsValue(LLVMContextRef C
, LLVMMetadataRef MD
) {
1076 return wrap(MetadataAsValue::get(*unwrap(C
), unwrap(MD
)));
1079 LLVMMetadataRef
LLVMValueAsMetadata(LLVMValueRef Val
) {
1080 auto *V
= unwrap(Val
);
1081 if (auto *C
= dyn_cast
<Constant
>(V
))
1082 return wrap(ConstantAsMetadata::get(C
));
1083 if (auto *MAV
= dyn_cast
<MetadataAsValue
>(V
))
1084 return wrap(MAV
->getMetadata());
1085 return wrap(ValueAsMetadata::get(V
));
1088 const char *LLVMGetMDString(LLVMValueRef V
, unsigned *Length
) {
1089 if (const auto *MD
= dyn_cast
<MetadataAsValue
>(unwrap(V
)))
1090 if (const MDString
*S
= dyn_cast
<MDString
>(MD
->getMetadata())) {
1091 *Length
= S
->getString().size();
1092 return S
->getString().data();
1098 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V
) {
1099 auto *MD
= cast
<MetadataAsValue
>(unwrap(V
));
1100 if (isa
<ValueAsMetadata
>(MD
->getMetadata()))
1102 return cast
<MDNode
>(MD
->getMetadata())->getNumOperands();
1105 LLVMNamedMDNodeRef
LLVMGetFirstNamedMetadata(LLVMModuleRef M
) {
1106 Module
*Mod
= unwrap(M
);
1107 Module::named_metadata_iterator I
= Mod
->named_metadata_begin();
1108 if (I
== Mod
->named_metadata_end())
1113 LLVMNamedMDNodeRef
LLVMGetLastNamedMetadata(LLVMModuleRef M
) {
1114 Module
*Mod
= unwrap(M
);
1115 Module::named_metadata_iterator I
= Mod
->named_metadata_end();
1116 if (I
== Mod
->named_metadata_begin())
1121 LLVMNamedMDNodeRef
LLVMGetNextNamedMetadata(LLVMNamedMDNodeRef NMD
) {
1122 NamedMDNode
*NamedNode
= unwrap
<NamedMDNode
>(NMD
);
1123 Module::named_metadata_iterator
I(NamedNode
);
1124 if (++I
== NamedNode
->getParent()->named_metadata_end())
1129 LLVMNamedMDNodeRef
LLVMGetPreviousNamedMetadata(LLVMNamedMDNodeRef NMD
) {
1130 NamedMDNode
*NamedNode
= unwrap
<NamedMDNode
>(NMD
);
1131 Module::named_metadata_iterator
I(NamedNode
);
1132 if (I
== NamedNode
->getParent()->named_metadata_begin())
1137 LLVMNamedMDNodeRef
LLVMGetNamedMetadata(LLVMModuleRef M
,
1138 const char *Name
, size_t NameLen
) {
1139 return wrap(unwrap(M
)->getNamedMetadata(StringRef(Name
, NameLen
)));
1142 LLVMNamedMDNodeRef
LLVMGetOrInsertNamedMetadata(LLVMModuleRef M
,
1143 const char *Name
, size_t NameLen
) {
1144 return wrap(unwrap(M
)->getOrInsertNamedMetadata({Name
, NameLen
}));
1147 const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD
, size_t *NameLen
) {
1148 NamedMDNode
*NamedNode
= unwrap
<NamedMDNode
>(NMD
);
1149 *NameLen
= NamedNode
->getName().size();
1150 return NamedNode
->getName().data();
1153 void LLVMGetMDNodeOperands(LLVMValueRef V
, LLVMValueRef
*Dest
) {
1154 auto *MD
= cast
<MetadataAsValue
>(unwrap(V
));
1155 if (auto *MDV
= dyn_cast
<ValueAsMetadata
>(MD
->getMetadata())) {
1156 *Dest
= wrap(MDV
->getValue());
1159 const auto *N
= cast
<MDNode
>(MD
->getMetadata());
1160 const unsigned numOperands
= N
->getNumOperands();
1161 LLVMContext
&Context
= unwrap(V
)->getContext();
1162 for (unsigned i
= 0; i
< numOperands
; i
++)
1163 Dest
[i
] = getMDNodeOperandImpl(Context
, N
, i
);
1166 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M
, const char *Name
) {
1167 if (NamedMDNode
*N
= unwrap(M
)->getNamedMetadata(Name
)) {
1168 return N
->getNumOperands();
1173 void LLVMGetNamedMetadataOperands(LLVMModuleRef M
, const char *Name
,
1174 LLVMValueRef
*Dest
) {
1175 NamedMDNode
*N
= unwrap(M
)->getNamedMetadata(Name
);
1178 LLVMContext
&Context
= unwrap(M
)->getContext();
1179 for (unsigned i
=0;i
<N
->getNumOperands();i
++)
1180 Dest
[i
] = wrap(MetadataAsValue::get(Context
, N
->getOperand(i
)));
1183 void LLVMAddNamedMetadataOperand(LLVMModuleRef M
, const char *Name
,
1185 NamedMDNode
*N
= unwrap(M
)->getOrInsertNamedMetadata(Name
);
1190 N
->addOperand(extractMDNode(unwrap
<MetadataAsValue
>(Val
)));
1193 const char *LLVMGetDebugLocDirectory(LLVMValueRef Val
, unsigned *Length
) {
1194 if (!Length
) return nullptr;
1196 if (const auto *I
= unwrap
<Instruction
>(Val
)) {
1197 S
= I
->getDebugLoc()->getDirectory();
1198 } else if (const auto *GV
= unwrap
<GlobalVariable
>(Val
)) {
1199 SmallVector
<DIGlobalVariableExpression
*, 1> GVEs
;
1200 GV
->getDebugInfo(GVEs
);
1202 if (const DIGlobalVariable
*DGV
= GVEs
[0]->getVariable())
1203 S
= DGV
->getDirectory();
1204 } else if (const auto *F
= unwrap
<Function
>(Val
)) {
1205 if (const DISubprogram
*DSP
= F
->getSubprogram())
1206 S
= DSP
->getDirectory();
1208 assert(0 && "Expected Instruction, GlobalVariable or Function");
1215 const char *LLVMGetDebugLocFilename(LLVMValueRef Val
, unsigned *Length
) {
1216 if (!Length
) return nullptr;
1218 if (const auto *I
= unwrap
<Instruction
>(Val
)) {
1219 S
= I
->getDebugLoc()->getFilename();
1220 } else if (const auto *GV
= unwrap
<GlobalVariable
>(Val
)) {
1221 SmallVector
<DIGlobalVariableExpression
*, 1> GVEs
;
1222 GV
->getDebugInfo(GVEs
);
1224 if (const DIGlobalVariable
*DGV
= GVEs
[0]->getVariable())
1225 S
= DGV
->getFilename();
1226 } else if (const auto *F
= unwrap
<Function
>(Val
)) {
1227 if (const DISubprogram
*DSP
= F
->getSubprogram())
1228 S
= DSP
->getFilename();
1230 assert(0 && "Expected Instruction, GlobalVariable or Function");
1237 unsigned LLVMGetDebugLocLine(LLVMValueRef Val
) {
1239 if (const auto *I
= unwrap
<Instruction
>(Val
)) {
1240 L
= I
->getDebugLoc()->getLine();
1241 } else if (const auto *GV
= unwrap
<GlobalVariable
>(Val
)) {
1242 SmallVector
<DIGlobalVariableExpression
*, 1> GVEs
;
1243 GV
->getDebugInfo(GVEs
);
1245 if (const DIGlobalVariable
*DGV
= GVEs
[0]->getVariable())
1247 } else if (const auto *F
= unwrap
<Function
>(Val
)) {
1248 if (const DISubprogram
*DSP
= F
->getSubprogram())
1251 assert(0 && "Expected Instruction, GlobalVariable or Function");
1257 unsigned LLVMGetDebugLocColumn(LLVMValueRef Val
) {
1259 if (const auto *I
= unwrap
<Instruction
>(Val
))
1260 if (const auto &L
= I
->getDebugLoc())
1265 /*--.. Operations on scalar constants ......................................--*/
1267 LLVMValueRef
LLVMConstInt(LLVMTypeRef IntTy
, unsigned long long N
,
1268 LLVMBool SignExtend
) {
1269 return wrap(ConstantInt::get(unwrap
<IntegerType
>(IntTy
), N
, SignExtend
!= 0));
1272 LLVMValueRef
LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy
,
1274 const uint64_t Words
[]) {
1275 IntegerType
*Ty
= unwrap
<IntegerType
>(IntTy
);
1276 return wrap(ConstantInt::get(Ty
->getContext(),
1277 APInt(Ty
->getBitWidth(),
1278 makeArrayRef(Words
, NumWords
))));
1281 LLVMValueRef
LLVMConstIntOfString(LLVMTypeRef IntTy
, const char Str
[],
1283 return wrap(ConstantInt::get(unwrap
<IntegerType
>(IntTy
), StringRef(Str
),
1287 LLVMValueRef
LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy
, const char Str
[],
1288 unsigned SLen
, uint8_t Radix
) {
1289 return wrap(ConstantInt::get(unwrap
<IntegerType
>(IntTy
), StringRef(Str
, SLen
),
1293 LLVMValueRef
LLVMConstReal(LLVMTypeRef RealTy
, double N
) {
1294 return wrap(ConstantFP::get(unwrap(RealTy
), N
));
1297 LLVMValueRef
LLVMConstRealOfString(LLVMTypeRef RealTy
, const char *Text
) {
1298 return wrap(ConstantFP::get(unwrap(RealTy
), StringRef(Text
)));
1301 LLVMValueRef
LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy
, const char Str
[],
1303 return wrap(ConstantFP::get(unwrap(RealTy
), StringRef(Str
, SLen
)));
1306 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal
) {
1307 return unwrap
<ConstantInt
>(ConstantVal
)->getZExtValue();
1310 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal
) {
1311 return unwrap
<ConstantInt
>(ConstantVal
)->getSExtValue();
1314 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal
, LLVMBool
*LosesInfo
) {
1315 ConstantFP
*cFP
= unwrap
<ConstantFP
>(ConstantVal
) ;
1316 Type
*Ty
= cFP
->getType();
1318 if (Ty
->isFloatTy()) {
1320 return cFP
->getValueAPF().convertToFloat();
1323 if (Ty
->isDoubleTy()) {
1325 return cFP
->getValueAPF().convertToDouble();
1329 APFloat APF
= cFP
->getValueAPF();
1330 APF
.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven
, &APFLosesInfo
);
1331 *LosesInfo
= APFLosesInfo
;
1332 return APF
.convertToDouble();
1335 /*--.. Operations on composite constants ...................................--*/
1337 LLVMValueRef
LLVMConstStringInContext(LLVMContextRef C
, const char *Str
,
1339 LLVMBool DontNullTerminate
) {
1340 /* Inverted the sense of AddNull because ', 0)' is a
1341 better mnemonic for null termination than ', 1)'. */
1342 return wrap(ConstantDataArray::getString(*unwrap(C
), StringRef(Str
, Length
),
1343 DontNullTerminate
== 0));
1346 LLVMValueRef
LLVMConstString(const char *Str
, unsigned Length
,
1347 LLVMBool DontNullTerminate
) {
1348 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str
, Length
,
1352 LLVMValueRef
LLVMGetElementAsConstant(LLVMValueRef C
, unsigned idx
) {
1353 return wrap(unwrap
<ConstantDataSequential
>(C
)->getElementAsConstant(idx
));
1356 LLVMBool
LLVMIsConstantString(LLVMValueRef C
) {
1357 return unwrap
<ConstantDataSequential
>(C
)->isString();
1360 const char *LLVMGetAsString(LLVMValueRef C
, size_t *Length
) {
1361 StringRef Str
= unwrap
<ConstantDataSequential
>(C
)->getAsString();
1362 *Length
= Str
.size();
1366 LLVMValueRef
LLVMConstArray(LLVMTypeRef ElementTy
,
1367 LLVMValueRef
*ConstantVals
, unsigned Length
) {
1368 ArrayRef
<Constant
*> V(unwrap
<Constant
>(ConstantVals
, Length
), Length
);
1369 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy
), Length
), V
));
1372 LLVMValueRef
LLVMConstStructInContext(LLVMContextRef C
,
1373 LLVMValueRef
*ConstantVals
,
1374 unsigned Count
, LLVMBool Packed
) {
1375 Constant
**Elements
= unwrap
<Constant
>(ConstantVals
, Count
);
1376 return wrap(ConstantStruct::getAnon(*unwrap(C
), makeArrayRef(Elements
, Count
),
1380 LLVMValueRef
LLVMConstStruct(LLVMValueRef
*ConstantVals
, unsigned Count
,
1382 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals
, Count
,
1386 LLVMValueRef
LLVMConstNamedStruct(LLVMTypeRef StructTy
,
1387 LLVMValueRef
*ConstantVals
,
1389 Constant
**Elements
= unwrap
<Constant
>(ConstantVals
, Count
);
1390 StructType
*Ty
= cast
<StructType
>(unwrap(StructTy
));
1392 return wrap(ConstantStruct::get(Ty
, makeArrayRef(Elements
, Count
)));
1395 LLVMValueRef
LLVMConstVector(LLVMValueRef
*ScalarConstantVals
, unsigned Size
) {
1396 return wrap(ConstantVector::get(makeArrayRef(
1397 unwrap
<Constant
>(ScalarConstantVals
, Size
), Size
)));
1400 /*-- Opcode mapping */
1402 static LLVMOpcode
map_to_llvmopcode(int opcode
)
1405 default: llvm_unreachable("Unhandled Opcode.");
1406 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1407 #include "llvm/IR/Instruction.def"
1412 static int map_from_llvmopcode(LLVMOpcode code
)
1415 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1416 #include "llvm/IR/Instruction.def"
1419 llvm_unreachable("Unhandled Opcode.");
1422 /*--.. Constant expressions ................................................--*/
1424 LLVMOpcode
LLVMGetConstOpcode(LLVMValueRef ConstantVal
) {
1425 return map_to_llvmopcode(unwrap
<ConstantExpr
>(ConstantVal
)->getOpcode());
1428 LLVMValueRef
LLVMAlignOf(LLVMTypeRef Ty
) {
1429 return wrap(ConstantExpr::getAlignOf(unwrap(Ty
)));
1432 LLVMValueRef
LLVMSizeOf(LLVMTypeRef Ty
) {
1433 return wrap(ConstantExpr::getSizeOf(unwrap(Ty
)));
1436 LLVMValueRef
LLVMConstNeg(LLVMValueRef ConstantVal
) {
1437 return wrap(ConstantExpr::getNeg(unwrap
<Constant
>(ConstantVal
)));
1440 LLVMValueRef
LLVMConstNSWNeg(LLVMValueRef ConstantVal
) {
1441 return wrap(ConstantExpr::getNSWNeg(unwrap
<Constant
>(ConstantVal
)));
1444 LLVMValueRef
LLVMConstNUWNeg(LLVMValueRef ConstantVal
) {
1445 return wrap(ConstantExpr::getNUWNeg(unwrap
<Constant
>(ConstantVal
)));
1449 LLVMValueRef
LLVMConstFNeg(LLVMValueRef ConstantVal
) {
1450 return wrap(ConstantExpr::getFNeg(unwrap
<Constant
>(ConstantVal
)));
1453 LLVMValueRef
LLVMConstNot(LLVMValueRef ConstantVal
) {
1454 return wrap(ConstantExpr::getNot(unwrap
<Constant
>(ConstantVal
)));
1457 LLVMValueRef
LLVMConstAdd(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1458 return wrap(ConstantExpr::getAdd(unwrap
<Constant
>(LHSConstant
),
1459 unwrap
<Constant
>(RHSConstant
)));
1462 LLVMValueRef
LLVMConstNSWAdd(LLVMValueRef LHSConstant
,
1463 LLVMValueRef RHSConstant
) {
1464 return wrap(ConstantExpr::getNSWAdd(unwrap
<Constant
>(LHSConstant
),
1465 unwrap
<Constant
>(RHSConstant
)));
1468 LLVMValueRef
LLVMConstNUWAdd(LLVMValueRef LHSConstant
,
1469 LLVMValueRef RHSConstant
) {
1470 return wrap(ConstantExpr::getNUWAdd(unwrap
<Constant
>(LHSConstant
),
1471 unwrap
<Constant
>(RHSConstant
)));
1474 LLVMValueRef
LLVMConstFAdd(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1475 return wrap(ConstantExpr::getFAdd(unwrap
<Constant
>(LHSConstant
),
1476 unwrap
<Constant
>(RHSConstant
)));
1479 LLVMValueRef
LLVMConstSub(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1480 return wrap(ConstantExpr::getSub(unwrap
<Constant
>(LHSConstant
),
1481 unwrap
<Constant
>(RHSConstant
)));
1484 LLVMValueRef
LLVMConstNSWSub(LLVMValueRef LHSConstant
,
1485 LLVMValueRef RHSConstant
) {
1486 return wrap(ConstantExpr::getNSWSub(unwrap
<Constant
>(LHSConstant
),
1487 unwrap
<Constant
>(RHSConstant
)));
1490 LLVMValueRef
LLVMConstNUWSub(LLVMValueRef LHSConstant
,
1491 LLVMValueRef RHSConstant
) {
1492 return wrap(ConstantExpr::getNUWSub(unwrap
<Constant
>(LHSConstant
),
1493 unwrap
<Constant
>(RHSConstant
)));
1496 LLVMValueRef
LLVMConstFSub(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1497 return wrap(ConstantExpr::getFSub(unwrap
<Constant
>(LHSConstant
),
1498 unwrap
<Constant
>(RHSConstant
)));
1501 LLVMValueRef
LLVMConstMul(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1502 return wrap(ConstantExpr::getMul(unwrap
<Constant
>(LHSConstant
),
1503 unwrap
<Constant
>(RHSConstant
)));
1506 LLVMValueRef
LLVMConstNSWMul(LLVMValueRef LHSConstant
,
1507 LLVMValueRef RHSConstant
) {
1508 return wrap(ConstantExpr::getNSWMul(unwrap
<Constant
>(LHSConstant
),
1509 unwrap
<Constant
>(RHSConstant
)));
1512 LLVMValueRef
LLVMConstNUWMul(LLVMValueRef LHSConstant
,
1513 LLVMValueRef RHSConstant
) {
1514 return wrap(ConstantExpr::getNUWMul(unwrap
<Constant
>(LHSConstant
),
1515 unwrap
<Constant
>(RHSConstant
)));
1518 LLVMValueRef
LLVMConstFMul(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1519 return wrap(ConstantExpr::getFMul(unwrap
<Constant
>(LHSConstant
),
1520 unwrap
<Constant
>(RHSConstant
)));
1523 LLVMValueRef
LLVMConstUDiv(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1524 return wrap(ConstantExpr::getUDiv(unwrap
<Constant
>(LHSConstant
),
1525 unwrap
<Constant
>(RHSConstant
)));
1528 LLVMValueRef
LLVMConstExactUDiv(LLVMValueRef LHSConstant
,
1529 LLVMValueRef RHSConstant
) {
1530 return wrap(ConstantExpr::getExactUDiv(unwrap
<Constant
>(LHSConstant
),
1531 unwrap
<Constant
>(RHSConstant
)));
1534 LLVMValueRef
LLVMConstSDiv(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1535 return wrap(ConstantExpr::getSDiv(unwrap
<Constant
>(LHSConstant
),
1536 unwrap
<Constant
>(RHSConstant
)));
1539 LLVMValueRef
LLVMConstExactSDiv(LLVMValueRef LHSConstant
,
1540 LLVMValueRef RHSConstant
) {
1541 return wrap(ConstantExpr::getExactSDiv(unwrap
<Constant
>(LHSConstant
),
1542 unwrap
<Constant
>(RHSConstant
)));
1545 LLVMValueRef
LLVMConstFDiv(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1546 return wrap(ConstantExpr::getFDiv(unwrap
<Constant
>(LHSConstant
),
1547 unwrap
<Constant
>(RHSConstant
)));
1550 LLVMValueRef
LLVMConstURem(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1551 return wrap(ConstantExpr::getURem(unwrap
<Constant
>(LHSConstant
),
1552 unwrap
<Constant
>(RHSConstant
)));
1555 LLVMValueRef
LLVMConstSRem(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1556 return wrap(ConstantExpr::getSRem(unwrap
<Constant
>(LHSConstant
),
1557 unwrap
<Constant
>(RHSConstant
)));
1560 LLVMValueRef
LLVMConstFRem(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1561 return wrap(ConstantExpr::getFRem(unwrap
<Constant
>(LHSConstant
),
1562 unwrap
<Constant
>(RHSConstant
)));
1565 LLVMValueRef
LLVMConstAnd(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1566 return wrap(ConstantExpr::getAnd(unwrap
<Constant
>(LHSConstant
),
1567 unwrap
<Constant
>(RHSConstant
)));
1570 LLVMValueRef
LLVMConstOr(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1571 return wrap(ConstantExpr::getOr(unwrap
<Constant
>(LHSConstant
),
1572 unwrap
<Constant
>(RHSConstant
)));
1575 LLVMValueRef
LLVMConstXor(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1576 return wrap(ConstantExpr::getXor(unwrap
<Constant
>(LHSConstant
),
1577 unwrap
<Constant
>(RHSConstant
)));
1580 LLVMValueRef
LLVMConstICmp(LLVMIntPredicate Predicate
,
1581 LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1582 return wrap(ConstantExpr::getICmp(Predicate
,
1583 unwrap
<Constant
>(LHSConstant
),
1584 unwrap
<Constant
>(RHSConstant
)));
1587 LLVMValueRef
LLVMConstFCmp(LLVMRealPredicate Predicate
,
1588 LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1589 return wrap(ConstantExpr::getFCmp(Predicate
,
1590 unwrap
<Constant
>(LHSConstant
),
1591 unwrap
<Constant
>(RHSConstant
)));
1594 LLVMValueRef
LLVMConstShl(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1595 return wrap(ConstantExpr::getShl(unwrap
<Constant
>(LHSConstant
),
1596 unwrap
<Constant
>(RHSConstant
)));
1599 LLVMValueRef
LLVMConstLShr(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1600 return wrap(ConstantExpr::getLShr(unwrap
<Constant
>(LHSConstant
),
1601 unwrap
<Constant
>(RHSConstant
)));
1604 LLVMValueRef
LLVMConstAShr(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
1605 return wrap(ConstantExpr::getAShr(unwrap
<Constant
>(LHSConstant
),
1606 unwrap
<Constant
>(RHSConstant
)));
1609 LLVMValueRef
LLVMConstGEP(LLVMValueRef ConstantVal
,
1610 LLVMValueRef
*ConstantIndices
, unsigned NumIndices
) {
1611 ArrayRef
<Constant
*> IdxList(unwrap
<Constant
>(ConstantIndices
, NumIndices
),
1613 return wrap(ConstantExpr::getGetElementPtr(
1614 nullptr, unwrap
<Constant
>(ConstantVal
), IdxList
));
1617 LLVMValueRef
LLVMConstInBoundsGEP(LLVMValueRef ConstantVal
,
1618 LLVMValueRef
*ConstantIndices
,
1619 unsigned NumIndices
) {
1620 Constant
* Val
= unwrap
<Constant
>(ConstantVal
);
1621 ArrayRef
<Constant
*> IdxList(unwrap
<Constant
>(ConstantIndices
, NumIndices
),
1623 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val
, IdxList
));
1626 LLVMValueRef
LLVMConstTrunc(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1627 return wrap(ConstantExpr::getTrunc(unwrap
<Constant
>(ConstantVal
),
1631 LLVMValueRef
LLVMConstSExt(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1632 return wrap(ConstantExpr::getSExt(unwrap
<Constant
>(ConstantVal
),
1636 LLVMValueRef
LLVMConstZExt(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1637 return wrap(ConstantExpr::getZExt(unwrap
<Constant
>(ConstantVal
),
1641 LLVMValueRef
LLVMConstFPTrunc(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1642 return wrap(ConstantExpr::getFPTrunc(unwrap
<Constant
>(ConstantVal
),
1646 LLVMValueRef
LLVMConstFPExt(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1647 return wrap(ConstantExpr::getFPExtend(unwrap
<Constant
>(ConstantVal
),
1651 LLVMValueRef
LLVMConstUIToFP(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1652 return wrap(ConstantExpr::getUIToFP(unwrap
<Constant
>(ConstantVal
),
1656 LLVMValueRef
LLVMConstSIToFP(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1657 return wrap(ConstantExpr::getSIToFP(unwrap
<Constant
>(ConstantVal
),
1661 LLVMValueRef
LLVMConstFPToUI(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1662 return wrap(ConstantExpr::getFPToUI(unwrap
<Constant
>(ConstantVal
),
1666 LLVMValueRef
LLVMConstFPToSI(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1667 return wrap(ConstantExpr::getFPToSI(unwrap
<Constant
>(ConstantVal
),
1671 LLVMValueRef
LLVMConstPtrToInt(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1672 return wrap(ConstantExpr::getPtrToInt(unwrap
<Constant
>(ConstantVal
),
1676 LLVMValueRef
LLVMConstIntToPtr(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1677 return wrap(ConstantExpr::getIntToPtr(unwrap
<Constant
>(ConstantVal
),
1681 LLVMValueRef
LLVMConstBitCast(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1682 return wrap(ConstantExpr::getBitCast(unwrap
<Constant
>(ConstantVal
),
1686 LLVMValueRef
LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal
,
1687 LLVMTypeRef ToType
) {
1688 return wrap(ConstantExpr::getAddrSpaceCast(unwrap
<Constant
>(ConstantVal
),
1692 LLVMValueRef
LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal
,
1693 LLVMTypeRef ToType
) {
1694 return wrap(ConstantExpr::getZExtOrBitCast(unwrap
<Constant
>(ConstantVal
),
1698 LLVMValueRef
LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal
,
1699 LLVMTypeRef ToType
) {
1700 return wrap(ConstantExpr::getSExtOrBitCast(unwrap
<Constant
>(ConstantVal
),
1704 LLVMValueRef
LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal
,
1705 LLVMTypeRef ToType
) {
1706 return wrap(ConstantExpr::getTruncOrBitCast(unwrap
<Constant
>(ConstantVal
),
1710 LLVMValueRef
LLVMConstPointerCast(LLVMValueRef ConstantVal
,
1711 LLVMTypeRef ToType
) {
1712 return wrap(ConstantExpr::getPointerCast(unwrap
<Constant
>(ConstantVal
),
1716 LLVMValueRef
LLVMConstIntCast(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
,
1717 LLVMBool isSigned
) {
1718 return wrap(ConstantExpr::getIntegerCast(unwrap
<Constant
>(ConstantVal
),
1719 unwrap(ToType
), isSigned
));
1722 LLVMValueRef
LLVMConstFPCast(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
1723 return wrap(ConstantExpr::getFPCast(unwrap
<Constant
>(ConstantVal
),
1727 LLVMValueRef
LLVMConstSelect(LLVMValueRef ConstantCondition
,
1728 LLVMValueRef ConstantIfTrue
,
1729 LLVMValueRef ConstantIfFalse
) {
1730 return wrap(ConstantExpr::getSelect(unwrap
<Constant
>(ConstantCondition
),
1731 unwrap
<Constant
>(ConstantIfTrue
),
1732 unwrap
<Constant
>(ConstantIfFalse
)));
1735 LLVMValueRef
LLVMConstExtractElement(LLVMValueRef VectorConstant
,
1736 LLVMValueRef IndexConstant
) {
1737 return wrap(ConstantExpr::getExtractElement(unwrap
<Constant
>(VectorConstant
),
1738 unwrap
<Constant
>(IndexConstant
)));
1741 LLVMValueRef
LLVMConstInsertElement(LLVMValueRef VectorConstant
,
1742 LLVMValueRef ElementValueConstant
,
1743 LLVMValueRef IndexConstant
) {
1744 return wrap(ConstantExpr::getInsertElement(unwrap
<Constant
>(VectorConstant
),
1745 unwrap
<Constant
>(ElementValueConstant
),
1746 unwrap
<Constant
>(IndexConstant
)));
1749 LLVMValueRef
LLVMConstShuffleVector(LLVMValueRef VectorAConstant
,
1750 LLVMValueRef VectorBConstant
,
1751 LLVMValueRef MaskConstant
) {
1752 return wrap(ConstantExpr::getShuffleVector(unwrap
<Constant
>(VectorAConstant
),
1753 unwrap
<Constant
>(VectorBConstant
),
1754 unwrap
<Constant
>(MaskConstant
)));
1757 LLVMValueRef
LLVMConstExtractValue(LLVMValueRef AggConstant
, unsigned *IdxList
,
1759 return wrap(ConstantExpr::getExtractValue(unwrap
<Constant
>(AggConstant
),
1760 makeArrayRef(IdxList
, NumIdx
)));
1763 LLVMValueRef
LLVMConstInsertValue(LLVMValueRef AggConstant
,
1764 LLVMValueRef ElementValueConstant
,
1765 unsigned *IdxList
, unsigned NumIdx
) {
1766 return wrap(ConstantExpr::getInsertValue(unwrap
<Constant
>(AggConstant
),
1767 unwrap
<Constant
>(ElementValueConstant
),
1768 makeArrayRef(IdxList
, NumIdx
)));
1771 LLVMValueRef
LLVMConstInlineAsm(LLVMTypeRef Ty
, const char *AsmString
,
1772 const char *Constraints
,
1773 LLVMBool HasSideEffects
,
1774 LLVMBool IsAlignStack
) {
1775 return wrap(InlineAsm::get(dyn_cast
<FunctionType
>(unwrap(Ty
)), AsmString
,
1776 Constraints
, HasSideEffects
, IsAlignStack
));
1779 LLVMValueRef
LLVMBlockAddress(LLVMValueRef F
, LLVMBasicBlockRef BB
) {
1780 return wrap(BlockAddress::get(unwrap
<Function
>(F
), unwrap(BB
)));
1783 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1785 LLVMModuleRef
LLVMGetGlobalParent(LLVMValueRef Global
) {
1786 return wrap(unwrap
<GlobalValue
>(Global
)->getParent());
1789 LLVMBool
LLVMIsDeclaration(LLVMValueRef Global
) {
1790 return unwrap
<GlobalValue
>(Global
)->isDeclaration();
1793 LLVMLinkage
LLVMGetLinkage(LLVMValueRef Global
) {
1794 switch (unwrap
<GlobalValue
>(Global
)->getLinkage()) {
1795 case GlobalValue::ExternalLinkage
:
1796 return LLVMExternalLinkage
;
1797 case GlobalValue::AvailableExternallyLinkage
:
1798 return LLVMAvailableExternallyLinkage
;
1799 case GlobalValue::LinkOnceAnyLinkage
:
1800 return LLVMLinkOnceAnyLinkage
;
1801 case GlobalValue::LinkOnceODRLinkage
:
1802 return LLVMLinkOnceODRLinkage
;
1803 case GlobalValue::WeakAnyLinkage
:
1804 return LLVMWeakAnyLinkage
;
1805 case GlobalValue::WeakODRLinkage
:
1806 return LLVMWeakODRLinkage
;
1807 case GlobalValue::AppendingLinkage
:
1808 return LLVMAppendingLinkage
;
1809 case GlobalValue::InternalLinkage
:
1810 return LLVMInternalLinkage
;
1811 case GlobalValue::PrivateLinkage
:
1812 return LLVMPrivateLinkage
;
1813 case GlobalValue::ExternalWeakLinkage
:
1814 return LLVMExternalWeakLinkage
;
1815 case GlobalValue::CommonLinkage
:
1816 return LLVMCommonLinkage
;
1819 llvm_unreachable("Invalid GlobalValue linkage!");
1822 void LLVMSetLinkage(LLVMValueRef Global
, LLVMLinkage Linkage
) {
1823 GlobalValue
*GV
= unwrap
<GlobalValue
>(Global
);
1826 case LLVMExternalLinkage
:
1827 GV
->setLinkage(GlobalValue::ExternalLinkage
);
1829 case LLVMAvailableExternallyLinkage
:
1830 GV
->setLinkage(GlobalValue::AvailableExternallyLinkage
);
1832 case LLVMLinkOnceAnyLinkage
:
1833 GV
->setLinkage(GlobalValue::LinkOnceAnyLinkage
);
1835 case LLVMLinkOnceODRLinkage
:
1836 GV
->setLinkage(GlobalValue::LinkOnceODRLinkage
);
1838 case LLVMLinkOnceODRAutoHideLinkage
:
1840 errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1841 "longer supported.");
1843 case LLVMWeakAnyLinkage
:
1844 GV
->setLinkage(GlobalValue::WeakAnyLinkage
);
1846 case LLVMWeakODRLinkage
:
1847 GV
->setLinkage(GlobalValue::WeakODRLinkage
);
1849 case LLVMAppendingLinkage
:
1850 GV
->setLinkage(GlobalValue::AppendingLinkage
);
1852 case LLVMInternalLinkage
:
1853 GV
->setLinkage(GlobalValue::InternalLinkage
);
1855 case LLVMPrivateLinkage
:
1856 GV
->setLinkage(GlobalValue::PrivateLinkage
);
1858 case LLVMLinkerPrivateLinkage
:
1859 GV
->setLinkage(GlobalValue::PrivateLinkage
);
1861 case LLVMLinkerPrivateWeakLinkage
:
1862 GV
->setLinkage(GlobalValue::PrivateLinkage
);
1864 case LLVMDLLImportLinkage
:
1867 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1869 case LLVMDLLExportLinkage
:
1872 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1874 case LLVMExternalWeakLinkage
:
1875 GV
->setLinkage(GlobalValue::ExternalWeakLinkage
);
1877 case LLVMGhostLinkage
:
1879 errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1881 case LLVMCommonLinkage
:
1882 GV
->setLinkage(GlobalValue::CommonLinkage
);
1887 const char *LLVMGetSection(LLVMValueRef Global
) {
1888 // Using .data() is safe because of how GlobalObject::setSection is
1890 return unwrap
<GlobalValue
>(Global
)->getSection().data();
1893 void LLVMSetSection(LLVMValueRef Global
, const char *Section
) {
1894 unwrap
<GlobalObject
>(Global
)->setSection(Section
);
1897 LLVMVisibility
LLVMGetVisibility(LLVMValueRef Global
) {
1898 return static_cast<LLVMVisibility
>(
1899 unwrap
<GlobalValue
>(Global
)->getVisibility());
1902 void LLVMSetVisibility(LLVMValueRef Global
, LLVMVisibility Viz
) {
1903 unwrap
<GlobalValue
>(Global
)
1904 ->setVisibility(static_cast<GlobalValue::VisibilityTypes
>(Viz
));
1907 LLVMDLLStorageClass
LLVMGetDLLStorageClass(LLVMValueRef Global
) {
1908 return static_cast<LLVMDLLStorageClass
>(
1909 unwrap
<GlobalValue
>(Global
)->getDLLStorageClass());
1912 void LLVMSetDLLStorageClass(LLVMValueRef Global
, LLVMDLLStorageClass Class
) {
1913 unwrap
<GlobalValue
>(Global
)->setDLLStorageClass(
1914 static_cast<GlobalValue::DLLStorageClassTypes
>(Class
));
1917 LLVMUnnamedAddr
LLVMGetUnnamedAddress(LLVMValueRef Global
) {
1918 switch (unwrap
<GlobalValue
>(Global
)->getUnnamedAddr()) {
1919 case GlobalVariable::UnnamedAddr::None
:
1920 return LLVMNoUnnamedAddr
;
1921 case GlobalVariable::UnnamedAddr::Local
:
1922 return LLVMLocalUnnamedAddr
;
1923 case GlobalVariable::UnnamedAddr::Global
:
1924 return LLVMGlobalUnnamedAddr
;
1926 llvm_unreachable("Unknown UnnamedAddr kind!");
1929 void LLVMSetUnnamedAddress(LLVMValueRef Global
, LLVMUnnamedAddr UnnamedAddr
) {
1930 GlobalValue
*GV
= unwrap
<GlobalValue
>(Global
);
1932 switch (UnnamedAddr
) {
1933 case LLVMNoUnnamedAddr
:
1934 return GV
->setUnnamedAddr(GlobalVariable::UnnamedAddr::None
);
1935 case LLVMLocalUnnamedAddr
:
1936 return GV
->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local
);
1937 case LLVMGlobalUnnamedAddr
:
1938 return GV
->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global
);
1942 LLVMBool
LLVMHasUnnamedAddr(LLVMValueRef Global
) {
1943 return unwrap
<GlobalValue
>(Global
)->hasGlobalUnnamedAddr();
1946 void LLVMSetUnnamedAddr(LLVMValueRef Global
, LLVMBool HasUnnamedAddr
) {
1947 unwrap
<GlobalValue
>(Global
)->setUnnamedAddr(
1948 HasUnnamedAddr
? GlobalValue::UnnamedAddr::Global
1949 : GlobalValue::UnnamedAddr::None
);
1952 LLVMTypeRef
LLVMGlobalGetValueType(LLVMValueRef Global
) {
1953 return wrap(unwrap
<GlobalValue
>(Global
)->getValueType());
1956 /*--.. Operations on global variables, load and store instructions .........--*/
1958 unsigned LLVMGetAlignment(LLVMValueRef V
) {
1959 Value
*P
= unwrap
<Value
>(V
);
1960 if (GlobalValue
*GV
= dyn_cast
<GlobalValue
>(P
))
1961 return GV
->getAlignment();
1962 if (AllocaInst
*AI
= dyn_cast
<AllocaInst
>(P
))
1963 return AI
->getAlignment();
1964 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
1965 return LI
->getAlignment();
1966 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(P
))
1967 return SI
->getAlignment();
1970 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1973 void LLVMSetAlignment(LLVMValueRef V
, unsigned Bytes
) {
1974 Value
*P
= unwrap
<Value
>(V
);
1975 if (GlobalObject
*GV
= dyn_cast
<GlobalObject
>(P
))
1976 GV
->setAlignment(Bytes
);
1977 else if (AllocaInst
*AI
= dyn_cast
<AllocaInst
>(P
))
1978 AI
->setAlignment(Bytes
);
1979 else if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
1980 LI
->setAlignment(Bytes
);
1981 else if (StoreInst
*SI
= dyn_cast
<StoreInst
>(P
))
1982 SI
->setAlignment(Bytes
);
1985 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1988 LLVMValueMetadataEntry
*LLVMGlobalCopyAllMetadata(LLVMValueRef Value
,
1989 size_t *NumEntries
) {
1990 return llvm_getMetadata(NumEntries
, [&Value
](MetadataEntries
&Entries
) {
1991 if (Instruction
*Instr
= dyn_cast
<Instruction
>(unwrap(Value
))) {
1992 Instr
->getAllMetadata(Entries
);
1994 unwrap
<GlobalObject
>(Value
)->getAllMetadata(Entries
);
1999 unsigned LLVMValueMetadataEntriesGetKind(LLVMValueMetadataEntry
*Entries
,
2001 LLVMOpaqueValueMetadataEntry MVE
=
2002 static_cast<LLVMOpaqueValueMetadataEntry
>(Entries
[Index
]);
2007 LLVMValueMetadataEntriesGetMetadata(LLVMValueMetadataEntry
*Entries
,
2009 LLVMOpaqueValueMetadataEntry MVE
=
2010 static_cast<LLVMOpaqueValueMetadataEntry
>(Entries
[Index
]);
2011 return MVE
.Metadata
;
2014 void LLVMDisposeValueMetadataEntries(LLVMValueMetadataEntry
*Entries
) {
2018 void LLVMGlobalSetMetadata(LLVMValueRef Global
, unsigned Kind
,
2019 LLVMMetadataRef MD
) {
2020 unwrap
<GlobalObject
>(Global
)->setMetadata(Kind
, unwrap
<MDNode
>(MD
));
2023 void LLVMGlobalEraseMetadata(LLVMValueRef Global
, unsigned Kind
) {
2024 unwrap
<GlobalObject
>(Global
)->eraseMetadata(Kind
);
2027 void LLVMGlobalClearMetadata(LLVMValueRef Global
) {
2028 unwrap
<GlobalObject
>(Global
)->clearMetadata();
2031 /*--.. Operations on global variables ......................................--*/
2033 LLVMValueRef
LLVMAddGlobal(LLVMModuleRef M
, LLVMTypeRef Ty
, const char *Name
) {
2034 return wrap(new GlobalVariable(*unwrap(M
), unwrap(Ty
), false,
2035 GlobalValue::ExternalLinkage
, nullptr, Name
));
2038 LLVMValueRef
LLVMAddGlobalInAddressSpace(LLVMModuleRef M
, LLVMTypeRef Ty
,
2040 unsigned AddressSpace
) {
2041 return wrap(new GlobalVariable(*unwrap(M
), unwrap(Ty
), false,
2042 GlobalValue::ExternalLinkage
, nullptr, Name
,
2043 nullptr, GlobalVariable::NotThreadLocal
,
2047 LLVMValueRef
LLVMGetNamedGlobal(LLVMModuleRef M
, const char *Name
) {
2048 return wrap(unwrap(M
)->getNamedGlobal(Name
));
2051 LLVMValueRef
LLVMGetFirstGlobal(LLVMModuleRef M
) {
2052 Module
*Mod
= unwrap(M
);
2053 Module::global_iterator I
= Mod
->global_begin();
2054 if (I
== Mod
->global_end())
2059 LLVMValueRef
LLVMGetLastGlobal(LLVMModuleRef M
) {
2060 Module
*Mod
= unwrap(M
);
2061 Module::global_iterator I
= Mod
->global_end();
2062 if (I
== Mod
->global_begin())
2067 LLVMValueRef
LLVMGetNextGlobal(LLVMValueRef GlobalVar
) {
2068 GlobalVariable
*GV
= unwrap
<GlobalVariable
>(GlobalVar
);
2069 Module::global_iterator
I(GV
);
2070 if (++I
== GV
->getParent()->global_end())
2075 LLVMValueRef
LLVMGetPreviousGlobal(LLVMValueRef GlobalVar
) {
2076 GlobalVariable
*GV
= unwrap
<GlobalVariable
>(GlobalVar
);
2077 Module::global_iterator
I(GV
);
2078 if (I
== GV
->getParent()->global_begin())
2083 void LLVMDeleteGlobal(LLVMValueRef GlobalVar
) {
2084 unwrap
<GlobalVariable
>(GlobalVar
)->eraseFromParent();
2087 LLVMValueRef
LLVMGetInitializer(LLVMValueRef GlobalVar
) {
2088 GlobalVariable
* GV
= unwrap
<GlobalVariable
>(GlobalVar
);
2089 if ( !GV
->hasInitializer() )
2091 return wrap(GV
->getInitializer());
2094 void LLVMSetInitializer(LLVMValueRef GlobalVar
, LLVMValueRef ConstantVal
) {
2095 unwrap
<GlobalVariable
>(GlobalVar
)
2096 ->setInitializer(unwrap
<Constant
>(ConstantVal
));
2099 LLVMBool
LLVMIsThreadLocal(LLVMValueRef GlobalVar
) {
2100 return unwrap
<GlobalVariable
>(GlobalVar
)->isThreadLocal();
2103 void LLVMSetThreadLocal(LLVMValueRef GlobalVar
, LLVMBool IsThreadLocal
) {
2104 unwrap
<GlobalVariable
>(GlobalVar
)->setThreadLocal(IsThreadLocal
!= 0);
2107 LLVMBool
LLVMIsGlobalConstant(LLVMValueRef GlobalVar
) {
2108 return unwrap
<GlobalVariable
>(GlobalVar
)->isConstant();
2111 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar
, LLVMBool IsConstant
) {
2112 unwrap
<GlobalVariable
>(GlobalVar
)->setConstant(IsConstant
!= 0);
2115 LLVMThreadLocalMode
LLVMGetThreadLocalMode(LLVMValueRef GlobalVar
) {
2116 switch (unwrap
<GlobalVariable
>(GlobalVar
)->getThreadLocalMode()) {
2117 case GlobalVariable::NotThreadLocal
:
2118 return LLVMNotThreadLocal
;
2119 case GlobalVariable::GeneralDynamicTLSModel
:
2120 return LLVMGeneralDynamicTLSModel
;
2121 case GlobalVariable::LocalDynamicTLSModel
:
2122 return LLVMLocalDynamicTLSModel
;
2123 case GlobalVariable::InitialExecTLSModel
:
2124 return LLVMInitialExecTLSModel
;
2125 case GlobalVariable::LocalExecTLSModel
:
2126 return LLVMLocalExecTLSModel
;
2129 llvm_unreachable("Invalid GlobalVariable thread local mode");
2132 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar
, LLVMThreadLocalMode Mode
) {
2133 GlobalVariable
*GV
= unwrap
<GlobalVariable
>(GlobalVar
);
2136 case LLVMNotThreadLocal
:
2137 GV
->setThreadLocalMode(GlobalVariable::NotThreadLocal
);
2139 case LLVMGeneralDynamicTLSModel
:
2140 GV
->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel
);
2142 case LLVMLocalDynamicTLSModel
:
2143 GV
->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel
);
2145 case LLVMInitialExecTLSModel
:
2146 GV
->setThreadLocalMode(GlobalVariable::InitialExecTLSModel
);
2148 case LLVMLocalExecTLSModel
:
2149 GV
->setThreadLocalMode(GlobalVariable::LocalExecTLSModel
);
2154 LLVMBool
LLVMIsExternallyInitialized(LLVMValueRef GlobalVar
) {
2155 return unwrap
<GlobalVariable
>(GlobalVar
)->isExternallyInitialized();
2158 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar
, LLVMBool IsExtInit
) {
2159 unwrap
<GlobalVariable
>(GlobalVar
)->setExternallyInitialized(IsExtInit
);
2162 /*--.. Operations on aliases ......................................--*/
2164 LLVMValueRef
LLVMAddAlias(LLVMModuleRef M
, LLVMTypeRef Ty
, LLVMValueRef Aliasee
,
2166 auto *PTy
= cast
<PointerType
>(unwrap(Ty
));
2167 return wrap(GlobalAlias::create(PTy
->getElementType(), PTy
->getAddressSpace(),
2168 GlobalValue::ExternalLinkage
, Name
,
2169 unwrap
<Constant
>(Aliasee
), unwrap(M
)));
2172 LLVMValueRef
LLVMGetNamedGlobalAlias(LLVMModuleRef M
,
2173 const char *Name
, size_t NameLen
) {
2174 return wrap(unwrap(M
)->getNamedAlias(Name
));
2177 LLVMValueRef
LLVMGetFirstGlobalAlias(LLVMModuleRef M
) {
2178 Module
*Mod
= unwrap(M
);
2179 Module::alias_iterator I
= Mod
->alias_begin();
2180 if (I
== Mod
->alias_end())
2185 LLVMValueRef
LLVMGetLastGlobalAlias(LLVMModuleRef M
) {
2186 Module
*Mod
= unwrap(M
);
2187 Module::alias_iterator I
= Mod
->alias_end();
2188 if (I
== Mod
->alias_begin())
2193 LLVMValueRef
LLVMGetNextGlobalAlias(LLVMValueRef GA
) {
2194 GlobalAlias
*Alias
= unwrap
<GlobalAlias
>(GA
);
2195 Module::alias_iterator
I(Alias
);
2196 if (++I
== Alias
->getParent()->alias_end())
2201 LLVMValueRef
LLVMGetPreviousGlobalAlias(LLVMValueRef GA
) {
2202 GlobalAlias
*Alias
= unwrap
<GlobalAlias
>(GA
);
2203 Module::alias_iterator
I(Alias
);
2204 if (I
== Alias
->getParent()->alias_begin())
2209 LLVMValueRef
LLVMAliasGetAliasee(LLVMValueRef Alias
) {
2210 return wrap(unwrap
<GlobalAlias
>(Alias
)->getAliasee());
2213 void LLVMAliasSetAliasee(LLVMValueRef Alias
, LLVMValueRef Aliasee
) {
2214 unwrap
<GlobalAlias
>(Alias
)->setAliasee(unwrap
<Constant
>(Aliasee
));
2217 /*--.. Operations on functions .............................................--*/
2219 LLVMValueRef
LLVMAddFunction(LLVMModuleRef M
, const char *Name
,
2220 LLVMTypeRef FunctionTy
) {
2221 return wrap(Function::Create(unwrap
<FunctionType
>(FunctionTy
),
2222 GlobalValue::ExternalLinkage
, Name
, unwrap(M
)));
2225 LLVMValueRef
LLVMGetNamedFunction(LLVMModuleRef M
, const char *Name
) {
2226 return wrap(unwrap(M
)->getFunction(Name
));
2229 LLVMValueRef
LLVMGetFirstFunction(LLVMModuleRef M
) {
2230 Module
*Mod
= unwrap(M
);
2231 Module::iterator I
= Mod
->begin();
2232 if (I
== Mod
->end())
2237 LLVMValueRef
LLVMGetLastFunction(LLVMModuleRef M
) {
2238 Module
*Mod
= unwrap(M
);
2239 Module::iterator I
= Mod
->end();
2240 if (I
== Mod
->begin())
2245 LLVMValueRef
LLVMGetNextFunction(LLVMValueRef Fn
) {
2246 Function
*Func
= unwrap
<Function
>(Fn
);
2247 Module::iterator
I(Func
);
2248 if (++I
== Func
->getParent()->end())
2253 LLVMValueRef
LLVMGetPreviousFunction(LLVMValueRef Fn
) {
2254 Function
*Func
= unwrap
<Function
>(Fn
);
2255 Module::iterator
I(Func
);
2256 if (I
== Func
->getParent()->begin())
2261 void LLVMDeleteFunction(LLVMValueRef Fn
) {
2262 unwrap
<Function
>(Fn
)->eraseFromParent();
2265 LLVMBool
LLVMHasPersonalityFn(LLVMValueRef Fn
) {
2266 return unwrap
<Function
>(Fn
)->hasPersonalityFn();
2269 LLVMValueRef
LLVMGetPersonalityFn(LLVMValueRef Fn
) {
2270 return wrap(unwrap
<Function
>(Fn
)->getPersonalityFn());
2273 void LLVMSetPersonalityFn(LLVMValueRef Fn
, LLVMValueRef PersonalityFn
) {
2274 unwrap
<Function
>(Fn
)->setPersonalityFn(unwrap
<Constant
>(PersonalityFn
));
2277 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn
) {
2278 if (Function
*F
= dyn_cast
<Function
>(unwrap(Fn
)))
2279 return F
->getIntrinsicID();
2283 static Intrinsic::ID
llvm_map_to_intrinsic_id(unsigned ID
) {
2284 assert(ID
< llvm::Intrinsic::num_intrinsics
&& "Intrinsic ID out of range");
2285 return llvm::Intrinsic::ID(ID
);
2288 LLVMValueRef
LLVMGetIntrinsicDeclaration(LLVMModuleRef Mod
,
2290 LLVMTypeRef
*ParamTypes
,
2291 size_t ParamCount
) {
2292 ArrayRef
<Type
*> Tys(unwrap(ParamTypes
), ParamCount
);
2293 auto IID
= llvm_map_to_intrinsic_id(ID
);
2294 return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod
), IID
, Tys
));
2297 const char *LLVMIntrinsicGetName(unsigned ID
, size_t *NameLength
) {
2298 auto IID
= llvm_map_to_intrinsic_id(ID
);
2299 auto Str
= llvm::Intrinsic::getName(IID
);
2300 *NameLength
= Str
.size();
2304 LLVMTypeRef
LLVMIntrinsicGetType(LLVMContextRef Ctx
, unsigned ID
,
2305 LLVMTypeRef
*ParamTypes
, size_t ParamCount
) {
2306 auto IID
= llvm_map_to_intrinsic_id(ID
);
2307 ArrayRef
<Type
*> Tys(unwrap(ParamTypes
), ParamCount
);
2308 return wrap(llvm::Intrinsic::getType(*unwrap(Ctx
), IID
, Tys
));
2311 const char *LLVMIntrinsicCopyOverloadedName(unsigned ID
,
2312 LLVMTypeRef
*ParamTypes
,
2314 size_t *NameLength
) {
2315 auto IID
= llvm_map_to_intrinsic_id(ID
);
2316 ArrayRef
<Type
*> Tys(unwrap(ParamTypes
), ParamCount
);
2317 auto Str
= llvm::Intrinsic::getName(IID
, Tys
);
2318 *NameLength
= Str
.length();
2319 return strdup(Str
.c_str());
2322 LLVMBool
LLVMIntrinsicIsOverloaded(unsigned ID
) {
2323 auto IID
= llvm_map_to_intrinsic_id(ID
);
2324 return llvm::Intrinsic::isOverloaded(IID
);
2327 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn
) {
2328 return unwrap
<Function
>(Fn
)->getCallingConv();
2331 void LLVMSetFunctionCallConv(LLVMValueRef Fn
, unsigned CC
) {
2332 return unwrap
<Function
>(Fn
)->setCallingConv(
2333 static_cast<CallingConv::ID
>(CC
));
2336 const char *LLVMGetGC(LLVMValueRef Fn
) {
2337 Function
*F
= unwrap
<Function
>(Fn
);
2338 return F
->hasGC()? F
->getGC().c_str() : nullptr;
2341 void LLVMSetGC(LLVMValueRef Fn
, const char *GC
) {
2342 Function
*F
= unwrap
<Function
>(Fn
);
2349 void LLVMAddAttributeAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
,
2350 LLVMAttributeRef A
) {
2351 unwrap
<Function
>(F
)->addAttribute(Idx
, unwrap(A
));
2354 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
) {
2355 auto AS
= unwrap
<Function
>(F
)->getAttributes().getAttributes(Idx
);
2356 return AS
.getNumAttributes();
2359 void LLVMGetAttributesAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
,
2360 LLVMAttributeRef
*Attrs
) {
2361 auto AS
= unwrap
<Function
>(F
)->getAttributes().getAttributes(Idx
);
2366 LLVMAttributeRef
LLVMGetEnumAttributeAtIndex(LLVMValueRef F
,
2367 LLVMAttributeIndex Idx
,
2369 return wrap(unwrap
<Function
>(F
)->getAttribute(Idx
,
2370 (Attribute::AttrKind
)KindID
));
2373 LLVMAttributeRef
LLVMGetStringAttributeAtIndex(LLVMValueRef F
,
2374 LLVMAttributeIndex Idx
,
2375 const char *K
, unsigned KLen
) {
2376 return wrap(unwrap
<Function
>(F
)->getAttribute(Idx
, StringRef(K
, KLen
)));
2379 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
,
2381 unwrap
<Function
>(F
)->removeAttribute(Idx
, (Attribute::AttrKind
)KindID
);
2384 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
,
2385 const char *K
, unsigned KLen
) {
2386 unwrap
<Function
>(F
)->removeAttribute(Idx
, StringRef(K
, KLen
));
2389 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn
, const char *A
,
2391 Function
*Func
= unwrap
<Function
>(Fn
);
2392 Attribute Attr
= Attribute::get(Func
->getContext(), A
, V
);
2393 Func
->addAttribute(AttributeList::FunctionIndex
, Attr
);
2396 /*--.. Operations on parameters ............................................--*/
2398 unsigned LLVMCountParams(LLVMValueRef FnRef
) {
2399 // This function is strictly redundant to
2400 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2401 return unwrap
<Function
>(FnRef
)->arg_size();
2404 void LLVMGetParams(LLVMValueRef FnRef
, LLVMValueRef
*ParamRefs
) {
2405 Function
*Fn
= unwrap
<Function
>(FnRef
);
2406 for (Function::arg_iterator I
= Fn
->arg_begin(),
2407 E
= Fn
->arg_end(); I
!= E
; I
++)
2408 *ParamRefs
++ = wrap(&*I
);
2411 LLVMValueRef
LLVMGetParam(LLVMValueRef FnRef
, unsigned index
) {
2412 Function
*Fn
= unwrap
<Function
>(FnRef
);
2413 return wrap(&Fn
->arg_begin()[index
]);
2416 LLVMValueRef
LLVMGetParamParent(LLVMValueRef V
) {
2417 return wrap(unwrap
<Argument
>(V
)->getParent());
2420 LLVMValueRef
LLVMGetFirstParam(LLVMValueRef Fn
) {
2421 Function
*Func
= unwrap
<Function
>(Fn
);
2422 Function::arg_iterator I
= Func
->arg_begin();
2423 if (I
== Func
->arg_end())
2428 LLVMValueRef
LLVMGetLastParam(LLVMValueRef Fn
) {
2429 Function
*Func
= unwrap
<Function
>(Fn
);
2430 Function::arg_iterator I
= Func
->arg_end();
2431 if (I
== Func
->arg_begin())
2436 LLVMValueRef
LLVMGetNextParam(LLVMValueRef Arg
) {
2437 Argument
*A
= unwrap
<Argument
>(Arg
);
2438 Function
*Fn
= A
->getParent();
2439 if (A
->getArgNo() + 1 >= Fn
->arg_size())
2441 return wrap(&Fn
->arg_begin()[A
->getArgNo() + 1]);
2444 LLVMValueRef
LLVMGetPreviousParam(LLVMValueRef Arg
) {
2445 Argument
*A
= unwrap
<Argument
>(Arg
);
2446 if (A
->getArgNo() == 0)
2448 return wrap(&A
->getParent()->arg_begin()[A
->getArgNo() - 1]);
2451 void LLVMSetParamAlignment(LLVMValueRef Arg
, unsigned align
) {
2452 Argument
*A
= unwrap
<Argument
>(Arg
);
2453 A
->addAttr(Attribute::getWithAlignment(A
->getContext(), align
));
2456 /*--.. Operations on basic blocks ..........................................--*/
2458 LLVMValueRef
LLVMBasicBlockAsValue(LLVMBasicBlockRef BB
) {
2459 return wrap(static_cast<Value
*>(unwrap(BB
)));
2462 LLVMBool
LLVMValueIsBasicBlock(LLVMValueRef Val
) {
2463 return isa
<BasicBlock
>(unwrap(Val
));
2466 LLVMBasicBlockRef
LLVMValueAsBasicBlock(LLVMValueRef Val
) {
2467 return wrap(unwrap
<BasicBlock
>(Val
));
2470 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB
) {
2471 return unwrap(BB
)->getName().data();
2474 LLVMValueRef
LLVMGetBasicBlockParent(LLVMBasicBlockRef BB
) {
2475 return wrap(unwrap(BB
)->getParent());
2478 LLVMValueRef
LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB
) {
2479 return wrap(unwrap(BB
)->getTerminator());
2482 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef
) {
2483 return unwrap
<Function
>(FnRef
)->size();
2486 void LLVMGetBasicBlocks(LLVMValueRef FnRef
, LLVMBasicBlockRef
*BasicBlocksRefs
){
2487 Function
*Fn
= unwrap
<Function
>(FnRef
);
2488 for (BasicBlock
&BB
: *Fn
)
2489 *BasicBlocksRefs
++ = wrap(&BB
);
2492 LLVMBasicBlockRef
LLVMGetEntryBasicBlock(LLVMValueRef Fn
) {
2493 return wrap(&unwrap
<Function
>(Fn
)->getEntryBlock());
2496 LLVMBasicBlockRef
LLVMGetFirstBasicBlock(LLVMValueRef Fn
) {
2497 Function
*Func
= unwrap
<Function
>(Fn
);
2498 Function::iterator I
= Func
->begin();
2499 if (I
== Func
->end())
2504 LLVMBasicBlockRef
LLVMGetLastBasicBlock(LLVMValueRef Fn
) {
2505 Function
*Func
= unwrap
<Function
>(Fn
);
2506 Function::iterator I
= Func
->end();
2507 if (I
== Func
->begin())
2512 LLVMBasicBlockRef
LLVMGetNextBasicBlock(LLVMBasicBlockRef BB
) {
2513 BasicBlock
*Block
= unwrap(BB
);
2514 Function::iterator
I(Block
);
2515 if (++I
== Block
->getParent()->end())
2520 LLVMBasicBlockRef
LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB
) {
2521 BasicBlock
*Block
= unwrap(BB
);
2522 Function::iterator
I(Block
);
2523 if (I
== Block
->getParent()->begin())
2528 LLVMBasicBlockRef
LLVMAppendBasicBlockInContext(LLVMContextRef C
,
2531 return wrap(BasicBlock::Create(*unwrap(C
), Name
, unwrap
<Function
>(FnRef
)));
2534 LLVMBasicBlockRef
LLVMAppendBasicBlock(LLVMValueRef FnRef
, const char *Name
) {
2535 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef
, Name
);
2538 LLVMBasicBlockRef
LLVMInsertBasicBlockInContext(LLVMContextRef C
,
2539 LLVMBasicBlockRef BBRef
,
2541 BasicBlock
*BB
= unwrap(BBRef
);
2542 return wrap(BasicBlock::Create(*unwrap(C
), Name
, BB
->getParent(), BB
));
2545 LLVMBasicBlockRef
LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef
,
2547 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef
, Name
);
2550 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef
) {
2551 unwrap(BBRef
)->eraseFromParent();
2554 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef
) {
2555 unwrap(BBRef
)->removeFromParent();
2558 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB
, LLVMBasicBlockRef MovePos
) {
2559 unwrap(BB
)->moveBefore(unwrap(MovePos
));
2562 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB
, LLVMBasicBlockRef MovePos
) {
2563 unwrap(BB
)->moveAfter(unwrap(MovePos
));
2566 /*--.. Operations on instructions ..........................................--*/
2568 LLVMBasicBlockRef
LLVMGetInstructionParent(LLVMValueRef Inst
) {
2569 return wrap(unwrap
<Instruction
>(Inst
)->getParent());
2572 LLVMValueRef
LLVMGetFirstInstruction(LLVMBasicBlockRef BB
) {
2573 BasicBlock
*Block
= unwrap(BB
);
2574 BasicBlock::iterator I
= Block
->begin();
2575 if (I
== Block
->end())
2580 LLVMValueRef
LLVMGetLastInstruction(LLVMBasicBlockRef BB
) {
2581 BasicBlock
*Block
= unwrap(BB
);
2582 BasicBlock::iterator I
= Block
->end();
2583 if (I
== Block
->begin())
2588 LLVMValueRef
LLVMGetNextInstruction(LLVMValueRef Inst
) {
2589 Instruction
*Instr
= unwrap
<Instruction
>(Inst
);
2590 BasicBlock::iterator
I(Instr
);
2591 if (++I
== Instr
->getParent()->end())
2596 LLVMValueRef
LLVMGetPreviousInstruction(LLVMValueRef Inst
) {
2597 Instruction
*Instr
= unwrap
<Instruction
>(Inst
);
2598 BasicBlock::iterator
I(Instr
);
2599 if (I
== Instr
->getParent()->begin())
2604 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst
) {
2605 unwrap
<Instruction
>(Inst
)->removeFromParent();
2608 void LLVMInstructionEraseFromParent(LLVMValueRef Inst
) {
2609 unwrap
<Instruction
>(Inst
)->eraseFromParent();
2612 LLVMIntPredicate
LLVMGetICmpPredicate(LLVMValueRef Inst
) {
2613 if (ICmpInst
*I
= dyn_cast
<ICmpInst
>(unwrap(Inst
)))
2614 return (LLVMIntPredicate
)I
->getPredicate();
2615 if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(unwrap(Inst
)))
2616 if (CE
->getOpcode() == Instruction::ICmp
)
2617 return (LLVMIntPredicate
)CE
->getPredicate();
2618 return (LLVMIntPredicate
)0;
2621 LLVMRealPredicate
LLVMGetFCmpPredicate(LLVMValueRef Inst
) {
2622 if (FCmpInst
*I
= dyn_cast
<FCmpInst
>(unwrap(Inst
)))
2623 return (LLVMRealPredicate
)I
->getPredicate();
2624 if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(unwrap(Inst
)))
2625 if (CE
->getOpcode() == Instruction::FCmp
)
2626 return (LLVMRealPredicate
)CE
->getPredicate();
2627 return (LLVMRealPredicate
)0;
2630 LLVMOpcode
LLVMGetInstructionOpcode(LLVMValueRef Inst
) {
2631 if (Instruction
*C
= dyn_cast
<Instruction
>(unwrap(Inst
)))
2632 return map_to_llvmopcode(C
->getOpcode());
2633 return (LLVMOpcode
)0;
2636 LLVMValueRef
LLVMInstructionClone(LLVMValueRef Inst
) {
2637 if (Instruction
*C
= dyn_cast
<Instruction
>(unwrap(Inst
)))
2638 return wrap(C
->clone());
2642 LLVMValueRef
LLVMIsATerminatorInst(LLVMValueRef Inst
) {
2643 Instruction
*I
= dyn_cast
<Instruction
>(unwrap(Inst
));
2644 return (I
&& I
->isTerminator()) ? wrap(I
) : nullptr;
2647 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr
) {
2648 if (FuncletPadInst
*FPI
= dyn_cast
<FuncletPadInst
>(unwrap(Instr
))) {
2649 return FPI
->getNumArgOperands();
2651 return CallSite(unwrap
<Instruction
>(Instr
)).getNumArgOperands();
2654 /*--.. Call and invoke instructions ........................................--*/
2656 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr
) {
2657 return CallSite(unwrap
<Instruction
>(Instr
)).getCallingConv();
2660 void LLVMSetInstructionCallConv(LLVMValueRef Instr
, unsigned CC
) {
2661 return CallSite(unwrap
<Instruction
>(Instr
))
2662 .setCallingConv(static_cast<CallingConv::ID
>(CC
));
2665 void LLVMSetInstrParamAlignment(LLVMValueRef Instr
, unsigned index
,
2667 CallSite Call
= CallSite(unwrap
<Instruction
>(Instr
));
2668 Attribute AlignAttr
= Attribute::getWithAlignment(Call
->getContext(), align
);
2669 Call
.addAttribute(index
, AlignAttr
);
2672 void LLVMAddCallSiteAttribute(LLVMValueRef C
, LLVMAttributeIndex Idx
,
2673 LLVMAttributeRef A
) {
2674 CallSite(unwrap
<Instruction
>(C
)).addAttribute(Idx
, unwrap(A
));
2677 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C
,
2678 LLVMAttributeIndex Idx
) {
2679 auto CS
= CallSite(unwrap
<Instruction
>(C
));
2680 auto AS
= CS
.getAttributes().getAttributes(Idx
);
2681 return AS
.getNumAttributes();
2684 void LLVMGetCallSiteAttributes(LLVMValueRef C
, LLVMAttributeIndex Idx
,
2685 LLVMAttributeRef
*Attrs
) {
2686 auto CS
= CallSite(unwrap
<Instruction
>(C
));
2687 auto AS
= CS
.getAttributes().getAttributes(Idx
);
2692 LLVMAttributeRef
LLVMGetCallSiteEnumAttribute(LLVMValueRef C
,
2693 LLVMAttributeIndex Idx
,
2695 return wrap(CallSite(unwrap
<Instruction
>(C
))
2696 .getAttribute(Idx
, (Attribute::AttrKind
)KindID
));
2699 LLVMAttributeRef
LLVMGetCallSiteStringAttribute(LLVMValueRef C
,
2700 LLVMAttributeIndex Idx
,
2701 const char *K
, unsigned KLen
) {
2702 return wrap(CallSite(unwrap
<Instruction
>(C
))
2703 .getAttribute(Idx
, StringRef(K
, KLen
)));
2706 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C
, LLVMAttributeIndex Idx
,
2708 CallSite(unwrap
<Instruction
>(C
))
2709 .removeAttribute(Idx
, (Attribute::AttrKind
)KindID
);
2712 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C
, LLVMAttributeIndex Idx
,
2713 const char *K
, unsigned KLen
) {
2714 CallSite(unwrap
<Instruction
>(C
)).removeAttribute(Idx
, StringRef(K
, KLen
));
2717 LLVMValueRef
LLVMGetCalledValue(LLVMValueRef Instr
) {
2718 return wrap(CallSite(unwrap
<Instruction
>(Instr
)).getCalledValue());
2721 /*--.. Operations on call instructions (only) ..............................--*/
2723 LLVMBool
LLVMIsTailCall(LLVMValueRef Call
) {
2724 return unwrap
<CallInst
>(Call
)->isTailCall();
2727 void LLVMSetTailCall(LLVMValueRef Call
, LLVMBool isTailCall
) {
2728 unwrap
<CallInst
>(Call
)->setTailCall(isTailCall
);
2731 /*--.. Operations on invoke instructions (only) ............................--*/
2733 LLVMBasicBlockRef
LLVMGetNormalDest(LLVMValueRef Invoke
) {
2734 return wrap(unwrap
<InvokeInst
>(Invoke
)->getNormalDest());
2737 LLVMBasicBlockRef
LLVMGetUnwindDest(LLVMValueRef Invoke
) {
2738 if (CleanupReturnInst
*CRI
= dyn_cast
<CleanupReturnInst
>(unwrap(Invoke
))) {
2739 return wrap(CRI
->getUnwindDest());
2740 } else if (CatchSwitchInst
*CSI
= dyn_cast
<CatchSwitchInst
>(unwrap(Invoke
))) {
2741 return wrap(CSI
->getUnwindDest());
2743 return wrap(unwrap
<InvokeInst
>(Invoke
)->getUnwindDest());
2746 void LLVMSetNormalDest(LLVMValueRef Invoke
, LLVMBasicBlockRef B
) {
2747 unwrap
<InvokeInst
>(Invoke
)->setNormalDest(unwrap(B
));
2750 void LLVMSetUnwindDest(LLVMValueRef Invoke
, LLVMBasicBlockRef B
) {
2751 if (CleanupReturnInst
*CRI
= dyn_cast
<CleanupReturnInst
>(unwrap(Invoke
))) {
2752 return CRI
->setUnwindDest(unwrap(B
));
2753 } else if (CatchSwitchInst
*CSI
= dyn_cast
<CatchSwitchInst
>(unwrap(Invoke
))) {
2754 return CSI
->setUnwindDest(unwrap(B
));
2756 unwrap
<InvokeInst
>(Invoke
)->setUnwindDest(unwrap(B
));
2759 /*--.. Operations on terminators ...........................................--*/
2761 unsigned LLVMGetNumSuccessors(LLVMValueRef Term
) {
2762 return unwrap
<Instruction
>(Term
)->getNumSuccessors();
2765 LLVMBasicBlockRef
LLVMGetSuccessor(LLVMValueRef Term
, unsigned i
) {
2766 return wrap(unwrap
<Instruction
>(Term
)->getSuccessor(i
));
2769 void LLVMSetSuccessor(LLVMValueRef Term
, unsigned i
, LLVMBasicBlockRef block
) {
2770 return unwrap
<Instruction
>(Term
)->setSuccessor(i
, unwrap(block
));
2773 /*--.. Operations on branch instructions (only) ............................--*/
2775 LLVMBool
LLVMIsConditional(LLVMValueRef Branch
) {
2776 return unwrap
<BranchInst
>(Branch
)->isConditional();
2779 LLVMValueRef
LLVMGetCondition(LLVMValueRef Branch
) {
2780 return wrap(unwrap
<BranchInst
>(Branch
)->getCondition());
2783 void LLVMSetCondition(LLVMValueRef Branch
, LLVMValueRef Cond
) {
2784 return unwrap
<BranchInst
>(Branch
)->setCondition(unwrap(Cond
));
2787 /*--.. Operations on switch instructions (only) ............................--*/
2789 LLVMBasicBlockRef
LLVMGetSwitchDefaultDest(LLVMValueRef Switch
) {
2790 return wrap(unwrap
<SwitchInst
>(Switch
)->getDefaultDest());
2793 /*--.. Operations on alloca instructions (only) ............................--*/
2795 LLVMTypeRef
LLVMGetAllocatedType(LLVMValueRef Alloca
) {
2796 return wrap(unwrap
<AllocaInst
>(Alloca
)->getAllocatedType());
2799 /*--.. Operations on gep instructions (only) ...............................--*/
2801 LLVMBool
LLVMIsInBounds(LLVMValueRef GEP
) {
2802 return unwrap
<GetElementPtrInst
>(GEP
)->isInBounds();
2805 void LLVMSetIsInBounds(LLVMValueRef GEP
, LLVMBool InBounds
) {
2806 return unwrap
<GetElementPtrInst
>(GEP
)->setIsInBounds(InBounds
);
2809 /*--.. Operations on phi nodes .............................................--*/
2811 void LLVMAddIncoming(LLVMValueRef PhiNode
, LLVMValueRef
*IncomingValues
,
2812 LLVMBasicBlockRef
*IncomingBlocks
, unsigned Count
) {
2813 PHINode
*PhiVal
= unwrap
<PHINode
>(PhiNode
);
2814 for (unsigned I
= 0; I
!= Count
; ++I
)
2815 PhiVal
->addIncoming(unwrap(IncomingValues
[I
]), unwrap(IncomingBlocks
[I
]));
2818 unsigned LLVMCountIncoming(LLVMValueRef PhiNode
) {
2819 return unwrap
<PHINode
>(PhiNode
)->getNumIncomingValues();
2822 LLVMValueRef
LLVMGetIncomingValue(LLVMValueRef PhiNode
, unsigned Index
) {
2823 return wrap(unwrap
<PHINode
>(PhiNode
)->getIncomingValue(Index
));
2826 LLVMBasicBlockRef
LLVMGetIncomingBlock(LLVMValueRef PhiNode
, unsigned Index
) {
2827 return wrap(unwrap
<PHINode
>(PhiNode
)->getIncomingBlock(Index
));
2830 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2832 unsigned LLVMGetNumIndices(LLVMValueRef Inst
) {
2833 auto *I
= unwrap(Inst
);
2834 if (auto *GEP
= dyn_cast
<GetElementPtrInst
>(I
))
2835 return GEP
->getNumIndices();
2836 if (auto *EV
= dyn_cast
<ExtractValueInst
>(I
))
2837 return EV
->getNumIndices();
2838 if (auto *IV
= dyn_cast
<InsertValueInst
>(I
))
2839 return IV
->getNumIndices();
2840 if (auto *CE
= dyn_cast
<ConstantExpr
>(I
))
2841 return CE
->getIndices().size();
2843 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2846 const unsigned *LLVMGetIndices(LLVMValueRef Inst
) {
2847 auto *I
= unwrap(Inst
);
2848 if (auto *EV
= dyn_cast
<ExtractValueInst
>(I
))
2849 return EV
->getIndices().data();
2850 if (auto *IV
= dyn_cast
<InsertValueInst
>(I
))
2851 return IV
->getIndices().data();
2852 if (auto *CE
= dyn_cast
<ConstantExpr
>(I
))
2853 return CE
->getIndices().data();
2855 "LLVMGetIndices applies only to extractvalue and insertvalue!");
2859 /*===-- Instruction builders ----------------------------------------------===*/
2861 LLVMBuilderRef
LLVMCreateBuilderInContext(LLVMContextRef C
) {
2862 return wrap(new IRBuilder
<>(*unwrap(C
)));
2865 LLVMBuilderRef
LLVMCreateBuilder(void) {
2866 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
2869 void LLVMPositionBuilder(LLVMBuilderRef Builder
, LLVMBasicBlockRef Block
,
2870 LLVMValueRef Instr
) {
2871 BasicBlock
*BB
= unwrap(Block
);
2872 auto I
= Instr
? unwrap
<Instruction
>(Instr
)->getIterator() : BB
->end();
2873 unwrap(Builder
)->SetInsertPoint(BB
, I
);
2876 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder
, LLVMValueRef Instr
) {
2877 Instruction
*I
= unwrap
<Instruction
>(Instr
);
2878 unwrap(Builder
)->SetInsertPoint(I
->getParent(), I
->getIterator());
2881 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder
, LLVMBasicBlockRef Block
) {
2882 BasicBlock
*BB
= unwrap(Block
);
2883 unwrap(Builder
)->SetInsertPoint(BB
);
2886 LLVMBasicBlockRef
LLVMGetInsertBlock(LLVMBuilderRef Builder
) {
2887 return wrap(unwrap(Builder
)->GetInsertBlock());
2890 void LLVMClearInsertionPosition(LLVMBuilderRef Builder
) {
2891 unwrap(Builder
)->ClearInsertionPoint();
2894 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder
, LLVMValueRef Instr
) {
2895 unwrap(Builder
)->Insert(unwrap
<Instruction
>(Instr
));
2898 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder
, LLVMValueRef Instr
,
2900 unwrap(Builder
)->Insert(unwrap
<Instruction
>(Instr
), Name
);
2903 void LLVMDisposeBuilder(LLVMBuilderRef Builder
) {
2904 delete unwrap(Builder
);
2907 /*--.. Metadata builders ...................................................--*/
2909 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder
, LLVMValueRef L
) {
2911 L
? cast
<MDNode
>(unwrap
<MetadataAsValue
>(L
)->getMetadata()) : nullptr;
2912 unwrap(Builder
)->SetCurrentDebugLocation(DebugLoc(Loc
));
2915 LLVMValueRef
LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder
) {
2916 LLVMContext
&Context
= unwrap(Builder
)->getContext();
2917 return wrap(MetadataAsValue::get(
2918 Context
, unwrap(Builder
)->getCurrentDebugLocation().getAsMDNode()));
2921 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder
, LLVMValueRef Inst
) {
2922 unwrap(Builder
)->SetInstDebugLocation(unwrap
<Instruction
>(Inst
));
2926 /*--.. Instruction builders ................................................--*/
2928 LLVMValueRef
LLVMBuildRetVoid(LLVMBuilderRef B
) {
2929 return wrap(unwrap(B
)->CreateRetVoid());
2932 LLVMValueRef
LLVMBuildRet(LLVMBuilderRef B
, LLVMValueRef V
) {
2933 return wrap(unwrap(B
)->CreateRet(unwrap(V
)));
2936 LLVMValueRef
LLVMBuildAggregateRet(LLVMBuilderRef B
, LLVMValueRef
*RetVals
,
2938 return wrap(unwrap(B
)->CreateAggregateRet(unwrap(RetVals
), N
));
2941 LLVMValueRef
LLVMBuildBr(LLVMBuilderRef B
, LLVMBasicBlockRef Dest
) {
2942 return wrap(unwrap(B
)->CreateBr(unwrap(Dest
)));
2945 LLVMValueRef
LLVMBuildCondBr(LLVMBuilderRef B
, LLVMValueRef If
,
2946 LLVMBasicBlockRef Then
, LLVMBasicBlockRef Else
) {
2947 return wrap(unwrap(B
)->CreateCondBr(unwrap(If
), unwrap(Then
), unwrap(Else
)));
2950 LLVMValueRef
LLVMBuildSwitch(LLVMBuilderRef B
, LLVMValueRef V
,
2951 LLVMBasicBlockRef Else
, unsigned NumCases
) {
2952 return wrap(unwrap(B
)->CreateSwitch(unwrap(V
), unwrap(Else
), NumCases
));
2955 LLVMValueRef
LLVMBuildIndirectBr(LLVMBuilderRef B
, LLVMValueRef Addr
,
2956 unsigned NumDests
) {
2957 return wrap(unwrap(B
)->CreateIndirectBr(unwrap(Addr
), NumDests
));
2960 LLVMValueRef
LLVMBuildInvoke(LLVMBuilderRef B
, LLVMValueRef Fn
,
2961 LLVMValueRef
*Args
, unsigned NumArgs
,
2962 LLVMBasicBlockRef Then
, LLVMBasicBlockRef Catch
,
2964 return wrap(unwrap(B
)->CreateInvoke(unwrap(Fn
), unwrap(Then
), unwrap(Catch
),
2965 makeArrayRef(unwrap(Args
), NumArgs
),
2969 LLVMValueRef
LLVMBuildLandingPad(LLVMBuilderRef B
, LLVMTypeRef Ty
,
2970 LLVMValueRef PersFn
, unsigned NumClauses
,
2972 // The personality used to live on the landingpad instruction, but now it
2973 // lives on the parent function. For compatibility, take the provided
2974 // personality and put it on the parent function.
2976 unwrap(B
)->GetInsertBlock()->getParent()->setPersonalityFn(
2977 cast
<Function
>(unwrap(PersFn
)));
2978 return wrap(unwrap(B
)->CreateLandingPad(unwrap(Ty
), NumClauses
, Name
));
2981 LLVMValueRef
LLVMBuildCatchPad(LLVMBuilderRef B
, LLVMValueRef ParentPad
,
2982 LLVMValueRef
*Args
, unsigned NumArgs
,
2984 return wrap(unwrap(B
)->CreateCatchPad(unwrap(ParentPad
),
2985 makeArrayRef(unwrap(Args
), NumArgs
),
2989 LLVMValueRef
LLVMBuildCleanupPad(LLVMBuilderRef B
, LLVMValueRef ParentPad
,
2990 LLVMValueRef
*Args
, unsigned NumArgs
,
2992 if (ParentPad
== nullptr) {
2993 Type
*Ty
= Type::getTokenTy(unwrap(B
)->getContext());
2994 ParentPad
= wrap(Constant::getNullValue(Ty
));
2996 return wrap(unwrap(B
)->CreateCleanupPad(unwrap(ParentPad
),
2997 makeArrayRef(unwrap(Args
), NumArgs
),
3001 LLVMValueRef
LLVMBuildResume(LLVMBuilderRef B
, LLVMValueRef Exn
) {
3002 return wrap(unwrap(B
)->CreateResume(unwrap(Exn
)));
3005 LLVMValueRef
LLVMBuildCatchSwitch(LLVMBuilderRef B
, LLVMValueRef ParentPad
,
3006 LLVMBasicBlockRef UnwindBB
,
3007 unsigned NumHandlers
, const char *Name
) {
3008 if (ParentPad
== nullptr) {
3009 Type
*Ty
= Type::getTokenTy(unwrap(B
)->getContext());
3010 ParentPad
= wrap(Constant::getNullValue(Ty
));
3012 return wrap(unwrap(B
)->CreateCatchSwitch(unwrap(ParentPad
), unwrap(UnwindBB
),
3013 NumHandlers
, Name
));
3016 LLVMValueRef
LLVMBuildCatchRet(LLVMBuilderRef B
, LLVMValueRef CatchPad
,
3017 LLVMBasicBlockRef BB
) {
3018 return wrap(unwrap(B
)->CreateCatchRet(unwrap
<CatchPadInst
>(CatchPad
),
3022 LLVMValueRef
LLVMBuildCleanupRet(LLVMBuilderRef B
, LLVMValueRef CatchPad
,
3023 LLVMBasicBlockRef BB
) {
3024 return wrap(unwrap(B
)->CreateCleanupRet(unwrap
<CleanupPadInst
>(CatchPad
),
3028 LLVMValueRef
LLVMBuildUnreachable(LLVMBuilderRef B
) {
3029 return wrap(unwrap(B
)->CreateUnreachable());
3032 void LLVMAddCase(LLVMValueRef Switch
, LLVMValueRef OnVal
,
3033 LLVMBasicBlockRef Dest
) {
3034 unwrap
<SwitchInst
>(Switch
)->addCase(unwrap
<ConstantInt
>(OnVal
), unwrap(Dest
));
3037 void LLVMAddDestination(LLVMValueRef IndirectBr
, LLVMBasicBlockRef Dest
) {
3038 unwrap
<IndirectBrInst
>(IndirectBr
)->addDestination(unwrap(Dest
));
3041 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad
) {
3042 return unwrap
<LandingPadInst
>(LandingPad
)->getNumClauses();
3045 LLVMValueRef
LLVMGetClause(LLVMValueRef LandingPad
, unsigned Idx
) {
3046 return wrap(unwrap
<LandingPadInst
>(LandingPad
)->getClause(Idx
));
3049 void LLVMAddClause(LLVMValueRef LandingPad
, LLVMValueRef ClauseVal
) {
3050 unwrap
<LandingPadInst
>(LandingPad
)->
3051 addClause(cast
<Constant
>(unwrap(ClauseVal
)));
3054 LLVMBool
LLVMIsCleanup(LLVMValueRef LandingPad
) {
3055 return unwrap
<LandingPadInst
>(LandingPad
)->isCleanup();
3058 void LLVMSetCleanup(LLVMValueRef LandingPad
, LLVMBool Val
) {
3059 unwrap
<LandingPadInst
>(LandingPad
)->setCleanup(Val
);
3062 void LLVMAddHandler(LLVMValueRef CatchSwitch
, LLVMBasicBlockRef Dest
) {
3063 unwrap
<CatchSwitchInst
>(CatchSwitch
)->addHandler(unwrap(Dest
));
3066 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch
) {
3067 return unwrap
<CatchSwitchInst
>(CatchSwitch
)->getNumHandlers();
3070 void LLVMGetHandlers(LLVMValueRef CatchSwitch
, LLVMBasicBlockRef
*Handlers
) {
3071 CatchSwitchInst
*CSI
= unwrap
<CatchSwitchInst
>(CatchSwitch
);
3072 for (CatchSwitchInst::handler_iterator I
= CSI
->handler_begin(),
3073 E
= CSI
->handler_end(); I
!= E
; ++I
)
3074 *Handlers
++ = wrap(*I
);
3077 LLVMValueRef
LLVMGetParentCatchSwitch(LLVMValueRef CatchPad
) {
3078 return wrap(unwrap
<CatchPadInst
>(CatchPad
)->getCatchSwitch());
3081 void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad
, LLVMValueRef CatchSwitch
) {
3082 unwrap
<CatchPadInst
>(CatchPad
)
3083 ->setCatchSwitch(unwrap
<CatchSwitchInst
>(CatchSwitch
));
3086 /*--.. Funclets ...........................................................--*/
3088 LLVMValueRef
LLVMGetArgOperand(LLVMValueRef Funclet
, unsigned i
) {
3089 return wrap(unwrap
<FuncletPadInst
>(Funclet
)->getArgOperand(i
));
3092 void LLVMSetArgOperand(LLVMValueRef Funclet
, unsigned i
, LLVMValueRef value
) {
3093 unwrap
<FuncletPadInst
>(Funclet
)->setArgOperand(i
, unwrap(value
));
3096 /*--.. Arithmetic ..........................................................--*/
3098 LLVMValueRef
LLVMBuildAdd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3100 return wrap(unwrap(B
)->CreateAdd(unwrap(LHS
), unwrap(RHS
), Name
));
3103 LLVMValueRef
LLVMBuildNSWAdd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3105 return wrap(unwrap(B
)->CreateNSWAdd(unwrap(LHS
), unwrap(RHS
), Name
));
3108 LLVMValueRef
LLVMBuildNUWAdd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3110 return wrap(unwrap(B
)->CreateNUWAdd(unwrap(LHS
), unwrap(RHS
), Name
));
3113 LLVMValueRef
LLVMBuildFAdd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3115 return wrap(unwrap(B
)->CreateFAdd(unwrap(LHS
), unwrap(RHS
), Name
));
3118 LLVMValueRef
LLVMBuildSub(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3120 return wrap(unwrap(B
)->CreateSub(unwrap(LHS
), unwrap(RHS
), Name
));
3123 LLVMValueRef
LLVMBuildNSWSub(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3125 return wrap(unwrap(B
)->CreateNSWSub(unwrap(LHS
), unwrap(RHS
), Name
));
3128 LLVMValueRef
LLVMBuildNUWSub(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3130 return wrap(unwrap(B
)->CreateNUWSub(unwrap(LHS
), unwrap(RHS
), Name
));
3133 LLVMValueRef
LLVMBuildFSub(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3135 return wrap(unwrap(B
)->CreateFSub(unwrap(LHS
), unwrap(RHS
), Name
));
3138 LLVMValueRef
LLVMBuildMul(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3140 return wrap(unwrap(B
)->CreateMul(unwrap(LHS
), unwrap(RHS
), Name
));
3143 LLVMValueRef
LLVMBuildNSWMul(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3145 return wrap(unwrap(B
)->CreateNSWMul(unwrap(LHS
), unwrap(RHS
), Name
));
3148 LLVMValueRef
LLVMBuildNUWMul(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3150 return wrap(unwrap(B
)->CreateNUWMul(unwrap(LHS
), unwrap(RHS
), Name
));
3153 LLVMValueRef
LLVMBuildFMul(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3155 return wrap(unwrap(B
)->CreateFMul(unwrap(LHS
), unwrap(RHS
), Name
));
3158 LLVMValueRef
LLVMBuildUDiv(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3160 return wrap(unwrap(B
)->CreateUDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3163 LLVMValueRef
LLVMBuildExactUDiv(LLVMBuilderRef B
, LLVMValueRef LHS
,
3164 LLVMValueRef RHS
, const char *Name
) {
3165 return wrap(unwrap(B
)->CreateExactUDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3168 LLVMValueRef
LLVMBuildSDiv(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3170 return wrap(unwrap(B
)->CreateSDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3173 LLVMValueRef
LLVMBuildExactSDiv(LLVMBuilderRef B
, LLVMValueRef LHS
,
3174 LLVMValueRef RHS
, const char *Name
) {
3175 return wrap(unwrap(B
)->CreateExactSDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3178 LLVMValueRef
LLVMBuildFDiv(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3180 return wrap(unwrap(B
)->CreateFDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3183 LLVMValueRef
LLVMBuildURem(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3185 return wrap(unwrap(B
)->CreateURem(unwrap(LHS
), unwrap(RHS
), Name
));
3188 LLVMValueRef
LLVMBuildSRem(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3190 return wrap(unwrap(B
)->CreateSRem(unwrap(LHS
), unwrap(RHS
), Name
));
3193 LLVMValueRef
LLVMBuildFRem(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3195 return wrap(unwrap(B
)->CreateFRem(unwrap(LHS
), unwrap(RHS
), Name
));
3198 LLVMValueRef
LLVMBuildShl(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3200 return wrap(unwrap(B
)->CreateShl(unwrap(LHS
), unwrap(RHS
), Name
));
3203 LLVMValueRef
LLVMBuildLShr(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3205 return wrap(unwrap(B
)->CreateLShr(unwrap(LHS
), unwrap(RHS
), Name
));
3208 LLVMValueRef
LLVMBuildAShr(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3210 return wrap(unwrap(B
)->CreateAShr(unwrap(LHS
), unwrap(RHS
), Name
));
3213 LLVMValueRef
LLVMBuildAnd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3215 return wrap(unwrap(B
)->CreateAnd(unwrap(LHS
), unwrap(RHS
), Name
));
3218 LLVMValueRef
LLVMBuildOr(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3220 return wrap(unwrap(B
)->CreateOr(unwrap(LHS
), unwrap(RHS
), Name
));
3223 LLVMValueRef
LLVMBuildXor(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3225 return wrap(unwrap(B
)->CreateXor(unwrap(LHS
), unwrap(RHS
), Name
));
3228 LLVMValueRef
LLVMBuildBinOp(LLVMBuilderRef B
, LLVMOpcode Op
,
3229 LLVMValueRef LHS
, LLVMValueRef RHS
,
3231 return wrap(unwrap(B
)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op
)), unwrap(LHS
),
3232 unwrap(RHS
), Name
));
3235 LLVMValueRef
LLVMBuildNeg(LLVMBuilderRef B
, LLVMValueRef V
, const char *Name
) {
3236 return wrap(unwrap(B
)->CreateNeg(unwrap(V
), Name
));
3239 LLVMValueRef
LLVMBuildNSWNeg(LLVMBuilderRef B
, LLVMValueRef V
,
3241 return wrap(unwrap(B
)->CreateNSWNeg(unwrap(V
), Name
));
3244 LLVMValueRef
LLVMBuildNUWNeg(LLVMBuilderRef B
, LLVMValueRef V
,
3246 return wrap(unwrap(B
)->CreateNUWNeg(unwrap(V
), Name
));
3249 LLVMValueRef
LLVMBuildFNeg(LLVMBuilderRef B
, LLVMValueRef V
, const char *Name
) {
3250 return wrap(unwrap(B
)->CreateFNeg(unwrap(V
), Name
));
3253 LLVMValueRef
LLVMBuildNot(LLVMBuilderRef B
, LLVMValueRef V
, const char *Name
) {
3254 return wrap(unwrap(B
)->CreateNot(unwrap(V
), Name
));
3257 /*--.. Memory ..............................................................--*/
3259 LLVMValueRef
LLVMBuildMalloc(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3261 Type
* ITy
= Type::getInt32Ty(unwrap(B
)->GetInsertBlock()->getContext());
3262 Constant
* AllocSize
= ConstantExpr::getSizeOf(unwrap(Ty
));
3263 AllocSize
= ConstantExpr::getTruncOrBitCast(AllocSize
, ITy
);
3264 Instruction
* Malloc
= CallInst::CreateMalloc(unwrap(B
)->GetInsertBlock(),
3265 ITy
, unwrap(Ty
), AllocSize
,
3266 nullptr, nullptr, "");
3267 return wrap(unwrap(B
)->Insert(Malloc
, Twine(Name
)));
3270 LLVMValueRef
LLVMBuildArrayMalloc(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3271 LLVMValueRef Val
, const char *Name
) {
3272 Type
* ITy
= Type::getInt32Ty(unwrap(B
)->GetInsertBlock()->getContext());
3273 Constant
* AllocSize
= ConstantExpr::getSizeOf(unwrap(Ty
));
3274 AllocSize
= ConstantExpr::getTruncOrBitCast(AllocSize
, ITy
);
3275 Instruction
* Malloc
= CallInst::CreateMalloc(unwrap(B
)->GetInsertBlock(),
3276 ITy
, unwrap(Ty
), AllocSize
,
3277 unwrap(Val
), nullptr, "");
3278 return wrap(unwrap(B
)->Insert(Malloc
, Twine(Name
)));
3281 LLVMValueRef
LLVMBuildMemSet(LLVMBuilderRef B
, LLVMValueRef Ptr
,
3282 LLVMValueRef Val
, LLVMValueRef Len
,
3284 return wrap(unwrap(B
)->CreateMemSet(unwrap(Ptr
), unwrap(Val
), unwrap(Len
), Align
));
3287 LLVMValueRef
LLVMBuildMemCpy(LLVMBuilderRef B
,
3288 LLVMValueRef Dst
, unsigned DstAlign
,
3289 LLVMValueRef Src
, unsigned SrcAlign
,
3290 LLVMValueRef Size
) {
3291 return wrap(unwrap(B
)->CreateMemCpy(unwrap(Dst
), DstAlign
,
3292 unwrap(Src
), SrcAlign
,
3296 LLVMValueRef
LLVMBuildMemMove(LLVMBuilderRef B
,
3297 LLVMValueRef Dst
, unsigned DstAlign
,
3298 LLVMValueRef Src
, unsigned SrcAlign
,
3299 LLVMValueRef Size
) {
3300 return wrap(unwrap(B
)->CreateMemMove(unwrap(Dst
), DstAlign
,
3301 unwrap(Src
), SrcAlign
,
3305 LLVMValueRef
LLVMBuildAlloca(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3307 return wrap(unwrap(B
)->CreateAlloca(unwrap(Ty
), nullptr, Name
));
3310 LLVMValueRef
LLVMBuildArrayAlloca(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3311 LLVMValueRef Val
, const char *Name
) {
3312 return wrap(unwrap(B
)->CreateAlloca(unwrap(Ty
), unwrap(Val
), Name
));
3315 LLVMValueRef
LLVMBuildFree(LLVMBuilderRef B
, LLVMValueRef PointerVal
) {
3316 return wrap(unwrap(B
)->Insert(
3317 CallInst::CreateFree(unwrap(PointerVal
), unwrap(B
)->GetInsertBlock())));
3320 LLVMValueRef
LLVMBuildLoad(LLVMBuilderRef B
, LLVMValueRef PointerVal
,
3322 return wrap(unwrap(B
)->CreateLoad(unwrap(PointerVal
), Name
));
3325 LLVMValueRef
LLVMBuildStore(LLVMBuilderRef B
, LLVMValueRef Val
,
3326 LLVMValueRef PointerVal
) {
3327 return wrap(unwrap(B
)->CreateStore(unwrap(Val
), unwrap(PointerVal
)));
3330 static AtomicOrdering
mapFromLLVMOrdering(LLVMAtomicOrdering Ordering
) {
3332 case LLVMAtomicOrderingNotAtomic
: return AtomicOrdering::NotAtomic
;
3333 case LLVMAtomicOrderingUnordered
: return AtomicOrdering::Unordered
;
3334 case LLVMAtomicOrderingMonotonic
: return AtomicOrdering::Monotonic
;
3335 case LLVMAtomicOrderingAcquire
: return AtomicOrdering::Acquire
;
3336 case LLVMAtomicOrderingRelease
: return AtomicOrdering::Release
;
3337 case LLVMAtomicOrderingAcquireRelease
:
3338 return AtomicOrdering::AcquireRelease
;
3339 case LLVMAtomicOrderingSequentiallyConsistent
:
3340 return AtomicOrdering::SequentiallyConsistent
;
3343 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3346 static LLVMAtomicOrdering
mapToLLVMOrdering(AtomicOrdering Ordering
) {
3348 case AtomicOrdering::NotAtomic
: return LLVMAtomicOrderingNotAtomic
;
3349 case AtomicOrdering::Unordered
: return LLVMAtomicOrderingUnordered
;
3350 case AtomicOrdering::Monotonic
: return LLVMAtomicOrderingMonotonic
;
3351 case AtomicOrdering::Acquire
: return LLVMAtomicOrderingAcquire
;
3352 case AtomicOrdering::Release
: return LLVMAtomicOrderingRelease
;
3353 case AtomicOrdering::AcquireRelease
:
3354 return LLVMAtomicOrderingAcquireRelease
;
3355 case AtomicOrdering::SequentiallyConsistent
:
3356 return LLVMAtomicOrderingSequentiallyConsistent
;
3359 llvm_unreachable("Invalid AtomicOrdering value!");
3362 // TODO: Should this and other atomic instructions support building with
3364 LLVMValueRef
LLVMBuildFence(LLVMBuilderRef B
, LLVMAtomicOrdering Ordering
,
3365 LLVMBool isSingleThread
, const char *Name
) {
3367 unwrap(B
)->CreateFence(mapFromLLVMOrdering(Ordering
),
3368 isSingleThread
? SyncScope::SingleThread
3369 : SyncScope::System
,
3373 LLVMValueRef
LLVMBuildGEP(LLVMBuilderRef B
, LLVMValueRef Pointer
,
3374 LLVMValueRef
*Indices
, unsigned NumIndices
,
3376 ArrayRef
<Value
*> IdxList(unwrap(Indices
), NumIndices
);
3377 return wrap(unwrap(B
)->CreateGEP(nullptr, unwrap(Pointer
), IdxList
, Name
));
3380 LLVMValueRef
LLVMBuildInBoundsGEP(LLVMBuilderRef B
, LLVMValueRef Pointer
,
3381 LLVMValueRef
*Indices
, unsigned NumIndices
,
3383 ArrayRef
<Value
*> IdxList(unwrap(Indices
), NumIndices
);
3385 unwrap(B
)->CreateInBoundsGEP(nullptr, unwrap(Pointer
), IdxList
, Name
));
3388 LLVMValueRef
LLVMBuildStructGEP(LLVMBuilderRef B
, LLVMValueRef Pointer
,
3389 unsigned Idx
, const char *Name
) {
3390 return wrap(unwrap(B
)->CreateStructGEP(nullptr, unwrap(Pointer
), Idx
, Name
));
3393 LLVMValueRef
LLVMBuildGlobalString(LLVMBuilderRef B
, const char *Str
,
3395 return wrap(unwrap(B
)->CreateGlobalString(Str
, Name
));
3398 LLVMValueRef
LLVMBuildGlobalStringPtr(LLVMBuilderRef B
, const char *Str
,
3400 return wrap(unwrap(B
)->CreateGlobalStringPtr(Str
, Name
));
3403 LLVMBool
LLVMGetVolatile(LLVMValueRef MemAccessInst
) {
3404 Value
*P
= unwrap
<Value
>(MemAccessInst
);
3405 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
3406 return LI
->isVolatile();
3407 return cast
<StoreInst
>(P
)->isVolatile();
3410 void LLVMSetVolatile(LLVMValueRef MemAccessInst
, LLVMBool isVolatile
) {
3411 Value
*P
= unwrap
<Value
>(MemAccessInst
);
3412 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
3413 return LI
->setVolatile(isVolatile
);
3414 return cast
<StoreInst
>(P
)->setVolatile(isVolatile
);
3417 LLVMAtomicOrdering
LLVMGetOrdering(LLVMValueRef MemAccessInst
) {
3418 Value
*P
= unwrap
<Value
>(MemAccessInst
);
3420 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
3421 O
= LI
->getOrdering();
3423 O
= cast
<StoreInst
>(P
)->getOrdering();
3424 return mapToLLVMOrdering(O
);
3427 void LLVMSetOrdering(LLVMValueRef MemAccessInst
, LLVMAtomicOrdering Ordering
) {
3428 Value
*P
= unwrap
<Value
>(MemAccessInst
);
3429 AtomicOrdering O
= mapFromLLVMOrdering(Ordering
);
3431 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
3432 return LI
->setOrdering(O
);
3433 return cast
<StoreInst
>(P
)->setOrdering(O
);
3436 /*--.. Casts ...............................................................--*/
3438 LLVMValueRef
LLVMBuildTrunc(LLVMBuilderRef B
, LLVMValueRef Val
,
3439 LLVMTypeRef DestTy
, const char *Name
) {
3440 return wrap(unwrap(B
)->CreateTrunc(unwrap(Val
), unwrap(DestTy
), Name
));
3443 LLVMValueRef
LLVMBuildZExt(LLVMBuilderRef B
, LLVMValueRef Val
,
3444 LLVMTypeRef DestTy
, const char *Name
) {
3445 return wrap(unwrap(B
)->CreateZExt(unwrap(Val
), unwrap(DestTy
), Name
));
3448 LLVMValueRef
LLVMBuildSExt(LLVMBuilderRef B
, LLVMValueRef Val
,
3449 LLVMTypeRef DestTy
, const char *Name
) {
3450 return wrap(unwrap(B
)->CreateSExt(unwrap(Val
), unwrap(DestTy
), Name
));
3453 LLVMValueRef
LLVMBuildFPToUI(LLVMBuilderRef B
, LLVMValueRef Val
,
3454 LLVMTypeRef DestTy
, const char *Name
) {
3455 return wrap(unwrap(B
)->CreateFPToUI(unwrap(Val
), unwrap(DestTy
), Name
));
3458 LLVMValueRef
LLVMBuildFPToSI(LLVMBuilderRef B
, LLVMValueRef Val
,
3459 LLVMTypeRef DestTy
, const char *Name
) {
3460 return wrap(unwrap(B
)->CreateFPToSI(unwrap(Val
), unwrap(DestTy
), Name
));
3463 LLVMValueRef
LLVMBuildUIToFP(LLVMBuilderRef B
, LLVMValueRef Val
,
3464 LLVMTypeRef DestTy
, const char *Name
) {
3465 return wrap(unwrap(B
)->CreateUIToFP(unwrap(Val
), unwrap(DestTy
), Name
));
3468 LLVMValueRef
LLVMBuildSIToFP(LLVMBuilderRef B
, LLVMValueRef Val
,
3469 LLVMTypeRef DestTy
, const char *Name
) {
3470 return wrap(unwrap(B
)->CreateSIToFP(unwrap(Val
), unwrap(DestTy
), Name
));
3473 LLVMValueRef
LLVMBuildFPTrunc(LLVMBuilderRef B
, LLVMValueRef Val
,
3474 LLVMTypeRef DestTy
, const char *Name
) {
3475 return wrap(unwrap(B
)->CreateFPTrunc(unwrap(Val
), unwrap(DestTy
), Name
));
3478 LLVMValueRef
LLVMBuildFPExt(LLVMBuilderRef B
, LLVMValueRef Val
,
3479 LLVMTypeRef DestTy
, const char *Name
) {
3480 return wrap(unwrap(B
)->CreateFPExt(unwrap(Val
), unwrap(DestTy
), Name
));
3483 LLVMValueRef
LLVMBuildPtrToInt(LLVMBuilderRef B
, LLVMValueRef Val
,
3484 LLVMTypeRef DestTy
, const char *Name
) {
3485 return wrap(unwrap(B
)->CreatePtrToInt(unwrap(Val
), unwrap(DestTy
), Name
));
3488 LLVMValueRef
LLVMBuildIntToPtr(LLVMBuilderRef B
, LLVMValueRef Val
,
3489 LLVMTypeRef DestTy
, const char *Name
) {
3490 return wrap(unwrap(B
)->CreateIntToPtr(unwrap(Val
), unwrap(DestTy
), Name
));
3493 LLVMValueRef
LLVMBuildBitCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3494 LLVMTypeRef DestTy
, const char *Name
) {
3495 return wrap(unwrap(B
)->CreateBitCast(unwrap(Val
), unwrap(DestTy
), Name
));
3498 LLVMValueRef
LLVMBuildAddrSpaceCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3499 LLVMTypeRef DestTy
, const char *Name
) {
3500 return wrap(unwrap(B
)->CreateAddrSpaceCast(unwrap(Val
), unwrap(DestTy
), Name
));
3503 LLVMValueRef
LLVMBuildZExtOrBitCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3504 LLVMTypeRef DestTy
, const char *Name
) {
3505 return wrap(unwrap(B
)->CreateZExtOrBitCast(unwrap(Val
), unwrap(DestTy
),
3509 LLVMValueRef
LLVMBuildSExtOrBitCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3510 LLVMTypeRef DestTy
, const char *Name
) {
3511 return wrap(unwrap(B
)->CreateSExtOrBitCast(unwrap(Val
), unwrap(DestTy
),
3515 LLVMValueRef
LLVMBuildTruncOrBitCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3516 LLVMTypeRef DestTy
, const char *Name
) {
3517 return wrap(unwrap(B
)->CreateTruncOrBitCast(unwrap(Val
), unwrap(DestTy
),
3521 LLVMValueRef
LLVMBuildCast(LLVMBuilderRef B
, LLVMOpcode Op
, LLVMValueRef Val
,
3522 LLVMTypeRef DestTy
, const char *Name
) {
3523 return wrap(unwrap(B
)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op
)), unwrap(Val
),
3524 unwrap(DestTy
), Name
));
3527 LLVMValueRef
LLVMBuildPointerCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3528 LLVMTypeRef DestTy
, const char *Name
) {
3529 return wrap(unwrap(B
)->CreatePointerCast(unwrap(Val
), unwrap(DestTy
), Name
));
3532 LLVMValueRef
LLVMBuildIntCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3533 LLVMTypeRef DestTy
, const char *Name
) {
3534 return wrap(unwrap(B
)->CreateIntCast(unwrap(Val
), unwrap(DestTy
),
3535 /*isSigned*/true, Name
));
3538 LLVMValueRef
LLVMBuildFPCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3539 LLVMTypeRef DestTy
, const char *Name
) {
3540 return wrap(unwrap(B
)->CreateFPCast(unwrap(Val
), unwrap(DestTy
), Name
));
3543 /*--.. Comparisons .........................................................--*/
3545 LLVMValueRef
LLVMBuildICmp(LLVMBuilderRef B
, LLVMIntPredicate Op
,
3546 LLVMValueRef LHS
, LLVMValueRef RHS
,
3548 return wrap(unwrap(B
)->CreateICmp(static_cast<ICmpInst::Predicate
>(Op
),
3549 unwrap(LHS
), unwrap(RHS
), Name
));
3552 LLVMValueRef
LLVMBuildFCmp(LLVMBuilderRef B
, LLVMRealPredicate Op
,
3553 LLVMValueRef LHS
, LLVMValueRef RHS
,
3555 return wrap(unwrap(B
)->CreateFCmp(static_cast<FCmpInst::Predicate
>(Op
),
3556 unwrap(LHS
), unwrap(RHS
), Name
));
3559 /*--.. Miscellaneous instructions ..........................................--*/
3561 LLVMValueRef
LLVMBuildPhi(LLVMBuilderRef B
, LLVMTypeRef Ty
, const char *Name
) {
3562 return wrap(unwrap(B
)->CreatePHI(unwrap(Ty
), 0, Name
));
3565 LLVMValueRef
LLVMBuildCall(LLVMBuilderRef B
, LLVMValueRef Fn
,
3566 LLVMValueRef
*Args
, unsigned NumArgs
,
3568 return wrap(unwrap(B
)->CreateCall(unwrap(Fn
),
3569 makeArrayRef(unwrap(Args
), NumArgs
),
3573 LLVMValueRef
LLVMBuildSelect(LLVMBuilderRef B
, LLVMValueRef If
,
3574 LLVMValueRef Then
, LLVMValueRef Else
,
3576 return wrap(unwrap(B
)->CreateSelect(unwrap(If
), unwrap(Then
), unwrap(Else
),
3580 LLVMValueRef
LLVMBuildVAArg(LLVMBuilderRef B
, LLVMValueRef List
,
3581 LLVMTypeRef Ty
, const char *Name
) {
3582 return wrap(unwrap(B
)->CreateVAArg(unwrap(List
), unwrap(Ty
), Name
));
3585 LLVMValueRef
LLVMBuildExtractElement(LLVMBuilderRef B
, LLVMValueRef VecVal
,
3586 LLVMValueRef Index
, const char *Name
) {
3587 return wrap(unwrap(B
)->CreateExtractElement(unwrap(VecVal
), unwrap(Index
),
3591 LLVMValueRef
LLVMBuildInsertElement(LLVMBuilderRef B
, LLVMValueRef VecVal
,
3592 LLVMValueRef EltVal
, LLVMValueRef Index
,
3594 return wrap(unwrap(B
)->CreateInsertElement(unwrap(VecVal
), unwrap(EltVal
),
3595 unwrap(Index
), Name
));
3598 LLVMValueRef
LLVMBuildShuffleVector(LLVMBuilderRef B
, LLVMValueRef V1
,
3599 LLVMValueRef V2
, LLVMValueRef Mask
,
3601 return wrap(unwrap(B
)->CreateShuffleVector(unwrap(V1
), unwrap(V2
),
3602 unwrap(Mask
), Name
));
3605 LLVMValueRef
LLVMBuildExtractValue(LLVMBuilderRef B
, LLVMValueRef AggVal
,
3606 unsigned Index
, const char *Name
) {
3607 return wrap(unwrap(B
)->CreateExtractValue(unwrap(AggVal
), Index
, Name
));
3610 LLVMValueRef
LLVMBuildInsertValue(LLVMBuilderRef B
, LLVMValueRef AggVal
,
3611 LLVMValueRef EltVal
, unsigned Index
,
3613 return wrap(unwrap(B
)->CreateInsertValue(unwrap(AggVal
), unwrap(EltVal
),
3617 LLVMValueRef
LLVMBuildIsNull(LLVMBuilderRef B
, LLVMValueRef Val
,
3619 return wrap(unwrap(B
)->CreateIsNull(unwrap(Val
), Name
));
3622 LLVMValueRef
LLVMBuildIsNotNull(LLVMBuilderRef B
, LLVMValueRef Val
,
3624 return wrap(unwrap(B
)->CreateIsNotNull(unwrap(Val
), Name
));
3627 LLVMValueRef
LLVMBuildPtrDiff(LLVMBuilderRef B
, LLVMValueRef LHS
,
3628 LLVMValueRef RHS
, const char *Name
) {
3629 return wrap(unwrap(B
)->CreatePtrDiff(unwrap(LHS
), unwrap(RHS
), Name
));
3632 LLVMValueRef
LLVMBuildAtomicRMW(LLVMBuilderRef B
,LLVMAtomicRMWBinOp op
,
3633 LLVMValueRef PTR
, LLVMValueRef Val
,
3634 LLVMAtomicOrdering ordering
,
3635 LLVMBool singleThread
) {
3636 AtomicRMWInst::BinOp intop
;
3638 case LLVMAtomicRMWBinOpXchg
: intop
= AtomicRMWInst::Xchg
; break;
3639 case LLVMAtomicRMWBinOpAdd
: intop
= AtomicRMWInst::Add
; break;
3640 case LLVMAtomicRMWBinOpSub
: intop
= AtomicRMWInst::Sub
; break;
3641 case LLVMAtomicRMWBinOpAnd
: intop
= AtomicRMWInst::And
; break;
3642 case LLVMAtomicRMWBinOpNand
: intop
= AtomicRMWInst::Nand
; break;
3643 case LLVMAtomicRMWBinOpOr
: intop
= AtomicRMWInst::Or
; break;
3644 case LLVMAtomicRMWBinOpXor
: intop
= AtomicRMWInst::Xor
; break;
3645 case LLVMAtomicRMWBinOpMax
: intop
= AtomicRMWInst::Max
; break;
3646 case LLVMAtomicRMWBinOpMin
: intop
= AtomicRMWInst::Min
; break;
3647 case LLVMAtomicRMWBinOpUMax
: intop
= AtomicRMWInst::UMax
; break;
3648 case LLVMAtomicRMWBinOpUMin
: intop
= AtomicRMWInst::UMin
; break;
3650 return wrap(unwrap(B
)->CreateAtomicRMW(intop
, unwrap(PTR
), unwrap(Val
),
3651 mapFromLLVMOrdering(ordering
), singleThread
? SyncScope::SingleThread
3652 : SyncScope::System
));
3655 LLVMValueRef
LLVMBuildAtomicCmpXchg(LLVMBuilderRef B
, LLVMValueRef Ptr
,
3656 LLVMValueRef Cmp
, LLVMValueRef New
,
3657 LLVMAtomicOrdering SuccessOrdering
,
3658 LLVMAtomicOrdering FailureOrdering
,
3659 LLVMBool singleThread
) {
3661 return wrap(unwrap(B
)->CreateAtomicCmpXchg(unwrap(Ptr
), unwrap(Cmp
),
3662 unwrap(New
), mapFromLLVMOrdering(SuccessOrdering
),
3663 mapFromLLVMOrdering(FailureOrdering
),
3664 singleThread
? SyncScope::SingleThread
: SyncScope::System
));
3668 LLVMBool
LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst
) {
3669 Value
*P
= unwrap
<Value
>(AtomicInst
);
3671 if (AtomicRMWInst
*I
= dyn_cast
<AtomicRMWInst
>(P
))
3672 return I
->getSyncScopeID() == SyncScope::SingleThread
;
3673 return cast
<AtomicCmpXchgInst
>(P
)->getSyncScopeID() ==
3674 SyncScope::SingleThread
;
3677 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst
, LLVMBool NewValue
) {
3678 Value
*P
= unwrap
<Value
>(AtomicInst
);
3679 SyncScope::ID SSID
= NewValue
? SyncScope::SingleThread
: SyncScope::System
;
3681 if (AtomicRMWInst
*I
= dyn_cast
<AtomicRMWInst
>(P
))
3682 return I
->setSyncScopeID(SSID
);
3683 return cast
<AtomicCmpXchgInst
>(P
)->setSyncScopeID(SSID
);
3686 LLVMAtomicOrdering
LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst
) {
3687 Value
*P
= unwrap
<Value
>(CmpXchgInst
);
3688 return mapToLLVMOrdering(cast
<AtomicCmpXchgInst
>(P
)->getSuccessOrdering());
3691 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst
,
3692 LLVMAtomicOrdering Ordering
) {
3693 Value
*P
= unwrap
<Value
>(CmpXchgInst
);
3694 AtomicOrdering O
= mapFromLLVMOrdering(Ordering
);
3696 return cast
<AtomicCmpXchgInst
>(P
)->setSuccessOrdering(O
);
3699 LLVMAtomicOrdering
LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst
) {
3700 Value
*P
= unwrap
<Value
>(CmpXchgInst
);
3701 return mapToLLVMOrdering(cast
<AtomicCmpXchgInst
>(P
)->getFailureOrdering());
3704 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst
,
3705 LLVMAtomicOrdering Ordering
) {
3706 Value
*P
= unwrap
<Value
>(CmpXchgInst
);
3707 AtomicOrdering O
= mapFromLLVMOrdering(Ordering
);
3709 return cast
<AtomicCmpXchgInst
>(P
)->setFailureOrdering(O
);
3712 /*===-- Module providers --------------------------------------------------===*/
3714 LLVMModuleProviderRef
3715 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M
) {
3716 return reinterpret_cast<LLVMModuleProviderRef
>(M
);
3719 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP
) {
3724 /*===-- Memory buffers ----------------------------------------------------===*/
3726 LLVMBool
LLVMCreateMemoryBufferWithContentsOfFile(
3728 LLVMMemoryBufferRef
*OutMemBuf
,
3729 char **OutMessage
) {
3731 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MBOrErr
= MemoryBuffer::getFile(Path
);
3732 if (std::error_code EC
= MBOrErr
.getError()) {
3733 *OutMessage
= strdup(EC
.message().c_str());
3736 *OutMemBuf
= wrap(MBOrErr
.get().release());
3740 LLVMBool
LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef
*OutMemBuf
,
3741 char **OutMessage
) {
3742 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MBOrErr
= MemoryBuffer::getSTDIN();
3743 if (std::error_code EC
= MBOrErr
.getError()) {
3744 *OutMessage
= strdup(EC
.message().c_str());
3747 *OutMemBuf
= wrap(MBOrErr
.get().release());
3751 LLVMMemoryBufferRef
LLVMCreateMemoryBufferWithMemoryRange(
3752 const char *InputData
,
3753 size_t InputDataLength
,
3754 const char *BufferName
,
3755 LLVMBool RequiresNullTerminator
) {
3757 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData
, InputDataLength
),
3758 StringRef(BufferName
),
3759 RequiresNullTerminator
).release());
3762 LLVMMemoryBufferRef
LLVMCreateMemoryBufferWithMemoryRangeCopy(
3763 const char *InputData
,
3764 size_t InputDataLength
,
3765 const char *BufferName
) {
3768 MemoryBuffer::getMemBufferCopy(StringRef(InputData
, InputDataLength
),
3769 StringRef(BufferName
)).release());
3772 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf
) {
3773 return unwrap(MemBuf
)->getBufferStart();
3776 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf
) {
3777 return unwrap(MemBuf
)->getBufferSize();
3780 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf
) {
3781 delete unwrap(MemBuf
);
3784 /*===-- Pass Registry -----------------------------------------------------===*/
3786 LLVMPassRegistryRef
LLVMGetGlobalPassRegistry(void) {
3787 return wrap(PassRegistry::getPassRegistry());
3790 /*===-- Pass Manager ------------------------------------------------------===*/
3792 LLVMPassManagerRef
LLVMCreatePassManager() {
3793 return wrap(new legacy::PassManager());
3796 LLVMPassManagerRef
LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M
) {
3797 return wrap(new legacy::FunctionPassManager(unwrap(M
)));
3800 LLVMPassManagerRef
LLVMCreateFunctionPassManager(LLVMModuleProviderRef P
) {
3801 return LLVMCreateFunctionPassManagerForModule(
3802 reinterpret_cast<LLVMModuleRef
>(P
));
3805 LLVMBool
LLVMRunPassManager(LLVMPassManagerRef PM
, LLVMModuleRef M
) {
3806 return unwrap
<legacy::PassManager
>(PM
)->run(*unwrap(M
));
3809 LLVMBool
LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM
) {
3810 return unwrap
<legacy::FunctionPassManager
>(FPM
)->doInitialization();
3813 LLVMBool
LLVMRunFunctionPassManager(LLVMPassManagerRef FPM
, LLVMValueRef F
) {
3814 return unwrap
<legacy::FunctionPassManager
>(FPM
)->run(*unwrap
<Function
>(F
));
3817 LLVMBool
LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM
) {
3818 return unwrap
<legacy::FunctionPassManager
>(FPM
)->doFinalization();
3821 void LLVMDisposePassManager(LLVMPassManagerRef PM
) {
3825 /*===-- Threading ------------------------------------------------------===*/
3827 LLVMBool
LLVMStartMultithreaded() {
3828 return LLVMIsMultithreaded();
3831 void LLVMStopMultithreaded() {
3834 LLVMBool
LLVMIsMultithreaded() {
3835 return llvm_is_multithreaded();