2009-06-30 00:48:55 +00:00
|
|
|
//===-- LLVMContext.cpp - Implement LLVMContext -----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-06-30 17:06:46 +00:00
|
|
|
//
|
|
|
|
// This file implements LLVMContext, as a wrapper around the opaque
|
|
|
|
// class LLVMContextImpl.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-06-30 00:48:55 +00:00
|
|
|
|
|
|
|
#include "llvm/LLVMContext.h"
|
|
|
|
#include "llvm/Constants.h"
|
2009-06-30 17:50:28 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2009-07-13 04:09:18 +00:00
|
|
|
#include "llvm/Instruction.h"
|
2009-07-02 17:12:48 +00:00
|
|
|
#include "llvm/MDNode.h"
|
2009-06-30 23:39:59 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2009-06-30 00:48:55 +00:00
|
|
|
#include "LLVMContextImpl.h"
|
2009-07-13 05:49:04 +00:00
|
|
|
#include <cstdarg>
|
2009-06-30 00:48:55 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2009-06-30 23:39:59 +00:00
|
|
|
static ManagedStatic<LLVMContext> GlobalContext;
|
|
|
|
|
2009-07-01 23:13:44 +00:00
|
|
|
LLVMContext& llvm::getGlobalContext() {
|
2009-07-01 21:22:36 +00:00
|
|
|
return *GlobalContext;
|
2009-06-30 23:39:59 +00:00
|
|
|
}
|
|
|
|
|
2009-06-30 00:48:55 +00:00
|
|
|
LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl()) { }
|
|
|
|
LLVMContext::~LLVMContext() { delete pImpl; }
|
|
|
|
|
2009-07-01 23:56:45 +00:00
|
|
|
// Constant accessors
|
2009-07-13 04:09:18 +00:00
|
|
|
|
|
|
|
// Constructor to create a '0' constant of arbitrary type...
|
|
|
|
static const uint64_t zero[2] = {0, 0};
|
2009-07-01 23:56:45 +00:00
|
|
|
Constant* LLVMContext::getNullValue(const Type* Ty) {
|
2009-07-13 04:09:18 +00:00
|
|
|
switch (Ty->getTypeID()) {
|
|
|
|
case Type::IntegerTyID:
|
|
|
|
return getConstantInt(Ty, 0);
|
|
|
|
case Type::FloatTyID:
|
|
|
|
return getConstantFP(APFloat(APInt(32, 0)));
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
return getConstantFP(APFloat(APInt(64, 0)));
|
|
|
|
case Type::X86_FP80TyID:
|
|
|
|
return getConstantFP(APFloat(APInt(80, 2, zero)));
|
|
|
|
case Type::FP128TyID:
|
|
|
|
return getConstantFP(APFloat(APInt(128, 2, zero), true));
|
|
|
|
case Type::PPC_FP128TyID:
|
|
|
|
return getConstantFP(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;
|
|
|
|
}
|
2009-07-01 23:56:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getAllOnesValue(const Type* Ty) {
|
2009-07-13 20:58:05 +00:00
|
|
|
if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
|
|
|
|
return getConstantInt(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));
|
2009-07-01 23:56:45 +00:00
|
|
|
}
|
|
|
|
|
2009-07-02 16:51:51 +00:00
|
|
|
// UndefValue accessors.
|
|
|
|
UndefValue* LLVMContext::getUndef(const Type* Ty) {
|
|
|
|
return UndefValue::get(Ty);
|
|
|
|
}
|
|
|
|
|
2009-06-30 00:48:55 +00:00
|
|
|
// ConstantInt accessors.
|
2009-07-01 22:33:26 +00:00
|
|
|
ConstantInt* LLVMContext::getConstantIntTrue() {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantInt::getTrue();
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
ConstantInt* LLVMContext::getConstantIntFalse() {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantInt::getFalse();
|
|
|
|
}
|
|
|
|
|
2009-07-02 23:58:19 +00:00
|
|
|
Constant* LLVMContext::getConstantInt(const Type* Ty, uint64_t V,
|
|
|
|
bool isSigned) {
|
2009-07-14 23:09:55 +00:00
|
|
|
Constant *C = getConstantInt(cast<IntegerType>(Ty->getScalarType()),
|
|
|
|
V, isSigned);
|
|
|
|
|
|
|
|
// For vectors, broadcast the value.
|
|
|
|
if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
|
|
|
|
return
|
|
|
|
getConstantVector(std::vector<Constant *>(VTy->getNumElements(), C));
|
|
|
|
|
|
|
|
return C;
|
2009-07-02 23:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-30 00:48:55 +00:00
|
|
|
ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
|
2009-07-01 22:33:26 +00:00
|
|
|
bool isSigned) {
|
2009-07-14 23:09:55 +00:00
|
|
|
return getConstantInt(APInt(Ty->getBitWidth(), V, isSigned));
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstantInt* LLVMContext::getConstantIntSigned(const IntegerType* Ty,
|
2009-07-01 22:33:26 +00:00
|
|
|
int64_t V) {
|
2009-07-14 23:09:55 +00:00
|
|
|
return getConstantInt(Ty, V, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant *LLVMContext::getConstantIntSigned(const Type *Ty, int64_t V) {
|
|
|
|
return getConstantInt(Ty, V, true);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
ConstantInt* LLVMContext::getConstantInt(const APInt& V) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantInt::get(V);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
|
2009-07-14 23:09:55 +00:00
|
|
|
ConstantInt *C = getConstantInt(V);
|
|
|
|
assert(C->getType() == Ty->getScalarType() &&
|
|
|
|
"ConstantInt type doesn't match the type implied by its value!");
|
|
|
|
|
|
|
|
// For vectors, broadcast the value.
|
|
|
|
if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
|
|
|
|
return
|
|
|
|
ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
|
|
|
|
|
|
|
|
return C;
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConstantPointerNull accessors.
|
2009-07-01 22:33:26 +00:00
|
|
|
ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantPointerNull::get(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ConstantStruct accessors.
|
|
|
|
Constant* LLVMContext::getConstantStruct(const StructType* T,
|
2009-07-01 22:33:26 +00:00
|
|
|
const std::vector<Constant*>& V) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantStruct::get(T, V);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
|
2009-07-01 22:33:26 +00:00
|
|
|
bool Packed) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantStruct::get(V, Packed);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
|
2009-07-01 22:33:26 +00:00
|
|
|
unsigned NumVals, bool Packed) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantStruct::get(Vals, NumVals, Packed);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ConstantAggregateZero accessors.
|
2009-07-01 22:33:26 +00:00
|
|
|
ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantAggregateZero::get(Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ConstantArray accessors.
|
|
|
|
Constant* LLVMContext::getConstantArray(const ArrayType* T,
|
2009-07-01 22:33:26 +00:00
|
|
|
const std::vector<Constant*>& V) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantArray::get(T, V);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantArray(const ArrayType* T,
|
|
|
|
Constant* const* Vals,
|
2009-07-01 22:33:26 +00:00
|
|
|
unsigned NumVals) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantArray::get(T, Vals, NumVals);
|
|
|
|
}
|
|
|
|
|
2009-07-14 23:09:55 +00:00
|
|
|
/// 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 std::string& Str,
|
2009-07-01 22:33:26 +00:00
|
|
|
bool AddNull) {
|
2009-07-14 23:09:55 +00:00
|
|
|
std::vector<Constant*> ElementVals;
|
|
|
|
for (unsigned i = 0; i < Str.length(); ++i)
|
|
|
|
ElementVals.push_back(getConstantInt(Type::Int8Ty, Str[i]));
|
|
|
|
|
|
|
|
// Add a null terminator to the string...
|
|
|
|
if (AddNull) {
|
|
|
|
ElementVals.push_back(getConstantInt(Type::Int8Ty, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayType *ATy = getArrayType(Type::Int8Ty, ElementVals.size());
|
|
|
|
return getConstantArray(ATy, ElementVals);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ConstantExpr accessors.
|
|
|
|
Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* C2) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::get(Opcode, C1, C2);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getTrunc(C, Ty);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getSExt(C, Ty);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getZExt(C, Ty);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getFPTrunc(C, Ty);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getFPExtend(C, Ty);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getUIToFP(C, Ty);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getSIToFP(C, Ty);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getFPToUI(C, Ty);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getFPToSI(C, Ty);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getPtrToInt(C, Ty);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getIntToPtr(C, Ty);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getBitCast(C, Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
|
2009-07-01 22:33:26 +00:00
|
|
|
const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getCast(ops, C, Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
|
2009-07-01 22:33:26 +00:00
|
|
|
const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getZExtOrBitCast(C, Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
|
2009-07-01 22:33:26 +00:00
|
|
|
const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getSExtOrBitCast(C, Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
|
2009-07-01 22:33:26 +00:00
|
|
|
const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getTruncOrBitCast(C, Ty);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getPointerCast(C, Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
|
2009-07-01 22:33:26 +00:00
|
|
|
bool isSigned) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getIntegerCast(C, Ty, isSigned);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getFPCast(C, Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* V2) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getSelect(C, V1, V2);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
|
2009-07-13 04:09:18 +00:00
|
|
|
// 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 = getConstantInt(Type::Int32Ty, 0);
|
|
|
|
Constant *One = getConstantInt(Type::Int32Ty, 1);
|
|
|
|
Constant *Indices[2] = { Zero, One };
|
|
|
|
Constant *GEP = getConstantExprGetElementPtr(NullPtr, Indices, 2);
|
|
|
|
return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* C1, Constant* C2) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getCompare(pred, C1, C2);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprNeg(Constant* C) {
|
2009-07-13 04:09:18 +00:00
|
|
|
// 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,
|
|
|
|
getZeroValueForNegation(C->getType()),
|
|
|
|
C);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
|
2009-07-13 04:09:18 +00:00
|
|
|
assert(C->getType()->isFPOrFPVector() &&
|
|
|
|
"Cannot FNEG a non-floating-point value!");
|
|
|
|
return getConstantExpr(Instruction::FSub,
|
|
|
|
getZeroValueForNegation(C->getType()),
|
|
|
|
C);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprNot(Constant* C) {
|
2009-07-13 20:58:05 +00:00
|
|
|
assert(C->getType()->isIntOrIntVector() &&
|
|
|
|
"Cannot NOT a nonintegral value!");
|
|
|
|
return getConstantExpr(Instruction::Xor, C, getAllOnesValue(C->getType()));
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
|
2009-07-13 23:50:59 +00:00
|
|
|
return getConstantExpr(Instruction::Add, C1, C2);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
|
2009-07-13 23:50:59 +00:00
|
|
|
return getConstantExpr(Instruction::FAdd, C1, C2);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
|
2009-07-13 23:50:59 +00:00
|
|
|
return getConstantExpr(Instruction::Sub, C1, C2);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
|
2009-07-13 23:50:59 +00:00
|
|
|
return getConstantExpr(Instruction::FSub, C1, C2);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
|
2009-07-13 23:50:59 +00:00
|
|
|
return getConstantExpr(Instruction::Mul, C1, C2);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
|
2009-07-13 23:50:59 +00:00
|
|
|
return getConstantExpr(Instruction::FMul, C1, C2);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
|
2009-07-13 23:50:59 +00:00
|
|
|
return getConstantExpr(Instruction::UDiv, C1, C2);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
|
2009-07-13 23:50:59 +00:00
|
|
|
return getConstantExpr(Instruction::SDiv, C1, C2);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
|
2009-07-13 23:50:59 +00:00
|
|
|
return getConstantExpr(Instruction::FDiv, C1, C2);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
|
2009-07-13 23:50:59 +00:00
|
|
|
return getConstantExpr(Instruction::URem, C1, C2);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
|
2009-07-13 23:50:59 +00:00
|
|
|
return getConstantExpr(Instruction::SRem, C1, C2);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
|
2009-07-13 23:50:59 +00:00
|
|
|
return getConstantExpr(Instruction::FRem, C1, C2);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
|
2009-07-13 23:50:59 +00:00
|
|
|
return getConstantExpr(Instruction::And, C1, C2);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
|
2009-07-13 23:50:59 +00:00
|
|
|
return getConstantExpr(Instruction::Or, C1, C2);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
|
2009-07-13 23:50:59 +00:00
|
|
|
return getConstantExpr(Instruction::Xor, C1, C2);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* RHS) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getICmp(pred, LHS, RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* RHS) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getFCmp(pred, LHS, RHS);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
|
2009-07-13 23:50:59 +00:00
|
|
|
return getConstantExpr(Instruction::Shl, C1, C2);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
|
2009-07-13 23:50:59 +00:00
|
|
|
return getConstantExpr(Instruction::LShr, C1, C2);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
|
2009-07-13 23:50:59 +00:00
|
|
|
return getConstantExpr(Instruction::AShr, C1, C2);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
|
|
|
|
Constant* const* IdxList,
|
2009-07-01 22:33:26 +00:00
|
|
|
unsigned NumIdx) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
|
|
|
|
Value* const* IdxList,
|
2009-07-01 22:33:26 +00:00
|
|
|
unsigned NumIdx) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* Idx) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getExtractElement(Vec, Idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
|
|
|
|
Constant* Elt,
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* Idx) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getInsertElement(Vec, Elt, Idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* Mask) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getShuffleVector(V1, V2, Mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
|
|
|
|
const unsigned* IdxList,
|
2009-07-01 22:33:26 +00:00
|
|
|
unsigned NumIdx) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
|
|
|
|
const unsigned* IdxList,
|
2009-07-01 22:33:26 +00:00
|
|
|
unsigned NumIdx) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
|
|
|
|
}
|
|
|
|
|
2009-07-05 22:41:43 +00:00
|
|
|
Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
|
2009-07-13 04:09:18 +00:00
|
|
|
// sizeof is implemented as: (i64) gep (Ty*)null, 1
|
|
|
|
Constant *GEPIdx = getConstantInt(Type::Int32Ty, 1);
|
|
|
|
Constant *GEP = getConstantExprGetElementPtr(
|
|
|
|
getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
|
|
|
|
return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
|
2009-07-05 22:41:43 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
|
2009-07-13 04:09:18 +00:00
|
|
|
if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
|
|
|
|
if (PTy->getElementType()->isFloatingPoint()) {
|
|
|
|
std::vector<Constant*> zeros(PTy->getNumElements(),
|
|
|
|
getConstantFPNegativeZero(PTy->getElementType()));
|
|
|
|
return getConstantVector(PTy, zeros);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Ty->isFloatingPoint())
|
|
|
|
return getConstantFPNegativeZero(Ty);
|
|
|
|
|
|
|
|
return getNullValue(Ty);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ConstantFP accessors.
|
2009-07-01 22:33:26 +00:00
|
|
|
ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantFP::get(V);
|
|
|
|
}
|
|
|
|
|
2009-07-13 23:16:26 +00:00
|
|
|
static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
|
|
|
|
if (Ty == Type::FloatTy)
|
|
|
|
return &APFloat::IEEEsingle;
|
|
|
|
if (Ty == Type::DoubleTy)
|
|
|
|
return &APFloat::IEEEdouble;
|
|
|
|
if (Ty == Type::X86_FP80Ty)
|
|
|
|
return &APFloat::x87DoubleExtended;
|
|
|
|
else if (Ty == Type::FP128Ty)
|
|
|
|
return &APFloat::IEEEquad;
|
|
|
|
|
|
|
|
assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
|
|
|
|
return &APFloat::PPCDoubleDouble;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// get() - This returns a constant fp for the specified value in the
|
|
|
|
/// specified type. This should only be used for simple constant values like
|
|
|
|
/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
|
2009-07-13 23:16:26 +00:00
|
|
|
APFloat FV(V);
|
|
|
|
bool ignored;
|
|
|
|
FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
|
|
|
|
APFloat::rmNearestTiesToEven, &ignored);
|
|
|
|
Constant *C = getConstantFP(FV);
|
|
|
|
|
|
|
|
// For vectors, broadcast the value.
|
|
|
|
if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
|
|
|
|
return
|
|
|
|
getConstantVector(std::vector<Constant *>(VTy->getNumElements(), C));
|
|
|
|
|
|
|
|
return C;
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
|
2009-07-13 04:09:18 +00:00
|
|
|
APFloat apf = cast <ConstantFP>(getNullValue(Ty))->getValueAPF();
|
|
|
|
apf.changeSign();
|
|
|
|
return getConstantFP(apf);
|
2009-06-30 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ConstantVector accessors.
|
|
|
|
Constant* LLVMContext::getConstantVector(const VectorType* T,
|
2009-07-01 22:33:26 +00:00
|
|
|
const std::vector<Constant*>& V) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantVector::get(T, V);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantVector::get(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant* LLVMContext::getConstantVector(Constant* const* Vals,
|
2009-07-01 22:33:26 +00:00
|
|
|
unsigned NumVals) {
|
2009-06-30 00:48:55 +00:00
|
|
|
return ConstantVector::get(Vals, NumVals);
|
|
|
|
}
|
|
|
|
|
2009-07-02 17:12:48 +00:00
|
|
|
// MDNode accessors
|
|
|
|
MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
|
|
|
|
return MDNode::get(Vals, NumVals);
|
|
|
|
}
|
|
|
|
|
2009-07-02 17:19:47 +00:00
|
|
|
// MDString accessors
|
|
|
|
MDString* LLVMContext::getMDString(const char *StrBegin, const char *StrEnd) {
|
|
|
|
return MDString::get(StrBegin, StrEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
MDString* LLVMContext::getMDString(const std::string &Str) {
|
|
|
|
return MDString::get(Str);
|
|
|
|
}
|
|
|
|
|
2009-06-30 17:50:28 +00:00
|
|
|
// FunctionType accessors
|
2009-07-05 22:41:43 +00:00
|
|
|
FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
|
|
|
|
return FunctionType::get(Result, isVarArg);
|
|
|
|
}
|
|
|
|
|
2009-06-30 17:50:28 +00:00
|
|
|
FunctionType* LLVMContext::getFunctionType(const Type* Result,
|
|
|
|
const std::vector<const Type*>& Params,
|
2009-07-01 22:33:26 +00:00
|
|
|
bool isVarArg) {
|
2009-06-30 17:50:28 +00:00
|
|
|
return FunctionType::get(Result, Params, isVarArg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntegerType accessors
|
2009-07-01 22:33:26 +00:00
|
|
|
const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
|
2009-06-30 17:50:28 +00:00
|
|
|
return IntegerType::get(NumBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpaqueType accessors
|
2009-07-01 22:33:26 +00:00
|
|
|
OpaqueType* LLVMContext::getOpaqueType() {
|
2009-06-30 17:50:28 +00:00
|
|
|
return OpaqueType::get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// StructType accessors
|
2009-07-01 23:56:45 +00:00
|
|
|
StructType* LLVMContext::getStructType(bool isPacked) {
|
|
|
|
return StructType::get(isPacked);
|
|
|
|
}
|
|
|
|
|
2009-06-30 17:50:28 +00:00
|
|
|
StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
|
2009-07-01 22:33:26 +00:00
|
|
|
bool isPacked) {
|
2009-06-30 17:50:28 +00:00
|
|
|
return StructType::get(Params, isPacked);
|
|
|
|
}
|
|
|
|
|
2009-07-13 04:09:18 +00:00
|
|
|
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*);
|
|
|
|
}
|
|
|
|
return StructType::get(StructFields);
|
|
|
|
}
|
|
|
|
|
2009-06-30 17:50:28 +00:00
|
|
|
// ArrayType accessors
|
|
|
|
ArrayType* LLVMContext::getArrayType(const Type* ElementType,
|
2009-07-01 22:33:26 +00:00
|
|
|
uint64_t NumElements) {
|
2009-06-30 17:50:28 +00:00
|
|
|
return ArrayType::get(ElementType, NumElements);
|
|
|
|
}
|
|
|
|
|
|
|
|
// PointerType accessors
|
|
|
|
PointerType* LLVMContext::getPointerType(const Type* ElementType,
|
2009-07-01 22:33:26 +00:00
|
|
|
unsigned AddressSpace) {
|
2009-06-30 17:50:28 +00:00
|
|
|
return PointerType::get(ElementType, AddressSpace);
|
|
|
|
}
|
|
|
|
|
2009-07-01 23:56:45 +00:00
|
|
|
PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
|
2009-06-30 17:50:28 +00:00
|
|
|
return PointerType::getUnqual(ElementType);
|
|
|
|
}
|
|
|
|
|
|
|
|
// VectorType accessors
|
|
|
|
VectorType* LLVMContext::getVectorType(const Type* ElementType,
|
2009-07-01 22:33:26 +00:00
|
|
|
unsigned NumElements) {
|
2009-06-30 17:50:28 +00:00
|
|
|
return VectorType::get(ElementType, NumElements);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
|
2009-06-30 17:50:28 +00:00
|
|
|
return VectorType::getInteger(VTy);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
|
2009-06-30 17:50:28 +00:00
|
|
|
return VectorType::getExtendedElementVectorType(VTy);
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:33:26 +00:00
|
|
|
VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
|
2009-06-30 17:50:28 +00:00
|
|
|
return VectorType::getTruncatedElementVectorType(VTy);
|
|
|
|
}
|
2009-07-09 23:48:35 +00:00
|
|
|
|
|
|
|
const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
|
|
|
|
if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
|
|
|
|
return getVectorType(Type::Int1Ty, vt->getNumElements());
|
|
|
|
}
|
|
|
|
return Type::Int1Ty;
|
|
|
|
}
|