81c0d4c544fc99f17f43b870db6e390886f63ff4
[oota-llvm.git] / utils / TableGen / TGValueTypes.cpp
1 //===- ValueTypes.cpp - Tablegen extended ValueType implementation --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // The EVT type is used by tablegen as well as in LLVM. In order to handle
11 // extended types, the EVT type uses support functions that call into
12 // LLVM's type system code. These aren't accessible in tablegen, so this
13 // file provides simple replacements.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/CodeGen/ValueTypes.h"
18 #include "llvm/Support/Casting.h"
19 #include <map>
20 using namespace llvm;
21
22 namespace llvm {
23
24 class Type {
25 protected:
26   enum TypeKind {
27     TK_ExtendedIntegerType,
28     TK_ExtendedVectorType
29   };
30 private:
31   TypeKind Kind;
32 public:
33   TypeKind getKind() const {
34     return Kind;
35   }
36   Type(TypeKind K) : Kind(K) {}
37   virtual unsigned getSizeInBits() const = 0;
38   virtual ~Type();
39 };
40
41 Type::~Type() {}
42
43 }
44
45 class ExtendedIntegerType : public Type {
46   unsigned BitWidth;
47 public:
48   explicit ExtendedIntegerType(unsigned bits)
49     : Type(TK_ExtendedIntegerType), BitWidth(bits) {}
50   virtual ~ExtendedIntegerType();
51   static bool classof(const Type *T) {
52     return T->getKind() == TK_ExtendedIntegerType;
53   }
54   virtual unsigned getSizeInBits() const {
55     return getBitWidth();
56   }
57   unsigned getBitWidth() const {
58     return BitWidth;
59   }
60 };
61
62 ExtendedIntegerType::~ExtendedIntegerType() {}
63
64
65 class ExtendedVectorType : public Type {
66   EVT ElementType;
67   unsigned NumElements;
68 public:
69   ExtendedVectorType(EVT elty, unsigned num)
70     : Type(TK_ExtendedVectorType), ElementType(elty), NumElements(num) {}
71   virtual ~ExtendedVectorType();
72   static bool classof(const Type *T) {
73     return T->getKind() == TK_ExtendedVectorType;
74   }
75   virtual unsigned getSizeInBits() const {
76     return getNumElements() * getElementType().getSizeInBits();
77   }
78   EVT getElementType() const {
79     return ElementType;
80   }
81   unsigned getNumElements() const {
82     return NumElements;
83   }
84 };
85
86 ExtendedVectorType::~ExtendedVectorType() {}
87
88
89 static std::map<unsigned, const Type *>
90   ExtendedIntegerTypeMap;
91 static std::map<std::pair<uintptr_t, uintptr_t>, const Type *>
92   ExtendedVectorTypeMap;
93
94 bool EVT::isExtendedFloatingPoint() const {
95   assert(isExtended() && "Type is not extended!");
96   // Extended floating-point types are not supported yet.
97   return false;
98 }
99
100 bool EVT::isExtendedInteger() const {
101   assert(isExtended() && "Type is not extended!");
102   return isa<ExtendedIntegerType>(LLVMTy);
103 }
104
105 bool EVT::isExtendedVector() const {
106   assert(isExtended() && "Type is not extended!");
107   return isa<ExtendedVectorType>(LLVMTy);
108 }
109
110 bool EVT::isExtended64BitVector() const {
111   assert(isExtended() && "Type is not extended!");
112   return isExtendedVector() && getSizeInBits() == 64;
113 }
114
115 bool EVT::isExtended128BitVector() const {
116   assert(isExtended() && "Type is not extended!");
117   return isExtendedVector() && getSizeInBits() == 128;
118 }
119
120 EVT EVT::getExtendedVectorElementType() const {
121   assert(isExtendedVector() && "Type is not an extended vector!");
122   return static_cast<const ExtendedVectorType *>(LLVMTy)->getElementType();
123 }
124
125 unsigned EVT::getExtendedVectorNumElements() const {
126   assert(isExtendedVector() && "Type is not an extended vector!");
127   return static_cast<const ExtendedVectorType *>(LLVMTy)->getNumElements();
128 }
129
130 unsigned EVT::getExtendedSizeInBits() const {
131   assert(isExtended() && "Type is not extended!");
132   return LLVMTy->getSizeInBits();
133 }