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