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