1 //===- RDFGraph.cpp -------------------------------------------------------===// 2 // 3 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Target-independent, SSA-based data flow graph for register data flow (RDF). 10 // 11 #include "llvm/ADT/BitVector.h" 12 #include "llvm/ADT/STLExtras.h" 13 #include "llvm/ADT/SetVector.h" 14 #include "llvm/CodeGen/MachineBasicBlock.h" 15 #include "llvm/CodeGen/MachineDominanceFrontier.h" 16 #include "llvm/CodeGen/MachineDominators.h" 17 #include "llvm/CodeGen/MachineFunction.h" 18 #include "llvm/CodeGen/MachineInstr.h" 19 #include "llvm/CodeGen/MachineOperand.h" 20 #include "llvm/CodeGen/MachineRegisterInfo.h" 21 #include "llvm/CodeGen/RDFGraph.h" 22 #include "llvm/CodeGen/RDFRegisters.h" 23 #include "llvm/CodeGen/TargetInstrInfo.h" 24 #include "llvm/CodeGen/TargetLowering.h" 25 #include "llvm/CodeGen/TargetRegisterInfo.h" 26 #include "llvm/CodeGen/TargetSubtargetInfo.h" 27 #include "llvm/IR/Function.h" 28 #include "llvm/MC/LaneBitmask.h" 29 #include "llvm/MC/MCInstrDesc.h" 30 #include "llvm/Support/ErrorHandling.h" 31 #include "llvm/Support/raw_ostream.h" 32 #include <algorithm> 33 #include <cassert> 34 #include <cstdint> 35 #include <cstring> 36 #include <iterator> 37 #include <set> 38 #include <utility> 39 #include <vector> 40 41 using namespace llvm; 42 using namespace rdf; 43 44 // Printing functions. Have them here first, so that the rest of the code 45 // can use them. 46 namespace llvm { 47 namespace rdf { 48 49 raw_ostream &operator<<(raw_ostream &OS, const Print<RegisterRef> &P) { 50 P.G.getPRI().print(OS, P.Obj); 51 return OS; 52 } 53 54 raw_ostream &operator<<(raw_ostream &OS, const Print<NodeId> &P) { 55 auto NA = P.G.addr<NodeBase *>(P.Obj); 56 uint16_t Attrs = NA.Addr->getAttrs(); 57 uint16_t Kind = NodeAttrs::kind(Attrs); 58 uint16_t Flags = NodeAttrs::flags(Attrs); 59 switch (NodeAttrs::type(Attrs)) { 60 case NodeAttrs::Code: 61 switch (Kind) { 62 case NodeAttrs::Func: 63 OS << 'f'; 64 break; 65 case NodeAttrs::Block: 66 OS << 'b'; 67 break; 68 case NodeAttrs::Stmt: 69 OS << 's'; 70 break; 71 case NodeAttrs::Phi: 72 OS << 'p'; 73 break; 74 default: 75 OS << "c?"; 76 break; 77 } 78 break; 79 case NodeAttrs::Ref: 80 if (Flags & NodeAttrs::Undef) 81 OS << '/'; 82 if (Flags & NodeAttrs::Dead) 83 OS << '\\'; 84 if (Flags & NodeAttrs::Preserving) 85 OS << '+'; 86 if (Flags & NodeAttrs::Clobbering) 87 OS << '~'; 88 switch (Kind) { 89 case NodeAttrs::Use: 90 OS << 'u'; 91 break; 92 case NodeAttrs::Def: 93 OS << 'd'; 94 break; 95 case NodeAttrs::Block: 96 OS << 'b'; 97 break; 98 default: 99 OS << "r?"; 100 break; 101 } 102 break; 103 default: 104 OS << '?'; 105 break; 106 } 107 OS << P.Obj; 108 if (Flags & NodeAttrs::Shadow) 109 OS << '"'; 110 return OS; 111 } 112 113 static void printRefHeader(raw_ostream &OS, const Ref RA, 114 const DataFlowGraph &G) { 115 OS << Print(RA.Id, G) << '<' << Print(RA.Addr->getRegRef(G), G) << '>'; 116 if (RA.Addr->getFlags() & NodeAttrs::Fixed) 117 OS << '!'; 118 } 119 120 raw_ostream &operator<<(raw_ostream &OS, const Print<Def> &P) { 121 printRefHeader(OS, P.Obj, P.G); 122 OS << '('; 123 if (NodeId N = P.Obj.Addr->getReachingDef()) 124 OS << Print(N, P.G); 125 OS << ','; 126 if (NodeId N = P.Obj.Addr->getReachedDef()) 127 OS << Print(N, P.G); 128 OS << ','; 129 if (NodeId N = P.Obj.Addr->getReachedUse()) 130 OS << Print(N, P.G); 131 OS << "):"; 132 if (NodeId N = P.Obj.Addr->getSibling()) 133 OS << Print(N, P.G); 134 return OS; 135 } 136 137 raw_ostream &operator<<(raw_ostream &OS, const Print<Use> &P) { 138 printRefHeader(OS, P.Obj, P.G); 139 OS << '('; 140 if (NodeId N = P.Obj.Addr->getReachingDef()) 141 OS << Print(N, P.G); 142 OS << "):"; 143 if (NodeId N = P.Obj.Addr->getSibling()) 144 OS << Print(N, P.G); 145 return OS; 146 } 147 148 raw_ostream &operator<<(raw_ostream &OS, const Print<PhiUse> &P) { 149 printRefHeader(OS, P.Obj, P.G); 150 OS << '('; 151 if (NodeId N = P.Obj.Addr->getReachingDef()) 152 OS << Print(N, P.G); 153 OS << ','; 154 if (NodeId N = P.Obj.Addr->getPredecessor()) 155 OS << Print(N, P.G); 156 OS << "):"; 157 if (NodeId N = P.Obj.Addr->getSibling()) 158 OS << Print(N, P.G); 159 return OS; 160 } 161 162 raw_ostream &operator<<(raw_ostream &OS, const Print<Ref> &P) { 163 switch (P.Obj.Addr->getKind()) { 164 case NodeAttrs::Def: 165 OS << PrintNode<DefNode *>(P.Obj, P.G); 166 break; 167 case NodeAttrs::Use: 168 if (P.Obj.Addr->getFlags() & NodeAttrs::PhiRef) 169 OS << PrintNode<PhiUseNode *>(P.Obj, P.G); 170 else 171 OS << PrintNode<UseNode *>(P.Obj, P.G); 172 break; 173 } 174 return OS; 175 } 176 177 raw_ostream &operator<<(raw_ostream &OS, const Print<NodeList> &P) { 178 unsigned N = P.Obj.size(); 179 for (auto I : P.Obj) { 180 OS << Print(I.Id, P.G); 181 if (--N) 182 OS << ' '; 183 } 184 return OS; 185 } 186 187 raw_ostream &operator<<(raw_ostream &OS, const Print<NodeSet> &P) { 188 unsigned N = P.Obj.size(); 189 for (auto I : P.Obj) { 190 OS << Print(I, P.G); 191 if (--N) 192 OS << ' '; 193 } 194 return OS; 195 } 196 197 namespace { 198 199 template <typename T> struct PrintListV { 200 PrintListV(const NodeList &L, const DataFlowGraph &G) : List(L), G(G) {} 201 202 using Type = T; 203 const NodeList &List; 204 const DataFlowGraph &G; 205 }; 206 207 template <typename T> 208 raw_ostream &operator<<(raw_ostream &OS, const PrintListV<T> &P) { 209 unsigned N = P.List.size(); 210 for (NodeAddr<T> A : P.List) { 211 OS << PrintNode<T>(A, P.G); 212 if (--N) 213 OS << ", "; 214 } 215 return OS; 216 } 217 218 } // end anonymous namespace 219 220 raw_ostream &operator<<(raw_ostream &OS, const Print<Phi> &P) { 221 OS << Print(P.Obj.Id, P.G) << ": phi [" 222 << PrintListV<RefNode *>(P.Obj.Addr->members(P.G), P.G) << ']'; 223 return OS; 224 } 225 226 raw_ostream &operator<<(raw_ostream &OS, const Print<Stmt> &P) { 227 const MachineInstr &MI = *P.Obj.Addr->getCode(); 228 unsigned Opc = MI.getOpcode(); 229 OS << Print(P.Obj.Id, P.G) << ": " << P.G.getTII().getName(Opc); 230 // Print the target for calls and branches (for readability). 231 if (MI.isCall() || MI.isBranch()) { 232 MachineInstr::const_mop_iterator T = 233 llvm::find_if(MI.operands(), [](const MachineOperand &Op) -> bool { 234 return Op.isMBB() || Op.isGlobal() || Op.isSymbol(); 235 }); 236 if (T != MI.operands_end()) { 237 OS << ' '; 238 if (T->isMBB()) 239 OS << printMBBReference(*T->getMBB()); 240 else if (T->isGlobal()) 241 OS << T->getGlobal()->getName(); 242 else if (T->isSymbol()) 243 OS << T->getSymbolName(); 244 } 245 } 246 OS << " [" << PrintListV<RefNode *>(P.Obj.Addr->members(P.G), P.G) << ']'; 247 return OS; 248 } 249 250 raw_ostream &operator<<(raw_ostream &OS, const Print<Instr> &P) { 251 switch (P.Obj.Addr->getKind()) { 252 case NodeAttrs::Phi: 253 OS << PrintNode<PhiNode *>(P.Obj, P.G); 254 break; 255 case NodeAttrs::Stmt: 256 OS << PrintNode<StmtNode *>(P.Obj, P.G); 257 break; 258 default: 259 OS << "instr? " << Print(P.Obj.Id, P.G); 260 break; 261 } 262 return OS; 263 } 264 265 raw_ostream &operator<<(raw_ostream &OS, const Print<Block> &P) { 266 MachineBasicBlock *BB = P.Obj.Addr->getCode(); 267 unsigned NP = BB->pred_size(); 268 std::vector<int> Ns; 269 auto PrintBBs = [&OS](std::vector<int> Ns) -> void { 270 unsigned N = Ns.size(); 271 for (int I : Ns) { 272 OS << "%bb." << I; 273 if (--N) 274 OS << ", "; 275 } 276 }; 277 278 OS << Print(P.Obj.Id, P.G) << ": --- " << printMBBReference(*BB) 279 << " --- preds(" << NP << "): "; 280 for (MachineBasicBlock *B : BB->predecessors()) 281 Ns.push_back(B->getNumber()); 282 PrintBBs(Ns); 283 284 unsigned NS = BB->succ_size(); 285 OS << " succs(" << NS << "): "; 286 Ns.clear(); 287 for (MachineBasicBlock *B : BB->successors()) 288 Ns.push_back(B->getNumber()); 289 PrintBBs(Ns); 290 OS << '\n'; 291 292 for (auto I : P.Obj.Addr->members(P.G)) 293 OS << PrintNode<InstrNode *>(I, P.G) << '\n'; 294 return OS; 295 } 296 297 raw_ostream &operator<<(raw_ostream &OS, const Print<Func> &P) { 298 OS << "DFG dump:[\n" 299 << Print(P.Obj.Id, P.G) 300 << ": Function: " << P.Obj.Addr->getCode()->getName() << '\n'; 301 for (auto I : P.Obj.Addr->members(P.G)) 302 OS << PrintNode<BlockNode *>(I, P.G) << '\n'; 303 OS << "]\n"; 304 return OS; 305 } 306 307 raw_ostream &operator<<(raw_ostream &OS, const Print<RegisterSet> &P) { 308 OS << '{'; 309 for (auto I : P.Obj) 310 OS << ' ' << Print(I, P.G); 311 OS << " }"; 312 return OS; 313 } 314 315 raw_ostream &operator<<(raw_ostream &OS, const Print<RegisterAggr> &P) { 316 OS << P.Obj; 317 return OS; 318 } 319 320 raw_ostream &operator<<(raw_ostream &OS, 321 const Print<DataFlowGraph::DefStack> &P) { 322 for (auto I = P.Obj.top(), E = P.Obj.bottom(); I != E;) { 323 OS << Print(I->Id, P.G) << '<' << Print(I->Addr->getRegRef(P.G), P.G) 324 << '>'; 325 I.down(); 326 if (I != E) 327 OS << ' '; 328 } 329 return OS; 330 } 331 332 } // end namespace rdf 333 } // end namespace llvm 334 335 // Node allocation functions. 336 // 337 // Node allocator is like a slab memory allocator: it allocates blocks of 338 // memory in sizes that are multiples of the size of a node. Each block has 339 // the same size. Nodes are allocated from the currently active block, and 340 // when it becomes full, a new one is created. 341 // There is a mapping scheme between node id and its location in a block, 342 // and within that block is described in the header file. 343 // 344 void NodeAllocator::startNewBlock() { 345 void *T = MemPool.Allocate(NodesPerBlock * NodeMemSize, NodeMemSize); 346 char *P = static_cast<char *>(T); 347 Blocks.push_back(P); 348 // Check if the block index is still within the allowed range, i.e. less 349 // than 2^N, where N is the number of bits in NodeId for the block index. 350 // BitsPerIndex is the number of bits per node index. 351 assert((Blocks.size() < ((size_t)1 << (8 * sizeof(NodeId) - BitsPerIndex))) && 352 "Out of bits for block index"); 353 ActiveEnd = P; 354 } 355 356 bool NodeAllocator::needNewBlock() { 357 if (Blocks.empty()) 358 return true; 359 360 char *ActiveBegin = Blocks.back(); 361 uint32_t Index = (ActiveEnd - ActiveBegin) / NodeMemSize; 362 return Index >= NodesPerBlock; 363 } 364 365 Node NodeAllocator::New() { 366 if (needNewBlock()) 367 startNewBlock(); 368 369 uint32_t ActiveB = Blocks.size() - 1; 370 uint32_t Index = (ActiveEnd - Blocks[ActiveB]) / NodeMemSize; 371 Node NA = {reinterpret_cast<NodeBase *>(ActiveEnd), makeId(ActiveB, Index)}; 372 ActiveEnd += NodeMemSize; 373 return NA; 374 } 375 376 NodeId NodeAllocator::id(const NodeBase *P) const { 377 uintptr_t A = reinterpret_cast<uintptr_t>(P); 378 for (unsigned i = 0, n = Blocks.size(); i != n; ++i) { 379 uintptr_t B = reinterpret_cast<uintptr_t>(Blocks[i]); 380 if (A < B || A >= B + NodesPerBlock * NodeMemSize) 381 continue; 382 uint32_t Idx = (A - B) / NodeMemSize; 383 return makeId(i, Idx); 384 } 385 llvm_unreachable("Invalid node address"); 386 } 387 388 void NodeAllocator::clear() { 389 MemPool.Reset(); 390 Blocks.clear(); 391 ActiveEnd = nullptr; 392 } 393 394 // Insert node NA after "this" in the circular chain. 395 void NodeBase::append(Node NA) { 396 NodeId Nx = Next; 397 // If NA is already "next", do nothing. 398 if (Next != NA.Id) { 399 Next = NA.Id; 400 NA.Addr->Next = Nx; 401 } 402 } 403 404 // Fundamental node manipulator functions. 405 406 // Obtain the register reference from a reference node. 407 RegisterRef RefNode::getRegRef(const DataFlowGraph &G) const { 408 assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref); 409 if (NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef) 410 return G.unpack(RefData.PR); 411 assert(RefData.Op != nullptr); 412 return G.makeRegRef(*RefData.Op); 413 } 414 415 // Set the register reference in the reference node directly (for references 416 // in phi nodes). 417 void RefNode::setRegRef(RegisterRef RR, DataFlowGraph &G) { 418 assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref); 419 assert(NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef); 420 RefData.PR = G.pack(RR); 421 } 422 423 // Set the register reference in the reference node based on a machine 424 // operand (for references in statement nodes). 425 void RefNode::setRegRef(MachineOperand *Op, DataFlowGraph &G) { 426 assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref); 427 assert(!(NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef)); 428 (void)G; 429 RefData.Op = Op; 430 } 431 432 // Get the owner of a given reference node. 433 Node RefNode::getOwner(const DataFlowGraph &G) { 434 Node NA = G.addr<NodeBase *>(getNext()); 435 436 while (NA.Addr != this) { 437 if (NA.Addr->getType() == NodeAttrs::Code) 438 return NA; 439 NA = G.addr<NodeBase *>(NA.Addr->getNext()); 440 } 441 llvm_unreachable("No owner in circular list"); 442 } 443 444 // Connect the def node to the reaching def node. 445 void DefNode::linkToDef(NodeId Self, Def DA) { 446 RefData.RD = DA.Id; 447 RefData.Sib = DA.Addr->getReachedDef(); 448 DA.Addr->setReachedDef(Self); 449 } 450 451 // Connect the use node to the reaching def node. 452 void UseNode::linkToDef(NodeId Self, Def DA) { 453 RefData.RD = DA.Id; 454 RefData.Sib = DA.Addr->getReachedUse(); 455 DA.Addr->setReachedUse(Self); 456 } 457 458 // Get the first member of the code node. 459 Node CodeNode::getFirstMember(const DataFlowGraph &G) const { 460 if (CodeData.FirstM == 0) 461 return Node(); 462 return G.addr<NodeBase *>(CodeData.FirstM); 463 } 464 465 // Get the last member of the code node. 466 Node CodeNode::getLastMember(const DataFlowGraph &G) const { 467 if (CodeData.LastM == 0) 468 return Node(); 469 return G.addr<NodeBase *>(CodeData.LastM); 470 } 471 472 // Add node NA at the end of the member list of the given code node. 473 void CodeNode::addMember(Node NA, const DataFlowGraph &G) { 474 Node ML = getLastMember(G); 475 if (ML.Id != 0) { 476 ML.Addr->append(NA); 477 } else { 478 CodeData.FirstM = NA.Id; 479 NodeId Self = G.id(this); 480 NA.Addr->setNext(Self); 481 } 482 CodeData.LastM = NA.Id; 483 } 484 485 // Add node NA after member node MA in the given code node. 486 void CodeNode::addMemberAfter(Node MA, Node NA, const DataFlowGraph &G) { 487 MA.Addr->append(NA); 488 if (CodeData.LastM == MA.Id) 489 CodeData.LastM = NA.Id; 490 } 491 492 // Remove member node NA from the given code node. 493 void CodeNode::removeMember(Node NA, const DataFlowGraph &G) { 494 Node MA = getFirstMember(G); 495 assert(MA.Id != 0); 496 497 // Special handling if the member to remove is the first member. 498 if (MA.Id == NA.Id) { 499 if (CodeData.LastM == MA.Id) { 500 // If it is the only member, set both first and last to 0. 501 CodeData.FirstM = CodeData.LastM = 0; 502 } else { 503 // Otherwise, advance the first member. 504 CodeData.FirstM = MA.Addr->getNext(); 505 } 506 return; 507 } 508 509 while (MA.Addr != this) { 510 NodeId MX = MA.Addr->getNext(); 511 if (MX == NA.Id) { 512 MA.Addr->setNext(NA.Addr->getNext()); 513 // If the member to remove happens to be the last one, update the 514 // LastM indicator. 515 if (CodeData.LastM == NA.Id) 516 CodeData.LastM = MA.Id; 517 return; 518 } 519 MA = G.addr<NodeBase *>(MX); 520 } 521 llvm_unreachable("No such member"); 522 } 523 524 // Return the list of all members of the code node. 525 NodeList CodeNode::members(const DataFlowGraph &G) const { 526 static auto True = [](Node) -> bool { return true; }; 527 return members_if(True, G); 528 } 529 530 // Return the owner of the given instr node. 531 Node InstrNode::getOwner(const DataFlowGraph &G) { 532 Node NA = G.addr<NodeBase *>(getNext()); 533 534 while (NA.Addr != this) { 535 assert(NA.Addr->getType() == NodeAttrs::Code); 536 if (NA.Addr->getKind() == NodeAttrs::Block) 537 return NA; 538 NA = G.addr<NodeBase *>(NA.Addr->getNext()); 539 } 540 llvm_unreachable("No owner in circular list"); 541 } 542 543 // Add the phi node PA to the given block node. 544 void BlockNode::addPhi(Phi PA, const DataFlowGraph &G) { 545 Node M = getFirstMember(G); 546 if (M.Id == 0) { 547 addMember(PA, G); 548 return; 549 } 550 551 assert(M.Addr->getType() == NodeAttrs::Code); 552 if (M.Addr->getKind() == NodeAttrs::Stmt) { 553 // If the first member of the block is a statement, insert the phi as 554 // the first member. 555 CodeData.FirstM = PA.Id; 556 PA.Addr->setNext(M.Id); 557 } else { 558 // If the first member is a phi, find the last phi, and append PA to it. 559 assert(M.Addr->getKind() == NodeAttrs::Phi); 560 Node MN = M; 561 do { 562 M = MN; 563 MN = G.addr<NodeBase *>(M.Addr->getNext()); 564 assert(MN.Addr->getType() == NodeAttrs::Code); 565 } while (MN.Addr->getKind() == NodeAttrs::Phi); 566 567 // M is the last phi. 568 addMemberAfter(M, PA, G); 569 } 570 } 571 572 // Find the block node corresponding to the machine basic block BB in the 573 // given func node. 574 Block FuncNode::findBlock(const MachineBasicBlock *BB, 575 const DataFlowGraph &G) const { 576 auto EqBB = [BB](Node NA) -> bool { return Block(NA).Addr->getCode() == BB; }; 577 NodeList Ms = members_if(EqBB, G); 578 if (!Ms.empty()) 579 return Ms[0]; 580 return Block(); 581 } 582 583 // Get the block node for the entry block in the given function. 584 Block FuncNode::getEntryBlock(const DataFlowGraph &G) { 585 MachineBasicBlock *EntryB = &getCode()->front(); 586 return findBlock(EntryB, G); 587 } 588 589 // Target operand information. 590 // 591 592 // For a given instruction, check if there are any bits of RR that can remain 593 // unchanged across this def. 594 bool TargetOperandInfo::isPreserving(const MachineInstr &In, 595 unsigned OpNum) const { 596 return TII.isPredicated(In); 597 } 598 599 // Check if the definition of RR produces an unspecified value. 600 bool TargetOperandInfo::isClobbering(const MachineInstr &In, 601 unsigned OpNum) const { 602 const MachineOperand &Op = In.getOperand(OpNum); 603 if (Op.isRegMask()) 604 return true; 605 assert(Op.isReg()); 606 if (In.isCall()) 607 if (Op.isDef() && Op.isDead()) 608 return true; 609 return false; 610 } 611 612 // Check if the given instruction specifically requires 613 bool TargetOperandInfo::isFixedReg(const MachineInstr &In, 614 unsigned OpNum) const { 615 if (In.isCall() || In.isReturn() || In.isInlineAsm()) 616 return true; 617 // Check for a tail call. 618 if (In.isBranch()) 619 for (const MachineOperand &O : In.operands()) 620 if (O.isGlobal() || O.isSymbol()) 621 return true; 622 623 const MCInstrDesc &D = In.getDesc(); 624 if (D.implicit_defs().empty() && D.implicit_uses().empty()) 625 return false; 626 const MachineOperand &Op = In.getOperand(OpNum); 627 // If there is a sub-register, treat the operand as non-fixed. Currently, 628 // fixed registers are those that are listed in the descriptor as implicit 629 // uses or defs, and those lists do not allow sub-registers. 630 if (Op.getSubReg() != 0) 631 return false; 632 Register Reg = Op.getReg(); 633 ArrayRef<MCPhysReg> ImpOps = 634 Op.isDef() ? D.implicit_defs() : D.implicit_uses(); 635 return is_contained(ImpOps, Reg); 636 } 637 638 // 639 // The data flow graph construction. 640 // 641 642 DataFlowGraph::DataFlowGraph(MachineFunction &mf, const TargetInstrInfo &tii, 643 const TargetRegisterInfo &tri, 644 const MachineDominatorTree &mdt, 645 const MachineDominanceFrontier &mdf) 646 : DefaultTOI(std::make_unique<TargetOperandInfo>(tii)), MF(mf), TII(tii), 647 TRI(tri), PRI(tri, mf), MDT(mdt), MDF(mdf), TOI(*DefaultTOI), 648 LiveIns(PRI) {} 649 650 DataFlowGraph::DataFlowGraph(MachineFunction &mf, const TargetInstrInfo &tii, 651 const TargetRegisterInfo &tri, 652 const MachineDominatorTree &mdt, 653 const MachineDominanceFrontier &mdf, 654 const TargetOperandInfo &toi) 655 : MF(mf), TII(tii), TRI(tri), PRI(tri, mf), MDT(mdt), MDF(mdf), TOI(toi), 656 LiveIns(PRI) {} 657 658 // The implementation of the definition stack. 659 // Each register reference has its own definition stack. In particular, 660 // for a register references "Reg" and "Reg:subreg" will each have their 661 // own definition stacks. 662 663 // Construct a stack iterator. 664 DataFlowGraph::DefStack::Iterator::Iterator(const DataFlowGraph::DefStack &S, 665 bool Top) 666 : DS(S) { 667 if (!Top) { 668 // Initialize to bottom. 669 Pos = 0; 670 return; 671 } 672 // Initialize to the top, i.e. top-most non-delimiter (or 0, if empty). 673 Pos = DS.Stack.size(); 674 while (Pos > 0 && DS.isDelimiter(DS.Stack[Pos - 1])) 675 Pos--; 676 } 677 678 // Return the size of the stack, including block delimiters. 679 unsigned DataFlowGraph::DefStack::size() const { 680 unsigned S = 0; 681 for (auto I = top(), E = bottom(); I != E; I.down()) 682 S++; 683 return S; 684 } 685 686 // Remove the top entry from the stack. Remove all intervening delimiters 687 // so that after this, the stack is either empty, or the top of the stack 688 // is a non-delimiter. 689 void DataFlowGraph::DefStack::pop() { 690 assert(!empty()); 691 unsigned P = nextDown(Stack.size()); 692 Stack.resize(P); 693 } 694 695 // Push a delimiter for block node N on the stack. 696 void DataFlowGraph::DefStack::start_block(NodeId N) { 697 assert(N != 0); 698 Stack.push_back(Def(nullptr, N)); 699 } 700 701 // Remove all nodes from the top of the stack, until the delimited for 702 // block node N is encountered. Remove the delimiter as well. In effect, 703 // this will remove from the stack all definitions from block N. 704 void DataFlowGraph::DefStack::clear_block(NodeId N) { 705 assert(N != 0); 706 unsigned P = Stack.size(); 707 while (P > 0) { 708 bool Found = isDelimiter(Stack[P - 1], N); 709 P--; 710 if (Found) 711 break; 712 } 713 // This will also remove the delimiter, if found. 714 Stack.resize(P); 715 } 716 717 // Move the stack iterator up by one. 718 unsigned DataFlowGraph::DefStack::nextUp(unsigned P) const { 719 // Get the next valid position after P (skipping all delimiters). 720 // The input position P does not have to point to a non-delimiter. 721 unsigned SS = Stack.size(); 722 bool IsDelim; 723 assert(P < SS); 724 do { 725 P++; 726 IsDelim = isDelimiter(Stack[P - 1]); 727 } while (P < SS && IsDelim); 728 assert(!IsDelim); 729 return P; 730 } 731 732 // Move the stack iterator down by one. 733 unsigned DataFlowGraph::DefStack::nextDown(unsigned P) const { 734 // Get the preceding valid position before P (skipping all delimiters). 735 // The input position P does not have to point to a non-delimiter. 736 assert(P > 0 && P <= Stack.size()); 737 bool IsDelim = isDelimiter(Stack[P - 1]); 738 do { 739 if (--P == 0) 740 break; 741 IsDelim = isDelimiter(Stack[P - 1]); 742 } while (P > 0 && IsDelim); 743 assert(!IsDelim); 744 return P; 745 } 746 747 // Register information. 748 749 RegisterAggr DataFlowGraph::getLandingPadLiveIns() const { 750 RegisterAggr LR(getPRI()); 751 const Function &F = MF.getFunction(); 752 const Constant *PF = F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr; 753 const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering(); 754 if (RegisterId R = TLI.getExceptionPointerRegister(PF)) 755 LR.insert(RegisterRef(R)); 756 if (!isFuncletEHPersonality(classifyEHPersonality(PF))) { 757 if (RegisterId R = TLI.getExceptionSelectorRegister(PF)) 758 LR.insert(RegisterRef(R)); 759 } 760 return LR; 761 } 762 763 // Node management functions. 764 765 // Get the pointer to the node with the id N. 766 NodeBase *DataFlowGraph::ptr(NodeId N) const { 767 if (N == 0) 768 return nullptr; 769 return Memory.ptr(N); 770 } 771 772 // Get the id of the node at the address P. 773 NodeId DataFlowGraph::id(const NodeBase *P) const { 774 if (P == nullptr) 775 return 0; 776 return Memory.id(P); 777 } 778 779 // Allocate a new node and set the attributes to Attrs. 780 Node DataFlowGraph::newNode(uint16_t Attrs) { 781 Node P = Memory.New(); 782 P.Addr->init(); 783 P.Addr->setAttrs(Attrs); 784 return P; 785 } 786 787 // Make a copy of the given node B, except for the data-flow links, which 788 // are set to 0. 789 Node DataFlowGraph::cloneNode(const Node B) { 790 Node NA = newNode(0); 791 memcpy(NA.Addr, B.Addr, sizeof(NodeBase)); 792 // Ref nodes need to have the data-flow links reset. 793 if (NA.Addr->getType() == NodeAttrs::Ref) { 794 Ref RA = NA; 795 RA.Addr->setReachingDef(0); 796 RA.Addr->setSibling(0); 797 if (NA.Addr->getKind() == NodeAttrs::Def) { 798 Def DA = NA; 799 DA.Addr->setReachedDef(0); 800 DA.Addr->setReachedUse(0); 801 } 802 } 803 return NA; 804 } 805 806 // Allocation routines for specific node types/kinds. 807 808 Use DataFlowGraph::newUse(Instr Owner, MachineOperand &Op, uint16_t Flags) { 809 Use UA = newNode(NodeAttrs::Ref | NodeAttrs::Use | Flags); 810 UA.Addr->setRegRef(&Op, *this); 811 return UA; 812 } 813 814 PhiUse DataFlowGraph::newPhiUse(Phi Owner, RegisterRef RR, Block PredB, 815 uint16_t Flags) { 816 PhiUse PUA = newNode(NodeAttrs::Ref | NodeAttrs::Use | Flags); 817 assert(Flags & NodeAttrs::PhiRef); 818 PUA.Addr->setRegRef(RR, *this); 819 PUA.Addr->setPredecessor(PredB.Id); 820 return PUA; 821 } 822 823 Def DataFlowGraph::newDef(Instr Owner, MachineOperand &Op, uint16_t Flags) { 824 Def DA = newNode(NodeAttrs::Ref | NodeAttrs::Def | Flags); 825 DA.Addr->setRegRef(&Op, *this); 826 return DA; 827 } 828 829 Def DataFlowGraph::newDef(Instr Owner, RegisterRef RR, uint16_t Flags) { 830 Def DA = newNode(NodeAttrs::Ref | NodeAttrs::Def | Flags); 831 assert(Flags & NodeAttrs::PhiRef); 832 DA.Addr->setRegRef(RR, *this); 833 return DA; 834 } 835 836 Phi DataFlowGraph::newPhi(Block Owner) { 837 Phi PA = newNode(NodeAttrs::Code | NodeAttrs::Phi); 838 Owner.Addr->addPhi(PA, *this); 839 return PA; 840 } 841 842 Stmt DataFlowGraph::newStmt(Block Owner, MachineInstr *MI) { 843 Stmt SA = newNode(NodeAttrs::Code | NodeAttrs::Stmt); 844 SA.Addr->setCode(MI); 845 Owner.Addr->addMember(SA, *this); 846 return SA; 847 } 848 849 Block DataFlowGraph::newBlock(Func Owner, MachineBasicBlock *BB) { 850 Block BA = newNode(NodeAttrs::Code | NodeAttrs::Block); 851 BA.Addr->setCode(BB); 852 Owner.Addr->addMember(BA, *this); 853 return BA; 854 } 855 856 Func DataFlowGraph::newFunc(MachineFunction *MF) { 857 Func FA = newNode(NodeAttrs::Code | NodeAttrs::Func); 858 FA.Addr->setCode(MF); 859 return FA; 860 } 861 862 // Build the data flow graph. 863 void DataFlowGraph::build(unsigned Options) { 864 reset(); 865 TheFunc = newFunc(&MF); 866 867 if (MF.empty()) 868 return; 869 870 for (MachineBasicBlock &B : MF) { 871 Block BA = newBlock(TheFunc, &B); 872 BlockNodes.insert(std::make_pair(&B, BA)); 873 for (MachineInstr &I : B) { 874 if (I.isDebugInstr()) 875 continue; 876 buildStmt(BA, I); 877 } 878 } 879 880 Block EA = TheFunc.Addr->getEntryBlock(*this); 881 NodeList Blocks = TheFunc.Addr->members(*this); 882 883 // Collect information about block references. 884 RegisterSet AllRefs(getPRI()); 885 for (Block BA : Blocks) 886 for (Instr IA : BA.Addr->members(*this)) 887 for (Ref RA : IA.Addr->members(*this)) 888 AllRefs.insert(RA.Addr->getRegRef(*this)); 889 890 // Collect function live-ins and entry block live-ins. 891 MachineRegisterInfo &MRI = MF.getRegInfo(); 892 MachineBasicBlock &EntryB = *EA.Addr->getCode(); 893 assert(EntryB.pred_empty() && "Function entry block has predecessors"); 894 for (std::pair<unsigned, unsigned> P : MRI.liveins()) 895 LiveIns.insert(RegisterRef(P.first)); 896 if (MRI.tracksLiveness()) { 897 for (auto I : EntryB.liveins()) 898 LiveIns.insert(RegisterRef(I.PhysReg, I.LaneMask)); 899 } 900 901 // Add function-entry phi nodes for the live-in registers. 902 for (RegisterRef RR : LiveIns.refs()) { 903 Phi PA = newPhi(EA); 904 uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving; 905 Def DA = newDef(PA, RR, PhiFlags); 906 PA.Addr->addMember(DA, *this); 907 } 908 909 // Add phis for landing pads. 910 // Landing pads, unlike usual backs blocks, are not entered through 911 // branches in the program, or fall-throughs from other blocks. They 912 // are entered from the exception handling runtime and target's ABI 913 // may define certain registers as defined on entry to such a block. 914 RegisterAggr EHRegs = getLandingPadLiveIns(); 915 if (!EHRegs.empty()) { 916 for (Block BA : Blocks) { 917 const MachineBasicBlock &B = *BA.Addr->getCode(); 918 if (!B.isEHPad()) 919 continue; 920 921 // Prepare a list of NodeIds of the block's predecessors. 922 NodeList Preds; 923 for (MachineBasicBlock *PB : B.predecessors()) 924 Preds.push_back(findBlock(PB)); 925 926 // Build phi nodes for each live-in. 927 for (RegisterRef RR : EHRegs.refs()) { 928 Phi PA = newPhi(BA); 929 uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving; 930 // Add def: 931 Def DA = newDef(PA, RR, PhiFlags); 932 PA.Addr->addMember(DA, *this); 933 // Add uses (no reaching defs for phi uses): 934 for (Block PBA : Preds) { 935 PhiUse PUA = newPhiUse(PA, RR, PBA); 936 PA.Addr->addMember(PUA, *this); 937 } 938 } 939 } 940 } 941 942 // Build a map "PhiM" which will contain, for each block, the set 943 // of references that will require phi definitions in that block. 944 BlockRefsMap PhiM(getPRI()); 945 for (Block BA : Blocks) 946 recordDefsForDF(PhiM, BA); 947 for (Block BA : Blocks) 948 buildPhis(PhiM, AllRefs, BA); 949 950 // Link all the refs. This will recursively traverse the dominator tree. 951 DefStackMap DM; 952 linkBlockRefs(DM, EA); 953 954 // Finally, remove all unused phi nodes. 955 if (!(Options & BuildOptions::KeepDeadPhis)) 956 removeUnusedPhis(); 957 } 958 959 RegisterRef DataFlowGraph::makeRegRef(unsigned Reg, unsigned Sub) const { 960 assert(RegisterRef::isRegId(Reg) || RegisterRef::isMaskId(Reg)); 961 assert(Reg != 0); 962 if (Sub != 0) 963 Reg = TRI.getSubReg(Reg, Sub); 964 return RegisterRef(Reg); 965 } 966 967 RegisterRef DataFlowGraph::makeRegRef(const MachineOperand &Op) const { 968 assert(Op.isReg() || Op.isRegMask()); 969 if (Op.isReg()) 970 return makeRegRef(Op.getReg(), Op.getSubReg()); 971 return RegisterRef(getPRI().getRegMaskId(Op.getRegMask()), 972 LaneBitmask::getAll()); 973 } 974 975 // For each stack in the map DefM, push the delimiter for block B on it. 976 void DataFlowGraph::markBlock(NodeId B, DefStackMap &DefM) { 977 // Push block delimiters. 978 for (auto &P : DefM) 979 P.second.start_block(B); 980 } 981 982 // Remove all definitions coming from block B from each stack in DefM. 983 void DataFlowGraph::releaseBlock(NodeId B, DefStackMap &DefM) { 984 // Pop all defs from this block from the definition stack. Defs that were 985 // added to the map during the traversal of instructions will not have a 986 // delimiter, but for those, the whole stack will be emptied. 987 for (auto &P : DefM) 988 P.second.clear_block(B); 989 990 // Finally, remove empty stacks from the map. 991 for (auto I = DefM.begin(), E = DefM.end(), NextI = I; I != E; I = NextI) { 992 NextI = std::next(I); 993 // This preserves the validity of iterators other than I. 994 if (I->second.empty()) 995 DefM.erase(I); 996 } 997 } 998 999 // Push all definitions from the instruction node IA to an appropriate 1000 // stack in DefM. 1001 void DataFlowGraph::pushAllDefs(Instr IA, DefStackMap &DefM) { 1002 pushClobbers(IA, DefM); 1003 pushDefs(IA, DefM); 1004 } 1005 1006 // Push all definitions from the instruction node IA to an appropriate 1007 // stack in DefM. 1008 void DataFlowGraph::pushClobbers(Instr IA, DefStackMap &DefM) { 1009 NodeSet Visited; 1010 std::set<RegisterId> Defined; 1011 1012 // The important objectives of this function are: 1013 // - to be able to handle instructions both while the graph is being 1014 // constructed, and after the graph has been constructed, and 1015 // - maintain proper ordering of definitions on the stack for each 1016 // register reference: 1017 // - if there are two or more related defs in IA (i.e. coming from 1018 // the same machine operand), then only push one def on the stack, 1019 // - if there are multiple unrelated defs of non-overlapping 1020 // subregisters of S, then the stack for S will have both (in an 1021 // unspecified order), but the order does not matter from the data- 1022 // -flow perspective. 1023 1024 for (Def DA : IA.Addr->members_if(IsDef, *this)) { 1025 if (Visited.count(DA.Id)) 1026 continue; 1027 if (!(DA.Addr->getFlags() & NodeAttrs::Clobbering)) 1028 continue; 1029 1030 NodeList Rel = getRelatedRefs(IA, DA); 1031 Def PDA = Rel.front(); 1032 RegisterRef RR = PDA.Addr->getRegRef(*this); 1033 1034 // Push the definition on the stack for the register and all aliases. 1035 // The def stack traversal in linkNodeUp will check the exact aliasing. 1036 DefM[RR.Reg].push(DA); 1037 Defined.insert(RR.Reg); 1038 for (RegisterId A : getPRI().getAliasSet(RR.Reg)) { 1039 // Check that we don't push the same def twice. 1040 assert(A != RR.Reg); 1041 if (!Defined.count(A)) 1042 DefM[A].push(DA); 1043 } 1044 // Mark all the related defs as visited. 1045 for (Node T : Rel) 1046 Visited.insert(T.Id); 1047 } 1048 } 1049 1050 // Push all definitions from the instruction node IA to an appropriate 1051 // stack in DefM. 1052 void DataFlowGraph::pushDefs(Instr IA, DefStackMap &DefM) { 1053 NodeSet Visited; 1054 #ifndef NDEBUG 1055 std::set<RegisterId> Defined; 1056 #endif 1057 1058 // The important objectives of this function are: 1059 // - to be able to handle instructions both while the graph is being 1060 // constructed, and after the graph has been constructed, and 1061 // - maintain proper ordering of definitions on the stack for each 1062 // register reference: 1063 // - if there are two or more related defs in IA (i.e. coming from 1064 // the same machine operand), then only push one def on the stack, 1065 // - if there are multiple unrelated defs of non-overlapping 1066 // subregisters of S, then the stack for S will have both (in an 1067 // unspecified order), but the order does not matter from the data- 1068 // -flow perspective. 1069 1070 for (Def DA : IA.Addr->members_if(IsDef, *this)) { 1071 if (Visited.count(DA.Id)) 1072 continue; 1073 if (DA.Addr->getFlags() & NodeAttrs::Clobbering) 1074 continue; 1075 1076 NodeList Rel = getRelatedRefs(IA, DA); 1077 Def PDA = Rel.front(); 1078 RegisterRef RR = PDA.Addr->getRegRef(*this); 1079 #ifndef NDEBUG 1080 // Assert if the register is defined in two or more unrelated defs. 1081 // This could happen if there are two or more def operands defining it. 1082 if (!Defined.insert(RR.Reg).second) { 1083 MachineInstr *MI = Stmt(IA).Addr->getCode(); 1084 dbgs() << "Multiple definitions of register: " << Print(RR, *this) 1085 << " in\n " << *MI << "in " << printMBBReference(*MI->getParent()) 1086 << '\n'; 1087 llvm_unreachable(nullptr); 1088 } 1089 #endif 1090 // Push the definition on the stack for the register and all aliases. 1091 // The def stack traversal in linkNodeUp will check the exact aliasing. 1092 DefM[RR.Reg].push(DA); 1093 for (RegisterId A : getPRI().getAliasSet(RR.Reg)) { 1094 // Check that we don't push the same def twice. 1095 assert(A != RR.Reg); 1096 DefM[A].push(DA); 1097 } 1098 // Mark all the related defs as visited. 1099 for (Node T : Rel) 1100 Visited.insert(T.Id); 1101 } 1102 } 1103 1104 // Return the list of all reference nodes related to RA, including RA itself. 1105 // See "getNextRelated" for the meaning of a "related reference". 1106 NodeList DataFlowGraph::getRelatedRefs(Instr IA, Ref RA) const { 1107 assert(IA.Id != 0 && RA.Id != 0); 1108 1109 NodeList Refs; 1110 NodeId Start = RA.Id; 1111 do { 1112 Refs.push_back(RA); 1113 RA = getNextRelated(IA, RA); 1114 } while (RA.Id != 0 && RA.Id != Start); 1115 return Refs; 1116 } 1117 1118 // Clear all information in the graph. 1119 void DataFlowGraph::reset() { 1120 Memory.clear(); 1121 BlockNodes.clear(); 1122 TheFunc = Func(); 1123 } 1124 1125 // Return the next reference node in the instruction node IA that is related 1126 // to RA. Conceptually, two reference nodes are related if they refer to the 1127 // same instance of a register access, but differ in flags or other minor 1128 // characteristics. Specific examples of related nodes are shadow reference 1129 // nodes. 1130 // Return the equivalent of nullptr if there are no more related references. 1131 Ref DataFlowGraph::getNextRelated(Instr IA, Ref RA) const { 1132 assert(IA.Id != 0 && RA.Id != 0); 1133 1134 auto Related = [this, RA](Ref TA) -> bool { 1135 if (TA.Addr->getKind() != RA.Addr->getKind()) 1136 return false; 1137 if (!getPRI().equal_to(TA.Addr->getRegRef(*this), 1138 RA.Addr->getRegRef(*this))) { 1139 return false; 1140 } 1141 return true; 1142 }; 1143 auto RelatedStmt = [&Related, RA](Ref TA) -> bool { 1144 return Related(TA) && &RA.Addr->getOp() == &TA.Addr->getOp(); 1145 }; 1146 auto RelatedPhi = [&Related, RA](Ref TA) -> bool { 1147 if (!Related(TA)) 1148 return false; 1149 if (TA.Addr->getKind() != NodeAttrs::Use) 1150 return true; 1151 // For phi uses, compare predecessor blocks. 1152 const NodeAddr<const PhiUseNode *> TUA = TA; 1153 const NodeAddr<const PhiUseNode *> RUA = RA; 1154 return TUA.Addr->getPredecessor() == RUA.Addr->getPredecessor(); 1155 }; 1156 1157 RegisterRef RR = RA.Addr->getRegRef(*this); 1158 if (IA.Addr->getKind() == NodeAttrs::Stmt) 1159 return RA.Addr->getNextRef(RR, RelatedStmt, true, *this); 1160 return RA.Addr->getNextRef(RR, RelatedPhi, true, *this); 1161 } 1162 1163 // Find the next node related to RA in IA that satisfies condition P. 1164 // If such a node was found, return a pair where the second element is the 1165 // located node. If such a node does not exist, return a pair where the 1166 // first element is the element after which such a node should be inserted, 1167 // and the second element is a null-address. 1168 template <typename Predicate> 1169 std::pair<Ref, Ref> DataFlowGraph::locateNextRef(Instr IA, Ref RA, 1170 Predicate P) const { 1171 assert(IA.Id != 0 && RA.Id != 0); 1172 1173 Ref NA; 1174 NodeId Start = RA.Id; 1175 while (true) { 1176 NA = getNextRelated(IA, RA); 1177 if (NA.Id == 0 || NA.Id == Start) 1178 break; 1179 if (P(NA)) 1180 break; 1181 RA = NA; 1182 } 1183 1184 if (NA.Id != 0 && NA.Id != Start) 1185 return std::make_pair(RA, NA); 1186 return std::make_pair(RA, Ref()); 1187 } 1188 1189 // Get the next shadow node in IA corresponding to RA, and optionally create 1190 // such a node if it does not exist. 1191 Ref DataFlowGraph::getNextShadow(Instr IA, Ref RA, bool Create) { 1192 assert(IA.Id != 0 && RA.Id != 0); 1193 1194 uint16_t Flags = RA.Addr->getFlags() | NodeAttrs::Shadow; 1195 auto IsShadow = [Flags](Ref TA) -> bool { 1196 return TA.Addr->getFlags() == Flags; 1197 }; 1198 auto Loc = locateNextRef(IA, RA, IsShadow); 1199 if (Loc.second.Id != 0 || !Create) 1200 return Loc.second; 1201 1202 // Create a copy of RA and mark is as shadow. 1203 Ref NA = cloneNode(RA); 1204 NA.Addr->setFlags(Flags | NodeAttrs::Shadow); 1205 IA.Addr->addMemberAfter(Loc.first, NA, *this); 1206 return NA; 1207 } 1208 1209 // Get the next shadow node in IA corresponding to RA. Return null-address 1210 // if such a node does not exist. 1211 Ref DataFlowGraph::getNextShadow(Instr IA, Ref RA) const { 1212 assert(IA.Id != 0 && RA.Id != 0); 1213 uint16_t Flags = RA.Addr->getFlags() | NodeAttrs::Shadow; 1214 auto IsShadow = [Flags](Ref TA) -> bool { 1215 return TA.Addr->getFlags() == Flags; 1216 }; 1217 return locateNextRef(IA, RA, IsShadow).second; 1218 } 1219 1220 // Create a new statement node in the block node BA that corresponds to 1221 // the machine instruction MI. 1222 void DataFlowGraph::buildStmt(Block BA, MachineInstr &In) { 1223 Stmt SA = newStmt(BA, &In); 1224 1225 auto isCall = [](const MachineInstr &In) -> bool { 1226 if (In.isCall()) 1227 return true; 1228 // Is tail call? 1229 if (In.isBranch()) { 1230 for (const MachineOperand &Op : In.operands()) 1231 if (Op.isGlobal() || Op.isSymbol()) 1232 return true; 1233 // Assume indirect branches are calls. This is for the purpose of 1234 // keeping implicit operands, and so it won't hurt on intra-function 1235 // indirect branches. 1236 if (In.isIndirectBranch()) 1237 return true; 1238 } 1239 return false; 1240 }; 1241 1242 auto isDefUndef = [this](const MachineInstr &In, RegisterRef DR) -> bool { 1243 // This instruction defines DR. Check if there is a use operand that 1244 // would make DR live on entry to the instruction. 1245 for (const MachineOperand &Op : In.all_uses()) { 1246 if (Op.getReg() == 0 || Op.isUndef()) 1247 continue; 1248 RegisterRef UR = makeRegRef(Op); 1249 if (getPRI().alias(DR, UR)) 1250 return false; 1251 } 1252 return true; 1253 }; 1254 1255 bool IsCall = isCall(In); 1256 unsigned NumOps = In.getNumOperands(); 1257 1258 // Avoid duplicate implicit defs. This will not detect cases of implicit 1259 // defs that define registers that overlap, but it is not clear how to 1260 // interpret that in the absence of explicit defs. Overlapping explicit 1261 // defs are likely illegal already. 1262 BitVector DoneDefs(TRI.getNumRegs()); 1263 // Process explicit defs first. 1264 for (unsigned OpN = 0; OpN < NumOps; ++OpN) { 1265 MachineOperand &Op = In.getOperand(OpN); 1266 if (!Op.isReg() || !Op.isDef() || Op.isImplicit()) 1267 continue; 1268 Register R = Op.getReg(); 1269 if (!R || !R.isPhysical()) 1270 continue; 1271 uint16_t Flags = NodeAttrs::None; 1272 if (TOI.isPreserving(In, OpN)) { 1273 Flags |= NodeAttrs::Preserving; 1274 // If the def is preserving, check if it is also undefined. 1275 if (isDefUndef(In, makeRegRef(Op))) 1276 Flags |= NodeAttrs::Undef; 1277 } 1278 if (TOI.isClobbering(In, OpN)) 1279 Flags |= NodeAttrs::Clobbering; 1280 if (TOI.isFixedReg(In, OpN)) 1281 Flags |= NodeAttrs::Fixed; 1282 if (IsCall && Op.isDead()) 1283 Flags |= NodeAttrs::Dead; 1284 Def DA = newDef(SA, Op, Flags); 1285 SA.Addr->addMember(DA, *this); 1286 assert(!DoneDefs.test(R)); 1287 DoneDefs.set(R); 1288 } 1289 1290 // Process reg-masks (as clobbers). 1291 BitVector DoneClobbers(TRI.getNumRegs()); 1292 for (unsigned OpN = 0; OpN < NumOps; ++OpN) { 1293 MachineOperand &Op = In.getOperand(OpN); 1294 if (!Op.isRegMask()) 1295 continue; 1296 uint16_t Flags = NodeAttrs::Clobbering | NodeAttrs::Fixed | NodeAttrs::Dead; 1297 Def DA = newDef(SA, Op, Flags); 1298 SA.Addr->addMember(DA, *this); 1299 // Record all clobbered registers in DoneDefs. 1300 const uint32_t *RM = Op.getRegMask(); 1301 for (unsigned i = 1, e = TRI.getNumRegs(); i != e; ++i) 1302 if (!(RM[i / 32] & (1u << (i % 32)))) 1303 DoneClobbers.set(i); 1304 } 1305 1306 // Process implicit defs, skipping those that have already been added 1307 // as explicit. 1308 for (unsigned OpN = 0; OpN < NumOps; ++OpN) { 1309 MachineOperand &Op = In.getOperand(OpN); 1310 if (!Op.isReg() || !Op.isDef() || !Op.isImplicit()) 1311 continue; 1312 Register R = Op.getReg(); 1313 if (!R || !R.isPhysical() || DoneDefs.test(R)) 1314 continue; 1315 RegisterRef RR = makeRegRef(Op); 1316 uint16_t Flags = NodeAttrs::None; 1317 if (TOI.isPreserving(In, OpN)) { 1318 Flags |= NodeAttrs::Preserving; 1319 // If the def is preserving, check if it is also undefined. 1320 if (isDefUndef(In, RR)) 1321 Flags |= NodeAttrs::Undef; 1322 } 1323 if (TOI.isClobbering(In, OpN)) 1324 Flags |= NodeAttrs::Clobbering; 1325 if (TOI.isFixedReg(In, OpN)) 1326 Flags |= NodeAttrs::Fixed; 1327 if (IsCall && Op.isDead()) { 1328 if (DoneClobbers.test(R)) 1329 continue; 1330 Flags |= NodeAttrs::Dead; 1331 } 1332 Def DA = newDef(SA, Op, Flags); 1333 SA.Addr->addMember(DA, *this); 1334 DoneDefs.set(R); 1335 } 1336 1337 for (unsigned OpN = 0; OpN < NumOps; ++OpN) { 1338 MachineOperand &Op = In.getOperand(OpN); 1339 if (!Op.isReg() || !Op.isUse()) 1340 continue; 1341 Register R = Op.getReg(); 1342 if (!R || !R.isPhysical()) 1343 continue; 1344 uint16_t Flags = NodeAttrs::None; 1345 if (Op.isUndef()) 1346 Flags |= NodeAttrs::Undef; 1347 if (TOI.isFixedReg(In, OpN)) 1348 Flags |= NodeAttrs::Fixed; 1349 Use UA = newUse(SA, Op, Flags); 1350 SA.Addr->addMember(UA, *this); 1351 } 1352 } 1353 1354 // Scan all defs in the block node BA and record in PhiM the locations of 1355 // phi nodes corresponding to these defs. 1356 void DataFlowGraph::recordDefsForDF(BlockRefsMap &PhiM, Block BA) { 1357 // Check all defs from block BA and record them in each block in BA's 1358 // iterated dominance frontier. This information will later be used to 1359 // create phi nodes. 1360 MachineBasicBlock *BB = BA.Addr->getCode(); 1361 assert(BB); 1362 auto DFLoc = MDF.find(BB); 1363 if (DFLoc == MDF.end() || DFLoc->second.empty()) 1364 return; 1365 1366 // Traverse all instructions in the block and collect the set of all 1367 // defined references. For each reference there will be a phi created 1368 // in the block's iterated dominance frontier. 1369 // This is done to make sure that each defined reference gets only one 1370 // phi node, even if it is defined multiple times. 1371 RegisterAggr Defs(getPRI()); 1372 for (Instr IA : BA.Addr->members(*this)) 1373 for (Ref RA : IA.Addr->members_if(IsDef, *this)) 1374 Defs.insert(RA.Addr->getRegRef(*this)); 1375 1376 // Calculate the iterated dominance frontier of BB. 1377 const MachineDominanceFrontier::DomSetType &DF = DFLoc->second; 1378 SetVector<MachineBasicBlock *> IDF(DF.begin(), DF.end()); 1379 for (unsigned i = 0; i < IDF.size(); ++i) { 1380 auto F = MDF.find(IDF[i]); 1381 if (F != MDF.end()) 1382 IDF.insert(F->second.begin(), F->second.end()); 1383 } 1384 1385 // Finally, add the set of defs to each block in the iterated dominance 1386 // frontier. 1387 for (auto *DB : IDF) { 1388 Block DBA = findBlock(DB); 1389 PhiM[DBA.Id].insert(Defs); 1390 } 1391 } 1392 1393 // Given the locations of phi nodes in the map PhiM, create the phi nodes 1394 // that are located in the block node BA. 1395 void DataFlowGraph::buildPhis(BlockRefsMap &PhiM, RegisterSet &AllRefs, 1396 Block BA) { 1397 // Check if this blocks has any DF defs, i.e. if there are any defs 1398 // that this block is in the iterated dominance frontier of. 1399 auto HasDF = PhiM.find(BA.Id); 1400 if (HasDF == PhiM.end() || HasDF->second.empty()) 1401 return; 1402 1403 // Prepare a list of NodeIds of the block's predecessors. 1404 NodeList Preds; 1405 const MachineBasicBlock *MBB = BA.Addr->getCode(); 1406 for (MachineBasicBlock *PB : MBB->predecessors()) 1407 Preds.push_back(findBlock(PB)); 1408 1409 const RegisterAggr &Defs = PhiM[BA.Id]; 1410 uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving; 1411 1412 for (RegisterRef RR : Defs.refs()) { 1413 Phi PA = newPhi(BA); 1414 PA.Addr->addMember(newDef(PA, RR, PhiFlags), *this); 1415 1416 // Add phi uses. 1417 for (Block PBA : Preds) { 1418 PA.Addr->addMember(newPhiUse(PA, RR, PBA), *this); 1419 } 1420 } 1421 } 1422 1423 // Remove any unneeded phi nodes that were created during the build process. 1424 void DataFlowGraph::removeUnusedPhis() { 1425 // This will remove unused phis, i.e. phis where each def does not reach 1426 // any uses or other defs. This will not detect or remove circular phi 1427 // chains that are otherwise dead. Unused/dead phis are created during 1428 // the build process and this function is intended to remove these cases 1429 // that are easily determinable to be unnecessary. 1430 1431 SetVector<NodeId> PhiQ; 1432 for (Block BA : TheFunc.Addr->members(*this)) { 1433 for (auto P : BA.Addr->members_if(IsPhi, *this)) 1434 PhiQ.insert(P.Id); 1435 } 1436 1437 static auto HasUsedDef = [](NodeList &Ms) -> bool { 1438 for (Node M : Ms) { 1439 if (M.Addr->getKind() != NodeAttrs::Def) 1440 continue; 1441 Def DA = M; 1442 if (DA.Addr->getReachedDef() != 0 || DA.Addr->getReachedUse() != 0) 1443 return true; 1444 } 1445 return false; 1446 }; 1447 1448 // Any phi, if it is removed, may affect other phis (make them dead). 1449 // For each removed phi, collect the potentially affected phis and add 1450 // them back to the queue. 1451 while (!PhiQ.empty()) { 1452 auto PA = addr<PhiNode *>(PhiQ[0]); 1453 PhiQ.remove(PA.Id); 1454 NodeList Refs = PA.Addr->members(*this); 1455 if (HasUsedDef(Refs)) 1456 continue; 1457 for (Ref RA : Refs) { 1458 if (NodeId RD = RA.Addr->getReachingDef()) { 1459 auto RDA = addr<DefNode *>(RD); 1460 Instr OA = RDA.Addr->getOwner(*this); 1461 if (IsPhi(OA)) 1462 PhiQ.insert(OA.Id); 1463 } 1464 if (RA.Addr->isDef()) 1465 unlinkDef(RA, true); 1466 else 1467 unlinkUse(RA, true); 1468 } 1469 Block BA = PA.Addr->getOwner(*this); 1470 BA.Addr->removeMember(PA, *this); 1471 } 1472 } 1473 1474 // For a given reference node TA in an instruction node IA, connect the 1475 // reaching def of TA to the appropriate def node. Create any shadow nodes 1476 // as appropriate. 1477 template <typename T> 1478 void DataFlowGraph::linkRefUp(Instr IA, NodeAddr<T> TA, DefStack &DS) { 1479 if (DS.empty()) 1480 return; 1481 RegisterRef RR = TA.Addr->getRegRef(*this); 1482 NodeAddr<T> TAP; 1483 1484 // References from the def stack that have been examined so far. 1485 RegisterAggr Defs(getPRI()); 1486 1487 for (auto I = DS.top(), E = DS.bottom(); I != E; I.down()) { 1488 RegisterRef QR = I->Addr->getRegRef(*this); 1489 1490 // Skip all defs that are aliased to any of the defs that we have already 1491 // seen. If this completes a cover of RR, stop the stack traversal. 1492 bool Alias = Defs.hasAliasOf(QR); 1493 bool Cover = Defs.insert(QR).hasCoverOf(RR); 1494 if (Alias) { 1495 if (Cover) 1496 break; 1497 continue; 1498 } 1499 1500 // The reaching def. 1501 Def RDA = *I; 1502 1503 // Pick the reached node. 1504 if (TAP.Id == 0) { 1505 TAP = TA; 1506 } else { 1507 // Mark the existing ref as "shadow" and create a new shadow. 1508 TAP.Addr->setFlags(TAP.Addr->getFlags() | NodeAttrs::Shadow); 1509 TAP = getNextShadow(IA, TAP, true); 1510 } 1511 1512 // Create the link. 1513 TAP.Addr->linkToDef(TAP.Id, RDA); 1514 1515 if (Cover) 1516 break; 1517 } 1518 } 1519 1520 // Create data-flow links for all reference nodes in the statement node SA. 1521 template <typename Predicate> 1522 void DataFlowGraph::linkStmtRefs(DefStackMap &DefM, Stmt SA, Predicate P) { 1523 #ifndef NDEBUG 1524 RegisterSet Defs(getPRI()); 1525 #endif 1526 1527 // Link all nodes (upwards in the data-flow) with their reaching defs. 1528 for (Ref RA : SA.Addr->members_if(P, *this)) { 1529 uint16_t Kind = RA.Addr->getKind(); 1530 assert(Kind == NodeAttrs::Def || Kind == NodeAttrs::Use); 1531 RegisterRef RR = RA.Addr->getRegRef(*this); 1532 #ifndef NDEBUG 1533 // Do not expect multiple defs of the same reference. 1534 assert(Kind != NodeAttrs::Def || !Defs.count(RR)); 1535 Defs.insert(RR); 1536 #endif 1537 1538 auto F = DefM.find(RR.Reg); 1539 if (F == DefM.end()) 1540 continue; 1541 DefStack &DS = F->second; 1542 if (Kind == NodeAttrs::Use) 1543 linkRefUp<UseNode *>(SA, RA, DS); 1544 else if (Kind == NodeAttrs::Def) 1545 linkRefUp<DefNode *>(SA, RA, DS); 1546 else 1547 llvm_unreachable("Unexpected node in instruction"); 1548 } 1549 } 1550 1551 // Create data-flow links for all instructions in the block node BA. This 1552 // will include updating any phi nodes in BA. 1553 void DataFlowGraph::linkBlockRefs(DefStackMap &DefM, Block BA) { 1554 // Push block delimiters. 1555 markBlock(BA.Id, DefM); 1556 1557 auto IsClobber = [](Ref RA) -> bool { 1558 return IsDef(RA) && (RA.Addr->getFlags() & NodeAttrs::Clobbering); 1559 }; 1560 auto IsNoClobber = [](Ref RA) -> bool { 1561 return IsDef(RA) && !(RA.Addr->getFlags() & NodeAttrs::Clobbering); 1562 }; 1563 1564 assert(BA.Addr && "block node address is needed to create a data-flow link"); 1565 // For each non-phi instruction in the block, link all the defs and uses 1566 // to their reaching defs. For any member of the block (including phis), 1567 // push the defs on the corresponding stacks. 1568 for (Instr IA : BA.Addr->members(*this)) { 1569 // Ignore phi nodes here. They will be linked part by part from the 1570 // predecessors. 1571 if (IA.Addr->getKind() == NodeAttrs::Stmt) { 1572 linkStmtRefs(DefM, IA, IsUse); 1573 linkStmtRefs(DefM, IA, IsClobber); 1574 } 1575 1576 // Push the definitions on the stack. 1577 pushClobbers(IA, DefM); 1578 1579 if (IA.Addr->getKind() == NodeAttrs::Stmt) 1580 linkStmtRefs(DefM, IA, IsNoClobber); 1581 1582 pushDefs(IA, DefM); 1583 } 1584 1585 // Recursively process all children in the dominator tree. 1586 MachineDomTreeNode *N = MDT.getNode(BA.Addr->getCode()); 1587 for (auto *I : *N) { 1588 MachineBasicBlock *SB = I->getBlock(); 1589 Block SBA = findBlock(SB); 1590 linkBlockRefs(DefM, SBA); 1591 } 1592 1593 // Link the phi uses from the successor blocks. 1594 auto IsUseForBA = [BA](Node NA) -> bool { 1595 if (NA.Addr->getKind() != NodeAttrs::Use) 1596 return false; 1597 assert(NA.Addr->getFlags() & NodeAttrs::PhiRef); 1598 PhiUse PUA = NA; 1599 return PUA.Addr->getPredecessor() == BA.Id; 1600 }; 1601 1602 RegisterAggr EHLiveIns = getLandingPadLiveIns(); 1603 MachineBasicBlock *MBB = BA.Addr->getCode(); 1604 1605 for (MachineBasicBlock *SB : MBB->successors()) { 1606 bool IsEHPad = SB->isEHPad(); 1607 Block SBA = findBlock(SB); 1608 for (Instr IA : SBA.Addr->members_if(IsPhi, *this)) { 1609 // Do not link phi uses for landing pad live-ins. 1610 if (IsEHPad) { 1611 // Find what register this phi is for. 1612 Ref RA = IA.Addr->getFirstMember(*this); 1613 assert(RA.Id != 0); 1614 if (EHLiveIns.hasCoverOf(RA.Addr->getRegRef(*this))) 1615 continue; 1616 } 1617 // Go over each phi use associated with MBB, and link it. 1618 for (auto U : IA.Addr->members_if(IsUseForBA, *this)) { 1619 PhiUse PUA = U; 1620 RegisterRef RR = PUA.Addr->getRegRef(*this); 1621 linkRefUp<UseNode *>(IA, PUA, DefM[RR.Reg]); 1622 } 1623 } 1624 } 1625 1626 // Pop all defs from this block from the definition stacks. 1627 releaseBlock(BA.Id, DefM); 1628 } 1629 1630 // Remove the use node UA from any data-flow and structural links. 1631 void DataFlowGraph::unlinkUseDF(Use UA) { 1632 NodeId RD = UA.Addr->getReachingDef(); 1633 NodeId Sib = UA.Addr->getSibling(); 1634 1635 if (RD == 0) { 1636 assert(Sib == 0); 1637 return; 1638 } 1639 1640 auto RDA = addr<DefNode *>(RD); 1641 auto TA = addr<UseNode *>(RDA.Addr->getReachedUse()); 1642 if (TA.Id == UA.Id) { 1643 RDA.Addr->setReachedUse(Sib); 1644 return; 1645 } 1646 1647 while (TA.Id != 0) { 1648 NodeId S = TA.Addr->getSibling(); 1649 if (S == UA.Id) { 1650 TA.Addr->setSibling(UA.Addr->getSibling()); 1651 return; 1652 } 1653 TA = addr<UseNode *>(S); 1654 } 1655 } 1656 1657 // Remove the def node DA from any data-flow and structural links. 1658 void DataFlowGraph::unlinkDefDF(Def DA) { 1659 // 1660 // RD 1661 // | reached 1662 // | def 1663 // : 1664 // . 1665 // +----+ 1666 // ... -- | DA | -- ... -- 0 : sibling chain of DA 1667 // +----+ 1668 // | | reached 1669 // | : def 1670 // | . 1671 // | ... : Siblings (defs) 1672 // | 1673 // : reached 1674 // . use 1675 // ... : sibling chain of reached uses 1676 1677 NodeId RD = DA.Addr->getReachingDef(); 1678 1679 // Visit all siblings of the reached def and reset their reaching defs. 1680 // Also, defs reached by DA are now "promoted" to being reached by RD, 1681 // so all of them will need to be spliced into the sibling chain where 1682 // DA belongs. 1683 auto getAllNodes = [this](NodeId N) -> NodeList { 1684 NodeList Res; 1685 while (N) { 1686 auto RA = addr<RefNode *>(N); 1687 // Keep the nodes in the exact sibling order. 1688 Res.push_back(RA); 1689 N = RA.Addr->getSibling(); 1690 } 1691 return Res; 1692 }; 1693 NodeList ReachedDefs = getAllNodes(DA.Addr->getReachedDef()); 1694 NodeList ReachedUses = getAllNodes(DA.Addr->getReachedUse()); 1695 1696 if (RD == 0) { 1697 for (Ref I : ReachedDefs) 1698 I.Addr->setSibling(0); 1699 for (Ref I : ReachedUses) 1700 I.Addr->setSibling(0); 1701 } 1702 for (Def I : ReachedDefs) 1703 I.Addr->setReachingDef(RD); 1704 for (Use I : ReachedUses) 1705 I.Addr->setReachingDef(RD); 1706 1707 NodeId Sib = DA.Addr->getSibling(); 1708 if (RD == 0) { 1709 assert(Sib == 0); 1710 return; 1711 } 1712 1713 // Update the reaching def node and remove DA from the sibling list. 1714 auto RDA = addr<DefNode *>(RD); 1715 auto TA = addr<DefNode *>(RDA.Addr->getReachedDef()); 1716 if (TA.Id == DA.Id) { 1717 // If DA is the first reached def, just update the RD's reached def 1718 // to the DA's sibling. 1719 RDA.Addr->setReachedDef(Sib); 1720 } else { 1721 // Otherwise, traverse the sibling list of the reached defs and remove 1722 // DA from it. 1723 while (TA.Id != 0) { 1724 NodeId S = TA.Addr->getSibling(); 1725 if (S == DA.Id) { 1726 TA.Addr->setSibling(Sib); 1727 break; 1728 } 1729 TA = addr<DefNode *>(S); 1730 } 1731 } 1732 1733 // Splice the DA's reached defs into the RDA's reached def chain. 1734 if (!ReachedDefs.empty()) { 1735 auto Last = Def(ReachedDefs.back()); 1736 Last.Addr->setSibling(RDA.Addr->getReachedDef()); 1737 RDA.Addr->setReachedDef(ReachedDefs.front().Id); 1738 } 1739 // Splice the DA's reached uses into the RDA's reached use chain. 1740 if (!ReachedUses.empty()) { 1741 auto Last = Use(ReachedUses.back()); 1742 Last.Addr->setSibling(RDA.Addr->getReachedUse()); 1743 RDA.Addr->setReachedUse(ReachedUses.front().Id); 1744 } 1745 } 1746