RDFGraph.h 34 KB

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