blob: 7e1e1864356c8cfcd9691d6154c8d4811d718e7f [file] [log] [blame]
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001//===--- InfoByHwMode.cpp -------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// Classes that implement data parameterized by HW modes for instruction
10// selection. Currently it is ValueTypeByHwMode (parameterized ValueType),
11// and RegSizeInfoByHwMode (parameterized register/spill size and alignment
12// data).
13//===----------------------------------------------------------------------===//
14
15#include "CodeGenTarget.h"
16#include "InfoByHwMode.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/Support/Debug.h"
20#include "llvm/Support/raw_ostream.h"
21
22#include <set>
23#include <sstream>
24#include <string>
25
26using namespace llvm;
27
28std::string llvm::getModeName(unsigned Mode) {
29 if (Mode == DefaultMode)
30 return "*";
31 return (Twine('m') + Twine(Mode)).str();
32}
33
34ValueTypeByHwMode::ValueTypeByHwMode(Record *R, const CodeGenHwModes &CGH) {
35 const HwModeSelect &MS = CGH.getHwModeSelect(R);
36 for (const HwModeSelect::PairType &P : MS.Items) {
37 auto I = Map.insert({P.first, MVT(llvm::getValueType(P.second))});
38 assert(I.second && "Duplicate entry?");
39 (void)I;
40 }
41}
42
43bool ValueTypeByHwMode::operator== (const ValueTypeByHwMode &T) const {
44 assert(isValid() && T.isValid() && "Invalid type in assignment");
45 bool Simple = isSimple();
46 if (Simple != T.isSimple())
47 return false;
48 if (Simple)
49 return getSimple() == T.getSimple();
50
51 return Map == T.Map;
52}
53
54bool ValueTypeByHwMode::operator< (const ValueTypeByHwMode &T) const {
55 assert(isValid() && T.isValid() && "Invalid type in comparison");
56 // Default order for maps.
57 return Map < T.Map;
58}
59
60MVT &ValueTypeByHwMode::getOrCreateTypeForMode(unsigned Mode, MVT Type) {
61 auto F = Map.find(Mode);
62 if (F != Map.end())
63 return F->second;
64 // If Mode is not in the map, look up the default mode. If it exists,
65 // make a copy of it for Mode and return it.
66 auto D = Map.find(DefaultMode);
67 if (D != Map.end())
68 return Map.insert(std::make_pair(Mode, D->second)).first->second;
69 // If default mode is not present either, use provided Type.
70 return Map.insert(std::make_pair(Mode, Type)).first->second;
71}
72
Simon Pilgrim6f05a742017-09-22 13:32:26 +000073StringRef ValueTypeByHwMode::getMVTName(MVT T) {
74 StringRef N = llvm::getEnumName(T.SimpleTy);
75 N.consume_front("MVT::");
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000076 return N;
77}
78
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +000079void ValueTypeByHwMode::writeToStream(raw_ostream &OS) const {
80 if (isSimple()) {
81 OS << getMVTName(getSimple());
82 return;
83 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000084
85 std::vector<const PairType*> Pairs;
86 for (const auto &P : Map)
87 Pairs.push_back(&P);
88 std::sort(Pairs.begin(), Pairs.end(), deref<std::less<PairType>>());
89
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +000090 OS << '{';
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000091 for (unsigned i = 0, e = Pairs.size(); i != e; ++i) {
92 const PairType *P = Pairs[i];
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +000093 OS << '(' << getModeName(P->first)
94 << ':' << getMVTName(P->second).str() << ')';
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000095 if (i != e-1)
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +000096 OS << ',';
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000097 }
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +000098 OS << '}';
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000099}
100
101LLVM_DUMP_METHOD
102void ValueTypeByHwMode::dump() const {
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000103 dbgs() << *this << '\n';
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000104}
105
106ValueTypeByHwMode llvm::getValueTypeByHwMode(Record *Rec,
107 const CodeGenHwModes &CGH) {
108#ifndef NDEBUG
109 if (!Rec->isSubClassOf("ValueType"))
110 Rec->dump();
111#endif
112 assert(Rec->isSubClassOf("ValueType") &&
113 "Record must be derived from ValueType");
114 if (Rec->isSubClassOf("HwModeSelect"))
115 return ValueTypeByHwMode(Rec, CGH);
116 return ValueTypeByHwMode(llvm::getValueType(Rec));
117}
118
119RegSizeInfo::RegSizeInfo(Record *R, const CodeGenHwModes &CGH) {
120 RegSize = R->getValueAsInt("RegSize");
121 SpillSize = R->getValueAsInt("SpillSize");
122 SpillAlignment = R->getValueAsInt("SpillAlignment");
123}
124
125bool RegSizeInfo::operator< (const RegSizeInfo &I) const {
126 return std::tie(RegSize, SpillSize, SpillAlignment) <
127 std::tie(I.RegSize, I.SpillSize, I.SpillAlignment);
128}
129
130bool RegSizeInfo::isSubClassOf(const RegSizeInfo &I) const {
131 return RegSize <= I.RegSize &&
132 SpillAlignment && I.SpillAlignment % SpillAlignment == 0 &&
133 SpillSize <= I.SpillSize;
134}
135
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000136void RegSizeInfo::writeToStream(raw_ostream &OS) const {
137 OS << "[R=" << RegSize << ",S=" << SpillSize
138 << ",A=" << SpillAlignment << ']';
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000139}
140
141RegSizeInfoByHwMode::RegSizeInfoByHwMode(Record *R,
142 const CodeGenHwModes &CGH) {
143 const HwModeSelect &MS = CGH.getHwModeSelect(R);
144 for (const HwModeSelect::PairType &P : MS.Items) {
145 auto I = Map.insert({P.first, RegSizeInfo(P.second, CGH)});
146 assert(I.second && "Duplicate entry?");
147 (void)I;
148 }
149}
150
151bool RegSizeInfoByHwMode::operator< (const RegSizeInfoByHwMode &I) const {
152 unsigned M0 = Map.begin()->first;
153 return get(M0) < I.get(M0);
154}
155
156bool RegSizeInfoByHwMode::operator== (const RegSizeInfoByHwMode &I) const {
157 unsigned M0 = Map.begin()->first;
158 return get(M0) == I.get(M0);
159}
160
161bool RegSizeInfoByHwMode::isSubClassOf(const RegSizeInfoByHwMode &I) const {
162 unsigned M0 = Map.begin()->first;
163 return get(M0).isSubClassOf(I.get(M0));
164}
165
166bool RegSizeInfoByHwMode::hasStricterSpillThan(const RegSizeInfoByHwMode &I)
167 const {
168 unsigned M0 = Map.begin()->first;
169 const RegSizeInfo &A0 = get(M0);
170 const RegSizeInfo &B0 = I.get(M0);
171 return std::tie(A0.SpillSize, A0.SpillAlignment) >
172 std::tie(B0.SpillSize, B0.SpillAlignment);
173}
174
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000175void RegSizeInfoByHwMode::writeToStream(raw_ostream &OS) const {
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000176 typedef typename decltype(Map)::value_type PairType;
177 std::vector<const PairType*> Pairs;
178 for (const auto &P : Map)
179 Pairs.push_back(&P);
180 std::sort(Pairs.begin(), Pairs.end(), deref<std::less<PairType>>());
181
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000182 OS << '{';
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000183 for (unsigned i = 0, e = Pairs.size(); i != e; ++i) {
184 const PairType *P = Pairs[i];
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000185 OS << '(' << getModeName(P->first) << ':' << P->second << ')';
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000186 if (i != e-1)
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000187 OS << ',';
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000188 }
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000189 OS << '}';
190}
191
192namespace llvm {
193 raw_ostream &operator<<(raw_ostream &OS, const ValueTypeByHwMode &T) {
194 T.writeToStream(OS);
195 return OS;
196 }
197
198 raw_ostream &operator<<(raw_ostream &OS, const RegSizeInfo &T) {
199 T.writeToStream(OS);
200 return OS;
201 }
202
203 raw_ostream &operator<<(raw_ostream &OS, const RegSizeInfoByHwMode &T) {
204 T.writeToStream(OS);
205 return OS;
206 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000207}