OSDN Git Service

bea4dee15c13c9c033d8fa21a56d535065733329
[android-x86/external-llvm.git] / lib / IR / Core.cpp
1 //===-- Core.cpp ----------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the common infrastructure (including the C bindings)
11 // for libLLVMCore.a, which implements the LLVM intermediate representation.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm-c/Core.h"
16 #include "llvm/ADT/StringSwitch.h"
17 #include "llvm/IR/Attributes.h"
18 #include "llvm/IR/CallSite.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/DiagnosticInfo.h"
22 #include "llvm/IR/DiagnosticPrinter.h"
23 #include "llvm/IR/GlobalAlias.h"
24 #include "llvm/IR/GlobalVariable.h"
25 #include "llvm/IR/IRBuilder.h"
26 #include "llvm/IR/InlineAsm.h"
27 #include "llvm/IR/IntrinsicInst.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/LegacyPassManager.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/FileSystem.h"
34 #include "llvm/Support/ManagedStatic.h"
35 #include "llvm/Support/MemoryBuffer.h"
36 #include "llvm/Support/Threading.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include <cassert>
39 #include <cstdlib>
40 #include <cstring>
41 #include <system_error>
42
43 using namespace llvm;
44
45 #define DEBUG_TYPE "ir"
46
47 void llvm::initializeCore(PassRegistry &Registry) {
48   initializeDominatorTreeWrapperPassPass(Registry);
49   initializePrintModulePassWrapperPass(Registry);
50   initializePrintFunctionPassWrapperPass(Registry);
51   initializePrintBasicBlockPassPass(Registry);
52   initializeSafepointIRVerifierPass(Registry);
53   initializeVerifierLegacyPassPass(Registry);
54 }
55
56 void LLVMInitializeCore(LLVMPassRegistryRef R) {
57   initializeCore(*unwrap(R));
58 }
59
60 void LLVMShutdown() {
61   llvm_shutdown();
62 }
63
64 /*===-- Error handling ----------------------------------------------------===*/
65
66 char *LLVMCreateMessage(const char *Message) {
67   return strdup(Message);
68 }
69
70 void LLVMDisposeMessage(char *Message) {
71   free(Message);
72 }
73
74
75 /*===-- Operations on contexts --------------------------------------------===*/
76
77 static ManagedStatic<LLVMContext> GlobalContext;
78
79 LLVMContextRef LLVMContextCreate() {
80   return wrap(new LLVMContext());
81 }
82
83 LLVMContextRef LLVMGetGlobalContext() { return wrap(&*GlobalContext); }
84
85 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
86                                      LLVMDiagnosticHandler Handler,
87                                      void *DiagnosticContext) {
88   unwrap(C)->setDiagnosticHandlerCallBack(
89       LLVM_EXTENSION reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy>(
90           Handler),
91       DiagnosticContext);
92 }
93
94 LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) {
95   return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
96       unwrap(C)->getDiagnosticHandlerCallBack());
97 }
98
99 void *LLVMContextGetDiagnosticContext(LLVMContextRef C) {
100   return unwrap(C)->getDiagnosticContext();
101 }
102
103 void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
104                                  void *OpaqueHandle) {
105   auto YieldCallback =
106     LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
107   unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
108 }
109
110 void LLVMContextDispose(LLVMContextRef C) {
111   delete unwrap(C);
112 }
113
114 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,
115                                   unsigned SLen) {
116   return unwrap(C)->getMDKindID(StringRef(Name, SLen));
117 }
118
119 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
120   return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
121 }
122
123 #define GET_ATTR_KIND_FROM_NAME
124 #include "AttributesCompatFunc.inc"
125
126 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
127   return getAttrKindFromName(StringRef(Name, SLen));
128 }
129
130 unsigned LLVMGetLastEnumAttributeKind(void) {
131   return Attribute::AttrKind::EndAttrKinds;
132 }
133
134 LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID,
135                                          uint64_t Val) {
136   return wrap(Attribute::get(*unwrap(C), (Attribute::AttrKind)KindID, Val));
137 }
138
139 unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) {
140   return unwrap(A).getKindAsEnum();
141 }
142
143 uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) {
144   auto Attr = unwrap(A);
145   if (Attr.isEnumAttribute())
146     return 0;
147   return Attr.getValueAsInt();
148 }
149
150 LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
151                                            const char *K, unsigned KLength,
152                                            const char *V, unsigned VLength) {
153   return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
154                              StringRef(V, VLength)));
155 }
156
157 const char *LLVMGetStringAttributeKind(LLVMAttributeRef A,
158                                        unsigned *Length) {
159   auto S = unwrap(A).getKindAsString();
160   *Length = S.size();
161   return S.data();
162 }
163
164 const char *LLVMGetStringAttributeValue(LLVMAttributeRef A,
165                                         unsigned *Length) {
166   auto S = unwrap(A).getValueAsString();
167   *Length = S.size();
168   return S.data();
169 }
170
171 LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) {
172   auto Attr = unwrap(A);
173   return Attr.isEnumAttribute() || Attr.isIntAttribute();
174 }
175
176 LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) {
177   return unwrap(A).isStringAttribute();
178 }
179
180 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
181   std::string MsgStorage;
182   raw_string_ostream Stream(MsgStorage);
183   DiagnosticPrinterRawOStream DP(Stream);
184
185   unwrap(DI)->print(DP);
186   Stream.flush();
187
188   return LLVMCreateMessage(MsgStorage.c_str());
189 }
190
191 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {
192     LLVMDiagnosticSeverity severity;
193
194     switch(unwrap(DI)->getSeverity()) {
195     default:
196       severity = LLVMDSError;
197       break;
198     case DS_Warning:
199       severity = LLVMDSWarning;
200       break;
201     case DS_Remark:
202       severity = LLVMDSRemark;
203       break;
204     case DS_Note:
205       severity = LLVMDSNote;
206       break;
207     }
208
209     return severity;
210 }
211
212 /*===-- Operations on modules ---------------------------------------------===*/
213
214 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
215   return wrap(new Module(ModuleID, *GlobalContext));
216 }
217
218 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
219                                                 LLVMContextRef C) {
220   return wrap(new Module(ModuleID, *unwrap(C)));
221 }
222
223 void LLVMDisposeModule(LLVMModuleRef M) {
224   delete unwrap(M);
225 }
226
227 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
228   auto &Str = unwrap(M)->getModuleIdentifier();
229   *Len = Str.length();
230   return Str.c_str();
231 }
232
233 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
234   unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
235 }
236
237 const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
238   auto &Str = unwrap(M)->getSourceFileName();
239   *Len = Str.length();
240   return Str.c_str();
241 }
242
243 void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
244   unwrap(M)->setSourceFileName(StringRef(Name, Len));
245 }
246
247 /*--.. Data layout .........................................................--*/
248 const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
249   return unwrap(M)->getDataLayoutStr().c_str();
250 }
251
252 const char *LLVMGetDataLayout(LLVMModuleRef M) {
253   return LLVMGetDataLayoutStr(M);
254 }
255
256 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
257   unwrap(M)->setDataLayout(DataLayoutStr);
258 }
259
260 /*--.. Target triple .......................................................--*/
261 const char * LLVMGetTarget(LLVMModuleRef M) {
262   return unwrap(M)->getTargetTriple().c_str();
263 }
264
265 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
266   unwrap(M)->setTargetTriple(Triple);
267 }
268
269 /*--.. Module flags ........................................................--*/
270 struct LLVMOpaqueModuleFlagEntry {
271   LLVMModuleFlagBehavior Behavior;
272   const char *Key;
273   size_t KeyLen;
274   LLVMMetadataRef Metadata;
275 };
276
277 static Module::ModFlagBehavior
278 map_to_llvmModFlagBehavior(LLVMModuleFlagBehavior Behavior) {
279   switch (Behavior) {
280   case LLVMModuleFlagBehaviorError:
281     return Module::ModFlagBehavior::Error;
282   case LLVMModuleFlagBehaviorWarning:
283     return Module::ModFlagBehavior::Warning;
284   case LLVMModuleFlagBehaviorRequire:
285     return Module::ModFlagBehavior::Require;
286   case LLVMModuleFlagBehaviorOverride:
287     return Module::ModFlagBehavior::Override;
288   case LLVMModuleFlagBehaviorAppend:
289     return Module::ModFlagBehavior::Append;
290   case LLVMModuleFlagBehaviorAppendUnique:
291     return Module::ModFlagBehavior::AppendUnique;
292   }
293   llvm_unreachable("Unknown LLVMModuleFlagBehavior");
294 }
295
296 static LLVMModuleFlagBehavior
297 map_from_llvmModFlagBehavior(Module::ModFlagBehavior Behavior) {
298   switch (Behavior) {
299   case Module::ModFlagBehavior::Error:
300     return LLVMModuleFlagBehaviorError;
301   case Module::ModFlagBehavior::Warning:
302     return LLVMModuleFlagBehaviorWarning;
303   case Module::ModFlagBehavior::Require:
304     return LLVMModuleFlagBehaviorRequire;
305   case Module::ModFlagBehavior::Override:
306     return LLVMModuleFlagBehaviorOverride;
307   case Module::ModFlagBehavior::Append:
308     return LLVMModuleFlagBehaviorAppend;
309   case Module::ModFlagBehavior::AppendUnique:
310     return LLVMModuleFlagBehaviorAppendUnique;
311   default:
312     llvm_unreachable("Unhandled Flag Behavior");
313   }
314 }
315
316 LLVMModuleFlagEntry *LLVMCopyModuleFlagsMetadata(LLVMModuleRef M, size_t *Len) {
317   SmallVector<Module::ModuleFlagEntry, 8> MFEs;
318   unwrap(M)->getModuleFlagsMetadata(MFEs);
319
320   LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>(
321       safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
322   for (unsigned i = 0; i < MFEs.size(); ++i) {
323     const auto &ModuleFlag = MFEs[i];
324     Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
325     Result[i].Key = ModuleFlag.Key->getString().data();
326     Result[i].KeyLen = ModuleFlag.Key->getString().size();
327     Result[i].Metadata = wrap(ModuleFlag.Val);
328   }
329   *Len = MFEs.size();
330   return Result;
331 }
332
333 void LLVMDisposeModuleFlagsMetadata(LLVMModuleFlagEntry *Entries) {
334   free(Entries);
335 }
336
337 LLVMModuleFlagBehavior
338 LLVMModuleFlagEntriesGetFlagBehavior(LLVMModuleFlagEntry *Entries,
339                                      unsigned Index) {
340   LLVMOpaqueModuleFlagEntry MFE =
341       static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
342   return MFE.Behavior;
343 }
344
345 const char *LLVMModuleFlagEntriesGetKey(LLVMModuleFlagEntry *Entries,
346                                         unsigned Index, size_t *Len) {
347   LLVMOpaqueModuleFlagEntry MFE =
348       static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
349   *Len = MFE.KeyLen;
350   return MFE.Key;
351 }
352
353 LLVMMetadataRef LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry *Entries,
354                                                  unsigned Index) {
355   LLVMOpaqueModuleFlagEntry MFE =
356       static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
357   return MFE.Metadata;
358 }
359
360 LLVMMetadataRef LLVMGetModuleFlag(LLVMModuleRef M,
361                                   const char *Key, size_t KeyLen) {
362   return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
363 }
364
365 void LLVMAddModuleFlag(LLVMModuleRef M, LLVMModuleFlagBehavior Behavior,
366                        const char *Key, size_t KeyLen,
367                        LLVMMetadataRef Val) {
368   unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
369                            {Key, KeyLen}, unwrap(Val));
370 }
371
372 /*--.. Printing modules ....................................................--*/
373
374 void LLVMDumpModule(LLVMModuleRef M) {
375   unwrap(M)->print(errs(), nullptr,
376                    /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
377 }
378
379 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
380                                char **ErrorMessage) {
381   std::error_code EC;
382   raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
383   if (EC) {
384     *ErrorMessage = strdup(EC.message().c_str());
385     return true;
386   }
387
388   unwrap(M)->print(dest, nullptr);
389
390   dest.close();
391
392   if (dest.has_error()) {
393     std::string E = "Error printing to file: " + dest.error().message();
394     *ErrorMessage = strdup(E.c_str());
395     return true;
396   }
397
398   return false;
399 }
400
401 char *LLVMPrintModuleToString(LLVMModuleRef M) {
402   std::string buf;
403   raw_string_ostream os(buf);
404
405   unwrap(M)->print(os, nullptr);
406   os.flush();
407
408   return strdup(buf.c_str());
409 }
410
411 /*--.. Operations on inline assembler ......................................--*/
412 void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
413   unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
414 }
415
416 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
417   unwrap(M)->setModuleInlineAsm(StringRef(Asm));
418 }
419
420 void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
421   unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
422 }
423
424 const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
425   auto &Str = unwrap(M)->getModuleInlineAsm();
426   *Len = Str.length();
427   return Str.c_str();
428 }
429
430 LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty,
431                               char *AsmString, size_t AsmStringSize,
432                               char *Constraints, size_t ConstraintsSize,
433                               LLVMBool HasSideEffects, LLVMBool IsAlignStack,
434                               LLVMInlineAsmDialect Dialect) {
435   InlineAsm::AsmDialect AD;
436   switch (Dialect) {
437   case LLVMInlineAsmDialectATT:
438     AD = InlineAsm::AD_ATT;
439     break;
440   case LLVMInlineAsmDialectIntel:
441     AD = InlineAsm::AD_Intel;
442     break;
443   }
444   return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
445                              StringRef(AsmString, AsmStringSize),
446                              StringRef(Constraints, ConstraintsSize),
447                              HasSideEffects, IsAlignStack, AD));
448 }
449
450
451 /*--.. Operations on module contexts ......................................--*/
452 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
453   return wrap(&unwrap(M)->getContext());
454 }
455
456
457 /*===-- Operations on types -----------------------------------------------===*/
458
459 /*--.. Operations on all types (mostly) ....................................--*/
460
461 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
462   switch (unwrap(Ty)->getTypeID()) {
463   case Type::VoidTyID:
464     return LLVMVoidTypeKind;
465   case Type::HalfTyID:
466     return LLVMHalfTypeKind;
467   case Type::FloatTyID:
468     return LLVMFloatTypeKind;
469   case Type::DoubleTyID:
470     return LLVMDoubleTypeKind;
471   case Type::X86_FP80TyID:
472     return LLVMX86_FP80TypeKind;
473   case Type::FP128TyID:
474     return LLVMFP128TypeKind;
475   case Type::PPC_FP128TyID:
476     return LLVMPPC_FP128TypeKind;
477   case Type::LabelTyID:
478     return LLVMLabelTypeKind;
479   case Type::MetadataTyID:
480     return LLVMMetadataTypeKind;
481   case Type::IntegerTyID:
482     return LLVMIntegerTypeKind;
483   case Type::FunctionTyID:
484     return LLVMFunctionTypeKind;
485   case Type::StructTyID:
486     return LLVMStructTypeKind;
487   case Type::ArrayTyID:
488     return LLVMArrayTypeKind;
489   case Type::PointerTyID:
490     return LLVMPointerTypeKind;
491   case Type::VectorTyID:
492     return LLVMVectorTypeKind;
493   case Type::X86_MMXTyID:
494     return LLVMX86_MMXTypeKind;
495   case Type::TokenTyID:
496     return LLVMTokenTypeKind;
497   }
498   llvm_unreachable("Unhandled TypeID.");
499 }
500
501 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
502 {
503     return unwrap(Ty)->isSized();
504 }
505
506 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
507   return wrap(&unwrap(Ty)->getContext());
508 }
509
510 void LLVMDumpType(LLVMTypeRef Ty) {
511   return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
512 }
513
514 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
515   std::string buf;
516   raw_string_ostream os(buf);
517
518   if (unwrap(Ty))
519     unwrap(Ty)->print(os);
520   else
521     os << "Printing <null> Type";
522
523   os.flush();
524
525   return strdup(buf.c_str());
526 }
527
528 /*--.. Operations on integer types .........................................--*/
529
530 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C)  {
531   return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
532 }
533 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C)  {
534   return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
535 }
536 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
537   return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
538 }
539 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
540   return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
541 }
542 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
543   return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
544 }
545 LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
546   return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
547 }
548 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
549   return wrap(IntegerType::get(*unwrap(C), NumBits));
550 }
551
552 LLVMTypeRef LLVMInt1Type(void)  {
553   return LLVMInt1TypeInContext(LLVMGetGlobalContext());
554 }
555 LLVMTypeRef LLVMInt8Type(void)  {
556   return LLVMInt8TypeInContext(LLVMGetGlobalContext());
557 }
558 LLVMTypeRef LLVMInt16Type(void) {
559   return LLVMInt16TypeInContext(LLVMGetGlobalContext());
560 }
561 LLVMTypeRef LLVMInt32Type(void) {
562   return LLVMInt32TypeInContext(LLVMGetGlobalContext());
563 }
564 LLVMTypeRef LLVMInt64Type(void) {
565   return LLVMInt64TypeInContext(LLVMGetGlobalContext());
566 }
567 LLVMTypeRef LLVMInt128Type(void) {
568   return LLVMInt128TypeInContext(LLVMGetGlobalContext());
569 }
570 LLVMTypeRef LLVMIntType(unsigned NumBits) {
571   return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
572 }
573
574 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
575   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
576 }
577
578 /*--.. Operations on real types ............................................--*/
579
580 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
581   return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
582 }
583 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
584   return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
585 }
586 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
587   return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
588 }
589 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
590   return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
591 }
592 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
593   return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
594 }
595 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
596   return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
597 }
598 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
599   return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
600 }
601
602 LLVMTypeRef LLVMHalfType(void) {
603   return LLVMHalfTypeInContext(LLVMGetGlobalContext());
604 }
605 LLVMTypeRef LLVMFloatType(void) {
606   return LLVMFloatTypeInContext(LLVMGetGlobalContext());
607 }
608 LLVMTypeRef LLVMDoubleType(void) {
609   return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
610 }
611 LLVMTypeRef LLVMX86FP80Type(void) {
612   return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
613 }
614 LLVMTypeRef LLVMFP128Type(void) {
615   return LLVMFP128TypeInContext(LLVMGetGlobalContext());
616 }
617 LLVMTypeRef LLVMPPCFP128Type(void) {
618   return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
619 }
620 LLVMTypeRef LLVMX86MMXType(void) {
621   return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
622 }
623
624 /*--.. Operations on function types ........................................--*/
625
626 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
627                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
628                              LLVMBool IsVarArg) {
629   ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
630   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
631 }
632
633 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
634   return unwrap<FunctionType>(FunctionTy)->isVarArg();
635 }
636
637 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
638   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
639 }
640
641 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
642   return unwrap<FunctionType>(FunctionTy)->getNumParams();
643 }
644
645 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
646   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
647   for (FunctionType::param_iterator I = Ty->param_begin(),
648                                     E = Ty->param_end(); I != E; ++I)
649     *Dest++ = wrap(*I);
650 }
651
652 /*--.. Operations on struct types ..........................................--*/
653
654 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
655                            unsigned ElementCount, LLVMBool Packed) {
656   ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
657   return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
658 }
659
660 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
661                            unsigned ElementCount, LLVMBool Packed) {
662   return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
663                                  ElementCount, Packed);
664 }
665
666 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
667 {
668   return wrap(StructType::create(*unwrap(C), Name));
669 }
670
671 const char *LLVMGetStructName(LLVMTypeRef Ty)
672 {
673   StructType *Type = unwrap<StructType>(Ty);
674   if (!Type->hasName())
675     return nullptr;
676   return Type->getName().data();
677 }
678
679 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
680                        unsigned ElementCount, LLVMBool Packed) {
681   ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
682   unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
683 }
684
685 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
686   return unwrap<StructType>(StructTy)->getNumElements();
687 }
688
689 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
690   StructType *Ty = unwrap<StructType>(StructTy);
691   for (StructType::element_iterator I = Ty->element_begin(),
692                                     E = Ty->element_end(); I != E; ++I)
693     *Dest++ = wrap(*I);
694 }
695
696 LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
697   StructType *Ty = unwrap<StructType>(StructTy);
698   return wrap(Ty->getTypeAtIndex(i));
699 }
700
701 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
702   return unwrap<StructType>(StructTy)->isPacked();
703 }
704
705 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
706   return unwrap<StructType>(StructTy)->isOpaque();
707 }
708
709 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
710   return wrap(unwrap(M)->getTypeByName(Name));
711 }
712
713 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
714
715 void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
716     int i = 0;
717     for (auto *T : unwrap(Tp)->subtypes()) {
718         Arr[i] = wrap(T);
719         i++;
720     }
721 }
722
723 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
724   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
725 }
726
727 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
728   return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
729 }
730
731 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
732   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
733 }
734
735 LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
736   auto *Ty = unwrap<Type>(WrappedTy);
737   if (auto *PTy = dyn_cast<PointerType>(Ty))
738     return wrap(PTy->getElementType());
739   return wrap(cast<SequentialType>(Ty)->getElementType());
740 }
741
742 unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
743     return unwrap(Tp)->getNumContainedTypes();
744 }
745
746 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
747   return unwrap<ArrayType>(ArrayTy)->getNumElements();
748 }
749
750 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
751   return unwrap<PointerType>(PointerTy)->getAddressSpace();
752 }
753
754 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
755   return unwrap<VectorType>(VectorTy)->getNumElements();
756 }
757
758 /*--.. Operations on other types ...........................................--*/
759
760 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C)  {
761   return wrap(Type::getVoidTy(*unwrap(C)));
762 }
763 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
764   return wrap(Type::getLabelTy(*unwrap(C)));
765 }
766 LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
767   return wrap(Type::getTokenTy(*unwrap(C)));
768 }
769 LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) {
770   return wrap(Type::getMetadataTy(*unwrap(C)));
771 }
772
773 LLVMTypeRef LLVMVoidType(void)  {
774   return LLVMVoidTypeInContext(LLVMGetGlobalContext());
775 }
776 LLVMTypeRef LLVMLabelType(void) {
777   return LLVMLabelTypeInContext(LLVMGetGlobalContext());
778 }
779
780 /*===-- Operations on values ----------------------------------------------===*/
781
782 /*--.. Operations on all values ............................................--*/
783
784 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
785   return wrap(unwrap(Val)->getType());
786 }
787
788 LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
789     switch(unwrap(Val)->getValueID()) {
790 #define HANDLE_VALUE(Name) \
791   case Value::Name##Val: \
792     return LLVM##Name##ValueKind;
793 #include "llvm/IR/Value.def"
794   default:
795     return LLVMInstructionValueKind;
796   }
797 }
798
799 const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
800   auto *V = unwrap(Val);
801   *Length = V->getName().size();
802   return V->getName().data();
803 }
804
805 void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
806   unwrap(Val)->setName(StringRef(Name, NameLen));
807 }
808
809 const char *LLVMGetValueName(LLVMValueRef Val) {
810   return unwrap(Val)->getName().data();
811 }
812
813 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
814   unwrap(Val)->setName(Name);
815 }
816
817 void LLVMDumpValue(LLVMValueRef Val) {
818   unwrap(Val)->print(errs(), /*IsForDebug=*/true);
819 }
820
821 char* LLVMPrintValueToString(LLVMValueRef Val) {
822   std::string buf;
823   raw_string_ostream os(buf);
824
825   if (unwrap(Val))
826     unwrap(Val)->print(os);
827   else
828     os << "Printing <null> Value";
829
830   os.flush();
831
832   return strdup(buf.c_str());
833 }
834
835 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
836   unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
837 }
838
839 int LLVMHasMetadata(LLVMValueRef Inst) {
840   return unwrap<Instruction>(Inst)->hasMetadata();
841 }
842
843 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
844   auto *I = unwrap<Instruction>(Inst);
845   assert(I && "Expected instruction");
846   if (auto *MD = I->getMetadata(KindID))
847     return wrap(MetadataAsValue::get(I->getContext(), MD));
848   return nullptr;
849 }
850
851 // MetadataAsValue uses a canonical format which strips the actual MDNode for
852 // MDNode with just a single constant value, storing just a ConstantAsMetadata
853 // This undoes this canonicalization, reconstructing the MDNode.
854 static MDNode *extractMDNode(MetadataAsValue *MAV) {
855   Metadata *MD = MAV->getMetadata();
856   assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
857       "Expected a metadata node or a canonicalized constant");
858
859   if (MDNode *N = dyn_cast<MDNode>(MD))
860     return N;
861
862   return MDNode::get(MAV->getContext(), MD);
863 }
864
865 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
866   MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
867
868   unwrap<Instruction>(Inst)->setMetadata(KindID, N);
869 }
870
871 /*--.. Conversion functions ................................................--*/
872
873 #define LLVM_DEFINE_VALUE_CAST(name)                                       \
874   LLVMValueRef LLVMIsA##name(LLVMValueRef Val) {                           \
875     return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
876   }
877
878 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
879
880 LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
881   if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
882     if (isa<MDNode>(MD->getMetadata()) ||
883         isa<ValueAsMetadata>(MD->getMetadata()))
884       return Val;
885   return nullptr;
886 }
887
888 LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
889   if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
890     if (isa<MDString>(MD->getMetadata()))
891       return Val;
892   return nullptr;
893 }
894
895 /*--.. Operations on Uses ..................................................--*/
896 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
897   Value *V = unwrap(Val);
898   Value::use_iterator I = V->use_begin();
899   if (I == V->use_end())
900     return nullptr;
901   return wrap(&*I);
902 }
903
904 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
905   Use *Next = unwrap(U)->getNext();
906   if (Next)
907     return wrap(Next);
908   return nullptr;
909 }
910
911 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
912   return wrap(unwrap(U)->getUser());
913 }
914
915 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
916   return wrap(unwrap(U)->get());
917 }
918
919 /*--.. Operations on Users .................................................--*/
920
921 static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
922                                          unsigned Index) {
923   Metadata *Op = N->getOperand(Index);
924   if (!Op)
925     return nullptr;
926   if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
927     return wrap(C->getValue());
928   return wrap(MetadataAsValue::get(Context, Op));
929 }
930
931 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
932   Value *V = unwrap(Val);
933   if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
934     if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
935       assert(Index == 0 && "Function-local metadata can only have one operand");
936       return wrap(L->getValue());
937     }
938     return getMDNodeOperandImpl(V->getContext(),
939                                 cast<MDNode>(MD->getMetadata()), Index);
940   }
941
942   return wrap(cast<User>(V)->getOperand(Index));
943 }
944
945 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
946   Value *V = unwrap(Val);
947   return wrap(&cast<User>(V)->getOperandUse(Index));
948 }
949
950 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
951   unwrap<User>(Val)->setOperand(Index, unwrap(Op));
952 }
953
954 int LLVMGetNumOperands(LLVMValueRef Val) {
955   Value *V = unwrap(Val);
956   if (isa<MetadataAsValue>(V))
957     return LLVMGetMDNodeNumOperands(Val);
958
959   return cast<User>(V)->getNumOperands();
960 }
961
962 /*--.. Operations on constants of any type .................................--*/
963
964 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
965   return wrap(Constant::getNullValue(unwrap(Ty)));
966 }
967
968 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
969   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
970 }
971
972 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
973   return wrap(UndefValue::get(unwrap(Ty)));
974 }
975
976 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
977   return isa<Constant>(unwrap(Ty));
978 }
979
980 LLVMBool LLVMIsNull(LLVMValueRef Val) {
981   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
982     return C->isNullValue();
983   return false;
984 }
985
986 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
987   return isa<UndefValue>(unwrap(Val));
988 }
989
990 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
991   return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
992 }
993
994 /*--.. Operations on metadata nodes ........................................--*/
995
996 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
997                                    unsigned SLen) {
998   LLVMContext &Context = *unwrap(C);
999   return wrap(MetadataAsValue::get(
1000       Context, MDString::get(Context, StringRef(Str, SLen))));
1001 }
1002
1003 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1004   return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1005 }
1006
1007 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
1008                                  unsigned Count) {
1009   LLVMContext &Context = *unwrap(C);
1010   SmallVector<Metadata *, 8> MDs;
1011   for (auto *OV : makeArrayRef(Vals, Count)) {
1012     Value *V = unwrap(OV);
1013     Metadata *MD;
1014     if (!V)
1015       MD = nullptr;
1016     else if (auto *C = dyn_cast<Constant>(V))
1017       MD = ConstantAsMetadata::get(C);
1018     else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1019       MD = MDV->getMetadata();
1020       assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1021                                           "outside of direct argument to call");
1022     } else {
1023       // This is function-local metadata.  Pretend to make an MDNode.
1024       assert(Count == 1 &&
1025              "Expected only one operand to function-local metadata");
1026       return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
1027     }
1028
1029     MDs.push_back(MD);
1030   }
1031   return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
1032 }
1033
1034 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1035   return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1036 }
1037
1038 LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
1039   return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1040 }
1041
1042 LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {
1043   auto *V = unwrap(Val);
1044   if (auto *C = dyn_cast<Constant>(V))
1045     return wrap(ConstantAsMetadata::get(C));
1046   if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1047     return wrap(MAV->getMetadata());
1048   return wrap(ValueAsMetadata::get(V));
1049 }
1050
1051 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1052   if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1053     if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1054       *Length = S->getString().size();
1055       return S->getString().data();
1056     }
1057   *Length = 0;
1058   return nullptr;
1059 }
1060
1061 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
1062   auto *MD = cast<MetadataAsValue>(unwrap(V));
1063   if (isa<ValueAsMetadata>(MD->getMetadata()))
1064     return 1;
1065   return cast<MDNode>(MD->getMetadata())->getNumOperands();
1066 }
1067
1068 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
1069   auto *MD = cast<MetadataAsValue>(unwrap(V));
1070   if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1071     *Dest = wrap(MDV->getValue());
1072     return;
1073   }
1074   const auto *N = cast<MDNode>(MD->getMetadata());
1075   const unsigned numOperands = N->getNumOperands();
1076   LLVMContext &Context = unwrap(V)->getContext();
1077   for (unsigned i = 0; i < numOperands; i++)
1078     Dest[i] = getMDNodeOperandImpl(Context, N, i);
1079 }
1080
1081 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
1082   if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1083     return N->getNumOperands();
1084   }
1085   return 0;
1086 }
1087
1088 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
1089                                   LLVMValueRef *Dest) {
1090   NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1091   if (!N)
1092     return;
1093   LLVMContext &Context = unwrap(M)->getContext();
1094   for (unsigned i=0;i<N->getNumOperands();i++)
1095     Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1096 }
1097
1098 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
1099                                  LLVMValueRef Val) {
1100   NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1101   if (!N)
1102     return;
1103   if (!Val)
1104     return;
1105   N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1106 }
1107
1108 /*--.. Operations on scalar constants ......................................--*/
1109
1110 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1111                           LLVMBool SignExtend) {
1112   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1113 }
1114
1115 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
1116                                               unsigned NumWords,
1117                                               const uint64_t Words[]) {
1118     IntegerType *Ty = unwrap<IntegerType>(IntTy);
1119     return wrap(ConstantInt::get(Ty->getContext(),
1120                                  APInt(Ty->getBitWidth(),
1121                                        makeArrayRef(Words, NumWords))));
1122 }
1123
1124 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
1125                                   uint8_t Radix) {
1126   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1127                                Radix));
1128 }
1129
1130 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
1131                                          unsigned SLen, uint8_t Radix) {
1132   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1133                                Radix));
1134 }
1135
1136 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
1137   return wrap(ConstantFP::get(unwrap(RealTy), N));
1138 }
1139
1140 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
1141   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1142 }
1143
1144 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
1145                                           unsigned SLen) {
1146   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1147 }
1148
1149 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1150   return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1151 }
1152
1153 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1154   return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1155 }
1156
1157 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1158   ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1159   Type *Ty = cFP->getType();
1160
1161   if (Ty->isFloatTy()) {
1162     *LosesInfo = false;
1163     return cFP->getValueAPF().convertToFloat();
1164   }
1165
1166   if (Ty->isDoubleTy()) {
1167     *LosesInfo = false;
1168     return cFP->getValueAPF().convertToDouble();
1169   }
1170
1171   bool APFLosesInfo;
1172   APFloat APF = cFP->getValueAPF();
1173   APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
1174   *LosesInfo = APFLosesInfo;
1175   return APF.convertToDouble();
1176 }
1177
1178 /*--.. Operations on composite constants ...................................--*/
1179
1180 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
1181                                       unsigned Length,
1182                                       LLVMBool DontNullTerminate) {
1183   /* Inverted the sense of AddNull because ', 0)' is a
1184      better mnemonic for null termination than ', 1)'. */
1185   return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1186                                            DontNullTerminate == 0));
1187 }
1188
1189 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1190                              LLVMBool DontNullTerminate) {
1191   return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1192                                   DontNullTerminate);
1193 }
1194
1195 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1196   return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1197 }
1198
1199 LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1200   return unwrap<ConstantDataSequential>(C)->isString();
1201 }
1202
1203 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1204   StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1205   *Length = Str.size();
1206   return Str.data();
1207 }
1208
1209 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1210                             LLVMValueRef *ConstantVals, unsigned Length) {
1211   ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1212   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1213 }
1214
1215 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1216                                       LLVMValueRef *ConstantVals,
1217                                       unsigned Count, LLVMBool Packed) {
1218   Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1219   return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1220                                       Packed != 0));
1221 }
1222
1223 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1224                              LLVMBool Packed) {
1225   return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1226                                   Packed);
1227 }
1228
1229 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1230                                   LLVMValueRef *ConstantVals,
1231                                   unsigned Count) {
1232   Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1233   StructType *Ty = cast<StructType>(unwrap(StructTy));
1234
1235   return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
1236 }
1237
1238 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1239   return wrap(ConstantVector::get(makeArrayRef(
1240                             unwrap<Constant>(ScalarConstantVals, Size), Size)));
1241 }
1242
1243 /*-- Opcode mapping */
1244
1245 static LLVMOpcode map_to_llvmopcode(int opcode)
1246 {
1247     switch (opcode) {
1248       default: llvm_unreachable("Unhandled Opcode.");
1249 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1250 #include "llvm/IR/Instruction.def"
1251 #undef HANDLE_INST
1252     }
1253 }
1254
1255 static int map_from_llvmopcode(LLVMOpcode code)
1256 {
1257     switch (code) {
1258 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1259 #include "llvm/IR/Instruction.def"
1260 #undef HANDLE_INST
1261     }
1262     llvm_unreachable("Unhandled Opcode.");
1263 }
1264
1265 /*--.. Constant expressions ................................................--*/
1266
1267 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
1268   return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1269 }
1270
1271 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
1272   return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1273 }
1274
1275 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
1276   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1277 }
1278
1279 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
1280   return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1281 }
1282
1283 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
1284   return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1285 }
1286
1287 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
1288   return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1289 }
1290
1291
1292 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
1293   return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
1294 }
1295
1296 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
1297   return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1298 }
1299
1300 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1301   return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1302                                    unwrap<Constant>(RHSConstant)));
1303 }
1304
1305 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1306                              LLVMValueRef RHSConstant) {
1307   return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1308                                       unwrap<Constant>(RHSConstant)));
1309 }
1310
1311 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1312                              LLVMValueRef RHSConstant) {
1313   return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1314                                       unwrap<Constant>(RHSConstant)));
1315 }
1316
1317 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1318   return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
1319                                     unwrap<Constant>(RHSConstant)));
1320 }
1321
1322 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1323   return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1324                                    unwrap<Constant>(RHSConstant)));
1325 }
1326
1327 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1328                              LLVMValueRef RHSConstant) {
1329   return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1330                                       unwrap<Constant>(RHSConstant)));
1331 }
1332
1333 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1334                              LLVMValueRef RHSConstant) {
1335   return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1336                                       unwrap<Constant>(RHSConstant)));
1337 }
1338
1339 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1340   return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1341                                     unwrap<Constant>(RHSConstant)));
1342 }
1343
1344 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1345   return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1346                                    unwrap<Constant>(RHSConstant)));
1347 }
1348
1349 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1350                              LLVMValueRef RHSConstant) {
1351   return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1352                                       unwrap<Constant>(RHSConstant)));
1353 }
1354
1355 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1356                              LLVMValueRef RHSConstant) {
1357   return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1358                                       unwrap<Constant>(RHSConstant)));
1359 }
1360
1361 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1362   return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
1363                                     unwrap<Constant>(RHSConstant)));
1364 }
1365
1366 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1367   return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
1368                                     unwrap<Constant>(RHSConstant)));
1369 }
1370
1371 LLVMValueRef LLVMConstExactUDiv(LLVMValueRef LHSConstant,
1372                                 LLVMValueRef RHSConstant) {
1373   return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1374                                          unwrap<Constant>(RHSConstant)));
1375 }
1376
1377 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1378   return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
1379                                     unwrap<Constant>(RHSConstant)));
1380 }
1381
1382 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
1383                                 LLVMValueRef RHSConstant) {
1384   return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
1385                                          unwrap<Constant>(RHSConstant)));
1386 }
1387
1388 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1389   return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
1390                                     unwrap<Constant>(RHSConstant)));
1391 }
1392
1393 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1394   return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
1395                                     unwrap<Constant>(RHSConstant)));
1396 }
1397
1398 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1399   return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
1400                                     unwrap<Constant>(RHSConstant)));
1401 }
1402
1403 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1404   return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
1405                                     unwrap<Constant>(RHSConstant)));
1406 }
1407
1408 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1409   return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1410                                    unwrap<Constant>(RHSConstant)));
1411 }
1412
1413 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1414   return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1415                                   unwrap<Constant>(RHSConstant)));
1416 }
1417
1418 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1419   return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1420                                    unwrap<Constant>(RHSConstant)));
1421 }
1422
1423 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1424                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1425   return wrap(ConstantExpr::getICmp(Predicate,
1426                                     unwrap<Constant>(LHSConstant),
1427                                     unwrap<Constant>(RHSConstant)));
1428 }
1429
1430 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1431                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1432   return wrap(ConstantExpr::getFCmp(Predicate,
1433                                     unwrap<Constant>(LHSConstant),
1434                                     unwrap<Constant>(RHSConstant)));
1435 }
1436
1437 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1438   return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1439                                    unwrap<Constant>(RHSConstant)));
1440 }
1441
1442 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1443   return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1444                                     unwrap<Constant>(RHSConstant)));
1445 }
1446
1447 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1448   return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1449                                     unwrap<Constant>(RHSConstant)));
1450 }
1451
1452 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1453                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1454   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1455                                NumIndices);
1456   return wrap(ConstantExpr::getGetElementPtr(
1457       nullptr, unwrap<Constant>(ConstantVal), IdxList));
1458 }
1459
1460 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1461                                   LLVMValueRef *ConstantIndices,
1462                                   unsigned NumIndices) {
1463   Constant* Val = unwrap<Constant>(ConstantVal);
1464   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1465                                NumIndices);
1466   return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList));
1467 }
1468
1469 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1470   return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1471                                      unwrap(ToType)));
1472 }
1473
1474 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1475   return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1476                                     unwrap(ToType)));
1477 }
1478
1479 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1480   return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1481                                     unwrap(ToType)));
1482 }
1483
1484 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1485   return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1486                                        unwrap(ToType)));
1487 }
1488
1489 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1490   return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1491                                         unwrap(ToType)));
1492 }
1493
1494 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1495   return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1496                                       unwrap(ToType)));
1497 }
1498
1499 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1500   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1501                                       unwrap(ToType)));
1502 }
1503
1504 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1505   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1506                                       unwrap(ToType)));
1507 }
1508
1509 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1510   return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1511                                       unwrap(ToType)));
1512 }
1513
1514 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1515   return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1516                                         unwrap(ToType)));
1517 }
1518
1519 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1520   return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1521                                         unwrap(ToType)));
1522 }
1523
1524 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1525   return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1526                                        unwrap(ToType)));
1527 }
1528
1529 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1530                                     LLVMTypeRef ToType) {
1531   return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1532                                              unwrap(ToType)));
1533 }
1534
1535 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1536                                     LLVMTypeRef ToType) {
1537   return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1538                                              unwrap(ToType)));
1539 }
1540
1541 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1542                                     LLVMTypeRef ToType) {
1543   return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1544                                              unwrap(ToType)));
1545 }
1546
1547 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1548                                      LLVMTypeRef ToType) {
1549   return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1550                                               unwrap(ToType)));
1551 }
1552
1553 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1554                                   LLVMTypeRef ToType) {
1555   return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1556                                            unwrap(ToType)));
1557 }
1558
1559 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1560                               LLVMBool isSigned) {
1561   return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1562                                            unwrap(ToType), isSigned));
1563 }
1564
1565 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1566   return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1567                                       unwrap(ToType)));
1568 }
1569
1570 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1571                              LLVMValueRef ConstantIfTrue,
1572                              LLVMValueRef ConstantIfFalse) {
1573   return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1574                                       unwrap<Constant>(ConstantIfTrue),
1575                                       unwrap<Constant>(ConstantIfFalse)));
1576 }
1577
1578 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1579                                      LLVMValueRef IndexConstant) {
1580   return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1581                                               unwrap<Constant>(IndexConstant)));
1582 }
1583
1584 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1585                                     LLVMValueRef ElementValueConstant,
1586                                     LLVMValueRef IndexConstant) {
1587   return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1588                                          unwrap<Constant>(ElementValueConstant),
1589                                              unwrap<Constant>(IndexConstant)));
1590 }
1591
1592 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1593                                     LLVMValueRef VectorBConstant,
1594                                     LLVMValueRef MaskConstant) {
1595   return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1596                                              unwrap<Constant>(VectorBConstant),
1597                                              unwrap<Constant>(MaskConstant)));
1598 }
1599
1600 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1601                                    unsigned NumIdx) {
1602   return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1603                                             makeArrayRef(IdxList, NumIdx)));
1604 }
1605
1606 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1607                                   LLVMValueRef ElementValueConstant,
1608                                   unsigned *IdxList, unsigned NumIdx) {
1609   return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1610                                          unwrap<Constant>(ElementValueConstant),
1611                                            makeArrayRef(IdxList, NumIdx)));
1612 }
1613
1614 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1615                                 const char *Constraints,
1616                                 LLVMBool HasSideEffects,
1617                                 LLVMBool IsAlignStack) {
1618   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1619                              Constraints, HasSideEffects, IsAlignStack));
1620 }
1621
1622 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1623   return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1624 }
1625
1626 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1627
1628 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1629   return wrap(unwrap<GlobalValue>(Global)->getParent());
1630 }
1631
1632 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1633   return unwrap<GlobalValue>(Global)->isDeclaration();
1634 }
1635
1636 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1637   switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1638   case GlobalValue::ExternalLinkage:
1639     return LLVMExternalLinkage;
1640   case GlobalValue::AvailableExternallyLinkage:
1641     return LLVMAvailableExternallyLinkage;
1642   case GlobalValue::LinkOnceAnyLinkage:
1643     return LLVMLinkOnceAnyLinkage;
1644   case GlobalValue::LinkOnceODRLinkage:
1645     return LLVMLinkOnceODRLinkage;
1646   case GlobalValue::WeakAnyLinkage:
1647     return LLVMWeakAnyLinkage;
1648   case GlobalValue::WeakODRLinkage:
1649     return LLVMWeakODRLinkage;
1650   case GlobalValue::AppendingLinkage:
1651     return LLVMAppendingLinkage;
1652   case GlobalValue::InternalLinkage:
1653     return LLVMInternalLinkage;
1654   case GlobalValue::PrivateLinkage:
1655     return LLVMPrivateLinkage;
1656   case GlobalValue::ExternalWeakLinkage:
1657     return LLVMExternalWeakLinkage;
1658   case GlobalValue::CommonLinkage:
1659     return LLVMCommonLinkage;
1660   }
1661
1662   llvm_unreachable("Invalid GlobalValue linkage!");
1663 }
1664
1665 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1666   GlobalValue *GV = unwrap<GlobalValue>(Global);
1667
1668   switch (Linkage) {
1669   case LLVMExternalLinkage:
1670     GV->setLinkage(GlobalValue::ExternalLinkage);
1671     break;
1672   case LLVMAvailableExternallyLinkage:
1673     GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1674     break;
1675   case LLVMLinkOnceAnyLinkage:
1676     GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1677     break;
1678   case LLVMLinkOnceODRLinkage:
1679     GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1680     break;
1681   case LLVMLinkOnceODRAutoHideLinkage:
1682     LLVM_DEBUG(
1683         errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1684                   "longer supported.");
1685     break;
1686   case LLVMWeakAnyLinkage:
1687     GV->setLinkage(GlobalValue::WeakAnyLinkage);
1688     break;
1689   case LLVMWeakODRLinkage:
1690     GV->setLinkage(GlobalValue::WeakODRLinkage);
1691     break;
1692   case LLVMAppendingLinkage:
1693     GV->setLinkage(GlobalValue::AppendingLinkage);
1694     break;
1695   case LLVMInternalLinkage:
1696     GV->setLinkage(GlobalValue::InternalLinkage);
1697     break;
1698   case LLVMPrivateLinkage:
1699     GV->setLinkage(GlobalValue::PrivateLinkage);
1700     break;
1701   case LLVMLinkerPrivateLinkage:
1702     GV->setLinkage(GlobalValue::PrivateLinkage);
1703     break;
1704   case LLVMLinkerPrivateWeakLinkage:
1705     GV->setLinkage(GlobalValue::PrivateLinkage);
1706     break;
1707   case LLVMDLLImportLinkage:
1708     LLVM_DEBUG(
1709         errs()
1710         << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1711     break;
1712   case LLVMDLLExportLinkage:
1713     LLVM_DEBUG(
1714         errs()
1715         << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1716     break;
1717   case LLVMExternalWeakLinkage:
1718     GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1719     break;
1720   case LLVMGhostLinkage:
1721     LLVM_DEBUG(
1722         errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1723     break;
1724   case LLVMCommonLinkage:
1725     GV->setLinkage(GlobalValue::CommonLinkage);
1726     break;
1727   }
1728 }
1729
1730 const char *LLVMGetSection(LLVMValueRef Global) {
1731   // Using .data() is safe because of how GlobalObject::setSection is
1732   // implemented.
1733   return unwrap<GlobalValue>(Global)->getSection().data();
1734 }
1735
1736 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1737   unwrap<GlobalObject>(Global)->setSection(Section);
1738 }
1739
1740 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1741   return static_cast<LLVMVisibility>(
1742     unwrap<GlobalValue>(Global)->getVisibility());
1743 }
1744
1745 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1746   unwrap<GlobalValue>(Global)
1747     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1748 }
1749
1750 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1751   return static_cast<LLVMDLLStorageClass>(
1752       unwrap<GlobalValue>(Global)->getDLLStorageClass());
1753 }
1754
1755 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1756   unwrap<GlobalValue>(Global)->setDLLStorageClass(
1757       static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1758 }
1759
1760 LLVMUnnamedAddr LLVMGetUnnamedAddress(LLVMValueRef Global) {
1761   switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
1762   case GlobalVariable::UnnamedAddr::None:
1763     return LLVMNoUnnamedAddr;
1764   case GlobalVariable::UnnamedAddr::Local:
1765     return LLVMLocalUnnamedAddr;
1766   case GlobalVariable::UnnamedAddr::Global:
1767     return LLVMGlobalUnnamedAddr;
1768   }
1769   llvm_unreachable("Unknown UnnamedAddr kind!");
1770 }
1771
1772 void LLVMSetUnnamedAddress(LLVMValueRef Global, LLVMUnnamedAddr UnnamedAddr) {
1773   GlobalValue *GV = unwrap<GlobalValue>(Global);
1774
1775   switch (UnnamedAddr) {
1776   case LLVMNoUnnamedAddr:
1777     return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);
1778   case LLVMLocalUnnamedAddr:
1779     return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);
1780   case LLVMGlobalUnnamedAddr:
1781     return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);
1782   }
1783 }
1784
1785 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1786   return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
1787 }
1788
1789 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1790   unwrap<GlobalValue>(Global)->setUnnamedAddr(
1791       HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1792                      : GlobalValue::UnnamedAddr::None);
1793 }
1794
1795 /*--.. Operations on global variables, load and store instructions .........--*/
1796
1797 unsigned LLVMGetAlignment(LLVMValueRef V) {
1798   Value *P = unwrap<Value>(V);
1799   if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1800     return GV->getAlignment();
1801   if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1802     return AI->getAlignment();
1803   if (LoadInst *LI = dyn_cast<LoadInst>(P))
1804     return LI->getAlignment();
1805   if (StoreInst *SI = dyn_cast<StoreInst>(P))
1806     return SI->getAlignment();
1807
1808   llvm_unreachable(
1809       "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1810 }
1811
1812 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1813   Value *P = unwrap<Value>(V);
1814   if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1815     GV->setAlignment(Bytes);
1816   else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1817     AI->setAlignment(Bytes);
1818   else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1819     LI->setAlignment(Bytes);
1820   else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1821     SI->setAlignment(Bytes);
1822   else
1823     llvm_unreachable(
1824         "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1825 }
1826
1827 /*--.. Operations on global variables ......................................--*/
1828
1829 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1830   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1831                                  GlobalValue::ExternalLinkage, nullptr, Name));
1832 }
1833
1834 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1835                                          const char *Name,
1836                                          unsigned AddressSpace) {
1837   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1838                                  GlobalValue::ExternalLinkage, nullptr, Name,
1839                                  nullptr, GlobalVariable::NotThreadLocal,
1840                                  AddressSpace));
1841 }
1842
1843 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1844   return wrap(unwrap(M)->getNamedGlobal(Name));
1845 }
1846
1847 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1848   Module *Mod = unwrap(M);
1849   Module::global_iterator I = Mod->global_begin();
1850   if (I == Mod->global_end())
1851     return nullptr;
1852   return wrap(&*I);
1853 }
1854
1855 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1856   Module *Mod = unwrap(M);
1857   Module::global_iterator I = Mod->global_end();
1858   if (I == Mod->global_begin())
1859     return nullptr;
1860   return wrap(&*--I);
1861 }
1862
1863 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1864   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1865   Module::global_iterator I(GV);
1866   if (++I == GV->getParent()->global_end())
1867     return nullptr;
1868   return wrap(&*I);
1869 }
1870
1871 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1872   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1873   Module::global_iterator I(GV);
1874   if (I == GV->getParent()->global_begin())
1875     return nullptr;
1876   return wrap(&*--I);
1877 }
1878
1879 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1880   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1881 }
1882
1883 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1884   GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1885   if ( !GV->hasInitializer() )
1886     return nullptr;
1887   return wrap(GV->getInitializer());
1888 }
1889
1890 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1891   unwrap<GlobalVariable>(GlobalVar)
1892     ->setInitializer(unwrap<Constant>(ConstantVal));
1893 }
1894
1895 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1896   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1897 }
1898
1899 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1900   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1901 }
1902
1903 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1904   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1905 }
1906
1907 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1908   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1909 }
1910
1911 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1912   switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1913   case GlobalVariable::NotThreadLocal:
1914     return LLVMNotThreadLocal;
1915   case GlobalVariable::GeneralDynamicTLSModel:
1916     return LLVMGeneralDynamicTLSModel;
1917   case GlobalVariable::LocalDynamicTLSModel:
1918     return LLVMLocalDynamicTLSModel;
1919   case GlobalVariable::InitialExecTLSModel:
1920     return LLVMInitialExecTLSModel;
1921   case GlobalVariable::LocalExecTLSModel:
1922     return LLVMLocalExecTLSModel;
1923   }
1924
1925   llvm_unreachable("Invalid GlobalVariable thread local mode");
1926 }
1927
1928 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1929   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1930
1931   switch (Mode) {
1932   case LLVMNotThreadLocal:
1933     GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1934     break;
1935   case LLVMGeneralDynamicTLSModel:
1936     GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1937     break;
1938   case LLVMLocalDynamicTLSModel:
1939     GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1940     break;
1941   case LLVMInitialExecTLSModel:
1942     GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1943     break;
1944   case LLVMLocalExecTLSModel:
1945     GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1946     break;
1947   }
1948 }
1949
1950 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1951   return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1952 }
1953
1954 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1955   unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1956 }
1957
1958 /*--.. Operations on aliases ......................................--*/
1959
1960 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1961                           const char *Name) {
1962   auto *PTy = cast<PointerType>(unwrap(Ty));
1963   return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1964                                   GlobalValue::ExternalLinkage, Name,
1965                                   unwrap<Constant>(Aliasee), unwrap(M)));
1966 }
1967
1968 LLVMValueRef LLVMGetNamedGlobalAlias(LLVMModuleRef M,
1969                                      const char *Name, size_t NameLen) {
1970   return wrap(unwrap(M)->getNamedAlias(Name));
1971 }
1972
1973 LLVMValueRef LLVMGetFirstGlobalAlias(LLVMModuleRef M) {
1974   Module *Mod = unwrap(M);
1975   Module::alias_iterator I = Mod->alias_begin();
1976   if (I == Mod->alias_end())
1977     return nullptr;
1978   return wrap(&*I);
1979 }
1980
1981 LLVMValueRef LLVMGetLastGlobalAlias(LLVMModuleRef M) {
1982   Module *Mod = unwrap(M);
1983   Module::alias_iterator I = Mod->alias_end();
1984   if (I == Mod->alias_begin())
1985     return nullptr;
1986   return wrap(&*--I);
1987 }
1988
1989 LLVMValueRef LLVMGetNextGlobalAlias(LLVMValueRef GA) {
1990   GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
1991   Module::alias_iterator I(Alias);
1992   if (++I == Alias->getParent()->alias_end())
1993     return nullptr;
1994   return wrap(&*I);
1995 }
1996
1997 LLVMValueRef LLVMGetPreviousGlobalAlias(LLVMValueRef GA) {
1998   GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
1999   Module::alias_iterator I(Alias);
2000   if (I == Alias->getParent()->alias_begin())
2001     return nullptr;
2002   return wrap(&*--I);
2003 }
2004
2005 LLVMValueRef LLVMAliasGetAliasee(LLVMValueRef Alias) {
2006   return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2007 }
2008
2009 void LLVMAliasSetAliasee(LLVMValueRef Alias, LLVMValueRef Aliasee) {
2010   unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2011 }
2012
2013 /*--.. Operations on functions .............................................--*/
2014
2015 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
2016                              LLVMTypeRef FunctionTy) {
2017   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2018                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
2019 }
2020
2021 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
2022   return wrap(unwrap(M)->getFunction(Name));
2023 }
2024
2025 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
2026   Module *Mod = unwrap(M);
2027   Module::iterator I = Mod->begin();
2028   if (I == Mod->end())
2029     return nullptr;
2030   return wrap(&*I);
2031 }
2032
2033 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
2034   Module *Mod = unwrap(M);
2035   Module::iterator I = Mod->end();
2036   if (I == Mod->begin())
2037     return nullptr;
2038   return wrap(&*--I);
2039 }
2040
2041 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
2042   Function *Func = unwrap<Function>(Fn);
2043   Module::iterator I(Func);
2044   if (++I == Func->getParent()->end())
2045     return nullptr;
2046   return wrap(&*I);
2047 }
2048
2049 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
2050   Function *Func = unwrap<Function>(Fn);
2051   Module::iterator I(Func);
2052   if (I == Func->getParent()->begin())
2053     return nullptr;
2054   return wrap(&*--I);
2055 }
2056
2057 void LLVMDeleteFunction(LLVMValueRef Fn) {
2058   unwrap<Function>(Fn)->eraseFromParent();
2059 }
2060
2061 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
2062   return unwrap<Function>(Fn)->hasPersonalityFn();
2063 }
2064
2065 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
2066   return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2067 }
2068
2069 void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
2070   unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2071 }
2072
2073 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
2074   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2075     return F->getIntrinsicID();
2076   return 0;
2077 }
2078
2079 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
2080   return unwrap<Function>(Fn)->getCallingConv();
2081 }
2082
2083 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
2084   return unwrap<Function>(Fn)->setCallingConv(
2085     static_cast<CallingConv::ID>(CC));
2086 }
2087
2088 const char *LLVMGetGC(LLVMValueRef Fn) {
2089   Function *F = unwrap<Function>(Fn);
2090   return F->hasGC()? F->getGC().c_str() : nullptr;
2091 }
2092
2093 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2094   Function *F = unwrap<Function>(Fn);
2095   if (GC)
2096     F->setGC(GC);
2097   else
2098     F->clearGC();
2099 }
2100
2101 void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2102                              LLVMAttributeRef A) {
2103   unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
2104 }
2105
2106 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
2107   auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2108   return AS.getNumAttributes();
2109 }
2110
2111 void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2112                               LLVMAttributeRef *Attrs) {
2113   auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2114   for (auto A : AS)
2115     *Attrs++ = wrap(A);
2116 }
2117
2118 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
2119                                              LLVMAttributeIndex Idx,
2120                                              unsigned KindID) {
2121   return wrap(unwrap<Function>(F)->getAttribute(Idx,
2122                                                 (Attribute::AttrKind)KindID));
2123 }
2124
2125 LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
2126                                                LLVMAttributeIndex Idx,
2127                                                const char *K, unsigned KLen) {
2128   return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
2129 }
2130
2131 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2132                                     unsigned KindID) {
2133   unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
2134 }
2135
2136 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2137                                       const char *K, unsigned KLen) {
2138   unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
2139 }
2140
2141 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
2142                                         const char *V) {
2143   Function *Func = unwrap<Function>(Fn);
2144   Attribute Attr = Attribute::get(Func->getContext(), A, V);
2145   Func->addAttribute(AttributeList::FunctionIndex, Attr);
2146 }
2147
2148 /*--.. Operations on parameters ............................................--*/
2149
2150 unsigned LLVMCountParams(LLVMValueRef FnRef) {
2151   // This function is strictly redundant to
2152   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2153   return unwrap<Function>(FnRef)->arg_size();
2154 }
2155
2156 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2157   Function *Fn = unwrap<Function>(FnRef);
2158   for (Function::arg_iterator I = Fn->arg_begin(),
2159                               E = Fn->arg_end(); I != E; I++)
2160     *ParamRefs++ = wrap(&*I);
2161 }
2162
2163 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
2164   Function *Fn = unwrap<Function>(FnRef);
2165   return wrap(&Fn->arg_begin()[index]);
2166 }
2167
2168 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
2169   return wrap(unwrap<Argument>(V)->getParent());
2170 }
2171
2172 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
2173   Function *Func = unwrap<Function>(Fn);
2174   Function::arg_iterator I = Func->arg_begin();
2175   if (I == Func->arg_end())
2176     return nullptr;
2177   return wrap(&*I);
2178 }
2179
2180 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
2181   Function *Func = unwrap<Function>(Fn);
2182   Function::arg_iterator I = Func->arg_end();
2183   if (I == Func->arg_begin())
2184     return nullptr;
2185   return wrap(&*--I);
2186 }
2187
2188 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
2189   Argument *A = unwrap<Argument>(Arg);
2190   Function *Fn = A->getParent();
2191   if (A->getArgNo() + 1 >= Fn->arg_size())
2192     return nullptr;
2193   return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2194 }
2195
2196 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
2197   Argument *A = unwrap<Argument>(Arg);
2198   if (A->getArgNo() == 0)
2199     return nullptr;
2200   return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2201 }
2202
2203 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
2204   Argument *A = unwrap<Argument>(Arg);
2205   A->addAttr(Attribute::getWithAlignment(A->getContext(), align));
2206 }
2207
2208 /*--.. Operations on basic blocks ..........................................--*/
2209
2210 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
2211   return wrap(static_cast<Value*>(unwrap(BB)));
2212 }
2213
2214 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
2215   return isa<BasicBlock>(unwrap(Val));
2216 }
2217
2218 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
2219   return wrap(unwrap<BasicBlock>(Val));
2220 }
2221
2222 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
2223   return unwrap(BB)->getName().data();
2224 }
2225
2226 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
2227   return wrap(unwrap(BB)->getParent());
2228 }
2229
2230 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
2231   return wrap(unwrap(BB)->getTerminator());
2232 }
2233
2234 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
2235   return unwrap<Function>(FnRef)->size();
2236 }
2237
2238 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2239   Function *Fn = unwrap<Function>(FnRef);
2240   for (BasicBlock &BB : *Fn)
2241     *BasicBlocksRefs++ = wrap(&BB);
2242 }
2243
2244 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2245   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2246 }
2247
2248 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2249   Function *Func = unwrap<Function>(Fn);
2250   Function::iterator I = Func->begin();
2251   if (I == Func->end())
2252     return nullptr;
2253   return wrap(&*I);
2254 }
2255
2256 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2257   Function *Func = unwrap<Function>(Fn);
2258   Function::iterator I = Func->end();
2259   if (I == Func->begin())
2260     return nullptr;
2261   return wrap(&*--I);
2262 }
2263
2264 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2265   BasicBlock *Block = unwrap(BB);
2266   Function::iterator I(Block);
2267   if (++I == Block->getParent()->end())
2268     return nullptr;
2269   return wrap(&*I);
2270 }
2271
2272 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2273   BasicBlock *Block = unwrap(BB);
2274   Function::iterator I(Block);
2275   if (I == Block->getParent()->begin())
2276     return nullptr;
2277   return wrap(&*--I);
2278 }
2279
2280 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2281                                                 LLVMValueRef FnRef,
2282                                                 const char *Name) {
2283   return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2284 }
2285
2286 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2287   return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2288 }
2289
2290 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2291                                                 LLVMBasicBlockRef BBRef,
2292                                                 const char *Name) {
2293   BasicBlock *BB = unwrap(BBRef);
2294   return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2295 }
2296
2297 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
2298                                        const char *Name) {
2299   return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
2300 }
2301
2302 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2303   unwrap(BBRef)->eraseFromParent();
2304 }
2305
2306 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2307   unwrap(BBRef)->removeFromParent();
2308 }
2309
2310 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2311   unwrap(BB)->moveBefore(unwrap(MovePos));
2312 }
2313
2314 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2315   unwrap(BB)->moveAfter(unwrap(MovePos));
2316 }
2317
2318 /*--.. Operations on instructions ..........................................--*/
2319
2320 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2321   return wrap(unwrap<Instruction>(Inst)->getParent());
2322 }
2323
2324 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2325   BasicBlock *Block = unwrap(BB);
2326   BasicBlock::iterator I = Block->begin();
2327   if (I == Block->end())
2328     return nullptr;
2329   return wrap(&*I);
2330 }
2331
2332 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2333   BasicBlock *Block = unwrap(BB);
2334   BasicBlock::iterator I = Block->end();
2335   if (I == Block->begin())
2336     return nullptr;
2337   return wrap(&*--I);
2338 }
2339
2340 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2341   Instruction *Instr = unwrap<Instruction>(Inst);
2342   BasicBlock::iterator I(Instr);
2343   if (++I == Instr->getParent()->end())
2344     return nullptr;
2345   return wrap(&*I);
2346 }
2347
2348 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2349   Instruction *Instr = unwrap<Instruction>(Inst);
2350   BasicBlock::iterator I(Instr);
2351   if (I == Instr->getParent()->begin())
2352     return nullptr;
2353   return wrap(&*--I);
2354 }
2355
2356 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2357   unwrap<Instruction>(Inst)->removeFromParent();
2358 }
2359
2360 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2361   unwrap<Instruction>(Inst)->eraseFromParent();
2362 }
2363
2364 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
2365   if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2366     return (LLVMIntPredicate)I->getPredicate();
2367   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2368     if (CE->getOpcode() == Instruction::ICmp)
2369       return (LLVMIntPredicate)CE->getPredicate();
2370   return (LLVMIntPredicate)0;
2371 }
2372
2373 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2374   if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2375     return (LLVMRealPredicate)I->getPredicate();
2376   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2377     if (CE->getOpcode() == Instruction::FCmp)
2378       return (LLVMRealPredicate)CE->getPredicate();
2379   return (LLVMRealPredicate)0;
2380 }
2381
2382 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2383   if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2384     return map_to_llvmopcode(C->getOpcode());
2385   return (LLVMOpcode)0;
2386 }
2387
2388 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2389   if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2390     return wrap(C->clone());
2391   return nullptr;
2392 }
2393
2394 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
2395   if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2396     return FPI->getNumArgOperands();
2397   }
2398   return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands();
2399 }
2400
2401 /*--.. Call and invoke instructions ........................................--*/
2402
2403 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
2404   return CallSite(unwrap<Instruction>(Instr)).getCallingConv();
2405 }
2406
2407 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2408   return CallSite(unwrap<Instruction>(Instr))
2409     .setCallingConv(static_cast<CallingConv::ID>(CC));
2410 }
2411
2412 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
2413                                 unsigned align) {
2414   CallSite Call = CallSite(unwrap<Instruction>(Instr));
2415   Attribute AlignAttr = Attribute::getWithAlignment(Call->getContext(), align);
2416   Call.addAttribute(index, AlignAttr);
2417 }
2418
2419 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2420                               LLVMAttributeRef A) {
2421   CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A));
2422 }
2423
2424 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2425                                        LLVMAttributeIndex Idx) {
2426   auto CS = CallSite(unwrap<Instruction>(C));
2427   auto AS = CS.getAttributes().getAttributes(Idx);
2428   return AS.getNumAttributes();
2429 }
2430
2431 void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2432                                LLVMAttributeRef *Attrs) {
2433   auto CS = CallSite(unwrap<Instruction>(C));
2434   auto AS = CS.getAttributes().getAttributes(Idx);
2435   for (auto A : AS)
2436     *Attrs++ = wrap(A);
2437 }
2438
2439 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
2440                                               LLVMAttributeIndex Idx,
2441                                               unsigned KindID) {
2442   return wrap(CallSite(unwrap<Instruction>(C))
2443     .getAttribute(Idx, (Attribute::AttrKind)KindID));
2444 }
2445
2446 LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
2447                                                 LLVMAttributeIndex Idx,
2448                                                 const char *K, unsigned KLen) {
2449   return wrap(CallSite(unwrap<Instruction>(C))
2450     .getAttribute(Idx, StringRef(K, KLen)));
2451 }
2452
2453 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2454                                      unsigned KindID) {
2455   CallSite(unwrap<Instruction>(C))
2456     .removeAttribute(Idx, (Attribute::AttrKind)KindID);
2457 }
2458
2459 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2460                                        const char *K, unsigned KLen) {
2461   CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen));
2462 }
2463
2464 LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2465   return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue());
2466 }
2467
2468 /*--.. Operations on call instructions (only) ..............................--*/
2469
2470 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
2471   return unwrap<CallInst>(Call)->isTailCall();
2472 }
2473
2474 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2475   unwrap<CallInst>(Call)->setTailCall(isTailCall);
2476 }
2477
2478 /*--.. Operations on invoke instructions (only) ............................--*/
2479
2480 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2481   return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2482 }
2483
2484 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
2485   if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2486     return wrap(CRI->getUnwindDest());
2487   } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2488     return wrap(CSI->getUnwindDest());
2489   }
2490   return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2491 }
2492
2493 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2494   unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2495 }
2496
2497 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2498   if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2499     return CRI->setUnwindDest(unwrap(B));
2500   } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2501     return CSI->setUnwindDest(unwrap(B));
2502   }
2503   unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2504 }
2505
2506 /*--.. Operations on terminators ...........................................--*/
2507
2508 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2509   return unwrap<TerminatorInst>(Term)->getNumSuccessors();
2510 }
2511
2512 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2513   return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
2514 }
2515
2516 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2517   return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
2518 }
2519
2520 /*--.. Operations on branch instructions (only) ............................--*/
2521
2522 LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2523   return unwrap<BranchInst>(Branch)->isConditional();
2524 }
2525
2526 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2527   return wrap(unwrap<BranchInst>(Branch)->getCondition());
2528 }
2529
2530 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2531   return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2532 }
2533
2534 /*--.. Operations on switch instructions (only) ............................--*/
2535
2536 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2537   return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2538 }
2539
2540 /*--.. Operations on alloca instructions (only) ............................--*/
2541
2542 LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
2543   return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2544 }
2545
2546 /*--.. Operations on gep instructions (only) ...............................--*/
2547
2548 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
2549   return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2550 }
2551
2552 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
2553   return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2554 }
2555
2556 /*--.. Operations on phi nodes .............................................--*/
2557
2558 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2559                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2560   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2561   for (unsigned I = 0; I != Count; ++I)
2562     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2563 }
2564
2565 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2566   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2567 }
2568
2569 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2570   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2571 }
2572
2573 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2574   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2575 }
2576
2577 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2578
2579 unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
2580   auto *I = unwrap(Inst);
2581   if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
2582     return GEP->getNumIndices();
2583   if (auto *EV = dyn_cast<ExtractValueInst>(I))
2584     return EV->getNumIndices();
2585   if (auto *IV = dyn_cast<InsertValueInst>(I))
2586     return IV->getNumIndices();
2587   llvm_unreachable(
2588     "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2589 }
2590
2591 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2592   auto *I = unwrap(Inst);
2593   if (auto *EV = dyn_cast<ExtractValueInst>(I))
2594     return EV->getIndices().data();
2595   if (auto *IV = dyn_cast<InsertValueInst>(I))
2596     return IV->getIndices().data();
2597   llvm_unreachable(
2598     "LLVMGetIndices applies only to extractvalue and insertvalue!");
2599 }
2600
2601
2602 /*===-- Instruction builders ----------------------------------------------===*/
2603
2604 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2605   return wrap(new IRBuilder<>(*unwrap(C)));
2606 }
2607
2608 LLVMBuilderRef LLVMCreateBuilder(void) {
2609   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
2610 }
2611
2612 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2613                          LLVMValueRef Instr) {
2614   BasicBlock *BB = unwrap(Block);
2615   auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
2616   unwrap(Builder)->SetInsertPoint(BB, I);
2617 }
2618
2619 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2620   Instruction *I = unwrap<Instruction>(Instr);
2621   unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
2622 }
2623
2624 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2625   BasicBlock *BB = unwrap(Block);
2626   unwrap(Builder)->SetInsertPoint(BB);
2627 }
2628
2629 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2630    return wrap(unwrap(Builder)->GetInsertBlock());
2631 }
2632
2633 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
2634   unwrap(Builder)->ClearInsertionPoint();
2635 }
2636
2637 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2638   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2639 }
2640
2641 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2642                                    const char *Name) {
2643   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2644 }
2645
2646 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2647   delete unwrap(Builder);
2648 }
2649
2650 /*--.. Metadata builders ...................................................--*/
2651
2652 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
2653   MDNode *Loc =
2654       L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
2655   unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
2656 }
2657
2658 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
2659   LLVMContext &Context = unwrap(Builder)->getContext();
2660   return wrap(MetadataAsValue::get(
2661       Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
2662 }
2663
2664 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2665   unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2666 }
2667
2668
2669 /*--.. Instruction builders ................................................--*/
2670
2671 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2672   return wrap(unwrap(B)->CreateRetVoid());
2673 }
2674
2675 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2676   return wrap(unwrap(B)->CreateRet(unwrap(V)));
2677 }
2678
2679 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2680                                    unsigned N) {
2681   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2682 }
2683
2684 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2685   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2686 }
2687
2688 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2689                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2690   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2691 }
2692
2693 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2694                              LLVMBasicBlockRef Else, unsigned NumCases) {
2695   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2696 }
2697
2698 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2699                                  unsigned NumDests) {
2700   return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2701 }
2702
2703 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2704                              LLVMValueRef *Args, unsigned NumArgs,
2705                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2706                              const char *Name) {
2707   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2708                                       makeArrayRef(unwrap(Args), NumArgs),
2709                                       Name));
2710 }
2711
2712 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2713                                  LLVMValueRef PersFn, unsigned NumClauses,
2714                                  const char *Name) {
2715   // The personality used to live on the landingpad instruction, but now it
2716   // lives on the parent function. For compatibility, take the provided
2717   // personality and put it on the parent function.
2718   if (PersFn)
2719     unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
2720         cast<Function>(unwrap(PersFn)));
2721   return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
2722 }
2723
2724 LLVMValueRef LLVMBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
2725                                LLVMValueRef *Args, unsigned NumArgs,
2726                                const char *Name) {
2727   return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
2728                                         makeArrayRef(unwrap(Args), NumArgs),
2729                                         Name));
2730 }
2731
2732 LLVMValueRef LLVMBuildCleanupPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
2733                                  LLVMValueRef *Args, unsigned NumArgs,
2734                                  const char *Name) {
2735   if (ParentPad == nullptr) {
2736     Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
2737     ParentPad = wrap(Constant::getNullValue(Ty));
2738   }
2739   return wrap(unwrap(B)->CreateCleanupPad(unwrap(ParentPad),
2740                                           makeArrayRef(unwrap(Args), NumArgs),
2741                                           Name));
2742 }
2743
2744 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2745   return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2746 }
2747
2748 LLVMValueRef LLVMBuildCatchSwitch(LLVMBuilderRef B, LLVMValueRef ParentPad,
2749                                   LLVMBasicBlockRef UnwindBB,
2750                                   unsigned NumHandlers, const char *Name) {
2751   if (ParentPad == nullptr) {
2752     Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
2753     ParentPad = wrap(Constant::getNullValue(Ty));
2754   }
2755   return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
2756                                            NumHandlers, Name));
2757 }
2758
2759 LLVMValueRef LLVMBuildCatchRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
2760                                LLVMBasicBlockRef BB) {
2761   return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
2762                                         unwrap(BB)));
2763 }
2764
2765 LLVMValueRef LLVMBuildCleanupRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
2766                                  LLVMBasicBlockRef BB) {
2767   return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
2768                                           unwrap(BB)));
2769 }
2770
2771 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2772   return wrap(unwrap(B)->CreateUnreachable());
2773 }
2774
2775 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2776                  LLVMBasicBlockRef Dest) {
2777   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2778 }
2779
2780 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2781   unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2782 }
2783
2784 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
2785   return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
2786 }
2787
2788 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
2789   return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
2790 }
2791
2792 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2793   unwrap<LandingPadInst>(LandingPad)->
2794     addClause(cast<Constant>(unwrap(ClauseVal)));
2795 }
2796
2797 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
2798   return unwrap<LandingPadInst>(LandingPad)->isCleanup();
2799 }
2800
2801 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2802   unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2803 }
2804
2805 void LLVMAddHandler(LLVMValueRef CatchSwitch, LLVMBasicBlockRef Dest) {
2806   unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
2807 }
2808
2809 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
2810   return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
2811 }
2812
2813 void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
2814   CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
2815   for (CatchSwitchInst::handler_iterator I = CSI->handler_begin(),
2816                                          E = CSI->handler_end(); I != E; ++I)
2817     *Handlers++ = wrap(*I);
2818 }
2819
2820 LLVMValueRef LLVMGetParentCatchSwitch(LLVMValueRef CatchPad) {
2821   return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
2822 }
2823
2824 void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad, LLVMValueRef CatchSwitch) {
2825   unwrap<CatchPadInst>(CatchPad)
2826     ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
2827 }
2828
2829 /*--.. Funclets ...........................................................--*/
2830
2831 LLVMValueRef LLVMGetArgOperand(LLVMValueRef Funclet, unsigned i) {
2832   return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
2833 }
2834
2835 void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
2836   unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
2837 }
2838
2839 /*--.. Arithmetic ..........................................................--*/
2840
2841 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2842                           const char *Name) {
2843   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2844 }
2845
2846 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2847                           const char *Name) {
2848   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2849 }
2850
2851 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2852                           const char *Name) {
2853   return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2854 }
2855
2856 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2857                           const char *Name) {
2858   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2859 }
2860
2861 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2862                           const char *Name) {
2863   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2864 }
2865
2866 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2867                           const char *Name) {
2868   return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2869 }
2870
2871 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2872                           const char *Name) {
2873   return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2874 }
2875
2876 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2877                           const char *Name) {
2878   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2879 }
2880
2881 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2882                           const char *Name) {
2883   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2884 }
2885
2886 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2887                           const char *Name) {
2888   return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2889 }
2890
2891 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2892                           const char *Name) {
2893   return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2894 }
2895
2896 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2897                           const char *Name) {
2898   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2899 }
2900
2901 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2902                            const char *Name) {
2903   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2904 }
2905
2906 LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2907                                 LLVMValueRef RHS, const char *Name) {
2908   return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
2909 }
2910
2911 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2912                            const char *Name) {
2913   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2914 }
2915
2916 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2917                                 LLVMValueRef RHS, const char *Name) {
2918   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2919 }
2920
2921 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2922                            const char *Name) {
2923   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2924 }
2925
2926 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2927                            const char *Name) {
2928   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2929 }
2930
2931 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2932                            const char *Name) {
2933   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2934 }
2935
2936 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2937                            const char *Name) {
2938   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2939 }
2940
2941 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2942                           const char *Name) {
2943   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2944 }
2945
2946 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2947                            const char *Name) {
2948   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2949 }
2950
2951 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2952                            const char *Name) {
2953   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2954 }
2955
2956 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2957                           const char *Name) {
2958   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2959 }
2960
2961 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2962                          const char *Name) {
2963   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2964 }
2965
2966 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2967                           const char *Name) {
2968   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2969 }
2970
2971 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2972                             LLVMValueRef LHS, LLVMValueRef RHS,
2973                             const char *Name) {
2974   return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2975                                      unwrap(RHS), Name));
2976 }
2977
2978 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2979   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2980 }
2981
2982 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2983                              const char *Name) {
2984   return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2985 }
2986
2987 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2988                              const char *Name) {
2989   return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2990 }
2991
2992 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2993   return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2994 }
2995
2996 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2997   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2998 }
2999
3000 /*--.. Memory ..............................................................--*/
3001
3002 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
3003                              const char *Name) {
3004   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3005   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3006   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3007   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3008                                                ITy, unwrap(Ty), AllocSize,
3009                                                nullptr, nullptr, "");
3010   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3011 }
3012
3013 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
3014                                   LLVMValueRef Val, const char *Name) {
3015   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3016   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3017   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3018   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3019                                                ITy, unwrap(Ty), AllocSize,
3020                                                unwrap(Val), nullptr, "");
3021   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3022 }
3023
3024 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
3025                              const char *Name) {
3026   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3027 }
3028
3029 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
3030                                   LLVMValueRef Val, const char *Name) {
3031   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3032 }
3033
3034 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
3035   return wrap(unwrap(B)->Insert(
3036      CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
3037 }
3038
3039 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
3040                            const char *Name) {
3041   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
3042 }
3043
3044 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
3045                             LLVMValueRef PointerVal) {
3046   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3047 }
3048
3049 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
3050   switch (Ordering) {
3051     case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
3052     case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
3053     case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
3054     case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
3055     case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
3056     case LLVMAtomicOrderingAcquireRelease:
3057       return AtomicOrdering::AcquireRelease;
3058     case LLVMAtomicOrderingSequentiallyConsistent:
3059       return AtomicOrdering::SequentiallyConsistent;
3060   }
3061
3062   llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3063 }
3064
3065 static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
3066   switch (Ordering) {
3067     case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
3068     case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
3069     case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
3070     case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
3071     case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
3072     case AtomicOrdering::AcquireRelease:
3073       return LLVMAtomicOrderingAcquireRelease;
3074     case AtomicOrdering::SequentiallyConsistent:
3075       return LLVMAtomicOrderingSequentiallyConsistent;
3076   }
3077
3078   llvm_unreachable("Invalid AtomicOrdering value!");
3079 }
3080
3081 // TODO: Should this and other atomic instructions support building with
3082 // "syncscope"?
3083 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
3084                             LLVMBool isSingleThread, const char *Name) {
3085   return wrap(
3086     unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3087                            isSingleThread ? SyncScope::SingleThread
3088                                           : SyncScope::System,
3089                            Name));
3090 }
3091
3092 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
3093                           LLVMValueRef *Indices, unsigned NumIndices,
3094                           const char *Name) {
3095   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3096   return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
3097 }
3098
3099 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
3100                                   LLVMValueRef *Indices, unsigned NumIndices,
3101                                   const char *Name) {
3102   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3103   return wrap(
3104       unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
3105 }
3106
3107 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
3108                                 unsigned Idx, const char *Name) {
3109   return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
3110 }
3111
3112 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
3113                                    const char *Name) {
3114   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3115 }
3116
3117 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
3118                                       const char *Name) {
3119   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
3120 }
3121
3122 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
3123   Value *P = unwrap<Value>(MemAccessInst);
3124   if (LoadInst *LI = dyn_cast<LoadInst>(P))
3125     return LI->isVolatile();
3126   return cast<StoreInst>(P)->isVolatile();
3127 }
3128
3129 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
3130   Value *P = unwrap<Value>(MemAccessInst);
3131   if (LoadInst *LI = dyn_cast<LoadInst>(P))
3132     return LI->setVolatile(isVolatile);
3133   return cast<StoreInst>(P)->setVolatile(isVolatile);
3134 }
3135
3136 LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
3137   Value *P = unwrap<Value>(MemAccessInst);
3138   AtomicOrdering O;
3139   if (LoadInst *LI = dyn_cast<LoadInst>(P))
3140     O = LI->getOrdering();
3141   else
3142     O = cast<StoreInst>(P)->getOrdering();
3143   return mapToLLVMOrdering(O);
3144 }
3145
3146 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
3147   Value *P = unwrap<Value>(MemAccessInst);
3148   AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3149
3150   if (LoadInst *LI = dyn_cast<LoadInst>(P))
3151     return LI->setOrdering(O);
3152   return cast<StoreInst>(P)->setOrdering(O);
3153 }
3154
3155 /*--.. Casts ...............................................................--*/
3156
3157 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
3158                             LLVMTypeRef DestTy, const char *Name) {
3159   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
3160 }
3161
3162 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
3163                            LLVMTypeRef DestTy, const char *Name) {
3164   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
3165 }
3166
3167 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
3168                            LLVMTypeRef DestTy, const char *Name) {
3169   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
3170 }
3171
3172 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
3173                              LLVMTypeRef DestTy, const char *Name) {
3174   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
3175 }
3176
3177 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
3178                              LLVMTypeRef DestTy, const char *Name) {
3179   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
3180 }
3181
3182 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
3183                              LLVMTypeRef DestTy, const char *Name) {
3184   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
3185 }
3186
3187 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
3188                              LLVMTypeRef DestTy, const char *Name) {
3189   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
3190 }
3191
3192 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
3193                               LLVMTypeRef DestTy, const char *Name) {
3194   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
3195 }
3196
3197 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
3198                             LLVMTypeRef DestTy, const char *Name) {
3199   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
3200 }
3201
3202 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
3203                                LLVMTypeRef DestTy, const char *Name) {
3204   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
3205 }
3206
3207 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
3208                                LLVMTypeRef DestTy, const char *Name) {
3209   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
3210 }
3211
3212 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3213                               LLVMTypeRef DestTy, const char *Name) {
3214   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
3215 }
3216
3217 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
3218                                     LLVMTypeRef DestTy, const char *Name) {
3219   return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
3220 }
3221
3222 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3223                                     LLVMTypeRef DestTy, const char *Name) {
3224   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
3225                                              Name));
3226 }
3227
3228 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3229                                     LLVMTypeRef DestTy, const char *Name) {
3230   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
3231                                              Name));
3232 }
3233
3234 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3235                                      LLVMTypeRef DestTy, const char *Name) {
3236   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
3237                                               Name));
3238 }
3239
3240 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
3241                            LLVMTypeRef DestTy, const char *Name) {
3242   return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
3243                                     unwrap(DestTy), Name));
3244 }
3245
3246 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
3247                                   LLVMTypeRef DestTy, const char *Name) {
3248   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
3249 }
3250
3251 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
3252                               LLVMTypeRef DestTy, const char *Name) {
3253   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
3254                                        /*isSigned*/true, Name));
3255 }
3256
3257 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
3258                              LLVMTypeRef DestTy, const char *Name) {
3259   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
3260 }
3261
3262 /*--.. Comparisons .........................................................--*/
3263
3264 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
3265                            LLVMValueRef LHS, LLVMValueRef RHS,
3266                            const char *Name) {
3267   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
3268                                     unwrap(LHS), unwrap(RHS), Name));
3269 }
3270
3271 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
3272                            LLVMValueRef LHS, LLVMValueRef RHS,
3273                            const char *Name) {
3274   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
3275                                     unwrap(LHS), unwrap(RHS), Name));
3276 }
3277
3278 /*--.. Miscellaneous instructions ..........................................--*/
3279
3280 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
3281   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
3282 }
3283
3284 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
3285                            LLVMValueRef *Args, unsigned NumArgs,
3286                            const char *Name) {
3287   return wrap(unwrap(B)->CreateCall(unwrap(Fn),
3288                                     makeArrayRef(unwrap(Args), NumArgs),
3289                                     Name));
3290 }
3291
3292 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
3293                              LLVMValueRef Then, LLVMValueRef Else,
3294                              const char *Name) {
3295   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
3296                                       Name));
3297 }
3298
3299 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
3300                             LLVMTypeRef Ty, const char *Name) {
3301   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
3302 }
3303
3304 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
3305                                       LLVMValueRef Index, const char *Name) {
3306   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
3307                                               Name));
3308 }
3309
3310 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
3311                                     LLVMValueRef EltVal, LLVMValueRef Index,
3312                                     const char *Name) {
3313   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
3314                                              unwrap(Index), Name));
3315 }
3316
3317 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
3318                                     LLVMValueRef V2, LLVMValueRef Mask,
3319                                     const char *Name) {
3320   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
3321                                              unwrap(Mask), Name));
3322 }
3323
3324 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3325                                    unsigned Index, const char *Name) {
3326   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
3327 }
3328
3329 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3330                                   LLVMValueRef EltVal, unsigned Index,
3331                                   const char *Name) {
3332   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3333                                            Index, Name));
3334 }
3335
3336 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
3337                              const char *Name) {
3338   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
3339 }
3340
3341 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
3342                                 const char *Name) {
3343   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3344 }
3345
3346 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
3347                               LLVMValueRef RHS, const char *Name) {
3348   return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
3349 }
3350
3351 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
3352                                LLVMValueRef PTR, LLVMValueRef Val,
3353                                LLVMAtomicOrdering ordering,
3354                                LLVMBool singleThread) {
3355   AtomicRMWInst::BinOp intop;
3356   switch (op) {
3357     case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
3358     case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
3359     case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
3360     case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
3361     case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
3362     case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
3363     case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
3364     case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
3365     case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
3366     case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
3367     case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
3368   }
3369   return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
3370     mapFromLLVMOrdering(ordering), singleThread ? SyncScope::SingleThread
3371                                                 : SyncScope::System));
3372 }
3373
3374 LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
3375                                     LLVMValueRef Cmp, LLVMValueRef New,
3376                                     LLVMAtomicOrdering SuccessOrdering,
3377                                     LLVMAtomicOrdering FailureOrdering,
3378                                     LLVMBool singleThread) {
3379
3380   return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
3381                 unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
3382                 mapFromLLVMOrdering(FailureOrdering),
3383                 singleThread ? SyncScope::SingleThread : SyncScope::System));
3384 }
3385
3386
3387 LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
3388   Value *P = unwrap<Value>(AtomicInst);
3389
3390   if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3391     return I->getSyncScopeID() == SyncScope::SingleThread;
3392   return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() ==
3393              SyncScope::SingleThread;
3394 }
3395
3396 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
3397   Value *P = unwrap<Value>(AtomicInst);
3398   SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
3399
3400   if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3401     return I->setSyncScopeID(SSID);
3402   return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID);
3403 }
3404
3405 LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst)  {
3406   Value *P = unwrap<Value>(CmpXchgInst);
3407   return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
3408 }
3409
3410 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
3411                                    LLVMAtomicOrdering Ordering) {
3412   Value *P = unwrap<Value>(CmpXchgInst);
3413   AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3414
3415   return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
3416 }
3417
3418 LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst)  {
3419   Value *P = unwrap<Value>(CmpXchgInst);
3420   return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
3421 }
3422
3423 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
3424                                    LLVMAtomicOrdering Ordering) {
3425   Value *P = unwrap<Value>(CmpXchgInst);
3426   AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3427
3428   return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
3429 }
3430
3431 /*===-- Module providers --------------------------------------------------===*/
3432
3433 LLVMModuleProviderRef
3434 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
3435   return reinterpret_cast<LLVMModuleProviderRef>(M);
3436 }
3437
3438 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
3439   delete unwrap(MP);
3440 }
3441
3442
3443 /*===-- Memory buffers ----------------------------------------------------===*/
3444
3445 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
3446     const char *Path,
3447     LLVMMemoryBufferRef *OutMemBuf,
3448     char **OutMessage) {
3449
3450   ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
3451   if (std::error_code EC = MBOrErr.getError()) {
3452     *OutMessage = strdup(EC.message().c_str());
3453     return 1;
3454   }
3455   *OutMemBuf = wrap(MBOrErr.get().release());
3456   return 0;
3457 }
3458
3459 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
3460                                          char **OutMessage) {
3461   ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
3462   if (std::error_code EC = MBOrErr.getError()) {
3463     *OutMessage = strdup(EC.message().c_str());
3464     return 1;
3465   }
3466   *OutMemBuf = wrap(MBOrErr.get().release());
3467   return 0;
3468 }
3469
3470 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
3471     const char *InputData,
3472     size_t InputDataLength,
3473     const char *BufferName,
3474     LLVMBool RequiresNullTerminator) {
3475
3476   return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
3477                                          StringRef(BufferName),
3478                                          RequiresNullTerminator).release());
3479 }
3480
3481 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
3482     const char *InputData,
3483     size_t InputDataLength,
3484     const char *BufferName) {
3485
3486   return wrap(
3487       MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
3488                                      StringRef(BufferName)).release());
3489 }
3490
3491 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
3492   return unwrap(MemBuf)->getBufferStart();
3493 }
3494
3495 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
3496   return unwrap(MemBuf)->getBufferSize();
3497 }
3498
3499 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
3500   delete unwrap(MemBuf);
3501 }
3502
3503 /*===-- Pass Registry -----------------------------------------------------===*/
3504
3505 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
3506   return wrap(PassRegistry::getPassRegistry());
3507 }
3508
3509 /*===-- Pass Manager ------------------------------------------------------===*/
3510
3511 LLVMPassManagerRef LLVMCreatePassManager() {
3512   return wrap(new legacy::PassManager());
3513 }
3514
3515 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
3516   return wrap(new legacy::FunctionPassManager(unwrap(M)));
3517 }
3518
3519 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
3520   return LLVMCreateFunctionPassManagerForModule(
3521                                             reinterpret_cast<LLVMModuleRef>(P));
3522 }
3523
3524 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
3525   return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
3526 }
3527
3528 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
3529   return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
3530 }
3531
3532 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
3533   return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
3534 }
3535
3536 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
3537   return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
3538 }
3539
3540 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
3541   delete unwrap(PM);
3542 }
3543
3544 /*===-- Threading ------------------------------------------------------===*/
3545
3546 LLVMBool LLVMStartMultithreaded() {
3547   return LLVMIsMultithreaded();
3548 }
3549
3550 void LLVMStopMultithreaded() {
3551 }
3552
3553 LLVMBool LLVMIsMultithreaded() {
3554   return llvm_is_multithreaded();
3555 }