xref: /llvm-project-15.0.7/llvm/lib/IR/Core.cpp (revision 4bb7b6fa)
1ef860a24SChandler Carruth //===-- Core.cpp ----------------------------------------------------------===//
2ef860a24SChandler Carruth //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ef860a24SChandler Carruth //
7ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
8ef860a24SChandler Carruth //
9ef860a24SChandler Carruth // This file implements the common infrastructure (including the C bindings)
10ef860a24SChandler Carruth // for libLLVMCore.a, which implements the LLVM intermediate representation.
11ef860a24SChandler Carruth //
12ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
13ef860a24SChandler Carruth 
14ef860a24SChandler Carruth #include "llvm-c/Core.h"
159fb823bbSChandler Carruth #include "llvm/IR/Attributes.h"
16e188aae4Sserge-sans-paille #include "llvm/IR/BasicBlock.h"
179fb823bbSChandler Carruth #include "llvm/IR/Constants.h"
180d1cbcc3SSaleem Abdulrasool #include "llvm/IR/DebugInfoMetadata.h"
19a6e9c3e4SRafael Espindola #include "llvm/IR/DerivedTypes.h"
201580dc78STom Stellard #include "llvm/IR/DiagnosticInfo.h"
211580dc78STom Stellard #include "llvm/IR/DiagnosticPrinter.h"
229fb823bbSChandler Carruth #include "llvm/IR/GlobalAlias.h"
239fb823bbSChandler Carruth #include "llvm/IR/GlobalVariable.h"
248a8cd2baSChandler Carruth #include "llvm/IR/IRBuilder.h"
259fb823bbSChandler Carruth #include "llvm/IR/InlineAsm.h"
269fb823bbSChandler Carruth #include "llvm/IR/IntrinsicInst.h"
279fb823bbSChandler Carruth #include "llvm/IR/LLVMContext.h"
2830d69c2eSChandler Carruth #include "llvm/IR/LegacyPassManager.h"
29dec20e43SFilip Pizlo #include "llvm/IR/Module.h"
3005da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
31e188aae4Sserge-sans-paille #include "llvm/PassRegistry.h"
32ef860a24SChandler Carruth #include "llvm/Support/Debug.h"
33ef860a24SChandler Carruth #include "llvm/Support/ErrorHandling.h"
34d59664f4SBenjamin Kramer #include "llvm/Support/FileSystem.h"
351cba0a8eSDuncan Sands #include "llvm/Support/ManagedStatic.h"
36ef860a24SChandler Carruth #include "llvm/Support/MemoryBuffer.h"
378a8cd2baSChandler Carruth #include "llvm/Support/Threading.h"
38ef860a24SChandler Carruth #include "llvm/Support/raw_ostream.h"
39ef860a24SChandler Carruth #include <cassert>
40ef860a24SChandler Carruth #include <cstdlib>
41ef860a24SChandler Carruth #include <cstring>
42a6e9c3e4SRafael Espindola #include <system_error>
43ef860a24SChandler Carruth 
44ef860a24SChandler Carruth using namespace llvm;
45ef860a24SChandler Carruth 
46e96dd897SChandler Carruth #define DEBUG_TYPE "ir"
47e96dd897SChandler Carruth 
initializeCore(PassRegistry & Registry)48ef860a24SChandler Carruth void llvm::initializeCore(PassRegistry &Registry) {
4973523021SChandler Carruth   initializeDominatorTreeWrapperPassPass(Registry);
5052eef887SChandler Carruth   initializePrintModulePassWrapperPass(Registry);
5152eef887SChandler Carruth   initializePrintFunctionPassWrapperPass(Registry);
52740f529dSAnna Thomas   initializeSafepointIRVerifierPass(Registry);
534d35631aSChandler Carruth   initializeVerifierLegacyPassPass(Registry);
54ef860a24SChandler Carruth }
55ef860a24SChandler Carruth 
LLVMInitializeCore(LLVMPassRegistryRef R)56ef860a24SChandler Carruth void LLVMInitializeCore(LLVMPassRegistryRef R) {
57ef860a24SChandler Carruth   initializeCore(*unwrap(R));
58ef860a24SChandler Carruth }
59ef860a24SChandler Carruth 
LLVMShutdown()601cba0a8eSDuncan Sands void LLVMShutdown() {
611cba0a8eSDuncan Sands   llvm_shutdown();
621cba0a8eSDuncan Sands }
631cba0a8eSDuncan Sands 
64ef860a24SChandler Carruth /*===-- Error handling ----------------------------------------------------===*/
65ef860a24SChandler Carruth 
LLVMCreateMessage(const char * Message)663fdbaff3SFilip Pizlo char *LLVMCreateMessage(const char *Message) {
673fdbaff3SFilip Pizlo   return strdup(Message);
683fdbaff3SFilip Pizlo }
693fdbaff3SFilip Pizlo 
LLVMDisposeMessage(char * Message)70ef860a24SChandler Carruth void LLVMDisposeMessage(char *Message) {
71ef860a24SChandler Carruth   free(Message);
72ef860a24SChandler Carruth }
73ef860a24SChandler Carruth 
74ef860a24SChandler Carruth 
75ef860a24SChandler Carruth /*===-- Operations on contexts --------------------------------------------===*/
76ef860a24SChandler Carruth 
getGlobalContext()77*ede60037SNicolai Hähnle static LLVMContext &getGlobalContext() {
78*ede60037SNicolai Hähnle   static LLVMContext GlobalContext;
79*ede60037SNicolai Hähnle   return GlobalContext;
80*ede60037SNicolai Hähnle }
81dc4c095dSMehdi Amini 
LLVMContextCreate()82ef860a24SChandler Carruth LLVMContextRef LLVMContextCreate() {
83ef860a24SChandler Carruth   return wrap(new LLVMContext());
84ef860a24SChandler Carruth }
85ef860a24SChandler Carruth 
LLVMGetGlobalContext()86*ede60037SNicolai Hähnle LLVMContextRef LLVMGetGlobalContext() { return wrap(&getGlobalContext()); }
87ef860a24SChandler Carruth 
LLVMContextSetDiagnosticHandler(LLVMContextRef C,LLVMDiagnosticHandler Handler,void * DiagnosticContext)881580dc78STom Stellard void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
891580dc78STom Stellard                                      LLVMDiagnosticHandler Handler,
901580dc78STom Stellard                                      void *DiagnosticContext) {
91b5ab895eSVivek Pandya   unwrap(C)->setDiagnosticHandlerCallBack(
92b5ab895eSVivek Pandya       LLVM_EXTENSION reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy>(
93ad659c34SJeroen Ketema           Handler),
941580dc78STom Stellard       DiagnosticContext);
951580dc78STom Stellard }
961580dc78STom Stellard 
LLVMContextGetDiagnosticHandler(LLVMContextRef C)97ad659c34SJeroen Ketema LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) {
98ad659c34SJeroen Ketema   return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
99b5ab895eSVivek Pandya       unwrap(C)->getDiagnosticHandlerCallBack());
100ad659c34SJeroen Ketema }
101ad659c34SJeroen Ketema 
LLVMContextGetDiagnosticContext(LLVMContextRef C)102ad659c34SJeroen Ketema void *LLVMContextGetDiagnosticContext(LLVMContextRef C) {
103ad659c34SJeroen Ketema   return unwrap(C)->getDiagnosticContext();
104ad659c34SJeroen Ketema }
105ad659c34SJeroen Ketema 
LLVMContextSetYieldCallback(LLVMContextRef C,LLVMYieldCallback Callback,void * OpaqueHandle)10634390c70SJuergen Ributzka void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
10734390c70SJuergen Ributzka                                  void *OpaqueHandle) {
10834390c70SJuergen Ributzka   auto YieldCallback =
10934390c70SJuergen Ributzka     LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
11034390c70SJuergen Ributzka   unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
11134390c70SJuergen Ributzka }
11234390c70SJuergen Ributzka 
LLVMContextShouldDiscardValueNames(LLVMContextRef C)113db5b537fSRobert Widmann LLVMBool LLVMContextShouldDiscardValueNames(LLVMContextRef C) {
1145d1dfa3eSRobert Widmann   return unwrap(C)->shouldDiscardValueNames();
1155d1dfa3eSRobert Widmann }
1165d1dfa3eSRobert Widmann 
LLVMContextSetDiscardValueNames(LLVMContextRef C,LLVMBool Discard)117db5b537fSRobert Widmann void LLVMContextSetDiscardValueNames(LLVMContextRef C, LLVMBool Discard) {
1185d1dfa3eSRobert Widmann   unwrap(C)->setDiscardValueNames(Discard);
1195d1dfa3eSRobert Widmann }
1205d1dfa3eSRobert Widmann 
LLVMContextSetOpaquePointers(LLVMContextRef C,LLVMBool OpaquePointers)121436bbce7SNicolas Abram Lujan void LLVMContextSetOpaquePointers(LLVMContextRef C, LLVMBool OpaquePointers) {
122436bbce7SNicolas Abram Lujan   unwrap(C)->setOpaquePointers(OpaquePointers);
123436bbce7SNicolas Abram Lujan }
124436bbce7SNicolas Abram Lujan 
LLVMContextDispose(LLVMContextRef C)125ef860a24SChandler Carruth void LLVMContextDispose(LLVMContextRef C) {
126ef860a24SChandler Carruth   delete unwrap(C);
127ef860a24SChandler Carruth }
128ef860a24SChandler Carruth 
LLVMGetMDKindIDInContext(LLVMContextRef C,const char * Name,unsigned SLen)129ef860a24SChandler Carruth unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,
130ef860a24SChandler Carruth                                   unsigned SLen) {
131ef860a24SChandler Carruth   return unwrap(C)->getMDKindID(StringRef(Name, SLen));
132ef860a24SChandler Carruth }
133ef860a24SChandler Carruth 
LLVMGetMDKindID(const char * Name,unsigned SLen)134ef860a24SChandler Carruth unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
135ef860a24SChandler Carruth   return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
136ef860a24SChandler Carruth }
137ef860a24SChandler Carruth 
LLVMGetEnumAttributeKindForName(const char * Name,size_t SLen)1385db224e1SAmaury Sechet unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
139a7bbe45aSTyker   return Attribute::getAttrKindFromName(StringRef(Name, SLen));
14060b31453SAmaury Sechet }
14160b31453SAmaury Sechet 
LLVMGetLastEnumAttributeKind(void)14248b0665bSAmaury Sechet unsigned LLVMGetLastEnumAttributeKind(void) {
1435db224e1SAmaury Sechet   return Attribute::AttrKind::EndAttrKinds;
1445db224e1SAmaury Sechet }
1455db224e1SAmaury Sechet 
LLVMCreateEnumAttribute(LLVMContextRef C,unsigned KindID,uint64_t Val)1465db224e1SAmaury Sechet LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID,
1475db224e1SAmaury Sechet                                          uint64_t Val) {
1480af82068SHans Wennborg   auto &Ctx = *unwrap(C);
1490af82068SHans Wennborg   auto AttrKind = (Attribute::AttrKind)KindID;
1500af82068SHans Wennborg 
1510af82068SHans Wennborg   if (AttrKind == Attribute::AttrKind::ByVal) {
1520af82068SHans Wennborg     // After r362128, byval attributes need to have a type attribute. Provide a
1530af82068SHans Wennborg     // NULL one until a proper API is added for this.
1545a667c0eSKazu Hirata     return wrap(Attribute::getWithByValType(Ctx, nullptr));
1550af82068SHans Wennborg   }
156667e1f71STim Northover 
1570a7cd99aSMatt Arsenault   if (AttrKind == Attribute::AttrKind::StructRet) {
1580a7cd99aSMatt Arsenault     // Same as byval.
1595a667c0eSKazu Hirata     return wrap(Attribute::getWithStructRetType(Ctx, nullptr));
1600a7cd99aSMatt Arsenault   }
1610a7cd99aSMatt Arsenault 
162667e1f71STim Northover   return wrap(Attribute::get(Ctx, AttrKind, Val));
1635db224e1SAmaury Sechet }
1645db224e1SAmaury Sechet 
LLVMGetEnumAttributeKind(LLVMAttributeRef A)1655db224e1SAmaury Sechet unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) {
1665db224e1SAmaury Sechet   return unwrap(A).getKindAsEnum();
1675db224e1SAmaury Sechet }
1685db224e1SAmaury Sechet 
LLVMGetEnumAttributeValue(LLVMAttributeRef A)1695db224e1SAmaury Sechet uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) {
1705db224e1SAmaury Sechet   auto Attr = unwrap(A);
1715db224e1SAmaury Sechet   if (Attr.isEnumAttribute())
1725db224e1SAmaury Sechet     return 0;
1735db224e1SAmaury Sechet   return Attr.getValueAsInt();
1745db224e1SAmaury Sechet }
1755db224e1SAmaury Sechet 
LLVMCreateTypeAttribute(LLVMContextRef C,unsigned KindID,LLVMTypeRef type_ref)176528f6f7dSChristoffer Lernö LLVMAttributeRef LLVMCreateTypeAttribute(LLVMContextRef C, unsigned KindID,
177528f6f7dSChristoffer Lernö                                          LLVMTypeRef type_ref) {
178528f6f7dSChristoffer Lernö   auto &Ctx = *unwrap(C);
179528f6f7dSChristoffer Lernö   auto AttrKind = (Attribute::AttrKind)KindID;
180528f6f7dSChristoffer Lernö   return wrap(Attribute::get(Ctx, AttrKind, unwrap(type_ref)));
181528f6f7dSChristoffer Lernö }
182528f6f7dSChristoffer Lernö 
LLVMGetTypeAttributeValue(LLVMAttributeRef A)183528f6f7dSChristoffer Lernö LLVMTypeRef LLVMGetTypeAttributeValue(LLVMAttributeRef A) {
184528f6f7dSChristoffer Lernö   auto Attr = unwrap(A);
185528f6f7dSChristoffer Lernö   return wrap(Attr.getValueAsType());
186528f6f7dSChristoffer Lernö }
187528f6f7dSChristoffer Lernö 
LLVMCreateStringAttribute(LLVMContextRef C,const char * K,unsigned KLength,const char * V,unsigned VLength)1885db224e1SAmaury Sechet LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
1895db224e1SAmaury Sechet                                            const char *K, unsigned KLength,
1905db224e1SAmaury Sechet                                            const char *V, unsigned VLength) {
1915db224e1SAmaury Sechet   return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
1925db224e1SAmaury Sechet                              StringRef(V, VLength)));
1935db224e1SAmaury Sechet }
1945db224e1SAmaury Sechet 
LLVMGetStringAttributeKind(LLVMAttributeRef A,unsigned * Length)1955db224e1SAmaury Sechet const char *LLVMGetStringAttributeKind(LLVMAttributeRef A,
1965db224e1SAmaury Sechet                                        unsigned *Length) {
1975db224e1SAmaury Sechet   auto S = unwrap(A).getKindAsString();
1985db224e1SAmaury Sechet   *Length = S.size();
1995db224e1SAmaury Sechet   return S.data();
2005db224e1SAmaury Sechet }
2015db224e1SAmaury Sechet 
LLVMGetStringAttributeValue(LLVMAttributeRef A,unsigned * Length)2025db224e1SAmaury Sechet const char *LLVMGetStringAttributeValue(LLVMAttributeRef A,
2035db224e1SAmaury Sechet                                         unsigned *Length) {
2045db224e1SAmaury Sechet   auto S = unwrap(A).getValueAsString();
2055db224e1SAmaury Sechet   *Length = S.size();
2065db224e1SAmaury Sechet   return S.data();
2075db224e1SAmaury Sechet }
2085db224e1SAmaury Sechet 
LLVMIsEnumAttribute(LLVMAttributeRef A)2095db224e1SAmaury Sechet LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) {
2105db224e1SAmaury Sechet   auto Attr = unwrap(A);
2115db224e1SAmaury Sechet   return Attr.isEnumAttribute() || Attr.isIntAttribute();
2125db224e1SAmaury Sechet }
2135db224e1SAmaury Sechet 
LLVMIsStringAttribute(LLVMAttributeRef A)2145db224e1SAmaury Sechet LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) {
2155db224e1SAmaury Sechet   return unwrap(A).isStringAttribute();
2165db224e1SAmaury Sechet }
2175db224e1SAmaury Sechet 
LLVMIsTypeAttribute(LLVMAttributeRef A)218528f6f7dSChristoffer Lernö LLVMBool LLVMIsTypeAttribute(LLVMAttributeRef A) {
219528f6f7dSChristoffer Lernö   return unwrap(A).isTypeAttribute();
220528f6f7dSChristoffer Lernö }
221528f6f7dSChristoffer Lernö 
LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI)2221580dc78STom Stellard char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
223e69170a1SAlp Toker   std::string MsgStorage;
224e69170a1SAlp Toker   raw_string_ostream Stream(MsgStorage);
225e69170a1SAlp Toker   DiagnosticPrinterRawOStream DP(Stream);
226e69170a1SAlp Toker 
2271580dc78STom Stellard   unwrap(DI)->print(DP);
228e69170a1SAlp Toker   Stream.flush();
229e69170a1SAlp Toker 
230e69170a1SAlp Toker   return LLVMCreateMessage(MsgStorage.c_str());
2311580dc78STom Stellard }
2321580dc78STom Stellard 
LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI)2331580dc78STom Stellard LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {
2341580dc78STom Stellard     LLVMDiagnosticSeverity severity;
2351580dc78STom Stellard 
2361580dc78STom Stellard     switch(unwrap(DI)->getSeverity()) {
2371580dc78STom Stellard     default:
2381580dc78STom Stellard       severity = LLVMDSError;
2391580dc78STom Stellard       break;
2401580dc78STom Stellard     case DS_Warning:
2411580dc78STom Stellard       severity = LLVMDSWarning;
2421580dc78STom Stellard       break;
2431580dc78STom Stellard     case DS_Remark:
2441580dc78STom Stellard       severity = LLVMDSRemark;
2451580dc78STom Stellard       break;
2461580dc78STom Stellard     case DS_Note:
2471580dc78STom Stellard       severity = LLVMDSNote;
2481580dc78STom Stellard       break;
2491580dc78STom Stellard     }
2501580dc78STom Stellard 
2511580dc78STom Stellard     return severity;
2521580dc78STom Stellard }
2531580dc78STom Stellard 
254ef860a24SChandler Carruth /*===-- Operations on modules ---------------------------------------------===*/
255ef860a24SChandler Carruth 
LLVMModuleCreateWithName(const char * ModuleID)256ef860a24SChandler Carruth LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
257*ede60037SNicolai Hähnle   return wrap(new Module(ModuleID, getGlobalContext()));
258ef860a24SChandler Carruth }
259ef860a24SChandler Carruth 
LLVMModuleCreateWithNameInContext(const char * ModuleID,LLVMContextRef C)260ef860a24SChandler Carruth LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
261ef860a24SChandler Carruth                                                 LLVMContextRef C) {
262ef860a24SChandler Carruth   return wrap(new Module(ModuleID, *unwrap(C)));
263ef860a24SChandler Carruth }
264ef860a24SChandler Carruth 
LLVMDisposeModule(LLVMModuleRef M)265ef860a24SChandler Carruth void LLVMDisposeModule(LLVMModuleRef M) {
266ef860a24SChandler Carruth   delete unwrap(M);
267ef860a24SChandler Carruth }
268ef860a24SChandler Carruth 
LLVMGetModuleIdentifier(LLVMModuleRef M,size_t * Len)2690a2fa0a1SPeter Zotov const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
2700a2fa0a1SPeter Zotov   auto &Str = unwrap(M)->getModuleIdentifier();
2710a2fa0a1SPeter Zotov   *Len = Str.length();
2720a2fa0a1SPeter Zotov   return Str.c_str();
2730a2fa0a1SPeter Zotov }
2740a2fa0a1SPeter Zotov 
LLVMSetModuleIdentifier(LLVMModuleRef M,const char * Ident,size_t Len)2750a2fa0a1SPeter Zotov void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
2760a2fa0a1SPeter Zotov   unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
2770a2fa0a1SPeter Zotov }
2780a2fa0a1SPeter Zotov 
LLVMGetSourceFileName(LLVMModuleRef M,size_t * Len)279490a5808SRobert Widmann const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
280490a5808SRobert Widmann   auto &Str = unwrap(M)->getSourceFileName();
281490a5808SRobert Widmann   *Len = Str.length();
282490a5808SRobert Widmann   return Str.c_str();
283490a5808SRobert Widmann }
284490a5808SRobert Widmann 
LLVMSetSourceFileName(LLVMModuleRef M,const char * Name,size_t Len)285490a5808SRobert Widmann void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
286490a5808SRobert Widmann   unwrap(M)->setSourceFileName(StringRef(Name, Len));
287490a5808SRobert Widmann }
2880a2fa0a1SPeter Zotov 
289ef860a24SChandler Carruth /*--.. Data layout .........................................................--*/
LLVMGetDataLayoutStr(LLVMModuleRef M)290f3549c4aSAmaury Sechet const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
291f863ee29SRafael Espindola   return unwrap(M)->getDataLayoutStr().c_str();
292ef860a24SChandler Carruth }
293ef860a24SChandler Carruth 
LLVMGetDataLayout(LLVMModuleRef M)294f3549c4aSAmaury Sechet const char *LLVMGetDataLayout(LLVMModuleRef M) {
295f3549c4aSAmaury Sechet   return LLVMGetDataLayoutStr(M);
296f3549c4aSAmaury Sechet }
297f3549c4aSAmaury Sechet 
LLVMSetDataLayout(LLVMModuleRef M,const char * DataLayoutStr)2986ada31c2SAmaury Sechet void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
2996ada31c2SAmaury Sechet   unwrap(M)->setDataLayout(DataLayoutStr);
300ef860a24SChandler Carruth }
301ef860a24SChandler Carruth 
302ef860a24SChandler Carruth /*--.. Target triple .......................................................--*/
LLVMGetTarget(LLVMModuleRef M)303ef860a24SChandler Carruth const char * LLVMGetTarget(LLVMModuleRef M) {
304ef860a24SChandler Carruth   return unwrap(M)->getTargetTriple().c_str();
305ef860a24SChandler Carruth }
306ef860a24SChandler Carruth 
LLVMSetTarget(LLVMModuleRef M,const char * Triple)307ef860a24SChandler Carruth void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
308ef860a24SChandler Carruth   unwrap(M)->setTargetTriple(Triple);
309ef860a24SChandler Carruth }
310ef860a24SChandler Carruth 
311bce36770SRobert Widmann /*--.. Module flags ........................................................--*/
312bce36770SRobert Widmann struct LLVMOpaqueModuleFlagEntry {
313bce36770SRobert Widmann   LLVMModuleFlagBehavior Behavior;
314bce36770SRobert Widmann   const char *Key;
315bce36770SRobert Widmann   size_t KeyLen;
316bce36770SRobert Widmann   LLVMMetadataRef Metadata;
317bce36770SRobert Widmann };
318bce36770SRobert Widmann 
319bce36770SRobert Widmann static Module::ModFlagBehavior
map_to_llvmModFlagBehavior(LLVMModuleFlagBehavior Behavior)320bce36770SRobert Widmann map_to_llvmModFlagBehavior(LLVMModuleFlagBehavior Behavior) {
321bce36770SRobert Widmann   switch (Behavior) {
322bce36770SRobert Widmann   case LLVMModuleFlagBehaviorError:
323bce36770SRobert Widmann     return Module::ModFlagBehavior::Error;
324bce36770SRobert Widmann   case LLVMModuleFlagBehaviorWarning:
325bce36770SRobert Widmann     return Module::ModFlagBehavior::Warning;
326bce36770SRobert Widmann   case LLVMModuleFlagBehaviorRequire:
327bce36770SRobert Widmann     return Module::ModFlagBehavior::Require;
328bce36770SRobert Widmann   case LLVMModuleFlagBehaviorOverride:
329bce36770SRobert Widmann     return Module::ModFlagBehavior::Override;
330bce36770SRobert Widmann   case LLVMModuleFlagBehaviorAppend:
331bce36770SRobert Widmann     return Module::ModFlagBehavior::Append;
332bce36770SRobert Widmann   case LLVMModuleFlagBehaviorAppendUnique:
333bce36770SRobert Widmann     return Module::ModFlagBehavior::AppendUnique;
334bce36770SRobert Widmann   }
335e91a631dSSimon Pilgrim   llvm_unreachable("Unknown LLVMModuleFlagBehavior");
336bce36770SRobert Widmann }
337bce36770SRobert Widmann 
338bce36770SRobert Widmann static LLVMModuleFlagBehavior
map_from_llvmModFlagBehavior(Module::ModFlagBehavior Behavior)339bce36770SRobert Widmann map_from_llvmModFlagBehavior(Module::ModFlagBehavior Behavior) {
340bce36770SRobert Widmann   switch (Behavior) {
341bce36770SRobert Widmann   case Module::ModFlagBehavior::Error:
342bce36770SRobert Widmann     return LLVMModuleFlagBehaviorError;
343bce36770SRobert Widmann   case Module::ModFlagBehavior::Warning:
344bce36770SRobert Widmann     return LLVMModuleFlagBehaviorWarning;
345bce36770SRobert Widmann   case Module::ModFlagBehavior::Require:
346bce36770SRobert Widmann     return LLVMModuleFlagBehaviorRequire;
347bce36770SRobert Widmann   case Module::ModFlagBehavior::Override:
348bce36770SRobert Widmann     return LLVMModuleFlagBehaviorOverride;
349bce36770SRobert Widmann   case Module::ModFlagBehavior::Append:
350bce36770SRobert Widmann     return LLVMModuleFlagBehaviorAppend;
351bce36770SRobert Widmann   case Module::ModFlagBehavior::AppendUnique:
352bce36770SRobert Widmann     return LLVMModuleFlagBehaviorAppendUnique;
353bce36770SRobert Widmann   default:
354bce36770SRobert Widmann     llvm_unreachable("Unhandled Flag Behavior");
355bce36770SRobert Widmann   }
356bce36770SRobert Widmann }
357bce36770SRobert Widmann 
LLVMCopyModuleFlagsMetadata(LLVMModuleRef M,size_t * Len)358bce36770SRobert Widmann LLVMModuleFlagEntry *LLVMCopyModuleFlagsMetadata(LLVMModuleRef M, size_t *Len) {
359bce36770SRobert Widmann   SmallVector<Module::ModuleFlagEntry, 8> MFEs;
360bce36770SRobert Widmann   unwrap(M)->getModuleFlagsMetadata(MFEs);
361bce36770SRobert Widmann 
362bce36770SRobert Widmann   LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>(
363bce36770SRobert Widmann       safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
364bce36770SRobert Widmann   for (unsigned i = 0; i < MFEs.size(); ++i) {
365bce36770SRobert Widmann     const auto &ModuleFlag = MFEs[i];
366bce36770SRobert Widmann     Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
367bce36770SRobert Widmann     Result[i].Key = ModuleFlag.Key->getString().data();
368bce36770SRobert Widmann     Result[i].KeyLen = ModuleFlag.Key->getString().size();
369bce36770SRobert Widmann     Result[i].Metadata = wrap(ModuleFlag.Val);
370bce36770SRobert Widmann   }
371bce36770SRobert Widmann   *Len = MFEs.size();
372bce36770SRobert Widmann   return Result;
373bce36770SRobert Widmann }
374bce36770SRobert Widmann 
LLVMDisposeModuleFlagsMetadata(LLVMModuleFlagEntry * Entries)375bce36770SRobert Widmann void LLVMDisposeModuleFlagsMetadata(LLVMModuleFlagEntry *Entries) {
376bce36770SRobert Widmann   free(Entries);
377bce36770SRobert Widmann }
378bce36770SRobert Widmann 
379bce36770SRobert Widmann LLVMModuleFlagBehavior
LLVMModuleFlagEntriesGetFlagBehavior(LLVMModuleFlagEntry * Entries,unsigned Index)380bce36770SRobert Widmann LLVMModuleFlagEntriesGetFlagBehavior(LLVMModuleFlagEntry *Entries,
381bce36770SRobert Widmann                                      unsigned Index) {
382bce36770SRobert Widmann   LLVMOpaqueModuleFlagEntry MFE =
383bce36770SRobert Widmann       static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
384bce36770SRobert Widmann   return MFE.Behavior;
385bce36770SRobert Widmann }
386bce36770SRobert Widmann 
LLVMModuleFlagEntriesGetKey(LLVMModuleFlagEntry * Entries,unsigned Index,size_t * Len)387bce36770SRobert Widmann const char *LLVMModuleFlagEntriesGetKey(LLVMModuleFlagEntry *Entries,
388bce36770SRobert Widmann                                         unsigned Index, size_t *Len) {
389bce36770SRobert Widmann   LLVMOpaqueModuleFlagEntry MFE =
390bce36770SRobert Widmann       static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
391bce36770SRobert Widmann   *Len = MFE.KeyLen;
392bce36770SRobert Widmann   return MFE.Key;
393bce36770SRobert Widmann }
394bce36770SRobert Widmann 
LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry * Entries,unsigned Index)395bce36770SRobert Widmann LLVMMetadataRef LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry *Entries,
396bce36770SRobert Widmann                                                  unsigned Index) {
397bce36770SRobert Widmann   LLVMOpaqueModuleFlagEntry MFE =
398bce36770SRobert Widmann       static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
399bce36770SRobert Widmann   return MFE.Metadata;
400bce36770SRobert Widmann }
401bce36770SRobert Widmann 
LLVMGetModuleFlag(LLVMModuleRef M,const char * Key,size_t KeyLen)402bce36770SRobert Widmann LLVMMetadataRef LLVMGetModuleFlag(LLVMModuleRef M,
403bce36770SRobert Widmann                                   const char *Key, size_t KeyLen) {
404bce36770SRobert Widmann   return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
405bce36770SRobert Widmann }
406bce36770SRobert Widmann 
LLVMAddModuleFlag(LLVMModuleRef M,LLVMModuleFlagBehavior Behavior,const char * Key,size_t KeyLen,LLVMMetadataRef Val)407bce36770SRobert Widmann void LLVMAddModuleFlag(LLVMModuleRef M, LLVMModuleFlagBehavior Behavior,
408bce36770SRobert Widmann                        const char *Key, size_t KeyLen,
409bce36770SRobert Widmann                        LLVMMetadataRef Val) {
410bce36770SRobert Widmann   unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
411bce36770SRobert Widmann                            {Key, KeyLen}, unwrap(Val));
412bce36770SRobert Widmann }
413bce36770SRobert Widmann 
414bce36770SRobert Widmann /*--.. Printing modules ....................................................--*/
415bce36770SRobert Widmann 
LLVMDumpModule(LLVMModuleRef M)416de58b61bSMatthias Braun void LLVMDumpModule(LLVMModuleRef M) {
417de58b61bSMatthias Braun   unwrap(M)->print(errs(), nullptr,
418de58b61bSMatthias Braun                    /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
419ef860a24SChandler Carruth }
420ef860a24SChandler Carruth 
LLVMPrintModuleToFile(LLVMModuleRef M,const char * Filename,char ** ErrorMessage)421ef860a24SChandler Carruth LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
422ef860a24SChandler Carruth                                char **ErrorMessage) {
4233fd1e993SRafael Espindola   std::error_code EC;
42482b3e28eSAbhina Sreeskantharajan   raw_fd_ostream dest(Filename, EC, sys::fs::OF_TextWithCRLF);
4253fd1e993SRafael Espindola   if (EC) {
4263fd1e993SRafael Espindola     *ErrorMessage = strdup(EC.message().c_str());
427ef860a24SChandler Carruth     return true;
428ef860a24SChandler Carruth   }
429ef860a24SChandler Carruth 
430c620761cSCraig Topper   unwrap(M)->print(dest, nullptr);
431ef860a24SChandler Carruth 
4323fd1e993SRafael Espindola   dest.close();
4333fd1e993SRafael Espindola 
4343fd1e993SRafael Espindola   if (dest.has_error()) {
4359ce2d03eSBob Haarman     std::string E = "Error printing to file: " + dest.error().message();
4369ce2d03eSBob Haarman     *ErrorMessage = strdup(E.c_str());
437ef860a24SChandler Carruth     return true;
438ef860a24SChandler Carruth   }
4393fd1e993SRafael Espindola 
440ef860a24SChandler Carruth   return false;
441ef860a24SChandler Carruth }
442ef860a24SChandler Carruth 
LLVMPrintModuleToString(LLVMModuleRef M)44384355db7SAnders Waldenborg char *LLVMPrintModuleToString(LLVMModuleRef M) {
444e69170a1SAlp Toker   std::string buf;
445e69170a1SAlp Toker   raw_string_ostream os(buf);
446e69170a1SAlp Toker 
447c620761cSCraig Topper   unwrap(M)->print(os, nullptr);
448e69170a1SAlp Toker   os.flush();
449e69170a1SAlp Toker 
450e69170a1SAlp Toker   return strdup(buf.c_str());
45184355db7SAnders Waldenborg }
45284355db7SAnders Waldenborg 
453ef860a24SChandler Carruth /*--.. Operations on inline assembler ......................................--*/
LLVMSetModuleInlineAsm2(LLVMModuleRef M,const char * Asm,size_t Len)454f108d57fSRobert Widmann void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
455f108d57fSRobert Widmann   unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
456f108d57fSRobert Widmann }
457f108d57fSRobert Widmann 
LLVMSetModuleInlineAsm(LLVMModuleRef M,const char * Asm)458ef860a24SChandler Carruth void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
459ef860a24SChandler Carruth   unwrap(M)->setModuleInlineAsm(StringRef(Asm));
460ef860a24SChandler Carruth }
461ef860a24SChandler Carruth 
LLVMAppendModuleInlineAsm(LLVMModuleRef M,const char * Asm,size_t Len)462f108d57fSRobert Widmann void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
463f108d57fSRobert Widmann   unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
464f108d57fSRobert Widmann }
465f108d57fSRobert Widmann 
LLVMGetModuleInlineAsm(LLVMModuleRef M,size_t * Len)466f108d57fSRobert Widmann const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
467f108d57fSRobert Widmann   auto &Str = unwrap(M)->getModuleInlineAsm();
468f108d57fSRobert Widmann   *Len = Str.length();
469f108d57fSRobert Widmann   return Str.c_str();
470f108d57fSRobert Widmann }
471f108d57fSRobert Widmann 
LLVMGetInlineAsm(LLVMTypeRef Ty,char * AsmString,size_t AsmStringSize,char * Constraints,size_t ConstraintsSize,LLVMBool HasSideEffects,LLVMBool IsAlignStack,LLVMInlineAsmDialect Dialect,LLVMBool CanThrow)4728ec9fd48Scynecx LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty, char *AsmString,
4738ec9fd48Scynecx                               size_t AsmStringSize, char *Constraints,
4748ec9fd48Scynecx                               size_t ConstraintsSize, LLVMBool HasSideEffects,
4758ec9fd48Scynecx                               LLVMBool IsAlignStack,
4768ec9fd48Scynecx                               LLVMInlineAsmDialect Dialect, LLVMBool CanThrow) {
477f108d57fSRobert Widmann   InlineAsm::AsmDialect AD;
478f108d57fSRobert Widmann   switch (Dialect) {
479f108d57fSRobert Widmann   case LLVMInlineAsmDialectATT:
480f108d57fSRobert Widmann     AD = InlineAsm::AD_ATT;
48158568254SRobert Widmann     break;
482f108d57fSRobert Widmann   case LLVMInlineAsmDialectIntel:
483f108d57fSRobert Widmann     AD = InlineAsm::AD_Intel;
48458568254SRobert Widmann     break;
485f108d57fSRobert Widmann   }
486f108d57fSRobert Widmann   return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
487f108d57fSRobert Widmann                              StringRef(AsmString, AsmStringSize),
488f108d57fSRobert Widmann                              StringRef(Constraints, ConstraintsSize),
4898ec9fd48Scynecx                              HasSideEffects, IsAlignStack, AD, CanThrow));
490f108d57fSRobert Widmann }
491f108d57fSRobert Widmann 
492ef860a24SChandler Carruth /*--.. Operations on module contexts ......................................--*/
LLVMGetModuleContext(LLVMModuleRef M)493ef860a24SChandler Carruth LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
494ef860a24SChandler Carruth   return wrap(&unwrap(M)->getContext());
495ef860a24SChandler Carruth }
496ef860a24SChandler Carruth 
497ef860a24SChandler Carruth 
498ef860a24SChandler Carruth /*===-- Operations on types -----------------------------------------------===*/
499ef860a24SChandler Carruth 
500ef860a24SChandler Carruth /*--.. Operations on all types (mostly) ....................................--*/
501ef860a24SChandler Carruth 
LLVMGetTypeKind(LLVMTypeRef Ty)502ef860a24SChandler Carruth LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
503ef860a24SChandler Carruth   switch (unwrap(Ty)->getTypeID()) {
504ef860a24SChandler Carruth   case Type::VoidTyID:
505ef860a24SChandler Carruth     return LLVMVoidTypeKind;
506ef860a24SChandler Carruth   case Type::HalfTyID:
507ef860a24SChandler Carruth     return LLVMHalfTypeKind;
5088c24f331STies Stuij   case Type::BFloatTyID:
5098c24f331STies Stuij     return LLVMBFloatTypeKind;
510ef860a24SChandler Carruth   case Type::FloatTyID:
511ef860a24SChandler Carruth     return LLVMFloatTypeKind;
512ef860a24SChandler Carruth   case Type::DoubleTyID:
513ef860a24SChandler Carruth     return LLVMDoubleTypeKind;
514ef860a24SChandler Carruth   case Type::X86_FP80TyID:
515ef860a24SChandler Carruth     return LLVMX86_FP80TypeKind;
516ef860a24SChandler Carruth   case Type::FP128TyID:
517ef860a24SChandler Carruth     return LLVMFP128TypeKind;
518ef860a24SChandler Carruth   case Type::PPC_FP128TyID:
519ef860a24SChandler Carruth     return LLVMPPC_FP128TypeKind;
520ef860a24SChandler Carruth   case Type::LabelTyID:
521ef860a24SChandler Carruth     return LLVMLabelTypeKind;
522ef860a24SChandler Carruth   case Type::MetadataTyID:
523ef860a24SChandler Carruth     return LLVMMetadataTypeKind;
524ef860a24SChandler Carruth   case Type::IntegerTyID:
525ef860a24SChandler Carruth     return LLVMIntegerTypeKind;
526ef860a24SChandler Carruth   case Type::FunctionTyID:
527ef860a24SChandler Carruth     return LLVMFunctionTypeKind;
528ef860a24SChandler Carruth   case Type::StructTyID:
529ef860a24SChandler Carruth     return LLVMStructTypeKind;
530ef860a24SChandler Carruth   case Type::ArrayTyID:
531ef860a24SChandler Carruth     return LLVMArrayTypeKind;
532ef860a24SChandler Carruth   case Type::PointerTyID:
533ef860a24SChandler Carruth     return LLVMPointerTypeKind;
534015e297aSChristopher Tetreault   case Type::FixedVectorTyID:
535015e297aSChristopher Tetreault     return LLVMVectorTypeKind;
536ef860a24SChandler Carruth   case Type::X86_MMXTyID:
537ef860a24SChandler Carruth     return LLVMX86_MMXTypeKind;
538981a0bd8SLuo, Yuanke   case Type::X86_AMXTyID:
539981a0bd8SLuo, Yuanke     return LLVMX86_AMXTypeKind;
540b611e3f5SDavid Majnemer   case Type::TokenTyID:
541b611e3f5SDavid Majnemer     return LLVMTokenTypeKind;
5422dea3f12SChristopher Tetreault   case Type::ScalableVectorTyID:
5432dea3f12SChristopher Tetreault     return LLVMScalableVectorTypeKind;
544e6f44a3cSChris Bieneman   case Type::DXILPointerTyID:
545e6f44a3cSChris Bieneman     llvm_unreachable("DXIL pointers are unsupported via the C API");
546ef860a24SChandler Carruth   }
547ba7df704SRafael Espindola   llvm_unreachable("Unhandled TypeID.");
548ef860a24SChandler Carruth }
549ef860a24SChandler Carruth 
LLVMTypeIsSized(LLVMTypeRef Ty)550ef860a24SChandler Carruth LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
551ef860a24SChandler Carruth {
552ef860a24SChandler Carruth     return unwrap(Ty)->isSized();
553ef860a24SChandler Carruth }
554ef860a24SChandler Carruth 
LLVMGetTypeContext(LLVMTypeRef Ty)555ef860a24SChandler Carruth LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
556ef860a24SChandler Carruth   return wrap(&unwrap(Ty)->getContext());
557ef860a24SChandler Carruth }
558ef860a24SChandler Carruth 
LLVMDumpType(LLVMTypeRef Ty)55931dff533Swhitequark void LLVMDumpType(LLVMTypeRef Ty) {
56031dff533Swhitequark   return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
561b822cffaSAnders Waldenborg }
562b822cffaSAnders Waldenborg 
LLVMPrintTypeToString(LLVMTypeRef Ty)56347b3bd3fSAnders Waldenborg char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
564e69170a1SAlp Toker   std::string buf;
565e69170a1SAlp Toker   raw_string_ostream os(buf);
56647b3bd3fSAnders Waldenborg 
567c1485223SRichard Trieu   if (unwrap(Ty))
56847b3bd3fSAnders Waldenborg     unwrap(Ty)->print(os);
569c1485223SRichard Trieu   else
570c1485223SRichard Trieu     os << "Printing <null> Type";
571c1485223SRichard Trieu 
572e69170a1SAlp Toker   os.flush();
573e69170a1SAlp Toker 
574e69170a1SAlp Toker   return strdup(buf.c_str());
57547b3bd3fSAnders Waldenborg }
57647b3bd3fSAnders Waldenborg 
577ef860a24SChandler Carruth /*--.. Operations on integer types .........................................--*/
578ef860a24SChandler Carruth 
LLVMInt1TypeInContext(LLVMContextRef C)579ef860a24SChandler Carruth LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C)  {
580ef860a24SChandler Carruth   return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
581ef860a24SChandler Carruth }
LLVMInt8TypeInContext(LLVMContextRef C)582ef860a24SChandler Carruth LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C)  {
583ef860a24SChandler Carruth   return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
584ef860a24SChandler Carruth }
LLVMInt16TypeInContext(LLVMContextRef C)585ef860a24SChandler Carruth LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
586ef860a24SChandler Carruth   return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
587ef860a24SChandler Carruth }
LLVMInt32TypeInContext(LLVMContextRef C)588ef860a24SChandler Carruth LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
589ef860a24SChandler Carruth   return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
590ef860a24SChandler Carruth }
LLVMInt64TypeInContext(LLVMContextRef C)591ef860a24SChandler Carruth LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
592ef860a24SChandler Carruth   return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
593ef860a24SChandler Carruth }
LLVMInt128TypeInContext(LLVMContextRef C)59472918025SKit Barton LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
59572918025SKit Barton   return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
59672918025SKit Barton }
LLVMIntTypeInContext(LLVMContextRef C,unsigned NumBits)597ef860a24SChandler Carruth LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
598ef860a24SChandler Carruth   return wrap(IntegerType::get(*unwrap(C), NumBits));
599ef860a24SChandler Carruth }
600ef860a24SChandler Carruth 
LLVMInt1Type(void)601ef860a24SChandler Carruth LLVMTypeRef LLVMInt1Type(void)  {
602ef860a24SChandler Carruth   return LLVMInt1TypeInContext(LLVMGetGlobalContext());
603ef860a24SChandler Carruth }
LLVMInt8Type(void)604ef860a24SChandler Carruth LLVMTypeRef LLVMInt8Type(void)  {
605ef860a24SChandler Carruth   return LLVMInt8TypeInContext(LLVMGetGlobalContext());
606ef860a24SChandler Carruth }
LLVMInt16Type(void)607ef860a24SChandler Carruth LLVMTypeRef LLVMInt16Type(void) {
608ef860a24SChandler Carruth   return LLVMInt16TypeInContext(LLVMGetGlobalContext());
609ef860a24SChandler Carruth }
LLVMInt32Type(void)610ef860a24SChandler Carruth LLVMTypeRef LLVMInt32Type(void) {
611ef860a24SChandler Carruth   return LLVMInt32TypeInContext(LLVMGetGlobalContext());
612ef860a24SChandler Carruth }
LLVMInt64Type(void)613ef860a24SChandler Carruth LLVMTypeRef LLVMInt64Type(void) {
614ef860a24SChandler Carruth   return LLVMInt64TypeInContext(LLVMGetGlobalContext());
615ef860a24SChandler Carruth }
LLVMInt128Type(void)61672918025SKit Barton LLVMTypeRef LLVMInt128Type(void) {
61772918025SKit Barton   return LLVMInt128TypeInContext(LLVMGetGlobalContext());
61872918025SKit Barton }
LLVMIntType(unsigned NumBits)619ef860a24SChandler Carruth LLVMTypeRef LLVMIntType(unsigned NumBits) {
620ef860a24SChandler Carruth   return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
621ef860a24SChandler Carruth }
622ef860a24SChandler Carruth 
LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy)623ef860a24SChandler Carruth unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
624ef860a24SChandler Carruth   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
625ef860a24SChandler Carruth }
626ef860a24SChandler Carruth 
627ef860a24SChandler Carruth /*--.. Operations on real types ............................................--*/
628ef860a24SChandler Carruth 
LLVMHalfTypeInContext(LLVMContextRef C)629ef860a24SChandler Carruth LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
630ef860a24SChandler Carruth   return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
631ef860a24SChandler Carruth }
LLVMBFloatTypeInContext(LLVMContextRef C)6328c24f331STies Stuij LLVMTypeRef LLVMBFloatTypeInContext(LLVMContextRef C) {
6338c24f331STies Stuij   return (LLVMTypeRef) Type::getBFloatTy(*unwrap(C));
6348c24f331STies Stuij }
LLVMFloatTypeInContext(LLVMContextRef C)635ef860a24SChandler Carruth LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
636ef860a24SChandler Carruth   return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
637ef860a24SChandler Carruth }
LLVMDoubleTypeInContext(LLVMContextRef C)638ef860a24SChandler Carruth LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
639ef860a24SChandler Carruth   return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
640ef860a24SChandler Carruth }
LLVMX86FP80TypeInContext(LLVMContextRef C)641ef860a24SChandler Carruth LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
642ef860a24SChandler Carruth   return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
643ef860a24SChandler Carruth }
LLVMFP128TypeInContext(LLVMContextRef C)644ef860a24SChandler Carruth LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
645ef860a24SChandler Carruth   return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
646ef860a24SChandler Carruth }
LLVMPPCFP128TypeInContext(LLVMContextRef C)647ef860a24SChandler Carruth LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
648ef860a24SChandler Carruth   return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
649ef860a24SChandler Carruth }
LLVMX86MMXTypeInContext(LLVMContextRef C)650ef860a24SChandler Carruth LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
651ef860a24SChandler Carruth   return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
652ef860a24SChandler Carruth }
LLVMX86AMXTypeInContext(LLVMContextRef C)653981a0bd8SLuo, Yuanke LLVMTypeRef LLVMX86AMXTypeInContext(LLVMContextRef C) {
654981a0bd8SLuo, Yuanke   return (LLVMTypeRef) Type::getX86_AMXTy(*unwrap(C));
655981a0bd8SLuo, Yuanke }
656ef860a24SChandler Carruth 
LLVMHalfType(void)657ef860a24SChandler Carruth LLVMTypeRef LLVMHalfType(void) {
658ef860a24SChandler Carruth   return LLVMHalfTypeInContext(LLVMGetGlobalContext());
659ef860a24SChandler Carruth }
LLVMBFloatType(void)6608c24f331STies Stuij LLVMTypeRef LLVMBFloatType(void) {
6618c24f331STies Stuij   return LLVMBFloatTypeInContext(LLVMGetGlobalContext());
6628c24f331STies Stuij }
LLVMFloatType(void)663ef860a24SChandler Carruth LLVMTypeRef LLVMFloatType(void) {
664ef860a24SChandler Carruth   return LLVMFloatTypeInContext(LLVMGetGlobalContext());
665ef860a24SChandler Carruth }
LLVMDoubleType(void)666ef860a24SChandler Carruth LLVMTypeRef LLVMDoubleType(void) {
667ef860a24SChandler Carruth   return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
668ef860a24SChandler Carruth }
LLVMX86FP80Type(void)669ef860a24SChandler Carruth LLVMTypeRef LLVMX86FP80Type(void) {
670ef860a24SChandler Carruth   return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
671ef860a24SChandler Carruth }
LLVMFP128Type(void)672ef860a24SChandler Carruth LLVMTypeRef LLVMFP128Type(void) {
673ef860a24SChandler Carruth   return LLVMFP128TypeInContext(LLVMGetGlobalContext());
674ef860a24SChandler Carruth }
LLVMPPCFP128Type(void)675ef860a24SChandler Carruth LLVMTypeRef LLVMPPCFP128Type(void) {
676ef860a24SChandler Carruth   return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
677ef860a24SChandler Carruth }
LLVMX86MMXType(void)678ef860a24SChandler Carruth LLVMTypeRef LLVMX86MMXType(void) {
679ef860a24SChandler Carruth   return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
680ef860a24SChandler Carruth }
LLVMX86AMXType(void)681981a0bd8SLuo, Yuanke LLVMTypeRef LLVMX86AMXType(void) {
682981a0bd8SLuo, Yuanke   return LLVMX86AMXTypeInContext(LLVMGetGlobalContext());
683981a0bd8SLuo, Yuanke }
684ef860a24SChandler Carruth 
685ef860a24SChandler Carruth /*--.. Operations on function types ........................................--*/
686ef860a24SChandler Carruth 
LLVMFunctionType(LLVMTypeRef ReturnType,LLVMTypeRef * ParamTypes,unsigned ParamCount,LLVMBool IsVarArg)687ef860a24SChandler Carruth LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
688ef860a24SChandler Carruth                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
689ef860a24SChandler Carruth                              LLVMBool IsVarArg) {
690ef860a24SChandler Carruth   ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
691ef860a24SChandler Carruth   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
692ef860a24SChandler Carruth }
693ef860a24SChandler Carruth 
LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy)694ef860a24SChandler Carruth LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
695ef860a24SChandler Carruth   return unwrap<FunctionType>(FunctionTy)->isVarArg();
696ef860a24SChandler Carruth }
697ef860a24SChandler Carruth 
LLVMGetReturnType(LLVMTypeRef FunctionTy)698ef860a24SChandler Carruth LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
699ef860a24SChandler Carruth   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
700ef860a24SChandler Carruth }
701ef860a24SChandler Carruth 
LLVMCountParamTypes(LLVMTypeRef FunctionTy)702ef860a24SChandler Carruth unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
703ef860a24SChandler Carruth   return unwrap<FunctionType>(FunctionTy)->getNumParams();
704ef860a24SChandler Carruth }
705ef860a24SChandler Carruth 
LLVMGetParamTypes(LLVMTypeRef FunctionTy,LLVMTypeRef * Dest)706ef860a24SChandler Carruth void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
707ef860a24SChandler Carruth   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
7086a337f85SKazu Hirata   for (Type *T : Ty->params())
7096a337f85SKazu Hirata     *Dest++ = wrap(T);
710ef860a24SChandler Carruth }
711ef860a24SChandler Carruth 
712ef860a24SChandler Carruth /*--.. Operations on struct types ..........................................--*/
713ef860a24SChandler Carruth 
LLVMStructTypeInContext(LLVMContextRef C,LLVMTypeRef * ElementTypes,unsigned ElementCount,LLVMBool Packed)714ef860a24SChandler Carruth LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
715ef860a24SChandler Carruth                            unsigned ElementCount, LLVMBool Packed) {
716ef860a24SChandler Carruth   ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
717ef860a24SChandler Carruth   return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
718ef860a24SChandler Carruth }
719ef860a24SChandler Carruth 
LLVMStructType(LLVMTypeRef * ElementTypes,unsigned ElementCount,LLVMBool Packed)720ef860a24SChandler Carruth LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
721ef860a24SChandler Carruth                            unsigned ElementCount, LLVMBool Packed) {
722ef860a24SChandler Carruth   return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
723ef860a24SChandler Carruth                                  ElementCount, Packed);
724ef860a24SChandler Carruth }
725ef860a24SChandler Carruth 
LLVMStructCreateNamed(LLVMContextRef C,const char * Name)726ef860a24SChandler Carruth LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
727ef860a24SChandler Carruth {
728ef860a24SChandler Carruth   return wrap(StructType::create(*unwrap(C), Name));
729ef860a24SChandler Carruth }
730ef860a24SChandler Carruth 
LLVMGetStructName(LLVMTypeRef Ty)731ef860a24SChandler Carruth const char *LLVMGetStructName(LLVMTypeRef Ty)
732ef860a24SChandler Carruth {
733ef860a24SChandler Carruth   StructType *Type = unwrap<StructType>(Ty);
734ef860a24SChandler Carruth   if (!Type->hasName())
735c620761cSCraig Topper     return nullptr;
736ef860a24SChandler Carruth   return Type->getName().data();
737ef860a24SChandler Carruth }
738ef860a24SChandler Carruth 
LLVMStructSetBody(LLVMTypeRef StructTy,LLVMTypeRef * ElementTypes,unsigned ElementCount,LLVMBool Packed)739ef860a24SChandler Carruth void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
740ef860a24SChandler Carruth                        unsigned ElementCount, LLVMBool Packed) {
741ef860a24SChandler Carruth   ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
742ef860a24SChandler Carruth   unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
743ef860a24SChandler Carruth }
744ef860a24SChandler Carruth 
LLVMCountStructElementTypes(LLVMTypeRef StructTy)745ef860a24SChandler Carruth unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
746ef860a24SChandler Carruth   return unwrap<StructType>(StructTy)->getNumElements();
747ef860a24SChandler Carruth }
748ef860a24SChandler Carruth 
LLVMGetStructElementTypes(LLVMTypeRef StructTy,LLVMTypeRef * Dest)749ef860a24SChandler Carruth void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
750ef860a24SChandler Carruth   StructType *Ty = unwrap<StructType>(StructTy);
7516a337f85SKazu Hirata   for (Type *T : Ty->elements())
7526a337f85SKazu Hirata     *Dest++ = wrap(T);
753ef860a24SChandler Carruth }
754ef860a24SChandler Carruth 
LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy,unsigned i)755c164a3f4SPeter Zotov LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
756c164a3f4SPeter Zotov   StructType *Ty = unwrap<StructType>(StructTy);
757c164a3f4SPeter Zotov   return wrap(Ty->getTypeAtIndex(i));
758c164a3f4SPeter Zotov }
759c164a3f4SPeter Zotov 
LLVMIsPackedStruct(LLVMTypeRef StructTy)760ef860a24SChandler Carruth LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
761ef860a24SChandler Carruth   return unwrap<StructType>(StructTy)->isPacked();
762ef860a24SChandler Carruth }
763ef860a24SChandler Carruth 
LLVMIsOpaqueStruct(LLVMTypeRef StructTy)764ef860a24SChandler Carruth LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
765ef860a24SChandler Carruth   return unwrap<StructType>(StructTy)->isOpaque();
766ef860a24SChandler Carruth }
767ef860a24SChandler Carruth 
LLVMIsLiteralStruct(LLVMTypeRef StructTy)768b486107cSwhitequark LLVMBool LLVMIsLiteralStruct(LLVMTypeRef StructTy) {
769b486107cSwhitequark   return unwrap<StructType>(StructTy)->isLiteral();
770b486107cSwhitequark }
771b486107cSwhitequark 
LLVMGetTypeByName(LLVMModuleRef M,const char * Name)772ef860a24SChandler Carruth LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
773fe431683SNick Lewycky   return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));
774fe431683SNick Lewycky }
775fe431683SNick Lewycky 
LLVMGetTypeByName2(LLVMContextRef C,const char * Name)776fe431683SNick Lewycky LLVMTypeRef LLVMGetTypeByName2(LLVMContextRef C, const char *Name) {
777fe431683SNick Lewycky   return wrap(StructType::getTypeByName(*unwrap(C), Name));
778ef860a24SChandler Carruth }
779ef860a24SChandler Carruth 
780ef860a24SChandler Carruth /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
781ef860a24SChandler Carruth 
LLVMGetSubtypes(LLVMTypeRef Tp,LLVMTypeRef * Arr)782f6059fdcSwhitequark void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
783f6059fdcSwhitequark     int i = 0;
784f6059fdcSwhitequark     for (auto *T : unwrap(Tp)->subtypes()) {
785f6059fdcSwhitequark         Arr[i] = wrap(T);
786f6059fdcSwhitequark         i++;
787f6059fdcSwhitequark     }
788f6059fdcSwhitequark }
789f6059fdcSwhitequark 
LLVMArrayType(LLVMTypeRef ElementType,unsigned ElementCount)790ef860a24SChandler Carruth LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
791ef860a24SChandler Carruth   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
792ef860a24SChandler Carruth }
793ef860a24SChandler Carruth 
LLVMPointerType(LLVMTypeRef ElementType,unsigned AddressSpace)794ef860a24SChandler Carruth LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
795ef860a24SChandler Carruth   return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
796ef860a24SChandler Carruth }
797ef860a24SChandler Carruth 
LLVMPointerTypeIsOpaque(LLVMTypeRef Ty)798436bbce7SNicolas Abram Lujan LLVMBool LLVMPointerTypeIsOpaque(LLVMTypeRef Ty) {
799436bbce7SNicolas Abram Lujan   return unwrap(Ty)->isOpaquePointerTy();
800436bbce7SNicolas Abram Lujan }
801436bbce7SNicolas Abram Lujan 
LLVMVectorType(LLVMTypeRef ElementType,unsigned ElementCount)802ef860a24SChandler Carruth LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
803900f78a7SChristopher Tetreault   return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount));
804ef860a24SChandler Carruth }
805ef860a24SChandler Carruth 
LLVMScalableVectorType(LLVMTypeRef ElementType,unsigned ElementCount)806c3783847SCraig Disselkoen LLVMTypeRef LLVMScalableVectorType(LLVMTypeRef ElementType,
807c3783847SCraig Disselkoen                                    unsigned ElementCount) {
808c3783847SCraig Disselkoen   return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount));
809c3783847SCraig Disselkoen }
810c3783847SCraig Disselkoen 
LLVMGetElementType(LLVMTypeRef WrappedTy)8114568158cSPeter Collingbourne LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
8124568158cSPeter Collingbourne   auto *Ty = unwrap<Type>(WrappedTy);
8134568158cSPeter Collingbourne   if (auto *PTy = dyn_cast<PointerType>(Ty))
814dcc4b94dSNikita Popov     return wrap(PTy->getNonOpaquePointerElementType());
81568b03aeeSEli Friedman   if (auto *ATy = dyn_cast<ArrayType>(Ty))
81668b03aeeSEli Friedman     return wrap(ATy->getElementType());
81768b03aeeSEli Friedman   return wrap(cast<VectorType>(Ty)->getElementType());
818ef860a24SChandler Carruth }
819ef860a24SChandler Carruth 
LLVMGetNumContainedTypes(LLVMTypeRef Tp)820f6059fdcSwhitequark unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
821f6059fdcSwhitequark     return unwrap(Tp)->getNumContainedTypes();
822f6059fdcSwhitequark }
823f6059fdcSwhitequark 
LLVMGetArrayLength(LLVMTypeRef ArrayTy)824ef860a24SChandler Carruth unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
825ef860a24SChandler Carruth   return unwrap<ArrayType>(ArrayTy)->getNumElements();
826ef860a24SChandler Carruth }
827ef860a24SChandler Carruth 
LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy)828ef860a24SChandler Carruth unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
829ef860a24SChandler Carruth   return unwrap<PointerType>(PointerTy)->getAddressSpace();
830ef860a24SChandler Carruth }
831ef860a24SChandler Carruth 
LLVMGetVectorSize(LLVMTypeRef VectorTy)832ef860a24SChandler Carruth unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
833f4257c58SDavid Sherwood   return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();
834ef860a24SChandler Carruth }
835ef860a24SChandler Carruth 
836ef860a24SChandler Carruth /*--.. Operations on other types ...........................................--*/
837ef860a24SChandler Carruth 
LLVMPointerTypeInContext(LLVMContextRef C,unsigned AddressSpace)838436bbce7SNicolas Abram Lujan LLVMTypeRef LLVMPointerTypeInContext(LLVMContextRef C, unsigned AddressSpace) {
839436bbce7SNicolas Abram Lujan   return wrap(PointerType::get(*unwrap(C), AddressSpace));
840436bbce7SNicolas Abram Lujan }
841436bbce7SNicolas Abram Lujan 
LLVMVoidTypeInContext(LLVMContextRef C)842ef860a24SChandler Carruth LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C)  {
843ef860a24SChandler Carruth   return wrap(Type::getVoidTy(*unwrap(C)));
844ef860a24SChandler Carruth }
LLVMLabelTypeInContext(LLVMContextRef C)845ef860a24SChandler Carruth LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
846ef860a24SChandler Carruth   return wrap(Type::getLabelTy(*unwrap(C)));
847ef860a24SChandler Carruth }
LLVMTokenTypeInContext(LLVMContextRef C)848131f98f0Swhitequark LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
849131f98f0Swhitequark   return wrap(Type::getTokenTy(*unwrap(C)));
850131f98f0Swhitequark }
LLVMMetadataTypeInContext(LLVMContextRef C)851131f98f0Swhitequark LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) {
852131f98f0Swhitequark   return wrap(Type::getMetadataTy(*unwrap(C)));
853131f98f0Swhitequark }
854ef860a24SChandler Carruth 
LLVMVoidType(void)855ef860a24SChandler Carruth LLVMTypeRef LLVMVoidType(void)  {
856ef860a24SChandler Carruth   return LLVMVoidTypeInContext(LLVMGetGlobalContext());
857ef860a24SChandler Carruth }
LLVMLabelType(void)858ef860a24SChandler Carruth LLVMTypeRef LLVMLabelType(void) {
859ef860a24SChandler Carruth   return LLVMLabelTypeInContext(LLVMGetGlobalContext());
860ef860a24SChandler Carruth }
861ef860a24SChandler Carruth 
862ef860a24SChandler Carruth /*===-- Operations on values ----------------------------------------------===*/
863ef860a24SChandler Carruth 
864ef860a24SChandler Carruth /*--.. Operations on all values ............................................--*/
865ef860a24SChandler Carruth 
LLVMTypeOf(LLVMValueRef Val)866ef860a24SChandler Carruth LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
867ef860a24SChandler Carruth   return wrap(unwrap(Val)->getType());
868ef860a24SChandler Carruth }
869ef860a24SChandler Carruth 
LLVMGetValueKind(LLVMValueRef Val)8703e4561ceSPeter Zotov LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
8713e4561ceSPeter Zotov     switch(unwrap(Val)->getValueID()) {
872a97f6283SLeonard Chan #define LLVM_C_API 1
8733e4561ceSPeter Zotov #define HANDLE_VALUE(Name) \
8743e4561ceSPeter Zotov   case Value::Name##Val: \
8753e4561ceSPeter Zotov     return LLVM##Name##ValueKind;
8763e4561ceSPeter Zotov #include "llvm/IR/Value.def"
8773e4561ceSPeter Zotov   default:
8783e4561ceSPeter Zotov     return LLVMInstructionValueKind;
8793e4561ceSPeter Zotov   }
8803e4561ceSPeter Zotov }
8813e4561ceSPeter Zotov 
LLVMGetValueName2(LLVMValueRef Val,size_t * Length)882025c78f5SRobert Widmann const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
883025c78f5SRobert Widmann   auto *V = unwrap(Val);
884025c78f5SRobert Widmann   *Length = V->getName().size();
885025c78f5SRobert Widmann   return V->getName().data();
886025c78f5SRobert Widmann }
887025c78f5SRobert Widmann 
LLVMSetValueName2(LLVMValueRef Val,const char * Name,size_t NameLen)888025c78f5SRobert Widmann void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
889025c78f5SRobert Widmann   unwrap(Val)->setName(StringRef(Name, NameLen));
890025c78f5SRobert Widmann }
891025c78f5SRobert Widmann 
LLVMGetValueName(LLVMValueRef Val)892ef860a24SChandler Carruth const char *LLVMGetValueName(LLVMValueRef Val) {
893ef860a24SChandler Carruth   return unwrap(Val)->getName().data();
894ef860a24SChandler Carruth }
895ef860a24SChandler Carruth 
LLVMSetValueName(LLVMValueRef Val,const char * Name)896ef860a24SChandler Carruth void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
897ef860a24SChandler Carruth   unwrap(Val)->setName(Name);
898ef860a24SChandler Carruth }
899ef860a24SChandler Carruth 
LLVMDumpValue(LLVMValueRef Val)90031dff533Swhitequark void LLVMDumpValue(LLVMValueRef Val) {
901a682dfb3SSam McCall   unwrap(Val)->print(errs(), /*IsForDebug=*/true);
902ef860a24SChandler Carruth }
903ef860a24SChandler Carruth 
LLVMPrintValueToString(LLVMValueRef Val)904cd93b370SPeter Zotov char* LLVMPrintValueToString(LLVMValueRef Val) {
905e69170a1SAlp Toker   std::string buf;
906e69170a1SAlp Toker   raw_string_ostream os(buf);
907cd93b370SPeter Zotov 
908c1485223SRichard Trieu   if (unwrap(Val))
909cd93b370SPeter Zotov     unwrap(Val)->print(os);
910c1485223SRichard Trieu   else
911c1485223SRichard Trieu     os << "Printing <null> Value";
912c1485223SRichard Trieu 
913e69170a1SAlp Toker   os.flush();
914e69170a1SAlp Toker 
915e69170a1SAlp Toker   return strdup(buf.c_str());
916cd93b370SPeter Zotov }
917cd93b370SPeter Zotov 
LLVMReplaceAllUsesWith(LLVMValueRef OldVal,LLVMValueRef NewVal)918ef860a24SChandler Carruth void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
919ef860a24SChandler Carruth   unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
920ef860a24SChandler Carruth }
921ef860a24SChandler Carruth 
LLVMHasMetadata(LLVMValueRef Inst)922ef860a24SChandler Carruth int LLVMHasMetadata(LLVMValueRef Inst) {
923ef860a24SChandler Carruth   return unwrap<Instruction>(Inst)->hasMetadata();
924ef860a24SChandler Carruth }
925ef860a24SChandler Carruth 
LLVMGetMetadata(LLVMValueRef Inst,unsigned KindID)926ef860a24SChandler Carruth LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
9275bf8fef5SDuncan P. N. Exon Smith   auto *I = unwrap<Instruction>(Inst);
9285bf8fef5SDuncan P. N. Exon Smith   assert(I && "Expected instruction");
9295bf8fef5SDuncan P. N. Exon Smith   if (auto *MD = I->getMetadata(KindID))
9305bf8fef5SDuncan P. N. Exon Smith     return wrap(MetadataAsValue::get(I->getContext(), MD));
9315bf8fef5SDuncan P. N. Exon Smith   return nullptr;
932ef860a24SChandler Carruth }
933ef860a24SChandler Carruth 
934a09ac008SBjorn Steinbrink // MetadataAsValue uses a canonical format which strips the actual MDNode for
935a09ac008SBjorn Steinbrink // MDNode with just a single constant value, storing just a ConstantAsMetadata
936a09ac008SBjorn Steinbrink // This undoes this canonicalization, reconstructing the MDNode.
extractMDNode(MetadataAsValue * MAV)937a09ac008SBjorn Steinbrink static MDNode *extractMDNode(MetadataAsValue *MAV) {
938a09ac008SBjorn Steinbrink   Metadata *MD = MAV->getMetadata();
939a09ac008SBjorn Steinbrink   assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
940a09ac008SBjorn Steinbrink       "Expected a metadata node or a canonicalized constant");
941a09ac008SBjorn Steinbrink 
942a09ac008SBjorn Steinbrink   if (MDNode *N = dyn_cast<MDNode>(MD))
943a09ac008SBjorn Steinbrink     return N;
944a09ac008SBjorn Steinbrink 
945a09ac008SBjorn Steinbrink   return MDNode::get(MAV->getContext(), MD);
946a09ac008SBjorn Steinbrink }
947a09ac008SBjorn Steinbrink 
LLVMSetMetadata(LLVMValueRef Inst,unsigned KindID,LLVMValueRef Val)948a09ac008SBjorn Steinbrink void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
949a09ac008SBjorn Steinbrink   MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
950a09ac008SBjorn Steinbrink 
9515bf8fef5SDuncan P. N. Exon Smith   unwrap<Instruction>(Inst)->setMetadata(KindID, N);
952ef860a24SChandler Carruth }
953ef860a24SChandler Carruth 
954d22ee946SRobert Widmann struct LLVMOpaqueValueMetadataEntry {
955d22ee946SRobert Widmann   unsigned Kind;
956d22ee946SRobert Widmann   LLVMMetadataRef Metadata;
957d22ee946SRobert Widmann };
958d22ee946SRobert Widmann 
959d22ee946SRobert Widmann using MetadataEntries = SmallVectorImpl<std::pair<unsigned, MDNode *>>;
960d22ee946SRobert Widmann static LLVMValueMetadataEntry *
llvm_getMetadata(size_t * NumEntries,llvm::function_ref<void (MetadataEntries &)> AccessMD)961d22ee946SRobert Widmann llvm_getMetadata(size_t *NumEntries,
962d22ee946SRobert Widmann                  llvm::function_ref<void(MetadataEntries &)> AccessMD) {
963d22ee946SRobert Widmann   SmallVector<std::pair<unsigned, MDNode *>, 8> MVEs;
964d22ee946SRobert Widmann   AccessMD(MVEs);
965d22ee946SRobert Widmann 
966d22ee946SRobert Widmann   LLVMOpaqueValueMetadataEntry *Result =
967d22ee946SRobert Widmann   static_cast<LLVMOpaqueValueMetadataEntry *>(
968d22ee946SRobert Widmann                                               safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry)));
969d22ee946SRobert Widmann   for (unsigned i = 0; i < MVEs.size(); ++i) {
970d22ee946SRobert Widmann     const auto &ModuleFlag = MVEs[i];
971d22ee946SRobert Widmann     Result[i].Kind = ModuleFlag.first;
972d22ee946SRobert Widmann     Result[i].Metadata = wrap(ModuleFlag.second);
973d22ee946SRobert Widmann   }
974d22ee946SRobert Widmann   *NumEntries = MVEs.size();
975d22ee946SRobert Widmann   return Result;
976d22ee946SRobert Widmann }
977d22ee946SRobert Widmann 
9789cba4eceSRobert Widmann LLVMValueMetadataEntry *
LLVMInstructionGetAllMetadataOtherThanDebugLoc(LLVMValueRef Value,size_t * NumEntries)9799cba4eceSRobert Widmann LLVMInstructionGetAllMetadataOtherThanDebugLoc(LLVMValueRef Value,
9809cba4eceSRobert Widmann                                                size_t *NumEntries) {
9819cba4eceSRobert Widmann   return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
9827975b8c3SSerge Pavlov     Entries.clear();
9839cba4eceSRobert Widmann     unwrap<Instruction>(Value)->getAllMetadata(Entries);
9849cba4eceSRobert Widmann   });
9859cba4eceSRobert Widmann }
9869cba4eceSRobert Widmann 
987ef860a24SChandler Carruth /*--.. Conversion functions ................................................--*/
988ef860a24SChandler Carruth 
989ef860a24SChandler Carruth #define LLVM_DEFINE_VALUE_CAST(name)                                       \
990ef860a24SChandler Carruth   LLVMValueRef LLVMIsA##name(LLVMValueRef Val) {                           \
991ef860a24SChandler Carruth     return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
992ef860a24SChandler Carruth   }
993ef860a24SChandler Carruth 
LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)994ef860a24SChandler Carruth LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
995ef860a24SChandler Carruth 
9965bf8fef5SDuncan P. N. Exon Smith LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
9975bf8fef5SDuncan P. N. Exon Smith   if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
9985bf8fef5SDuncan P. N. Exon Smith     if (isa<MDNode>(MD->getMetadata()) ||
9995bf8fef5SDuncan P. N. Exon Smith         isa<ValueAsMetadata>(MD->getMetadata()))
10005bf8fef5SDuncan P. N. Exon Smith       return Val;
10015bf8fef5SDuncan P. N. Exon Smith   return nullptr;
10025bf8fef5SDuncan P. N. Exon Smith }
10035bf8fef5SDuncan P. N. Exon Smith 
LLVMIsAMDString(LLVMValueRef Val)10045bf8fef5SDuncan P. N. Exon Smith LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
10055bf8fef5SDuncan P. N. Exon Smith   if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
10065bf8fef5SDuncan P. N. Exon Smith     if (isa<MDString>(MD->getMetadata()))
10075bf8fef5SDuncan P. N. Exon Smith       return Val;
10085bf8fef5SDuncan P. N. Exon Smith   return nullptr;
10095bf8fef5SDuncan P. N. Exon Smith }
10105bf8fef5SDuncan P. N. Exon Smith 
1011ef860a24SChandler Carruth /*--.. Operations on Uses ..................................................--*/
LLVMGetFirstUse(LLVMValueRef Val)1012ef860a24SChandler Carruth LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
1013ef860a24SChandler Carruth   Value *V = unwrap(Val);
1014ef860a24SChandler Carruth   Value::use_iterator I = V->use_begin();
1015ef860a24SChandler Carruth   if (I == V->use_end())
1016c620761cSCraig Topper     return nullptr;
1017cdf47884SChandler Carruth   return wrap(&*I);
1018ef860a24SChandler Carruth }
1019ef860a24SChandler Carruth 
LLVMGetNextUse(LLVMUseRef U)1020ef860a24SChandler Carruth LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
1021ef860a24SChandler Carruth   Use *Next = unwrap(U)->getNext();
1022ef860a24SChandler Carruth   if (Next)
1023ef860a24SChandler Carruth     return wrap(Next);
1024c620761cSCraig Topper   return nullptr;
1025ef860a24SChandler Carruth }
1026ef860a24SChandler Carruth 
LLVMGetUser(LLVMUseRef U)1027ef860a24SChandler Carruth LLVMValueRef LLVMGetUser(LLVMUseRef U) {
1028ef860a24SChandler Carruth   return wrap(unwrap(U)->getUser());
1029ef860a24SChandler Carruth }
1030ef860a24SChandler Carruth 
LLVMGetUsedValue(LLVMUseRef U)1031ef860a24SChandler Carruth LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
1032ef860a24SChandler Carruth   return wrap(unwrap(U)->get());
1033ef860a24SChandler Carruth }
1034ef860a24SChandler Carruth 
1035ef860a24SChandler Carruth /*--.. Operations on Users .................................................--*/
10365bf8fef5SDuncan P. N. Exon Smith 
getMDNodeOperandImpl(LLVMContext & Context,const MDNode * N,unsigned Index)10375bf8fef5SDuncan P. N. Exon Smith static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
10385bf8fef5SDuncan P. N. Exon Smith                                          unsigned Index) {
10395bf8fef5SDuncan P. N. Exon Smith   Metadata *Op = N->getOperand(Index);
10405bf8fef5SDuncan P. N. Exon Smith   if (!Op)
10415bf8fef5SDuncan P. N. Exon Smith     return nullptr;
10425bf8fef5SDuncan P. N. Exon Smith   if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
10435bf8fef5SDuncan P. N. Exon Smith     return wrap(C->getValue());
10445bf8fef5SDuncan P. N. Exon Smith   return wrap(MetadataAsValue::get(Context, Op));
10455bf8fef5SDuncan P. N. Exon Smith }
10465bf8fef5SDuncan P. N. Exon Smith 
LLVMGetOperand(LLVMValueRef Val,unsigned Index)1047ef860a24SChandler Carruth LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
1048ef860a24SChandler Carruth   Value *V = unwrap(Val);
10495bf8fef5SDuncan P. N. Exon Smith   if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
10505bf8fef5SDuncan P. N. Exon Smith     if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
10515bf8fef5SDuncan P. N. Exon Smith       assert(Index == 0 && "Function-local metadata can only have one operand");
10525bf8fef5SDuncan P. N. Exon Smith       return wrap(L->getValue());
10535bf8fef5SDuncan P. N. Exon Smith     }
10545bf8fef5SDuncan P. N. Exon Smith     return getMDNodeOperandImpl(V->getContext(),
10555bf8fef5SDuncan P. N. Exon Smith                                 cast<MDNode>(MD->getMetadata()), Index);
10565bf8fef5SDuncan P. N. Exon Smith   }
10575bf8fef5SDuncan P. N. Exon Smith 
1058ef860a24SChandler Carruth   return wrap(cast<User>(V)->getOperand(Index));
1059ef860a24SChandler Carruth }
1060ef860a24SChandler Carruth 
LLVMGetOperandUse(LLVMValueRef Val,unsigned Index)1061b19f78f0SPeter Zotov LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
1062b19f78f0SPeter Zotov   Value *V = unwrap(Val);
1063b19f78f0SPeter Zotov   return wrap(&cast<User>(V)->getOperandUse(Index));
1064b19f78f0SPeter Zotov }
1065b19f78f0SPeter Zotov 
LLVMSetOperand(LLVMValueRef Val,unsigned Index,LLVMValueRef Op)1066ef860a24SChandler Carruth void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
1067ef860a24SChandler Carruth   unwrap<User>(Val)->setOperand(Index, unwrap(Op));
1068ef860a24SChandler Carruth }
1069ef860a24SChandler Carruth 
LLVMGetNumOperands(LLVMValueRef Val)1070ef860a24SChandler Carruth int LLVMGetNumOperands(LLVMValueRef Val) {
1071ef860a24SChandler Carruth   Value *V = unwrap(Val);
10725bf8fef5SDuncan P. N. Exon Smith   if (isa<MetadataAsValue>(V))
10735bf8fef5SDuncan P. N. Exon Smith     return LLVMGetMDNodeNumOperands(Val);
10745bf8fef5SDuncan P. N. Exon Smith 
1075ef860a24SChandler Carruth   return cast<User>(V)->getNumOperands();
1076ef860a24SChandler Carruth }
1077ef860a24SChandler Carruth 
1078ef860a24SChandler Carruth /*--.. Operations on constants of any type .................................--*/
1079ef860a24SChandler Carruth 
LLVMConstNull(LLVMTypeRef Ty)1080ef860a24SChandler Carruth LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
1081ef860a24SChandler Carruth   return wrap(Constant::getNullValue(unwrap(Ty)));
1082ef860a24SChandler Carruth }
1083ef860a24SChandler Carruth 
LLVMConstAllOnes(LLVMTypeRef Ty)1084ef860a24SChandler Carruth LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
1085ef860a24SChandler Carruth   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
1086ef860a24SChandler Carruth }
1087ef860a24SChandler Carruth 
LLVMGetUndef(LLVMTypeRef Ty)1088ef860a24SChandler Carruth LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
1089ef860a24SChandler Carruth   return wrap(UndefValue::get(unwrap(Ty)));
1090ef860a24SChandler Carruth }
1091ef860a24SChandler Carruth 
LLVMGetPoison(LLVMTypeRef Ty)109275f50e15SZhengyang Liu LLVMValueRef LLVMGetPoison(LLVMTypeRef Ty) {
109375f50e15SZhengyang Liu   return wrap(PoisonValue::get(unwrap(Ty)));
109475f50e15SZhengyang Liu }
109575f50e15SZhengyang Liu 
LLVMIsConstant(LLVMValueRef Ty)1096ef860a24SChandler Carruth LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
1097ef860a24SChandler Carruth   return isa<Constant>(unwrap(Ty));
1098ef860a24SChandler Carruth }
1099ef860a24SChandler Carruth 
LLVMIsNull(LLVMValueRef Val)1100ef860a24SChandler Carruth LLVMBool LLVMIsNull(LLVMValueRef Val) {
1101ef860a24SChandler Carruth   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
1102ef860a24SChandler Carruth     return C->isNullValue();
1103ef860a24SChandler Carruth   return false;
1104ef860a24SChandler Carruth }
1105ef860a24SChandler Carruth 
LLVMIsUndef(LLVMValueRef Val)1106ef860a24SChandler Carruth LLVMBool LLVMIsUndef(LLVMValueRef Val) {
1107ef860a24SChandler Carruth   return isa<UndefValue>(unwrap(Val));
1108ef860a24SChandler Carruth }
1109ef860a24SChandler Carruth 
LLVMIsPoison(LLVMValueRef Val)111075f50e15SZhengyang Liu LLVMBool LLVMIsPoison(LLVMValueRef Val) {
111175f50e15SZhengyang Liu   return isa<PoisonValue>(unwrap(Val));
111275f50e15SZhengyang Liu }
111375f50e15SZhengyang Liu 
LLVMConstPointerNull(LLVMTypeRef Ty)1114ef860a24SChandler Carruth LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
11159bbda191SAmaury Sechet   return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
1116ef860a24SChandler Carruth }
1117ef860a24SChandler Carruth 
1118ef860a24SChandler Carruth /*--.. Operations on metadata nodes ........................................--*/
1119ef860a24SChandler Carruth 
LLVMMDStringInContext2(LLVMContextRef C,const char * Str,size_t SLen)112009c5b883SRobert Widmann LLVMMetadataRef LLVMMDStringInContext2(LLVMContextRef C, const char *Str,
112109c5b883SRobert Widmann                                        size_t SLen) {
112209c5b883SRobert Widmann   return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
112309c5b883SRobert Widmann }
112409c5b883SRobert Widmann 
LLVMMDNodeInContext2(LLVMContextRef C,LLVMMetadataRef * MDs,size_t Count)112509c5b883SRobert Widmann LLVMMetadataRef LLVMMDNodeInContext2(LLVMContextRef C, LLVMMetadataRef *MDs,
112609c5b883SRobert Widmann                                      size_t Count) {
112709c5b883SRobert Widmann   return wrap(MDNode::get(*unwrap(C), ArrayRef<Metadata*>(unwrap(MDs), Count)));
112809c5b883SRobert Widmann }
112909c5b883SRobert Widmann 
LLVMMDStringInContext(LLVMContextRef C,const char * Str,unsigned SLen)1130ef860a24SChandler Carruth LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
1131ef860a24SChandler Carruth                                    unsigned SLen) {
11325bf8fef5SDuncan P. N. Exon Smith   LLVMContext &Context = *unwrap(C);
11335bf8fef5SDuncan P. N. Exon Smith   return wrap(MetadataAsValue::get(
11345bf8fef5SDuncan P. N. Exon Smith       Context, MDString::get(Context, StringRef(Str, SLen))));
1135ef860a24SChandler Carruth }
1136ef860a24SChandler Carruth 
LLVMMDString(const char * Str,unsigned SLen)1137ef860a24SChandler Carruth LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1138ef860a24SChandler Carruth   return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1139ef860a24SChandler Carruth }
1140ef860a24SChandler Carruth 
LLVMMDNodeInContext(LLVMContextRef C,LLVMValueRef * Vals,unsigned Count)1141ef860a24SChandler Carruth LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
1142ef860a24SChandler Carruth                                  unsigned Count) {
11435bf8fef5SDuncan P. N. Exon Smith   LLVMContext &Context = *unwrap(C);
11445bf8fef5SDuncan P. N. Exon Smith   SmallVector<Metadata *, 8> MDs;
11455bf8fef5SDuncan P. N. Exon Smith   for (auto *OV : makeArrayRef(Vals, Count)) {
11465bf8fef5SDuncan P. N. Exon Smith     Value *V = unwrap(OV);
11475bf8fef5SDuncan P. N. Exon Smith     Metadata *MD;
11485bf8fef5SDuncan P. N. Exon Smith     if (!V)
11495bf8fef5SDuncan P. N. Exon Smith       MD = nullptr;
11505bf8fef5SDuncan P. N. Exon Smith     else if (auto *C = dyn_cast<Constant>(V))
11515bf8fef5SDuncan P. N. Exon Smith       MD = ConstantAsMetadata::get(C);
11525bf8fef5SDuncan P. N. Exon Smith     else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
11535bf8fef5SDuncan P. N. Exon Smith       MD = MDV->getMetadata();
11545bf8fef5SDuncan P. N. Exon Smith       assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
11555bf8fef5SDuncan P. N. Exon Smith                                           "outside of direct argument to call");
11565bf8fef5SDuncan P. N. Exon Smith     } else {
11575bf8fef5SDuncan P. N. Exon Smith       // This is function-local metadata.  Pretend to make an MDNode.
11585bf8fef5SDuncan P. N. Exon Smith       assert(Count == 1 &&
11595bf8fef5SDuncan P. N. Exon Smith              "Expected only one operand to function-local metadata");
11605bf8fef5SDuncan P. N. Exon Smith       return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
11615bf8fef5SDuncan P. N. Exon Smith     }
11625bf8fef5SDuncan P. N. Exon Smith 
11635bf8fef5SDuncan P. N. Exon Smith     MDs.push_back(MD);
11645bf8fef5SDuncan P. N. Exon Smith   }
11655bf8fef5SDuncan P. N. Exon Smith   return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
1166ef860a24SChandler Carruth }
1167ef860a24SChandler Carruth 
LLVMMDNode(LLVMValueRef * Vals,unsigned Count)1168ef860a24SChandler Carruth LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1169ef860a24SChandler Carruth   return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1170ef860a24SChandler Carruth }
1171ef860a24SChandler Carruth 
LLVMMetadataAsValue(LLVMContextRef C,LLVMMetadataRef MD)1172f8429754SAmaury Sechet LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
1173f8429754SAmaury Sechet   return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1174f8429754SAmaury Sechet }
1175f8429754SAmaury Sechet 
LLVMValueAsMetadata(LLVMValueRef Val)1176f8429754SAmaury Sechet LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {
1177f8429754SAmaury Sechet   auto *V = unwrap(Val);
1178f8429754SAmaury Sechet   if (auto *C = dyn_cast<Constant>(V))
1179f8429754SAmaury Sechet     return wrap(ConstantAsMetadata::get(C));
1180f8429754SAmaury Sechet   if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1181f8429754SAmaury Sechet     return wrap(MAV->getMetadata());
1182f8429754SAmaury Sechet   return wrap(ValueAsMetadata::get(V));
1183f8429754SAmaury Sechet }
1184f8429754SAmaury Sechet 
LLVMGetMDString(LLVMValueRef V,unsigned * Length)11857c2883cfSAmaury Sechet const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
11865bf8fef5SDuncan P. N. Exon Smith   if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
11875bf8fef5SDuncan P. N. Exon Smith     if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
11887c2883cfSAmaury Sechet       *Length = S->getString().size();
1189ef860a24SChandler Carruth       return S->getString().data();
1190ef860a24SChandler Carruth     }
11917c2883cfSAmaury Sechet   *Length = 0;
1192c620761cSCraig Topper   return nullptr;
1193ef860a24SChandler Carruth }
1194ef860a24SChandler Carruth 
LLVMGetMDNodeNumOperands(LLVMValueRef V)1195b130f43bSAmaury Sechet unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
11965bf8fef5SDuncan P. N. Exon Smith   auto *MD = cast<MetadataAsValue>(unwrap(V));
11975bf8fef5SDuncan P. N. Exon Smith   if (isa<ValueAsMetadata>(MD->getMetadata()))
11985bf8fef5SDuncan P. N. Exon Smith     return 1;
11995bf8fef5SDuncan P. N. Exon Smith   return cast<MDNode>(MD->getMetadata())->getNumOperands();
1200ef860a24SChandler Carruth }
1201ef860a24SChandler Carruth 
LLVMGetFirstNamedMetadata(LLVMModuleRef M)12020a35b766SRobert Widmann LLVMNamedMDNodeRef LLVMGetFirstNamedMetadata(LLVMModuleRef M) {
12030a35b766SRobert Widmann   Module *Mod = unwrap(M);
12040a35b766SRobert Widmann   Module::named_metadata_iterator I = Mod->named_metadata_begin();
12050a35b766SRobert Widmann   if (I == Mod->named_metadata_end())
12060a35b766SRobert Widmann     return nullptr;
12070a35b766SRobert Widmann   return wrap(&*I);
12080a35b766SRobert Widmann }
12090a35b766SRobert Widmann 
LLVMGetLastNamedMetadata(LLVMModuleRef M)12100a35b766SRobert Widmann LLVMNamedMDNodeRef LLVMGetLastNamedMetadata(LLVMModuleRef M) {
12110a35b766SRobert Widmann   Module *Mod = unwrap(M);
12120a35b766SRobert Widmann   Module::named_metadata_iterator I = Mod->named_metadata_end();
12130a35b766SRobert Widmann   if (I == Mod->named_metadata_begin())
12140a35b766SRobert Widmann     return nullptr;
12150a35b766SRobert Widmann   return wrap(&*--I);
12160a35b766SRobert Widmann }
12170a35b766SRobert Widmann 
LLVMGetNextNamedMetadata(LLVMNamedMDNodeRef NMD)12180a35b766SRobert Widmann LLVMNamedMDNodeRef LLVMGetNextNamedMetadata(LLVMNamedMDNodeRef NMD) {
12190a35b766SRobert Widmann   NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
12200a35b766SRobert Widmann   Module::named_metadata_iterator I(NamedNode);
12210a35b766SRobert Widmann   if (++I == NamedNode->getParent()->named_metadata_end())
12220a35b766SRobert Widmann     return nullptr;
12230a35b766SRobert Widmann   return wrap(&*I);
12240a35b766SRobert Widmann }
12250a35b766SRobert Widmann 
LLVMGetPreviousNamedMetadata(LLVMNamedMDNodeRef NMD)12260a35b766SRobert Widmann LLVMNamedMDNodeRef LLVMGetPreviousNamedMetadata(LLVMNamedMDNodeRef NMD) {
12270a35b766SRobert Widmann   NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
12280a35b766SRobert Widmann   Module::named_metadata_iterator I(NamedNode);
12290a35b766SRobert Widmann   if (I == NamedNode->getParent()->named_metadata_begin())
12300a35b766SRobert Widmann     return nullptr;
12310a35b766SRobert Widmann   return wrap(&*--I);
12320a35b766SRobert Widmann }
12330a35b766SRobert Widmann 
LLVMGetNamedMetadata(LLVMModuleRef M,const char * Name,size_t NameLen)12340a35b766SRobert Widmann LLVMNamedMDNodeRef LLVMGetNamedMetadata(LLVMModuleRef M,
12350a35b766SRobert Widmann                                         const char *Name, size_t NameLen) {
12360a35b766SRobert Widmann   return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
12370a35b766SRobert Widmann }
12380a35b766SRobert Widmann 
LLVMGetOrInsertNamedMetadata(LLVMModuleRef M,const char * Name,size_t NameLen)12390a35b766SRobert Widmann LLVMNamedMDNodeRef LLVMGetOrInsertNamedMetadata(LLVMModuleRef M,
12400a35b766SRobert Widmann                                                 const char *Name, size_t NameLen) {
12410a35b766SRobert Widmann   return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
12420a35b766SRobert Widmann }
12430a35b766SRobert Widmann 
LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD,size_t * NameLen)12440a35b766SRobert Widmann const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
12450a35b766SRobert Widmann   NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
12460a35b766SRobert Widmann   *NameLen = NamedNode->getName().size();
12470a35b766SRobert Widmann   return NamedNode->getName().data();
12480a35b766SRobert Widmann }
12490a35b766SRobert Widmann 
LLVMGetMDNodeOperands(LLVMValueRef V,LLVMValueRef * Dest)1250b130f43bSAmaury Sechet void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
12515bf8fef5SDuncan P. N. Exon Smith   auto *MD = cast<MetadataAsValue>(unwrap(V));
12525bf8fef5SDuncan P. N. Exon Smith   if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
12535bf8fef5SDuncan P. N. Exon Smith     *Dest = wrap(MDV->getValue());
12545bf8fef5SDuncan P. N. Exon Smith     return;
12555bf8fef5SDuncan P. N. Exon Smith   }
12565bf8fef5SDuncan P. N. Exon Smith   const auto *N = cast<MDNode>(MD->getMetadata());
1257ef860a24SChandler Carruth   const unsigned numOperands = N->getNumOperands();
12585bf8fef5SDuncan P. N. Exon Smith   LLVMContext &Context = unwrap(V)->getContext();
1259ef860a24SChandler Carruth   for (unsigned i = 0; i < numOperands; i++)
12605bf8fef5SDuncan P. N. Exon Smith     Dest[i] = getMDNodeOperandImpl(Context, N, i);
1261ef860a24SChandler Carruth }
1262ef860a24SChandler Carruth 
LLVMGetNamedMetadataNumOperands(LLVMModuleRef M,const char * Name)1263b130f43bSAmaury Sechet unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
1264b130f43bSAmaury Sechet   if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1265ef860a24SChandler Carruth     return N->getNumOperands();
1266ef860a24SChandler Carruth   }
1267ef860a24SChandler Carruth   return 0;
1268ef860a24SChandler Carruth }
1269ef860a24SChandler Carruth 
LLVMGetNamedMetadataOperands(LLVMModuleRef M,const char * Name,LLVMValueRef * Dest)1270b130f43bSAmaury Sechet void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
1271b130f43bSAmaury Sechet                                   LLVMValueRef *Dest) {
1272b130f43bSAmaury Sechet   NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1273ef860a24SChandler Carruth   if (!N)
1274ef860a24SChandler Carruth     return;
12755bf8fef5SDuncan P. N. Exon Smith   LLVMContext &Context = unwrap(M)->getContext();
1276ef860a24SChandler Carruth   for (unsigned i=0;i<N->getNumOperands();i++)
12775bf8fef5SDuncan P. N. Exon Smith     Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1278ef860a24SChandler Carruth }
1279ef860a24SChandler Carruth 
LLVMAddNamedMetadataOperand(LLVMModuleRef M,const char * Name,LLVMValueRef Val)1280b130f43bSAmaury Sechet void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
1281b130f43bSAmaury Sechet                                  LLVMValueRef Val) {
1282b130f43bSAmaury Sechet   NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1283ef860a24SChandler Carruth   if (!N)
1284ef860a24SChandler Carruth     return;
12855bf8fef5SDuncan P. N. Exon Smith   if (!Val)
12865bf8fef5SDuncan P. N. Exon Smith     return;
1287a09ac008SBjorn Steinbrink   N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1288ef860a24SChandler Carruth }
1289ef860a24SChandler Carruth 
LLVMGetDebugLocDirectory(LLVMValueRef Val,unsigned * Length)12900d1cbcc3SSaleem Abdulrasool const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
12910d1cbcc3SSaleem Abdulrasool   if (!Length) return nullptr;
12920d1cbcc3SSaleem Abdulrasool   StringRef S;
129350392a3bSwhitequark   if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
129450392a3bSwhitequark     if (const auto &DL = I->getDebugLoc()) {
129550392a3bSwhitequark       S = DL->getDirectory();
129650392a3bSwhitequark     }
129750392a3bSwhitequark   } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
12980d1cbcc3SSaleem Abdulrasool     SmallVector<DIGlobalVariableExpression *, 1> GVEs;
12990d1cbcc3SSaleem Abdulrasool     GV->getDebugInfo(GVEs);
13000d1cbcc3SSaleem Abdulrasool     if (GVEs.size())
13010d1cbcc3SSaleem Abdulrasool       if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
13020d1cbcc3SSaleem Abdulrasool         S = DGV->getDirectory();
130350392a3bSwhitequark   } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
13040d1cbcc3SSaleem Abdulrasool     if (const DISubprogram *DSP = F->getSubprogram())
13050d1cbcc3SSaleem Abdulrasool       S = DSP->getDirectory();
13060d1cbcc3SSaleem Abdulrasool   } else {
13070d1cbcc3SSaleem Abdulrasool     assert(0 && "Expected Instruction, GlobalVariable or Function");
13080d1cbcc3SSaleem Abdulrasool     return nullptr;
13090d1cbcc3SSaleem Abdulrasool   }
13100d1cbcc3SSaleem Abdulrasool   *Length = S.size();
13110d1cbcc3SSaleem Abdulrasool   return S.data();
13120d1cbcc3SSaleem Abdulrasool }
13130d1cbcc3SSaleem Abdulrasool 
LLVMGetDebugLocFilename(LLVMValueRef Val,unsigned * Length)13140d1cbcc3SSaleem Abdulrasool const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
13150d1cbcc3SSaleem Abdulrasool   if (!Length) return nullptr;
13160d1cbcc3SSaleem Abdulrasool   StringRef S;
131750392a3bSwhitequark   if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
131850392a3bSwhitequark     if (const auto &DL = I->getDebugLoc()) {
131950392a3bSwhitequark       S = DL->getFilename();
132050392a3bSwhitequark     }
132150392a3bSwhitequark   } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
13220d1cbcc3SSaleem Abdulrasool     SmallVector<DIGlobalVariableExpression *, 1> GVEs;
13230d1cbcc3SSaleem Abdulrasool     GV->getDebugInfo(GVEs);
13240d1cbcc3SSaleem Abdulrasool     if (GVEs.size())
13250d1cbcc3SSaleem Abdulrasool       if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
13260d1cbcc3SSaleem Abdulrasool         S = DGV->getFilename();
132750392a3bSwhitequark   } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
13280d1cbcc3SSaleem Abdulrasool     if (const DISubprogram *DSP = F->getSubprogram())
13290d1cbcc3SSaleem Abdulrasool       S = DSP->getFilename();
13300d1cbcc3SSaleem Abdulrasool   } else {
13310d1cbcc3SSaleem Abdulrasool     assert(0 && "Expected Instruction, GlobalVariable or Function");
13320d1cbcc3SSaleem Abdulrasool     return nullptr;
13330d1cbcc3SSaleem Abdulrasool   }
13340d1cbcc3SSaleem Abdulrasool   *Length = S.size();
13350d1cbcc3SSaleem Abdulrasool   return S.data();
13360d1cbcc3SSaleem Abdulrasool }
13370d1cbcc3SSaleem Abdulrasool 
LLVMGetDebugLocLine(LLVMValueRef Val)13380d1cbcc3SSaleem Abdulrasool unsigned LLVMGetDebugLocLine(LLVMValueRef Val) {
13390d1cbcc3SSaleem Abdulrasool   unsigned L = 0;
134050392a3bSwhitequark   if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
134150392a3bSwhitequark     if (const auto &DL = I->getDebugLoc()) {
134250392a3bSwhitequark       L = DL->getLine();
134350392a3bSwhitequark     }
134450392a3bSwhitequark   } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
13450d1cbcc3SSaleem Abdulrasool     SmallVector<DIGlobalVariableExpression *, 1> GVEs;
13460d1cbcc3SSaleem Abdulrasool     GV->getDebugInfo(GVEs);
13470d1cbcc3SSaleem Abdulrasool     if (GVEs.size())
13480d1cbcc3SSaleem Abdulrasool       if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
13490d1cbcc3SSaleem Abdulrasool         L = DGV->getLine();
135050392a3bSwhitequark   } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
13510d1cbcc3SSaleem Abdulrasool     if (const DISubprogram *DSP = F->getSubprogram())
13520d1cbcc3SSaleem Abdulrasool       L = DSP->getLine();
13530d1cbcc3SSaleem Abdulrasool   } else {
13540d1cbcc3SSaleem Abdulrasool     assert(0 && "Expected Instruction, GlobalVariable or Function");
13550d1cbcc3SSaleem Abdulrasool     return -1;
13560d1cbcc3SSaleem Abdulrasool   }
13570d1cbcc3SSaleem Abdulrasool   return L;
13580d1cbcc3SSaleem Abdulrasool }
13590d1cbcc3SSaleem Abdulrasool 
LLVMGetDebugLocColumn(LLVMValueRef Val)13600d1cbcc3SSaleem Abdulrasool unsigned LLVMGetDebugLocColumn(LLVMValueRef Val) {
13610d1cbcc3SSaleem Abdulrasool   unsigned C = 0;
136250392a3bSwhitequark   if (const auto *I = dyn_cast<Instruction>(unwrap(Val)))
136350392a3bSwhitequark     if (const auto &DL = I->getDebugLoc())
136450392a3bSwhitequark       C = DL->getColumn();
13650d1cbcc3SSaleem Abdulrasool   return C;
13660d1cbcc3SSaleem Abdulrasool }
13670d1cbcc3SSaleem Abdulrasool 
1368ef860a24SChandler Carruth /*--.. Operations on scalar constants ......................................--*/
1369ef860a24SChandler Carruth 
LLVMConstInt(LLVMTypeRef IntTy,unsigned long long N,LLVMBool SignExtend)1370ef860a24SChandler Carruth LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1371ef860a24SChandler Carruth                           LLVMBool SignExtend) {
1372ef860a24SChandler Carruth   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1373ef860a24SChandler Carruth }
1374ef860a24SChandler Carruth 
LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,unsigned NumWords,const uint64_t Words[])1375ef860a24SChandler Carruth LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
1376ef860a24SChandler Carruth                                               unsigned NumWords,
1377ef860a24SChandler Carruth                                               const uint64_t Words[]) {
1378ef860a24SChandler Carruth     IntegerType *Ty = unwrap<IntegerType>(IntTy);
1379ef860a24SChandler Carruth     return wrap(ConstantInt::get(Ty->getContext(),
1380ef860a24SChandler Carruth                                  APInt(Ty->getBitWidth(),
1381ef860a24SChandler Carruth                                        makeArrayRef(Words, NumWords))));
1382ef860a24SChandler Carruth }
1383ef860a24SChandler Carruth 
LLVMConstIntOfString(LLVMTypeRef IntTy,const char Str[],uint8_t Radix)1384ef860a24SChandler Carruth LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
1385ef860a24SChandler Carruth                                   uint8_t Radix) {
1386ef860a24SChandler Carruth   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1387ef860a24SChandler Carruth                                Radix));
1388ef860a24SChandler Carruth }
1389ef860a24SChandler Carruth 
LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy,const char Str[],unsigned SLen,uint8_t Radix)1390ef860a24SChandler Carruth LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
1391ef860a24SChandler Carruth                                          unsigned SLen, uint8_t Radix) {
1392ef860a24SChandler Carruth   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1393ef860a24SChandler Carruth                                Radix));
1394ef860a24SChandler Carruth }
1395ef860a24SChandler Carruth 
LLVMConstReal(LLVMTypeRef RealTy,double N)1396ef860a24SChandler Carruth LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
1397ef860a24SChandler Carruth   return wrap(ConstantFP::get(unwrap(RealTy), N));
1398ef860a24SChandler Carruth }
1399ef860a24SChandler Carruth 
LLVMConstRealOfString(LLVMTypeRef RealTy,const char * Text)1400ef860a24SChandler Carruth LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
1401ef860a24SChandler Carruth   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1402ef860a24SChandler Carruth }
1403ef860a24SChandler Carruth 
LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy,const char Str[],unsigned SLen)1404ef860a24SChandler Carruth LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
1405ef860a24SChandler Carruth                                           unsigned SLen) {
1406ef860a24SChandler Carruth   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1407ef860a24SChandler Carruth }
1408ef860a24SChandler Carruth 
LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal)1409ef860a24SChandler Carruth unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1410ef860a24SChandler Carruth   return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1411ef860a24SChandler Carruth }
1412ef860a24SChandler Carruth 
LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal)1413ef860a24SChandler Carruth long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1414ef860a24SChandler Carruth   return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1415ef860a24SChandler Carruth }
1416ef860a24SChandler Carruth 
LLVMConstRealGetDouble(LLVMValueRef ConstantVal,LLVMBool * LosesInfo)14171d98e6ddSPeter Zotov double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
14181d98e6ddSPeter Zotov   ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
14191d98e6ddSPeter Zotov   Type *Ty = cFP->getType();
14201d98e6ddSPeter Zotov 
1421c162f086SSerge Pavlov   if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
1422c162f086SSerge Pavlov       Ty->isDoubleTy()) {
14231d98e6ddSPeter Zotov     *LosesInfo = false;
14241d98e6ddSPeter Zotov     return cFP->getValueAPF().convertToDouble();
14251d98e6ddSPeter Zotov   }
14261d98e6ddSPeter Zotov 
14271d98e6ddSPeter Zotov   bool APFLosesInfo;
14281d98e6ddSPeter Zotov   APFloat APF = cFP->getValueAPF();
142917c7f703SStephan Bergmann   APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
14301d98e6ddSPeter Zotov   *LosesInfo = APFLosesInfo;
14311d98e6ddSPeter Zotov   return APF.convertToDouble();
14321d98e6ddSPeter Zotov }
14331d98e6ddSPeter Zotov 
1434ef860a24SChandler Carruth /*--.. Operations on composite constants ...................................--*/
1435ef860a24SChandler Carruth 
LLVMConstStringInContext(LLVMContextRef C,const char * Str,unsigned Length,LLVMBool DontNullTerminate)1436ef860a24SChandler Carruth LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
1437ef860a24SChandler Carruth                                       unsigned Length,
1438ef860a24SChandler Carruth                                       LLVMBool DontNullTerminate) {
1439ef860a24SChandler Carruth   /* Inverted the sense of AddNull because ', 0)' is a
1440ef860a24SChandler Carruth      better mnemonic for null termination than ', 1)'. */
1441ef860a24SChandler Carruth   return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1442ef860a24SChandler Carruth                                            DontNullTerminate == 0));
1443ef860a24SChandler Carruth }
1444006ce632SAmaury Sechet 
LLVMConstString(const char * Str,unsigned Length,LLVMBool DontNullTerminate)1445ef860a24SChandler Carruth LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1446ef860a24SChandler Carruth                              LLVMBool DontNullTerminate) {
1447ef860a24SChandler Carruth   return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1448ef860a24SChandler Carruth                                   DontNullTerminate);
1449ef860a24SChandler Carruth }
1450f9aa882cSPeter Zotov 
LLVMGetAggregateElement(LLVMValueRef C,unsigned Idx)1451da34966aSNikita Popov LLVMValueRef LLVMGetAggregateElement(LLVMValueRef C, unsigned Idx) {
1452da34966aSNikita Popov   return wrap(unwrap<Constant>(C)->getAggregateElement(Idx));
1453da34966aSNikita Popov }
1454da34966aSNikita Popov 
LLVMGetElementAsConstant(LLVMValueRef C,unsigned idx)1455006ce632SAmaury Sechet LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1456006ce632SAmaury Sechet   return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1457f9aa882cSPeter Zotov }
1458f9aa882cSPeter Zotov 
LLVMIsConstantString(LLVMValueRef C)1459006ce632SAmaury Sechet LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1460006ce632SAmaury Sechet   return unwrap<ConstantDataSequential>(C)->isString();
1461f9aa882cSPeter Zotov }
1462f9aa882cSPeter Zotov 
LLVMGetAsString(LLVMValueRef C,size_t * Length)1463006ce632SAmaury Sechet const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
14647c2883cfSAmaury Sechet   StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
14657c2883cfSAmaury Sechet   *Length = Str.size();
14667c2883cfSAmaury Sechet   return Str.data();
1467f9aa882cSPeter Zotov }
1468f9aa882cSPeter Zotov 
LLVMConstArray(LLVMTypeRef ElementTy,LLVMValueRef * ConstantVals,unsigned Length)1469ef860a24SChandler Carruth LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1470ef860a24SChandler Carruth                             LLVMValueRef *ConstantVals, unsigned Length) {
1471ef860a24SChandler Carruth   ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1472ef860a24SChandler Carruth   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1473ef860a24SChandler Carruth }
1474f9aa882cSPeter Zotov 
LLVMConstStructInContext(LLVMContextRef C,LLVMValueRef * ConstantVals,unsigned Count,LLVMBool Packed)1475c78768f1SAmaury Sechet LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1476c78768f1SAmaury Sechet                                       LLVMValueRef *ConstantVals,
1477c78768f1SAmaury Sechet                                       unsigned Count, LLVMBool Packed) {
1478c78768f1SAmaury Sechet   Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1479c78768f1SAmaury Sechet   return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1480c78768f1SAmaury Sechet                                       Packed != 0));
1481c78768f1SAmaury Sechet }
1482c78768f1SAmaury Sechet 
LLVMConstStruct(LLVMValueRef * ConstantVals,unsigned Count,LLVMBool Packed)1483ef860a24SChandler Carruth LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1484ef860a24SChandler Carruth                              LLVMBool Packed) {
1485ef860a24SChandler Carruth   return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1486ef860a24SChandler Carruth                                   Packed);
1487ef860a24SChandler Carruth }
1488ef860a24SChandler Carruth 
LLVMConstNamedStruct(LLVMTypeRef StructTy,LLVMValueRef * ConstantVals,unsigned Count)1489ef860a24SChandler Carruth LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1490ef860a24SChandler Carruth                                   LLVMValueRef *ConstantVals,
1491ef860a24SChandler Carruth                                   unsigned Count) {
1492ef860a24SChandler Carruth   Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1493ef860a24SChandler Carruth   StructType *Ty = cast<StructType>(unwrap(StructTy));
1494ef860a24SChandler Carruth 
1495ef860a24SChandler Carruth   return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
1496ef860a24SChandler Carruth }
1497ef860a24SChandler Carruth 
LLVMConstVector(LLVMValueRef * ScalarConstantVals,unsigned Size)1498ef860a24SChandler Carruth LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1499ef860a24SChandler Carruth   return wrap(ConstantVector::get(makeArrayRef(
1500ef860a24SChandler Carruth                             unwrap<Constant>(ScalarConstantVals, Size), Size)));
1501ef860a24SChandler Carruth }
1502ef860a24SChandler Carruth 
1503ef860a24SChandler Carruth /*-- Opcode mapping */
1504ef860a24SChandler Carruth 
map_to_llvmopcode(int opcode)1505ef860a24SChandler Carruth static LLVMOpcode map_to_llvmopcode(int opcode)
1506ef860a24SChandler Carruth {
1507ef860a24SChandler Carruth     switch (opcode) {
1508ef860a24SChandler Carruth       default: llvm_unreachable("Unhandled Opcode.");
1509ef860a24SChandler Carruth #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
15109fb823bbSChandler Carruth #include "llvm/IR/Instruction.def"
1511ef860a24SChandler Carruth #undef HANDLE_INST
1512ef860a24SChandler Carruth     }
1513ef860a24SChandler Carruth }
1514ef860a24SChandler Carruth 
map_from_llvmopcode(LLVMOpcode code)1515ef860a24SChandler Carruth static int map_from_llvmopcode(LLVMOpcode code)
1516ef860a24SChandler Carruth {
1517ef860a24SChandler Carruth     switch (code) {
1518ef860a24SChandler Carruth #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
15199fb823bbSChandler Carruth #include "llvm/IR/Instruction.def"
1520ef860a24SChandler Carruth #undef HANDLE_INST
1521ef860a24SChandler Carruth     }
1522ef860a24SChandler Carruth     llvm_unreachable("Unhandled Opcode.");
1523ef860a24SChandler Carruth }
1524ef860a24SChandler Carruth 
1525ef860a24SChandler Carruth /*--.. Constant expressions ................................................--*/
1526ef860a24SChandler Carruth 
LLVMGetConstOpcode(LLVMValueRef ConstantVal)1527ef860a24SChandler Carruth LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
1528ef860a24SChandler Carruth   return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1529ef860a24SChandler Carruth }
1530ef860a24SChandler Carruth 
LLVMAlignOf(LLVMTypeRef Ty)1531ef860a24SChandler Carruth LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
1532ef860a24SChandler Carruth   return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1533ef860a24SChandler Carruth }
1534ef860a24SChandler Carruth 
LLVMSizeOf(LLVMTypeRef Ty)1535ef860a24SChandler Carruth LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
1536ef860a24SChandler Carruth   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1537ef860a24SChandler Carruth }
1538ef860a24SChandler Carruth 
LLVMConstNeg(LLVMValueRef ConstantVal)1539ef860a24SChandler Carruth LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
1540ef860a24SChandler Carruth   return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1541ef860a24SChandler Carruth }
1542ef860a24SChandler Carruth 
LLVMConstNSWNeg(LLVMValueRef ConstantVal)1543ef860a24SChandler Carruth LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
1544ef860a24SChandler Carruth   return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1545ef860a24SChandler Carruth }
1546ef860a24SChandler Carruth 
LLVMConstNUWNeg(LLVMValueRef ConstantVal)1547ef860a24SChandler Carruth LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
1548ef860a24SChandler Carruth   return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1549ef860a24SChandler Carruth }
1550ef860a24SChandler Carruth 
1551ef860a24SChandler Carruth 
LLVMConstFNeg(LLVMValueRef ConstantVal)1552ef860a24SChandler Carruth LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
1553ef860a24SChandler Carruth   return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
1554ef860a24SChandler Carruth }
1555ef860a24SChandler Carruth 
LLVMConstNot(LLVMValueRef ConstantVal)1556ef860a24SChandler Carruth LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
1557ef860a24SChandler Carruth   return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1558ef860a24SChandler Carruth }
1559ef860a24SChandler Carruth 
LLVMConstAdd(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)1560ef860a24SChandler Carruth LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1561ef860a24SChandler Carruth   return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1562ef860a24SChandler Carruth                                    unwrap<Constant>(RHSConstant)));
1563ef860a24SChandler Carruth }
1564ef860a24SChandler Carruth 
LLVMConstNSWAdd(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)1565ef860a24SChandler Carruth LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1566ef860a24SChandler Carruth                              LLVMValueRef RHSConstant) {
1567ef860a24SChandler Carruth   return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1568ef860a24SChandler Carruth                                       unwrap<Constant>(RHSConstant)));
1569ef860a24SChandler Carruth }
1570ef860a24SChandler Carruth 
LLVMConstNUWAdd(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)1571ef860a24SChandler Carruth LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1572ef860a24SChandler Carruth                              LLVMValueRef RHSConstant) {
1573ef860a24SChandler Carruth   return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1574ef860a24SChandler Carruth                                       unwrap<Constant>(RHSConstant)));
1575ef860a24SChandler Carruth }
1576ef860a24SChandler Carruth 
LLVMConstSub(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)1577ef860a24SChandler Carruth LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1578ef860a24SChandler Carruth   return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1579ef860a24SChandler Carruth                                    unwrap<Constant>(RHSConstant)));
1580ef860a24SChandler Carruth }
1581ef860a24SChandler Carruth 
LLVMConstNSWSub(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)1582ef860a24SChandler Carruth LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1583ef860a24SChandler Carruth                              LLVMValueRef RHSConstant) {
1584ef860a24SChandler Carruth   return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1585ef860a24SChandler Carruth                                       unwrap<Constant>(RHSConstant)));
1586ef860a24SChandler Carruth }
1587ef860a24SChandler Carruth 
LLVMConstNUWSub(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)1588ef860a24SChandler Carruth LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1589ef860a24SChandler Carruth                              LLVMValueRef RHSConstant) {
1590ef860a24SChandler Carruth   return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1591ef860a24SChandler Carruth                                       unwrap<Constant>(RHSConstant)));
1592ef860a24SChandler Carruth }
1593ef860a24SChandler Carruth 
LLVMConstMul(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)1594ef860a24SChandler Carruth LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1595ef860a24SChandler Carruth   return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1596ef860a24SChandler Carruth                                    unwrap<Constant>(RHSConstant)));
1597ef860a24SChandler Carruth }
1598ef860a24SChandler Carruth 
LLVMConstNSWMul(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)1599ef860a24SChandler Carruth LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1600ef860a24SChandler Carruth                              LLVMValueRef RHSConstant) {
1601ef860a24SChandler Carruth   return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1602ef860a24SChandler Carruth                                       unwrap<Constant>(RHSConstant)));
1603ef860a24SChandler Carruth }
1604ef860a24SChandler Carruth 
LLVMConstNUWMul(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)1605ef860a24SChandler Carruth LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1606ef860a24SChandler Carruth                              LLVMValueRef RHSConstant) {
1607ef860a24SChandler Carruth   return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1608ef860a24SChandler Carruth                                       unwrap<Constant>(RHSConstant)));
1609ef860a24SChandler Carruth }
1610ef860a24SChandler Carruth 
LLVMConstAnd(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)1611ef860a24SChandler Carruth LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1612ef860a24SChandler Carruth   return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1613ef860a24SChandler Carruth                                    unwrap<Constant>(RHSConstant)));
1614ef860a24SChandler Carruth }
1615ef860a24SChandler Carruth 
LLVMConstOr(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)1616ef860a24SChandler Carruth LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1617ef860a24SChandler Carruth   return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1618ef860a24SChandler Carruth                                   unwrap<Constant>(RHSConstant)));
1619ef860a24SChandler Carruth }
1620ef860a24SChandler Carruth 
LLVMConstXor(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)1621ef860a24SChandler Carruth LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1622ef860a24SChandler Carruth   return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1623ef860a24SChandler Carruth                                    unwrap<Constant>(RHSConstant)));
1624ef860a24SChandler Carruth }
1625ef860a24SChandler Carruth 
LLVMConstICmp(LLVMIntPredicate Predicate,LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)1626ef860a24SChandler Carruth LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1627ef860a24SChandler Carruth                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1628ef860a24SChandler Carruth   return wrap(ConstantExpr::getICmp(Predicate,
1629ef860a24SChandler Carruth                                     unwrap<Constant>(LHSConstant),
1630ef860a24SChandler Carruth                                     unwrap<Constant>(RHSConstant)));
1631ef860a24SChandler Carruth }
1632ef860a24SChandler Carruth 
LLVMConstFCmp(LLVMRealPredicate Predicate,LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)1633ef860a24SChandler Carruth LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1634ef860a24SChandler Carruth                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1635ef860a24SChandler Carruth   return wrap(ConstantExpr::getFCmp(Predicate,
1636ef860a24SChandler Carruth                                     unwrap<Constant>(LHSConstant),
1637ef860a24SChandler Carruth                                     unwrap<Constant>(RHSConstant)));
1638ef860a24SChandler Carruth }
1639ef860a24SChandler Carruth 
LLVMConstShl(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)1640ef860a24SChandler Carruth LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1641ef860a24SChandler Carruth   return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1642ef860a24SChandler Carruth                                    unwrap<Constant>(RHSConstant)));
1643ef860a24SChandler Carruth }
1644ef860a24SChandler Carruth 
LLVMConstLShr(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)1645ef860a24SChandler Carruth LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1646ef860a24SChandler Carruth   return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1647ef860a24SChandler Carruth                                     unwrap<Constant>(RHSConstant)));
1648ef860a24SChandler Carruth }
1649ef860a24SChandler Carruth 
LLVMConstAShr(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)1650ef860a24SChandler Carruth LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1651ef860a24SChandler Carruth   return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1652ef860a24SChandler Carruth                                     unwrap<Constant>(RHSConstant)));
1653ef860a24SChandler Carruth }
1654ef860a24SChandler Carruth 
LLVMConstGEP(LLVMValueRef ConstantVal,LLVMValueRef * ConstantIndices,unsigned NumIndices)1655ef860a24SChandler Carruth LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1656ef860a24SChandler Carruth                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1657ef860a24SChandler Carruth   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1658ef860a24SChandler Carruth                                NumIndices);
1659544fa425SJames Y Knight   Constant *Val = unwrap<Constant>(ConstantVal);
1660d29e3192SNikita Popov   Type *Ty = Val->getType()->getScalarType()->getNonOpaquePointerElementType();
1661544fa425SJames Y Knight   return wrap(ConstantExpr::getGetElementPtr(Ty, Val, IdxList));
1662ef860a24SChandler Carruth }
1663ef860a24SChandler Carruth 
LLVMConstGEP2(LLVMTypeRef Ty,LLVMValueRef ConstantVal,LLVMValueRef * ConstantIndices,unsigned NumIndices)166468cb111fSNikita Popov LLVMValueRef LLVMConstGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
166568cb111fSNikita Popov                            LLVMValueRef *ConstantIndices, unsigned NumIndices) {
166668cb111fSNikita Popov   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
166768cb111fSNikita Popov                                NumIndices);
166868cb111fSNikita Popov   Constant *Val = unwrap<Constant>(ConstantVal);
166968cb111fSNikita Popov   return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList));
167068cb111fSNikita Popov }
167168cb111fSNikita Popov 
LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,LLVMValueRef * ConstantIndices,unsigned NumIndices)1672ef860a24SChandler Carruth LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1673ef860a24SChandler Carruth                                   LLVMValueRef *ConstantIndices,
1674ef860a24SChandler Carruth                                   unsigned NumIndices) {
1675ef860a24SChandler Carruth   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1676ef860a24SChandler Carruth                                NumIndices);
1677544fa425SJames Y Knight   Constant *Val = unwrap<Constant>(ConstantVal);
1678d29e3192SNikita Popov   Type *Ty = Val->getType()->getScalarType()->getNonOpaquePointerElementType();
1679544fa425SJames Y Knight   return wrap(ConstantExpr::getInBoundsGetElementPtr(Ty, Val, IdxList));
1680ef860a24SChandler Carruth }
1681ef860a24SChandler Carruth 
LLVMConstInBoundsGEP2(LLVMTypeRef Ty,LLVMValueRef ConstantVal,LLVMValueRef * ConstantIndices,unsigned NumIndices)168268cb111fSNikita Popov LLVMValueRef LLVMConstInBoundsGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
168368cb111fSNikita Popov                                    LLVMValueRef *ConstantIndices,
168468cb111fSNikita Popov                                    unsigned NumIndices) {
168568cb111fSNikita Popov   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
168668cb111fSNikita Popov                                NumIndices);
168768cb111fSNikita Popov   Constant *Val = unwrap<Constant>(ConstantVal);
168868cb111fSNikita Popov   return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList));
168968cb111fSNikita Popov }
169068cb111fSNikita Popov 
LLVMConstTrunc(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1691ef860a24SChandler Carruth LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1692ef860a24SChandler Carruth   return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1693ef860a24SChandler Carruth                                      unwrap(ToType)));
1694ef860a24SChandler Carruth }
1695ef860a24SChandler Carruth 
LLVMConstSExt(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1696ef860a24SChandler Carruth LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1697ef860a24SChandler Carruth   return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1698ef860a24SChandler Carruth                                     unwrap(ToType)));
1699ef860a24SChandler Carruth }
1700ef860a24SChandler Carruth 
LLVMConstZExt(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1701ef860a24SChandler Carruth LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1702ef860a24SChandler Carruth   return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1703ef860a24SChandler Carruth                                     unwrap(ToType)));
1704ef860a24SChandler Carruth }
1705ef860a24SChandler Carruth 
LLVMConstFPTrunc(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1706ef860a24SChandler Carruth LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1707ef860a24SChandler Carruth   return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1708ef860a24SChandler Carruth                                        unwrap(ToType)));
1709ef860a24SChandler Carruth }
1710ef860a24SChandler Carruth 
LLVMConstFPExt(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1711ef860a24SChandler Carruth LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1712ef860a24SChandler Carruth   return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1713ef860a24SChandler Carruth                                         unwrap(ToType)));
1714ef860a24SChandler Carruth }
1715ef860a24SChandler Carruth 
LLVMConstUIToFP(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1716ef860a24SChandler Carruth LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1717ef860a24SChandler Carruth   return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1718ef860a24SChandler Carruth                                       unwrap(ToType)));
1719ef860a24SChandler Carruth }
1720ef860a24SChandler Carruth 
LLVMConstSIToFP(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1721ef860a24SChandler Carruth LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1722ef860a24SChandler Carruth   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1723ef860a24SChandler Carruth                                       unwrap(ToType)));
1724ef860a24SChandler Carruth }
1725ef860a24SChandler Carruth 
LLVMConstFPToUI(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1726ef860a24SChandler Carruth LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1727ef860a24SChandler Carruth   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1728ef860a24SChandler Carruth                                       unwrap(ToType)));
1729ef860a24SChandler Carruth }
1730ef860a24SChandler Carruth 
LLVMConstFPToSI(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1731ef860a24SChandler Carruth LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1732ef860a24SChandler Carruth   return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1733ef860a24SChandler Carruth                                       unwrap(ToType)));
1734ef860a24SChandler Carruth }
1735ef860a24SChandler Carruth 
LLVMConstPtrToInt(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1736ef860a24SChandler Carruth LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1737ef860a24SChandler Carruth   return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1738ef860a24SChandler Carruth                                         unwrap(ToType)));
1739ef860a24SChandler Carruth }
1740ef860a24SChandler Carruth 
LLVMConstIntToPtr(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1741ef860a24SChandler Carruth LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1742ef860a24SChandler Carruth   return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1743ef860a24SChandler Carruth                                         unwrap(ToType)));
1744ef860a24SChandler Carruth }
1745ef860a24SChandler Carruth 
LLVMConstBitCast(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1746ef860a24SChandler Carruth LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1747ef860a24SChandler Carruth   return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1748ef860a24SChandler Carruth                                        unwrap(ToType)));
1749ef860a24SChandler Carruth }
1750ef860a24SChandler Carruth 
LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1751b03bd4d9SMatt Arsenault LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1752b03bd4d9SMatt Arsenault                                     LLVMTypeRef ToType) {
1753b03bd4d9SMatt Arsenault   return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1754b03bd4d9SMatt Arsenault                                              unwrap(ToType)));
1755b03bd4d9SMatt Arsenault }
1756b03bd4d9SMatt Arsenault 
LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1757ef860a24SChandler Carruth LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1758ef860a24SChandler Carruth                                     LLVMTypeRef ToType) {
1759ef860a24SChandler Carruth   return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1760ef860a24SChandler Carruth                                              unwrap(ToType)));
1761ef860a24SChandler Carruth }
1762ef860a24SChandler Carruth 
LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1763ef860a24SChandler Carruth LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1764ef860a24SChandler Carruth                                     LLVMTypeRef ToType) {
1765ef860a24SChandler Carruth   return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1766ef860a24SChandler Carruth                                              unwrap(ToType)));
1767ef860a24SChandler Carruth }
1768ef860a24SChandler Carruth 
LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1769ef860a24SChandler Carruth LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1770ef860a24SChandler Carruth                                      LLVMTypeRef ToType) {
1771ef860a24SChandler Carruth   return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1772ef860a24SChandler Carruth                                               unwrap(ToType)));
1773ef860a24SChandler Carruth }
1774ef860a24SChandler Carruth 
LLVMConstPointerCast(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1775ef860a24SChandler Carruth LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1776ef860a24SChandler Carruth                                   LLVMTypeRef ToType) {
1777ef860a24SChandler Carruth   return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1778ef860a24SChandler Carruth                                            unwrap(ToType)));
1779ef860a24SChandler Carruth }
1780ef860a24SChandler Carruth 
LLVMConstIntCast(LLVMValueRef ConstantVal,LLVMTypeRef ToType,LLVMBool isSigned)1781ef860a24SChandler Carruth LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1782ef860a24SChandler Carruth                               LLVMBool isSigned) {
1783ef860a24SChandler Carruth   return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1784ef860a24SChandler Carruth                                            unwrap(ToType), isSigned));
1785ef860a24SChandler Carruth }
1786ef860a24SChandler Carruth 
LLVMConstFPCast(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1787ef860a24SChandler Carruth LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1788ef860a24SChandler Carruth   return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1789ef860a24SChandler Carruth                                       unwrap(ToType)));
1790ef860a24SChandler Carruth }
1791ef860a24SChandler Carruth 
LLVMConstSelect(LLVMValueRef ConstantCondition,LLVMValueRef ConstantIfTrue,LLVMValueRef ConstantIfFalse)1792ef860a24SChandler Carruth LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1793ef860a24SChandler Carruth                              LLVMValueRef ConstantIfTrue,
1794ef860a24SChandler Carruth                              LLVMValueRef ConstantIfFalse) {
1795ef860a24SChandler Carruth   return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1796ef860a24SChandler Carruth                                       unwrap<Constant>(ConstantIfTrue),
1797ef860a24SChandler Carruth                                       unwrap<Constant>(ConstantIfFalse)));
1798ef860a24SChandler Carruth }
1799ef860a24SChandler Carruth 
LLVMConstExtractElement(LLVMValueRef VectorConstant,LLVMValueRef IndexConstant)1800ef860a24SChandler Carruth LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1801ef860a24SChandler Carruth                                      LLVMValueRef IndexConstant) {
1802ef860a24SChandler Carruth   return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1803ef860a24SChandler Carruth                                               unwrap<Constant>(IndexConstant)));
1804ef860a24SChandler Carruth }
1805ef860a24SChandler Carruth 
LLVMConstInsertElement(LLVMValueRef VectorConstant,LLVMValueRef ElementValueConstant,LLVMValueRef IndexConstant)1806ef860a24SChandler Carruth LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1807ef860a24SChandler Carruth                                     LLVMValueRef ElementValueConstant,
1808ef860a24SChandler Carruth                                     LLVMValueRef IndexConstant) {
1809ef860a24SChandler Carruth   return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1810ef860a24SChandler Carruth                                          unwrap<Constant>(ElementValueConstant),
1811ef860a24SChandler Carruth                                              unwrap<Constant>(IndexConstant)));
1812ef860a24SChandler Carruth }
1813ef860a24SChandler Carruth 
LLVMConstShuffleVector(LLVMValueRef VectorAConstant,LLVMValueRef VectorBConstant,LLVMValueRef MaskConstant)1814ef860a24SChandler Carruth LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1815ef860a24SChandler Carruth                                     LLVMValueRef VectorBConstant,
1816ef860a24SChandler Carruth                                     LLVMValueRef MaskConstant) {
18171ee6ec2bSEli Friedman   SmallVector<int, 16> IntMask;
18181ee6ec2bSEli Friedman   ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask);
1819ef860a24SChandler Carruth   return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1820ef860a24SChandler Carruth                                              unwrap<Constant>(VectorBConstant),
18211ee6ec2bSEli Friedman                                              IntMask));
1822ef860a24SChandler Carruth }
1823ef860a24SChandler Carruth 
LLVMConstInlineAsm(LLVMTypeRef Ty,const char * AsmString,const char * Constraints,LLVMBool HasSideEffects,LLVMBool IsAlignStack)1824ef860a24SChandler Carruth LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1825ef860a24SChandler Carruth                                 const char *Constraints,
1826ef860a24SChandler Carruth                                 LLVMBool HasSideEffects,
1827ef860a24SChandler Carruth                                 LLVMBool IsAlignStack) {
1828ef860a24SChandler Carruth   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1829ef860a24SChandler Carruth                              Constraints, HasSideEffects, IsAlignStack));
1830ef860a24SChandler Carruth }
1831ef860a24SChandler Carruth 
LLVMBlockAddress(LLVMValueRef F,LLVMBasicBlockRef BB)1832ef860a24SChandler Carruth LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1833ef860a24SChandler Carruth   return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1834ef860a24SChandler Carruth }
1835ef860a24SChandler Carruth 
1836ef860a24SChandler Carruth /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1837ef860a24SChandler Carruth 
LLVMGetGlobalParent(LLVMValueRef Global)1838ef860a24SChandler Carruth LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1839ef860a24SChandler Carruth   return wrap(unwrap<GlobalValue>(Global)->getParent());
1840ef860a24SChandler Carruth }
1841ef860a24SChandler Carruth 
LLVMIsDeclaration(LLVMValueRef Global)1842ef860a24SChandler Carruth LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1843ef860a24SChandler Carruth   return unwrap<GlobalValue>(Global)->isDeclaration();
1844ef860a24SChandler Carruth }
1845ef860a24SChandler Carruth 
LLVMGetLinkage(LLVMValueRef Global)1846ef860a24SChandler Carruth LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1847ef860a24SChandler Carruth   switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1848ef860a24SChandler Carruth   case GlobalValue::ExternalLinkage:
1849ef860a24SChandler Carruth     return LLVMExternalLinkage;
1850ef860a24SChandler Carruth   case GlobalValue::AvailableExternallyLinkage:
1851ef860a24SChandler Carruth     return LLVMAvailableExternallyLinkage;
1852ef860a24SChandler Carruth   case GlobalValue::LinkOnceAnyLinkage:
1853ef860a24SChandler Carruth     return LLVMLinkOnceAnyLinkage;
1854ef860a24SChandler Carruth   case GlobalValue::LinkOnceODRLinkage:
1855ef860a24SChandler Carruth     return LLVMLinkOnceODRLinkage;
1856ef860a24SChandler Carruth   case GlobalValue::WeakAnyLinkage:
1857ef860a24SChandler Carruth     return LLVMWeakAnyLinkage;
1858ef860a24SChandler Carruth   case GlobalValue::WeakODRLinkage:
1859ef860a24SChandler Carruth     return LLVMWeakODRLinkage;
1860ef860a24SChandler Carruth   case GlobalValue::AppendingLinkage:
1861ef860a24SChandler Carruth     return LLVMAppendingLinkage;
1862ef860a24SChandler Carruth   case GlobalValue::InternalLinkage:
1863ef860a24SChandler Carruth     return LLVMInternalLinkage;
1864ef860a24SChandler Carruth   case GlobalValue::PrivateLinkage:
1865ef860a24SChandler Carruth     return LLVMPrivateLinkage;
1866ef860a24SChandler Carruth   case GlobalValue::ExternalWeakLinkage:
1867ef860a24SChandler Carruth     return LLVMExternalWeakLinkage;
1868ef860a24SChandler Carruth   case GlobalValue::CommonLinkage:
1869ef860a24SChandler Carruth     return LLVMCommonLinkage;
1870ef860a24SChandler Carruth   }
1871ef860a24SChandler Carruth 
1872ef860a24SChandler Carruth   llvm_unreachable("Invalid GlobalValue linkage!");
1873ef860a24SChandler Carruth }
1874ef860a24SChandler Carruth 
LLVMSetLinkage(LLVMValueRef Global,LLVMLinkage Linkage)1875ef860a24SChandler Carruth void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1876ef860a24SChandler Carruth   GlobalValue *GV = unwrap<GlobalValue>(Global);
1877ef860a24SChandler Carruth 
1878ef860a24SChandler Carruth   switch (Linkage) {
1879ef860a24SChandler Carruth   case LLVMExternalLinkage:
1880ef860a24SChandler Carruth     GV->setLinkage(GlobalValue::ExternalLinkage);
1881ef860a24SChandler Carruth     break;
1882ef860a24SChandler Carruth   case LLVMAvailableExternallyLinkage:
1883ef860a24SChandler Carruth     GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1884ef860a24SChandler Carruth     break;
1885ef860a24SChandler Carruth   case LLVMLinkOnceAnyLinkage:
1886ef860a24SChandler Carruth     GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1887ef860a24SChandler Carruth     break;
1888ef860a24SChandler Carruth   case LLVMLinkOnceODRLinkage:
1889ef860a24SChandler Carruth     GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1890ef860a24SChandler Carruth     break;
1891ef860a24SChandler Carruth   case LLVMLinkOnceODRAutoHideLinkage:
1892d34e60caSNicola Zaghen     LLVM_DEBUG(
1893d34e60caSNicola Zaghen         errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1894716e7405SRafael Espindola                   "longer supported.");
1895ef860a24SChandler Carruth     break;
1896ef860a24SChandler Carruth   case LLVMWeakAnyLinkage:
1897ef860a24SChandler Carruth     GV->setLinkage(GlobalValue::WeakAnyLinkage);
1898ef860a24SChandler Carruth     break;
1899ef860a24SChandler Carruth   case LLVMWeakODRLinkage:
1900ef860a24SChandler Carruth     GV->setLinkage(GlobalValue::WeakODRLinkage);
1901ef860a24SChandler Carruth     break;
1902ef860a24SChandler Carruth   case LLVMAppendingLinkage:
1903ef860a24SChandler Carruth     GV->setLinkage(GlobalValue::AppendingLinkage);
1904ef860a24SChandler Carruth     break;
1905ef860a24SChandler Carruth   case LLVMInternalLinkage:
1906ef860a24SChandler Carruth     GV->setLinkage(GlobalValue::InternalLinkage);
1907ef860a24SChandler Carruth     break;
1908ef860a24SChandler Carruth   case LLVMPrivateLinkage:
1909ef860a24SChandler Carruth     GV->setLinkage(GlobalValue::PrivateLinkage);
1910ef860a24SChandler Carruth     break;
1911ef860a24SChandler Carruth   case LLVMLinkerPrivateLinkage:
19122fb5bc33SRafael Espindola     GV->setLinkage(GlobalValue::PrivateLinkage);
1913ef860a24SChandler Carruth     break;
1914ef860a24SChandler Carruth   case LLVMLinkerPrivateWeakLinkage:
19152fb5bc33SRafael Espindola     GV->setLinkage(GlobalValue::PrivateLinkage);
1916ef860a24SChandler Carruth     break;
1917ef860a24SChandler Carruth   case LLVMDLLImportLinkage:
1918d34e60caSNicola Zaghen     LLVM_DEBUG(
1919d34e60caSNicola Zaghen         errs()
19207157bb76SNico Rieck         << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1921ef860a24SChandler Carruth     break;
1922ef860a24SChandler Carruth   case LLVMDLLExportLinkage:
1923d34e60caSNicola Zaghen     LLVM_DEBUG(
1924d34e60caSNicola Zaghen         errs()
19257157bb76SNico Rieck         << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1926ef860a24SChandler Carruth     break;
1927ef860a24SChandler Carruth   case LLVMExternalWeakLinkage:
1928ef860a24SChandler Carruth     GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1929ef860a24SChandler Carruth     break;
1930ef860a24SChandler Carruth   case LLVMGhostLinkage:
1931d34e60caSNicola Zaghen     LLVM_DEBUG(
1932d34e60caSNicola Zaghen         errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1933ef860a24SChandler Carruth     break;
1934ef860a24SChandler Carruth   case LLVMCommonLinkage:
1935ef860a24SChandler Carruth     GV->setLinkage(GlobalValue::CommonLinkage);
1936ef860a24SChandler Carruth     break;
1937ef860a24SChandler Carruth   }
1938ef860a24SChandler Carruth }
1939ef860a24SChandler Carruth 
LLVMGetSection(LLVMValueRef Global)1940ef860a24SChandler Carruth const char *LLVMGetSection(LLVMValueRef Global) {
194183658d6eSRafael Espindola   // Using .data() is safe because of how GlobalObject::setSection is
194283658d6eSRafael Espindola   // implemented.
194383658d6eSRafael Espindola   return unwrap<GlobalValue>(Global)->getSection().data();
1944ef860a24SChandler Carruth }
1945ef860a24SChandler Carruth 
LLVMSetSection(LLVMValueRef Global,const char * Section)1946ef860a24SChandler Carruth void LLVMSetSection(LLVMValueRef Global, const char *Section) {
194799e05cf1SRafael Espindola   unwrap<GlobalObject>(Global)->setSection(Section);
1948ef860a24SChandler Carruth }
1949ef860a24SChandler Carruth 
LLVMGetVisibility(LLVMValueRef Global)1950ef860a24SChandler Carruth LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1951ef860a24SChandler Carruth   return static_cast<LLVMVisibility>(
1952ef860a24SChandler Carruth     unwrap<GlobalValue>(Global)->getVisibility());
1953ef860a24SChandler Carruth }
1954ef860a24SChandler Carruth 
LLVMSetVisibility(LLVMValueRef Global,LLVMVisibility Viz)1955ef860a24SChandler Carruth void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1956ef860a24SChandler Carruth   unwrap<GlobalValue>(Global)
1957ef860a24SChandler Carruth     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1958ef860a24SChandler Carruth }
1959ef860a24SChandler Carruth 
LLVMGetDLLStorageClass(LLVMValueRef Global)19602fae26faSReid Kleckner LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
19612fae26faSReid Kleckner   return static_cast<LLVMDLLStorageClass>(
19622fae26faSReid Kleckner       unwrap<GlobalValue>(Global)->getDLLStorageClass());
19632fae26faSReid Kleckner }
19642fae26faSReid Kleckner 
LLVMSetDLLStorageClass(LLVMValueRef Global,LLVMDLLStorageClass Class)19652fae26faSReid Kleckner void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
19662fae26faSReid Kleckner   unwrap<GlobalValue>(Global)->setDLLStorageClass(
19672fae26faSReid Kleckner       static_cast<GlobalValue::DLLStorageClassTypes>(Class));
19682fae26faSReid Kleckner }
19692fae26faSReid Kleckner 
LLVMGetUnnamedAddress(LLVMValueRef Global)19704bb481b2SRobert Widmann LLVMUnnamedAddr LLVMGetUnnamedAddress(LLVMValueRef Global) {
19714bb481b2SRobert Widmann   switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
19724bb481b2SRobert Widmann   case GlobalVariable::UnnamedAddr::None:
19734bb481b2SRobert Widmann     return LLVMNoUnnamedAddr;
19744bb481b2SRobert Widmann   case GlobalVariable::UnnamedAddr::Local:
19754bb481b2SRobert Widmann     return LLVMLocalUnnamedAddr;
19764bb481b2SRobert Widmann   case GlobalVariable::UnnamedAddr::Global:
19774bb481b2SRobert Widmann     return LLVMGlobalUnnamedAddr;
19784bb481b2SRobert Widmann   }
1979f0ccaae5SSimon Pilgrim   llvm_unreachable("Unknown UnnamedAddr kind!");
19804bb481b2SRobert Widmann }
19814bb481b2SRobert Widmann 
LLVMSetUnnamedAddress(LLVMValueRef Global,LLVMUnnamedAddr UnnamedAddr)19824bb481b2SRobert Widmann void LLVMSetUnnamedAddress(LLVMValueRef Global, LLVMUnnamedAddr UnnamedAddr) {
19834bb481b2SRobert Widmann   GlobalValue *GV = unwrap<GlobalValue>(Global);
19844bb481b2SRobert Widmann 
19854bb481b2SRobert Widmann   switch (UnnamedAddr) {
19864bb481b2SRobert Widmann   case LLVMNoUnnamedAddr:
19874bb481b2SRobert Widmann     return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);
19884bb481b2SRobert Widmann   case LLVMLocalUnnamedAddr:
19894bb481b2SRobert Widmann     return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);
19904bb481b2SRobert Widmann   case LLVMGlobalUnnamedAddr:
19914bb481b2SRobert Widmann     return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);
19924bb481b2SRobert Widmann   }
19934bb481b2SRobert Widmann }
19944bb481b2SRobert Widmann 
LLVMHasUnnamedAddr(LLVMValueRef Global)1995ad96d012STim Northover LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
199696efdd61SPeter Collingbourne   return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
1997ad96d012STim Northover }
1998ad96d012STim Northover 
LLVMSetUnnamedAddr(LLVMValueRef Global,LLVMBool HasUnnamedAddr)1999ad96d012STim Northover void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
200096efdd61SPeter Collingbourne   unwrap<GlobalValue>(Global)->setUnnamedAddr(
200196efdd61SPeter Collingbourne       HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
200296efdd61SPeter Collingbourne                      : GlobalValue::UnnamedAddr::None);
2003ad96d012STim Northover }
2004ad96d012STim Northover 
LLVMGlobalGetValueType(LLVMValueRef Global)2005e63a12ccSRobert Widmann LLVMTypeRef LLVMGlobalGetValueType(LLVMValueRef Global) {
2006e63a12ccSRobert Widmann   return wrap(unwrap<GlobalValue>(Global)->getValueType());
2007e63a12ccSRobert Widmann }
2008e63a12ccSRobert Widmann 
2009213a63feSAnders Waldenborg /*--.. Operations on global variables, load and store instructions .........--*/
2010213a63feSAnders Waldenborg 
LLVMGetAlignment(LLVMValueRef V)2011213a63feSAnders Waldenborg unsigned LLVMGetAlignment(LLVMValueRef V) {
2012213a63feSAnders Waldenborg   Value *P = unwrap<Value>(V);
2013a2caa3b6SEli Friedman   if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
201440109fa1SGuillaume Chatelet     return GV->getAlign() ? GV->getAlign()->value() : 0;
20159f584e67SPeter Zotov   if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
201640109fa1SGuillaume Chatelet     return AI->getAlign().value();
2017213a63feSAnders Waldenborg   if (LoadInst *LI = dyn_cast<LoadInst>(P))
201840109fa1SGuillaume Chatelet     return LI->getAlign().value();
2019213a63feSAnders Waldenborg   if (StoreInst *SI = dyn_cast<StoreInst>(P))
202040109fa1SGuillaume Chatelet     return SI->getAlign().value();
20218bd8534aSJames Y Knight   if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
20228bd8534aSJames Y Knight     return RMWI->getAlign().value();
20238bd8534aSJames Y Knight   if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
20248bd8534aSJames Y Knight     return CXI->getAlign().value();
2025213a63feSAnders Waldenborg 
20269f584e67SPeter Zotov   llvm_unreachable(
20278bd8534aSJames Y Knight       "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
20288bd8534aSJames Y Knight       "and AtomicCmpXchgInst have alignment");
2029ef860a24SChandler Carruth }
2030ef860a24SChandler Carruth 
LLVMSetAlignment(LLVMValueRef V,unsigned Bytes)2031213a63feSAnders Waldenborg void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2032213a63feSAnders Waldenborg   Value *P = unwrap<Value>(V);
203399e05cf1SRafael Espindola   if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
20340e62011dSGuillaume Chatelet     GV->setAlignment(MaybeAlign(Bytes));
20359f584e67SPeter Zotov   else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
20364f04db4bSEli Friedman     AI->setAlignment(Align(Bytes));
2037213a63feSAnders Waldenborg   else if (LoadInst *LI = dyn_cast<LoadInst>(P))
2038accc6b55SEli Friedman     LI->setAlignment(Align(Bytes));
2039213a63feSAnders Waldenborg   else if (StoreInst *SI = dyn_cast<StoreInst>(P))
204011aa3707SEli Friedman     SI->setAlignment(Align(Bytes));
20418bd8534aSJames Y Knight   else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
20428bd8534aSJames Y Knight     RMWI->setAlignment(Align(Bytes));
20438bd8534aSJames Y Knight   else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
20448bd8534aSJames Y Knight     CXI->setAlignment(Align(Bytes));
2045a36a7825SAnders Waldenborg   else
20469f584e67SPeter Zotov     llvm_unreachable(
20478bd8534aSJames Y Knight         "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
20488bd8534aSJames Y Knight         "and AtomicCmpXchgInst have alignment");
2049ef860a24SChandler Carruth }
2050ef860a24SChandler Carruth 
LLVMGlobalCopyAllMetadata(LLVMValueRef Value,size_t * NumEntries)20519cba4eceSRobert Widmann LLVMValueMetadataEntry *LLVMGlobalCopyAllMetadata(LLVMValueRef Value,
20529cba4eceSRobert Widmann                                                   size_t *NumEntries) {
20539cba4eceSRobert Widmann   return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
20547975b8c3SSerge Pavlov     Entries.clear();
20559cba4eceSRobert Widmann     if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
20569cba4eceSRobert Widmann       Instr->getAllMetadata(Entries);
20579cba4eceSRobert Widmann     } else {
20589cba4eceSRobert Widmann       unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
20599cba4eceSRobert Widmann     }
20609cba4eceSRobert Widmann   });
20619cba4eceSRobert Widmann }
20629cba4eceSRobert Widmann 
LLVMValueMetadataEntriesGetKind(LLVMValueMetadataEntry * Entries,unsigned Index)20639cba4eceSRobert Widmann unsigned LLVMValueMetadataEntriesGetKind(LLVMValueMetadataEntry *Entries,
20649cba4eceSRobert Widmann                                          unsigned Index) {
20659cba4eceSRobert Widmann   LLVMOpaqueValueMetadataEntry MVE =
20669cba4eceSRobert Widmann       static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
20679cba4eceSRobert Widmann   return MVE.Kind;
20689cba4eceSRobert Widmann }
20699cba4eceSRobert Widmann 
20709cba4eceSRobert Widmann LLVMMetadataRef
LLVMValueMetadataEntriesGetMetadata(LLVMValueMetadataEntry * Entries,unsigned Index)20719cba4eceSRobert Widmann LLVMValueMetadataEntriesGetMetadata(LLVMValueMetadataEntry *Entries,
20729cba4eceSRobert Widmann                                     unsigned Index) {
20739cba4eceSRobert Widmann   LLVMOpaqueValueMetadataEntry MVE =
20749cba4eceSRobert Widmann       static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
20759cba4eceSRobert Widmann   return MVE.Metadata;
20769cba4eceSRobert Widmann }
20779cba4eceSRobert Widmann 
LLVMDisposeValueMetadataEntries(LLVMValueMetadataEntry * Entries)20789cba4eceSRobert Widmann void LLVMDisposeValueMetadataEntries(LLVMValueMetadataEntry *Entries) {
20799cba4eceSRobert Widmann   free(Entries);
20809cba4eceSRobert Widmann }
20819cba4eceSRobert Widmann 
LLVMGlobalSetMetadata(LLVMValueRef Global,unsigned Kind,LLVMMetadataRef MD)20829cba4eceSRobert Widmann void LLVMGlobalSetMetadata(LLVMValueRef Global, unsigned Kind,
20839cba4eceSRobert Widmann                            LLVMMetadataRef MD) {
2084d22ee946SRobert Widmann   unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
20859cba4eceSRobert Widmann }
20869cba4eceSRobert Widmann 
LLVMGlobalEraseMetadata(LLVMValueRef Global,unsigned Kind)20879cba4eceSRobert Widmann void LLVMGlobalEraseMetadata(LLVMValueRef Global, unsigned Kind) {
20889cba4eceSRobert Widmann   unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
20899cba4eceSRobert Widmann }
20909cba4eceSRobert Widmann 
LLVMGlobalClearMetadata(LLVMValueRef Global)20919cba4eceSRobert Widmann void LLVMGlobalClearMetadata(LLVMValueRef Global) {
20929cba4eceSRobert Widmann   unwrap<GlobalObject>(Global)->clearMetadata();
20939cba4eceSRobert Widmann }
20949cba4eceSRobert Widmann 
2095ef860a24SChandler Carruth /*--.. Operations on global variables ......................................--*/
2096ef860a24SChandler Carruth 
LLVMAddGlobal(LLVMModuleRef M,LLVMTypeRef Ty,const char * Name)2097ef860a24SChandler Carruth LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
2098ef860a24SChandler Carruth   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2099c620761cSCraig Topper                                  GlobalValue::ExternalLinkage, nullptr, Name));
2100ef860a24SChandler Carruth }
2101ef860a24SChandler Carruth 
LLVMAddGlobalInAddressSpace(LLVMModuleRef M,LLVMTypeRef Ty,const char * Name,unsigned AddressSpace)2102ef860a24SChandler Carruth LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
2103ef860a24SChandler Carruth                                          const char *Name,
2104ef860a24SChandler Carruth                                          unsigned AddressSpace) {
2105ef860a24SChandler Carruth   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2106c620761cSCraig Topper                                  GlobalValue::ExternalLinkage, nullptr, Name,
2107c620761cSCraig Topper                                  nullptr, GlobalVariable::NotThreadLocal,
2108c620761cSCraig Topper                                  AddressSpace));
2109ef860a24SChandler Carruth }
2110ef860a24SChandler Carruth 
LLVMGetNamedGlobal(LLVMModuleRef M,const char * Name)2111ef860a24SChandler Carruth LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
2112ef860a24SChandler Carruth   return wrap(unwrap(M)->getNamedGlobal(Name));
2113ef860a24SChandler Carruth }
2114ef860a24SChandler Carruth 
LLVMGetFirstGlobal(LLVMModuleRef M)2115ef860a24SChandler Carruth LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
2116ef860a24SChandler Carruth   Module *Mod = unwrap(M);
2117ef860a24SChandler Carruth   Module::global_iterator I = Mod->global_begin();
2118ef860a24SChandler Carruth   if (I == Mod->global_end())
2119c620761cSCraig Topper     return nullptr;
212052888a67SDuncan P. N. Exon Smith   return wrap(&*I);
2121ef860a24SChandler Carruth }
2122ef860a24SChandler Carruth 
LLVMGetLastGlobal(LLVMModuleRef M)2123ef860a24SChandler Carruth LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
2124ef860a24SChandler Carruth   Module *Mod = unwrap(M);
2125ef860a24SChandler Carruth   Module::global_iterator I = Mod->global_end();
2126ef860a24SChandler Carruth   if (I == Mod->global_begin())
2127c620761cSCraig Topper     return nullptr;
212852888a67SDuncan P. N. Exon Smith   return wrap(&*--I);
2129ef860a24SChandler Carruth }
2130ef860a24SChandler Carruth 
LLVMGetNextGlobal(LLVMValueRef GlobalVar)2131ef860a24SChandler Carruth LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
2132ef860a24SChandler Carruth   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
213352888a67SDuncan P. N. Exon Smith   Module::global_iterator I(GV);
2134ef860a24SChandler Carruth   if (++I == GV->getParent()->global_end())
2135c620761cSCraig Topper     return nullptr;
213652888a67SDuncan P. N. Exon Smith   return wrap(&*I);
2137ef860a24SChandler Carruth }
2138ef860a24SChandler Carruth 
LLVMGetPreviousGlobal(LLVMValueRef GlobalVar)2139ef860a24SChandler Carruth LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
2140ef860a24SChandler Carruth   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
214152888a67SDuncan P. N. Exon Smith   Module::global_iterator I(GV);
2142ef860a24SChandler Carruth   if (I == GV->getParent()->global_begin())
2143c620761cSCraig Topper     return nullptr;
214452888a67SDuncan P. N. Exon Smith   return wrap(&*--I);
2145ef860a24SChandler Carruth }
2146ef860a24SChandler Carruth 
LLVMDeleteGlobal(LLVMValueRef GlobalVar)2147ef860a24SChandler Carruth void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
2148ef860a24SChandler Carruth   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2149ef860a24SChandler Carruth }
2150ef860a24SChandler Carruth 
LLVMGetInitializer(LLVMValueRef GlobalVar)2151ef860a24SChandler Carruth LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
2152ef860a24SChandler Carruth   GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2153ef860a24SChandler Carruth   if ( !GV->hasInitializer() )
2154c620761cSCraig Topper     return nullptr;
2155ef860a24SChandler Carruth   return wrap(GV->getInitializer());
2156ef860a24SChandler Carruth }
2157ef860a24SChandler Carruth 
LLVMSetInitializer(LLVMValueRef GlobalVar,LLVMValueRef ConstantVal)2158ef860a24SChandler Carruth void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
2159ef860a24SChandler Carruth   unwrap<GlobalVariable>(GlobalVar)
2160ef860a24SChandler Carruth     ->setInitializer(unwrap<Constant>(ConstantVal));
2161ef860a24SChandler Carruth }
2162ef860a24SChandler Carruth 
LLVMIsThreadLocal(LLVMValueRef GlobalVar)2163ef860a24SChandler Carruth LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
2164ef860a24SChandler Carruth   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2165ef860a24SChandler Carruth }
2166ef860a24SChandler Carruth 
LLVMSetThreadLocal(LLVMValueRef GlobalVar,LLVMBool IsThreadLocal)2167ef860a24SChandler Carruth void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
2168ef860a24SChandler Carruth   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2169ef860a24SChandler Carruth }
2170ef860a24SChandler Carruth 
LLVMIsGlobalConstant(LLVMValueRef GlobalVar)2171ef860a24SChandler Carruth LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
2172ef860a24SChandler Carruth   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2173ef860a24SChandler Carruth }
2174ef860a24SChandler Carruth 
LLVMSetGlobalConstant(LLVMValueRef GlobalVar,LLVMBool IsConstant)2175ef860a24SChandler Carruth void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
2176ef860a24SChandler Carruth   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2177ef860a24SChandler Carruth }
2178ef860a24SChandler Carruth 
LLVMGetThreadLocalMode(LLVMValueRef GlobalVar)21795ff71205SHans Wennborg LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
21805ff71205SHans Wennborg   switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
21815ff71205SHans Wennborg   case GlobalVariable::NotThreadLocal:
21825ff71205SHans Wennborg     return LLVMNotThreadLocal;
21835ff71205SHans Wennborg   case GlobalVariable::GeneralDynamicTLSModel:
21845ff71205SHans Wennborg     return LLVMGeneralDynamicTLSModel;
21855ff71205SHans Wennborg   case GlobalVariable::LocalDynamicTLSModel:
21865ff71205SHans Wennborg     return LLVMLocalDynamicTLSModel;
21875ff71205SHans Wennborg   case GlobalVariable::InitialExecTLSModel:
21885ff71205SHans Wennborg     return LLVMInitialExecTLSModel;
21895ff71205SHans Wennborg   case GlobalVariable::LocalExecTLSModel:
21905ff71205SHans Wennborg     return LLVMLocalExecTLSModel;
21915ff71205SHans Wennborg   }
21925ff71205SHans Wennborg 
21935ff71205SHans Wennborg   llvm_unreachable("Invalid GlobalVariable thread local mode");
21945ff71205SHans Wennborg }
21955ff71205SHans Wennborg 
LLVMSetThreadLocalMode(LLVMValueRef GlobalVar,LLVMThreadLocalMode Mode)21965ff71205SHans Wennborg void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
21975ff71205SHans Wennborg   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
21985ff71205SHans Wennborg 
21995ff71205SHans Wennborg   switch (Mode) {
22005ff71205SHans Wennborg   case LLVMNotThreadLocal:
22015ff71205SHans Wennborg     GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
22025ff71205SHans Wennborg     break;
22035ff71205SHans Wennborg   case LLVMGeneralDynamicTLSModel:
22045ff71205SHans Wennborg     GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
22055ff71205SHans Wennborg     break;
22065ff71205SHans Wennborg   case LLVMLocalDynamicTLSModel:
22075ff71205SHans Wennborg     GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
22085ff71205SHans Wennborg     break;
22095ff71205SHans Wennborg   case LLVMInitialExecTLSModel:
22105ff71205SHans Wennborg     GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
22115ff71205SHans Wennborg     break;
22125ff71205SHans Wennborg   case LLVMLocalExecTLSModel:
22135ff71205SHans Wennborg     GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
22145ff71205SHans Wennborg     break;
22155ff71205SHans Wennborg   }
22165ff71205SHans Wennborg }
22175ff71205SHans Wennborg 
LLVMIsExternallyInitialized(LLVMValueRef GlobalVar)22185ff71205SHans Wennborg LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
22195ff71205SHans Wennborg   return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
22205ff71205SHans Wennborg }
22215ff71205SHans Wennborg 
LLVMSetExternallyInitialized(LLVMValueRef GlobalVar,LLVMBool IsExtInit)22225ff71205SHans Wennborg void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
22235ff71205SHans Wennborg   unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
22245ff71205SHans Wennborg }
22255ff71205SHans Wennborg 
2226ef860a24SChandler Carruth /*--.. Operations on aliases ......................................--*/
2227ef860a24SChandler Carruth 
LLVMAddAlias(LLVMModuleRef M,LLVMTypeRef Ty,LLVMValueRef Aliasee,const char * Name)2228ef860a24SChandler Carruth LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
2229ef860a24SChandler Carruth                           const char *Name) {
22304fe0094fSRafael Espindola   auto *PTy = cast<PointerType>(unwrap(Ty));
2231d29e3192SNikita Popov   return wrap(GlobalAlias::create(PTy->getNonOpaquePointerElementType(),
2232d29e3192SNikita Popov                                   PTy->getAddressSpace(),
223316a2f3e3SDavid Blaikie                                   GlobalValue::ExternalLinkage, Name,
2234993502eaSRafael Espindola                                   unwrap<Constant>(Aliasee), unwrap(M)));
2235ef860a24SChandler Carruth }
2236ef860a24SChandler Carruth 
LLVMAddAlias2(LLVMModuleRef M,LLVMTypeRef ValueTy,unsigned AddrSpace,LLVMValueRef Aliasee,const char * Name)223755d392ccSNikita Popov LLVMValueRef LLVMAddAlias2(LLVMModuleRef M, LLVMTypeRef ValueTy,
223855d392ccSNikita Popov                            unsigned AddrSpace, LLVMValueRef Aliasee,
223955d392ccSNikita Popov                            const char *Name) {
224055d392ccSNikita Popov   return wrap(GlobalAlias::create(unwrap(ValueTy), AddrSpace,
224155d392ccSNikita Popov                                   GlobalValue::ExternalLinkage, Name,
224255d392ccSNikita Popov                                   unwrap<Constant>(Aliasee), unwrap(M)));
224355d392ccSNikita Popov }
224455d392ccSNikita Popov 
LLVMGetNamedGlobalAlias(LLVMModuleRef M,const char * Name,size_t NameLen)2245360d6e35SRobert Widmann LLVMValueRef LLVMGetNamedGlobalAlias(LLVMModuleRef M,
2246360d6e35SRobert Widmann                                      const char *Name, size_t NameLen) {
2247bfed654eSJakob Bornecrantz   return wrap(unwrap(M)->getNamedAlias(StringRef(Name, NameLen)));
2248360d6e35SRobert Widmann }
2249360d6e35SRobert Widmann 
LLVMGetFirstGlobalAlias(LLVMModuleRef M)2250360d6e35SRobert Widmann LLVMValueRef LLVMGetFirstGlobalAlias(LLVMModuleRef M) {
2251360d6e35SRobert Widmann   Module *Mod = unwrap(M);
2252360d6e35SRobert Widmann   Module::alias_iterator I = Mod->alias_begin();
2253360d6e35SRobert Widmann   if (I == Mod->alias_end())
2254360d6e35SRobert Widmann     return nullptr;
2255360d6e35SRobert Widmann   return wrap(&*I);
2256360d6e35SRobert Widmann }
2257360d6e35SRobert Widmann 
LLVMGetLastGlobalAlias(LLVMModuleRef M)2258360d6e35SRobert Widmann LLVMValueRef LLVMGetLastGlobalAlias(LLVMModuleRef M) {
2259360d6e35SRobert Widmann   Module *Mod = unwrap(M);
2260360d6e35SRobert Widmann   Module::alias_iterator I = Mod->alias_end();
2261360d6e35SRobert Widmann   if (I == Mod->alias_begin())
2262360d6e35SRobert Widmann     return nullptr;
2263360d6e35SRobert Widmann   return wrap(&*--I);
2264360d6e35SRobert Widmann }
2265360d6e35SRobert Widmann 
LLVMGetNextGlobalAlias(LLVMValueRef GA)2266360d6e35SRobert Widmann LLVMValueRef LLVMGetNextGlobalAlias(LLVMValueRef GA) {
2267360d6e35SRobert Widmann   GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2268360d6e35SRobert Widmann   Module::alias_iterator I(Alias);
2269360d6e35SRobert Widmann   if (++I == Alias->getParent()->alias_end())
2270360d6e35SRobert Widmann     return nullptr;
2271360d6e35SRobert Widmann   return wrap(&*I);
2272360d6e35SRobert Widmann }
2273360d6e35SRobert Widmann 
LLVMGetPreviousGlobalAlias(LLVMValueRef GA)2274360d6e35SRobert Widmann LLVMValueRef LLVMGetPreviousGlobalAlias(LLVMValueRef GA) {
2275360d6e35SRobert Widmann   GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2276360d6e35SRobert Widmann   Module::alias_iterator I(Alias);
2277360d6e35SRobert Widmann   if (I == Alias->getParent()->alias_begin())
2278360d6e35SRobert Widmann     return nullptr;
2279360d6e35SRobert Widmann   return wrap(&*--I);
2280360d6e35SRobert Widmann }
2281360d6e35SRobert Widmann 
LLVMAliasGetAliasee(LLVMValueRef Alias)2282360d6e35SRobert Widmann LLVMValueRef LLVMAliasGetAliasee(LLVMValueRef Alias) {
2283360d6e35SRobert Widmann   return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2284360d6e35SRobert Widmann }
2285360d6e35SRobert Widmann 
LLVMAliasSetAliasee(LLVMValueRef Alias,LLVMValueRef Aliasee)2286360d6e35SRobert Widmann void LLVMAliasSetAliasee(LLVMValueRef Alias, LLVMValueRef Aliasee) {
2287360d6e35SRobert Widmann   unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2288360d6e35SRobert Widmann }
2289360d6e35SRobert Widmann 
2290ef860a24SChandler Carruth /*--.. Operations on functions .............................................--*/
2291ef860a24SChandler Carruth 
LLVMAddFunction(LLVMModuleRef M,const char * Name,LLVMTypeRef FunctionTy)2292ef860a24SChandler Carruth LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
2293ef860a24SChandler Carruth                              LLVMTypeRef FunctionTy) {
2294ef860a24SChandler Carruth   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2295ef860a24SChandler Carruth                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
2296ef860a24SChandler Carruth }
2297ef860a24SChandler Carruth 
LLVMGetNamedFunction(LLVMModuleRef M,const char * Name)2298ef860a24SChandler Carruth LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
2299ef860a24SChandler Carruth   return wrap(unwrap(M)->getFunction(Name));
2300ef860a24SChandler Carruth }
2301ef860a24SChandler Carruth 
LLVMGetFirstFunction(LLVMModuleRef M)2302ef860a24SChandler Carruth LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
2303ef860a24SChandler Carruth   Module *Mod = unwrap(M);
2304ef860a24SChandler Carruth   Module::iterator I = Mod->begin();
2305ef860a24SChandler Carruth   if (I == Mod->end())
2306c620761cSCraig Topper     return nullptr;
230752888a67SDuncan P. N. Exon Smith   return wrap(&*I);
2308ef860a24SChandler Carruth }
2309ef860a24SChandler Carruth 
LLVMGetLastFunction(LLVMModuleRef M)2310ef860a24SChandler Carruth LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
2311ef860a24SChandler Carruth   Module *Mod = unwrap(M);
2312ef860a24SChandler Carruth   Module::iterator I = Mod->end();
2313ef860a24SChandler Carruth   if (I == Mod->begin())
2314c620761cSCraig Topper     return nullptr;
231552888a67SDuncan P. N. Exon Smith   return wrap(&*--I);
2316ef860a24SChandler Carruth }
2317ef860a24SChandler Carruth 
LLVMGetNextFunction(LLVMValueRef Fn)2318ef860a24SChandler Carruth LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
2319ef860a24SChandler Carruth   Function *Func = unwrap<Function>(Fn);
232052888a67SDuncan P. N. Exon Smith   Module::iterator I(Func);
2321ef860a24SChandler Carruth   if (++I == Func->getParent()->end())
2322c620761cSCraig Topper     return nullptr;
232352888a67SDuncan P. N. Exon Smith   return wrap(&*I);
2324ef860a24SChandler Carruth }
2325ef860a24SChandler Carruth 
LLVMGetPreviousFunction(LLVMValueRef Fn)2326ef860a24SChandler Carruth LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
2327ef860a24SChandler Carruth   Function *Func = unwrap<Function>(Fn);
232852888a67SDuncan P. N. Exon Smith   Module::iterator I(Func);
2329ef860a24SChandler Carruth   if (I == Func->getParent()->begin())
2330c620761cSCraig Topper     return nullptr;
233152888a67SDuncan P. N. Exon Smith   return wrap(&*--I);
2332ef860a24SChandler Carruth }
2333ef860a24SChandler Carruth 
LLVMDeleteFunction(LLVMValueRef Fn)2334ef860a24SChandler Carruth void LLVMDeleteFunction(LLVMValueRef Fn) {
2335ef860a24SChandler Carruth   unwrap<Function>(Fn)->eraseFromParent();
2336ef860a24SChandler Carruth }
2337ef860a24SChandler Carruth 
LLVMHasPersonalityFn(LLVMValueRef Fn)2338e39e8530SAmaury Sechet LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
2339e39e8530SAmaury Sechet   return unwrap<Function>(Fn)->hasPersonalityFn();
2340e39e8530SAmaury Sechet }
2341e39e8530SAmaury Sechet 
LLVMGetPersonalityFn(LLVMValueRef Fn)23423bdfc1cdSAndrew Wilkins LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
23433bdfc1cdSAndrew Wilkins   return wrap(unwrap<Function>(Fn)->getPersonalityFn());
23443bdfc1cdSAndrew Wilkins }
23453bdfc1cdSAndrew Wilkins 
LLVMSetPersonalityFn(LLVMValueRef Fn,LLVMValueRef PersonalityFn)23463bdfc1cdSAndrew Wilkins void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
23473bdfc1cdSAndrew Wilkins   unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
23483bdfc1cdSAndrew Wilkins }
23493bdfc1cdSAndrew Wilkins 
LLVMGetIntrinsicID(LLVMValueRef Fn)2350ef860a24SChandler Carruth unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
2351ef860a24SChandler Carruth   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2352ef860a24SChandler Carruth     return F->getIntrinsicID();
2353ef860a24SChandler Carruth   return 0;
2354ef860a24SChandler Carruth }
2355ef860a24SChandler Carruth 
llvm_map_to_intrinsic_id(unsigned ID)2356d36f3b0fSRobert Widmann static Intrinsic::ID llvm_map_to_intrinsic_id(unsigned ID) {
2357d36f3b0fSRobert Widmann   assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2358d36f3b0fSRobert Widmann   return llvm::Intrinsic::ID(ID);
2359d36f3b0fSRobert Widmann }
2360d36f3b0fSRobert Widmann 
LLVMGetIntrinsicDeclaration(LLVMModuleRef Mod,unsigned ID,LLVMTypeRef * ParamTypes,size_t ParamCount)2361d36f3b0fSRobert Widmann LLVMValueRef LLVMGetIntrinsicDeclaration(LLVMModuleRef Mod,
2362d36f3b0fSRobert Widmann                                          unsigned ID,
2363d36f3b0fSRobert Widmann                                          LLVMTypeRef *ParamTypes,
2364d36f3b0fSRobert Widmann                                          size_t ParamCount) {
2365d36f3b0fSRobert Widmann   ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2366d36f3b0fSRobert Widmann   auto IID = llvm_map_to_intrinsic_id(ID);
2367d36f3b0fSRobert Widmann   return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2368d36f3b0fSRobert Widmann }
2369d36f3b0fSRobert Widmann 
LLVMIntrinsicGetName(unsigned ID,size_t * NameLength)2370d36f3b0fSRobert Widmann const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2371d36f3b0fSRobert Widmann   auto IID = llvm_map_to_intrinsic_id(ID);
2372d36f3b0fSRobert Widmann   auto Str = llvm::Intrinsic::getName(IID);
2373d36f3b0fSRobert Widmann   *NameLength = Str.size();
2374d36f3b0fSRobert Widmann   return Str.data();
2375d36f3b0fSRobert Widmann }
2376d36f3b0fSRobert Widmann 
LLVMIntrinsicGetType(LLVMContextRef Ctx,unsigned ID,LLVMTypeRef * ParamTypes,size_t ParamCount)2377d36f3b0fSRobert Widmann LLVMTypeRef LLVMIntrinsicGetType(LLVMContextRef Ctx, unsigned ID,
2378d36f3b0fSRobert Widmann                                  LLVMTypeRef *ParamTypes, size_t ParamCount) {
2379d36f3b0fSRobert Widmann   auto IID = llvm_map_to_intrinsic_id(ID);
2380d36f3b0fSRobert Widmann   ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2381d36f3b0fSRobert Widmann   return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2382d36f3b0fSRobert Widmann }
2383d36f3b0fSRobert Widmann 
LLVMIntrinsicCopyOverloadedName(unsigned ID,LLVMTypeRef * ParamTypes,size_t ParamCount,size_t * NameLength)2384d36f3b0fSRobert Widmann const char *LLVMIntrinsicCopyOverloadedName(unsigned ID,
2385d36f3b0fSRobert Widmann                                             LLVMTypeRef *ParamTypes,
2386d36f3b0fSRobert Widmann                                             size_t ParamCount,
2387d36f3b0fSRobert Widmann                                             size_t *NameLength) {
2388d36f3b0fSRobert Widmann   auto IID = llvm_map_to_intrinsic_id(ID);
2389d36f3b0fSRobert Widmann   ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2390bb8ce25eSJeroen Dobbelaere   auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys);
2391bb8ce25eSJeroen Dobbelaere   *NameLength = Str.length();
2392bb8ce25eSJeroen Dobbelaere   return strdup(Str.c_str());
2393bb8ce25eSJeroen Dobbelaere }
2394bb8ce25eSJeroen Dobbelaere 
LLVMIntrinsicCopyOverloadedName2(LLVMModuleRef Mod,unsigned ID,LLVMTypeRef * ParamTypes,size_t ParamCount,size_t * NameLength)2395bb8ce25eSJeroen Dobbelaere const char *LLVMIntrinsicCopyOverloadedName2(LLVMModuleRef Mod, unsigned ID,
2396bb8ce25eSJeroen Dobbelaere                                              LLVMTypeRef *ParamTypes,
2397bb8ce25eSJeroen Dobbelaere                                              size_t ParamCount,
2398bb8ce25eSJeroen Dobbelaere                                              size_t *NameLength) {
2399bb8ce25eSJeroen Dobbelaere   auto IID = llvm_map_to_intrinsic_id(ID);
2400bb8ce25eSJeroen Dobbelaere   ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount);
2401bb8ce25eSJeroen Dobbelaere   auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod));
2402d36f3b0fSRobert Widmann   *NameLength = Str.length();
24036c7073f2SRobert Widmann   return strdup(Str.c_str());
2404d36f3b0fSRobert Widmann }
2405d36f3b0fSRobert Widmann 
LLVMLookupIntrinsicID(const char * Name,size_t NameLen)24069d94a684SRobert Widmann unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
24079d94a684SRobert Widmann   return Function::lookupIntrinsicID({Name, NameLen});
24089d94a684SRobert Widmann }
24099d94a684SRobert Widmann 
LLVMIntrinsicIsOverloaded(unsigned ID)2410d36f3b0fSRobert Widmann LLVMBool LLVMIntrinsicIsOverloaded(unsigned ID) {
2411d36f3b0fSRobert Widmann   auto IID = llvm_map_to_intrinsic_id(ID);
2412d36f3b0fSRobert Widmann   return llvm::Intrinsic::isOverloaded(IID);
2413d36f3b0fSRobert Widmann }
2414d36f3b0fSRobert Widmann 
LLVMGetFunctionCallConv(LLVMValueRef Fn)2415ef860a24SChandler Carruth unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
2416ef860a24SChandler Carruth   return unwrap<Function>(Fn)->getCallingConv();
2417ef860a24SChandler Carruth }
2418ef860a24SChandler Carruth 
LLVMSetFunctionCallConv(LLVMValueRef Fn,unsigned CC)2419ef860a24SChandler Carruth void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
2420ef860a24SChandler Carruth   return unwrap<Function>(Fn)->setCallingConv(
2421ef860a24SChandler Carruth     static_cast<CallingConv::ID>(CC));
2422ef860a24SChandler Carruth }
2423ef860a24SChandler Carruth 
LLVMGetGC(LLVMValueRef Fn)2424ef860a24SChandler Carruth const char *LLVMGetGC(LLVMValueRef Fn) {
2425ef860a24SChandler Carruth   Function *F = unwrap<Function>(Fn);
2426599ebf27SMehdi Amini   return F->hasGC()? F->getGC().c_str() : nullptr;
2427ef860a24SChandler Carruth }
2428ef860a24SChandler Carruth 
LLVMSetGC(LLVMValueRef Fn,const char * GC)2429ef860a24SChandler Carruth void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2430ef860a24SChandler Carruth   Function *F = unwrap<Function>(Fn);
2431ef860a24SChandler Carruth   if (GC)
2432ef860a24SChandler Carruth     F->setGC(GC);
2433ef860a24SChandler Carruth   else
2434ef860a24SChandler Carruth     F->clearGC();
2435ef860a24SChandler Carruth }
2436ef860a24SChandler Carruth 
LLVMAddAttributeAtIndex(LLVMValueRef F,LLVMAttributeIndex Idx,LLVMAttributeRef A)24375db224e1SAmaury Sechet void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
24385db224e1SAmaury Sechet                              LLVMAttributeRef A) {
243952e6d70cSArthur Eubanks   unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A));
24405db224e1SAmaury Sechet }
24415db224e1SAmaury Sechet 
LLVMGetAttributeCountAtIndex(LLVMValueRef F,LLVMAttributeIndex Idx)244217b67cd1SAmaury Sechet unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
2443c2cb5600SReid Kleckner   auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2444c2cb5600SReid Kleckner   return AS.getNumAttributes();
244517b67cd1SAmaury Sechet }
244617b67cd1SAmaury Sechet 
LLVMGetAttributesAtIndex(LLVMValueRef F,LLVMAttributeIndex Idx,LLVMAttributeRef * Attrs)244717b67cd1SAmaury Sechet void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
244817b67cd1SAmaury Sechet                               LLVMAttributeRef *Attrs) {
2449c2cb5600SReid Kleckner   auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2450c2cb5600SReid Kleckner   for (auto A : AS)
245117b67cd1SAmaury Sechet     *Attrs++ = wrap(A);
245217b67cd1SAmaury Sechet }
245317b67cd1SAmaury Sechet 
LLVMGetEnumAttributeAtIndex(LLVMValueRef F,LLVMAttributeIndex Idx,unsigned KindID)24545db224e1SAmaury Sechet LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
24555db224e1SAmaury Sechet                                              LLVMAttributeIndex Idx,
24565db224e1SAmaury Sechet                                              unsigned KindID) {
245752e6d70cSArthur Eubanks   return wrap(unwrap<Function>(F)->getAttributeAtIndex(
245852e6d70cSArthur Eubanks       Idx, (Attribute::AttrKind)KindID));
24595db224e1SAmaury Sechet }
24605db224e1SAmaury Sechet 
LLVMGetStringAttributeAtIndex(LLVMValueRef F,LLVMAttributeIndex Idx,const char * K,unsigned KLen)24616100adfeSAmaury Sechet LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
24626100adfeSAmaury Sechet                                                LLVMAttributeIndex Idx,
24636100adfeSAmaury Sechet                                                const char *K, unsigned KLen) {
246452e6d70cSArthur Eubanks   return wrap(
246552e6d70cSArthur Eubanks       unwrap<Function>(F)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
24666100adfeSAmaury Sechet }
24676100adfeSAmaury Sechet 
LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F,LLVMAttributeIndex Idx,unsigned KindID)24685db224e1SAmaury Sechet void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
24695db224e1SAmaury Sechet                                     unsigned KindID) {
247052e6d70cSArthur Eubanks   unwrap<Function>(F)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
24715db224e1SAmaury Sechet }
24725db224e1SAmaury Sechet 
LLVMRemoveStringAttributeAtIndex(LLVMValueRef F,LLVMAttributeIndex Idx,const char * K,unsigned KLen)24736100adfeSAmaury Sechet void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
24746100adfeSAmaury Sechet                                       const char *K, unsigned KLen) {
247552e6d70cSArthur Eubanks   unwrap<Function>(F)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
24766100adfeSAmaury Sechet }
24776100adfeSAmaury Sechet 
LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn,const char * A,const char * V)2478e8f35e15STom Stellard void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
2479e8f35e15STom Stellard                                         const char *V) {
2480e8f35e15STom Stellard   Function *Func = unwrap<Function>(Fn);
24819d16fa09SReid Kleckner   Attribute Attr = Attribute::get(Func->getContext(), A, V);
248246cf8253SArthur Eubanks   Func->addFnAttr(Attr);
2483e8f35e15STom Stellard }
2484e8f35e15STom Stellard 
2485ef860a24SChandler Carruth /*--.. Operations on parameters ............................................--*/
2486ef860a24SChandler Carruth 
LLVMCountParams(LLVMValueRef FnRef)2487ef860a24SChandler Carruth unsigned LLVMCountParams(LLVMValueRef FnRef) {
2488ef860a24SChandler Carruth   // This function is strictly redundant to
2489ef860a24SChandler Carruth   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2490ef860a24SChandler Carruth   return unwrap<Function>(FnRef)->arg_size();
2491ef860a24SChandler Carruth }
2492ef860a24SChandler Carruth 
LLVMGetParams(LLVMValueRef FnRef,LLVMValueRef * ParamRefs)2493ef860a24SChandler Carruth void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2494ef860a24SChandler Carruth   Function *Fn = unwrap<Function>(FnRef);
24956a337f85SKazu Hirata   for (Argument &A : Fn->args())
24966a337f85SKazu Hirata     *ParamRefs++ = wrap(&A);
2497ef860a24SChandler Carruth }
2498ef860a24SChandler Carruth 
LLVMGetParam(LLVMValueRef FnRef,unsigned index)2499ef860a24SChandler Carruth LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
250056d028d9SReid Kleckner   Function *Fn = unwrap<Function>(FnRef);
250156d028d9SReid Kleckner   return wrap(&Fn->arg_begin()[index]);
2502ef860a24SChandler Carruth }
2503ef860a24SChandler Carruth 
LLVMGetParamParent(LLVMValueRef V)2504ef860a24SChandler Carruth LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
2505ef860a24SChandler Carruth   return wrap(unwrap<Argument>(V)->getParent());
2506ef860a24SChandler Carruth }
2507ef860a24SChandler Carruth 
LLVMGetFirstParam(LLVMValueRef Fn)2508ef860a24SChandler Carruth LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
2509ef860a24SChandler Carruth   Function *Func = unwrap<Function>(Fn);
2510ef860a24SChandler Carruth   Function::arg_iterator I = Func->arg_begin();
2511ef860a24SChandler Carruth   if (I == Func->arg_end())
2512c620761cSCraig Topper     return nullptr;
251352888a67SDuncan P. N. Exon Smith   return wrap(&*I);
2514ef860a24SChandler Carruth }
2515ef860a24SChandler Carruth 
LLVMGetLastParam(LLVMValueRef Fn)2516ef860a24SChandler Carruth LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
2517ef860a24SChandler Carruth   Function *Func = unwrap<Function>(Fn);
2518ef860a24SChandler Carruth   Function::arg_iterator I = Func->arg_end();
2519ef860a24SChandler Carruth   if (I == Func->arg_begin())
2520c620761cSCraig Topper     return nullptr;
252152888a67SDuncan P. N. Exon Smith   return wrap(&*--I);
2522ef860a24SChandler Carruth }
2523ef860a24SChandler Carruth 
LLVMGetNextParam(LLVMValueRef Arg)2524ef860a24SChandler Carruth LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
2525ef860a24SChandler Carruth   Argument *A = unwrap<Argument>(Arg);
252656d028d9SReid Kleckner   Function *Fn = A->getParent();
252756d028d9SReid Kleckner   if (A->getArgNo() + 1 >= Fn->arg_size())
2528c620761cSCraig Topper     return nullptr;
252956d028d9SReid Kleckner   return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2530ef860a24SChandler Carruth }
2531ef860a24SChandler Carruth 
LLVMGetPreviousParam(LLVMValueRef Arg)2532ef860a24SChandler Carruth LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
2533ef860a24SChandler Carruth   Argument *A = unwrap<Argument>(Arg);
253456d028d9SReid Kleckner   if (A->getArgNo() == 0)
2535c620761cSCraig Topper     return nullptr;
253656d028d9SReid Kleckner   return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2537ef860a24SChandler Carruth }
2538ef860a24SChandler Carruth 
LLVMSetParamAlignment(LLVMValueRef Arg,unsigned align)2539ef860a24SChandler Carruth void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
254049bc76cbSBill Wendling   Argument *A = unwrap<Argument>(Arg);
2541b65fa483SGuillaume Chatelet   A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align)));
2542ef860a24SChandler Carruth }
2543ef860a24SChandler Carruth 
2544d5444ccfSRobert Widmann /*--.. Operations on ifuncs ................................................--*/
2545d5444ccfSRobert Widmann 
LLVMAddGlobalIFunc(LLVMModuleRef M,const char * Name,size_t NameLen,LLVMTypeRef Ty,unsigned AddrSpace,LLVMValueRef Resolver)2546d5444ccfSRobert Widmann LLVMValueRef LLVMAddGlobalIFunc(LLVMModuleRef M,
2547d5444ccfSRobert Widmann                                 const char *Name, size_t NameLen,
2548d5444ccfSRobert Widmann                                 LLVMTypeRef Ty, unsigned AddrSpace,
2549d5444ccfSRobert Widmann                                 LLVMValueRef Resolver) {
2550d5444ccfSRobert Widmann   return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,
2551d5444ccfSRobert Widmann                                   GlobalValue::ExternalLinkage,
2552d5444ccfSRobert Widmann                                   StringRef(Name, NameLen),
2553d5444ccfSRobert Widmann                                   unwrap<Constant>(Resolver), unwrap(M)));
2554d5444ccfSRobert Widmann }
2555d5444ccfSRobert Widmann 
LLVMGetNamedGlobalIFunc(LLVMModuleRef M,const char * Name,size_t NameLen)2556d5444ccfSRobert Widmann LLVMValueRef LLVMGetNamedGlobalIFunc(LLVMModuleRef M,
2557d5444ccfSRobert Widmann                                      const char *Name, size_t NameLen) {
2558d5444ccfSRobert Widmann   return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));
2559d5444ccfSRobert Widmann }
2560d5444ccfSRobert Widmann 
LLVMGetFirstGlobalIFunc(LLVMModuleRef M)2561d5444ccfSRobert Widmann LLVMValueRef LLVMGetFirstGlobalIFunc(LLVMModuleRef M) {
2562d5444ccfSRobert Widmann   Module *Mod = unwrap(M);
2563d5444ccfSRobert Widmann   Module::ifunc_iterator I = Mod->ifunc_begin();
2564d5444ccfSRobert Widmann   if (I == Mod->ifunc_end())
2565d5444ccfSRobert Widmann     return nullptr;
2566d5444ccfSRobert Widmann   return wrap(&*I);
2567d5444ccfSRobert Widmann }
2568d5444ccfSRobert Widmann 
LLVMGetLastGlobalIFunc(LLVMModuleRef M)2569d5444ccfSRobert Widmann LLVMValueRef LLVMGetLastGlobalIFunc(LLVMModuleRef M) {
2570d5444ccfSRobert Widmann   Module *Mod = unwrap(M);
2571d5444ccfSRobert Widmann   Module::ifunc_iterator I = Mod->ifunc_end();
2572d5444ccfSRobert Widmann   if (I == Mod->ifunc_begin())
2573d5444ccfSRobert Widmann     return nullptr;
2574d5444ccfSRobert Widmann   return wrap(&*--I);
2575d5444ccfSRobert Widmann }
2576d5444ccfSRobert Widmann 
LLVMGetNextGlobalIFunc(LLVMValueRef IFunc)2577d5444ccfSRobert Widmann LLVMValueRef LLVMGetNextGlobalIFunc(LLVMValueRef IFunc) {
2578d5444ccfSRobert Widmann   GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2579d5444ccfSRobert Widmann   Module::ifunc_iterator I(GIF);
2580d5444ccfSRobert Widmann   if (++I == GIF->getParent()->ifunc_end())
2581d5444ccfSRobert Widmann     return nullptr;
2582d5444ccfSRobert Widmann   return wrap(&*I);
2583d5444ccfSRobert Widmann }
2584d5444ccfSRobert Widmann 
LLVMGetPreviousGlobalIFunc(LLVMValueRef IFunc)2585d5444ccfSRobert Widmann LLVMValueRef LLVMGetPreviousGlobalIFunc(LLVMValueRef IFunc) {
2586d5444ccfSRobert Widmann   GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2587d5444ccfSRobert Widmann   Module::ifunc_iterator I(GIF);
2588d5444ccfSRobert Widmann   if (I == GIF->getParent()->ifunc_begin())
2589d5444ccfSRobert Widmann     return nullptr;
2590d5444ccfSRobert Widmann   return wrap(&*--I);
2591d5444ccfSRobert Widmann }
2592d5444ccfSRobert Widmann 
LLVMGetGlobalIFuncResolver(LLVMValueRef IFunc)2593d5444ccfSRobert Widmann LLVMValueRef LLVMGetGlobalIFuncResolver(LLVMValueRef IFunc) {
2594d5444ccfSRobert Widmann   return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());
2595d5444ccfSRobert Widmann }
2596d5444ccfSRobert Widmann 
LLVMSetGlobalIFuncResolver(LLVMValueRef IFunc,LLVMValueRef Resolver)2597d5444ccfSRobert Widmann void LLVMSetGlobalIFuncResolver(LLVMValueRef IFunc, LLVMValueRef Resolver) {
2598d5444ccfSRobert Widmann   unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));
2599d5444ccfSRobert Widmann }
2600d5444ccfSRobert Widmann 
LLVMEraseGlobalIFunc(LLVMValueRef IFunc)2601d5444ccfSRobert Widmann void LLVMEraseGlobalIFunc(LLVMValueRef IFunc) {
2602d5444ccfSRobert Widmann   unwrap<GlobalIFunc>(IFunc)->eraseFromParent();
2603d5444ccfSRobert Widmann }
2604d5444ccfSRobert Widmann 
LLVMRemoveGlobalIFunc(LLVMValueRef IFunc)2605d5444ccfSRobert Widmann void LLVMRemoveGlobalIFunc(LLVMValueRef IFunc) {
2606d5444ccfSRobert Widmann   unwrap<GlobalIFunc>(IFunc)->removeFromParent();
2607d5444ccfSRobert Widmann }
2608d5444ccfSRobert Widmann 
2609ef860a24SChandler Carruth /*--.. Operations on basic blocks ..........................................--*/
2610ef860a24SChandler Carruth 
LLVMBasicBlockAsValue(LLVMBasicBlockRef BB)2611ef860a24SChandler Carruth LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
2612ef860a24SChandler Carruth   return wrap(static_cast<Value*>(unwrap(BB)));
2613ef860a24SChandler Carruth }
2614ef860a24SChandler Carruth 
LLVMValueIsBasicBlock(LLVMValueRef Val)2615ef860a24SChandler Carruth LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
2616ef860a24SChandler Carruth   return isa<BasicBlock>(unwrap(Val));
2617ef860a24SChandler Carruth }
2618ef860a24SChandler Carruth 
LLVMValueAsBasicBlock(LLVMValueRef Val)2619ef860a24SChandler Carruth LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
2620ef860a24SChandler Carruth   return wrap(unwrap<BasicBlock>(Val));
2621ef860a24SChandler Carruth }
2622ef860a24SChandler Carruth 
LLVMGetBasicBlockName(LLVMBasicBlockRef BB)2623a82042ebSAmaury Sechet const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
2624a82042ebSAmaury Sechet   return unwrap(BB)->getName().data();
2625a82042ebSAmaury Sechet }
2626a82042ebSAmaury Sechet 
LLVMGetBasicBlockParent(LLVMBasicBlockRef BB)2627ef860a24SChandler Carruth LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
2628ef860a24SChandler Carruth   return wrap(unwrap(BB)->getParent());
2629ef860a24SChandler Carruth }
2630ef860a24SChandler Carruth 
LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB)2631ef860a24SChandler Carruth LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
2632ef860a24SChandler Carruth   return wrap(unwrap(BB)->getTerminator());
2633ef860a24SChandler Carruth }
2634ef860a24SChandler Carruth 
LLVMCountBasicBlocks(LLVMValueRef FnRef)2635ef860a24SChandler Carruth unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
2636ef860a24SChandler Carruth   return unwrap<Function>(FnRef)->size();
2637ef860a24SChandler Carruth }
2638ef860a24SChandler Carruth 
LLVMGetBasicBlocks(LLVMValueRef FnRef,LLVMBasicBlockRef * BasicBlocksRefs)2639ef860a24SChandler Carruth void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2640ef860a24SChandler Carruth   Function *Fn = unwrap<Function>(FnRef);
2641af28e7d6SBenjamin Kramer   for (BasicBlock &BB : *Fn)
2642af28e7d6SBenjamin Kramer     *BasicBlocksRefs++ = wrap(&BB);
2643ef860a24SChandler Carruth }
2644ef860a24SChandler Carruth 
LLVMGetEntryBasicBlock(LLVMValueRef Fn)2645ef860a24SChandler Carruth LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2646ef860a24SChandler Carruth   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2647ef860a24SChandler Carruth }
2648ef860a24SChandler Carruth 
LLVMGetFirstBasicBlock(LLVMValueRef Fn)2649ef860a24SChandler Carruth LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2650ef860a24SChandler Carruth   Function *Func = unwrap<Function>(Fn);
2651ef860a24SChandler Carruth   Function::iterator I = Func->begin();
2652ef860a24SChandler Carruth   if (I == Func->end())
2653c620761cSCraig Topper     return nullptr;
265452888a67SDuncan P. N. Exon Smith   return wrap(&*I);
2655ef860a24SChandler Carruth }
2656ef860a24SChandler Carruth 
LLVMGetLastBasicBlock(LLVMValueRef Fn)2657ef860a24SChandler Carruth LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2658ef860a24SChandler Carruth   Function *Func = unwrap<Function>(Fn);
2659ef860a24SChandler Carruth   Function::iterator I = Func->end();
2660ef860a24SChandler Carruth   if (I == Func->begin())
2661c620761cSCraig Topper     return nullptr;
266252888a67SDuncan P. N. Exon Smith   return wrap(&*--I);
2663ef860a24SChandler Carruth }
2664ef860a24SChandler Carruth 
LLVMGetNextBasicBlock(LLVMBasicBlockRef BB)2665ef860a24SChandler Carruth LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2666ef860a24SChandler Carruth   BasicBlock *Block = unwrap(BB);
266752888a67SDuncan P. N. Exon Smith   Function::iterator I(Block);
2668ef860a24SChandler Carruth   if (++I == Block->getParent()->end())
2669c620761cSCraig Topper     return nullptr;
267052888a67SDuncan P. N. Exon Smith   return wrap(&*I);
2671ef860a24SChandler Carruth }
2672ef860a24SChandler Carruth 
LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB)2673ef860a24SChandler Carruth LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2674ef860a24SChandler Carruth   BasicBlock *Block = unwrap(BB);
267552888a67SDuncan P. N. Exon Smith   Function::iterator I(Block);
2676ef860a24SChandler Carruth   if (I == Block->getParent()->begin())
2677c620761cSCraig Topper     return nullptr;
267852888a67SDuncan P. N. Exon Smith   return wrap(&*--I);
2679ef860a24SChandler Carruth }
2680ef860a24SChandler Carruth 
LLVMCreateBasicBlockInContext(LLVMContextRef C,const char * Name)2681616ed172SRobert Widmann LLVMBasicBlockRef LLVMCreateBasicBlockInContext(LLVMContextRef C,
268268729f94SJames Y Knight                                                 const char *Name) {
268368729f94SJames Y Knight   return wrap(llvm::BasicBlock::Create(*unwrap(C), Name));
2684616ed172SRobert Widmann }
2685616ed172SRobert Widmann 
LLVMInsertExistingBasicBlockAfterInsertBlock(LLVMBuilderRef Builder,LLVMBasicBlockRef BB)2686b4baa560SRobert Widmann void LLVMInsertExistingBasicBlockAfterInsertBlock(LLVMBuilderRef Builder,
2687b4baa560SRobert Widmann                                                   LLVMBasicBlockRef BB) {
2688b4baa560SRobert Widmann   BasicBlock *ToInsert = unwrap(BB);
2689b4baa560SRobert Widmann   BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock();
2690b4baa560SRobert Widmann   assert(CurBB && "current insertion point is invalid!");
2691b4baa560SRobert Widmann   CurBB->getParent()->getBasicBlockList().insertAfter(CurBB->getIterator(),
2692b4baa560SRobert Widmann                                                       ToInsert);
2693b4baa560SRobert Widmann }
2694b4baa560SRobert Widmann 
LLVMAppendExistingBasicBlock(LLVMValueRef Fn,LLVMBasicBlockRef BB)2695b4baa560SRobert Widmann void LLVMAppendExistingBasicBlock(LLVMValueRef Fn,
2696b4baa560SRobert Widmann                                   LLVMBasicBlockRef BB) {
2697b4baa560SRobert Widmann   unwrap<Function>(Fn)->getBasicBlockList().push_back(unwrap(BB));
2698b4baa560SRobert Widmann }
2699b4baa560SRobert Widmann 
LLVMAppendBasicBlockInContext(LLVMContextRef C,LLVMValueRef FnRef,const char * Name)2700ef860a24SChandler Carruth LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2701ef860a24SChandler Carruth                                                 LLVMValueRef FnRef,
2702ef860a24SChandler Carruth                                                 const char *Name) {
2703ef860a24SChandler Carruth   return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2704ef860a24SChandler Carruth }
2705ef860a24SChandler Carruth 
LLVMAppendBasicBlock(LLVMValueRef FnRef,const char * Name)2706ef860a24SChandler Carruth LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2707ef860a24SChandler Carruth   return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2708ef860a24SChandler Carruth }
2709ef860a24SChandler Carruth 
LLVMInsertBasicBlockInContext(LLVMContextRef C,LLVMBasicBlockRef BBRef,const char * Name)2710ef860a24SChandler Carruth LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2711ef860a24SChandler Carruth                                                 LLVMBasicBlockRef BBRef,
2712ef860a24SChandler Carruth                                                 const char *Name) {
2713ef860a24SChandler Carruth   BasicBlock *BB = unwrap(BBRef);
2714ef860a24SChandler Carruth   return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2715ef860a24SChandler Carruth }
2716ef860a24SChandler Carruth 
LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,const char * Name)2717ef860a24SChandler Carruth LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
2718ef860a24SChandler Carruth                                        const char *Name) {
2719ef860a24SChandler Carruth   return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
2720ef860a24SChandler Carruth }
2721ef860a24SChandler Carruth 
LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef)2722ef860a24SChandler Carruth void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2723ef860a24SChandler Carruth   unwrap(BBRef)->eraseFromParent();
2724ef860a24SChandler Carruth }
2725ef860a24SChandler Carruth 
LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef)2726ef860a24SChandler Carruth void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2727ef860a24SChandler Carruth   unwrap(BBRef)->removeFromParent();
2728ef860a24SChandler Carruth }
2729ef860a24SChandler Carruth 
LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB,LLVMBasicBlockRef MovePos)2730ef860a24SChandler Carruth void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2731ef860a24SChandler Carruth   unwrap(BB)->moveBefore(unwrap(MovePos));
2732ef860a24SChandler Carruth }
2733ef860a24SChandler Carruth 
LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB,LLVMBasicBlockRef MovePos)2734ef860a24SChandler Carruth void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2735ef860a24SChandler Carruth   unwrap(BB)->moveAfter(unwrap(MovePos));
2736ef860a24SChandler Carruth }
2737ef860a24SChandler Carruth 
2738ef860a24SChandler Carruth /*--.. Operations on instructions ..........................................--*/
2739ef860a24SChandler Carruth 
LLVMGetInstructionParent(LLVMValueRef Inst)2740ef860a24SChandler Carruth LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2741ef860a24SChandler Carruth   return wrap(unwrap<Instruction>(Inst)->getParent());
2742ef860a24SChandler Carruth }
2743ef860a24SChandler Carruth 
LLVMGetFirstInstruction(LLVMBasicBlockRef BB)2744ef860a24SChandler Carruth LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2745ef860a24SChandler Carruth   BasicBlock *Block = unwrap(BB);
2746ef860a24SChandler Carruth   BasicBlock::iterator I = Block->begin();
2747ef860a24SChandler Carruth   if (I == Block->end())
2748c620761cSCraig Topper     return nullptr;
274952888a67SDuncan P. N. Exon Smith   return wrap(&*I);
2750ef860a24SChandler Carruth }
2751ef860a24SChandler Carruth 
LLVMGetLastInstruction(LLVMBasicBlockRef BB)2752ef860a24SChandler Carruth LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2753ef860a24SChandler Carruth   BasicBlock *Block = unwrap(BB);
2754ef860a24SChandler Carruth   BasicBlock::iterator I = Block->end();
2755ef860a24SChandler Carruth   if (I == Block->begin())
2756c620761cSCraig Topper     return nullptr;
275752888a67SDuncan P. N. Exon Smith   return wrap(&*--I);
2758ef860a24SChandler Carruth }
2759ef860a24SChandler Carruth 
LLVMGetNextInstruction(LLVMValueRef Inst)2760ef860a24SChandler Carruth LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2761ef860a24SChandler Carruth   Instruction *Instr = unwrap<Instruction>(Inst);
276252888a67SDuncan P. N. Exon Smith   BasicBlock::iterator I(Instr);
2763ef860a24SChandler Carruth   if (++I == Instr->getParent()->end())
2764c620761cSCraig Topper     return nullptr;
276552888a67SDuncan P. N. Exon Smith   return wrap(&*I);
2766ef860a24SChandler Carruth }
2767ef860a24SChandler Carruth 
LLVMGetPreviousInstruction(LLVMValueRef Inst)2768ef860a24SChandler Carruth LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2769ef860a24SChandler Carruth   Instruction *Instr = unwrap<Instruction>(Inst);
277052888a67SDuncan P. N. Exon Smith   BasicBlock::iterator I(Instr);
2771ef860a24SChandler Carruth   if (I == Instr->getParent()->begin())
2772c620761cSCraig Topper     return nullptr;
277352888a67SDuncan P. N. Exon Smith   return wrap(&*--I);
2774ef860a24SChandler Carruth }
2775ef860a24SChandler Carruth 
LLVMInstructionRemoveFromParent(LLVMValueRef Inst)27762f43208cSAmaury Sechet void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
27772f43208cSAmaury Sechet   unwrap<Instruction>(Inst)->removeFromParent();
27782f43208cSAmaury Sechet }
27792f43208cSAmaury Sechet 
LLVMInstructionEraseFromParent(LLVMValueRef Inst)2780ef860a24SChandler Carruth void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2781ef860a24SChandler Carruth   unwrap<Instruction>(Inst)->eraseFromParent();
2782ef860a24SChandler Carruth }
2783ef860a24SChandler Carruth 
LLVMDeleteInstruction(LLVMValueRef Inst)2784fdf7e437SNicolai Hähnle void LLVMDeleteInstruction(LLVMValueRef Inst) {
2785fdf7e437SNicolai Hähnle   unwrap<Instruction>(Inst)->deleteValue();
2786fdf7e437SNicolai Hähnle }
2787fdf7e437SNicolai Hähnle 
LLVMGetICmpPredicate(LLVMValueRef Inst)2788ef860a24SChandler Carruth LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
2789ef860a24SChandler Carruth   if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2790ef860a24SChandler Carruth     return (LLVMIntPredicate)I->getPredicate();
2791ef860a24SChandler Carruth   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2792ef860a24SChandler Carruth     if (CE->getOpcode() == Instruction::ICmp)
2793ef860a24SChandler Carruth       return (LLVMIntPredicate)CE->getPredicate();
2794ef860a24SChandler Carruth   return (LLVMIntPredicate)0;
2795ef860a24SChandler Carruth }
2796ef860a24SChandler Carruth 
LLVMGetFCmpPredicate(LLVMValueRef Inst)27971d98e6ddSPeter Zotov LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
27981d98e6ddSPeter Zotov   if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
27991d98e6ddSPeter Zotov     return (LLVMRealPredicate)I->getPredicate();
28001d98e6ddSPeter Zotov   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
28011d98e6ddSPeter Zotov     if (CE->getOpcode() == Instruction::FCmp)
28021d98e6ddSPeter Zotov       return (LLVMRealPredicate)CE->getPredicate();
28031d98e6ddSPeter Zotov   return (LLVMRealPredicate)0;
28041d98e6ddSPeter Zotov }
28051d98e6ddSPeter Zotov 
LLVMGetInstructionOpcode(LLVMValueRef Inst)2806ef860a24SChandler Carruth LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2807ef860a24SChandler Carruth   if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2808ef860a24SChandler Carruth     return map_to_llvmopcode(C->getOpcode());
2809ef860a24SChandler Carruth   return (LLVMOpcode)0;
2810ef860a24SChandler Carruth }
2811ef860a24SChandler Carruth 
LLVMInstructionClone(LLVMValueRef Inst)2812aff492c6SPeter Zotov LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2813aff492c6SPeter Zotov   if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2814aff492c6SPeter Zotov     return wrap(C->clone());
2815aff492c6SPeter Zotov   return nullptr;
2816aff492c6SPeter Zotov }
2817aff492c6SPeter Zotov 
LLVMIsATerminatorInst(LLVMValueRef Inst)28187c80c3a8SChandler Carruth LLVMValueRef LLVMIsATerminatorInst(LLVMValueRef Inst) {
28197c80c3a8SChandler Carruth   Instruction *I = dyn_cast<Instruction>(unwrap(Inst));
28207c80c3a8SChandler Carruth   return (I && I->isTerminator()) ? wrap(I) : nullptr;
28217c80c3a8SChandler Carruth }
28227c80c3a8SChandler Carruth 
LLVMGetNumArgOperands(LLVMValueRef Instr)28235c7b3af5SAmaury Sechet unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
2824478fce9eSRobert Widmann   if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2825478fce9eSRobert Widmann     return FPI->getNumArgOperands();
2826478fce9eSRobert Widmann   }
2827e6e29831SKazu Hirata   return unwrap<CallBase>(Instr)->arg_size();
28285c7b3af5SAmaury Sechet }
28295c7b3af5SAmaury Sechet 
2830478fce9eSRobert Widmann /*--.. Call and invoke instructions ........................................--*/
2831478fce9eSRobert Widmann 
LLVMGetInstructionCallConv(LLVMValueRef Instr)2832ef860a24SChandler Carruth unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
283390c09232SChandler Carruth   return unwrap<CallBase>(Instr)->getCallingConv();
2834ef860a24SChandler Carruth }
2835ef860a24SChandler Carruth 
LLVMSetInstructionCallConv(LLVMValueRef Instr,unsigned CC)2836ef860a24SChandler Carruth void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
283790c09232SChandler Carruth   return unwrap<CallBase>(Instr)->setCallingConv(
283890c09232SChandler Carruth       static_cast<CallingConv::ID>(CC));
2839ef860a24SChandler Carruth }
2840ef860a24SChandler Carruth 
LLVMSetInstrParamAlignment(LLVMValueRef Instr,LLVMAttributeIndex Idx,unsigned align)284139e2e3bdSArthur Eubanks void LLVMSetInstrParamAlignment(LLVMValueRef Instr, LLVMAttributeIndex Idx,
2842ef860a24SChandler Carruth                                 unsigned align) {
284390c09232SChandler Carruth   auto *Call = unwrap<CallBase>(Instr);
2844b65fa483SGuillaume Chatelet   Attribute AlignAttr =
2845b65fa483SGuillaume Chatelet       Attribute::getWithAlignment(Call->getContext(), Align(align));
284639e2e3bdSArthur Eubanks   Call->addAttributeAtIndex(Idx, AlignAttr);
2847ef860a24SChandler Carruth }
2848ef860a24SChandler Carruth 
LLVMAddCallSiteAttribute(LLVMValueRef C,LLVMAttributeIndex Idx,LLVMAttributeRef A)2849a65a2378SAmaury Sechet void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2850a65a2378SAmaury Sechet                               LLVMAttributeRef A) {
285152e6d70cSArthur Eubanks   unwrap<CallBase>(C)->addAttributeAtIndex(Idx, unwrap(A));
2852a65a2378SAmaury Sechet }
2853a65a2378SAmaury Sechet 
LLVMGetCallSiteAttributeCount(LLVMValueRef C,LLVMAttributeIndex Idx)285417b67cd1SAmaury Sechet unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
285517b67cd1SAmaury Sechet                                        LLVMAttributeIndex Idx) {
285690c09232SChandler Carruth   auto *Call = unwrap<CallBase>(C);
285790c09232SChandler Carruth   auto AS = Call->getAttributes().getAttributes(Idx);
2858c2cb5600SReid Kleckner   return AS.getNumAttributes();
285917b67cd1SAmaury Sechet }
286017b67cd1SAmaury Sechet 
LLVMGetCallSiteAttributes(LLVMValueRef C,LLVMAttributeIndex Idx,LLVMAttributeRef * Attrs)286117b67cd1SAmaury Sechet void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
286217b67cd1SAmaury Sechet                                LLVMAttributeRef *Attrs) {
286390c09232SChandler Carruth   auto *Call = unwrap<CallBase>(C);
286490c09232SChandler Carruth   auto AS = Call->getAttributes().getAttributes(Idx);
2865c2cb5600SReid Kleckner   for (auto A : AS)
286617b67cd1SAmaury Sechet     *Attrs++ = wrap(A);
286717b67cd1SAmaury Sechet }
286817b67cd1SAmaury Sechet 
LLVMGetCallSiteEnumAttribute(LLVMValueRef C,LLVMAttributeIndex Idx,unsigned KindID)2869a65a2378SAmaury Sechet LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
2870a65a2378SAmaury Sechet                                               LLVMAttributeIndex Idx,
2871a65a2378SAmaury Sechet                                               unsigned KindID) {
287252e6d70cSArthur Eubanks   return wrap(unwrap<CallBase>(C)->getAttributeAtIndex(
287352e6d70cSArthur Eubanks       Idx, (Attribute::AttrKind)KindID));
2874a65a2378SAmaury Sechet }
2875a65a2378SAmaury Sechet 
LLVMGetCallSiteStringAttribute(LLVMValueRef C,LLVMAttributeIndex Idx,const char * K,unsigned KLen)28766100adfeSAmaury Sechet LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
28776100adfeSAmaury Sechet                                                 LLVMAttributeIndex Idx,
28786100adfeSAmaury Sechet                                                 const char *K, unsigned KLen) {
287952e6d70cSArthur Eubanks   return wrap(
288052e6d70cSArthur Eubanks       unwrap<CallBase>(C)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
28816100adfeSAmaury Sechet }
28826100adfeSAmaury Sechet 
LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C,LLVMAttributeIndex Idx,unsigned KindID)2883a65a2378SAmaury Sechet void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2884a65a2378SAmaury Sechet                                      unsigned KindID) {
288552e6d70cSArthur Eubanks   unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2886a65a2378SAmaury Sechet }
2887a65a2378SAmaury Sechet 
LLVMRemoveCallSiteStringAttribute(LLVMValueRef C,LLVMAttributeIndex Idx,const char * K,unsigned KLen)28886100adfeSAmaury Sechet void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
28896100adfeSAmaury Sechet                                        const char *K, unsigned KLen) {
289052e6d70cSArthur Eubanks   unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
28916100adfeSAmaury Sechet }
28926100adfeSAmaury Sechet 
LLVMGetCalledValue(LLVMValueRef Instr)28935c7b3af5SAmaury Sechet LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2894a58b62b4SCraig Topper   return wrap(unwrap<CallBase>(Instr)->getCalledOperand());
28955c7b3af5SAmaury Sechet }
28965c7b3af5SAmaury Sechet 
LLVMGetCalledFunctionType(LLVMValueRef Instr)2897f9563909SJames Y Knight LLVMTypeRef LLVMGetCalledFunctionType(LLVMValueRef Instr) {
2898f9563909SJames Y Knight   return wrap(unwrap<CallBase>(Instr)->getFunctionType());
2899f9563909SJames Y Knight }
2900f9563909SJames Y Knight 
2901ef860a24SChandler Carruth /*--.. Operations on call instructions (only) ..............................--*/
2902ef860a24SChandler Carruth 
LLVMIsTailCall(LLVMValueRef Call)2903ef860a24SChandler Carruth LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
2904ef860a24SChandler Carruth   return unwrap<CallInst>(Call)->isTailCall();
2905ef860a24SChandler Carruth }
2906ef860a24SChandler Carruth 
LLVMSetTailCall(LLVMValueRef Call,LLVMBool isTailCall)2907ef860a24SChandler Carruth void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2908ef860a24SChandler Carruth   unwrap<CallInst>(Call)->setTailCall(isTailCall);
2909ef860a24SChandler Carruth }
2910ef860a24SChandler Carruth 
2911e39e8530SAmaury Sechet /*--.. Operations on invoke instructions (only) ............................--*/
2912e39e8530SAmaury Sechet 
LLVMGetNormalDest(LLVMValueRef Invoke)2913e39e8530SAmaury Sechet LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2914e39e8530SAmaury Sechet   return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2915e39e8530SAmaury Sechet }
2916e39e8530SAmaury Sechet 
LLVMGetUnwindDest(LLVMValueRef Invoke)2917e39e8530SAmaury Sechet LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
2918478fce9eSRobert Widmann   if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2919478fce9eSRobert Widmann     return wrap(CRI->getUnwindDest());
2920478fce9eSRobert Widmann   } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2921478fce9eSRobert Widmann     return wrap(CSI->getUnwindDest());
2922478fce9eSRobert Widmann   }
2923e39e8530SAmaury Sechet   return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2924e39e8530SAmaury Sechet }
2925e39e8530SAmaury Sechet 
LLVMSetNormalDest(LLVMValueRef Invoke,LLVMBasicBlockRef B)2926e39e8530SAmaury Sechet void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2927e39e8530SAmaury Sechet   unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2928e39e8530SAmaury Sechet }
2929e39e8530SAmaury Sechet 
LLVMSetUnwindDest(LLVMValueRef Invoke,LLVMBasicBlockRef B)2930e39e8530SAmaury Sechet void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2931478fce9eSRobert Widmann   if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2932478fce9eSRobert Widmann     return CRI->setUnwindDest(unwrap(B));
2933478fce9eSRobert Widmann   } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2934478fce9eSRobert Widmann     return CSI->setUnwindDest(unwrap(B));
2935478fce9eSRobert Widmann   }
2936e39e8530SAmaury Sechet   unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2937e39e8530SAmaury Sechet }
2938e39e8530SAmaury Sechet 
29392481c75fSPeter Zotov /*--.. Operations on terminators ...........................................--*/
29402481c75fSPeter Zotov 
LLVMGetNumSuccessors(LLVMValueRef Term)29412481c75fSPeter Zotov unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
29427c80c3a8SChandler Carruth   return unwrap<Instruction>(Term)->getNumSuccessors();
29432481c75fSPeter Zotov }
29442481c75fSPeter Zotov 
LLVMGetSuccessor(LLVMValueRef Term,unsigned i)29452481c75fSPeter Zotov LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
29467c80c3a8SChandler Carruth   return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
29472481c75fSPeter Zotov }
29482481c75fSPeter Zotov 
LLVMSetSuccessor(LLVMValueRef Term,unsigned i,LLVMBasicBlockRef block)29492481c75fSPeter Zotov void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
29507c80c3a8SChandler Carruth   return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
29512481c75fSPeter Zotov }
29522481c75fSPeter Zotov 
29532481c75fSPeter Zotov /*--.. Operations on branch instructions (only) ............................--*/
29542481c75fSPeter Zotov 
LLVMIsConditional(LLVMValueRef Branch)29552481c75fSPeter Zotov LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
29562481c75fSPeter Zotov   return unwrap<BranchInst>(Branch)->isConditional();
29572481c75fSPeter Zotov }
29582481c75fSPeter Zotov 
LLVMGetCondition(LLVMValueRef Branch)29592481c75fSPeter Zotov LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
29602481c75fSPeter Zotov   return wrap(unwrap<BranchInst>(Branch)->getCondition());
29612481c75fSPeter Zotov }
29622481c75fSPeter Zotov 
LLVMSetCondition(LLVMValueRef Branch,LLVMValueRef Cond)29632481c75fSPeter Zotov void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
29642481c75fSPeter Zotov   return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
29652481c75fSPeter Zotov }
29662481c75fSPeter Zotov 
2967ef860a24SChandler Carruth /*--.. Operations on switch instructions (only) ............................--*/
2968ef860a24SChandler Carruth 
LLVMGetSwitchDefaultDest(LLVMValueRef Switch)2969ef860a24SChandler Carruth LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2970ef860a24SChandler Carruth   return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2971ef860a24SChandler Carruth }
2972ef860a24SChandler Carruth 
29731dcf577aSAmaury Sechet /*--.. Operations on alloca instructions (only) ............................--*/
29741dcf577aSAmaury Sechet 
LLVMGetAllocatedType(LLVMValueRef Alloca)29751dcf577aSAmaury Sechet LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
29761dcf577aSAmaury Sechet   return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
29771dcf577aSAmaury Sechet }
29781dcf577aSAmaury Sechet 
2979053ac453SAmaury Sechet /*--.. Operations on gep instructions (only) ...............................--*/
2980053ac453SAmaury Sechet 
LLVMIsInBounds(LLVMValueRef GEP)2981053ac453SAmaury Sechet LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
298265777addSNikita Popov   return unwrap<GEPOperator>(GEP)->isInBounds();
2983053ac453SAmaury Sechet }
2984053ac453SAmaury Sechet 
LLVMSetIsInBounds(LLVMValueRef GEP,LLVMBool InBounds)29858a367d40SAmaury Sechet void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
29868a367d40SAmaury Sechet   return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2987053ac453SAmaury Sechet }
2988053ac453SAmaury Sechet 
LLVMGetGEPSourceElementType(LLVMValueRef GEP)2989573a9bc4SNikita Popov LLVMTypeRef LLVMGetGEPSourceElementType(LLVMValueRef GEP) {
299065777addSNikita Popov   return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType());
2991573a9bc4SNikita Popov }
2992573a9bc4SNikita Popov 
2993ef860a24SChandler Carruth /*--.. Operations on phi nodes .............................................--*/
2994ef860a24SChandler Carruth 
LLVMAddIncoming(LLVMValueRef PhiNode,LLVMValueRef * IncomingValues,LLVMBasicBlockRef * IncomingBlocks,unsigned Count)2995ef860a24SChandler Carruth void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2996ef860a24SChandler Carruth                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2997ef860a24SChandler Carruth   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2998ef860a24SChandler Carruth   for (unsigned I = 0; I != Count; ++I)
2999ef860a24SChandler Carruth     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
3000ef860a24SChandler Carruth }
3001ef860a24SChandler Carruth 
LLVMCountIncoming(LLVMValueRef PhiNode)3002ef860a24SChandler Carruth unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
3003ef860a24SChandler Carruth   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
3004ef860a24SChandler Carruth }
3005ef860a24SChandler Carruth 
LLVMGetIncomingValue(LLVMValueRef PhiNode,unsigned Index)3006ef860a24SChandler Carruth LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
3007ef860a24SChandler Carruth   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
3008ef860a24SChandler Carruth }
3009ef860a24SChandler Carruth 
LLVMGetIncomingBlock(LLVMValueRef PhiNode,unsigned Index)3010ef860a24SChandler Carruth LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
3011ef860a24SChandler Carruth   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
3012ef860a24SChandler Carruth }
3013ef860a24SChandler Carruth 
3014aad93537SAmaury Sechet /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
3015aad93537SAmaury Sechet 
LLVMGetNumIndices(LLVMValueRef Inst)3016aad93537SAmaury Sechet unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
3017aad93537SAmaury Sechet   auto *I = unwrap(Inst);
301865777addSNikita Popov   if (auto *GEP = dyn_cast<GEPOperator>(I))
3019053ac453SAmaury Sechet     return GEP->getNumIndices();
3020aad93537SAmaury Sechet   if (auto *EV = dyn_cast<ExtractValueInst>(I))
3021aad93537SAmaury Sechet     return EV->getNumIndices();
3022aad93537SAmaury Sechet   if (auto *IV = dyn_cast<InsertValueInst>(I))
3023aad93537SAmaury Sechet     return IV->getNumIndices();
3024aad93537SAmaury Sechet   llvm_unreachable(
3025aad93537SAmaury Sechet     "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3026aad93537SAmaury Sechet }
3027aad93537SAmaury Sechet 
LLVMGetIndices(LLVMValueRef Inst)3028aad93537SAmaury Sechet const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
3029aad93537SAmaury Sechet   auto *I = unwrap(Inst);
3030aad93537SAmaury Sechet   if (auto *EV = dyn_cast<ExtractValueInst>(I))
3031aad93537SAmaury Sechet     return EV->getIndices().data();
3032aad93537SAmaury Sechet   if (auto *IV = dyn_cast<InsertValueInst>(I))
3033aad93537SAmaury Sechet     return IV->getIndices().data();
3034aad93537SAmaury Sechet   llvm_unreachable(
3035aad93537SAmaury Sechet     "LLVMGetIndices applies only to extractvalue and insertvalue!");
3036aad93537SAmaury Sechet }
3037aad93537SAmaury Sechet 
3038ef860a24SChandler Carruth 
3039ef860a24SChandler Carruth /*===-- Instruction builders ----------------------------------------------===*/
3040ef860a24SChandler Carruth 
LLVMCreateBuilderInContext(LLVMContextRef C)3041ef860a24SChandler Carruth LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
3042ef860a24SChandler Carruth   return wrap(new IRBuilder<>(*unwrap(C)));
3043ef860a24SChandler Carruth }
3044ef860a24SChandler Carruth 
LLVMCreateBuilder(void)3045ef860a24SChandler Carruth LLVMBuilderRef LLVMCreateBuilder(void) {
3046ef860a24SChandler Carruth   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
3047ef860a24SChandler Carruth }
3048ef860a24SChandler Carruth 
LLVMPositionBuilder(LLVMBuilderRef Builder,LLVMBasicBlockRef Block,LLVMValueRef Instr)3049ef860a24SChandler Carruth void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
3050ef860a24SChandler Carruth                          LLVMValueRef Instr) {
3051ef860a24SChandler Carruth   BasicBlock *BB = unwrap(Block);
305243724649SDuncan P. N. Exon Smith   auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
305343724649SDuncan P. N. Exon Smith   unwrap(Builder)->SetInsertPoint(BB, I);
3054ef860a24SChandler Carruth }
3055ef860a24SChandler Carruth 
LLVMPositionBuilderBefore(LLVMBuilderRef Builder,LLVMValueRef Instr)3056ef860a24SChandler Carruth void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
3057ef860a24SChandler Carruth   Instruction *I = unwrap<Instruction>(Instr);
305852888a67SDuncan P. N. Exon Smith   unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
3059ef860a24SChandler Carruth }
3060ef860a24SChandler Carruth 
LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder,LLVMBasicBlockRef Block)3061ef860a24SChandler Carruth void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
3062ef860a24SChandler Carruth   BasicBlock *BB = unwrap(Block);
3063ef860a24SChandler Carruth   unwrap(Builder)->SetInsertPoint(BB);
3064ef860a24SChandler Carruth }
3065ef860a24SChandler Carruth 
LLVMGetInsertBlock(LLVMBuilderRef Builder)3066ef860a24SChandler Carruth LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
3067ef860a24SChandler Carruth    return wrap(unwrap(Builder)->GetInsertBlock());
3068ef860a24SChandler Carruth }
3069ef860a24SChandler Carruth 
LLVMClearInsertionPosition(LLVMBuilderRef Builder)3070ef860a24SChandler Carruth void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
3071ef860a24SChandler Carruth   unwrap(Builder)->ClearInsertionPoint();
3072ef860a24SChandler Carruth }
3073ef860a24SChandler Carruth 
LLVMInsertIntoBuilder(LLVMBuilderRef Builder,LLVMValueRef Instr)3074ef860a24SChandler Carruth void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
3075ef860a24SChandler Carruth   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
3076ef860a24SChandler Carruth }
3077ef860a24SChandler Carruth 
LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder,LLVMValueRef Instr,const char * Name)3078ef860a24SChandler Carruth void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
3079ef860a24SChandler Carruth                                    const char *Name) {
3080ef860a24SChandler Carruth   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
3081ef860a24SChandler Carruth }
3082ef860a24SChandler Carruth 
LLVMDisposeBuilder(LLVMBuilderRef Builder)3083ef860a24SChandler Carruth void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
3084ef860a24SChandler Carruth   delete unwrap(Builder);
3085ef860a24SChandler Carruth }
3086ef860a24SChandler Carruth 
3087ef860a24SChandler Carruth /*--.. Metadata builders ...................................................--*/
3088ef860a24SChandler Carruth 
LLVMGetCurrentDebugLocation2(LLVMBuilderRef Builder)3089cce47418SRobert Widmann LLVMMetadataRef LLVMGetCurrentDebugLocation2(LLVMBuilderRef Builder) {
3090cce47418SRobert Widmann   return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
3091cce47418SRobert Widmann }
3092cce47418SRobert Widmann 
LLVMSetCurrentDebugLocation2(LLVMBuilderRef Builder,LLVMMetadataRef Loc)3093cce47418SRobert Widmann void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Builder, LLVMMetadataRef Loc) {
3094cce47418SRobert Widmann   if (Loc)
3095cce47418SRobert Widmann     unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
3096cce47418SRobert Widmann   else
3097cce47418SRobert Widmann     unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
3098cce47418SRobert Widmann }
3099cce47418SRobert Widmann 
LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder,LLVMValueRef L)3100ef860a24SChandler Carruth void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
31015bf8fef5SDuncan P. N. Exon Smith   MDNode *Loc =
31025bf8fef5SDuncan P. N. Exon Smith       L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
3103ab659fb3SDuncan P. N. Exon Smith   unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3104ef860a24SChandler Carruth }
3105ef860a24SChandler Carruth 
LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder)3106ef860a24SChandler Carruth LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
31075bf8fef5SDuncan P. N. Exon Smith   LLVMContext &Context = unwrap(Builder)->getContext();
31085bf8fef5SDuncan P. N. Exon Smith   return wrap(MetadataAsValue::get(
3109ab659fb3SDuncan P. N. Exon Smith       Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3110ef860a24SChandler Carruth }
3111ef860a24SChandler Carruth 
LLVMSetInstDebugLocation(LLVMBuilderRef Builder,LLVMValueRef Inst)3112ef860a24SChandler Carruth void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
3113ef860a24SChandler Carruth   unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3114ef860a24SChandler Carruth }
3115ef860a24SChandler Carruth 
LLVMAddMetadataToInst(LLVMBuilderRef Builder,LLVMValueRef Inst)3116d4653156SFlorian Hahn void LLVMAddMetadataToInst(LLVMBuilderRef Builder, LLVMValueRef Inst) {
3117d4653156SFlorian Hahn   unwrap(Builder)->AddMetadataToInst(unwrap<Instruction>(Inst));
3118d4653156SFlorian Hahn }
3119d4653156SFlorian Hahn 
LLVMBuilderSetDefaultFPMathTag(LLVMBuilderRef Builder,LLVMMetadataRef FPMathTag)3120ff8febcbSRobert Widmann void LLVMBuilderSetDefaultFPMathTag(LLVMBuilderRef Builder,
3121ff8febcbSRobert Widmann                                     LLVMMetadataRef FPMathTag) {
3122ff8febcbSRobert Widmann 
3123ff8febcbSRobert Widmann   unwrap(Builder)->setDefaultFPMathTag(FPMathTag
3124ff8febcbSRobert Widmann                                        ? unwrap<MDNode>(FPMathTag)
3125ff8febcbSRobert Widmann                                        : nullptr);
3126ff8febcbSRobert Widmann }
3127ff8febcbSRobert Widmann 
LLVMBuilderGetDefaultFPMathTag(LLVMBuilderRef Builder)3128ff8febcbSRobert Widmann LLVMMetadataRef LLVMBuilderGetDefaultFPMathTag(LLVMBuilderRef Builder) {
3129ff8febcbSRobert Widmann   return wrap(unwrap(Builder)->getDefaultFPMathTag());
3130ff8febcbSRobert Widmann }
3131ff8febcbSRobert Widmann 
3132ef860a24SChandler Carruth /*--.. Instruction builders ................................................--*/
3133ef860a24SChandler Carruth 
LLVMBuildRetVoid(LLVMBuilderRef B)3134ef860a24SChandler Carruth LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
3135ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateRetVoid());
3136ef860a24SChandler Carruth }
3137ef860a24SChandler Carruth 
LLVMBuildRet(LLVMBuilderRef B,LLVMValueRef V)3138ef860a24SChandler Carruth LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
3139ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateRet(unwrap(V)));
3140ef860a24SChandler Carruth }
3141ef860a24SChandler Carruth 
LLVMBuildAggregateRet(LLVMBuilderRef B,LLVMValueRef * RetVals,unsigned N)3142ef860a24SChandler Carruth LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
3143ef860a24SChandler Carruth                                    unsigned N) {
3144ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3145ef860a24SChandler Carruth }
3146ef860a24SChandler Carruth 
LLVMBuildBr(LLVMBuilderRef B,LLVMBasicBlockRef Dest)3147ef860a24SChandler Carruth LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
3148ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3149ef860a24SChandler Carruth }
3150ef860a24SChandler Carruth 
LLVMBuildCondBr(LLVMBuilderRef B,LLVMValueRef If,LLVMBasicBlockRef Then,LLVMBasicBlockRef Else)3151ef860a24SChandler Carruth LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
3152ef860a24SChandler Carruth                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
3153ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3154ef860a24SChandler Carruth }
3155ef860a24SChandler Carruth 
LLVMBuildSwitch(LLVMBuilderRef B,LLVMValueRef V,LLVMBasicBlockRef Else,unsigned NumCases)3156ef860a24SChandler Carruth LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
3157ef860a24SChandler Carruth                              LLVMBasicBlockRef Else, unsigned NumCases) {
3158ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3159ef860a24SChandler Carruth }
3160ef860a24SChandler Carruth 
LLVMBuildIndirectBr(LLVMBuilderRef B,LLVMValueRef Addr,unsigned NumDests)3161ef860a24SChandler Carruth LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
3162ef860a24SChandler Carruth                                  unsigned NumDests) {
3163ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3164ef860a24SChandler Carruth }
3165ef860a24SChandler Carruth 
LLVMBuildInvoke(LLVMBuilderRef B,LLVMValueRef Fn,LLVMValueRef * Args,unsigned NumArgs,LLVMBasicBlockRef Then,LLVMBasicBlockRef Catch,const char * Name)3166ef860a24SChandler Carruth LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
3167ef860a24SChandler Carruth                              LLVMValueRef *Args, unsigned NumArgs,
3168ef860a24SChandler Carruth                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
3169ef860a24SChandler Carruth                              const char *Name) {
3170eb2c4af1SJames Y Knight   Value *V = unwrap(Fn);
3171eb2c4af1SJames Y Knight   FunctionType *FnT =
3172d29e3192SNikita Popov       cast<FunctionType>(V->getType()->getNonOpaquePointerElementType());
3173eb2c4af1SJames Y Knight 
3174eb2c4af1SJames Y Knight   return wrap(
3175eb2c4af1SJames Y Knight       unwrap(B)->CreateInvoke(FnT, unwrap(Fn), unwrap(Then), unwrap(Catch),
3176eb2c4af1SJames Y Knight                               makeArrayRef(unwrap(Args), NumArgs), Name));
3177eb2c4af1SJames Y Knight }
3178eb2c4af1SJames Y Knight 
LLVMBuildInvoke2(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef Fn,LLVMValueRef * Args,unsigned NumArgs,LLVMBasicBlockRef Then,LLVMBasicBlockRef Catch,const char * Name)3179eb2c4af1SJames Y Knight LLVMValueRef LLVMBuildInvoke2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
3180eb2c4af1SJames Y Knight                               LLVMValueRef *Args, unsigned NumArgs,
3181eb2c4af1SJames Y Knight                               LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
3182eb2c4af1SJames Y Knight                               const char *Name) {
3183eb2c4af1SJames Y Knight   return wrap(unwrap(B)->CreateInvoke(
3184eb2c4af1SJames Y Knight       unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(Then), unwrap(Catch),
3185eb2c4af1SJames Y Knight       makeArrayRef(unwrap(Args), NumArgs), Name));
3186ef860a24SChandler Carruth }
3187ef860a24SChandler Carruth 
LLVMBuildLandingPad(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef PersFn,unsigned NumClauses,const char * Name)3188ef860a24SChandler Carruth LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
3189ef9828fbSReid Kleckner                                  LLVMValueRef PersFn, unsigned NumClauses,
3190ef9828fbSReid Kleckner                                  const char *Name) {
3191ef9828fbSReid Kleckner   // The personality used to live on the landingpad instruction, but now it
3192ef9828fbSReid Kleckner   // lives on the parent function. For compatibility, take the provided
3193ef9828fbSReid Kleckner   // personality and put it on the parent function.
3194ef9828fbSReid Kleckner   if (PersFn)
3195ef9828fbSReid Kleckner     unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3196ef9828fbSReid Kleckner         cast<Function>(unwrap(PersFn)));
31977fddeccbSDavid Majnemer   return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3198ef860a24SChandler Carruth }
3199ef860a24SChandler Carruth 
LLVMBuildCatchPad(LLVMBuilderRef B,LLVMValueRef ParentPad,LLVMValueRef * Args,unsigned NumArgs,const char * Name)3200478fce9eSRobert Widmann LLVMValueRef LLVMBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
3201478fce9eSRobert Widmann                                LLVMValueRef *Args, unsigned NumArgs,
3202478fce9eSRobert Widmann                                const char *Name) {
3203478fce9eSRobert Widmann   return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3204478fce9eSRobert Widmann                                         makeArrayRef(unwrap(Args), NumArgs),
3205478fce9eSRobert Widmann                                         Name));
3206478fce9eSRobert Widmann }
3207478fce9eSRobert Widmann 
LLVMBuildCleanupPad(LLVMBuilderRef B,LLVMValueRef ParentPad,LLVMValueRef * Args,unsigned NumArgs,const char * Name)3208478fce9eSRobert Widmann LLVMValueRef LLVMBuildCleanupPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
3209478fce9eSRobert Widmann                                  LLVMValueRef *Args, unsigned NumArgs,
3210478fce9eSRobert Widmann                                  const char *Name) {
3211478fce9eSRobert Widmann   if (ParentPad == nullptr) {
3212478fce9eSRobert Widmann     Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3213478fce9eSRobert Widmann     ParentPad = wrap(Constant::getNullValue(Ty));
3214478fce9eSRobert Widmann   }
3215478fce9eSRobert Widmann   return wrap(unwrap(B)->CreateCleanupPad(unwrap(ParentPad),
3216478fce9eSRobert Widmann                                           makeArrayRef(unwrap(Args), NumArgs),
3217478fce9eSRobert Widmann                                           Name));
3218478fce9eSRobert Widmann }
3219478fce9eSRobert Widmann 
LLVMBuildResume(LLVMBuilderRef B,LLVMValueRef Exn)3220ef860a24SChandler Carruth LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
3221ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3222ef860a24SChandler Carruth }
3223ef860a24SChandler Carruth 
LLVMBuildCatchSwitch(LLVMBuilderRef B,LLVMValueRef ParentPad,LLVMBasicBlockRef UnwindBB,unsigned NumHandlers,const char * Name)3224478fce9eSRobert Widmann LLVMValueRef LLVMBuildCatchSwitch(LLVMBuilderRef B, LLVMValueRef ParentPad,
3225478fce9eSRobert Widmann                                   LLVMBasicBlockRef UnwindBB,
3226478fce9eSRobert Widmann                                   unsigned NumHandlers, const char *Name) {
3227478fce9eSRobert Widmann   if (ParentPad == nullptr) {
3228478fce9eSRobert Widmann     Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3229478fce9eSRobert Widmann     ParentPad = wrap(Constant::getNullValue(Ty));
3230478fce9eSRobert Widmann   }
3231478fce9eSRobert Widmann   return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3232478fce9eSRobert Widmann                                            NumHandlers, Name));
3233478fce9eSRobert Widmann }
3234478fce9eSRobert Widmann 
LLVMBuildCatchRet(LLVMBuilderRef B,LLVMValueRef CatchPad,LLVMBasicBlockRef BB)3235478fce9eSRobert Widmann LLVMValueRef LLVMBuildCatchRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
3236478fce9eSRobert Widmann                                LLVMBasicBlockRef BB) {
3237478fce9eSRobert Widmann   return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3238478fce9eSRobert Widmann                                         unwrap(BB)));
3239478fce9eSRobert Widmann }
3240478fce9eSRobert Widmann 
LLVMBuildCleanupRet(LLVMBuilderRef B,LLVMValueRef CatchPad,LLVMBasicBlockRef BB)3241478fce9eSRobert Widmann LLVMValueRef LLVMBuildCleanupRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
3242478fce9eSRobert Widmann                                  LLVMBasicBlockRef BB) {
3243478fce9eSRobert Widmann   return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3244478fce9eSRobert Widmann                                           unwrap(BB)));
3245478fce9eSRobert Widmann }
3246478fce9eSRobert Widmann 
LLVMBuildUnreachable(LLVMBuilderRef B)3247ef860a24SChandler Carruth LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
3248ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateUnreachable());
3249ef860a24SChandler Carruth }
3250ef860a24SChandler Carruth 
LLVMAddCase(LLVMValueRef Switch,LLVMValueRef OnVal,LLVMBasicBlockRef Dest)3251ef860a24SChandler Carruth void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
3252ef860a24SChandler Carruth                  LLVMBasicBlockRef Dest) {
3253ef860a24SChandler Carruth   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3254ef860a24SChandler Carruth }
3255ef860a24SChandler Carruth 
LLVMAddDestination(LLVMValueRef IndirectBr,LLVMBasicBlockRef Dest)3256ef860a24SChandler Carruth void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
3257ef860a24SChandler Carruth   unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3258ef860a24SChandler Carruth }
3259ef860a24SChandler Carruth 
LLVMGetNumClauses(LLVMValueRef LandingPad)3260e39e8530SAmaury Sechet unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3261e39e8530SAmaury Sechet   return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3262e39e8530SAmaury Sechet }
3263e39e8530SAmaury Sechet 
LLVMGetClause(LLVMValueRef LandingPad,unsigned Idx)3264e39e8530SAmaury Sechet LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
3265e39e8530SAmaury Sechet   return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3266e39e8530SAmaury Sechet }
3267e39e8530SAmaury Sechet 
LLVMAddClause(LLVMValueRef LandingPad,LLVMValueRef ClauseVal)3268ef860a24SChandler Carruth void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
3269ef860a24SChandler Carruth   unwrap<LandingPadInst>(LandingPad)->
3270ef860a24SChandler Carruth     addClause(cast<Constant>(unwrap(ClauseVal)));
3271ef860a24SChandler Carruth }
3272ef860a24SChandler Carruth 
LLVMIsCleanup(LLVMValueRef LandingPad)3273e39e8530SAmaury Sechet LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
3274e39e8530SAmaury Sechet   return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3275e39e8530SAmaury Sechet }
3276e39e8530SAmaury Sechet 
LLVMSetCleanup(LLVMValueRef LandingPad,LLVMBool Val)3277ef860a24SChandler Carruth void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3278ef860a24SChandler Carruth   unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3279ef860a24SChandler Carruth }
3280ef860a24SChandler Carruth 
LLVMAddHandler(LLVMValueRef CatchSwitch,LLVMBasicBlockRef Dest)3281478fce9eSRobert Widmann void LLVMAddHandler(LLVMValueRef CatchSwitch, LLVMBasicBlockRef Dest) {
3282478fce9eSRobert Widmann   unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3283478fce9eSRobert Widmann }
3284478fce9eSRobert Widmann 
LLVMGetNumHandlers(LLVMValueRef CatchSwitch)3285478fce9eSRobert Widmann unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3286478fce9eSRobert Widmann   return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3287478fce9eSRobert Widmann }
3288478fce9eSRobert Widmann 
LLVMGetHandlers(LLVMValueRef CatchSwitch,LLVMBasicBlockRef * Handlers)3289478fce9eSRobert Widmann void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3290478fce9eSRobert Widmann   CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
32914444b343SKazu Hirata   for (const BasicBlock *H : CSI->handlers())
32924444b343SKazu Hirata     *Handlers++ = wrap(H);
3293478fce9eSRobert Widmann }
3294478fce9eSRobert Widmann 
LLVMGetParentCatchSwitch(LLVMValueRef CatchPad)3295478fce9eSRobert Widmann LLVMValueRef LLVMGetParentCatchSwitch(LLVMValueRef CatchPad) {
3296478fce9eSRobert Widmann   return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3297478fce9eSRobert Widmann }
3298478fce9eSRobert Widmann 
LLVMSetParentCatchSwitch(LLVMValueRef CatchPad,LLVMValueRef CatchSwitch)3299478fce9eSRobert Widmann void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad, LLVMValueRef CatchSwitch) {
3300478fce9eSRobert Widmann   unwrap<CatchPadInst>(CatchPad)
3301478fce9eSRobert Widmann     ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3302478fce9eSRobert Widmann }
3303478fce9eSRobert Widmann 
3304478fce9eSRobert Widmann /*--.. Funclets ...........................................................--*/
3305478fce9eSRobert Widmann 
LLVMGetArgOperand(LLVMValueRef Funclet,unsigned i)3306478fce9eSRobert Widmann LLVMValueRef LLVMGetArgOperand(LLVMValueRef Funclet, unsigned i) {
3307478fce9eSRobert Widmann   return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3308478fce9eSRobert Widmann }
3309478fce9eSRobert Widmann 
LLVMSetArgOperand(LLVMValueRef Funclet,unsigned i,LLVMValueRef value)3310478fce9eSRobert Widmann void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
3311478fce9eSRobert Widmann   unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3312478fce9eSRobert Widmann }
3313478fce9eSRobert Widmann 
3314ef860a24SChandler Carruth /*--.. Arithmetic ..........................................................--*/
3315ef860a24SChandler Carruth 
LLVMBuildAdd(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3316ef860a24SChandler Carruth LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3317ef860a24SChandler Carruth                           const char *Name) {
3318ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3319ef860a24SChandler Carruth }
3320ef860a24SChandler Carruth 
LLVMBuildNSWAdd(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3321ef860a24SChandler Carruth LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3322ef860a24SChandler Carruth                           const char *Name) {
3323ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3324ef860a24SChandler Carruth }
3325ef860a24SChandler Carruth 
LLVMBuildNUWAdd(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3326ef860a24SChandler Carruth LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3327ef860a24SChandler Carruth                           const char *Name) {
3328ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3329ef860a24SChandler Carruth }
3330ef860a24SChandler Carruth 
LLVMBuildFAdd(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3331ef860a24SChandler Carruth LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3332ef860a24SChandler Carruth                           const char *Name) {
3333ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3334ef860a24SChandler Carruth }
3335ef860a24SChandler Carruth 
LLVMBuildSub(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3336ef860a24SChandler Carruth LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3337ef860a24SChandler Carruth                           const char *Name) {
3338ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3339ef860a24SChandler Carruth }
3340ef860a24SChandler Carruth 
LLVMBuildNSWSub(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3341ef860a24SChandler Carruth LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3342ef860a24SChandler Carruth                           const char *Name) {
3343ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3344ef860a24SChandler Carruth }
3345ef860a24SChandler Carruth 
LLVMBuildNUWSub(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3346ef860a24SChandler Carruth LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3347ef860a24SChandler Carruth                           const char *Name) {
3348ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3349ef860a24SChandler Carruth }
3350ef860a24SChandler Carruth 
LLVMBuildFSub(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3351ef860a24SChandler Carruth LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3352ef860a24SChandler Carruth                           const char *Name) {
3353ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3354ef860a24SChandler Carruth }
3355ef860a24SChandler Carruth 
LLVMBuildMul(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3356ef860a24SChandler Carruth LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3357ef860a24SChandler Carruth                           const char *Name) {
3358ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3359ef860a24SChandler Carruth }
3360ef860a24SChandler Carruth 
LLVMBuildNSWMul(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3361ef860a24SChandler Carruth LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3362ef860a24SChandler Carruth                           const char *Name) {
3363ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3364ef860a24SChandler Carruth }
3365ef860a24SChandler Carruth 
LLVMBuildNUWMul(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3366ef860a24SChandler Carruth LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3367ef860a24SChandler Carruth                           const char *Name) {
3368ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3369ef860a24SChandler Carruth }
3370ef860a24SChandler Carruth 
LLVMBuildFMul(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3371ef860a24SChandler Carruth LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3372ef860a24SChandler Carruth                           const char *Name) {
3373ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3374ef860a24SChandler Carruth }
3375ef860a24SChandler Carruth 
LLVMBuildUDiv(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3376ef860a24SChandler Carruth LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3377ef860a24SChandler Carruth                            const char *Name) {
3378ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3379ef860a24SChandler Carruth }
3380ef860a24SChandler Carruth 
LLVMBuildExactUDiv(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)338149fafb11SManuel Jacob LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
338249fafb11SManuel Jacob                                 LLVMValueRef RHS, const char *Name) {
338349fafb11SManuel Jacob   return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
338449fafb11SManuel Jacob }
338549fafb11SManuel Jacob 
LLVMBuildSDiv(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3386ef860a24SChandler Carruth LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3387ef860a24SChandler Carruth                            const char *Name) {
3388ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3389ef860a24SChandler Carruth }
3390ef860a24SChandler Carruth 
LLVMBuildExactSDiv(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3391ef860a24SChandler Carruth LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
3392ef860a24SChandler Carruth                                 LLVMValueRef RHS, const char *Name) {
3393ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3394ef860a24SChandler Carruth }
3395ef860a24SChandler Carruth 
LLVMBuildFDiv(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3396ef860a24SChandler Carruth LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3397ef860a24SChandler Carruth                            const char *Name) {
3398ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3399ef860a24SChandler Carruth }
3400ef860a24SChandler Carruth 
LLVMBuildURem(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3401ef860a24SChandler Carruth LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3402ef860a24SChandler Carruth                            const char *Name) {
3403ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3404ef860a24SChandler Carruth }
3405ef860a24SChandler Carruth 
LLVMBuildSRem(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3406ef860a24SChandler Carruth LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3407ef860a24SChandler Carruth                            const char *Name) {
3408ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3409ef860a24SChandler Carruth }
3410ef860a24SChandler Carruth 
LLVMBuildFRem(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3411ef860a24SChandler Carruth LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3412ef860a24SChandler Carruth                            const char *Name) {
3413ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3414ef860a24SChandler Carruth }
3415ef860a24SChandler Carruth 
LLVMBuildShl(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3416ef860a24SChandler Carruth LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3417ef860a24SChandler Carruth                           const char *Name) {
3418ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3419ef860a24SChandler Carruth }
3420ef860a24SChandler Carruth 
LLVMBuildLShr(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3421ef860a24SChandler Carruth LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3422ef860a24SChandler Carruth                            const char *Name) {
3423ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3424ef860a24SChandler Carruth }
3425ef860a24SChandler Carruth 
LLVMBuildAShr(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3426ef860a24SChandler Carruth LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3427ef860a24SChandler Carruth                            const char *Name) {
3428ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3429ef860a24SChandler Carruth }
3430ef860a24SChandler Carruth 
LLVMBuildAnd(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3431ef860a24SChandler Carruth LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3432ef860a24SChandler Carruth                           const char *Name) {
3433ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3434ef860a24SChandler Carruth }
3435ef860a24SChandler Carruth 
LLVMBuildOr(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3436ef860a24SChandler Carruth LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3437ef860a24SChandler Carruth                          const char *Name) {
3438ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3439ef860a24SChandler Carruth }
3440ef860a24SChandler Carruth 
LLVMBuildXor(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3441ef860a24SChandler Carruth LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3442ef860a24SChandler Carruth                           const char *Name) {
3443ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3444ef860a24SChandler Carruth }
3445ef860a24SChandler Carruth 
LLVMBuildBinOp(LLVMBuilderRef B,LLVMOpcode Op,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3446ef860a24SChandler Carruth LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
3447ef860a24SChandler Carruth                             LLVMValueRef LHS, LLVMValueRef RHS,
3448ef860a24SChandler Carruth                             const char *Name) {
3449ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
3450ef860a24SChandler Carruth                                      unwrap(RHS), Name));
3451ef860a24SChandler Carruth }
3452ef860a24SChandler Carruth 
LLVMBuildNeg(LLVMBuilderRef B,LLVMValueRef V,const char * Name)3453ef860a24SChandler Carruth LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3454ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3455ef860a24SChandler Carruth }
3456ef860a24SChandler Carruth 
LLVMBuildNSWNeg(LLVMBuilderRef B,LLVMValueRef V,const char * Name)3457ef860a24SChandler Carruth LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
3458ef860a24SChandler Carruth                              const char *Name) {
3459ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3460ef860a24SChandler Carruth }
3461ef860a24SChandler Carruth 
LLVMBuildNUWNeg(LLVMBuilderRef B,LLVMValueRef V,const char * Name)3462ef860a24SChandler Carruth LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
3463ef860a24SChandler Carruth                              const char *Name) {
3464ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
3465ef860a24SChandler Carruth }
3466ef860a24SChandler Carruth 
LLVMBuildFNeg(LLVMBuilderRef B,LLVMValueRef V,const char * Name)3467ef860a24SChandler Carruth LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3468ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3469ef860a24SChandler Carruth }
3470ef860a24SChandler Carruth 
LLVMBuildNot(LLVMBuilderRef B,LLVMValueRef V,const char * Name)3471ef860a24SChandler Carruth LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3472ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3473ef860a24SChandler Carruth }
3474ef860a24SChandler Carruth 
3475ef860a24SChandler Carruth /*--.. Memory ..............................................................--*/
3476ef860a24SChandler Carruth 
LLVMBuildMalloc(LLVMBuilderRef B,LLVMTypeRef Ty,const char * Name)3477ef860a24SChandler Carruth LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
3478ef860a24SChandler Carruth                              const char *Name) {
3479ef860a24SChandler Carruth   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3480ef860a24SChandler Carruth   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3481ef860a24SChandler Carruth   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3482ef860a24SChandler Carruth   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3483ef860a24SChandler Carruth                                                ITy, unwrap(Ty), AllocSize,
3484c620761cSCraig Topper                                                nullptr, nullptr, "");
3485ef860a24SChandler Carruth   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3486ef860a24SChandler Carruth }
3487ef860a24SChandler Carruth 
LLVMBuildArrayMalloc(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef Val,const char * Name)3488ef860a24SChandler Carruth LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
3489ef860a24SChandler Carruth                                   LLVMValueRef Val, const char *Name) {
3490ef860a24SChandler Carruth   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3491ef860a24SChandler Carruth   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3492ef860a24SChandler Carruth   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3493ef860a24SChandler Carruth   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3494ef860a24SChandler Carruth                                                ITy, unwrap(Ty), AllocSize,
3495c620761cSCraig Topper                                                unwrap(Val), nullptr, "");
3496ef860a24SChandler Carruth   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3497ef860a24SChandler Carruth }
3498ef860a24SChandler Carruth 
LLVMBuildMemSet(LLVMBuilderRef B,LLVMValueRef Ptr,LLVMValueRef Val,LLVMValueRef Len,unsigned Align)349998640f84SRobert Widmann LLVMValueRef LLVMBuildMemSet(LLVMBuilderRef B, LLVMValueRef Ptr,
350098640f84SRobert Widmann                              LLVMValueRef Val, LLVMValueRef Len,
350198640f84SRobert Widmann                              unsigned Align) {
35021b2842bfSGuillaume Chatelet   return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len),
35031b2842bfSGuillaume Chatelet                                       MaybeAlign(Align)));
350498640f84SRobert Widmann }
350598640f84SRobert Widmann 
LLVMBuildMemCpy(LLVMBuilderRef B,LLVMValueRef Dst,unsigned DstAlign,LLVMValueRef Src,unsigned SrcAlign,LLVMValueRef Size)350698640f84SRobert Widmann LLVMValueRef LLVMBuildMemCpy(LLVMBuilderRef B,
350798640f84SRobert Widmann                              LLVMValueRef Dst, unsigned DstAlign,
350898640f84SRobert Widmann                              LLVMValueRef Src, unsigned SrcAlign,
350998640f84SRobert Widmann                              LLVMValueRef Size) {
3510531c1161SGuillaume Chatelet   return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign),
3511531c1161SGuillaume Chatelet                                       unwrap(Src), MaybeAlign(SrcAlign),
351298640f84SRobert Widmann                                       unwrap(Size)));
351398640f84SRobert Widmann }
351498640f84SRobert Widmann 
LLVMBuildMemMove(LLVMBuilderRef B,LLVMValueRef Dst,unsigned DstAlign,LLVMValueRef Src,unsigned SrcAlign,LLVMValueRef Size)351598640f84SRobert Widmann LLVMValueRef LLVMBuildMemMove(LLVMBuilderRef B,
351698640f84SRobert Widmann                               LLVMValueRef Dst, unsigned DstAlign,
351798640f84SRobert Widmann                               LLVMValueRef Src, unsigned SrcAlign,
351898640f84SRobert Widmann                               LLVMValueRef Size) {
3519531c1161SGuillaume Chatelet   return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), MaybeAlign(DstAlign),
3520531c1161SGuillaume Chatelet                                        unwrap(Src), MaybeAlign(SrcAlign),
352198640f84SRobert Widmann                                        unwrap(Size)));
352298640f84SRobert Widmann }
352398640f84SRobert Widmann 
LLVMBuildAlloca(LLVMBuilderRef B,LLVMTypeRef Ty,const char * Name)3524ef860a24SChandler Carruth LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
3525ef860a24SChandler Carruth                              const char *Name) {
3526c620761cSCraig Topper   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3527ef860a24SChandler Carruth }
3528ef860a24SChandler Carruth 
LLVMBuildArrayAlloca(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef Val,const char * Name)3529ef860a24SChandler Carruth LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
3530ef860a24SChandler Carruth                                   LLVMValueRef Val, const char *Name) {
3531ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3532ef860a24SChandler Carruth }
3533ef860a24SChandler Carruth 
LLVMBuildFree(LLVMBuilderRef B,LLVMValueRef PointerVal)3534ef860a24SChandler Carruth LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
3535ef860a24SChandler Carruth   return wrap(unwrap(B)->Insert(
3536ef860a24SChandler Carruth      CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
3537ef860a24SChandler Carruth }
3538ef860a24SChandler Carruth 
LLVMBuildLoad(LLVMBuilderRef B,LLVMValueRef PointerVal,const char * Name)3539ef860a24SChandler Carruth LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
3540ef860a24SChandler Carruth                            const char *Name) {
354184c1dbdeSJames Y Knight   Value *V = unwrap(PointerVal);
354284c1dbdeSJames Y Knight   PointerType *Ty = cast<PointerType>(V->getType());
354384c1dbdeSJames Y Knight 
3544d29e3192SNikita Popov   return wrap(
3545d29e3192SNikita Popov       unwrap(B)->CreateLoad(Ty->getNonOpaquePointerElementType(), V, Name));
354684c1dbdeSJames Y Knight }
354784c1dbdeSJames Y Knight 
LLVMBuildLoad2(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef PointerVal,const char * Name)354884c1dbdeSJames Y Knight LLVMValueRef LLVMBuildLoad2(LLVMBuilderRef B, LLVMTypeRef Ty,
354984c1dbdeSJames Y Knight                             LLVMValueRef PointerVal, const char *Name) {
355084c1dbdeSJames Y Knight   return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name));
3551ef860a24SChandler Carruth }
3552ef860a24SChandler Carruth 
LLVMBuildStore(LLVMBuilderRef B,LLVMValueRef Val,LLVMValueRef PointerVal)3553ef860a24SChandler Carruth LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
3554ef860a24SChandler Carruth                             LLVMValueRef PointerVal) {
3555ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3556ef860a24SChandler Carruth }
3557ef860a24SChandler Carruth 
mapFromLLVMOrdering(LLVMAtomicOrdering Ordering)35580d3f7ecaSFilip Pizlo static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
35590d3f7ecaSFilip Pizlo   switch (Ordering) {
3560800f87a8SJF Bastien     case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
3561800f87a8SJF Bastien     case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
3562800f87a8SJF Bastien     case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
3563800f87a8SJF Bastien     case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
3564800f87a8SJF Bastien     case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
3565800f87a8SJF Bastien     case LLVMAtomicOrderingAcquireRelease:
3566800f87a8SJF Bastien       return AtomicOrdering::AcquireRelease;
35670d3f7ecaSFilip Pizlo     case LLVMAtomicOrderingSequentiallyConsistent:
3568800f87a8SJF Bastien       return AtomicOrdering::SequentiallyConsistent;
35690d3f7ecaSFilip Pizlo   }
35700d3f7ecaSFilip Pizlo 
35710d3f7ecaSFilip Pizlo   llvm_unreachable("Invalid LLVMAtomicOrdering value!");
35720d3f7ecaSFilip Pizlo }
35730d3f7ecaSFilip Pizlo 
mapToLLVMOrdering(AtomicOrdering Ordering)3574b7362ce5SAndrew Wilkins static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
3575b7362ce5SAndrew Wilkins   switch (Ordering) {
3576800f87a8SJF Bastien     case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
3577800f87a8SJF Bastien     case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
3578800f87a8SJF Bastien     case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
3579800f87a8SJF Bastien     case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
3580800f87a8SJF Bastien     case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
3581800f87a8SJF Bastien     case AtomicOrdering::AcquireRelease:
3582800f87a8SJF Bastien       return LLVMAtomicOrderingAcquireRelease;
3583800f87a8SJF Bastien     case AtomicOrdering::SequentiallyConsistent:
3584b7362ce5SAndrew Wilkins       return LLVMAtomicOrderingSequentiallyConsistent;
3585b7362ce5SAndrew Wilkins   }
3586b7362ce5SAndrew Wilkins 
3587b7362ce5SAndrew Wilkins   llvm_unreachable("Invalid AtomicOrdering value!");
3588b7362ce5SAndrew Wilkins }
3589b7362ce5SAndrew Wilkins 
mapFromLLVMRMWBinOp(LLVMAtomicRMWBinOp BinOp)3590f57e968dSNick Lewycky static AtomicRMWInst::BinOp mapFromLLVMRMWBinOp(LLVMAtomicRMWBinOp BinOp) {
3591f57e968dSNick Lewycky   switch (BinOp) {
3592f57e968dSNick Lewycky     case LLVMAtomicRMWBinOpXchg: return AtomicRMWInst::Xchg;
3593f57e968dSNick Lewycky     case LLVMAtomicRMWBinOpAdd: return AtomicRMWInst::Add;
3594f57e968dSNick Lewycky     case LLVMAtomicRMWBinOpSub: return AtomicRMWInst::Sub;
3595f57e968dSNick Lewycky     case LLVMAtomicRMWBinOpAnd: return AtomicRMWInst::And;
3596f57e968dSNick Lewycky     case LLVMAtomicRMWBinOpNand: return AtomicRMWInst::Nand;
3597f57e968dSNick Lewycky     case LLVMAtomicRMWBinOpOr: return AtomicRMWInst::Or;
3598f57e968dSNick Lewycky     case LLVMAtomicRMWBinOpXor: return AtomicRMWInst::Xor;
3599f57e968dSNick Lewycky     case LLVMAtomicRMWBinOpMax: return AtomicRMWInst::Max;
3600f57e968dSNick Lewycky     case LLVMAtomicRMWBinOpMin: return AtomicRMWInst::Min;
3601f57e968dSNick Lewycky     case LLVMAtomicRMWBinOpUMax: return AtomicRMWInst::UMax;
3602f57e968dSNick Lewycky     case LLVMAtomicRMWBinOpUMin: return AtomicRMWInst::UMin;
3603f57e968dSNick Lewycky     case LLVMAtomicRMWBinOpFAdd: return AtomicRMWInst::FAdd;
3604f57e968dSNick Lewycky     case LLVMAtomicRMWBinOpFSub: return AtomicRMWInst::FSub;
36051023ddafSShilei Tian     case LLVMAtomicRMWBinOpFMax: return AtomicRMWInst::FMax;
36061023ddafSShilei Tian     case LLVMAtomicRMWBinOpFMin: return AtomicRMWInst::FMin;
3607f57e968dSNick Lewycky   }
3608f57e968dSNick Lewycky 
3609f57e968dSNick Lewycky   llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3610f57e968dSNick Lewycky }
3611f57e968dSNick Lewycky 
mapToLLVMRMWBinOp(AtomicRMWInst::BinOp BinOp)3612f57e968dSNick Lewycky static LLVMAtomicRMWBinOp mapToLLVMRMWBinOp(AtomicRMWInst::BinOp BinOp) {
3613f57e968dSNick Lewycky   switch (BinOp) {
3614f57e968dSNick Lewycky     case AtomicRMWInst::Xchg: return LLVMAtomicRMWBinOpXchg;
3615f57e968dSNick Lewycky     case AtomicRMWInst::Add: return LLVMAtomicRMWBinOpAdd;
3616f57e968dSNick Lewycky     case AtomicRMWInst::Sub: return LLVMAtomicRMWBinOpSub;
3617f57e968dSNick Lewycky     case AtomicRMWInst::And: return LLVMAtomicRMWBinOpAnd;
3618f57e968dSNick Lewycky     case AtomicRMWInst::Nand: return LLVMAtomicRMWBinOpNand;
3619f57e968dSNick Lewycky     case AtomicRMWInst::Or: return LLVMAtomicRMWBinOpOr;
3620f57e968dSNick Lewycky     case AtomicRMWInst::Xor: return LLVMAtomicRMWBinOpXor;
3621f57e968dSNick Lewycky     case AtomicRMWInst::Max: return LLVMAtomicRMWBinOpMax;
3622f57e968dSNick Lewycky     case AtomicRMWInst::Min: return LLVMAtomicRMWBinOpMin;
3623f57e968dSNick Lewycky     case AtomicRMWInst::UMax: return LLVMAtomicRMWBinOpUMax;
3624f57e968dSNick Lewycky     case AtomicRMWInst::UMin: return LLVMAtomicRMWBinOpUMin;
3625f57e968dSNick Lewycky     case AtomicRMWInst::FAdd: return LLVMAtomicRMWBinOpFAdd;
3626f57e968dSNick Lewycky     case AtomicRMWInst::FSub: return LLVMAtomicRMWBinOpFSub;
36271023ddafSShilei Tian     case AtomicRMWInst::FMax: return LLVMAtomicRMWBinOpFMax;
36281023ddafSShilei Tian     case AtomicRMWInst::FMin: return LLVMAtomicRMWBinOpFMin;
3629f57e968dSNick Lewycky     default: break;
3630f57e968dSNick Lewycky   }
3631f57e968dSNick Lewycky 
3632f57e968dSNick Lewycky   llvm_unreachable("Invalid AtomicRMWBinOp value!");
3633f57e968dSNick Lewycky }
3634f57e968dSNick Lewycky 
3635bb80d3e1SKonstantin Zhuravlyov // TODO: Should this and other atomic instructions support building with
3636bb80d3e1SKonstantin Zhuravlyov // "syncscope"?
LLVMBuildFence(LLVMBuilderRef B,LLVMAtomicOrdering Ordering,LLVMBool isSingleThread,const char * Name)36370d3f7ecaSFilip Pizlo LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
36380d3f7ecaSFilip Pizlo                             LLVMBool isSingleThread, const char *Name) {
36390d3f7ecaSFilip Pizlo   return wrap(
36400d3f7ecaSFilip Pizlo     unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3641bb80d3e1SKonstantin Zhuravlyov                            isSingleThread ? SyncScope::SingleThread
3642bb80d3e1SKonstantin Zhuravlyov                                           : SyncScope::System,
36430d3f7ecaSFilip Pizlo                            Name));
36440d3f7ecaSFilip Pizlo }
36450d3f7ecaSFilip Pizlo 
LLVMBuildGEP(LLVMBuilderRef B,LLVMValueRef Pointer,LLVMValueRef * Indices,unsigned NumIndices,const char * Name)3646ef860a24SChandler Carruth LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
3647ef860a24SChandler Carruth                           LLVMValueRef *Indices, unsigned NumIndices,
3648ef860a24SChandler Carruth                           const char *Name) {
3649ef860a24SChandler Carruth   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3650544fa425SJames Y Knight   Value *Val = unwrap(Pointer);
3651d29e3192SNikita Popov   Type *Ty = Val->getType()->getScalarType()->getNonOpaquePointerElementType();
3652544fa425SJames Y Knight   return wrap(unwrap(B)->CreateGEP(Ty, Val, IdxList, Name));
3653544fa425SJames Y Knight }
3654544fa425SJames Y Knight 
LLVMBuildGEP2(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef Pointer,LLVMValueRef * Indices,unsigned NumIndices,const char * Name)3655544fa425SJames Y Knight LLVMValueRef LLVMBuildGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
3656544fa425SJames Y Knight                            LLVMValueRef Pointer, LLVMValueRef *Indices,
3657544fa425SJames Y Knight                            unsigned NumIndices, const char *Name) {
3658544fa425SJames Y Knight   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3659544fa425SJames Y Knight   return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3660ef860a24SChandler Carruth }
3661ef860a24SChandler Carruth 
LLVMBuildInBoundsGEP(LLVMBuilderRef B,LLVMValueRef Pointer,LLVMValueRef * Indices,unsigned NumIndices,const char * Name)3662ef860a24SChandler Carruth LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
3663ef860a24SChandler Carruth                                   LLVMValueRef *Indices, unsigned NumIndices,
3664ef860a24SChandler Carruth                                   const char *Name) {
3665ef860a24SChandler Carruth   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3666544fa425SJames Y Knight   Value *Val = unwrap(Pointer);
3667d29e3192SNikita Popov   Type *Ty = Val->getType()->getScalarType()->getNonOpaquePointerElementType();
3668544fa425SJames Y Knight   return wrap(unwrap(B)->CreateInBoundsGEP(Ty, Val, IdxList, Name));
3669544fa425SJames Y Knight }
3670544fa425SJames Y Knight 
LLVMBuildInBoundsGEP2(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef Pointer,LLVMValueRef * Indices,unsigned NumIndices,const char * Name)3671544fa425SJames Y Knight LLVMValueRef LLVMBuildInBoundsGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
3672544fa425SJames Y Knight                                    LLVMValueRef Pointer, LLVMValueRef *Indices,
3673544fa425SJames Y Knight                                    unsigned NumIndices, const char *Name) {
3674544fa425SJames Y Knight   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3675aa41cd57SDavid Blaikie   return wrap(
3676544fa425SJames Y Knight       unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3677ef860a24SChandler Carruth }
3678ef860a24SChandler Carruth 
LLVMBuildStructGEP(LLVMBuilderRef B,LLVMValueRef Pointer,unsigned Idx,const char * Name)3679ef860a24SChandler Carruth LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
3680ef860a24SChandler Carruth                                 unsigned Idx, const char *Name) {
3681544fa425SJames Y Knight   Value *Val = unwrap(Pointer);
3682d29e3192SNikita Popov   Type *Ty = Val->getType()->getScalarType()->getNonOpaquePointerElementType();
3683544fa425SJames Y Knight   return wrap(unwrap(B)->CreateStructGEP(Ty, Val, Idx, Name));
3684544fa425SJames Y Knight }
3685544fa425SJames Y Knight 
LLVMBuildStructGEP2(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef Pointer,unsigned Idx,const char * Name)3686544fa425SJames Y Knight LLVMValueRef LLVMBuildStructGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
3687544fa425SJames Y Knight                                  LLVMValueRef Pointer, unsigned Idx,
3688544fa425SJames Y Knight                                  const char *Name) {
3689544fa425SJames Y Knight   return wrap(
3690544fa425SJames Y Knight       unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name));
3691ef860a24SChandler Carruth }
3692ef860a24SChandler Carruth 
LLVMBuildGlobalString(LLVMBuilderRef B,const char * Str,const char * Name)3693ef860a24SChandler Carruth LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
3694ef860a24SChandler Carruth                                    const char *Name) {
3695ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3696ef860a24SChandler Carruth }
3697ef860a24SChandler Carruth 
LLVMBuildGlobalStringPtr(LLVMBuilderRef B,const char * Str,const char * Name)3698ef860a24SChandler Carruth LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
3699ef860a24SChandler Carruth                                       const char *Name) {
3700ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
3701ef860a24SChandler Carruth }
3702ef860a24SChandler Carruth 
LLVMGetVolatile(LLVMValueRef MemAccessInst)3703ef860a24SChandler Carruth LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
3704ef860a24SChandler Carruth   Value *P = unwrap<Value>(MemAccessInst);
3705ef860a24SChandler Carruth   if (LoadInst *LI = dyn_cast<LoadInst>(P))
3706ef860a24SChandler Carruth     return LI->isVolatile();
3707f57e968dSNick Lewycky   if (StoreInst *SI = dyn_cast<StoreInst>(P))
3708f57e968dSNick Lewycky     return SI->isVolatile();
3709f57e968dSNick Lewycky   if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3710f57e968dSNick Lewycky     return AI->isVolatile();
3711f57e968dSNick Lewycky   return cast<AtomicCmpXchgInst>(P)->isVolatile();
3712ef860a24SChandler Carruth }
3713ef860a24SChandler Carruth 
LLVMSetVolatile(LLVMValueRef MemAccessInst,LLVMBool isVolatile)3714ef860a24SChandler Carruth void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
3715ef860a24SChandler Carruth   Value *P = unwrap<Value>(MemAccessInst);
3716ef860a24SChandler Carruth   if (LoadInst *LI = dyn_cast<LoadInst>(P))
3717ef860a24SChandler Carruth     return LI->setVolatile(isVolatile);
3718f57e968dSNick Lewycky   if (StoreInst *SI = dyn_cast<StoreInst>(P))
3719f57e968dSNick Lewycky     return SI->setVolatile(isVolatile);
3720f57e968dSNick Lewycky   if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3721f57e968dSNick Lewycky     return AI->setVolatile(isVolatile);
3722f57e968dSNick Lewycky   return cast<AtomicCmpXchgInst>(P)->setVolatile(isVolatile);
3723f57e968dSNick Lewycky }
3724f57e968dSNick Lewycky 
LLVMGetWeak(LLVMValueRef CmpXchgInst)3725f57e968dSNick Lewycky LLVMBool LLVMGetWeak(LLVMValueRef CmpXchgInst) {
3726f57e968dSNick Lewycky   return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->isWeak();
3727f57e968dSNick Lewycky }
3728f57e968dSNick Lewycky 
LLVMSetWeak(LLVMValueRef CmpXchgInst,LLVMBool isWeak)3729f57e968dSNick Lewycky void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) {
3730f57e968dSNick Lewycky   return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->setWeak(isWeak);
3731ef860a24SChandler Carruth }
3732ef860a24SChandler Carruth 
LLVMGetOrdering(LLVMValueRef MemAccessInst)3733b7362ce5SAndrew Wilkins LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
3734b7362ce5SAndrew Wilkins   Value *P = unwrap<Value>(MemAccessInst);
3735b7362ce5SAndrew Wilkins   AtomicOrdering O;
3736b7362ce5SAndrew Wilkins   if (LoadInst *LI = dyn_cast<LoadInst>(P))
3737b7362ce5SAndrew Wilkins     O = LI->getOrdering();
3738f57e968dSNick Lewycky   else if (StoreInst *SI = dyn_cast<StoreInst>(P))
3739f57e968dSNick Lewycky     O = SI->getOrdering();
3740b7362ce5SAndrew Wilkins   else
3741f57e968dSNick Lewycky     O = cast<AtomicRMWInst>(P)->getOrdering();
3742b7362ce5SAndrew Wilkins   return mapToLLVMOrdering(O);
3743b7362ce5SAndrew Wilkins }
3744b7362ce5SAndrew Wilkins 
LLVMSetOrdering(LLVMValueRef MemAccessInst,LLVMAtomicOrdering Ordering)3745b7362ce5SAndrew Wilkins void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
3746b7362ce5SAndrew Wilkins   Value *P = unwrap<Value>(MemAccessInst);
3747b7362ce5SAndrew Wilkins   AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3748b7362ce5SAndrew Wilkins 
3749b7362ce5SAndrew Wilkins   if (LoadInst *LI = dyn_cast<LoadInst>(P))
3750b7362ce5SAndrew Wilkins     return LI->setOrdering(O);
3751b7362ce5SAndrew Wilkins   return cast<StoreInst>(P)->setOrdering(O);
3752b7362ce5SAndrew Wilkins }
3753b7362ce5SAndrew Wilkins 
LLVMGetAtomicRMWBinOp(LLVMValueRef Inst)3754f57e968dSNick Lewycky LLVMAtomicRMWBinOp LLVMGetAtomicRMWBinOp(LLVMValueRef Inst) {
3755f57e968dSNick Lewycky   return mapToLLVMRMWBinOp(unwrap<AtomicRMWInst>(Inst)->getOperation());
3756f57e968dSNick Lewycky }
3757f57e968dSNick Lewycky 
LLVMSetAtomicRMWBinOp(LLVMValueRef Inst,LLVMAtomicRMWBinOp BinOp)3758f57e968dSNick Lewycky void LLVMSetAtomicRMWBinOp(LLVMValueRef Inst, LLVMAtomicRMWBinOp BinOp) {
3759f57e968dSNick Lewycky   unwrap<AtomicRMWInst>(Inst)->setOperation(mapFromLLVMRMWBinOp(BinOp));
3760f57e968dSNick Lewycky }
3761f57e968dSNick Lewycky 
3762ef860a24SChandler Carruth /*--.. Casts ...............................................................--*/
3763ef860a24SChandler Carruth 
LLVMBuildTrunc(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3764ef860a24SChandler Carruth LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
3765ef860a24SChandler Carruth                             LLVMTypeRef DestTy, const char *Name) {
3766ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
3767ef860a24SChandler Carruth }
3768ef860a24SChandler Carruth 
LLVMBuildZExt(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3769ef860a24SChandler Carruth LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
3770ef860a24SChandler Carruth                            LLVMTypeRef DestTy, const char *Name) {
3771ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
3772ef860a24SChandler Carruth }
3773ef860a24SChandler Carruth 
LLVMBuildSExt(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3774ef860a24SChandler Carruth LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
3775ef860a24SChandler Carruth                            LLVMTypeRef DestTy, const char *Name) {
3776ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
3777ef860a24SChandler Carruth }
3778ef860a24SChandler Carruth 
LLVMBuildFPToUI(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3779ef860a24SChandler Carruth LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
3780ef860a24SChandler Carruth                              LLVMTypeRef DestTy, const char *Name) {
3781ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
3782ef860a24SChandler Carruth }
3783ef860a24SChandler Carruth 
LLVMBuildFPToSI(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3784ef860a24SChandler Carruth LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
3785ef860a24SChandler Carruth                              LLVMTypeRef DestTy, const char *Name) {
3786ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
3787ef860a24SChandler Carruth }
3788ef860a24SChandler Carruth 
LLVMBuildUIToFP(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3789ef860a24SChandler Carruth LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
3790ef860a24SChandler Carruth                              LLVMTypeRef DestTy, const char *Name) {
3791ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
3792ef860a24SChandler Carruth }
3793ef860a24SChandler Carruth 
LLVMBuildSIToFP(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3794ef860a24SChandler Carruth LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
3795ef860a24SChandler Carruth                              LLVMTypeRef DestTy, const char *Name) {
3796ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
3797ef860a24SChandler Carruth }
3798ef860a24SChandler Carruth 
LLVMBuildFPTrunc(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3799ef860a24SChandler Carruth LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
3800ef860a24SChandler Carruth                               LLVMTypeRef DestTy, const char *Name) {
3801ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
3802ef860a24SChandler Carruth }
3803ef860a24SChandler Carruth 
LLVMBuildFPExt(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3804ef860a24SChandler Carruth LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
3805ef860a24SChandler Carruth                             LLVMTypeRef DestTy, const char *Name) {
3806ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
3807ef860a24SChandler Carruth }
3808ef860a24SChandler Carruth 
LLVMBuildPtrToInt(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3809ef860a24SChandler Carruth LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
3810ef860a24SChandler Carruth                                LLVMTypeRef DestTy, const char *Name) {
3811ef860a24SChandler Carruth   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
3812ef860a24SChandler Carruth }
3813ef860a24SChandler Carruth 
LLVMBuildIntToPtr(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3814ef860a24SChandler Carruth LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
3815ef860a24SChandler Carruth                                LLVMTypeRef DestTy, const char *Name) {
3816ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
3817ef860a24SChandler Carruth }
3818ef860a24SChandler Carruth 
LLVMBuildBitCast(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3819ef860a24SChandler Carruth LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3820ef860a24SChandler Carruth                               LLVMTypeRef DestTy, const char *Name) {
3821ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
3822ef860a24SChandler Carruth }
3823ef860a24SChandler Carruth 
LLVMBuildAddrSpaceCast(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3824b03bd4d9SMatt Arsenault LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
3825b03bd4d9SMatt Arsenault                                     LLVMTypeRef DestTy, const char *Name) {
3826b03bd4d9SMatt Arsenault   return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
3827b03bd4d9SMatt Arsenault }
3828b03bd4d9SMatt Arsenault 
LLVMBuildZExtOrBitCast(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3829ef860a24SChandler Carruth LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3830ef860a24SChandler Carruth                                     LLVMTypeRef DestTy, const char *Name) {
3831ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
3832ef860a24SChandler Carruth                                              Name));
3833ef860a24SChandler Carruth }
3834ef860a24SChandler Carruth 
LLVMBuildSExtOrBitCast(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3835ef860a24SChandler Carruth LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3836ef860a24SChandler Carruth                                     LLVMTypeRef DestTy, const char *Name) {
3837ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
3838ef860a24SChandler Carruth                                              Name));
3839ef860a24SChandler Carruth }
3840ef860a24SChandler Carruth 
LLVMBuildTruncOrBitCast(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3841ef860a24SChandler Carruth LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3842ef860a24SChandler Carruth                                      LLVMTypeRef DestTy, const char *Name) {
3843ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
3844ef860a24SChandler Carruth                                               Name));
3845ef860a24SChandler Carruth }
3846ef860a24SChandler Carruth 
LLVMBuildCast(LLVMBuilderRef B,LLVMOpcode Op,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3847ef860a24SChandler Carruth LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
3848ef860a24SChandler Carruth                            LLVMTypeRef DestTy, const char *Name) {
3849ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
3850ef860a24SChandler Carruth                                     unwrap(DestTy), Name));
3851ef860a24SChandler Carruth }
3852ef860a24SChandler Carruth 
LLVMBuildPointerCast(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3853ef860a24SChandler Carruth LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
3854ef860a24SChandler Carruth                                   LLVMTypeRef DestTy, const char *Name) {
3855ef860a24SChandler Carruth   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
3856ef860a24SChandler Carruth }
3857ef860a24SChandler Carruth 
LLVMBuildIntCast2(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,LLVMBool IsSigned,const char * Name)385840dc48beSRobert Widmann LLVMValueRef LLVMBuildIntCast2(LLVMBuilderRef B, LLVMValueRef Val,
385940dc48beSRobert Widmann                                LLVMTypeRef DestTy, LLVMBool IsSigned,
386068729f94SJames Y Knight                                const char *Name) {
386168729f94SJames Y Knight   return wrap(
386268729f94SJames Y Knight       unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), IsSigned, Name));
386340dc48beSRobert Widmann }
386440dc48beSRobert Widmann 
LLVMBuildIntCast(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3865ef860a24SChandler Carruth LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
3866ef860a24SChandler Carruth                               LLVMTypeRef DestTy, const char *Name) {
3867ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
3868ef860a24SChandler Carruth                                        /*isSigned*/true, Name));
3869ef860a24SChandler Carruth }
3870ef860a24SChandler Carruth 
LLVMBuildFPCast(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)3871ef860a24SChandler Carruth LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
3872ef860a24SChandler Carruth                              LLVMTypeRef DestTy, const char *Name) {
3873ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
3874ef860a24SChandler Carruth }
3875ef860a24SChandler Carruth 
LLVMGetCastOpcode(LLVMValueRef Src,LLVMBool SrcIsSigned,LLVMTypeRef DestTy,LLVMBool DestIsSigned)387609325d36SJack Andersen LLVMOpcode LLVMGetCastOpcode(LLVMValueRef Src, LLVMBool SrcIsSigned,
387709325d36SJack Andersen                              LLVMTypeRef DestTy, LLVMBool DestIsSigned) {
387809325d36SJack Andersen   return map_to_llvmopcode(CastInst::getCastOpcode(
387909325d36SJack Andersen       unwrap(Src), SrcIsSigned, unwrap(DestTy), DestIsSigned));
388009325d36SJack Andersen }
388109325d36SJack Andersen 
3882ef860a24SChandler Carruth /*--.. Comparisons .........................................................--*/
3883ef860a24SChandler Carruth 
LLVMBuildICmp(LLVMBuilderRef B,LLVMIntPredicate Op,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3884ef860a24SChandler Carruth LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
3885ef860a24SChandler Carruth                            LLVMValueRef LHS, LLVMValueRef RHS,
3886ef860a24SChandler Carruth                            const char *Name) {
3887ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
3888ef860a24SChandler Carruth                                     unwrap(LHS), unwrap(RHS), Name));
3889ef860a24SChandler Carruth }
3890ef860a24SChandler Carruth 
LLVMBuildFCmp(LLVMBuilderRef B,LLVMRealPredicate Op,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3891ef860a24SChandler Carruth LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
3892ef860a24SChandler Carruth                            LLVMValueRef LHS, LLVMValueRef RHS,
3893ef860a24SChandler Carruth                            const char *Name) {
3894ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
3895ef860a24SChandler Carruth                                     unwrap(LHS), unwrap(RHS), Name));
3896ef860a24SChandler Carruth }
3897ef860a24SChandler Carruth 
3898ef860a24SChandler Carruth /*--.. Miscellaneous instructions ..........................................--*/
3899ef860a24SChandler Carruth 
LLVMBuildPhi(LLVMBuilderRef B,LLVMTypeRef Ty,const char * Name)3900ef860a24SChandler Carruth LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
3901ef860a24SChandler Carruth   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
3902ef860a24SChandler Carruth }
3903ef860a24SChandler Carruth 
LLVMBuildCall(LLVMBuilderRef B,LLVMValueRef Fn,LLVMValueRef * Args,unsigned NumArgs,const char * Name)3904ef860a24SChandler Carruth LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
3905ef860a24SChandler Carruth                            LLVMValueRef *Args, unsigned NumArgs,
3906ef860a24SChandler Carruth                            const char *Name) {
3907f9563909SJames Y Knight   Value *V = unwrap(Fn);
3908f9563909SJames Y Knight   FunctionType *FnT =
3909d29e3192SNikita Popov       cast<FunctionType>(V->getType()->getNonOpaquePointerElementType());
3910f9563909SJames Y Knight 
3911f9563909SJames Y Knight   return wrap(unwrap(B)->CreateCall(FnT, unwrap(Fn),
3912f9563909SJames Y Knight                                     makeArrayRef(unwrap(Args), NumArgs), Name));
3913f9563909SJames Y Knight }
3914f9563909SJames Y Knight 
LLVMBuildCall2(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef Fn,LLVMValueRef * Args,unsigned NumArgs,const char * Name)3915f9563909SJames Y Knight LLVMValueRef LLVMBuildCall2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
3916f9563909SJames Y Knight                             LLVMValueRef *Args, unsigned NumArgs,
3917f9563909SJames Y Knight                             const char *Name) {
3918f9563909SJames Y Knight   FunctionType *FTy = unwrap<FunctionType>(Ty);
3919f9563909SJames Y Knight   return wrap(unwrap(B)->CreateCall(FTy, unwrap(Fn),
3920f9563909SJames Y Knight                                     makeArrayRef(unwrap(Args), NumArgs), Name));
3921ef860a24SChandler Carruth }
3922ef860a24SChandler Carruth 
LLVMBuildSelect(LLVMBuilderRef B,LLVMValueRef If,LLVMValueRef Then,LLVMValueRef Else,const char * Name)3923ef860a24SChandler Carruth LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
3924ef860a24SChandler Carruth                              LLVMValueRef Then, LLVMValueRef Else,
3925ef860a24SChandler Carruth                              const char *Name) {
3926ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
3927ef860a24SChandler Carruth                                       Name));
3928ef860a24SChandler Carruth }
3929ef860a24SChandler Carruth 
LLVMBuildVAArg(LLVMBuilderRef B,LLVMValueRef List,LLVMTypeRef Ty,const char * Name)3930ef860a24SChandler Carruth LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
3931ef860a24SChandler Carruth                             LLVMTypeRef Ty, const char *Name) {
3932ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
3933ef860a24SChandler Carruth }
3934ef860a24SChandler Carruth 
LLVMBuildExtractElement(LLVMBuilderRef B,LLVMValueRef VecVal,LLVMValueRef Index,const char * Name)3935ef860a24SChandler Carruth LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
3936ef860a24SChandler Carruth                                       LLVMValueRef Index, const char *Name) {
3937ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
3938ef860a24SChandler Carruth                                               Name));
3939ef860a24SChandler Carruth }
3940ef860a24SChandler Carruth 
LLVMBuildInsertElement(LLVMBuilderRef B,LLVMValueRef VecVal,LLVMValueRef EltVal,LLVMValueRef Index,const char * Name)3941ef860a24SChandler Carruth LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
3942ef860a24SChandler Carruth                                     LLVMValueRef EltVal, LLVMValueRef Index,
3943ef860a24SChandler Carruth                                     const char *Name) {
3944ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
3945ef860a24SChandler Carruth                                              unwrap(Index), Name));
3946ef860a24SChandler Carruth }
3947ef860a24SChandler Carruth 
LLVMBuildShuffleVector(LLVMBuilderRef B,LLVMValueRef V1,LLVMValueRef V2,LLVMValueRef Mask,const char * Name)3948ef860a24SChandler Carruth LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
3949ef860a24SChandler Carruth                                     LLVMValueRef V2, LLVMValueRef Mask,
3950ef860a24SChandler Carruth                                     const char *Name) {
3951ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
3952ef860a24SChandler Carruth                                              unwrap(Mask), Name));
3953ef860a24SChandler Carruth }
3954ef860a24SChandler Carruth 
LLVMBuildExtractValue(LLVMBuilderRef B,LLVMValueRef AggVal,unsigned Index,const char * Name)3955ef860a24SChandler Carruth LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3956ef860a24SChandler Carruth                                    unsigned Index, const char *Name) {
3957ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
3958ef860a24SChandler Carruth }
3959ef860a24SChandler Carruth 
LLVMBuildInsertValue(LLVMBuilderRef B,LLVMValueRef AggVal,LLVMValueRef EltVal,unsigned Index,const char * Name)3960ef860a24SChandler Carruth LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3961ef860a24SChandler Carruth                                   LLVMValueRef EltVal, unsigned Index,
3962ef860a24SChandler Carruth                                   const char *Name) {
3963ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3964ef860a24SChandler Carruth                                            Index, Name));
3965ef860a24SChandler Carruth }
3966ef860a24SChandler Carruth 
LLVMBuildFreeze(LLVMBuilderRef B,LLVMValueRef Val,const char * Name)3967e87d7166Saqjune LLVMValueRef LLVMBuildFreeze(LLVMBuilderRef B, LLVMValueRef Val,
3968e87d7166Saqjune                              const char *Name) {
3969e87d7166Saqjune   return wrap(unwrap(B)->CreateFreeze(unwrap(Val), Name));
3970e87d7166Saqjune }
3971e87d7166Saqjune 
LLVMBuildIsNull(LLVMBuilderRef B,LLVMValueRef Val,const char * Name)3972ef860a24SChandler Carruth LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
3973ef860a24SChandler Carruth                              const char *Name) {
3974ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
3975ef860a24SChandler Carruth }
3976ef860a24SChandler Carruth 
LLVMBuildIsNotNull(LLVMBuilderRef B,LLVMValueRef Val,const char * Name)3977ef860a24SChandler Carruth LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
3978ef860a24SChandler Carruth                                 const char *Name) {
3979ef860a24SChandler Carruth   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3980ef860a24SChandler Carruth }
3981ef860a24SChandler Carruth 
LLVMBuildPtrDiff(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3982ef860a24SChandler Carruth LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
3983ef860a24SChandler Carruth                               LLVMValueRef RHS, const char *Name) {
398430d4a7e2SNikita Popov   Value *L = unwrap(LHS);
3985d8962b41SNikita Popov   Type *ElemTy = L->getType()->getNonOpaquePointerElementType();
398630d4a7e2SNikita Popov   return wrap(unwrap(B)->CreatePtrDiff(ElemTy, L, unwrap(RHS), Name));
3987ef860a24SChandler Carruth }
3988ef860a24SChandler Carruth 
LLVMBuildPtrDiff2(LLVMBuilderRef B,LLVMTypeRef ElemTy,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)3989d8962b41SNikita Popov LLVMValueRef LLVMBuildPtrDiff2(LLVMBuilderRef B, LLVMTypeRef ElemTy,
3990d8962b41SNikita Popov                                LLVMValueRef LHS, LLVMValueRef RHS,
3991d8962b41SNikita Popov                                const char *Name) {
3992d8962b41SNikita Popov   return wrap(unwrap(B)->CreatePtrDiff(unwrap(ElemTy), unwrap(LHS),
3993d8962b41SNikita Popov                                        unwrap(RHS), Name));
3994d8962b41SNikita Popov }
3995d8962b41SNikita Popov 
LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,LLVMValueRef PTR,LLVMValueRef Val,LLVMAtomicOrdering ordering,LLVMBool singleThread)39968c6719bfSCarlo Kok LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
39978c6719bfSCarlo Kok                                LLVMValueRef PTR, LLVMValueRef Val,
39988c6719bfSCarlo Kok                                LLVMAtomicOrdering ordering,
39998c6719bfSCarlo Kok                                LLVMBool singleThread) {
4000f57e968dSNick Lewycky   AtomicRMWInst::BinOp intop = mapFromLLVMRMWBinOp(op);
400124539f1eSJames Y Knight   return wrap(unwrap(B)->CreateAtomicRMW(
400224539f1eSJames Y Knight       intop, unwrap(PTR), unwrap(Val), MaybeAlign(),
400324539f1eSJames Y Knight       mapFromLLVMOrdering(ordering),
400424539f1eSJames Y Knight       singleThread ? SyncScope::SingleThread : SyncScope::System));
40058c6719bfSCarlo Kok }
40068c6719bfSCarlo Kok 
LLVMBuildAtomicCmpXchg(LLVMBuilderRef B,LLVMValueRef Ptr,LLVMValueRef Cmp,LLVMValueRef New,LLVMAtomicOrdering SuccessOrdering,LLVMAtomicOrdering FailureOrdering,LLVMBool singleThread)400743165d91SMehdi Amini LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
400843165d91SMehdi Amini                                     LLVMValueRef Cmp, LLVMValueRef New,
400943165d91SMehdi Amini                                     LLVMAtomicOrdering SuccessOrdering,
401043165d91SMehdi Amini                                     LLVMAtomicOrdering FailureOrdering,
401143165d91SMehdi Amini                                     LLVMBool singleThread) {
401243165d91SMehdi Amini 
401324539f1eSJames Y Knight   return wrap(unwrap(B)->CreateAtomicCmpXchg(
401424539f1eSJames Y Knight       unwrap(Ptr), unwrap(Cmp), unwrap(New), MaybeAlign(),
401524539f1eSJames Y Knight       mapFromLLVMOrdering(SuccessOrdering),
401643165d91SMehdi Amini       mapFromLLVMOrdering(FailureOrdering),
4017bb80d3e1SKonstantin Zhuravlyov       singleThread ? SyncScope::SingleThread : SyncScope::System));
401843165d91SMehdi Amini }
401943165d91SMehdi Amini 
LLVMGetNumMaskElements(LLVMValueRef SVInst)402051cad041SCraig Disselkoen unsigned LLVMGetNumMaskElements(LLVMValueRef SVInst) {
402151cad041SCraig Disselkoen   Value *P = unwrap<Value>(SVInst);
402251cad041SCraig Disselkoen   ShuffleVectorInst *I = cast<ShuffleVectorInst>(P);
402351cad041SCraig Disselkoen   return I->getShuffleMask().size();
402451cad041SCraig Disselkoen }
402551cad041SCraig Disselkoen 
LLVMGetMaskValue(LLVMValueRef SVInst,unsigned Elt)402651cad041SCraig Disselkoen int LLVMGetMaskValue(LLVMValueRef SVInst, unsigned Elt) {
402751cad041SCraig Disselkoen   Value *P = unwrap<Value>(SVInst);
402851cad041SCraig Disselkoen   ShuffleVectorInst *I = cast<ShuffleVectorInst>(P);
402951cad041SCraig Disselkoen   return I->getMaskValue(Elt);
403051cad041SCraig Disselkoen }
403155f72730SRobert Widmann 
LLVMGetUndefMaskElem(void)403255f72730SRobert Widmann int LLVMGetUndefMaskElem(void) { return UndefMaskElem; }
403343165d91SMehdi Amini 
LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst)403443165d91SMehdi Amini LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
403543165d91SMehdi Amini   Value *P = unwrap<Value>(AtomicInst);
403643165d91SMehdi Amini 
403743165d91SMehdi Amini   if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
4038bb80d3e1SKonstantin Zhuravlyov     return I->getSyncScopeID() == SyncScope::SingleThread;
4039bb80d3e1SKonstantin Zhuravlyov   return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() ==
4040bb80d3e1SKonstantin Zhuravlyov              SyncScope::SingleThread;
404143165d91SMehdi Amini }
404243165d91SMehdi Amini 
LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst,LLVMBool NewValue)404343165d91SMehdi Amini void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
404443165d91SMehdi Amini   Value *P = unwrap<Value>(AtomicInst);
4045bb80d3e1SKonstantin Zhuravlyov   SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
404643165d91SMehdi Amini 
404743165d91SMehdi Amini   if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
4048bb80d3e1SKonstantin Zhuravlyov     return I->setSyncScopeID(SSID);
4049bb80d3e1SKonstantin Zhuravlyov   return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID);
405043165d91SMehdi Amini }
405143165d91SMehdi Amini 
LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst)405243165d91SMehdi Amini LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst)  {
405343165d91SMehdi Amini   Value *P = unwrap<Value>(CmpXchgInst);
405443165d91SMehdi Amini   return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
405543165d91SMehdi Amini }
405643165d91SMehdi Amini 
LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,LLVMAtomicOrdering Ordering)405743165d91SMehdi Amini void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
405843165d91SMehdi Amini                                    LLVMAtomicOrdering Ordering) {
405943165d91SMehdi Amini   Value *P = unwrap<Value>(CmpXchgInst);
406043165d91SMehdi Amini   AtomicOrdering O = mapFromLLVMOrdering(Ordering);
406143165d91SMehdi Amini 
406243165d91SMehdi Amini   return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
406343165d91SMehdi Amini }
406443165d91SMehdi Amini 
LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst)406543165d91SMehdi Amini LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst)  {
406643165d91SMehdi Amini   Value *P = unwrap<Value>(CmpXchgInst);
406743165d91SMehdi Amini   return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
406843165d91SMehdi Amini }
406943165d91SMehdi Amini 
LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,LLVMAtomicOrdering Ordering)407043165d91SMehdi Amini void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
407143165d91SMehdi Amini                                    LLVMAtomicOrdering Ordering) {
407243165d91SMehdi Amini   Value *P = unwrap<Value>(CmpXchgInst);
407343165d91SMehdi Amini   AtomicOrdering O = mapFromLLVMOrdering(Ordering);
407443165d91SMehdi Amini 
407543165d91SMehdi Amini   return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
407643165d91SMehdi Amini }
4077ef860a24SChandler Carruth 
4078ef860a24SChandler Carruth /*===-- Module providers --------------------------------------------------===*/
4079ef860a24SChandler Carruth 
4080ef860a24SChandler Carruth LLVMModuleProviderRef
LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M)4081ef860a24SChandler Carruth LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
4082ef860a24SChandler Carruth   return reinterpret_cast<LLVMModuleProviderRef>(M);
4083ef860a24SChandler Carruth }
4084ef860a24SChandler Carruth 
LLVMDisposeModuleProvider(LLVMModuleProviderRef MP)4085ef860a24SChandler Carruth void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
4086ef860a24SChandler Carruth   delete unwrap(MP);
4087ef860a24SChandler Carruth }
4088ef860a24SChandler Carruth 
4089ef860a24SChandler Carruth 
4090ef860a24SChandler Carruth /*===-- Memory buffers ----------------------------------------------------===*/
4091ef860a24SChandler Carruth 
LLVMCreateMemoryBufferWithContentsOfFile(const char * Path,LLVMMemoryBufferRef * OutMemBuf,char ** OutMessage)4092ef860a24SChandler Carruth LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
4093ef860a24SChandler Carruth     const char *Path,
4094ef860a24SChandler Carruth     LLVMMemoryBufferRef *OutMemBuf,
4095ef860a24SChandler Carruth     char **OutMessage) {
4096ef860a24SChandler Carruth 
4097adf21f2aSRafael Espindola   ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
4098adf21f2aSRafael Espindola   if (std::error_code EC = MBOrErr.getError()) {
4099adf21f2aSRafael Espindola     *OutMessage = strdup(EC.message().c_str());
4100ef860a24SChandler Carruth     return 1;
4101ef860a24SChandler Carruth   }
4102adf21f2aSRafael Espindola   *OutMemBuf = wrap(MBOrErr.get().release());
4103adf21f2aSRafael Espindola   return 0;
4104adf21f2aSRafael Espindola }
4105ef860a24SChandler Carruth 
LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef * OutMemBuf,char ** OutMessage)4106ef860a24SChandler Carruth LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
4107ef860a24SChandler Carruth                                          char **OutMessage) {
4108adf21f2aSRafael Espindola   ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
4109adf21f2aSRafael Espindola   if (std::error_code EC = MBOrErr.getError()) {
4110adf21f2aSRafael Espindola     *OutMessage = strdup(EC.message().c_str());
4111ef860a24SChandler Carruth     return 1;
4112ef860a24SChandler Carruth   }
4113adf21f2aSRafael Espindola   *OutMemBuf = wrap(MBOrErr.get().release());
4114adf21f2aSRafael Espindola   return 0;
4115adf21f2aSRafael Espindola }
4116ef860a24SChandler Carruth 
LLVMCreateMemoryBufferWithMemoryRange(const char * InputData,size_t InputDataLength,const char * BufferName,LLVMBool RequiresNullTerminator)4117526276afSBill Wendling LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
4118526276afSBill Wendling     const char *InputData,
4119526276afSBill Wendling     size_t InputDataLength,
4120526276afSBill Wendling     const char *BufferName,
4121ff61da96SBill Wendling     LLVMBool RequiresNullTerminator) {
4122526276afSBill Wendling 
41233560ff2cSRafael Espindola   return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
4124526276afSBill Wendling                                          StringRef(BufferName),
41253560ff2cSRafael Espindola                                          RequiresNullTerminator).release());
4126526276afSBill Wendling }
4127526276afSBill Wendling 
LLVMCreateMemoryBufferWithMemoryRangeCopy(const char * InputData,size_t InputDataLength,const char * BufferName)4128526276afSBill Wendling LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
4129526276afSBill Wendling     const char *InputData,
4130526276afSBill Wendling     size_t InputDataLength,
4131526276afSBill Wendling     const char *BufferName) {
4132526276afSBill Wendling 
41333560ff2cSRafael Espindola   return wrap(
41343560ff2cSRafael Espindola       MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
41353560ff2cSRafael Espindola                                      StringRef(BufferName)).release());
4136526276afSBill Wendling }
4137526276afSBill Wendling 
LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf)4138385fa26fSTom Stellard const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
4139385fa26fSTom Stellard   return unwrap(MemBuf)->getBufferStart();
4140385fa26fSTom Stellard }
4141526276afSBill Wendling 
LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf)4142b7fb724bSTom Stellard size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
4143b7fb724bSTom Stellard   return unwrap(MemBuf)->getBufferSize();
4144b7fb724bSTom Stellard }
4145b7fb724bSTom Stellard 
LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf)4146ef860a24SChandler Carruth void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
4147ef860a24SChandler Carruth   delete unwrap(MemBuf);
4148ef860a24SChandler Carruth }
4149ef860a24SChandler Carruth 
4150ef860a24SChandler Carruth /*===-- Pass Registry -----------------------------------------------------===*/
4151ef860a24SChandler Carruth 
LLVMGetGlobalPassRegistry(void)4152ef860a24SChandler Carruth LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
4153ef860a24SChandler Carruth   return wrap(PassRegistry::getPassRegistry());
4154ef860a24SChandler Carruth }
4155ef860a24SChandler Carruth 
4156ef860a24SChandler Carruth /*===-- Pass Manager ------------------------------------------------------===*/
4157ef860a24SChandler Carruth 
LLVMCreatePassManager()4158ef860a24SChandler Carruth LLVMPassManagerRef LLVMCreatePassManager() {
415930d69c2eSChandler Carruth   return wrap(new legacy::PassManager());
4160ef860a24SChandler Carruth }
4161ef860a24SChandler Carruth 
LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M)4162ef860a24SChandler Carruth LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
416330d69c2eSChandler Carruth   return wrap(new legacy::FunctionPassManager(unwrap(M)));
4164ef860a24SChandler Carruth }
4165ef860a24SChandler Carruth 
LLVMCreateFunctionPassManager(LLVMModuleProviderRef P)4166ef860a24SChandler Carruth LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
4167ef860a24SChandler Carruth   return LLVMCreateFunctionPassManagerForModule(
4168ef860a24SChandler Carruth                                             reinterpret_cast<LLVMModuleRef>(P));
4169ef860a24SChandler Carruth }
4170ef860a24SChandler Carruth 
LLVMRunPassManager(LLVMPassManagerRef PM,LLVMModuleRef M)4171ef860a24SChandler Carruth LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
417230d69c2eSChandler Carruth   return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
4173ef860a24SChandler Carruth }
4174ef860a24SChandler Carruth 
LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM)4175ef860a24SChandler Carruth LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
417630d69c2eSChandler Carruth   return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
4177ef860a24SChandler Carruth }
4178ef860a24SChandler Carruth 
LLVMRunFunctionPassManager(LLVMPassManagerRef FPM,LLVMValueRef F)4179ef860a24SChandler Carruth LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
418030d69c2eSChandler Carruth   return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
4181ef860a24SChandler Carruth }
4182ef860a24SChandler Carruth 
LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM)4183ef860a24SChandler Carruth LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
418430d69c2eSChandler Carruth   return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
4185ef860a24SChandler Carruth }
4186ef860a24SChandler Carruth 
LLVMDisposePassManager(LLVMPassManagerRef PM)4187ef860a24SChandler Carruth void LLVMDisposePassManager(LLVMPassManagerRef PM) {
4188ef860a24SChandler Carruth   delete unwrap(PM);
4189ef860a24SChandler Carruth }
41901cba0a8eSDuncan Sands 
41911cba0a8eSDuncan Sands /*===-- Threading ------------------------------------------------------===*/
41921cba0a8eSDuncan Sands 
LLVMStartMultithreaded()41931cba0a8eSDuncan Sands LLVMBool LLVMStartMultithreaded() {
419439cd216fSChandler Carruth   return LLVMIsMultithreaded();
41951cba0a8eSDuncan Sands }
41961cba0a8eSDuncan Sands 
LLVMStopMultithreaded()41971cba0a8eSDuncan Sands void LLVMStopMultithreaded() {
41981cba0a8eSDuncan Sands }
41991cba0a8eSDuncan Sands 
LLVMIsMultithreaded()42001cba0a8eSDuncan Sands LLVMBool LLVMIsMultithreaded() {
42011cba0a8eSDuncan Sands   return llvm_is_multithreaded();
42021cba0a8eSDuncan Sands }
4203