e421d5d43036d143792c4e3b91db2ca4c4fef07c
[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::v16i8: return "v16i8";
42   case MVT::v8i16: return "v8i16";
43   case MVT::v4i32: return "v4i32";
44   case MVT::v2i64: return "v2i64";
45   case MVT::v4f32: return "v4f32";
46   case MVT::v2f64: return "v2f64";
47   }
48 }
49
50 /// MVT::getVectorType - Returns the ValueType that represents a vector
51 /// NumElements in length, where each element is of type VT.  If there is no
52 /// ValueType that represents this vector, a ValueType of Other is returned.
53 ///
54 MVT::ValueType MVT::getVectorType(ValueType VT, unsigned NumElements) {
55   switch (VT) {
56   default: 
57     break;
58   case MVT::i8:
59     if (NumElements == 8)  return MVT::v8i8;
60     if (NumElements == 16) return MVT::v16i8;
61     break;
62   case MVT::i16:
63     if (NumElements == 4)  return MVT::v4i16;
64     if (NumElements == 8)  return MVT::v8i16;
65     break;
66   case MVT::i32:
67     if (NumElements == 2)  return MVT::v2i32;
68     if (NumElements == 4)  return MVT::v4i32;
69     break;
70   case MVT::i64:
71     if (NumElements == 2)  return MVT::v2i64;
72     break;
73   case MVT::f32:
74     if (NumElements == 2)  return MVT::v2f32;
75     if (NumElements == 4)  return MVT::v4f32;
76     break;
77   case MVT::f64:
78     if (NumElements == 2)  return MVT::v2f64;
79     break;
80   }
81   return MVT::Other;
82 }
83
84 /// MVT::getTypeForValueType - This method returns an LLVM type corresponding
85 /// to the specified ValueType.  Note that this will abort for types that cannot
86 /// be represented.
87 const Type *MVT::getTypeForValueType(MVT::ValueType VT) {
88   switch (VT) {
89   default: assert(0 && "ValueType does not correspond to LLVM type!");
90   case MVT::isVoid:return Type::VoidTy;
91   case MVT::i1:    return Type::Int1Ty;
92   case MVT::i8:    return Type::Int8Ty;
93   case MVT::i16:   return Type::Int16Ty;
94   case MVT::i32:   return Type::Int32Ty;
95   case MVT::i64:   return Type::Int64Ty;
96   case MVT::f32:   return Type::FloatTy;
97   case MVT::f64:   return Type::DoubleTy;
98   case MVT::v8i8:  return PackedType::get(Type::Int8Ty, 8);
99   case MVT::v4i16: return PackedType::get(Type::Int16Ty, 4);
100   case MVT::v2i32: return PackedType::get(Type::Int32Ty, 2);
101   case MVT::v16i8: return PackedType::get(Type::Int8Ty, 16);
102   case MVT::v8i16: return PackedType::get(Type::Int16Ty, 8);
103   case MVT::v4i32: return PackedType::get(Type::Int32Ty, 4);
104   case MVT::v2i64: return PackedType::get(Type::Int64Ty, 2);
105   case MVT::v4f32: return PackedType::get(Type::FloatTy, 4);
106   case MVT::v2f64: return PackedType::get(Type::DoubleTy, 2);
107   }
108 }