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/Support/ManagedStatic.h"
19 #include "LLVMContextImpl.h"
23 static ManagedStatic<LLVMContext> GlobalContext;
25 const LLVMContext& llvm::getGlobalContext() {
26 return *GlobalContext;
29 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl()) { }
30 LLVMContext::~LLVMContext() { delete pImpl; }
32 // ConstantInt accessors.
33 ConstantInt* LLVMContext::getConstantIntTrue() const {
34 return ConstantInt::getTrue();
37 ConstantInt* LLVMContext::getConstantIntFalse() const {
38 return ConstantInt::getFalse();
41 ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
42 bool isSigned) const {
43 return ConstantInt::get(Ty, V, isSigned);
46 ConstantInt* LLVMContext::getConstantIntSigned(const IntegerType* Ty,
48 return ConstantInt::getSigned(Ty, V);
51 ConstantInt* LLVMContext::getConstantInt(const APInt& V) const {
52 return ConstantInt::get(V);
55 Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) const {
56 return ConstantInt::get(Ty, V);
59 ConstantInt* LLVMContext::getAllOnesConstantInt(const Type* Ty) const {
60 return ConstantInt::getAllOnesValue(Ty);
64 // ConstantPointerNull accessors.
66 LLVMContext::getConstantPointerNull(const PointerType* T) const {
67 return ConstantPointerNull::get(T);
71 // ConstantStruct accessors.
72 Constant* LLVMContext::getConstantStruct(const StructType* T,
73 const std::vector<Constant*>& V) const {
74 return ConstantStruct::get(T, V);
77 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
79 return ConstantStruct::get(V, Packed);
82 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
83 unsigned NumVals, bool Packed) const {
84 return ConstantStruct::get(Vals, NumVals, Packed);
88 // ConstantAggregateZero accessors.
89 ConstantAggregateZero*
90 LLVMContext::getConstantAggregateZero(const Type* Ty) const {
91 return ConstantAggregateZero::get(Ty);
95 // ConstantArray accessors.
96 Constant* LLVMContext::getConstantArray(const ArrayType* T,
97 const std::vector<Constant*>& V) const {
98 return ConstantArray::get(T, V);
101 Constant* LLVMContext::getConstantArray(const ArrayType* T,
102 Constant* const* Vals,
103 unsigned NumVals) const {
104 return ConstantArray::get(T, Vals, NumVals);
107 Constant* LLVMContext::getConstantArray(const std::string& Initializer,
108 bool AddNull) const {
109 return ConstantArray::get(Initializer, AddNull);
113 // ConstantExpr accessors.
114 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
115 Constant* C2) const {
116 return ConstantExpr::get(Opcode, C1, C2);
119 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) const {
120 return ConstantExpr::getTrunc(C, Ty);
123 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) const {
124 return ConstantExpr::getSExt(C, Ty);
127 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) const {
128 return ConstantExpr::getZExt(C, Ty);
132 LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) const {
133 return ConstantExpr::getFPTrunc(C, Ty);
137 LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) const {
138 return ConstantExpr::getFPExtend(C, Ty);
142 LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) const {
143 return ConstantExpr::getUIToFP(C, Ty);
147 LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) const {
148 return ConstantExpr::getSIToFP(C, Ty);
152 LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) const {
153 return ConstantExpr::getFPToUI(C, Ty);
157 LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) const {
158 return ConstantExpr::getFPToSI(C, Ty);
162 LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) const {
163 return ConstantExpr::getPtrToInt(C, Ty);
167 LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) const {
168 return ConstantExpr::getIntToPtr(C, Ty);
172 LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) const {
173 return ConstantExpr::getBitCast(C, Ty);
176 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
177 const Type* Ty) const {
178 return ConstantExpr::getCast(ops, C, Ty);
181 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
182 const Type* Ty) const {
183 return ConstantExpr::getZExtOrBitCast(C, Ty);
186 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
187 const Type* Ty) const {
188 return ConstantExpr::getSExtOrBitCast(C, Ty);
191 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
192 const Type* Ty) const {
193 return ConstantExpr::getTruncOrBitCast(C, Ty);
197 LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) const {
198 return ConstantExpr::getPointerCast(C, Ty);
201 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
202 bool isSigned) const {
203 return ConstantExpr::getIntegerCast(C, Ty, isSigned);
207 LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) const {
208 return ConstantExpr::getFPCast(C, Ty);
211 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
212 Constant* V2) const {
213 return ConstantExpr::getSelect(C, V1, V2);
216 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) const {
217 return ConstantExpr::getAlignOf(Ty);
220 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
221 Constant* C1, Constant* C2) const {
222 return ConstantExpr::getCompare(pred, C1, C2);
225 Constant* LLVMContext::getConstantExprNeg(Constant* C) const {
226 return ConstantExpr::getNeg(C);
229 Constant* LLVMContext::getConstantExprFNeg(Constant* C) const {
230 return ConstantExpr::getFNeg(C);
233 Constant* LLVMContext::getConstantExprNot(Constant* C) const {
234 return ConstantExpr::getNot(C);
237 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) const {
238 return ConstantExpr::getAdd(C1, C2);
241 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) const {
242 return ConstantExpr::getFAdd(C1, C2);
245 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) const {
246 return ConstantExpr::getSub(C1, C2);
249 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) const {
250 return ConstantExpr::getFSub(C1, C2);
253 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) const {
254 return ConstantExpr::getMul(C1, C2);
257 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) const {
258 return ConstantExpr::getFMul(C1, C2);
261 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) const {
262 return ConstantExpr::getUDiv(C1, C2);
265 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) const {
266 return ConstantExpr::getSDiv(C1, C2);
269 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) const {
270 return ConstantExpr::getFDiv(C1, C2);
273 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) const {
274 return ConstantExpr::getURem(C1, C2);
277 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) const {
278 return ConstantExpr::getSRem(C1, C2);
281 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) const {
282 return ConstantExpr::getFRem(C1, C2);
285 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) const {
286 return ConstantExpr::getAnd(C1, C2);
289 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) const {
290 return ConstantExpr::getOr(C1, C2);
293 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) const {
294 return ConstantExpr::getXor(C1, C2);
297 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
298 Constant* RHS) const {
299 return ConstantExpr::getICmp(pred, LHS, RHS);
302 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
303 Constant* RHS) const {
304 return ConstantExpr::getFCmp(pred, LHS, RHS);
307 Constant* LLVMContext::getConstantExprVICmp(unsigned short pred, Constant* LHS,
308 Constant* RHS) const {
309 return ConstantExpr::getVICmp(pred, LHS, RHS);
312 Constant* LLVMContext::getConstantExprVFCmp(unsigned short pred, Constant* LHS,
313 Constant* RHS) const {
314 return ConstantExpr::getVFCmp(pred, LHS, RHS);
317 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) const {
318 return ConstantExpr::getShl(C1, C2);
321 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) const {
322 return ConstantExpr::getLShr(C1, C2);
325 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) const {
326 return ConstantExpr::getAShr(C1, C2);
329 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
330 Constant* const* IdxList,
331 unsigned NumIdx) const {
332 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
335 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
336 Value* const* IdxList,
337 unsigned NumIdx) const {
338 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
341 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
342 Constant* Idx) const {
343 return ConstantExpr::getExtractElement(Vec, Idx);
346 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
348 Constant* Idx) const {
349 return ConstantExpr::getInsertElement(Vec, Elt, Idx);
352 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
353 Constant* Mask) const {
354 return ConstantExpr::getShuffleVector(V1, V2, Mask);
357 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
358 const unsigned* IdxList,
359 unsigned NumIdx) const {
360 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
363 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
364 const unsigned* IdxList,
365 unsigned NumIdx) const {
366 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
369 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) const {
370 return ConstantExpr::getZeroValueForNegationExpr(Ty);
374 // ConstantFP accessors.
375 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) const {
376 return ConstantFP::get(V);
379 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) const {
380 return ConstantFP::get(Ty, V);
383 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) const {
384 return ConstantFP::getNegativeZero(Ty);
388 // ConstantVector accessors.
389 Constant* LLVMContext::getConstantVector(const VectorType* T,
390 const std::vector<Constant*>& V) const {
391 return ConstantVector::get(T, V);
395 LLVMContext::getConstantVector(const std::vector<Constant*>& V) const {
396 return ConstantVector::get(V);
399 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
400 unsigned NumVals) const {
401 return ConstantVector::get(Vals, NumVals);
405 LLVMContext::getConstantVectorAllOnes(const VectorType* Ty) const {
406 return ConstantVector::getAllOnesValue(Ty);
409 // FunctionType accessors
410 FunctionType* LLVMContext::getFunctionType(const Type* Result,
411 const std::vector<const Type*>& Params,
412 bool isVarArg) const {
413 return FunctionType::get(Result, Params, isVarArg);
416 // IntegerType accessors
417 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) const {
418 return IntegerType::get(NumBits);
421 // OpaqueType accessors
422 OpaqueType* LLVMContext::getOpaqueType() const {
423 return OpaqueType::get();
426 // StructType accessors
427 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
428 bool isPacked) const {
429 return StructType::get(Params, isPacked);
432 // ArrayType accessors
433 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
434 uint64_t NumElements) const {
435 return ArrayType::get(ElementType, NumElements);
438 // PointerType accessors
439 PointerType* LLVMContext::getPointerType(const Type* ElementType,
440 unsigned AddressSpace) const {
441 return PointerType::get(ElementType, AddressSpace);
445 LLVMContext::getPointerTypeUnqualified(const Type* ElementType) const {
446 return PointerType::getUnqual(ElementType);
449 // VectorType accessors
450 VectorType* LLVMContext::getVectorType(const Type* ElementType,
451 unsigned NumElements) const {
452 return VectorType::get(ElementType, NumElements);
455 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) const {
456 return VectorType::getInteger(VTy);
460 LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) const {
461 return VectorType::getExtendedElementVectorType(VTy);
465 LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) const {
466 return VectorType::getTruncatedElementVectorType(VTy);