Return ConstantVector to 2.5 API.
[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 ConstantInt::get(Ty, 0);
43   case Type::FloatTyID:
44     return ConstantFP::get(Ty->getContext(), APFloat(APInt(32, 0)));
45   case Type::DoubleTyID:
46     return ConstantFP::get(Ty->getContext(), APFloat(APInt(64, 0)));
47   case Type::X86_FP80TyID:
48     return ConstantFP::get(Ty->getContext(), APFloat(APInt(80, 2, zero)));
49   case Type::FP128TyID:
50     return ConstantFP::get(Ty->getContext(),
51                            APFloat(APInt(128, 2, zero), true));
52   case Type::PPC_FP128TyID:
53     return ConstantFP::get(Ty->getContext(), APFloat(APInt(128, 2, zero)));
54   case Type::PointerTyID:
55     return getConstantPointerNull(cast<PointerType>(Ty));
56   case Type::StructTyID:
57   case Type::ArrayTyID:
58   case Type::VectorTyID:
59     return getConstantAggregateZero(Ty);
60   default:
61     // Function, Label, or Opaque type?
62     assert(!"Cannot create a null constant of that type!");
63     return 0;
64   }
65 }
66
67 Constant* LLVMContext::getAllOnesValue(const Type* Ty) {
68   if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
69     return ConstantInt::get(*this, APInt::getAllOnesValue(ITy->getBitWidth()));
70   
71   std::vector<Constant*> Elts;
72   const VectorType* VTy = cast<VectorType>(Ty);
73   Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
74   assert(Elts[0] && "Not a vector integer type!");
75   return cast<ConstantVector>(ConstantVector::get(Elts));
76 }
77
78 // UndefValue accessors.
79 UndefValue* LLVMContext::getUndef(const Type* Ty) {
80   return UndefValue::get(Ty);
81 }
82
83 // ConstantInt accessors.
84 ConstantInt* LLVMContext::getTrue() {
85   assert(this && "Context not initialized!");
86   assert(pImpl && "Context not initialized!");
87   return pImpl->getTrue();
88 }
89
90 ConstantInt* LLVMContext::getFalse() {
91   assert(this && "Context not initialized!");
92   assert(pImpl && "Context not initialized!");
93   return pImpl->getFalse();
94 }
95
96 // ConstantPointerNull accessors.
97 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
98   return ConstantPointerNull::get(T);
99 }
100
101 // ConstantAggregateZero accessors.
102 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
103   return pImpl->getConstantAggregateZero(Ty);
104 }
105
106 // ConstantExpr accessors.
107 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
108                                        Constant* C2) {
109   return ConstantExpr::get(Opcode, C1, C2);
110 }
111
112 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
113   return ConstantExpr::getTrunc(C, Ty);
114 }
115
116 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
117   return ConstantExpr::getSExt(C, Ty);
118 }
119
120 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
121   return ConstantExpr::getZExt(C, Ty);  
122 }
123
124 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
125   return ConstantExpr::getFPTrunc(C, Ty);
126 }
127
128 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
129   return ConstantExpr::getFPExtend(C, Ty);
130 }
131
132 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
133   return ConstantExpr::getUIToFP(C, Ty);
134 }
135
136 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
137   return ConstantExpr::getSIToFP(C, Ty);
138 }
139
140 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
141   return ConstantExpr::getFPToUI(C, Ty);
142 }
143
144 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
145   return ConstantExpr::getFPToSI(C, Ty);
146 }
147
148 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
149   return ConstantExpr::getPtrToInt(C, Ty);
150 }
151
152 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
153   return ConstantExpr::getIntToPtr(C, Ty);
154 }
155
156 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
157   return ConstantExpr::getBitCast(C, Ty);
158 }
159
160 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
161                                            const Type* Ty) {
162   return ConstantExpr::getCast(ops, C, Ty);
163 }
164
165 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
166                                                     const Type* Ty) {
167   return ConstantExpr::getZExtOrBitCast(C, Ty);
168 }
169
170 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
171                                                     const Type* Ty) {
172   return ConstantExpr::getSExtOrBitCast(C, Ty);
173 }
174
175 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
176                                                      const Type* Ty) {
177   return ConstantExpr::getTruncOrBitCast(C, Ty);  
178 }
179
180 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
181   return ConstantExpr::getPointerCast(C, Ty);
182 }
183
184 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
185                                                   bool isSigned) {
186   return ConstantExpr::getIntegerCast(C, Ty, isSigned);
187 }
188
189 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
190   return ConstantExpr::getFPCast(C, Ty);
191 }
192
193 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
194                                              Constant* V2) {
195   return ConstantExpr::getSelect(C, V1, V2);
196 }
197
198 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
199   // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
200   const Type *AligningTy = getStructType(Type::Int8Ty, Ty, NULL);
201   Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
202   Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
203   Constant *One = ConstantInt::get(Type::Int32Ty, 1);
204   Constant *Indices[2] = { Zero, One };
205   Constant *GEP = getConstantExprGetElementPtr(NullPtr, Indices, 2);
206   return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
207 }
208
209 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
210                                  Constant* C1, Constant* C2) {
211   return ConstantExpr::getCompare(pred, C1, C2);
212 }
213
214 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
215   // API compatibility: Adjust integer opcodes to floating-point opcodes.
216   if (C->getType()->isFPOrFPVector())
217     return getConstantExprFNeg(C);
218   assert(C->getType()->isIntOrIntVector() &&
219          "Cannot NEG a nonintegral value!");
220   return getConstantExpr(Instruction::Sub,
221              ConstantFP::getZeroValueForNegation(C->getType()),
222              C);
223 }
224
225 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
226   assert(C->getType()->isFPOrFPVector() &&
227          "Cannot FNEG a non-floating-point value!");
228   return getConstantExpr(Instruction::FSub,
229              ConstantFP::getZeroValueForNegation(C->getType()),
230              C);
231 }
232
233 Constant* LLVMContext::getConstantExprNot(Constant* C) {
234   assert(C->getType()->isIntOrIntVector() &&
235          "Cannot NOT a nonintegral value!");
236   return getConstantExpr(Instruction::Xor, C, getAllOnesValue(C->getType()));
237 }
238
239 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
240   return getConstantExpr(Instruction::Add, C1, C2);
241 }
242
243 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
244   return getConstantExpr(Instruction::FAdd, C1, C2);
245 }
246
247 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
248   return getConstantExpr(Instruction::Sub, C1, C2);
249 }
250
251 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
252   return getConstantExpr(Instruction::FSub, C1, C2);
253 }
254
255 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
256   return getConstantExpr(Instruction::Mul, C1, C2);
257 }
258
259 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
260   return getConstantExpr(Instruction::FMul, C1, C2);
261 }
262
263 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
264   return getConstantExpr(Instruction::UDiv, C1, C2);
265 }
266
267 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
268   return getConstantExpr(Instruction::SDiv, C1, C2);
269 }
270
271 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
272   return getConstantExpr(Instruction::FDiv, C1, C2);
273 }
274
275 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
276   return getConstantExpr(Instruction::URem, C1, C2);
277 }
278
279 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
280   return getConstantExpr(Instruction::SRem, C1, C2);
281 }
282
283 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
284   return getConstantExpr(Instruction::FRem, C1, C2);
285 }
286
287 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
288   return getConstantExpr(Instruction::And, C1, C2);
289 }
290
291 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
292   return getConstantExpr(Instruction::Or, C1, C2);
293 }
294
295 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
296   return getConstantExpr(Instruction::Xor, C1, C2);
297 }
298
299 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
300                               Constant* RHS) {
301   return ConstantExpr::getICmp(pred, LHS, RHS);
302 }
303
304 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
305                               Constant* RHS) {
306   return ConstantExpr::getFCmp(pred, LHS, RHS);
307 }
308
309 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
310   return getConstantExpr(Instruction::Shl, C1, C2);
311 }
312
313 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
314   return getConstantExpr(Instruction::LShr, C1, C2);
315 }
316
317 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
318   return getConstantExpr(Instruction::AShr, C1, C2);
319 }
320
321 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
322                                                     Constant* const* IdxList, 
323                                                     unsigned NumIdx) {
324   return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
325 }
326
327 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
328                                                     Value* const* IdxList, 
329                                                     unsigned NumIdx) {
330   return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
331 }
332
333 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
334                                                      Constant* Idx) {
335   return ConstantExpr::getExtractElement(Vec, Idx);
336 }
337
338 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
339                                                     Constant* Elt,
340                                                     Constant* Idx) {
341   return ConstantExpr::getInsertElement(Vec, Elt, Idx);
342 }
343
344 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
345                                                     Constant* Mask) {
346   return ConstantExpr::getShuffleVector(V1, V2, Mask);
347 }
348
349 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
350                                                    const unsigned* IdxList, 
351                                                    unsigned NumIdx) {
352   return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
353 }
354
355 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
356                                                   const unsigned* IdxList,
357                                                   unsigned NumIdx) {
358   return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
359 }
360
361 Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
362   // sizeof is implemented as: (i64) gep (Ty*)null, 1
363   // Note that a non-inbounds gep is used, as null isn't within any object.
364   Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
365   Constant *GEP = getConstantExprGetElementPtr(
366                             getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
367   return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
368 }
369
370 // MDNode accessors
371 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
372   return pImpl->getMDNode(Vals, NumVals);
373 }
374
375 // MDString accessors
376 MDString* LLVMContext::getMDString(const StringRef &Str) {
377   return pImpl->getMDString(Str.data(), Str.size());
378 }
379
380 // FunctionType accessors
381 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
382   return FunctionType::get(Result, isVarArg);
383 }
384
385 FunctionType* LLVMContext::getFunctionType(const Type* Result,
386                                          const std::vector<const Type*>& Params,
387                                          bool isVarArg) {
388   return FunctionType::get(Result, Params, isVarArg);
389 }
390                                 
391 // IntegerType accessors
392 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
393   return IntegerType::get(NumBits);
394 }
395   
396 // OpaqueType accessors
397 OpaqueType* LLVMContext::getOpaqueType() {
398   return OpaqueType::get();
399 }
400
401 // StructType accessors
402 StructType* LLVMContext::getStructType(bool isPacked) {
403   return StructType::get(isPacked);
404 }
405
406 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
407                                        bool isPacked) {
408   return StructType::get(Params, isPacked);
409 }
410
411 StructType *LLVMContext::getStructType(const Type *type, ...) {
412   va_list ap;
413   std::vector<const llvm::Type*> StructFields;
414   va_start(ap, type);
415   while (type) {
416     StructFields.push_back(type);
417     type = va_arg(ap, llvm::Type*);
418   }
419   return StructType::get(StructFields);
420 }
421
422 // ArrayType accessors
423 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
424                                      uint64_t NumElements) {
425   return ArrayType::get(ElementType, NumElements);
426 }
427   
428 // PointerType accessors
429 PointerType* LLVMContext::getPointerType(const Type* ElementType,
430                                          unsigned AddressSpace) {
431   return PointerType::get(ElementType, AddressSpace);
432 }
433
434 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
435   return PointerType::getUnqual(ElementType);
436 }
437   
438 // VectorType accessors
439 VectorType* LLVMContext::getVectorType(const Type* ElementType,
440                                        unsigned NumElements) {
441   return VectorType::get(ElementType, NumElements);
442 }
443
444 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
445   return VectorType::getInteger(VTy);  
446 }
447
448 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
449   return VectorType::getExtendedElementVectorType(VTy);
450 }
451
452 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
453   return VectorType::getTruncatedElementVectorType(VTy);
454 }
455
456 const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
457   if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
458     return getVectorType(Type::Int1Ty, vt->getNumElements());
459   }
460   return Type::Int1Ty;
461 }
462
463 void LLVMContext::erase(MDString *M) {
464   pImpl->erase(M);
465 }
466
467 void LLVMContext::erase(MDNode *M) {
468   pImpl->erase(M);
469 }
470
471 void LLVMContext::erase(ConstantAggregateZero *Z) {
472   pImpl->erase(Z);
473 }