CodeGenHwModes.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //===--- CodeGenHwModes.cpp -----------------------------------------------===//
  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. // Classes to parse and store HW mode information for instruction selection
  9. //===----------------------------------------------------------------------===//
  10. #include "CodeGenHwModes.h"
  11. #include "llvm/Support/Debug.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. #include "llvm/TableGen/Error.h"
  14. #include "llvm/TableGen/Record.h"
  15. using namespace llvm;
  16. StringRef CodeGenHwModes::DefaultModeName = "DefaultMode";
  17. HwMode::HwMode(Record *R) {
  18. Name = R->getName();
  19. Features = std::string(R->getValueAsString("Features"));
  20. }
  21. LLVM_DUMP_METHOD
  22. void HwMode::dump() const {
  23. dbgs() << Name << ": " << Features << '\n';
  24. }
  25. HwModeSelect::HwModeSelect(Record *R, CodeGenHwModes &CGH) {
  26. std::vector<Record*> Modes = R->getValueAsListOfDefs("Modes");
  27. std::vector<Record*> Objects = R->getValueAsListOfDefs("Objects");
  28. if (Modes.size() != Objects.size()) {
  29. PrintError(R->getLoc(), "in record " + R->getName() +
  30. " derived from HwModeSelect: the lists Modes and Objects should "
  31. "have the same size");
  32. report_fatal_error("error in target description.");
  33. }
  34. for (unsigned i = 0, e = Modes.size(); i != e; ++i) {
  35. unsigned ModeId = CGH.getHwModeId(Modes[i]->getName());
  36. Items.push_back(std::make_pair(ModeId, Objects[i]));
  37. }
  38. }
  39. LLVM_DUMP_METHOD
  40. void HwModeSelect::dump() const {
  41. dbgs() << '{';
  42. for (const PairType &P : Items)
  43. dbgs() << " (" << P.first << ',' << P.second->getName() << ')';
  44. dbgs() << " }\n";
  45. }
  46. CodeGenHwModes::CodeGenHwModes(RecordKeeper &RK) : Records(RK) {
  47. std::vector<Record*> MRs = Records.getAllDerivedDefinitions("HwMode");
  48. // The default mode needs a definition in the .td sources for TableGen
  49. // to accept references to it. We need to ignore the definition here.
  50. for (auto I = MRs.begin(), E = MRs.end(); I != E; ++I) {
  51. if ((*I)->getName() != DefaultModeName)
  52. continue;
  53. MRs.erase(I);
  54. break;
  55. }
  56. for (Record *R : MRs) {
  57. Modes.emplace_back(R);
  58. unsigned NewId = Modes.size();
  59. ModeIds.insert(std::make_pair(Modes[NewId-1].Name, NewId));
  60. }
  61. std::vector<Record*> MSs = Records.getAllDerivedDefinitions("HwModeSelect");
  62. for (Record *R : MSs) {
  63. auto P = ModeSelects.emplace(std::make_pair(R, HwModeSelect(R, *this)));
  64. assert(P.second);
  65. (void)P;
  66. }
  67. }
  68. unsigned CodeGenHwModes::getHwModeId(StringRef Name) const {
  69. if (Name == DefaultModeName)
  70. return DefaultMode;
  71. auto F = ModeIds.find(Name);
  72. assert(F != ModeIds.end() && "Unknown mode name");
  73. return F->second;
  74. }
  75. const HwModeSelect &CodeGenHwModes::getHwModeSelect(Record *R) const {
  76. auto F = ModeSelects.find(R);
  77. assert(F != ModeSelects.end() && "Record is not a \"mode select\"");
  78. return F->second;
  79. }
  80. LLVM_DUMP_METHOD
  81. void CodeGenHwModes::dump() const {
  82. dbgs() << "Modes: {\n";
  83. for (const HwMode &M : Modes) {
  84. dbgs() << " ";
  85. M.dump();
  86. }
  87. dbgs() << "}\n";
  88. dbgs() << "ModeIds: {\n";
  89. for (const auto &P : ModeIds)
  90. dbgs() << " " << P.first() << " -> " << P.second << '\n';
  91. dbgs() << "}\n";
  92. dbgs() << "ModeSelects: {\n";
  93. for (const auto &P : ModeSelects) {
  94. dbgs() << " " << P.first->getName() << " -> ";
  95. P.second.dump();
  96. }
  97. dbgs() << "}\n";
  98. }