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 Constant* LLVMContext::getConstantInt(const Type* Ty, uint64_t V,
58 return ConstantInt::get(Ty, V, isSigned);
62 ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
64 return ConstantInt::get(Ty, V, isSigned);
67 ConstantInt* LLVMContext::getConstantIntSigned(const IntegerType* Ty,
69 return ConstantInt::getSigned(Ty, V);
72 ConstantInt* LLVMContext::getConstantInt(const APInt& V) {
73 return ConstantInt::get(V);
76 Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
77 return ConstantInt::get(Ty, V);
80 ConstantInt* LLVMContext::getConstantIntAllOnesValue(const Type* Ty) {
81 return ConstantInt::getAllOnesValue(Ty);
85 // ConstantPointerNull accessors.
86 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
87 return ConstantPointerNull::get(T);
91 // ConstantStruct accessors.
92 Constant* LLVMContext::getConstantStruct(const StructType* T,
93 const std::vector<Constant*>& V) {
94 return ConstantStruct::get(T, V);
97 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
99 return ConstantStruct::get(V, Packed);
102 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
103 unsigned NumVals, bool Packed) {
104 return ConstantStruct::get(Vals, NumVals, Packed);
108 // ConstantAggregateZero accessors.
109 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
110 return ConstantAggregateZero::get(Ty);
114 // ConstantArray accessors.
115 Constant* LLVMContext::getConstantArray(const ArrayType* T,
116 const std::vector<Constant*>& V) {
117 return ConstantArray::get(T, V);
120 Constant* LLVMContext::getConstantArray(const ArrayType* T,
121 Constant* const* Vals,
123 return ConstantArray::get(T, Vals, NumVals);
126 Constant* LLVMContext::getConstantArray(const std::string& Initializer,
128 return ConstantArray::get(Initializer, AddNull);
132 // ConstantExpr accessors.
133 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
135 return ConstantExpr::get(Opcode, C1, C2);
138 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
139 return ConstantExpr::getTrunc(C, Ty);
142 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
143 return ConstantExpr::getSExt(C, Ty);
146 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
147 return ConstantExpr::getZExt(C, Ty);
150 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
151 return ConstantExpr::getFPTrunc(C, Ty);
154 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
155 return ConstantExpr::getFPExtend(C, Ty);
158 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
159 return ConstantExpr::getUIToFP(C, Ty);
162 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
163 return ConstantExpr::getSIToFP(C, Ty);
166 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
167 return ConstantExpr::getFPToUI(C, Ty);
170 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
171 return ConstantExpr::getFPToSI(C, Ty);
174 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
175 return ConstantExpr::getPtrToInt(C, Ty);
178 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
179 return ConstantExpr::getIntToPtr(C, Ty);
182 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
183 return ConstantExpr::getBitCast(C, Ty);
186 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
188 return ConstantExpr::getCast(ops, C, Ty);
191 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
193 return ConstantExpr::getZExtOrBitCast(C, Ty);
196 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
198 return ConstantExpr::getSExtOrBitCast(C, Ty);
201 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
203 return ConstantExpr::getTruncOrBitCast(C, Ty);
206 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
207 return ConstantExpr::getPointerCast(C, Ty);
210 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
212 return ConstantExpr::getIntegerCast(C, Ty, isSigned);
215 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
216 return ConstantExpr::getFPCast(C, Ty);
219 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
221 return ConstantExpr::getSelect(C, V1, V2);
224 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
225 return ConstantExpr::getAlignOf(Ty);
228 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
229 Constant* C1, Constant* C2) {
230 return ConstantExpr::getCompare(pred, C1, C2);
233 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
234 return ConstantExpr::getNeg(C);
237 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
238 return ConstantExpr::getFNeg(C);
241 Constant* LLVMContext::getConstantExprNot(Constant* C) {
242 return ConstantExpr::getNot(C);
245 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
246 return ConstantExpr::getAdd(C1, C2);
249 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
250 return ConstantExpr::getFAdd(C1, C2);
253 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
254 return ConstantExpr::getSub(C1, C2);
257 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
258 return ConstantExpr::getFSub(C1, C2);
261 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
262 return ConstantExpr::getMul(C1, C2);
265 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
266 return ConstantExpr::getFMul(C1, C2);
269 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
270 return ConstantExpr::getUDiv(C1, C2);
273 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
274 return ConstantExpr::getSDiv(C1, C2);
277 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
278 return ConstantExpr::getFDiv(C1, C2);
281 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
282 return ConstantExpr::getURem(C1, C2);
285 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
286 return ConstantExpr::getSRem(C1, C2);
289 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
290 return ConstantExpr::getFRem(C1, C2);
293 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
294 return ConstantExpr::getAnd(C1, C2);
297 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
298 return ConstantExpr::getOr(C1, C2);
301 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
302 return ConstantExpr::getXor(C1, C2);
305 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
307 return ConstantExpr::getICmp(pred, LHS, RHS);
310 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
312 return ConstantExpr::getFCmp(pred, LHS, RHS);
315 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
316 return ConstantExpr::getShl(C1, C2);
319 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
320 return ConstantExpr::getLShr(C1, C2);
323 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
324 return ConstantExpr::getAShr(C1, C2);
327 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
328 Constant* const* IdxList,
330 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
333 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
334 Value* const* IdxList,
336 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
339 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
341 return ConstantExpr::getExtractElement(Vec, Idx);
344 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
347 return ConstantExpr::getInsertElement(Vec, Elt, Idx);
350 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
352 return ConstantExpr::getShuffleVector(V1, V2, Mask);
355 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
356 const unsigned* IdxList,
358 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
361 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
362 const unsigned* IdxList,
364 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
367 Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
368 return ConstantExpr::getSizeOf(Ty);
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::getConstantVectorAllOnesValue(
406 const VectorType* Ty) {
407 return ConstantVector::getAllOnesValue(Ty);
411 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
412 return MDNode::get(Vals, NumVals);
415 // MDString accessors
416 MDString* LLVMContext::getMDString(const char *StrBegin, const char *StrEnd) {
417 return MDString::get(StrBegin, StrEnd);
420 MDString* LLVMContext::getMDString(const std::string &Str) {
421 return MDString::get(Str);
424 // FunctionType accessors
425 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
426 return FunctionType::get(Result, isVarArg);
429 FunctionType* LLVMContext::getFunctionType(const Type* Result,
430 const std::vector<const Type*>& Params,
432 return FunctionType::get(Result, Params, isVarArg);
435 // IntegerType accessors
436 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
437 return IntegerType::get(NumBits);
440 // OpaqueType accessors
441 OpaqueType* LLVMContext::getOpaqueType() {
442 return OpaqueType::get();
445 // StructType accessors
446 StructType* LLVMContext::getStructType(bool isPacked) {
447 return StructType::get(isPacked);
450 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
452 return StructType::get(Params, isPacked);
455 // ArrayType accessors
456 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
457 uint64_t NumElements) {
458 return ArrayType::get(ElementType, NumElements);
461 // PointerType accessors
462 PointerType* LLVMContext::getPointerType(const Type* ElementType,
463 unsigned AddressSpace) {
464 return PointerType::get(ElementType, AddressSpace);
467 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
468 return PointerType::getUnqual(ElementType);
471 // VectorType accessors
472 VectorType* LLVMContext::getVectorType(const Type* ElementType,
473 unsigned NumElements) {
474 return VectorType::get(ElementType, NumElements);
477 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
478 return VectorType::getInteger(VTy);
481 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
482 return VectorType::getExtendedElementVectorType(VTy);
485 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
486 return VectorType::getTruncatedElementVectorType(VTy);