Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 1 | //===--- RDFGraph.h -------------------------------------------------------===// |
| 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 | // for a non-SSA program representation (e.g. post-RA machine code). |
| 12 | // |
| 13 | // |
| 14 | // *** Introduction |
| 15 | // |
| 16 | // The RDF graph is a collection of nodes, each of which denotes some element |
| 17 | // of the program. There are two main types of such elements: code and refe- |
| 18 | // rences. Conceptually, "code" is something that represents the structure |
| 19 | // of the program, e.g. basic block or a statement, while "reference" is an |
| 20 | // instance of accessing a register, e.g. a definition or a use. Nodes are |
| 21 | // connected with each other based on the structure of the program (such as |
| 22 | // blocks, instructions, etc.), and based on the data flow (e.g. reaching |
| 23 | // definitions, reached uses, etc.). The single-reaching-definition principle |
| 24 | // of SSA is generally observed, although, due to the non-SSA representation |
| 25 | // of the program, there are some differences between the graph and a "pure" |
| 26 | // SSA representation. |
| 27 | // |
| 28 | // |
| 29 | // *** Implementation remarks |
| 30 | // |
| 31 | // Since the graph can contain a large number of nodes, memory consumption |
| 32 | // was one of the major design considerations. As a result, there is a single |
| 33 | // base class NodeBase which defines all members used by all possible derived |
| 34 | // classes. The members are arranged in a union, and a derived class cannot |
| 35 | // add any data members of its own. Each derived class only defines the |
| 36 | // functional interface, i.e. member functions. NodeBase must be a POD, |
| 37 | // which implies that all of its members must also be PODs. |
| 38 | // Since nodes need to be connected with other nodes, pointers have been |
| 39 | // replaced with 32-bit identifiers: each node has an id of type NodeId. |
| 40 | // There are mapping functions in the graph that translate between actual |
| 41 | // memory addresses and the corresponding identifiers. |
| 42 | // A node id of 0 is equivalent to nullptr. |
| 43 | // |
| 44 | // |
| 45 | // *** Structure of the graph |
| 46 | // |
| 47 | // A code node is always a collection of other nodes. For example, a code |
| 48 | // node corresponding to a basic block will contain code nodes corresponding |
| 49 | // to instructions. In turn, a code node corresponding to an instruction will |
| 50 | // contain a list of reference nodes that correspond to the definitions and |
| 51 | // uses of registers in that instruction. The members are arranged into a |
| 52 | // circular list, which is yet another consequence of the effort to save |
| 53 | // memory: for each member node it should be possible to obtain its owner, |
| 54 | // and it should be possible to access all other members. There are other |
| 55 | // ways to accomplish that, but the circular list seemed the most natural. |
| 56 | // |
| 57 | // +- CodeNode -+ |
| 58 | // | | <---------------------------------------------------+ |
| 59 | // +-+--------+-+ | |
| 60 | // |FirstM |LastM | |
| 61 | // | +-------------------------------------+ | |
| 62 | // | | | |
| 63 | // V V | |
| 64 | // +----------+ Next +----------+ Next Next +----------+ Next | |
| 65 | // | |----->| |-----> ... ----->| |----->-+ |
| 66 | // +- Member -+ +- Member -+ +- Member -+ |
| 67 | // |
| 68 | // The order of members is such that related reference nodes (see below) |
| 69 | // should be contiguous on the member list. |
| 70 | // |
| 71 | // A reference node is a node that encapsulates an access to a register, |
| 72 | // in other words, data flowing into or out of a register. There are two |
| 73 | // major kinds of reference nodes: defs and uses. A def node will contain |
| 74 | // the id of the first reached use, and the id of the first reached def. |
| 75 | // Each def and use will contain the id of the reaching def, and also the |
| 76 | // id of the next reached def (for def nodes) or use (for use nodes). |
| 77 | // The "next node sharing the same reaching def" is denoted as "sibling". |
| 78 | // In summary: |
| 79 | // - Def node contains: reaching def, sibling, first reached def, and first |
| 80 | // reached use. |
| 81 | // - Use node contains: reaching def and sibling. |
| 82 | // |
| 83 | // +-- DefNode --+ |
| 84 | // | R2 = ... | <---+--------------------+ |
| 85 | // ++---------+--+ | | |
| 86 | // |Reached |Reached | | |
| 87 | // |Def |Use | | |
| 88 | // | | |Reaching |Reaching |
| 89 | // | V |Def |Def |
| 90 | // | +-- UseNode --+ Sib +-- UseNode --+ Sib Sib |
| 91 | // | | ... = R2 |----->| ... = R2 |----> ... ----> 0 |
| 92 | // | +-------------+ +-------------+ |
| 93 | // V |
| 94 | // +-- DefNode --+ Sib |
| 95 | // | R2 = ... |----> ... |
| 96 | // ++---------+--+ |
| 97 | // | | |
| 98 | // | | |
| 99 | // ... ... |
| 100 | // |
| 101 | // To get a full picture, the circular lists connecting blocks within a |
| 102 | // function, instructions within a block, etc. should be superimposed with |
| 103 | // the def-def, def-use links shown above. |
| 104 | // To illustrate this, consider a small example in a pseudo-assembly: |
| 105 | // foo: |
| 106 | // add r2, r0, r1 ; r2 = r0+r1 |
| 107 | // addi r0, r2, 1 ; r0 = r2+1 |
| 108 | // ret r0 ; return value in r0 |
| 109 | // |
| 110 | // The graph (in a format used by the debugging functions) would look like: |
| 111 | // |
| 112 | // DFG dump:[ |
| 113 | // f1: Function foo |
| 114 | // b2: === BB#0 === preds(0), succs(0): |
| 115 | // p3: phi [d4<r0>(,d12,u9):] |
| 116 | // p5: phi [d6<r1>(,,u10):] |
| 117 | // s7: add [d8<r2>(,,u13):, u9<r0>(d4):, u10<r1>(d6):] |
| 118 | // s11: addi [d12<r0>(d4,,u15):, u13<r2>(d8):] |
| 119 | // s14: ret [u15<r0>(d12):] |
| 120 | // ] |
| 121 | // |
| 122 | // The f1, b2, p3, etc. are node ids. The letter is prepended to indicate the |
| 123 | // kind of the node (i.e. f - function, b - basic block, p - phi, s - state- |
| 124 | // ment, d - def, u - use). |
| 125 | // The format of a def node is: |
| 126 | // dN<R>(rd,d,u):sib, |
| 127 | // where |
| 128 | // N - numeric node id, |
| 129 | // R - register being defined |
| 130 | // rd - reaching def, |
| 131 | // d - reached def, |
| 132 | // u - reached use, |
| 133 | // sib - sibling. |
| 134 | // The format of a use node is: |
| 135 | // uN<R>[!](rd):sib, |
| 136 | // where |
| 137 | // N - numeric node id, |
| 138 | // R - register being used, |
| 139 | // rd - reaching def, |
| 140 | // sib - sibling. |
| 141 | // Possible annotations (usually preceding the node id): |
| 142 | // + - preserving def, |
| 143 | // ~ - clobbering def, |
| 144 | // " - shadow ref (follows the node id), |
| 145 | // ! - fixed register (appears after register name). |
| 146 | // |
| 147 | // The circular lists are not explicit in the dump. |
| 148 | // |
| 149 | // |
| 150 | // *** Node attributes |
| 151 | // |
| 152 | // NodeBase has a member "Attrs", which is the primary way of determining |
| 153 | // the node's characteristics. The fields in this member decide whether |
| 154 | // the node is a code node or a reference node (i.e. node's "type"), then |
| 155 | // within each type, the "kind" determines what specifically this node |
| 156 | // represents. The remaining bits, "flags", contain additional information |
| 157 | // that is even more detailed than the "kind". |
| 158 | // CodeNode's kinds are: |
| 159 | // - Phi: Phi node, members are reference nodes. |
| 160 | // - Stmt: Statement, members are reference nodes. |
| 161 | // - Block: Basic block, members are instruction nodes (i.e. Phi or Stmt). |
| 162 | // - Func: The whole function. The members are basic block nodes. |
| 163 | // RefNode's kinds are: |
| 164 | // - Use. |
| 165 | // - Def. |
| 166 | // |
| 167 | // Meaning of flags: |
| 168 | // - Preserving: applies only to defs. A preserving def is one that can |
| 169 | // preserve some of the original bits among those that are included in |
| 170 | // the register associated with that def. For example, if R0 is a 32-bit |
| 171 | // register, but a def can only change the lower 16 bits, then it will |
| 172 | // be marked as preserving. |
| 173 | // - Shadow: a reference that has duplicates holding additional reaching |
| 174 | // defs (see more below). |
| 175 | // - Clobbering: applied only to defs, indicates that the value generated |
| 176 | // by this def is unspecified. A typical example would be volatile registers |
| 177 | // after function calls. |
Krzysztof Parzyszek | 1ff9952 | 2016-09-07 20:10:56 +0000 | [diff] [blame] | 178 | // - Fixed: the register in this def/use cannot be replaced with any other |
| 179 | // register. A typical case would be a parameter register to a call, or |
| 180 | // the register with the return value from a function. |
| 181 | // - Undef: the register in this reference the register is assumed to have |
| 182 | // no pre-existing value, even if it appears to be reached by some def. |
| 183 | // This is typically used to prevent keeping registers artificially live |
| 184 | // in cases when they are defined via predicated instructions. For example: |
| 185 | // r0 = add-if-true cond, r10, r11 (1) |
| 186 | // r0 = add-if-false cond, r12, r13, r0<imp-use> (2) |
| 187 | // ... = r0 (3) |
| 188 | // Before (1), r0 is not intended to be live, and the use of r0 in (3) is |
| 189 | // not meant to be reached by any def preceding (1). However, since the |
| 190 | // defs in (1) and (2) are both preserving, these properties alone would |
| 191 | // imply that the use in (3) may indeed be reached by some prior def. |
| 192 | // Adding Undef flag to the def in (1) prevents that. The Undef flag |
| 193 | // may be applied to both defs and uses. |
Krzysztof Parzyszek | 586fc12 | 2016-09-27 18:24:33 +0000 | [diff] [blame] | 194 | // - Dead: applies only to defs. The value coming out of a "dead" def is |
| 195 | // assumed to be unused, even if the def appears to be reaching other defs |
| 196 | // or uses. The motivation for this flag comes from dead defs on function |
| 197 | // calls: there is no way to determine if such a def is dead without |
| 198 | // analyzing the target's ABI. Hence the graph should contain this info, |
| 199 | // as it is unavailable otherwise. On the other hand, a def without any |
| 200 | // uses on a typical instruction is not the intended target for this flag. |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 201 | // |
| 202 | // *** Shadow references |
| 203 | // |
| 204 | // It may happen that a super-register can have two (or more) non-overlapping |
| 205 | // sub-registers. When both of these sub-registers are defined and followed |
| 206 | // by a use of the super-register, the use of the super-register will not |
| 207 | // have a unique reaching def: both defs of the sub-registers need to be |
| 208 | // accounted for. In such cases, a duplicate use of the super-register is |
| 209 | // added and it points to the extra reaching def. Both uses are marked with |
| 210 | // a flag "shadow". Example: |
| 211 | // Assume t0 is a super-register of r0 and r1, r0 and r1 do not overlap: |
| 212 | // set r0, 1 ; r0 = 1 |
| 213 | // set r1, 1 ; r1 = 1 |
| 214 | // addi t1, t0, 1 ; t1 = t0+1 |
| 215 | // |
| 216 | // The DFG: |
| 217 | // s1: set [d2<r0>(,,u9):] |
| 218 | // s3: set [d4<r1>(,,u10):] |
| 219 | // s5: addi [d6<t1>(,,):, u7"<t0>(d2):, u8"<t0>(d4):] |
| 220 | // |
| 221 | // The statement s5 has two use nodes for t0: u7" and u9". The quotation |
| 222 | // mark " indicates that the node is a shadow. |
| 223 | // |
| 224 | #ifndef RDF_GRAPH_H |
| 225 | #define RDF_GRAPH_H |
| 226 | |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 227 | #include "llvm/ADT/BitVector.h" |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 228 | #include "llvm/Support/Allocator.h" |
| 229 | #include "llvm/Support/Debug.h" |
| 230 | #include "llvm/Support/raw_ostream.h" |
| 231 | #include "llvm/Support/Timer.h" |
Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 232 | #include "llvm/Target/TargetRegisterInfo.h" |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 233 | |
| 234 | #include <functional> |
| 235 | #include <map> |
| 236 | #include <set> |
Krzysztof Parzyszek | 047149f | 2016-07-22 16:09:47 +0000 | [diff] [blame] | 237 | #include <unordered_map> |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 238 | #include <vector> |
| 239 | |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 240 | // RDF uses uint32_t to refer to registers. This is to ensure that the type |
| 241 | // size remains specific. In other places, registers are often stored using |
| 242 | // unsigned. |
| 243 | static_assert(sizeof(uint32_t) == sizeof(unsigned), "Those should be equal"); |
| 244 | |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 245 | namespace llvm { |
| 246 | class MachineBasicBlock; |
| 247 | class MachineFunction; |
| 248 | class MachineInstr; |
| 249 | class MachineOperand; |
| 250 | class MachineDominanceFrontier; |
| 251 | class MachineDominatorTree; |
| 252 | class TargetInstrInfo; |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 253 | |
| 254 | namespace rdf { |
| 255 | typedef uint32_t NodeId; |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 256 | typedef uint32_t RegisterId; |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 257 | |
Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 258 | struct DataFlowGraph; |
| 259 | |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 260 | struct NodeAttrs { |
| 261 | enum : uint16_t { |
| 262 | None = 0x0000, // Nothing |
| 263 | |
| 264 | // Types: 2 bits |
| 265 | TypeMask = 0x0003, |
| 266 | Code = 0x0001, // 01, Container |
| 267 | Ref = 0x0002, // 10, Reference |
| 268 | |
| 269 | // Kind: 3 bits |
| 270 | KindMask = 0x0007 << 2, |
| 271 | Def = 0x0001 << 2, // 001 |
| 272 | Use = 0x0002 << 2, // 010 |
| 273 | Phi = 0x0003 << 2, // 011 |
| 274 | Stmt = 0x0004 << 2, // 100 |
| 275 | Block = 0x0005 << 2, // 101 |
| 276 | Func = 0x0006 << 2, // 110 |
| 277 | |
Krzysztof Parzyszek | 586fc12 | 2016-09-27 18:24:33 +0000 | [diff] [blame] | 278 | // Flags: 7 bits for now |
| 279 | FlagMask = 0x007F << 5, |
| 280 | Shadow = 0x0001 << 5, // 0000001, Has extra reaching defs. |
| 281 | Clobbering = 0x0002 << 5, // 0000010, Produces unspecified values. |
| 282 | PhiRef = 0x0004 << 5, // 0000100, Member of PhiNode. |
| 283 | Preserving = 0x0008 << 5, // 0001000, Def can keep original bits. |
| 284 | Fixed = 0x0010 << 5, // 0010000, Fixed register. |
| 285 | Undef = 0x0020 << 5, // 0100000, Has no pre-existing value. |
| 286 | Dead = 0x0040 << 5, // 1000000, Does not define a value. |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 287 | }; |
| 288 | |
| 289 | static uint16_t type(uint16_t T) { return T & TypeMask; } |
| 290 | static uint16_t kind(uint16_t T) { return T & KindMask; } |
| 291 | static uint16_t flags(uint16_t T) { return T & FlagMask; } |
| 292 | |
| 293 | static uint16_t set_type(uint16_t A, uint16_t T) { |
| 294 | return (A & ~TypeMask) | T; |
| 295 | } |
| 296 | static uint16_t set_kind(uint16_t A, uint16_t K) { |
| 297 | return (A & ~KindMask) | K; |
| 298 | } |
| 299 | static uint16_t set_flags(uint16_t A, uint16_t F) { |
| 300 | return (A & ~FlagMask) | F; |
| 301 | } |
| 302 | |
| 303 | // Test if A contains B. |
| 304 | static bool contains(uint16_t A, uint16_t B) { |
| 305 | if (type(A) != Code) |
| 306 | return false; |
| 307 | uint16_t KB = kind(B); |
| 308 | switch (kind(A)) { |
| 309 | case Func: |
| 310 | return KB == Block; |
| 311 | case Block: |
| 312 | return KB == Phi || KB == Stmt; |
| 313 | case Phi: |
| 314 | case Stmt: |
| 315 | return type(B) == Ref; |
| 316 | } |
| 317 | return false; |
| 318 | } |
| 319 | }; |
| 320 | |
Krzysztof Parzyszek | 55874cf | 2016-04-28 20:17:06 +0000 | [diff] [blame] | 321 | struct BuildOptions { |
| 322 | enum : unsigned { |
| 323 | None = 0x00, |
| 324 | KeepDeadPhis = 0x01, // Do not remove dead phis during build. |
| 325 | }; |
| 326 | }; |
| 327 | |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 328 | template <typename T> struct NodeAddr { |
| 329 | NodeAddr() : Addr(nullptr), Id(0) {} |
| 330 | NodeAddr(T A, NodeId I) : Addr(A), Id(I) {} |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 331 | |
| 332 | bool operator== (const NodeAddr<T> &NA) const { |
| 333 | assert((Addr == NA.Addr) == (Id == NA.Id)); |
| 334 | return Addr == NA.Addr; |
| 335 | } |
| 336 | bool operator!= (const NodeAddr<T> &NA) const { |
| 337 | return !operator==(NA); |
| 338 | } |
| 339 | // Type cast (casting constructor). The reason for having this class |
| 340 | // instead of std::pair. |
| 341 | template <typename S> NodeAddr(const NodeAddr<S> &NA) |
| 342 | : Addr(static_cast<T>(NA.Addr)), Id(NA.Id) {} |
| 343 | |
| 344 | T Addr; |
| 345 | NodeId Id; |
| 346 | }; |
| 347 | |
| 348 | struct NodeBase; |
| 349 | |
| 350 | // Fast memory allocation and translation between node id and node address. |
| 351 | // This is really the same idea as the one underlying the "bump pointer |
| 352 | // allocator", the difference being in the translation. A node id is |
| 353 | // composed of two components: the index of the block in which it was |
| 354 | // allocated, and the index within the block. With the default settings, |
| 355 | // where the number of nodes per block is 4096, the node id (minus 1) is: |
| 356 | // |
| 357 | // bit position: 11 0 |
| 358 | // +----------------------------+--------------+ |
| 359 | // | Index of the block |Index in block| |
| 360 | // +----------------------------+--------------+ |
| 361 | // |
| 362 | // The actual node id is the above plus 1, to avoid creating a node id of 0. |
| 363 | // |
| 364 | // This method significantly improved the build time, compared to using maps |
| 365 | // (std::unordered_map or DenseMap) to translate between pointers and ids. |
| 366 | struct NodeAllocator { |
| 367 | // Amount of storage for a single node. |
| 368 | enum { NodeMemSize = 32 }; |
| 369 | NodeAllocator(uint32_t NPB = 4096) |
| 370 | : NodesPerBlock(NPB), BitsPerIndex(Log2_32(NPB)), |
| 371 | IndexMask((1 << BitsPerIndex)-1), ActiveEnd(nullptr) { |
| 372 | assert(isPowerOf2_32(NPB)); |
| 373 | } |
| 374 | NodeBase *ptr(NodeId N) const { |
| 375 | uint32_t N1 = N-1; |
| 376 | uint32_t BlockN = N1 >> BitsPerIndex; |
| 377 | uint32_t Offset = (N1 & IndexMask) * NodeMemSize; |
| 378 | return reinterpret_cast<NodeBase*>(Blocks[BlockN]+Offset); |
| 379 | } |
| 380 | NodeId id(const NodeBase *P) const; |
| 381 | NodeAddr<NodeBase*> New(); |
| 382 | void clear(); |
| 383 | |
| 384 | private: |
| 385 | void startNewBlock(); |
| 386 | bool needNewBlock(); |
| 387 | uint32_t makeId(uint32_t Block, uint32_t Index) const { |
| 388 | // Add 1 to the id, to avoid the id of 0, which is treated as "null". |
| 389 | return ((Block << BitsPerIndex) | Index) + 1; |
| 390 | } |
| 391 | |
| 392 | const uint32_t NodesPerBlock; |
| 393 | const uint32_t BitsPerIndex; |
| 394 | const uint32_t IndexMask; |
| 395 | char *ActiveEnd; |
| 396 | std::vector<char*> Blocks; |
| 397 | typedef BumpPtrAllocatorImpl<MallocAllocator, 65536> AllocatorTy; |
| 398 | AllocatorTy MemPool; |
| 399 | }; |
| 400 | |
| 401 | struct RegisterRef { |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 402 | RegisterId Reg; |
| 403 | LaneBitmask Mask; |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 404 | |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 405 | RegisterRef() : RegisterRef(0) {} |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame^] | 406 | explicit RegisterRef(RegisterId R, LaneBitmask M = LaneBitmask::getAll()) |
| 407 | : Reg(R), Mask(R != 0 ? M : LaneBitmask::getNone()) {} |
| 408 | operator bool() const { return Reg != 0 && !Mask.none(); } |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 409 | bool operator== (const RegisterRef &RR) const { |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 410 | return Reg == RR.Reg && Mask == RR.Mask; |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 411 | } |
| 412 | bool operator!= (const RegisterRef &RR) const { |
| 413 | return !operator==(RR); |
| 414 | } |
| 415 | bool operator< (const RegisterRef &RR) const { |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 416 | return Reg < RR.Reg || (Reg == RR.Reg && Mask < RR.Mask); |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 417 | } |
| 418 | }; |
| 419 | typedef std::set<RegisterRef> RegisterSet; |
| 420 | |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 421 | struct TargetOperandInfo { |
| 422 | TargetOperandInfo(const TargetInstrInfo &tii) : TII(tii) {} |
| 423 | virtual ~TargetOperandInfo() {} |
| 424 | virtual bool isPreserving(const MachineInstr &In, unsigned OpNum) const; |
| 425 | virtual bool isClobbering(const MachineInstr &In, unsigned OpNum) const; |
| 426 | virtual bool isFixedReg(const MachineInstr &In, unsigned OpNum) const; |
| 427 | |
| 428 | const TargetInstrInfo &TII; |
| 429 | }; |
| 430 | |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 431 | |
| 432 | // Packed register reference. Only used for storage. |
| 433 | struct PackedRegisterRef { |
| 434 | RegisterId Reg; |
| 435 | uint32_t MaskId; |
| 436 | }; |
| 437 | |
Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 438 | // Template class for a map translating uint32_t into arbitrary types. |
| 439 | // The map will act like an indexed set: upon insertion of a new object, |
| 440 | // it will automatically assign a new index to it. Index of 0 is treated |
| 441 | // as invalid and is never allocated. |
| 442 | template <typename T, unsigned N = 32> |
| 443 | struct IndexedSet { |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 444 | IndexedSet() : Map() { Map.reserve(N); } |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 445 | T get(uint32_t Idx) const { |
Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 446 | // Index Idx corresponds to Map[Idx-1]. |
| 447 | assert(Idx != 0 && !Map.empty() && Idx-1 < Map.size()); |
| 448 | return Map[Idx-1]; |
| 449 | } |
| 450 | uint32_t insert(T Val) { |
| 451 | // Linear search. |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 452 | auto F = llvm::find(Map, Val); |
Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 453 | if (F != Map.end()) |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 454 | return F - Map.begin() + 1; |
Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 455 | Map.push_back(Val); |
| 456 | return Map.size(); // Return actual_index + 1. |
| 457 | } |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 458 | uint32_t find(T Val) const { |
| 459 | auto F = llvm::find(Map, Val); |
| 460 | assert(F != Map.end()); |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame^] | 461 | return F - Map.begin(); |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 462 | } |
Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 463 | private: |
| 464 | std::vector<T> Map; |
| 465 | }; |
| 466 | |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 467 | struct LaneMaskIndex : private IndexedSet<LaneBitmask> { |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 468 | LaneMaskIndex() = default; |
| 469 | |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 470 | LaneBitmask getLaneMaskForIndex(uint32_t K) const { |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame^] | 471 | return K == 0 ? LaneBitmask::getAll() : get(K); |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 472 | } |
| 473 | uint32_t getIndexForLaneMask(LaneBitmask LM) { |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame^] | 474 | assert(!LM.none()); |
| 475 | return LM.all() ? 0 : insert(LM); |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 476 | } |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 477 | uint32_t getIndexForLaneMask(LaneBitmask LM) const { |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame^] | 478 | assert(!LM.none()); |
| 479 | return LM.all() ? 0 : find(LM); |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 480 | } |
| 481 | PackedRegisterRef pack(RegisterRef RR) { |
| 482 | return { RR.Reg, getIndexForLaneMask(RR.Mask) }; |
| 483 | } |
| 484 | PackedRegisterRef pack(RegisterRef RR) const { |
| 485 | return { RR.Reg, getIndexForLaneMask(RR.Mask) }; |
| 486 | } |
| 487 | RegisterRef unpack(PackedRegisterRef PR) const { |
| 488 | return RegisterRef(PR.Reg, getLaneMaskForIndex(PR.MaskId)); |
| 489 | } |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 490 | }; |
| 491 | |
| 492 | struct RegisterAggr { |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 493 | RegisterAggr(const TargetRegisterInfo &tri) |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 494 | : Masks(), ExpAliasUnits(tri.getNumRegUnits()), CheckUnits(false), |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 495 | TRI(tri) {} |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 496 | RegisterAggr(const RegisterAggr &RG) |
| 497 | : Masks(RG.Masks), ExpAliasUnits(RG.ExpAliasUnits), |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 498 | CheckUnits(RG.CheckUnits), TRI(RG.TRI) {} |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 499 | |
| 500 | bool empty() const { return Masks.empty(); } |
| 501 | bool hasAliasOf(RegisterRef RR) const; |
| 502 | bool hasCoverOf(RegisterRef RR) const; |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 503 | static bool isCoverOf(RegisterRef RA, RegisterRef RB, |
| 504 | const TargetRegisterInfo &TRI) { |
| 505 | return RegisterAggr(TRI).insert(RA).hasCoverOf(RB); |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | RegisterAggr &insert(RegisterRef RR); |
| 509 | RegisterAggr &insert(const RegisterAggr &RG); |
| 510 | RegisterAggr &clear(RegisterRef RR); |
Krzysztof Parzyszek | 7bb63ac | 2016-10-19 16:30:56 +0000 | [diff] [blame] | 511 | RegisterAggr &clear(const RegisterAggr &RG); |
| 512 | |
| 513 | RegisterRef clearIn(RegisterRef RR) const; |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 514 | |
| 515 | void print(raw_ostream &OS) const; |
| 516 | |
| 517 | private: |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 518 | typedef std::unordered_map<RegisterId, LaneBitmask> MapType; |
| 519 | |
| 520 | public: |
| 521 | typedef MapType::const_iterator iterator; |
| 522 | iterator begin() const { return Masks.begin(); } |
| 523 | iterator end() const { return Masks.end(); } |
| 524 | RegisterRef normalize(RegisterRef RR) const; |
| 525 | |
| 526 | private: |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 527 | MapType Masks; |
| 528 | BitVector ExpAliasUnits; // Register units for explicit aliases. |
| 529 | bool CheckUnits; |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 530 | const TargetRegisterInfo &TRI; |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 531 | }; |
| 532 | |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 533 | |
| 534 | struct NodeBase { |
| 535 | public: |
| 536 | // Make sure this is a POD. |
| 537 | NodeBase() = default; |
| 538 | uint16_t getType() const { return NodeAttrs::type(Attrs); } |
| 539 | uint16_t getKind() const { return NodeAttrs::kind(Attrs); } |
| 540 | uint16_t getFlags() const { return NodeAttrs::flags(Attrs); } |
| 541 | NodeId getNext() const { return Next; } |
| 542 | |
| 543 | uint16_t getAttrs() const { return Attrs; } |
| 544 | void setAttrs(uint16_t A) { Attrs = A; } |
| 545 | void setFlags(uint16_t F) { setAttrs(NodeAttrs::set_flags(getAttrs(), F)); } |
| 546 | |
| 547 | // Insert node NA after "this" in the circular chain. |
| 548 | void append(NodeAddr<NodeBase*> NA); |
| 549 | // Initialize all members to 0. |
| 550 | void init() { memset(this, 0, sizeof *this); } |
| 551 | void setNext(NodeId N) { Next = N; } |
| 552 | |
| 553 | protected: |
| 554 | uint16_t Attrs; |
| 555 | uint16_t Reserved; |
| 556 | NodeId Next; // Id of the next node in the circular chain. |
| 557 | // Definitions of nested types. Using anonymous nested structs would make |
| 558 | // this class definition clearer, but unnamed structs are not a part of |
| 559 | // the standard. |
| 560 | struct Def_struct { |
| 561 | NodeId DD, DU; // Ids of the first reached def and use. |
| 562 | }; |
| 563 | struct PhiU_struct { |
| 564 | NodeId PredB; // Id of the predecessor block for a phi use. |
| 565 | }; |
| 566 | struct Code_struct { |
| 567 | void *CP; // Pointer to the actual code. |
| 568 | NodeId FirstM, LastM; // Id of the first member and last. |
| 569 | }; |
| 570 | struct Ref_struct { |
| 571 | NodeId RD, Sib; // Ids of the reaching def and the sibling. |
| 572 | union { |
| 573 | Def_struct Def; |
| 574 | PhiU_struct PhiU; |
| 575 | }; |
| 576 | union { |
| 577 | MachineOperand *Op; // Non-phi refs point to a machine operand. |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 578 | PackedRegisterRef PR; // Phi refs store register info directly. |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 579 | }; |
| 580 | }; |
| 581 | |
| 582 | // The actual payload. |
| 583 | union { |
| 584 | Ref_struct Ref; |
| 585 | Code_struct Code; |
| 586 | }; |
| 587 | }; |
| 588 | // The allocator allocates chunks of 32 bytes for each node. The fact that |
| 589 | // each node takes 32 bytes in memory is used for fast translation between |
| 590 | // the node id and the node address. |
| 591 | static_assert(sizeof(NodeBase) <= NodeAllocator::NodeMemSize, |
| 592 | "NodeBase must be at most NodeAllocator::NodeMemSize bytes"); |
| 593 | |
| 594 | typedef std::vector<NodeAddr<NodeBase*>> NodeList; |
| 595 | typedef std::set<NodeId> NodeSet; |
| 596 | |
| 597 | struct RefNode : public NodeBase { |
| 598 | RefNode() = default; |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 599 | RegisterRef getRegRef(const DataFlowGraph &G) const; |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 600 | MachineOperand &getOp() { |
| 601 | assert(!(getFlags() & NodeAttrs::PhiRef)); |
| 602 | return *Ref.Op; |
| 603 | } |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 604 | void setRegRef(RegisterRef RR, DataFlowGraph &G); |
| 605 | void setRegRef(MachineOperand *Op, DataFlowGraph &G); |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 606 | NodeId getReachingDef() const { |
| 607 | return Ref.RD; |
| 608 | } |
| 609 | void setReachingDef(NodeId RD) { |
| 610 | Ref.RD = RD; |
| 611 | } |
| 612 | NodeId getSibling() const { |
| 613 | return Ref.Sib; |
| 614 | } |
| 615 | void setSibling(NodeId Sib) { |
| 616 | Ref.Sib = Sib; |
| 617 | } |
| 618 | bool isUse() const { |
| 619 | assert(getType() == NodeAttrs::Ref); |
| 620 | return getKind() == NodeAttrs::Use; |
| 621 | } |
| 622 | bool isDef() const { |
| 623 | assert(getType() == NodeAttrs::Ref); |
| 624 | return getKind() == NodeAttrs::Def; |
| 625 | } |
| 626 | |
| 627 | template <typename Predicate> |
| 628 | NodeAddr<RefNode*> getNextRef(RegisterRef RR, Predicate P, bool NextOnly, |
| 629 | const DataFlowGraph &G); |
| 630 | NodeAddr<NodeBase*> getOwner(const DataFlowGraph &G); |
| 631 | }; |
| 632 | |
| 633 | struct DefNode : public RefNode { |
| 634 | NodeId getReachedDef() const { |
| 635 | return Ref.Def.DD; |
| 636 | } |
| 637 | void setReachedDef(NodeId D) { |
| 638 | Ref.Def.DD = D; |
| 639 | } |
| 640 | NodeId getReachedUse() const { |
| 641 | return Ref.Def.DU; |
| 642 | } |
| 643 | void setReachedUse(NodeId U) { |
| 644 | Ref.Def.DU = U; |
| 645 | } |
| 646 | |
| 647 | void linkToDef(NodeId Self, NodeAddr<DefNode*> DA); |
| 648 | }; |
| 649 | |
| 650 | struct UseNode : public RefNode { |
| 651 | void linkToDef(NodeId Self, NodeAddr<DefNode*> DA); |
| 652 | }; |
| 653 | |
| 654 | struct PhiUseNode : public UseNode { |
| 655 | NodeId getPredecessor() const { |
| 656 | assert(getFlags() & NodeAttrs::PhiRef); |
| 657 | return Ref.PhiU.PredB; |
| 658 | } |
| 659 | void setPredecessor(NodeId B) { |
| 660 | assert(getFlags() & NodeAttrs::PhiRef); |
| 661 | Ref.PhiU.PredB = B; |
| 662 | } |
| 663 | }; |
| 664 | |
| 665 | struct CodeNode : public NodeBase { |
| 666 | template <typename T> T getCode() const { |
| 667 | return static_cast<T>(Code.CP); |
| 668 | } |
| 669 | void setCode(void *C) { |
| 670 | Code.CP = C; |
| 671 | } |
| 672 | |
| 673 | NodeAddr<NodeBase*> getFirstMember(const DataFlowGraph &G) const; |
| 674 | NodeAddr<NodeBase*> getLastMember(const DataFlowGraph &G) const; |
| 675 | void addMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G); |
| 676 | void addMemberAfter(NodeAddr<NodeBase*> MA, NodeAddr<NodeBase*> NA, |
| 677 | const DataFlowGraph &G); |
| 678 | void removeMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G); |
| 679 | |
| 680 | NodeList members(const DataFlowGraph &G) const; |
| 681 | template <typename Predicate> |
| 682 | NodeList members_if(Predicate P, const DataFlowGraph &G) const; |
| 683 | }; |
| 684 | |
| 685 | struct InstrNode : public CodeNode { |
| 686 | NodeAddr<NodeBase*> getOwner(const DataFlowGraph &G); |
| 687 | }; |
| 688 | |
| 689 | struct PhiNode : public InstrNode { |
| 690 | MachineInstr *getCode() const { |
| 691 | return nullptr; |
| 692 | } |
| 693 | }; |
| 694 | |
| 695 | struct StmtNode : public InstrNode { |
| 696 | MachineInstr *getCode() const { |
| 697 | return CodeNode::getCode<MachineInstr*>(); |
| 698 | } |
| 699 | }; |
| 700 | |
| 701 | struct BlockNode : public CodeNode { |
| 702 | MachineBasicBlock *getCode() const { |
| 703 | return CodeNode::getCode<MachineBasicBlock*>(); |
| 704 | } |
| 705 | void addPhi(NodeAddr<PhiNode*> PA, const DataFlowGraph &G); |
| 706 | }; |
| 707 | |
| 708 | struct FuncNode : public CodeNode { |
| 709 | MachineFunction *getCode() const { |
| 710 | return CodeNode::getCode<MachineFunction*>(); |
| 711 | } |
| 712 | NodeAddr<BlockNode*> findBlock(const MachineBasicBlock *BB, |
| 713 | const DataFlowGraph &G) const; |
| 714 | NodeAddr<BlockNode*> getEntryBlock(const DataFlowGraph &G); |
| 715 | }; |
| 716 | |
| 717 | struct DataFlowGraph { |
| 718 | DataFlowGraph(MachineFunction &mf, const TargetInstrInfo &tii, |
| 719 | const TargetRegisterInfo &tri, const MachineDominatorTree &mdt, |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 720 | const MachineDominanceFrontier &mdf, const TargetOperandInfo &toi); |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 721 | |
| 722 | NodeBase *ptr(NodeId N) const; |
| 723 | template <typename T> T ptr(NodeId N) const { |
| 724 | return static_cast<T>(ptr(N)); |
| 725 | } |
| 726 | NodeId id(const NodeBase *P) const; |
| 727 | |
| 728 | template <typename T> NodeAddr<T> addr(NodeId N) const { |
| 729 | return { ptr<T>(N), N }; |
| 730 | } |
| 731 | |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 732 | NodeAddr<FuncNode*> getFunc() const { return Func; } |
| 733 | MachineFunction &getMF() const { return MF; } |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 734 | const TargetInstrInfo &getTII() const { return TII; } |
| 735 | const TargetRegisterInfo &getTRI() const { return TRI; } |
| 736 | const MachineDominatorTree &getDT() const { return MDT; } |
| 737 | const MachineDominanceFrontier &getDF() const { return MDF; } |
Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 738 | |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 739 | struct DefStack { |
| 740 | DefStack() = default; |
| 741 | bool empty() const { return Stack.empty() || top() == bottom(); } |
| 742 | private: |
| 743 | typedef NodeAddr<DefNode*> value_type; |
| 744 | struct Iterator { |
| 745 | typedef DefStack::value_type value_type; |
| 746 | Iterator &up() { Pos = DS.nextUp(Pos); return *this; } |
| 747 | Iterator &down() { Pos = DS.nextDown(Pos); return *this; } |
| 748 | value_type operator*() const { |
| 749 | assert(Pos >= 1); |
| 750 | return DS.Stack[Pos-1]; |
| 751 | } |
| 752 | const value_type *operator->() const { |
| 753 | assert(Pos >= 1); |
| 754 | return &DS.Stack[Pos-1]; |
| 755 | } |
| 756 | bool operator==(const Iterator &It) const { return Pos == It.Pos; } |
| 757 | bool operator!=(const Iterator &It) const { return Pos != It.Pos; } |
| 758 | private: |
| 759 | Iterator(const DefStack &S, bool Top); |
| 760 | // Pos-1 is the index in the StorageType object that corresponds to |
| 761 | // the top of the DefStack. |
| 762 | const DefStack &DS; |
| 763 | unsigned Pos; |
| 764 | friend struct DefStack; |
| 765 | }; |
| 766 | public: |
| 767 | typedef Iterator iterator; |
| 768 | iterator top() const { return Iterator(*this, true); } |
| 769 | iterator bottom() const { return Iterator(*this, false); } |
| 770 | unsigned size() const; |
| 771 | |
| 772 | void push(NodeAddr<DefNode*> DA) { Stack.push_back(DA); } |
| 773 | void pop(); |
| 774 | void start_block(NodeId N); |
| 775 | void clear_block(NodeId N); |
| 776 | private: |
| 777 | friend struct Iterator; |
| 778 | typedef std::vector<value_type> StorageType; |
| 779 | bool isDelimiter(const StorageType::value_type &P, NodeId N = 0) const { |
| 780 | return (P.Addr == nullptr) && (N == 0 || P.Id == N); |
| 781 | } |
| 782 | unsigned nextUp(unsigned P) const; |
| 783 | unsigned nextDown(unsigned P) const; |
| 784 | StorageType Stack; |
| 785 | }; |
| 786 | |
Krzysztof Parzyszek | 047149f | 2016-07-22 16:09:47 +0000 | [diff] [blame] | 787 | // Make this std::unordered_map for speed of accessing elements. |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 788 | // Map: Register (physical or virtual) -> DefStack |
Krzysztof Parzyszek | 6e7fa99 | 2016-10-21 19:12:13 +0000 | [diff] [blame] | 789 | typedef std::unordered_map<RegisterId,DefStack> DefStackMap; |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 790 | |
Krzysztof Parzyszek | 55874cf | 2016-04-28 20:17:06 +0000 | [diff] [blame] | 791 | void build(unsigned Options = BuildOptions::None); |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 792 | void pushDefs(NodeAddr<InstrNode*> IA, DefStackMap &DM); |
| 793 | void markBlock(NodeId B, DefStackMap &DefM); |
| 794 | void releaseBlock(NodeId B, DefStackMap &DefM); |
| 795 | |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 796 | PackedRegisterRef pack(RegisterRef RR) { return LMI.pack(RR); } |
| 797 | PackedRegisterRef pack(RegisterRef RR) const { return LMI.pack(RR); } |
| 798 | RegisterRef unpack(PackedRegisterRef PR) const { return LMI.unpack(PR); } |
| 799 | RegisterRef makeRegRef(unsigned Reg, unsigned Sub) const; |
Krzysztof Parzyszek | 7bb63ac | 2016-10-19 16:30:56 +0000 | [diff] [blame] | 800 | RegisterRef normalizeRef(RegisterRef RR) const; |
| 801 | RegisterRef restrictRef(RegisterRef AR, RegisterRef BR) const; |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 802 | |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 803 | NodeAddr<RefNode*> getNextRelated(NodeAddr<InstrNode*> IA, |
| 804 | NodeAddr<RefNode*> RA) const; |
| 805 | NodeAddr<RefNode*> getNextImp(NodeAddr<InstrNode*> IA, |
| 806 | NodeAddr<RefNode*> RA, bool Create); |
| 807 | NodeAddr<RefNode*> getNextImp(NodeAddr<InstrNode*> IA, |
| 808 | NodeAddr<RefNode*> RA) const; |
| 809 | NodeAddr<RefNode*> getNextShadow(NodeAddr<InstrNode*> IA, |
| 810 | NodeAddr<RefNode*> RA, bool Create); |
| 811 | NodeAddr<RefNode*> getNextShadow(NodeAddr<InstrNode*> IA, |
| 812 | NodeAddr<RefNode*> RA) const; |
| 813 | |
| 814 | NodeList getRelatedRefs(NodeAddr<InstrNode*> IA, |
| 815 | NodeAddr<RefNode*> RA) const; |
| 816 | |
Krzysztof Parzyszek | 69e670d5 | 2016-01-18 20:41:34 +0000 | [diff] [blame] | 817 | void unlinkUse(NodeAddr<UseNode*> UA, bool RemoveFromOwner) { |
| 818 | unlinkUseDF(UA); |
| 819 | if (RemoveFromOwner) |
| 820 | removeFromOwner(UA); |
| 821 | } |
| 822 | void unlinkDef(NodeAddr<DefNode*> DA, bool RemoveFromOwner) { |
| 823 | unlinkDefDF(DA); |
| 824 | if (RemoveFromOwner) |
| 825 | removeFromOwner(DA); |
| 826 | } |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 827 | |
| 828 | // Some useful filters. |
| 829 | template <uint16_t Kind> |
| 830 | static bool IsRef(const NodeAddr<NodeBase*> BA) { |
| 831 | return BA.Addr->getType() == NodeAttrs::Ref && |
| 832 | BA.Addr->getKind() == Kind; |
| 833 | } |
| 834 | template <uint16_t Kind> |
| 835 | static bool IsCode(const NodeAddr<NodeBase*> BA) { |
| 836 | return BA.Addr->getType() == NodeAttrs::Code && |
| 837 | BA.Addr->getKind() == Kind; |
| 838 | } |
| 839 | static bool IsDef(const NodeAddr<NodeBase*> BA) { |
| 840 | return BA.Addr->getType() == NodeAttrs::Ref && |
| 841 | BA.Addr->getKind() == NodeAttrs::Def; |
| 842 | } |
| 843 | static bool IsUse(const NodeAddr<NodeBase*> BA) { |
| 844 | return BA.Addr->getType() == NodeAttrs::Ref && |
| 845 | BA.Addr->getKind() == NodeAttrs::Use; |
| 846 | } |
| 847 | static bool IsPhi(const NodeAddr<NodeBase*> BA) { |
| 848 | return BA.Addr->getType() == NodeAttrs::Code && |
| 849 | BA.Addr->getKind() == NodeAttrs::Phi; |
| 850 | } |
Krzysztof Parzyszek | 1ff9952 | 2016-09-07 20:10:56 +0000 | [diff] [blame] | 851 | static bool IsPreservingDef(const NodeAddr<DefNode*> DA) { |
| 852 | uint16_t Flags = DA.Addr->getFlags(); |
| 853 | return (Flags & NodeAttrs::Preserving) && !(Flags & NodeAttrs::Undef); |
| 854 | } |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 855 | |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 856 | // Register aliasing. |
| 857 | bool alias(RegisterRef RA, RegisterRef RB) const; |
| 858 | |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 859 | private: |
| 860 | void reset(); |
| 861 | |
Krzysztof Parzyszek | 6e7fa99 | 2016-10-21 19:12:13 +0000 | [diff] [blame] | 862 | RegisterSet getAliasSet(RegisterId Reg) const; |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 863 | RegisterSet getLandingPadLiveIns() const; |
Krzysztof Parzyszek | 1d32220 | 2016-09-27 18:18:44 +0000 | [diff] [blame] | 864 | |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 865 | NodeAddr<NodeBase*> newNode(uint16_t Attrs); |
| 866 | NodeAddr<NodeBase*> cloneNode(const NodeAddr<NodeBase*> B); |
| 867 | NodeAddr<UseNode*> newUse(NodeAddr<InstrNode*> Owner, |
| 868 | MachineOperand &Op, uint16_t Flags = NodeAttrs::None); |
| 869 | NodeAddr<PhiUseNode*> newPhiUse(NodeAddr<PhiNode*> Owner, |
| 870 | RegisterRef RR, NodeAddr<BlockNode*> PredB, |
| 871 | uint16_t Flags = NodeAttrs::PhiRef); |
| 872 | NodeAddr<DefNode*> newDef(NodeAddr<InstrNode*> Owner, |
| 873 | MachineOperand &Op, uint16_t Flags = NodeAttrs::None); |
| 874 | NodeAddr<DefNode*> newDef(NodeAddr<InstrNode*> Owner, |
| 875 | RegisterRef RR, uint16_t Flags = NodeAttrs::PhiRef); |
| 876 | NodeAddr<PhiNode*> newPhi(NodeAddr<BlockNode*> Owner); |
| 877 | NodeAddr<StmtNode*> newStmt(NodeAddr<BlockNode*> Owner, |
| 878 | MachineInstr *MI); |
| 879 | NodeAddr<BlockNode*> newBlock(NodeAddr<FuncNode*> Owner, |
| 880 | MachineBasicBlock *BB); |
| 881 | NodeAddr<FuncNode*> newFunc(MachineFunction *MF); |
| 882 | |
| 883 | template <typename Predicate> |
| 884 | std::pair<NodeAddr<RefNode*>,NodeAddr<RefNode*>> |
| 885 | locateNextRef(NodeAddr<InstrNode*> IA, NodeAddr<RefNode*> RA, |
| 886 | Predicate P) const; |
| 887 | |
| 888 | typedef std::map<NodeId,RegisterSet> BlockRefsMap; |
| 889 | |
| 890 | void buildStmt(NodeAddr<BlockNode*> BA, MachineInstr &In); |
| 891 | void buildBlockRefs(NodeAddr<BlockNode*> BA, BlockRefsMap &RefM); |
| 892 | void recordDefsForDF(BlockRefsMap &PhiM, BlockRefsMap &RefM, |
| 893 | NodeAddr<BlockNode*> BA); |
| 894 | void buildPhis(BlockRefsMap &PhiM, BlockRefsMap &RefM, |
| 895 | NodeAddr<BlockNode*> BA); |
| 896 | void removeUnusedPhis(); |
| 897 | |
| 898 | template <typename T> void linkRefUp(NodeAddr<InstrNode*> IA, |
| 899 | NodeAddr<T> TA, DefStack &DS); |
| 900 | void linkStmtRefs(DefStackMap &DefM, NodeAddr<StmtNode*> SA); |
| 901 | void linkBlockRefs(DefStackMap &DefM, NodeAddr<BlockNode*> BA); |
| 902 | |
Krzysztof Parzyszek | 69e670d5 | 2016-01-18 20:41:34 +0000 | [diff] [blame] | 903 | void unlinkUseDF(NodeAddr<UseNode*> UA); |
| 904 | void unlinkDefDF(NodeAddr<DefNode*> DA); |
| 905 | void removeFromOwner(NodeAddr<RefNode*> RA) { |
| 906 | NodeAddr<InstrNode*> IA = RA.Addr->getOwner(*this); |
| 907 | IA.Addr->removeMember(RA, *this); |
| 908 | } |
| 909 | |
Krzysztof Parzyszek | 047149f | 2016-07-22 16:09:47 +0000 | [diff] [blame] | 910 | NodeAddr<BlockNode*> findBlock(MachineBasicBlock *BB) { |
| 911 | return BlockNodes[BB]; |
| 912 | } |
| 913 | |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 914 | NodeAddr<FuncNode*> Func; |
| 915 | NodeAllocator Memory; |
Krzysztof Parzyszek | 047149f | 2016-07-22 16:09:47 +0000 | [diff] [blame] | 916 | // Local map: MachineBasicBlock -> NodeAddr<BlockNode*> |
| 917 | std::map<MachineBasicBlock*,NodeAddr<BlockNode*>> BlockNodes; |
Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 918 | // Lane mask map. |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 919 | LaneMaskIndex LMI; |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 920 | |
| 921 | MachineFunction &MF; |
| 922 | const TargetInstrInfo &TII; |
| 923 | const TargetRegisterInfo &TRI; |
| 924 | const MachineDominatorTree &MDT; |
| 925 | const MachineDominanceFrontier &MDF; |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 926 | const TargetOperandInfo &TOI; |
| 927 | }; // struct DataFlowGraph |
| 928 | |
| 929 | template <typename Predicate> |
| 930 | NodeAddr<RefNode*> RefNode::getNextRef(RegisterRef RR, Predicate P, |
| 931 | bool NextOnly, const DataFlowGraph &G) { |
| 932 | // Get the "Next" reference in the circular list that references RR and |
| 933 | // satisfies predicate "Pred". |
| 934 | auto NA = G.addr<NodeBase*>(getNext()); |
| 935 | |
| 936 | while (NA.Addr != this) { |
| 937 | if (NA.Addr->getType() == NodeAttrs::Ref) { |
| 938 | NodeAddr<RefNode*> RA = NA; |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 939 | if (RA.Addr->getRegRef(G) == RR && P(NA)) |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 940 | return NA; |
| 941 | if (NextOnly) |
| 942 | break; |
| 943 | NA = G.addr<NodeBase*>(NA.Addr->getNext()); |
| 944 | } else { |
| 945 | // We've hit the beginning of the chain. |
| 946 | assert(NA.Addr->getType() == NodeAttrs::Code); |
| 947 | NodeAddr<CodeNode*> CA = NA; |
| 948 | NA = CA.Addr->getFirstMember(G); |
| 949 | } |
| 950 | } |
| 951 | // Return the equivalent of "nullptr" if such a node was not found. |
| 952 | return NodeAddr<RefNode*>(); |
| 953 | } |
| 954 | |
| 955 | template <typename Predicate> |
| 956 | NodeList CodeNode::members_if(Predicate P, const DataFlowGraph &G) const { |
| 957 | NodeList MM; |
| 958 | auto M = getFirstMember(G); |
| 959 | if (M.Id == 0) |
| 960 | return MM; |
| 961 | |
| 962 | while (M.Addr != this) { |
| 963 | if (P(M)) |
| 964 | MM.push_back(M); |
| 965 | M = G.addr<NodeBase*>(M.Addr->getNext()); |
| 966 | } |
| 967 | return MM; |
| 968 | } |
| 969 | |
| 970 | |
Krzysztof Parzyszek | 7bb63ac | 2016-10-19 16:30:56 +0000 | [diff] [blame] | 971 | // Optionally print the lane mask, if it is not ~0. |
| 972 | struct PrintLaneMaskOpt { |
| 973 | PrintLaneMaskOpt(LaneBitmask M) : Mask(M) {} |
| 974 | LaneBitmask Mask; |
| 975 | }; |
| 976 | raw_ostream &operator<< (raw_ostream &OS, const PrintLaneMaskOpt &P); |
| 977 | |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 978 | template <typename T> struct Print; |
| 979 | template <typename T> |
| 980 | raw_ostream &operator<< (raw_ostream &OS, const Print<T> &P); |
| 981 | |
| 982 | template <typename T> |
| 983 | struct Print { |
| 984 | Print(const T &x, const DataFlowGraph &g) : Obj(x), G(g) {} |
| 985 | const T &Obj; |
| 986 | const DataFlowGraph &G; |
| 987 | }; |
| 988 | |
| 989 | template <typename T> |
| 990 | struct PrintNode : Print<NodeAddr<T>> { |
| 991 | PrintNode(const NodeAddr<T> &x, const DataFlowGraph &g) |
| 992 | : Print<NodeAddr<T>>(x, g) {} |
| 993 | }; |
| 994 | } // namespace rdf |
Benjamin Kramer | 922efd7 | 2016-05-27 10:06:40 +0000 | [diff] [blame] | 995 | } // namespace llvm |
Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 996 | |
| 997 | #endif // RDF_GRAPH_H |