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