RDFGraph.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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 <set>
  240. #include <unordered_map>
  241. #include <utility>
  242. #include <vector>
  243. // RDF uses uint32_t to refer to registers. This is to ensure that the type
  244. // size remains specific. In other places, registers are often stored using
  245. // unsigned.
  246. static_assert(sizeof(uint32_t) == sizeof(unsigned), "Those should be equal");
  247. namespace llvm {
  248. class MachineBasicBlock;
  249. class MachineDominanceFrontier;
  250. class MachineDominatorTree;
  251. class MachineFunction;
  252. class MachineInstr;
  253. class MachineOperand;
  254. class raw_ostream;
  255. class TargetInstrInfo;
  256. class TargetRegisterInfo;
  257. namespace rdf {
  258. using NodeId = uint32_t;
  259. struct DataFlowGraph;
  260. struct NodeAttrs {
  261. enum : uint16_t {
  262. None = 0x0000, // Nothing
  263. // Types: 2 bits
  264. TypeMask = 0x0003,
  265. Code = 0x0001, // 01, Container
  266. Ref = 0x0002, // 10, Reference
  267. // Kind: 3 bits
  268. KindMask = 0x0007 << 2,
  269. Def = 0x0001 << 2, // 001
  270. Use = 0x0002 << 2, // 010
  271. Phi = 0x0003 << 2, // 011
  272. Stmt = 0x0004 << 2, // 100
  273. Block = 0x0005 << 2, // 101
  274. Func = 0x0006 << 2, // 110
  275. // Flags: 7 bits for now
  276. FlagMask = 0x007F << 5,
  277. Shadow = 0x0001 << 5, // 0000001, Has extra reaching defs.
  278. Clobbering = 0x0002 << 5, // 0000010, Produces unspecified values.
  279. PhiRef = 0x0004 << 5, // 0000100, Member of PhiNode.
  280. Preserving = 0x0008 << 5, // 0001000, Def can keep original bits.
  281. Fixed = 0x0010 << 5, // 0010000, Fixed register.
  282. Undef = 0x0020 << 5, // 0100000, Has no pre-existing value.
  283. Dead = 0x0040 << 5, // 1000000, Does not define a value.
  284. };
  285. static uint16_t type(uint16_t T) { return T & TypeMask; }
  286. static uint16_t kind(uint16_t T) { return T & KindMask; }
  287. static uint16_t flags(uint16_t T) { return T & FlagMask; }
  288. static uint16_t set_type(uint16_t A, uint16_t T) {
  289. return (A & ~TypeMask) | T;
  290. }
  291. static uint16_t set_kind(uint16_t A, uint16_t K) {
  292. return (A & ~KindMask) | K;
  293. }
  294. static uint16_t set_flags(uint16_t A, uint16_t F) {
  295. return (A & ~FlagMask) | F;
  296. }
  297. // Test if A contains B.
  298. static bool contains(uint16_t A, uint16_t B) {
  299. if (type(A) != Code)
  300. return false;
  301. uint16_t KB = kind(B);
  302. switch (kind(A)) {
  303. case Func:
  304. return KB == Block;
  305. case Block:
  306. return KB == Phi || KB == Stmt;
  307. case Phi:
  308. case Stmt:
  309. return type(B) == Ref;
  310. }
  311. return false;
  312. }
  313. };
  314. struct BuildOptions {
  315. enum : unsigned {
  316. None = 0x00,
  317. KeepDeadPhis = 0x01, // Do not remove dead phis during build.
  318. };
  319. };
  320. template <typename T> struct NodeAddr {
  321. NodeAddr() = default;
  322. NodeAddr(T A, NodeId I) : Addr(A), Id(I) {}
  323. // Type cast (casting constructor). The reason for having this class
  324. // instead of std::pair.
  325. template <typename S> NodeAddr(const NodeAddr<S> &NA)
  326. : Addr(static_cast<T>(NA.Addr)), Id(NA.Id) {}
  327. bool operator== (const NodeAddr<T> &NA) const {
  328. assert((Addr == NA.Addr) == (Id == NA.Id));
  329. return Addr == NA.Addr;
  330. }
  331. bool operator!= (const NodeAddr<T> &NA) const {
  332. return !operator==(NA);
  333. }
  334. T Addr = nullptr;
  335. NodeId Id = 0;
  336. };
  337. struct NodeBase;
  338. // Fast memory allocation and translation between node id and node address.
  339. // This is really the same idea as the one underlying the "bump pointer
  340. // allocator", the difference being in the translation. A node id is
  341. // composed of two components: the index of the block in which it was
  342. // allocated, and the index within the block. With the default settings,
  343. // where the number of nodes per block is 4096, the node id (minus 1) is:
  344. //
  345. // bit position: 11 0
  346. // +----------------------------+--------------+
  347. // | Index of the block |Index in block|
  348. // +----------------------------+--------------+
  349. //
  350. // The actual node id is the above plus 1, to avoid creating a node id of 0.
  351. //
  352. // This method significantly improved the build time, compared to using maps
  353. // (std::unordered_map or DenseMap) to translate between pointers and ids.
  354. struct NodeAllocator {
  355. // Amount of storage for a single node.
  356. enum { NodeMemSize = 32 };
  357. NodeAllocator(uint32_t NPB = 4096)
  358. : NodesPerBlock(NPB), BitsPerIndex(Log2_32(NPB)),
  359. IndexMask((1 << BitsPerIndex)-1) {
  360. assert(isPowerOf2_32(NPB));
  361. }
  362. NodeBase *ptr(NodeId N) const {
  363. uint32_t N1 = N-1;
  364. uint32_t BlockN = N1 >> BitsPerIndex;
  365. uint32_t Offset = (N1 & IndexMask) * NodeMemSize;
  366. return reinterpret_cast<NodeBase*>(Blocks[BlockN]+Offset);
  367. }
  368. NodeId id(const NodeBase *P) const;
  369. NodeAddr<NodeBase*> New();
  370. void clear();
  371. private:
  372. void startNewBlock();
  373. bool needNewBlock();
  374. uint32_t makeId(uint32_t Block, uint32_t Index) const {
  375. // Add 1 to the id, to avoid the id of 0, which is treated as "null".
  376. return ((Block << BitsPerIndex) | Index) + 1;
  377. }
  378. const uint32_t NodesPerBlock;
  379. const uint32_t BitsPerIndex;
  380. const uint32_t IndexMask;
  381. char *ActiveEnd = nullptr;
  382. std::vector<char*> Blocks;
  383. using AllocatorTy = BumpPtrAllocatorImpl<MallocAllocator, 65536>;
  384. AllocatorTy MemPool;
  385. };
  386. using RegisterSet = std::set<RegisterRef>;
  387. struct TargetOperandInfo {
  388. TargetOperandInfo(const TargetInstrInfo &tii) : TII(tii) {}
  389. virtual ~TargetOperandInfo() = default;
  390. virtual bool isPreserving(const MachineInstr &In, unsigned OpNum) const;
  391. virtual bool isClobbering(const MachineInstr &In, unsigned OpNum) const;
  392. virtual bool isFixedReg(const MachineInstr &In, unsigned OpNum) const;
  393. const TargetInstrInfo &TII;
  394. };
  395. // Packed register reference. Only used for storage.
  396. struct PackedRegisterRef {
  397. RegisterId Reg;
  398. uint32_t MaskId;
  399. };
  400. struct LaneMaskIndex : private IndexedSet<LaneBitmask> {
  401. LaneMaskIndex() = default;
  402. LaneBitmask getLaneMaskForIndex(uint32_t K) const {
  403. return K == 0 ? LaneBitmask::getAll() : get(K);
  404. }
  405. uint32_t getIndexForLaneMask(LaneBitmask LM) {
  406. assert(LM.any());
  407. return LM.all() ? 0 : insert(LM);
  408. }
  409. uint32_t getIndexForLaneMask(LaneBitmask LM) const {
  410. assert(LM.any());
  411. return LM.all() ? 0 : find(LM);
  412. }
  413. };
  414. struct NodeBase {
  415. public:
  416. // Make sure this is a POD.
  417. NodeBase() = default;
  418. uint16_t getType() const { return NodeAttrs::type(Attrs); }
  419. uint16_t getKind() const { return NodeAttrs::kind(Attrs); }
  420. uint16_t getFlags() const { return NodeAttrs::flags(Attrs); }
  421. NodeId getNext() const { return Next; }
  422. uint16_t getAttrs() const { return Attrs; }
  423. void setAttrs(uint16_t A) { Attrs = A; }
  424. void setFlags(uint16_t F) { setAttrs(NodeAttrs::set_flags(getAttrs(), F)); }
  425. // Insert node NA after "this" in the circular chain.
  426. void append(NodeAddr<NodeBase*> NA);
  427. // Initialize all members to 0.
  428. void init() { memset(this, 0, sizeof *this); }
  429. void setNext(NodeId N) { Next = N; }
  430. protected:
  431. uint16_t Attrs;
  432. uint16_t Reserved;
  433. NodeId Next; // Id of the next node in the circular chain.
  434. // Definitions of nested types. Using anonymous nested structs would make
  435. // this class definition clearer, but unnamed structs are not a part of
  436. // the standard.
  437. struct Def_struct {
  438. NodeId DD, DU; // Ids of the first reached def and use.
  439. };
  440. struct PhiU_struct {
  441. NodeId PredB; // Id of the predecessor block for a phi use.
  442. };
  443. struct Code_struct {
  444. void *CP; // Pointer to the actual code.
  445. NodeId FirstM, LastM; // Id of the first member and last.
  446. };
  447. struct Ref_struct {
  448. NodeId RD, Sib; // Ids of the reaching def and the sibling.
  449. union {
  450. Def_struct Def;
  451. PhiU_struct PhiU;
  452. };
  453. union {
  454. MachineOperand *Op; // Non-phi refs point to a machine operand.
  455. PackedRegisterRef PR; // Phi refs store register info directly.
  456. };
  457. };
  458. // The actual payload.
  459. union {
  460. Ref_struct Ref;
  461. Code_struct Code;
  462. };
  463. };
  464. // The allocator allocates chunks of 32 bytes for each node. The fact that
  465. // each node takes 32 bytes in memory is used for fast translation between
  466. // the node id and the node address.
  467. static_assert(sizeof(NodeBase) <= NodeAllocator::NodeMemSize,
  468. "NodeBase must be at most NodeAllocator::NodeMemSize bytes");
  469. using NodeList = SmallVector<NodeAddr<NodeBase *>, 4>;
  470. using NodeSet = std::set<NodeId>;
  471. struct RefNode : public NodeBase {
  472. RefNode() = default;
  473. RegisterRef getRegRef(const DataFlowGraph &G) const;
  474. MachineOperand &getOp() {
  475. assert(!(getFlags() & NodeAttrs::PhiRef));
  476. return *Ref.Op;
  477. }
  478. void setRegRef(RegisterRef RR, DataFlowGraph &G);
  479. void setRegRef(MachineOperand *Op, DataFlowGraph &G);
  480. NodeId getReachingDef() const {
  481. return Ref.RD;
  482. }
  483. void setReachingDef(NodeId RD) {
  484. Ref.RD = RD;
  485. }
  486. NodeId getSibling() const {
  487. return Ref.Sib;
  488. }
  489. void setSibling(NodeId Sib) {
  490. Ref.Sib = Sib;
  491. }
  492. bool isUse() const {
  493. assert(getType() == NodeAttrs::Ref);
  494. return getKind() == NodeAttrs::Use;
  495. }
  496. bool isDef() const {
  497. assert(getType() == NodeAttrs::Ref);
  498. return getKind() == NodeAttrs::Def;
  499. }
  500. template <typename Predicate>
  501. NodeAddr<RefNode*> getNextRef(RegisterRef RR, Predicate P, bool NextOnly,
  502. const DataFlowGraph &G);
  503. NodeAddr<NodeBase*> getOwner(const DataFlowGraph &G);
  504. };
  505. struct DefNode : public RefNode {
  506. NodeId getReachedDef() const {
  507. return Ref.Def.DD;
  508. }
  509. void setReachedDef(NodeId D) {
  510. Ref.Def.DD = D;
  511. }
  512. NodeId getReachedUse() const {
  513. return Ref.Def.DU;
  514. }
  515. void setReachedUse(NodeId U) {
  516. Ref.Def.DU = U;
  517. }
  518. void linkToDef(NodeId Self, NodeAddr<DefNode*> DA);
  519. };
  520. struct UseNode : public RefNode {
  521. void linkToDef(NodeId Self, NodeAddr<DefNode*> DA);
  522. };
  523. struct PhiUseNode : public UseNode {
  524. NodeId getPredecessor() const {
  525. assert(getFlags() & NodeAttrs::PhiRef);
  526. return Ref.PhiU.PredB;
  527. }
  528. void setPredecessor(NodeId B) {
  529. assert(getFlags() & NodeAttrs::PhiRef);
  530. Ref.PhiU.PredB = B;
  531. }
  532. };
  533. struct CodeNode : public NodeBase {
  534. template <typename T> T getCode() const {
  535. return static_cast<T>(Code.CP);
  536. }
  537. void setCode(void *C) {
  538. Code.CP = C;
  539. }
  540. NodeAddr<NodeBase*> getFirstMember(const DataFlowGraph &G) const;
  541. NodeAddr<NodeBase*> getLastMember(const DataFlowGraph &G) const;
  542. void addMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G);
  543. void addMemberAfter(NodeAddr<NodeBase*> MA, NodeAddr<NodeBase*> NA,
  544. const DataFlowGraph &G);
  545. void removeMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G);
  546. NodeList members(const DataFlowGraph &G) const;
  547. template <typename Predicate>
  548. NodeList members_if(Predicate P, const DataFlowGraph &G) const;
  549. };
  550. struct InstrNode : public CodeNode {
  551. NodeAddr<NodeBase*> getOwner(const DataFlowGraph &G);
  552. };
  553. struct PhiNode : public InstrNode {
  554. MachineInstr *getCode() const {
  555. return nullptr;
  556. }
  557. };
  558. struct StmtNode : public InstrNode {
  559. MachineInstr *getCode() const {
  560. return CodeNode::getCode<MachineInstr*>();
  561. }
  562. };
  563. struct BlockNode : public CodeNode {
  564. MachineBasicBlock *getCode() const {
  565. return CodeNode::getCode<MachineBasicBlock*>();
  566. }
  567. void addPhi(NodeAddr<PhiNode*> PA, const DataFlowGraph &G);
  568. };
  569. struct FuncNode : public CodeNode {
  570. MachineFunction *getCode() const {
  571. return CodeNode::getCode<MachineFunction*>();
  572. }
  573. NodeAddr<BlockNode*> findBlock(const MachineBasicBlock *BB,
  574. const DataFlowGraph &G) const;
  575. NodeAddr<BlockNode*> getEntryBlock(const DataFlowGraph &G);
  576. };
  577. struct DataFlowGraph {
  578. DataFlowGraph(MachineFunction &mf, const TargetInstrInfo &tii,
  579. const TargetRegisterInfo &tri, const MachineDominatorTree &mdt,
  580. const MachineDominanceFrontier &mdf, const TargetOperandInfo &toi);
  581. NodeBase *ptr(NodeId N) const;
  582. template <typename T> T ptr(NodeId N) const {
  583. return static_cast<T>(ptr(N));
  584. }
  585. NodeId id(const NodeBase *P) const;
  586. template <typename T> NodeAddr<T> addr(NodeId N) const {
  587. return { ptr<T>(N), N };
  588. }
  589. NodeAddr<FuncNode*> getFunc() const { return Func; }
  590. MachineFunction &getMF() const { return MF; }
  591. const TargetInstrInfo &getTII() const { return TII; }
  592. const TargetRegisterInfo &getTRI() const { return TRI; }
  593. const PhysicalRegisterInfo &getPRI() const { return PRI; }
  594. const MachineDominatorTree &getDT() const { return MDT; }
  595. const MachineDominanceFrontier &getDF() const { return MDF; }
  596. const RegisterAggr &getLiveIns() const { return LiveIns; }
  597. struct DefStack {
  598. DefStack() = default;
  599. bool empty() const { return Stack.empty() || top() == bottom(); }
  600. private:
  601. using value_type = NodeAddr<DefNode *>;
  602. struct Iterator {
  603. using value_type = DefStack::value_type;
  604. Iterator &up() { Pos = DS.nextUp(Pos); return *this; }
  605. Iterator &down() { Pos = DS.nextDown(Pos); return *this; }
  606. value_type operator*() const {
  607. assert(Pos >= 1);
  608. return DS.Stack[Pos-1];
  609. }
  610. const value_type *operator->() const {
  611. assert(Pos >= 1);
  612. return &DS.Stack[Pos-1];
  613. }
  614. bool operator==(const Iterator &It) const { return Pos == It.Pos; }
  615. bool operator!=(const Iterator &It) const { return Pos != It.Pos; }
  616. private:
  617. friend struct DefStack;
  618. Iterator(const DefStack &S, bool Top);
  619. // Pos-1 is the index in the StorageType object that corresponds to
  620. // the top of the DefStack.
  621. const DefStack &DS;
  622. unsigned Pos;
  623. };
  624. public:
  625. using iterator = Iterator;
  626. iterator top() const { return Iterator(*this, true); }
  627. iterator bottom() const { return Iterator(*this, false); }
  628. unsigned size() const;
  629. void push(NodeAddr<DefNode*> DA) { Stack.push_back(DA); }
  630. void pop();
  631. void start_block(NodeId N);
  632. void clear_block(NodeId N);
  633. private:
  634. friend struct Iterator;
  635. using StorageType = std::vector<value_type>;
  636. bool isDelimiter(const StorageType::value_type &P, NodeId N = 0) const {
  637. return (P.Addr == nullptr) && (N == 0 || P.Id == N);
  638. }
  639. unsigned nextUp(unsigned P) const;
  640. unsigned nextDown(unsigned P) const;
  641. StorageType Stack;
  642. };
  643. // Make this std::unordered_map for speed of accessing elements.
  644. // Map: Register (physical or virtual) -> DefStack
  645. using DefStackMap = std::unordered_map<RegisterId, DefStack>;
  646. void build(unsigned Options = BuildOptions::None);
  647. void pushAllDefs(NodeAddr<InstrNode*> IA, DefStackMap &DM);
  648. void markBlock(NodeId B, DefStackMap &DefM);
  649. void releaseBlock(NodeId B, DefStackMap &DefM);
  650. PackedRegisterRef pack(RegisterRef RR) {
  651. return { RR.Reg, LMI.getIndexForLaneMask(RR.Mask) };
  652. }
  653. PackedRegisterRef pack(RegisterRef RR) const {
  654. return { RR.Reg, LMI.getIndexForLaneMask(RR.Mask) };
  655. }
  656. RegisterRef unpack(PackedRegisterRef PR) const {
  657. return RegisterRef(PR.Reg, LMI.getLaneMaskForIndex(PR.MaskId));
  658. }
  659. RegisterRef makeRegRef(unsigned Reg, unsigned Sub) const;
  660. RegisterRef makeRegRef(const MachineOperand &Op) const;
  661. RegisterRef restrictRef(RegisterRef AR, RegisterRef BR) const;
  662. NodeAddr<RefNode*> getNextRelated(NodeAddr<InstrNode*> IA,
  663. NodeAddr<RefNode*> RA) const;
  664. NodeAddr<RefNode*> getNextShadow(NodeAddr<InstrNode*> IA,
  665. NodeAddr<RefNode*> RA, bool Create);
  666. NodeAddr<RefNode*> getNextShadow(NodeAddr<InstrNode*> IA,
  667. NodeAddr<RefNode*> RA) const;
  668. NodeList getRelatedRefs(NodeAddr<InstrNode*> IA,
  669. NodeAddr<RefNode*> RA) const;
  670. NodeAddr<BlockNode*> findBlock(MachineBasicBlock *BB) const {
  671. return BlockNodes.at(BB);
  672. }
  673. void unlinkUse(NodeAddr<UseNode*> UA, bool RemoveFromOwner) {
  674. unlinkUseDF(UA);
  675. if (RemoveFromOwner)
  676. removeFromOwner(UA);
  677. }
  678. void unlinkDef(NodeAddr<DefNode*> DA, bool RemoveFromOwner) {
  679. unlinkDefDF(DA);
  680. if (RemoveFromOwner)
  681. removeFromOwner(DA);
  682. }
  683. // Some useful filters.
  684. template <uint16_t Kind>
  685. static bool IsRef(const NodeAddr<NodeBase*> BA) {
  686. return BA.Addr->getType() == NodeAttrs::Ref &&
  687. BA.Addr->getKind() == Kind;
  688. }
  689. template <uint16_t Kind>
  690. static bool IsCode(const NodeAddr<NodeBase*> BA) {
  691. return BA.Addr->getType() == NodeAttrs::Code &&
  692. BA.Addr->getKind() == Kind;
  693. }
  694. static bool IsDef(const NodeAddr<NodeBase*> BA) {
  695. return BA.Addr->getType() == NodeAttrs::Ref &&
  696. BA.Addr->getKind() == NodeAttrs::Def;
  697. }
  698. static bool IsUse(const NodeAddr<NodeBase*> BA) {
  699. return BA.Addr->getType() == NodeAttrs::Ref &&
  700. BA.Addr->getKind() == NodeAttrs::Use;
  701. }
  702. static bool IsPhi(const NodeAddr<NodeBase*> BA) {
  703. return BA.Addr->getType() == NodeAttrs::Code &&
  704. BA.Addr->getKind() == NodeAttrs::Phi;
  705. }
  706. static bool IsPreservingDef(const NodeAddr<DefNode*> DA) {
  707. uint16_t Flags = DA.Addr->getFlags();
  708. return (Flags & NodeAttrs::Preserving) && !(Flags & NodeAttrs::Undef);
  709. }
  710. private:
  711. void reset();
  712. RegisterSet getLandingPadLiveIns() const;
  713. NodeAddr<NodeBase*> newNode(uint16_t Attrs);
  714. NodeAddr<NodeBase*> cloneNode(const NodeAddr<NodeBase*> B);
  715. NodeAddr<UseNode*> newUse(NodeAddr<InstrNode*> Owner,
  716. MachineOperand &Op, uint16_t Flags = NodeAttrs::None);
  717. NodeAddr<PhiUseNode*> newPhiUse(NodeAddr<PhiNode*> Owner,
  718. RegisterRef RR, NodeAddr<BlockNode*> PredB,
  719. uint16_t Flags = NodeAttrs::PhiRef);
  720. NodeAddr<DefNode*> newDef(NodeAddr<InstrNode*> Owner,
  721. MachineOperand &Op, uint16_t Flags = NodeAttrs::None);
  722. NodeAddr<DefNode*> newDef(NodeAddr<InstrNode*> Owner,
  723. RegisterRef RR, uint16_t Flags = NodeAttrs::PhiRef);
  724. NodeAddr<PhiNode*> newPhi(NodeAddr<BlockNode*> Owner);
  725. NodeAddr<StmtNode*> newStmt(NodeAddr<BlockNode*> Owner,
  726. MachineInstr *MI);
  727. NodeAddr<BlockNode*> newBlock(NodeAddr<FuncNode*> Owner,
  728. MachineBasicBlock *BB);
  729. NodeAddr<FuncNode*> newFunc(MachineFunction *MF);
  730. template <typename Predicate>
  731. std::pair<NodeAddr<RefNode*>,NodeAddr<RefNode*>>
  732. locateNextRef(NodeAddr<InstrNode*> IA, NodeAddr<RefNode*> RA,
  733. Predicate P) const;
  734. using BlockRefsMap = std::map<NodeId, RegisterSet>;
  735. void buildStmt(NodeAddr<BlockNode*> BA, MachineInstr &In);
  736. void recordDefsForDF(BlockRefsMap &PhiM, NodeAddr<BlockNode*> BA);
  737. void buildPhis(BlockRefsMap &PhiM, RegisterSet &AllRefs,
  738. NodeAddr<BlockNode*> BA);
  739. void removeUnusedPhis();
  740. void pushClobbers(NodeAddr<InstrNode*> IA, DefStackMap &DM);
  741. void pushDefs(NodeAddr<InstrNode*> IA, DefStackMap &DM);
  742. template <typename T> void linkRefUp(NodeAddr<InstrNode*> IA,
  743. NodeAddr<T> TA, DefStack &DS);
  744. template <typename Predicate> void linkStmtRefs(DefStackMap &DefM,
  745. NodeAddr<StmtNode*> SA, Predicate P);
  746. void linkBlockRefs(DefStackMap &DefM, NodeAddr<BlockNode*> BA);
  747. void unlinkUseDF(NodeAddr<UseNode*> UA);
  748. void unlinkDefDF(NodeAddr<DefNode*> DA);
  749. void removeFromOwner(NodeAddr<RefNode*> RA) {
  750. NodeAddr<InstrNode*> IA = RA.Addr->getOwner(*this);
  751. IA.Addr->removeMember(RA, *this);
  752. }
  753. MachineFunction &MF;
  754. const TargetInstrInfo &TII;
  755. const TargetRegisterInfo &TRI;
  756. const PhysicalRegisterInfo PRI;
  757. const MachineDominatorTree &MDT;
  758. const MachineDominanceFrontier &MDF;
  759. const TargetOperandInfo &TOI;
  760. RegisterAggr LiveIns;
  761. NodeAddr<FuncNode*> Func;
  762. NodeAllocator Memory;
  763. // Local map: MachineBasicBlock -> NodeAddr<BlockNode*>
  764. std::map<MachineBasicBlock*,NodeAddr<BlockNode*>> BlockNodes;
  765. // Lane mask map.
  766. LaneMaskIndex LMI;
  767. }; // struct DataFlowGraph
  768. template <typename Predicate>
  769. NodeAddr<RefNode*> RefNode::getNextRef(RegisterRef RR, Predicate P,
  770. bool NextOnly, const DataFlowGraph &G) {
  771. // Get the "Next" reference in the circular list that references RR and
  772. // satisfies predicate "Pred".
  773. auto NA = G.addr<NodeBase*>(getNext());
  774. while (NA.Addr != this) {
  775. if (NA.Addr->getType() == NodeAttrs::Ref) {
  776. NodeAddr<RefNode*> RA = NA;
  777. if (RA.Addr->getRegRef(G) == RR && P(NA))
  778. return NA;
  779. if (NextOnly)
  780. break;
  781. NA = G.addr<NodeBase*>(NA.Addr->getNext());
  782. } else {
  783. // We've hit the beginning of the chain.
  784. assert(NA.Addr->getType() == NodeAttrs::Code);
  785. NodeAddr<CodeNode*> CA = NA;
  786. NA = CA.Addr->getFirstMember(G);
  787. }
  788. }
  789. // Return the equivalent of "nullptr" if such a node was not found.
  790. return NodeAddr<RefNode*>();
  791. }
  792. template <typename Predicate>
  793. NodeList CodeNode::members_if(Predicate P, const DataFlowGraph &G) const {
  794. NodeList MM;
  795. auto M = getFirstMember(G);
  796. if (M.Id == 0)
  797. return MM;
  798. while (M.Addr != this) {
  799. if (P(M))
  800. MM.push_back(M);
  801. M = G.addr<NodeBase*>(M.Addr->getNext());
  802. }
  803. return MM;
  804. }
  805. template <typename T>
  806. struct Print {
  807. Print(const T &x, const DataFlowGraph &g) : Obj(x), G(g) {}
  808. const T &Obj;
  809. const DataFlowGraph &G;
  810. };
  811. template <typename T>
  812. struct PrintNode : Print<NodeAddr<T>> {
  813. PrintNode(const NodeAddr<T> &x, const DataFlowGraph &g)
  814. : Print<NodeAddr<T>>(x, g) {}
  815. };
  816. raw_ostream &operator<<(raw_ostream &OS, const Print<RegisterRef> &P);
  817. raw_ostream &operator<<(raw_ostream &OS, const Print<NodeId> &P);
  818. raw_ostream &operator<<(raw_ostream &OS, const Print<NodeAddr<DefNode *>> &P);
  819. raw_ostream &operator<<(raw_ostream &OS, const Print<NodeAddr<UseNode *>> &P);
  820. raw_ostream &operator<<(raw_ostream &OS,
  821. const Print<NodeAddr<PhiUseNode *>> &P);
  822. raw_ostream &operator<<(raw_ostream &OS, const Print<NodeAddr<RefNode *>> &P);
  823. raw_ostream &operator<<(raw_ostream &OS, const Print<NodeList> &P);
  824. raw_ostream &operator<<(raw_ostream &OS, const Print<NodeSet> &P);
  825. raw_ostream &operator<<(raw_ostream &OS, const Print<NodeAddr<PhiNode *>> &P);
  826. raw_ostream &operator<<(raw_ostream &OS,
  827. const Print<NodeAddr<StmtNode *>> &P);
  828. raw_ostream &operator<<(raw_ostream &OS,
  829. const Print<NodeAddr<InstrNode *>> &P);
  830. raw_ostream &operator<<(raw_ostream &OS,
  831. const Print<NodeAddr<BlockNode *>> &P);
  832. raw_ostream &operator<<(raw_ostream &OS,
  833. const Print<NodeAddr<FuncNode *>> &P);
  834. raw_ostream &operator<<(raw_ostream &OS, const Print<RegisterSet> &P);
  835. raw_ostream &operator<<(raw_ostream &OS, const Print<RegisterAggr> &P);
  836. raw_ostream &operator<<(raw_ostream &OS,
  837. const Print<DataFlowGraph::DefStack> &P);
  838. } // end namespace rdf
  839. } // end namespace llvm
  840. #endif // LLVM_CODEGEN_RDFGRAPH_H
  841. #ifdef __GNUC__
  842. #pragma GCC diagnostic pop
  843. #endif