1 //===--- InfoByHwMode.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 that implement data parameterized by HW modes for instruction
9 // selection. Currently it is ValueTypeByHwMode (parameterized ValueType),
10 // and RegSizeInfoByHwMode (parameterized register/spill size and alignment
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenTarget.h"
15 #include "InfoByHwMode.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/TableGen/Record.h"
25 std::string
llvm::getModeName(unsigned Mode
) {
26 if (Mode
== DefaultMode
)
28 return (Twine('m') + Twine(Mode
)).str();
31 ValueTypeByHwMode::ValueTypeByHwMode(Record
*R
, const CodeGenHwModes
&CGH
) {
32 const HwModeSelect
&MS
= CGH
.getHwModeSelect(R
);
33 for (const HwModeSelect::PairType
&P
: MS
.Items
) {
34 auto I
= Map
.insert({P
.first
, MVT(llvm::getValueType(P
.second
))});
35 assert(I
.second
&& "Duplicate entry?");
38 if (R
->isSubClassOf("PtrValueType"))
39 PtrAddrSpace
= R
->getValueAsInt("AddrSpace");
42 ValueTypeByHwMode::ValueTypeByHwMode(Record
*R
, MVT T
) : ValueTypeByHwMode(T
) {
43 if (R
->isSubClassOf("PtrValueType"))
44 PtrAddrSpace
= R
->getValueAsInt("AddrSpace");
47 bool ValueTypeByHwMode::operator== (const ValueTypeByHwMode
&T
) const {
48 assert(isValid() && T
.isValid() && "Invalid type in assignment");
49 bool Simple
= isSimple();
50 if (Simple
!= T
.isSimple())
53 return getSimple() == T
.getSimple();
58 bool ValueTypeByHwMode::operator< (const ValueTypeByHwMode
&T
) const {
59 assert(isValid() && T
.isValid() && "Invalid type in comparison");
60 // Default order for maps.
64 MVT
&ValueTypeByHwMode::getOrCreateTypeForMode(unsigned Mode
, MVT Type
) {
65 auto F
= Map
.find(Mode
);
68 // If Mode is not in the map, look up the default mode. If it exists,
69 // make a copy of it for Mode and return it.
71 if (D
!= Map
.end() && D
->first
== DefaultMode
)
72 return Map
.insert(std::make_pair(Mode
, D
->second
)).first
->second
;
73 // If default mode is not present either, use provided Type.
74 return Map
.insert(std::make_pair(Mode
, Type
)).first
->second
;
77 StringRef
ValueTypeByHwMode::getMVTName(MVT T
) {
78 StringRef N
= llvm::getEnumName(T
.SimpleTy
);
79 N
.consume_front("MVT::");
83 void ValueTypeByHwMode::writeToStream(raw_ostream
&OS
) const {
85 OS
<< getMVTName(getSimple());
89 std::vector
<const PairType
*> Pairs
;
90 for (const auto &P
: Map
)
92 llvm::sort(Pairs
, deref
<std::less
<PairType
>>());
95 ListSeparator
LS(",");
96 for (const PairType
*P
: Pairs
)
97 OS
<< LS
<< '(' << getModeName(P
->first
) << ':'
98 << getMVTName(P
->second
).str() << ')';
103 void ValueTypeByHwMode::dump() const {
104 dbgs() << *this << '\n';
107 ValueTypeByHwMode
llvm::getValueTypeByHwMode(Record
*Rec
,
108 const CodeGenHwModes
&CGH
) {
110 if (!Rec
->isSubClassOf("ValueType"))
113 assert(Rec
->isSubClassOf("ValueType") &&
114 "Record must be derived from ValueType");
115 if (Rec
->isSubClassOf("HwModeSelect"))
116 return ValueTypeByHwMode(Rec
, CGH
);
117 return ValueTypeByHwMode(Rec
, llvm::getValueType(Rec
));
120 RegSizeInfo::RegSizeInfo(Record
*R
, const CodeGenHwModes
&CGH
) {
121 RegSize
= R
->getValueAsInt("RegSize");
122 SpillSize
= R
->getValueAsInt("SpillSize");
123 SpillAlignment
= R
->getValueAsInt("SpillAlignment");
126 bool RegSizeInfo::operator< (const RegSizeInfo
&I
) const {
127 return std::tie(RegSize
, SpillSize
, SpillAlignment
) <
128 std::tie(I
.RegSize
, I
.SpillSize
, I
.SpillAlignment
);
131 bool RegSizeInfo::isSubClassOf(const RegSizeInfo
&I
) const {
132 return RegSize
<= I
.RegSize
&&
133 SpillAlignment
&& I
.SpillAlignment
% SpillAlignment
== 0 &&
134 SpillSize
<= I
.SpillSize
;
137 void RegSizeInfo::writeToStream(raw_ostream
&OS
) const {
138 OS
<< "[R=" << RegSize
<< ",S=" << SpillSize
139 << ",A=" << SpillAlignment
<< ']';
142 RegSizeInfoByHwMode::RegSizeInfoByHwMode(Record
*R
,
143 const CodeGenHwModes
&CGH
) {
144 const HwModeSelect
&MS
= CGH
.getHwModeSelect(R
);
145 for (const HwModeSelect::PairType
&P
: MS
.Items
) {
146 auto I
= Map
.insert({P
.first
, RegSizeInfo(P
.second
, CGH
)});
147 assert(I
.second
&& "Duplicate entry?");
152 bool RegSizeInfoByHwMode::operator< (const RegSizeInfoByHwMode
&I
) const {
153 unsigned M0
= Map
.begin()->first
;
154 return get(M0
) < I
.get(M0
);
157 bool RegSizeInfoByHwMode::operator== (const RegSizeInfoByHwMode
&I
) const {
158 unsigned M0
= Map
.begin()->first
;
159 return get(M0
) == I
.get(M0
);
162 bool RegSizeInfoByHwMode::isSubClassOf(const RegSizeInfoByHwMode
&I
) const {
163 unsigned M0
= Map
.begin()->first
;
164 return get(M0
).isSubClassOf(I
.get(M0
));
167 bool RegSizeInfoByHwMode::hasStricterSpillThan(const RegSizeInfoByHwMode
&I
)
169 unsigned M0
= Map
.begin()->first
;
170 const RegSizeInfo
&A0
= get(M0
);
171 const RegSizeInfo
&B0
= I
.get(M0
);
172 return std::tie(A0
.SpillSize
, A0
.SpillAlignment
) >
173 std::tie(B0
.SpillSize
, B0
.SpillAlignment
);
176 void RegSizeInfoByHwMode::writeToStream(raw_ostream
&OS
) const {
177 typedef typename
decltype(Map
)::value_type PairType
;
178 std::vector
<const PairType
*> Pairs
;
179 for (const auto &P
: Map
)
181 llvm::sort(Pairs
, deref
<std::less
<PairType
>>());
184 ListSeparator
LS(",");
185 for (const PairType
*P
: Pairs
)
186 OS
<< LS
<< '(' << getModeName(P
->first
) << ':' << P
->second
<< ')';
190 EncodingInfoByHwMode::EncodingInfoByHwMode(Record
*R
, const CodeGenHwModes
&CGH
) {
191 const HwModeSelect
&MS
= CGH
.getHwModeSelect(R
);
192 for (const HwModeSelect::PairType
&P
: MS
.Items
) {
193 assert(P
.second
&& P
.second
->isSubClassOf("InstructionEncoding") &&
194 "Encoding must subclass InstructionEncoding");
195 auto I
= Map
.insert({P
.first
, P
.second
});
196 assert(I
.second
&& "Duplicate entry?");
202 raw_ostream
&operator<<(raw_ostream
&OS
, const ValueTypeByHwMode
&T
) {
207 raw_ostream
&operator<<(raw_ostream
&OS
, const RegSizeInfo
&T
) {
212 raw_ostream
&operator<<(raw_ostream
&OS
, const RegSizeInfoByHwMode
&T
) {