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