-// 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);
-}
-
-
-// ConstantArray accessors.
-Constant* LLVMContext::getConstantArray(const ArrayType* T,
- const std::vector<Constant*>& V) {
- return pImpl->getConstantArray(T, V);
-}
-
-Constant* LLVMContext::getConstantArray(const ArrayType* T,
- Constant* const* Vals,
- unsigned NumVals) {
- // FIXME: make this the primary ctor method.
- return getConstantArray(T, std::vector<Constant*>(Vals, Vals+NumVals));
-}
-
-/// ConstantArray::get(const string&) - Return an array that is initialized to
-/// contain the specified string. If length is zero then a null terminator is
-/// added to the specified string so that it may be used in a natural way.
-/// Otherwise, the length parameter specifies how much of the string to use
-/// and it won't be null terminated.
-///
-Constant* LLVMContext::getConstantArray(const StringRef &Str,
- bool AddNull) {
- std::vector<Constant*> ElementVals;
- for (unsigned i = 0; i < Str.size(); ++i)
- ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
-
- // Add a null terminator to the string...
- if (AddNull) {
- ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
- }
-
- ArrayType *ATy = getArrayType(Type::Int8Ty, ElementVals.size());
- return getConstantArray(ATy, ElementVals);
-}
-
-
-// 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);
-}
-
-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);
-}
-
-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);
-}
-
-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);