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 LLVMContext& llvm::getGlobalContext() {
26 return *GlobalContext;
29 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl()) { }
30 LLVMContext::~LLVMContext() { delete pImpl; }
33 Constant* LLVMContext::getNullValue(const Type* Ty) {
34 return Constant::getNullValue(Ty);
37 Constant* LLVMContext::getAllOnesValue(const Type* Ty) {
38 return Constant::getAllOnesValue(Ty);
41 // ConstantInt accessors.
42 ConstantInt* LLVMContext::getConstantIntTrue() {
43 return ConstantInt::getTrue();
46 ConstantInt* LLVMContext::getConstantIntFalse() {
47 return ConstantInt::getFalse();
50 ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
52 return ConstantInt::get(Ty, V, isSigned);
55 ConstantInt* LLVMContext::getConstantIntSigned(const IntegerType* Ty,
57 return ConstantInt::getSigned(Ty, V);
60 ConstantInt* LLVMContext::getConstantInt(const APInt& V) {
61 return ConstantInt::get(V);
64 Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
65 return ConstantInt::get(Ty, V);
68 ConstantInt* LLVMContext::getAllOnesConstantInt(const Type* Ty) {
69 return ConstantInt::getAllOnesValue(Ty);
73 // ConstantPointerNull accessors.
74 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
75 return ConstantPointerNull::get(T);
79 // ConstantStruct accessors.
80 Constant* LLVMContext::getConstantStruct(const StructType* T,
81 const std::vector<Constant*>& V) {
82 return ConstantStruct::get(T, V);
85 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
87 return ConstantStruct::get(V, Packed);
90 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
91 unsigned NumVals, bool Packed) {
92 return ConstantStruct::get(Vals, NumVals, Packed);
96 // ConstantAggregateZero accessors.
97 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
98 return ConstantAggregateZero::get(Ty);
102 // ConstantArray accessors.
103 Constant* LLVMContext::getConstantArray(const ArrayType* T,
104 const std::vector<Constant*>& V) {
105 return ConstantArray::get(T, V);
108 Constant* LLVMContext::getConstantArray(const ArrayType* T,
109 Constant* const* Vals,
111 return ConstantArray::get(T, Vals, NumVals);
114 Constant* LLVMContext::getConstantArray(const std::string& Initializer,
116 return ConstantArray::get(Initializer, AddNull);
120 // ConstantExpr accessors.
121 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
123 return ConstantExpr::get(Opcode, C1, C2);
126 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
127 return ConstantExpr::getTrunc(C, Ty);
130 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
131 return ConstantExpr::getSExt(C, Ty);
134 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
135 return ConstantExpr::getZExt(C, Ty);
138 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
139 return ConstantExpr::getFPTrunc(C, Ty);
142 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
143 return ConstantExpr::getFPExtend(C, Ty);
146 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
147 return ConstantExpr::getUIToFP(C, Ty);
150 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
151 return ConstantExpr::getSIToFP(C, Ty);
154 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
155 return ConstantExpr::getFPToUI(C, Ty);
158 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
159 return ConstantExpr::getFPToSI(C, Ty);
162 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
163 return ConstantExpr::getPtrToInt(C, Ty);
166 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
167 return ConstantExpr::getIntToPtr(C, Ty);
170 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
171 return ConstantExpr::getBitCast(C, Ty);
174 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
176 return ConstantExpr::getCast(ops, C, Ty);
179 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
181 return ConstantExpr::getZExtOrBitCast(C, Ty);
184 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
186 return ConstantExpr::getSExtOrBitCast(C, Ty);
189 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
191 return ConstantExpr::getTruncOrBitCast(C, Ty);
194 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
195 return ConstantExpr::getPointerCast(C, Ty);
198 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
200 return ConstantExpr::getIntegerCast(C, Ty, isSigned);
203 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
204 return ConstantExpr::getFPCast(C, Ty);
207 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
209 return ConstantExpr::getSelect(C, V1, V2);
212 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
213 return ConstantExpr::getAlignOf(Ty);
216 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
217 Constant* C1, Constant* C2) {
218 return ConstantExpr::getCompare(pred, C1, C2);
221 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
222 return ConstantExpr::getNeg(C);
225 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
226 return ConstantExpr::getFNeg(C);
229 Constant* LLVMContext::getConstantExprNot(Constant* C) {
230 return ConstantExpr::getNot(C);
233 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
234 return ConstantExpr::getAdd(C1, C2);
237 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
238 return ConstantExpr::getFAdd(C1, C2);
241 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
242 return ConstantExpr::getSub(C1, C2);
245 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
246 return ConstantExpr::getFSub(C1, C2);
249 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
250 return ConstantExpr::getMul(C1, C2);
253 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
254 return ConstantExpr::getFMul(C1, C2);
257 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
258 return ConstantExpr::getUDiv(C1, C2);
261 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
262 return ConstantExpr::getSDiv(C1, C2);
265 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
266 return ConstantExpr::getFDiv(C1, C2);
269 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
270 return ConstantExpr::getURem(C1, C2);
273 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
274 return ConstantExpr::getSRem(C1, C2);
277 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
278 return ConstantExpr::getFRem(C1, C2);
281 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
282 return ConstantExpr::getAnd(C1, C2);
285 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
286 return ConstantExpr::getOr(C1, C2);
289 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
290 return ConstantExpr::getXor(C1, C2);
293 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
295 return ConstantExpr::getICmp(pred, LHS, RHS);
298 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
300 return ConstantExpr::getFCmp(pred, LHS, RHS);
303 Constant* LLVMContext::getConstantExprVICmp(unsigned short pred, Constant* LHS,
305 return ConstantExpr::getVICmp(pred, LHS, RHS);
308 Constant* LLVMContext::getConstantExprVFCmp(unsigned short pred, Constant* LHS,
310 return ConstantExpr::getVFCmp(pred, LHS, RHS);
313 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
314 return ConstantExpr::getShl(C1, C2);
317 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
318 return ConstantExpr::getLShr(C1, C2);
321 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
322 return ConstantExpr::getAShr(C1, C2);
325 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
326 Constant* const* IdxList,
328 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
331 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
332 Value* const* IdxList,
334 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
337 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
339 return ConstantExpr::getExtractElement(Vec, Idx);
342 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
345 return ConstantExpr::getInsertElement(Vec, Elt, Idx);
348 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
350 return ConstantExpr::getShuffleVector(V1, V2, Mask);
353 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
354 const unsigned* IdxList,
356 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
359 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
360 const unsigned* IdxList,
362 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
365 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
366 return ConstantExpr::getZeroValueForNegationExpr(Ty);
370 // ConstantFP accessors.
371 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
372 return ConstantFP::get(V);
375 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
376 return ConstantFP::get(Ty, V);
379 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
380 return ConstantFP::getNegativeZero(Ty);
384 // ConstantVector accessors.
385 Constant* LLVMContext::getConstantVector(const VectorType* T,
386 const std::vector<Constant*>& V) {
387 return ConstantVector::get(T, V);
390 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
391 return ConstantVector::get(V);
394 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
396 return ConstantVector::get(Vals, NumVals);
399 ConstantVector* LLVMContext::getConstantVectorAllOnes(const VectorType* Ty) {
400 return ConstantVector::getAllOnesValue(Ty);
403 // FunctionType accessors
404 FunctionType* LLVMContext::getFunctionType(const Type* Result,
405 const std::vector<const Type*>& Params,
407 return FunctionType::get(Result, Params, isVarArg);
410 // IntegerType accessors
411 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
412 return IntegerType::get(NumBits);
415 // OpaqueType accessors
416 OpaqueType* LLVMContext::getOpaqueType() {
417 return OpaqueType::get();
420 // StructType accessors
421 StructType* LLVMContext::getStructType(bool isPacked) {
422 return StructType::get(isPacked);
425 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
427 return StructType::get(Params, isPacked);
430 // ArrayType accessors
431 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
432 uint64_t NumElements) {
433 return ArrayType::get(ElementType, NumElements);
436 // PointerType accessors
437 PointerType* LLVMContext::getPointerType(const Type* ElementType,
438 unsigned AddressSpace) {
439 return PointerType::get(ElementType, AddressSpace);
442 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
443 return PointerType::getUnqual(ElementType);
446 // VectorType accessors
447 VectorType* LLVMContext::getVectorType(const Type* ElementType,
448 unsigned NumElements) {
449 return VectorType::get(ElementType, NumElements);
452 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
453 return VectorType::getInteger(VTy);
456 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
457 return VectorType::getExtendedElementVectorType(VTy);
460 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
461 return VectorType::getTruncatedElementVectorType(VTy);