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