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);
107 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
108 return pImpl->getMDNode(Vals, NumVals);
111 // MDString accessors
112 MDString* LLVMContext::getMDString(const StringRef &Str) {
113 return pImpl->getMDString(Str.data(), Str.size());
116 void LLVMContext::erase(MDString *M) {
120 void LLVMContext::erase(MDNode *M) {
124 void LLVMContext::erase(ConstantAggregateZero *Z) {