| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 1 | //===--- RDFGraph.h ---------------------------------------------*- C++ -*-===// |
| 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 | // 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 | // |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 224 | |
| 225 | #ifndef LLVM_LIB_TARGET_HEXAGON_RDFGRAPH_H |
| 226 | #define LLVM_LIB_TARGET_HEXAGON_RDFGRAPH_H |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 227 | |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 228 | #include "llvm/ADT/BitVector.h" |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 229 | #include "llvm/ADT/STLExtras.h" |
| 230 | #include "llvm/MC/LaneBitmask.h" |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 231 | #include "llvm/Support/Allocator.h" |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 232 | #include "llvm/Support/MathExtras.h" |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 233 | #include "llvm/Support/raw_ostream.h" |
| Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 234 | #include "llvm/Target/TargetRegisterInfo.h" |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 235 | #include <cassert> |
| 236 | #include <cstdint> |
| 237 | #include <cstring> |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 238 | #include <functional> |
| 239 | #include <map> |
| 240 | #include <set> |
| Krzysztof Parzyszek | 047149f | 2016-07-22 16:09:47 +0000 | [diff] [blame] | 241 | #include <unordered_map> |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 242 | #include <utility> |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 243 | #include <vector> |
| 244 | |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 245 | // RDF uses uint32_t to refer to registers. This is to ensure that the type |
| 246 | // size remains specific. In other places, registers are often stored using |
| 247 | // unsigned. |
| 248 | static_assert(sizeof(uint32_t) == sizeof(unsigned), "Those should be equal"); |
| 249 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 250 | namespace llvm { |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 251 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 252 | class MachineBasicBlock; |
| 253 | class MachineFunction; |
| 254 | class MachineInstr; |
| 255 | class MachineOperand; |
| 256 | class MachineDominanceFrontier; |
| 257 | class MachineDominatorTree; |
| 258 | class TargetInstrInfo; |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 259 | |
| 260 | namespace rdf { |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 261 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 262 | typedef uint32_t NodeId; |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 263 | typedef uint32_t RegisterId; |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 264 | |
| Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 265 | struct DataFlowGraph; |
| 266 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 267 | struct NodeAttrs { |
| 268 | enum : uint16_t { |
| 269 | None = 0x0000, // Nothing |
| 270 | |
| 271 | // Types: 2 bits |
| 272 | TypeMask = 0x0003, |
| 273 | Code = 0x0001, // 01, Container |
| 274 | Ref = 0x0002, // 10, Reference |
| 275 | |
| 276 | // Kind: 3 bits |
| 277 | KindMask = 0x0007 << 2, |
| 278 | Def = 0x0001 << 2, // 001 |
| 279 | Use = 0x0002 << 2, // 010 |
| 280 | Phi = 0x0003 << 2, // 011 |
| 281 | Stmt = 0x0004 << 2, // 100 |
| 282 | Block = 0x0005 << 2, // 101 |
| 283 | Func = 0x0006 << 2, // 110 |
| 284 | |
| Krzysztof Parzyszek | 586fc12 | 2016-09-27 18:24:33 +0000 | [diff] [blame] | 285 | // Flags: 7 bits for now |
| 286 | FlagMask = 0x007F << 5, |
| 287 | Shadow = 0x0001 << 5, // 0000001, Has extra reaching defs. |
| 288 | Clobbering = 0x0002 << 5, // 0000010, Produces unspecified values. |
| 289 | PhiRef = 0x0004 << 5, // 0000100, Member of PhiNode. |
| 290 | Preserving = 0x0008 << 5, // 0001000, Def can keep original bits. |
| 291 | Fixed = 0x0010 << 5, // 0010000, Fixed register. |
| 292 | Undef = 0x0020 << 5, // 0100000, Has no pre-existing value. |
| 293 | Dead = 0x0040 << 5, // 1000000, Does not define a value. |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 294 | }; |
| 295 | |
| 296 | static uint16_t type(uint16_t T) { return T & TypeMask; } |
| 297 | static uint16_t kind(uint16_t T) { return T & KindMask; } |
| 298 | static uint16_t flags(uint16_t T) { return T & FlagMask; } |
| 299 | |
| 300 | static uint16_t set_type(uint16_t A, uint16_t T) { |
| 301 | return (A & ~TypeMask) | T; |
| 302 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 303 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 304 | static uint16_t set_kind(uint16_t A, uint16_t K) { |
| 305 | return (A & ~KindMask) | K; |
| 306 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 307 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 308 | static uint16_t set_flags(uint16_t A, uint16_t F) { |
| 309 | return (A & ~FlagMask) | F; |
| 310 | } |
| 311 | |
| 312 | // Test if A contains B. |
| 313 | static bool contains(uint16_t A, uint16_t B) { |
| 314 | if (type(A) != Code) |
| 315 | return false; |
| 316 | uint16_t KB = kind(B); |
| 317 | switch (kind(A)) { |
| 318 | case Func: |
| 319 | return KB == Block; |
| 320 | case Block: |
| 321 | return KB == Phi || KB == Stmt; |
| 322 | case Phi: |
| 323 | case Stmt: |
| 324 | return type(B) == Ref; |
| 325 | } |
| 326 | return false; |
| 327 | } |
| 328 | }; |
| 329 | |
| Krzysztof Parzyszek | 55874cf | 2016-04-28 20:17:06 +0000 | [diff] [blame] | 330 | struct BuildOptions { |
| 331 | enum : unsigned { |
| 332 | None = 0x00, |
| 333 | KeepDeadPhis = 0x01, // Do not remove dead phis during build. |
| 334 | }; |
| 335 | }; |
| 336 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 337 | template <typename T> struct NodeAddr { |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 338 | NodeAddr() : Addr(nullptr) {} |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 339 | NodeAddr(T A, NodeId I) : Addr(A), Id(I) {} |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 340 | |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 341 | // Type cast (casting constructor). The reason for having this class |
| 342 | // instead of std::pair. |
| 343 | template <typename S> NodeAddr(const NodeAddr<S> &NA) |
| 344 | : Addr(static_cast<T>(NA.Addr)), Id(NA.Id) {} |
| 345 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 346 | bool operator== (const NodeAddr<T> &NA) const { |
| 347 | assert((Addr == NA.Addr) == (Id == NA.Id)); |
| 348 | return Addr == NA.Addr; |
| 349 | } |
| 350 | bool operator!= (const NodeAddr<T> &NA) const { |
| 351 | return !operator==(NA); |
| 352 | } |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 353 | |
| 354 | T Addr; |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 355 | NodeId Id = 0; |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 356 | }; |
| 357 | |
| 358 | struct NodeBase; |
| 359 | |
| 360 | // Fast memory allocation and translation between node id and node address. |
| 361 | // This is really the same idea as the one underlying the "bump pointer |
| 362 | // allocator", the difference being in the translation. A node id is |
| 363 | // composed of two components: the index of the block in which it was |
| 364 | // allocated, and the index within the block. With the default settings, |
| 365 | // where the number of nodes per block is 4096, the node id (minus 1) is: |
| 366 | // |
| 367 | // bit position: 11 0 |
| 368 | // +----------------------------+--------------+ |
| 369 | // | Index of the block |Index in block| |
| 370 | // +----------------------------+--------------+ |
| 371 | // |
| 372 | // The actual node id is the above plus 1, to avoid creating a node id of 0. |
| 373 | // |
| 374 | // This method significantly improved the build time, compared to using maps |
| 375 | // (std::unordered_map or DenseMap) to translate between pointers and ids. |
| 376 | struct NodeAllocator { |
| 377 | // Amount of storage for a single node. |
| 378 | enum { NodeMemSize = 32 }; |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 379 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 380 | NodeAllocator(uint32_t NPB = 4096) |
| 381 | : NodesPerBlock(NPB), BitsPerIndex(Log2_32(NPB)), |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 382 | IndexMask((1 << BitsPerIndex)-1) { |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 383 | assert(isPowerOf2_32(NPB)); |
| 384 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 385 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 386 | NodeBase *ptr(NodeId N) const { |
| 387 | uint32_t N1 = N-1; |
| 388 | uint32_t BlockN = N1 >> BitsPerIndex; |
| 389 | uint32_t Offset = (N1 & IndexMask) * NodeMemSize; |
| 390 | return reinterpret_cast<NodeBase*>(Blocks[BlockN]+Offset); |
| 391 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 392 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 393 | NodeId id(const NodeBase *P) const; |
| 394 | NodeAddr<NodeBase*> New(); |
| 395 | void clear(); |
| 396 | |
| 397 | private: |
| 398 | void startNewBlock(); |
| 399 | bool needNewBlock(); |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 400 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 401 | uint32_t makeId(uint32_t Block, uint32_t Index) const { |
| 402 | // Add 1 to the id, to avoid the id of 0, which is treated as "null". |
| 403 | return ((Block << BitsPerIndex) | Index) + 1; |
| 404 | } |
| 405 | |
| 406 | const uint32_t NodesPerBlock; |
| 407 | const uint32_t BitsPerIndex; |
| 408 | const uint32_t IndexMask; |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 409 | char *ActiveEnd = nullptr; |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 410 | std::vector<char*> Blocks; |
| 411 | typedef BumpPtrAllocatorImpl<MallocAllocator, 65536> AllocatorTy; |
| 412 | AllocatorTy MemPool; |
| 413 | }; |
| 414 | |
| 415 | struct RegisterRef { |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 416 | RegisterId Reg; |
| 417 | LaneBitmask Mask; |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 418 | |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 419 | RegisterRef() : RegisterRef(0) {} |
| Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 420 | explicit RegisterRef(RegisterId R, LaneBitmask M = LaneBitmask::getAll()) |
| 421 | : Reg(R), Mask(R != 0 ? M : LaneBitmask::getNone()) {} |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 422 | |
| Krzysztof Parzyszek | ea9f8ce | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 423 | operator bool() const { return Reg != 0 && Mask.any(); } |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 424 | bool operator== (const RegisterRef &RR) const { |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 425 | return Reg == RR.Reg && Mask == RR.Mask; |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 426 | } |
| 427 | bool operator!= (const RegisterRef &RR) const { |
| 428 | return !operator==(RR); |
| 429 | } |
| 430 | bool operator< (const RegisterRef &RR) const { |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 431 | return Reg < RR.Reg || (Reg == RR.Reg && Mask < RR.Mask); |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 432 | } |
| 433 | }; |
| 434 | typedef std::set<RegisterRef> RegisterSet; |
| 435 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 436 | struct TargetOperandInfo { |
| 437 | TargetOperandInfo(const TargetInstrInfo &tii) : TII(tii) {} |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 438 | virtual ~TargetOperandInfo() = default; |
| 439 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 440 | virtual bool isPreserving(const MachineInstr &In, unsigned OpNum) const; |
| 441 | virtual bool isClobbering(const MachineInstr &In, unsigned OpNum) const; |
| 442 | virtual bool isFixedReg(const MachineInstr &In, unsigned OpNum) const; |
| 443 | |
| 444 | const TargetInstrInfo &TII; |
| 445 | }; |
| 446 | |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 447 | // Packed register reference. Only used for storage. |
| 448 | struct PackedRegisterRef { |
| 449 | RegisterId Reg; |
| 450 | uint32_t MaskId; |
| 451 | }; |
| 452 | |
| Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 453 | // Template class for a map translating uint32_t into arbitrary types. |
| 454 | // The map will act like an indexed set: upon insertion of a new object, |
| 455 | // it will automatically assign a new index to it. Index of 0 is treated |
| 456 | // as invalid and is never allocated. |
| 457 | template <typename T, unsigned N = 32> |
| 458 | struct IndexedSet { |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 459 | IndexedSet() : Map() { Map.reserve(N); } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 460 | |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 461 | T get(uint32_t Idx) const { |
| Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 462 | // Index Idx corresponds to Map[Idx-1]. |
| 463 | assert(Idx != 0 && !Map.empty() && Idx-1 < Map.size()); |
| 464 | return Map[Idx-1]; |
| 465 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 466 | |
| Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 467 | uint32_t insert(T Val) { |
| 468 | // Linear search. |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 469 | auto F = llvm::find(Map, Val); |
| Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 470 | if (F != Map.end()) |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 471 | return F - Map.begin() + 1; |
| Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 472 | Map.push_back(Val); |
| 473 | return Map.size(); // Return actual_index + 1. |
| 474 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 475 | |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 476 | uint32_t find(T Val) const { |
| 477 | auto F = llvm::find(Map, Val); |
| 478 | assert(F != Map.end()); |
| Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 479 | return F - Map.begin(); |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 480 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 481 | |
| Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 482 | private: |
| 483 | std::vector<T> Map; |
| 484 | }; |
| 485 | |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 486 | struct LaneMaskIndex : private IndexedSet<LaneBitmask> { |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 487 | LaneMaskIndex() = default; |
| 488 | |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 489 | LaneBitmask getLaneMaskForIndex(uint32_t K) const { |
| Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 490 | return K == 0 ? LaneBitmask::getAll() : get(K); |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 491 | } |
| 492 | uint32_t getIndexForLaneMask(LaneBitmask LM) { |
| Krzysztof Parzyszek | ea9f8ce | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 493 | assert(LM.any()); |
| Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 494 | return LM.all() ? 0 : insert(LM); |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 495 | } |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 496 | uint32_t getIndexForLaneMask(LaneBitmask LM) const { |
| Krzysztof Parzyszek | ea9f8ce | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 497 | assert(LM.any()); |
| Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 498 | return LM.all() ? 0 : find(LM); |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 499 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 500 | |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 501 | PackedRegisterRef pack(RegisterRef RR) { |
| 502 | return { RR.Reg, getIndexForLaneMask(RR.Mask) }; |
| 503 | } |
| 504 | PackedRegisterRef pack(RegisterRef RR) const { |
| 505 | return { RR.Reg, getIndexForLaneMask(RR.Mask) }; |
| 506 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 507 | |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 508 | RegisterRef unpack(PackedRegisterRef PR) const { |
| 509 | return RegisterRef(PR.Reg, getLaneMaskForIndex(PR.MaskId)); |
| 510 | } |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 511 | }; |
| 512 | |
| 513 | struct RegisterAggr { |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 514 | RegisterAggr(const TargetRegisterInfo &tri) |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 515 | : ExpAliasUnits(tri.getNumRegUnits()), CheckUnits(false), TRI(tri) {} |
| 516 | RegisterAggr(const RegisterAggr &RG) = default; |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 517 | |
| 518 | bool empty() const { return Masks.empty(); } |
| 519 | bool hasAliasOf(RegisterRef RR) const; |
| 520 | bool hasCoverOf(RegisterRef RR) const; |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 521 | static bool isCoverOf(RegisterRef RA, RegisterRef RB, |
| 522 | const TargetRegisterInfo &TRI) { |
| 523 | return RegisterAggr(TRI).insert(RA).hasCoverOf(RB); |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | RegisterAggr &insert(RegisterRef RR); |
| 527 | RegisterAggr &insert(const RegisterAggr &RG); |
| 528 | RegisterAggr &clear(RegisterRef RR); |
| Krzysztof Parzyszek | 7bb63ac | 2016-10-19 16:30:56 +0000 | [diff] [blame] | 529 | RegisterAggr &clear(const RegisterAggr &RG); |
| 530 | |
| 531 | RegisterRef clearIn(RegisterRef RR) const; |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 532 | |
| 533 | void print(raw_ostream &OS) const; |
| 534 | |
| 535 | private: |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 536 | typedef std::unordered_map<RegisterId, LaneBitmask> MapType; |
| 537 | |
| 538 | public: |
| 539 | typedef MapType::const_iterator iterator; |
| 540 | iterator begin() const { return Masks.begin(); } |
| 541 | iterator end() const { return Masks.end(); } |
| 542 | RegisterRef normalize(RegisterRef RR) const; |
| 543 | |
| 544 | private: |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 545 | MapType Masks; |
| 546 | BitVector ExpAliasUnits; // Register units for explicit aliases. |
| 547 | bool CheckUnits; |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 548 | const TargetRegisterInfo &TRI; |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 549 | }; |
| 550 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 551 | struct NodeBase { |
| 552 | public: |
| 553 | // Make sure this is a POD. |
| 554 | NodeBase() = default; |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 555 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 556 | uint16_t getType() const { return NodeAttrs::type(Attrs); } |
| 557 | uint16_t getKind() const { return NodeAttrs::kind(Attrs); } |
| 558 | uint16_t getFlags() const { return NodeAttrs::flags(Attrs); } |
| 559 | NodeId getNext() const { return Next; } |
| 560 | |
| 561 | uint16_t getAttrs() const { return Attrs; } |
| 562 | void setAttrs(uint16_t A) { Attrs = A; } |
| 563 | void setFlags(uint16_t F) { setAttrs(NodeAttrs::set_flags(getAttrs(), F)); } |
| 564 | |
| 565 | // Insert node NA after "this" in the circular chain. |
| 566 | void append(NodeAddr<NodeBase*> NA); |
| 567 | // Initialize all members to 0. |
| 568 | void init() { memset(this, 0, sizeof *this); } |
| 569 | void setNext(NodeId N) { Next = N; } |
| 570 | |
| 571 | protected: |
| 572 | uint16_t Attrs; |
| 573 | uint16_t Reserved; |
| 574 | NodeId Next; // Id of the next node in the circular chain. |
| 575 | // Definitions of nested types. Using anonymous nested structs would make |
| 576 | // this class definition clearer, but unnamed structs are not a part of |
| 577 | // the standard. |
| 578 | struct Def_struct { |
| 579 | NodeId DD, DU; // Ids of the first reached def and use. |
| 580 | }; |
| 581 | struct PhiU_struct { |
| 582 | NodeId PredB; // Id of the predecessor block for a phi use. |
| 583 | }; |
| 584 | struct Code_struct { |
| 585 | void *CP; // Pointer to the actual code. |
| 586 | NodeId FirstM, LastM; // Id of the first member and last. |
| 587 | }; |
| 588 | struct Ref_struct { |
| 589 | NodeId RD, Sib; // Ids of the reaching def and the sibling. |
| 590 | union { |
| 591 | Def_struct Def; |
| 592 | PhiU_struct PhiU; |
| 593 | }; |
| 594 | union { |
| 595 | MachineOperand *Op; // Non-phi refs point to a machine operand. |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 596 | PackedRegisterRef PR; // Phi refs store register info directly. |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 597 | }; |
| 598 | }; |
| 599 | |
| 600 | // The actual payload. |
| 601 | union { |
| 602 | Ref_struct Ref; |
| 603 | Code_struct Code; |
| 604 | }; |
| 605 | }; |
| 606 | // The allocator allocates chunks of 32 bytes for each node. The fact that |
| 607 | // each node takes 32 bytes in memory is used for fast translation between |
| 608 | // the node id and the node address. |
| 609 | static_assert(sizeof(NodeBase) <= NodeAllocator::NodeMemSize, |
| 610 | "NodeBase must be at most NodeAllocator::NodeMemSize bytes"); |
| 611 | |
| 612 | typedef std::vector<NodeAddr<NodeBase*>> NodeList; |
| 613 | typedef std::set<NodeId> NodeSet; |
| 614 | |
| 615 | struct RefNode : public NodeBase { |
| 616 | RefNode() = default; |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 617 | |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 618 | RegisterRef getRegRef(const DataFlowGraph &G) const; |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 619 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 620 | MachineOperand &getOp() { |
| 621 | assert(!(getFlags() & NodeAttrs::PhiRef)); |
| 622 | return *Ref.Op; |
| 623 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 624 | |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 625 | void setRegRef(RegisterRef RR, DataFlowGraph &G); |
| 626 | void setRegRef(MachineOperand *Op, DataFlowGraph &G); |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 627 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 628 | NodeId getReachingDef() const { |
| 629 | return Ref.RD; |
| 630 | } |
| 631 | void setReachingDef(NodeId RD) { |
| 632 | Ref.RD = RD; |
| 633 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 634 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 635 | NodeId getSibling() const { |
| 636 | return Ref.Sib; |
| 637 | } |
| 638 | void setSibling(NodeId Sib) { |
| 639 | Ref.Sib = Sib; |
| 640 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 641 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 642 | bool isUse() const { |
| 643 | assert(getType() == NodeAttrs::Ref); |
| 644 | return getKind() == NodeAttrs::Use; |
| 645 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 646 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 647 | bool isDef() const { |
| 648 | assert(getType() == NodeAttrs::Ref); |
| 649 | return getKind() == NodeAttrs::Def; |
| 650 | } |
| 651 | |
| 652 | template <typename Predicate> |
| 653 | NodeAddr<RefNode*> getNextRef(RegisterRef RR, Predicate P, bool NextOnly, |
| 654 | const DataFlowGraph &G); |
| 655 | NodeAddr<NodeBase*> getOwner(const DataFlowGraph &G); |
| 656 | }; |
| 657 | |
| 658 | struct DefNode : public RefNode { |
| 659 | NodeId getReachedDef() const { |
| 660 | return Ref.Def.DD; |
| 661 | } |
| 662 | void setReachedDef(NodeId D) { |
| 663 | Ref.Def.DD = D; |
| 664 | } |
| 665 | NodeId getReachedUse() const { |
| 666 | return Ref.Def.DU; |
| 667 | } |
| 668 | void setReachedUse(NodeId U) { |
| 669 | Ref.Def.DU = U; |
| 670 | } |
| 671 | |
| 672 | void linkToDef(NodeId Self, NodeAddr<DefNode*> DA); |
| 673 | }; |
| 674 | |
| 675 | struct UseNode : public RefNode { |
| 676 | void linkToDef(NodeId Self, NodeAddr<DefNode*> DA); |
| 677 | }; |
| 678 | |
| 679 | struct PhiUseNode : public UseNode { |
| 680 | NodeId getPredecessor() const { |
| 681 | assert(getFlags() & NodeAttrs::PhiRef); |
| 682 | return Ref.PhiU.PredB; |
| 683 | } |
| 684 | void setPredecessor(NodeId B) { |
| 685 | assert(getFlags() & NodeAttrs::PhiRef); |
| 686 | Ref.PhiU.PredB = B; |
| 687 | } |
| 688 | }; |
| 689 | |
| 690 | struct CodeNode : public NodeBase { |
| 691 | template <typename T> T getCode() const { |
| 692 | return static_cast<T>(Code.CP); |
| 693 | } |
| 694 | void setCode(void *C) { |
| 695 | Code.CP = C; |
| 696 | } |
| 697 | |
| 698 | NodeAddr<NodeBase*> getFirstMember(const DataFlowGraph &G) const; |
| 699 | NodeAddr<NodeBase*> getLastMember(const DataFlowGraph &G) const; |
| 700 | void addMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G); |
| 701 | void addMemberAfter(NodeAddr<NodeBase*> MA, NodeAddr<NodeBase*> NA, |
| 702 | const DataFlowGraph &G); |
| 703 | void removeMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G); |
| 704 | |
| 705 | NodeList members(const DataFlowGraph &G) const; |
| 706 | template <typename Predicate> |
| 707 | NodeList members_if(Predicate P, const DataFlowGraph &G) const; |
| 708 | }; |
| 709 | |
| 710 | struct InstrNode : public CodeNode { |
| 711 | NodeAddr<NodeBase*> getOwner(const DataFlowGraph &G); |
| 712 | }; |
| 713 | |
| 714 | struct PhiNode : public InstrNode { |
| 715 | MachineInstr *getCode() const { |
| 716 | return nullptr; |
| 717 | } |
| 718 | }; |
| 719 | |
| 720 | struct StmtNode : public InstrNode { |
| 721 | MachineInstr *getCode() const { |
| 722 | return CodeNode::getCode<MachineInstr*>(); |
| 723 | } |
| 724 | }; |
| 725 | |
| 726 | struct BlockNode : public CodeNode { |
| 727 | MachineBasicBlock *getCode() const { |
| 728 | return CodeNode::getCode<MachineBasicBlock*>(); |
| 729 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 730 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 731 | void addPhi(NodeAddr<PhiNode*> PA, const DataFlowGraph &G); |
| 732 | }; |
| 733 | |
| 734 | struct FuncNode : public CodeNode { |
| 735 | MachineFunction *getCode() const { |
| 736 | return CodeNode::getCode<MachineFunction*>(); |
| 737 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 738 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 739 | NodeAddr<BlockNode*> findBlock(const MachineBasicBlock *BB, |
| 740 | const DataFlowGraph &G) const; |
| 741 | NodeAddr<BlockNode*> getEntryBlock(const DataFlowGraph &G); |
| 742 | }; |
| 743 | |
| 744 | struct DataFlowGraph { |
| 745 | DataFlowGraph(MachineFunction &mf, const TargetInstrInfo &tii, |
| 746 | const TargetRegisterInfo &tri, const MachineDominatorTree &mdt, |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 747 | const MachineDominanceFrontier &mdf, const TargetOperandInfo &toi); |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 748 | |
| 749 | NodeBase *ptr(NodeId N) const; |
| 750 | template <typename T> T ptr(NodeId N) const { |
| 751 | return static_cast<T>(ptr(N)); |
| 752 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 753 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 754 | NodeId id(const NodeBase *P) const; |
| 755 | |
| 756 | template <typename T> NodeAddr<T> addr(NodeId N) const { |
| 757 | return { ptr<T>(N), N }; |
| 758 | } |
| 759 | |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 760 | NodeAddr<FuncNode*> getFunc() const { return Func; } |
| 761 | MachineFunction &getMF() const { return MF; } |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 762 | const TargetInstrInfo &getTII() const { return TII; } |
| 763 | const TargetRegisterInfo &getTRI() const { return TRI; } |
| 764 | const MachineDominatorTree &getDT() const { return MDT; } |
| 765 | const MachineDominanceFrontier &getDF() const { return MDF; } |
| Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 766 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 767 | struct DefStack { |
| 768 | DefStack() = default; |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 769 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 770 | bool empty() const { return Stack.empty() || top() == bottom(); } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 771 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 772 | private: |
| 773 | typedef NodeAddr<DefNode*> value_type; |
| 774 | struct Iterator { |
| 775 | typedef DefStack::value_type value_type; |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 776 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 777 | Iterator &up() { Pos = DS.nextUp(Pos); return *this; } |
| 778 | Iterator &down() { Pos = DS.nextDown(Pos); return *this; } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 779 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 780 | value_type operator*() const { |
| 781 | assert(Pos >= 1); |
| 782 | return DS.Stack[Pos-1]; |
| 783 | } |
| 784 | const value_type *operator->() const { |
| 785 | assert(Pos >= 1); |
| 786 | return &DS.Stack[Pos-1]; |
| 787 | } |
| 788 | bool operator==(const Iterator &It) const { return Pos == It.Pos; } |
| 789 | bool operator!=(const Iterator &It) const { return Pos != It.Pos; } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 790 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 791 | private: |
| 792 | Iterator(const DefStack &S, bool Top); |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 793 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 794 | // Pos-1 is the index in the StorageType object that corresponds to |
| 795 | // the top of the DefStack. |
| 796 | const DefStack &DS; |
| 797 | unsigned Pos; |
| 798 | friend struct DefStack; |
| 799 | }; |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 800 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 801 | public: |
| 802 | typedef Iterator iterator; |
| 803 | iterator top() const { return Iterator(*this, true); } |
| 804 | iterator bottom() const { return Iterator(*this, false); } |
| 805 | unsigned size() const; |
| 806 | |
| 807 | void push(NodeAddr<DefNode*> DA) { Stack.push_back(DA); } |
| 808 | void pop(); |
| 809 | void start_block(NodeId N); |
| 810 | void clear_block(NodeId N); |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 811 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 812 | private: |
| 813 | friend struct Iterator; |
| 814 | typedef std::vector<value_type> StorageType; |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 815 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 816 | bool isDelimiter(const StorageType::value_type &P, NodeId N = 0) const { |
| 817 | return (P.Addr == nullptr) && (N == 0 || P.Id == N); |
| 818 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 819 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 820 | unsigned nextUp(unsigned P) const; |
| 821 | unsigned nextDown(unsigned P) const; |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 822 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 823 | StorageType Stack; |
| 824 | }; |
| 825 | |
| Krzysztof Parzyszek | 047149f | 2016-07-22 16:09:47 +0000 | [diff] [blame] | 826 | // Make this std::unordered_map for speed of accessing elements. |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 827 | // Map: Register (physical or virtual) -> DefStack |
| Krzysztof Parzyszek | 6e7fa99 | 2016-10-21 19:12:13 +0000 | [diff] [blame] | 828 | typedef std::unordered_map<RegisterId,DefStack> DefStackMap; |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 829 | |
| Krzysztof Parzyszek | 55874cf | 2016-04-28 20:17:06 +0000 | [diff] [blame] | 830 | void build(unsigned Options = BuildOptions::None); |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 831 | void pushDefs(NodeAddr<InstrNode*> IA, DefStackMap &DM); |
| 832 | void markBlock(NodeId B, DefStackMap &DefM); |
| 833 | void releaseBlock(NodeId B, DefStackMap &DefM); |
| 834 | |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 835 | PackedRegisterRef pack(RegisterRef RR) { return LMI.pack(RR); } |
| 836 | PackedRegisterRef pack(RegisterRef RR) const { return LMI.pack(RR); } |
| 837 | RegisterRef unpack(PackedRegisterRef PR) const { return LMI.unpack(PR); } |
| 838 | RegisterRef makeRegRef(unsigned Reg, unsigned Sub) const; |
| Krzysztof Parzyszek | 7bb63ac | 2016-10-19 16:30:56 +0000 | [diff] [blame] | 839 | RegisterRef normalizeRef(RegisterRef RR) const; |
| 840 | RegisterRef restrictRef(RegisterRef AR, RegisterRef BR) const; |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 841 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 842 | NodeAddr<RefNode*> getNextRelated(NodeAddr<InstrNode*> IA, |
| 843 | NodeAddr<RefNode*> RA) const; |
| 844 | NodeAddr<RefNode*> getNextImp(NodeAddr<InstrNode*> IA, |
| 845 | NodeAddr<RefNode*> RA, bool Create); |
| 846 | NodeAddr<RefNode*> getNextImp(NodeAddr<InstrNode*> IA, |
| 847 | NodeAddr<RefNode*> RA) const; |
| 848 | NodeAddr<RefNode*> getNextShadow(NodeAddr<InstrNode*> IA, |
| 849 | NodeAddr<RefNode*> RA, bool Create); |
| 850 | NodeAddr<RefNode*> getNextShadow(NodeAddr<InstrNode*> IA, |
| 851 | NodeAddr<RefNode*> RA) const; |
| 852 | |
| 853 | NodeList getRelatedRefs(NodeAddr<InstrNode*> IA, |
| 854 | NodeAddr<RefNode*> RA) const; |
| 855 | |
| Krzysztof Parzyszek | 69e670d5 | 2016-01-18 20:41:34 +0000 | [diff] [blame] | 856 | void unlinkUse(NodeAddr<UseNode*> UA, bool RemoveFromOwner) { |
| 857 | unlinkUseDF(UA); |
| 858 | if (RemoveFromOwner) |
| 859 | removeFromOwner(UA); |
| 860 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 861 | |
| Krzysztof Parzyszek | 69e670d5 | 2016-01-18 20:41:34 +0000 | [diff] [blame] | 862 | void unlinkDef(NodeAddr<DefNode*> DA, bool RemoveFromOwner) { |
| 863 | unlinkDefDF(DA); |
| 864 | if (RemoveFromOwner) |
| 865 | removeFromOwner(DA); |
| 866 | } |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 867 | |
| 868 | // Some useful filters. |
| 869 | template <uint16_t Kind> |
| 870 | static bool IsRef(const NodeAddr<NodeBase*> BA) { |
| 871 | return BA.Addr->getType() == NodeAttrs::Ref && |
| 872 | BA.Addr->getKind() == Kind; |
| 873 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 874 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 875 | template <uint16_t Kind> |
| 876 | static bool IsCode(const NodeAddr<NodeBase*> BA) { |
| 877 | return BA.Addr->getType() == NodeAttrs::Code && |
| 878 | BA.Addr->getKind() == Kind; |
| 879 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 880 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 881 | static bool IsDef(const NodeAddr<NodeBase*> BA) { |
| 882 | return BA.Addr->getType() == NodeAttrs::Ref && |
| 883 | BA.Addr->getKind() == NodeAttrs::Def; |
| 884 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 885 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 886 | static bool IsUse(const NodeAddr<NodeBase*> BA) { |
| 887 | return BA.Addr->getType() == NodeAttrs::Ref && |
| 888 | BA.Addr->getKind() == NodeAttrs::Use; |
| 889 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 890 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 891 | static bool IsPhi(const NodeAddr<NodeBase*> BA) { |
| 892 | return BA.Addr->getType() == NodeAttrs::Code && |
| 893 | BA.Addr->getKind() == NodeAttrs::Phi; |
| 894 | } |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 895 | |
| Krzysztof Parzyszek | 1ff9952 | 2016-09-07 20:10:56 +0000 | [diff] [blame] | 896 | static bool IsPreservingDef(const NodeAddr<DefNode*> DA) { |
| 897 | uint16_t Flags = DA.Addr->getFlags(); |
| 898 | return (Flags & NodeAttrs::Preserving) && !(Flags & NodeAttrs::Undef); |
| 899 | } |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 900 | |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 901 | // Register aliasing. |
| 902 | bool alias(RegisterRef RA, RegisterRef RB) const; |
| 903 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 904 | private: |
| 905 | void reset(); |
| 906 | |
| Krzysztof Parzyszek | 6e7fa99 | 2016-10-21 19:12:13 +0000 | [diff] [blame] | 907 | RegisterSet getAliasSet(RegisterId Reg) const; |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 908 | RegisterSet getLandingPadLiveIns() const; |
| Krzysztof Parzyszek | 1d32220 | 2016-09-27 18:18:44 +0000 | [diff] [blame] | 909 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 910 | NodeAddr<NodeBase*> newNode(uint16_t Attrs); |
| 911 | NodeAddr<NodeBase*> cloneNode(const NodeAddr<NodeBase*> B); |
| 912 | NodeAddr<UseNode*> newUse(NodeAddr<InstrNode*> Owner, |
| 913 | MachineOperand &Op, uint16_t Flags = NodeAttrs::None); |
| 914 | NodeAddr<PhiUseNode*> newPhiUse(NodeAddr<PhiNode*> Owner, |
| 915 | RegisterRef RR, NodeAddr<BlockNode*> PredB, |
| 916 | uint16_t Flags = NodeAttrs::PhiRef); |
| 917 | NodeAddr<DefNode*> newDef(NodeAddr<InstrNode*> Owner, |
| 918 | MachineOperand &Op, uint16_t Flags = NodeAttrs::None); |
| 919 | NodeAddr<DefNode*> newDef(NodeAddr<InstrNode*> Owner, |
| 920 | RegisterRef RR, uint16_t Flags = NodeAttrs::PhiRef); |
| 921 | NodeAddr<PhiNode*> newPhi(NodeAddr<BlockNode*> Owner); |
| 922 | NodeAddr<StmtNode*> newStmt(NodeAddr<BlockNode*> Owner, |
| 923 | MachineInstr *MI); |
| 924 | NodeAddr<BlockNode*> newBlock(NodeAddr<FuncNode*> Owner, |
| 925 | MachineBasicBlock *BB); |
| 926 | NodeAddr<FuncNode*> newFunc(MachineFunction *MF); |
| 927 | |
| 928 | template <typename Predicate> |
| 929 | std::pair<NodeAddr<RefNode*>,NodeAddr<RefNode*>> |
| 930 | locateNextRef(NodeAddr<InstrNode*> IA, NodeAddr<RefNode*> RA, |
| 931 | Predicate P) const; |
| 932 | |
| 933 | typedef std::map<NodeId,RegisterSet> BlockRefsMap; |
| 934 | |
| 935 | void buildStmt(NodeAddr<BlockNode*> BA, MachineInstr &In); |
| 936 | void buildBlockRefs(NodeAddr<BlockNode*> BA, BlockRefsMap &RefM); |
| 937 | void recordDefsForDF(BlockRefsMap &PhiM, BlockRefsMap &RefM, |
| 938 | NodeAddr<BlockNode*> BA); |
| 939 | void buildPhis(BlockRefsMap &PhiM, BlockRefsMap &RefM, |
| 940 | NodeAddr<BlockNode*> BA); |
| 941 | void removeUnusedPhis(); |
| 942 | |
| 943 | template <typename T> void linkRefUp(NodeAddr<InstrNode*> IA, |
| 944 | NodeAddr<T> TA, DefStack &DS); |
| 945 | void linkStmtRefs(DefStackMap &DefM, NodeAddr<StmtNode*> SA); |
| 946 | void linkBlockRefs(DefStackMap &DefM, NodeAddr<BlockNode*> BA); |
| 947 | |
| Krzysztof Parzyszek | 69e670d5 | 2016-01-18 20:41:34 +0000 | [diff] [blame] | 948 | void unlinkUseDF(NodeAddr<UseNode*> UA); |
| 949 | void unlinkDefDF(NodeAddr<DefNode*> DA); |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 950 | |
| Krzysztof Parzyszek | 69e670d5 | 2016-01-18 20:41:34 +0000 | [diff] [blame] | 951 | void removeFromOwner(NodeAddr<RefNode*> RA) { |
| 952 | NodeAddr<InstrNode*> IA = RA.Addr->getOwner(*this); |
| 953 | IA.Addr->removeMember(RA, *this); |
| 954 | } |
| 955 | |
| Krzysztof Parzyszek | 047149f | 2016-07-22 16:09:47 +0000 | [diff] [blame] | 956 | NodeAddr<BlockNode*> findBlock(MachineBasicBlock *BB) { |
| 957 | return BlockNodes[BB]; |
| 958 | } |
| 959 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 960 | NodeAddr<FuncNode*> Func; |
| 961 | NodeAllocator Memory; |
| Krzysztof Parzyszek | 047149f | 2016-07-22 16:09:47 +0000 | [diff] [blame] | 962 | // Local map: MachineBasicBlock -> NodeAddr<BlockNode*> |
| 963 | std::map<MachineBasicBlock*,NodeAddr<BlockNode*>> BlockNodes; |
| Krzysztof Parzyszek | 29e93f3 | 2016-09-22 21:01:24 +0000 | [diff] [blame] | 964 | // Lane mask map. |
| Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 965 | LaneMaskIndex LMI; |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 966 | |
| 967 | MachineFunction &MF; |
| 968 | const TargetInstrInfo &TII; |
| 969 | const TargetRegisterInfo &TRI; |
| 970 | const MachineDominatorTree &MDT; |
| 971 | const MachineDominanceFrontier &MDF; |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 972 | const TargetOperandInfo &TOI; |
| 973 | }; // struct DataFlowGraph |
| 974 | |
| 975 | template <typename Predicate> |
| 976 | NodeAddr<RefNode*> RefNode::getNextRef(RegisterRef RR, Predicate P, |
| 977 | bool NextOnly, const DataFlowGraph &G) { |
| 978 | // Get the "Next" reference in the circular list that references RR and |
| 979 | // satisfies predicate "Pred". |
| 980 | auto NA = G.addr<NodeBase*>(getNext()); |
| 981 | |
| 982 | while (NA.Addr != this) { |
| 983 | if (NA.Addr->getType() == NodeAttrs::Ref) { |
| 984 | NodeAddr<RefNode*> RA = NA; |
| Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 985 | if (RA.Addr->getRegRef(G) == RR && P(NA)) |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 986 | return NA; |
| 987 | if (NextOnly) |
| 988 | break; |
| 989 | NA = G.addr<NodeBase*>(NA.Addr->getNext()); |
| 990 | } else { |
| 991 | // We've hit the beginning of the chain. |
| 992 | assert(NA.Addr->getType() == NodeAttrs::Code); |
| 993 | NodeAddr<CodeNode*> CA = NA; |
| 994 | NA = CA.Addr->getFirstMember(G); |
| 995 | } |
| 996 | } |
| 997 | // Return the equivalent of "nullptr" if such a node was not found. |
| 998 | return NodeAddr<RefNode*>(); |
| 999 | } |
| 1000 | |
| 1001 | template <typename Predicate> |
| 1002 | NodeList CodeNode::members_if(Predicate P, const DataFlowGraph &G) const { |
| 1003 | NodeList MM; |
| 1004 | auto M = getFirstMember(G); |
| 1005 | if (M.Id == 0) |
| 1006 | return MM; |
| 1007 | |
| 1008 | while (M.Addr != this) { |
| 1009 | if (P(M)) |
| 1010 | MM.push_back(M); |
| 1011 | M = G.addr<NodeBase*>(M.Addr->getNext()); |
| 1012 | } |
| 1013 | return MM; |
| 1014 | } |
| 1015 | |
| Krzysztof Parzyszek | 7bb63ac | 2016-10-19 16:30:56 +0000 | [diff] [blame] | 1016 | // Optionally print the lane mask, if it is not ~0. |
| 1017 | struct PrintLaneMaskOpt { |
| 1018 | PrintLaneMaskOpt(LaneBitmask M) : Mask(M) {} |
| 1019 | LaneBitmask Mask; |
| 1020 | }; |
| 1021 | raw_ostream &operator<< (raw_ostream &OS, const PrintLaneMaskOpt &P); |
| 1022 | |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 1023 | template <typename T> struct Print; |
| 1024 | template <typename T> |
| 1025 | raw_ostream &operator<< (raw_ostream &OS, const Print<T> &P); |
| 1026 | |
| 1027 | template <typename T> |
| 1028 | struct Print { |
| 1029 | Print(const T &x, const DataFlowGraph &g) : Obj(x), G(g) {} |
| 1030 | const T &Obj; |
| 1031 | const DataFlowGraph &G; |
| 1032 | }; |
| 1033 | |
| 1034 | template <typename T> |
| 1035 | struct PrintNode : Print<NodeAddr<T>> { |
| 1036 | PrintNode(const NodeAddr<T> &x, const DataFlowGraph &g) |
| 1037 | : Print<NodeAddr<T>>(x, g) {} |
| 1038 | }; |
| Krzysztof Parzyszek | b5b5a1d | 2016-01-12 15:09:49 +0000 | [diff] [blame] | 1039 | |
| Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 1040 | } // end namespace rdf |
| 1041 | |
| 1042 | } // end namespace llvm |
| 1043 | |
| 1044 | #endif // LLVM_LIB_TARGET_HEXAGON_RDFGRAPH_H |