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"
25 static ManagedStatic<LLVMContext> GlobalContext;
27 LLVMContext& llvm::getGlobalContext() {
28 return *GlobalContext;
31 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl()) { }
32 LLVMContext::~LLVMContext() { delete pImpl; }
36 // Constructor to create a '0' constant of arbitrary type...
37 static const uint64_t zero[2] = {0, 0};
38 Constant* LLVMContext::getNullValue(const Type* Ty) {
39 switch (Ty->getTypeID()) {
40 case Type::IntegerTyID:
41 return getConstantInt(Ty, 0);
43 return getConstantFP(APFloat(APInt(32, 0)));
44 case Type::DoubleTyID:
45 return getConstantFP(APFloat(APInt(64, 0)));
46 case Type::X86_FP80TyID:
47 return getConstantFP(APFloat(APInt(80, 2, zero)));
49 return getConstantFP(APFloat(APInt(128, 2, zero), true));
50 case Type::PPC_FP128TyID:
51 return getConstantFP(APFloat(APInt(128, 2, zero)));
52 case Type::PointerTyID:
53 return getConstantPointerNull(cast<PointerType>(Ty));
54 case Type::StructTyID:
56 case Type::VectorTyID:
57 return getConstantAggregateZero(Ty);
59 // Function, Label, or Opaque type?
60 assert(!"Cannot create a null constant of that type!");
65 Constant* LLVMContext::getAllOnesValue(const Type* Ty) {
66 return Constant::getAllOnesValue(Ty);
69 // UndefValue accessors.
70 UndefValue* LLVMContext::getUndef(const Type* Ty) {
71 return UndefValue::get(Ty);
74 // ConstantInt accessors.
75 ConstantInt* LLVMContext::getConstantIntTrue() {
76 return ConstantInt::getTrue();
79 ConstantInt* LLVMContext::getConstantIntFalse() {
80 return ConstantInt::getFalse();
83 Constant* LLVMContext::getConstantInt(const Type* Ty, uint64_t V,
85 return ConstantInt::get(Ty, V, isSigned);
89 ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
91 return ConstantInt::get(Ty, V, isSigned);
94 ConstantInt* LLVMContext::getConstantIntSigned(const IntegerType* Ty,
96 return ConstantInt::getSigned(Ty, V);
99 ConstantInt* LLVMContext::getConstantInt(const APInt& V) {
100 return ConstantInt::get(V);
103 Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
104 return ConstantInt::get(Ty, V);
107 ConstantInt* LLVMContext::getConstantIntAllOnesValue(const Type* Ty) {
108 return ConstantInt::getAllOnesValue(Ty);
112 // ConstantPointerNull accessors.
113 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
114 return ConstantPointerNull::get(T);
118 // ConstantStruct accessors.
119 Constant* LLVMContext::getConstantStruct(const StructType* T,
120 const std::vector<Constant*>& V) {
121 return ConstantStruct::get(T, V);
124 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
126 return ConstantStruct::get(V, Packed);
129 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
130 unsigned NumVals, bool Packed) {
131 return ConstantStruct::get(Vals, NumVals, Packed);
135 // ConstantAggregateZero accessors.
136 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
137 return ConstantAggregateZero::get(Ty);
141 // ConstantArray accessors.
142 Constant* LLVMContext::getConstantArray(const ArrayType* T,
143 const std::vector<Constant*>& V) {
144 return ConstantArray::get(T, V);
147 Constant* LLVMContext::getConstantArray(const ArrayType* T,
148 Constant* const* Vals,
150 return ConstantArray::get(T, Vals, NumVals);
153 Constant* LLVMContext::getConstantArray(const std::string& Initializer,
155 return ConstantArray::get(Initializer, AddNull);
159 // ConstantExpr accessors.
160 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
162 return ConstantExpr::get(Opcode, C1, C2);
165 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
166 return ConstantExpr::getTrunc(C, Ty);
169 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
170 return ConstantExpr::getSExt(C, Ty);
173 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
174 return ConstantExpr::getZExt(C, Ty);
177 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
178 return ConstantExpr::getFPTrunc(C, Ty);
181 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
182 return ConstantExpr::getFPExtend(C, Ty);
185 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
186 return ConstantExpr::getUIToFP(C, Ty);
189 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
190 return ConstantExpr::getSIToFP(C, Ty);
193 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
194 return ConstantExpr::getFPToUI(C, Ty);
197 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
198 return ConstantExpr::getFPToSI(C, Ty);
201 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
202 return ConstantExpr::getPtrToInt(C, Ty);
205 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
206 return ConstantExpr::getIntToPtr(C, Ty);
209 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
210 return ConstantExpr::getBitCast(C, Ty);
213 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
215 return ConstantExpr::getCast(ops, C, Ty);
218 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
220 return ConstantExpr::getZExtOrBitCast(C, Ty);
223 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
225 return ConstantExpr::getSExtOrBitCast(C, Ty);
228 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
230 return ConstantExpr::getTruncOrBitCast(C, Ty);
233 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
234 return ConstantExpr::getPointerCast(C, Ty);
237 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
239 return ConstantExpr::getIntegerCast(C, Ty, isSigned);
242 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
243 return ConstantExpr::getFPCast(C, Ty);
246 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
248 return ConstantExpr::getSelect(C, V1, V2);
251 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
252 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
253 const Type *AligningTy = getStructType(Type::Int8Ty, Ty, NULL);
254 Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
255 Constant *Zero = getConstantInt(Type::Int32Ty, 0);
256 Constant *One = getConstantInt(Type::Int32Ty, 1);
257 Constant *Indices[2] = { Zero, One };
258 Constant *GEP = getConstantExprGetElementPtr(NullPtr, Indices, 2);
259 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
262 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
263 Constant* C1, Constant* C2) {
264 return ConstantExpr::getCompare(pred, C1, C2);
267 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
268 // API compatibility: Adjust integer opcodes to floating-point opcodes.
269 if (C->getType()->isFPOrFPVector())
270 return getConstantExprFNeg(C);
271 assert(C->getType()->isIntOrIntVector() &&
272 "Cannot NEG a nonintegral value!");
273 return getConstantExpr(Instruction::Sub,
274 getZeroValueForNegation(C->getType()),
278 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
279 assert(C->getType()->isFPOrFPVector() &&
280 "Cannot FNEG a non-floating-point value!");
281 return getConstantExpr(Instruction::FSub,
282 getZeroValueForNegation(C->getType()),
286 Constant* LLVMContext::getConstantExprNot(Constant* C) {
287 return ConstantExpr::getNot(C);
290 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
291 return ConstantExpr::getAdd(C1, C2);
294 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
295 return ConstantExpr::getFAdd(C1, C2);
298 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
299 return ConstantExpr::getSub(C1, C2);
302 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
303 return ConstantExpr::getFSub(C1, C2);
306 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
307 return ConstantExpr::getMul(C1, C2);
310 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
311 return ConstantExpr::getFMul(C1, C2);
314 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
315 return ConstantExpr::getUDiv(C1, C2);
318 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
319 return ConstantExpr::getSDiv(C1, C2);
322 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
323 return ConstantExpr::getFDiv(C1, C2);
326 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
327 return ConstantExpr::getURem(C1, C2);
330 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
331 return ConstantExpr::getSRem(C1, C2);
334 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
335 return ConstantExpr::getFRem(C1, C2);
338 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
339 return ConstantExpr::getAnd(C1, C2);
342 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
343 return ConstantExpr::getOr(C1, C2);
346 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
347 return ConstantExpr::getXor(C1, C2);
350 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
352 return ConstantExpr::getICmp(pred, LHS, RHS);
355 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
357 return ConstantExpr::getFCmp(pred, LHS, RHS);
360 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
361 return ConstantExpr::getShl(C1, C2);
364 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
365 return ConstantExpr::getLShr(C1, C2);
368 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
369 return ConstantExpr::getAShr(C1, C2);
372 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
373 Constant* const* IdxList,
375 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
378 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
379 Value* const* IdxList,
381 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
384 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
386 return ConstantExpr::getExtractElement(Vec, Idx);
389 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
392 return ConstantExpr::getInsertElement(Vec, Elt, Idx);
395 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
397 return ConstantExpr::getShuffleVector(V1, V2, Mask);
400 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
401 const unsigned* IdxList,
403 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
406 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
407 const unsigned* IdxList,
409 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
412 Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
413 // sizeof is implemented as: (i64) gep (Ty*)null, 1
414 Constant *GEPIdx = getConstantInt(Type::Int32Ty, 1);
415 Constant *GEP = getConstantExprGetElementPtr(
416 getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
417 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
420 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
421 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
422 if (PTy->getElementType()->isFloatingPoint()) {
423 std::vector<Constant*> zeros(PTy->getNumElements(),
424 getConstantFPNegativeZero(PTy->getElementType()));
425 return getConstantVector(PTy, zeros);
428 if (Ty->isFloatingPoint())
429 return getConstantFPNegativeZero(Ty);
431 return getNullValue(Ty);
435 // ConstantFP accessors.
436 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
437 return ConstantFP::get(V);
440 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
441 return ConstantFP::get(Ty, V);
444 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
445 APFloat apf = cast <ConstantFP>(getNullValue(Ty))->getValueAPF();
447 return getConstantFP(apf);
451 // ConstantVector accessors.
452 Constant* LLVMContext::getConstantVector(const VectorType* T,
453 const std::vector<Constant*>& V) {
454 return ConstantVector::get(T, V);
457 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
458 return ConstantVector::get(V);
461 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
463 return ConstantVector::get(Vals, NumVals);
466 ConstantVector* LLVMContext::getConstantVectorAllOnesValue(
467 const VectorType* Ty) {
468 return ConstantVector::getAllOnesValue(Ty);
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());