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/Instruction.h"
19 #include "llvm/MDNode.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include "LLVMContextImpl.h"
26 static ManagedStatic<LLVMContext> GlobalContext;
28 LLVMContext& llvm::getGlobalContext() {
29 return *GlobalContext;
32 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl()) { }
33 LLVMContext::~LLVMContext() { delete pImpl; }
37 // Constructor to create a '0' constant of arbitrary type...
38 static const uint64_t zero[2] = {0, 0};
39 Constant* LLVMContext::getNullValue(const Type* Ty) {
40 switch (Ty->getTypeID()) {
41 case Type::IntegerTyID:
42 return getConstantInt(Ty, 0);
44 return getConstantFP(APFloat(APInt(32, 0)));
45 case Type::DoubleTyID:
46 return getConstantFP(APFloat(APInt(64, 0)));
47 case Type::X86_FP80TyID:
48 return getConstantFP(APFloat(APInt(80, 2, zero)));
50 return getConstantFP(APFloat(APInt(128, 2, zero), true));
51 case Type::PPC_FP128TyID:
52 return getConstantFP(APFloat(APInt(128, 2, zero)));
53 case Type::PointerTyID:
54 return getConstantPointerNull(cast<PointerType>(Ty));
55 case Type::StructTyID:
57 case Type::VectorTyID:
58 return getConstantAggregateZero(Ty);
60 // Function, Label, or Opaque type?
61 assert(!"Cannot create a null constant of that type!");
66 Constant* LLVMContext::getAllOnesValue(const Type* Ty) {
67 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
68 return getConstantInt(APInt::getAllOnesValue(ITy->getBitWidth()));
70 std::vector<Constant*> Elts;
71 const VectorType* VTy = cast<VectorType>(Ty);
72 Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
73 assert(Elts[0] && "Not a vector integer type!");
74 return cast<ConstantVector>(getConstantVector(Elts));
77 // UndefValue accessors.
78 UndefValue* LLVMContext::getUndef(const Type* Ty) {
79 return UndefValue::get(Ty);
82 // ConstantInt accessors.
83 ConstantInt* LLVMContext::getConstantIntTrue() {
84 return ConstantInt::getTrue();
87 ConstantInt* LLVMContext::getConstantIntFalse() {
88 return ConstantInt::getFalse();
91 Constant* LLVMContext::getConstantInt(const Type* Ty, uint64_t V,
93 return ConstantInt::get(Ty, V, isSigned);
97 ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
99 return ConstantInt::get(Ty, V, isSigned);
102 ConstantInt* LLVMContext::getConstantIntSigned(const IntegerType* Ty,
104 return ConstantInt::getSigned(Ty, V);
107 ConstantInt* LLVMContext::getConstantInt(const APInt& V) {
108 return ConstantInt::get(V);
111 Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
112 return ConstantInt::get(Ty, V);
115 // ConstantPointerNull accessors.
116 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
117 return ConstantPointerNull::get(T);
121 // ConstantStruct accessors.
122 Constant* LLVMContext::getConstantStruct(const StructType* T,
123 const std::vector<Constant*>& V) {
124 return ConstantStruct::get(T, V);
127 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
129 return ConstantStruct::get(V, Packed);
132 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
133 unsigned NumVals, bool Packed) {
134 return ConstantStruct::get(Vals, NumVals, Packed);
138 // ConstantAggregateZero accessors.
139 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
140 return ConstantAggregateZero::get(Ty);
144 // ConstantArray accessors.
145 Constant* LLVMContext::getConstantArray(const ArrayType* T,
146 const std::vector<Constant*>& V) {
147 return ConstantArray::get(T, V);
150 Constant* LLVMContext::getConstantArray(const ArrayType* T,
151 Constant* const* Vals,
153 return ConstantArray::get(T, Vals, NumVals);
156 Constant* LLVMContext::getConstantArray(const std::string& Initializer,
158 return ConstantArray::get(Initializer, AddNull);
162 // ConstantExpr accessors.
163 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
165 return ConstantExpr::get(Opcode, C1, C2);
168 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
169 return ConstantExpr::getTrunc(C, Ty);
172 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
173 return ConstantExpr::getSExt(C, Ty);
176 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
177 return ConstantExpr::getZExt(C, Ty);
180 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
181 return ConstantExpr::getFPTrunc(C, Ty);
184 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
185 return ConstantExpr::getFPExtend(C, Ty);
188 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
189 return ConstantExpr::getUIToFP(C, Ty);
192 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
193 return ConstantExpr::getSIToFP(C, Ty);
196 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
197 return ConstantExpr::getFPToUI(C, Ty);
200 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
201 return ConstantExpr::getFPToSI(C, Ty);
204 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
205 return ConstantExpr::getPtrToInt(C, Ty);
208 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
209 return ConstantExpr::getIntToPtr(C, Ty);
212 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
213 return ConstantExpr::getBitCast(C, Ty);
216 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
218 return ConstantExpr::getCast(ops, C, Ty);
221 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
223 return ConstantExpr::getZExtOrBitCast(C, Ty);
226 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
228 return ConstantExpr::getSExtOrBitCast(C, Ty);
231 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
233 return ConstantExpr::getTruncOrBitCast(C, Ty);
236 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
237 return ConstantExpr::getPointerCast(C, Ty);
240 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
242 return ConstantExpr::getIntegerCast(C, Ty, isSigned);
245 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
246 return ConstantExpr::getFPCast(C, Ty);
249 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
251 return ConstantExpr::getSelect(C, V1, V2);
254 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
255 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
256 const Type *AligningTy = getStructType(Type::Int8Ty, Ty, NULL);
257 Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
258 Constant *Zero = getConstantInt(Type::Int32Ty, 0);
259 Constant *One = getConstantInt(Type::Int32Ty, 1);
260 Constant *Indices[2] = { Zero, One };
261 Constant *GEP = getConstantExprGetElementPtr(NullPtr, Indices, 2);
262 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
265 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
266 Constant* C1, Constant* C2) {
267 return ConstantExpr::getCompare(pred, C1, C2);
270 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
271 // API compatibility: Adjust integer opcodes to floating-point opcodes.
272 if (C->getType()->isFPOrFPVector())
273 return getConstantExprFNeg(C);
274 assert(C->getType()->isIntOrIntVector() &&
275 "Cannot NEG a nonintegral value!");
276 return getConstantExpr(Instruction::Sub,
277 getZeroValueForNegation(C->getType()),
281 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
282 assert(C->getType()->isFPOrFPVector() &&
283 "Cannot FNEG a non-floating-point value!");
284 return getConstantExpr(Instruction::FSub,
285 getZeroValueForNegation(C->getType()),
289 Constant* LLVMContext::getConstantExprNot(Constant* C) {
290 assert(C->getType()->isIntOrIntVector() &&
291 "Cannot NOT a nonintegral value!");
292 return getConstantExpr(Instruction::Xor, C, getAllOnesValue(C->getType()));
295 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
296 return ConstantExpr::getAdd(C1, C2);
299 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
300 return ConstantExpr::getFAdd(C1, C2);
303 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
304 return ConstantExpr::getSub(C1, C2);
307 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
308 return ConstantExpr::getFSub(C1, C2);
311 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
312 return ConstantExpr::getMul(C1, C2);
315 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
316 return ConstantExpr::getFMul(C1, C2);
319 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
320 return ConstantExpr::getUDiv(C1, C2);
323 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
324 return ConstantExpr::getSDiv(C1, C2);
327 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
328 return ConstantExpr::getFDiv(C1, C2);
331 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
332 return ConstantExpr::getURem(C1, C2);
335 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
336 return ConstantExpr::getSRem(C1, C2);
339 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
340 return ConstantExpr::getFRem(C1, C2);
343 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
344 return ConstantExpr::getAnd(C1, C2);
347 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
348 return ConstantExpr::getOr(C1, C2);
351 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
352 return ConstantExpr::getXor(C1, C2);
355 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
357 return ConstantExpr::getICmp(pred, LHS, RHS);
360 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
362 return ConstantExpr::getFCmp(pred, LHS, RHS);
365 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
366 return ConstantExpr::getShl(C1, C2);
369 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
370 return ConstantExpr::getLShr(C1, C2);
373 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
374 return ConstantExpr::getAShr(C1, C2);
377 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
378 Constant* const* IdxList,
380 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
383 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
384 Value* const* IdxList,
386 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
389 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
391 return ConstantExpr::getExtractElement(Vec, Idx);
394 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
397 return ConstantExpr::getInsertElement(Vec, Elt, Idx);
400 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
402 return ConstantExpr::getShuffleVector(V1, V2, Mask);
405 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
406 const unsigned* IdxList,
408 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
411 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
412 const unsigned* IdxList,
414 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
417 Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
418 // sizeof is implemented as: (i64) gep (Ty*)null, 1
419 Constant *GEPIdx = getConstantInt(Type::Int32Ty, 1);
420 Constant *GEP = getConstantExprGetElementPtr(
421 getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
422 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
425 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
426 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
427 if (PTy->getElementType()->isFloatingPoint()) {
428 std::vector<Constant*> zeros(PTy->getNumElements(),
429 getConstantFPNegativeZero(PTy->getElementType()));
430 return getConstantVector(PTy, zeros);
433 if (Ty->isFloatingPoint())
434 return getConstantFPNegativeZero(Ty);
436 return getNullValue(Ty);
440 // ConstantFP accessors.
441 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
442 return ConstantFP::get(V);
445 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
446 return ConstantFP::get(Ty, V);
449 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
450 APFloat apf = cast <ConstantFP>(getNullValue(Ty))->getValueAPF();
452 return getConstantFP(apf);
456 // ConstantVector accessors.
457 Constant* LLVMContext::getConstantVector(const VectorType* T,
458 const std::vector<Constant*>& V) {
459 return ConstantVector::get(T, V);
462 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
463 return ConstantVector::get(V);
466 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
468 return ConstantVector::get(Vals, NumVals);
472 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
473 return MDNode::get(Vals, NumVals);
476 // MDString accessors
477 MDString* LLVMContext::getMDString(const char *StrBegin, const char *StrEnd) {
478 return MDString::get(StrBegin, StrEnd);
481 MDString* LLVMContext::getMDString(const std::string &Str) {
482 return MDString::get(Str);
485 // FunctionType accessors
486 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
487 return FunctionType::get(Result, isVarArg);
490 FunctionType* LLVMContext::getFunctionType(const Type* Result,
491 const std::vector<const Type*>& Params,
493 return FunctionType::get(Result, Params, isVarArg);
496 // IntegerType accessors
497 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
498 return IntegerType::get(NumBits);
501 // OpaqueType accessors
502 OpaqueType* LLVMContext::getOpaqueType() {
503 return OpaqueType::get();
506 // StructType accessors
507 StructType* LLVMContext::getStructType(bool isPacked) {
508 return StructType::get(isPacked);
511 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
513 return StructType::get(Params, isPacked);
516 StructType *LLVMContext::getStructType(const Type *type, ...) {
518 std::vector<const llvm::Type*> StructFields;
521 StructFields.push_back(type);
522 type = va_arg(ap, llvm::Type*);
524 return StructType::get(StructFields);
527 // ArrayType accessors
528 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
529 uint64_t NumElements) {
530 return ArrayType::get(ElementType, NumElements);
533 // PointerType accessors
534 PointerType* LLVMContext::getPointerType(const Type* ElementType,
535 unsigned AddressSpace) {
536 return PointerType::get(ElementType, AddressSpace);
539 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
540 return PointerType::getUnqual(ElementType);
543 // VectorType accessors
544 VectorType* LLVMContext::getVectorType(const Type* ElementType,
545 unsigned NumElements) {
546 return VectorType::get(ElementType, NumElements);
549 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
550 return VectorType::getInteger(VTy);
553 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
554 return VectorType::getExtendedElementVectorType(VTy);
557 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
558 return VectorType::getTruncatedElementVectorType(VTy);
561 const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
562 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
563 return getVectorType(Type::Int1Ty, vt->getNumElements());