| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#ifndef LLVM_CODEGEN_GLOBALISEL_LEGACYLEGALIZERINFO_H |
| 16 |
#define LLVM_CODEGEN_GLOBALISEL_LEGACYLEGALIZERINFO_H |
| 17 |
|
| 18 |
#include "llvm/ADT/DenseMap.h" |
| 19 |
#include "llvm/CodeGen/TargetOpcodes.h" |
| 20 |
#include "llvm/Support/LowLevelTypeImpl.h" |
| 21 |
#include <unordered_map> |
| 22 |
|
| 23 |
namespace llvm { |
| 24 |
struct LegalityQuery; |
| 25 |
|
| 26 |
namespace LegacyLegalizeActions { |
| 27 |
enum LegacyLegalizeAction : std::uint8_t { |
| 28 |
|
| 29 |
|
| 30 |
Legal, |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
NarrowScalar, |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
WidenScalar, |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
FewerElements, |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
MoreElements, |
| 52 |
|
| 53 |
|
| 54 |
Bitcast, |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
Lower, |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
Libcall, |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
Custom, |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
Unsupported, |
| 72 |
|
| 73 |
|
| 74 |
NotFound, |
| 75 |
}; |
| 76 |
} |
| 77 |
raw_ostream &operator<<(raw_ostream &OS, |
| 78 |
LegacyLegalizeActions::LegacyLegalizeAction Action); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
struct InstrAspect { |
| 84 |
unsigned Opcode; |
| 85 |
unsigned Idx = 0; |
| 86 |
LLT Type; |
| 87 |
|
| 88 |
InstrAspect(unsigned Opcode, LLT Type) : Opcode(Opcode), Type(Type) {} |
| 89 |
InstrAspect(unsigned Opcode, unsigned Idx, LLT Type) |
| 90 |
: Opcode(Opcode), Idx(Idx), Type(Type) {} |
| 91 |
|
| 92 |
bool operator==(const InstrAspect &RHS) const { |
| 93 |
return Opcode == RHS.Opcode && Idx == RHS.Idx && Type == RHS.Type; |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
struct LegacyLegalizeActionStep { |
| 101 |
|
| 102 |
LegacyLegalizeActions::LegacyLegalizeAction Action; |
| 103 |
|
| 104 |
unsigned TypeIdx; |
| 105 |
|
| 106 |
LLT NewType; |
| 107 |
|
| 108 |
LegacyLegalizeActionStep(LegacyLegalizeActions::LegacyLegalizeAction Action, |
| 109 |
unsigned TypeIdx, const LLT NewType) |
| 110 |
: Action(Action), TypeIdx(TypeIdx), NewType(NewType) {} |
| 111 |
|
| 112 |
bool operator==(const LegacyLegalizeActionStep &RHS) const { |
| 113 |
return std::tie(Action, TypeIdx, NewType) == |
| 114 |
std::tie(RHS.Action, RHS.TypeIdx, RHS.NewType); |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
class LegacyLegalizerInfo { |
| 120 |
public: |
| 121 |
using SizeAndAction = |
| 122 |
std::pair<uint16_t, LegacyLegalizeActions::LegacyLegalizeAction>; |
| 123 |
using SizeAndActionsVec = std::vector<SizeAndAction>; |
| 124 |
using SizeChangeStrategy = |
| 125 |
std::function<SizeAndActionsVec(const SizeAndActionsVec &v)>; |
| 126 |
|
| 127 |
LegacyLegalizerInfo(); |
| 128 |
|
| 129 |
static bool needsLegalizingToDifferentSize( |
| 130 |
const LegacyLegalizeActions::LegacyLegalizeAction Action) { |
| 131 |
using namespace LegacyLegalizeActions; |
| 132 |
switch (Action) { |
| 133 |
case NarrowScalar: |
| 134 |
case WidenScalar: |
| 135 |
case FewerElements: |
| 136 |
case MoreElements: |
| 137 |
case Unsupported: |
| 138 |
return true; |
| 139 |
default: |
| 140 |
return false; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
void computeTables(); |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
void setAction(const InstrAspect &Aspect, |
| 154 |
LegacyLegalizeActions::LegacyLegalizeAction Action) { |
| 155 |
assert(!needsLegalizingToDifferentSize(Action)); |
| 156 |
TablesInitialized = false; |
| 157 |
const unsigned OpcodeIdx = Aspect.Opcode - FirstOp; |
| 158 |
if (SpecifiedActions[OpcodeIdx].size() <= Aspect.Idx) |
| 159 |
SpecifiedActions[OpcodeIdx].resize(Aspect.Idx + 1); |
| 160 |
SpecifiedActions[OpcodeIdx][Aspect.Idx][Aspect.Type] = Action; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
void setLegalizeScalarToDifferentSizeStrategy(const unsigned Opcode, |
| 180 |
const unsigned TypeIdx, |
| 181 |
SizeChangeStrategy S) { |
| 182 |
const unsigned OpcodeIdx = Opcode - FirstOp; |
| 183 |
if (ScalarSizeChangeStrategies[OpcodeIdx].size() <= TypeIdx) |
| 184 |
ScalarSizeChangeStrategies[OpcodeIdx].resize(TypeIdx + 1); |
| 185 |
ScalarSizeChangeStrategies[OpcodeIdx][TypeIdx] = S; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
void setLegalizeVectorElementToDifferentSizeStrategy(const unsigned Opcode, |
| 191 |
const unsigned TypeIdx, |
| 192 |
SizeChangeStrategy S) { |
| 193 |
const unsigned OpcodeIdx = Opcode - FirstOp; |
| 194 |
if (VectorElementSizeChangeStrategies[OpcodeIdx].size() <= TypeIdx) |
| 195 |
VectorElementSizeChangeStrategies[OpcodeIdx].resize(TypeIdx + 1); |
| 196 |
VectorElementSizeChangeStrategies[OpcodeIdx][TypeIdx] = S; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
static SizeAndActionsVec |
| 209 |
unsupportedForDifferentSizes(const SizeAndActionsVec &v) { |
| 210 |
using namespace LegacyLegalizeActions; |
| 211 |
return increaseToLargerTypesAndDecreaseToLargest(v, Unsupported, |
| 212 |
Unsupported); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
static SizeAndActionsVec |
| 220 |
widenToLargerTypesAndNarrowToLargest(const SizeAndActionsVec &v) { |
| 221 |
using namespace LegacyLegalizeActions; |
| 222 |
assert(v.size() > 0 && |
| 223 |
"At least one size that can be legalized towards is needed" |
| 224 |
" for this SizeChangeStrategy"); |
| 225 |
return increaseToLargerTypesAndDecreaseToLargest(v, WidenScalar, |
| 226 |
NarrowScalar); |
| 227 |
} |
| 228 |
|
| 229 |
static SizeAndActionsVec |
| 230 |
widenToLargerTypesUnsupportedOtherwise(const SizeAndActionsVec &v) { |
| 231 |
using namespace LegacyLegalizeActions; |
| 232 |
return increaseToLargerTypesAndDecreaseToLargest(v, WidenScalar, |
| 233 |
Unsupported); |
| 234 |
} |
| 235 |
|
| 236 |
static SizeAndActionsVec |
| 237 |
narrowToSmallerAndUnsupportedIfTooSmall(const SizeAndActionsVec &v) { |
| 238 |
using namespace LegacyLegalizeActions; |
| 239 |
return decreaseToSmallerTypesAndIncreaseToSmallest(v, NarrowScalar, |
| 240 |
Unsupported); |
| 241 |
} |
| 242 |
|
| 243 |
static SizeAndActionsVec |
| 244 |
narrowToSmallerAndWidenToSmallest(const SizeAndActionsVec &v) { |
| 245 |
using namespace LegacyLegalizeActions; |
| 246 |
assert(v.size() > 0 && |
| 247 |
"At least one size that can be legalized towards is needed" |
| 248 |
" for this SizeChangeStrategy"); |
| 249 |
return decreaseToSmallerTypesAndIncreaseToSmallest(v, NarrowScalar, |
| 250 |
WidenScalar); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
static SizeAndActionsVec |
| 272 |
moreToWiderTypesAndLessToWidest(const SizeAndActionsVec &v) { |
| 273 |
using namespace LegacyLegalizeActions; |
| 274 |
return increaseToLargerTypesAndDecreaseToLargest(v, MoreElements, |
| 275 |
FewerElements); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
static SizeAndActionsVec increaseToLargerTypesAndDecreaseToLargest( |
| 280 |
const SizeAndActionsVec &v, |
| 281 |
LegacyLegalizeActions::LegacyLegalizeAction IncreaseAction, |
| 282 |
LegacyLegalizeActions::LegacyLegalizeAction DecreaseAction); |
| 283 |
|
| 284 |
static SizeAndActionsVec decreaseToSmallerTypesAndIncreaseToSmallest( |
| 285 |
const SizeAndActionsVec &v, |
| 286 |
LegacyLegalizeActions::LegacyLegalizeAction DecreaseAction, |
| 287 |
LegacyLegalizeActions::LegacyLegalizeAction IncreaseAction); |
| 288 |
|
| 289 |
LegacyLegalizeActionStep getAction(const LegalityQuery &Query) const; |
| 290 |
|
| 291 |
unsigned getOpcodeIdxForOpcode(unsigned Opcode) const; |
| 292 |
|
| 293 |
private: |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
std::pair<LegacyLegalizeActions::LegacyLegalizeAction, LLT> |
| 301 |
getAspectAction(const InstrAspect &Aspect) const; |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
void setScalarAction(const unsigned Opcode, const unsigned TypeIndex, |
| 321 |
const SizeAndActionsVec &SizeAndActions) { |
| 322 |
const unsigned OpcodeIdx = Opcode - FirstOp; |
| 323 |
SmallVector<SizeAndActionsVec, 1> &Actions = ScalarActions[OpcodeIdx]; |
| 324 |
setActions(TypeIndex, Actions, SizeAndActions); |
| 325 |
} |
| 326 |
void setPointerAction(const unsigned Opcode, const unsigned TypeIndex, |
| 327 |
const unsigned AddressSpace, |
| 328 |
const SizeAndActionsVec &SizeAndActions) { |
| 329 |
const unsigned OpcodeIdx = Opcode - FirstOp; |
| 330 |
if (AddrSpace2PointerActions[OpcodeIdx].find(AddressSpace) == |
| 331 |
AddrSpace2PointerActions[OpcodeIdx].end()) |
| 332 |
AddrSpace2PointerActions[OpcodeIdx][AddressSpace] = {{}}; |
| 333 |
SmallVector<SizeAndActionsVec, 1> &Actions = |
| 334 |
AddrSpace2PointerActions[OpcodeIdx].find(AddressSpace)->second; |
| 335 |
setActions(TypeIndex, Actions, SizeAndActions); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
void setScalarInVectorAction(const unsigned Opcode, const unsigned TypeIndex, |
| 344 |
const SizeAndActionsVec &SizeAndActions) { |
| 345 |
unsigned OpcodeIdx = Opcode - FirstOp; |
| 346 |
SmallVector<SizeAndActionsVec, 1> &Actions = |
| 347 |
ScalarInVectorActions[OpcodeIdx]; |
| 348 |
setActions(TypeIndex, Actions, SizeAndActions); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
void setVectorNumElementAction(const unsigned Opcode, |
| 355 |
const unsigned TypeIndex, |
| 356 |
const unsigned ElementSize, |
| 357 |
const SizeAndActionsVec &SizeAndActions) { |
| 358 |
const unsigned OpcodeIdx = Opcode - FirstOp; |
| 359 |
if (NumElements2Actions[OpcodeIdx].find(ElementSize) == |
| 360 |
NumElements2Actions[OpcodeIdx].end()) |
| 361 |
NumElements2Actions[OpcodeIdx][ElementSize] = {{}}; |
| 362 |
SmallVector<SizeAndActionsVec, 1> &Actions = |
| 363 |
NumElements2Actions[OpcodeIdx].find(ElementSize)->second; |
| 364 |
setActions(TypeIndex, Actions, SizeAndActions); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
static void checkPartialSizeAndActionsVector(const SizeAndActionsVec& v) { |
| 370 |
using namespace LegacyLegalizeActions; |
| 371 |
#ifndef NDEBUG |
| 372 |
|
| 373 |
int prev_size = -1; |
| 374 |
for(auto SizeAndAction: v) { |
| 375 |
assert(SizeAndAction.first > prev_size); |
| 376 |
prev_size = SizeAndAction.first; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
int SmallestNarrowIdx = -1; |
| 384 |
int LargestWidenIdx = -1; |
| 385 |
int SmallestLegalizableToSameSizeIdx = -1; |
| 386 |
int LargestLegalizableToSameSizeIdx = -1; |
| 387 |
for(size_t i=0; i<v.size(); ++i) { |
| 388 |
switch (v[i].second) { |
| 389 |
case FewerElements: |
| 390 |
case NarrowScalar: |
| 391 |
if (SmallestNarrowIdx == -1) |
| 392 |
SmallestNarrowIdx = i; |
| 393 |
break; |
| 394 |
case WidenScalar: |
| 395 |
case MoreElements: |
| 396 |
LargestWidenIdx = i; |
| 397 |
break; |
| 398 |
case Unsupported: |
| 399 |
break; |
| 400 |
default: |
| 401 |
if (SmallestLegalizableToSameSizeIdx == -1) |
| 402 |
SmallestLegalizableToSameSizeIdx = i; |
| 403 |
LargestLegalizableToSameSizeIdx = i; |
| 404 |
} |
| 405 |
} |
| 406 |
if (SmallestNarrowIdx != -1) { |
| 407 |
assert(SmallestLegalizableToSameSizeIdx != -1); |
| 408 |
assert(SmallestNarrowIdx > SmallestLegalizableToSameSizeIdx); |
| 409 |
} |
| 410 |
if (LargestWidenIdx != -1) |
| 411 |
assert(LargestWidenIdx < LargestLegalizableToSameSizeIdx); |
| 412 |
#endif |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
static void checkFullSizeAndActionsVector(const SizeAndActionsVec& v) { |
| 418 |
#ifndef NDEBUG |
| 419 |
|
| 420 |
assert(v.size() >= 1); |
| 421 |
assert(v[0].first == 1); |
| 422 |
checkPartialSizeAndActionsVector(v); |
| 423 |
#endif |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
void setActions(unsigned TypeIndex, |
| 429 |
SmallVector<SizeAndActionsVec, 1> &Actions, |
| 430 |
const SizeAndActionsVec &SizeAndActions) { |
| 431 |
checkFullSizeAndActionsVector(SizeAndActions); |
| 432 |
if (Actions.size() <= TypeIndex) |
| 433 |
Actions.resize(TypeIndex + 1); |
| 434 |
Actions[TypeIndex] = SizeAndActions; |
| 435 |
} |
| 436 |
|
| 437 |
static SizeAndAction findAction(const SizeAndActionsVec &Vec, |
| 438 |
const uint32_t Size); |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
std::pair<LegacyLegalizeActions::LegacyLegalizeAction, LLT> |
| 452 |
findScalarLegalAction(const InstrAspect &Aspect) const; |
| 453 |
|
| 454 |
|
| 455 |
std::pair<LegacyLegalizeActions::LegacyLegalizeAction, LLT> |
| 456 |
findVectorLegalAction(const InstrAspect &Aspect) const; |
| 457 |
|
| 458 |
static const int FirstOp = TargetOpcode::PRE_ISEL_GENERIC_OPCODE_START; |
| 459 |
static const int LastOp = TargetOpcode::PRE_ISEL_GENERIC_OPCODE_END; |
| 460 |
|
| 461 |
|
| 462 |
using TypeMap = DenseMap<LLT, LegacyLegalizeActions::LegacyLegalizeAction>; |
| 463 |
SmallVector<TypeMap, 1> SpecifiedActions[LastOp - FirstOp + 1]; |
| 464 |
SmallVector<SizeChangeStrategy, 1> |
| 465 |
ScalarSizeChangeStrategies[LastOp - FirstOp + 1]; |
| 466 |
SmallVector<SizeChangeStrategy, 1> |
| 467 |
VectorElementSizeChangeStrategies[LastOp - FirstOp + 1]; |
| 468 |
bool TablesInitialized = false; |
| 469 |
|
| 470 |
|
| 471 |
SmallVector<SizeAndActionsVec, 1> ScalarActions[LastOp - FirstOp + 1]; |
| 472 |
SmallVector<SizeAndActionsVec, 1> ScalarInVectorActions[LastOp - FirstOp + 1]; |
| 473 |
std::unordered_map<uint16_t, SmallVector<SizeAndActionsVec, 1>> |
| 474 |
AddrSpace2PointerActions[LastOp - FirstOp + 1]; |
| 475 |
std::unordered_map<uint16_t, SmallVector<SizeAndActionsVec, 1>> |
| 476 |
NumElements2Actions[LastOp - FirstOp + 1]; |
| 477 |
}; |
| 478 |
|
| 479 |
} |
| 480 |
|
| 481 |
#endif |