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