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