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 getConstantExpr(Instruction::Add, C1, C2);
299 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
300 return getConstantExpr(Instruction::FAdd, C1, C2);
303 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
304 return getConstantExpr(Instruction::Sub, C1, C2);
307 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
308 return getConstantExpr(Instruction::FSub, C1, C2);
311 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
312 return getConstantExpr(Instruction::Mul, C1, C2);
315 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
316 return getConstantExpr(Instruction::FMul, C1, C2);
319 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
320 return getConstantExpr(Instruction::UDiv, C1, C2);
323 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
324 return getConstantExpr(Instruction::SDiv, C1, C2);
327 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
328 return getConstantExpr(Instruction::FDiv, C1, C2);
331 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
332 return getConstantExpr(Instruction::URem, C1, C2);
335 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
336 return getConstantExpr(Instruction::SRem, C1, C2);
339 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
340 return getConstantExpr(Instruction::FRem, C1, C2);
343 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
344 return getConstantExpr(Instruction::And, C1, C2);
347 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
348 return getConstantExpr(Instruction::Or, C1, C2);
351 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
352 return getConstantExpr(Instruction::Xor, 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 getConstantExpr(Instruction::Shl, C1, C2);
369 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
370 return getConstantExpr(Instruction::LShr, C1, C2);
373 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
374 return getConstantExpr(Instruction::AShr, 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 static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
446 if (Ty == Type::FloatTy)
447 return &APFloat::IEEEsingle;
448 if (Ty == Type::DoubleTy)
449 return &APFloat::IEEEdouble;
450 if (Ty == Type::X86_FP80Ty)
451 return &APFloat::x87DoubleExtended;
452 else if (Ty == Type::FP128Ty)
453 return &APFloat::IEEEquad;
455 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
456 return &APFloat::PPCDoubleDouble;
459 /// get() - This returns a constant fp for the specified value in the
460 /// specified type. This should only be used for simple constant values like
461 /// 2.0/1.0 etc, that are known-valid both as double and as the target format.
462 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
465 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
466 APFloat::rmNearestTiesToEven, &ignored);
467 Constant *C = getConstantFP(FV);
469 // For vectors, broadcast the value.
470 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
472 getConstantVector(std::vector<Constant *>(VTy->getNumElements(), C));
477 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
478 APFloat apf = cast <ConstantFP>(getNullValue(Ty))->getValueAPF();
480 return getConstantFP(apf);
484 // ConstantVector accessors.
485 Constant* LLVMContext::getConstantVector(const VectorType* T,
486 const std::vector<Constant*>& V) {
487 return ConstantVector::get(T, V);
490 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
491 return ConstantVector::get(V);
494 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
496 return ConstantVector::get(Vals, NumVals);
500 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
501 return MDNode::get(Vals, NumVals);
504 // MDString accessors
505 MDString* LLVMContext::getMDString(const char *StrBegin, const char *StrEnd) {
506 return MDString::get(StrBegin, StrEnd);
509 MDString* LLVMContext::getMDString(const std::string &Str) {
510 return MDString::get(Str);
513 // FunctionType accessors
514 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
515 return FunctionType::get(Result, isVarArg);
518 FunctionType* LLVMContext::getFunctionType(const Type* Result,
519 const std::vector<const Type*>& Params,
521 return FunctionType::get(Result, Params, isVarArg);
524 // IntegerType accessors
525 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
526 return IntegerType::get(NumBits);
529 // OpaqueType accessors
530 OpaqueType* LLVMContext::getOpaqueType() {
531 return OpaqueType::get();
534 // StructType accessors
535 StructType* LLVMContext::getStructType(bool isPacked) {
536 return StructType::get(isPacked);
539 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
541 return StructType::get(Params, isPacked);
544 StructType *LLVMContext::getStructType(const Type *type, ...) {
546 std::vector<const llvm::Type*> StructFields;
549 StructFields.push_back(type);
550 type = va_arg(ap, llvm::Type*);
552 return StructType::get(StructFields);
555 // ArrayType accessors
556 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
557 uint64_t NumElements) {
558 return ArrayType::get(ElementType, NumElements);
561 // PointerType accessors
562 PointerType* LLVMContext::getPointerType(const Type* ElementType,
563 unsigned AddressSpace) {
564 return PointerType::get(ElementType, AddressSpace);
567 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
568 return PointerType::getUnqual(ElementType);
571 // VectorType accessors
572 VectorType* LLVMContext::getVectorType(const Type* ElementType,
573 unsigned NumElements) {
574 return VectorType::get(ElementType, NumElements);
577 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
578 return VectorType::getInteger(VTy);
581 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
582 return VectorType::getExtendedElementVectorType(VTy);
585 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
586 return VectorType::getTruncatedElementVectorType(VTy);
589 const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
590 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
591 return getVectorType(Type::Int1Ty, vt->getNumElements());