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
17using namespace llvm;
18
19StringRef CodeGenHwModes::DefaultModeName = "DefaultMode";
20
21HwMode::HwMode(Record *R) {
22 Name = R->getName();
23 Features = std::string(R->getValueAsString(FieldName: "Features"));
24
25 std::vector<Record *> PredicateRecs = R->getValueAsListOfDefs(FieldName: "Predicates");
26 SmallString<128> PredicateCheck;
27 raw_svector_ostream OS(PredicateCheck);
28 ListSeparator LS(" && ");
29 for (Record *Pred : PredicateRecs) {
30 StringRef CondString = Pred->getValueAsString(FieldName: "CondString");
31 if (CondString.empty())
32 continue;
33 OS << LS << '(' << CondString << ')';
34 }
35
36 Predicates = std::string(PredicateCheck);
37}
38
39LLVM_DUMP_METHOD
40void HwMode::dump() const { dbgs() << Name << ": " << Features << '\n'; }
41
42HwModeSelect::HwModeSelect(Record *R, CodeGenHwModes &CGH) {
43 std::vector<Record *> Modes = R->getValueAsListOfDefs(FieldName: "Modes");
44 std::vector<Record *> Objects = R->getValueAsListOfDefs(FieldName: "Objects");
45 if (Modes.size() != Objects.size()) {
46 PrintError(
47 ErrorLoc: R->getLoc(),
48 Msg: "in record " + R->getName() +
49 " derived from HwModeSelect: the lists Modes and Objects should "
50 "have the same size");
51 report_fatal_error(reason: "error in target description.");
52 }
53 for (unsigned i = 0, e = Modes.size(); i != e; ++i) {
54 unsigned ModeId = CGH.getHwModeId(R: Modes[i]);
55 Items.push_back(x: std::pair(ModeId, Objects[i]));
56 }
57}
58
59LLVM_DUMP_METHOD
60void HwModeSelect::dump() const {
61 dbgs() << '{';
62 for (const PairType &P : Items)
63 dbgs() << " (" << P.first << ',' << P.second->getName() << ')';
64 dbgs() << " }\n";
65}
66
67CodeGenHwModes::CodeGenHwModes(RecordKeeper &RK) : Records(RK) {
68 for (Record *R : Records.getAllDerivedDefinitions(ClassName: "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)
72 continue;
73 Modes.emplace_back(args&: R);
74 ModeIds.insert(KV: std::pair(R, Modes.size()));
75 }
76
77 assert(Modes.size() <= 32 && "number of HwModes exceeds maximum of 32");
78
79 for (Record *R : Records.getAllDerivedDefinitions(ClassName: "HwModeSelect")) {
80 auto P = ModeSelects.emplace(args: std::pair(R, HwModeSelect(R, *this)));
81 assert(P.second);
82 (void)P;
83 }
84}
85
86unsigned CodeGenHwModes::getHwModeId(Record *R) const {
87 if (R->getName() == DefaultModeName)
88 return DefaultMode;
89 auto F = ModeIds.find(Val: R);
90 assert(F != ModeIds.end() && "Unknown mode name");
91 return F->second;
92}
93
94const HwModeSelect &CodeGenHwModes::getHwModeSelect(Record *R) const {
95 auto F = ModeSelects.find(x: R);
96 assert(F != ModeSelects.end() && "Record is not a \"mode select\"");
97 return F->second;
98}
99
100LLVM_DUMP_METHOD
101void CodeGenHwModes::dump() const {
102 dbgs() << "Modes: {\n";
103 for (const HwMode &M : Modes) {
104 dbgs() << " ";
105 M.dump();
106 }
107 dbgs() << "}\n";
108
109 dbgs() << "ModeIds: {\n";
110 for (const auto &P : ModeIds)
111 dbgs() << " " << P.first->getName() << " -> " << P.second << '\n';
112 dbgs() << "}\n";
113
114 dbgs() << "ModeSelects: {\n";
115 for (const auto &P : ModeSelects) {
116 dbgs() << " " << P.first->getName() << " -> ";
117 P.second.dump();
118 }
119 dbgs() << "}\n";
120}
121