LLVM 18.0.0git
RegisterBankInfo.h
Go to the documentation of this file.
1//===- llvm/CodeGen/RegisterBankInfo.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/// \file This file declares the API for the register bank info.
10/// This API is responsible for handling the register banks.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CODEGEN_REGISTERBANKINFO_H
15#define LLVM_CODEGEN_REGISTERBANKINFO_H
16
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/Hashing.h"
25#include <cassert>
26#include <initializer_list>
27#include <memory>
28
29namespace llvm {
30
31class MachineInstr;
32class MachineIRBuilder;
33class MachineRegisterInfo;
34class raw_ostream;
35class TargetInstrInfo;
36class TargetRegisterClass;
37class TargetRegisterInfo;
38
39/// Holds all the information related to register banks.
41public:
42 /// Helper struct that represents how a value is partially mapped
43 /// into a register.
44 /// The StartIdx and Length represent what region of the orginal
45 /// value this partial mapping covers.
46 /// This can be represented as a Mask of contiguous bit starting
47 /// at StartIdx bit and spanning Length bits.
48 /// StartIdx is the number of bits from the less significant bits.
50 /// Number of bits at which this partial mapping starts in the
51 /// original value. The bits are counted from less significant
52 /// bits to most significant bits.
53 unsigned StartIdx;
54
55 /// Length of this mapping in bits. This is how many bits this
56 /// partial mapping covers in the original value:
57 /// from StartIdx to StartIdx + Length -1.
58 unsigned Length;
59
60 /// Register bank where the partial value lives.
62
63 PartialMapping() = default;
64
65 /// Provide a shortcut for quickly building PartialMapping.
66 PartialMapping(unsigned StartIdx, unsigned Length,
67 const RegisterBank &RegBank)
69
70 /// \return the index of in the original value of the most
71 /// significant bit that this partial mapping covers.
72 unsigned getHighBitIdx() const { return StartIdx + Length - 1; }
73
74 /// Print this partial mapping on dbgs() stream.
75 void dump() const;
76
77 /// Print this partial mapping on \p OS;
78 void print(raw_ostream &OS) const;
79
80 /// Check that the Mask is compatible with the RegBank.
81 /// Indeed, if the RegBank cannot accomadate the "active bits" of the mask,
82 /// there is no way this mapping is valid.
83 ///
84 /// \note This method does not check anything when assertions are disabled.
85 ///
86 /// \return True is the check was successful.
87 bool verify(const RegisterBankInfo &RBI) const;
88 };
89
90 /// Helper struct that represents how a value is mapped through
91 /// different register banks.
92 ///
93 /// \note: So far we do not have any users of the complex mappings
94 /// (mappings with more than one partial mapping), but when we do,
95 /// we would have needed to duplicate partial mappings.
96 /// The alternative could be to use an array of pointers of partial
97 /// mapping (i.e., PartialMapping **BreakDown) and duplicate the
98 /// pointers instead.
99 ///
100 /// E.g.,
101 /// Let say we have a 32-bit add and a <2 x 32-bit> vadd. We
102 /// can expand the
103 /// <2 x 32-bit> add into 2 x 32-bit add.
104 ///
105 /// Currently the TableGen-like file would look like:
106 /// \code
107 /// PartialMapping[] = {
108 /// /*32-bit add*/ {0, 32, GPR}, // Scalar entry repeated for first
109 /// // vec elt.
110 /// /*2x32-bit add*/ {0, 32, GPR}, {32, 32, GPR},
111 /// /*<2x32-bit> vadd*/ {0, 64, VPR}
112 /// }; // PartialMapping duplicated.
113 ///
114 /// ValueMapping[] {
115 /// /*plain 32-bit add*/ {&PartialMapping[0], 1},
116 /// /*expanded vadd on 2xadd*/ {&PartialMapping[1], 2},
117 /// /*plain <2x32-bit> vadd*/ {&PartialMapping[3], 1}
118 /// };
119 /// \endcode
120 ///
121 /// With the array of pointer, we would have:
122 /// \code
123 /// PartialMapping[] = {
124 /// /*32-bit add lower */ { 0, 32, GPR},
125 /// /*32-bit add upper */ {32, 32, GPR},
126 /// /*<2x32-bit> vadd */ { 0, 64, VPR}
127 /// }; // No more duplication.
128 ///
129 /// BreakDowns[] = {
130 /// /*AddBreakDown*/ &PartialMapping[0],
131 /// /*2xAddBreakDown*/ &PartialMapping[0], &PartialMapping[1],
132 /// /*VAddBreakDown*/ &PartialMapping[2]
133 /// }; // Addresses of PartialMapping duplicated (smaller).
134 ///
135 /// ValueMapping[] {
136 /// /*plain 32-bit add*/ {&BreakDowns[0], 1},
137 /// /*expanded vadd on 2xadd*/ {&BreakDowns[1], 2},
138 /// /*plain <2x32-bit> vadd*/ {&BreakDowns[3], 1}
139 /// };
140 /// \endcode
141 ///
142 /// Given that a PartialMapping is actually small, the code size
143 /// impact is actually a degradation. Moreover the compile time will
144 /// be hit by the additional indirection.
145 /// If PartialMapping gets bigger we may reconsider.
147 /// How the value is broken down between the different register banks.
149
150 /// Number of partial mapping to break down this value.
152
153 /// The default constructor creates an invalid (isValid() == false)
154 /// instance.
155 ValueMapping() : ValueMapping(nullptr, 0) {}
156
157 /// Initialize a ValueMapping with the given parameter.
158 /// \p BreakDown needs to have a life time at least as long
159 /// as this instance.
162
163 /// Iterators through the PartialMappings.
164 const PartialMapping *begin() const { return BreakDown; }
165 const PartialMapping *end() const { return BreakDown + NumBreakDowns; }
166
167 /// \return true if all partial mappings are the same size and register
168 /// bank.
169 bool partsAllUniform() const;
170
171 /// Check if this ValueMapping is valid.
172 bool isValid() const { return BreakDown && NumBreakDowns; }
173
174 /// Verify that this mapping makes sense for a value of
175 /// \p MeaningfulBitWidth.
176 /// \note This method does not check anything when assertions are disabled.
177 ///
178 /// \return True is the check was successful.
179 bool verify(const RegisterBankInfo &RBI, unsigned MeaningfulBitWidth) const;
180
181 /// Print this on dbgs() stream.
182 void dump() const;
183
184 /// Print this on \p OS;
185 void print(raw_ostream &OS) const;
186 };
187
188 /// Helper class that represents how the value of an instruction may be
189 /// mapped and what is the related cost of such mapping.
191 /// Identifier of the mapping.
192 /// This is used to communicate between the target and the optimizers
193 /// which mapping should be realized.
194 unsigned ID = InvalidMappingID;
195
196 /// Cost of this mapping.
197 unsigned Cost = 0;
198
199 /// Mapping of all the operands.
200 const ValueMapping *OperandsMapping = nullptr;
201
202 /// Number of operands.
203 unsigned NumOperands = 0;
204
205 const ValueMapping &getOperandMapping(unsigned i) {
206 assert(i < getNumOperands() && "Out of bound operand");
207 return OperandsMapping[i];
208 }
209
210 public:
211 /// Constructor for the mapping of an instruction.
212 /// \p NumOperands must be equal to number of all the operands of
213 /// the related instruction.
214 /// The rationale is that it is more efficient for the optimizers
215 /// to be able to assume that the mapping of the ith operand is
216 /// at the index i.
217 InstructionMapping(unsigned ID, unsigned Cost,
218 const ValueMapping *OperandsMapping,
219 unsigned NumOperands)
220 : ID(ID), Cost(Cost), OperandsMapping(OperandsMapping),
221 NumOperands(NumOperands) {}
222
223 /// Default constructor.
224 /// Use this constructor to express that the mapping is invalid.
226
227 /// Get the cost.
228 unsigned getCost() const { return Cost; }
229
230 /// Get the ID.
231 unsigned getID() const { return ID; }
232
233 /// Get the number of operands.
234 unsigned getNumOperands() const { return NumOperands; }
235
236 /// Get the value mapping of the ith operand.
237 /// \pre The mapping for the ith operand has been set.
238 /// \pre The ith operand is a register.
239 const ValueMapping &getOperandMapping(unsigned i) const {
240 const ValueMapping &ValMapping =
241 const_cast<InstructionMapping *>(this)->getOperandMapping(i);
242 return ValMapping;
243 }
244
245 /// Set the mapping for all the operands.
246 /// In other words, OpdsMapping should hold at least getNumOperands
247 /// ValueMapping.
248 void setOperandsMapping(const ValueMapping *OpdsMapping) {
249 OperandsMapping = OpdsMapping;
250 }
251
252 /// Check whether this object is valid.
253 /// This is a lightweight check for obvious wrong instance.
254 bool isValid() const {
255 return getID() != InvalidMappingID && OperandsMapping;
256 }
257
258 /// Verifiy that this mapping makes sense for \p MI.
259 /// \pre \p MI must be connected to a MachineFunction.
260 ///
261 /// \note This method does not check anything when assertions are disabled.
262 ///
263 /// \return True is the check was successful.
264 bool verify(const MachineInstr &MI) const;
265
266 /// Print this on dbgs() stream.
267 void dump() const;
268
269 /// Print this on \p OS;
270 void print(raw_ostream &OS) const;
271 };
272
273 /// Convenient type to represent the alternatives for mapping an
274 /// instruction.
275 /// \todo When we move to TableGen this should be an array ref.
277
278 /// Helper class used to get/create the virtual registers that will be used
279 /// to replace the MachineOperand when applying a mapping.
281 /// The OpIdx-th cell contains the index in NewVRegs where the VRegs of the
282 /// OpIdx-th operand starts. -1 means we do not have such mapping yet.
283 /// Note: We use a SmallVector to avoid heap allocation for most cases.
284 SmallVector<int, 8> OpToNewVRegIdx;
285
286 /// Hold the registers that will be used to map MI with InstrMapping.
288
289 /// Current MachineRegisterInfo, used to create new virtual registers.
291
292 /// Instruction being remapped.
293 MachineInstr &MI;
294
295 /// New mapping of the instruction.
296 const InstructionMapping &InstrMapping;
297
298 /// Constant value identifying that the index in OpToNewVRegIdx
299 /// for an operand has not been set yet.
300 static const int DontKnowIdx;
301
302 /// Get the range in NewVRegs to store all the partial
303 /// values for the \p OpIdx-th operand.
304 ///
305 /// \return The iterator range for the space created.
306 //
307 /// \pre getMI().getOperand(OpIdx).isReg()
309 getVRegsMem(unsigned OpIdx);
310
311 /// Get the end iterator for a range starting at \p StartIdx and
312 /// spannig \p NumVal in NewVRegs.
313 /// \pre StartIdx + NumVal <= NewVRegs.size()
315 getNewVRegsEnd(unsigned StartIdx, unsigned NumVal) const;
316 SmallVectorImpl<Register>::iterator getNewVRegsEnd(unsigned StartIdx,
317 unsigned NumVal);
318
319 public:
320 /// Create an OperandsMapper that will hold the information to apply \p
321 /// InstrMapping to \p MI.
322 /// \pre InstrMapping.verify(MI)
323 OperandsMapper(MachineInstr &MI, const InstructionMapping &InstrMapping,
325
326 /// \name Getters.
327 /// @{
328 /// The MachineInstr being remapped.
329 MachineInstr &getMI() const { return MI; }
330
331 /// The final mapping of the instruction.
332 const InstructionMapping &getInstrMapping() const { return InstrMapping; }
333
334 /// The MachineRegisterInfo we used to realize the mapping.
335 MachineRegisterInfo &getMRI() const { return MRI; }
336 /// @}
337
338 /// Create as many new virtual registers as needed for the mapping of the \p
339 /// OpIdx-th operand.
340 /// The number of registers is determined by the number of breakdown for the
341 /// related operand in the instruction mapping.
342 /// The type of the new registers is a plain scalar of the right size.
343 /// The proper type is expected to be set when the mapping is applied to
344 /// the instruction(s) that realizes the mapping.
345 ///
346 /// \pre getMI().getOperand(OpIdx).isReg()
347 ///
348 /// \post All the partial mapping of the \p OpIdx-th operand have been
349 /// assigned a new virtual register.
350 void createVRegs(unsigned OpIdx);
351
352 /// Set the virtual register of the \p PartialMapIdx-th partial mapping of
353 /// the OpIdx-th operand to \p NewVReg.
354 ///
355 /// \pre getMI().getOperand(OpIdx).isReg()
356 /// \pre getInstrMapping().getOperandMapping(OpIdx).BreakDown.size() >
357 /// PartialMapIdx
358 /// \pre NewReg != 0
359 ///
360 /// \post the \p PartialMapIdx-th register of the value mapping of the \p
361 /// OpIdx-th operand has been set.
362 void setVRegs(unsigned OpIdx, unsigned PartialMapIdx, Register NewVReg);
363
364 /// Get all the virtual registers required to map the \p OpIdx-th operand of
365 /// the instruction.
366 ///
367 /// This return an empty range when createVRegs or setVRegs has not been
368 /// called.
369 /// The iterator may be invalidated by a call to setVRegs or createVRegs.
370 ///
371 /// When \p ForDebug is true, we will not check that the list of new virtual
372 /// registers does not contain uninitialized values.
373 ///
374 /// \pre getMI().getOperand(OpIdx).isReg()
375 /// \pre ForDebug || All partial mappings have been set a register
377 getVRegs(unsigned OpIdx, bool ForDebug = false) const;
378
379 /// Print this operands mapper on dbgs() stream.
380 void dump() const;
381
382 /// Print this operands mapper on \p OS stream.
383 void print(raw_ostream &OS, bool ForDebug = false) const;
384 };
385
386protected:
387 /// Hold the set of supported register banks.
389
390 /// Total number of register banks.
391 unsigned NumRegBanks;
392
393 /// Hold the sizes of the register banks for all HwModes.
394 const unsigned *Sizes;
395
396 /// Current HwMode for the target.
397 unsigned HwMode;
398
399 /// Keep dynamically allocated PartialMapping in a separate map.
400 /// This shouldn't be needed when everything gets TableGen'ed.
403
404 /// Keep dynamically allocated ValueMapping in a separate map.
405 /// This shouldn't be needed when everything gets TableGen'ed.
408
409 /// Keep dynamically allocated array of ValueMapping in a separate map.
410 /// This shouldn't be needed when everything gets TableGen'ed.
413
414 /// Keep dynamically allocated InstructionMapping in a separate map.
415 /// This shouldn't be needed when everything gets TableGen'ed.
418
419 /// Getting the minimal register class of a physreg is expensive.
420 /// Cache this information as we get it.
422
423 /// Create a RegisterBankInfo that can accommodate up to \p NumRegBanks
424 /// RegisterBank instances.
426 const unsigned *Sizes, unsigned HwMode);
427
428 /// This constructor is meaningless.
429 /// It just provides a default constructor that can be used at link time
430 /// when GlobalISel is not built.
431 /// That way, targets can still inherit from this class without doing
432 /// crazy gymnastic to avoid link time failures.
433 /// \note That works because the constructor is inlined.
435 llvm_unreachable("This constructor should not be executed");
436 }
437
438 /// Get the register bank identified by \p ID.
439 const RegisterBank &getRegBank(unsigned ID) {
440 assert(ID < getNumRegBanks() && "Accessing an unknown register bank");
441 return *RegBanks[ID];
442 }
443
444 /// Get the MinimalPhysRegClass for Reg.
445 /// \pre Reg is a physical register.
446 const TargetRegisterClass *
448
449 /// Try to get the mapping of \p MI.
450 /// See getInstrMapping for more details on what a mapping represents.
451 ///
452 /// Unlike getInstrMapping the returned InstructionMapping may be invalid
453 /// (isValid() == false).
454 /// This means that the target independent code is not smart enough
455 /// to get the mapping of \p MI and thus, the target has to provide the
456 /// information for \p MI.
457 ///
458 /// This implementation is able to get the mapping of:
459 /// - Target specific instructions by looking at the encoding constraints.
460 /// - Any instruction if all the register operands have already been assigned
461 /// a register, a register class, or a register bank.
462 /// - Copies and phis if at least one of the operands has been assigned a
463 /// register, a register class, or a register bank.
464 /// In other words, this method will likely fail to find a mapping for
465 /// any generic opcode that has not been lowered by target specific code.
466 const InstructionMapping &getInstrMappingImpl(const MachineInstr &MI) const;
467
468 /// Get the uniquely generated PartialMapping for the
469 /// given arguments.
470 const PartialMapping &getPartialMapping(unsigned StartIdx, unsigned Length,
471 const RegisterBank &RegBank) const;
472
473 /// \name Methods to get a uniquely generated ValueMapping.
474 /// @{
475
476 /// The most common ValueMapping consists of a single PartialMapping.
477 /// Feature a method for that.
478 const ValueMapping &getValueMapping(unsigned StartIdx, unsigned Length,
479 const RegisterBank &RegBank) const;
480
481 /// Get the ValueMapping for the given arguments.
482 const ValueMapping &getValueMapping(const PartialMapping *BreakDown,
483 unsigned NumBreakDowns) const;
484 /// @}
485
486 /// \name Methods to get a uniquely generated array of ValueMapping.
487 /// @{
488
489 /// Get the uniquely generated array of ValueMapping for the
490 /// elements of between \p Begin and \p End.
491 ///
492 /// Elements that are nullptr will be replaced by
493 /// invalid ValueMapping (ValueMapping::isValid == false).
494 ///
495 /// \pre The pointers on ValueMapping between \p Begin and \p End
496 /// must uniquely identify a ValueMapping. Otherwise, there is no
497 /// guarantee that the return instance will be unique, i.e., another
498 /// OperandsMapping could have the same content.
499 template <typename Iterator>
500 const ValueMapping *getOperandsMapping(Iterator Begin, Iterator End) const;
501
502 /// Get the uniquely generated array of ValueMapping for the
503 /// elements of \p OpdsMapping.
504 ///
505 /// Elements of \p OpdsMapping that are nullptr will be replaced by
506 /// invalid ValueMapping (ValueMapping::isValid == false).
507 const ValueMapping *getOperandsMapping(
508 const SmallVectorImpl<const ValueMapping *> &OpdsMapping) const;
509
510 /// Get the uniquely generated array of ValueMapping for the
511 /// given arguments.
512 ///
513 /// Arguments that are nullptr will be replaced by invalid
514 /// ValueMapping (ValueMapping::isValid == false).
516 std::initializer_list<const ValueMapping *> OpdsMapping) const;
517 /// @}
518
519 /// \name Methods to get a uniquely generated InstructionMapping.
520 /// @{
521
522private:
523 /// Method to get a uniquely generated InstructionMapping.
524 const InstructionMapping &
525 getInstructionMappingImpl(bool IsInvalid, unsigned ID = InvalidMappingID,
526 unsigned Cost = 0,
527 const ValueMapping *OperandsMapping = nullptr,
528 unsigned NumOperands = 0) const;
529
530public:
531 /// Method to get a uniquely generated InstructionMapping.
532 const InstructionMapping &
533 getInstructionMapping(unsigned ID, unsigned Cost,
534 const ValueMapping *OperandsMapping,
535 unsigned NumOperands) const {
536 return getInstructionMappingImpl(/*IsInvalid*/ false, ID, Cost,
537 OperandsMapping, NumOperands);
538 }
539
540 /// Method to get a uniquely generated invalid InstructionMapping.
542 return getInstructionMappingImpl(/*IsInvalid*/ true);
543 }
544 /// @}
545
546 /// Get the register bank for the \p OpIdx-th operand of \p MI form
547 /// the encoding constraints, if any.
548 ///
549 /// \return A register bank that covers the register class of the
550 /// related encoding constraints or nullptr if \p MI did not provide
551 /// enough information to deduce it.
552 const RegisterBank *
553 getRegBankFromConstraints(const MachineInstr &MI, unsigned OpIdx,
554 const TargetInstrInfo &TII,
555 const MachineRegisterInfo &MRI) const;
556
557 /// Helper method to apply something that is like the default mapping.
558 /// Basically, that means that \p OpdMapper.getMI() is left untouched
559 /// aside from the reassignment of the register operand that have been
560 /// remapped.
561 ///
562 /// The type of all the new registers that have been created by the
563 /// mapper are properly remapped to the type of the original registers
564 /// they replace. In other words, the semantic of the instruction does
565 /// not change, only the register banks.
566 ///
567 /// If the mapping of one of the operand spans several registers, this
568 /// method will abort as this is not like a default mapping anymore.
569 ///
570 /// \pre For OpIdx in {0..\p OpdMapper.getMI().getNumOperands())
571 /// the range OpdMapper.getVRegs(OpIdx) is empty or of size 1.
572 static void applyDefaultMapping(const OperandsMapper &OpdMapper);
573
574 /// See ::applyMapping.
575 virtual void applyMappingImpl(MachineIRBuilder &Builder,
576 const OperandsMapper &OpdMapper) const {
577 llvm_unreachable("The target has to implement this");
578 }
579
580public:
581 virtual ~RegisterBankInfo() = default;
582
583 /// Get the register bank identified by \p ID.
584 const RegisterBank &getRegBank(unsigned ID) const {
585 return const_cast<RegisterBankInfo *>(this)->getRegBank(ID);
586 }
587
588 /// Get the maximum size in bits that fits in the given register bank.
589 unsigned getMaximumSize(unsigned RegBankID) const {
590 return Sizes[RegBankID + HwMode * NumRegBanks];
591 }
592
593 /// Get the register bank of \p Reg.
594 /// If Reg has not been assigned a register, a register class,
595 /// or a register bank, then this returns nullptr.
596 ///
597 /// \pre Reg != 0 (NoRegister)
599 const TargetRegisterInfo &TRI) const;
600
601 /// Get the total number of register banks.
602 unsigned getNumRegBanks() const { return NumRegBanks; }
603
604 /// Returns true if the register bank is considered divergent.
605 virtual bool isDivergentRegBank(const RegisterBank *RB) const {
606 return false;
607 }
608
609 /// Get a register bank that covers \p RC.
610 ///
611 /// \pre \p RC is a user-defined register class (as opposed as one
612 /// generated by TableGen).
613 ///
614 /// \note The mapping RC -> RegBank could be built while adding the
615 /// coverage for the register banks. However, we do not do it, because,
616 /// at least for now, we only need this information for register classes
617 /// that are used in the description of instruction. In other words,
618 /// there are just a handful of them and we do not want to waste space.
619 ///
620 /// \todo This should be TableGen'ed.
621 virtual const RegisterBank &
623 llvm_unreachable("The target must override this method");
624 }
625
626 /// Get the cost of a copy from \p B to \p A, or put differently,
627 /// get the cost of A = COPY B. Since register banks may cover
628 /// different size, \p Size specifies what will be the size in bits
629 /// that will be copied around.
630 ///
631 /// \note Since this is a copy, both registers have the same size.
632 virtual unsigned copyCost(const RegisterBank &A, const RegisterBank &B,
633 unsigned Size) const {
634 // Optimistically assume that copies are coalesced. I.e., when
635 // they are on the same bank, they are free.
636 // Otherwise assume a non-zero cost of 1. The targets are supposed
637 // to override that properly anyway if they care.
638 return &A != &B;
639 }
640
641 /// \returns true if emitting a copy from \p Src to \p Dst is impossible.
642 bool cannotCopy(const RegisterBank &Dst, const RegisterBank &Src,
643 unsigned Size) const {
644 return copyCost(Dst, Src, Size) == std::numeric_limits<unsigned>::max();
645 }
646
647 /// Get the cost of using \p ValMapping to decompose a register. This is
648 /// similar to ::copyCost, except for cases where multiple copy-like
649 /// operations need to be inserted. If the register is used as a source
650 /// operand and already has a bank assigned, \p CurBank is non-null.
651 virtual unsigned
653 const RegisterBank *CurBank = nullptr) const {
654 return std::numeric_limits<unsigned>::max();
655 }
656
657 /// Constrain the (possibly generic) virtual register \p Reg to \p RC.
658 ///
659 /// \pre \p Reg is a virtual register that either has a bank or a class.
660 /// \returns The constrained register class, or nullptr if there is none.
661 /// \note This is a generic variant of MachineRegisterInfo::constrainRegClass
662 /// \note Use MachineRegisterInfo::constrainRegAttrs instead for any non-isel
663 /// purpose, including non-select passes of GlobalISel
664 static const TargetRegisterClass *
667
668 /// Identifier used when the related instruction mapping instance
669 /// is generated by target independent code.
670 /// Make sure not to use that identifier to avoid possible collision.
671 static const unsigned DefaultMappingID;
672
673 /// Identifier used when the related instruction mapping instance
674 /// is generated by the default constructor.
675 /// Make sure not to use that identifier.
676 static const unsigned InvalidMappingID;
677
678 /// Get the mapping of the different operands of \p MI
679 /// on the register bank.
680 /// This mapping should be the direct translation of \p MI.
681 /// In other words, when \p MI is mapped with the returned mapping,
682 /// only the register banks of the operands of \p MI need to be updated.
683 /// In particular, neither the opcode nor the type of \p MI needs to be
684 /// updated for this direct mapping.
685 ///
686 /// The target independent implementation gives a mapping based on
687 /// the register classes for the target specific opcode.
688 /// It uses the ID RegisterBankInfo::DefaultMappingID for that mapping.
689 /// Make sure you do not use that ID for the alternative mapping
690 /// for MI. See getInstrAlternativeMappings for the alternative
691 /// mappings.
692 ///
693 /// For instance, if \p MI is a vector add, the mapping should
694 /// not be a scalarization of the add.
695 ///
696 /// \post returnedVal.verify(MI).
697 ///
698 /// \note If returnedVal does not verify MI, this would probably mean
699 /// that the target does not support that instruction.
700 virtual const InstructionMapping &
701 getInstrMapping(const MachineInstr &MI) const;
702
703 /// Get the alternative mappings for \p MI.
704 /// Alternative in the sense different from getInstrMapping.
705 virtual InstructionMappings
707
708 /// Get the possible mapping for \p MI.
709 /// A mapping defines where the different operands may live and at what cost.
710 /// For instance, let us consider:
711 /// v0(16) = G_ADD <2 x i8> v1, v2
712 /// The possible mapping could be:
713 ///
714 /// {/*ID*/VectorAdd, /*Cost*/1, /*v0*/{(0xFFFF, VPR)}, /*v1*/{(0xFFFF, VPR)},
715 /// /*v2*/{(0xFFFF, VPR)}}
716 /// {/*ID*/ScalarAddx2, /*Cost*/2, /*v0*/{(0x00FF, GPR),(0xFF00, GPR)},
717 /// /*v1*/{(0x00FF, GPR),(0xFF00, GPR)},
718 /// /*v2*/{(0x00FF, GPR),(0xFF00, GPR)}}
719 ///
720 /// \note The first alternative of the returned mapping should be the
721 /// direct translation of \p MI current form.
722 ///
723 /// \post !returnedVal.empty().
725
726 /// Apply \p OpdMapper.getInstrMapping() to \p OpdMapper.getMI().
727 /// After this call \p OpdMapper.getMI() may not be valid anymore.
728 /// \p OpdMapper.getInstrMapping().getID() carries the information of
729 /// what has been chosen to map \p OpdMapper.getMI(). This ID is set
730 /// by the various getInstrXXXMapping method.
731 ///
732 /// Therefore, getting the mapping and applying it should be kept in
733 /// sync.
735 const OperandsMapper &OpdMapper) const {
736 // The only mapping we know how to handle is the default mapping.
737 if (OpdMapper.getInstrMapping().getID() == DefaultMappingID)
738 return applyDefaultMapping(OpdMapper);
739 // For other mapping, the target needs to do the right thing.
740 // If that means calling applyDefaultMapping, fine, but this
741 // must be explicitly stated.
742 applyMappingImpl(Builder, OpdMapper);
743 }
744
745 /// Get the size in bits of \p Reg.
746 /// Utility method to get the size of any registers. Unlike
747 /// MachineRegisterInfo::getSize, the register does not need to be a
748 /// virtual register.
749 ///
750 /// \pre \p Reg != 0 (NoRegister).
752 const TargetRegisterInfo &TRI) const;
753
754 /// Check that information hold by this instance make sense for the
755 /// given \p TRI.
756 ///
757 /// \note This method does not check anything when assertions are disabled.
758 ///
759 /// \return True is the check was successful.
760 bool verify(const TargetRegisterInfo &TRI) const;
761};
762
765 const RegisterBankInfo::PartialMapping &PartMapping) {
766 PartMapping.print(OS);
767 return OS;
768}
769
772 ValMapping.print(OS);
773 return OS;
774}
775
778 const RegisterBankInfo::InstructionMapping &InstrMapping) {
779 InstrMapping.print(OS);
780 return OS;
781}
782
785 OpdMapper.print(OS, /*ForDebug*/ false);
786 return OS;
787}
788
789/// Hashing function for PartialMapping.
790/// It is required for the hashing of ValueMapping.
791hash_code hash_value(const RegisterBankInfo::PartialMapping &PartMapping);
792
793} // end namespace llvm
794
795#endif // LLVM_CODEGEN_REGISTERBANKINFO_H
unsigned const MachineRegisterInfo * MRI
assume Assume Builder
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file defines the DenseMap class.
uint64_t Size
bool End
Definition: ELF_riscv.cpp:469
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Implement a low-level type suitable for MachineInstr level instruction selection.
unsigned const TargetRegisterInfo * TRI
unsigned Reg
ppc ctr loops verify
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the SmallVector class.
Helper class to build MachineInstr.
Representation of each machine instruction.
Definition: MachineInstr.h:68
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Helper class that represents how the value of an instruction may be mapped and what is the related co...
unsigned getNumOperands() const
Get the number of operands.
unsigned getCost() const
Get the cost.
InstructionMapping()=default
Default constructor.
void setOperandsMapping(const ValueMapping *OpdsMapping)
Set the mapping for all the operands.
void dump() const
Print this on dbgs() stream.
void print(raw_ostream &OS) const
Print this on OS;.
InstructionMapping(unsigned ID, unsigned Cost, const ValueMapping *OperandsMapping, unsigned NumOperands)
Constructor for the mapping of an instruction.
const ValueMapping & getOperandMapping(unsigned i) const
Get the value mapping of the ith operand.
bool isValid() const
Check whether this object is valid.
Helper class used to get/create the virtual registers that will be used to replace the MachineOperand...
const InstructionMapping & getInstrMapping() const
The final mapping of the instruction.
void setVRegs(unsigned OpIdx, unsigned PartialMapIdx, Register NewVReg)
Set the virtual register of the PartialMapIdx-th partial mapping of the OpIdx-th operand to NewVReg.
void print(raw_ostream &OS, bool ForDebug=false) const
Print this operands mapper on OS stream.
void createVRegs(unsigned OpIdx)
Create as many new virtual registers as needed for the mapping of the OpIdx-th operand.
MachineRegisterInfo & getMRI() const
The MachineRegisterInfo we used to realize the mapping.
void dump() const
Print this operands mapper on dbgs() stream.
iterator_range< SmallVectorImpl< Register >::const_iterator > getVRegs(unsigned OpIdx, bool ForDebug=false) const
Get all the virtual registers required to map the OpIdx-th operand of the instruction.
Holds all the information related to register banks.
virtual bool isDivergentRegBank(const RegisterBank *RB) const
Returns true if the register bank is considered divergent.
unsigned getSizeInBits(Register Reg, const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI) const
Get the size in bits of Reg.
const PartialMapping & getPartialMapping(unsigned StartIdx, unsigned Length, const RegisterBank &RegBank) const
Get the uniquely generated PartialMapping for the given arguments.
void applyMapping(MachineIRBuilder &Builder, const OperandsMapper &OpdMapper) const
Apply OpdMapper.getInstrMapping() to OpdMapper.getMI().
virtual InstructionMappings getInstrAlternativeMappings(const MachineInstr &MI) const
Get the alternative mappings for MI.
virtual ~RegisterBankInfo()=default
static const TargetRegisterClass * constrainGenericRegister(Register Reg, const TargetRegisterClass &RC, MachineRegisterInfo &MRI)
Constrain the (possibly generic) virtual register Reg to RC.
const RegisterBank ** RegBanks
Hold the set of supported register banks.
RegisterBankInfo()
This constructor is meaningless.
virtual const InstructionMapping & getInstrMapping(const MachineInstr &MI) const
Get the mapping of the different operands of MI on the register bank.
virtual void applyMappingImpl(MachineIRBuilder &Builder, const OperandsMapper &OpdMapper) const
See applyMapping.
unsigned NumRegBanks
Total number of register banks.
const InstructionMapping & getInstructionMapping(unsigned ID, unsigned Cost, const ValueMapping *OperandsMapping, unsigned NumOperands) const
Method to get a uniquely generated InstructionMapping.
static void applyDefaultMapping(const OperandsMapper &OpdMapper)
Helper method to apply something that is like the default mapping.
const TargetRegisterClass * getMinimalPhysRegClass(Register Reg, const TargetRegisterInfo &TRI) const
Get the MinimalPhysRegClass for Reg.
const ValueMapping & getValueMapping(unsigned StartIdx, unsigned Length, const RegisterBank &RegBank) const
The most common ValueMapping consists of a single PartialMapping.
const InstructionMapping & getInvalidInstructionMapping() const
Method to get a uniquely generated invalid InstructionMapping.
virtual unsigned copyCost(const RegisterBank &A, const RegisterBank &B, unsigned Size) const
Get the cost of a copy from B to A, or put differently, get the cost of A = COPY B.
const RegisterBank & getRegBank(unsigned ID)
Get the register bank identified by ID.
DenseMap< unsigned, std::unique_ptr< ValueMapping[]> > MapOfOperandsMappings
Keep dynamically allocated array of ValueMapping in a separate map.
DenseMap< unsigned, std::unique_ptr< const ValueMapping > > MapOfValueMappings
Keep dynamically allocated ValueMapping in a separate map.
const unsigned * Sizes
Hold the sizes of the register banks for all HwModes.
unsigned getMaximumSize(unsigned RegBankID) const
Get the maximum size in bits that fits in the given register bank.
DenseMap< unsigned, std::unique_ptr< const InstructionMapping > > MapOfInstructionMappings
Keep dynamically allocated InstructionMapping in a separate map.
unsigned getNumRegBanks() const
Get the total number of register banks.
const RegisterBank * getRegBankFromConstraints(const MachineInstr &MI, unsigned OpIdx, const TargetInstrInfo &TII, const MachineRegisterInfo &MRI) const
Get the register bank for the OpIdx-th operand of MI form the encoding constraints,...
virtual const RegisterBank & getRegBankFromRegClass(const TargetRegisterClass &RC, LLT Ty) const
Get a register bank that covers RC.
InstructionMappings getInstrPossibleMappings(const MachineInstr &MI) const
Get the possible mapping for MI.
DenseMap< unsigned, std::unique_ptr< const PartialMapping > > MapOfPartialMappings
Keep dynamically allocated PartialMapping in a separate map.
DenseMap< unsigned, const TargetRegisterClass * > PhysRegMinimalRCs
Getting the minimal register class of a physreg is expensive.
const RegisterBank & getRegBank(unsigned ID) const
Get the register bank identified by ID.
static const unsigned InvalidMappingID
Identifier used when the related instruction mapping instance is generated by the default constructor...
const ValueMapping * getOperandsMapping(Iterator Begin, Iterator End) const
Get the uniquely generated array of ValueMapping for the elements of between Begin and End.
bool cannotCopy(const RegisterBank &Dst, const RegisterBank &Src, unsigned Size) const
static const unsigned DefaultMappingID
Identifier used when the related instruction mapping instance is generated by target independent code...
unsigned HwMode
Current HwMode for the target.
const ValueMapping * getOperandsMapping(std::initializer_list< const ValueMapping * > OpdsMapping) const
Get the uniquely generated array of ValueMapping for the given arguments.
virtual unsigned getBreakDownCost(const ValueMapping &ValMapping, const RegisterBank *CurBank=nullptr) const
Get the cost of using ValMapping to decompose a register.
const InstructionMapping & getInstrMappingImpl(const MachineInstr &MI) const
Try to get the mapping of MI.
This class implements the register bank concept.
Definition: RegisterBank.h:28
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:582
typename SuperClass::iterator iterator
Definition: SmallVector.h:581
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Length
Definition: DWP.cpp:440
hash_code hash_value(const FixedPointSemantics &Val)
Definition: APFixedPoint.h:128
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:292
Helper struct that represents how a value is partially mapped into a register.
void print(raw_ostream &OS) const
Print this partial mapping on OS;.
unsigned StartIdx
Number of bits at which this partial mapping starts in the original value.
PartialMapping(unsigned StartIdx, unsigned Length, const RegisterBank &RegBank)
Provide a shortcut for quickly building PartialMapping.
void dump() const
Print this partial mapping on dbgs() stream.
const RegisterBank * RegBank
Register bank where the partial value lives.
unsigned Length
Length of this mapping in bits.
Helper struct that represents how a value is mapped through different register banks.
const PartialMapping * begin() const
Iterators through the PartialMappings.
const PartialMapping * end() const
void print(raw_ostream &OS) const
Print this on OS;.
bool isValid() const
Check if this ValueMapping is valid.
unsigned NumBreakDowns
Number of partial mapping to break down this value.
ValueMapping()
The default constructor creates an invalid (isValid() == false) instance.
void dump() const
Print this on dbgs() stream.
ValueMapping(const PartialMapping *BreakDown, unsigned NumBreakDowns)
Initialize a ValueMapping with the given parameter.
const PartialMapping * BreakDown
How the value is broken down between the different register banks.