489435fb1c04c2f415dc77aa45c186273402330c
[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 #include "llvm/ADT/ArrayRef.h"
19
20 namespace llvm {
21
22 /// Cost Table Entry
23 template <class TypeTy>
24 struct CostTblEntry {
25   int ISD;
26   TypeTy Type;
27   unsigned Cost;
28 };
29
30 /// Find in cost table, TypeTy must be comparable to CompareTy by ==
31 template <class TypeTy, class CompareTy>
32 const CostTblEntry<TypeTy> *CostTableLookup(ArrayRef<CostTblEntry<TypeTy>> Tbl,
33                                             int ISD, CompareTy Ty) {
34   auto I = std::find_if(Tbl.begin(), Tbl.end(),
35                         [=](const CostTblEntry<TypeTy> &Entry) {
36                           return ISD == Entry.ISD && Ty == Entry.Type; });
37   if (I != Tbl.end())
38     return I;
39
40   // Could not find an entry.
41   return nullptr;
42 }
43
44 /// Find in cost table, TypeTy must be comparable to CompareTy by ==
45 template <class TypeTy, class CompareTy, unsigned N>
46 const CostTblEntry<TypeTy> *CostTableLookup(const CostTblEntry<TypeTy>(&Tbl)[N],
47                                             int ISD, CompareTy Ty) {
48   return CostTableLookup(makeArrayRef(Tbl), ISD, Ty);
49 }
50
51 /// Type Conversion Cost Table
52 template <class TypeTy>
53 struct TypeConversionCostTblEntry {
54   int ISD;
55   TypeTy Dst;
56   TypeTy Src;
57   unsigned Cost;
58 };
59
60 /// Find in type conversion cost table, TypeTy must be comparable to CompareTy
61 /// by ==
62 template <class TypeTy, class CompareTy>
63 const TypeConversionCostTblEntry<TypeTy> *
64 ConvertCostTableLookup(ArrayRef<TypeConversionCostTblEntry<TypeTy>> Tbl,
65                        int ISD, CompareTy Dst, CompareTy Src) {
66   auto I = std::find_if(Tbl.begin(), Tbl.end(),
67                         [=](const TypeConversionCostTblEntry<TypeTy> &Entry) {
68                           return ISD == Entry.ISD && Src == Entry.Src &&
69                                  Dst == Entry.Dst;
70                         });
71   if (I != Tbl.end())
72     return I;
73
74   // Could not find an entry.
75   return nullptr;
76 }
77
78 /// Find in type conversion cost table, TypeTy must be comparable to CompareTy
79 /// by ==
80 template <class TypeTy, class CompareTy, unsigned N>
81 const TypeConversionCostTblEntry<TypeTy> *
82 ConvertCostTableLookup(const TypeConversionCostTblEntry<TypeTy>(&Tbl)[N],
83                        int ISD, CompareTy Dst, CompareTy Src) {
84   return ConvertCostTableLookup(makeArrayRef(Tbl), ISD, Dst, Src);
85 }
86
87 } // namespace llvm
88
89
90 #endif /* LLVM_TARGET_COSTTABLE_H_ */