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