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/Metadata.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>(ConstantVector::get(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 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
108 return pImpl->getMDNode(Vals, NumVals);
111 // MDString accessors
112 MDString* LLVMContext::getMDString(const StringRef &Str) {
113 return pImpl->getMDString(Str.data(), Str.size());
116 // FunctionType accessors
117 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
118 return FunctionType::get(Result, isVarArg);
121 FunctionType* LLVMContext::getFunctionType(const Type* Result,
122 const std::vector<const Type*>& Params,
124 return FunctionType::get(Result, Params, isVarArg);
127 // IntegerType accessors
128 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
129 return IntegerType::get(NumBits);
132 // OpaqueType accessors
133 OpaqueType* LLVMContext::getOpaqueType() {
134 return OpaqueType::get();
137 // StructType accessors
138 StructType* LLVMContext::getStructType(bool isPacked) {
139 return StructType::get(isPacked);
142 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
144 return StructType::get(Params, isPacked);
147 StructType *LLVMContext::getStructType(const Type *type, ...) {
149 std::vector<const llvm::Type*> StructFields;
152 StructFields.push_back(type);
153 type = va_arg(ap, llvm::Type*);
155 return StructType::get(StructFields);
158 // ArrayType accessors
159 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
160 uint64_t NumElements) {
161 return ArrayType::get(ElementType, NumElements);
164 // PointerType accessors
165 PointerType* LLVMContext::getPointerType(const Type* ElementType,
166 unsigned AddressSpace) {
167 return PointerType::get(ElementType, AddressSpace);
170 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
171 return PointerType::getUnqual(ElementType);
174 // VectorType accessors
175 VectorType* LLVMContext::getVectorType(const Type* ElementType,
176 unsigned NumElements) {
177 return VectorType::get(ElementType, NumElements);
180 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
181 return VectorType::getInteger(VTy);
184 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
185 return VectorType::getExtendedElementVectorType(VTy);
188 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
189 return VectorType::getTruncatedElementVectorType(VTy);
192 const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
193 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
194 return getVectorType(Type::Int1Ty, vt->getNumElements());
199 void LLVMContext::erase(MDString *M) {
203 void LLVMContext::erase(MDNode *M) {
207 void LLVMContext::erase(ConstantAggregateZero *Z) {