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