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);
101 // ConstantAggregateZero accessors.
102 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
103 return pImpl->getConstantAggregateZero(Ty);
107 // ConstantArray accessors.
108 Constant* LLVMContext::getConstantArray(const ArrayType* T,
109 const std::vector<Constant*>& V) {
110 return pImpl->getConstantArray(T, V);
113 Constant* LLVMContext::getConstantArray(const ArrayType* T,
114 Constant* const* Vals,
116 // FIXME: make this the primary ctor method.
117 return getConstantArray(T, std::vector<Constant*>(Vals, Vals+NumVals));
120 /// ConstantArray::get(const string&) - Return an array that is initialized to
121 /// contain the specified string. If length is zero then a null terminator is
122 /// added to the specified string so that it may be used in a natural way.
123 /// Otherwise, the length parameter specifies how much of the string to use
124 /// and it won't be null terminated.
126 Constant* LLVMContext::getConstantArray(const StringRef &Str,
128 std::vector<Constant*> ElementVals;
129 for (unsigned i = 0; i < Str.size(); ++i)
130 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
132 // Add a null terminator to the string...
134 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
137 ArrayType *ATy = getArrayType(Type::Int8Ty, ElementVals.size());
138 return getConstantArray(ATy, ElementVals);
142 // ConstantExpr accessors.
143 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
145 return ConstantExpr::get(Opcode, C1, C2);
148 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
149 return ConstantExpr::getTrunc(C, Ty);
152 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
153 return ConstantExpr::getSExt(C, Ty);
156 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
157 return ConstantExpr::getZExt(C, Ty);
160 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
161 return ConstantExpr::getFPTrunc(C, Ty);
164 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
165 return ConstantExpr::getFPExtend(C, Ty);
168 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
169 return ConstantExpr::getUIToFP(C, Ty);
172 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
173 return ConstantExpr::getSIToFP(C, Ty);
176 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
177 return ConstantExpr::getFPToUI(C, Ty);
180 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
181 return ConstantExpr::getFPToSI(C, Ty);
184 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
185 return ConstantExpr::getPtrToInt(C, Ty);
188 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
189 return ConstantExpr::getIntToPtr(C, Ty);
192 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
193 return ConstantExpr::getBitCast(C, Ty);
196 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
198 return ConstantExpr::getCast(ops, C, Ty);
201 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
203 return ConstantExpr::getZExtOrBitCast(C, Ty);
206 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
208 return ConstantExpr::getSExtOrBitCast(C, Ty);
211 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
213 return ConstantExpr::getTruncOrBitCast(C, Ty);
216 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
217 return ConstantExpr::getPointerCast(C, Ty);
220 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
222 return ConstantExpr::getIntegerCast(C, Ty, isSigned);
225 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
226 return ConstantExpr::getFPCast(C, Ty);
229 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
231 return ConstantExpr::getSelect(C, V1, V2);
234 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
235 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
236 const Type *AligningTy = getStructType(Type::Int8Ty, Ty, NULL);
237 Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
238 Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
239 Constant *One = ConstantInt::get(Type::Int32Ty, 1);
240 Constant *Indices[2] = { Zero, One };
241 Constant *GEP = getConstantExprGetElementPtr(NullPtr, Indices, 2);
242 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
245 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
246 Constant* C1, Constant* C2) {
247 return ConstantExpr::getCompare(pred, C1, C2);
250 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
251 // API compatibility: Adjust integer opcodes to floating-point opcodes.
252 if (C->getType()->isFPOrFPVector())
253 return getConstantExprFNeg(C);
254 assert(C->getType()->isIntOrIntVector() &&
255 "Cannot NEG a nonintegral value!");
256 return getConstantExpr(Instruction::Sub,
257 ConstantFP::getZeroValueForNegation(C->getType()),
261 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
262 assert(C->getType()->isFPOrFPVector() &&
263 "Cannot FNEG a non-floating-point value!");
264 return getConstantExpr(Instruction::FSub,
265 ConstantFP::getZeroValueForNegation(C->getType()),
269 Constant* LLVMContext::getConstantExprNot(Constant* C) {
270 assert(C->getType()->isIntOrIntVector() &&
271 "Cannot NOT a nonintegral value!");
272 return getConstantExpr(Instruction::Xor, C, getAllOnesValue(C->getType()));
275 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
276 return getConstantExpr(Instruction::Add, C1, C2);
279 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
280 return getConstantExpr(Instruction::FAdd, C1, C2);
283 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
284 return getConstantExpr(Instruction::Sub, C1, C2);
287 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
288 return getConstantExpr(Instruction::FSub, C1, C2);
291 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
292 return getConstantExpr(Instruction::Mul, C1, C2);
295 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
296 return getConstantExpr(Instruction::FMul, C1, C2);
299 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
300 return getConstantExpr(Instruction::UDiv, C1, C2);
303 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
304 return getConstantExpr(Instruction::SDiv, C1, C2);
307 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
308 return getConstantExpr(Instruction::FDiv, C1, C2);
311 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
312 return getConstantExpr(Instruction::URem, C1, C2);
315 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
316 return getConstantExpr(Instruction::SRem, C1, C2);
319 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
320 return getConstantExpr(Instruction::FRem, C1, C2);
323 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
324 return getConstantExpr(Instruction::And, C1, C2);
327 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
328 return getConstantExpr(Instruction::Or, C1, C2);
331 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
332 return getConstantExpr(Instruction::Xor, C1, C2);
335 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
337 return ConstantExpr::getICmp(pred, LHS, RHS);
340 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
342 return ConstantExpr::getFCmp(pred, LHS, RHS);
345 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
346 return getConstantExpr(Instruction::Shl, C1, C2);
349 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
350 return getConstantExpr(Instruction::LShr, C1, C2);
353 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
354 return getConstantExpr(Instruction::AShr, C1, C2);
357 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
358 Constant* const* IdxList,
360 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
363 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
364 Value* const* IdxList,
366 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
369 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
371 return ConstantExpr::getExtractElement(Vec, Idx);
374 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
377 return ConstantExpr::getInsertElement(Vec, Elt, Idx);
380 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
382 return ConstantExpr::getShuffleVector(V1, V2, Mask);
385 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
386 const unsigned* IdxList,
388 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
391 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
392 const unsigned* IdxList,
394 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
397 Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
398 // sizeof is implemented as: (i64) gep (Ty*)null, 1
399 // Note that a non-inbounds gep is used, as null isn't within any object.
400 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
401 Constant *GEP = getConstantExprGetElementPtr(
402 getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
403 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
406 // ConstantVector accessors.
407 Constant* LLVMContext::getConstantVector(const VectorType* T,
408 const std::vector<Constant*>& V) {
409 return pImpl->getConstantVector(T, V);
412 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
413 assert(!V.empty() && "Cannot infer type if V is empty");
414 return getConstantVector(getVectorType(V.front()->getType(),V.size()), V);
417 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
419 // FIXME: make this the primary ctor method.
420 return getConstantVector(std::vector<Constant*>(Vals, Vals+NumVals));
424 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
425 return pImpl->getMDNode(Vals, NumVals);
428 // MDString accessors
429 MDString* LLVMContext::getMDString(const StringRef &Str) {
430 return pImpl->getMDString(Str.data(), Str.size());
433 // FunctionType accessors
434 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
435 return FunctionType::get(Result, isVarArg);
438 FunctionType* LLVMContext::getFunctionType(const Type* Result,
439 const std::vector<const Type*>& Params,
441 return FunctionType::get(Result, Params, isVarArg);
444 // IntegerType accessors
445 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
446 return IntegerType::get(NumBits);
449 // OpaqueType accessors
450 OpaqueType* LLVMContext::getOpaqueType() {
451 return OpaqueType::get();
454 // StructType accessors
455 StructType* LLVMContext::getStructType(bool isPacked) {
456 return StructType::get(isPacked);
459 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
461 return StructType::get(Params, isPacked);
464 StructType *LLVMContext::getStructType(const Type *type, ...) {
466 std::vector<const llvm::Type*> StructFields;
469 StructFields.push_back(type);
470 type = va_arg(ap, llvm::Type*);
472 return StructType::get(StructFields);
475 // ArrayType accessors
476 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
477 uint64_t NumElements) {
478 return ArrayType::get(ElementType, NumElements);
481 // PointerType accessors
482 PointerType* LLVMContext::getPointerType(const Type* ElementType,
483 unsigned AddressSpace) {
484 return PointerType::get(ElementType, AddressSpace);
487 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
488 return PointerType::getUnqual(ElementType);
491 // VectorType accessors
492 VectorType* LLVMContext::getVectorType(const Type* ElementType,
493 unsigned NumElements) {
494 return VectorType::get(ElementType, NumElements);
497 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
498 return VectorType::getInteger(VTy);
501 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
502 return VectorType::getExtendedElementVectorType(VTy);
505 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
506 return VectorType::getTruncatedElementVectorType(VTy);
509 const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
510 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
511 return getVectorType(Type::Int1Ty, vt->getNumElements());
516 void LLVMContext::erase(MDString *M) {
520 void LLVMContext::erase(MDNode *M) {
524 void LLVMContext::erase(ConstantAggregateZero *Z) {
528 void LLVMContext::erase(ConstantArray *C) {
532 void LLVMContext::erase(ConstantVector *V) {
536 Constant *LLVMContext::replaceUsesOfWithOnConstant(ConstantArray *CA,
537 Value *From, Value *To, Use *U) {
538 return pImpl->replaceUsesOfWithOnConstant(CA, From, To, U);