Change MCOperand to use Create style instead of Make style for constructing
[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 <map>
19 #include <vector>
20 using namespace llvm;
21
22 namespace llvm {
23
24 class Type {
25 public:
26   virtual unsigned getSizeInBits() const = 0;
27   virtual ~Type() {}
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   assert(VT.isExtended() && "Type is not extended!");
73   return VT;
74 }
75
76 MVT MVT::getExtendedVectorVT(MVT VT, unsigned NumElements) {
77   const Type *&ET = ExtendedVectorTypeMap[std::make_pair(VT.getRawBits(),
78                                                          NumElements)];
79   if (!ET) ET = new ExtendedVectorType(VT, NumElements);
80   MVT ResultVT;
81   ResultVT.LLVMTy = ET;
82   assert(ResultVT.isExtended() && "Type is not extended!");
83   return ResultVT;
84 }
85
86 bool MVT::isExtendedFloatingPoint() const {
87   assert(isExtended() && "Type is not extended!");
88   // Extended floating-point types are not supported yet.
89   return false;
90 }
91
92 bool MVT::isExtendedInteger() const {
93   assert(isExtended() && "Type is not extended!");
94   return dynamic_cast<const ExtendedIntegerType *>(LLVMTy) != 0;
95 }
96
97 bool MVT::isExtendedVector() const {
98   assert(isExtended() && "Type is not extended!");
99   return dynamic_cast<const ExtendedVectorType *>(LLVMTy) != 0;
100 }
101
102 bool MVT::isExtended64BitVector() const {
103   assert(isExtended() && "Type is not extended!");
104   return isExtendedVector() && getSizeInBits() == 64;
105 }
106
107 bool MVT::isExtended128BitVector() const {
108   assert(isExtended() && "Type is not extended!");
109   return isExtendedVector() && getSizeInBits() == 128;
110 }
111
112 MVT MVT::getExtendedVectorElementType() const {
113   assert(isExtendedVector() && "Type is not an extended vector!");
114   return static_cast<const ExtendedVectorType *>(LLVMTy)->getElementType();
115 }
116
117 unsigned MVT::getExtendedVectorNumElements() const {
118   assert(isExtendedVector() && "Type is not an extended vector!");
119   return static_cast<const ExtendedVectorType *>(LLVMTy)->getNumElements();
120 }
121
122 unsigned MVT::getExtendedSizeInBits() const {
123   assert(isExtended() && "Type is not extended!");
124   return LLVMTy->getSizeInBits();
125 }