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