1 //===--- CodeGenHwModes.cpp -----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
8 // Classes to parse and store HW mode information for instruction selection
9 //===----------------------------------------------------------------------===//
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"
19 StringRef
CodeGenHwModes::DefaultModeName
= "DefaultMode";
21 HwMode::HwMode(Record
*R
) {
23 Features
= R
->getValueAsString("Features");
27 void HwMode::dump() const {
28 dbgs() << Name
<< ": " << Features
<< '\n';
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.");
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
]));
47 void HwModeSelect::dump() const {
49 for (const PairType
&P
: Items
)
50 dbgs() << " (" << P
.first
<< ',' << P
.second
->getName() << ')';
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
)
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
));
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)));
79 unsigned CodeGenHwModes::getHwModeId(StringRef Name
) const {
80 if (Name
== DefaultModeName
)
82 auto F
= ModeIds
.find(Name
);
83 assert(F
!= ModeIds
.end() && "Unknown mode name");
87 const HwModeSelect
&CodeGenHwModes::getHwModeSelect(Record
*R
) const {
88 auto F
= ModeSelects
.find(R
);
89 assert(F
!= ModeSelects
.end() && "Record is not a \"mode select\"");
94 void CodeGenHwModes::dump() const {
95 dbgs() << "Modes: {\n";
96 for (const HwMode
&M
: Modes
) {
102 dbgs() << "ModeIds: {\n";
103 for (const auto &P
: ModeIds
)
104 dbgs() << " " << P
.first() << " -> " << P
.second
<< '\n';
107 dbgs() << "ModeSelects: {\n";
108 for (const auto &P
: ModeSelects
) {
109 dbgs() << " " << P
.first
->getName() << " -> ";