| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
#ifndef LLVM_CODEGEN_RDFGRAPH_H |
| 225 |
#define LLVM_CODEGEN_RDFGRAPH_H |
| 226 |
|
| 227 |
#include "RDFRegisters.h" |
| 228 |
#include "llvm/ADT/SmallVector.h" |
| 229 |
#include "llvm/MC/LaneBitmask.h" |
| 230 |
#include "llvm/Support/Allocator.h" |
| 231 |
#include "llvm/Support/MathExtras.h" |
| 232 |
#include <cassert> |
| 233 |
#include <cstdint> |
| 234 |
#include <cstring> |
| 235 |
#include <map> |
| 236 |
#include <set> |
| 237 |
#include <unordered_map> |
| 238 |
#include <utility> |
| 239 |
#include <vector> |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
static_assert(sizeof(uint32_t) == sizeof(unsigned), "Those should be equal"); |
| 245 |
|
| 246 |
namespace llvm { |
| 247 |
|
| 248 |
class MachineBasicBlock; |
| 249 |
class MachineDominanceFrontier; |
| 250 |
class MachineDominatorTree; |
| 251 |
class MachineFunction; |
| 252 |
class MachineInstr; |
| 253 |
class MachineOperand; |
| 254 |
class raw_ostream; |
| 255 |
class TargetInstrInfo; |
| 256 |
class TargetRegisterInfo; |
| 257 |
|
| 258 |
namespace rdf { |
| 259 |
|
| 260 |
using NodeId = uint32_t; |
| 261 |
|
| 262 |
struct DataFlowGraph; |
| 263 |
|
| 264 |
struct NodeAttrs { |
| 265 |
enum : uint16_t { |
| 266 |
None = 0x0000, |
| 267 |
|
| 268 |
|
| 269 |
TypeMask = 0x0003, |
| 270 |
Code = 0x0001, |
| 271 |
Ref = 0x0002, |
| 272 |
|
| 273 |
|
| 274 |
KindMask = 0x0007 << 2, |
| 275 |
Def = 0x0001 << 2, |
| 276 |
Use = 0x0002 << 2, |
| 277 |
Phi = 0x0003 << 2, |
| 278 |
Stmt = 0x0004 << 2, |
| 279 |
Block = 0x0005 << 2, |
| 280 |
Func = 0x0006 << 2, |
| 281 |
|
| 282 |
|
| 283 |
FlagMask = 0x007F << 5, |
| 284 |
Shadow = 0x0001 << 5, |
| 285 |
Clobbering = 0x0002 << 5, |
| 286 |
PhiRef = 0x0004 << 5, |
| 287 |
Preserving = 0x0008 << 5, |
| 288 |
Fixed = 0x0010 << 5, |
| 289 |
Undef = 0x0020 << 5, |
| 290 |
Dead = 0x0040 << 5, |
| 291 |
}; |
| 292 |
|
| 293 |
static uint16_t type(uint16_t T) { return T & TypeMask; } |
| 294 |
static uint16_t kind(uint16_t T) { return T & KindMask; } |
| 295 |
static uint16_t flags(uint16_t T) { return T & FlagMask; } |
| 296 |
|
| 297 |
static uint16_t set_type(uint16_t A, uint16_t T) { |
| 298 |
return (A & ~TypeMask) | T; |
| 299 |
} |
| 300 |
|
| 301 |
static uint16_t set_kind(uint16_t A, uint16_t K) { |
| 302 |
return (A & ~KindMask) | K; |
| 303 |
} |
| 304 |
|
| 305 |
static uint16_t set_flags(uint16_t A, uint16_t F) { |
| 306 |
return (A & ~FlagMask) | F; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
static bool contains(uint16_t A, uint16_t B) { |
| 311 |
if (type(A) != Code) |
| 312 |
return false; |
| 313 |
uint16_t KB = kind(B); |
| 314 |
switch (kind(A)) { |
| 315 |
case Func: |
| 316 |
return KB == Block; |
| 317 |
case Block: |
| 318 |
return KB == Phi || KB == Stmt; |
| 319 |
case Phi: |
| 320 |
case Stmt: |
| 321 |
return type(B) == Ref; |
| 322 |
} |
| 323 |
return false; |
| 324 |
} |
| 325 |
}; |
| 326 |
|
| 327 |
struct BuildOptions { |
| 328 |
enum : unsigned { |
| 329 |
None = 0x00, |
| 330 |
KeepDeadPhis = 0x01, |
| 331 |
}; |
| 332 |
}; |
| 333 |
|
| 334 |
template <typename T> struct NodeAddr { |
| 335 |
NodeAddr() = default; |
| 336 |
NodeAddr(T A, NodeId I) : Addr(A), Id(I) {} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
template <typename S> NodeAddr(const NodeAddr<S> &NA) |
| 341 |
: Addr(static_cast<T>(NA.Addr)), Id(NA.Id) {} |
| 342 |
|
| 343 |
bool operator== (const NodeAddr<T> &NA) const { |
| 344 |
assert((Addr == NA.Addr) == (Id == NA.Id)); |
| 345 |
return Addr == NA.Addr; |
| 346 |
} |
| 347 |
bool operator!= (const NodeAddr<T> &NA) const { |
| 348 |
return !operator==(NA); |
| 349 |
} |
| 350 |
|
| 351 |
T Addr = nullptr; |
| 352 |
NodeId Id = 0; |
| 353 |
}; |
| 354 |
|
| 355 |
struct NodeBase; |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
struct NodeAllocator { |
| 374 |
|
| 375 |
enum { NodeMemSize = 32 }; |
| 376 |
|
| 377 |
NodeAllocator(uint32_t NPB = 4096) |
| 378 |
: NodesPerBlock(NPB), BitsPerIndex(Log2_32(NPB)), |
| 379 |
IndexMask((1 << BitsPerIndex)-1) { |
| 380 |
assert(isPowerOf2_32(NPB)); |
| 381 |
} |
| 382 |
|
| 383 |
NodeBase *ptr(NodeId N) const { |
| 384 |
uint32_t N1 = N-1; |
| 385 |
uint32_t BlockN = N1 >> BitsPerIndex; |
| 386 |
uint32_t Offset = (N1 & IndexMask) * NodeMemSize; |
| 387 |
return reinterpret_cast<NodeBase*>(Blocks[BlockN]+Offset); |
| 388 |
} |
| 389 |
|
| 390 |
NodeId id(const NodeBase *P) const; |
| 391 |
NodeAddr<NodeBase*> New(); |
| 392 |
void clear(); |
| 393 |
|
| 394 |
private: |
| 395 |
void startNewBlock(); |
| 396 |
bool needNewBlock(); |
| 397 |
|
| 398 |
uint32_t makeId(uint32_t Block, uint32_t Index) const { |
| 399 |
|
| 400 |
return ((Block << BitsPerIndex) | Index) + 1; |
| 401 |
} |
| 402 |
|
| 403 |
const uint32_t NodesPerBlock; |
| 404 |
const uint32_t BitsPerIndex; |
| 405 |
const uint32_t IndexMask; |
| 406 |
char *ActiveEnd = nullptr; |
| 407 |
std::vector<char*> Blocks; |
| 408 |
using AllocatorTy = BumpPtrAllocatorImpl<MallocAllocator, 65536>; |
| 409 |
AllocatorTy MemPool; |
| 410 |
}; |
| 411 |
|
| 412 |
using RegisterSet = std::set<RegisterRef>; |
| 413 |
|
| 414 |
struct TargetOperandInfo { |
| 415 |
TargetOperandInfo(const TargetInstrInfo &tii) : TII(tii) {} |
| 416 |
virtual ~TargetOperandInfo() = default; |
| 417 |
|
| 418 |
virtual bool isPreserving(const MachineInstr &In, unsigned OpNum) const; |
| 419 |
virtual bool isClobbering(const MachineInstr &In, unsigned OpNum) const; |
| 420 |
virtual bool isFixedReg(const MachineInstr &In, unsigned OpNum) const; |
| 421 |
|
| 422 |
const TargetInstrInfo &TII; |
| 423 |
}; |
| 424 |
|
| 425 |
|
| 426 |
struct PackedRegisterRef { |
| 427 |
RegisterId Reg; |
| 428 |
uint32_t MaskId; |
| 429 |
}; |
| 430 |
|
| 431 |
struct LaneMaskIndex : private IndexedSet<LaneBitmask> { |
| 432 |
LaneMaskIndex() = default; |
| 433 |
|
| 434 |
LaneBitmask getLaneMaskForIndex(uint32_t K) const { |
| 435 |
return K == 0 ? LaneBitmask::getAll() : get(K); |
| 436 |
} |
| 437 |
|
| 438 |
uint32_t getIndexForLaneMask(LaneBitmask LM) { |
| 439 |
assert(LM.any()); |
| 440 |
return LM.all() ? 0 : insert(LM); |
| 441 |
} |
| 442 |
|
| 443 |
uint32_t getIndexForLaneMask(LaneBitmask LM) const { |
| 444 |
assert(LM.any()); |
| 445 |
return LM.all() ? 0 : find(LM); |
| 446 |
} |
| 447 |
}; |
| 448 |
|
| 449 |
struct NodeBase { |
| 450 |
public: |
| 451 |
|
| 452 |
NodeBase() = default; |
| 453 |
|
| 454 |
uint16_t getType() const { return NodeAttrs::type(Attrs); } |
| 455 |
uint16_t getKind() const { return NodeAttrs::kind(Attrs); } |
| 456 |
uint16_t getFlags() const { return NodeAttrs::flags(Attrs); } |
| 457 |
NodeId getNext() const { return Next; } |
| 458 |
|
| 459 |
uint16_t getAttrs() const { return Attrs; } |
| 460 |
void setAttrs(uint16_t A) { Attrs = A; } |
| 461 |
void setFlags(uint16_t F) { setAttrs(NodeAttrs::set_flags(getAttrs(), F)); } |
| 462 |
|
| 463 |
|
| 464 |
void append(NodeAddr<NodeBase*> NA); |
| 465 |
|
| 466 |
|
| 467 |
void init() { memset(this, 0, sizeof *this); } |
| 468 |
|
| 469 |
void setNext(NodeId N) { Next = N; } |
| 470 |
|
| 471 |
protected: |
| 472 |
uint16_t Attrs; |
| 473 |
uint16_t Reserved; |
| 474 |
NodeId Next; |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
struct Def_struct { |
| 479 |
NodeId DD, DU; |
| 480 |
}; |
| 481 |
struct PhiU_struct { |
| 482 |
NodeId PredB; |
| 483 |
}; |
| 484 |
struct Code_struct { |
| 485 |
void *CP; |
| 486 |
NodeId FirstM, LastM; |
| 487 |
}; |
| 488 |
struct Ref_struct { |
| 489 |
NodeId RD, Sib; |
| 490 |
union { |
| 491 |
Def_struct Def; |
| 492 |
PhiU_struct PhiU; |
| 493 |
}; |
| 494 |
union { |
| 495 |
MachineOperand *Op; |
| 496 |
PackedRegisterRef PR; |
| 497 |
}; |
| 498 |
}; |
| 499 |
|
| 500 |
|
| 501 |
union { |
| 502 |
Ref_struct Ref; |
| 503 |
Code_struct Code; |
| 504 |
}; |
| 505 |
}; |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
static_assert(sizeof(NodeBase) <= NodeAllocator::NodeMemSize, |
| 510 |
"NodeBase must be at most NodeAllocator::NodeMemSize bytes"); |
| 511 |
|
| 512 |
using NodeList = SmallVector<NodeAddr<NodeBase *>, 4>; |
| 513 |
using NodeSet = std::set<NodeId>; |
| 514 |
|
| 515 |
struct RefNode : public NodeBase { |
| 516 |
RefNode() = default; |
| 517 |
|
| 518 |
RegisterRef getRegRef(const DataFlowGraph &G) const; |
| 519 |
|
| 520 |
MachineOperand &getOp() { |
| 521 |
assert(!(getFlags() & NodeAttrs::PhiRef)); |
| 522 |
return *Ref.Op; |
| 523 |
} |
| 524 |
|
| 525 |
void setRegRef(RegisterRef RR, DataFlowGraph &G); |
| 526 |
void setRegRef(MachineOperand *Op, DataFlowGraph &G); |
| 527 |
|
| 528 |
NodeId getReachingDef() const { |
| 529 |
return Ref.RD; |
| 530 |
} |
| 531 |
void setReachingDef(NodeId RD) { |
| 532 |
Ref.RD = RD; |
| 533 |
} |
| 534 |
|
| 535 |
NodeId getSibling() const { |
| 536 |
return Ref.Sib; |
| 537 |
} |
| 538 |
void setSibling(NodeId Sib) { |
| 539 |
Ref.Sib = Sib; |
| 540 |
} |
| 541 |
|
| 542 |
bool isUse() const { |
| 543 |
assert(getType() == NodeAttrs::Ref); |
| 544 |
return getKind() == NodeAttrs::Use; |
| 545 |
} |
| 546 |
|
| 547 |
bool isDef() const { |
| 548 |
assert(getType() == NodeAttrs::Ref); |
| 549 |
return getKind() == NodeAttrs::Def; |
| 550 |
} |
| 551 |
|
| 552 |
template <typename Predicate> |
| 553 |
NodeAddr<RefNode*> getNextRef(RegisterRef RR, Predicate P, bool NextOnly, |
| 554 |
const DataFlowGraph &G); |
| 555 |
NodeAddr<NodeBase*> getOwner(const DataFlowGraph &G); |
| 556 |
}; |
| 557 |
|
| 558 |
struct DefNode : public RefNode { |
| 559 |
NodeId getReachedDef() const { |
| 560 |
return Ref.Def.DD; |
| 561 |
} |
| 562 |
void setReachedDef(NodeId D) { |
| 563 |
Ref.Def.DD = D; |
| 564 |
} |
| 565 |
NodeId getReachedUse() const { |
| 566 |
return Ref.Def.DU; |
| 567 |
} |
| 568 |
void setReachedUse(NodeId U) { |
| 569 |
Ref.Def.DU = U; |
| 570 |
} |
| 571 |
|
| 572 |
void linkToDef(NodeId Self, NodeAddr<DefNode*> DA); |
| 573 |
}; |
| 574 |
|
| 575 |
struct UseNode : public RefNode { |
| 576 |
void linkToDef(NodeId Self, NodeAddr<DefNode*> DA); |
| 577 |
}; |
| 578 |
|
| 579 |
struct PhiUseNode : public UseNode { |
| 580 |
NodeId getPredecessor() const { |
| 581 |
assert(getFlags() & NodeAttrs::PhiRef); |
| 582 |
return Ref.PhiU.PredB; |
| 583 |
} |
| 584 |
void setPredecessor(NodeId B) { |
| 585 |
assert(getFlags() & NodeAttrs::PhiRef); |
| 586 |
Ref.PhiU.PredB = B; |
| 587 |
} |
| 588 |
}; |
| 589 |
|
| 590 |
struct CodeNode : public NodeBase { |
| 591 |
template <typename T> T getCode() const { |
| 592 |
return static_cast<T>(Code.CP); |
| 593 |
} |
| 594 |
void setCode(void *C) { |
| 595 |
Code.CP = C; |
| 596 |
} |
| 597 |
|
| 598 |
NodeAddr<NodeBase*> getFirstMember(const DataFlowGraph &G) const; |
| 599 |
NodeAddr<NodeBase*> getLastMember(const DataFlowGraph &G) const; |
| 600 |
void addMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G); |
| 601 |
void addMemberAfter(NodeAddr<NodeBase*> MA, NodeAddr<NodeBase*> NA, |
| 602 |
const DataFlowGraph &G); |
| 603 |
void removeMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G); |
| 604 |
|
| 605 |
NodeList members(const DataFlowGraph &G) const; |
| 606 |
template <typename Predicate> |
| 607 |
NodeList members_if(Predicate P, const DataFlowGraph &G) const; |
| 608 |
}; |
| 609 |
|
| 610 |
struct InstrNode : public CodeNode { |
| 611 |
NodeAddr<NodeBase*> getOwner(const DataFlowGraph &G); |
| 612 |
}; |
| 613 |
|
| 614 |
struct PhiNode : public InstrNode { |
| 615 |
MachineInstr *getCode() const { |
| 616 |
return nullptr; |
| 617 |
} |
| 618 |
}; |
| 619 |
|
| 620 |
struct StmtNode : public InstrNode { |
| 621 |
MachineInstr *getCode() const { |
| 622 |
return CodeNode::getCode<MachineInstr*>(); |
| 623 |
} |
| 624 |
}; |
| 625 |
|
| 626 |
struct BlockNode : public CodeNode { |
| 627 |
MachineBasicBlock *getCode() const { |
| 628 |
return CodeNode::getCode<MachineBasicBlock*>(); |
| 629 |
} |
| 630 |
|
| 631 |
void addPhi(NodeAddr<PhiNode*> PA, const DataFlowGraph &G); |
| 632 |
}; |
| 633 |
|
| 634 |
struct FuncNode : public CodeNode { |
| 635 |
MachineFunction *getCode() const { |
| 636 |
return CodeNode::getCode<MachineFunction*>(); |
| 637 |
} |
| 638 |
|
| 639 |
NodeAddr<BlockNode*> findBlock(const MachineBasicBlock *BB, |
| 640 |
const DataFlowGraph &G) const; |
| 641 |
NodeAddr<BlockNode*> getEntryBlock(const DataFlowGraph &G); |
| 642 |
}; |
| 643 |
|
| 644 |
struct DataFlowGraph { |
| 645 |
DataFlowGraph(MachineFunction &mf, const TargetInstrInfo &tii, |
| 646 |
const TargetRegisterInfo &tri, const MachineDominatorTree &mdt, |
| 647 |
const MachineDominanceFrontier &mdf, const TargetOperandInfo &toi); |
| 648 |
|
| 649 |
NodeBase *ptr(NodeId N) const; |
| 650 |
template <typename T> T ptr(NodeId N) const { |
| 651 |
return static_cast<T>(ptr(N)); |
| 652 |
} |
| 653 |
|
| 654 |
NodeId id(const NodeBase *P) const; |
| 655 |
|
| 656 |
template <typename T> NodeAddr<T> addr(NodeId N) const { |
| 657 |
return { ptr<T>(N), N }; |
| 658 |
} |
| 659 |
|
| 660 |
NodeAddr<FuncNode*> getFunc() const { return Func; } |
| 661 |
MachineFunction &getMF() const { return MF; } |
| 662 |
const TargetInstrInfo &getTII() const { return TII; } |
| 663 |
const TargetRegisterInfo &getTRI() const { return TRI; } |
| 664 |
const PhysicalRegisterInfo &getPRI() const { return PRI; } |
| 665 |
const MachineDominatorTree &getDT() const { return MDT; } |
| 666 |
const MachineDominanceFrontier &getDF() const { return MDF; } |
| 667 |
const RegisterAggr &getLiveIns() const { return LiveIns; } |
| 668 |
|
| 669 |
struct DefStack { |
| 670 |
DefStack() = default; |
| 671 |
|
| 672 |
bool empty() const { return Stack.empty() || top() == bottom(); } |
| 673 |
|
| 674 |
private: |
| 675 |
using value_type = NodeAddr<DefNode *>; |
| 676 |
struct Iterator { |
| 677 |
using value_type = DefStack::value_type; |
| 678 |
|
| 679 |
Iterator &up() { Pos = DS.nextUp(Pos); return *this; } |
| 680 |
Iterator &down() { Pos = DS.nextDown(Pos); return *this; } |
| 681 |
|
| 682 |
value_type operator*() const { |
| 683 |
assert(Pos >= 1); |
| 684 |
return DS.Stack[Pos-1]; |
| 685 |
} |
| 686 |
const value_type *operator->() const { |
| 687 |
assert(Pos >= 1); |
| 688 |
return &DS.Stack[Pos-1]; |
| 689 |
} |
| 690 |
bool operator==(const Iterator &It) const { return Pos == It.Pos; } |
| 691 |
bool operator!=(const Iterator &It) const { return Pos != It.Pos; } |
| 692 |
|
| 693 |
private: |
| 694 |
friend struct DefStack; |
| 695 |
|
| 696 |
Iterator(const DefStack &S, bool Top); |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
const DefStack &DS; |
| 701 |
unsigned Pos; |
| 702 |
}; |
| 703 |
|
| 704 |
public: |
| 705 |
using iterator = Iterator; |
| 706 |
|
| 707 |
iterator top() const { return Iterator(*this, true); } |
| 708 |
iterator bottom() const { return Iterator(*this, false); } |
| 709 |
unsigned size() const; |
| 710 |
|
| 711 |
void push(NodeAddr<DefNode*> DA) { Stack.push_back(DA); } |
| 712 |
void pop(); |
| 713 |
void start_block(NodeId N); |
| 714 |
void clear_block(NodeId N); |
| 715 |
|
| 716 |
private: |
| 717 |
friend struct Iterator; |
| 718 |
|
| 719 |
using StorageType = std::vector<value_type>; |
| 720 |
|
| 721 |
bool isDelimiter(const StorageType::value_type &P, NodeId N = 0) const { |
| 722 |
return (P.Addr == nullptr) && (N == 0 || P.Id == N); |
| 723 |
} |
| 724 |
|
| 725 |
unsigned nextUp(unsigned P) const; |
| 726 |
unsigned nextDown(unsigned P) const; |
| 727 |
|
| 728 |
StorageType Stack; |
| 729 |
}; |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
using DefStackMap = std::unordered_map<RegisterId, DefStack>; |
| 734 |
|
| 735 |
void build(unsigned Options = BuildOptions::None); |
| 736 |
void pushAllDefs(NodeAddr<InstrNode*> IA, DefStackMap &DM); |
| 737 |
void markBlock(NodeId B, DefStackMap &DefM); |
| 738 |
void releaseBlock(NodeId B, DefStackMap &DefM); |
| 739 |
|
| 740 |
PackedRegisterRef pack(RegisterRef RR) { |
| 741 |
return { RR.Reg, LMI.getIndexForLaneMask(RR.Mask) }; |
| 742 |
} |
| 743 |
PackedRegisterRef pack(RegisterRef RR) const { |
| 744 |
return { RR.Reg, LMI.getIndexForLaneMask(RR.Mask) }; |
| 745 |
} |
| 746 |
RegisterRef unpack(PackedRegisterRef PR) const { |
| 747 |
return RegisterRef(PR.Reg, LMI.getLaneMaskForIndex(PR.MaskId)); |
| 748 |
} |
| 749 |
|
| 750 |
RegisterRef makeRegRef(unsigned Reg, unsigned Sub) const; |
| 751 |
RegisterRef makeRegRef(const MachineOperand &Op) const; |
| 752 |
RegisterRef restrictRef(RegisterRef AR, RegisterRef BR) const; |
| 753 |
|
| 754 |
NodeAddr<RefNode*> getNextRelated(NodeAddr<InstrNode*> IA, |
| 755 |
NodeAddr<RefNode*> RA) const; |
| 756 |
NodeAddr<RefNode*> getNextShadow(NodeAddr<InstrNode*> IA, |
| 757 |
NodeAddr<RefNode*> RA, bool Create); |
| 758 |
NodeAddr<RefNode*> getNextShadow(NodeAddr<InstrNode*> IA, |
| 759 |
NodeAddr<RefNode*> RA) const; |
| 760 |
|
| 761 |
NodeList getRelatedRefs(NodeAddr<InstrNode*> IA, |
| 762 |
NodeAddr<RefNode*> RA) const; |
| 763 |
|
| 764 |
NodeAddr<BlockNode*> findBlock(MachineBasicBlock *BB) const { |
| 765 |
return BlockNodes.at(BB); |
| 766 |
} |
| 767 |
|
| 768 |
void unlinkUse(NodeAddr<UseNode*> UA, bool RemoveFromOwner) { |
| 769 |
unlinkUseDF(UA); |
| 770 |
if (RemoveFromOwner) |
| 771 |
removeFromOwner(UA); |
| 772 |
} |
| 773 |
|
| 774 |
void unlinkDef(NodeAddr<DefNode*> DA, bool RemoveFromOwner) { |
| 775 |
unlinkDefDF(DA); |
| 776 |
if (RemoveFromOwner) |
| 777 |
removeFromOwner(DA); |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
template <uint16_t Kind> |
| 782 |
static bool IsRef(const NodeAddr<NodeBase*> BA) { |
| 783 |
return BA.Addr->getType() == NodeAttrs::Ref && |
| 784 |
BA.Addr->getKind() == Kind; |
| 785 |
} |
| 786 |
|
| 787 |
template <uint16_t Kind> |
| 788 |
static bool IsCode(const NodeAddr<NodeBase*> BA) { |
| 789 |
return BA.Addr->getType() == NodeAttrs::Code && |
| 790 |
BA.Addr->getKind() == Kind; |
| 791 |
} |
| 792 |
|
| 793 |
static bool IsDef(const NodeAddr<NodeBase*> BA) { |
| 794 |
return BA.Addr->getType() == NodeAttrs::Ref && |
| 795 |
BA.Addr->getKind() == NodeAttrs::Def; |
| 796 |
} |
| 797 |
|
| 798 |
static bool IsUse(const NodeAddr<NodeBase*> BA) { |
| 799 |
return BA.Addr->getType() == NodeAttrs::Ref && |
| 800 |
BA.Addr->getKind() == NodeAttrs::Use; |
| 801 |
} |
| 802 |
|
| 803 |
static bool IsPhi(const NodeAddr<NodeBase*> BA) { |
| 804 |
return BA.Addr->getType() == NodeAttrs::Code && |
| 805 |
BA.Addr->getKind() == NodeAttrs::Phi; |
| 806 |
} |
| 807 |
|
| 808 |
static bool IsPreservingDef(const NodeAddr<DefNode*> DA) { |
| 809 |
uint16_t Flags = DA.Addr->getFlags(); |
| 810 |
return (Flags & NodeAttrs::Preserving) && !(Flags & NodeAttrs::Undef); |
| 811 |
} |
| 812 |
|
| 813 |
private: |
| 814 |
void reset(); |
| 815 |
|
| 816 |
RegisterSet getLandingPadLiveIns() const; |
| 817 |
|
| 818 |
NodeAddr<NodeBase*> newNode(uint16_t Attrs); |
| 819 |
NodeAddr<NodeBase*> cloneNode(const NodeAddr<NodeBase*> B); |
| 820 |
NodeAddr<UseNode*> newUse(NodeAddr<InstrNode*> Owner, |
| 821 |
MachineOperand &Op, uint16_t Flags = NodeAttrs::None); |
| 822 |
NodeAddr<PhiUseNode*> newPhiUse(NodeAddr<PhiNode*> Owner, |
| 823 |
RegisterRef RR, NodeAddr<BlockNode*> PredB, |
| 824 |
uint16_t Flags = NodeAttrs::PhiRef); |
| 825 |
NodeAddr<DefNode*> newDef(NodeAddr<InstrNode*> Owner, |
| 826 |
MachineOperand &Op, uint16_t Flags = NodeAttrs::None); |
| 827 |
NodeAddr<DefNode*> newDef(NodeAddr<InstrNode*> Owner, |
| 828 |
RegisterRef RR, uint16_t Flags = NodeAttrs::PhiRef); |
| 829 |
NodeAddr<PhiNode*> newPhi(NodeAddr<BlockNode*> Owner); |
| 830 |
NodeAddr<StmtNode*> newStmt(NodeAddr<BlockNode*> Owner, |
| 831 |
MachineInstr *MI); |
| 832 |
NodeAddr<BlockNode*> newBlock(NodeAddr<FuncNode*> Owner, |
| 833 |
MachineBasicBlock *BB); |
| 834 |
NodeAddr<FuncNode*> newFunc(MachineFunction *MF); |
| 835 |
|
| 836 |
template <typename Predicate> |
| 837 |
std::pair<NodeAddr<RefNode*>,NodeAddr<RefNode*>> |
| 838 |
locateNextRef(NodeAddr<InstrNode*> IA, NodeAddr<RefNode*> RA, |
| 839 |
Predicate P) const; |
| 840 |
|
| 841 |
using BlockRefsMap = std::map<NodeId, RegisterSet>; |
| 842 |
|
| 843 |
void buildStmt(NodeAddr<BlockNode*> BA, MachineInstr &In); |
| 844 |
void recordDefsForDF(BlockRefsMap &PhiM, NodeAddr<BlockNode*> BA); |
| 845 |
void buildPhis(BlockRefsMap &PhiM, RegisterSet &AllRefs, |
| 846 |
NodeAddr<BlockNode*> BA); |
| 847 |
void removeUnusedPhis(); |
| 848 |
|
| 849 |
void pushClobbers(NodeAddr<InstrNode*> IA, DefStackMap &DM); |
| 850 |
void pushDefs(NodeAddr<InstrNode*> IA, DefStackMap &DM); |
| 851 |
template <typename T> void linkRefUp(NodeAddr<InstrNode*> IA, |
| 852 |
NodeAddr<T> TA, DefStack &DS); |
| 853 |
template <typename Predicate> void linkStmtRefs(DefStackMap &DefM, |
| 854 |
NodeAddr<StmtNode*> SA, Predicate P); |
| 855 |
void linkBlockRefs(DefStackMap &DefM, NodeAddr<BlockNode*> BA); |
| 856 |
|
| 857 |
void unlinkUseDF(NodeAddr<UseNode*> UA); |
| 858 |
void unlinkDefDF(NodeAddr<DefNode*> DA); |
| 859 |
|
| 860 |
void removeFromOwner(NodeAddr<RefNode*> RA) { |
| 861 |
NodeAddr<InstrNode*> IA = RA.Addr->getOwner(*this); |
| 862 |
IA.Addr->removeMember(RA, *this); |
| 863 |
} |
| 864 |
|
| 865 |
MachineFunction &MF; |
| 866 |
const TargetInstrInfo &TII; |
| 867 |
const TargetRegisterInfo &TRI; |
| 868 |
const PhysicalRegisterInfo PRI; |
| 869 |
const MachineDominatorTree &MDT; |
| 870 |
const MachineDominanceFrontier &MDF; |
| 871 |
const TargetOperandInfo &TOI; |
| 872 |
|
| 873 |
RegisterAggr LiveIns; |
| 874 |
NodeAddr<FuncNode*> Func; |
| 875 |
NodeAllocator Memory; |
| 876 |
|
| 877 |
std::map<MachineBasicBlock*,NodeAddr<BlockNode*>> BlockNodes; |
| 878 |
|
| 879 |
LaneMaskIndex LMI; |
| 880 |
}; |
| 881 |
|
| 882 |
template <typename Predicate> |
| 883 |
NodeAddr<RefNode*> RefNode::getNextRef(RegisterRef RR, Predicate P, |
| 884 |
bool NextOnly, const DataFlowGraph &G) { |
| 885 |
|
| 886 |
|
| 887 |
auto NA = G.addr<NodeBase*>(getNext()); |
| 888 |
|
| 889 |
while (NA.Addr != this) { |
| 890 |
if (NA.Addr->getType() == NodeAttrs::Ref) { |
| 891 |
NodeAddr<RefNode*> RA = NA; |
| 892 |
if (RA.Addr->getRegRef(G) == RR && P(NA)) |
| 893 |
return NA; |
| 894 |
if (NextOnly) |
| 895 |
break; |
| 896 |
NA = G.addr<NodeBase*>(NA.Addr->getNext()); |
| 897 |
} else { |
| 898 |
|
| 899 |
assert(NA.Addr->getType() == NodeAttrs::Code); |
| 900 |
NodeAddr<CodeNode*> CA = NA; |
| 901 |
NA = CA.Addr->getFirstMember(G); |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
return NodeAddr<RefNode*>(); |
| 906 |
} |
| 907 |
|
| 908 |
template <typename Predicate> |
| 909 |
NodeList CodeNode::members_if(Predicate P, const DataFlowGraph &G) const { |
| 910 |
NodeList MM; |
| 911 |
auto M = getFirstMember(G); |
| 912 |
if (M.Id == 0) |
| 913 |
return MM; |
| 914 |
|
| 915 |
while (M.Addr != this) { |
| 916 |
if (P(M)) |
| 917 |
MM.push_back(M); |
| 918 |
M = G.addr<NodeBase*>(M.Addr->getNext()); |
| 919 |
} |
| 920 |
return MM; |
| 921 |
} |
| 922 |
|
| 923 |
template <typename T> |
| 924 |
struct Print { |
| 925 |
Print(const T &x, const DataFlowGraph &g) : Obj(x), G(g) {} |
| 926 |
|
| 927 |
const T &Obj; |
| 928 |
const DataFlowGraph &G; |
| 929 |
}; |
| 930 |
|
| 931 |
template <typename T> |
| 932 |
struct PrintNode : Print<NodeAddr<T>> { |
| 933 |
PrintNode(const NodeAddr<T> &x, const DataFlowGraph &g) |
| 934 |
: Print<NodeAddr<T>>(x, g) {} |
| 935 |
}; |
| 936 |
|
| 937 |
raw_ostream &operator<<(raw_ostream &OS, const Print<RegisterRef> &P); |
| 938 |
raw_ostream &operator<<(raw_ostream &OS, const Print<NodeId> &P); |
| 939 |
raw_ostream &operator<<(raw_ostream &OS, const Print<NodeAddr<DefNode *>> &P); |
| 940 |
raw_ostream &operator<<(raw_ostream &OS, const Print<NodeAddr<UseNode *>> &P); |
| 941 |
raw_ostream &operator<<(raw_ostream &OS, |
| 942 |
const Print<NodeAddr<PhiUseNode *>> &P); |
| 943 |
raw_ostream &operator<<(raw_ostream &OS, const Print<NodeAddr<RefNode *>> &P); |
| 944 |
raw_ostream &operator<<(raw_ostream &OS, const Print<NodeList> &P); |
| 945 |
raw_ostream &operator<<(raw_ostream &OS, const Print<NodeSet> &P); |
| 946 |
raw_ostream &operator<<(raw_ostream &OS, const Print<NodeAddr<PhiNode *>> &P); |
| 947 |
raw_ostream &operator<<(raw_ostream &OS, |
| 948 |
const Print<NodeAddr<StmtNode *>> &P); |
| 949 |
raw_ostream &operator<<(raw_ostream &OS, |
| 950 |
const Print<NodeAddr<InstrNode *>> &P); |
| 951 |
raw_ostream &operator<<(raw_ostream &OS, |
| 952 |
const Print<NodeAddr<BlockNode *>> &P); |
| 953 |
raw_ostream &operator<<(raw_ostream &OS, |
| 954 |
const Print<NodeAddr<FuncNode *>> &P); |
| 955 |
raw_ostream &operator<<(raw_ostream &OS, const Print<RegisterSet> &P); |
| 956 |
raw_ostream &operator<<(raw_ostream &OS, const Print<RegisterAggr> &P); |
| 957 |
raw_ostream &operator<<(raw_ostream &OS, |
| 958 |
const Print<DataFlowGraph::DefStack> &P); |
| 959 |
|
| 960 |
} |
| 961 |
|
| 962 |
} |
| 963 |
|
| 964 |
#endif |