LLVM  5.0.0
RDFRegisters.cpp
Go to the documentation of this file.
1 //===--- RDFRegisters.cpp ---------------------------------------*- C++ -*-===//
2 //
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 #include "RDFRegisters.h"
11 #include "llvm/ADT/BitVector.h"
13 
14 using namespace llvm;
15 using namespace rdf;
16 
18  const MachineFunction &mf)
19  : TRI(tri) {
20  RegInfos.resize(TRI.getNumRegs());
21 
22  BitVector BadRC(TRI.getNumRegs());
23  for (const TargetRegisterClass *RC : TRI.regclasses()) {
24  for (MCPhysReg R : *RC) {
25  RegInfo &RI = RegInfos[R];
26  if (RI.RegClass != nullptr && !BadRC[R]) {
27  if (RC->LaneMask != RI.RegClass->LaneMask) {
28  BadRC.set(R);
29  RI.RegClass = nullptr;
30  }
31  } else
32  RI.RegClass = RC;
33  }
34  }
35 
36  UnitInfos.resize(TRI.getNumRegUnits());
37 
38  for (uint32_t U = 0, NU = TRI.getNumRegUnits(); U != NU; ++U) {
39  if (UnitInfos[U].Reg != 0)
40  continue;
41  MCRegUnitRootIterator R(U, &TRI);
42  assert(R.isValid());
43  RegisterId F = *R;
44  ++R;
45  if (R.isValid()) {
46  UnitInfos[U].Mask = LaneBitmask::getAll();
47  UnitInfos[U].Reg = F;
48  } else {
49  for (MCRegUnitMaskIterator I(F, &TRI); I.isValid(); ++I) {
50  std::pair<uint32_t,LaneBitmask> P = *I;
51  UnitInfo &UI = UnitInfos[P.first];
52  UI.Reg = F;
53  if (P.second.any()) {
54  UI.Mask = P.second;
55  } else {
56  if (const TargetRegisterClass *RC = RegInfos[F].RegClass)
57  UI.Mask = RC->LaneMask;
58  else
59  UI.Mask = LaneBitmask::getAll();
60  }
61  }
62  }
63  }
64 
65  for (const uint32_t *RM : TRI.getRegMasks())
66  RegMasks.insert(RM);
67  for (const MachineBasicBlock &B : mf)
68  for (const MachineInstr &In : B)
69  for (const MachineOperand &Op : In.operands())
70  if (Op.isRegMask())
71  RegMasks.insert(Op.getRegMask());
72 
73  MaskInfos.resize(RegMasks.size()+1);
74  for (uint32_t M = 1, NM = RegMasks.size(); M <= NM; ++M) {
75  BitVector PU(TRI.getNumRegUnits());
76  const uint32_t *MB = RegMasks.get(M);
77  for (unsigned i = 1, e = TRI.getNumRegs(); i != e; ++i) {
78  if (!(MB[i/32] & (1u << (i%32))))
79  continue;
80  for (MCRegUnitIterator U(i, &TRI); U.isValid(); ++U)
81  PU.set(*U);
82  }
83  MaskInfos[M].Units = PU.flip();
84  }
85 }
86 
88  return RR;
89 }
90 
91 std::set<RegisterId> PhysicalRegisterInfo::getAliasSet(RegisterId Reg) const {
92  // Do not include RR in the alias set.
93  std::set<RegisterId> AS;
95  if (isRegMaskId(Reg)) {
96  // XXX SLOW
97  const uint32_t *MB = getRegMaskBits(Reg);
98  for (unsigned i = 1, e = TRI.getNumRegs(); i != e; ++i) {
99  if (MB[i/32] & (1u << (i%32)))
100  continue;
101  AS.insert(i);
102  }
103  for (const uint32_t *RM : RegMasks) {
105  if (MI != Reg && aliasMM(RegisterRef(Reg), RegisterRef(MI)))
106  AS.insert(MI);
107  }
108  return AS;
109  }
110 
111  for (MCRegAliasIterator AI(Reg, &TRI, false); AI.isValid(); ++AI)
112  AS.insert(*AI);
113  for (const uint32_t *RM : RegMasks) {
115  if (aliasRM(RegisterRef(Reg), RegisterRef(MI)))
116  AS.insert(MI);
117  }
118  return AS;
119 }
120 
121 bool PhysicalRegisterInfo::aliasRR(RegisterRef RA, RegisterRef RB) const {
124 
125  MCRegUnitMaskIterator UMA(RA.Reg, &TRI);
126  MCRegUnitMaskIterator UMB(RB.Reg, &TRI);
127  // Reg units are returned in the numerical order.
128  while (UMA.isValid() && UMB.isValid()) {
129  // Skip units that are masked off in RA.
130  std::pair<RegisterId,LaneBitmask> PA = *UMA;
131  if (PA.second.any() && (PA.second & RA.Mask).none()) {
132  ++UMA;
133  continue;
134  }
135  // Skip units that are masked off in RB.
136  std::pair<RegisterId,LaneBitmask> PB = *UMB;
137  if (PB.second.any() && (PB.second & RB.Mask).none()) {
138  ++UMB;
139  continue;
140  }
141 
142  if (PA.first == PB.first)
143  return true;
144  if (PA.first < PB.first)
145  ++UMA;
146  else if (PB.first < PA.first)
147  ++UMB;
148  }
149  return false;
150 }
151 
152 bool PhysicalRegisterInfo::aliasRM(RegisterRef RR, RegisterRef RM) const {
154  const uint32_t *MB = getRegMaskBits(RM.Reg);
155  bool Preserved = MB[RR.Reg/32] & (1u << (RR.Reg%32));
156  // If the lane mask information is "full", e.g. when the given lane mask
157  // is a superset of the lane mask from the register class, check the regmask
158  // bit directly.
159  if (RR.Mask == LaneBitmask::getAll())
160  return !Preserved;
161  const TargetRegisterClass *RC = RegInfos[RR.Reg].RegClass;
162  if (RC != nullptr && (RR.Mask & RC->LaneMask) == RC->LaneMask)
163  return !Preserved;
164 
165  // Otherwise, check all subregisters whose lane mask overlaps the given
166  // mask. For each such register, if it is preserved by the regmask, then
167  // clear the corresponding bits in the given mask. If at the end, all
168  // bits have been cleared, the register does not alias the regmask (i.e.
169  // is it preserved by it).
170  LaneBitmask M = RR.Mask;
171  for (MCSubRegIndexIterator SI(RR.Reg, &TRI); SI.isValid(); ++SI) {
172  LaneBitmask SM = TRI.getSubRegIndexLaneMask(SI.getSubRegIndex());
173  if ((SM & RR.Mask).none())
174  continue;
175  unsigned SR = SI.getSubReg();
176  if (!(MB[SR/32] & (1u << (SR%32))))
177  continue;
178  // The subregister SR is preserved.
179  M &= ~SM;
180  if (M.none())
181  return false;
182  }
183 
184  return true;
185 }
186 
187 bool PhysicalRegisterInfo::aliasMM(RegisterRef RM, RegisterRef RN) const {
188  assert(isRegMaskId(RM.Reg) && isRegMaskId(RN.Reg));
189  unsigned NumRegs = TRI.getNumRegs();
190  const uint32_t *BM = getRegMaskBits(RM.Reg);
191  const uint32_t *BN = getRegMaskBits(RN.Reg);
192 
193  for (unsigned w = 0, nw = NumRegs/32; w != nw; ++w) {
194  // Intersect the negations of both words. Disregard reg=0,
195  // i.e. 0th bit in the 0th word.
196  uint32_t C = ~BM[w] & ~BN[w];
197  if (w == 0)
198  C &= ~1;
199  if (C)
200  return true;
201  }
202 
203  // Check the remaining registers in the last word.
204  unsigned TailRegs = NumRegs % 32;
205  if (TailRegs == 0)
206  return false;
207  unsigned TW = NumRegs / 32;
208  uint32_t TailMask = (1u << TailRegs) - 1;
209  if (~BM[TW] & ~BN[TW] & TailMask)
210  return true;
211 
212  return false;
213 }
214 
216  if (RR.Reg == R)
217  return RR;
218  if (unsigned Idx = TRI.getSubRegIndex(R, RR.Reg))
219  return RegisterRef(R, TRI.composeSubRegIndexLaneMask(Idx, RR.Mask));
220  if (unsigned Idx = TRI.getSubRegIndex(RR.Reg, R)) {
221  const RegInfo &RI = RegInfos[R];
222  LaneBitmask RCM = RI.RegClass ? RI.RegClass->LaneMask
225  return RegisterRef(R, M & RCM);
226  }
227  llvm_unreachable("Invalid arguments: unrelated registers?");
228 }
229 
230 
233  return Units.anyCommon(PRI.getMaskUnits(RR.Reg));
234 
235  for (MCRegUnitMaskIterator U(RR.Reg, &PRI.getTRI()); U.isValid(); ++U) {
236  std::pair<uint32_t,LaneBitmask> P = *U;
237  if (P.second.none() || (P.second & RR.Mask).any())
238  if (Units.test(P.first))
239  return true;
240  }
241  return false;
242 }
243 
246  BitVector T(PRI.getMaskUnits(RR.Reg));
247  return T.reset(Units).none();
248  }
249 
250  for (MCRegUnitMaskIterator U(RR.Reg, &PRI.getTRI()); U.isValid(); ++U) {
251  std::pair<uint32_t,LaneBitmask> P = *U;
252  if (P.second.none() || (P.second & RR.Mask).any())
253  if (!Units.test(P.first))
254  return false;
255  }
256  return true;
257 }
258 
261  Units |= PRI.getMaskUnits(RR.Reg);
262  return *this;
263  }
264 
265  for (MCRegUnitMaskIterator U(RR.Reg, &PRI.getTRI()); U.isValid(); ++U) {
266  std::pair<uint32_t,LaneBitmask> P = *U;
267  if (P.second.none() || (P.second & RR.Mask).any())
268  Units.set(P.first);
269  }
270  return *this;
271 }
272 
274  Units |= RG.Units;
275  return *this;
276 }
277 
279  return intersect(RegisterAggr(PRI).insert(RR));
280 }
281 
283  Units &= RG.Units;
284  return *this;
285 }
286 
288  return clear(RegisterAggr(PRI).insert(RR));
289 }
290 
292  Units.reset(RG.Units);
293  return *this;
294 }
295 
297  RegisterAggr T(PRI);
298  T.insert(RR).intersect(*this);
299  if (T.empty())
300  return RegisterRef();
301  RegisterRef NR = T.makeRegRef();
302  assert(NR);
303  return NR;
304 }
305 
307  return RegisterAggr(PRI).insert(RR).clear(*this).makeRegRef();
308 }
309 
311  int U = Units.find_first();
312  if (U < 0)
313  return RegisterRef();
314 
315  auto AliasedRegs = [this] (uint32_t Unit, BitVector &Regs) {
316  for (MCRegUnitRootIterator R(Unit, &PRI.getTRI()); R.isValid(); ++R)
317  for (MCSuperRegIterator S(*R, &PRI.getTRI(), true); S.isValid(); ++S)
318  Regs.set(*S);
319  };
320 
321  // Find the set of all registers that are aliased to all the units
322  // in this aggregate.
323 
324  // Get all the registers aliased to the first unit in the bit vector.
325  BitVector Regs(PRI.getTRI().getNumRegs());
326  AliasedRegs(U, Regs);
327  U = Units.find_next(U);
328 
329  // For each other unit, intersect it with the set of all registers
330  // aliased that unit.
331  while (U >= 0) {
332  BitVector AR(PRI.getTRI().getNumRegs());
333  AliasedRegs(U, AR);
334  Regs &= AR;
335  U = Units.find_next(U);
336  }
337 
338  // If there is at least one register remaining, pick the first one,
339  // and consolidate the masks of all of its units contained in this
340  // aggregate.
341 
342  int F = Regs.find_first();
343  if (F <= 0)
344  return RegisterRef();
345 
346  LaneBitmask M;
347  for (MCRegUnitMaskIterator I(F, &PRI.getTRI()); I.isValid(); ++I) {
348  std::pair<uint32_t,LaneBitmask> P = *I;
349  if (Units.test(P.first))
350  M |= P.second.none() ? LaneBitmask::getAll() : P.second;
351  }
352  return RegisterRef(F, M);
353 }
354 
356  OS << '{';
357  for (int U = Units.find_first(); U >= 0; U = Units.find_next(U))
358  OS << ' ' << PrintRegUnit(U, &PRI.getTRI());
359  OS << " }";
360 }
361 
363  bool End)
364  : Owner(&RG) {
365  for (int U = RG.Units.find_first(); U >= 0; U = RG.Units.find_next(U)) {
366  RegisterRef R = RG.PRI.getRefForUnit(U);
367  Masks[R.Reg] |= R.Mask;
368  }
369  Pos = End ? Masks.end() : Masks.begin();
370  Index = End ? Masks.size() : 0;
371 }
372 
std::set< RegisterId > getAliasSet(RegisterId Reg) const
BitVector & set()
Definition: BitVector.h:398
bool isValid() const
Check if the iterator is at the end of the list.
int find_first() const
find_first - Returns the index of the first set bit, -1 if none of the bits are set.
Definition: BitVector.h:332
bool isValid() const
Returns true if this iterator is not yet at the end.
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
bool anyCommon(const BitVector &RHS) const
Test if any common bits are set.
Definition: BitVector.h:507
int find_next(unsigned Prev) const
find_next - Returns the index of the next set bit following the "Prev" bit.
Definition: BitVector.h:340
static LaneBitmask getAll()
Definition: LaneBitmask.h:77
RegisterId getRegMaskId(const uint32_t *RM) const
Definition: RDFRegisters.h:94
LaneBitmask getSubRegIndexLaneMask(unsigned SubIdx) const
Return a bitmask representing the parts of a register that are covered by SubIdx. ...
unsigned getSubRegIndex(unsigned RegNo, unsigned SubRegNo) const
For a given register pair, return the sub-register index if the second register is a sub-register of ...
iterator_range< regclass_iterator > regclasses() const
MCRegUnitMaskIterator enumerates a list of register units and their associated lane masks for Reg...
MCSuperRegIterator enumerates all super-registers of Reg.
uint32_t size() const
Definition: RDFRegisters.h:54
Reg
All possible values of the reg field in the ModR/M byte.
RegisterRef mapTo(RegisterRef RR, unsigned R) const
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
#define F(x, y, z)
Definition: MD5.cpp:55
RegisterRef makeRegRef() const
MCRegUnitRootIterator enumerates the root registers of a register unit.
#define T
T get(uint32_t Idx) const
Definition: RDFRegisters.h:33
const RegList & Regs
void print(raw_ostream &OS) const
uint32_t insert(T Val)
Definition: RDFRegisters.h:39
PhysicalRegisterInfo(const TargetRegisterInfo &tri, const MachineFunction &mf)
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
RegisterAggr & clear(RegisterRef RR)
const TargetRegisterInfo & getTRI() const
Definition: RDFRegisters.h:117
#define P(N)
LaneBitmask composeSubRegIndexLaneMask(unsigned IdxA, LaneBitmask Mask) const
Transforms a LaneMask computed for one subregister to the lanemask that would have been computed when...
bool hasCoverOf(RegisterRef RR) const
constexpr bool none() const
Definition: LaneBitmask.h:52
Iterator that enumerates the sub-registers of a Reg and the associated sub-register indices...
MCRegAliasIterator enumerates all registers aliasing Reg.
BitVector & reset()
Definition: BitVector.h:439
static const unsigned End
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
const BitVector & getMaskUnits(RegisterId MaskId) const
Definition: RDFRegisters.h:112
RegisterRef intersectWith(RegisterRef RR) const
RegisterAggr & insert(RegisterRef RR)
Printable PrintRegUnit(unsigned Unit, const TargetRegisterInfo *TRI)
Create Printable object to print register units on a raw_ostream.
RegisterRef normalize(RegisterRef RR) const
#define B
Definition: LargeTest.cpp:24
RegisterAggr(const PhysicalRegisterInfo &pri)
Definition: RDFRegisters.h:144
MachineOperand class - Representation of each machine instruction operand.
bool test(unsigned Idx) const
Definition: BitVector.h:502
LaneBitmask reverseComposeSubRegIndexLaneMask(unsigned IdxA, LaneBitmask LaneMask) const
Transform a lanemask given for a virtual register to the corresponding lanemask before using subregis...
rr_iterator(const RegisterAggr &RG, bool End)
unsigned getNumRegUnits() const
Return the number of (native) register units in the target.
llvm_unreachable("Unknown RegBank!")
const uint32_t * getRegMaskBits(RegisterId R) const
Definition: RDFRegisters.h:97
RegisterRef getRefForUnit(uint32_t U) const
Definition: RDFRegisters.h:109
Representation of each machine instruction.
Definition: MachineInstr.h:59
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
RegisterRef clearIn(RegisterRef RR) const
virtual ArrayRef< const uint32_t * > getRegMasks() const =0
Return all the call-preserved register masks defined for this target.
#define C
Definition: LargeTest.cpp:25
#define I(x, y, z)
Definition: MD5.cpp:58
std::vector< uint8_t > Unit
Definition: FuzzerDefs.h:105
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool isRegMaskId(RegisterId R)
Definition: RDFRegisters.h:91
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
bool hasAliasOf(RegisterRef RR) const
IRTranslator LLVM IR MI
RegisterAggr & intersect(RegisterRef RR)