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 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn
) {
2284 return unwrap
<Function
>(Fn
)->getCallingConv();
2287 void LLVMSetFunctionCallConv(LLVMValueRef Fn
, unsigned CC
) {
2288 return unwrap
<Function
>(Fn
)->setCallingConv(
2289 static_cast<CallingConv::ID
>(CC
));
2292 const char *LLVMGetGC(LLVMValueRef Fn
) {
2293 Function
*F
= unwrap
<Function
>(Fn
);
2294 return F
->hasGC()? F
->getGC().c_str() : nullptr;
2297 void LLVMSetGC(LLVMValueRef Fn
, const char *GC
) {
2298 Function
*F
= unwrap
<Function
>(Fn
);
2305 void LLVMAddAttributeAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
,
2306 LLVMAttributeRef A
) {
2307 unwrap
<Function
>(F
)->addAttribute(Idx
, unwrap(A
));
2310 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
) {
2311 auto AS
= unwrap
<Function
>(F
)->getAttributes().getAttributes(Idx
);
2312 return AS
.getNumAttributes();
2315 void LLVMGetAttributesAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
,
2316 LLVMAttributeRef
*Attrs
) {
2317 auto AS
= unwrap
<Function
>(F
)->getAttributes().getAttributes(Idx
);
2322 LLVMAttributeRef
LLVMGetEnumAttributeAtIndex(LLVMValueRef F
,
2323 LLVMAttributeIndex Idx
,
2325 return wrap(unwrap
<Function
>(F
)->getAttribute(Idx
,
2326 (Attribute::AttrKind
)KindID
));
2329 LLVMAttributeRef
LLVMGetStringAttributeAtIndex(LLVMValueRef F
,
2330 LLVMAttributeIndex Idx
,
2331 const char *K
, unsigned KLen
) {
2332 return wrap(unwrap
<Function
>(F
)->getAttribute(Idx
, StringRef(K
, KLen
)));
2335 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
,
2337 unwrap
<Function
>(F
)->removeAttribute(Idx
, (Attribute::AttrKind
)KindID
);
2340 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F
, LLVMAttributeIndex Idx
,
2341 const char *K
, unsigned KLen
) {
2342 unwrap
<Function
>(F
)->removeAttribute(Idx
, StringRef(K
, KLen
));
2345 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn
, const char *A
,
2347 Function
*Func
= unwrap
<Function
>(Fn
);
2348 Attribute Attr
= Attribute::get(Func
->getContext(), A
, V
);
2349 Func
->addAttribute(AttributeList::FunctionIndex
, Attr
);
2352 /*--.. Operations on parameters ............................................--*/
2354 unsigned LLVMCountParams(LLVMValueRef FnRef
) {
2355 // This function is strictly redundant to
2356 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2357 return unwrap
<Function
>(FnRef
)->arg_size();
2360 void LLVMGetParams(LLVMValueRef FnRef
, LLVMValueRef
*ParamRefs
) {
2361 Function
*Fn
= unwrap
<Function
>(FnRef
);
2362 for (Function::arg_iterator I
= Fn
->arg_begin(),
2363 E
= Fn
->arg_end(); I
!= E
; I
++)
2364 *ParamRefs
++ = wrap(&*I
);
2367 LLVMValueRef
LLVMGetParam(LLVMValueRef FnRef
, unsigned index
) {
2368 Function
*Fn
= unwrap
<Function
>(FnRef
);
2369 return wrap(&Fn
->arg_begin()[index
]);
2372 LLVMValueRef
LLVMGetParamParent(LLVMValueRef V
) {
2373 return wrap(unwrap
<Argument
>(V
)->getParent());
2376 LLVMValueRef
LLVMGetFirstParam(LLVMValueRef Fn
) {
2377 Function
*Func
= unwrap
<Function
>(Fn
);
2378 Function::arg_iterator I
= Func
->arg_begin();
2379 if (I
== Func
->arg_end())
2384 LLVMValueRef
LLVMGetLastParam(LLVMValueRef Fn
) {
2385 Function
*Func
= unwrap
<Function
>(Fn
);
2386 Function::arg_iterator I
= Func
->arg_end();
2387 if (I
== Func
->arg_begin())
2392 LLVMValueRef
LLVMGetNextParam(LLVMValueRef Arg
) {
2393 Argument
*A
= unwrap
<Argument
>(Arg
);
2394 Function
*Fn
= A
->getParent();
2395 if (A
->getArgNo() + 1 >= Fn
->arg_size())
2397 return wrap(&Fn
->arg_begin()[A
->getArgNo() + 1]);
2400 LLVMValueRef
LLVMGetPreviousParam(LLVMValueRef Arg
) {
2401 Argument
*A
= unwrap
<Argument
>(Arg
);
2402 if (A
->getArgNo() == 0)
2404 return wrap(&A
->getParent()->arg_begin()[A
->getArgNo() - 1]);
2407 void LLVMSetParamAlignment(LLVMValueRef Arg
, unsigned align
) {
2408 Argument
*A
= unwrap
<Argument
>(Arg
);
2409 A
->addAttr(Attribute::getWithAlignment(A
->getContext(), align
));
2412 /*--.. Operations on basic blocks ..........................................--*/
2414 LLVMValueRef
LLVMBasicBlockAsValue(LLVMBasicBlockRef BB
) {
2415 return wrap(static_cast<Value
*>(unwrap(BB
)));
2418 LLVMBool
LLVMValueIsBasicBlock(LLVMValueRef Val
) {
2419 return isa
<BasicBlock
>(unwrap(Val
));
2422 LLVMBasicBlockRef
LLVMValueAsBasicBlock(LLVMValueRef Val
) {
2423 return wrap(unwrap
<BasicBlock
>(Val
));
2426 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB
) {
2427 return unwrap(BB
)->getName().data();
2430 LLVMValueRef
LLVMGetBasicBlockParent(LLVMBasicBlockRef BB
) {
2431 return wrap(unwrap(BB
)->getParent());
2434 LLVMValueRef
LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB
) {
2435 return wrap(unwrap(BB
)->getTerminator());
2438 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef
) {
2439 return unwrap
<Function
>(FnRef
)->size();
2442 void LLVMGetBasicBlocks(LLVMValueRef FnRef
, LLVMBasicBlockRef
*BasicBlocksRefs
){
2443 Function
*Fn
= unwrap
<Function
>(FnRef
);
2444 for (BasicBlock
&BB
: *Fn
)
2445 *BasicBlocksRefs
++ = wrap(&BB
);
2448 LLVMBasicBlockRef
LLVMGetEntryBasicBlock(LLVMValueRef Fn
) {
2449 return wrap(&unwrap
<Function
>(Fn
)->getEntryBlock());
2452 LLVMBasicBlockRef
LLVMGetFirstBasicBlock(LLVMValueRef Fn
) {
2453 Function
*Func
= unwrap
<Function
>(Fn
);
2454 Function::iterator I
= Func
->begin();
2455 if (I
== Func
->end())
2460 LLVMBasicBlockRef
LLVMGetLastBasicBlock(LLVMValueRef Fn
) {
2461 Function
*Func
= unwrap
<Function
>(Fn
);
2462 Function::iterator I
= Func
->end();
2463 if (I
== Func
->begin())
2468 LLVMBasicBlockRef
LLVMGetNextBasicBlock(LLVMBasicBlockRef BB
) {
2469 BasicBlock
*Block
= unwrap(BB
);
2470 Function::iterator
I(Block
);
2471 if (++I
== Block
->getParent()->end())
2476 LLVMBasicBlockRef
LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB
) {
2477 BasicBlock
*Block
= unwrap(BB
);
2478 Function::iterator
I(Block
);
2479 if (I
== Block
->getParent()->begin())
2484 LLVMBasicBlockRef
LLVMAppendBasicBlockInContext(LLVMContextRef C
,
2487 return wrap(BasicBlock::Create(*unwrap(C
), Name
, unwrap
<Function
>(FnRef
)));
2490 LLVMBasicBlockRef
LLVMAppendBasicBlock(LLVMValueRef FnRef
, const char *Name
) {
2491 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef
, Name
);
2494 LLVMBasicBlockRef
LLVMInsertBasicBlockInContext(LLVMContextRef C
,
2495 LLVMBasicBlockRef BBRef
,
2497 BasicBlock
*BB
= unwrap(BBRef
);
2498 return wrap(BasicBlock::Create(*unwrap(C
), Name
, BB
->getParent(), BB
));
2501 LLVMBasicBlockRef
LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef
,
2503 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef
, Name
);
2506 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef
) {
2507 unwrap(BBRef
)->eraseFromParent();
2510 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef
) {
2511 unwrap(BBRef
)->removeFromParent();
2514 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB
, LLVMBasicBlockRef MovePos
) {
2515 unwrap(BB
)->moveBefore(unwrap(MovePos
));
2518 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB
, LLVMBasicBlockRef MovePos
) {
2519 unwrap(BB
)->moveAfter(unwrap(MovePos
));
2522 /*--.. Operations on instructions ..........................................--*/
2524 LLVMBasicBlockRef
LLVMGetInstructionParent(LLVMValueRef Inst
) {
2525 return wrap(unwrap
<Instruction
>(Inst
)->getParent());
2528 LLVMValueRef
LLVMGetFirstInstruction(LLVMBasicBlockRef BB
) {
2529 BasicBlock
*Block
= unwrap(BB
);
2530 BasicBlock::iterator I
= Block
->begin();
2531 if (I
== Block
->end())
2536 LLVMValueRef
LLVMGetLastInstruction(LLVMBasicBlockRef BB
) {
2537 BasicBlock
*Block
= unwrap(BB
);
2538 BasicBlock::iterator I
= Block
->end();
2539 if (I
== Block
->begin())
2544 LLVMValueRef
LLVMGetNextInstruction(LLVMValueRef Inst
) {
2545 Instruction
*Instr
= unwrap
<Instruction
>(Inst
);
2546 BasicBlock::iterator
I(Instr
);
2547 if (++I
== Instr
->getParent()->end())
2552 LLVMValueRef
LLVMGetPreviousInstruction(LLVMValueRef Inst
) {
2553 Instruction
*Instr
= unwrap
<Instruction
>(Inst
);
2554 BasicBlock::iterator
I(Instr
);
2555 if (I
== Instr
->getParent()->begin())
2560 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst
) {
2561 unwrap
<Instruction
>(Inst
)->removeFromParent();
2564 void LLVMInstructionEraseFromParent(LLVMValueRef Inst
) {
2565 unwrap
<Instruction
>(Inst
)->eraseFromParent();
2568 LLVMIntPredicate
LLVMGetICmpPredicate(LLVMValueRef Inst
) {
2569 if (ICmpInst
*I
= dyn_cast
<ICmpInst
>(unwrap(Inst
)))
2570 return (LLVMIntPredicate
)I
->getPredicate();
2571 if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(unwrap(Inst
)))
2572 if (CE
->getOpcode() == Instruction::ICmp
)
2573 return (LLVMIntPredicate
)CE
->getPredicate();
2574 return (LLVMIntPredicate
)0;
2577 LLVMRealPredicate
LLVMGetFCmpPredicate(LLVMValueRef Inst
) {
2578 if (FCmpInst
*I
= dyn_cast
<FCmpInst
>(unwrap(Inst
)))
2579 return (LLVMRealPredicate
)I
->getPredicate();
2580 if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(unwrap(Inst
)))
2581 if (CE
->getOpcode() == Instruction::FCmp
)
2582 return (LLVMRealPredicate
)CE
->getPredicate();
2583 return (LLVMRealPredicate
)0;
2586 LLVMOpcode
LLVMGetInstructionOpcode(LLVMValueRef Inst
) {
2587 if (Instruction
*C
= dyn_cast
<Instruction
>(unwrap(Inst
)))
2588 return map_to_llvmopcode(C
->getOpcode());
2589 return (LLVMOpcode
)0;
2592 LLVMValueRef
LLVMInstructionClone(LLVMValueRef Inst
) {
2593 if (Instruction
*C
= dyn_cast
<Instruction
>(unwrap(Inst
)))
2594 return wrap(C
->clone());
2598 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr
) {
2599 if (FuncletPadInst
*FPI
= dyn_cast
<FuncletPadInst
>(unwrap(Instr
))) {
2600 return FPI
->getNumArgOperands();
2602 return CallSite(unwrap
<Instruction
>(Instr
)).getNumArgOperands();
2605 /*--.. Call and invoke instructions ........................................--*/
2607 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr
) {
2608 return CallSite(unwrap
<Instruction
>(Instr
)).getCallingConv();
2611 void LLVMSetInstructionCallConv(LLVMValueRef Instr
, unsigned CC
) {
2612 return CallSite(unwrap
<Instruction
>(Instr
))
2613 .setCallingConv(static_cast<CallingConv::ID
>(CC
));
2616 void LLVMSetInstrParamAlignment(LLVMValueRef Instr
, unsigned index
,
2618 CallSite Call
= CallSite(unwrap
<Instruction
>(Instr
));
2619 Attribute AlignAttr
= Attribute::getWithAlignment(Call
->getContext(), align
);
2620 Call
.addAttribute(index
, AlignAttr
);
2623 void LLVMAddCallSiteAttribute(LLVMValueRef C
, LLVMAttributeIndex Idx
,
2624 LLVMAttributeRef A
) {
2625 CallSite(unwrap
<Instruction
>(C
)).addAttribute(Idx
, unwrap(A
));
2628 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C
,
2629 LLVMAttributeIndex Idx
) {
2630 auto CS
= CallSite(unwrap
<Instruction
>(C
));
2631 auto AS
= CS
.getAttributes().getAttributes(Idx
);
2632 return AS
.getNumAttributes();
2635 void LLVMGetCallSiteAttributes(LLVMValueRef C
, LLVMAttributeIndex Idx
,
2636 LLVMAttributeRef
*Attrs
) {
2637 auto CS
= CallSite(unwrap
<Instruction
>(C
));
2638 auto AS
= CS
.getAttributes().getAttributes(Idx
);
2643 LLVMAttributeRef
LLVMGetCallSiteEnumAttribute(LLVMValueRef C
,
2644 LLVMAttributeIndex Idx
,
2646 return wrap(CallSite(unwrap
<Instruction
>(C
))
2647 .getAttribute(Idx
, (Attribute::AttrKind
)KindID
));
2650 LLVMAttributeRef
LLVMGetCallSiteStringAttribute(LLVMValueRef C
,
2651 LLVMAttributeIndex Idx
,
2652 const char *K
, unsigned KLen
) {
2653 return wrap(CallSite(unwrap
<Instruction
>(C
))
2654 .getAttribute(Idx
, StringRef(K
, KLen
)));
2657 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C
, LLVMAttributeIndex Idx
,
2659 CallSite(unwrap
<Instruction
>(C
))
2660 .removeAttribute(Idx
, (Attribute::AttrKind
)KindID
);
2663 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C
, LLVMAttributeIndex Idx
,
2664 const char *K
, unsigned KLen
) {
2665 CallSite(unwrap
<Instruction
>(C
)).removeAttribute(Idx
, StringRef(K
, KLen
));
2668 LLVMValueRef
LLVMGetCalledValue(LLVMValueRef Instr
) {
2669 return wrap(CallSite(unwrap
<Instruction
>(Instr
)).getCalledValue());
2672 /*--.. Operations on call instructions (only) ..............................--*/
2674 LLVMBool
LLVMIsTailCall(LLVMValueRef Call
) {
2675 return unwrap
<CallInst
>(Call
)->isTailCall();
2678 void LLVMSetTailCall(LLVMValueRef Call
, LLVMBool isTailCall
) {
2679 unwrap
<CallInst
>(Call
)->setTailCall(isTailCall
);
2682 /*--.. Operations on invoke instructions (only) ............................--*/
2684 LLVMBasicBlockRef
LLVMGetNormalDest(LLVMValueRef Invoke
) {
2685 return wrap(unwrap
<InvokeInst
>(Invoke
)->getNormalDest());
2688 LLVMBasicBlockRef
LLVMGetUnwindDest(LLVMValueRef Invoke
) {
2689 if (CleanupReturnInst
*CRI
= dyn_cast
<CleanupReturnInst
>(unwrap(Invoke
))) {
2690 return wrap(CRI
->getUnwindDest());
2691 } else if (CatchSwitchInst
*CSI
= dyn_cast
<CatchSwitchInst
>(unwrap(Invoke
))) {
2692 return wrap(CSI
->getUnwindDest());
2694 return wrap(unwrap
<InvokeInst
>(Invoke
)->getUnwindDest());
2697 void LLVMSetNormalDest(LLVMValueRef Invoke
, LLVMBasicBlockRef B
) {
2698 unwrap
<InvokeInst
>(Invoke
)->setNormalDest(unwrap(B
));
2701 void LLVMSetUnwindDest(LLVMValueRef Invoke
, LLVMBasicBlockRef B
) {
2702 if (CleanupReturnInst
*CRI
= dyn_cast
<CleanupReturnInst
>(unwrap(Invoke
))) {
2703 return CRI
->setUnwindDest(unwrap(B
));
2704 } else if (CatchSwitchInst
*CSI
= dyn_cast
<CatchSwitchInst
>(unwrap(Invoke
))) {
2705 return CSI
->setUnwindDest(unwrap(B
));
2707 unwrap
<InvokeInst
>(Invoke
)->setUnwindDest(unwrap(B
));
2710 /*--.. Operations on terminators ...........................................--*/
2712 unsigned LLVMGetNumSuccessors(LLVMValueRef Term
) {
2713 return unwrap
<TerminatorInst
>(Term
)->getNumSuccessors();
2716 LLVMBasicBlockRef
LLVMGetSuccessor(LLVMValueRef Term
, unsigned i
) {
2717 return wrap(unwrap
<TerminatorInst
>(Term
)->getSuccessor(i
));
2720 void LLVMSetSuccessor(LLVMValueRef Term
, unsigned i
, LLVMBasicBlockRef block
) {
2721 return unwrap
<TerminatorInst
>(Term
)->setSuccessor(i
,unwrap(block
));
2724 /*--.. Operations on branch instructions (only) ............................--*/
2726 LLVMBool
LLVMIsConditional(LLVMValueRef Branch
) {
2727 return unwrap
<BranchInst
>(Branch
)->isConditional();
2730 LLVMValueRef
LLVMGetCondition(LLVMValueRef Branch
) {
2731 return wrap(unwrap
<BranchInst
>(Branch
)->getCondition());
2734 void LLVMSetCondition(LLVMValueRef Branch
, LLVMValueRef Cond
) {
2735 return unwrap
<BranchInst
>(Branch
)->setCondition(unwrap(Cond
));
2738 /*--.. Operations on switch instructions (only) ............................--*/
2740 LLVMBasicBlockRef
LLVMGetSwitchDefaultDest(LLVMValueRef Switch
) {
2741 return wrap(unwrap
<SwitchInst
>(Switch
)->getDefaultDest());
2744 /*--.. Operations on alloca instructions (only) ............................--*/
2746 LLVMTypeRef
LLVMGetAllocatedType(LLVMValueRef Alloca
) {
2747 return wrap(unwrap
<AllocaInst
>(Alloca
)->getAllocatedType());
2750 /*--.. Operations on gep instructions (only) ...............................--*/
2752 LLVMBool
LLVMIsInBounds(LLVMValueRef GEP
) {
2753 return unwrap
<GetElementPtrInst
>(GEP
)->isInBounds();
2756 void LLVMSetIsInBounds(LLVMValueRef GEP
, LLVMBool InBounds
) {
2757 return unwrap
<GetElementPtrInst
>(GEP
)->setIsInBounds(InBounds
);
2760 /*--.. Operations on phi nodes .............................................--*/
2762 void LLVMAddIncoming(LLVMValueRef PhiNode
, LLVMValueRef
*IncomingValues
,
2763 LLVMBasicBlockRef
*IncomingBlocks
, unsigned Count
) {
2764 PHINode
*PhiVal
= unwrap
<PHINode
>(PhiNode
);
2765 for (unsigned I
= 0; I
!= Count
; ++I
)
2766 PhiVal
->addIncoming(unwrap(IncomingValues
[I
]), unwrap(IncomingBlocks
[I
]));
2769 unsigned LLVMCountIncoming(LLVMValueRef PhiNode
) {
2770 return unwrap
<PHINode
>(PhiNode
)->getNumIncomingValues();
2773 LLVMValueRef
LLVMGetIncomingValue(LLVMValueRef PhiNode
, unsigned Index
) {
2774 return wrap(unwrap
<PHINode
>(PhiNode
)->getIncomingValue(Index
));
2777 LLVMBasicBlockRef
LLVMGetIncomingBlock(LLVMValueRef PhiNode
, unsigned Index
) {
2778 return wrap(unwrap
<PHINode
>(PhiNode
)->getIncomingBlock(Index
));
2781 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2783 unsigned LLVMGetNumIndices(LLVMValueRef Inst
) {
2784 auto *I
= unwrap(Inst
);
2785 if (auto *GEP
= dyn_cast
<GetElementPtrInst
>(I
))
2786 return GEP
->getNumIndices();
2787 if (auto *EV
= dyn_cast
<ExtractValueInst
>(I
))
2788 return EV
->getNumIndices();
2789 if (auto *IV
= dyn_cast
<InsertValueInst
>(I
))
2790 return IV
->getNumIndices();
2791 if (auto *CE
= dyn_cast
<ConstantExpr
>(I
))
2792 return CE
->getIndices().size();
2794 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2797 const unsigned *LLVMGetIndices(LLVMValueRef Inst
) {
2798 auto *I
= unwrap(Inst
);
2799 if (auto *EV
= dyn_cast
<ExtractValueInst
>(I
))
2800 return EV
->getIndices().data();
2801 if (auto *IV
= dyn_cast
<InsertValueInst
>(I
))
2802 return IV
->getIndices().data();
2803 if (auto *CE
= dyn_cast
<ConstantExpr
>(I
))
2804 return CE
->getIndices().data();
2806 "LLVMGetIndices applies only to extractvalue and insertvalue!");
2810 /*===-- Instruction builders ----------------------------------------------===*/
2812 LLVMBuilderRef
LLVMCreateBuilderInContext(LLVMContextRef C
) {
2813 return wrap(new IRBuilder
<>(*unwrap(C
)));
2816 LLVMBuilderRef
LLVMCreateBuilder(void) {
2817 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
2820 void LLVMPositionBuilder(LLVMBuilderRef Builder
, LLVMBasicBlockRef Block
,
2821 LLVMValueRef Instr
) {
2822 BasicBlock
*BB
= unwrap(Block
);
2823 auto I
= Instr
? unwrap
<Instruction
>(Instr
)->getIterator() : BB
->end();
2824 unwrap(Builder
)->SetInsertPoint(BB
, I
);
2827 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder
, LLVMValueRef Instr
) {
2828 Instruction
*I
= unwrap
<Instruction
>(Instr
);
2829 unwrap(Builder
)->SetInsertPoint(I
->getParent(), I
->getIterator());
2832 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder
, LLVMBasicBlockRef Block
) {
2833 BasicBlock
*BB
= unwrap(Block
);
2834 unwrap(Builder
)->SetInsertPoint(BB
);
2837 LLVMBasicBlockRef
LLVMGetInsertBlock(LLVMBuilderRef Builder
) {
2838 return wrap(unwrap(Builder
)->GetInsertBlock());
2841 void LLVMClearInsertionPosition(LLVMBuilderRef Builder
) {
2842 unwrap(Builder
)->ClearInsertionPoint();
2845 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder
, LLVMValueRef Instr
) {
2846 unwrap(Builder
)->Insert(unwrap
<Instruction
>(Instr
));
2849 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder
, LLVMValueRef Instr
,
2851 unwrap(Builder
)->Insert(unwrap
<Instruction
>(Instr
), Name
);
2854 void LLVMDisposeBuilder(LLVMBuilderRef Builder
) {
2855 delete unwrap(Builder
);
2858 /*--.. Metadata builders ...................................................--*/
2860 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder
, LLVMValueRef L
) {
2862 L
? cast
<MDNode
>(unwrap
<MetadataAsValue
>(L
)->getMetadata()) : nullptr;
2863 unwrap(Builder
)->SetCurrentDebugLocation(DebugLoc(Loc
));
2866 LLVMValueRef
LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder
) {
2867 LLVMContext
&Context
= unwrap(Builder
)->getContext();
2868 return wrap(MetadataAsValue::get(
2869 Context
, unwrap(Builder
)->getCurrentDebugLocation().getAsMDNode()));
2872 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder
, LLVMValueRef Inst
) {
2873 unwrap(Builder
)->SetInstDebugLocation(unwrap
<Instruction
>(Inst
));
2877 /*--.. Instruction builders ................................................--*/
2879 LLVMValueRef
LLVMBuildRetVoid(LLVMBuilderRef B
) {
2880 return wrap(unwrap(B
)->CreateRetVoid());
2883 LLVMValueRef
LLVMBuildRet(LLVMBuilderRef B
, LLVMValueRef V
) {
2884 return wrap(unwrap(B
)->CreateRet(unwrap(V
)));
2887 LLVMValueRef
LLVMBuildAggregateRet(LLVMBuilderRef B
, LLVMValueRef
*RetVals
,
2889 return wrap(unwrap(B
)->CreateAggregateRet(unwrap(RetVals
), N
));
2892 LLVMValueRef
LLVMBuildBr(LLVMBuilderRef B
, LLVMBasicBlockRef Dest
) {
2893 return wrap(unwrap(B
)->CreateBr(unwrap(Dest
)));
2896 LLVMValueRef
LLVMBuildCondBr(LLVMBuilderRef B
, LLVMValueRef If
,
2897 LLVMBasicBlockRef Then
, LLVMBasicBlockRef Else
) {
2898 return wrap(unwrap(B
)->CreateCondBr(unwrap(If
), unwrap(Then
), unwrap(Else
)));
2901 LLVMValueRef
LLVMBuildSwitch(LLVMBuilderRef B
, LLVMValueRef V
,
2902 LLVMBasicBlockRef Else
, unsigned NumCases
) {
2903 return wrap(unwrap(B
)->CreateSwitch(unwrap(V
), unwrap(Else
), NumCases
));
2906 LLVMValueRef
LLVMBuildIndirectBr(LLVMBuilderRef B
, LLVMValueRef Addr
,
2907 unsigned NumDests
) {
2908 return wrap(unwrap(B
)->CreateIndirectBr(unwrap(Addr
), NumDests
));
2911 LLVMValueRef
LLVMBuildInvoke(LLVMBuilderRef B
, LLVMValueRef Fn
,
2912 LLVMValueRef
*Args
, unsigned NumArgs
,
2913 LLVMBasicBlockRef Then
, LLVMBasicBlockRef Catch
,
2915 return wrap(unwrap(B
)->CreateInvoke(unwrap(Fn
), unwrap(Then
), unwrap(Catch
),
2916 makeArrayRef(unwrap(Args
), NumArgs
),
2920 LLVMValueRef
LLVMBuildLandingPad(LLVMBuilderRef B
, LLVMTypeRef Ty
,
2921 LLVMValueRef PersFn
, unsigned NumClauses
,
2923 // The personality used to live on the landingpad instruction, but now it
2924 // lives on the parent function. For compatibility, take the provided
2925 // personality and put it on the parent function.
2927 unwrap(B
)->GetInsertBlock()->getParent()->setPersonalityFn(
2928 cast
<Function
>(unwrap(PersFn
)));
2929 return wrap(unwrap(B
)->CreateLandingPad(unwrap(Ty
), NumClauses
, Name
));
2932 LLVMValueRef
LLVMBuildCatchPad(LLVMBuilderRef B
, LLVMValueRef ParentPad
,
2933 LLVMValueRef
*Args
, unsigned NumArgs
,
2935 return wrap(unwrap(B
)->CreateCatchPad(unwrap(ParentPad
),
2936 makeArrayRef(unwrap(Args
), NumArgs
),
2940 LLVMValueRef
LLVMBuildCleanupPad(LLVMBuilderRef B
, LLVMValueRef ParentPad
,
2941 LLVMValueRef
*Args
, unsigned NumArgs
,
2943 if (ParentPad
== nullptr) {
2944 Type
*Ty
= Type::getTokenTy(unwrap(B
)->getContext());
2945 ParentPad
= wrap(Constant::getNullValue(Ty
));
2947 return wrap(unwrap(B
)->CreateCleanupPad(unwrap(ParentPad
),
2948 makeArrayRef(unwrap(Args
), NumArgs
),
2952 LLVMValueRef
LLVMBuildResume(LLVMBuilderRef B
, LLVMValueRef Exn
) {
2953 return wrap(unwrap(B
)->CreateResume(unwrap(Exn
)));
2956 LLVMValueRef
LLVMBuildCatchSwitch(LLVMBuilderRef B
, LLVMValueRef ParentPad
,
2957 LLVMBasicBlockRef UnwindBB
,
2958 unsigned NumHandlers
, const char *Name
) {
2959 if (ParentPad
== nullptr) {
2960 Type
*Ty
= Type::getTokenTy(unwrap(B
)->getContext());
2961 ParentPad
= wrap(Constant::getNullValue(Ty
));
2963 return wrap(unwrap(B
)->CreateCatchSwitch(unwrap(ParentPad
), unwrap(UnwindBB
),
2964 NumHandlers
, Name
));
2967 LLVMValueRef
LLVMBuildCatchRet(LLVMBuilderRef B
, LLVMValueRef CatchPad
,
2968 LLVMBasicBlockRef BB
) {
2969 return wrap(unwrap(B
)->CreateCatchRet(unwrap
<CatchPadInst
>(CatchPad
),
2973 LLVMValueRef
LLVMBuildCleanupRet(LLVMBuilderRef B
, LLVMValueRef CatchPad
,
2974 LLVMBasicBlockRef BB
) {
2975 return wrap(unwrap(B
)->CreateCleanupRet(unwrap
<CleanupPadInst
>(CatchPad
),
2979 LLVMValueRef
LLVMBuildUnreachable(LLVMBuilderRef B
) {
2980 return wrap(unwrap(B
)->CreateUnreachable());
2983 void LLVMAddCase(LLVMValueRef Switch
, LLVMValueRef OnVal
,
2984 LLVMBasicBlockRef Dest
) {
2985 unwrap
<SwitchInst
>(Switch
)->addCase(unwrap
<ConstantInt
>(OnVal
), unwrap(Dest
));
2988 void LLVMAddDestination(LLVMValueRef IndirectBr
, LLVMBasicBlockRef Dest
) {
2989 unwrap
<IndirectBrInst
>(IndirectBr
)->addDestination(unwrap(Dest
));
2992 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad
) {
2993 return unwrap
<LandingPadInst
>(LandingPad
)->getNumClauses();
2996 LLVMValueRef
LLVMGetClause(LLVMValueRef LandingPad
, unsigned Idx
) {
2997 return wrap(unwrap
<LandingPadInst
>(LandingPad
)->getClause(Idx
));
3000 void LLVMAddClause(LLVMValueRef LandingPad
, LLVMValueRef ClauseVal
) {
3001 unwrap
<LandingPadInst
>(LandingPad
)->
3002 addClause(cast
<Constant
>(unwrap(ClauseVal
)));
3005 LLVMBool
LLVMIsCleanup(LLVMValueRef LandingPad
) {
3006 return unwrap
<LandingPadInst
>(LandingPad
)->isCleanup();
3009 void LLVMSetCleanup(LLVMValueRef LandingPad
, LLVMBool Val
) {
3010 unwrap
<LandingPadInst
>(LandingPad
)->setCleanup(Val
);
3013 void LLVMAddHandler(LLVMValueRef CatchSwitch
, LLVMBasicBlockRef Dest
) {
3014 unwrap
<CatchSwitchInst
>(CatchSwitch
)->addHandler(unwrap(Dest
));
3017 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch
) {
3018 return unwrap
<CatchSwitchInst
>(CatchSwitch
)->getNumHandlers();
3021 void LLVMGetHandlers(LLVMValueRef CatchSwitch
, LLVMBasicBlockRef
*Handlers
) {
3022 CatchSwitchInst
*CSI
= unwrap
<CatchSwitchInst
>(CatchSwitch
);
3023 for (CatchSwitchInst::handler_iterator I
= CSI
->handler_begin(),
3024 E
= CSI
->handler_end(); I
!= E
; ++I
)
3025 *Handlers
++ = wrap(*I
);
3028 LLVMValueRef
LLVMGetParentCatchSwitch(LLVMValueRef CatchPad
) {
3029 return wrap(unwrap
<CatchPadInst
>(CatchPad
)->getCatchSwitch());
3032 void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad
, LLVMValueRef CatchSwitch
) {
3033 unwrap
<CatchPadInst
>(CatchPad
)
3034 ->setCatchSwitch(unwrap
<CatchSwitchInst
>(CatchSwitch
));
3037 /*--.. Funclets ...........................................................--*/
3039 LLVMValueRef
LLVMGetArgOperand(LLVMValueRef Funclet
, unsigned i
) {
3040 return wrap(unwrap
<FuncletPadInst
>(Funclet
)->getArgOperand(i
));
3043 void LLVMSetArgOperand(LLVMValueRef Funclet
, unsigned i
, LLVMValueRef value
) {
3044 unwrap
<FuncletPadInst
>(Funclet
)->setArgOperand(i
, unwrap(value
));
3047 /*--.. Arithmetic ..........................................................--*/
3049 LLVMValueRef
LLVMBuildAdd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3051 return wrap(unwrap(B
)->CreateAdd(unwrap(LHS
), unwrap(RHS
), Name
));
3054 LLVMValueRef
LLVMBuildNSWAdd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3056 return wrap(unwrap(B
)->CreateNSWAdd(unwrap(LHS
), unwrap(RHS
), Name
));
3059 LLVMValueRef
LLVMBuildNUWAdd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3061 return wrap(unwrap(B
)->CreateNUWAdd(unwrap(LHS
), unwrap(RHS
), Name
));
3064 LLVMValueRef
LLVMBuildFAdd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3066 return wrap(unwrap(B
)->CreateFAdd(unwrap(LHS
), unwrap(RHS
), Name
));
3069 LLVMValueRef
LLVMBuildSub(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3071 return wrap(unwrap(B
)->CreateSub(unwrap(LHS
), unwrap(RHS
), Name
));
3074 LLVMValueRef
LLVMBuildNSWSub(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3076 return wrap(unwrap(B
)->CreateNSWSub(unwrap(LHS
), unwrap(RHS
), Name
));
3079 LLVMValueRef
LLVMBuildNUWSub(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3081 return wrap(unwrap(B
)->CreateNUWSub(unwrap(LHS
), unwrap(RHS
), Name
));
3084 LLVMValueRef
LLVMBuildFSub(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3086 return wrap(unwrap(B
)->CreateFSub(unwrap(LHS
), unwrap(RHS
), Name
));
3089 LLVMValueRef
LLVMBuildMul(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3091 return wrap(unwrap(B
)->CreateMul(unwrap(LHS
), unwrap(RHS
), Name
));
3094 LLVMValueRef
LLVMBuildNSWMul(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3096 return wrap(unwrap(B
)->CreateNSWMul(unwrap(LHS
), unwrap(RHS
), Name
));
3099 LLVMValueRef
LLVMBuildNUWMul(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3101 return wrap(unwrap(B
)->CreateNUWMul(unwrap(LHS
), unwrap(RHS
), Name
));
3104 LLVMValueRef
LLVMBuildFMul(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3106 return wrap(unwrap(B
)->CreateFMul(unwrap(LHS
), unwrap(RHS
), Name
));
3109 LLVMValueRef
LLVMBuildUDiv(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3111 return wrap(unwrap(B
)->CreateUDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3114 LLVMValueRef
LLVMBuildExactUDiv(LLVMBuilderRef B
, LLVMValueRef LHS
,
3115 LLVMValueRef RHS
, const char *Name
) {
3116 return wrap(unwrap(B
)->CreateExactUDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3119 LLVMValueRef
LLVMBuildSDiv(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3121 return wrap(unwrap(B
)->CreateSDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3124 LLVMValueRef
LLVMBuildExactSDiv(LLVMBuilderRef B
, LLVMValueRef LHS
,
3125 LLVMValueRef RHS
, const char *Name
) {
3126 return wrap(unwrap(B
)->CreateExactSDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3129 LLVMValueRef
LLVMBuildFDiv(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3131 return wrap(unwrap(B
)->CreateFDiv(unwrap(LHS
), unwrap(RHS
), Name
));
3134 LLVMValueRef
LLVMBuildURem(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3136 return wrap(unwrap(B
)->CreateURem(unwrap(LHS
), unwrap(RHS
), Name
));
3139 LLVMValueRef
LLVMBuildSRem(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3141 return wrap(unwrap(B
)->CreateSRem(unwrap(LHS
), unwrap(RHS
), Name
));
3144 LLVMValueRef
LLVMBuildFRem(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3146 return wrap(unwrap(B
)->CreateFRem(unwrap(LHS
), unwrap(RHS
), Name
));
3149 LLVMValueRef
LLVMBuildShl(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3151 return wrap(unwrap(B
)->CreateShl(unwrap(LHS
), unwrap(RHS
), Name
));
3154 LLVMValueRef
LLVMBuildLShr(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3156 return wrap(unwrap(B
)->CreateLShr(unwrap(LHS
), unwrap(RHS
), Name
));
3159 LLVMValueRef
LLVMBuildAShr(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3161 return wrap(unwrap(B
)->CreateAShr(unwrap(LHS
), unwrap(RHS
), Name
));
3164 LLVMValueRef
LLVMBuildAnd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3166 return wrap(unwrap(B
)->CreateAnd(unwrap(LHS
), unwrap(RHS
), Name
));
3169 LLVMValueRef
LLVMBuildOr(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3171 return wrap(unwrap(B
)->CreateOr(unwrap(LHS
), unwrap(RHS
), Name
));
3174 LLVMValueRef
LLVMBuildXor(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
3176 return wrap(unwrap(B
)->CreateXor(unwrap(LHS
), unwrap(RHS
), Name
));
3179 LLVMValueRef
LLVMBuildBinOp(LLVMBuilderRef B
, LLVMOpcode Op
,
3180 LLVMValueRef LHS
, LLVMValueRef RHS
,
3182 return wrap(unwrap(B
)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op
)), unwrap(LHS
),
3183 unwrap(RHS
), Name
));
3186 LLVMValueRef
LLVMBuildNeg(LLVMBuilderRef B
, LLVMValueRef V
, const char *Name
) {
3187 return wrap(unwrap(B
)->CreateNeg(unwrap(V
), Name
));
3190 LLVMValueRef
LLVMBuildNSWNeg(LLVMBuilderRef B
, LLVMValueRef V
,
3192 return wrap(unwrap(B
)->CreateNSWNeg(unwrap(V
), Name
));
3195 LLVMValueRef
LLVMBuildNUWNeg(LLVMBuilderRef B
, LLVMValueRef V
,
3197 return wrap(unwrap(B
)->CreateNUWNeg(unwrap(V
), Name
));
3200 LLVMValueRef
LLVMBuildFNeg(LLVMBuilderRef B
, LLVMValueRef V
, const char *Name
) {
3201 return wrap(unwrap(B
)->CreateFNeg(unwrap(V
), Name
));
3204 LLVMValueRef
LLVMBuildNot(LLVMBuilderRef B
, LLVMValueRef V
, const char *Name
) {
3205 return wrap(unwrap(B
)->CreateNot(unwrap(V
), Name
));
3208 /*--.. Memory ..............................................................--*/
3210 LLVMValueRef
LLVMBuildMalloc(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3212 Type
* ITy
= Type::getInt32Ty(unwrap(B
)->GetInsertBlock()->getContext());
3213 Constant
* AllocSize
= ConstantExpr::getSizeOf(unwrap(Ty
));
3214 AllocSize
= ConstantExpr::getTruncOrBitCast(AllocSize
, ITy
);
3215 Instruction
* Malloc
= CallInst::CreateMalloc(unwrap(B
)->GetInsertBlock(),
3216 ITy
, unwrap(Ty
), AllocSize
,
3217 nullptr, nullptr, "");
3218 return wrap(unwrap(B
)->Insert(Malloc
, Twine(Name
)));
3221 LLVMValueRef
LLVMBuildArrayMalloc(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3222 LLVMValueRef Val
, const char *Name
) {
3223 Type
* ITy
= Type::getInt32Ty(unwrap(B
)->GetInsertBlock()->getContext());
3224 Constant
* AllocSize
= ConstantExpr::getSizeOf(unwrap(Ty
));
3225 AllocSize
= ConstantExpr::getTruncOrBitCast(AllocSize
, ITy
);
3226 Instruction
* Malloc
= CallInst::CreateMalloc(unwrap(B
)->GetInsertBlock(),
3227 ITy
, unwrap(Ty
), AllocSize
,
3228 unwrap(Val
), nullptr, "");
3229 return wrap(unwrap(B
)->Insert(Malloc
, Twine(Name
)));
3232 LLVMValueRef
LLVMBuildAlloca(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3234 return wrap(unwrap(B
)->CreateAlloca(unwrap(Ty
), nullptr, Name
));
3237 LLVMValueRef
LLVMBuildArrayAlloca(LLVMBuilderRef B
, LLVMTypeRef Ty
,
3238 LLVMValueRef Val
, const char *Name
) {
3239 return wrap(unwrap(B
)->CreateAlloca(unwrap(Ty
), unwrap(Val
), Name
));
3242 LLVMValueRef
LLVMBuildFree(LLVMBuilderRef B
, LLVMValueRef PointerVal
) {
3243 return wrap(unwrap(B
)->Insert(
3244 CallInst::CreateFree(unwrap(PointerVal
), unwrap(B
)->GetInsertBlock())));
3247 LLVMValueRef
LLVMBuildLoad(LLVMBuilderRef B
, LLVMValueRef PointerVal
,
3249 return wrap(unwrap(B
)->CreateLoad(unwrap(PointerVal
), Name
));
3252 LLVMValueRef
LLVMBuildStore(LLVMBuilderRef B
, LLVMValueRef Val
,
3253 LLVMValueRef PointerVal
) {
3254 return wrap(unwrap(B
)->CreateStore(unwrap(Val
), unwrap(PointerVal
)));
3257 static AtomicOrdering
mapFromLLVMOrdering(LLVMAtomicOrdering Ordering
) {
3259 case LLVMAtomicOrderingNotAtomic
: return AtomicOrdering::NotAtomic
;
3260 case LLVMAtomicOrderingUnordered
: return AtomicOrdering::Unordered
;
3261 case LLVMAtomicOrderingMonotonic
: return AtomicOrdering::Monotonic
;
3262 case LLVMAtomicOrderingAcquire
: return AtomicOrdering::Acquire
;
3263 case LLVMAtomicOrderingRelease
: return AtomicOrdering::Release
;
3264 case LLVMAtomicOrderingAcquireRelease
:
3265 return AtomicOrdering::AcquireRelease
;
3266 case LLVMAtomicOrderingSequentiallyConsistent
:
3267 return AtomicOrdering::SequentiallyConsistent
;
3270 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3273 static LLVMAtomicOrdering
mapToLLVMOrdering(AtomicOrdering Ordering
) {
3275 case AtomicOrdering::NotAtomic
: return LLVMAtomicOrderingNotAtomic
;
3276 case AtomicOrdering::Unordered
: return LLVMAtomicOrderingUnordered
;
3277 case AtomicOrdering::Monotonic
: return LLVMAtomicOrderingMonotonic
;
3278 case AtomicOrdering::Acquire
: return LLVMAtomicOrderingAcquire
;
3279 case AtomicOrdering::Release
: return LLVMAtomicOrderingRelease
;
3280 case AtomicOrdering::AcquireRelease
:
3281 return LLVMAtomicOrderingAcquireRelease
;
3282 case AtomicOrdering::SequentiallyConsistent
:
3283 return LLVMAtomicOrderingSequentiallyConsistent
;
3286 llvm_unreachable("Invalid AtomicOrdering value!");
3289 // TODO: Should this and other atomic instructions support building with
3291 LLVMValueRef
LLVMBuildFence(LLVMBuilderRef B
, LLVMAtomicOrdering Ordering
,
3292 LLVMBool isSingleThread
, const char *Name
) {
3294 unwrap(B
)->CreateFence(mapFromLLVMOrdering(Ordering
),
3295 isSingleThread
? SyncScope::SingleThread
3296 : SyncScope::System
,
3300 LLVMValueRef
LLVMBuildGEP(LLVMBuilderRef B
, LLVMValueRef Pointer
,
3301 LLVMValueRef
*Indices
, unsigned NumIndices
,
3303 ArrayRef
<Value
*> IdxList(unwrap(Indices
), NumIndices
);
3304 return wrap(unwrap(B
)->CreateGEP(nullptr, unwrap(Pointer
), IdxList
, Name
));
3307 LLVMValueRef
LLVMBuildInBoundsGEP(LLVMBuilderRef B
, LLVMValueRef Pointer
,
3308 LLVMValueRef
*Indices
, unsigned NumIndices
,
3310 ArrayRef
<Value
*> IdxList(unwrap(Indices
), NumIndices
);
3312 unwrap(B
)->CreateInBoundsGEP(nullptr, unwrap(Pointer
), IdxList
, Name
));
3315 LLVMValueRef
LLVMBuildStructGEP(LLVMBuilderRef B
, LLVMValueRef Pointer
,
3316 unsigned Idx
, const char *Name
) {
3317 return wrap(unwrap(B
)->CreateStructGEP(nullptr, unwrap(Pointer
), Idx
, Name
));
3320 LLVMValueRef
LLVMBuildGlobalString(LLVMBuilderRef B
, const char *Str
,
3322 return wrap(unwrap(B
)->CreateGlobalString(Str
, Name
));
3325 LLVMValueRef
LLVMBuildGlobalStringPtr(LLVMBuilderRef B
, const char *Str
,
3327 return wrap(unwrap(B
)->CreateGlobalStringPtr(Str
, Name
));
3330 LLVMBool
LLVMGetVolatile(LLVMValueRef MemAccessInst
) {
3331 Value
*P
= unwrap
<Value
>(MemAccessInst
);
3332 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
3333 return LI
->isVolatile();
3334 return cast
<StoreInst
>(P
)->isVolatile();
3337 void LLVMSetVolatile(LLVMValueRef MemAccessInst
, LLVMBool isVolatile
) {
3338 Value
*P
= unwrap
<Value
>(MemAccessInst
);
3339 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
3340 return LI
->setVolatile(isVolatile
);
3341 return cast
<StoreInst
>(P
)->setVolatile(isVolatile
);
3344 LLVMAtomicOrdering
LLVMGetOrdering(LLVMValueRef MemAccessInst
) {
3345 Value
*P
= unwrap
<Value
>(MemAccessInst
);
3347 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
3348 O
= LI
->getOrdering();
3350 O
= cast
<StoreInst
>(P
)->getOrdering();
3351 return mapToLLVMOrdering(O
);
3354 void LLVMSetOrdering(LLVMValueRef MemAccessInst
, LLVMAtomicOrdering Ordering
) {
3355 Value
*P
= unwrap
<Value
>(MemAccessInst
);
3356 AtomicOrdering O
= mapFromLLVMOrdering(Ordering
);
3358 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(P
))
3359 return LI
->setOrdering(O
);
3360 return cast
<StoreInst
>(P
)->setOrdering(O
);
3363 /*--.. Casts ...............................................................--*/
3365 LLVMValueRef
LLVMBuildTrunc(LLVMBuilderRef B
, LLVMValueRef Val
,
3366 LLVMTypeRef DestTy
, const char *Name
) {
3367 return wrap(unwrap(B
)->CreateTrunc(unwrap(Val
), unwrap(DestTy
), Name
));
3370 LLVMValueRef
LLVMBuildZExt(LLVMBuilderRef B
, LLVMValueRef Val
,
3371 LLVMTypeRef DestTy
, const char *Name
) {
3372 return wrap(unwrap(B
)->CreateZExt(unwrap(Val
), unwrap(DestTy
), Name
));
3375 LLVMValueRef
LLVMBuildSExt(LLVMBuilderRef B
, LLVMValueRef Val
,
3376 LLVMTypeRef DestTy
, const char *Name
) {
3377 return wrap(unwrap(B
)->CreateSExt(unwrap(Val
), unwrap(DestTy
), Name
));
3380 LLVMValueRef
LLVMBuildFPToUI(LLVMBuilderRef B
, LLVMValueRef Val
,
3381 LLVMTypeRef DestTy
, const char *Name
) {
3382 return wrap(unwrap(B
)->CreateFPToUI(unwrap(Val
), unwrap(DestTy
), Name
));
3385 LLVMValueRef
LLVMBuildFPToSI(LLVMBuilderRef B
, LLVMValueRef Val
,
3386 LLVMTypeRef DestTy
, const char *Name
) {
3387 return wrap(unwrap(B
)->CreateFPToSI(unwrap(Val
), unwrap(DestTy
), Name
));
3390 LLVMValueRef
LLVMBuildUIToFP(LLVMBuilderRef B
, LLVMValueRef Val
,
3391 LLVMTypeRef DestTy
, const char *Name
) {
3392 return wrap(unwrap(B
)->CreateUIToFP(unwrap(Val
), unwrap(DestTy
), Name
));
3395 LLVMValueRef
LLVMBuildSIToFP(LLVMBuilderRef B
, LLVMValueRef Val
,
3396 LLVMTypeRef DestTy
, const char *Name
) {
3397 return wrap(unwrap(B
)->CreateSIToFP(unwrap(Val
), unwrap(DestTy
), Name
));
3400 LLVMValueRef
LLVMBuildFPTrunc(LLVMBuilderRef B
, LLVMValueRef Val
,
3401 LLVMTypeRef DestTy
, const char *Name
) {
3402 return wrap(unwrap(B
)->CreateFPTrunc(unwrap(Val
), unwrap(DestTy
), Name
));
3405 LLVMValueRef
LLVMBuildFPExt(LLVMBuilderRef B
, LLVMValueRef Val
,
3406 LLVMTypeRef DestTy
, const char *Name
) {
3407 return wrap(unwrap(B
)->CreateFPExt(unwrap(Val
), unwrap(DestTy
), Name
));
3410 LLVMValueRef
LLVMBuildPtrToInt(LLVMBuilderRef B
, LLVMValueRef Val
,
3411 LLVMTypeRef DestTy
, const char *Name
) {
3412 return wrap(unwrap(B
)->CreatePtrToInt(unwrap(Val
), unwrap(DestTy
), Name
));
3415 LLVMValueRef
LLVMBuildIntToPtr(LLVMBuilderRef B
, LLVMValueRef Val
,
3416 LLVMTypeRef DestTy
, const char *Name
) {
3417 return wrap(unwrap(B
)->CreateIntToPtr(unwrap(Val
), unwrap(DestTy
), Name
));
3420 LLVMValueRef
LLVMBuildBitCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3421 LLVMTypeRef DestTy
, const char *Name
) {
3422 return wrap(unwrap(B
)->CreateBitCast(unwrap(Val
), unwrap(DestTy
), Name
));
3425 LLVMValueRef
LLVMBuildAddrSpaceCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3426 LLVMTypeRef DestTy
, const char *Name
) {
3427 return wrap(unwrap(B
)->CreateAddrSpaceCast(unwrap(Val
), unwrap(DestTy
), Name
));
3430 LLVMValueRef
LLVMBuildZExtOrBitCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3431 LLVMTypeRef DestTy
, const char *Name
) {
3432 return wrap(unwrap(B
)->CreateZExtOrBitCast(unwrap(Val
), unwrap(DestTy
),
3436 LLVMValueRef
LLVMBuildSExtOrBitCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3437 LLVMTypeRef DestTy
, const char *Name
) {
3438 return wrap(unwrap(B
)->CreateSExtOrBitCast(unwrap(Val
), unwrap(DestTy
),
3442 LLVMValueRef
LLVMBuildTruncOrBitCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3443 LLVMTypeRef DestTy
, const char *Name
) {
3444 return wrap(unwrap(B
)->CreateTruncOrBitCast(unwrap(Val
), unwrap(DestTy
),
3448 LLVMValueRef
LLVMBuildCast(LLVMBuilderRef B
, LLVMOpcode Op
, LLVMValueRef Val
,
3449 LLVMTypeRef DestTy
, const char *Name
) {
3450 return wrap(unwrap(B
)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op
)), unwrap(Val
),
3451 unwrap(DestTy
), Name
));
3454 LLVMValueRef
LLVMBuildPointerCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3455 LLVMTypeRef DestTy
, const char *Name
) {
3456 return wrap(unwrap(B
)->CreatePointerCast(unwrap(Val
), unwrap(DestTy
), Name
));
3459 LLVMValueRef
LLVMBuildIntCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3460 LLVMTypeRef DestTy
, const char *Name
) {
3461 return wrap(unwrap(B
)->CreateIntCast(unwrap(Val
), unwrap(DestTy
),
3462 /*isSigned*/true, Name
));
3465 LLVMValueRef
LLVMBuildFPCast(LLVMBuilderRef B
, LLVMValueRef Val
,
3466 LLVMTypeRef DestTy
, const char *Name
) {
3467 return wrap(unwrap(B
)->CreateFPCast(unwrap(Val
), unwrap(DestTy
), Name
));
3470 /*--.. Comparisons .........................................................--*/
3472 LLVMValueRef
LLVMBuildICmp(LLVMBuilderRef B
, LLVMIntPredicate Op
,
3473 LLVMValueRef LHS
, LLVMValueRef RHS
,
3475 return wrap(unwrap(B
)->CreateICmp(static_cast<ICmpInst::Predicate
>(Op
),
3476 unwrap(LHS
), unwrap(RHS
), Name
));
3479 LLVMValueRef
LLVMBuildFCmp(LLVMBuilderRef B
, LLVMRealPredicate Op
,
3480 LLVMValueRef LHS
, LLVMValueRef RHS
,
3482 return wrap(unwrap(B
)->CreateFCmp(static_cast<FCmpInst::Predicate
>(Op
),
3483 unwrap(LHS
), unwrap(RHS
), Name
));
3486 /*--.. Miscellaneous instructions ..........................................--*/
3488 LLVMValueRef
LLVMBuildPhi(LLVMBuilderRef B
, LLVMTypeRef Ty
, const char *Name
) {
3489 return wrap(unwrap(B
)->CreatePHI(unwrap(Ty
), 0, Name
));
3492 LLVMValueRef
LLVMBuildCall(LLVMBuilderRef B
, LLVMValueRef Fn
,
3493 LLVMValueRef
*Args
, unsigned NumArgs
,
3495 return wrap(unwrap(B
)->CreateCall(unwrap(Fn
),
3496 makeArrayRef(unwrap(Args
), NumArgs
),
3500 LLVMValueRef
LLVMBuildSelect(LLVMBuilderRef B
, LLVMValueRef If
,
3501 LLVMValueRef Then
, LLVMValueRef Else
,
3503 return wrap(unwrap(B
)->CreateSelect(unwrap(If
), unwrap(Then
), unwrap(Else
),
3507 LLVMValueRef
LLVMBuildVAArg(LLVMBuilderRef B
, LLVMValueRef List
,
3508 LLVMTypeRef Ty
, const char *Name
) {
3509 return wrap(unwrap(B
)->CreateVAArg(unwrap(List
), unwrap(Ty
), Name
));
3512 LLVMValueRef
LLVMBuildExtractElement(LLVMBuilderRef B
, LLVMValueRef VecVal
,
3513 LLVMValueRef Index
, const char *Name
) {
3514 return wrap(unwrap(B
)->CreateExtractElement(unwrap(VecVal
), unwrap(Index
),
3518 LLVMValueRef
LLVMBuildInsertElement(LLVMBuilderRef B
, LLVMValueRef VecVal
,
3519 LLVMValueRef EltVal
, LLVMValueRef Index
,
3521 return wrap(unwrap(B
)->CreateInsertElement(unwrap(VecVal
), unwrap(EltVal
),
3522 unwrap(Index
), Name
));
3525 LLVMValueRef
LLVMBuildShuffleVector(LLVMBuilderRef B
, LLVMValueRef V1
,
3526 LLVMValueRef V2
, LLVMValueRef Mask
,
3528 return wrap(unwrap(B
)->CreateShuffleVector(unwrap(V1
), unwrap(V2
),
3529 unwrap(Mask
), Name
));
3532 LLVMValueRef
LLVMBuildExtractValue(LLVMBuilderRef B
, LLVMValueRef AggVal
,
3533 unsigned Index
, const char *Name
) {
3534 return wrap(unwrap(B
)->CreateExtractValue(unwrap(AggVal
), Index
, Name
));
3537 LLVMValueRef
LLVMBuildInsertValue(LLVMBuilderRef B
, LLVMValueRef AggVal
,
3538 LLVMValueRef EltVal
, unsigned Index
,
3540 return wrap(unwrap(B
)->CreateInsertValue(unwrap(AggVal
), unwrap(EltVal
),
3544 LLVMValueRef
LLVMBuildIsNull(LLVMBuilderRef B
, LLVMValueRef Val
,
3546 return wrap(unwrap(B
)->CreateIsNull(unwrap(Val
), Name
));
3549 LLVMValueRef
LLVMBuildIsNotNull(LLVMBuilderRef B
, LLVMValueRef Val
,
3551 return wrap(unwrap(B
)->CreateIsNotNull(unwrap(Val
), Name
));
3554 LLVMValueRef
LLVMBuildPtrDiff(LLVMBuilderRef B
, LLVMValueRef LHS
,
3555 LLVMValueRef RHS
, const char *Name
) {
3556 return wrap(unwrap(B
)->CreatePtrDiff(unwrap(LHS
), unwrap(RHS
), Name
));
3559 LLVMValueRef
LLVMBuildAtomicRMW(LLVMBuilderRef B
,LLVMAtomicRMWBinOp op
,
3560 LLVMValueRef PTR
, LLVMValueRef Val
,
3561 LLVMAtomicOrdering ordering
,
3562 LLVMBool singleThread
) {
3563 AtomicRMWInst::BinOp intop
;
3565 case LLVMAtomicRMWBinOpXchg
: intop
= AtomicRMWInst::Xchg
; break;
3566 case LLVMAtomicRMWBinOpAdd
: intop
= AtomicRMWInst::Add
; break;
3567 case LLVMAtomicRMWBinOpSub
: intop
= AtomicRMWInst::Sub
; break;
3568 case LLVMAtomicRMWBinOpAnd
: intop
= AtomicRMWInst::And
; break;
3569 case LLVMAtomicRMWBinOpNand
: intop
= AtomicRMWInst::Nand
; break;
3570 case LLVMAtomicRMWBinOpOr
: intop
= AtomicRMWInst::Or
; break;
3571 case LLVMAtomicRMWBinOpXor
: intop
= AtomicRMWInst::Xor
; break;
3572 case LLVMAtomicRMWBinOpMax
: intop
= AtomicRMWInst::Max
; break;
3573 case LLVMAtomicRMWBinOpMin
: intop
= AtomicRMWInst::Min
; break;
3574 case LLVMAtomicRMWBinOpUMax
: intop
= AtomicRMWInst::UMax
; break;
3575 case LLVMAtomicRMWBinOpUMin
: intop
= AtomicRMWInst::UMin
; break;
3577 return wrap(unwrap(B
)->CreateAtomicRMW(intop
, unwrap(PTR
), unwrap(Val
),
3578 mapFromLLVMOrdering(ordering
), singleThread
? SyncScope::SingleThread
3579 : SyncScope::System
));
3582 LLVMValueRef
LLVMBuildAtomicCmpXchg(LLVMBuilderRef B
, LLVMValueRef Ptr
,
3583 LLVMValueRef Cmp
, LLVMValueRef New
,
3584 LLVMAtomicOrdering SuccessOrdering
,
3585 LLVMAtomicOrdering FailureOrdering
,
3586 LLVMBool singleThread
) {
3588 return wrap(unwrap(B
)->CreateAtomicCmpXchg(unwrap(Ptr
), unwrap(Cmp
),
3589 unwrap(New
), mapFromLLVMOrdering(SuccessOrdering
),
3590 mapFromLLVMOrdering(FailureOrdering
),
3591 singleThread
? SyncScope::SingleThread
: SyncScope::System
));
3595 LLVMBool
LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst
) {
3596 Value
*P
= unwrap
<Value
>(AtomicInst
);
3598 if (AtomicRMWInst
*I
= dyn_cast
<AtomicRMWInst
>(P
))
3599 return I
->getSyncScopeID() == SyncScope::SingleThread
;
3600 return cast
<AtomicCmpXchgInst
>(P
)->getSyncScopeID() ==
3601 SyncScope::SingleThread
;
3604 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst
, LLVMBool NewValue
) {
3605 Value
*P
= unwrap
<Value
>(AtomicInst
);
3606 SyncScope::ID SSID
= NewValue
? SyncScope::SingleThread
: SyncScope::System
;
3608 if (AtomicRMWInst
*I
= dyn_cast
<AtomicRMWInst
>(P
))
3609 return I
->setSyncScopeID(SSID
);
3610 return cast
<AtomicCmpXchgInst
>(P
)->setSyncScopeID(SSID
);
3613 LLVMAtomicOrdering
LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst
) {
3614 Value
*P
= unwrap
<Value
>(CmpXchgInst
);
3615 return mapToLLVMOrdering(cast
<AtomicCmpXchgInst
>(P
)->getSuccessOrdering());
3618 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst
,
3619 LLVMAtomicOrdering Ordering
) {
3620 Value
*P
= unwrap
<Value
>(CmpXchgInst
);
3621 AtomicOrdering O
= mapFromLLVMOrdering(Ordering
);
3623 return cast
<AtomicCmpXchgInst
>(P
)->setSuccessOrdering(O
);
3626 LLVMAtomicOrdering
LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst
) {
3627 Value
*P
= unwrap
<Value
>(CmpXchgInst
);
3628 return mapToLLVMOrdering(cast
<AtomicCmpXchgInst
>(P
)->getFailureOrdering());
3631 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst
,
3632 LLVMAtomicOrdering Ordering
) {
3633 Value
*P
= unwrap
<Value
>(CmpXchgInst
);
3634 AtomicOrdering O
= mapFromLLVMOrdering(Ordering
);
3636 return cast
<AtomicCmpXchgInst
>(P
)->setFailureOrdering(O
);
3639 /*===-- Module providers --------------------------------------------------===*/
3641 LLVMModuleProviderRef
3642 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M
) {
3643 return reinterpret_cast<LLVMModuleProviderRef
>(M
);
3646 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP
) {
3651 /*===-- Memory buffers ----------------------------------------------------===*/
3653 LLVMBool
LLVMCreateMemoryBufferWithContentsOfFile(
3655 LLVMMemoryBufferRef
*OutMemBuf
,
3656 char **OutMessage
) {
3658 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MBOrErr
= MemoryBuffer::getFile(Path
);
3659 if (std::error_code EC
= MBOrErr
.getError()) {
3660 *OutMessage
= strdup(EC
.message().c_str());
3663 *OutMemBuf
= wrap(MBOrErr
.get().release());
3667 LLVMBool
LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef
*OutMemBuf
,
3668 char **OutMessage
) {
3669 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MBOrErr
= MemoryBuffer::getSTDIN();
3670 if (std::error_code EC
= MBOrErr
.getError()) {
3671 *OutMessage
= strdup(EC
.message().c_str());
3674 *OutMemBuf
= wrap(MBOrErr
.get().release());
3678 LLVMMemoryBufferRef
LLVMCreateMemoryBufferWithMemoryRange(
3679 const char *InputData
,
3680 size_t InputDataLength
,
3681 const char *BufferName
,
3682 LLVMBool RequiresNullTerminator
) {
3684 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData
, InputDataLength
),
3685 StringRef(BufferName
),
3686 RequiresNullTerminator
).release());
3689 LLVMMemoryBufferRef
LLVMCreateMemoryBufferWithMemoryRangeCopy(
3690 const char *InputData
,
3691 size_t InputDataLength
,
3692 const char *BufferName
) {
3695 MemoryBuffer::getMemBufferCopy(StringRef(InputData
, InputDataLength
),
3696 StringRef(BufferName
)).release());
3699 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf
) {
3700 return unwrap(MemBuf
)->getBufferStart();
3703 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf
) {
3704 return unwrap(MemBuf
)->getBufferSize();
3707 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf
) {
3708 delete unwrap(MemBuf
);
3711 /*===-- Pass Registry -----------------------------------------------------===*/
3713 LLVMPassRegistryRef
LLVMGetGlobalPassRegistry(void) {
3714 return wrap(PassRegistry::getPassRegistry());
3717 /*===-- Pass Manager ------------------------------------------------------===*/
3719 LLVMPassManagerRef
LLVMCreatePassManager() {
3720 return wrap(new legacy::PassManager());
3723 LLVMPassManagerRef
LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M
) {
3724 return wrap(new legacy::FunctionPassManager(unwrap(M
)));
3727 LLVMPassManagerRef
LLVMCreateFunctionPassManager(LLVMModuleProviderRef P
) {
3728 return LLVMCreateFunctionPassManagerForModule(
3729 reinterpret_cast<LLVMModuleRef
>(P
));
3732 LLVMBool
LLVMRunPassManager(LLVMPassManagerRef PM
, LLVMModuleRef M
) {
3733 return unwrap
<legacy::PassManager
>(PM
)->run(*unwrap(M
));
3736 LLVMBool
LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM
) {
3737 return unwrap
<legacy::FunctionPassManager
>(FPM
)->doInitialization();
3740 LLVMBool
LLVMRunFunctionPassManager(LLVMPassManagerRef FPM
, LLVMValueRef F
) {
3741 return unwrap
<legacy::FunctionPassManager
>(FPM
)->run(*unwrap
<Function
>(F
));
3744 LLVMBool
LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM
) {
3745 return unwrap
<legacy::FunctionPassManager
>(FPM
)->doFinalization();
3748 void LLVMDisposePassManager(LLVMPassManagerRef PM
) {
3752 /*===-- Threading ------------------------------------------------------===*/
3754 LLVMBool
LLVMStartMultithreaded() {
3755 return LLVMIsMultithreaded();
3758 void LLVMStopMultithreaded() {
3761 LLVMBool
LLVMIsMultithreaded() {
3762 return llvm_is_multithreaded();