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