LLVM  5.0.0
RegisterBankInfo.cpp
Go to the documentation of this file.
1 //===- llvm/CodeGen/GlobalISel/RegisterBankInfo.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 /// \file
10 /// This file implements the RegisterBankInfo class.
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/Statistic.h"
22 #include "llvm/IR/Type.h"
23 #include "llvm/Support/Debug.h"
29 
30 #include <algorithm> // For std::max.
31 
32 #define DEBUG_TYPE "registerbankinfo"
33 
34 using namespace llvm;
35 
36 STATISTIC(NumPartialMappingsCreated,
37  "Number of partial mappings dynamically created");
38 STATISTIC(NumPartialMappingsAccessed,
39  "Number of partial mappings dynamically accessed");
40 STATISTIC(NumValueMappingsCreated,
41  "Number of value mappings dynamically created");
42 STATISTIC(NumValueMappingsAccessed,
43  "Number of value mappings dynamically accessed");
44 STATISTIC(NumOperandsMappingsCreated,
45  "Number of operands mappings dynamically created");
46 STATISTIC(NumOperandsMappingsAccessed,
47  "Number of operands mappings dynamically accessed");
48 STATISTIC(NumInstructionMappingsCreated,
49  "Number of instruction mappings dynamically created");
50 STATISTIC(NumInstructionMappingsAccessed,
51  "Number of instruction mappings dynamically accessed");
52 
53 const unsigned RegisterBankInfo::DefaultMappingID = UINT_MAX;
54 const unsigned RegisterBankInfo::InvalidMappingID = UINT_MAX - 1;
55 
56 //------------------------------------------------------------------------------
57 // RegisterBankInfo implementation.
58 //------------------------------------------------------------------------------
60  unsigned NumRegBanks)
61  : RegBanks(RegBanks), NumRegBanks(NumRegBanks) {
62 #ifndef NDEBUG
63  for (unsigned Idx = 0, End = getNumRegBanks(); Idx != End; ++Idx) {
64  assert(RegBanks[Idx] != nullptr && "Invalid RegisterBank");
65  assert(RegBanks[Idx]->isValid() && "RegisterBank should be valid");
66  }
67 #endif // NDEBUG
68 }
69 
70 bool RegisterBankInfo::verify(const TargetRegisterInfo &TRI) const {
71 #ifndef NDEBUG
72  for (unsigned Idx = 0, End = getNumRegBanks(); Idx != End; ++Idx) {
73  const RegisterBank &RegBank = getRegBank(Idx);
74  assert(Idx == RegBank.getID() &&
75  "ID does not match the index in the array");
76  DEBUG(dbgs() << "Verify " << RegBank << '\n');
77  assert(RegBank.verify(TRI) && "RegBank is invalid");
78  }
79 #endif // NDEBUG
80  return true;
81 }
82 
83 const RegisterBank *
85  const TargetRegisterInfo &TRI) const {
88 
89  assert(Reg && "NoRegister does not have a register bank");
90  const RegClassOrRegBank &RegClassOrBank = MRI.getRegClassOrRegBank(Reg);
91  if (auto *RB = RegClassOrBank.dyn_cast<const RegisterBank *>())
92  return RB;
93  if (auto *RC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>())
94  return &getRegBankFromRegClass(*RC);
95  return nullptr;
96 }
97 
99  const MachineInstr &MI, unsigned OpIdx, const TargetInstrInfo &TII,
100  const TargetRegisterInfo &TRI) const {
101  // The mapping of the registers may be available via the
102  // register class constraints.
103  const TargetRegisterClass *RC = MI.getRegClassConstraint(OpIdx, &TII, &TRI);
104 
105  if (!RC)
106  return nullptr;
107 
108  const RegisterBank &RegBank = getRegBankFromRegClass(*RC);
109  // Sanity check that the target properly implemented getRegBankFromRegClass.
110  assert(RegBank.covers(*RC) &&
111  "The mapping of the register bank does not make sense");
112  return &RegBank;
113 }
114 
116  unsigned Reg, const TargetRegisterClass &RC, MachineRegisterInfo &MRI) {
117 
118  // If the register already has a class, fallback to MRI::constrainRegClass.
119  auto &RegClassOrBank = MRI.getRegClassOrRegBank(Reg);
120  if (RegClassOrBank.is<const TargetRegisterClass *>())
121  return MRI.constrainRegClass(Reg, &RC);
122 
123  const RegisterBank *RB = RegClassOrBank.get<const RegisterBank *>();
124  // Otherwise, all we can do is ensure the bank covers the class, and set it.
125  if (RB && !RB->covers(RC))
126  return nullptr;
127 
128  // If nothing was set or the class is simply compatible, set it.
129  MRI.setRegClass(Reg, &RC);
130  return &RC;
131 }
132 
133 /// Check whether or not \p MI should be treated like a copy
134 /// for the mappings.
135 /// Copy like instruction are special for mapping because
136 /// they don't have actual register constraints. Moreover,
137 /// they sometimes have register classes assigned and we can
138 /// just use that instead of failing to provide a generic mapping.
139 static bool isCopyLike(const MachineInstr &MI) {
140  return MI.isCopy() || MI.isPHI() ||
141  MI.getOpcode() == TargetOpcode::REG_SEQUENCE;
142 }
143 
146  // For copies we want to walk over the operands and try to find one
147  // that has a register bank since the instruction itself will not get
148  // us any constraint.
149  bool IsCopyLike = isCopyLike(MI);
150  // For copy like instruction, only the mapping of the definition
151  // is important. The rest is not constrained.
152  unsigned NumOperandsForMapping = IsCopyLike ? 1 : MI.getNumOperands();
153 
154  const MachineFunction &MF = *MI.getParent()->getParent();
155  const TargetSubtargetInfo &STI = MF.getSubtarget();
156  const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
157  const MachineRegisterInfo &MRI = MF.getRegInfo();
158  // We may need to query the instruction encoding to guess the mapping.
159  const TargetInstrInfo &TII = *STI.getInstrInfo();
160 
161  // Before doing anything complicated check if the mapping is not
162  // directly available.
163  bool CompleteMapping = true;
164 
165  SmallVector<const ValueMapping *, 8> OperandsMapping(NumOperandsForMapping);
166  for (unsigned OpIdx = 0, EndIdx = MI.getNumOperands(); OpIdx != EndIdx;
167  ++OpIdx) {
168  const MachineOperand &MO = MI.getOperand(OpIdx);
169  if (!MO.isReg())
170  continue;
171  unsigned Reg = MO.getReg();
172  if (!Reg)
173  continue;
174  // The register bank of Reg is just a side effect of the current
175  // excution and in particular, there is no reason to believe this
176  // is the best default mapping for the current instruction. Keep
177  // it as an alternative register bank if we cannot figure out
178  // something.
179  const RegisterBank *AltRegBank = getRegBank(Reg, MRI, TRI);
180  // For copy-like instruction, we want to reuse the register bank
181  // that is already set on Reg, if any, since those instructions do
182  // not have any constraints.
183  const RegisterBank *CurRegBank = IsCopyLike ? AltRegBank : nullptr;
184  if (!CurRegBank) {
185  // If this is a target specific instruction, we can deduce
186  // the register bank from the encoding constraints.
187  CurRegBank = getRegBankFromConstraints(MI, OpIdx, TII, TRI);
188  if (!CurRegBank) {
189  // All our attempts failed, give up.
190  CompleteMapping = false;
191 
192  if (!IsCopyLike)
193  // MI does not carry enough information to guess the mapping.
195  continue;
196  }
197  }
198  const ValueMapping *ValMapping =
199  &getValueMapping(0, getSizeInBits(Reg, MRI, TRI), *CurRegBank);
200  if (IsCopyLike) {
201  OperandsMapping[0] = ValMapping;
202  CompleteMapping = true;
203  break;
204  }
205  OperandsMapping[OpIdx] = ValMapping;
206  }
207 
208  if (IsCopyLike && !CompleteMapping)
209  // No way to deduce the type from what we have.
211 
212  assert(CompleteMapping && "Setting an uncomplete mapping");
213  return getInstructionMapping(
214  DefaultMappingID, /*Cost*/ 1,
215  /*OperandsMapping*/ getOperandsMapping(OperandsMapping),
216  NumOperandsForMapping);
217 }
218 
219 /// Hashing function for PartialMapping.
220 static hash_code hashPartialMapping(unsigned StartIdx, unsigned Length,
221  const RegisterBank *RegBank) {
222  return hash_combine(StartIdx, Length, RegBank ? RegBank->getID() : 0);
223 }
224 
225 /// Overloaded version of hash_value for a PartialMapping.
226 hash_code
228  return hashPartialMapping(PartMapping.StartIdx, PartMapping.Length,
229  PartMapping.RegBank);
230 }
231 
233 RegisterBankInfo::getPartialMapping(unsigned StartIdx, unsigned Length,
234  const RegisterBank &RegBank) const {
235  ++NumPartialMappingsAccessed;
236 
237  hash_code Hash = hashPartialMapping(StartIdx, Length, &RegBank);
238  const auto &It = MapOfPartialMappings.find(Hash);
239  if (It != MapOfPartialMappings.end())
240  return *It->second;
241 
242  ++NumPartialMappingsCreated;
243 
244  auto &PartMapping = MapOfPartialMappings[Hash];
245  PartMapping = llvm::make_unique<PartialMapping>(StartIdx, Length, RegBank);
246  return *PartMapping;
247 }
248 
250 RegisterBankInfo::getValueMapping(unsigned StartIdx, unsigned Length,
251  const RegisterBank &RegBank) const {
252  return getValueMapping(&getPartialMapping(StartIdx, Length, RegBank), 1);
253 }
254 
255 static hash_code
257  unsigned NumBreakDowns) {
258  if (LLVM_LIKELY(NumBreakDowns == 1))
259  return hash_value(*BreakDown);
260  SmallVector<size_t, 8> Hashes(NumBreakDowns);
261  for (unsigned Idx = 0; Idx != NumBreakDowns; ++Idx)
262  Hashes.push_back(hash_value(BreakDown[Idx]));
263  return hash_combine_range(Hashes.begin(), Hashes.end());
264 }
265 
268  unsigned NumBreakDowns) const {
269  ++NumValueMappingsAccessed;
270 
271  hash_code Hash = hashValueMapping(BreakDown, NumBreakDowns);
272  const auto &It = MapOfValueMappings.find(Hash);
273  if (It != MapOfValueMappings.end())
274  return *It->second;
275 
276  ++NumValueMappingsCreated;
277 
278  auto &ValMapping = MapOfValueMappings[Hash];
279  ValMapping = llvm::make_unique<ValueMapping>(BreakDown, NumBreakDowns);
280  return *ValMapping;
281 }
282 
283 template <typename Iterator>
285 RegisterBankInfo::getOperandsMapping(Iterator Begin, Iterator End) const {
286 
287  ++NumOperandsMappingsAccessed;
288 
289  // The addresses of the value mapping are unique.
290  // Therefore, we can use them directly to hash the operand mapping.
291  hash_code Hash = hash_combine_range(Begin, End);
292  auto &Res = MapOfOperandsMappings[Hash];
293  if (Res)
294  return Res.get();
295 
296  ++NumOperandsMappingsCreated;
297 
298  // Create the array of ValueMapping.
299  // Note: this array will not hash to this instance of operands
300  // mapping, because we use the pointer of the ValueMapping
301  // to hash and we expect them to uniquely identify an instance
302  // of value mapping.
303  Res = llvm::make_unique<ValueMapping[]>(std::distance(Begin, End));
304  unsigned Idx = 0;
305  for (Iterator It = Begin; It != End; ++It, ++Idx) {
306  const ValueMapping *ValMap = *It;
307  if (!ValMap)
308  continue;
309  Res[Idx] = *ValMap;
310  }
311  return Res.get();
312 }
313 
316  const {
317  return getOperandsMapping(OpdsMapping.begin(), OpdsMapping.end());
318 }
319 
321  std::initializer_list<const RegisterBankInfo::ValueMapping *> OpdsMapping)
322  const {
323  return getOperandsMapping(OpdsMapping.begin(), OpdsMapping.end());
324 }
325 
326 static hash_code
327 hashInstructionMapping(unsigned ID, unsigned Cost,
328  const RegisterBankInfo::ValueMapping *OperandsMapping,
329  unsigned NumOperands) {
330  return hash_combine(ID, Cost, OperandsMapping, NumOperands);
331 }
332 
334 RegisterBankInfo::getInstructionMappingImpl(
335  bool IsInvalid, unsigned ID, unsigned Cost,
336  const RegisterBankInfo::ValueMapping *OperandsMapping,
337  unsigned NumOperands) const {
338  assert(((IsInvalid && ID == InvalidMappingID && Cost == 0 &&
339  OperandsMapping == nullptr && NumOperands == 0) ||
340  !IsInvalid) &&
341  "Mismatch argument for invalid input");
342  ++NumInstructionMappingsAccessed;
343 
344  hash_code Hash =
345  hashInstructionMapping(ID, Cost, OperandsMapping, NumOperands);
346  const auto &It = MapOfInstructionMappings.find(Hash);
347  if (It != MapOfInstructionMappings.end())
348  return *It->second;
349 
350  ++NumInstructionMappingsCreated;
351 
352  auto &InstrMapping = MapOfInstructionMappings[Hash];
353  if (IsInvalid)
354  InstrMapping = llvm::make_unique<InstructionMapping>();
355  else
356  InstrMapping = llvm::make_unique<InstructionMapping>(
357  ID, Cost, OperandsMapping, NumOperands);
358  return *InstrMapping;
359 }
360 
364  if (Mapping.isValid())
365  return Mapping;
366  llvm_unreachable("The target must implement this");
367 }
368 
370 RegisterBankInfo::getInstrPossibleMappings(const MachineInstr &MI) const {
371  InstructionMappings PossibleMappings;
372  // Put the default mapping first.
373  PossibleMappings.push_back(&getInstrMapping(MI));
374  // Then the alternative mapping, if any.
376  for (const InstructionMapping *AltMapping : AltMappings)
377  PossibleMappings.push_back(AltMapping);
378 #ifndef NDEBUG
379  for (const InstructionMapping *Mapping : PossibleMappings)
380  assert(Mapping->verify(MI) && "Mapping is invalid");
381 #endif
382  return PossibleMappings;
383 }
384 
387  // No alternative for MI.
388  return InstructionMappings();
389 }
390 
392  MachineInstr &MI = OpdMapper.getMI();
393  MachineRegisterInfo &MRI = OpdMapper.getMRI();
394  DEBUG(dbgs() << "Applying default-like mapping\n");
395  for (unsigned OpIdx = 0,
396  EndIdx = OpdMapper.getInstrMapping().getNumOperands();
397  OpIdx != EndIdx; ++OpIdx) {
398  DEBUG(dbgs() << "OpIdx " << OpIdx);
399  MachineOperand &MO = MI.getOperand(OpIdx);
400  if (!MO.isReg()) {
401  DEBUG(dbgs() << " is not a register, nothing to be done\n");
402  continue;
403  }
404  if (!MO.getReg()) {
405  DEBUG(dbgs() << " is %%noreg, nothing to be done\n");
406  continue;
407  }
408  assert(OpdMapper.getInstrMapping().getOperandMapping(OpIdx).NumBreakDowns !=
409  0 &&
410  "Invalid mapping");
411  assert(OpdMapper.getInstrMapping().getOperandMapping(OpIdx).NumBreakDowns ==
412  1 &&
413  "This mapping is too complex for this function");
415  OpdMapper.getVRegs(OpIdx);
416  if (NewRegs.begin() == NewRegs.end()) {
417  DEBUG(dbgs() << " has not been repaired, nothing to be done\n");
418  continue;
419  }
420  unsigned OrigReg = MO.getReg();
421  unsigned NewReg = *NewRegs.begin();
422  DEBUG(dbgs() << " changed, replace " << PrintReg(OrigReg, nullptr));
423  MO.setReg(NewReg);
424  DEBUG(dbgs() << " with " << PrintReg(NewReg, nullptr));
425 
426  // The OperandsMapper creates plain scalar, we may have to fix that.
427  // Check if the types match and if not, fix that.
428  LLT OrigTy = MRI.getType(OrigReg);
429  LLT NewTy = MRI.getType(NewReg);
430  if (OrigTy != NewTy) {
431  assert(OrigTy.getSizeInBits() == NewTy.getSizeInBits() &&
432  "Types with difference size cannot be handled by the default "
433  "mapping");
434  DEBUG(dbgs() << "\nChange type of new opd from " << NewTy << " to "
435  << OrigTy);
436  MRI.setType(NewReg, OrigTy);
437  }
438  DEBUG(dbgs() << '\n');
439  }
440 }
441 
442 unsigned RegisterBankInfo::getSizeInBits(unsigned Reg,
443  const MachineRegisterInfo &MRI,
444  const TargetRegisterInfo &TRI) {
445  const TargetRegisterClass *RC = nullptr;
447  // The size is not directly available for physical registers.
448  // Instead, we need to access a register class that contains Reg and
449  // get the size of that register class.
450  RC = TRI.getMinimalPhysRegClass(Reg);
451  } else {
452  LLT Ty = MRI.getType(Reg);
453  unsigned RegSize = Ty.isValid() ? Ty.getSizeInBits() : 0;
454  // If Reg is not a generic register, query the register class to
455  // get its size.
456  if (RegSize)
457  return RegSize;
458  // Since Reg is not a generic register, it must have a register class.
459  RC = MRI.getRegClass(Reg);
460  }
461  assert(RC && "Unable to deduce the register class");
462  return TRI.getRegSizeInBits(*RC);
463 }
464 
465 //------------------------------------------------------------------------------
466 // Helper classes implementation.
467 //------------------------------------------------------------------------------
468 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
470  print(dbgs());
471  dbgs() << '\n';
472 }
473 #endif
474 
476  assert(RegBank && "Register bank not set");
477  assert(Length && "Empty mapping");
478  assert((StartIdx <= getHighBitIdx()) && "Overflow, switch to APInt?");
479  // Check if the minimum width fits into RegBank.
480  assert(RegBank->getSize() >= Length && "Register bank too small for Mask");
481  return true;
482 }
483 
485  OS << "[" << StartIdx << ", " << getHighBitIdx() << "], RegBank = ";
486  if (RegBank)
487  OS << *RegBank;
488  else
489  OS << "nullptr";
490 }
491 
492 bool RegisterBankInfo::ValueMapping::verify(unsigned MeaningfulBitWidth) const {
493  assert(NumBreakDowns && "Value mapped nowhere?!");
494  unsigned OrigValueBitWidth = 0;
495  for (const RegisterBankInfo::PartialMapping &PartMap : *this) {
496  // Check that each register bank is big enough to hold the partial value:
497  // this check is done by PartialMapping::verify
498  assert(PartMap.verify() && "Partial mapping is invalid");
499  // The original value should completely be mapped.
500  // Thus the maximum accessed index + 1 is the size of the original value.
501  OrigValueBitWidth =
502  std::max(OrigValueBitWidth, PartMap.getHighBitIdx() + 1);
503  }
504  assert(OrigValueBitWidth >= MeaningfulBitWidth &&
505  "Meaningful bits not covered by the mapping");
506  APInt ValueMask(OrigValueBitWidth, 0);
507  for (const RegisterBankInfo::PartialMapping &PartMap : *this) {
508  // Check that the union of the partial mappings covers the whole value,
509  // without overlaps.
510  // The high bit is exclusive in the APInt API, thus getHighBitIdx + 1.
511  APInt PartMapMask = APInt::getBitsSet(OrigValueBitWidth, PartMap.StartIdx,
512  PartMap.getHighBitIdx() + 1);
513  ValueMask ^= PartMapMask;
514  assert((ValueMask & PartMapMask) == PartMapMask &&
515  "Some partial mappings overlap");
516  }
517  assert(ValueMask.isAllOnesValue() && "Value is not fully mapped");
518  return true;
519 }
520 
521 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
523  print(dbgs());
524  dbgs() << '\n';
525 }
526 #endif
527 
529  OS << "#BreakDown: " << NumBreakDowns << " ";
530  bool IsFirst = true;
531  for (const PartialMapping &PartMap : *this) {
532  if (!IsFirst)
533  OS << ", ";
534  OS << '[' << PartMap << ']';
535  IsFirst = false;
536  }
537 }
538 
540  const MachineInstr &MI) const {
541  // Check that all the register operands are properly mapped.
542  // Check the constructor invariant.
543  // For PHI, we only care about mapping the definition.
544  assert(NumOperands == (isCopyLike(MI) ? 1 : MI.getNumOperands()) &&
545  "NumOperands must match, see constructor");
546  assert(MI.getParent() && MI.getParent()->getParent() &&
547  "MI must be connected to a MachineFunction");
548  const MachineFunction &MF = *MI.getParent()->getParent();
549  (void)MF;
550 
551  for (unsigned Idx = 0; Idx < NumOperands; ++Idx) {
552  const MachineOperand &MO = MI.getOperand(Idx);
553  if (!MO.isReg()) {
554  assert(!getOperandMapping(Idx).isValid() &&
555  "We should not care about non-reg mapping");
556  continue;
557  }
558  unsigned Reg = MO.getReg();
559  if (!Reg)
560  continue;
561  assert(getOperandMapping(Idx).isValid() &&
562  "We must have a mapping for reg operands");
563  const RegisterBankInfo::ValueMapping &MOMapping = getOperandMapping(Idx);
564  (void)MOMapping;
565  // Register size in bits.
566  // This size must match what the mapping expects.
567  assert(MOMapping.verify(getSizeInBits(
568  Reg, MF.getRegInfo(), *MF.getSubtarget().getRegisterInfo())) &&
569  "Value mapping is invalid");
570  }
571  return true;
572 }
573 
574 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
576  print(dbgs());
577  dbgs() << '\n';
578 }
579 #endif
580 
582  OS << "ID: " << getID() << " Cost: " << getCost() << " Mapping: ";
583 
584  for (unsigned OpIdx = 0; OpIdx != NumOperands; ++OpIdx) {
585  const ValueMapping &ValMapping = getOperandMapping(OpIdx);
586  if (OpIdx)
587  OS << ", ";
588  OS << "{ Idx: " << OpIdx << " Map: " << ValMapping << '}';
589  }
590 }
591 
592 const int RegisterBankInfo::OperandsMapper::DontKnowIdx = -1;
593 
595  MachineInstr &MI, const InstructionMapping &InstrMapping,
596  MachineRegisterInfo &MRI)
597  : MRI(MRI), MI(MI), InstrMapping(InstrMapping) {
598  unsigned NumOpds = InstrMapping.getNumOperands();
599  OpToNewVRegIdx.resize(NumOpds, OperandsMapper::DontKnowIdx);
600  assert(InstrMapping.verify(MI) && "Invalid mapping for MI");
601 }
602 
604 RegisterBankInfo::OperandsMapper::getVRegsMem(unsigned OpIdx) {
605  assert(OpIdx < getInstrMapping().getNumOperands() && "Out-of-bound access");
606  unsigned NumPartialVal =
607  getInstrMapping().getOperandMapping(OpIdx).NumBreakDowns;
608  int StartIdx = OpToNewVRegIdx[OpIdx];
609 
610  if (StartIdx == OperandsMapper::DontKnowIdx) {
611  // This is the first time we try to access OpIdx.
612  // Create the cells that will hold all the partial values at the
613  // end of the list of NewVReg.
614  StartIdx = NewVRegs.size();
615  OpToNewVRegIdx[OpIdx] = StartIdx;
616  for (unsigned i = 0; i < NumPartialVal; ++i)
617  NewVRegs.push_back(0);
618  }
620  getNewVRegsEnd(StartIdx, NumPartialVal);
621 
622  return make_range(&NewVRegs[StartIdx], End);
623 }
624 
626 RegisterBankInfo::OperandsMapper::getNewVRegsEnd(unsigned StartIdx,
627  unsigned NumVal) const {
628  return const_cast<OperandsMapper *>(this)->getNewVRegsEnd(StartIdx, NumVal);
629 }
631 RegisterBankInfo::OperandsMapper::getNewVRegsEnd(unsigned StartIdx,
632  unsigned NumVal) {
633  assert((NewVRegs.size() == StartIdx + NumVal ||
634  NewVRegs.size() > StartIdx + NumVal) &&
635  "NewVRegs too small to contain all the partial mapping");
636  return NewVRegs.size() <= StartIdx + NumVal ? NewVRegs.end()
637  : &NewVRegs[StartIdx + NumVal];
638 }
639 
641  assert(OpIdx < getInstrMapping().getNumOperands() && "Out-of-bound access");
643  getVRegsMem(OpIdx);
644  const ValueMapping &ValMapping = getInstrMapping().getOperandMapping(OpIdx);
645  const PartialMapping *PartMap = ValMapping.begin();
646  for (unsigned &NewVReg : NewVRegsForOpIdx) {
647  assert(PartMap != ValMapping.end() && "Out-of-bound access");
648  assert(NewVReg == 0 && "Register has already been created");
649  // The new registers are always bound to scalar with the right size.
650  // The actual type has to be set when the target does the mapping
651  // of the instruction.
652  // The rationale is that this generic code cannot guess how the
653  // target plans to split the input type.
654  NewVReg = MRI.createGenericVirtualRegister(LLT::scalar(PartMap->Length));
655  MRI.setRegBank(NewVReg, *PartMap->RegBank);
656  ++PartMap;
657  }
658 }
659 
661  unsigned PartialMapIdx,
662  unsigned NewVReg) {
663  assert(OpIdx < getInstrMapping().getNumOperands() && "Out-of-bound access");
664  assert(getInstrMapping().getOperandMapping(OpIdx).NumBreakDowns >
665  PartialMapIdx &&
666  "Out-of-bound access for partial mapping");
667  // Make sure the memory is initialized for that operand.
668  (void)getVRegsMem(OpIdx);
669  assert(NewVRegs[OpToNewVRegIdx[OpIdx] + PartialMapIdx] == 0 &&
670  "This value is already set");
671  NewVRegs[OpToNewVRegIdx[OpIdx] + PartialMapIdx] = NewVReg;
672 }
673 
676  bool ForDebug) const {
677  (void)ForDebug;
678  assert(OpIdx < getInstrMapping().getNumOperands() && "Out-of-bound access");
679  int StartIdx = OpToNewVRegIdx[OpIdx];
680 
681  if (StartIdx == OperandsMapper::DontKnowIdx)
682  return make_range(NewVRegs.end(), NewVRegs.end());
683 
684  unsigned PartMapSize =
685  getInstrMapping().getOperandMapping(OpIdx).NumBreakDowns;
687  getNewVRegsEnd(StartIdx, PartMapSize);
689  make_range(&NewVRegs[StartIdx], End);
690 #ifndef NDEBUG
691  for (unsigned VReg : Res)
692  assert((VReg || ForDebug) && "Some registers are uninitialized");
693 #endif
694  return Res;
695 }
696 
697 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
699  print(dbgs(), true);
700  dbgs() << '\n';
701 }
702 #endif
703 
705  bool ForDebug) const {
706  unsigned NumOpds = getInstrMapping().getNumOperands();
707  if (ForDebug) {
708  OS << "Mapping for " << getMI() << "\nwith " << getInstrMapping() << '\n';
709  // Print out the internal state of the index table.
710  OS << "Populated indices (CellNumber, IndexInNewVRegs): ";
711  bool IsFirst = true;
712  for (unsigned Idx = 0; Idx != NumOpds; ++Idx) {
713  if (OpToNewVRegIdx[Idx] != DontKnowIdx) {
714  if (!IsFirst)
715  OS << ", ";
716  OS << '(' << Idx << ", " << OpToNewVRegIdx[Idx] << ')';
717  IsFirst = false;
718  }
719  }
720  OS << '\n';
721  } else
722  OS << "Mapping ID: " << getInstrMapping().getID() << ' ';
723 
724  OS << "Operand Mapping: ";
725  // If we have a function, we can pretty print the name of the registers.
726  // Otherwise we will print the raw numbers.
727  const TargetRegisterInfo *TRI =
728  getMI().getParent() && getMI().getParent()->getParent()
729  ? getMI().getParent()->getParent()->getSubtarget().getRegisterInfo()
730  : nullptr;
731  bool IsFirst = true;
732  for (unsigned Idx = 0; Idx != NumOpds; ++Idx) {
733  if (OpToNewVRegIdx[Idx] == DontKnowIdx)
734  continue;
735  if (!IsFirst)
736  OS << ", ";
737  IsFirst = false;
738  OS << '(' << PrintReg(getMI().getOperand(Idx).getReg(), TRI) << ", [";
739  bool IsFirstNewVReg = true;
740  for (unsigned VReg : getVRegs(Idx)) {
741  if (!IsFirstNewVReg)
742  OS << ", ";
743  IsFirstNewVReg = false;
744  OS << PrintReg(VReg, TRI);
745  }
746  OS << "])";
747  }
748 }
unsigned NumRegBanks
Total number of register banks.
void dump() const
Print this on dbgs() stream.
static const unsigned InvalidMappingID
Identifier used when the related instruction mapping instance is generated by the default constructor...
RegisterBank ** RegBanks
Hold the set of supported register banks.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
const InstructionMapping & getInvalidInstructionMapping() const
Method to get a uniquely generated invalid InstructionMapping.
Helper class that represents how the value of an instruction may be mapped and what is the related co...
const InstructionMapping & getInstructionMapping(unsigned ID, unsigned Cost, const ValueMapping *OperandsMapping, unsigned NumOperands) const
Method to get a uniquely generated InstructionMapping.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
virtual const RegisterBank & getRegBankFromRegClass(const TargetRegisterClass &RC) const
Get a register bank that covers RC.
LLT getType(unsigned VReg) const
Get the low-level type of VReg or LLT{} if VReg is not a generic (target independent) virtual registe...
STATISTIC(NumFunctions,"Total number of functions")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:449
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:175
DenseMap< unsigned, std::unique_ptr< const PartialMapping > > MapOfPartialMappings
Keep dynamically allocated PartialMapping in a separate map.
Helper class that represents how the value of an instruction may be mapped and what is the related co...
unsigned getRegSizeInBits(const TargetRegisterClass &RC) const
Return the size in bits of a register from class RC.
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
T dyn_cast() const
Returns the current pointer if it is of the specified pointer type, otherwises returns null...
Definition: PointerUnion.h:142
const ValueMapping & getValueMapping(unsigned StartIdx, unsigned Length, const RegisterBank &RegBank) const
The most common ValueMapping consists of a single PartialMapping.
iterator_range< SmallVectorImpl< unsigned >::const_iterator > getVRegs(unsigned OpIdx, bool ForDebug=false) const
Get all the virtual registers required to map the OpIdx-th operand of the instruction.
Helper class used to get/create the virtual registers that will be used to replace the MachineOperand...
void setRegBank(unsigned Reg, const RegisterBank &RegBank)
Set the register bank to RegBank for Reg.
unsigned createGenericVirtualRegister(LLT Ty)
Create and return a new generic virtual register with low-level type Ty.
static hash_code hashValueMapping(const RegisterBankInfo::PartialMapping *BreakDown, unsigned NumBreakDowns)
bool verify(const TargetRegisterInfo &TRI) const
Check if this register bank is valid.
virtual const InstructionMapping & getInstrMapping(const MachineInstr &MI) const
Get the mapping of the different operands of MI on the register bank.
void print(raw_ostream &OS, bool ForDebug=false) const
Print this operands mapper on OS stream.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
const HexagonInstrInfo * TII
bool isPHI() const
Definition: MachineInstr.h:795
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
bool isReg() const
isReg - Tests if this is a MO_Register operand.
const TargetRegisterClass * getRegClass(unsigned Reg) const
Return the register class of the specified virtual register.
Reg
All possible values of the reg field in the ModR/M byte.
unsigned StartIdx
Number of bits at which this partial mapping starts in the original value.
const RegisterBank * getRegBankFromConstraints(const MachineInstr &MI, unsigned OpIdx, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI) const
Get the register bank for the OpIdx-th operand of MI form the encoding constraints, if any.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:282
bool verify(unsigned MeaningfulBitWidth) const
Verify that this mapping makes sense for a value of MeaningfulBitWidth.
virtual InstructionMappings getInstrAlternativeMappings(const MachineInstr &MI) const
Get the alternative mappings for MI.
bool verify() const
Check that the Mask is compatible with the RegBank.
static int getID(struct InternalInstruction *insn, const void *miiArg)
hash_code hash_value(const APFloat &Arg)
See friend declarations above.
Definition: APFloat.cpp:4428
static LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
Printable PrintReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubRegIdx=0)
Prints virtual and physical registers with or without a TRI instance.
const TargetRegisterClass * constrainRegClass(unsigned Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
constrainRegClass - Constrain the register class of the specified virtual register to be a common sub...
const ValueMapping * getOperandsMapping(Iterator Begin, Iterator End) const
Get the uniquely generated array of ValueMapping for the elements of between Begin and End...
const RegisterBank * RegBank
Register bank where the partial value lives.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:279
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:139
TargetInstrInfo - Interface to description of machine instruction set.
void setVRegs(unsigned OpIdx, unsigned PartialMapIdx, unsigned NewVReg)
Set the virtual register of the PartialMapIdx-th partial mapping of the OpIdx-th operand to NewVReg...
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
unsigned const MachineRegisterInfo * MRI
static unsigned getSizeInBits(unsigned Reg, const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI)
Get the size in bits of Reg.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:284
const InstructionMapping & getInstrMapping() const
The final mapping of the instruction.
unsigned getID() const
Get the identifier of this register bank.
Definition: RegisterBank.h:48
Helper struct that represents how a value is partially mapped into a register.
unsigned Length
Length of this mapping in bits.
void setType(unsigned VReg, LLT Ty)
Set the low-level type of VReg to Ty.
bool isCopy() const
Definition: MachineInstr.h:823
static bool isCopyLike(const MachineInstr &MI)
Check whether or not MI should be treated like a copy for the mappings.
static const unsigned End
static const unsigned DefaultMappingID
Identifier used when the related instruction mapping instance is generated by target independent code...
const PartialMapping * begin() const
Iterators through the PartialMappings.
void dump() const
Print this partial mapping on dbgs() stream.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Helper class used to get/create the virtual registers that will be used to replace the MachineOperand...
OperandsMapper(MachineInstr &MI, const InstructionMapping &InstrMapping, MachineRegisterInfo &MRI)
Create an OperandsMapper that will hold the information to apply InstrMapping to MI.
static hash_code hashPartialMapping(unsigned StartIdx, unsigned Length, const RegisterBank *RegBank)
Hashing function for PartialMapping.
MachineRegisterInfo & getMRI() const
The MachineRegisterInfo we used to realize the mapping.
RegisterBank & getRegBank(unsigned ID)
Get the register bank identified by ID.
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
static void applyDefaultMapping(const OperandsMapper &OpdMapper)
Helper method to apply something that is like the default mapping.
This class implements the register bank concept.
Definition: RegisterBank.h:29
Helper struct that represents how a value is mapped through different register banks.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
A range adaptor for a pair of iterators.
void dump() const
Print this operands mapper on dbgs() stream.
Class for arbitrary precision integers.
Definition: APInt.h:69
const TargetRegisterClass * getRegClassConstraint(unsigned OpIdx, const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const
Compute the static register class constraint for operand OpIdx.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:602
static unsigned getReg(const void *D, unsigned RC, unsigned RegNo)
void print(raw_ostream &OS) const
Print this on OS;.
const TargetRegisterClass * getMinimalPhysRegClass(unsigned Reg, MVT VT=MVT::Other) const
Returns the Register Class of a physical register of the given type, picking the most sub register cl...
bool isValid() const
Check whether this object is valid.
static hash_code hashInstructionMapping(unsigned ID, unsigned Cost, const RegisterBankInfo::ValueMapping *OperandsMapping, unsigned NumOperands)
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition: Hashing.h:480
An opaque object representing a hash code.
Definition: Hashing.h:72
void createVRegs(unsigned OpIdx)
Create as many new virtual registers as needed for the mapping of the OpIdx-th operand.
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition: APInt.h:600
llvm_unreachable("Unknown RegBank!")
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
TargetSubtargetInfo - Generic base class for all target subtargets.
bool isAllOnesValue() const
Determine if all bits are set.
Definition: APInt.h:389
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.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void print(raw_ostream &OS) const
Print this on OS;.
const RegClassOrRegBank & getRegClassOrRegBank(unsigned Reg) const
Return the register bank or register class of Reg.
DenseMap< unsigned, std::unique_ptr< ValueMapping[]> > MapOfOperandsMappings
Keep dynamically allocated array of ValueMapping in a separate map.
void setReg(unsigned Reg)
Change the register this operand corresponds to.
unsigned getNumRegBanks() const
Get the total number of register banks.
static const TargetRegisterClass * constrainGenericRegister(unsigned Reg, const TargetRegisterClass &RC, MachineRegisterInfo &MRI)
Constrain the (possibly generic) virtual register Reg to RC.
bool isValid() const
unsigned getNumOperands() const
Get the number of operands.
unsigned getSize() const
Get the maximal size in bits that fits in this register bank.
Definition: RegisterBank.h:55
INITIALIZE_PASS(HexagonGenMux,"hexagon-gen-mux","Hexagon generate mux instructions", false, false) void HexagonGenMux I isValid()
bool covers(const TargetRegisterClass &RC) const
Check whether this register bank covers RC.
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const InstructionMapping & getInstrMappingImpl(const MachineInstr &MI) const
Try to get the mapping of MI.
void dump() const
Print this on dbgs() stream.
unsigned NumBreakDowns
Number of partial mapping to break down this value.
virtual const TargetInstrInfo * getInstrInfo() const
bool verify(const MachineInstr &MI) const
Verifiy that this mapping makes sense for MI.
const PartialMapping & getPartialMapping(unsigned StartIdx, unsigned Length, const RegisterBank &RegBank) const
Get the uniquely generated PartialMapping for the given arguments.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
#define DEBUG(X)
Definition: Debug.h:118
DenseMap< unsigned, std::unique_ptr< const InstructionMapping > > MapOfInstructionMappings
Keep dynamically allocated InstructionMapping in a separate map.
std::string Hash(const Unit &U)
Definition: FuzzerSHA1.cpp:216
IRTranslator LLVM IR MI
const PartialMapping * end() const
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
void setRegClass(unsigned Reg, const TargetRegisterClass *RC)
setRegClass - Set the register class of the specified virtual register.
unsigned getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
RegisterBankInfo()
This constructor is meaningless.
ppc ctr loops verify
DenseMap< unsigned, std::unique_ptr< const ValueMapping > > MapOfValueMappings
Keep dynamically allocated ValueMapping in a separate map.
SmallVector< const InstructionMapping *, 4 > InstructionMappings
Convenient type to represent the alternatives for mapping an instruction.
void print(raw_ostream &OS) const
Print this partial mapping on OS;.
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...
Definition: PointerUnion.h:87
void resize(size_type N)
Definition: SmallVector.h:352