a8a8928cdfe605bf2949bbe26ef2230eccb7de8e
[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()) { }
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   return Constant::getAllOnesValue(Ty);
68 }
69
70 // UndefValue accessors.
71 UndefValue* LLVMContext::getUndef(const Type* Ty) {
72   return UndefValue::get(Ty);
73 }
74
75 // ConstantInt accessors.
76 ConstantInt* LLVMContext::getConstantIntTrue() {
77   return ConstantInt::getTrue();
78 }
79
80 ConstantInt* LLVMContext::getConstantIntFalse() {
81   return ConstantInt::getFalse();
82 }
83
84 Constant* LLVMContext::getConstantInt(const Type* Ty, uint64_t V,
85                                          bool isSigned) {
86   return ConstantInt::get(Ty, V, isSigned);
87 }
88
89
90 ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
91                                          bool isSigned) {
92   return ConstantInt::get(Ty, V, isSigned);
93 }
94
95 ConstantInt* LLVMContext::getConstantIntSigned(const IntegerType* Ty,
96                                                int64_t V) {
97   return ConstantInt::getSigned(Ty, V);
98 }
99
100 ConstantInt* LLVMContext::getConstantInt(const APInt& V) {
101   return ConstantInt::get(V);
102 }
103
104 Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
105   return ConstantInt::get(Ty, V);
106 }
107
108 ConstantInt* LLVMContext::getConstantIntAllOnesValue(const Type* Ty) {
109   return ConstantInt::getAllOnesValue(Ty);
110 }
111
112
113 // ConstantPointerNull accessors.
114 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
115   return ConstantPointerNull::get(T);
116 }
117
118
119 // ConstantStruct accessors.
120 Constant* LLVMContext::getConstantStruct(const StructType* T,
121                                          const std::vector<Constant*>& V) {
122   return ConstantStruct::get(T, V);
123 }
124
125 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
126                                          bool Packed) {
127   return ConstantStruct::get(V, Packed);
128 }
129
130 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
131                                          unsigned NumVals, bool Packed) {
132   return ConstantStruct::get(Vals, NumVals, Packed);
133 }
134
135
136 // ConstantAggregateZero accessors.
137 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
138   return ConstantAggregateZero::get(Ty);
139 }
140
141
142 // ConstantArray accessors.
143 Constant* LLVMContext::getConstantArray(const ArrayType* T,
144                                         const std::vector<Constant*>& V) {
145   return ConstantArray::get(T, V);
146 }
147
148 Constant* LLVMContext::getConstantArray(const ArrayType* T,
149                                         Constant* const* Vals,
150                                         unsigned NumVals) {
151   return ConstantArray::get(T, Vals, NumVals);
152 }
153
154 Constant* LLVMContext::getConstantArray(const std::string& Initializer,
155                                         bool AddNull) {
156   return ConstantArray::get(Initializer, AddNull);
157 }
158
159
160 // ConstantExpr accessors.
161 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
162                                        Constant* C2) {
163   return ConstantExpr::get(Opcode, C1, C2);
164 }
165
166 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
167   return ConstantExpr::getTrunc(C, Ty);
168 }
169
170 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
171   return ConstantExpr::getSExt(C, Ty);
172 }
173
174 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
175   return ConstantExpr::getZExt(C, Ty);  
176 }
177
178 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
179   return ConstantExpr::getFPTrunc(C, Ty);
180 }
181
182 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
183   return ConstantExpr::getFPExtend(C, Ty);
184 }
185
186 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
187   return ConstantExpr::getUIToFP(C, Ty);
188 }
189
190 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
191   return ConstantExpr::getSIToFP(C, Ty);
192 }
193
194 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
195   return ConstantExpr::getFPToUI(C, Ty);
196 }
197
198 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
199   return ConstantExpr::getFPToSI(C, Ty);
200 }
201
202 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
203   return ConstantExpr::getPtrToInt(C, Ty);
204 }
205
206 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
207   return ConstantExpr::getIntToPtr(C, Ty);
208 }
209
210 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
211   return ConstantExpr::getBitCast(C, Ty);
212 }
213
214 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
215                                            const Type* Ty) {
216   return ConstantExpr::getCast(ops, C, Ty);
217 }
218
219 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
220                                                     const Type* Ty) {
221   return ConstantExpr::getZExtOrBitCast(C, Ty);
222 }
223
224 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
225                                                     const Type* Ty) {
226   return ConstantExpr::getSExtOrBitCast(C, Ty);
227 }
228
229 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
230                                                      const Type* Ty) {
231   return ConstantExpr::getTruncOrBitCast(C, Ty);  
232 }
233
234 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
235   return ConstantExpr::getPointerCast(C, Ty);
236 }
237
238 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
239                                                   bool isSigned) {
240   return ConstantExpr::getIntegerCast(C, Ty, isSigned);
241 }
242
243 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
244   return ConstantExpr::getFPCast(C, Ty);
245 }
246
247 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
248                                              Constant* V2) {
249   return ConstantExpr::getSelect(C, V1, V2);
250 }
251
252 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
253   // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
254   const Type *AligningTy = getStructType(Type::Int8Ty, Ty, NULL);
255   Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
256   Constant *Zero = getConstantInt(Type::Int32Ty, 0);
257   Constant *One = getConstantInt(Type::Int32Ty, 1);
258   Constant *Indices[2] = { Zero, One };
259   Constant *GEP = getConstantExprGetElementPtr(NullPtr, Indices, 2);
260   return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
261 }
262
263 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
264                                  Constant* C1, Constant* C2) {
265   return ConstantExpr::getCompare(pred, C1, C2);
266 }
267
268 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
269   // API compatibility: Adjust integer opcodes to floating-point opcodes.
270   if (C->getType()->isFPOrFPVector())
271     return getConstantExprFNeg(C);
272   assert(C->getType()->isIntOrIntVector() &&
273          "Cannot NEG a nonintegral value!");
274   return getConstantExpr(Instruction::Sub,
275              getZeroValueForNegation(C->getType()),
276              C);
277 }
278
279 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
280   assert(C->getType()->isFPOrFPVector() &&
281          "Cannot FNEG a non-floating-point value!");
282   return getConstantExpr(Instruction::FSub,
283              getZeroValueForNegation(C->getType()),
284              C);
285 }
286
287 Constant* LLVMContext::getConstantExprNot(Constant* C) {
288   return ConstantExpr::getNot(C);
289 }
290
291 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
292   return ConstantExpr::getAdd(C1, C2);
293 }
294
295 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
296   return ConstantExpr::getFAdd(C1, C2);
297 }
298
299 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
300   return ConstantExpr::getSub(C1, C2);
301 }
302
303 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
304   return ConstantExpr::getFSub(C1, C2);
305 }
306
307 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
308   return ConstantExpr::getMul(C1, C2);
309 }
310
311 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
312   return ConstantExpr::getFMul(C1, C2);
313 }
314
315 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
316   return ConstantExpr::getUDiv(C1, C2);
317 }
318
319 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
320   return ConstantExpr::getSDiv(C1, C2);
321 }
322
323 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
324   return ConstantExpr::getFDiv(C1, C2);
325 }
326
327 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
328   return ConstantExpr::getURem(C1, C2);
329 }
330
331 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
332   return ConstantExpr::getSRem(C1, C2);
333 }
334
335 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
336   return ConstantExpr::getFRem(C1, C2);
337 }
338
339 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
340   return ConstantExpr::getAnd(C1, C2);
341 }
342
343 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
344   return ConstantExpr::getOr(C1, C2);
345 }
346
347 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
348   return ConstantExpr::getXor(C1, C2);
349 }
350
351 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
352                               Constant* RHS) {
353   return ConstantExpr::getICmp(pred, LHS, RHS);
354 }
355
356 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
357                               Constant* RHS) {
358   return ConstantExpr::getFCmp(pred, LHS, RHS);
359 }
360
361 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
362   return ConstantExpr::getShl(C1, C2);
363 }
364
365 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
366   return ConstantExpr::getLShr(C1, C2);
367 }
368
369 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
370   return ConstantExpr::getAShr(C1, C2);
371 }
372
373 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
374                                                     Constant* const* IdxList, 
375                                                     unsigned NumIdx) {
376   return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
377 }
378
379 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
380                                                     Value* const* IdxList, 
381                                                     unsigned NumIdx) {
382   return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
383 }
384
385 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
386                                                      Constant* Idx) {
387   return ConstantExpr::getExtractElement(Vec, Idx);
388 }
389
390 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
391                                                     Constant* Elt,
392                                                     Constant* Idx) {
393   return ConstantExpr::getInsertElement(Vec, Elt, Idx);
394 }
395
396 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
397                                                     Constant* Mask) {
398   return ConstantExpr::getShuffleVector(V1, V2, Mask);
399 }
400
401 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
402                                                    const unsigned* IdxList, 
403                                                    unsigned NumIdx) {
404   return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
405 }
406
407 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
408                                                   const unsigned* IdxList,
409                                                   unsigned NumIdx) {
410   return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
411 }
412
413 Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
414   // sizeof is implemented as: (i64) gep (Ty*)null, 1
415   Constant *GEPIdx = getConstantInt(Type::Int32Ty, 1);
416   Constant *GEP = getConstantExprGetElementPtr(
417                             getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
418   return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
419 }
420
421 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
422   if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
423     if (PTy->getElementType()->isFloatingPoint()) {
424       std::vector<Constant*> zeros(PTy->getNumElements(),
425                            getConstantFPNegativeZero(PTy->getElementType()));
426       return getConstantVector(PTy, zeros);
427     }
428
429   if (Ty->isFloatingPoint()) 
430     return getConstantFPNegativeZero(Ty);
431
432   return getNullValue(Ty);
433 }
434
435
436 // ConstantFP accessors.
437 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
438   return ConstantFP::get(V);
439 }
440
441 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
442   return ConstantFP::get(Ty, V);
443 }
444
445 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
446   APFloat apf = cast <ConstantFP>(getNullValue(Ty))->getValueAPF();
447   apf.changeSign();
448   return getConstantFP(apf);
449 }
450
451
452 // ConstantVector accessors.
453 Constant* LLVMContext::getConstantVector(const VectorType* T,
454                             const std::vector<Constant*>& V) {
455   return ConstantVector::get(T, V);
456 }
457
458 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
459   return ConstantVector::get(V);
460 }
461
462 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
463                                          unsigned NumVals) {
464   return ConstantVector::get(Vals, NumVals);
465 }
466
467 ConstantVector* LLVMContext::getConstantVectorAllOnesValue(
468                                                          const VectorType* Ty) {
469   return ConstantVector::getAllOnesValue(Ty);
470 }
471
472 // MDNode accessors
473 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
474   return MDNode::get(Vals, NumVals);
475 }
476
477 // MDString accessors
478 MDString* LLVMContext::getMDString(const char *StrBegin, const char *StrEnd) {
479   return MDString::get(StrBegin, StrEnd);
480 }
481
482 MDString* LLVMContext::getMDString(const std::string &Str) {
483   return MDString::get(Str);
484 }
485
486 // FunctionType accessors
487 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
488   return FunctionType::get(Result, isVarArg);
489 }
490
491 FunctionType* LLVMContext::getFunctionType(const Type* Result,
492                                          const std::vector<const Type*>& Params,
493                                          bool isVarArg) {
494   return FunctionType::get(Result, Params, isVarArg);
495 }
496                                 
497 // IntegerType accessors
498 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
499   return IntegerType::get(NumBits);
500 }
501   
502 // OpaqueType accessors
503 OpaqueType* LLVMContext::getOpaqueType() {
504   return OpaqueType::get();
505 }
506
507 // StructType accessors
508 StructType* LLVMContext::getStructType(bool isPacked) {
509   return StructType::get(isPacked);
510 }
511
512 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
513                                        bool isPacked) {
514   return StructType::get(Params, isPacked);
515 }
516
517 StructType *LLVMContext::getStructType(const Type *type, ...) {
518   va_list ap;
519   std::vector<const llvm::Type*> StructFields;
520   va_start(ap, type);
521   while (type) {
522     StructFields.push_back(type);
523     type = va_arg(ap, llvm::Type*);
524   }
525   return StructType::get(StructFields);
526 }
527
528 // ArrayType accessors
529 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
530                                      uint64_t NumElements) {
531   return ArrayType::get(ElementType, NumElements);
532 }
533   
534 // PointerType accessors
535 PointerType* LLVMContext::getPointerType(const Type* ElementType,
536                                          unsigned AddressSpace) {
537   return PointerType::get(ElementType, AddressSpace);
538 }
539
540 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
541   return PointerType::getUnqual(ElementType);
542 }
543   
544 // VectorType accessors
545 VectorType* LLVMContext::getVectorType(const Type* ElementType,
546                                        unsigned NumElements) {
547   return VectorType::get(ElementType, NumElements);
548 }
549
550 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
551   return VectorType::getInteger(VTy);  
552 }
553
554 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
555   return VectorType::getExtendedElementVectorType(VTy);
556 }
557
558 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
559   return VectorType::getTruncatedElementVectorType(VTy);
560 }
561
562 const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
563   if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
564     return getVectorType(Type::Int1Ty, vt->getNumElements());
565   }
566   return Type::Int1Ty;
567 }