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