1 //===-- LLVMContext.cpp - Implement LLVMContext -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements LLVMContext, as a wrapper around the opaque
11 // class LLVMContextImpl.
13 //===----------------------------------------------------------------------===//
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"
26 static ManagedStatic<LLVMContext> GlobalContext;
28 LLVMContext& llvm::getGlobalContext() {
29 return *GlobalContext;
32 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl()) { }
33 LLVMContext::~LLVMContext() { delete pImpl; }
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);
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)));
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:
57 case Type::VectorTyID:
58 return getConstantAggregateZero(Ty);
60 // Function, Label, or Opaque type?
61 assert(!"Cannot create a null constant of that type!");
66 Constant* LLVMContext::getAllOnesValue(const Type* Ty) {
67 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
68 return getConstantInt(APInt::getAllOnesValue(ITy->getBitWidth()));
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));
77 // UndefValue accessors.
78 UndefValue* LLVMContext::getUndef(const Type* Ty) {
79 return UndefValue::get(Ty);
82 // ConstantInt accessors.
83 ConstantInt* LLVMContext::getConstantIntTrue() {
84 return ConstantInt::getTrue();
87 ConstantInt* LLVMContext::getConstantIntFalse() {
88 return ConstantInt::getFalse();
91 Constant* LLVMContext::getConstantInt(const Type* Ty, uint64_t V,
93 Constant *C = getConstantInt(cast<IntegerType>(Ty->getScalarType()),
96 // For vectors, broadcast the value.
97 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
99 getConstantVector(std::vector<Constant *>(VTy->getNumElements(), C));
105 ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
107 return getConstantInt(APInt(Ty->getBitWidth(), V, isSigned));
110 ConstantInt* LLVMContext::getConstantIntSigned(const IntegerType* Ty,
112 return getConstantInt(Ty, V, true);
115 Constant *LLVMContext::getConstantIntSigned(const Type *Ty, int64_t V) {
116 return getConstantInt(Ty, V, true);
119 ConstantInt* LLVMContext::getConstantInt(const APInt& V) {
120 return ConstantInt::get(V);
123 Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
124 ConstantInt *C = getConstantInt(V);
125 assert(C->getType() == Ty->getScalarType() &&
126 "ConstantInt type doesn't match the type implied by its value!");
128 // For vectors, broadcast the value.
129 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
131 ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
136 // ConstantPointerNull accessors.
137 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
138 return ConstantPointerNull::get(T);
142 // ConstantStruct accessors.
143 Constant* LLVMContext::getConstantStruct(const StructType* T,
144 const std::vector<Constant*>& V) {
145 return ConstantStruct::get(T, V);
148 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
150 std::vector<const Type*> StructEls;
151 StructEls.reserve(V.size());
152 for (unsigned i = 0, e = V.size(); i != e; ++i)
153 StructEls.push_back(V[i]->getType());
154 return getConstantStruct(getStructType(StructEls, packed), V);
157 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
158 unsigned NumVals, bool Packed) {
159 // FIXME: make this the primary ctor method.
160 return getConstantStruct(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
164 // ConstantAggregateZero accessors.
165 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
166 return ConstantAggregateZero::get(Ty);
170 // ConstantArray accessors.
171 Constant* LLVMContext::getConstantArray(const ArrayType* T,
172 const std::vector<Constant*>& V) {
173 return ConstantArray::get(T, V);
176 Constant* LLVMContext::getConstantArray(const ArrayType* T,
177 Constant* const* Vals,
179 return ConstantArray::get(T, Vals, NumVals);
182 /// ConstantArray::get(const string&) - Return an array that is initialized to
183 /// contain the specified string. If length is zero then a null terminator is
184 /// added to the specified string so that it may be used in a natural way.
185 /// Otherwise, the length parameter specifies how much of the string to use
186 /// and it won't be null terminated.
188 Constant* LLVMContext::getConstantArray(const std::string& Str,
190 std::vector<Constant*> ElementVals;
191 for (unsigned i = 0; i < Str.length(); ++i)
192 ElementVals.push_back(getConstantInt(Type::Int8Ty, Str[i]));
194 // Add a null terminator to the string...
196 ElementVals.push_back(getConstantInt(Type::Int8Ty, 0));
199 ArrayType *ATy = getArrayType(Type::Int8Ty, ElementVals.size());
200 return getConstantArray(ATy, ElementVals);
204 // ConstantExpr accessors.
205 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
207 return ConstantExpr::get(Opcode, C1, C2);
210 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
211 return ConstantExpr::getTrunc(C, Ty);
214 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
215 return ConstantExpr::getSExt(C, Ty);
218 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
219 return ConstantExpr::getZExt(C, Ty);
222 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
223 return ConstantExpr::getFPTrunc(C, Ty);
226 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
227 return ConstantExpr::getFPExtend(C, Ty);
230 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
231 return ConstantExpr::getUIToFP(C, Ty);
234 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
235 return ConstantExpr::getSIToFP(C, Ty);
238 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
239 return ConstantExpr::getFPToUI(C, Ty);
242 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
243 return ConstantExpr::getFPToSI(C, Ty);
246 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
247 return ConstantExpr::getPtrToInt(C, Ty);
250 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
251 return ConstantExpr::getIntToPtr(C, Ty);
254 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
255 return ConstantExpr::getBitCast(C, Ty);
258 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
260 return ConstantExpr::getCast(ops, C, Ty);
263 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
265 return ConstantExpr::getZExtOrBitCast(C, Ty);
268 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
270 return ConstantExpr::getSExtOrBitCast(C, Ty);
273 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
275 return ConstantExpr::getTruncOrBitCast(C, Ty);
278 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
279 return ConstantExpr::getPointerCast(C, Ty);
282 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
284 return ConstantExpr::getIntegerCast(C, Ty, isSigned);
287 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
288 return ConstantExpr::getFPCast(C, Ty);
291 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
293 return ConstantExpr::getSelect(C, V1, V2);
296 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
297 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
298 const Type *AligningTy = getStructType(Type::Int8Ty, Ty, NULL);
299 Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
300 Constant *Zero = getConstantInt(Type::Int32Ty, 0);
301 Constant *One = getConstantInt(Type::Int32Ty, 1);
302 Constant *Indices[2] = { Zero, One };
303 Constant *GEP = getConstantExprGetElementPtr(NullPtr, Indices, 2);
304 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
307 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
308 Constant* C1, Constant* C2) {
309 return ConstantExpr::getCompare(pred, C1, C2);
312 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
313 // API compatibility: Adjust integer opcodes to floating-point opcodes.
314 if (C->getType()->isFPOrFPVector())
315 return getConstantExprFNeg(C);
316 assert(C->getType()->isIntOrIntVector() &&
317 "Cannot NEG a nonintegral value!");
318 return getConstantExpr(Instruction::Sub,
319 getZeroValueForNegation(C->getType()),
323 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
324 assert(C->getType()->isFPOrFPVector() &&
325 "Cannot FNEG a non-floating-point value!");
326 return getConstantExpr(Instruction::FSub,
327 getZeroValueForNegation(C->getType()),
331 Constant* LLVMContext::getConstantExprNot(Constant* C) {
332 assert(C->getType()->isIntOrIntVector() &&
333 "Cannot NOT a nonintegral value!");
334 return getConstantExpr(Instruction::Xor, C, getAllOnesValue(C->getType()));
337 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
338 return getConstantExpr(Instruction::Add, C1, C2);
341 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
342 return getConstantExpr(Instruction::FAdd, C1, C2);
345 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
346 return getConstantExpr(Instruction::Sub, C1, C2);
349 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
350 return getConstantExpr(Instruction::FSub, C1, C2);
353 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
354 return getConstantExpr(Instruction::Mul, C1, C2);
357 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
358 return getConstantExpr(Instruction::FMul, C1, C2);
361 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
362 return getConstantExpr(Instruction::UDiv, C1, C2);
365 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
366 return getConstantExpr(Instruction::SDiv, C1, C2);
369 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
370 return getConstantExpr(Instruction::FDiv, C1, C2);
373 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
374 return getConstantExpr(Instruction::URem, C1, C2);
377 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
378 return getConstantExpr(Instruction::SRem, C1, C2);
381 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
382 return getConstantExpr(Instruction::FRem, C1, C2);
385 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
386 return getConstantExpr(Instruction::And, C1, C2);
389 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
390 return getConstantExpr(Instruction::Or, C1, C2);
393 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
394 return getConstantExpr(Instruction::Xor, C1, C2);
397 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
399 return ConstantExpr::getICmp(pred, LHS, RHS);
402 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
404 return ConstantExpr::getFCmp(pred, LHS, RHS);
407 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
408 return getConstantExpr(Instruction::Shl, C1, C2);
411 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
412 return getConstantExpr(Instruction::LShr, C1, C2);
415 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
416 return getConstantExpr(Instruction::AShr, C1, C2);
419 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
420 Constant* const* IdxList,
422 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
425 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
426 Value* const* IdxList,
428 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
431 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
433 return ConstantExpr::getExtractElement(Vec, Idx);
436 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
439 return ConstantExpr::getInsertElement(Vec, Elt, Idx);
442 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
444 return ConstantExpr::getShuffleVector(V1, V2, Mask);
447 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
448 const unsigned* IdxList,
450 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
453 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
454 const unsigned* IdxList,
456 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
459 Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
460 // sizeof is implemented as: (i64) gep (Ty*)null, 1
461 Constant *GEPIdx = getConstantInt(Type::Int32Ty, 1);
462 Constant *GEP = getConstantExprGetElementPtr(
463 getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
464 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
467 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
468 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
469 if (PTy->getElementType()->isFloatingPoint()) {
470 std::vector<Constant*> zeros(PTy->getNumElements(),
471 getConstantFPNegativeZero(PTy->getElementType()));
472 return getConstantVector(PTy, zeros);
475 if (Ty->isFloatingPoint())
476 return getConstantFPNegativeZero(Ty);
478 return getNullValue(Ty);
482 // ConstantFP accessors.
483 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
484 return ConstantFP::get(V);
487 static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
488 if (Ty == Type::FloatTy)
489 return &APFloat::IEEEsingle;
490 if (Ty == Type::DoubleTy)
491 return &APFloat::IEEEdouble;
492 if (Ty == Type::X86_FP80Ty)
493 return &APFloat::x87DoubleExtended;
494 else if (Ty == Type::FP128Ty)
495 return &APFloat::IEEEquad;
497 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
498 return &APFloat::PPCDoubleDouble;
501 /// get() - This returns a constant fp for the specified value in the
502 /// specified type. This should only be used for simple constant values like
503 /// 2.0/1.0 etc, that are known-valid both as double and as the target format.
504 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
507 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
508 APFloat::rmNearestTiesToEven, &ignored);
509 Constant *C = getConstantFP(FV);
511 // For vectors, broadcast the value.
512 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
514 getConstantVector(std::vector<Constant *>(VTy->getNumElements(), C));
519 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
520 APFloat apf = cast <ConstantFP>(getNullValue(Ty))->getValueAPF();
522 return getConstantFP(apf);
526 // ConstantVector accessors.
527 Constant* LLVMContext::getConstantVector(const VectorType* T,
528 const std::vector<Constant*>& V) {
529 return ConstantVector::get(T, V);
532 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
533 return ConstantVector::get(V);
536 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
538 return ConstantVector::get(Vals, NumVals);
542 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
543 return MDNode::get(Vals, NumVals);
546 // MDString accessors
547 MDString* LLVMContext::getMDString(const char *StrBegin, const char *StrEnd) {
548 return MDString::get(StrBegin, StrEnd);
551 MDString* LLVMContext::getMDString(const std::string &Str) {
552 return MDString::get(Str);
555 // FunctionType accessors
556 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
557 return FunctionType::get(Result, isVarArg);
560 FunctionType* LLVMContext::getFunctionType(const Type* Result,
561 const std::vector<const Type*>& Params,
563 return FunctionType::get(Result, Params, isVarArg);
566 // IntegerType accessors
567 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
568 return IntegerType::get(NumBits);
571 // OpaqueType accessors
572 OpaqueType* LLVMContext::getOpaqueType() {
573 return OpaqueType::get();
576 // StructType accessors
577 StructType* LLVMContext::getStructType(bool isPacked) {
578 return StructType::get(isPacked);
581 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
583 return StructType::get(Params, isPacked);
586 StructType *LLVMContext::getStructType(const Type *type, ...) {
588 std::vector<const llvm::Type*> StructFields;
591 StructFields.push_back(type);
592 type = va_arg(ap, llvm::Type*);
594 return StructType::get(StructFields);
597 // ArrayType accessors
598 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
599 uint64_t NumElements) {
600 return ArrayType::get(ElementType, NumElements);
603 // PointerType accessors
604 PointerType* LLVMContext::getPointerType(const Type* ElementType,
605 unsigned AddressSpace) {
606 return PointerType::get(ElementType, AddressSpace);
609 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
610 return PointerType::getUnqual(ElementType);
613 // VectorType accessors
614 VectorType* LLVMContext::getVectorType(const Type* ElementType,
615 unsigned NumElements) {
616 return VectorType::get(ElementType, NumElements);
619 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
620 return VectorType::getInteger(VTy);
623 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
624 return VectorType::getExtendedElementVectorType(VTy);
627 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
628 return VectorType::getTruncatedElementVectorType(VTy);
631 const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
632 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
633 return getVectorType(Type::Int1Ty, vt->getNumElements());