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