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