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