blob: d5faca4cd6f4b5be0321e25055a5076e637fa210 [file] [log] [blame]
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +00001//===--- RDFGraph.h ---------------------------------------------*- C++ -*-===//
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00002//
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 Parzyszek1ff99522016-09-07 20:10:56 +0000178// - 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 Parzyszek586fc122016-09-27 18:24:33 +0000194// - 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 Parzyszekb5b5a1d2016-01-12 15:09:49 +0000201//
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 Zelenkob2ca1b32017-01-04 02:02:05 +0000224
225#ifndef LLVM_LIB_TARGET_HEXAGON_RDFGRAPH_H
226#define LLVM_LIB_TARGET_HEXAGON_RDFGRAPH_H
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000227
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +0000228#include "RDFRegisters.h"
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000229#include "llvm/ADT/BitVector.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000230#include "llvm/ADT/STLExtras.h"
231#include "llvm/MC/LaneBitmask.h"
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000232#include "llvm/Support/Allocator.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000233#include "llvm/Support/MathExtras.h"
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000234#include "llvm/Support/raw_ostream.h"
Krzysztof Parzyszek29e93f32016-09-22 21:01:24 +0000235#include "llvm/Target/TargetRegisterInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000236#include <cassert>
237#include <cstdint>
238#include <cstring>
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000239#include <functional>
240#include <map>
241#include <set>
Krzysztof Parzyszek047149f2016-07-22 16:09:47 +0000242#include <unordered_map>
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000243#include <utility>
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000244#include <vector>
245
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000246// RDF uses uint32_t to refer to registers. This is to ensure that the type
247// size remains specific. In other places, registers are often stored using
248// unsigned.
249static_assert(sizeof(uint32_t) == sizeof(unsigned), "Those should be equal");
250
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000251namespace llvm {
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000252
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000253 class MachineBasicBlock;
254 class MachineFunction;
255 class MachineInstr;
256 class MachineOperand;
257 class MachineDominanceFrontier;
258 class MachineDominatorTree;
259 class TargetInstrInfo;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000260
261namespace rdf {
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000262
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000263 typedef uint32_t NodeId;
264
Krzysztof Parzyszek29e93f32016-09-22 21:01:24 +0000265 struct DataFlowGraph;
266
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000267 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 Parzyszek586fc122016-09-27 18:24:33 +0000285 // 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 Parzyszekb5b5a1d2016-01-12 15:09:49 +0000294 };
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 Zelenkob2ca1b32017-01-04 02:02:05 +0000303
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000304 static uint16_t set_kind(uint16_t A, uint16_t K) {
305 return (A & ~KindMask) | K;
306 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000307
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000308 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 Parzyszek55874cf2016-04-28 20:17:06 +0000330 struct BuildOptions {
331 enum : unsigned {
332 None = 0x00,
333 KeepDeadPhis = 0x01, // Do not remove dead phis during build.
334 };
335 };
336
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000337 template <typename T> struct NodeAddr {
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000338 NodeAddr() : Addr(nullptr) {}
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000339 NodeAddr(T A, NodeId I) : Addr(A), Id(I) {}
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000340
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000341 // 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 Parzyszekb5b5a1d2016-01-12 15:09:49 +0000346 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 Parzyszekb5b5a1d2016-01-12 15:09:49 +0000353
354 T Addr;
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000355 NodeId Id = 0;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000356 };
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 Zelenkob2ca1b32017-01-04 02:02:05 +0000379
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000380 NodeAllocator(uint32_t NPB = 4096)
381 : NodesPerBlock(NPB), BitsPerIndex(Log2_32(NPB)),
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000382 IndexMask((1 << BitsPerIndex)-1) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000383 assert(isPowerOf2_32(NPB));
384 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000385
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000386 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 Zelenkob2ca1b32017-01-04 02:02:05 +0000392
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000393 NodeId id(const NodeBase *P) const;
394 NodeAddr<NodeBase*> New();
395 void clear();
396
397 private:
398 void startNewBlock();
399 bool needNewBlock();
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000400
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000401 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 Zelenkob2ca1b32017-01-04 02:02:05 +0000409 char *ActiveEnd = nullptr;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000410 std::vector<char*> Blocks;
411 typedef BumpPtrAllocatorImpl<MallocAllocator, 65536> AllocatorTy;
412 AllocatorTy MemPool;
413 };
414
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000415 typedef std::set<RegisterRef> RegisterSet;
416
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000417 struct TargetOperandInfo {
418 TargetOperandInfo(const TargetInstrInfo &tii) : TII(tii) {}
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000419 virtual ~TargetOperandInfo() = default;
420
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000421 virtual bool isPreserving(const MachineInstr &In, unsigned OpNum) const;
422 virtual bool isClobbering(const MachineInstr &In, unsigned OpNum) const;
423 virtual bool isFixedReg(const MachineInstr &In, unsigned OpNum) const;
424
425 const TargetInstrInfo &TII;
426 };
427
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000428 // Packed register reference. Only used for storage.
429 struct PackedRegisterRef {
430 RegisterId Reg;
431 uint32_t MaskId;
432 };
433
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000434 struct LaneMaskIndex : private IndexedSet<LaneBitmask> {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000435 LaneMaskIndex() = default;
436
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000437 LaneBitmask getLaneMaskForIndex(uint32_t K) const {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000438 return K == 0 ? LaneBitmask::getAll() : get(K);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000439 }
440 uint32_t getIndexForLaneMask(LaneBitmask LM) {
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000441 assert(LM.any());
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000442 return LM.all() ? 0 : insert(LM);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000443 }
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000444 uint32_t getIndexForLaneMask(LaneBitmask LM) const {
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000445 assert(LM.any());
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000446 return LM.all() ? 0 : find(LM);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000447 }
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000448 };
449
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000450 struct NodeBase {
451 public:
452 // Make sure this is a POD.
453 NodeBase() = default;
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000454
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000455 uint16_t getType() const { return NodeAttrs::type(Attrs); }
456 uint16_t getKind() const { return NodeAttrs::kind(Attrs); }
457 uint16_t getFlags() const { return NodeAttrs::flags(Attrs); }
458 NodeId getNext() const { return Next; }
459
460 uint16_t getAttrs() const { return Attrs; }
461 void setAttrs(uint16_t A) { Attrs = A; }
462 void setFlags(uint16_t F) { setAttrs(NodeAttrs::set_flags(getAttrs(), F)); }
463
464 // Insert node NA after "this" in the circular chain.
465 void append(NodeAddr<NodeBase*> NA);
466 // Initialize all members to 0.
467 void init() { memset(this, 0, sizeof *this); }
468 void setNext(NodeId N) { Next = N; }
469
470 protected:
471 uint16_t Attrs;
472 uint16_t Reserved;
473 NodeId Next; // Id of the next node in the circular chain.
474 // Definitions of nested types. Using anonymous nested structs would make
475 // this class definition clearer, but unnamed structs are not a part of
476 // the standard.
477 struct Def_struct {
478 NodeId DD, DU; // Ids of the first reached def and use.
479 };
480 struct PhiU_struct {
481 NodeId PredB; // Id of the predecessor block for a phi use.
482 };
483 struct Code_struct {
484 void *CP; // Pointer to the actual code.
485 NodeId FirstM, LastM; // Id of the first member and last.
486 };
487 struct Ref_struct {
488 NodeId RD, Sib; // Ids of the reaching def and the sibling.
489 union {
490 Def_struct Def;
491 PhiU_struct PhiU;
492 };
493 union {
494 MachineOperand *Op; // Non-phi refs point to a machine operand.
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000495 PackedRegisterRef PR; // Phi refs store register info directly.
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000496 };
497 };
498
499 // The actual payload.
500 union {
501 Ref_struct Ref;
502 Code_struct Code;
503 };
504 };
505 // The allocator allocates chunks of 32 bytes for each node. The fact that
506 // each node takes 32 bytes in memory is used for fast translation between
507 // the node id and the node address.
508 static_assert(sizeof(NodeBase) <= NodeAllocator::NodeMemSize,
509 "NodeBase must be at most NodeAllocator::NodeMemSize bytes");
510
511 typedef std::vector<NodeAddr<NodeBase*>> NodeList;
512 typedef std::set<NodeId> NodeSet;
513
514 struct RefNode : public NodeBase {
515 RefNode() = default;
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000516
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000517 RegisterRef getRegRef(const DataFlowGraph &G) const;
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000518
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000519 MachineOperand &getOp() {
520 assert(!(getFlags() & NodeAttrs::PhiRef));
521 return *Ref.Op;
522 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000523
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000524 void setRegRef(RegisterRef RR, DataFlowGraph &G);
525 void setRegRef(MachineOperand *Op, DataFlowGraph &G);
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000526
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000527 NodeId getReachingDef() const {
528 return Ref.RD;
529 }
530 void setReachingDef(NodeId RD) {
531 Ref.RD = RD;
532 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000533
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000534 NodeId getSibling() const {
535 return Ref.Sib;
536 }
537 void setSibling(NodeId Sib) {
538 Ref.Sib = Sib;
539 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000540
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000541 bool isUse() const {
542 assert(getType() == NodeAttrs::Ref);
543 return getKind() == NodeAttrs::Use;
544 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000545
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000546 bool isDef() const {
547 assert(getType() == NodeAttrs::Ref);
548 return getKind() == NodeAttrs::Def;
549 }
550
551 template <typename Predicate>
552 NodeAddr<RefNode*> getNextRef(RegisterRef RR, Predicate P, bool NextOnly,
553 const DataFlowGraph &G);
554 NodeAddr<NodeBase*> getOwner(const DataFlowGraph &G);
555 };
556
557 struct DefNode : public RefNode {
558 NodeId getReachedDef() const {
559 return Ref.Def.DD;
560 }
561 void setReachedDef(NodeId D) {
562 Ref.Def.DD = D;
563 }
564 NodeId getReachedUse() const {
565 return Ref.Def.DU;
566 }
567 void setReachedUse(NodeId U) {
568 Ref.Def.DU = U;
569 }
570
571 void linkToDef(NodeId Self, NodeAddr<DefNode*> DA);
572 };
573
574 struct UseNode : public RefNode {
575 void linkToDef(NodeId Self, NodeAddr<DefNode*> DA);
576 };
577
578 struct PhiUseNode : public UseNode {
579 NodeId getPredecessor() const {
580 assert(getFlags() & NodeAttrs::PhiRef);
581 return Ref.PhiU.PredB;
582 }
583 void setPredecessor(NodeId B) {
584 assert(getFlags() & NodeAttrs::PhiRef);
585 Ref.PhiU.PredB = B;
586 }
587 };
588
589 struct CodeNode : public NodeBase {
590 template <typename T> T getCode() const {
591 return static_cast<T>(Code.CP);
592 }
593 void setCode(void *C) {
594 Code.CP = C;
595 }
596
597 NodeAddr<NodeBase*> getFirstMember(const DataFlowGraph &G) const;
598 NodeAddr<NodeBase*> getLastMember(const DataFlowGraph &G) const;
599 void addMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G);
600 void addMemberAfter(NodeAddr<NodeBase*> MA, NodeAddr<NodeBase*> NA,
601 const DataFlowGraph &G);
602 void removeMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G);
603
604 NodeList members(const DataFlowGraph &G) const;
605 template <typename Predicate>
606 NodeList members_if(Predicate P, const DataFlowGraph &G) const;
607 };
608
609 struct InstrNode : public CodeNode {
610 NodeAddr<NodeBase*> getOwner(const DataFlowGraph &G);
611 };
612
613 struct PhiNode : public InstrNode {
614 MachineInstr *getCode() const {
615 return nullptr;
616 }
617 };
618
619 struct StmtNode : public InstrNode {
620 MachineInstr *getCode() const {
621 return CodeNode::getCode<MachineInstr*>();
622 }
623 };
624
625 struct BlockNode : public CodeNode {
626 MachineBasicBlock *getCode() const {
627 return CodeNode::getCode<MachineBasicBlock*>();
628 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000629
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000630 void addPhi(NodeAddr<PhiNode*> PA, const DataFlowGraph &G);
631 };
632
633 struct FuncNode : public CodeNode {
634 MachineFunction *getCode() const {
635 return CodeNode::getCode<MachineFunction*>();
636 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000637
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000638 NodeAddr<BlockNode*> findBlock(const MachineBasicBlock *BB,
639 const DataFlowGraph &G) const;
640 NodeAddr<BlockNode*> getEntryBlock(const DataFlowGraph &G);
641 };
642
643 struct DataFlowGraph {
644 DataFlowGraph(MachineFunction &mf, const TargetInstrInfo &tii,
645 const TargetRegisterInfo &tri, const MachineDominatorTree &mdt,
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000646 const MachineDominanceFrontier &mdf, const TargetOperandInfo &toi);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000647
648 NodeBase *ptr(NodeId N) const;
649 template <typename T> T ptr(NodeId N) const {
650 return static_cast<T>(ptr(N));
651 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000652
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000653 NodeId id(const NodeBase *P) const;
654
655 template <typename T> NodeAddr<T> addr(NodeId N) const {
656 return { ptr<T>(N), N };
657 }
658
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000659 NodeAddr<FuncNode*> getFunc() const { return Func; }
660 MachineFunction &getMF() const { return MF; }
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000661 const TargetInstrInfo &getTII() const { return TII; }
662 const TargetRegisterInfo &getTRI() const { return TRI; }
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +0000663 const PhysicalRegisterInfo &getPRI() const { return PRI; }
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000664 const MachineDominatorTree &getDT() const { return MDT; }
665 const MachineDominanceFrontier &getDF() const { return MDF; }
Krzysztof Parzyszekb561cf92017-01-30 16:20:30 +0000666 const RegisterAggr &getLiveIns() const { return LiveIns; }
Krzysztof Parzyszek29e93f32016-09-22 21:01:24 +0000667
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000668 struct DefStack {
669 DefStack() = default;
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000670
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000671 bool empty() const { return Stack.empty() || top() == bottom(); }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000672
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000673 private:
674 typedef NodeAddr<DefNode*> value_type;
675 struct Iterator {
676 typedef DefStack::value_type value_type;
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000677
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000678 Iterator &up() { Pos = DS.nextUp(Pos); return *this; }
679 Iterator &down() { Pos = DS.nextDown(Pos); return *this; }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000680
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000681 value_type operator*() const {
682 assert(Pos >= 1);
683 return DS.Stack[Pos-1];
684 }
685 const value_type *operator->() const {
686 assert(Pos >= 1);
687 return &DS.Stack[Pos-1];
688 }
689 bool operator==(const Iterator &It) const { return Pos == It.Pos; }
690 bool operator!=(const Iterator &It) const { return Pos != It.Pos; }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000691
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000692 private:
693 Iterator(const DefStack &S, bool Top);
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000694
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000695 // Pos-1 is the index in the StorageType object that corresponds to
696 // the top of the DefStack.
697 const DefStack &DS;
698 unsigned Pos;
699 friend struct DefStack;
700 };
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000701
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000702 public:
703 typedef Iterator iterator;
704 iterator top() const { return Iterator(*this, true); }
705 iterator bottom() const { return Iterator(*this, false); }
706 unsigned size() const;
707
708 void push(NodeAddr<DefNode*> DA) { Stack.push_back(DA); }
709 void pop();
710 void start_block(NodeId N);
711 void clear_block(NodeId N);
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000712
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000713 private:
714 friend struct Iterator;
715 typedef std::vector<value_type> StorageType;
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000716
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000717 bool isDelimiter(const StorageType::value_type &P, NodeId N = 0) const {
718 return (P.Addr == nullptr) && (N == 0 || P.Id == N);
719 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000720
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000721 unsigned nextUp(unsigned P) const;
722 unsigned nextDown(unsigned P) const;
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000723
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000724 StorageType Stack;
725 };
726
Krzysztof Parzyszek047149f2016-07-22 16:09:47 +0000727 // Make this std::unordered_map for speed of accessing elements.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000728 // Map: Register (physical or virtual) -> DefStack
Krzysztof Parzyszek6e7fa992016-10-21 19:12:13 +0000729 typedef std::unordered_map<RegisterId,DefStack> DefStackMap;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000730
Krzysztof Parzyszek55874cf2016-04-28 20:17:06 +0000731 void build(unsigned Options = BuildOptions::None);
Krzysztof Parzyszek84cd4ea2017-02-16 18:53:04 +0000732 void pushAllDefs(NodeAddr<InstrNode*> IA, DefStackMap &DM);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000733 void markBlock(NodeId B, DefStackMap &DefM);
734 void releaseBlock(NodeId B, DefStackMap &DefM);
735
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +0000736 PackedRegisterRef pack(RegisterRef RR) {
737 return { RR.Reg, LMI.getIndexForLaneMask(RR.Mask) };
738 }
739 PackedRegisterRef pack(RegisterRef RR) const {
740 return { RR.Reg, LMI.getIndexForLaneMask(RR.Mask) };
741 }
742 RegisterRef unpack(PackedRegisterRef PR) const {
743 return RegisterRef(PR.Reg, LMI.getLaneMaskForIndex(PR.MaskId));
744 }
745
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000746 RegisterRef makeRegRef(unsigned Reg, unsigned Sub) const;
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +0000747 RegisterRef makeRegRef(const MachineOperand &Op) const;
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000748 RegisterRef restrictRef(RegisterRef AR, RegisterRef BR) const;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000749
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000750 NodeAddr<RefNode*> getNextRelated(NodeAddr<InstrNode*> IA,
751 NodeAddr<RefNode*> RA) const;
752 NodeAddr<RefNode*> getNextImp(NodeAddr<InstrNode*> IA,
753 NodeAddr<RefNode*> RA, bool Create);
754 NodeAddr<RefNode*> getNextImp(NodeAddr<InstrNode*> IA,
755 NodeAddr<RefNode*> RA) const;
756 NodeAddr<RefNode*> getNextShadow(NodeAddr<InstrNode*> IA,
757 NodeAddr<RefNode*> RA, bool Create);
758 NodeAddr<RefNode*> getNextShadow(NodeAddr<InstrNode*> IA,
759 NodeAddr<RefNode*> RA) const;
760
761 NodeList getRelatedRefs(NodeAddr<InstrNode*> IA,
762 NodeAddr<RefNode*> RA) const;
763
Krzysztof Parzyszek0b8f1842017-03-10 22:42:17 +0000764 NodeAddr<BlockNode*> findBlock(MachineBasicBlock *BB) const {
765 return BlockNodes.at(BB);
766 }
767
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +0000768 void unlinkUse(NodeAddr<UseNode*> UA, bool RemoveFromOwner) {
769 unlinkUseDF(UA);
770 if (RemoveFromOwner)
771 removeFromOwner(UA);
772 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000773
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +0000774 void unlinkDef(NodeAddr<DefNode*> DA, bool RemoveFromOwner) {
775 unlinkDefDF(DA);
776 if (RemoveFromOwner)
777 removeFromOwner(DA);
778 }
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000779
780 // Some useful filters.
781 template <uint16_t Kind>
782 static bool IsRef(const NodeAddr<NodeBase*> BA) {
783 return BA.Addr->getType() == NodeAttrs::Ref &&
784 BA.Addr->getKind() == Kind;
785 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000786
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000787 template <uint16_t Kind>
788 static bool IsCode(const NodeAddr<NodeBase*> BA) {
789 return BA.Addr->getType() == NodeAttrs::Code &&
790 BA.Addr->getKind() == Kind;
791 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000792
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000793 static bool IsDef(const NodeAddr<NodeBase*> BA) {
794 return BA.Addr->getType() == NodeAttrs::Ref &&
795 BA.Addr->getKind() == NodeAttrs::Def;
796 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000797
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000798 static bool IsUse(const NodeAddr<NodeBase*> BA) {
799 return BA.Addr->getType() == NodeAttrs::Ref &&
800 BA.Addr->getKind() == NodeAttrs::Use;
801 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000802
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000803 static bool IsPhi(const NodeAddr<NodeBase*> BA) {
804 return BA.Addr->getType() == NodeAttrs::Code &&
805 BA.Addr->getKind() == NodeAttrs::Phi;
806 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000807
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000808 static bool IsPreservingDef(const NodeAddr<DefNode*> DA) {
809 uint16_t Flags = DA.Addr->getFlags();
810 return (Flags & NodeAttrs::Preserving) && !(Flags & NodeAttrs::Undef);
811 }
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000812
813 private:
814 void reset();
815
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000816 RegisterSet getLandingPadLiveIns() const;
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +0000817
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000818 NodeAddr<NodeBase*> newNode(uint16_t Attrs);
819 NodeAddr<NodeBase*> cloneNode(const NodeAddr<NodeBase*> B);
820 NodeAddr<UseNode*> newUse(NodeAddr<InstrNode*> Owner,
821 MachineOperand &Op, uint16_t Flags = NodeAttrs::None);
822 NodeAddr<PhiUseNode*> newPhiUse(NodeAddr<PhiNode*> Owner,
823 RegisterRef RR, NodeAddr<BlockNode*> PredB,
824 uint16_t Flags = NodeAttrs::PhiRef);
825 NodeAddr<DefNode*> newDef(NodeAddr<InstrNode*> Owner,
826 MachineOperand &Op, uint16_t Flags = NodeAttrs::None);
827 NodeAddr<DefNode*> newDef(NodeAddr<InstrNode*> Owner,
828 RegisterRef RR, uint16_t Flags = NodeAttrs::PhiRef);
829 NodeAddr<PhiNode*> newPhi(NodeAddr<BlockNode*> Owner);
830 NodeAddr<StmtNode*> newStmt(NodeAddr<BlockNode*> Owner,
831 MachineInstr *MI);
832 NodeAddr<BlockNode*> newBlock(NodeAddr<FuncNode*> Owner,
833 MachineBasicBlock *BB);
834 NodeAddr<FuncNode*> newFunc(MachineFunction *MF);
835
836 template <typename Predicate>
837 std::pair<NodeAddr<RefNode*>,NodeAddr<RefNode*>>
838 locateNextRef(NodeAddr<InstrNode*> IA, NodeAddr<RefNode*> RA,
839 Predicate P) const;
840
841 typedef std::map<NodeId,RegisterSet> BlockRefsMap;
842
843 void buildStmt(NodeAddr<BlockNode*> BA, MachineInstr &In);
844 void buildBlockRefs(NodeAddr<BlockNode*> BA, BlockRefsMap &RefM);
845 void recordDefsForDF(BlockRefsMap &PhiM, BlockRefsMap &RefM,
846 NodeAddr<BlockNode*> BA);
847 void buildPhis(BlockRefsMap &PhiM, BlockRefsMap &RefM,
848 NodeAddr<BlockNode*> BA);
849 void removeUnusedPhis();
850
Krzysztof Parzyszek84cd4ea2017-02-16 18:53:04 +0000851 void pushClobbers(NodeAddr<InstrNode*> IA, DefStackMap &DM);
852 void pushDefs(NodeAddr<InstrNode*> IA, DefStackMap &DM);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000853 template <typename T> void linkRefUp(NodeAddr<InstrNode*> IA,
854 NodeAddr<T> TA, DefStack &DS);
Krzysztof Parzyszek84cd4ea2017-02-16 18:53:04 +0000855 template <typename Predicate> void linkStmtRefs(DefStackMap &DefM,
856 NodeAddr<StmtNode*> SA, Predicate P);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000857 void linkBlockRefs(DefStackMap &DefM, NodeAddr<BlockNode*> BA);
858
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +0000859 void unlinkUseDF(NodeAddr<UseNode*> UA);
860 void unlinkDefDF(NodeAddr<DefNode*> DA);
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000861
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +0000862 void removeFromOwner(NodeAddr<RefNode*> RA) {
863 NodeAddr<InstrNode*> IA = RA.Addr->getOwner(*this);
864 IA.Addr->removeMember(RA, *this);
865 }
866
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000867 MachineFunction &MF;
868 const TargetInstrInfo &TII;
869 const TargetRegisterInfo &TRI;
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +0000870 const PhysicalRegisterInfo PRI;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000871 const MachineDominatorTree &MDT;
872 const MachineDominanceFrontier &MDF;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000873 const TargetOperandInfo &TOI;
Krzysztof Parzyszekb561cf92017-01-30 16:20:30 +0000874
875 RegisterAggr LiveIns;
876 NodeAddr<FuncNode*> Func;
877 NodeAllocator Memory;
878 // Local map: MachineBasicBlock -> NodeAddr<BlockNode*>
879 std::map<MachineBasicBlock*,NodeAddr<BlockNode*>> BlockNodes;
880 // Lane mask map.
881 LaneMaskIndex LMI;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000882 }; // struct DataFlowGraph
883
884 template <typename Predicate>
885 NodeAddr<RefNode*> RefNode::getNextRef(RegisterRef RR, Predicate P,
886 bool NextOnly, const DataFlowGraph &G) {
887 // Get the "Next" reference in the circular list that references RR and
888 // satisfies predicate "Pred".
889 auto NA = G.addr<NodeBase*>(getNext());
890
891 while (NA.Addr != this) {
892 if (NA.Addr->getType() == NodeAttrs::Ref) {
893 NodeAddr<RefNode*> RA = NA;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000894 if (RA.Addr->getRegRef(G) == RR && P(NA))
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000895 return NA;
896 if (NextOnly)
897 break;
898 NA = G.addr<NodeBase*>(NA.Addr->getNext());
899 } else {
900 // We've hit the beginning of the chain.
901 assert(NA.Addr->getType() == NodeAttrs::Code);
902 NodeAddr<CodeNode*> CA = NA;
903 NA = CA.Addr->getFirstMember(G);
904 }
905 }
906 // Return the equivalent of "nullptr" if such a node was not found.
907 return NodeAddr<RefNode*>();
908 }
909
910 template <typename Predicate>
911 NodeList CodeNode::members_if(Predicate P, const DataFlowGraph &G) const {
912 NodeList MM;
913 auto M = getFirstMember(G);
914 if (M.Id == 0)
915 return MM;
916
917 while (M.Addr != this) {
918 if (P(M))
919 MM.push_back(M);
920 M = G.addr<NodeBase*>(M.Addr->getNext());
921 }
922 return MM;
923 }
924
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000925
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000926 template <typename T> struct Print;
927 template <typename T>
928 raw_ostream &operator<< (raw_ostream &OS, const Print<T> &P);
929
930 template <typename T>
931 struct Print {
932 Print(const T &x, const DataFlowGraph &g) : Obj(x), G(g) {}
933 const T &Obj;
934 const DataFlowGraph &G;
935 };
936
937 template <typename T>
938 struct PrintNode : Print<NodeAddr<T>> {
939 PrintNode(const NodeAddr<T> &x, const DataFlowGraph &g)
940 : Print<NodeAddr<T>>(x, g) {}
941 };
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000942
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000943} // end namespace rdf
944
945} // end namespace llvm
946
947#endif // LLVM_LIB_TARGET_HEXAGON_RDFGRAPH_H