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:
getKind() const33   TypeKind getKind() const {
34     return Kind;
35   }
Type(TypeKind K)36   Type(TypeKind K) : Kind(K) {}
37   virtual unsigned getSizeInBits() const = 0;
38   virtual ~Type();
39 };
40 
41 // Provide out-of-line definition to prevent weak vtable.
~Type()42 Type::~Type() {}
43 
44 }
45 
46 namespace {
47 class ExtendedIntegerType : public Type {
48   unsigned BitWidth;
49 public:
ExtendedIntegerType(unsigned bits)50   explicit ExtendedIntegerType(unsigned bits)
51     : Type(TK_ExtendedIntegerType), BitWidth(bits) {}
classof(const Type * T)52   static bool classof(const Type *T) {
53     return T->getKind() == TK_ExtendedIntegerType;
54   }
getSizeInBits() const55   virtual unsigned getSizeInBits() const {
56     return getBitWidth();
57   }
getBitWidth() const58   unsigned getBitWidth() const {
59     return BitWidth;
60   }
61 };
62 
63 class ExtendedVectorType : public Type {
64   EVT ElementType;
65   unsigned NumElements;
66 public:
ExtendedVectorType(EVT elty,unsigned num)67   ExtendedVectorType(EVT elty, unsigned num)
68     : Type(TK_ExtendedVectorType), ElementType(elty), NumElements(num) {}
classof(const Type * T)69   static bool classof(const Type *T) {
70     return T->getKind() == TK_ExtendedVectorType;
71   }
getSizeInBits() const72   virtual unsigned getSizeInBits() const {
73     return getNumElements() * getElementType().getSizeInBits();
74   }
getElementType() const75   EVT getElementType() const {
76     return ElementType;
77   }
getNumElements() const78   unsigned getNumElements() const {
79     return NumElements;
80   }
81 };
82 } // end anonymous namespace
83 
84 static std::map<unsigned, const Type *>
85   ExtendedIntegerTypeMap;
86 static std::map<std::pair<uintptr_t, uintptr_t>, const Type *>
87   ExtendedVectorTypeMap;
88 
isExtendedFloatingPoint() const89 bool EVT::isExtendedFloatingPoint() const {
90   assert(isExtended() && "Type is not extended!");
91   // Extended floating-point types are not supported yet.
92   return false;
93 }
94 
isExtendedInteger() const95 bool EVT::isExtendedInteger() const {
96   assert(isExtended() && "Type is not extended!");
97   return isa<ExtendedIntegerType>(LLVMTy);
98 }
99 
isExtendedVector() const100 bool EVT::isExtendedVector() const {
101   assert(isExtended() && "Type is not extended!");
102   return isa<ExtendedVectorType>(LLVMTy);
103 }
104 
isExtended64BitVector() const105 bool EVT::isExtended64BitVector() const {
106   assert(isExtended() && "Type is not extended!");
107   return isExtendedVector() && getSizeInBits() == 64;
108 }
109 
isExtended128BitVector() const110 bool EVT::isExtended128BitVector() const {
111   assert(isExtended() && "Type is not extended!");
112   return isExtendedVector() && getSizeInBits() == 128;
113 }
114 
getExtendedVectorElementType() const115 EVT EVT::getExtendedVectorElementType() const {
116   assert(isExtendedVector() && "Type is not an extended vector!");
117   return static_cast<const ExtendedVectorType *>(LLVMTy)->getElementType();
118 }
119 
getExtendedVectorNumElements() const120 unsigned EVT::getExtendedVectorNumElements() const {
121   assert(isExtendedVector() && "Type is not an extended vector!");
122   return static_cast<const ExtendedVectorType *>(LLVMTy)->getNumElements();
123 }
124 
getExtendedSizeInBits() const125 unsigned EVT::getExtendedSizeInBits() const {
126   assert(isExtended() && "Type is not extended!");
127   return LLVMTy->getSizeInBits();
128 }
129