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