Fix a typo that caused combiner to create mal-formed pre-indexed store where value...
[oota-llvm.git] / lib / VMCore / ValueTypes.cpp
1 //===-- ValueTypes.cpp - Implementation of MVT::ValueType methods ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements methods in the CodeGen/ValueTypes.h header.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/CodeGen/ValueTypes.h"
15 #include "llvm/Type.h"
16 #include "llvm/DerivedTypes.h"
17 using namespace llvm;
18
19 /// MVT::getValueTypeString - This function returns value type as a string,
20 /// e.g. "i32".
21 const char *MVT::getValueTypeString(MVT::ValueType VT) {
22   switch (VT) {
23   default: assert(0 && "Invalid ValueType!");
24   case MVT::i1:    return "i1";
25   case MVT::i8:    return "i8";
26   case MVT::i16:   return "i16";
27   case MVT::i32:   return "i32";
28   case MVT::i64:   return "i64";
29   case MVT::i128:  return "i128";
30   case MVT::f32:   return "f32";
31   case MVT::f64:   return "f64";
32   case MVT::f80:   return "f80";
33   case MVT::f128:  return "f128";
34   case MVT::isVoid:return "isVoid";
35   case MVT::Other: return "ch";
36   case MVT::Flag:  return "flag";
37   case MVT::Vector:return "vec";
38   case MVT::v8i8:  return "v8i8";
39   case MVT::v4i16: return "v4i16";
40   case MVT::v2i32: return "v2i32";
41   case MVT::v1i64: return "v1i64";
42   case MVT::v16i8: return "v16i8";
43   case MVT::v8i16: return "v8i16";
44   case MVT::v4i32: return "v4i32";
45   case MVT::v2i64: return "v2i64";
46   case MVT::v4f32: return "v4f32";
47   case MVT::v2f64: return "v2f64";
48   }
49 }
50
51 /// MVT::getVectorType - Returns the ValueType that represents a vector
52 /// NumElements in length, where each element is of type VT.  If there is no
53 /// ValueType that represents this vector, a ValueType of Other is returned.
54 ///
55 MVT::ValueType MVT::getVectorType(ValueType VT, unsigned NumElements) {
56   switch (VT) {
57   default: 
58     break;
59   case MVT::i8:
60     if (NumElements == 8)  return MVT::v8i8;
61     if (NumElements == 16) return MVT::v16i8;
62     break;
63   case MVT::i16:
64     if (NumElements == 4)  return MVT::v4i16;
65     if (NumElements == 8)  return MVT::v8i16;
66     break;
67   case MVT::i32:
68     if (NumElements == 2)  return MVT::v2i32;
69     if (NumElements == 4)  return MVT::v4i32;
70     break;
71   case MVT::i64:
72     if (NumElements == 1)  return MVT::v1i64;
73     if (NumElements == 2)  return MVT::v2i64;
74     break;
75   case MVT::f32:
76     if (NumElements == 2)  return MVT::v2f32;
77     if (NumElements == 4)  return MVT::v4f32;
78     break;
79   case MVT::f64:
80     if (NumElements == 2)  return MVT::v2f64;
81     break;
82   }
83   return MVT::Other;
84 }
85
86 /// MVT::getTypeForValueType - This method returns an LLVM type corresponding
87 /// to the specified ValueType.  Note that this will abort for types that cannot
88 /// be represented.
89 const Type *MVT::getTypeForValueType(MVT::ValueType VT) {
90   switch (VT) {
91   default: assert(0 && "ValueType does not correspond to LLVM type!");
92   case MVT::isVoid:return Type::VoidTy;
93   case MVT::i1:    return Type::Int1Ty;
94   case MVT::i8:    return Type::Int8Ty;
95   case MVT::i16:   return Type::Int16Ty;
96   case MVT::i32:   return Type::Int32Ty;
97   case MVT::i64:   return Type::Int64Ty;
98   case MVT::i128:  return IntegerType::get(128);
99   case MVT::f32:   return Type::FloatTy;
100   case MVT::f64:   return Type::DoubleTy;
101   case MVT::v8i8:  return VectorType::get(Type::Int8Ty, 8);
102   case MVT::v4i16: return VectorType::get(Type::Int16Ty, 4);
103   case MVT::v2i32: return VectorType::get(Type::Int32Ty, 2);
104   case MVT::v1i64: return VectorType::get(Type::Int64Ty, 1);
105   case MVT::v16i8: return VectorType::get(Type::Int8Ty, 16);
106   case MVT::v8i16: return VectorType::get(Type::Int16Ty, 8);
107   case MVT::v4i32: return VectorType::get(Type::Int32Ty, 4);
108   case MVT::v2i64: return VectorType::get(Type::Int64Ty, 2);
109   case MVT::v4f32: return VectorType::get(Type::FloatTy, 4);
110   case MVT::v2f64: return VectorType::get(Type::DoubleTy, 2);
111   }
112 }
113
114 /// MVT::getValueType - Return the value type corresponding to the specified
115 /// type.  This returns all vectors as MVT::Vector and all pointers as
116 /// MVT::iPTR.  If HandleUnknown is true, unknown types are returned as Other,
117 /// otherwise they are invalid.
118 MVT::ValueType MVT::getValueType(const Type *Ty, bool HandleUnknown) {
119   switch (Ty->getTypeID()) {
120   default:
121     if (HandleUnknown) return MVT::Other;
122     assert(0 && "Unknown type!");
123   case Type::VoidTyID:
124     return MVT::isVoid;
125   case Type::IntegerTyID:
126     switch (cast<IntegerType>(Ty)->getBitWidth()) {
127     default:
128       // FIXME: Return MVT::iANY.
129       if (HandleUnknown) return MVT::Other;
130       assert(0 && "Invalid width for value type");
131     case 1:    return MVT::i1;
132     case 8:    return MVT::i8;
133     case 16:   return MVT::i16;
134     case 32:   return MVT::i32;
135     case 64:   return MVT::i64;
136     case 128:  return MVT::i128;
137     }
138     break;
139   case Type::FloatTyID:   return MVT::f32;
140   case Type::DoubleTyID:  return MVT::f64;
141   case Type::PointerTyID: return MVT::iPTR;
142   case Type::VectorTyID:  return MVT::Vector;
143   }
144 }