CostTable.h 2.8 KB

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