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