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