blob: ce1b78a8f17d94ab1cb11159eb1f0b22bee558f9 [file] [log] [blame]
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001//===--- InfoByHwMode.cpp -------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00006//
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
11// data).
12//===----------------------------------------------------------------------===//
13
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
21#include <set>
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000022#include <string>
23
24using namespace llvm;
25
26std::string llvm::getModeName(unsigned Mode) {
27 if (Mode == DefaultMode)
28 return "*";
29 return (Twine('m') + Twine(Mode)).str();
30}
31
32ValueTypeByHwMode::ValueTypeByHwMode(Record *R, const CodeGenHwModes &CGH) {
33 const HwModeSelect &MS = CGH.getHwModeSelect(R);
34 for (const HwModeSelect::PairType &P : MS.Items) {
35 auto I = Map.insert({P.first, MVT(llvm::getValueType(P.second))});
36 assert(I.second && "Duplicate entry?");
37 (void)I;
38 }
39}
40
41bool ValueTypeByHwMode::operator== (const ValueTypeByHwMode &T) const {
42 assert(isValid() && T.isValid() && "Invalid type in assignment");
43 bool Simple = isSimple();
44 if (Simple != T.isSimple())
45 return false;
46 if (Simple)
47 return getSimple() == T.getSimple();
48
49 return Map == T.Map;
50}
51
52bool ValueTypeByHwMode::operator< (const ValueTypeByHwMode &T) const {
53 assert(isValid() && T.isValid() && "Invalid type in comparison");
54 // Default order for maps.
55 return Map < T.Map;
56}
57
58MVT &ValueTypeByHwMode::getOrCreateTypeForMode(unsigned Mode, MVT Type) {
59 auto F = Map.find(Mode);
60 if (F != Map.end())
61 return F->second;
62 // If Mode is not in the map, look up the default mode. If it exists,
63 // make a copy of it for Mode and return it.
64 auto D = Map.find(DefaultMode);
65 if (D != Map.end())
66 return Map.insert(std::make_pair(Mode, D->second)).first->second;
67 // If default mode is not present either, use provided Type.
68 return Map.insert(std::make_pair(Mode, Type)).first->second;
69}
70
Simon Pilgrim6f05a742017-09-22 13:32:26 +000071StringRef ValueTypeByHwMode::getMVTName(MVT T) {
72 StringRef N = llvm::getEnumName(T.SimpleTy);
73 N.consume_front("MVT::");
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000074 return N;
75}
76
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +000077void ValueTypeByHwMode::writeToStream(raw_ostream &OS) const {
78 if (isSimple()) {
79 OS << getMVTName(getSimple());
80 return;
81 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000082
83 std::vector<const PairType*> Pairs;
84 for (const auto &P : Map)
85 Pairs.push_back(&P);
Fangrui Song0cac7262018-09-27 02:13:45 +000086 llvm::sort(Pairs, deref<std::less<PairType>>());
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000087
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +000088 OS << '{';
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000089 for (unsigned i = 0, e = Pairs.size(); i != e; ++i) {
90 const PairType *P = Pairs[i];
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +000091 OS << '(' << getModeName(P->first)
92 << ':' << getMVTName(P->second).str() << ')';
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000093 if (i != e-1)
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +000094 OS << ',';
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000095 }
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +000096 OS << '}';
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000097}
98
99LLVM_DUMP_METHOD
100void ValueTypeByHwMode::dump() const {
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000101 dbgs() << *this << '\n';
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000102}
103
104ValueTypeByHwMode llvm::getValueTypeByHwMode(Record *Rec,
105 const CodeGenHwModes &CGH) {
106#ifndef NDEBUG
107 if (!Rec->isSubClassOf("ValueType"))
108 Rec->dump();
109#endif
110 assert(Rec->isSubClassOf("ValueType") &&
111 "Record must be derived from ValueType");
112 if (Rec->isSubClassOf("HwModeSelect"))
113 return ValueTypeByHwMode(Rec, CGH);
114 return ValueTypeByHwMode(llvm::getValueType(Rec));
115}
116
117RegSizeInfo::RegSizeInfo(Record *R, const CodeGenHwModes &CGH) {
118 RegSize = R->getValueAsInt("RegSize");
119 SpillSize = R->getValueAsInt("SpillSize");
120 SpillAlignment = R->getValueAsInt("SpillAlignment");
121}
122
123bool RegSizeInfo::operator< (const RegSizeInfo &I) const {
124 return std::tie(RegSize, SpillSize, SpillAlignment) <
125 std::tie(I.RegSize, I.SpillSize, I.SpillAlignment);
126}
127
128bool RegSizeInfo::isSubClassOf(const RegSizeInfo &I) const {
129 return RegSize <= I.RegSize &&
130 SpillAlignment && I.SpillAlignment % SpillAlignment == 0 &&
131 SpillSize <= I.SpillSize;
132}
133
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000134void RegSizeInfo::writeToStream(raw_ostream &OS) const {
135 OS << "[R=" << RegSize << ",S=" << SpillSize
136 << ",A=" << SpillAlignment << ']';
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000137}
138
139RegSizeInfoByHwMode::RegSizeInfoByHwMode(Record *R,
140 const CodeGenHwModes &CGH) {
141 const HwModeSelect &MS = CGH.getHwModeSelect(R);
142 for (const HwModeSelect::PairType &P : MS.Items) {
143 auto I = Map.insert({P.first, RegSizeInfo(P.second, CGH)});
144 assert(I.second && "Duplicate entry?");
145 (void)I;
146 }
147}
148
149bool RegSizeInfoByHwMode::operator< (const RegSizeInfoByHwMode &I) const {
150 unsigned M0 = Map.begin()->first;
151 return get(M0) < I.get(M0);
152}
153
154bool RegSizeInfoByHwMode::operator== (const RegSizeInfoByHwMode &I) const {
155 unsigned M0 = Map.begin()->first;
156 return get(M0) == I.get(M0);
157}
158
159bool RegSizeInfoByHwMode::isSubClassOf(const RegSizeInfoByHwMode &I) const {
160 unsigned M0 = Map.begin()->first;
161 return get(M0).isSubClassOf(I.get(M0));
162}
163
164bool RegSizeInfoByHwMode::hasStricterSpillThan(const RegSizeInfoByHwMode &I)
165 const {
166 unsigned M0 = Map.begin()->first;
167 const RegSizeInfo &A0 = get(M0);
168 const RegSizeInfo &B0 = I.get(M0);
169 return std::tie(A0.SpillSize, A0.SpillAlignment) >
170 std::tie(B0.SpillSize, B0.SpillAlignment);
171}
172
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000173void RegSizeInfoByHwMode::writeToStream(raw_ostream &OS) const {
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000174 typedef typename decltype(Map)::value_type PairType;
175 std::vector<const PairType*> Pairs;
176 for (const auto &P : Map)
177 Pairs.push_back(&P);
Fangrui Song0cac7262018-09-27 02:13:45 +0000178 llvm::sort(Pairs, deref<std::less<PairType>>());
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000179
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000180 OS << '{';
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000181 for (unsigned i = 0, e = Pairs.size(); i != e; ++i) {
182 const PairType *P = Pairs[i];
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000183 OS << '(' << getModeName(P->first) << ':' << P->second << ')';
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000184 if (i != e-1)
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000185 OS << ',';
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000186 }
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000187 OS << '}';
188}
189
190namespace llvm {
191 raw_ostream &operator<<(raw_ostream &OS, const ValueTypeByHwMode &T) {
192 T.writeToStream(OS);
193 return OS;
194 }
195
196 raw_ostream &operator<<(raw_ostream &OS, const RegSizeInfo &T) {
197 T.writeToStream(OS);
198 return OS;
199 }
200
201 raw_ostream &operator<<(raw_ostream &OS, const RegSizeInfoByHwMode &T) {
202 T.writeToStream(OS);
203 return OS;
204 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000205}