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 return Constant::getAllOnesValue(Ty);
70 // UndefValue accessors.
71 UndefValue* LLVMContext::getUndef(const Type* Ty) {
72 return UndefValue::get(Ty);
75 // ConstantInt accessors.
76 ConstantInt* LLVMContext::getConstantIntTrue() {
77 return ConstantInt::getTrue();
80 ConstantInt* LLVMContext::getConstantIntFalse() {
81 return ConstantInt::getFalse();
84 Constant* LLVMContext::getConstantInt(const Type* Ty, uint64_t V,
86 return ConstantInt::get(Ty, V, isSigned);
90 ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
92 return ConstantInt::get(Ty, V, isSigned);
95 ConstantInt* LLVMContext::getConstantIntSigned(const IntegerType* Ty,
97 return ConstantInt::getSigned(Ty, V);
100 ConstantInt* LLVMContext::getConstantInt(const APInt& V) {
101 return ConstantInt::get(V);
104 Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
105 return ConstantInt::get(Ty, V);
108 ConstantInt* LLVMContext::getConstantIntAllOnesValue(const Type* Ty) {
109 return ConstantInt::getAllOnesValue(Ty);
113 // ConstantPointerNull accessors.
114 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
115 return ConstantPointerNull::get(T);
119 // ConstantStruct accessors.
120 Constant* LLVMContext::getConstantStruct(const StructType* T,
121 const std::vector<Constant*>& V) {
122 return ConstantStruct::get(T, V);
125 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
127 return ConstantStruct::get(V, Packed);
130 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
131 unsigned NumVals, bool Packed) {
132 return ConstantStruct::get(Vals, NumVals, Packed);
136 // ConstantAggregateZero accessors.
137 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
138 return ConstantAggregateZero::get(Ty);
142 // ConstantArray accessors.
143 Constant* LLVMContext::getConstantArray(const ArrayType* T,
144 const std::vector<Constant*>& V) {
145 return ConstantArray::get(T, V);
148 Constant* LLVMContext::getConstantArray(const ArrayType* T,
149 Constant* const* Vals,
151 return ConstantArray::get(T, Vals, NumVals);
154 Constant* LLVMContext::getConstantArray(const std::string& Initializer,
156 return ConstantArray::get(Initializer, AddNull);
160 // ConstantExpr accessors.
161 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
163 return ConstantExpr::get(Opcode, C1, C2);
166 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
167 return ConstantExpr::getTrunc(C, Ty);
170 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
171 return ConstantExpr::getSExt(C, Ty);
174 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
175 return ConstantExpr::getZExt(C, Ty);
178 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
179 return ConstantExpr::getFPTrunc(C, Ty);
182 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
183 return ConstantExpr::getFPExtend(C, Ty);
186 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
187 return ConstantExpr::getUIToFP(C, Ty);
190 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
191 return ConstantExpr::getSIToFP(C, Ty);
194 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
195 return ConstantExpr::getFPToUI(C, Ty);
198 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
199 return ConstantExpr::getFPToSI(C, Ty);
202 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
203 return ConstantExpr::getPtrToInt(C, Ty);
206 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
207 return ConstantExpr::getIntToPtr(C, Ty);
210 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
211 return ConstantExpr::getBitCast(C, Ty);
214 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
216 return ConstantExpr::getCast(ops, C, Ty);
219 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
221 return ConstantExpr::getZExtOrBitCast(C, Ty);
224 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
226 return ConstantExpr::getSExtOrBitCast(C, Ty);
229 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
231 return ConstantExpr::getTruncOrBitCast(C, Ty);
234 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
235 return ConstantExpr::getPointerCast(C, Ty);
238 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
240 return ConstantExpr::getIntegerCast(C, Ty, isSigned);
243 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
244 return ConstantExpr::getFPCast(C, Ty);
247 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
249 return ConstantExpr::getSelect(C, V1, V2);
252 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
253 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
254 const Type *AligningTy = getStructType(Type::Int8Ty, Ty, NULL);
255 Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
256 Constant *Zero = getConstantInt(Type::Int32Ty, 0);
257 Constant *One = getConstantInt(Type::Int32Ty, 1);
258 Constant *Indices[2] = { Zero, One };
259 Constant *GEP = getConstantExprGetElementPtr(NullPtr, Indices, 2);
260 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
263 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
264 Constant* C1, Constant* C2) {
265 return ConstantExpr::getCompare(pred, C1, C2);
268 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
269 // API compatibility: Adjust integer opcodes to floating-point opcodes.
270 if (C->getType()->isFPOrFPVector())
271 return getConstantExprFNeg(C);
272 assert(C->getType()->isIntOrIntVector() &&
273 "Cannot NEG a nonintegral value!");
274 return getConstantExpr(Instruction::Sub,
275 getZeroValueForNegation(C->getType()),
279 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
280 assert(C->getType()->isFPOrFPVector() &&
281 "Cannot FNEG a non-floating-point value!");
282 return getConstantExpr(Instruction::FSub,
283 getZeroValueForNegation(C->getType()),
287 Constant* LLVMContext::getConstantExprNot(Constant* C) {
288 return ConstantExpr::getNot(C);
291 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
292 return ConstantExpr::getAdd(C1, C2);
295 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
296 return ConstantExpr::getFAdd(C1, C2);
299 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
300 return ConstantExpr::getSub(C1, C2);
303 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
304 return ConstantExpr::getFSub(C1, C2);
307 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
308 return ConstantExpr::getMul(C1, C2);
311 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
312 return ConstantExpr::getFMul(C1, C2);
315 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
316 return ConstantExpr::getUDiv(C1, C2);
319 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
320 return ConstantExpr::getSDiv(C1, C2);
323 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
324 return ConstantExpr::getFDiv(C1, C2);
327 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
328 return ConstantExpr::getURem(C1, C2);
331 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
332 return ConstantExpr::getSRem(C1, C2);
335 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
336 return ConstantExpr::getFRem(C1, C2);
339 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
340 return ConstantExpr::getAnd(C1, C2);
343 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
344 return ConstantExpr::getOr(C1, C2);
347 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
348 return ConstantExpr::getXor(C1, C2);
351 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
353 return ConstantExpr::getICmp(pred, LHS, RHS);
356 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
358 return ConstantExpr::getFCmp(pred, LHS, RHS);
361 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
362 return ConstantExpr::getShl(C1, C2);
365 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
366 return ConstantExpr::getLShr(C1, C2);
369 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
370 return ConstantExpr::getAShr(C1, C2);
373 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
374 Constant* const* IdxList,
376 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
379 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
380 Value* const* IdxList,
382 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
385 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
387 return ConstantExpr::getExtractElement(Vec, Idx);
390 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
393 return ConstantExpr::getInsertElement(Vec, Elt, Idx);
396 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
398 return ConstantExpr::getShuffleVector(V1, V2, Mask);
401 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
402 const unsigned* IdxList,
404 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
407 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
408 const unsigned* IdxList,
410 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
413 Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
414 // sizeof is implemented as: (i64) gep (Ty*)null, 1
415 Constant *GEPIdx = getConstantInt(Type::Int32Ty, 1);
416 Constant *GEP = getConstantExprGetElementPtr(
417 getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
418 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
421 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
422 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
423 if (PTy->getElementType()->isFloatingPoint()) {
424 std::vector<Constant*> zeros(PTy->getNumElements(),
425 getConstantFPNegativeZero(PTy->getElementType()));
426 return getConstantVector(PTy, zeros);
429 if (Ty->isFloatingPoint())
430 return getConstantFPNegativeZero(Ty);
432 return getNullValue(Ty);
436 // ConstantFP accessors.
437 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
438 return ConstantFP::get(V);
441 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
442 return ConstantFP::get(Ty, V);
445 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
446 APFloat apf = cast <ConstantFP>(getNullValue(Ty))->getValueAPF();
448 return getConstantFP(apf);
452 // ConstantVector accessors.
453 Constant* LLVMContext::getConstantVector(const VectorType* T,
454 const std::vector<Constant*>& V) {
455 return ConstantVector::get(T, V);
458 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
459 return ConstantVector::get(V);
462 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
464 return ConstantVector::get(Vals, NumVals);
467 ConstantVector* LLVMContext::getConstantVectorAllOnesValue(
468 const VectorType* Ty) {
469 return ConstantVector::getAllOnesValue(Ty);
473 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
474 return MDNode::get(Vals, NumVals);
477 // MDString accessors
478 MDString* LLVMContext::getMDString(const char *StrBegin, const char *StrEnd) {
479 return MDString::get(StrBegin, StrEnd);
482 MDString* LLVMContext::getMDString(const std::string &Str) {
483 return MDString::get(Str);
486 // FunctionType accessors
487 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
488 return FunctionType::get(Result, isVarArg);
491 FunctionType* LLVMContext::getFunctionType(const Type* Result,
492 const std::vector<const Type*>& Params,
494 return FunctionType::get(Result, Params, isVarArg);
497 // IntegerType accessors
498 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
499 return IntegerType::get(NumBits);
502 // OpaqueType accessors
503 OpaqueType* LLVMContext::getOpaqueType() {
504 return OpaqueType::get();
507 // StructType accessors
508 StructType* LLVMContext::getStructType(bool isPacked) {
509 return StructType::get(isPacked);
512 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
514 return StructType::get(Params, isPacked);
517 StructType *LLVMContext::getStructType(const Type *type, ...) {
519 std::vector<const llvm::Type*> StructFields;
522 StructFields.push_back(type);
523 type = va_arg(ap, llvm::Type*);
525 return StructType::get(StructFields);
528 // ArrayType accessors
529 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
530 uint64_t NumElements) {
531 return ArrayType::get(ElementType, NumElements);
534 // PointerType accessors
535 PointerType* LLVMContext::getPointerType(const Type* ElementType,
536 unsigned AddressSpace) {
537 return PointerType::get(ElementType, AddressSpace);
540 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
541 return PointerType::getUnqual(ElementType);
544 // VectorType accessors
545 VectorType* LLVMContext::getVectorType(const Type* ElementType,
546 unsigned NumElements) {
547 return VectorType::get(ElementType, NumElements);
550 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
551 return VectorType::getInteger(VTy);
554 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
555 return VectorType::getExtendedElementVectorType(VTy);
558 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
559 return VectorType::getTruncatedElementVectorType(VTy);
562 const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
563 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
564 return getVectorType(Type::Int1Ty, vt->getNumElements());