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
= std::string(R
->getValueAsString("Features"));
25 std::vector
<Record
*> PredicateRecs
= R
->getValueAsListOfDefs("Predicates");
26 SmallString
<128> PredicateCheck
;
27 raw_svector_ostream
OS(PredicateCheck
);
28 ListSeparator
LS(" && ");
29 for (Record
*Pred
: PredicateRecs
) {
30 StringRef CondString
= Pred
->getValueAsString("CondString");
31 if (CondString
.empty())
33 OS
<< LS
<< '(' << CondString
<< ')';
36 Predicates
= std::string(PredicateCheck
);
40 void HwMode::dump() const {
41 dbgs() << Name
<< ": " << Features
<< '\n';
44 HwModeSelect::HwModeSelect(Record
*R
, CodeGenHwModes
&CGH
) {
45 std::vector
<Record
*> Modes
= R
->getValueAsListOfDefs("Modes");
46 std::vector
<Record
*> Objects
= R
->getValueAsListOfDefs("Objects");
47 if (Modes
.size() != Objects
.size()) {
48 PrintError(R
->getLoc(), "in record " + R
->getName() +
49 " derived from HwModeSelect: the lists Modes and Objects should "
50 "have the same size");
51 report_fatal_error("error in target description.");
53 for (unsigned i
= 0, e
= Modes
.size(); i
!= e
; ++i
) {
54 unsigned ModeId
= CGH
.getHwModeId(Modes
[i
]);
55 Items
.push_back(std::make_pair(ModeId
, Objects
[i
]));
60 void HwModeSelect::dump() const {
62 for (const PairType
&P
: Items
)
63 dbgs() << " (" << P
.first
<< ',' << P
.second
->getName() << ')';
67 CodeGenHwModes::CodeGenHwModes(RecordKeeper
&RK
) : Records(RK
) {
68 for (Record
*R
: Records
.getAllDerivedDefinitions("HwMode")) {
69 // The default mode needs a definition in the .td sources for TableGen
70 // to accept references to it. We need to ignore the definition here.
71 if (R
->getName() == DefaultModeName
)
73 Modes
.emplace_back(R
);
74 ModeIds
.insert(std::make_pair(R
, Modes
.size()));
77 for (Record
*R
: Records
.getAllDerivedDefinitions("HwModeSelect")) {
78 auto P
= ModeSelects
.emplace(std::make_pair(R
, HwModeSelect(R
, *this)));
84 unsigned CodeGenHwModes::getHwModeId(Record
*R
) const {
85 if (R
->getName() == DefaultModeName
)
87 auto F
= ModeIds
.find(R
);
88 assert(F
!= ModeIds
.end() && "Unknown mode name");
92 const HwModeSelect
&CodeGenHwModes::getHwModeSelect(Record
*R
) const {
93 auto F
= ModeSelects
.find(R
);
94 assert(F
!= ModeSelects
.end() && "Record is not a \"mode select\"");
99 void CodeGenHwModes::dump() const {
100 dbgs() << "Modes: {\n";
101 for (const HwMode
&M
: Modes
) {
107 dbgs() << "ModeIds: {\n";
108 for (const auto &P
: ModeIds
)
109 dbgs() << " " << P
.first
->getName() << " -> " << P
.second
<< '\n';
112 dbgs() << "ModeSelects: {\n";
113 for (const auto &P
: ModeSelects
) {
114 dbgs() << " " << P
.first
->getName() << " -> ";