1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-24 11:42:57 +01:00

Initial infrastructure for arbitrary precision integer

codegen support.  This should have no effect on codegen
for other types.  Debatable bits: (1) the use (abuse?)
of a set in SDNode::getValueTypeList; (2) the length of
getTypeToTransformTo, which maybe should be refactored
with a non-inline part for extended value types.

llvm-svn: 43030
This commit is contained in:
Duncan Sands 2007-10-16 09:56:48 +00:00
parent 12d0747c70
commit 9d622a6de1
4 changed files with 185 additions and 96 deletions

View File

@ -19,6 +19,7 @@
#include <cassert>
#include <string>
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/MathExtras.h"
namespace llvm {
class Type;
@ -38,6 +39,9 @@ namespace MVT { // MVT = Machine Value Types
i64 = 5, // This is a 64 bit integer value
i128 = 6, // This is a 128 bit integer value
FIRST_INTEGER_VALUETYPE = i1,
LAST_INTEGER_VALUETYPE = i128,
f32 = 7, // This is a 32 bit floating point value
f64 = 8, // This is a 64 bit floating point value
f80 = 9, // This is a 80 bit floating point value
@ -53,12 +57,12 @@ namespace MVT { // MVT = Machine Value Types
v1i64 = 17, // 1 x i64
v16i8 = 18, // 16 x i8
v8i16 = 19, // 8 x i16
v3i32 = 20, // 3 x i32
v3i32 = 20, // 3 x i32
v4i32 = 21, // 4 x i32
v2i64 = 22, // 2 x i64
v2f32 = 23, // 2 x f32
v3f32 = 24, // 3 x f32
v3f32 = 24, // 3 x f32
v4f32 = 25, // 4 x f32
v2f64 = 26, // 2 x f64
@ -93,17 +97,36 @@ namespace MVT { // MVT = Machine Value Types
/// value types that are not legal.
///
/// @internal
/// Currently extended types are always vector types. Extended types are
/// encoded by having the first SimpleTypeBits bits encode the vector
/// element type (which must be a scalar type) and the remaining upper
/// bits encode the vector length, offset by one.
/// Extended types are either vector types or arbitrary precision integers.
/// Arbitrary precision integers have iAny in the first SimpleTypeBits bits,
/// and the bit-width in the next PrecisionBits bits, offset by minus one.
/// Vector types are encoded by having the first SimpleTypeBits+PrecisionBits
/// bits encode the vector element type (which must be a scalar type, possibly
/// an arbitrary precision integer) and the remaining VectorBits upper bits
/// encode the vector length, offset by one.
///
/// 31--------------16-----------8-------------0
/// | Vector length | Precision | Simple type |
/// | Vector element |
typedef uint32_t ValueType;
static const int SimpleTypeBits = 8;
static const int PrecisionBits = 8;
static const int VectorBits = 32 - SimpleTypeBits - PrecisionBits;
static const uint32_t SimpleTypeMask =
(~uint32_t(0) << (32 - SimpleTypeBits)) >> (32 - SimpleTypeBits);
static const uint32_t PrecisionMask =
((~uint32_t(0) << VectorBits) >> (32 - PrecisionBits)) << SimpleTypeBits;
static const uint32_t VectorMask =
(~uint32_t(0) >> (32 - VectorBits)) << (32 - VectorBits);
static const uint32_t ElementMask =
(~uint32_t(0) << VectorBits) >> VectorBits;
/// MVT::isExtendedVT - Test if the given ValueType is extended
/// (as opposed to being simple).
static inline bool isExtendedVT(ValueType VT) {
@ -114,7 +137,8 @@ namespace MVT { // MVT = Machine Value Types
/// type.
static inline bool isInteger(ValueType VT) {
ValueType SVT = VT & SimpleTypeMask;
return (SVT >= i1 && SVT <= i128) || (SVT >= v8i8 && SVT <= v2i64);
return (SVT >= FIRST_INTEGER_VALUETYPE && SVT <= LAST_INTEGER_VALUETYPE) ||
(SVT >= v8i8 && SVT <= v2i64) || (SVT == iAny && (VT & PrecisionMask));
}
/// MVT::isFloatingPoint - Return true if this is an FP, or a vector FP type.
@ -126,17 +150,17 @@ namespace MVT { // MVT = Machine Value Types
/// MVT::isVector - Return true if this is a vector value type.
static inline bool isVector(ValueType VT) {
return (VT >= FIRST_VECTOR_VALUETYPE && VT <= LAST_VECTOR_VALUETYPE) ||
isExtendedVT(VT);
(VT & VectorMask);
}
/// MVT::getVectorElementType - Given a vector type, return the type of
/// each element.
static inline ValueType getVectorElementType(ValueType VT) {
assert(isVector(VT) && "Invalid vector type!");
switch (VT) {
default:
if (isExtendedVT(VT))
return VT & SimpleTypeMask;
assert(0 && "Invalid vector type!");
assert(isExtendedVT(VT) && "Unknown simple vector type!");
return VT & ElementMask;
case v8i8 :
case v16i8: return i8;
case v4i16:
@ -156,11 +180,11 @@ namespace MVT { // MVT = Machine Value Types
/// MVT::getVectorNumElements - Given a vector type, return the
/// number of elements it contains.
static inline unsigned getVectorNumElements(ValueType VT) {
assert(isVector(VT) && "Invalid vector type!");
switch (VT) {
default:
if (isExtendedVT(VT))
return ((VT & ~SimpleTypeMask) >> SimpleTypeBits) - 1;
assert(0 && "Invalid vector type!");
assert(isExtendedVT(VT) && "Unknown simple vector type!");
return ((VT & VectorMask) >> (32 - VectorBits)) - 1;
case v16i8: return 16;
case v8i8 :
case v8i16: return 8;
@ -183,10 +207,13 @@ namespace MVT { // MVT = Machine Value Types
static inline unsigned getSizeInBits(ValueType VT) {
switch (VT) {
default:
if (isExtendedVT(VT))
assert(isExtendedVT(VT) && "ValueType has no known size!");
if (isVector(VT))
return getSizeInBits(getVectorElementType(VT)) *
getVectorNumElements(VT);
assert(0 && "ValueType has no known size!");
if (isInteger(VT))
return ((VT & PrecisionMask) >> SimpleTypeBits) + 1;
assert(0 && "Unknown value type!");
case MVT::i1 : return 1;
case MVT::i8 : return 8;
case MVT::i16 : return 16;
@ -214,6 +241,45 @@ namespace MVT { // MVT = Machine Value Types
}
}
/// MVT::getIntegerType - Returns the ValueType that represents an integer
/// with the given number of bits.
///
static inline ValueType getIntegerType(unsigned BitWidth) {
switch (BitWidth) {
default:
break;
case 1:
return MVT::i1;
case 8:
return MVT::i8;
case 16:
return MVT::i16;
case 32:
return MVT::i32;
case 64:
return MVT::i64;
case 128:
return MVT::i128;
}
ValueType Result = iAny |
(((BitWidth - 1) << SimpleTypeBits) & PrecisionMask);
assert(getSizeInBits(Result) == BitWidth && "Bad bit width!");
return Result;
}
/// MVT::RoundIntegerType - Rounds the bit-width of the given integer
/// ValueType up to the nearest power of two (and at least to eight),
/// and returns the integer ValueType with that number of bits.
///
static inline ValueType RoundIntegerType(ValueType VT) {
assert(isInteger(VT) && !isVector(VT) && "Invalid integer type!");
unsigned BitWidth = getSizeInBits(VT);
if (BitWidth <= 8)
return MVT::i8;
else
return getIntegerType(1 << Log2_32_Ceil(BitWidth));
}
/// MVT::getVectorType - Returns the ValueType that represents a vector
/// NumElements in length, where each element is of type VT.
///
@ -247,7 +313,7 @@ namespace MVT { // MVT = Machine Value Types
if (NumElements == 2) return MVT::v2f64;
break;
}
ValueType Result = VT | ((NumElements + 1) << SimpleTypeBits);
ValueType Result = VT | ((NumElements + 1) << (32 - VectorBits));
assert(getVectorElementType(Result) == VT &&
"Bad vector element type!");
assert(getVectorNumElements(Result) == NumElements &&

View File

@ -150,7 +150,13 @@ public:
}
LegalizeAction getTypeAction(MVT::ValueType VT) const {
if (MVT::isExtendedVT(VT)) return Expand;
if (MVT::isExtendedVT(VT)) {
if (MVT::isVector(VT)) return Expand;
if (MVT::isInteger(VT))
// First promote to a power-of-two size, then expand if necessary.
return VT == MVT::RoundIntegerType(VT) ? Expand : Promote;
assert(0 && "Unsupported extended type!");
}
return (LegalizeAction)((ValueTypeActions[VT>>4] >> ((2*VT) & 31)) & 3);
}
void setTypeAction(MVT::ValueType VT, LegalizeAction Action) {
@ -179,11 +185,26 @@ public:
/// to get to the smaller register. For illegal floating point types, this
/// returns the integer type to transform to.
MVT::ValueType getTypeToTransformTo(MVT::ValueType VT) const {
if (MVT::isExtendedVT(VT))
if (!MVT::isExtendedVT(VT)) {
MVT::ValueType NVT = TransformToType[VT];
assert(getTypeAction(NVT) != Promote &&
"Promote may not follow Expand or Promote");
return NVT;
}
if (MVT::isVector(VT))
return MVT::getVectorType(MVT::getVectorElementType(VT),
MVT::getVectorNumElements(VT) / 2);
return TransformToType[VT];
if (MVT::isInteger(VT)) {
MVT::ValueType NVT = MVT::RoundIntegerType(VT);
if (NVT == VT)
// Size is a power of two - expand to half the size.
return MVT::getIntegerType(MVT::getSizeInBits(VT) / 2);
else
// Promote to a power of two size, avoiding multi-step promotion.
return getTypeAction(NVT) == Promote ? getTypeToTransformTo(NVT) : NVT;
}
assert(0 && "Unsupported extended type!");
}
/// getTypeToExpandTo - For types supported by the target, this is an
@ -191,7 +212,7 @@ public:
/// that are larger than the largest integer register or illegal floating
/// point types), this returns the largest legal type it will be expanded to.
MVT::ValueType getTypeToExpandTo(MVT::ValueType VT) const {
assert(!MVT::isExtendedVT(VT));
assert(!MVT::isVector(VT));
while (true) {
switch (getTypeAction(VT)) {
case Legal:
@ -252,7 +273,7 @@ public:
/// expanded to some other code sequence, or the target has a custom expander
/// for it.
LegalizeAction getOperationAction(unsigned Op, MVT::ValueType VT) const {
if (MVT::isExtendedVT(VT)) return Expand;
if (MVT::isExtendedVT(VT)) return getTypeAction(VT);
return (LegalizeAction)((OpActions[Op] >> (2*VT)) & 3);
}
@ -268,7 +289,7 @@ public:
/// expanded to some other code sequence, or the target has a custom expander
/// for it.
LegalizeAction getLoadXAction(unsigned LType, MVT::ValueType VT) const {
if (MVT::isExtendedVT(VT)) return Expand;
if (MVT::isExtendedVT(VT)) return getTypeAction(VT);
return (LegalizeAction)((LoadXActions[LType] >> (2*VT)) & 3);
}
@ -284,7 +305,7 @@ public:
/// expanded to some other code sequence, or the target has a custom expander
/// for it.
LegalizeAction getStoreXAction(MVT::ValueType VT) const {
if (MVT::isExtendedVT(VT)) return Expand;
if (MVT::isExtendedVT(VT)) return getTypeAction(VT);
return (LegalizeAction)((StoreXActions >> (2*VT)) & 3);
}
@ -300,7 +321,7 @@ public:
/// for it.
LegalizeAction
getIndexedLoadAction(unsigned IdxMode, MVT::ValueType VT) const {
if (MVT::isExtendedVT(VT)) return Expand;
if (MVT::isExtendedVT(VT)) return getTypeAction(VT);
return (LegalizeAction)((IndexedModeActions[0][IdxMode] >> (2*VT)) & 3);
}
@ -317,7 +338,7 @@ public:
/// for it.
LegalizeAction
getIndexedStoreAction(unsigned IdxMode, MVT::ValueType VT) const {
if (MVT::isExtendedVT(VT)) return Expand;
if (MVT::isExtendedVT(VT)) return getTypeAction(VT);
return (LegalizeAction)((IndexedModeActions[1][IdxMode] >> (2*VT)) & 3);
}
@ -385,11 +406,13 @@ public:
MVT::ValueType getRegisterType(MVT::ValueType VT) const {
if (!MVT::isExtendedVT(VT))
return RegisterTypeForVT[VT];
MVT::ValueType VT1, RegisterVT;
unsigned NumIntermediates;
(void)getVectorTypeBreakdown(VT, VT1, NumIntermediates, RegisterVT);
return RegisterVT;
if (MVT::isVector(VT)) {
MVT::ValueType VT1, RegisterVT;
unsigned NumIntermediates;
(void)getVectorTypeBreakdown(VT, VT1, NumIntermediates, RegisterVT);
return RegisterVT;
}
assert(0 && "Unsupported extended type!");
}
/// getNumRegisters - Return the number of registers that this ValueType will
@ -399,10 +422,12 @@ public:
unsigned getNumRegisters(MVT::ValueType VT) const {
if (!MVT::isExtendedVT(VT))
return NumRegistersForVT[VT];
MVT::ValueType VT1, VT2;
unsigned NumIntermediates;
return getVectorTypeBreakdown(VT, VT1, NumIntermediates, VT2);
if (MVT::isVector(VT)) {
MVT::ValueType VT1, VT2;
unsigned NumIntermediates;
return getVectorTypeBreakdown(VT, VT1, NumIntermediates, VT2);
}
assert(0 && "Unsupported extended type!");
}
/// hasTargetDAGCombine - If true, the target has custom DAG combine

View File

@ -28,6 +28,7 @@
#include "llvm/Target/TargetMachine.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include <algorithm>
@ -846,6 +847,7 @@ SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
}
SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
assert(!MVT::isExtendedVT(VT) && "Expecting a simple value type!");
if ((unsigned)VT >= ValueTypeNodes.size())
ValueTypeNodes.resize(VT+1);
if (ValueTypeNodes[VT] == 0) {
@ -1734,7 +1736,8 @@ SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
"Invalid SIGN_EXTEND!");
if (Operand.getValueType() == VT) return Operand; // noop extension
assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
&& "Invalid sext node, dst < src!");
if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
break;
@ -1742,7 +1745,8 @@ SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
"Invalid ZERO_EXTEND!");
if (Operand.getValueType() == VT) return Operand; // noop extension
assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
&& "Invalid zext node, dst < src!");
if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
break;
@ -1750,7 +1754,8 @@ SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
"Invalid ANY_EXTEND!");
if (Operand.getValueType() == VT) return Operand; // noop extension
assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
&& "Invalid anyext node, dst < src!");
if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
// (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
@ -1759,15 +1764,18 @@ SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
"Invalid TRUNCATE!");
if (Operand.getValueType() == VT) return Operand; // noop truncate
assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
&& "Invalid truncate node, src < dst!");
if (OpOpcode == ISD::TRUNCATE)
return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
OpOpcode == ISD::ANY_EXTEND) {
// If the source is smaller than the dest, we still need an extend.
if (Operand.Val->getOperand(0).getValueType() < VT)
if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
< MVT::getSizeInBits(VT))
return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
else if (Operand.Val->getOperand(0).getValueType() > VT)
else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
> MVT::getSizeInBits(VT))
return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
else
return Operand.Val->getOperand(0);
@ -1874,7 +1882,8 @@ SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
assert(VT == N1.getValueType() && "Not an inreg round!");
assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
"Cannot FP_ROUND_INREG integer types");
assert(EVT <= VT && "Not rounding down!");
assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
"Not rounding down!");
break;
}
case ISD::AssertSext:
@ -1884,7 +1893,8 @@ SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
assert(VT == N1.getValueType() && "Not an inreg extend!");
assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
"Cannot *_EXTEND_INREG FP types");
assert(EVT <= VT && "Not extending!");
assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
"Not extending!");
}
default: break;
@ -2299,7 +2309,8 @@ SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
if (MVT::isVector(VT))
assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
else
assert(EVT < VT && "Should only be an extending load, not truncating!");
assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
"Should only be an extending load, not truncating!");
assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
"Cannot sign/zero extend a FP/Vector load!");
assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
@ -2415,7 +2426,8 @@ SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
MVT::ValueType VT = Val.getValueType();
bool isTrunc = VT != SVT;
assert(VT > SVT && "Not a truncation?");
assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
"Not a truncation?");
assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
"Can't do FP-INT conversion!");
@ -2648,18 +2660,7 @@ SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
}
SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
if (!MVT::isExtendedVT(VT))
return makeVTList(SDNode::getValueTypeList(VT), 1);
for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
E = VTList.end(); I != E; ++I) {
if (I->size() == 1 && (*I)[0] == VT)
return makeVTList(&(*I)[0], 1);
}
std::vector<MVT::ValueType> V;
V.push_back(VT);
VTList.push_front(V);
return makeVTList(&(*VTList.begin())[0], 1);
return makeVTList(SDNode::getValueTypeList(VT), 1);
}
SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
@ -3427,9 +3428,14 @@ void SDNode::Profile(FoldingSetNodeID &ID) {
/// getValueTypeList - Return a pointer to the specified value type.
///
MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
VTs[VT] = VT;
return &VTs[VT];
if (MVT::isExtendedVT(VT)) {
static std::set<MVT::ValueType> EVTs;
return (MVT::ValueType *)&(*EVTs.insert(VT).first);
} else {
static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
VTs[VT] = VT;
return &VTs[VT];
}
}
/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the

View File

@ -22,9 +22,11 @@ using namespace llvm;
std::string MVT::getValueTypeString(MVT::ValueType VT) {
switch (VT) {
default:
if (isExtendedVT(VT))
if (isVector(VT))
return "v" + utostr(getVectorNumElements(VT)) +
getValueTypeString(getVectorElementType(VT));
if (isInteger(VT))
return "i" + utostr(getSizeInBits(VT));
assert(0 && "Invalid ValueType!");
case MVT::i1: return "i1";
case MVT::i8: return "i8";
@ -62,9 +64,11 @@ std::string MVT::getValueTypeString(MVT::ValueType VT) {
const Type *MVT::getTypeForValueType(MVT::ValueType VT) {
switch (VT) {
default:
if (isExtendedVT(VT))
if (isVector(VT))
return VectorType::get(getTypeForValueType(getVectorElementType(VT)),
getVectorNumElements(VT));
if (isInteger(VT))
return IntegerType::get(getSizeInBits(VT));
assert(0 && "ValueType does not correspond to LLVM type!");
case MVT::isVoid:return Type::VoidTy;
case MVT::i1: return Type::Int1Ty;
@ -105,19 +109,7 @@ MVT::ValueType MVT::getValueType(const Type *Ty, bool HandleUnknown) {
case Type::VoidTyID:
return MVT::isVoid;
case Type::IntegerTyID:
switch (cast<IntegerType>(Ty)->getBitWidth()) {
default:
// FIXME: Return MVT::iANY.
if (HandleUnknown) return MVT::Other;
assert(0 && "Invalid width for value type");
case 1: return MVT::i1;
case 8: return MVT::i8;
case 16: return MVT::i16;
case 32: return MVT::i32;
case 64: return MVT::i64;
case 128: return MVT::i128;
}
break;
return getIntegerType(cast<IntegerType>(Ty)->getBitWidth());
case Type::FloatTyID: return MVT::f32;
case Type::DoubleTyID: return MVT::f64;
case Type::X86_FP80TyID: return MVT::f80;