34f6041137c12635a870f64b66bb93a1201e1ad2
[oota-llvm.git] / include / llvm / Target / CostTable.h
1 //===-- CostTable.h - Instruction Cost Table handling -----------*- C++ -*-===//
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 /// \file
11 /// \brief Cost tables and simple lookup functions
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_TARGET_COSTTABLE_H_
16 #define LLVM_TARGET_COSTTABLE_H_
17
18 namespace llvm {
19
20 /// Cost Table Entry
21 template <class TypeTy>
22 struct CostTblEntry {
23   int ISD;
24   TypeTy Type;
25   unsigned Cost;
26 };
27
28 /// Find in cost table, TypeTy must be comparable to CompareTy by ==
29 template <class TypeTy, class CompareTy>
30 int CostTableLookup(const CostTblEntry<TypeTy> *Tbl, unsigned len, int ISD,
31                     CompareTy Ty) {
32   for (unsigned int i = 0; i < len; ++i)
33     if (ISD == Tbl[i].ISD && Ty == Tbl[i].Type)
34       return i;
35
36   // Could not find an entry.
37   return -1;
38 }
39
40 /// Find in cost table, TypeTy must be comparable to CompareTy by ==
41 template <class TypeTy, class CompareTy, unsigned N>
42 int CostTableLookup(const CostTblEntry<TypeTy>(&Tbl)[N], int ISD,
43                     CompareTy Ty) {
44   return CostTableLookup(Tbl, N, ISD, Ty);
45 }
46
47 /// Type Conversion Cost Table
48 template <class TypeTy>
49 struct TypeConversionCostTblEntry {
50   int ISD;
51   TypeTy Dst;
52   TypeTy Src;
53   unsigned Cost;
54 };
55
56 /// Find in type conversion cost table, TypeTy must be comparable to CompareTy
57 /// by ==
58 template <class TypeTy, class CompareTy>
59 int ConvertCostTableLookup(const TypeConversionCostTblEntry<TypeTy> *Tbl,
60                            unsigned len, int ISD, CompareTy Dst,
61                            CompareTy Src) {
62   for (unsigned int i = 0; i < len; ++i)
63     if (ISD == Tbl[i].ISD && Src == Tbl[i].Src && Dst == Tbl[i].Dst)
64       return i;
65
66   // Could not find an entry.
67   return -1;
68 }
69
70 /// Find in type conversion cost table, TypeTy must be comparable to CompareTy
71 /// by ==
72 template <class TypeTy, class CompareTy, unsigned N>
73 int ConvertCostTableLookup(const TypeConversionCostTblEntry<TypeTy>(&Tbl)[N],
74                            int ISD, CompareTy Dst, CompareTy Src) {
75   return ConvertCostTableLookup(Tbl, N, ISD, Dst, Src);
76 }
77
78 } // namespace llvm
79
80
81 #endif /* LLVM_TARGET_COSTTABLE_H_ */