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/Metadata.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(*this)) { }
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 ConstantInt::get(Ty, 0);
44 return ConstantFP::get(Ty->getContext(), APFloat(APInt(32, 0)));
45 case Type::DoubleTyID:
46 return ConstantFP::get(Ty->getContext(), APFloat(APInt(64, 0)));
47 case Type::X86_FP80TyID:
48 return ConstantFP::get(Ty->getContext(), APFloat(APInt(80, 2, zero)));
50 return ConstantFP::get(Ty->getContext(),
51 APFloat(APInt(128, 2, zero), true));
52 case Type::PPC_FP128TyID:
53 return ConstantFP::get(Ty->getContext(), APFloat(APInt(128, 2, zero)));
54 case Type::PointerTyID:
55 return getConstantPointerNull(cast<PointerType>(Ty));
56 case Type::StructTyID:
58 case Type::VectorTyID:
59 return getConstantAggregateZero(Ty);
61 // Function, Label, or Opaque type?
62 assert(!"Cannot create a null constant of that type!");
67 Constant* LLVMContext::getAllOnesValue(const Type* Ty) {
68 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
69 return ConstantInt::get(*this, APInt::getAllOnesValue(ITy->getBitWidth()));
71 std::vector<Constant*> Elts;
72 const VectorType* VTy = cast<VectorType>(Ty);
73 Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
74 assert(Elts[0] && "Not a vector integer type!");
75 return cast<ConstantVector>(ConstantVector::get(Elts));
78 // UndefValue accessors.
79 UndefValue* LLVMContext::getUndef(const Type* Ty) {
80 return UndefValue::get(Ty);
83 // ConstantInt accessors.
84 ConstantInt* LLVMContext::getTrue() {
85 assert(this && "Context not initialized!");
86 assert(pImpl && "Context not initialized!");
87 return pImpl->getTrue();
90 ConstantInt* LLVMContext::getFalse() {
91 assert(this && "Context not initialized!");
92 assert(pImpl && "Context not initialized!");
93 return pImpl->getFalse();
96 // ConstantPointerNull accessors.
97 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
98 return ConstantPointerNull::get(T);
101 // ConstantAggregateZero accessors.
102 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
103 return pImpl->getConstantAggregateZero(Ty);
106 // ConstantExpr accessors.
107 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
109 return ConstantExpr::get(Opcode, C1, C2);
112 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
113 return ConstantExpr::getTrunc(C, Ty);
116 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
117 return ConstantExpr::getSExt(C, Ty);
120 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
121 return ConstantExpr::getZExt(C, Ty);
124 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
125 return ConstantExpr::getFPTrunc(C, Ty);
128 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
129 return ConstantExpr::getFPExtend(C, Ty);
132 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
133 return ConstantExpr::getUIToFP(C, Ty);
136 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
137 return ConstantExpr::getSIToFP(C, Ty);
140 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
141 return ConstantExpr::getFPToUI(C, Ty);
144 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
145 return ConstantExpr::getFPToSI(C, Ty);
148 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
149 return ConstantExpr::getPtrToInt(C, Ty);
152 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
153 return ConstantExpr::getIntToPtr(C, Ty);
156 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
157 return ConstantExpr::getBitCast(C, Ty);
160 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
162 return ConstantExpr::getCast(ops, C, Ty);
165 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
167 return ConstantExpr::getZExtOrBitCast(C, Ty);
170 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
172 return ConstantExpr::getSExtOrBitCast(C, Ty);
175 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
177 return ConstantExpr::getTruncOrBitCast(C, Ty);
180 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
181 return ConstantExpr::getPointerCast(C, Ty);
184 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
186 return ConstantExpr::getIntegerCast(C, Ty, isSigned);
189 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
190 return ConstantExpr::getFPCast(C, Ty);
193 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
195 return ConstantExpr::getSelect(C, V1, V2);
198 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
199 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
200 const Type *AligningTy = getStructType(Type::Int8Ty, Ty, NULL);
201 Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
202 Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
203 Constant *One = ConstantInt::get(Type::Int32Ty, 1);
204 Constant *Indices[2] = { Zero, One };
205 Constant *GEP = getConstantExprGetElementPtr(NullPtr, Indices, 2);
206 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
209 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
210 Constant* C1, Constant* C2) {
211 return ConstantExpr::getCompare(pred, C1, C2);
214 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
215 // API compatibility: Adjust integer opcodes to floating-point opcodes.
216 if (C->getType()->isFPOrFPVector())
217 return getConstantExprFNeg(C);
218 assert(C->getType()->isIntOrIntVector() &&
219 "Cannot NEG a nonintegral value!");
220 return getConstantExpr(Instruction::Sub,
221 ConstantFP::getZeroValueForNegation(C->getType()),
225 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
226 assert(C->getType()->isFPOrFPVector() &&
227 "Cannot FNEG a non-floating-point value!");
228 return getConstantExpr(Instruction::FSub,
229 ConstantFP::getZeroValueForNegation(C->getType()),
233 Constant* LLVMContext::getConstantExprNot(Constant* C) {
234 assert(C->getType()->isIntOrIntVector() &&
235 "Cannot NOT a nonintegral value!");
236 return getConstantExpr(Instruction::Xor, C, getAllOnesValue(C->getType()));
239 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
240 return getConstantExpr(Instruction::Add, C1, C2);
243 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
244 return getConstantExpr(Instruction::FAdd, C1, C2);
247 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
248 return getConstantExpr(Instruction::Sub, C1, C2);
251 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
252 return getConstantExpr(Instruction::FSub, C1, C2);
255 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
256 return getConstantExpr(Instruction::Mul, C1, C2);
259 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
260 return getConstantExpr(Instruction::FMul, C1, C2);
263 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
264 return getConstantExpr(Instruction::UDiv, C1, C2);
267 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
268 return getConstantExpr(Instruction::SDiv, C1, C2);
271 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
272 return getConstantExpr(Instruction::FDiv, C1, C2);
275 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
276 return getConstantExpr(Instruction::URem, C1, C2);
279 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
280 return getConstantExpr(Instruction::SRem, C1, C2);
283 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
284 return getConstantExpr(Instruction::FRem, C1, C2);
287 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
288 return getConstantExpr(Instruction::And, C1, C2);
291 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
292 return getConstantExpr(Instruction::Or, C1, C2);
295 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
296 return getConstantExpr(Instruction::Xor, C1, C2);
299 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
301 return ConstantExpr::getICmp(pred, LHS, RHS);
304 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
306 return ConstantExpr::getFCmp(pred, LHS, RHS);
309 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
310 return getConstantExpr(Instruction::Shl, C1, C2);
313 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
314 return getConstantExpr(Instruction::LShr, C1, C2);
317 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
318 return getConstantExpr(Instruction::AShr, C1, C2);
321 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
322 Constant* const* IdxList,
324 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
327 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
328 Value* const* IdxList,
330 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
333 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
335 return ConstantExpr::getExtractElement(Vec, Idx);
338 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
341 return ConstantExpr::getInsertElement(Vec, Elt, Idx);
344 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
346 return ConstantExpr::getShuffleVector(V1, V2, Mask);
349 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
350 const unsigned* IdxList,
352 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
355 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
356 const unsigned* IdxList,
358 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
361 Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
362 // sizeof is implemented as: (i64) gep (Ty*)null, 1
363 // Note that a non-inbounds gep is used, as null isn't within any object.
364 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
365 Constant *GEP = getConstantExprGetElementPtr(
366 getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
367 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
371 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
372 return pImpl->getMDNode(Vals, NumVals);
375 // MDString accessors
376 MDString* LLVMContext::getMDString(const StringRef &Str) {
377 return pImpl->getMDString(Str.data(), Str.size());
380 // FunctionType accessors
381 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
382 return FunctionType::get(Result, isVarArg);
385 FunctionType* LLVMContext::getFunctionType(const Type* Result,
386 const std::vector<const Type*>& Params,
388 return FunctionType::get(Result, Params, isVarArg);
391 // IntegerType accessors
392 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
393 return IntegerType::get(NumBits);
396 // OpaqueType accessors
397 OpaqueType* LLVMContext::getOpaqueType() {
398 return OpaqueType::get();
401 // StructType accessors
402 StructType* LLVMContext::getStructType(bool isPacked) {
403 return StructType::get(isPacked);
406 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
408 return StructType::get(Params, isPacked);
411 StructType *LLVMContext::getStructType(const Type *type, ...) {
413 std::vector<const llvm::Type*> StructFields;
416 StructFields.push_back(type);
417 type = va_arg(ap, llvm::Type*);
419 return StructType::get(StructFields);
422 // ArrayType accessors
423 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
424 uint64_t NumElements) {
425 return ArrayType::get(ElementType, NumElements);
428 // PointerType accessors
429 PointerType* LLVMContext::getPointerType(const Type* ElementType,
430 unsigned AddressSpace) {
431 return PointerType::get(ElementType, AddressSpace);
434 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
435 return PointerType::getUnqual(ElementType);
438 // VectorType accessors
439 VectorType* LLVMContext::getVectorType(const Type* ElementType,
440 unsigned NumElements) {
441 return VectorType::get(ElementType, NumElements);
444 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
445 return VectorType::getInteger(VTy);
448 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
449 return VectorType::getExtendedElementVectorType(VTy);
452 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
453 return VectorType::getTruncatedElementVectorType(VTy);
456 const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
457 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
458 return getVectorType(Type::Int1Ty, vt->getNumElements());
463 void LLVMContext::erase(MDString *M) {
467 void LLVMContext::erase(MDNode *M) {
471 void LLVMContext::erase(ConstantAggregateZero *Z) {