d0f05eb414da576d23c65c7c740d38021813c883
[oota-llvm.git] / lib / VMCore / LLVMContext.cpp
1 //===-- LLVMContext.cpp - Implement LLVMContext -----------------------===//
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 LLVMContext, as a wrapper around the opaque
11 // class LLVMContextImpl.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/LLVMContext.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Instruction.h"
19 #include "llvm/MDNode.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include "LLVMContextImpl.h"
22 #include <cstdarg>
23
24 using namespace llvm;
25
26 static ManagedStatic<LLVMContext> GlobalContext;
27
28 LLVMContext& llvm::getGlobalContext() {
29   return *GlobalContext;
30 }
31
32 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { }
33 LLVMContext::~LLVMContext() { delete pImpl; }
34
35 // Constant accessors
36
37 // Constructor to create a '0' constant of arbitrary type...
38 static const uint64_t zero[2] = {0, 0};
39 Constant* LLVMContext::getNullValue(const Type* Ty) {
40   switch (Ty->getTypeID()) {
41   case Type::IntegerTyID:
42     return getConstantInt(Ty, 0);
43   case Type::FloatTyID:
44     return getConstantFP(APFloat(APInt(32, 0)));
45   case Type::DoubleTyID:
46     return getConstantFP(APFloat(APInt(64, 0)));
47   case Type::X86_FP80TyID:
48     return getConstantFP(APFloat(APInt(80, 2, zero)));
49   case Type::FP128TyID:
50     return getConstantFP(APFloat(APInt(128, 2, zero), true));
51   case Type::PPC_FP128TyID:
52     return getConstantFP(APFloat(APInt(128, 2, zero)));
53   case Type::PointerTyID:
54     return getConstantPointerNull(cast<PointerType>(Ty));
55   case Type::StructTyID:
56   case Type::ArrayTyID:
57   case Type::VectorTyID:
58     return getConstantAggregateZero(Ty);
59   default:
60     // Function, Label, or Opaque type?
61     assert(!"Cannot create a null constant of that type!");
62     return 0;
63   }
64 }
65
66 Constant* LLVMContext::getAllOnesValue(const Type* Ty) {
67   if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
68     return getConstantInt(APInt::getAllOnesValue(ITy->getBitWidth()));
69   
70   std::vector<Constant*> Elts;
71   const VectorType* VTy = cast<VectorType>(Ty);
72   Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
73   assert(Elts[0] && "Not a vector integer type!");
74   return cast<ConstantVector>(getConstantVector(Elts));
75 }
76
77 // UndefValue accessors.
78 UndefValue* LLVMContext::getUndef(const Type* Ty) {
79   return UndefValue::get(Ty);
80 }
81
82 // ConstantInt accessors.
83 ConstantInt* LLVMContext::getTrue() {
84   assert(this && "Context not initialized!");
85   assert(pImpl && "Context not initialized!");
86   return pImpl->getTrue();
87 }
88
89 ConstantInt* LLVMContext::getFalse() {
90   assert(this && "Context not initialized!");
91   assert(pImpl && "Context not initialized!");
92   return pImpl->getFalse();
93 }
94
95 Constant* LLVMContext::getConstantInt(const Type* Ty, uint64_t V,
96                                          bool isSigned) {
97   Constant *C = getConstantInt(cast<IntegerType>(Ty->getScalarType()),
98                                V, isSigned);
99
100   // For vectors, broadcast the value.
101   if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
102     return
103       getConstantVector(std::vector<Constant *>(VTy->getNumElements(), C));
104
105   return C;
106 }
107
108
109 ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
110                                          bool isSigned) {
111   return getConstantInt(APInt(Ty->getBitWidth(), V, isSigned));
112 }
113
114 ConstantInt* LLVMContext::getConstantIntSigned(const IntegerType* Ty,
115                                                int64_t V) {
116   return getConstantInt(Ty, V, true);
117 }
118
119 Constant *LLVMContext::getConstantIntSigned(const Type *Ty, int64_t V) {
120   return getConstantInt(Ty, V, true);
121 }
122
123 ConstantInt* LLVMContext::getConstantInt(const APInt& V) {
124   return pImpl->getConstantInt(V);
125 }
126
127 Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
128   ConstantInt *C = getConstantInt(V);
129   assert(C->getType() == Ty->getScalarType() &&
130          "ConstantInt type doesn't match the type implied by its value!");
131
132   // For vectors, broadcast the value.
133   if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
134     return
135       getConstantVector(std::vector<Constant *>(VTy->getNumElements(), C));
136
137   return C;
138 }
139
140 // ConstantPointerNull accessors.
141 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
142   return ConstantPointerNull::get(T);
143 }
144
145
146 // ConstantStruct accessors.
147 Constant* LLVMContext::getConstantStruct(const StructType* T,
148                                          const std::vector<Constant*>& V) {
149   return pImpl->getConstantStruct(T, V);
150 }
151
152 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
153                                          bool packed) {
154   std::vector<const Type*> StructEls;
155   StructEls.reserve(V.size());
156   for (unsigned i = 0, e = V.size(); i != e; ++i)
157     StructEls.push_back(V[i]->getType());
158   return getConstantStruct(getStructType(StructEls, packed), V);
159 }
160
161 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
162                                          unsigned NumVals, bool Packed) {
163   // FIXME: make this the primary ctor method.
164   return getConstantStruct(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
165 }
166
167
168 // ConstantAggregateZero accessors.
169 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
170   return pImpl->getConstantAggregateZero(Ty);
171 }
172
173
174 // ConstantArray accessors.
175 Constant* LLVMContext::getConstantArray(const ArrayType* T,
176                                         const std::vector<Constant*>& V) {
177   return pImpl->getConstantArray(T, V);
178 }
179
180 Constant* LLVMContext::getConstantArray(const ArrayType* T,
181                                         Constant* const* Vals,
182                                         unsigned NumVals) {
183   // FIXME: make this the primary ctor method.
184   return getConstantArray(T, std::vector<Constant*>(Vals, Vals+NumVals));
185 }
186
187 /// ConstantArray::get(const string&) - Return an array that is initialized to
188 /// contain the specified string.  If length is zero then a null terminator is 
189 /// added to the specified string so that it may be used in a natural way. 
190 /// Otherwise, the length parameter specifies how much of the string to use 
191 /// and it won't be null terminated.
192 ///
193 Constant* LLVMContext::getConstantArray(const std::string& Str,
194                                         bool AddNull) {
195   std::vector<Constant*> ElementVals;
196   for (unsigned i = 0; i < Str.length(); ++i)
197     ElementVals.push_back(getConstantInt(Type::Int8Ty, Str[i]));
198
199   // Add a null terminator to the string...
200   if (AddNull) {
201     ElementVals.push_back(getConstantInt(Type::Int8Ty, 0));
202   }
203
204   ArrayType *ATy = getArrayType(Type::Int8Ty, ElementVals.size());
205   return getConstantArray(ATy, ElementVals);
206 }
207
208
209 // ConstantExpr accessors.
210 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
211                                        Constant* C2) {
212   return ConstantExpr::get(Opcode, C1, C2);
213 }
214
215 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
216   return ConstantExpr::getTrunc(C, Ty);
217 }
218
219 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
220   return ConstantExpr::getSExt(C, Ty);
221 }
222
223 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
224   return ConstantExpr::getZExt(C, Ty);  
225 }
226
227 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
228   return ConstantExpr::getFPTrunc(C, Ty);
229 }
230
231 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
232   return ConstantExpr::getFPExtend(C, Ty);
233 }
234
235 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
236   return ConstantExpr::getUIToFP(C, Ty);
237 }
238
239 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
240   return ConstantExpr::getSIToFP(C, Ty);
241 }
242
243 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
244   return ConstantExpr::getFPToUI(C, Ty);
245 }
246
247 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
248   return ConstantExpr::getFPToSI(C, Ty);
249 }
250
251 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
252   return ConstantExpr::getPtrToInt(C, Ty);
253 }
254
255 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
256   return ConstantExpr::getIntToPtr(C, Ty);
257 }
258
259 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
260   return ConstantExpr::getBitCast(C, Ty);
261 }
262
263 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
264                                            const Type* Ty) {
265   return ConstantExpr::getCast(ops, C, Ty);
266 }
267
268 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
269                                                     const Type* Ty) {
270   return ConstantExpr::getZExtOrBitCast(C, Ty);
271 }
272
273 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
274                                                     const Type* Ty) {
275   return ConstantExpr::getSExtOrBitCast(C, Ty);
276 }
277
278 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
279                                                      const Type* Ty) {
280   return ConstantExpr::getTruncOrBitCast(C, Ty);  
281 }
282
283 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
284   return ConstantExpr::getPointerCast(C, Ty);
285 }
286
287 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
288                                                   bool isSigned) {
289   return ConstantExpr::getIntegerCast(C, Ty, isSigned);
290 }
291
292 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
293   return ConstantExpr::getFPCast(C, Ty);
294 }
295
296 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
297                                              Constant* V2) {
298   return ConstantExpr::getSelect(C, V1, V2);
299 }
300
301 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
302   // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
303   const Type *AligningTy = getStructType(Type::Int8Ty, Ty, NULL);
304   Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
305   Constant *Zero = getConstantInt(Type::Int32Ty, 0);
306   Constant *One = getConstantInt(Type::Int32Ty, 1);
307   Constant *Indices[2] = { Zero, One };
308   Constant *GEP = getConstantExprGetElementPtr(NullPtr, Indices, 2);
309   return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
310 }
311
312 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
313                                  Constant* C1, Constant* C2) {
314   return ConstantExpr::getCompare(pred, C1, C2);
315 }
316
317 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
318   // API compatibility: Adjust integer opcodes to floating-point opcodes.
319   if (C->getType()->isFPOrFPVector())
320     return getConstantExprFNeg(C);
321   assert(C->getType()->isIntOrIntVector() &&
322          "Cannot NEG a nonintegral value!");
323   return getConstantExpr(Instruction::Sub,
324              getZeroValueForNegation(C->getType()),
325              C);
326 }
327
328 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
329   assert(C->getType()->isFPOrFPVector() &&
330          "Cannot FNEG a non-floating-point value!");
331   return getConstantExpr(Instruction::FSub,
332              getZeroValueForNegation(C->getType()),
333              C);
334 }
335
336 Constant* LLVMContext::getConstantExprNot(Constant* C) {
337   assert(C->getType()->isIntOrIntVector() &&
338          "Cannot NOT a nonintegral value!");
339   return getConstantExpr(Instruction::Xor, C, getAllOnesValue(C->getType()));
340 }
341
342 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
343   return getConstantExpr(Instruction::Add, C1, C2);
344 }
345
346 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
347   return getConstantExpr(Instruction::FAdd, C1, C2);
348 }
349
350 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
351   return getConstantExpr(Instruction::Sub, C1, C2);
352 }
353
354 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
355   return getConstantExpr(Instruction::FSub, C1, C2);
356 }
357
358 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
359   return getConstantExpr(Instruction::Mul, C1, C2);
360 }
361
362 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
363   return getConstantExpr(Instruction::FMul, C1, C2);
364 }
365
366 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
367   return getConstantExpr(Instruction::UDiv, C1, C2);
368 }
369
370 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
371   return getConstantExpr(Instruction::SDiv, C1, C2);
372 }
373
374 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
375   return getConstantExpr(Instruction::FDiv, C1, C2);
376 }
377
378 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
379   return getConstantExpr(Instruction::URem, C1, C2);
380 }
381
382 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
383   return getConstantExpr(Instruction::SRem, C1, C2);
384 }
385
386 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
387   return getConstantExpr(Instruction::FRem, C1, C2);
388 }
389
390 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
391   return getConstantExpr(Instruction::And, C1, C2);
392 }
393
394 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
395   return getConstantExpr(Instruction::Or, C1, C2);
396 }
397
398 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
399   return getConstantExpr(Instruction::Xor, C1, C2);
400 }
401
402 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
403                               Constant* RHS) {
404   return ConstantExpr::getICmp(pred, LHS, RHS);
405 }
406
407 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
408                               Constant* RHS) {
409   return ConstantExpr::getFCmp(pred, LHS, RHS);
410 }
411
412 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
413   return getConstantExpr(Instruction::Shl, C1, C2);
414 }
415
416 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
417   return getConstantExpr(Instruction::LShr, C1, C2);
418 }
419
420 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
421   return getConstantExpr(Instruction::AShr, C1, C2);
422 }
423
424 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
425                                                     Constant* const* IdxList, 
426                                                     unsigned NumIdx) {
427   return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
428 }
429
430 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
431                                                     Value* const* IdxList, 
432                                                     unsigned NumIdx) {
433   return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
434 }
435
436 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
437                                                      Constant* Idx) {
438   return ConstantExpr::getExtractElement(Vec, Idx);
439 }
440
441 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
442                                                     Constant* Elt,
443                                                     Constant* Idx) {
444   return ConstantExpr::getInsertElement(Vec, Elt, Idx);
445 }
446
447 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
448                                                     Constant* Mask) {
449   return ConstantExpr::getShuffleVector(V1, V2, Mask);
450 }
451
452 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
453                                                    const unsigned* IdxList, 
454                                                    unsigned NumIdx) {
455   return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
456 }
457
458 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
459                                                   const unsigned* IdxList,
460                                                   unsigned NumIdx) {
461   return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
462 }
463
464 Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
465   // sizeof is implemented as: (i64) gep (Ty*)null, 1
466   Constant *GEPIdx = getConstantInt(Type::Int32Ty, 1);
467   Constant *GEP = getConstantExprGetElementPtr(
468                             getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
469   return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
470 }
471
472 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
473   if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
474     if (PTy->getElementType()->isFloatingPoint()) {
475       std::vector<Constant*> zeros(PTy->getNumElements(),
476                            getConstantFPNegativeZero(PTy->getElementType()));
477       return getConstantVector(PTy, zeros);
478     }
479
480   if (Ty->isFloatingPoint()) 
481     return getConstantFPNegativeZero(Ty);
482
483   return getNullValue(Ty);
484 }
485
486
487 // ConstantFP accessors.
488 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
489   return pImpl->getConstantFP(V);
490 }
491
492 static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
493   if (Ty == Type::FloatTy)
494     return &APFloat::IEEEsingle;
495   if (Ty == Type::DoubleTy)
496     return &APFloat::IEEEdouble;
497   if (Ty == Type::X86_FP80Ty)
498     return &APFloat::x87DoubleExtended;
499   else if (Ty == Type::FP128Ty)
500     return &APFloat::IEEEquad;
501   
502   assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
503   return &APFloat::PPCDoubleDouble;
504 }
505
506 /// get() - This returns a constant fp for the specified value in the
507 /// specified type.  This should only be used for simple constant values like
508 /// 2.0/1.0 etc, that are known-valid both as double and as the target format.
509 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
510   APFloat FV(V);
511   bool ignored;
512   FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
513              APFloat::rmNearestTiesToEven, &ignored);
514   Constant *C = getConstantFP(FV);
515
516   // For vectors, broadcast the value.
517   if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
518     return
519       getConstantVector(std::vector<Constant *>(VTy->getNumElements(), C));
520
521   return C;
522 }
523
524 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
525   APFloat apf = cast <ConstantFP>(getNullValue(Ty))->getValueAPF();
526   apf.changeSign();
527   return getConstantFP(apf);
528 }
529
530
531 // ConstantVector accessors.
532 Constant* LLVMContext::getConstantVector(const VectorType* T,
533                             const std::vector<Constant*>& V) {
534   return pImpl->getConstantVector(T, V);
535 }
536
537 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
538   assert(!V.empty() && "Cannot infer type if V is empty");
539   return getConstantVector(getVectorType(V.front()->getType(),V.size()), V);
540 }
541
542 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
543                                          unsigned NumVals) {
544   // FIXME: make this the primary ctor method.
545   return getConstantVector(std::vector<Constant*>(Vals, Vals+NumVals));
546 }
547
548 // MDNode accessors
549 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
550   return pImpl->getMDNode(Vals, NumVals);
551 }
552
553 // MDString accessors
554 MDString* LLVMContext::getMDString(const char *StrBegin, unsigned StrLength) {
555   return pImpl->getMDString(StrBegin, StrLength);
556 }
557
558 MDString* LLVMContext::getMDString(const std::string &Str) {
559   return getMDString(Str.data(), Str.size());
560 }
561
562 // FunctionType accessors
563 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
564   return FunctionType::get(Result, isVarArg);
565 }
566
567 FunctionType* LLVMContext::getFunctionType(const Type* Result,
568                                          const std::vector<const Type*>& Params,
569                                          bool isVarArg) {
570   return FunctionType::get(Result, Params, isVarArg);
571 }
572                                 
573 // IntegerType accessors
574 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
575   return IntegerType::get(NumBits);
576 }
577   
578 // OpaqueType accessors
579 OpaqueType* LLVMContext::getOpaqueType() {
580   return OpaqueType::get();
581 }
582
583 // StructType accessors
584 StructType* LLVMContext::getStructType(bool isPacked) {
585   return StructType::get(isPacked);
586 }
587
588 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
589                                        bool isPacked) {
590   return StructType::get(Params, isPacked);
591 }
592
593 StructType *LLVMContext::getStructType(const Type *type, ...) {
594   va_list ap;
595   std::vector<const llvm::Type*> StructFields;
596   va_start(ap, type);
597   while (type) {
598     StructFields.push_back(type);
599     type = va_arg(ap, llvm::Type*);
600   }
601   return StructType::get(StructFields);
602 }
603
604 // ArrayType accessors
605 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
606                                      uint64_t NumElements) {
607   return ArrayType::get(ElementType, NumElements);
608 }
609   
610 // PointerType accessors
611 PointerType* LLVMContext::getPointerType(const Type* ElementType,
612                                          unsigned AddressSpace) {
613   return PointerType::get(ElementType, AddressSpace);
614 }
615
616 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
617   return PointerType::getUnqual(ElementType);
618 }
619   
620 // VectorType accessors
621 VectorType* LLVMContext::getVectorType(const Type* ElementType,
622                                        unsigned NumElements) {
623   return VectorType::get(ElementType, NumElements);
624 }
625
626 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
627   return VectorType::getInteger(VTy);  
628 }
629
630 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
631   return VectorType::getExtendedElementVectorType(VTy);
632 }
633
634 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
635   return VectorType::getTruncatedElementVectorType(VTy);
636 }
637
638 const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
639   if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
640     return getVectorType(Type::Int1Ty, vt->getNumElements());
641   }
642   return Type::Int1Ty;
643 }
644
645 void LLVMContext::erase(MDString *M) {
646   pImpl->erase(M);
647 }
648
649 void LLVMContext::erase(MDNode *M) {
650   pImpl->erase(M);
651 }
652
653 void LLVMContext::erase(ConstantAggregateZero *Z) {
654   pImpl->erase(Z);
655 }
656
657 void LLVMContext::erase(ConstantArray *C) {
658   pImpl->erase(C);
659 }
660
661 void LLVMContext::erase(ConstantStruct *S) {
662   pImpl->erase(S);
663 }
664
665 void LLVMContext::erase(ConstantVector *V) {
666   pImpl->erase(V);
667 }
668
669 Constant *LLVMContext::replaceUsesOfWithOnConstant(ConstantArray *CA,
670                                                Value *From, Value *To, Use *U) {
671   return pImpl->replaceUsesOfWithOnConstant(CA, From, To, U);
672 }
673
674 Constant *LLVMContext::replaceUsesOfWithOnConstant(ConstantStruct *CS,
675                                                Value *From, Value *To, Use *U) {
676   return pImpl->replaceUsesOfWithOnConstant(CS, From, To, U);
677 }