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