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(*this)) { }
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 pImpl->getConstantInt(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 getConstantVector(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 // FIXME: make this the primary ctor method.
180 return getConstantArray(T, std::vector<Constant*>(Vals, Vals+NumVals));
183 /// ConstantArray::get(const string&) - Return an array that is initialized to
184 /// contain the specified string. If length is zero then a null terminator is
185 /// added to the specified string so that it may be used in a natural way.
186 /// Otherwise, the length parameter specifies how much of the string to use
187 /// and it won't be null terminated.
189 Constant* LLVMContext::getConstantArray(const std::string& Str,
191 std::vector<Constant*> ElementVals;
192 for (unsigned i = 0; i < Str.length(); ++i)
193 ElementVals.push_back(getConstantInt(Type::Int8Ty, Str[i]));
195 // Add a null terminator to the string...
197 ElementVals.push_back(getConstantInt(Type::Int8Ty, 0));
200 ArrayType *ATy = getArrayType(Type::Int8Ty, ElementVals.size());
201 return getConstantArray(ATy, ElementVals);
205 // ConstantExpr accessors.
206 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
208 return ConstantExpr::get(Opcode, C1, C2);
211 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
212 return ConstantExpr::getTrunc(C, Ty);
215 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
216 return ConstantExpr::getSExt(C, Ty);
219 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
220 return ConstantExpr::getZExt(C, Ty);
223 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
224 return ConstantExpr::getFPTrunc(C, Ty);
227 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
228 return ConstantExpr::getFPExtend(C, Ty);
231 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
232 return ConstantExpr::getUIToFP(C, Ty);
235 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
236 return ConstantExpr::getSIToFP(C, Ty);
239 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
240 return ConstantExpr::getFPToUI(C, Ty);
243 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
244 return ConstantExpr::getFPToSI(C, Ty);
247 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
248 return ConstantExpr::getPtrToInt(C, Ty);
251 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
252 return ConstantExpr::getIntToPtr(C, Ty);
255 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
256 return ConstantExpr::getBitCast(C, Ty);
259 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
261 return ConstantExpr::getCast(ops, C, Ty);
264 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
266 return ConstantExpr::getZExtOrBitCast(C, Ty);
269 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
271 return ConstantExpr::getSExtOrBitCast(C, Ty);
274 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
276 return ConstantExpr::getTruncOrBitCast(C, Ty);
279 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
280 return ConstantExpr::getPointerCast(C, Ty);
283 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
285 return ConstantExpr::getIntegerCast(C, Ty, isSigned);
288 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
289 return ConstantExpr::getFPCast(C, Ty);
292 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
294 return ConstantExpr::getSelect(C, V1, V2);
297 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
298 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
299 const Type *AligningTy = getStructType(Type::Int8Ty, Ty, NULL);
300 Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
301 Constant *Zero = getConstantInt(Type::Int32Ty, 0);
302 Constant *One = getConstantInt(Type::Int32Ty, 1);
303 Constant *Indices[2] = { Zero, One };
304 Constant *GEP = getConstantExprGetElementPtr(NullPtr, Indices, 2);
305 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
308 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
309 Constant* C1, Constant* C2) {
310 return ConstantExpr::getCompare(pred, C1, C2);
313 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
314 // API compatibility: Adjust integer opcodes to floating-point opcodes.
315 if (C->getType()->isFPOrFPVector())
316 return getConstantExprFNeg(C);
317 assert(C->getType()->isIntOrIntVector() &&
318 "Cannot NEG a nonintegral value!");
319 return getConstantExpr(Instruction::Sub,
320 getZeroValueForNegation(C->getType()),
324 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
325 assert(C->getType()->isFPOrFPVector() &&
326 "Cannot FNEG a non-floating-point value!");
327 return getConstantExpr(Instruction::FSub,
328 getZeroValueForNegation(C->getType()),
332 Constant* LLVMContext::getConstantExprNot(Constant* C) {
333 assert(C->getType()->isIntOrIntVector() &&
334 "Cannot NOT a nonintegral value!");
335 return getConstantExpr(Instruction::Xor, C, getAllOnesValue(C->getType()));
338 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
339 return getConstantExpr(Instruction::Add, C1, C2);
342 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
343 return getConstantExpr(Instruction::FAdd, C1, C2);
346 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
347 return getConstantExpr(Instruction::Sub, C1, C2);
350 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
351 return getConstantExpr(Instruction::FSub, C1, C2);
354 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
355 return getConstantExpr(Instruction::Mul, C1, C2);
358 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
359 return getConstantExpr(Instruction::FMul, C1, C2);
362 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
363 return getConstantExpr(Instruction::UDiv, C1, C2);
366 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
367 return getConstantExpr(Instruction::SDiv, C1, C2);
370 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
371 return getConstantExpr(Instruction::FDiv, C1, C2);
374 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
375 return getConstantExpr(Instruction::URem, C1, C2);
378 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
379 return getConstantExpr(Instruction::SRem, C1, C2);
382 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
383 return getConstantExpr(Instruction::FRem, C1, C2);
386 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
387 return getConstantExpr(Instruction::And, C1, C2);
390 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
391 return getConstantExpr(Instruction::Or, C1, C2);
394 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
395 return getConstantExpr(Instruction::Xor, C1, C2);
398 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
400 return ConstantExpr::getICmp(pred, LHS, RHS);
403 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
405 return ConstantExpr::getFCmp(pred, LHS, RHS);
408 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
409 return getConstantExpr(Instruction::Shl, C1, C2);
412 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
413 return getConstantExpr(Instruction::LShr, C1, C2);
416 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
417 return getConstantExpr(Instruction::AShr, C1, C2);
420 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
421 Constant* const* IdxList,
423 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
426 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
427 Value* const* IdxList,
429 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
432 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
434 return ConstantExpr::getExtractElement(Vec, Idx);
437 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
440 return ConstantExpr::getInsertElement(Vec, Elt, Idx);
443 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
445 return ConstantExpr::getShuffleVector(V1, V2, Mask);
448 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
449 const unsigned* IdxList,
451 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
454 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
455 const unsigned* IdxList,
457 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
460 Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
461 // sizeof is implemented as: (i64) gep (Ty*)null, 1
462 Constant *GEPIdx = getConstantInt(Type::Int32Ty, 1);
463 Constant *GEP = getConstantExprGetElementPtr(
464 getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
465 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
468 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
469 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
470 if (PTy->getElementType()->isFloatingPoint()) {
471 std::vector<Constant*> zeros(PTy->getNumElements(),
472 getConstantFPNegativeZero(PTy->getElementType()));
473 return getConstantVector(PTy, zeros);
476 if (Ty->isFloatingPoint())
477 return getConstantFPNegativeZero(Ty);
479 return getNullValue(Ty);
483 // ConstantFP accessors.
484 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
485 return ConstantFP::get(V);
488 static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
489 if (Ty == Type::FloatTy)
490 return &APFloat::IEEEsingle;
491 if (Ty == Type::DoubleTy)
492 return &APFloat::IEEEdouble;
493 if (Ty == Type::X86_FP80Ty)
494 return &APFloat::x87DoubleExtended;
495 else if (Ty == Type::FP128Ty)
496 return &APFloat::IEEEquad;
498 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
499 return &APFloat::PPCDoubleDouble;
502 /// get() - This returns a constant fp for the specified value in the
503 /// specified type. This should only be used for simple constant values like
504 /// 2.0/1.0 etc, that are known-valid both as double and as the target format.
505 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
508 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
509 APFloat::rmNearestTiesToEven, &ignored);
510 Constant *C = getConstantFP(FV);
512 // For vectors, broadcast the value.
513 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
515 getConstantVector(std::vector<Constant *>(VTy->getNumElements(), C));
520 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
521 APFloat apf = cast <ConstantFP>(getNullValue(Ty))->getValueAPF();
523 return getConstantFP(apf);
527 // ConstantVector accessors.
528 Constant* LLVMContext::getConstantVector(const VectorType* T,
529 const std::vector<Constant*>& V) {
530 return ConstantVector::get(T, V);
533 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
534 assert(!V.empty() && "Cannot infer type if V is empty");
535 return getConstantVector(getVectorType(V.front()->getType(),V.size()), V);
538 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
540 // FIXME: make this the primary ctor method.
541 return getConstantVector(std::vector<Constant*>(Vals, Vals+NumVals));
545 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
546 return MDNode::get(Vals, NumVals);
549 // MDString accessors
550 MDString* LLVMContext::getMDString(const char *StrBegin, const char *StrEnd) {
551 return MDString::get(StrBegin, StrEnd);
554 MDString* LLVMContext::getMDString(const std::string &Str) {
555 return MDString::get(Str);
558 // FunctionType accessors
559 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
560 return FunctionType::get(Result, isVarArg);
563 FunctionType* LLVMContext::getFunctionType(const Type* Result,
564 const std::vector<const Type*>& Params,
566 return FunctionType::get(Result, Params, isVarArg);
569 // IntegerType accessors
570 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
571 return IntegerType::get(NumBits);
574 // OpaqueType accessors
575 OpaqueType* LLVMContext::getOpaqueType() {
576 return OpaqueType::get();
579 // StructType accessors
580 StructType* LLVMContext::getStructType(bool isPacked) {
581 return StructType::get(isPacked);
584 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
586 return StructType::get(Params, isPacked);
589 StructType *LLVMContext::getStructType(const Type *type, ...) {
591 std::vector<const llvm::Type*> StructFields;
594 StructFields.push_back(type);
595 type = va_arg(ap, llvm::Type*);
597 return StructType::get(StructFields);
600 // ArrayType accessors
601 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
602 uint64_t NumElements) {
603 return ArrayType::get(ElementType, NumElements);
606 // PointerType accessors
607 PointerType* LLVMContext::getPointerType(const Type* ElementType,
608 unsigned AddressSpace) {
609 return PointerType::get(ElementType, AddressSpace);
612 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
613 return PointerType::getUnqual(ElementType);
616 // VectorType accessors
617 VectorType* LLVMContext::getVectorType(const Type* ElementType,
618 unsigned NumElements) {
619 return VectorType::get(ElementType, NumElements);
622 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
623 return VectorType::getInteger(VTy);
626 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
627 return VectorType::getExtendedElementVectorType(VTy);
630 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
631 return VectorType::getTruncatedElementVectorType(VTy);
634 const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
635 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
636 return getVectorType(Type::Int1Ty, vt->getNumElements());