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 Constant *C = getConstantInt(cast<IntegerType>(Ty->getScalarType()),
96 // For vectors, broadcast the value.
97 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
99 getConstantVector(std::vector<Constant *>(VTy->getNumElements(), C));
105 ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
107 return getConstantInt(APInt(Ty->getBitWidth(), V, isSigned));
110 ConstantInt* LLVMContext::getConstantIntSigned(const IntegerType* Ty,
112 return getConstantInt(Ty, V, true);
115 Constant *LLVMContext::getConstantIntSigned(const Type *Ty, int64_t V) {
116 return getConstantInt(Ty, V, true);
119 ConstantInt* LLVMContext::getConstantInt(const APInt& V) {
120 return ConstantInt::get(V);
123 Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
124 ConstantInt *C = getConstantInt(V);
125 assert(C->getType() == Ty->getScalarType() &&
126 "ConstantInt type doesn't match the type implied by its value!");
128 // For vectors, broadcast the value.
129 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
131 ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
136 // ConstantPointerNull accessors.
137 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
138 return ConstantPointerNull::get(T);
142 // ConstantStruct accessors.
143 Constant* LLVMContext::getConstantStruct(const StructType* T,
144 const std::vector<Constant*>& V) {
145 return ConstantStruct::get(T, V);
148 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
150 return ConstantStruct::get(V, Packed);
153 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
154 unsigned NumVals, bool Packed) {
155 return ConstantStruct::get(Vals, NumVals, Packed);
159 // ConstantAggregateZero accessors.
160 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
161 return ConstantAggregateZero::get(Ty);
165 // ConstantArray accessors.
166 Constant* LLVMContext::getConstantArray(const ArrayType* T,
167 const std::vector<Constant*>& V) {
168 return ConstantArray::get(T, V);
171 Constant* LLVMContext::getConstantArray(const ArrayType* T,
172 Constant* const* Vals,
174 return ConstantArray::get(T, Vals, NumVals);
177 /// ConstantArray::get(const string&) - Return an array that is initialized to
178 /// contain the specified string. If length is zero then a null terminator is
179 /// added to the specified string so that it may be used in a natural way.
180 /// Otherwise, the length parameter specifies how much of the string to use
181 /// and it won't be null terminated.
183 Constant* LLVMContext::getConstantArray(const std::string& Str,
185 std::vector<Constant*> ElementVals;
186 for (unsigned i = 0; i < Str.length(); ++i)
187 ElementVals.push_back(getConstantInt(Type::Int8Ty, Str[i]));
189 // Add a null terminator to the string...
191 ElementVals.push_back(getConstantInt(Type::Int8Ty, 0));
194 ArrayType *ATy = getArrayType(Type::Int8Ty, ElementVals.size());
195 return getConstantArray(ATy, ElementVals);
199 // ConstantExpr accessors.
200 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
202 return ConstantExpr::get(Opcode, C1, C2);
205 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
206 return ConstantExpr::getTrunc(C, Ty);
209 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
210 return ConstantExpr::getSExt(C, Ty);
213 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
214 return ConstantExpr::getZExt(C, Ty);
217 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
218 return ConstantExpr::getFPTrunc(C, Ty);
221 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
222 return ConstantExpr::getFPExtend(C, Ty);
225 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
226 return ConstantExpr::getUIToFP(C, Ty);
229 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
230 return ConstantExpr::getSIToFP(C, Ty);
233 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
234 return ConstantExpr::getFPToUI(C, Ty);
237 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
238 return ConstantExpr::getFPToSI(C, Ty);
241 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
242 return ConstantExpr::getPtrToInt(C, Ty);
245 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
246 return ConstantExpr::getIntToPtr(C, Ty);
249 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
250 return ConstantExpr::getBitCast(C, Ty);
253 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
255 return ConstantExpr::getCast(ops, C, Ty);
258 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
260 return ConstantExpr::getZExtOrBitCast(C, Ty);
263 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
265 return ConstantExpr::getSExtOrBitCast(C, Ty);
268 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
270 return ConstantExpr::getTruncOrBitCast(C, Ty);
273 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
274 return ConstantExpr::getPointerCast(C, Ty);
277 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
279 return ConstantExpr::getIntegerCast(C, Ty, isSigned);
282 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
283 return ConstantExpr::getFPCast(C, Ty);
286 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
288 return ConstantExpr::getSelect(C, V1, V2);
291 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
292 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
293 const Type *AligningTy = getStructType(Type::Int8Ty, Ty, NULL);
294 Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
295 Constant *Zero = getConstantInt(Type::Int32Ty, 0);
296 Constant *One = getConstantInt(Type::Int32Ty, 1);
297 Constant *Indices[2] = { Zero, One };
298 Constant *GEP = getConstantExprGetElementPtr(NullPtr, Indices, 2);
299 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
302 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
303 Constant* C1, Constant* C2) {
304 return ConstantExpr::getCompare(pred, C1, C2);
307 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
308 // API compatibility: Adjust integer opcodes to floating-point opcodes.
309 if (C->getType()->isFPOrFPVector())
310 return getConstantExprFNeg(C);
311 assert(C->getType()->isIntOrIntVector() &&
312 "Cannot NEG a nonintegral value!");
313 return getConstantExpr(Instruction::Sub,
314 getZeroValueForNegation(C->getType()),
318 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
319 assert(C->getType()->isFPOrFPVector() &&
320 "Cannot FNEG a non-floating-point value!");
321 return getConstantExpr(Instruction::FSub,
322 getZeroValueForNegation(C->getType()),
326 Constant* LLVMContext::getConstantExprNot(Constant* C) {
327 assert(C->getType()->isIntOrIntVector() &&
328 "Cannot NOT a nonintegral value!");
329 return getConstantExpr(Instruction::Xor, C, getAllOnesValue(C->getType()));
332 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
333 return getConstantExpr(Instruction::Add, C1, C2);
336 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
337 return getConstantExpr(Instruction::FAdd, C1, C2);
340 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
341 return getConstantExpr(Instruction::Sub, C1, C2);
344 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
345 return getConstantExpr(Instruction::FSub, C1, C2);
348 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
349 return getConstantExpr(Instruction::Mul, C1, C2);
352 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
353 return getConstantExpr(Instruction::FMul, C1, C2);
356 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
357 return getConstantExpr(Instruction::UDiv, C1, C2);
360 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
361 return getConstantExpr(Instruction::SDiv, C1, C2);
364 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
365 return getConstantExpr(Instruction::FDiv, C1, C2);
368 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
369 return getConstantExpr(Instruction::URem, C1, C2);
372 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
373 return getConstantExpr(Instruction::SRem, C1, C2);
376 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
377 return getConstantExpr(Instruction::FRem, C1, C2);
380 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
381 return getConstantExpr(Instruction::And, C1, C2);
384 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
385 return getConstantExpr(Instruction::Or, C1, C2);
388 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
389 return getConstantExpr(Instruction::Xor, C1, C2);
392 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
394 return ConstantExpr::getICmp(pred, LHS, RHS);
397 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
399 return ConstantExpr::getFCmp(pred, LHS, RHS);
402 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
403 return getConstantExpr(Instruction::Shl, C1, C2);
406 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
407 return getConstantExpr(Instruction::LShr, C1, C2);
410 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
411 return getConstantExpr(Instruction::AShr, C1, C2);
414 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
415 Constant* const* IdxList,
417 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
420 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
421 Value* const* IdxList,
423 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
426 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
428 return ConstantExpr::getExtractElement(Vec, Idx);
431 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
434 return ConstantExpr::getInsertElement(Vec, Elt, Idx);
437 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
439 return ConstantExpr::getShuffleVector(V1, V2, Mask);
442 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
443 const unsigned* IdxList,
445 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
448 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
449 const unsigned* IdxList,
451 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
454 Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
455 // sizeof is implemented as: (i64) gep (Ty*)null, 1
456 Constant *GEPIdx = getConstantInt(Type::Int32Ty, 1);
457 Constant *GEP = getConstantExprGetElementPtr(
458 getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
459 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
462 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
463 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
464 if (PTy->getElementType()->isFloatingPoint()) {
465 std::vector<Constant*> zeros(PTy->getNumElements(),
466 getConstantFPNegativeZero(PTy->getElementType()));
467 return getConstantVector(PTy, zeros);
470 if (Ty->isFloatingPoint())
471 return getConstantFPNegativeZero(Ty);
473 return getNullValue(Ty);
477 // ConstantFP accessors.
478 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
479 return ConstantFP::get(V);
482 static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
483 if (Ty == Type::FloatTy)
484 return &APFloat::IEEEsingle;
485 if (Ty == Type::DoubleTy)
486 return &APFloat::IEEEdouble;
487 if (Ty == Type::X86_FP80Ty)
488 return &APFloat::x87DoubleExtended;
489 else if (Ty == Type::FP128Ty)
490 return &APFloat::IEEEquad;
492 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
493 return &APFloat::PPCDoubleDouble;
496 /// get() - This returns a constant fp for the specified value in the
497 /// specified type. This should only be used for simple constant values like
498 /// 2.0/1.0 etc, that are known-valid both as double and as the target format.
499 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
502 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
503 APFloat::rmNearestTiesToEven, &ignored);
504 Constant *C = getConstantFP(FV);
506 // For vectors, broadcast the value.
507 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
509 getConstantVector(std::vector<Constant *>(VTy->getNumElements(), C));
514 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
515 APFloat apf = cast <ConstantFP>(getNullValue(Ty))->getValueAPF();
517 return getConstantFP(apf);
521 // ConstantVector accessors.
522 Constant* LLVMContext::getConstantVector(const VectorType* T,
523 const std::vector<Constant*>& V) {
524 return ConstantVector::get(T, V);
527 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
528 return ConstantVector::get(V);
531 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
533 return ConstantVector::get(Vals, NumVals);
537 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
538 return MDNode::get(Vals, NumVals);
541 // MDString accessors
542 MDString* LLVMContext::getMDString(const char *StrBegin, const char *StrEnd) {
543 return MDString::get(StrBegin, StrEnd);
546 MDString* LLVMContext::getMDString(const std::string &Str) {
547 return MDString::get(Str);
550 // FunctionType accessors
551 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
552 return FunctionType::get(Result, isVarArg);
555 FunctionType* LLVMContext::getFunctionType(const Type* Result,
556 const std::vector<const Type*>& Params,
558 return FunctionType::get(Result, Params, isVarArg);
561 // IntegerType accessors
562 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
563 return IntegerType::get(NumBits);
566 // OpaqueType accessors
567 OpaqueType* LLVMContext::getOpaqueType() {
568 return OpaqueType::get();
571 // StructType accessors
572 StructType* LLVMContext::getStructType(bool isPacked) {
573 return StructType::get(isPacked);
576 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
578 return StructType::get(Params, isPacked);
581 StructType *LLVMContext::getStructType(const Type *type, ...) {
583 std::vector<const llvm::Type*> StructFields;
586 StructFields.push_back(type);
587 type = va_arg(ap, llvm::Type*);
589 return StructType::get(StructFields);
592 // ArrayType accessors
593 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
594 uint64_t NumElements) {
595 return ArrayType::get(ElementType, NumElements);
598 // PointerType accessors
599 PointerType* LLVMContext::getPointerType(const Type* ElementType,
600 unsigned AddressSpace) {
601 return PointerType::get(ElementType, AddressSpace);
604 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
605 return PointerType::getUnqual(ElementType);
608 // VectorType accessors
609 VectorType* LLVMContext::getVectorType(const Type* ElementType,
610 unsigned NumElements) {
611 return VectorType::get(ElementType, NumElements);
614 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
615 return VectorType::getInteger(VTy);
618 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
619 return VectorType::getExtendedElementVectorType(VTy);
622 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
623 return VectorType::getTruncatedElementVectorType(VTy);
626 const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
627 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
628 return getVectorType(Type::Int1Ty, vt->getNumElements());