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() {
34 return ConstantInt::getTrue();
37 ConstantInt* LLVMContext::getConstantIntFalse() {
38 return ConstantInt::getFalse();
41 ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
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) {
52 return ConstantInt::get(V);
55 Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
56 return ConstantInt::get(Ty, V);
59 ConstantInt* LLVMContext::getAllOnesConstantInt(const Type* Ty) {
60 return ConstantInt::getAllOnesValue(Ty);
64 // ConstantPointerNull accessors.
65 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
66 return ConstantPointerNull::get(T);
70 // ConstantStruct accessors.
71 Constant* LLVMContext::getConstantStruct(const StructType* T,
72 const std::vector<Constant*>& V) {
73 return ConstantStruct::get(T, V);
76 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
78 return ConstantStruct::get(V, Packed);
81 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
82 unsigned NumVals, bool Packed) {
83 return ConstantStruct::get(Vals, NumVals, Packed);
87 // ConstantAggregateZero accessors.
88 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
89 return ConstantAggregateZero::get(Ty);
93 // ConstantArray accessors.
94 Constant* LLVMContext::getConstantArray(const ArrayType* T,
95 const std::vector<Constant*>& V) {
96 return ConstantArray::get(T, V);
99 Constant* LLVMContext::getConstantArray(const ArrayType* T,
100 Constant* const* Vals,
102 return ConstantArray::get(T, Vals, NumVals);
105 Constant* LLVMContext::getConstantArray(const std::string& Initializer,
107 return ConstantArray::get(Initializer, AddNull);
111 // ConstantExpr accessors.
112 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
114 return ConstantExpr::get(Opcode, C1, C2);
117 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
118 return ConstantExpr::getTrunc(C, Ty);
121 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
122 return ConstantExpr::getSExt(C, Ty);
125 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
126 return ConstantExpr::getZExt(C, Ty);
129 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
130 return ConstantExpr::getFPTrunc(C, Ty);
133 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
134 return ConstantExpr::getFPExtend(C, Ty);
137 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
138 return ConstantExpr::getUIToFP(C, Ty);
141 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
142 return ConstantExpr::getSIToFP(C, Ty);
145 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
146 return ConstantExpr::getFPToUI(C, Ty);
149 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
150 return ConstantExpr::getFPToSI(C, Ty);
153 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
154 return ConstantExpr::getPtrToInt(C, Ty);
157 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
158 return ConstantExpr::getIntToPtr(C, Ty);
161 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
162 return ConstantExpr::getBitCast(C, Ty);
165 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
167 return ConstantExpr::getCast(ops, C, Ty);
170 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
172 return ConstantExpr::getZExtOrBitCast(C, Ty);
175 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
177 return ConstantExpr::getSExtOrBitCast(C, Ty);
180 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
182 return ConstantExpr::getTruncOrBitCast(C, Ty);
185 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
186 return ConstantExpr::getPointerCast(C, Ty);
189 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
191 return ConstantExpr::getIntegerCast(C, Ty, isSigned);
194 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
195 return ConstantExpr::getFPCast(C, Ty);
198 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
200 return ConstantExpr::getSelect(C, V1, V2);
203 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
204 return ConstantExpr::getAlignOf(Ty);
207 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
208 Constant* C1, Constant* C2) {
209 return ConstantExpr::getCompare(pred, C1, C2);
212 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
213 return ConstantExpr::getNeg(C);
216 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
217 return ConstantExpr::getFNeg(C);
220 Constant* LLVMContext::getConstantExprNot(Constant* C) {
221 return ConstantExpr::getNot(C);
224 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
225 return ConstantExpr::getAdd(C1, C2);
228 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
229 return ConstantExpr::getFAdd(C1, C2);
232 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
233 return ConstantExpr::getSub(C1, C2);
236 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
237 return ConstantExpr::getFSub(C1, C2);
240 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
241 return ConstantExpr::getMul(C1, C2);
244 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
245 return ConstantExpr::getFMul(C1, C2);
248 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
249 return ConstantExpr::getUDiv(C1, C2);
252 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
253 return ConstantExpr::getSDiv(C1, C2);
256 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
257 return ConstantExpr::getFDiv(C1, C2);
260 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
261 return ConstantExpr::getURem(C1, C2);
264 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
265 return ConstantExpr::getSRem(C1, C2);
268 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
269 return ConstantExpr::getFRem(C1, C2);
272 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
273 return ConstantExpr::getAnd(C1, C2);
276 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
277 return ConstantExpr::getOr(C1, C2);
280 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
281 return ConstantExpr::getXor(C1, C2);
284 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
286 return ConstantExpr::getICmp(pred, LHS, RHS);
289 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
291 return ConstantExpr::getFCmp(pred, LHS, RHS);
294 Constant* LLVMContext::getConstantExprVICmp(unsigned short pred, Constant* LHS,
296 return ConstantExpr::getVICmp(pred, LHS, RHS);
299 Constant* LLVMContext::getConstantExprVFCmp(unsigned short pred, Constant* LHS,
301 return ConstantExpr::getVFCmp(pred, LHS, RHS);
304 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
305 return ConstantExpr::getShl(C1, C2);
308 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
309 return ConstantExpr::getLShr(C1, C2);
312 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
313 return ConstantExpr::getAShr(C1, C2);
316 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
317 Constant* const* IdxList,
319 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
322 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
323 Value* const* IdxList,
325 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
328 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
330 return ConstantExpr::getExtractElement(Vec, Idx);
333 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
336 return ConstantExpr::getInsertElement(Vec, Elt, Idx);
339 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
341 return ConstantExpr::getShuffleVector(V1, V2, Mask);
344 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
345 const unsigned* IdxList,
347 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
350 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
351 const unsigned* IdxList,
353 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
356 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
357 return ConstantExpr::getZeroValueForNegationExpr(Ty);
361 // ConstantFP accessors.
362 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
363 return ConstantFP::get(V);
366 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
367 return ConstantFP::get(Ty, V);
370 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
371 return ConstantFP::getNegativeZero(Ty);
375 // ConstantVector accessors.
376 Constant* LLVMContext::getConstantVector(const VectorType* T,
377 const std::vector<Constant*>& V) {
378 return ConstantVector::get(T, V);
381 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
382 return ConstantVector::get(V);
385 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
387 return ConstantVector::get(Vals, NumVals);
390 ConstantVector* LLVMContext::getConstantVectorAllOnes(const VectorType* Ty) {
391 return ConstantVector::getAllOnesValue(Ty);
394 // FunctionType accessors
395 FunctionType* LLVMContext::getFunctionType(const Type* Result,
396 const std::vector<const Type*>& Params,
398 return FunctionType::get(Result, Params, isVarArg);
401 // IntegerType accessors
402 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
403 return IntegerType::get(NumBits);
406 // OpaqueType accessors
407 OpaqueType* LLVMContext::getOpaqueType() {
408 return OpaqueType::get();
411 // StructType accessors
412 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
414 return StructType::get(Params, isPacked);
417 // ArrayType accessors
418 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
419 uint64_t NumElements) {
420 return ArrayType::get(ElementType, NumElements);
423 // PointerType accessors
424 PointerType* LLVMContext::getPointerType(const Type* ElementType,
425 unsigned AddressSpace) {
426 return PointerType::get(ElementType, AddressSpace);
429 PointerType* LLVMContext::getPointerTypeUnqualified(const Type* ElementType) {
430 return PointerType::getUnqual(ElementType);
433 // VectorType accessors
434 VectorType* LLVMContext::getVectorType(const Type* ElementType,
435 unsigned NumElements) {
436 return VectorType::get(ElementType, NumElements);
439 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
440 return VectorType::getInteger(VTy);
443 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
444 return VectorType::getExtendedElementVectorType(VTy);
447 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
448 return VectorType::getTruncatedElementVectorType(VTy);