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 // UndefValue accessors.
42 UndefValue* LLVMContext::getUndef(const Type* Ty) {
43 return UndefValue::get(Ty);
46 // ConstantInt accessors.
47 ConstantInt* LLVMContext::getConstantIntTrue() {
48 return ConstantInt::getTrue();
51 ConstantInt* LLVMContext::getConstantIntFalse() {
52 return ConstantInt::getFalse();
55 ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
57 return ConstantInt::get(Ty, V, isSigned);
60 ConstantInt* LLVMContext::getConstantIntSigned(const IntegerType* Ty,
62 return ConstantInt::getSigned(Ty, V);
65 ConstantInt* LLVMContext::getConstantInt(const APInt& V) {
66 return ConstantInt::get(V);
69 Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
70 return ConstantInt::get(Ty, V);
73 ConstantInt* LLVMContext::getAllOnesConstantInt(const Type* Ty) {
74 return ConstantInt::getAllOnesValue(Ty);
78 // ConstantPointerNull accessors.
79 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
80 return ConstantPointerNull::get(T);
84 // ConstantStruct accessors.
85 Constant* LLVMContext::getConstantStruct(const StructType* T,
86 const std::vector<Constant*>& V) {
87 return ConstantStruct::get(T, V);
90 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
92 return ConstantStruct::get(V, Packed);
95 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
96 unsigned NumVals, bool Packed) {
97 return ConstantStruct::get(Vals, NumVals, Packed);
101 // ConstantAggregateZero accessors.
102 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
103 return ConstantAggregateZero::get(Ty);
107 // ConstantArray accessors.
108 Constant* LLVMContext::getConstantArray(const ArrayType* T,
109 const std::vector<Constant*>& V) {
110 return ConstantArray::get(T, V);
113 Constant* LLVMContext::getConstantArray(const ArrayType* T,
114 Constant* const* Vals,
116 return ConstantArray::get(T, Vals, NumVals);
119 Constant* LLVMContext::getConstantArray(const std::string& Initializer,
121 return ConstantArray::get(Initializer, AddNull);
125 // ConstantExpr accessors.
126 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
128 return ConstantExpr::get(Opcode, C1, C2);
131 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
132 return ConstantExpr::getTrunc(C, Ty);
135 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
136 return ConstantExpr::getSExt(C, Ty);
139 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
140 return ConstantExpr::getZExt(C, Ty);
143 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
144 return ConstantExpr::getFPTrunc(C, Ty);
147 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
148 return ConstantExpr::getFPExtend(C, Ty);
151 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
152 return ConstantExpr::getUIToFP(C, Ty);
155 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
156 return ConstantExpr::getSIToFP(C, Ty);
159 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
160 return ConstantExpr::getFPToUI(C, Ty);
163 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
164 return ConstantExpr::getFPToSI(C, Ty);
167 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
168 return ConstantExpr::getPtrToInt(C, Ty);
171 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
172 return ConstantExpr::getIntToPtr(C, Ty);
175 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
176 return ConstantExpr::getBitCast(C, Ty);
179 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
181 return ConstantExpr::getCast(ops, C, Ty);
184 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
186 return ConstantExpr::getZExtOrBitCast(C, Ty);
189 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
191 return ConstantExpr::getSExtOrBitCast(C, Ty);
194 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
196 return ConstantExpr::getTruncOrBitCast(C, Ty);
199 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
200 return ConstantExpr::getPointerCast(C, Ty);
203 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
205 return ConstantExpr::getIntegerCast(C, Ty, isSigned);
208 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
209 return ConstantExpr::getFPCast(C, Ty);
212 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
214 return ConstantExpr::getSelect(C, V1, V2);
217 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
218 return ConstantExpr::getAlignOf(Ty);
221 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
222 Constant* C1, Constant* C2) {
223 return ConstantExpr::getCompare(pred, C1, C2);
226 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
227 return ConstantExpr::getNeg(C);
230 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
231 return ConstantExpr::getFNeg(C);
234 Constant* LLVMContext::getConstantExprNot(Constant* C) {
235 return ConstantExpr::getNot(C);
238 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
239 return ConstantExpr::getAdd(C1, C2);
242 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
243 return ConstantExpr::getFAdd(C1, C2);
246 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
247 return ConstantExpr::getSub(C1, C2);
250 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
251 return ConstantExpr::getFSub(C1, C2);
254 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
255 return ConstantExpr::getMul(C1, C2);
258 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
259 return ConstantExpr::getFMul(C1, C2);
262 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
263 return ConstantExpr::getUDiv(C1, C2);
266 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
267 return ConstantExpr::getSDiv(C1, C2);
270 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
271 return ConstantExpr::getFDiv(C1, C2);
274 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
275 return ConstantExpr::getURem(C1, C2);
278 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
279 return ConstantExpr::getSRem(C1, C2);
282 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
283 return ConstantExpr::getFRem(C1, C2);
286 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
287 return ConstantExpr::getAnd(C1, C2);
290 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
291 return ConstantExpr::getOr(C1, C2);
294 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
295 return ConstantExpr::getXor(C1, C2);
298 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
300 return ConstantExpr::getICmp(pred, LHS, RHS);
303 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
305 return ConstantExpr::getFCmp(pred, LHS, RHS);
308 Constant* LLVMContext::getConstantExprVICmp(unsigned short pred, Constant* LHS,
310 return ConstantExpr::getVICmp(pred, LHS, RHS);
313 Constant* LLVMContext::getConstantExprVFCmp(unsigned short pred, Constant* LHS,
315 return ConstantExpr::getVFCmp(pred, LHS, RHS);
318 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
319 return ConstantExpr::getShl(C1, C2);
322 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
323 return ConstantExpr::getLShr(C1, C2);
326 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
327 return ConstantExpr::getAShr(C1, C2);
330 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
331 Constant* const* IdxList,
333 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
336 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
337 Value* const* IdxList,
339 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
342 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
344 return ConstantExpr::getExtractElement(Vec, Idx);
347 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
350 return ConstantExpr::getInsertElement(Vec, Elt, Idx);
353 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
355 return ConstantExpr::getShuffleVector(V1, V2, Mask);
358 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
359 const unsigned* IdxList,
361 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
364 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
365 const unsigned* IdxList,
367 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
370 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
371 return ConstantExpr::getZeroValueForNegationExpr(Ty);
375 // ConstantFP accessors.
376 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
377 return ConstantFP::get(V);
380 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
381 return ConstantFP::get(Ty, V);
384 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
385 return ConstantFP::getNegativeZero(Ty);
389 // ConstantVector accessors.
390 Constant* LLVMContext::getConstantVector(const VectorType* T,
391 const std::vector<Constant*>& V) {
392 return ConstantVector::get(T, V);
395 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
396 return ConstantVector::get(V);
399 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
401 return ConstantVector::get(Vals, NumVals);
404 ConstantVector* LLVMContext::getConstantVectorAllOnes(const VectorType* Ty) {
405 return ConstantVector::getAllOnesValue(Ty);
408 // FunctionType accessors
409 FunctionType* LLVMContext::getFunctionType(const Type* Result,
410 const std::vector<const Type*>& Params,
412 return FunctionType::get(Result, Params, isVarArg);
415 // IntegerType accessors
416 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
417 return IntegerType::get(NumBits);
420 // OpaqueType accessors
421 OpaqueType* LLVMContext::getOpaqueType() {
422 return OpaqueType::get();
425 // StructType accessors
426 StructType* LLVMContext::getStructType(bool isPacked) {
427 return StructType::get(isPacked);
430 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
432 return StructType::get(Params, isPacked);
435 // ArrayType accessors
436 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
437 uint64_t NumElements) {
438 return ArrayType::get(ElementType, NumElements);
441 // PointerType accessors
442 PointerType* LLVMContext::getPointerType(const Type* ElementType,
443 unsigned AddressSpace) {
444 return PointerType::get(ElementType, AddressSpace);
447 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
448 return PointerType::getUnqual(ElementType);
451 // VectorType accessors
452 VectorType* LLVMContext::getVectorType(const Type* ElementType,
453 unsigned NumElements) {
454 return VectorType::get(ElementType, NumElements);
457 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
458 return VectorType::getInteger(VTy);
461 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
462 return VectorType::getExtendedElementVectorType(VTy);
465 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
466 return VectorType::getTruncatedElementVectorType(VTy);