xref: /llvm-project-15.0.7/llvm/lib/IR/Core.cpp (revision 28331ae6)
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/Bitcode/ReaderWriter.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/GlobalAlias.h"
22 #include "llvm/IR/GlobalVariable.h"
23 #include "llvm/IR/IRBuilder.h"
24 #include "llvm/IR/InlineAsm.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/PassManager.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/ManagedStatic.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/Threading.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Support/system_error.h"
36 #include <cassert>
37 #include <cstdlib>
38 #include <cstring>
39 
40 using namespace llvm;
41 
42 void llvm::initializeCore(PassRegistry &Registry) {
43   initializeDominatorTreeWrapperPassPass(Registry);
44   initializePrintModulePassWrapperPass(Registry);
45   initializePrintFunctionPassWrapperPass(Registry);
46   initializePrintBasicBlockPassPass(Registry);
47   initializeVerifierLegacyPassPass(Registry);
48 }
49 
50 void LLVMInitializeCore(LLVMPassRegistryRef R) {
51   initializeCore(*unwrap(R));
52 }
53 
54 void LLVMShutdown() {
55   llvm_shutdown();
56 }
57 
58 /*===-- Error handling ----------------------------------------------------===*/
59 
60 char *LLVMCreateMessage(const char *Message) {
61   return strdup(Message);
62 }
63 
64 void LLVMDisposeMessage(char *Message) {
65   free(Message);
66 }
67 
68 
69 /*===-- Operations on contexts --------------------------------------------===*/
70 
71 LLVMContextRef LLVMContextCreate() {
72   return wrap(new LLVMContext());
73 }
74 
75 LLVMContextRef LLVMGetGlobalContext() {
76   return wrap(&getGlobalContext());
77 }
78 
79 void LLVMContextDispose(LLVMContextRef C) {
80   delete unwrap(C);
81 }
82 
83 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
84                                   unsigned SLen) {
85   return unwrap(C)->getMDKindID(StringRef(Name, SLen));
86 }
87 
88 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
89   return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
90 }
91 
92 
93 /*===-- Operations on modules ---------------------------------------------===*/
94 
95 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
96   return wrap(new Module(ModuleID, getGlobalContext()));
97 }
98 
99 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
100                                                 LLVMContextRef C) {
101   return wrap(new Module(ModuleID, *unwrap(C)));
102 }
103 
104 void LLVMDisposeModule(LLVMModuleRef M) {
105   delete unwrap(M);
106 }
107 
108 /*--.. Data layout .........................................................--*/
109 const char * LLVMGetDataLayout(LLVMModuleRef M) {
110   return unwrap(M)->getDataLayoutStr().c_str();
111 }
112 
113 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
114   unwrap(M)->setDataLayout(Triple);
115 }
116 
117 /*--.. Target triple .......................................................--*/
118 const char * LLVMGetTarget(LLVMModuleRef M) {
119   return unwrap(M)->getTargetTriple().c_str();
120 }
121 
122 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
123   unwrap(M)->setTargetTriple(Triple);
124 }
125 
126 void LLVMDumpModule(LLVMModuleRef M) {
127   unwrap(M)->dump();
128 }
129 
130 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
131                                char **ErrorMessage) {
132   std::string error;
133   raw_fd_ostream dest(Filename, error, sys::fs::F_Text);
134   if (!error.empty()) {
135     *ErrorMessage = strdup(error.c_str());
136     return true;
137   }
138 
139   unwrap(M)->print(dest, nullptr);
140 
141   if (!error.empty()) {
142     *ErrorMessage = strdup(error.c_str());
143     return true;
144   }
145   dest.flush();
146   return false;
147 }
148 
149 char *LLVMPrintModuleToString(LLVMModuleRef M) {
150   std::string buf;
151   raw_string_ostream os(buf);
152 
153   unwrap(M)->print(os, nullptr);
154   os.flush();
155 
156   return strdup(buf.c_str());
157 }
158 
159 /*--.. Operations on inline assembler ......................................--*/
160 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
161   unwrap(M)->setModuleInlineAsm(StringRef(Asm));
162 }
163 
164 
165 /*--.. Operations on module contexts ......................................--*/
166 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
167   return wrap(&unwrap(M)->getContext());
168 }
169 
170 
171 /*===-- Operations on types -----------------------------------------------===*/
172 
173 /*--.. Operations on all types (mostly) ....................................--*/
174 
175 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
176   switch (unwrap(Ty)->getTypeID()) {
177   case Type::VoidTyID:
178     return LLVMVoidTypeKind;
179   case Type::HalfTyID:
180     return LLVMHalfTypeKind;
181   case Type::FloatTyID:
182     return LLVMFloatTypeKind;
183   case Type::DoubleTyID:
184     return LLVMDoubleTypeKind;
185   case Type::X86_FP80TyID:
186     return LLVMX86_FP80TypeKind;
187   case Type::FP128TyID:
188     return LLVMFP128TypeKind;
189   case Type::PPC_FP128TyID:
190     return LLVMPPC_FP128TypeKind;
191   case Type::LabelTyID:
192     return LLVMLabelTypeKind;
193   case Type::MetadataTyID:
194     return LLVMMetadataTypeKind;
195   case Type::IntegerTyID:
196     return LLVMIntegerTypeKind;
197   case Type::FunctionTyID:
198     return LLVMFunctionTypeKind;
199   case Type::StructTyID:
200     return LLVMStructTypeKind;
201   case Type::ArrayTyID:
202     return LLVMArrayTypeKind;
203   case Type::PointerTyID:
204     return LLVMPointerTypeKind;
205   case Type::VectorTyID:
206     return LLVMVectorTypeKind;
207   case Type::X86_MMXTyID:
208     return LLVMX86_MMXTypeKind;
209   }
210   llvm_unreachable("Unhandled TypeID.");
211 }
212 
213 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
214 {
215     return unwrap(Ty)->isSized();
216 }
217 
218 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
219   return wrap(&unwrap(Ty)->getContext());
220 }
221 
222 void LLVMDumpType(LLVMTypeRef Ty) {
223   return unwrap(Ty)->dump();
224 }
225 
226 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
227   std::string buf;
228   raw_string_ostream os(buf);
229 
230   unwrap(Ty)->print(os);
231   os.flush();
232 
233   return strdup(buf.c_str());
234 }
235 
236 /*--.. Operations on integer types .........................................--*/
237 
238 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C)  {
239   return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
240 }
241 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C)  {
242   return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
243 }
244 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
245   return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
246 }
247 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
248   return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
249 }
250 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
251   return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
252 }
253 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
254   return wrap(IntegerType::get(*unwrap(C), NumBits));
255 }
256 
257 LLVMTypeRef LLVMInt1Type(void)  {
258   return LLVMInt1TypeInContext(LLVMGetGlobalContext());
259 }
260 LLVMTypeRef LLVMInt8Type(void)  {
261   return LLVMInt8TypeInContext(LLVMGetGlobalContext());
262 }
263 LLVMTypeRef LLVMInt16Type(void) {
264   return LLVMInt16TypeInContext(LLVMGetGlobalContext());
265 }
266 LLVMTypeRef LLVMInt32Type(void) {
267   return LLVMInt32TypeInContext(LLVMGetGlobalContext());
268 }
269 LLVMTypeRef LLVMInt64Type(void) {
270   return LLVMInt64TypeInContext(LLVMGetGlobalContext());
271 }
272 LLVMTypeRef LLVMIntType(unsigned NumBits) {
273   return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
274 }
275 
276 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
277   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
278 }
279 
280 /*--.. Operations on real types ............................................--*/
281 
282 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
283   return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
284 }
285 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
286   return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
287 }
288 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
289   return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
290 }
291 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
292   return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
293 }
294 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
295   return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
296 }
297 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
298   return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
299 }
300 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
301   return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
302 }
303 
304 LLVMTypeRef LLVMHalfType(void) {
305   return LLVMHalfTypeInContext(LLVMGetGlobalContext());
306 }
307 LLVMTypeRef LLVMFloatType(void) {
308   return LLVMFloatTypeInContext(LLVMGetGlobalContext());
309 }
310 LLVMTypeRef LLVMDoubleType(void) {
311   return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
312 }
313 LLVMTypeRef LLVMX86FP80Type(void) {
314   return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
315 }
316 LLVMTypeRef LLVMFP128Type(void) {
317   return LLVMFP128TypeInContext(LLVMGetGlobalContext());
318 }
319 LLVMTypeRef LLVMPPCFP128Type(void) {
320   return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
321 }
322 LLVMTypeRef LLVMX86MMXType(void) {
323   return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
324 }
325 
326 /*--.. Operations on function types ........................................--*/
327 
328 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
329                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
330                              LLVMBool IsVarArg) {
331   ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
332   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
333 }
334 
335 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
336   return unwrap<FunctionType>(FunctionTy)->isVarArg();
337 }
338 
339 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
340   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
341 }
342 
343 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
344   return unwrap<FunctionType>(FunctionTy)->getNumParams();
345 }
346 
347 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
348   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
349   for (FunctionType::param_iterator I = Ty->param_begin(),
350                                     E = Ty->param_end(); I != E; ++I)
351     *Dest++ = wrap(*I);
352 }
353 
354 /*--.. Operations on struct types ..........................................--*/
355 
356 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
357                            unsigned ElementCount, LLVMBool Packed) {
358   ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
359   return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
360 }
361 
362 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
363                            unsigned ElementCount, LLVMBool Packed) {
364   return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
365                                  ElementCount, Packed);
366 }
367 
368 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
369 {
370   return wrap(StructType::create(*unwrap(C), Name));
371 }
372 
373 const char *LLVMGetStructName(LLVMTypeRef Ty)
374 {
375   StructType *Type = unwrap<StructType>(Ty);
376   if (!Type->hasName())
377     return nullptr;
378   return Type->getName().data();
379 }
380 
381 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
382                        unsigned ElementCount, LLVMBool Packed) {
383   ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
384   unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
385 }
386 
387 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
388   return unwrap<StructType>(StructTy)->getNumElements();
389 }
390 
391 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
392   StructType *Ty = unwrap<StructType>(StructTy);
393   for (StructType::element_iterator I = Ty->element_begin(),
394                                     E = Ty->element_end(); I != E; ++I)
395     *Dest++ = wrap(*I);
396 }
397 
398 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
399   return unwrap<StructType>(StructTy)->isPacked();
400 }
401 
402 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
403   return unwrap<StructType>(StructTy)->isOpaque();
404 }
405 
406 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
407   return wrap(unwrap(M)->getTypeByName(Name));
408 }
409 
410 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
411 
412 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
413   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
414 }
415 
416 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
417   return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
418 }
419 
420 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
421   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
422 }
423 
424 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
425   return wrap(unwrap<SequentialType>(Ty)->getElementType());
426 }
427 
428 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
429   return unwrap<ArrayType>(ArrayTy)->getNumElements();
430 }
431 
432 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
433   return unwrap<PointerType>(PointerTy)->getAddressSpace();
434 }
435 
436 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
437   return unwrap<VectorType>(VectorTy)->getNumElements();
438 }
439 
440 /*--.. Operations on other types ...........................................--*/
441 
442 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C)  {
443   return wrap(Type::getVoidTy(*unwrap(C)));
444 }
445 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
446   return wrap(Type::getLabelTy(*unwrap(C)));
447 }
448 
449 LLVMTypeRef LLVMVoidType(void)  {
450   return LLVMVoidTypeInContext(LLVMGetGlobalContext());
451 }
452 LLVMTypeRef LLVMLabelType(void) {
453   return LLVMLabelTypeInContext(LLVMGetGlobalContext());
454 }
455 
456 /*===-- Operations on values ----------------------------------------------===*/
457 
458 /*--.. Operations on all values ............................................--*/
459 
460 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
461   return wrap(unwrap(Val)->getType());
462 }
463 
464 const char *LLVMGetValueName(LLVMValueRef Val) {
465   return unwrap(Val)->getName().data();
466 }
467 
468 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
469   unwrap(Val)->setName(Name);
470 }
471 
472 void LLVMDumpValue(LLVMValueRef Val) {
473   unwrap(Val)->dump();
474 }
475 
476 char* LLVMPrintValueToString(LLVMValueRef Val) {
477   std::string buf;
478   raw_string_ostream os(buf);
479 
480   unwrap(Val)->print(os);
481   os.flush();
482 
483   return strdup(buf.c_str());
484 }
485 
486 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
487   unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
488 }
489 
490 int LLVMHasMetadata(LLVMValueRef Inst) {
491   return unwrap<Instruction>(Inst)->hasMetadata();
492 }
493 
494 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
495   return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
496 }
497 
498 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
499   unwrap<Instruction>(Inst)->setMetadata(KindID,
500                                          MD ? unwrap<MDNode>(MD) : nullptr);
501 }
502 
503 /*--.. Conversion functions ................................................--*/
504 
505 #define LLVM_DEFINE_VALUE_CAST(name)                                       \
506   LLVMValueRef LLVMIsA##name(LLVMValueRef Val) {                           \
507     return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
508   }
509 
510 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
511 
512 /*--.. Operations on Uses ..................................................--*/
513 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
514   Value *V = unwrap(Val);
515   Value::use_iterator I = V->use_begin();
516   if (I == V->use_end())
517     return nullptr;
518   return wrap(&*I);
519 }
520 
521 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
522   Use *Next = unwrap(U)->getNext();
523   if (Next)
524     return wrap(Next);
525   return nullptr;
526 }
527 
528 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
529   return wrap(unwrap(U)->getUser());
530 }
531 
532 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
533   return wrap(unwrap(U)->get());
534 }
535 
536 /*--.. Operations on Users .................................................--*/
537 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
538   Value *V = unwrap(Val);
539   if (MDNode *MD = dyn_cast<MDNode>(V))
540       return wrap(MD->getOperand(Index));
541   return wrap(cast<User>(V)->getOperand(Index));
542 }
543 
544 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
545   unwrap<User>(Val)->setOperand(Index, unwrap(Op));
546 }
547 
548 int LLVMGetNumOperands(LLVMValueRef Val) {
549   Value *V = unwrap(Val);
550   if (MDNode *MD = dyn_cast<MDNode>(V))
551       return MD->getNumOperands();
552   return cast<User>(V)->getNumOperands();
553 }
554 
555 /*--.. Operations on constants of any type .................................--*/
556 
557 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
558   return wrap(Constant::getNullValue(unwrap(Ty)));
559 }
560 
561 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
562   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
563 }
564 
565 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
566   return wrap(UndefValue::get(unwrap(Ty)));
567 }
568 
569 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
570   return isa<Constant>(unwrap(Ty));
571 }
572 
573 LLVMBool LLVMIsNull(LLVMValueRef Val) {
574   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
575     return C->isNullValue();
576   return false;
577 }
578 
579 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
580   return isa<UndefValue>(unwrap(Val));
581 }
582 
583 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
584   return
585       wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
586 }
587 
588 /*--.. Operations on metadata nodes ........................................--*/
589 
590 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
591                                    unsigned SLen) {
592   return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
593 }
594 
595 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
596   return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
597 }
598 
599 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
600                                  unsigned Count) {
601   return wrap(MDNode::get(*unwrap(C),
602                           makeArrayRef(unwrap<Value>(Vals, Count), Count)));
603 }
604 
605 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
606   return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
607 }
608 
609 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
610   if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
611     *Len = S->getString().size();
612     return S->getString().data();
613   }
614   *Len = 0;
615   return nullptr;
616 }
617 
618 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
619 {
620   return cast<MDNode>(unwrap(V))->getNumOperands();
621 }
622 
623 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
624 {
625   const MDNode *N = cast<MDNode>(unwrap(V));
626   const unsigned numOperands = N->getNumOperands();
627   for (unsigned i = 0; i < numOperands; i++)
628     Dest[i] = wrap(N->getOperand(i));
629 }
630 
631 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
632 {
633   if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
634     return N->getNumOperands();
635   }
636   return 0;
637 }
638 
639 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
640 {
641   NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
642   if (!N)
643     return;
644   for (unsigned i=0;i<N->getNumOperands();i++)
645     Dest[i] = wrap(N->getOperand(i));
646 }
647 
648 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
649                                  LLVMValueRef Val)
650 {
651   NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
652   if (!N)
653     return;
654   MDNode *Op = Val ? unwrap<MDNode>(Val) : nullptr;
655   if (Op)
656     N->addOperand(Op);
657 }
658 
659 /*--.. Operations on scalar constants ......................................--*/
660 
661 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
662                           LLVMBool SignExtend) {
663   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
664 }
665 
666 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
667                                               unsigned NumWords,
668                                               const uint64_t Words[]) {
669     IntegerType *Ty = unwrap<IntegerType>(IntTy);
670     return wrap(ConstantInt::get(Ty->getContext(),
671                                  APInt(Ty->getBitWidth(),
672                                        makeArrayRef(Words, NumWords))));
673 }
674 
675 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
676                                   uint8_t Radix) {
677   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
678                                Radix));
679 }
680 
681 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
682                                          unsigned SLen, uint8_t Radix) {
683   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
684                                Radix));
685 }
686 
687 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
688   return wrap(ConstantFP::get(unwrap(RealTy), N));
689 }
690 
691 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
692   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
693 }
694 
695 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
696                                           unsigned SLen) {
697   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
698 }
699 
700 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
701   return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
702 }
703 
704 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
705   return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
706 }
707 
708 /*--.. Operations on composite constants ...................................--*/
709 
710 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
711                                       unsigned Length,
712                                       LLVMBool DontNullTerminate) {
713   /* Inverted the sense of AddNull because ', 0)' is a
714      better mnemonic for null termination than ', 1)'. */
715   return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
716                                            DontNullTerminate == 0));
717 }
718 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
719                                       LLVMValueRef *ConstantVals,
720                                       unsigned Count, LLVMBool Packed) {
721   Constant **Elements = unwrap<Constant>(ConstantVals, Count);
722   return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
723                                       Packed != 0));
724 }
725 
726 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
727                              LLVMBool DontNullTerminate) {
728   return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
729                                   DontNullTerminate);
730 }
731 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
732                             LLVMValueRef *ConstantVals, unsigned Length) {
733   ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
734   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
735 }
736 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
737                              LLVMBool Packed) {
738   return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
739                                   Packed);
740 }
741 
742 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
743                                   LLVMValueRef *ConstantVals,
744                                   unsigned Count) {
745   Constant **Elements = unwrap<Constant>(ConstantVals, Count);
746   StructType *Ty = cast<StructType>(unwrap(StructTy));
747 
748   return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
749 }
750 
751 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
752   return wrap(ConstantVector::get(makeArrayRef(
753                             unwrap<Constant>(ScalarConstantVals, Size), Size)));
754 }
755 
756 /*-- Opcode mapping */
757 
758 static LLVMOpcode map_to_llvmopcode(int opcode)
759 {
760     switch (opcode) {
761       default: llvm_unreachable("Unhandled Opcode.");
762 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
763 #include "llvm/IR/Instruction.def"
764 #undef HANDLE_INST
765     }
766 }
767 
768 static int map_from_llvmopcode(LLVMOpcode code)
769 {
770     switch (code) {
771 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
772 #include "llvm/IR/Instruction.def"
773 #undef HANDLE_INST
774     }
775     llvm_unreachable("Unhandled Opcode.");
776 }
777 
778 /*--.. Constant expressions ................................................--*/
779 
780 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
781   return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
782 }
783 
784 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
785   return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
786 }
787 
788 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
789   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
790 }
791 
792 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
793   return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
794 }
795 
796 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
797   return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
798 }
799 
800 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
801   return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
802 }
803 
804 
805 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
806   return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
807 }
808 
809 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
810   return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
811 }
812 
813 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
814   return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
815                                    unwrap<Constant>(RHSConstant)));
816 }
817 
818 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
819                              LLVMValueRef RHSConstant) {
820   return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
821                                       unwrap<Constant>(RHSConstant)));
822 }
823 
824 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
825                              LLVMValueRef RHSConstant) {
826   return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
827                                       unwrap<Constant>(RHSConstant)));
828 }
829 
830 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
831   return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
832                                     unwrap<Constant>(RHSConstant)));
833 }
834 
835 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
836   return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
837                                    unwrap<Constant>(RHSConstant)));
838 }
839 
840 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
841                              LLVMValueRef RHSConstant) {
842   return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
843                                       unwrap<Constant>(RHSConstant)));
844 }
845 
846 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
847                              LLVMValueRef RHSConstant) {
848   return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
849                                       unwrap<Constant>(RHSConstant)));
850 }
851 
852 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
853   return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
854                                     unwrap<Constant>(RHSConstant)));
855 }
856 
857 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
858   return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
859                                    unwrap<Constant>(RHSConstant)));
860 }
861 
862 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
863                              LLVMValueRef RHSConstant) {
864   return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
865                                       unwrap<Constant>(RHSConstant)));
866 }
867 
868 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
869                              LLVMValueRef RHSConstant) {
870   return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
871                                       unwrap<Constant>(RHSConstant)));
872 }
873 
874 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
875   return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
876                                     unwrap<Constant>(RHSConstant)));
877 }
878 
879 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
880   return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
881                                     unwrap<Constant>(RHSConstant)));
882 }
883 
884 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
885   return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
886                                     unwrap<Constant>(RHSConstant)));
887 }
888 
889 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
890                                 LLVMValueRef RHSConstant) {
891   return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
892                                          unwrap<Constant>(RHSConstant)));
893 }
894 
895 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
896   return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
897                                     unwrap<Constant>(RHSConstant)));
898 }
899 
900 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
901   return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
902                                     unwrap<Constant>(RHSConstant)));
903 }
904 
905 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
906   return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
907                                     unwrap<Constant>(RHSConstant)));
908 }
909 
910 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
911   return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
912                                     unwrap<Constant>(RHSConstant)));
913 }
914 
915 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
916   return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
917                                    unwrap<Constant>(RHSConstant)));
918 }
919 
920 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
921   return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
922                                   unwrap<Constant>(RHSConstant)));
923 }
924 
925 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
926   return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
927                                    unwrap<Constant>(RHSConstant)));
928 }
929 
930 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
931                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
932   return wrap(ConstantExpr::getICmp(Predicate,
933                                     unwrap<Constant>(LHSConstant),
934                                     unwrap<Constant>(RHSConstant)));
935 }
936 
937 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
938                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
939   return wrap(ConstantExpr::getFCmp(Predicate,
940                                     unwrap<Constant>(LHSConstant),
941                                     unwrap<Constant>(RHSConstant)));
942 }
943 
944 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
945   return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
946                                    unwrap<Constant>(RHSConstant)));
947 }
948 
949 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
950   return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
951                                     unwrap<Constant>(RHSConstant)));
952 }
953 
954 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
955   return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
956                                     unwrap<Constant>(RHSConstant)));
957 }
958 
959 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
960                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
961   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
962                                NumIndices);
963   return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
964                                              IdxList));
965 }
966 
967 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
968                                   LLVMValueRef *ConstantIndices,
969                                   unsigned NumIndices) {
970   Constant* Val = unwrap<Constant>(ConstantVal);
971   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
972                                NumIndices);
973   return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
974 }
975 
976 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
977   return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
978                                      unwrap(ToType)));
979 }
980 
981 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
982   return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
983                                     unwrap(ToType)));
984 }
985 
986 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
987   return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
988                                     unwrap(ToType)));
989 }
990 
991 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
992   return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
993                                        unwrap(ToType)));
994 }
995 
996 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
997   return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
998                                         unwrap(ToType)));
999 }
1000 
1001 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1002   return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1003                                       unwrap(ToType)));
1004 }
1005 
1006 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1007   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1008                                       unwrap(ToType)));
1009 }
1010 
1011 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1012   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1013                                       unwrap(ToType)));
1014 }
1015 
1016 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1017   return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1018                                       unwrap(ToType)));
1019 }
1020 
1021 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1022   return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1023                                         unwrap(ToType)));
1024 }
1025 
1026 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1027   return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1028                                         unwrap(ToType)));
1029 }
1030 
1031 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1032   return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1033                                        unwrap(ToType)));
1034 }
1035 
1036 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1037                                     LLVMTypeRef ToType) {
1038   return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1039                                              unwrap(ToType)));
1040 }
1041 
1042 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1043                                     LLVMTypeRef ToType) {
1044   return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1045                                              unwrap(ToType)));
1046 }
1047 
1048 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1049                                     LLVMTypeRef ToType) {
1050   return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1051                                              unwrap(ToType)));
1052 }
1053 
1054 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1055                                      LLVMTypeRef ToType) {
1056   return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1057                                               unwrap(ToType)));
1058 }
1059 
1060 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1061                                   LLVMTypeRef ToType) {
1062   return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1063                                            unwrap(ToType)));
1064 }
1065 
1066 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1067                               LLVMBool isSigned) {
1068   return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1069                                            unwrap(ToType), isSigned));
1070 }
1071 
1072 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1073   return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1074                                       unwrap(ToType)));
1075 }
1076 
1077 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1078                              LLVMValueRef ConstantIfTrue,
1079                              LLVMValueRef ConstantIfFalse) {
1080   return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1081                                       unwrap<Constant>(ConstantIfTrue),
1082                                       unwrap<Constant>(ConstantIfFalse)));
1083 }
1084 
1085 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1086                                      LLVMValueRef IndexConstant) {
1087   return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1088                                               unwrap<Constant>(IndexConstant)));
1089 }
1090 
1091 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1092                                     LLVMValueRef ElementValueConstant,
1093                                     LLVMValueRef IndexConstant) {
1094   return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1095                                          unwrap<Constant>(ElementValueConstant),
1096                                              unwrap<Constant>(IndexConstant)));
1097 }
1098 
1099 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1100                                     LLVMValueRef VectorBConstant,
1101                                     LLVMValueRef MaskConstant) {
1102   return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1103                                              unwrap<Constant>(VectorBConstant),
1104                                              unwrap<Constant>(MaskConstant)));
1105 }
1106 
1107 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1108                                    unsigned NumIdx) {
1109   return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1110                                             makeArrayRef(IdxList, NumIdx)));
1111 }
1112 
1113 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1114                                   LLVMValueRef ElementValueConstant,
1115                                   unsigned *IdxList, unsigned NumIdx) {
1116   return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1117                                          unwrap<Constant>(ElementValueConstant),
1118                                            makeArrayRef(IdxList, NumIdx)));
1119 }
1120 
1121 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1122                                 const char *Constraints,
1123                                 LLVMBool HasSideEffects,
1124                                 LLVMBool IsAlignStack) {
1125   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1126                              Constraints, HasSideEffects, IsAlignStack));
1127 }
1128 
1129 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1130   return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1131 }
1132 
1133 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1134 
1135 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1136   return wrap(unwrap<GlobalValue>(Global)->getParent());
1137 }
1138 
1139 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1140   return unwrap<GlobalValue>(Global)->isDeclaration();
1141 }
1142 
1143 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1144   switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1145   case GlobalValue::ExternalLinkage:
1146     return LLVMExternalLinkage;
1147   case GlobalValue::AvailableExternallyLinkage:
1148     return LLVMAvailableExternallyLinkage;
1149   case GlobalValue::LinkOnceAnyLinkage:
1150     return LLVMLinkOnceAnyLinkage;
1151   case GlobalValue::LinkOnceODRLinkage:
1152     return LLVMLinkOnceODRLinkage;
1153   case GlobalValue::WeakAnyLinkage:
1154     return LLVMWeakAnyLinkage;
1155   case GlobalValue::WeakODRLinkage:
1156     return LLVMWeakODRLinkage;
1157   case GlobalValue::AppendingLinkage:
1158     return LLVMAppendingLinkage;
1159   case GlobalValue::InternalLinkage:
1160     return LLVMInternalLinkage;
1161   case GlobalValue::PrivateLinkage:
1162     return LLVMPrivateLinkage;
1163   case GlobalValue::ExternalWeakLinkage:
1164     return LLVMExternalWeakLinkage;
1165   case GlobalValue::CommonLinkage:
1166     return LLVMCommonLinkage;
1167   }
1168 
1169   llvm_unreachable("Invalid GlobalValue linkage!");
1170 }
1171 
1172 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1173   GlobalValue *GV = unwrap<GlobalValue>(Global);
1174 
1175   switch (Linkage) {
1176   case LLVMExternalLinkage:
1177     GV->setLinkage(GlobalValue::ExternalLinkage);
1178     break;
1179   case LLVMAvailableExternallyLinkage:
1180     GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1181     break;
1182   case LLVMLinkOnceAnyLinkage:
1183     GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1184     break;
1185   case LLVMLinkOnceODRLinkage:
1186     GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1187     break;
1188   case LLVMLinkOnceODRAutoHideLinkage:
1189     DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1190                     "longer supported.");
1191     break;
1192   case LLVMWeakAnyLinkage:
1193     GV->setLinkage(GlobalValue::WeakAnyLinkage);
1194     break;
1195   case LLVMWeakODRLinkage:
1196     GV->setLinkage(GlobalValue::WeakODRLinkage);
1197     break;
1198   case LLVMAppendingLinkage:
1199     GV->setLinkage(GlobalValue::AppendingLinkage);
1200     break;
1201   case LLVMInternalLinkage:
1202     GV->setLinkage(GlobalValue::InternalLinkage);
1203     break;
1204   case LLVMPrivateLinkage:
1205     GV->setLinkage(GlobalValue::PrivateLinkage);
1206     break;
1207   case LLVMLinkerPrivateLinkage:
1208     GV->setLinkage(GlobalValue::PrivateLinkage);
1209     break;
1210   case LLVMLinkerPrivateWeakLinkage:
1211     GV->setLinkage(GlobalValue::PrivateLinkage);
1212     break;
1213   case LLVMDLLImportLinkage:
1214     DEBUG(errs()
1215           << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1216     break;
1217   case LLVMDLLExportLinkage:
1218     DEBUG(errs()
1219           << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1220     break;
1221   case LLVMExternalWeakLinkage:
1222     GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1223     break;
1224   case LLVMGhostLinkage:
1225     DEBUG(errs()
1226           << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1227     break;
1228   case LLVMCommonLinkage:
1229     GV->setLinkage(GlobalValue::CommonLinkage);
1230     break;
1231   }
1232 }
1233 
1234 const char *LLVMGetSection(LLVMValueRef Global) {
1235   return unwrap<GlobalValue>(Global)->getSection().c_str();
1236 }
1237 
1238 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1239   unwrap<GlobalValue>(Global)->setSection(Section);
1240 }
1241 
1242 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1243   return static_cast<LLVMVisibility>(
1244     unwrap<GlobalValue>(Global)->getVisibility());
1245 }
1246 
1247 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1248   unwrap<GlobalValue>(Global)
1249     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1250 }
1251 
1252 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1253   return static_cast<LLVMDLLStorageClass>(
1254       unwrap<GlobalValue>(Global)->getDLLStorageClass());
1255 }
1256 
1257 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1258   unwrap<GlobalValue>(Global)->setDLLStorageClass(
1259       static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1260 }
1261 
1262 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1263   return unwrap<GlobalValue>(Global)->hasUnnamedAddr();
1264 }
1265 
1266 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1267   unwrap<GlobalValue>(Global)->setUnnamedAddr(HasUnnamedAddr);
1268 }
1269 
1270 /*--.. Operations on global variables, load and store instructions .........--*/
1271 
1272 unsigned LLVMGetAlignment(LLVMValueRef V) {
1273   Value *P = unwrap<Value>(V);
1274   if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1275     return GV->getAlignment();
1276   if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1277     return AI->getAlignment();
1278   if (LoadInst *LI = dyn_cast<LoadInst>(P))
1279     return LI->getAlignment();
1280   if (StoreInst *SI = dyn_cast<StoreInst>(P))
1281     return SI->getAlignment();
1282 
1283   llvm_unreachable(
1284       "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1285 }
1286 
1287 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1288   Value *P = unwrap<Value>(V);
1289   if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1290     GV->setAlignment(Bytes);
1291   else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1292     AI->setAlignment(Bytes);
1293   else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1294     LI->setAlignment(Bytes);
1295   else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1296     SI->setAlignment(Bytes);
1297   else
1298     llvm_unreachable(
1299         "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1300 }
1301 
1302 /*--.. Operations on global variables ......................................--*/
1303 
1304 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1305   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1306                                  GlobalValue::ExternalLinkage, nullptr, Name));
1307 }
1308 
1309 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1310                                          const char *Name,
1311                                          unsigned AddressSpace) {
1312   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1313                                  GlobalValue::ExternalLinkage, nullptr, Name,
1314                                  nullptr, GlobalVariable::NotThreadLocal,
1315                                  AddressSpace));
1316 }
1317 
1318 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1319   return wrap(unwrap(M)->getNamedGlobal(Name));
1320 }
1321 
1322 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1323   Module *Mod = unwrap(M);
1324   Module::global_iterator I = Mod->global_begin();
1325   if (I == Mod->global_end())
1326     return nullptr;
1327   return wrap(I);
1328 }
1329 
1330 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1331   Module *Mod = unwrap(M);
1332   Module::global_iterator I = Mod->global_end();
1333   if (I == Mod->global_begin())
1334     return nullptr;
1335   return wrap(--I);
1336 }
1337 
1338 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1339   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1340   Module::global_iterator I = GV;
1341   if (++I == GV->getParent()->global_end())
1342     return nullptr;
1343   return wrap(I);
1344 }
1345 
1346 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1347   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1348   Module::global_iterator I = GV;
1349   if (I == GV->getParent()->global_begin())
1350     return nullptr;
1351   return wrap(--I);
1352 }
1353 
1354 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1355   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1356 }
1357 
1358 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1359   GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1360   if ( !GV->hasInitializer() )
1361     return nullptr;
1362   return wrap(GV->getInitializer());
1363 }
1364 
1365 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1366   unwrap<GlobalVariable>(GlobalVar)
1367     ->setInitializer(unwrap<Constant>(ConstantVal));
1368 }
1369 
1370 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1371   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1372 }
1373 
1374 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1375   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1376 }
1377 
1378 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1379   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1380 }
1381 
1382 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1383   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1384 }
1385 
1386 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1387   switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1388   case GlobalVariable::NotThreadLocal:
1389     return LLVMNotThreadLocal;
1390   case GlobalVariable::GeneralDynamicTLSModel:
1391     return LLVMGeneralDynamicTLSModel;
1392   case GlobalVariable::LocalDynamicTLSModel:
1393     return LLVMLocalDynamicTLSModel;
1394   case GlobalVariable::InitialExecTLSModel:
1395     return LLVMInitialExecTLSModel;
1396   case GlobalVariable::LocalExecTLSModel:
1397     return LLVMLocalExecTLSModel;
1398   }
1399 
1400   llvm_unreachable("Invalid GlobalVariable thread local mode");
1401 }
1402 
1403 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1404   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1405 
1406   switch (Mode) {
1407   case LLVMNotThreadLocal:
1408     GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1409     break;
1410   case LLVMGeneralDynamicTLSModel:
1411     GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1412     break;
1413   case LLVMLocalDynamicTLSModel:
1414     GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1415     break;
1416   case LLVMInitialExecTLSModel:
1417     GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1418     break;
1419   case LLVMLocalExecTLSModel:
1420     GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1421     break;
1422   }
1423 }
1424 
1425 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1426   return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1427 }
1428 
1429 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1430   unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1431 }
1432 
1433 /*--.. Operations on aliases ......................................--*/
1434 
1435 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1436                           const char *Name) {
1437   return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1438                               unwrap<Constant>(Aliasee), unwrap (M)));
1439 }
1440 
1441 /*--.. Operations on functions .............................................--*/
1442 
1443 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1444                              LLVMTypeRef FunctionTy) {
1445   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1446                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
1447 }
1448 
1449 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1450   return wrap(unwrap(M)->getFunction(Name));
1451 }
1452 
1453 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1454   Module *Mod = unwrap(M);
1455   Module::iterator I = Mod->begin();
1456   if (I == Mod->end())
1457     return nullptr;
1458   return wrap(I);
1459 }
1460 
1461 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1462   Module *Mod = unwrap(M);
1463   Module::iterator I = Mod->end();
1464   if (I == Mod->begin())
1465     return nullptr;
1466   return wrap(--I);
1467 }
1468 
1469 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1470   Function *Func = unwrap<Function>(Fn);
1471   Module::iterator I = Func;
1472   if (++I == Func->getParent()->end())
1473     return nullptr;
1474   return wrap(I);
1475 }
1476 
1477 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1478   Function *Func = unwrap<Function>(Fn);
1479   Module::iterator I = Func;
1480   if (I == Func->getParent()->begin())
1481     return nullptr;
1482   return wrap(--I);
1483 }
1484 
1485 void LLVMDeleteFunction(LLVMValueRef Fn) {
1486   unwrap<Function>(Fn)->eraseFromParent();
1487 }
1488 
1489 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1490   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1491     return F->getIntrinsicID();
1492   return 0;
1493 }
1494 
1495 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1496   return unwrap<Function>(Fn)->getCallingConv();
1497 }
1498 
1499 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1500   return unwrap<Function>(Fn)->setCallingConv(
1501     static_cast<CallingConv::ID>(CC));
1502 }
1503 
1504 const char *LLVMGetGC(LLVMValueRef Fn) {
1505   Function *F = unwrap<Function>(Fn);
1506   return F->hasGC()? F->getGC() : nullptr;
1507 }
1508 
1509 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1510   Function *F = unwrap<Function>(Fn);
1511   if (GC)
1512     F->setGC(GC);
1513   else
1514     F->clearGC();
1515 }
1516 
1517 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1518   Function *Func = unwrap<Function>(Fn);
1519   const AttributeSet PAL = Func->getAttributes();
1520   AttrBuilder B(PA);
1521   const AttributeSet PALnew =
1522     PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1523                       AttributeSet::get(Func->getContext(),
1524                                         AttributeSet::FunctionIndex, B));
1525   Func->setAttributes(PALnew);
1526 }
1527 
1528 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1529                                         const char *V) {
1530   Function *Func = unwrap<Function>(Fn);
1531   AttributeSet::AttrIndex Idx =
1532     AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1533   AttrBuilder B;
1534 
1535   B.addAttribute(A, V);
1536   AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1537   Func->addAttributes(Idx, Set);
1538 }
1539 
1540 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1541   Function *Func = unwrap<Function>(Fn);
1542   const AttributeSet PAL = Func->getAttributes();
1543   AttrBuilder B(PA);
1544   const AttributeSet PALnew =
1545     PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1546                          AttributeSet::get(Func->getContext(),
1547                                            AttributeSet::FunctionIndex, B));
1548   Func->setAttributes(PALnew);
1549 }
1550 
1551 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1552   Function *Func = unwrap<Function>(Fn);
1553   const AttributeSet PAL = Func->getAttributes();
1554   return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1555 }
1556 
1557 /*--.. Operations on parameters ............................................--*/
1558 
1559 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1560   // This function is strictly redundant to
1561   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1562   return unwrap<Function>(FnRef)->arg_size();
1563 }
1564 
1565 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1566   Function *Fn = unwrap<Function>(FnRef);
1567   for (Function::arg_iterator I = Fn->arg_begin(),
1568                               E = Fn->arg_end(); I != E; I++)
1569     *ParamRefs++ = wrap(I);
1570 }
1571 
1572 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1573   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1574   while (index --> 0)
1575     AI++;
1576   return wrap(AI);
1577 }
1578 
1579 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1580   return wrap(unwrap<Argument>(V)->getParent());
1581 }
1582 
1583 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1584   Function *Func = unwrap<Function>(Fn);
1585   Function::arg_iterator I = Func->arg_begin();
1586   if (I == Func->arg_end())
1587     return nullptr;
1588   return wrap(I);
1589 }
1590 
1591 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1592   Function *Func = unwrap<Function>(Fn);
1593   Function::arg_iterator I = Func->arg_end();
1594   if (I == Func->arg_begin())
1595     return nullptr;
1596   return wrap(--I);
1597 }
1598 
1599 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1600   Argument *A = unwrap<Argument>(Arg);
1601   Function::arg_iterator I = A;
1602   if (++I == A->getParent()->arg_end())
1603     return nullptr;
1604   return wrap(I);
1605 }
1606 
1607 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1608   Argument *A = unwrap<Argument>(Arg);
1609   Function::arg_iterator I = A;
1610   if (I == A->getParent()->arg_begin())
1611     return nullptr;
1612   return wrap(--I);
1613 }
1614 
1615 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1616   Argument *A = unwrap<Argument>(Arg);
1617   AttrBuilder B(PA);
1618   A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1,  B));
1619 }
1620 
1621 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1622   Argument *A = unwrap<Argument>(Arg);
1623   AttrBuilder B(PA);
1624   A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1,  B));
1625 }
1626 
1627 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1628   Argument *A = unwrap<Argument>(Arg);
1629   return (LLVMAttribute)A->getParent()->getAttributes().
1630     Raw(A->getArgNo()+1);
1631 }
1632 
1633 
1634 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1635   Argument *A = unwrap<Argument>(Arg);
1636   AttrBuilder B;
1637   B.addAlignmentAttr(align);
1638   A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1639 }
1640 
1641 /*--.. Operations on basic blocks ..........................................--*/
1642 
1643 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1644   return wrap(static_cast<Value*>(unwrap(BB)));
1645 }
1646 
1647 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1648   return isa<BasicBlock>(unwrap(Val));
1649 }
1650 
1651 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1652   return wrap(unwrap<BasicBlock>(Val));
1653 }
1654 
1655 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1656   return wrap(unwrap(BB)->getParent());
1657 }
1658 
1659 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1660   return wrap(unwrap(BB)->getTerminator());
1661 }
1662 
1663 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1664   return unwrap<Function>(FnRef)->size();
1665 }
1666 
1667 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1668   Function *Fn = unwrap<Function>(FnRef);
1669   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1670     *BasicBlocksRefs++ = wrap(I);
1671 }
1672 
1673 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1674   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1675 }
1676 
1677 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1678   Function *Func = unwrap<Function>(Fn);
1679   Function::iterator I = Func->begin();
1680   if (I == Func->end())
1681     return nullptr;
1682   return wrap(I);
1683 }
1684 
1685 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1686   Function *Func = unwrap<Function>(Fn);
1687   Function::iterator I = Func->end();
1688   if (I == Func->begin())
1689     return nullptr;
1690   return wrap(--I);
1691 }
1692 
1693 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1694   BasicBlock *Block = unwrap(BB);
1695   Function::iterator I = Block;
1696   if (++I == Block->getParent()->end())
1697     return nullptr;
1698   return wrap(I);
1699 }
1700 
1701 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1702   BasicBlock *Block = unwrap(BB);
1703   Function::iterator I = Block;
1704   if (I == Block->getParent()->begin())
1705     return nullptr;
1706   return wrap(--I);
1707 }
1708 
1709 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1710                                                 LLVMValueRef FnRef,
1711                                                 const char *Name) {
1712   return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1713 }
1714 
1715 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1716   return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1717 }
1718 
1719 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1720                                                 LLVMBasicBlockRef BBRef,
1721                                                 const char *Name) {
1722   BasicBlock *BB = unwrap(BBRef);
1723   return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1724 }
1725 
1726 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1727                                        const char *Name) {
1728   return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1729 }
1730 
1731 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1732   unwrap(BBRef)->eraseFromParent();
1733 }
1734 
1735 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1736   unwrap(BBRef)->removeFromParent();
1737 }
1738 
1739 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1740   unwrap(BB)->moveBefore(unwrap(MovePos));
1741 }
1742 
1743 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1744   unwrap(BB)->moveAfter(unwrap(MovePos));
1745 }
1746 
1747 /*--.. Operations on instructions ..........................................--*/
1748 
1749 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1750   return wrap(unwrap<Instruction>(Inst)->getParent());
1751 }
1752 
1753 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1754   BasicBlock *Block = unwrap(BB);
1755   BasicBlock::iterator I = Block->begin();
1756   if (I == Block->end())
1757     return nullptr;
1758   return wrap(I);
1759 }
1760 
1761 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1762   BasicBlock *Block = unwrap(BB);
1763   BasicBlock::iterator I = Block->end();
1764   if (I == Block->begin())
1765     return nullptr;
1766   return wrap(--I);
1767 }
1768 
1769 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1770   Instruction *Instr = unwrap<Instruction>(Inst);
1771   BasicBlock::iterator I = Instr;
1772   if (++I == Instr->getParent()->end())
1773     return nullptr;
1774   return wrap(I);
1775 }
1776 
1777 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1778   Instruction *Instr = unwrap<Instruction>(Inst);
1779   BasicBlock::iterator I = Instr;
1780   if (I == Instr->getParent()->begin())
1781     return nullptr;
1782   return wrap(--I);
1783 }
1784 
1785 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1786   unwrap<Instruction>(Inst)->eraseFromParent();
1787 }
1788 
1789 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1790   if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1791     return (LLVMIntPredicate)I->getPredicate();
1792   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1793     if (CE->getOpcode() == Instruction::ICmp)
1794       return (LLVMIntPredicate)CE->getPredicate();
1795   return (LLVMIntPredicate)0;
1796 }
1797 
1798 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1799   if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1800     return map_to_llvmopcode(C->getOpcode());
1801   return (LLVMOpcode)0;
1802 }
1803 
1804 /*--.. Call and invoke instructions ........................................--*/
1805 
1806 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1807   Value *V = unwrap(Instr);
1808   if (CallInst *CI = dyn_cast<CallInst>(V))
1809     return CI->getCallingConv();
1810   if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1811     return II->getCallingConv();
1812   llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1813 }
1814 
1815 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1816   Value *V = unwrap(Instr);
1817   if (CallInst *CI = dyn_cast<CallInst>(V))
1818     return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1819   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1820     return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1821   llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1822 }
1823 
1824 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1825                            LLVMAttribute PA) {
1826   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1827   AttrBuilder B(PA);
1828   Call.setAttributes(
1829     Call.getAttributes().addAttributes(Call->getContext(), index,
1830                                        AttributeSet::get(Call->getContext(),
1831                                                          index, B)));
1832 }
1833 
1834 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1835                               LLVMAttribute PA) {
1836   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1837   AttrBuilder B(PA);
1838   Call.setAttributes(Call.getAttributes()
1839                        .removeAttributes(Call->getContext(), index,
1840                                          AttributeSet::get(Call->getContext(),
1841                                                            index, B)));
1842 }
1843 
1844 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1845                                 unsigned align) {
1846   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1847   AttrBuilder B;
1848   B.addAlignmentAttr(align);
1849   Call.setAttributes(Call.getAttributes()
1850                        .addAttributes(Call->getContext(), index,
1851                                       AttributeSet::get(Call->getContext(),
1852                                                         index, B)));
1853 }
1854 
1855 /*--.. Operations on call instructions (only) ..............................--*/
1856 
1857 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1858   return unwrap<CallInst>(Call)->isTailCall();
1859 }
1860 
1861 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1862   unwrap<CallInst>(Call)->setTailCall(isTailCall);
1863 }
1864 
1865 /*--.. Operations on switch instructions (only) ............................--*/
1866 
1867 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1868   return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1869 }
1870 
1871 /*--.. Operations on phi nodes .............................................--*/
1872 
1873 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1874                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1875   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1876   for (unsigned I = 0; I != Count; ++I)
1877     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1878 }
1879 
1880 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1881   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1882 }
1883 
1884 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1885   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1886 }
1887 
1888 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1889   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1890 }
1891 
1892 
1893 /*===-- Instruction builders ----------------------------------------------===*/
1894 
1895 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1896   return wrap(new IRBuilder<>(*unwrap(C)));
1897 }
1898 
1899 LLVMBuilderRef LLVMCreateBuilder(void) {
1900   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1901 }
1902 
1903 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1904                          LLVMValueRef Instr) {
1905   BasicBlock *BB = unwrap(Block);
1906   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1907   unwrap(Builder)->SetInsertPoint(BB, I);
1908 }
1909 
1910 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1911   Instruction *I = unwrap<Instruction>(Instr);
1912   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1913 }
1914 
1915 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1916   BasicBlock *BB = unwrap(Block);
1917   unwrap(Builder)->SetInsertPoint(BB);
1918 }
1919 
1920 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1921    return wrap(unwrap(Builder)->GetInsertBlock());
1922 }
1923 
1924 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1925   unwrap(Builder)->ClearInsertionPoint();
1926 }
1927 
1928 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1929   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1930 }
1931 
1932 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1933                                    const char *Name) {
1934   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1935 }
1936 
1937 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1938   delete unwrap(Builder);
1939 }
1940 
1941 /*--.. Metadata builders ...................................................--*/
1942 
1943 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1944   MDNode *Loc = L ? unwrap<MDNode>(L) : nullptr;
1945   unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1946 }
1947 
1948 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1949   return wrap(unwrap(Builder)->getCurrentDebugLocation()
1950               .getAsMDNode(unwrap(Builder)->getContext()));
1951 }
1952 
1953 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1954   unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1955 }
1956 
1957 
1958 /*--.. Instruction builders ................................................--*/
1959 
1960 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1961   return wrap(unwrap(B)->CreateRetVoid());
1962 }
1963 
1964 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1965   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1966 }
1967 
1968 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1969                                    unsigned N) {
1970   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1971 }
1972 
1973 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1974   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1975 }
1976 
1977 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1978                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1979   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1980 }
1981 
1982 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1983                              LLVMBasicBlockRef Else, unsigned NumCases) {
1984   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1985 }
1986 
1987 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1988                                  unsigned NumDests) {
1989   return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1990 }
1991 
1992 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1993                              LLVMValueRef *Args, unsigned NumArgs,
1994                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1995                              const char *Name) {
1996   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1997                                       makeArrayRef(unwrap(Args), NumArgs),
1998                                       Name));
1999 }
2000 
2001 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2002                                  LLVMValueRef PersFn, unsigned NumClauses,
2003                                  const char *Name) {
2004   return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
2005                                           cast<Function>(unwrap(PersFn)),
2006                                           NumClauses, Name));
2007 }
2008 
2009 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2010   return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2011 }
2012 
2013 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2014   return wrap(unwrap(B)->CreateUnreachable());
2015 }
2016 
2017 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2018                  LLVMBasicBlockRef Dest) {
2019   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2020 }
2021 
2022 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2023   unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2024 }
2025 
2026 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2027   unwrap<LandingPadInst>(LandingPad)->
2028     addClause(cast<Constant>(unwrap(ClauseVal)));
2029 }
2030 
2031 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2032   unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2033 }
2034 
2035 /*--.. Arithmetic ..........................................................--*/
2036 
2037 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2038                           const char *Name) {
2039   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2040 }
2041 
2042 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2043                           const char *Name) {
2044   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2045 }
2046 
2047 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2048                           const char *Name) {
2049   return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2050 }
2051 
2052 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2053                           const char *Name) {
2054   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2055 }
2056 
2057 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2058                           const char *Name) {
2059   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2060 }
2061 
2062 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2063                           const char *Name) {
2064   return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2065 }
2066 
2067 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2068                           const char *Name) {
2069   return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2070 }
2071 
2072 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2073                           const char *Name) {
2074   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2075 }
2076 
2077 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2078                           const char *Name) {
2079   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2080 }
2081 
2082 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2083                           const char *Name) {
2084   return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2085 }
2086 
2087 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2088                           const char *Name) {
2089   return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2090 }
2091 
2092 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2093                           const char *Name) {
2094   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2095 }
2096 
2097 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2098                            const char *Name) {
2099   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2100 }
2101 
2102 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2103                            const char *Name) {
2104   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2105 }
2106 
2107 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2108                                 LLVMValueRef RHS, const char *Name) {
2109   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2110 }
2111 
2112 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2113                            const char *Name) {
2114   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2115 }
2116 
2117 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2118                            const char *Name) {
2119   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2120 }
2121 
2122 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2123                            const char *Name) {
2124   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2125 }
2126 
2127 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2128                            const char *Name) {
2129   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2130 }
2131 
2132 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2133                           const char *Name) {
2134   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2135 }
2136 
2137 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2138                            const char *Name) {
2139   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2140 }
2141 
2142 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2143                            const char *Name) {
2144   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2145 }
2146 
2147 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2148                           const char *Name) {
2149   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2150 }
2151 
2152 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2153                          const char *Name) {
2154   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2155 }
2156 
2157 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2158                           const char *Name) {
2159   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2160 }
2161 
2162 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2163                             LLVMValueRef LHS, LLVMValueRef RHS,
2164                             const char *Name) {
2165   return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2166                                      unwrap(RHS), Name));
2167 }
2168 
2169 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2170   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2171 }
2172 
2173 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2174                              const char *Name) {
2175   return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2176 }
2177 
2178 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2179                              const char *Name) {
2180   return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2181 }
2182 
2183 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2184   return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2185 }
2186 
2187 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2188   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2189 }
2190 
2191 /*--.. Memory ..............................................................--*/
2192 
2193 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2194                              const char *Name) {
2195   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2196   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2197   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2198   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2199                                                ITy, unwrap(Ty), AllocSize,
2200                                                nullptr, nullptr, "");
2201   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2202 }
2203 
2204 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2205                                   LLVMValueRef Val, const char *Name) {
2206   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2207   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2208   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2209   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2210                                                ITy, unwrap(Ty), AllocSize,
2211                                                unwrap(Val), nullptr, "");
2212   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2213 }
2214 
2215 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2216                              const char *Name) {
2217   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
2218 }
2219 
2220 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2221                                   LLVMValueRef Val, const char *Name) {
2222   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2223 }
2224 
2225 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2226   return wrap(unwrap(B)->Insert(
2227      CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2228 }
2229 
2230 
2231 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2232                            const char *Name) {
2233   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2234 }
2235 
2236 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2237                             LLVMValueRef PointerVal) {
2238   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2239 }
2240 
2241 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2242   switch (Ordering) {
2243     case LLVMAtomicOrderingNotAtomic: return NotAtomic;
2244     case LLVMAtomicOrderingUnordered: return Unordered;
2245     case LLVMAtomicOrderingMonotonic: return Monotonic;
2246     case LLVMAtomicOrderingAcquire: return Acquire;
2247     case LLVMAtomicOrderingRelease: return Release;
2248     case LLVMAtomicOrderingAcquireRelease: return AcquireRelease;
2249     case LLVMAtomicOrderingSequentiallyConsistent:
2250       return SequentiallyConsistent;
2251   }
2252 
2253   llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2254 }
2255 
2256 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2257                             LLVMBool isSingleThread, const char *Name) {
2258   return wrap(
2259     unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2260                            isSingleThread ? SingleThread : CrossThread,
2261                            Name));
2262 }
2263 
2264 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2265                           LLVMValueRef *Indices, unsigned NumIndices,
2266                           const char *Name) {
2267   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2268   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2269 }
2270 
2271 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2272                                   LLVMValueRef *Indices, unsigned NumIndices,
2273                                   const char *Name) {
2274   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2275   return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2276 }
2277 
2278 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2279                                 unsigned Idx, const char *Name) {
2280   return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2281 }
2282 
2283 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2284                                    const char *Name) {
2285   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2286 }
2287 
2288 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2289                                       const char *Name) {
2290   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2291 }
2292 
2293 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2294   Value *P = unwrap<Value>(MemAccessInst);
2295   if (LoadInst *LI = dyn_cast<LoadInst>(P))
2296     return LI->isVolatile();
2297   return cast<StoreInst>(P)->isVolatile();
2298 }
2299 
2300 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2301   Value *P = unwrap<Value>(MemAccessInst);
2302   if (LoadInst *LI = dyn_cast<LoadInst>(P))
2303     return LI->setVolatile(isVolatile);
2304   return cast<StoreInst>(P)->setVolatile(isVolatile);
2305 }
2306 
2307 /*--.. Casts ...............................................................--*/
2308 
2309 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2310                             LLVMTypeRef DestTy, const char *Name) {
2311   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2312 }
2313 
2314 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2315                            LLVMTypeRef DestTy, const char *Name) {
2316   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2317 }
2318 
2319 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2320                            LLVMTypeRef DestTy, const char *Name) {
2321   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2322 }
2323 
2324 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2325                              LLVMTypeRef DestTy, const char *Name) {
2326   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2327 }
2328 
2329 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2330                              LLVMTypeRef DestTy, const char *Name) {
2331   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2332 }
2333 
2334 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2335                              LLVMTypeRef DestTy, const char *Name) {
2336   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2337 }
2338 
2339 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2340                              LLVMTypeRef DestTy, const char *Name) {
2341   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2342 }
2343 
2344 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2345                               LLVMTypeRef DestTy, const char *Name) {
2346   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2347 }
2348 
2349 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2350                             LLVMTypeRef DestTy, const char *Name) {
2351   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2352 }
2353 
2354 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2355                                LLVMTypeRef DestTy, const char *Name) {
2356   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2357 }
2358 
2359 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2360                                LLVMTypeRef DestTy, const char *Name) {
2361   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2362 }
2363 
2364 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2365                               LLVMTypeRef DestTy, const char *Name) {
2366   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2367 }
2368 
2369 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2370                                     LLVMTypeRef DestTy, const char *Name) {
2371   return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2372 }
2373 
2374 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2375                                     LLVMTypeRef DestTy, const char *Name) {
2376   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2377                                              Name));
2378 }
2379 
2380 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2381                                     LLVMTypeRef DestTy, const char *Name) {
2382   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2383                                              Name));
2384 }
2385 
2386 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2387                                      LLVMTypeRef DestTy, const char *Name) {
2388   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2389                                               Name));
2390 }
2391 
2392 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2393                            LLVMTypeRef DestTy, const char *Name) {
2394   return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2395                                     unwrap(DestTy), Name));
2396 }
2397 
2398 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2399                                   LLVMTypeRef DestTy, const char *Name) {
2400   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2401 }
2402 
2403 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2404                               LLVMTypeRef DestTy, const char *Name) {
2405   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2406                                        /*isSigned*/true, Name));
2407 }
2408 
2409 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2410                              LLVMTypeRef DestTy, const char *Name) {
2411   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2412 }
2413 
2414 /*--.. Comparisons .........................................................--*/
2415 
2416 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2417                            LLVMValueRef LHS, LLVMValueRef RHS,
2418                            const char *Name) {
2419   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2420                                     unwrap(LHS), unwrap(RHS), Name));
2421 }
2422 
2423 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2424                            LLVMValueRef LHS, LLVMValueRef RHS,
2425                            const char *Name) {
2426   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2427                                     unwrap(LHS), unwrap(RHS), Name));
2428 }
2429 
2430 /*--.. Miscellaneous instructions ..........................................--*/
2431 
2432 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2433   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2434 }
2435 
2436 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2437                            LLVMValueRef *Args, unsigned NumArgs,
2438                            const char *Name) {
2439   return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2440                                     makeArrayRef(unwrap(Args), NumArgs),
2441                                     Name));
2442 }
2443 
2444 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2445                              LLVMValueRef Then, LLVMValueRef Else,
2446                              const char *Name) {
2447   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2448                                       Name));
2449 }
2450 
2451 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2452                             LLVMTypeRef Ty, const char *Name) {
2453   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2454 }
2455 
2456 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2457                                       LLVMValueRef Index, const char *Name) {
2458   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2459                                               Name));
2460 }
2461 
2462 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2463                                     LLVMValueRef EltVal, LLVMValueRef Index,
2464                                     const char *Name) {
2465   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2466                                              unwrap(Index), Name));
2467 }
2468 
2469 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2470                                     LLVMValueRef V2, LLVMValueRef Mask,
2471                                     const char *Name) {
2472   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2473                                              unwrap(Mask), Name));
2474 }
2475 
2476 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2477                                    unsigned Index, const char *Name) {
2478   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2479 }
2480 
2481 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2482                                   LLVMValueRef EltVal, unsigned Index,
2483                                   const char *Name) {
2484   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2485                                            Index, Name));
2486 }
2487 
2488 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2489                              const char *Name) {
2490   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2491 }
2492 
2493 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2494                                 const char *Name) {
2495   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2496 }
2497 
2498 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2499                               LLVMValueRef RHS, const char *Name) {
2500   return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2501 }
2502 
2503 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2504                                LLVMValueRef PTR, LLVMValueRef Val,
2505                                LLVMAtomicOrdering ordering,
2506                                LLVMBool singleThread) {
2507   AtomicRMWInst::BinOp intop;
2508   switch (op) {
2509     case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2510     case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2511     case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2512     case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2513     case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2514     case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2515     case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2516     case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2517     case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2518     case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2519     case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2520   }
2521   return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2522     mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
2523 }
2524 
2525 
2526 /*===-- Module providers --------------------------------------------------===*/
2527 
2528 LLVMModuleProviderRef
2529 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2530   return reinterpret_cast<LLVMModuleProviderRef>(M);
2531 }
2532 
2533 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2534   delete unwrap(MP);
2535 }
2536 
2537 
2538 /*===-- Memory buffers ----------------------------------------------------===*/
2539 
2540 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2541     const char *Path,
2542     LLVMMemoryBufferRef *OutMemBuf,
2543     char **OutMessage) {
2544 
2545   std::unique_ptr<MemoryBuffer> MB;
2546   error_code ec;
2547   if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2548     *OutMemBuf = wrap(MB.release());
2549     return 0;
2550   }
2551 
2552   *OutMessage = strdup(ec.message().c_str());
2553   return 1;
2554 }
2555 
2556 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2557                                          char **OutMessage) {
2558   std::unique_ptr<MemoryBuffer> MB;
2559   error_code ec;
2560   if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2561     *OutMemBuf = wrap(MB.release());
2562     return 0;
2563   }
2564 
2565   *OutMessage = strdup(ec.message().c_str());
2566   return 1;
2567 }
2568 
2569 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2570     const char *InputData,
2571     size_t InputDataLength,
2572     const char *BufferName,
2573     LLVMBool RequiresNullTerminator) {
2574 
2575   return wrap(MemoryBuffer::getMemBuffer(
2576       StringRef(InputData, InputDataLength),
2577       StringRef(BufferName),
2578       RequiresNullTerminator));
2579 }
2580 
2581 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2582     const char *InputData,
2583     size_t InputDataLength,
2584     const char *BufferName) {
2585 
2586   return wrap(MemoryBuffer::getMemBufferCopy(
2587       StringRef(InputData, InputDataLength),
2588       StringRef(BufferName)));
2589 }
2590 
2591 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2592   return unwrap(MemBuf)->getBufferStart();
2593 }
2594 
2595 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2596   return unwrap(MemBuf)->getBufferSize();
2597 }
2598 
2599 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2600   delete unwrap(MemBuf);
2601 }
2602 
2603 /*===-- Pass Registry -----------------------------------------------------===*/
2604 
2605 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2606   return wrap(PassRegistry::getPassRegistry());
2607 }
2608 
2609 /*===-- Pass Manager ------------------------------------------------------===*/
2610 
2611 LLVMPassManagerRef LLVMCreatePassManager() {
2612   return wrap(new PassManager());
2613 }
2614 
2615 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2616   return wrap(new FunctionPassManager(unwrap(M)));
2617 }
2618 
2619 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2620   return LLVMCreateFunctionPassManagerForModule(
2621                                             reinterpret_cast<LLVMModuleRef>(P));
2622 }
2623 
2624 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2625   return unwrap<PassManager>(PM)->run(*unwrap(M));
2626 }
2627 
2628 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2629   return unwrap<FunctionPassManager>(FPM)->doInitialization();
2630 }
2631 
2632 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2633   return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2634 }
2635 
2636 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2637   return unwrap<FunctionPassManager>(FPM)->doFinalization();
2638 }
2639 
2640 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2641   delete unwrap(PM);
2642 }
2643 
2644 /*===-- Threading ------------------------------------------------------===*/
2645 
2646 LLVMBool LLVMStartMultithreaded() {
2647   return llvm_start_multithreaded();
2648 }
2649 
2650 void LLVMStopMultithreaded() {
2651   llvm_stop_multithreaded();
2652 }
2653 
2654 LLVMBool LLVMIsMultithreaded() {
2655   return llvm_is_multithreaded();
2656 }
2657