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 ConstantInt::get(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 ConstantInt::get(*this, 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 // ConstantPointerNull accessors.
96 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
97 return ConstantPointerNull::get(T);
101 // ConstantStruct accessors.
102 Constant* LLVMContext::getConstantStruct(const StructType* T,
103 const std::vector<Constant*>& V) {
104 return pImpl->getConstantStruct(T, V);
107 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
109 std::vector<const Type*> StructEls;
110 StructEls.reserve(V.size());
111 for (unsigned i = 0, e = V.size(); i != e; ++i)
112 StructEls.push_back(V[i]->getType());
113 return getConstantStruct(getStructType(StructEls, packed), V);
116 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
117 unsigned NumVals, bool Packed) {
118 // FIXME: make this the primary ctor method.
119 return getConstantStruct(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
123 // ConstantAggregateZero accessors.
124 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
125 return pImpl->getConstantAggregateZero(Ty);
129 // ConstantArray accessors.
130 Constant* LLVMContext::getConstantArray(const ArrayType* T,
131 const std::vector<Constant*>& V) {
132 return pImpl->getConstantArray(T, V);
135 Constant* LLVMContext::getConstantArray(const ArrayType* T,
136 Constant* const* Vals,
138 // FIXME: make this the primary ctor method.
139 return getConstantArray(T, std::vector<Constant*>(Vals, Vals+NumVals));
142 /// ConstantArray::get(const string&) - Return an array that is initialized to
143 /// contain the specified string. If length is zero then a null terminator is
144 /// added to the specified string so that it may be used in a natural way.
145 /// Otherwise, the length parameter specifies how much of the string to use
146 /// and it won't be null terminated.
148 Constant* LLVMContext::getConstantArray(const StringRef &Str,
150 std::vector<Constant*> ElementVals;
151 for (unsigned i = 0; i < Str.size(); ++i)
152 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
154 // Add a null terminator to the string...
156 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
159 ArrayType *ATy = getArrayType(Type::Int8Ty, ElementVals.size());
160 return getConstantArray(ATy, ElementVals);
164 // ConstantExpr accessors.
165 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
167 return ConstantExpr::get(Opcode, C1, C2);
170 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
171 return ConstantExpr::getTrunc(C, Ty);
174 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
175 return ConstantExpr::getSExt(C, Ty);
178 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
179 return ConstantExpr::getZExt(C, Ty);
182 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
183 return ConstantExpr::getFPTrunc(C, Ty);
186 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
187 return ConstantExpr::getFPExtend(C, Ty);
190 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
191 return ConstantExpr::getUIToFP(C, Ty);
194 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
195 return ConstantExpr::getSIToFP(C, Ty);
198 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
199 return ConstantExpr::getFPToUI(C, Ty);
202 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
203 return ConstantExpr::getFPToSI(C, Ty);
206 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
207 return ConstantExpr::getPtrToInt(C, Ty);
210 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
211 return ConstantExpr::getIntToPtr(C, Ty);
214 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
215 return ConstantExpr::getBitCast(C, Ty);
218 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
220 return ConstantExpr::getCast(ops, C, Ty);
223 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
225 return ConstantExpr::getZExtOrBitCast(C, Ty);
228 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
230 return ConstantExpr::getSExtOrBitCast(C, Ty);
233 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
235 return ConstantExpr::getTruncOrBitCast(C, Ty);
238 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
239 return ConstantExpr::getPointerCast(C, Ty);
242 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
244 return ConstantExpr::getIntegerCast(C, Ty, isSigned);
247 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
248 return ConstantExpr::getFPCast(C, Ty);
251 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
253 return ConstantExpr::getSelect(C, V1, V2);
256 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
257 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
258 const Type *AligningTy = getStructType(Type::Int8Ty, Ty, NULL);
259 Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
260 Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
261 Constant *One = ConstantInt::get(Type::Int32Ty, 1);
262 Constant *Indices[2] = { Zero, One };
263 Constant *GEP = getConstantExprGetElementPtr(NullPtr, Indices, 2);
264 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
267 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
268 Constant* C1, Constant* C2) {
269 return ConstantExpr::getCompare(pred, C1, C2);
272 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
273 // API compatibility: Adjust integer opcodes to floating-point opcodes.
274 if (C->getType()->isFPOrFPVector())
275 return getConstantExprFNeg(C);
276 assert(C->getType()->isIntOrIntVector() &&
277 "Cannot NEG a nonintegral value!");
278 return getConstantExpr(Instruction::Sub,
279 getZeroValueForNegation(C->getType()),
283 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
284 assert(C->getType()->isFPOrFPVector() &&
285 "Cannot FNEG a non-floating-point value!");
286 return getConstantExpr(Instruction::FSub,
287 getZeroValueForNegation(C->getType()),
291 Constant* LLVMContext::getConstantExprNot(Constant* C) {
292 assert(C->getType()->isIntOrIntVector() &&
293 "Cannot NOT a nonintegral value!");
294 return getConstantExpr(Instruction::Xor, C, getAllOnesValue(C->getType()));
297 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
298 return getConstantExpr(Instruction::Add, C1, C2);
301 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
302 return getConstantExpr(Instruction::FAdd, C1, C2);
305 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
306 return getConstantExpr(Instruction::Sub, C1, C2);
309 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
310 return getConstantExpr(Instruction::FSub, C1, C2);
313 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
314 return getConstantExpr(Instruction::Mul, C1, C2);
317 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
318 return getConstantExpr(Instruction::FMul, C1, C2);
321 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
322 return getConstantExpr(Instruction::UDiv, C1, C2);
325 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
326 return getConstantExpr(Instruction::SDiv, C1, C2);
329 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
330 return getConstantExpr(Instruction::FDiv, C1, C2);
333 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
334 return getConstantExpr(Instruction::URem, C1, C2);
337 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
338 return getConstantExpr(Instruction::SRem, C1, C2);
341 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
342 return getConstantExpr(Instruction::FRem, C1, C2);
345 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
346 return getConstantExpr(Instruction::And, C1, C2);
349 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
350 return getConstantExpr(Instruction::Or, C1, C2);
353 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
354 return getConstantExpr(Instruction::Xor, C1, C2);
357 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
359 return ConstantExpr::getICmp(pred, LHS, RHS);
362 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
364 return ConstantExpr::getFCmp(pred, LHS, RHS);
367 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
368 return getConstantExpr(Instruction::Shl, C1, C2);
371 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
372 return getConstantExpr(Instruction::LShr, C1, C2);
375 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
376 return getConstantExpr(Instruction::AShr, C1, C2);
379 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
380 Constant* const* IdxList,
382 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
385 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
386 Value* const* IdxList,
388 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
391 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
393 return ConstantExpr::getExtractElement(Vec, Idx);
396 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
399 return ConstantExpr::getInsertElement(Vec, Elt, Idx);
402 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
404 return ConstantExpr::getShuffleVector(V1, V2, Mask);
407 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
408 const unsigned* IdxList,
410 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
413 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
414 const unsigned* IdxList,
416 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
419 Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
420 // sizeof is implemented as: (i64) gep (Ty*)null, 1
421 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
422 Constant *GEP = getConstantExprGetElementPtr(
423 getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
424 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
427 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
428 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
429 if (PTy->getElementType()->isFloatingPoint()) {
430 std::vector<Constant*> zeros(PTy->getNumElements(),
431 getConstantFPNegativeZero(PTy->getElementType()));
432 return getConstantVector(PTy, zeros);
435 if (Ty->isFloatingPoint())
436 return getConstantFPNegativeZero(Ty);
438 return getNullValue(Ty);
442 // ConstantFP accessors.
443 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
444 return pImpl->getConstantFP(V);
447 static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
448 if (Ty == Type::FloatTy)
449 return &APFloat::IEEEsingle;
450 if (Ty == Type::DoubleTy)
451 return &APFloat::IEEEdouble;
452 if (Ty == Type::X86_FP80Ty)
453 return &APFloat::x87DoubleExtended;
454 else if (Ty == Type::FP128Ty)
455 return &APFloat::IEEEquad;
457 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
458 return &APFloat::PPCDoubleDouble;
461 /// get() - This returns a constant fp for the specified value in the
462 /// specified type. This should only be used for simple constant values like
463 /// 2.0/1.0 etc, that are known-valid both as double and as the target format.
464 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
467 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
468 APFloat::rmNearestTiesToEven, &ignored);
469 Constant *C = getConstantFP(FV);
471 // For vectors, broadcast the value.
472 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
474 getConstantVector(std::vector<Constant *>(VTy->getNumElements(), C));
479 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
480 APFloat apf = cast <ConstantFP>(getNullValue(Ty))->getValueAPF();
482 return getConstantFP(apf);
486 // ConstantVector accessors.
487 Constant* LLVMContext::getConstantVector(const VectorType* T,
488 const std::vector<Constant*>& V) {
489 return pImpl->getConstantVector(T, V);
492 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
493 assert(!V.empty() && "Cannot infer type if V is empty");
494 return getConstantVector(getVectorType(V.front()->getType(),V.size()), V);
497 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
499 // FIXME: make this the primary ctor method.
500 return getConstantVector(std::vector<Constant*>(Vals, Vals+NumVals));
504 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
505 return pImpl->getMDNode(Vals, NumVals);
508 // MDString accessors
509 MDString* LLVMContext::getMDString(const StringRef &Str) {
510 return pImpl->getMDString(Str.data(), Str.size());
513 // FunctionType accessors
514 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
515 return FunctionType::get(Result, isVarArg);
518 FunctionType* LLVMContext::getFunctionType(const Type* Result,
519 const std::vector<const Type*>& Params,
521 return FunctionType::get(Result, Params, isVarArg);
524 // IntegerType accessors
525 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
526 return IntegerType::get(NumBits);
529 // OpaqueType accessors
530 OpaqueType* LLVMContext::getOpaqueType() {
531 return OpaqueType::get();
534 // StructType accessors
535 StructType* LLVMContext::getStructType(bool isPacked) {
536 return StructType::get(isPacked);
539 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
541 return StructType::get(Params, isPacked);
544 StructType *LLVMContext::getStructType(const Type *type, ...) {
546 std::vector<const llvm::Type*> StructFields;
549 StructFields.push_back(type);
550 type = va_arg(ap, llvm::Type*);
552 return StructType::get(StructFields);
555 // ArrayType accessors
556 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
557 uint64_t NumElements) {
558 return ArrayType::get(ElementType, NumElements);
561 // PointerType accessors
562 PointerType* LLVMContext::getPointerType(const Type* ElementType,
563 unsigned AddressSpace) {
564 return PointerType::get(ElementType, AddressSpace);
567 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
568 return PointerType::getUnqual(ElementType);
571 // VectorType accessors
572 VectorType* LLVMContext::getVectorType(const Type* ElementType,
573 unsigned NumElements) {
574 return VectorType::get(ElementType, NumElements);
577 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
578 return VectorType::getInteger(VTy);
581 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
582 return VectorType::getExtendedElementVectorType(VTy);
585 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
586 return VectorType::getTruncatedElementVectorType(VTy);
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());
596 void LLVMContext::erase(MDString *M) {
600 void LLVMContext::erase(MDNode *M) {
604 void LLVMContext::erase(ConstantAggregateZero *Z) {
608 void LLVMContext::erase(ConstantArray *C) {
612 void LLVMContext::erase(ConstantStruct *S) {
616 void LLVMContext::erase(ConstantVector *V) {
620 Constant *LLVMContext::replaceUsesOfWithOnConstant(ConstantArray *CA,
621 Value *From, Value *To, Use *U) {
622 return pImpl->replaceUsesOfWithOnConstant(CA, From, To, U);
625 Constant *LLVMContext::replaceUsesOfWithOnConstant(ConstantStruct *CS,
626 Value *From, Value *To, Use *U) {
627 return pImpl->replaceUsesOfWithOnConstant(CS, From, To, U);