//===----------------------------------------------------------------------===//
//
// This file implements LLVMContext, as a wrapper around the opaque
-// class LLVMContextImpl.
+// class LLVMContextImpl.
//
//===----------------------------------------------------------------------===//
#include "llvm/LLVMContext.h"
+#include "llvm/Metadata.h"
#include "llvm/Constants.h"
-#include "llvm/DerivedTypes.h"
#include "llvm/Instruction.h"
-#include "llvm/MDNode.h"
#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/SourceMgr.h"
#include "LLVMContextImpl.h"
-#include <cstdarg>
-
+#include <cctype>
using namespace llvm;
static ManagedStatic<LLVMContext> GlobalContext;
return *GlobalContext;
}
-LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { }
-LLVMContext::~LLVMContext() { delete pImpl; }
-
-// Constant accessors
-
-// Constructor to create a '0' constant of arbitrary type...
-static const uint64_t zero[2] = {0, 0};
-Constant* LLVMContext::getNullValue(const Type* Ty) {
- switch (Ty->getTypeID()) {
- case Type::IntegerTyID:
- return ConstantInt::get(Ty, 0);
- case Type::FloatTyID:
- return ConstantFP::get(Ty->getContext(), APFloat(APInt(32, 0)));
- case Type::DoubleTyID:
- return ConstantFP::get(Ty->getContext(), APFloat(APInt(64, 0)));
- case Type::X86_FP80TyID:
- return ConstantFP::get(Ty->getContext(), APFloat(APInt(80, 2, zero)));
- case Type::FP128TyID:
- return ConstantFP::get(Ty->getContext(),
- APFloat(APInt(128, 2, zero), true));
- case Type::PPC_FP128TyID:
- return ConstantFP::get(Ty->getContext(), APFloat(APInt(128, 2, zero)));
- case Type::PointerTyID:
- return getConstantPointerNull(cast<PointerType>(Ty));
- case Type::StructTyID:
- case Type::ArrayTyID:
- case Type::VectorTyID:
- return getConstantAggregateZero(Ty);
- default:
- // Function, Label, or Opaque type?
- assert(!"Cannot create a null constant of that type!");
- return 0;
- }
-}
-
-Constant* LLVMContext::getAllOnesValue(const Type* Ty) {
- if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
- return ConstantInt::get(*this, APInt::getAllOnesValue(ITy->getBitWidth()));
-
- std::vector<Constant*> Elts;
- const VectorType* VTy = cast<VectorType>(Ty);
- Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
- assert(Elts[0] && "Not a vector integer type!");
- return cast<ConstantVector>(getConstantVector(Elts));
-}
-
-// UndefValue accessors.
-UndefValue* LLVMContext::getUndef(const Type* Ty) {
- return UndefValue::get(Ty);
-}
-
-// ConstantInt accessors.
-ConstantInt* LLVMContext::getTrue() {
- assert(this && "Context not initialized!");
- assert(pImpl && "Context not initialized!");
- return pImpl->getTrue();
-}
-
-ConstantInt* LLVMContext::getFalse() {
- assert(this && "Context not initialized!");
- assert(pImpl && "Context not initialized!");
- return pImpl->getFalse();
-}
-
-// ConstantPointerNull accessors.
-ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
- return ConstantPointerNull::get(T);
-}
-
-// ConstantAggregateZero accessors.
-ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
- return pImpl->getConstantAggregateZero(Ty);
-}
-
-// ConstantExpr accessors.
-Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
- Constant* C2) {
- return ConstantExpr::get(Opcode, C1, C2);
-}
-
-Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
- return ConstantExpr::getTrunc(C, Ty);
-}
-
-Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
- return ConstantExpr::getSExt(C, Ty);
-}
-
-Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
- return ConstantExpr::getZExt(C, Ty);
-}
-
-Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
- return ConstantExpr::getFPTrunc(C, Ty);
-}
-
-Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
- return ConstantExpr::getFPExtend(C, Ty);
-}
-
-Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
- return ConstantExpr::getUIToFP(C, Ty);
-}
-
-Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
- return ConstantExpr::getSIToFP(C, Ty);
-}
-
-Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
- return ConstantExpr::getFPToUI(C, Ty);
-}
-
-Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
- return ConstantExpr::getFPToSI(C, Ty);
-}
-
-Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
- return ConstantExpr::getPtrToInt(C, Ty);
-}
-
-Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
- return ConstantExpr::getIntToPtr(C, Ty);
-}
-
-Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
- return ConstantExpr::getBitCast(C, Ty);
-}
-
-Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
- const Type* Ty) {
- return ConstantExpr::getCast(ops, C, Ty);
-}
-
-Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
- const Type* Ty) {
- return ConstantExpr::getZExtOrBitCast(C, Ty);
-}
-
-Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
- const Type* Ty) {
- return ConstantExpr::getSExtOrBitCast(C, Ty);
-}
-
-Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
- const Type* Ty) {
- return ConstantExpr::getTruncOrBitCast(C, Ty);
-}
-
-Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
- return ConstantExpr::getPointerCast(C, Ty);
-}
-
-Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
- bool isSigned) {
- return ConstantExpr::getIntegerCast(C, Ty, isSigned);
-}
-
-Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
- return ConstantExpr::getFPCast(C, Ty);
-}
-
-Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
- Constant* V2) {
- return ConstantExpr::getSelect(C, V1, V2);
-}
-
-Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
- // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
- const Type *AligningTy = getStructType(Type::Int8Ty, Ty, NULL);
- Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
- Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
- Constant *One = ConstantInt::get(Type::Int32Ty, 1);
- Constant *Indices[2] = { Zero, One };
- Constant *GEP = getConstantExprGetElementPtr(NullPtr, Indices, 2);
- return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
-}
-
-Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
- Constant* C1, Constant* C2) {
- return ConstantExpr::getCompare(pred, C1, C2);
-}
+LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
+ // Create the fixed metadata kinds. This is done in the same order as the
+ // MD_* enum values so that they correspond.
-Constant* LLVMContext::getConstantExprNeg(Constant* C) {
- // API compatibility: Adjust integer opcodes to floating-point opcodes.
- if (C->getType()->isFPOrFPVector())
- return getConstantExprFNeg(C);
- assert(C->getType()->isIntOrIntVector() &&
- "Cannot NEG a nonintegral value!");
- return getConstantExpr(Instruction::Sub,
- ConstantFP::getZeroValueForNegation(C->getType()),
- C);
-}
+ // Create the 'dbg' metadata kind.
+ unsigned DbgID = getMDKindID("dbg");
+ assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
-Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
- assert(C->getType()->isFPOrFPVector() &&
- "Cannot FNEG a non-floating-point value!");
- return getConstantExpr(Instruction::FSub,
- ConstantFP::getZeroValueForNegation(C->getType()),
- C);
-}
-
-Constant* LLVMContext::getConstantExprNot(Constant* C) {
- assert(C->getType()->isIntOrIntVector() &&
- "Cannot NOT a nonintegral value!");
- return getConstantExpr(Instruction::Xor, C, getAllOnesValue(C->getType()));
-}
-
-Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
- return getConstantExpr(Instruction::Add, C1, C2);
-}
-
-Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
- return getConstantExpr(Instruction::FAdd, C1, C2);
-}
+ // Create the 'tbaa' metadata kind.
+ unsigned TBAAID = getMDKindID("tbaa");
+ assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
-Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
- return getConstantExpr(Instruction::Sub, C1, C2);
-}
-
-Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
- return getConstantExpr(Instruction::FSub, C1, C2);
-}
-
-Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
- return getConstantExpr(Instruction::Mul, C1, C2);
-}
-
-Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
- return getConstantExpr(Instruction::FMul, C1, C2);
-}
-
-Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
- return getConstantExpr(Instruction::UDiv, C1, C2);
-}
-
-Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
- return getConstantExpr(Instruction::SDiv, C1, C2);
-}
-
-Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
- return getConstantExpr(Instruction::FDiv, C1, C2);
-}
-
-Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
- return getConstantExpr(Instruction::URem, C1, C2);
-}
-
-Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
- return getConstantExpr(Instruction::SRem, C1, C2);
-}
-
-Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
- return getConstantExpr(Instruction::FRem, C1, C2);
-}
-
-Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
- return getConstantExpr(Instruction::And, C1, C2);
-}
-
-Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
- return getConstantExpr(Instruction::Or, C1, C2);
-}
-
-Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
- return getConstantExpr(Instruction::Xor, C1, C2);
-}
-
-Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
- Constant* RHS) {
- return ConstantExpr::getICmp(pred, LHS, RHS);
-}
-
-Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
- Constant* RHS) {
- return ConstantExpr::getFCmp(pred, LHS, RHS);
-}
-
-Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
- return getConstantExpr(Instruction::Shl, C1, C2);
-}
-
-Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
- return getConstantExpr(Instruction::LShr, C1, C2);
-}
-
-Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
- return getConstantExpr(Instruction::AShr, C1, C2);
-}
-
-Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
- Constant* const* IdxList,
- unsigned NumIdx) {
- return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
-}
-
-Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
- Value* const* IdxList,
- unsigned NumIdx) {
- return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
-}
-
-Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
- Constant* Idx) {
- return ConstantExpr::getExtractElement(Vec, Idx);
-}
-
-Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
- Constant* Elt,
- Constant* Idx) {
- return ConstantExpr::getInsertElement(Vec, Elt, Idx);
-}
-
-Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
- Constant* Mask) {
- return ConstantExpr::getShuffleVector(V1, V2, Mask);
-}
-
-Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
- const unsigned* IdxList,
- unsigned NumIdx) {
- return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
-}
-
-Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
- const unsigned* IdxList,
- unsigned NumIdx) {
- return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
-}
-
-Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
- // sizeof is implemented as: (i64) gep (Ty*)null, 1
- // Note that a non-inbounds gep is used, as null isn't within any object.
- Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
- Constant *GEP = getConstantExprGetElementPtr(
- getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
- return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
-}
-
-// ConstantVector accessors.
-Constant* LLVMContext::getConstantVector(const VectorType* T,
- const std::vector<Constant*>& V) {
- return pImpl->getConstantVector(T, V);
+ // Create the 'prof' metadata kind.
+ unsigned ProfID = getMDKindID("prof");
+ assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
}
+LLVMContext::~LLVMContext() { delete pImpl; }
-Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
- assert(!V.empty() && "Cannot infer type if V is empty");
- return getConstantVector(getVectorType(V.front()->getType(),V.size()), V);
+void LLVMContext::addModule(Module *M) {
+ pImpl->OwnedModules.insert(M);
}
-Constant* LLVMContext::getConstantVector(Constant* const* Vals,
- unsigned NumVals) {
- // FIXME: make this the primary ctor method.
- return getConstantVector(std::vector<Constant*>(Vals, Vals+NumVals));
+void LLVMContext::removeModule(Module *M) {
+ pImpl->OwnedModules.erase(M);
}
-// MDNode accessors
-MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
- return pImpl->getMDNode(Vals, NumVals);
-}
+//===----------------------------------------------------------------------===//
+// Recoverable Backend Errors
+//===----------------------------------------------------------------------===//
-// MDString accessors
-MDString* LLVMContext::getMDString(const StringRef &Str) {
- return pImpl->getMDString(Str.data(), Str.size());
+void LLVMContext::
+setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
+ void *DiagContext) {
+ pImpl->InlineAsmDiagHandler = DiagHandler;
+ pImpl->InlineAsmDiagContext = DiagContext;
}
-// FunctionType accessors
-FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
- return FunctionType::get(Result, isVarArg);
+/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
+/// setInlineAsmDiagnosticHandler.
+LLVMContext::InlineAsmDiagHandlerTy
+LLVMContext::getInlineAsmDiagnosticHandler() const {
+ return pImpl->InlineAsmDiagHandler;
}
-FunctionType* LLVMContext::getFunctionType(const Type* Result,
- const std::vector<const Type*>& Params,
- bool isVarArg) {
- return FunctionType::get(Result, Params, isVarArg);
-}
-
-// IntegerType accessors
-const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
- return IntegerType::get(NumBits);
-}
-
-// OpaqueType accessors
-OpaqueType* LLVMContext::getOpaqueType() {
- return OpaqueType::get();
+/// getInlineAsmDiagnosticContext - Return the diagnostic context set by
+/// setInlineAsmDiagnosticHandler.
+void *LLVMContext::getInlineAsmDiagnosticContext() const {
+ return pImpl->InlineAsmDiagContext;
}
-// StructType accessors
-StructType* LLVMContext::getStructType(bool isPacked) {
- return StructType::get(isPacked);
+void LLVMContext::emitError(StringRef ErrorStr) {
+ emitError(0U, ErrorStr);
}
-StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
- bool isPacked) {
- return StructType::get(Params, isPacked);
+void LLVMContext::emitError(const Instruction *I, StringRef ErrorStr) {
+ unsigned LocCookie = 0;
+ if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
+ if (SrcLoc->getNumOperands() != 0)
+ if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
+ LocCookie = CI->getZExtValue();
+ }
+ return emitError(LocCookie, ErrorStr);
}
-StructType *LLVMContext::getStructType(const Type *type, ...) {
- va_list ap;
- std::vector<const llvm::Type*> StructFields;
- va_start(ap, type);
- while (type) {
- StructFields.push_back(type);
- type = va_arg(ap, llvm::Type*);
+void LLVMContext::emitError(unsigned LocCookie, StringRef ErrorStr) {
+ // If there is no error handler installed, just print the error and exit.
+ if (pImpl->InlineAsmDiagHandler == 0) {
+ errs() << "error: " << ErrorStr << "\n";
+ exit(1);
}
- return StructType::get(StructFields);
-}
-// ArrayType accessors
-ArrayType* LLVMContext::getArrayType(const Type* ElementType,
- uint64_t NumElements) {
- return ArrayType::get(ElementType, NumElements);
-}
-
-// PointerType accessors
-PointerType* LLVMContext::getPointerType(const Type* ElementType,
- unsigned AddressSpace) {
- return PointerType::get(ElementType, AddressSpace);
-}
+ // If we do have an error handler, we can report the error and keep going.
+ SMDiagnostic Diag("", "error: " + ErrorStr.str());
-PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
- return PointerType::getUnqual(ElementType);
-}
-
-// VectorType accessors
-VectorType* LLVMContext::getVectorType(const Type* ElementType,
- unsigned NumElements) {
- return VectorType::get(ElementType, NumElements);
+ pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie);
}
-VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
- return VectorType::getInteger(VTy);
-}
+//===----------------------------------------------------------------------===//
+// Metadata Kind Uniquing
+//===----------------------------------------------------------------------===//
-VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
- return VectorType::getExtendedElementVectorType(VTy);
-}
+#ifndef NDEBUG
+/// isValidName - Return true if Name is a valid custom metadata handler name.
+static bool isValidName(StringRef MDName) {
+ if (MDName.empty())
+ return false;
-VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
- return VectorType::getTruncatedElementVectorType(VTy);
-}
+ if (!std::isalpha(MDName[0]))
+ return false;
-const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
- if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
- return getVectorType(Type::Int1Ty, vt->getNumElements());
+ for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
+ ++I) {
+ if (!std::isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
+ return false;
}
- return Type::Int1Ty;
-}
-
-void LLVMContext::erase(MDString *M) {
- pImpl->erase(M);
+ return true;
}
+#endif
-void LLVMContext::erase(MDNode *M) {
- pImpl->erase(M);
-}
+/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
+unsigned LLVMContext::getMDKindID(StringRef Name) const {
+ assert(isValidName(Name) && "Invalid MDNode name");
-void LLVMContext::erase(ConstantAggregateZero *Z) {
- pImpl->erase(Z);
+ // If this is new, assign it its ID.
+ return
+ pImpl->CustomMDKindNames.GetOrCreateValue(
+ Name, pImpl->CustomMDKindNames.size()).second;
}
-void LLVMContext::erase(ConstantVector *V) {
- pImpl->erase(V);
+/// getHandlerNames - Populate client supplied smallvector using custome
+/// metadata name and ID.
+void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
+ Names.resize(pImpl->CustomMDKindNames.size());
+ for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
+ E = pImpl->CustomMDKindNames.end(); I != E; ++I)
+ Names[I->second] = I->first();
}