1 //===-- CostTable.h - Instruction Cost Table handling -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// Cost tables and simple lookup functions
11 ///
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_COSTTABLE_H_
15 #define LLVM_CODEGEN_COSTTABLE_H_
16
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/CodeGenTypes/MachineValueType.h"
20
21 namespace llvm {
22
23 /// Cost Table Entry
24 template <typename CostType>
25 struct CostTblEntryT {
26 int ISD;
27 MVT::SimpleValueType Type;
28 CostType Cost;
29 };
30 using CostTblEntry = CostTblEntryT<unsigned>;
31
32 /// Find in cost table.
33 template <class CostType>
34 inline const CostTblEntryT<CostType> *
CostTableLookup(ArrayRef<CostTblEntryT<CostType>> Tbl,int ISD,MVT Ty)35 CostTableLookup(ArrayRef<CostTblEntryT<CostType>> Tbl, int ISD, MVT Ty) {
36 auto I = find_if(Tbl, [=](const CostTblEntryT<CostType> &Entry) {
37 return ISD == Entry.ISD && Ty == Entry.Type;
38 });
39 if (I != Tbl.end())
40 return I;
41
42 // Could not find an entry.
43 return nullptr;
44 }
45
46 template <size_t N, class CostType>
47 inline const CostTblEntryT<CostType> *
CostTableLookup(const CostTblEntryT<CostType> (& Table)[N],int ISD,MVT Ty)48 CostTableLookup(const CostTblEntryT<CostType> (&Table)[N], int ISD, MVT Ty) {
49 // Wrapper to fix template argument deduction failures.
50 return CostTableLookup<CostType>(Table, ISD, Ty);
51 }
52
53 /// Type Conversion Cost Table
54 template <typename CostType>
55 struct TypeConversionCostTblEntryT {
56 int ISD;
57 MVT::SimpleValueType Dst;
58 MVT::SimpleValueType Src;
59 CostType Cost;
60 };
61 using TypeConversionCostTblEntry = TypeConversionCostTblEntryT<unsigned>;
62
63 /// Find in type conversion cost table.
64 template <class CostType>
65 inline const TypeConversionCostTblEntryT<CostType> *
ConvertCostTableLookup(ArrayRef<TypeConversionCostTblEntryT<CostType>> Tbl,int ISD,MVT Dst,MVT Src)66 ConvertCostTableLookup(ArrayRef<TypeConversionCostTblEntryT<CostType>> Tbl,
67 int ISD, MVT Dst, MVT Src) {
68 auto I =
69 find_if(Tbl, [=](const TypeConversionCostTblEntryT<CostType> &Entry) {
70 return ISD == Entry.ISD && Src == Entry.Src && Dst == Entry.Dst;
71 });
72 if (I != Tbl.end())
73 return I;
74
75 // Could not find an entry.
76 return nullptr;
77 }
78
79 template <size_t N, class CostType>
80 inline const TypeConversionCostTblEntryT<CostType> *
ConvertCostTableLookup(const TypeConversionCostTblEntryT<CostType> (& Table)[N],int ISD,MVT Dst,MVT Src)81 ConvertCostTableLookup(const TypeConversionCostTblEntryT<CostType> (&Table)[N],
82 int ISD, MVT Dst, MVT Src) {
83 // Wrapper to fix template argument deduction failures.
84 return ConvertCostTableLookup<CostType>(Table, ISD, Dst, Src);
85 }
86
87 } // namespace llvm
88
89 #endif /* LLVM_CODEGEN_COSTTABLE_H_ */
90