| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#ifndef LLVM_CODEGEN_REGISTERPRESSURE_H |
| 15 |
#define LLVM_CODEGEN_REGISTERPRESSURE_H |
| 16 |
|
| 17 |
#include "llvm/ADT/ArrayRef.h" |
| 18 |
#include "llvm/ADT/SmallVector.h" |
| 19 |
#include "llvm/ADT/SparseSet.h" |
| 20 |
#include "llvm/CodeGen/MachineBasicBlock.h" |
| 21 |
#include "llvm/CodeGen/SlotIndexes.h" |
| 22 |
#include "llvm/CodeGen/TargetRegisterInfo.h" |
| 23 |
#include "llvm/MC/LaneBitmask.h" |
| 24 |
#include <cassert> |
| 25 |
#include <cstddef> |
| 26 |
#include <cstdint> |
| 27 |
#include <cstdlib> |
| 28 |
#include <limits> |
| 29 |
#include <vector> |
| 30 |
|
| 31 |
namespace llvm { |
| 32 |
|
| 33 |
class LiveIntervals; |
| 34 |
class MachineFunction; |
| 35 |
class MachineInstr; |
| 36 |
class MachineRegisterInfo; |
| 37 |
class RegisterClassInfo; |
| 38 |
|
| 39 |
struct RegisterMaskPair { |
| 40 |
Register RegUnit; |
| 41 |
LaneBitmask LaneMask; |
| 42 |
|
| 43 |
RegisterMaskPair(Register RegUnit, LaneBitmask LaneMask) |
| 44 |
: RegUnit(RegUnit), LaneMask(LaneMask) {} |
| 45 |
}; |
| 46 |
|
| 47 |
|
| 48 |
struct RegisterPressure { |
| 49 |
|
| 50 |
std::vector<unsigned> MaxSetPressure; |
| 51 |
|
| 52 |
|
| 53 |
SmallVector<RegisterMaskPair,8> LiveInRegs; |
| 54 |
SmallVector<RegisterMaskPair,8> LiveOutRegs; |
| 55 |
|
| 56 |
void dump(const TargetRegisterInfo *TRI) const; |
| 57 |
}; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
struct IntervalPressure : RegisterPressure { |
| 69 |
|
| 70 |
SlotIndex TopIdx; |
| 71 |
SlotIndex BottomIdx; |
| 72 |
|
| 73 |
void reset(); |
| 74 |
|
| 75 |
void openTop(SlotIndex NextTop); |
| 76 |
|
| 77 |
void openBottom(SlotIndex PrevBottom); |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
struct RegionPressure : RegisterPressure { |
| 84 |
|
| 85 |
MachineBasicBlock::const_iterator TopPos; |
| 86 |
MachineBasicBlock::const_iterator BottomPos; |
| 87 |
|
| 88 |
void reset(); |
| 89 |
|
| 90 |
void openTop(MachineBasicBlock::const_iterator PrevTop); |
| 91 |
|
| 92 |
void openBottom(MachineBasicBlock::const_iterator PrevBottom); |
| 93 |
}; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
class PressureChange { |
| 104 |
uint16_t PSetID = 0; |
| 105 |
int16_t UnitInc = 0; |
| 106 |
|
| 107 |
public: |
| 108 |
PressureChange() = default; |
| 109 |
PressureChange(unsigned id): PSetID(id + 1) { |
| 110 |
assert(id < std::numeric_limits<uint16_t>::max() && "PSetID overflow."); |
| 111 |
} |
| 112 |
|
| 113 |
bool isValid() const { return PSetID > 0; } |
| 114 |
|
| 115 |
unsigned getPSet() const { |
| 116 |
assert(isValid() && "invalid PressureChange"); |
| 117 |
return PSetID - 1; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
unsigned getPSetOrMax() const { |
| 122 |
return (PSetID - 1) & std::numeric_limits<uint16_t>::max(); |
| 123 |
} |
| 124 |
|
| 125 |
int getUnitInc() const { return UnitInc; } |
| 126 |
|
| 127 |
void setUnitInc(int Inc) { UnitInc = Inc; } |
| 128 |
|
| 129 |
bool operator==(const PressureChange &RHS) const { |
| 130 |
return PSetID == RHS.PSetID && UnitInc == RHS.UnitInc; |
| 131 |
} |
| 132 |
|
| 133 |
void dump() const; |
| 134 |
}; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
class PressureDiff { |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
enum { MaxPSets = 16 }; |
| 146 |
|
| 147 |
PressureChange PressureChanges[MaxPSets]; |
| 148 |
|
| 149 |
using iterator = PressureChange *; |
| 150 |
|
| 151 |
iterator nonconst_begin() { return &PressureChanges[0]; } |
| 152 |
iterator nonconst_end() { return &PressureChanges[MaxPSets]; } |
| 153 |
|
| 154 |
public: |
| 155 |
using const_iterator = const PressureChange *; |
| 156 |
|
| 157 |
const_iterator begin() const { return &PressureChanges[0]; } |
| 158 |
const_iterator end() const { return &PressureChanges[MaxPSets]; } |
| 159 |
|
| 160 |
void addPressureChange(Register RegUnit, bool IsDec, |
| 161 |
const MachineRegisterInfo *MRI); |
| 162 |
|
| 163 |
void dump(const TargetRegisterInfo &TRI) const; |
| 164 |
}; |
| 165 |
|
| 166 |
|
| 167 |
class RegisterOperands { |
| 168 |
public: |
| 169 |
|
| 170 |
SmallVector<RegisterMaskPair, 8> Uses; |
| 171 |
|
| 172 |
|
| 173 |
SmallVector<RegisterMaskPair, 8> Defs; |
| 174 |
|
| 175 |
|
| 176 |
SmallVector<RegisterMaskPair, 8> DeadDefs; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
void collect(const MachineInstr &MI, const TargetRegisterInfo &TRI, |
| 181 |
const MachineRegisterInfo &MRI, bool TrackLaneMasks, |
| 182 |
bool IgnoreDead); |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
void detectDeadDefs(const MachineInstr &MI, const LiveIntervals &LIS); |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
void adjustLaneLiveness(const LiveIntervals &LIS, |
| 193 |
const MachineRegisterInfo &MRI, SlotIndex Pos, |
| 194 |
MachineInstr *AddFlagsMI = nullptr); |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
class PressureDiffs { |
| 199 |
PressureDiff *PDiffArray = nullptr; |
| 200 |
unsigned Size = 0; |
| 201 |
unsigned Max = 0; |
| 202 |
|
| 203 |
public: |
| 204 |
PressureDiffs() = default; |
| 205 |
~PressureDiffs() { free(PDiffArray); } |
| 206 |
|
| 207 |
void clear() { Size = 0; } |
| 208 |
|
| 209 |
void init(unsigned N); |
| 210 |
|
| 211 |
PressureDiff &operator[](unsigned Idx) { |
| 212 |
assert(Idx < Size && "PressureDiff index out of bounds"); |
| 213 |
return PDiffArray[Idx]; |
| 214 |
} |
| 215 |
const PressureDiff &operator[](unsigned Idx) const { |
| 216 |
return const_cast<PressureDiffs*>(this)->operator[](Idx); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
void addInstruction(unsigned Idx, const RegisterOperands &RegOpers, |
| 222 |
const MachineRegisterInfo &MRI); |
| 223 |
}; |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
struct RegPressureDelta { |
| 240 |
PressureChange Excess; |
| 241 |
PressureChange CriticalMax; |
| 242 |
PressureChange CurrentMax; |
| 243 |
|
| 244 |
RegPressureDelta() = default; |
| 245 |
|
| 246 |
bool operator==(const RegPressureDelta &RHS) const { |
| 247 |
return Excess == RHS.Excess && CriticalMax == RHS.CriticalMax |
| 248 |
&& CurrentMax == RHS.CurrentMax; |
| 249 |
} |
| 250 |
bool operator!=(const RegPressureDelta &RHS) const { |
| 251 |
return !operator==(RHS); |
| 252 |
} |
| 253 |
void dump() const; |
| 254 |
}; |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
class LiveRegSet { |
| 261 |
private: |
| 262 |
struct IndexMaskPair { |
| 263 |
unsigned Index; |
| 264 |
LaneBitmask LaneMask; |
| 265 |
|
| 266 |
IndexMaskPair(unsigned Index, LaneBitmask LaneMask) |
| 267 |
: Index(Index), LaneMask(LaneMask) {} |
| 268 |
|
| 269 |
unsigned getSparseSetIndex() const { |
| 270 |
return Index; |
| 271 |
} |
| 272 |
}; |
| 273 |
|
| 274 |
using RegSet = SparseSet<IndexMaskPair>; |
| 275 |
RegSet Regs; |
| 276 |
unsigned NumRegUnits; |
| 277 |
|
| 278 |
unsigned getSparseIndexFromReg(Register Reg) const { |
| 279 |
if (Reg.isVirtual()) |
| 280 |
return Register::virtReg2Index(Reg) + NumRegUnits; |
| 281 |
assert(Reg < NumRegUnits); |
| 282 |
return Reg; |
| 283 |
} |
| 284 |
|
| 285 |
Register getRegFromSparseIndex(unsigned SparseIndex) const { |
| 286 |
if (SparseIndex >= NumRegUnits) |
| 287 |
return Register::index2VirtReg(SparseIndex - NumRegUnits); |
| 288 |
return Register(SparseIndex); |
| 289 |
} |
| 290 |
|
| 291 |
public: |
| 292 |
void clear(); |
| 293 |
void init(const MachineRegisterInfo &MRI); |
| 294 |
|
| 295 |
LaneBitmask contains(Register Reg) const { |
| 296 |
unsigned SparseIndex = getSparseIndexFromReg(Reg); |
| 297 |
RegSet::const_iterator I = Regs.find(SparseIndex); |
| 298 |
if (I == Regs.end()) |
| 299 |
return LaneBitmask::getNone(); |
| 300 |
return I->LaneMask; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
LaneBitmask insert(RegisterMaskPair Pair) { |
| 306 |
unsigned SparseIndex = getSparseIndexFromReg(Pair.RegUnit); |
| 307 |
auto InsertRes = Regs.insert(IndexMaskPair(SparseIndex, Pair.LaneMask)); |
| 308 |
if (!InsertRes.second) { |
| 309 |
LaneBitmask PrevMask = InsertRes.first->LaneMask; |
| 310 |
InsertRes.first->LaneMask |= Pair.LaneMask; |
| 311 |
return PrevMask; |
| 312 |
} |
| 313 |
return LaneBitmask::getNone(); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
LaneBitmask erase(RegisterMaskPair Pair) { |
| 319 |
unsigned SparseIndex = getSparseIndexFromReg(Pair.RegUnit); |
| 320 |
RegSet::iterator I = Regs.find(SparseIndex); |
| 321 |
if (I == Regs.end()) |
| 322 |
return LaneBitmask::getNone(); |
| 323 |
LaneBitmask PrevMask = I->LaneMask; |
| 324 |
I->LaneMask &= ~Pair.LaneMask; |
| 325 |
return PrevMask; |
| 326 |
} |
| 327 |
|
| 328 |
size_t size() const { |
| 329 |
return Regs.size(); |
| 330 |
} |
| 331 |
|
| 332 |
template<typename ContainerT> |
| 333 |
void appendTo(ContainerT &To) const { |
| 334 |
for (const IndexMaskPair &P : Regs) { |
| 335 |
Register Reg = getRegFromSparseIndex(P.Index); |
| 336 |
if (P.LaneMask.any()) |
| 337 |
To.push_back(RegisterMaskPair(Reg, P.LaneMask)); |
| 338 |
} |
| 339 |
} |
| 340 |
}; |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
class RegPressureTracker { |
| 359 |
const MachineFunction *MF = nullptr; |
| 360 |
const TargetRegisterInfo *TRI = nullptr; |
| 361 |
const RegisterClassInfo *RCI = nullptr; |
| 362 |
const MachineRegisterInfo *MRI; |
| 363 |
const LiveIntervals *LIS = nullptr; |
| 364 |
|
| 365 |
|
| 366 |
const MachineBasicBlock *MBB = nullptr; |
| 367 |
|
| 368 |
|
| 369 |
RegisterPressure &P; |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
bool RequireIntervals; |
| 374 |
|
| 375 |
|
| 376 |
bool TrackUntiedDefs = false; |
| 377 |
|
| 378 |
|
| 379 |
bool TrackLaneMasks = false; |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
MachineBasicBlock::const_iterator CurrPos; |
| 385 |
|
| 386 |
|
| 387 |
std::vector<unsigned> CurrSetPressure; |
| 388 |
|
| 389 |
|
| 390 |
LiveRegSet LiveRegs; |
| 391 |
|
| 392 |
|
| 393 |
SparseSet<Register, VirtReg2IndexFunctor> UntiedDefs; |
| 394 |
|
| 395 |
std::vector<unsigned> LiveThruPressure; |
| 396 |
|
| 397 |
public: |
| 398 |
RegPressureTracker(IntervalPressure &rp) : P(rp), RequireIntervals(true) {} |
| 399 |
RegPressureTracker(RegionPressure &rp) : P(rp), RequireIntervals(false) {} |
| 400 |
|
| 401 |
void reset(); |
| 402 |
|
| 403 |
void init(const MachineFunction *mf, const RegisterClassInfo *rci, |
| 404 |
const LiveIntervals *lis, const MachineBasicBlock *mbb, |
| 405 |
MachineBasicBlock::const_iterator pos, |
| 406 |
bool TrackLaneMasks, bool TrackUntiedDefs); |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
void addLiveRegs(ArrayRef<RegisterMaskPair> Regs); |
| 412 |
|
| 413 |
|
| 414 |
MachineBasicBlock::const_iterator getPos() const { return CurrPos; } |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
void setPos(MachineBasicBlock::const_iterator Pos) { CurrPos = Pos; } |
| 421 |
|
| 422 |
|
| 423 |
void recede(SmallVectorImpl<RegisterMaskPair> *LiveUses = nullptr); |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
void recede(const RegisterOperands &RegOpers, |
| 430 |
SmallVectorImpl<RegisterMaskPair> *LiveUses = nullptr); |
| 431 |
|
| 432 |
|
| 433 |
void recedeSkipDebugValues(); |
| 434 |
|
| 435 |
|
| 436 |
void advance(); |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
void advance(const RegisterOperands &RegOpers); |
| 442 |
|
| 443 |
|
| 444 |
void closeRegion(); |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
void initLiveThru(const RegPressureTracker &RPTracker); |
| 449 |
|
| 450 |
|
| 451 |
void initLiveThru(ArrayRef<unsigned> PressureSet) { |
| 452 |
LiveThruPressure.assign(PressureSet.begin(), PressureSet.end()); |
| 453 |
} |
| 454 |
|
| 455 |
ArrayRef<unsigned> getLiveThru() const { return LiveThruPressure; } |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
RegisterPressure &getPressure() { return P; } |
| 460 |
const RegisterPressure &getPressure() const { return P; } |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
const std::vector<unsigned> &getRegSetPressureAtPos() const { |
| 465 |
return CurrSetPressure; |
| 466 |
} |
| 467 |
|
| 468 |
bool isTopClosed() const; |
| 469 |
bool isBottomClosed() const; |
| 470 |
|
| 471 |
void closeTop(); |
| 472 |
void closeBottom(); |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
void getMaxUpwardPressureDelta(const MachineInstr *MI, |
| 479 |
PressureDiff *PDiff, |
| 480 |
RegPressureDelta &Delta, |
| 481 |
ArrayRef<PressureChange> CriticalPSets, |
| 482 |
ArrayRef<unsigned> MaxPressureLimit); |
| 483 |
|
| 484 |
void getUpwardPressureDelta(const MachineInstr *MI, |
| 485 |
PressureDiff &PDiff, |
| 486 |
RegPressureDelta &Delta, |
| 487 |
ArrayRef<PressureChange> CriticalPSets, |
| 488 |
ArrayRef<unsigned> MaxPressureLimit) const; |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
void getMaxDownwardPressureDelta(const MachineInstr *MI, |
| 495 |
RegPressureDelta &Delta, |
| 496 |
ArrayRef<PressureChange> CriticalPSets, |
| 497 |
ArrayRef<unsigned> MaxPressureLimit); |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
void getMaxPressureDelta(const MachineInstr *MI, |
| 503 |
RegPressureDelta &Delta, |
| 504 |
ArrayRef<PressureChange> CriticalPSets, |
| 505 |
ArrayRef<unsigned> MaxPressureLimit) { |
| 506 |
if (isTopClosed()) |
| 507 |
return getMaxDownwardPressureDelta(MI, Delta, CriticalPSets, |
| 508 |
MaxPressureLimit); |
| 509 |
|
| 510 |
assert(isBottomClosed() && "Uninitialized pressure tracker"); |
| 511 |
return getMaxUpwardPressureDelta(MI, nullptr, Delta, CriticalPSets, |
| 512 |
MaxPressureLimit); |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
void getUpwardPressure(const MachineInstr *MI, |
| 517 |
std::vector<unsigned> &PressureResult, |
| 518 |
std::vector<unsigned> &MaxPressureResult); |
| 519 |
|
| 520 |
|
| 521 |
void getDownwardPressure(const MachineInstr *MI, |
| 522 |
std::vector<unsigned> &PressureResult, |
| 523 |
std::vector<unsigned> &MaxPressureResult); |
| 524 |
|
| 525 |
void getPressureAfterInst(const MachineInstr *MI, |
| 526 |
std::vector<unsigned> &PressureResult, |
| 527 |
std::vector<unsigned> &MaxPressureResult) { |
| 528 |
if (isTopClosed()) |
| 529 |
return getUpwardPressure(MI, PressureResult, MaxPressureResult); |
| 530 |
|
| 531 |
assert(isBottomClosed() && "Uninitialized pressure tracker"); |
| 532 |
return getDownwardPressure(MI, PressureResult, MaxPressureResult); |
| 533 |
} |
| 534 |
|
| 535 |
bool hasUntiedDef(Register VirtReg) const { |
| 536 |
return UntiedDefs.count(VirtReg); |
| 537 |
} |
| 538 |
|
| 539 |
void dump() const; |
| 540 |
|
| 541 |
protected: |
| 542 |
|
| 543 |
void discoverLiveOut(RegisterMaskPair Pair); |
| 544 |
|
| 545 |
void discoverLiveIn(RegisterMaskPair Pair); |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
SlotIndex getCurrSlot() const; |
| 550 |
|
| 551 |
void increaseRegPressure(Register RegUnit, LaneBitmask PreviousMask, |
| 552 |
LaneBitmask NewMask); |
| 553 |
void decreaseRegPressure(Register RegUnit, LaneBitmask PreviousMask, |
| 554 |
LaneBitmask NewMask); |
| 555 |
|
| 556 |
void bumpDeadDefs(ArrayRef<RegisterMaskPair> DeadDefs); |
| 557 |
|
| 558 |
void bumpUpwardPressure(const MachineInstr *MI); |
| 559 |
void bumpDownwardPressure(const MachineInstr *MI); |
| 560 |
|
| 561 |
void discoverLiveInOrOut(RegisterMaskPair Pair, |
| 562 |
SmallVectorImpl<RegisterMaskPair> &LiveInOrOut); |
| 563 |
|
| 564 |
LaneBitmask getLastUsedLanes(Register RegUnit, SlotIndex Pos) const; |
| 565 |
LaneBitmask getLiveLanesAt(Register RegUnit, SlotIndex Pos) const; |
| 566 |
LaneBitmask getLiveThroughAt(Register RegUnit, SlotIndex Pos) const; |
| 567 |
}; |
| 568 |
|
| 569 |
void dumpRegSetPressure(ArrayRef<unsigned> SetPressure, |
| 570 |
const TargetRegisterInfo *TRI); |
| 571 |
|
| 572 |
} |
| 573 |
|
| 574 |
#endif |