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::getTrue() {
84 assert(this && "Context not initialized!");
85 assert(pImpl && "Context not initialized!");
86 return pImpl->getTrue();
89 ConstantInt* LLVMContext::getFalse() {
90 assert(this && "Context not initialized!");
91 assert(pImpl && "Context not initialized!");
92 return pImpl->getFalse();
95 Constant* LLVMContext::getConstantInt(const Type* Ty, uint64_t V,
97 Constant *C = getConstantInt(cast<IntegerType>(Ty->getScalarType()),
100 // For vectors, broadcast the value.
101 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
103 getConstantVector(std::vector<Constant *>(VTy->getNumElements(), C));
109 ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
111 return getConstantInt(APInt(Ty->getBitWidth(), V, isSigned));
114 ConstantInt* LLVMContext::getConstantIntSigned(const IntegerType* Ty,
116 return getConstantInt(Ty, V, true);
119 Constant *LLVMContext::getConstantIntSigned(const Type *Ty, int64_t V) {
120 return getConstantInt(Ty, V, true);
123 ConstantInt* LLVMContext::getConstantInt(const APInt& V) {
124 return pImpl->getConstantInt(V);
127 Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
128 ConstantInt *C = getConstantInt(V);
129 assert(C->getType() == Ty->getScalarType() &&
130 "ConstantInt type doesn't match the type implied by its value!");
132 // For vectors, broadcast the value.
133 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
135 getConstantVector(std::vector<Constant *>(VTy->getNumElements(), C));
140 // ConstantPointerNull accessors.
141 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
142 return ConstantPointerNull::get(T);
146 // ConstantStruct accessors.
147 Constant* LLVMContext::getConstantStruct(const StructType* T,
148 const std::vector<Constant*>& V) {
149 return pImpl->getConstantStruct(T, V);
152 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
154 std::vector<const Type*> StructEls;
155 StructEls.reserve(V.size());
156 for (unsigned i = 0, e = V.size(); i != e; ++i)
157 StructEls.push_back(V[i]->getType());
158 return getConstantStruct(getStructType(StructEls, packed), V);
161 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
162 unsigned NumVals, bool Packed) {
163 // FIXME: make this the primary ctor method.
164 return getConstantStruct(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
168 // ConstantAggregateZero accessors.
169 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
170 return pImpl->getConstantAggregateZero(Ty);
174 // ConstantArray accessors.
175 Constant* LLVMContext::getConstantArray(const ArrayType* T,
176 const std::vector<Constant*>& V) {
177 return pImpl->getConstantArray(T, V);
180 Constant* LLVMContext::getConstantArray(const ArrayType* T,
181 Constant* const* Vals,
183 // FIXME: make this the primary ctor method.
184 return getConstantArray(T, std::vector<Constant*>(Vals, Vals+NumVals));
187 /// ConstantArray::get(const string&) - Return an array that is initialized to
188 /// contain the specified string. If length is zero then a null terminator is
189 /// added to the specified string so that it may be used in a natural way.
190 /// Otherwise, the length parameter specifies how much of the string to use
191 /// and it won't be null terminated.
193 Constant* LLVMContext::getConstantArray(const std::string& Str,
195 std::vector<Constant*> ElementVals;
196 for (unsigned i = 0; i < Str.length(); ++i)
197 ElementVals.push_back(getConstantInt(Type::Int8Ty, Str[i]));
199 // Add a null terminator to the string...
201 ElementVals.push_back(getConstantInt(Type::Int8Ty, 0));
204 ArrayType *ATy = getArrayType(Type::Int8Ty, ElementVals.size());
205 return getConstantArray(ATy, ElementVals);
209 // ConstantExpr accessors.
210 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
212 return ConstantExpr::get(Opcode, C1, C2);
215 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
216 return ConstantExpr::getTrunc(C, Ty);
219 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
220 return ConstantExpr::getSExt(C, Ty);
223 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
224 return ConstantExpr::getZExt(C, Ty);
227 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
228 return ConstantExpr::getFPTrunc(C, Ty);
231 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
232 return ConstantExpr::getFPExtend(C, Ty);
235 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
236 return ConstantExpr::getUIToFP(C, Ty);
239 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
240 return ConstantExpr::getSIToFP(C, Ty);
243 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
244 return ConstantExpr::getFPToUI(C, Ty);
247 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
248 return ConstantExpr::getFPToSI(C, Ty);
251 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
252 return ConstantExpr::getPtrToInt(C, Ty);
255 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
256 return ConstantExpr::getIntToPtr(C, Ty);
259 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
260 return ConstantExpr::getBitCast(C, Ty);
263 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
265 return ConstantExpr::getCast(ops, C, Ty);
268 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
270 return ConstantExpr::getZExtOrBitCast(C, Ty);
273 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
275 return ConstantExpr::getSExtOrBitCast(C, Ty);
278 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
280 return ConstantExpr::getTruncOrBitCast(C, Ty);
283 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
284 return ConstantExpr::getPointerCast(C, Ty);
287 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
289 return ConstantExpr::getIntegerCast(C, Ty, isSigned);
292 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
293 return ConstantExpr::getFPCast(C, Ty);
296 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
298 return ConstantExpr::getSelect(C, V1, V2);
301 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
302 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
303 const Type *AligningTy = getStructType(Type::Int8Ty, Ty, NULL);
304 Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
305 Constant *Zero = getConstantInt(Type::Int32Ty, 0);
306 Constant *One = getConstantInt(Type::Int32Ty, 1);
307 Constant *Indices[2] = { Zero, One };
308 Constant *GEP = getConstantExprGetElementPtr(NullPtr, Indices, 2);
309 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
312 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
313 Constant* C1, Constant* C2) {
314 return ConstantExpr::getCompare(pred, C1, C2);
317 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
318 // API compatibility: Adjust integer opcodes to floating-point opcodes.
319 if (C->getType()->isFPOrFPVector())
320 return getConstantExprFNeg(C);
321 assert(C->getType()->isIntOrIntVector() &&
322 "Cannot NEG a nonintegral value!");
323 return getConstantExpr(Instruction::Sub,
324 getZeroValueForNegation(C->getType()),
328 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
329 assert(C->getType()->isFPOrFPVector() &&
330 "Cannot FNEG a non-floating-point value!");
331 return getConstantExpr(Instruction::FSub,
332 getZeroValueForNegation(C->getType()),
336 Constant* LLVMContext::getConstantExprNot(Constant* C) {
337 assert(C->getType()->isIntOrIntVector() &&
338 "Cannot NOT a nonintegral value!");
339 return getConstantExpr(Instruction::Xor, C, getAllOnesValue(C->getType()));
342 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
343 return getConstantExpr(Instruction::Add, C1, C2);
346 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
347 return getConstantExpr(Instruction::FAdd, C1, C2);
350 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
351 return getConstantExpr(Instruction::Sub, C1, C2);
354 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
355 return getConstantExpr(Instruction::FSub, C1, C2);
358 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
359 return getConstantExpr(Instruction::Mul, C1, C2);
362 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
363 return getConstantExpr(Instruction::FMul, C1, C2);
366 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
367 return getConstantExpr(Instruction::UDiv, C1, C2);
370 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
371 return getConstantExpr(Instruction::SDiv, C1, C2);
374 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
375 return getConstantExpr(Instruction::FDiv, C1, C2);
378 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
379 return getConstantExpr(Instruction::URem, C1, C2);
382 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
383 return getConstantExpr(Instruction::SRem, C1, C2);
386 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
387 return getConstantExpr(Instruction::FRem, C1, C2);
390 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
391 return getConstantExpr(Instruction::And, C1, C2);
394 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
395 return getConstantExpr(Instruction::Or, C1, C2);
398 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
399 return getConstantExpr(Instruction::Xor, C1, C2);
402 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
404 return ConstantExpr::getICmp(pred, LHS, RHS);
407 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
409 return ConstantExpr::getFCmp(pred, LHS, RHS);
412 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
413 return getConstantExpr(Instruction::Shl, C1, C2);
416 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
417 return getConstantExpr(Instruction::LShr, C1, C2);
420 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
421 return getConstantExpr(Instruction::AShr, C1, C2);
424 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
425 Constant* const* IdxList,
427 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
430 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
431 Value* const* IdxList,
433 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
436 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
438 return ConstantExpr::getExtractElement(Vec, Idx);
441 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
444 return ConstantExpr::getInsertElement(Vec, Elt, Idx);
447 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
449 return ConstantExpr::getShuffleVector(V1, V2, Mask);
452 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
453 const unsigned* IdxList,
455 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
458 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
459 const unsigned* IdxList,
461 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
464 Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
465 // sizeof is implemented as: (i64) gep (Ty*)null, 1
466 Constant *GEPIdx = getConstantInt(Type::Int32Ty, 1);
467 Constant *GEP = getConstantExprGetElementPtr(
468 getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
469 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
472 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
473 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
474 if (PTy->getElementType()->isFloatingPoint()) {
475 std::vector<Constant*> zeros(PTy->getNumElements(),
476 getConstantFPNegativeZero(PTy->getElementType()));
477 return getConstantVector(PTy, zeros);
480 if (Ty->isFloatingPoint())
481 return getConstantFPNegativeZero(Ty);
483 return getNullValue(Ty);
487 // ConstantFP accessors.
488 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
489 return pImpl->getConstantFP(V);
492 static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
493 if (Ty == Type::FloatTy)
494 return &APFloat::IEEEsingle;
495 if (Ty == Type::DoubleTy)
496 return &APFloat::IEEEdouble;
497 if (Ty == Type::X86_FP80Ty)
498 return &APFloat::x87DoubleExtended;
499 else if (Ty == Type::FP128Ty)
500 return &APFloat::IEEEquad;
502 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
503 return &APFloat::PPCDoubleDouble;
506 /// get() - This returns a constant fp for the specified value in the
507 /// specified type. This should only be used for simple constant values like
508 /// 2.0/1.0 etc, that are known-valid both as double and as the target format.
509 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
512 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
513 APFloat::rmNearestTiesToEven, &ignored);
514 Constant *C = getConstantFP(FV);
516 // For vectors, broadcast the value.
517 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
519 getConstantVector(std::vector<Constant *>(VTy->getNumElements(), C));
524 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
525 APFloat apf = cast <ConstantFP>(getNullValue(Ty))->getValueAPF();
527 return getConstantFP(apf);
531 // ConstantVector accessors.
532 Constant* LLVMContext::getConstantVector(const VectorType* T,
533 const std::vector<Constant*>& V) {
534 return pImpl->getConstantVector(T, V);
537 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
538 assert(!V.empty() && "Cannot infer type if V is empty");
539 return getConstantVector(getVectorType(V.front()->getType(),V.size()), V);
542 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
544 // FIXME: make this the primary ctor method.
545 return getConstantVector(std::vector<Constant*>(Vals, Vals+NumVals));
549 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
550 return pImpl->getMDNode(Vals, NumVals);
553 // MDString accessors
554 MDString* LLVMContext::getMDString(const char *StrBegin, unsigned StrLength) {
555 return pImpl->getMDString(StrBegin, StrLength);
558 MDString* LLVMContext::getMDString(const std::string &Str) {
559 return getMDString(Str.data(), Str.size());
562 // FunctionType accessors
563 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
564 return FunctionType::get(Result, isVarArg);
567 FunctionType* LLVMContext::getFunctionType(const Type* Result,
568 const std::vector<const Type*>& Params,
570 return FunctionType::get(Result, Params, isVarArg);
573 // IntegerType accessors
574 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
575 return IntegerType::get(NumBits);
578 // OpaqueType accessors
579 OpaqueType* LLVMContext::getOpaqueType() {
580 return OpaqueType::get();
583 // StructType accessors
584 StructType* LLVMContext::getStructType(bool isPacked) {
585 return StructType::get(isPacked);
588 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
590 return StructType::get(Params, isPacked);
593 StructType *LLVMContext::getStructType(const Type *type, ...) {
595 std::vector<const llvm::Type*> StructFields;
598 StructFields.push_back(type);
599 type = va_arg(ap, llvm::Type*);
601 return StructType::get(StructFields);
604 // ArrayType accessors
605 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
606 uint64_t NumElements) {
607 return ArrayType::get(ElementType, NumElements);
610 // PointerType accessors
611 PointerType* LLVMContext::getPointerType(const Type* ElementType,
612 unsigned AddressSpace) {
613 return PointerType::get(ElementType, AddressSpace);
616 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
617 return PointerType::getUnqual(ElementType);
620 // VectorType accessors
621 VectorType* LLVMContext::getVectorType(const Type* ElementType,
622 unsigned NumElements) {
623 return VectorType::get(ElementType, NumElements);
626 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
627 return VectorType::getInteger(VTy);
630 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
631 return VectorType::getExtendedElementVectorType(VTy);
634 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
635 return VectorType::getTruncatedElementVectorType(VTy);
638 const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
639 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
640 return getVectorType(Type::Int1Ty, vt->getNumElements());
645 void LLVMContext::erase(MDString *M) {
649 void LLVMContext::erase(MDNode *M) {
653 void LLVMContext::erase(ConstantAggregateZero *Z) {
657 void LLVMContext::erase(ConstantArray *C) {
661 void LLVMContext::erase(ConstantStruct *S) {
665 void LLVMContext::erase(ConstantVector *V) {
669 Constant *LLVMContext::replaceUsesOfWithOnConstant(ConstantArray *CA,
670 Value *From, Value *To, Use *U) {
671 return pImpl->replaceUsesOfWithOnConstant(CA, From, To, U);
674 Constant *LLVMContext::replaceUsesOfWithOnConstant(ConstantStruct *CS,
675 Value *From, Value *To, Use *U) {
676 return pImpl->replaceUsesOfWithOnConstant(CS, From, To, U);