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
21namespace llvm {
22
23/// Cost Table Entry
24template <typename CostType>
25struct CostTblEntryT {
26 int ISD;
27 MVT::SimpleValueType Type;
28 CostType Cost;
29};
30using CostTblEntry = CostTblEntryT<unsigned>;
31
32/// Find in cost table.
33template <class CostType>
34inline const CostTblEntryT<CostType> *
35CostTableLookup(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
46template <size_t N, class CostType>
47inline const CostTblEntryT<CostType> *
48CostTableLookup(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
54template <typename CostType>
55struct TypeConversionCostTblEntryT {
56 int ISD;
57 MVT::SimpleValueType Dst;
58 MVT::SimpleValueType Src;
59 CostType Cost;
60};
61using TypeConversionCostTblEntry = TypeConversionCostTblEntryT<unsigned>;
62
63/// Find in type conversion cost table.
64template <class CostType>
65inline const TypeConversionCostTblEntryT<CostType> *
66ConvertCostTableLookup(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
79template <size_t N, class CostType>
80inline const TypeConversionCostTblEntryT<CostType> *
81ConvertCostTableLookup(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

source code of llvm/include/llvm/CodeGen/CostTable.h