1 //===- RDFRegisters.cpp ---------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/BitVector.h"
10 #include "llvm/CodeGen/MachineFunction.h"
11 #include "llvm/CodeGen/MachineInstr.h"
12 #include "llvm/CodeGen/MachineOperand.h"
13 #include "llvm/CodeGen/RDFRegisters.h"
14 #include "llvm/CodeGen/TargetRegisterInfo.h"
15 #include "llvm/MC/LaneBitmask.h"
16 #include "llvm/MC/MCRegisterInfo.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/raw_ostream.h"
27 PhysicalRegisterInfo::PhysicalRegisterInfo(const TargetRegisterInfo
&tri
,
28 const MachineFunction
&mf
)
30 RegInfos
.resize(TRI
.getNumRegs());
32 BitVector
BadRC(TRI
.getNumRegs());
33 for (const TargetRegisterClass
*RC
: TRI
.regclasses()) {
34 for (MCPhysReg R
: *RC
) {
35 RegInfo
&RI
= RegInfos
[R
];
36 if (RI
.RegClass
!= nullptr && !BadRC
[R
]) {
37 if (RC
->LaneMask
!= RI
.RegClass
->LaneMask
) {
39 RI
.RegClass
= nullptr;
46 UnitInfos
.resize(TRI
.getNumRegUnits());
48 for (uint32_t U
= 0, NU
= TRI
.getNumRegUnits(); U
!= NU
; ++U
) {
49 if (UnitInfos
[U
].Reg
!= 0)
51 MCRegUnitRootIterator
R(U
, &TRI
);
56 UnitInfos
[U
].Mask
= LaneBitmask::getAll();
59 for (MCRegUnitMaskIterator
I(F
, &TRI
); I
.isValid(); ++I
) {
60 std::pair
<uint32_t,LaneBitmask
> P
= *I
;
61 UnitInfo
&UI
= UnitInfos
[P
.first
];
66 if (const TargetRegisterClass
*RC
= RegInfos
[F
].RegClass
)
67 UI
.Mask
= RC
->LaneMask
;
69 UI
.Mask
= LaneBitmask::getAll();
75 for (const uint32_t *RM
: TRI
.getRegMasks())
77 for (const MachineBasicBlock
&B
: mf
)
78 for (const MachineInstr
&In
: B
)
79 for (const MachineOperand
&Op
: In
.operands())
81 RegMasks
.insert(Op
.getRegMask());
83 MaskInfos
.resize(RegMasks
.size()+1);
84 for (uint32_t M
= 1, NM
= RegMasks
.size(); M
<= NM
; ++M
) {
85 BitVector
PU(TRI
.getNumRegUnits());
86 const uint32_t *MB
= RegMasks
.get(M
);
87 for (unsigned I
= 1, E
= TRI
.getNumRegs(); I
!= E
; ++I
) {
88 if (!(MB
[I
/ 32] & (1u << (I
% 32))))
90 for (MCRegUnitIterator
U(MCRegister::from(I
), &TRI
); U
.isValid(); ++U
)
93 MaskInfos
[M
].Units
= PU
.flip();
96 AliasInfos
.resize(TRI
.getNumRegUnits());
97 for (uint32_t U
= 0, NU
= TRI
.getNumRegUnits(); U
!= NU
; ++U
) {
98 BitVector
AS(TRI
.getNumRegs());
99 for (MCRegUnitRootIterator
R(U
, &TRI
); R
.isValid(); ++R
)
100 for (MCSuperRegIterator
S(*R
, &TRI
, true); S
.isValid(); ++S
)
102 AliasInfos
[U
].Regs
= AS
;
106 std::set
<RegisterId
> PhysicalRegisterInfo::getAliasSet(RegisterId Reg
) const {
107 // Do not include RR in the alias set.
108 std::set
<RegisterId
> AS
;
109 assert(isRegMaskId(Reg
) || Register::isPhysicalRegister(Reg
));
110 if (isRegMaskId(Reg
)) {
112 const uint32_t *MB
= getRegMaskBits(Reg
);
113 for (unsigned i
= 1, e
= TRI
.getNumRegs(); i
!= e
; ++i
) {
114 if (MB
[i
/32] & (1u << (i
%32)))
118 for (const uint32_t *RM
: RegMasks
) {
119 RegisterId MI
= getRegMaskId(RM
);
120 if (MI
!= Reg
&& aliasMM(RegisterRef(Reg
), RegisterRef(MI
)))
126 for (MCRegAliasIterator
AI(Reg
, &TRI
, false); AI
.isValid(); ++AI
)
128 for (const uint32_t *RM
: RegMasks
) {
129 RegisterId MI
= getRegMaskId(RM
);
130 if (aliasRM(RegisterRef(Reg
), RegisterRef(MI
)))
136 bool PhysicalRegisterInfo::aliasRR(RegisterRef RA
, RegisterRef RB
) const {
137 assert(Register::isPhysicalRegister(RA
.Reg
));
138 assert(Register::isPhysicalRegister(RB
.Reg
));
140 MCRegUnitMaskIterator
UMA(RA
.Reg
, &TRI
);
141 MCRegUnitMaskIterator
UMB(RB
.Reg
, &TRI
);
142 // Reg units are returned in the numerical order.
143 while (UMA
.isValid() && UMB
.isValid()) {
144 // Skip units that are masked off in RA.
145 std::pair
<RegisterId
,LaneBitmask
> PA
= *UMA
;
146 if (PA
.second
.any() && (PA
.second
& RA
.Mask
).none()) {
150 // Skip units that are masked off in RB.
151 std::pair
<RegisterId
,LaneBitmask
> PB
= *UMB
;
152 if (PB
.second
.any() && (PB
.second
& RB
.Mask
).none()) {
157 if (PA
.first
== PB
.first
)
159 if (PA
.first
< PB
.first
)
161 else if (PB
.first
< PA
.first
)
167 bool PhysicalRegisterInfo::aliasRM(RegisterRef RR
, RegisterRef RM
) const {
168 assert(Register::isPhysicalRegister(RR
.Reg
) && isRegMaskId(RM
.Reg
));
169 const uint32_t *MB
= getRegMaskBits(RM
.Reg
);
170 bool Preserved
= MB
[RR
.Reg
/32] & (1u << (RR
.Reg
%32));
171 // If the lane mask information is "full", e.g. when the given lane mask
172 // is a superset of the lane mask from the register class, check the regmask
174 if (RR
.Mask
== LaneBitmask::getAll())
176 const TargetRegisterClass
*RC
= RegInfos
[RR
.Reg
].RegClass
;
177 if (RC
!= nullptr && (RR
.Mask
& RC
->LaneMask
) == RC
->LaneMask
)
180 // Otherwise, check all subregisters whose lane mask overlaps the given
181 // mask. For each such register, if it is preserved by the regmask, then
182 // clear the corresponding bits in the given mask. If at the end, all
183 // bits have been cleared, the register does not alias the regmask (i.e.
184 // is it preserved by it).
185 LaneBitmask M
= RR
.Mask
;
186 for (MCSubRegIndexIterator
SI(RR
.Reg
, &TRI
); SI
.isValid(); ++SI
) {
187 LaneBitmask SM
= TRI
.getSubRegIndexLaneMask(SI
.getSubRegIndex());
188 if ((SM
& RR
.Mask
).none())
190 unsigned SR
= SI
.getSubReg();
191 if (!(MB
[SR
/32] & (1u << (SR
%32))))
193 // The subregister SR is preserved.
202 bool PhysicalRegisterInfo::aliasMM(RegisterRef RM
, RegisterRef RN
) const {
203 assert(isRegMaskId(RM
.Reg
) && isRegMaskId(RN
.Reg
));
204 unsigned NumRegs
= TRI
.getNumRegs();
205 const uint32_t *BM
= getRegMaskBits(RM
.Reg
);
206 const uint32_t *BN
= getRegMaskBits(RN
.Reg
);
208 for (unsigned w
= 0, nw
= NumRegs
/32; w
!= nw
; ++w
) {
209 // Intersect the negations of both words. Disregard reg=0,
210 // i.e. 0th bit in the 0th word.
211 uint32_t C
= ~BM
[w
] & ~BN
[w
];
218 // Check the remaining registers in the last word.
219 unsigned TailRegs
= NumRegs
% 32;
222 unsigned TW
= NumRegs
/ 32;
223 uint32_t TailMask
= (1u << TailRegs
) - 1;
224 if (~BM
[TW
] & ~BN
[TW
] & TailMask
)
230 RegisterRef
PhysicalRegisterInfo::mapTo(RegisterRef RR
, unsigned R
) const {
233 if (unsigned Idx
= TRI
.getSubRegIndex(R
, RR
.Reg
))
234 return RegisterRef(R
, TRI
.composeSubRegIndexLaneMask(Idx
, RR
.Mask
));
235 if (unsigned Idx
= TRI
.getSubRegIndex(RR
.Reg
, R
)) {
236 const RegInfo
&RI
= RegInfos
[R
];
237 LaneBitmask RCM
= RI
.RegClass
? RI
.RegClass
->LaneMask
238 : LaneBitmask::getAll();
239 LaneBitmask M
= TRI
.reverseComposeSubRegIndexLaneMask(Idx
, RR
.Mask
);
240 return RegisterRef(R
, M
& RCM
);
242 llvm_unreachable("Invalid arguments: unrelated registers?");
245 bool RegisterAggr::hasAliasOf(RegisterRef RR
) const {
246 if (PhysicalRegisterInfo::isRegMaskId(RR
.Reg
))
247 return Units
.anyCommon(PRI
.getMaskUnits(RR
.Reg
));
249 for (MCRegUnitMaskIterator
U(RR
.Reg
, &PRI
.getTRI()); U
.isValid(); ++U
) {
250 std::pair
<uint32_t,LaneBitmask
> P
= *U
;
251 if (P
.second
.none() || (P
.second
& RR
.Mask
).any())
252 if (Units
.test(P
.first
))
258 bool RegisterAggr::hasCoverOf(RegisterRef RR
) const {
259 if (PhysicalRegisterInfo::isRegMaskId(RR
.Reg
)) {
260 BitVector
T(PRI
.getMaskUnits(RR
.Reg
));
261 return T
.reset(Units
).none();
264 for (MCRegUnitMaskIterator
U(RR
.Reg
, &PRI
.getTRI()); U
.isValid(); ++U
) {
265 std::pair
<uint32_t,LaneBitmask
> P
= *U
;
266 if (P
.second
.none() || (P
.second
& RR
.Mask
).any())
267 if (!Units
.test(P
.first
))
273 RegisterAggr
&RegisterAggr::insert(RegisterRef RR
) {
274 if (PhysicalRegisterInfo::isRegMaskId(RR
.Reg
)) {
275 Units
|= PRI
.getMaskUnits(RR
.Reg
);
279 for (MCRegUnitMaskIterator
U(RR
.Reg
, &PRI
.getTRI()); U
.isValid(); ++U
) {
280 std::pair
<uint32_t,LaneBitmask
> P
= *U
;
281 if (P
.second
.none() || (P
.second
& RR
.Mask
).any())
287 RegisterAggr
&RegisterAggr::insert(const RegisterAggr
&RG
) {
292 RegisterAggr
&RegisterAggr::intersect(RegisterRef RR
) {
293 return intersect(RegisterAggr(PRI
).insert(RR
));
296 RegisterAggr
&RegisterAggr::intersect(const RegisterAggr
&RG
) {
301 RegisterAggr
&RegisterAggr::clear(RegisterRef RR
) {
302 return clear(RegisterAggr(PRI
).insert(RR
));
305 RegisterAggr
&RegisterAggr::clear(const RegisterAggr
&RG
) {
306 Units
.reset(RG
.Units
);
310 RegisterRef
RegisterAggr::intersectWith(RegisterRef RR
) const {
312 T
.insert(RR
).intersect(*this);
314 return RegisterRef();
315 RegisterRef NR
= T
.makeRegRef();
320 RegisterRef
RegisterAggr::clearIn(RegisterRef RR
) const {
321 return RegisterAggr(PRI
).insert(RR
).clear(*this).makeRegRef();
324 RegisterRef
RegisterAggr::makeRegRef() const {
325 int U
= Units
.find_first();
327 return RegisterRef();
329 // Find the set of all registers that are aliased to all the units
330 // in this aggregate.
332 // Get all the registers aliased to the first unit in the bit vector.
333 BitVector Regs
= PRI
.getUnitAliases(U
);
334 U
= Units
.find_next(U
);
336 // For each other unit, intersect it with the set of all registers
337 // aliased that unit.
339 Regs
&= PRI
.getUnitAliases(U
);
340 U
= Units
.find_next(U
);
343 // If there is at least one register remaining, pick the first one,
344 // and consolidate the masks of all of its units contained in this
347 int F
= Regs
.find_first();
349 return RegisterRef();
352 for (MCRegUnitMaskIterator
I(F
, &PRI
.getTRI()); I
.isValid(); ++I
) {
353 std::pair
<uint32_t,LaneBitmask
> P
= *I
;
354 if (Units
.test(P
.first
))
355 M
|= P
.second
.none() ? LaneBitmask::getAll() : P
.second
;
357 return RegisterRef(F
, M
);
360 void RegisterAggr::print(raw_ostream
&OS
) const {
362 for (int U
= Units
.find_first(); U
>= 0; U
= Units
.find_next(U
))
363 OS
<< ' ' << printRegUnit(U
, &PRI
.getTRI());
367 RegisterAggr::rr_iterator::rr_iterator(const RegisterAggr
&RG
,
370 for (int U
= RG
.Units
.find_first(); U
>= 0; U
= RG
.Units
.find_next(U
)) {
371 RegisterRef R
= RG
.PRI
.getRefForUnit(U
);
372 Masks
[R
.Reg
] |= R
.Mask
;
374 Pos
= End
? Masks
.end() : Masks
.begin();
375 Index
= End
? Masks
.size() : 0;
378 raw_ostream
&rdf::operator<<(raw_ostream
&OS
, const RegisterAggr
&A
) {