1//===- RegisterCoalescer.h - Register Coalescing Interface ------*- 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// This file contains the abstract interface for register coalescers,
10// allowing them to interact with and query register allocators.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_CODEGEN_REGISTERCOALESCER_H
15#define LLVM_LIB_CODEGEN_REGISTERCOALESCER_H
16
17#include "llvm/CodeGen/MachinePassManager.h"
18#include "llvm/CodeGen/Register.h"
19
20namespace llvm {
21
22class MachineInstr;
23class MCRegisterClass;
24using TargetRegisterClass = MCRegisterClass;
25class TargetRegisterInfo;
26
27/// A helper class for register coalescers. When deciding if
28/// two registers can be coalesced, CoalescerPair can determine if a copy
29/// instruction would become an identity copy after coalescing.
30class CoalescerPair {
31 const TargetRegisterInfo &TRI;
32
33 /// The register that will be left after coalescing. It can be a
34 /// virtual or physical register.
35 Register DstReg;
36
37 /// The virtual register that will be coalesced into dstReg.
38 Register SrcReg;
39
40 /// The sub-register index of the old DstReg in the new coalesced register.
41 unsigned DstIdx = 0;
42
43 /// The sub-register index of the old SrcReg in the new coalesced register.
44 unsigned SrcIdx = 0;
45
46 /// True when the original copy was a partial subregister copy.
47 bool Partial = false;
48
49 /// True when both regs are virtual and newRC is constrained.
50 bool CrossClass = false;
51
52 /// True when DstReg and SrcReg are reversed from the original
53 /// copy instruction.
54 bool Flipped = false;
55
56 /// The register class of the coalesced register, or NULL if DstReg
57 /// is a physreg. This register class may be a super-register of both
58 /// SrcReg and DstReg.
59 const TargetRegisterClass *NewRC = nullptr;
60
61public:
62 CoalescerPair(const TargetRegisterInfo &tri) : TRI(tri) {}
63
64 /// Create a CoalescerPair representing a virtreg-to-physreg copy.
65 /// No need to call setRegisters().
66 CoalescerPair(Register VirtReg, MCRegister PhysReg,
67 const TargetRegisterInfo &tri)
68 : TRI(tri), DstReg(PhysReg), SrcReg(VirtReg) {}
69
70 /// Set registers to match the copy instruction MI. Return
71 /// false if MI is not a coalescable copy instruction.
72 bool setRegisters(const MachineInstr *);
73
74 /// Swap SrcReg and DstReg. Return false if swapping is impossible
75 /// because DstReg is a physical register, or SubIdx is set.
76 bool flip();
77
78 /// Return true if MI is a copy instruction that will become
79 /// an identity copy after coalescing.
80 bool isCoalescable(const MachineInstr *) const;
81
82 /// Return true if DstReg is a physical register.
83 bool isPhys() const { return !NewRC; }
84
85 /// Return true if the original copy instruction did not copy
86 /// the full register, but was a subreg operation.
87 bool isPartial() const { return Partial; }
88
89 /// Return true if DstReg is virtual and NewRC is a smaller
90 /// register class than DstReg's.
91 bool isCrossClass() const { return CrossClass; }
92
93 /// Return true when getSrcReg is the register being defined by
94 /// the original copy instruction.
95 bool isFlipped() const { return Flipped; }
96
97 /// Return the register (virtual or physical) that will remain
98 /// after coalescing.
99 Register getDstReg() const { return DstReg; }
100
101 /// Return the virtual register that will be coalesced away.
102 Register getSrcReg() const { return SrcReg; }
103
104 /// Return the subregister index that DstReg will be coalesced into, or 0.
105 unsigned getDstIdx() const { return DstIdx; }
106
107 /// Return the subregister index that SrcReg will be coalesced into, or 0.
108 unsigned getSrcIdx() const { return SrcIdx; }
109
110 /// Return the register class of the coalesced register.
111 const TargetRegisterClass *getNewRC() const { return NewRC; }
112};
113
114} // end namespace llvm
115
116#endif // LLVM_LIB_CODEGEN_REGISTERCOALESCER_H
117