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::getConstantExprVICmp(unsigned short pred, Constant* LHS,
317 return ConstantExpr::getVICmp(pred, LHS, RHS);
320 Constant* LLVMContext::getConstantExprVFCmp(unsigned short pred, Constant* LHS,
322 return ConstantExpr::getVFCmp(pred, LHS, RHS);
325 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
326 return ConstantExpr::getShl(C1, C2);
329 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
330 return ConstantExpr::getLShr(C1, C2);
333 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
334 return ConstantExpr::getAShr(C1, C2);
337 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
338 Constant* const* IdxList,
340 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
343 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
344 Value* const* IdxList,
346 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
349 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
351 return ConstantExpr::getExtractElement(Vec, Idx);
354 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
357 return ConstantExpr::getInsertElement(Vec, Elt, Idx);
360 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
362 return ConstantExpr::getShuffleVector(V1, V2, Mask);
365 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
366 const unsigned* IdxList,
368 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
371 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
372 const unsigned* IdxList,
374 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
377 Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
378 return ConstantExpr::getSizeOf(Ty);
381 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
382 return ConstantExpr::getZeroValueForNegationExpr(Ty);
386 // ConstantFP accessors.
387 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
388 return ConstantFP::get(V);
391 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
392 return ConstantFP::get(Ty, V);
395 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
396 return ConstantFP::getNegativeZero(Ty);
400 // ConstantVector accessors.
401 Constant* LLVMContext::getConstantVector(const VectorType* T,
402 const std::vector<Constant*>& V) {
403 return ConstantVector::get(T, V);
406 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
407 return ConstantVector::get(V);
410 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
412 return ConstantVector::get(Vals, NumVals);
415 ConstantVector* LLVMContext::getConstantVectorAllOnesValue(
416 const VectorType* Ty) {
417 return ConstantVector::getAllOnesValue(Ty);
421 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
422 return MDNode::get(Vals, NumVals);
425 // MDString accessors
426 MDString* LLVMContext::getMDString(const char *StrBegin, const char *StrEnd) {
427 return MDString::get(StrBegin, StrEnd);
430 MDString* LLVMContext::getMDString(const std::string &Str) {
431 return MDString::get(Str);
434 // FunctionType accessors
435 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
436 return FunctionType::get(Result, isVarArg);
439 FunctionType* LLVMContext::getFunctionType(const Type* Result,
440 const std::vector<const Type*>& Params,
442 return FunctionType::get(Result, Params, isVarArg);
445 // IntegerType accessors
446 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
447 return IntegerType::get(NumBits);
450 // OpaqueType accessors
451 OpaqueType* LLVMContext::getOpaqueType() {
452 return OpaqueType::get();
455 // StructType accessors
456 StructType* LLVMContext::getStructType(bool isPacked) {
457 return StructType::get(isPacked);
460 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
462 return StructType::get(Params, isPacked);
465 // ArrayType accessors
466 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
467 uint64_t NumElements) {
468 return ArrayType::get(ElementType, NumElements);
471 // PointerType accessors
472 PointerType* LLVMContext::getPointerType(const Type* ElementType,
473 unsigned AddressSpace) {
474 return PointerType::get(ElementType, AddressSpace);
477 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
478 return PointerType::getUnqual(ElementType);
481 // VectorType accessors
482 VectorType* LLVMContext::getVectorType(const Type* ElementType,
483 unsigned NumElements) {
484 return VectorType::get(ElementType, NumElements);
487 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
488 return VectorType::getInteger(VTy);
491 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
492 return VectorType::getExtendedElementVectorType(VTy);
495 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
496 return VectorType::getTruncatedElementVectorType(VTy);