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/MDNode.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "LLVMContextImpl.h"
24 static ManagedStatic<LLVMContext> GlobalContext;
26 LLVMContext& llvm::getGlobalContext() {
27 return *GlobalContext;
30 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl()) { }
31 LLVMContext::~LLVMContext() { delete pImpl; }
34 Constant* LLVMContext::getNullValue(const Type* Ty) {
35 return Constant::getNullValue(Ty);
38 Constant* LLVMContext::getAllOnesValue(const Type* Ty) {
39 return Constant::getAllOnesValue(Ty);
42 // UndefValue accessors.
43 UndefValue* LLVMContext::getUndef(const Type* Ty) {
44 return UndefValue::get(Ty);
47 // ConstantInt accessors.
48 ConstantInt* LLVMContext::getConstantIntTrue() {
49 return ConstantInt::getTrue();
52 ConstantInt* LLVMContext::getConstantIntFalse() {
53 return ConstantInt::getFalse();
56 ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
58 return ConstantInt::get(Ty, V, isSigned);
61 ConstantInt* LLVMContext::getConstantIntSigned(const IntegerType* Ty,
63 return ConstantInt::getSigned(Ty, V);
66 ConstantInt* LLVMContext::getConstantInt(const APInt& V) {
67 return ConstantInt::get(V);
70 Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
71 return ConstantInt::get(Ty, V);
74 ConstantInt* LLVMContext::getAllOnesConstantInt(const Type* Ty) {
75 return ConstantInt::getAllOnesValue(Ty);
79 // ConstantPointerNull accessors.
80 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
81 return ConstantPointerNull::get(T);
85 // ConstantStruct accessors.
86 Constant* LLVMContext::getConstantStruct(const StructType* T,
87 const std::vector<Constant*>& V) {
88 return ConstantStruct::get(T, V);
91 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
93 return ConstantStruct::get(V, Packed);
96 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
97 unsigned NumVals, bool Packed) {
98 return ConstantStruct::get(Vals, NumVals, Packed);
102 // ConstantAggregateZero accessors.
103 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
104 return ConstantAggregateZero::get(Ty);
108 // ConstantArray accessors.
109 Constant* LLVMContext::getConstantArray(const ArrayType* T,
110 const std::vector<Constant*>& V) {
111 return ConstantArray::get(T, V);
114 Constant* LLVMContext::getConstantArray(const ArrayType* T,
115 Constant* const* Vals,
117 return ConstantArray::get(T, Vals, NumVals);
120 Constant* LLVMContext::getConstantArray(const std::string& Initializer,
122 return ConstantArray::get(Initializer, AddNull);
126 // ConstantExpr accessors.
127 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
129 return ConstantExpr::get(Opcode, C1, C2);
132 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
133 return ConstantExpr::getTrunc(C, Ty);
136 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
137 return ConstantExpr::getSExt(C, Ty);
140 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
141 return ConstantExpr::getZExt(C, Ty);
144 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
145 return ConstantExpr::getFPTrunc(C, Ty);
148 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
149 return ConstantExpr::getFPExtend(C, Ty);
152 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
153 return ConstantExpr::getUIToFP(C, Ty);
156 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
157 return ConstantExpr::getSIToFP(C, Ty);
160 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
161 return ConstantExpr::getFPToUI(C, Ty);
164 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
165 return ConstantExpr::getFPToSI(C, Ty);
168 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
169 return ConstantExpr::getPtrToInt(C, Ty);
172 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
173 return ConstantExpr::getIntToPtr(C, Ty);
176 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
177 return ConstantExpr::getBitCast(C, Ty);
180 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
182 return ConstantExpr::getCast(ops, C, Ty);
185 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
187 return ConstantExpr::getZExtOrBitCast(C, Ty);
190 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
192 return ConstantExpr::getSExtOrBitCast(C, Ty);
195 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
197 return ConstantExpr::getTruncOrBitCast(C, Ty);
200 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
201 return ConstantExpr::getPointerCast(C, Ty);
204 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
206 return ConstantExpr::getIntegerCast(C, Ty, isSigned);
209 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
210 return ConstantExpr::getFPCast(C, Ty);
213 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
215 return ConstantExpr::getSelect(C, V1, V2);
218 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
219 return ConstantExpr::getAlignOf(Ty);
222 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
223 Constant* C1, Constant* C2) {
224 return ConstantExpr::getCompare(pred, C1, C2);
227 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
228 return ConstantExpr::getNeg(C);
231 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
232 return ConstantExpr::getFNeg(C);
235 Constant* LLVMContext::getConstantExprNot(Constant* C) {
236 return ConstantExpr::getNot(C);
239 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
240 return ConstantExpr::getAdd(C1, C2);
243 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
244 return ConstantExpr::getFAdd(C1, C2);
247 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
248 return ConstantExpr::getSub(C1, C2);
251 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
252 return ConstantExpr::getFSub(C1, C2);
255 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
256 return ConstantExpr::getMul(C1, C2);
259 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
260 return ConstantExpr::getFMul(C1, C2);
263 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
264 return ConstantExpr::getUDiv(C1, C2);
267 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
268 return ConstantExpr::getSDiv(C1, C2);
271 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
272 return ConstantExpr::getFDiv(C1, C2);
275 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
276 return ConstantExpr::getURem(C1, C2);
279 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
280 return ConstantExpr::getSRem(C1, C2);
283 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
284 return ConstantExpr::getFRem(C1, C2);
287 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
288 return ConstantExpr::getAnd(C1, C2);
291 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
292 return ConstantExpr::getOr(C1, C2);
295 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
296 return ConstantExpr::getXor(C1, C2);
299 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
301 return ConstantExpr::getICmp(pred, LHS, RHS);
304 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
306 return ConstantExpr::getFCmp(pred, LHS, RHS);
309 Constant* LLVMContext::getConstantExprVICmp(unsigned short pred, Constant* LHS,
311 return ConstantExpr::getVICmp(pred, LHS, RHS);
314 Constant* LLVMContext::getConstantExprVFCmp(unsigned short pred, Constant* LHS,
316 return ConstantExpr::getVFCmp(pred, LHS, RHS);
319 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
320 return ConstantExpr::getShl(C1, C2);
323 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
324 return ConstantExpr::getLShr(C1, C2);
327 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
328 return ConstantExpr::getAShr(C1, C2);
331 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
332 Constant* const* IdxList,
334 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
337 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
338 Value* const* IdxList,
340 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
343 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
345 return ConstantExpr::getExtractElement(Vec, Idx);
348 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
351 return ConstantExpr::getInsertElement(Vec, Elt, Idx);
354 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
356 return ConstantExpr::getShuffleVector(V1, V2, Mask);
359 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
360 const unsigned* IdxList,
362 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
365 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
366 const unsigned* IdxList,
368 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
371 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
372 return ConstantExpr::getZeroValueForNegationExpr(Ty);
376 // ConstantFP accessors.
377 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
378 return ConstantFP::get(V);
381 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
382 return ConstantFP::get(Ty, V);
385 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
386 return ConstantFP::getNegativeZero(Ty);
390 // ConstantVector accessors.
391 Constant* LLVMContext::getConstantVector(const VectorType* T,
392 const std::vector<Constant*>& V) {
393 return ConstantVector::get(T, V);
396 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
397 return ConstantVector::get(V);
400 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
402 return ConstantVector::get(Vals, NumVals);
405 ConstantVector* LLVMContext::getConstantVectorAllOnes(const VectorType* Ty) {
406 return ConstantVector::getAllOnesValue(Ty);
410 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
411 return MDNode::get(Vals, NumVals);
414 // MDString accessors
415 MDString* LLVMContext::getMDString(const char *StrBegin, const char *StrEnd) {
416 return MDString::get(StrBegin, StrEnd);
419 MDString* LLVMContext::getMDString(const std::string &Str) {
420 return MDString::get(Str);
423 // FunctionType accessors
424 FunctionType* LLVMContext::getFunctionType(const Type* Result,
425 const std::vector<const Type*>& Params,
427 return FunctionType::get(Result, Params, isVarArg);
430 // IntegerType accessors
431 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
432 return IntegerType::get(NumBits);
435 // OpaqueType accessors
436 OpaqueType* LLVMContext::getOpaqueType() {
437 return OpaqueType::get();
440 // StructType accessors
441 StructType* LLVMContext::getStructType(bool isPacked) {
442 return StructType::get(isPacked);
445 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
447 return StructType::get(Params, isPacked);
450 // ArrayType accessors
451 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
452 uint64_t NumElements) {
453 return ArrayType::get(ElementType, NumElements);
456 // PointerType accessors
457 PointerType* LLVMContext::getPointerType(const Type* ElementType,
458 unsigned AddressSpace) {
459 return PointerType::get(ElementType, AddressSpace);
462 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
463 return PointerType::getUnqual(ElementType);
466 // VectorType accessors
467 VectorType* LLVMContext::getVectorType(const Type* ElementType,
468 unsigned NumElements) {
469 return VectorType::get(ElementType, NumElements);
472 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
473 return VectorType::getInteger(VTy);
476 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
477 return VectorType::getExtendedElementVectorType(VTy);
480 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
481 return VectorType::getTruncatedElementVectorType(VTy);