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 ConstantFP::get(Ty->getContext(), APFloat(APInt(32, 0)));
45 case Type::DoubleTyID:
46 return ConstantFP::get(Ty->getContext(), APFloat(APInt(64, 0)));
47 case Type::X86_FP80TyID:
48 return ConstantFP::get(Ty->getContext(), APFloat(APInt(80, 2, zero)));
50 return ConstantFP::get(Ty->getContext(),
51 APFloat(APInt(128, 2, zero), true));
52 case Type::PPC_FP128TyID:
53 return ConstantFP::get(Ty->getContext(), APFloat(APInt(128, 2, zero)));
54 case Type::PointerTyID:
55 return getConstantPointerNull(cast<PointerType>(Ty));
56 case Type::StructTyID:
58 case Type::VectorTyID:
59 return getConstantAggregateZero(Ty);
61 // Function, Label, or Opaque type?
62 assert(!"Cannot create a null constant of that type!");
67 Constant* LLVMContext::getAllOnesValue(const Type* Ty) {
68 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
69 return ConstantInt::get(*this, APInt::getAllOnesValue(ITy->getBitWidth()));
71 std::vector<Constant*> Elts;
72 const VectorType* VTy = cast<VectorType>(Ty);
73 Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
74 assert(Elts[0] && "Not a vector integer type!");
75 return cast<ConstantVector>(getConstantVector(Elts));
78 // UndefValue accessors.
79 UndefValue* LLVMContext::getUndef(const Type* Ty) {
80 return UndefValue::get(Ty);
83 // ConstantInt accessors.
84 ConstantInt* LLVMContext::getTrue() {
85 assert(this && "Context not initialized!");
86 assert(pImpl && "Context not initialized!");
87 return pImpl->getTrue();
90 ConstantInt* LLVMContext::getFalse() {
91 assert(this && "Context not initialized!");
92 assert(pImpl && "Context not initialized!");
93 return pImpl->getFalse();
96 // ConstantPointerNull accessors.
97 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
98 return ConstantPointerNull::get(T);
102 // ConstantStruct accessors.
103 Constant* LLVMContext::getConstantStruct(const StructType* T,
104 const std::vector<Constant*>& V) {
105 return pImpl->getConstantStruct(T, V);
108 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
110 std::vector<const Type*> StructEls;
111 StructEls.reserve(V.size());
112 for (unsigned i = 0, e = V.size(); i != e; ++i)
113 StructEls.push_back(V[i]->getType());
114 return getConstantStruct(getStructType(StructEls, packed), V);
117 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
118 unsigned NumVals, bool Packed) {
119 // FIXME: make this the primary ctor method.
120 return getConstantStruct(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
124 // ConstantAggregateZero accessors.
125 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
126 return pImpl->getConstantAggregateZero(Ty);
130 // ConstantArray accessors.
131 Constant* LLVMContext::getConstantArray(const ArrayType* T,
132 const std::vector<Constant*>& V) {
133 return pImpl->getConstantArray(T, V);
136 Constant* LLVMContext::getConstantArray(const ArrayType* T,
137 Constant* const* Vals,
139 // FIXME: make this the primary ctor method.
140 return getConstantArray(T, std::vector<Constant*>(Vals, Vals+NumVals));
143 /// ConstantArray::get(const string&) - Return an array that is initialized to
144 /// contain the specified string. If length is zero then a null terminator is
145 /// added to the specified string so that it may be used in a natural way.
146 /// Otherwise, the length parameter specifies how much of the string to use
147 /// and it won't be null terminated.
149 Constant* LLVMContext::getConstantArray(const StringRef &Str,
151 std::vector<Constant*> ElementVals;
152 for (unsigned i = 0; i < Str.size(); ++i)
153 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
155 // Add a null terminator to the string...
157 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
160 ArrayType *ATy = getArrayType(Type::Int8Ty, ElementVals.size());
161 return getConstantArray(ATy, ElementVals);
165 // ConstantExpr accessors.
166 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
168 return ConstantExpr::get(Opcode, C1, C2);
171 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
172 return ConstantExpr::getTrunc(C, Ty);
175 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
176 return ConstantExpr::getSExt(C, Ty);
179 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
180 return ConstantExpr::getZExt(C, Ty);
183 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
184 return ConstantExpr::getFPTrunc(C, Ty);
187 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
188 return ConstantExpr::getFPExtend(C, Ty);
191 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
192 return ConstantExpr::getUIToFP(C, Ty);
195 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
196 return ConstantExpr::getSIToFP(C, Ty);
199 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
200 return ConstantExpr::getFPToUI(C, Ty);
203 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
204 return ConstantExpr::getFPToSI(C, Ty);
207 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
208 return ConstantExpr::getPtrToInt(C, Ty);
211 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
212 return ConstantExpr::getIntToPtr(C, Ty);
215 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
216 return ConstantExpr::getBitCast(C, Ty);
219 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
221 return ConstantExpr::getCast(ops, C, Ty);
224 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
226 return ConstantExpr::getZExtOrBitCast(C, Ty);
229 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
231 return ConstantExpr::getSExtOrBitCast(C, Ty);
234 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
236 return ConstantExpr::getTruncOrBitCast(C, Ty);
239 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
240 return ConstantExpr::getPointerCast(C, Ty);
243 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
245 return ConstantExpr::getIntegerCast(C, Ty, isSigned);
248 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
249 return ConstantExpr::getFPCast(C, Ty);
252 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
254 return ConstantExpr::getSelect(C, V1, V2);
257 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
258 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
259 const Type *AligningTy = getStructType(Type::Int8Ty, Ty, NULL);
260 Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
261 Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
262 Constant *One = ConstantInt::get(Type::Int32Ty, 1);
263 Constant *Indices[2] = { Zero, One };
264 Constant *GEP = getConstantExprGetElementPtr(NullPtr, Indices, 2);
265 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
268 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
269 Constant* C1, Constant* C2) {
270 return ConstantExpr::getCompare(pred, C1, C2);
273 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
274 // API compatibility: Adjust integer opcodes to floating-point opcodes.
275 if (C->getType()->isFPOrFPVector())
276 return getConstantExprFNeg(C);
277 assert(C->getType()->isIntOrIntVector() &&
278 "Cannot NEG a nonintegral value!");
279 return getConstantExpr(Instruction::Sub,
280 ConstantFP::getZeroValueForNegation(C->getType()),
284 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
285 assert(C->getType()->isFPOrFPVector() &&
286 "Cannot FNEG a non-floating-point value!");
287 return getConstantExpr(Instruction::FSub,
288 ConstantFP::getZeroValueForNegation(C->getType()),
292 Constant* LLVMContext::getConstantExprNot(Constant* C) {
293 assert(C->getType()->isIntOrIntVector() &&
294 "Cannot NOT a nonintegral value!");
295 return getConstantExpr(Instruction::Xor, C, getAllOnesValue(C->getType()));
298 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
299 return getConstantExpr(Instruction::Add, C1, C2);
302 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
303 return getConstantExpr(Instruction::FAdd, C1, C2);
306 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
307 return getConstantExpr(Instruction::Sub, C1, C2);
310 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
311 return getConstantExpr(Instruction::FSub, C1, C2);
314 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
315 return getConstantExpr(Instruction::Mul, C1, C2);
318 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
319 return getConstantExpr(Instruction::FMul, C1, C2);
322 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
323 return getConstantExpr(Instruction::UDiv, C1, C2);
326 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
327 return getConstantExpr(Instruction::SDiv, C1, C2);
330 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
331 return getConstantExpr(Instruction::FDiv, C1, C2);
334 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
335 return getConstantExpr(Instruction::URem, C1, C2);
338 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
339 return getConstantExpr(Instruction::SRem, C1, C2);
342 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
343 return getConstantExpr(Instruction::FRem, C1, C2);
346 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
347 return getConstantExpr(Instruction::And, C1, C2);
350 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
351 return getConstantExpr(Instruction::Or, C1, C2);
354 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
355 return getConstantExpr(Instruction::Xor, C1, C2);
358 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
360 return ConstantExpr::getICmp(pred, LHS, RHS);
363 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
365 return ConstantExpr::getFCmp(pred, LHS, RHS);
368 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
369 return getConstantExpr(Instruction::Shl, C1, C2);
372 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
373 return getConstantExpr(Instruction::LShr, C1, C2);
376 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
377 return getConstantExpr(Instruction::AShr, C1, C2);
380 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
381 Constant* const* IdxList,
383 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
386 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
387 Value* const* IdxList,
389 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
392 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
394 return ConstantExpr::getExtractElement(Vec, Idx);
397 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
400 return ConstantExpr::getInsertElement(Vec, Elt, Idx);
403 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
405 return ConstantExpr::getShuffleVector(V1, V2, Mask);
408 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
409 const unsigned* IdxList,
411 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
414 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
415 const unsigned* IdxList,
417 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
420 Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
421 // sizeof is implemented as: (i64) gep (Ty*)null, 1
422 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
423 Constant *GEP = getConstantExprGetElementPtr(
424 getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
425 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
428 // ConstantVector accessors.
429 Constant* LLVMContext::getConstantVector(const VectorType* T,
430 const std::vector<Constant*>& V) {
431 return pImpl->getConstantVector(T, V);
434 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
435 assert(!V.empty() && "Cannot infer type if V is empty");
436 return getConstantVector(getVectorType(V.front()->getType(),V.size()), V);
439 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
441 // FIXME: make this the primary ctor method.
442 return getConstantVector(std::vector<Constant*>(Vals, Vals+NumVals));
446 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
447 return pImpl->getMDNode(Vals, NumVals);
450 // MDString accessors
451 MDString* LLVMContext::getMDString(const StringRef &Str) {
452 return pImpl->getMDString(Str.data(), Str.size());
455 // FunctionType accessors
456 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
457 return FunctionType::get(Result, isVarArg);
460 FunctionType* LLVMContext::getFunctionType(const Type* Result,
461 const std::vector<const Type*>& Params,
463 return FunctionType::get(Result, Params, isVarArg);
466 // IntegerType accessors
467 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
468 return IntegerType::get(NumBits);
471 // OpaqueType accessors
472 OpaqueType* LLVMContext::getOpaqueType() {
473 return OpaqueType::get();
476 // StructType accessors
477 StructType* LLVMContext::getStructType(bool isPacked) {
478 return StructType::get(isPacked);
481 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
483 return StructType::get(Params, isPacked);
486 StructType *LLVMContext::getStructType(const Type *type, ...) {
488 std::vector<const llvm::Type*> StructFields;
491 StructFields.push_back(type);
492 type = va_arg(ap, llvm::Type*);
494 return StructType::get(StructFields);
497 // ArrayType accessors
498 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
499 uint64_t NumElements) {
500 return ArrayType::get(ElementType, NumElements);
503 // PointerType accessors
504 PointerType* LLVMContext::getPointerType(const Type* ElementType,
505 unsigned AddressSpace) {
506 return PointerType::get(ElementType, AddressSpace);
509 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
510 return PointerType::getUnqual(ElementType);
513 // VectorType accessors
514 VectorType* LLVMContext::getVectorType(const Type* ElementType,
515 unsigned NumElements) {
516 return VectorType::get(ElementType, NumElements);
519 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
520 return VectorType::getInteger(VTy);
523 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
524 return VectorType::getExtendedElementVectorType(VTy);
527 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
528 return VectorType::getTruncatedElementVectorType(VTy);
531 const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
532 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
533 return getVectorType(Type::Int1Ty, vt->getNumElements());
538 void LLVMContext::erase(MDString *M) {
542 void LLVMContext::erase(MDNode *M) {
546 void LLVMContext::erase(ConstantAggregateZero *Z) {
550 void LLVMContext::erase(ConstantArray *C) {
554 void LLVMContext::erase(ConstantStruct *S) {
558 void LLVMContext::erase(ConstantVector *V) {
562 Constant *LLVMContext::replaceUsesOfWithOnConstant(ConstantArray *CA,
563 Value *From, Value *To, Use *U) {
564 return pImpl->replaceUsesOfWithOnConstant(CA, From, To, U);
567 Constant *LLVMContext::replaceUsesOfWithOnConstant(ConstantStruct *CS,
568 Value *From, Value *To, Use *U) {
569 return pImpl->replaceUsesOfWithOnConstant(CS, From, To, U);