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