209e7c92c55614fb8a6ccbb701108bb1059e9433
[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 MVT type is used by tablegen as well as in LLVM. In order to handle
11 // extended types, the MVT 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/Streams.h"
19 #include <map>
20 #include <vector>
21 using namespace llvm;
22
23 namespace llvm {
24
25 class Type {
26 public:
27   virtual unsigned getSizeInBits() const = 0;
28 };
29
30 }
31
32 class ExtendedIntegerType : public Type {
33   unsigned BitWidth;
34 public:
35   explicit ExtendedIntegerType(unsigned bits)
36     : BitWidth(bits) {}
37   unsigned getSizeInBits() const {
38     return getBitWidth();
39   }
40   unsigned getBitWidth() const {
41     return BitWidth;
42   }
43 };
44
45 class ExtendedVectorType : public Type {
46   MVT ElementType;
47   unsigned NumElements;
48 public:
49   ExtendedVectorType(MVT elty, unsigned num)
50     : ElementType(elty), NumElements(num) {}
51   unsigned getSizeInBits() const {
52     return getNumElements() * getElementType().getSizeInBits();
53   }
54   MVT getElementType() const {
55     return ElementType;
56   }
57   unsigned getNumElements() const {
58     return NumElements;
59   }
60 };
61
62 static std::map<unsigned, const Type *>
63   ExtendedIntegerTypeMap;
64 static std::map<std::pair<uintptr_t, uintptr_t>, const Type *>
65   ExtendedVectorTypeMap;
66
67 MVT MVT::getExtendedIntegerVT(unsigned BitWidth) {
68   const Type *&ET = ExtendedIntegerTypeMap[BitWidth];
69   if (!ET) ET = new ExtendedIntegerType(BitWidth);
70   MVT VT;
71   VT.LLVMTy = ET;
72   return VT;
73 }
74
75 MVT MVT::getExtendedVectorVT(MVT VT, unsigned NumElements) {
76   const Type *&ET = ExtendedVectorTypeMap[std::make_pair(VT.getRawBits(),
77                                                          NumElements)];
78   if (!ET) ET = new ExtendedVectorType(VT, NumElements);
79   MVT ResultVT;
80   ResultVT.LLVMTy = ET;
81   return ResultVT;
82 }
83
84 bool MVT::isExtendedFloatingPoint() const {
85   assert(isExtended() && "Type is not extended!");
86   // Extended floating-point types are not supported yet.
87   return false;
88 }
89
90 bool MVT::isExtendedInteger() const {
91   assert(isExtended() && "Type is not extended!");
92   return dynamic_cast<const ExtendedIntegerType *>(LLVMTy) != 0;
93 }
94
95 bool MVT::isExtendedVector() const {
96   assert(isExtended() && "Type is not extended!");
97   return dynamic_cast<const ExtendedVectorType *>(LLVMTy) != 0;
98 }
99
100 bool MVT::isExtended64BitVector() const {
101   assert(isExtended() && "Type is not extended!");
102   return isExtendedVector() && getSizeInBits() == 64;
103 }
104
105 bool MVT::isExtended128BitVector() const {
106   assert(isExtended() && "Type is not extended!");
107   return isExtendedVector() && getSizeInBits() == 128;
108 }
109
110 MVT MVT::getExtendedVectorElementType() const {
111   assert(isExtendedVector() && "Type is not an extended vector!");
112   return static_cast<const ExtendedVectorType *>(LLVMTy)->getElementType();
113 }
114
115 unsigned MVT::getExtendedVectorNumElements() const {
116   assert(isExtendedVector() && "Type is not an extended vector!");
117   return static_cast<const ExtendedVectorType *>(LLVMTy)->getNumElements();
118 }
119
120 unsigned MVT::getExtendedSizeInBits() const {
121   assert(isExtended() && "Type is not extended!");
122   return LLVMTy->getSizeInBits();
123 }