Remove linkonce_odr_auto_hide.
[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::WeakAnyLinkage:
1138     return LLVMWeakAnyLinkage;
1139   case GlobalValue::WeakODRLinkage:
1140     return LLVMWeakODRLinkage;
1141   case GlobalValue::AppendingLinkage:
1142     return LLVMAppendingLinkage;
1143   case GlobalValue::InternalLinkage:
1144     return LLVMInternalLinkage;
1145   case GlobalValue::PrivateLinkage:
1146     return LLVMPrivateLinkage;
1147   case GlobalValue::LinkerPrivateLinkage:
1148     return LLVMLinkerPrivateLinkage;
1149   case GlobalValue::LinkerPrivateWeakLinkage:
1150     return LLVMLinkerPrivateWeakLinkage;
1151   case GlobalValue::DLLImportLinkage:
1152     return LLVMDLLImportLinkage;
1153   case GlobalValue::DLLExportLinkage:
1154     return LLVMDLLExportLinkage;
1155   case GlobalValue::ExternalWeakLinkage:
1156     return LLVMExternalWeakLinkage;
1157   case GlobalValue::CommonLinkage:
1158     return LLVMCommonLinkage;
1159   }
1160
1161   llvm_unreachable("Invalid GlobalValue linkage!");
1162 }
1163
1164 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1165   GlobalValue *GV = unwrap<GlobalValue>(Global);
1166
1167   switch (Linkage) {
1168   case LLVMExternalLinkage:
1169     GV->setLinkage(GlobalValue::ExternalLinkage);
1170     break;
1171   case LLVMAvailableExternallyLinkage:
1172     GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1173     break;
1174   case LLVMLinkOnceAnyLinkage:
1175     GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1176     break;
1177   case LLVMLinkOnceODRLinkage:
1178     GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1179     break;
1180   case LLVMLinkOnceODRAutoHideLinkage:
1181     DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1182                     "longer supported.");
1183     break;
1184   case LLVMWeakAnyLinkage:
1185     GV->setLinkage(GlobalValue::WeakAnyLinkage);
1186     break;
1187   case LLVMWeakODRLinkage:
1188     GV->setLinkage(GlobalValue::WeakODRLinkage);
1189     break;
1190   case LLVMAppendingLinkage:
1191     GV->setLinkage(GlobalValue::AppendingLinkage);
1192     break;
1193   case LLVMInternalLinkage:
1194     GV->setLinkage(GlobalValue::InternalLinkage);
1195     break;
1196   case LLVMPrivateLinkage:
1197     GV->setLinkage(GlobalValue::PrivateLinkage);
1198     break;
1199   case LLVMLinkerPrivateLinkage:
1200     GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1201     break;
1202   case LLVMLinkerPrivateWeakLinkage:
1203     GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1204     break;
1205   case LLVMDLLImportLinkage:
1206     GV->setLinkage(GlobalValue::DLLImportLinkage);
1207     break;
1208   case LLVMDLLExportLinkage:
1209     GV->setLinkage(GlobalValue::DLLExportLinkage);
1210     break;
1211   case LLVMExternalWeakLinkage:
1212     GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1213     break;
1214   case LLVMGhostLinkage:
1215     DEBUG(errs()
1216           << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1217     break;
1218   case LLVMCommonLinkage:
1219     GV->setLinkage(GlobalValue::CommonLinkage);
1220     break;
1221   }
1222 }
1223
1224 const char *LLVMGetSection(LLVMValueRef Global) {
1225   return unwrap<GlobalValue>(Global)->getSection().c_str();
1226 }
1227
1228 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1229   unwrap<GlobalValue>(Global)->setSection(Section);
1230 }
1231
1232 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1233   return static_cast<LLVMVisibility>(
1234     unwrap<GlobalValue>(Global)->getVisibility());
1235 }
1236
1237 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1238   unwrap<GlobalValue>(Global)
1239     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1240 }
1241
1242 /*--.. Operations on global variables, load and store instructions .........--*/
1243
1244 unsigned LLVMGetAlignment(LLVMValueRef V) {
1245   Value *P = unwrap<Value>(V);
1246   if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1247     return GV->getAlignment();
1248   if (LoadInst *LI = dyn_cast<LoadInst>(P))
1249     return LI->getAlignment();
1250   if (StoreInst *SI = dyn_cast<StoreInst>(P))
1251     return SI->getAlignment();
1252
1253   llvm_unreachable("only GlobalValue, LoadInst and StoreInst have alignment");
1254 }
1255
1256 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1257   Value *P = unwrap<Value>(V);
1258   if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1259     GV->setAlignment(Bytes);
1260   else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1261     LI->setAlignment(Bytes);
1262   else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1263     SI->setAlignment(Bytes);
1264   else
1265     llvm_unreachable("only GlobalValue, LoadInst and StoreInst have alignment");
1266 }
1267
1268 /*--.. Operations on global variables ......................................--*/
1269
1270 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1271   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1272                                  GlobalValue::ExternalLinkage, 0, Name));
1273 }
1274
1275 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1276                                          const char *Name,
1277                                          unsigned AddressSpace) {
1278   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1279                                  GlobalValue::ExternalLinkage, 0, Name, 0,
1280                                  GlobalVariable::NotThreadLocal, AddressSpace));
1281 }
1282
1283 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1284   return wrap(unwrap(M)->getNamedGlobal(Name));
1285 }
1286
1287 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1288   Module *Mod = unwrap(M);
1289   Module::global_iterator I = Mod->global_begin();
1290   if (I == Mod->global_end())
1291     return 0;
1292   return wrap(I);
1293 }
1294
1295 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1296   Module *Mod = unwrap(M);
1297   Module::global_iterator I = Mod->global_end();
1298   if (I == Mod->global_begin())
1299     return 0;
1300   return wrap(--I);
1301 }
1302
1303 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1304   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1305   Module::global_iterator I = GV;
1306   if (++I == GV->getParent()->global_end())
1307     return 0;
1308   return wrap(I);
1309 }
1310
1311 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1312   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1313   Module::global_iterator I = GV;
1314   if (I == GV->getParent()->global_begin())
1315     return 0;
1316   return wrap(--I);
1317 }
1318
1319 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1320   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1321 }
1322
1323 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1324   GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1325   if ( !GV->hasInitializer() )
1326     return 0;
1327   return wrap(GV->getInitializer());
1328 }
1329
1330 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1331   unwrap<GlobalVariable>(GlobalVar)
1332     ->setInitializer(unwrap<Constant>(ConstantVal));
1333 }
1334
1335 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1336   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1337 }
1338
1339 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1340   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1341 }
1342
1343 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1344   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1345 }
1346
1347 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1348   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1349 }
1350
1351 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1352   switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1353   case GlobalVariable::NotThreadLocal:
1354     return LLVMNotThreadLocal;
1355   case GlobalVariable::GeneralDynamicTLSModel:
1356     return LLVMGeneralDynamicTLSModel;
1357   case GlobalVariable::LocalDynamicTLSModel:
1358     return LLVMLocalDynamicTLSModel;
1359   case GlobalVariable::InitialExecTLSModel:
1360     return LLVMInitialExecTLSModel;
1361   case GlobalVariable::LocalExecTLSModel:
1362     return LLVMLocalExecTLSModel;
1363   }
1364
1365   llvm_unreachable("Invalid GlobalVariable thread local mode");
1366 }
1367
1368 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1369   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1370
1371   switch (Mode) {
1372   case LLVMNotThreadLocal:
1373     GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1374     break;
1375   case LLVMGeneralDynamicTLSModel:
1376     GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1377     break;
1378   case LLVMLocalDynamicTLSModel:
1379     GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1380     break;
1381   case LLVMInitialExecTLSModel:
1382     GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1383     break;
1384   case LLVMLocalExecTLSModel:
1385     GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1386     break;
1387   }
1388 }
1389
1390 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1391   return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1392 }
1393
1394 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1395   unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1396 }
1397
1398 /*--.. Operations on aliases ......................................--*/
1399
1400 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1401                           const char *Name) {
1402   return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1403                               unwrap<Constant>(Aliasee), unwrap (M)));
1404 }
1405
1406 /*--.. Operations on functions .............................................--*/
1407
1408 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1409                              LLVMTypeRef FunctionTy) {
1410   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1411                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
1412 }
1413
1414 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1415   return wrap(unwrap(M)->getFunction(Name));
1416 }
1417
1418 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1419   Module *Mod = unwrap(M);
1420   Module::iterator I = Mod->begin();
1421   if (I == Mod->end())
1422     return 0;
1423   return wrap(I);
1424 }
1425
1426 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1427   Module *Mod = unwrap(M);
1428   Module::iterator I = Mod->end();
1429   if (I == Mod->begin())
1430     return 0;
1431   return wrap(--I);
1432 }
1433
1434 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1435   Function *Func = unwrap<Function>(Fn);
1436   Module::iterator I = Func;
1437   if (++I == Func->getParent()->end())
1438     return 0;
1439   return wrap(I);
1440 }
1441
1442 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1443   Function *Func = unwrap<Function>(Fn);
1444   Module::iterator I = Func;
1445   if (I == Func->getParent()->begin())
1446     return 0;
1447   return wrap(--I);
1448 }
1449
1450 void LLVMDeleteFunction(LLVMValueRef Fn) {
1451   unwrap<Function>(Fn)->eraseFromParent();
1452 }
1453
1454 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1455   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1456     return F->getIntrinsicID();
1457   return 0;
1458 }
1459
1460 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1461   return unwrap<Function>(Fn)->getCallingConv();
1462 }
1463
1464 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1465   return unwrap<Function>(Fn)->setCallingConv(
1466     static_cast<CallingConv::ID>(CC));
1467 }
1468
1469 const char *LLVMGetGC(LLVMValueRef Fn) {
1470   Function *F = unwrap<Function>(Fn);
1471   return F->hasGC()? F->getGC() : 0;
1472 }
1473
1474 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1475   Function *F = unwrap<Function>(Fn);
1476   if (GC)
1477     F->setGC(GC);
1478   else
1479     F->clearGC();
1480 }
1481
1482 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1483   Function *Func = unwrap<Function>(Fn);
1484   const AttributeSet PAL = Func->getAttributes();
1485   AttrBuilder B(PA);
1486   const AttributeSet PALnew =
1487     PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1488                       AttributeSet::get(Func->getContext(),
1489                                         AttributeSet::FunctionIndex, B));
1490   Func->setAttributes(PALnew);
1491 }
1492
1493 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1494                                         const char *V) {
1495   Function *Func = unwrap<Function>(Fn);
1496   AttributeSet::AttrIndex Idx =
1497     AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1498   AttrBuilder B;
1499
1500   B.addAttribute(A, V);
1501   AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1502   Func->addAttributes(Idx, Set);
1503 }
1504
1505 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1506   Function *Func = unwrap<Function>(Fn);
1507   const AttributeSet PAL = Func->getAttributes();
1508   AttrBuilder B(PA);
1509   const AttributeSet PALnew =
1510     PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1511                          AttributeSet::get(Func->getContext(),
1512                                            AttributeSet::FunctionIndex, B));
1513   Func->setAttributes(PALnew);
1514 }
1515
1516 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1517   Function *Func = unwrap<Function>(Fn);
1518   const AttributeSet PAL = Func->getAttributes();
1519   return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1520 }
1521
1522 /*--.. Operations on parameters ............................................--*/
1523
1524 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1525   // This function is strictly redundant to
1526   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1527   return unwrap<Function>(FnRef)->arg_size();
1528 }
1529
1530 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1531   Function *Fn = unwrap<Function>(FnRef);
1532   for (Function::arg_iterator I = Fn->arg_begin(),
1533                               E = Fn->arg_end(); I != E; I++)
1534     *ParamRefs++ = wrap(I);
1535 }
1536
1537 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1538   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1539   while (index --> 0)
1540     AI++;
1541   return wrap(AI);
1542 }
1543
1544 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1545   return wrap(unwrap<Argument>(V)->getParent());
1546 }
1547
1548 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1549   Function *Func = unwrap<Function>(Fn);
1550   Function::arg_iterator I = Func->arg_begin();
1551   if (I == Func->arg_end())
1552     return 0;
1553   return wrap(I);
1554 }
1555
1556 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1557   Function *Func = unwrap<Function>(Fn);
1558   Function::arg_iterator I = Func->arg_end();
1559   if (I == Func->arg_begin())
1560     return 0;
1561   return wrap(--I);
1562 }
1563
1564 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1565   Argument *A = unwrap<Argument>(Arg);
1566   Function::arg_iterator I = A;
1567   if (++I == A->getParent()->arg_end())
1568     return 0;
1569   return wrap(I);
1570 }
1571
1572 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1573   Argument *A = unwrap<Argument>(Arg);
1574   Function::arg_iterator I = A;
1575   if (I == A->getParent()->arg_begin())
1576     return 0;
1577   return wrap(--I);
1578 }
1579
1580 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1581   Argument *A = unwrap<Argument>(Arg);
1582   AttrBuilder B(PA);
1583   A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1,  B));
1584 }
1585
1586 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1587   Argument *A = unwrap<Argument>(Arg);
1588   AttrBuilder B(PA);
1589   A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1,  B));
1590 }
1591
1592 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1593   Argument *A = unwrap<Argument>(Arg);
1594   return (LLVMAttribute)A->getParent()->getAttributes().
1595     Raw(A->getArgNo()+1);
1596 }
1597
1598
1599 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1600   Argument *A = unwrap<Argument>(Arg);
1601   AttrBuilder B;
1602   B.addAlignmentAttr(align);
1603   A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1604 }
1605
1606 /*--.. Operations on basic blocks ..........................................--*/
1607
1608 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1609   return wrap(static_cast<Value*>(unwrap(BB)));
1610 }
1611
1612 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1613   return isa<BasicBlock>(unwrap(Val));
1614 }
1615
1616 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1617   return wrap(unwrap<BasicBlock>(Val));
1618 }
1619
1620 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1621   return wrap(unwrap(BB)->getParent());
1622 }
1623
1624 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1625   return wrap(unwrap(BB)->getTerminator());
1626 }
1627
1628 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1629   return unwrap<Function>(FnRef)->size();
1630 }
1631
1632 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1633   Function *Fn = unwrap<Function>(FnRef);
1634   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1635     *BasicBlocksRefs++ = wrap(I);
1636 }
1637
1638 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1639   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1640 }
1641
1642 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1643   Function *Func = unwrap<Function>(Fn);
1644   Function::iterator I = Func->begin();
1645   if (I == Func->end())
1646     return 0;
1647   return wrap(I);
1648 }
1649
1650 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1651   Function *Func = unwrap<Function>(Fn);
1652   Function::iterator I = Func->end();
1653   if (I == Func->begin())
1654     return 0;
1655   return wrap(--I);
1656 }
1657
1658 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1659   BasicBlock *Block = unwrap(BB);
1660   Function::iterator I = Block;
1661   if (++I == Block->getParent()->end())
1662     return 0;
1663   return wrap(I);
1664 }
1665
1666 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1667   BasicBlock *Block = unwrap(BB);
1668   Function::iterator I = Block;
1669   if (I == Block->getParent()->begin())
1670     return 0;
1671   return wrap(--I);
1672 }
1673
1674 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1675                                                 LLVMValueRef FnRef,
1676                                                 const char *Name) {
1677   return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1678 }
1679
1680 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1681   return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1682 }
1683
1684 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1685                                                 LLVMBasicBlockRef BBRef,
1686                                                 const char *Name) {
1687   BasicBlock *BB = unwrap(BBRef);
1688   return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1689 }
1690
1691 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1692                                        const char *Name) {
1693   return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1694 }
1695
1696 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1697   unwrap(BBRef)->eraseFromParent();
1698 }
1699
1700 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1701   unwrap(BBRef)->removeFromParent();
1702 }
1703
1704 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1705   unwrap(BB)->moveBefore(unwrap(MovePos));
1706 }
1707
1708 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1709   unwrap(BB)->moveAfter(unwrap(MovePos));
1710 }
1711
1712 /*--.. Operations on instructions ..........................................--*/
1713
1714 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1715   return wrap(unwrap<Instruction>(Inst)->getParent());
1716 }
1717
1718 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1719   BasicBlock *Block = unwrap(BB);
1720   BasicBlock::iterator I = Block->begin();
1721   if (I == Block->end())
1722     return 0;
1723   return wrap(I);
1724 }
1725
1726 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1727   BasicBlock *Block = unwrap(BB);
1728   BasicBlock::iterator I = Block->end();
1729   if (I == Block->begin())
1730     return 0;
1731   return wrap(--I);
1732 }
1733
1734 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1735   Instruction *Instr = unwrap<Instruction>(Inst);
1736   BasicBlock::iterator I = Instr;
1737   if (++I == Instr->getParent()->end())
1738     return 0;
1739   return wrap(I);
1740 }
1741
1742 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1743   Instruction *Instr = unwrap<Instruction>(Inst);
1744   BasicBlock::iterator I = Instr;
1745   if (I == Instr->getParent()->begin())
1746     return 0;
1747   return wrap(--I);
1748 }
1749
1750 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1751   unwrap<Instruction>(Inst)->eraseFromParent();
1752 }
1753
1754 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1755   if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1756     return (LLVMIntPredicate)I->getPredicate();
1757   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1758     if (CE->getOpcode() == Instruction::ICmp)
1759       return (LLVMIntPredicate)CE->getPredicate();
1760   return (LLVMIntPredicate)0;
1761 }
1762
1763 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1764   if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1765     return map_to_llvmopcode(C->getOpcode());
1766   return (LLVMOpcode)0;
1767 }
1768
1769 /*--.. Call and invoke instructions ........................................--*/
1770
1771 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1772   Value *V = unwrap(Instr);
1773   if (CallInst *CI = dyn_cast<CallInst>(V))
1774     return CI->getCallingConv();
1775   if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1776     return II->getCallingConv();
1777   llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1778 }
1779
1780 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1781   Value *V = unwrap(Instr);
1782   if (CallInst *CI = dyn_cast<CallInst>(V))
1783     return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1784   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1785     return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1786   llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1787 }
1788
1789 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1790                            LLVMAttribute PA) {
1791   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1792   AttrBuilder B(PA);
1793   Call.setAttributes(
1794     Call.getAttributes().addAttributes(Call->getContext(), index,
1795                                        AttributeSet::get(Call->getContext(),
1796                                                          index, B)));
1797 }
1798
1799 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1800                               LLVMAttribute PA) {
1801   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1802   AttrBuilder B(PA);
1803   Call.setAttributes(Call.getAttributes()
1804                        .removeAttributes(Call->getContext(), index,
1805                                          AttributeSet::get(Call->getContext(),
1806                                                            index, B)));
1807 }
1808
1809 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1810                                 unsigned align) {
1811   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1812   AttrBuilder B;
1813   B.addAlignmentAttr(align);
1814   Call.setAttributes(Call.getAttributes()
1815                        .addAttributes(Call->getContext(), index,
1816                                       AttributeSet::get(Call->getContext(),
1817                                                         index, B)));
1818 }
1819
1820 /*--.. Operations on call instructions (only) ..............................--*/
1821
1822 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1823   return unwrap<CallInst>(Call)->isTailCall();
1824 }
1825
1826 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1827   unwrap<CallInst>(Call)->setTailCall(isTailCall);
1828 }
1829
1830 /*--.. Operations on switch instructions (only) ............................--*/
1831
1832 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1833   return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1834 }
1835
1836 /*--.. Operations on phi nodes .............................................--*/
1837
1838 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1839                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1840   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1841   for (unsigned I = 0; I != Count; ++I)
1842     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1843 }
1844
1845 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1846   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1847 }
1848
1849 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1850   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1851 }
1852
1853 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1854   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1855 }
1856
1857
1858 /*===-- Instruction builders ----------------------------------------------===*/
1859
1860 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1861   return wrap(new IRBuilder<>(*unwrap(C)));
1862 }
1863
1864 LLVMBuilderRef LLVMCreateBuilder(void) {
1865   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1866 }
1867
1868 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1869                          LLVMValueRef Instr) {
1870   BasicBlock *BB = unwrap(Block);
1871   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1872   unwrap(Builder)->SetInsertPoint(BB, I);
1873 }
1874
1875 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1876   Instruction *I = unwrap<Instruction>(Instr);
1877   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1878 }
1879
1880 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1881   BasicBlock *BB = unwrap(Block);
1882   unwrap(Builder)->SetInsertPoint(BB);
1883 }
1884
1885 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1886    return wrap(unwrap(Builder)->GetInsertBlock());
1887 }
1888
1889 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1890   unwrap(Builder)->ClearInsertionPoint();
1891 }
1892
1893 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1894   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1895 }
1896
1897 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1898                                    const char *Name) {
1899   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1900 }
1901
1902 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1903   delete unwrap(Builder);
1904 }
1905
1906 /*--.. Metadata builders ...................................................--*/
1907
1908 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1909   MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1910   unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1911 }
1912
1913 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1914   return wrap(unwrap(Builder)->getCurrentDebugLocation()
1915               .getAsMDNode(unwrap(Builder)->getContext()));
1916 }
1917
1918 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1919   unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1920 }
1921
1922
1923 /*--.. Instruction builders ................................................--*/
1924
1925 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1926   return wrap(unwrap(B)->CreateRetVoid());
1927 }
1928
1929 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1930   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1931 }
1932
1933 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1934                                    unsigned N) {
1935   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1936 }
1937
1938 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1939   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1940 }
1941
1942 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1943                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1944   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1945 }
1946
1947 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1948                              LLVMBasicBlockRef Else, unsigned NumCases) {
1949   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1950 }
1951
1952 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1953                                  unsigned NumDests) {
1954   return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1955 }
1956
1957 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1958                              LLVMValueRef *Args, unsigned NumArgs,
1959                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1960                              const char *Name) {
1961   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1962                                       makeArrayRef(unwrap(Args), NumArgs),
1963                                       Name));
1964 }
1965
1966 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1967                                  LLVMValueRef PersFn, unsigned NumClauses,
1968                                  const char *Name) {
1969   return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1970                                           cast<Function>(unwrap(PersFn)),
1971                                           NumClauses, Name));
1972 }
1973
1974 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1975   return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1976 }
1977
1978 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1979   return wrap(unwrap(B)->CreateUnreachable());
1980 }
1981
1982 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1983                  LLVMBasicBlockRef Dest) {
1984   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1985 }
1986
1987 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1988   unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1989 }
1990
1991 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1992   unwrap<LandingPadInst>(LandingPad)->
1993     addClause(cast<Constant>(unwrap(ClauseVal)));
1994 }
1995
1996 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1997   unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
1998 }
1999
2000 /*--.. Arithmetic ..........................................................--*/
2001
2002 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2003                           const char *Name) {
2004   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2005 }
2006
2007 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2008                           const char *Name) {
2009   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2010 }
2011
2012 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2013                           const char *Name) {
2014   return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2015 }
2016
2017 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2018                           const char *Name) {
2019   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2020 }
2021
2022 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2023                           const char *Name) {
2024   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2025 }
2026
2027 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2028                           const char *Name) {
2029   return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2030 }
2031
2032 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2033                           const char *Name) {
2034   return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2035 }
2036
2037 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2038                           const char *Name) {
2039   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2040 }
2041
2042 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2043                           const char *Name) {
2044   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2045 }
2046
2047 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2048                           const char *Name) {
2049   return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2050 }
2051
2052 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2053                           const char *Name) {
2054   return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2055 }
2056
2057 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2058                           const char *Name) {
2059   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2060 }
2061
2062 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2063                            const char *Name) {
2064   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2065 }
2066
2067 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2068                            const char *Name) {
2069   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2070 }
2071
2072 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2073                                 LLVMValueRef RHS, const char *Name) {
2074   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2075 }
2076
2077 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2078                            const char *Name) {
2079   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2080 }
2081
2082 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2083                            const char *Name) {
2084   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2085 }
2086
2087 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2088                            const char *Name) {
2089   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2090 }
2091
2092 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2093                            const char *Name) {
2094   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2095 }
2096
2097 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2098                           const char *Name) {
2099   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2100 }
2101
2102 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2103                            const char *Name) {
2104   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2105 }
2106
2107 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2108                            const char *Name) {
2109   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2110 }
2111
2112 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2113                           const char *Name) {
2114   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2115 }
2116
2117 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2118                          const char *Name) {
2119   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2120 }
2121
2122 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2123                           const char *Name) {
2124   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2125 }
2126
2127 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2128                             LLVMValueRef LHS, LLVMValueRef RHS,
2129                             const char *Name) {
2130   return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2131                                      unwrap(RHS), Name));
2132 }
2133
2134 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2135   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2136 }
2137
2138 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2139                              const char *Name) {
2140   return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2141 }
2142
2143 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2144                              const char *Name) {
2145   return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2146 }
2147
2148 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2149   return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2150 }
2151
2152 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2153   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2154 }
2155
2156 /*--.. Memory ..............................................................--*/
2157
2158 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2159                              const char *Name) {
2160   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2161   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2162   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2163   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2164                                                ITy, unwrap(Ty), AllocSize,
2165                                                0, 0, "");
2166   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2167 }
2168
2169 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2170                                   LLVMValueRef Val, const char *Name) {
2171   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2172   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2173   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2174   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2175                                                ITy, unwrap(Ty), AllocSize,
2176                                                unwrap(Val), 0, "");
2177   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2178 }
2179
2180 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2181                              const char *Name) {
2182   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
2183 }
2184
2185 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2186                                   LLVMValueRef Val, const char *Name) {
2187   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2188 }
2189
2190 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2191   return wrap(unwrap(B)->Insert(
2192      CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2193 }
2194
2195
2196 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2197                            const char *Name) {
2198   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2199 }
2200
2201 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2202                             LLVMValueRef PointerVal) {
2203   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2204 }
2205
2206 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2207                           LLVMValueRef *Indices, unsigned NumIndices,
2208                           const char *Name) {
2209   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2210   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2211 }
2212
2213 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2214                                   LLVMValueRef *Indices, unsigned NumIndices,
2215                                   const char *Name) {
2216   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2217   return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2218 }
2219
2220 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2221                                 unsigned Idx, const char *Name) {
2222   return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2223 }
2224
2225 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2226                                    const char *Name) {
2227   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2228 }
2229
2230 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2231                                       const char *Name) {
2232   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2233 }
2234
2235 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2236   Value *P = unwrap<Value>(MemAccessInst);
2237   if (LoadInst *LI = dyn_cast<LoadInst>(P))
2238     return LI->isVolatile();
2239   return cast<StoreInst>(P)->isVolatile();
2240 }
2241
2242 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2243   Value *P = unwrap<Value>(MemAccessInst);
2244   if (LoadInst *LI = dyn_cast<LoadInst>(P))
2245     return LI->setVolatile(isVolatile);
2246   return cast<StoreInst>(P)->setVolatile(isVolatile);
2247 }
2248
2249 /*--.. Casts ...............................................................--*/
2250
2251 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2252                             LLVMTypeRef DestTy, const char *Name) {
2253   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2254 }
2255
2256 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2257                            LLVMTypeRef DestTy, const char *Name) {
2258   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2259 }
2260
2261 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2262                            LLVMTypeRef DestTy, const char *Name) {
2263   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2264 }
2265
2266 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2267                              LLVMTypeRef DestTy, const char *Name) {
2268   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2269 }
2270
2271 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2272                              LLVMTypeRef DestTy, const char *Name) {
2273   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2274 }
2275
2276 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2277                              LLVMTypeRef DestTy, const char *Name) {
2278   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2279 }
2280
2281 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2282                              LLVMTypeRef DestTy, const char *Name) {
2283   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2284 }
2285
2286 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2287                               LLVMTypeRef DestTy, const char *Name) {
2288   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2289 }
2290
2291 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2292                             LLVMTypeRef DestTy, const char *Name) {
2293   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2294 }
2295
2296 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2297                                LLVMTypeRef DestTy, const char *Name) {
2298   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2299 }
2300
2301 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2302                                LLVMTypeRef DestTy, const char *Name) {
2303   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2304 }
2305
2306 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2307                               LLVMTypeRef DestTy, const char *Name) {
2308   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2309 }
2310
2311 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2312                                     LLVMTypeRef DestTy, const char *Name) {
2313   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2314                                              Name));
2315 }
2316
2317 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2318                                     LLVMTypeRef DestTy, const char *Name) {
2319   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2320                                              Name));
2321 }
2322
2323 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2324                                      LLVMTypeRef DestTy, const char *Name) {
2325   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2326                                               Name));
2327 }
2328
2329 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2330                            LLVMTypeRef DestTy, const char *Name) {
2331   return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2332                                     unwrap(DestTy), Name));
2333 }
2334
2335 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2336                                   LLVMTypeRef DestTy, const char *Name) {
2337   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2338 }
2339
2340 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2341                               LLVMTypeRef DestTy, const char *Name) {
2342   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2343                                        /*isSigned*/true, Name));
2344 }
2345
2346 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2347                              LLVMTypeRef DestTy, const char *Name) {
2348   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2349 }
2350
2351 /*--.. Comparisons .........................................................--*/
2352
2353 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2354                            LLVMValueRef LHS, LLVMValueRef RHS,
2355                            const char *Name) {
2356   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2357                                     unwrap(LHS), unwrap(RHS), Name));
2358 }
2359
2360 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2361                            LLVMValueRef LHS, LLVMValueRef RHS,
2362                            const char *Name) {
2363   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2364                                     unwrap(LHS), unwrap(RHS), Name));
2365 }
2366
2367 /*--.. Miscellaneous instructions ..........................................--*/
2368
2369 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2370   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2371 }
2372
2373 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2374                            LLVMValueRef *Args, unsigned NumArgs,
2375                            const char *Name) {
2376   return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2377                                     makeArrayRef(unwrap(Args), NumArgs),
2378                                     Name));
2379 }
2380
2381 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2382                              LLVMValueRef Then, LLVMValueRef Else,
2383                              const char *Name) {
2384   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2385                                       Name));
2386 }
2387
2388 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2389                             LLVMTypeRef Ty, const char *Name) {
2390   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2391 }
2392
2393 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2394                                       LLVMValueRef Index, const char *Name) {
2395   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2396                                               Name));
2397 }
2398
2399 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2400                                     LLVMValueRef EltVal, LLVMValueRef Index,
2401                                     const char *Name) {
2402   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2403                                              unwrap(Index), Name));
2404 }
2405
2406 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2407                                     LLVMValueRef V2, LLVMValueRef Mask,
2408                                     const char *Name) {
2409   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2410                                              unwrap(Mask), Name));
2411 }
2412
2413 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2414                                    unsigned Index, const char *Name) {
2415   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2416 }
2417
2418 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2419                                   LLVMValueRef EltVal, unsigned Index,
2420                                   const char *Name) {
2421   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2422                                            Index, Name));
2423 }
2424
2425 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2426                              const char *Name) {
2427   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2428 }
2429
2430 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2431                                 const char *Name) {
2432   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2433 }
2434
2435 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2436                               LLVMValueRef RHS, const char *Name) {
2437   return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2438 }
2439
2440 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2441                                LLVMValueRef PTR, LLVMValueRef Val,
2442                                LLVMAtomicOrdering ordering,
2443                                LLVMBool singleThread) {
2444   AtomicRMWInst::BinOp intop;
2445   switch (op) {
2446     case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2447     case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2448     case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2449     case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2450     case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2451     case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2452     case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2453     case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2454     case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2455     case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2456     case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2457   }
2458   AtomicOrdering intordering;
2459   switch (ordering) {
2460     case LLVMAtomicOrderingNotAtomic: intordering = NotAtomic; break;
2461     case LLVMAtomicOrderingUnordered: intordering = Unordered; break;
2462     case LLVMAtomicOrderingMonotonic: intordering = Monotonic; break;
2463     case LLVMAtomicOrderingAcquire: intordering = Acquire; break;
2464     case LLVMAtomicOrderingRelease: intordering = Release; break;
2465     case LLVMAtomicOrderingAcquireRelease:
2466       intordering = AcquireRelease;
2467       break;
2468     case LLVMAtomicOrderingSequentiallyConsistent:
2469       intordering = SequentiallyConsistent;
2470       break;
2471   }
2472   return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2473     intordering, singleThread ? SingleThread : CrossThread));
2474 }
2475
2476
2477 /*===-- Module providers --------------------------------------------------===*/
2478
2479 LLVMModuleProviderRef
2480 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2481   return reinterpret_cast<LLVMModuleProviderRef>(M);
2482 }
2483
2484 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2485   delete unwrap(MP);
2486 }
2487
2488
2489 /*===-- Memory buffers ----------------------------------------------------===*/
2490
2491 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2492     const char *Path,
2493     LLVMMemoryBufferRef *OutMemBuf,
2494     char **OutMessage) {
2495
2496   OwningPtr<MemoryBuffer> MB;
2497   error_code ec;
2498   if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2499     *OutMemBuf = wrap(MB.take());
2500     return 0;
2501   }
2502
2503   *OutMessage = strdup(ec.message().c_str());
2504   return 1;
2505 }
2506
2507 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2508                                          char **OutMessage) {
2509   OwningPtr<MemoryBuffer> MB;
2510   error_code ec;
2511   if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2512     *OutMemBuf = wrap(MB.take());
2513     return 0;
2514   }
2515
2516   *OutMessage = strdup(ec.message().c_str());
2517   return 1;
2518 }
2519
2520 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2521     const char *InputData,
2522     size_t InputDataLength,
2523     const char *BufferName,
2524     LLVMBool RequiresNullTerminator) {
2525
2526   return wrap(MemoryBuffer::getMemBuffer(
2527       StringRef(InputData, InputDataLength),
2528       StringRef(BufferName),
2529       RequiresNullTerminator));
2530 }
2531
2532 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2533     const char *InputData,
2534     size_t InputDataLength,
2535     const char *BufferName) {
2536
2537   return wrap(MemoryBuffer::getMemBufferCopy(
2538       StringRef(InputData, InputDataLength),
2539       StringRef(BufferName)));
2540 }
2541
2542 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2543   return unwrap(MemBuf)->getBufferStart();
2544 }
2545
2546 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2547   return unwrap(MemBuf)->getBufferSize();
2548 }
2549
2550 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2551   delete unwrap(MemBuf);
2552 }
2553
2554 /*===-- Pass Registry -----------------------------------------------------===*/
2555
2556 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2557   return wrap(PassRegistry::getPassRegistry());
2558 }
2559
2560 /*===-- Pass Manager ------------------------------------------------------===*/
2561
2562 LLVMPassManagerRef LLVMCreatePassManager() {
2563   return wrap(new PassManager());
2564 }
2565
2566 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2567   return wrap(new FunctionPassManager(unwrap(M)));
2568 }
2569
2570 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2571   return LLVMCreateFunctionPassManagerForModule(
2572                                             reinterpret_cast<LLVMModuleRef>(P));
2573 }
2574
2575 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2576   return unwrap<PassManager>(PM)->run(*unwrap(M));
2577 }
2578
2579 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2580   return unwrap<FunctionPassManager>(FPM)->doInitialization();
2581 }
2582
2583 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2584   return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2585 }
2586
2587 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2588   return unwrap<FunctionPassManager>(FPM)->doFinalization();
2589 }
2590
2591 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2592   delete unwrap(PM);
2593 }
2594
2595 /*===-- Threading ------------------------------------------------------===*/
2596
2597 LLVMBool LLVMStartMultithreaded() {
2598   return llvm_start_multithreaded();
2599 }
2600
2601 void LLVMStopMultithreaded() {
2602   llvm_stop_multithreaded();
2603 }
2604
2605 LLVMBool LLVMIsMultithreaded() {
2606   return llvm_is_multithreaded();
2607 }