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;
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 constexpr 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 LLVM_ABI void dump() const;
76
77 /// Print this partial mapping on \p OS;
78 LLVM_ABI 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 LLVM_ABI 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.
163
164 /// Iterators through the PartialMappings.
165 const PartialMapping *begin() const { return BreakDown; }
166 const PartialMapping *end() const { return BreakDown + NumBreakDowns; }
167
168 /// \return true if all partial mappings are the same size and register
169 /// bank.
170 LLVM_ABI bool partsAllUniform() const;
171
172 /// Check if this ValueMapping is valid.
173 bool isValid() const { return BreakDown && NumBreakDowns; }
174
175 /// Verify that this mapping makes sense for a value of
176 /// \p MeaningfulBitWidth.
177 /// \note This method does not check anything when assertions are disabled.
178 ///
179 /// \return True is the check was successful.
180 LLVM_ABI bool verify(const RegisterBankInfo &RBI,
181 TypeSize MeaningfulBitWidth) const;
182
183 /// Print this on dbgs() stream.
184 LLVM_ABI void dump() const;
185
186 /// Print this on \p OS;
187 LLVM_ABI void print(raw_ostream &OS) const;
188 };
189
190 /// Helper class that represents how the value of an instruction may be
191 /// mapped and what is the related cost of such mapping.
193 /// Identifier of the mapping.
194 /// This is used to communicate between the target and the optimizers
195 /// which mapping should be realized.
196 unsigned ID = InvalidMappingID;
197
198 /// Cost of this mapping.
199 unsigned Cost = 0;
200
201 /// Mapping of all the operands.
202 const ValueMapping *OperandsMapping = nullptr;
203
204 /// Number of operands.
205 unsigned NumOperands = 0;
206
207 const ValueMapping &getOperandMapping(unsigned i) {
208 assert(i < getNumOperands() && "Out of bound operand");
209 return OperandsMapping[i];
210 }
211
212 public:
213 /// Constructor for the mapping of an instruction.
214 /// \p NumOperands must be equal to number of all the operands of
215 /// the related instruction.
216 /// The rationale is that it is more efficient for the optimizers
217 /// to be able to assume that the mapping of the ith operand is
218 /// at the index i.
219 InstructionMapping(unsigned ID, unsigned Cost,
220 const ValueMapping *OperandsMapping,
221 unsigned NumOperands)
222 : ID(ID), Cost(Cost), OperandsMapping(OperandsMapping),
223 NumOperands(NumOperands) {}
224
225 /// Default constructor.
226 /// Use this constructor to express that the mapping is invalid.
228
229 /// Get the cost.
230 unsigned getCost() const { return Cost; }
231
232 /// Get the ID.
233 unsigned getID() const { return ID; }
234
235 /// Get the number of operands.
236 unsigned getNumOperands() const { return NumOperands; }
237
238 /// Get the value mapping of the ith operand.
239 /// \pre The mapping for the ith operand has been set.
240 /// \pre The ith operand is a register.
241 const ValueMapping &getOperandMapping(unsigned i) const {
242 const ValueMapping &ValMapping =
243 const_cast<InstructionMapping *>(this)->getOperandMapping(i);
244 return ValMapping;
245 }
246
247 /// Set the mapping for all the operands.
248 /// In other words, OpdsMapping should hold at least getNumOperands
249 /// ValueMapping.
250 void setOperandsMapping(const ValueMapping *OpdsMapping) {
251 OperandsMapping = OpdsMapping;
252 }
253
254 /// Check whether this object is valid.
255 /// This is a lightweight check for obvious wrong instance.
256 bool isValid() const {
257 return getID() != InvalidMappingID && OperandsMapping;
258 }
259
260 /// Verifiy that this mapping makes sense for \p MI.
261 /// \pre \p MI must be connected to a MachineFunction.
262 ///
263 /// \note This method does not check anything when assertions are disabled.
264 ///
265 /// \return True is the check was successful.
266 LLVM_ABI bool verify(const MachineInstr &MI) const;
267
268 /// Print this on dbgs() stream.
269 LLVM_ABI void dump() const;
270
271 /// Print this on \p OS;
272 LLVM_ABI void print(raw_ostream &OS) const;
273 };
274
275 /// Convenient type to represent the alternatives for mapping an
276 /// instruction.
277 /// \todo When we move to TableGen this should be an array ref.
279
280 /// Helper class used to get/create the virtual registers that will be used
281 /// to replace the MachineOperand when applying a mapping.
283 /// The OpIdx-th cell contains the index in NewVRegs where the VRegs of the
284 /// OpIdx-th operand starts. -1 means we do not have such mapping yet.
285 /// Note: We use a SmallVector to avoid heap allocation for most cases.
286 SmallVector<int, 8> OpToNewVRegIdx;
287
288 /// Hold the registers that will be used to map MI with InstrMapping.
290
291 /// Current MachineRegisterInfo, used to create new virtual registers.
293
294 /// Instruction being remapped.
295 MachineInstr &MI;
296
297 /// New mapping of the instruction.
298 const InstructionMapping &InstrMapping;
299
300 /// Constant value identifying that the index in OpToNewVRegIdx
301 /// for an operand has not been set yet.
302 static const int DontKnowIdx;
303
304 /// Get the range in NewVRegs to store all the partial
305 /// values for the \p OpIdx-th operand.
306 ///
307 /// \return The iterator range for the space created.
308 //
309 /// \pre getMI().getOperand(OpIdx).isReg()
311 getVRegsMem(unsigned OpIdx);
312
313 /// Get the end iterator for a range starting at \p StartIdx and
314 /// spannig \p NumVal in NewVRegs.
315 /// \pre StartIdx + NumVal <= NewVRegs.size()
317 getNewVRegsEnd(unsigned StartIdx, unsigned NumVal) const;
318 SmallVectorImpl<Register>::iterator getNewVRegsEnd(unsigned StartIdx,
319 unsigned NumVal);
320
321 public:
322 /// Create an OperandsMapper that will hold the information to apply \p
323 /// InstrMapping to \p MI.
324 /// \pre InstrMapping.verify(MI)
326 const InstructionMapping &InstrMapping,
328
329 /// \name Getters.
330 /// @{
331 /// The MachineInstr being remapped.
332 MachineInstr &getMI() const { return MI; }
333
334 /// The final mapping of the instruction.
335 const InstructionMapping &getInstrMapping() const { return InstrMapping; }
336
337 /// The MachineRegisterInfo we used to realize the mapping.
338 MachineRegisterInfo &getMRI() const { return MRI; }
339 /// @}
340
341 /// Create as many new virtual registers as needed for the mapping of the \p
342 /// OpIdx-th operand.
343 /// The number of registers is determined by the number of breakdown for the
344 /// related operand in the instruction mapping.
345 /// The type of the new registers is a plain scalar of the right size.
346 /// The proper type is expected to be set when the mapping is applied to
347 /// the instruction(s) that realizes the mapping.
348 ///
349 /// \pre getMI().getOperand(OpIdx).isReg()
350 ///
351 /// \post All the partial mapping of the \p OpIdx-th operand have been
352 /// assigned a new virtual register.
353 LLVM_ABI void createVRegs(unsigned OpIdx);
354
355 /// Set the virtual register of the \p PartialMapIdx-th partial mapping of
356 /// the OpIdx-th operand to \p NewVReg.
357 ///
358 /// \pre getMI().getOperand(OpIdx).isReg()
359 /// \pre getInstrMapping().getOperandMapping(OpIdx).BreakDown.size() >
360 /// PartialMapIdx
361 /// \pre NewReg != 0
362 ///
363 /// \post the \p PartialMapIdx-th register of the value mapping of the \p
364 /// OpIdx-th operand has been set.
365 LLVM_ABI void setVRegs(unsigned OpIdx, unsigned PartialMapIdx,
366 Register NewVReg);
367
368 /// Get all the virtual registers required to map the \p OpIdx-th operand of
369 /// the instruction.
370 ///
371 /// This return an empty range when createVRegs or setVRegs has not been
372 /// called.
373 /// The iterator may be invalidated by a call to setVRegs or createVRegs.
374 ///
375 /// When \p ForDebug is true, we will not check that the list of new virtual
376 /// registers does not contain uninitialized values.
377 ///
378 /// \pre getMI().getOperand(OpIdx).isReg()
379 /// \pre ForDebug || All partial mappings have been set a register
381 getVRegs(unsigned OpIdx, bool ForDebug = false) const;
382
383 /// Print this operands mapper on dbgs() stream.
384 LLVM_ABI void dump() const;
385
386 /// Print this operands mapper on \p OS stream.
387 LLVM_ABI void print(raw_ostream &OS, bool ForDebug = false) const;
388 };
389
390protected:
391 /// Hold the set of supported register banks.
393
394 /// Total number of register banks.
395 unsigned NumRegBanks;
396
397 /// Hold the sizes of the register banks for all HwModes.
398 const unsigned *Sizes;
399
400 /// Current HwMode for the target.
401 unsigned HwMode;
402
403 /// Keep dynamically allocated PartialMapping in a separate map.
404 /// This shouldn't be needed when everything gets TableGen'ed.
407
408 /// Keep dynamically allocated ValueMapping in a separate map.
409 /// This shouldn't be needed when everything gets TableGen'ed.
412
413 /// Keep dynamically allocated array of ValueMapping in a separate map.
414 /// This shouldn't be needed when everything gets TableGen'ed.
417
418 /// Keep dynamically allocated InstructionMapping in a separate map.
419 /// This shouldn't be needed when everything gets TableGen'ed.
422
423 /// Getting the minimal register class of a physreg is expensive.
424 /// Cache this information as we get it.
426
427 /// Create a RegisterBankInfo that can accommodate up to \p NumRegBanks
428 /// RegisterBank instances.
430 const unsigned *Sizes, unsigned HwMode);
431
432 /// This constructor is meaningless.
433 /// It just provides a default constructor that can be used at link time
434 /// when GlobalISel is not built.
435 /// That way, targets can still inherit from this class without doing
436 /// crazy gymnastic to avoid link time failures.
437 /// \note That works because the constructor is inlined.
439 llvm_unreachable("This constructor should not be executed");
440 }
441
444
445 /// Get the register bank identified by \p ID.
446 const RegisterBank &getRegBank(unsigned ID) {
447 assert(ID < getNumRegBanks() && "Accessing an unknown register bank");
448 return *RegBanks[ID];
449 }
450
451 /// Get the MinimalPhysRegClass for Reg.
452 /// \pre Reg is a physical register.
453 const TargetRegisterClass *
455
456 /// Try to get the mapping of \p MI.
457 /// See getInstrMapping for more details on what a mapping represents.
458 ///
459 /// Unlike getInstrMapping the returned InstructionMapping may be invalid
460 /// (isValid() == false).
461 /// This means that the target independent code is not smart enough
462 /// to get the mapping of \p MI and thus, the target has to provide the
463 /// information for \p MI.
464 ///
465 /// This implementation is able to get the mapping of:
466 /// - Target specific instructions by looking at the encoding constraints.
467 /// - Any instruction if all the register operands have already been assigned
468 /// a register, a register class, or a register bank.
469 /// - Copies and phis if at least one of the operands has been assigned a
470 /// register, a register class, or a register bank.
471 /// In other words, this method will likely fail to find a mapping for
472 /// any generic opcode that has not been lowered by target specific code.
473 const InstructionMapping &getInstrMappingImpl(const MachineInstr &MI) const;
474
475 /// Get the uniquely generated PartialMapping for the
476 /// given arguments.
477 const PartialMapping &getPartialMapping(unsigned StartIdx, unsigned Length,
478 const RegisterBank &RegBank) const;
479
480 /// \name Methods to get a uniquely generated ValueMapping.
481 /// @{
482
483 /// The most common ValueMapping consists of a single PartialMapping.
484 /// Feature a method for that.
485 const ValueMapping &getValueMapping(unsigned StartIdx, unsigned Length,
486 const RegisterBank &RegBank) const;
487
488 /// Get the ValueMapping for the given arguments.
489 const ValueMapping &getValueMapping(const PartialMapping *BreakDown,
490 unsigned NumBreakDowns) const;
491 /// @}
492
493 /// \name Methods to get a uniquely generated array of ValueMapping.
494 /// @{
495
496 /// Get the uniquely generated array of ValueMapping for the
497 /// elements of between \p Begin and \p End.
498 ///
499 /// Elements that are nullptr will be replaced by
500 /// invalid ValueMapping (ValueMapping::isValid == false).
501 ///
502 /// \pre The pointers on ValueMapping between \p Begin and \p End
503 /// must uniquely identify a ValueMapping. Otherwise, there is no
504 /// guarantee that the return instance will be unique, i.e., another
505 /// OperandsMapping could have the same content.
506 template <typename Iterator>
507 const ValueMapping *getOperandsMapping(Iterator Begin, Iterator End) const;
508
509 /// Get the uniquely generated array of ValueMapping for the
510 /// elements of \p OpdsMapping.
511 ///
512 /// Elements of \p OpdsMapping that are nullptr will be replaced by
513 /// invalid ValueMapping (ValueMapping::isValid == false).
514 const ValueMapping *getOperandsMapping(
515 const SmallVectorImpl<const ValueMapping *> &OpdsMapping) const;
516
517 /// Get the uniquely generated array of ValueMapping for the
518 /// given arguments.
519 ///
520 /// Arguments that are nullptr will be replaced by invalid
521 /// ValueMapping (ValueMapping::isValid == false).
523 std::initializer_list<const ValueMapping *> OpdsMapping) const;
524 /// @}
525
526 /// \name Methods to get a uniquely generated InstructionMapping.
527 /// @{
528
529private:
530 /// Method to get a uniquely generated InstructionMapping.
531 const InstructionMapping &
532 getInstructionMappingImpl(bool IsInvalid, unsigned ID = InvalidMappingID,
533 unsigned Cost = 0,
534 const ValueMapping *OperandsMapping = nullptr,
535 unsigned NumOperands = 0) const;
536
537public:
538 /// Method to get a uniquely generated InstructionMapping.
539 const InstructionMapping &
540 getInstructionMapping(unsigned ID, unsigned Cost,
541 const ValueMapping *OperandsMapping,
542 unsigned NumOperands) const {
543 return getInstructionMappingImpl(/*IsInvalid*/ false, ID, Cost,
544 OperandsMapping, NumOperands);
545 }
546
547 /// Method to get a uniquely generated invalid InstructionMapping.
549 return getInstructionMappingImpl(/*IsInvalid*/ true);
550 }
551 /// @}
552
553 /// Get the register bank for the \p OpIdx-th operand of \p MI form
554 /// the encoding constraints, if any.
555 ///
556 /// \return A register bank that covers the register class of the
557 /// related encoding constraints or nullptr if \p MI did not provide
558 /// enough information to deduce it.
559 const RegisterBank *
560 getRegBankFromConstraints(const MachineInstr &MI, unsigned OpIdx,
561 const TargetInstrInfo &TII,
562 const MachineRegisterInfo &MRI) const;
563
564 /// Helper method to apply something that is like the default mapping.
565 /// Basically, that means that \p OpdMapper.getMI() is left untouched
566 /// aside from the reassignment of the register operand that have been
567 /// remapped.
568 ///
569 /// The type of all the new registers that have been created by the
570 /// mapper are properly remapped to the type of the original registers
571 /// they replace. In other words, the semantic of the instruction does
572 /// not change, only the register banks.
573 ///
574 /// If the mapping of one of the operand spans several registers, this
575 /// method will abort as this is not like a default mapping anymore.
576 ///
577 /// \pre For OpIdx in {0..\p OpdMapper.getMI().getNumOperands())
578 /// the range OpdMapper.getVRegs(OpIdx) is empty or of size 1.
579 static void applyDefaultMapping(const OperandsMapper &OpdMapper);
580
581 /// See ::applyMapping.
582 virtual void applyMappingImpl(MachineIRBuilder &Builder,
583 const OperandsMapper &OpdMapper) const {
584 llvm_unreachable("The target has to implement this");
585 }
586
587public:
588 virtual ~RegisterBankInfo() = default;
589
590 /// Get the register bank identified by \p ID.
591 const RegisterBank &getRegBank(unsigned ID) const {
592 return const_cast<RegisterBankInfo *>(this)->getRegBank(ID);
593 }
594
595 /// Get the maximum size in bits that fits in the given register bank.
596 unsigned getMaximumSize(unsigned RegBankID) const {
597 return Sizes[RegBankID + HwMode * NumRegBanks];
598 }
599
600 /// Get the register bank of \p Reg.
601 /// If Reg has not been assigned a register, a register class,
602 /// or a register bank, then this returns nullptr.
603 ///
604 /// \pre Reg != 0 (NoRegister)
605 const RegisterBank *getRegBank(Register Reg, const MachineRegisterInfo &MRI,
606 const TargetRegisterInfo &TRI) const;
607
608 /// Get the total number of register banks.
609 unsigned getNumRegBanks() const { return NumRegBanks; }
610
611 /// Returns true if the register bank is considered divergent.
612 virtual bool isDivergentRegBank(const RegisterBank *RB) const {
613 return false;
614 }
615
616 /// Get a register bank that covers \p RC.
617 ///
618 /// \pre \p RC is a user-defined register class (as opposed as one
619 /// generated by TableGen).
620 ///
621 /// \note The mapping RC -> RegBank could be built while adding the
622 /// coverage for the register banks. However, we do not do it, because,
623 /// at least for now, we only need this information for register classes
624 /// that are used in the description of instruction. In other words,
625 /// there are just a handful of them and we do not want to waste space.
626 ///
627 /// \todo This should be TableGen'ed.
628 virtual const RegisterBank &
630 llvm_unreachable("The target must override this method");
631 }
632
633 /// Get the cost of a copy from \p B to \p A, or put differently,
634 /// get the cost of A = COPY B. Since register banks may cover
635 /// different size, \p Size specifies what will be the size in bits
636 /// that will be copied around.
637 ///
638 /// \note Since this is a copy, both registers have the same size.
639 virtual unsigned copyCost(const RegisterBank &A, const RegisterBank &B,
640 TypeSize Size) const {
641 // Optimistically assume that copies are coalesced. I.e., when
642 // they are on the same bank, they are free.
643 // Otherwise assume a non-zero cost of 1. The targets are supposed
644 // to override that properly anyway if they care.
645 return &A != &B;
646 }
647
648 /// \returns true if emitting a copy from \p Src to \p Dst is impossible.
649 bool cannotCopy(const RegisterBank &Dst, const RegisterBank &Src,
650 TypeSize Size) const {
651 return copyCost(Dst, Src, Size) == std::numeric_limits<unsigned>::max();
652 }
653
654 /// Get the cost of using \p ValMapping to decompose a register. This is
655 /// similar to ::copyCost, except for cases where multiple copy-like
656 /// operations need to be inserted. If the register is used as a source
657 /// operand and already has a bank assigned, \p CurBank is non-null.
658 virtual unsigned
660 const RegisterBank *CurBank = nullptr) const {
661 return std::numeric_limits<unsigned>::max();
662 }
663
664 /// Constrain the (possibly generic) virtual register \p Reg to \p RC.
665 ///
666 /// \pre \p Reg is a virtual register that either has a bank or a class.
667 /// \returns The constrained register class, or nullptr if there is none.
668 /// \note This is a generic variant of MachineRegisterInfo::constrainRegClass
669 /// \note Use MachineRegisterInfo::constrainRegAttrs instead for any non-isel
670 /// purpose, including non-select passes of GlobalISel
671 static const TargetRegisterClass *
672 constrainGenericRegister(Register Reg, const TargetRegisterClass &RC,
674
675 /// Identifier used when the related instruction mapping instance
676 /// is generated by target independent code.
677 /// Make sure not to use that identifier to avoid possible collision.
678 static const unsigned DefaultMappingID;
679
680 /// Identifier used when the related instruction mapping instance
681 /// is generated by the default constructor.
682 /// Make sure not to use that identifier.
683 static const unsigned InvalidMappingID;
684
685 /// Get the mapping of the different operands of \p MI
686 /// on the register bank.
687 /// This mapping should be the direct translation of \p MI.
688 /// In other words, when \p MI is mapped with the returned mapping,
689 /// only the register banks of the operands of \p MI need to be updated.
690 /// In particular, neither the opcode nor the type of \p MI needs to be
691 /// updated for this direct mapping.
692 ///
693 /// The target independent implementation gives a mapping based on
694 /// the register classes for the target specific opcode.
695 /// It uses the ID RegisterBankInfo::DefaultMappingID for that mapping.
696 /// Make sure you do not use that ID for the alternative mapping
697 /// for MI. See getInstrAlternativeMappings for the alternative
698 /// mappings.
699 ///
700 /// For instance, if \p MI is a vector add, the mapping should
701 /// not be a scalarization of the add.
702 ///
703 /// \post returnedVal.verify(MI).
704 ///
705 /// \note If returnedVal does not verify MI, this would probably mean
706 /// that the target does not support that instruction.
707 virtual const InstructionMapping &
708 getInstrMapping(const MachineInstr &MI) const;
709
710 /// Get the alternative mappings for \p MI.
711 /// Alternative in the sense different from getInstrMapping.
712 virtual InstructionMappings
714
715 /// Get the possible mapping for \p MI.
716 /// A mapping defines where the different operands may live and at what cost.
717 /// For instance, let us consider:
718 /// v0(16) = G_ADD <2 x i8> v1, v2
719 /// The possible mapping could be:
720 ///
721 /// {/*ID*/VectorAdd, /*Cost*/1, /*v0*/{(0xFFFF, VPR)}, /*v1*/{(0xFFFF, VPR)},
722 /// /*v2*/{(0xFFFF, VPR)}}
723 /// {/*ID*/ScalarAddx2, /*Cost*/2, /*v0*/{(0x00FF, GPR),(0xFF00, GPR)},
724 /// /*v1*/{(0x00FF, GPR),(0xFF00, GPR)},
725 /// /*v2*/{(0x00FF, GPR),(0xFF00, GPR)}}
726 ///
727 /// \note The first alternative of the returned mapping should be the
728 /// direct translation of \p MI current form.
729 ///
730 /// \post !returnedVal.empty().
732
733 /// Apply \p OpdMapper.getInstrMapping() to \p OpdMapper.getMI().
734 /// After this call \p OpdMapper.getMI() may not be valid anymore.
735 /// \p OpdMapper.getInstrMapping().getID() carries the information of
736 /// what has been chosen to map \p OpdMapper.getMI(). This ID is set
737 /// by the various getInstrXXXMapping method.
738 ///
739 /// Therefore, getting the mapping and applying it should be kept in
740 /// sync.
742 const OperandsMapper &OpdMapper) const {
743 // The only mapping we know how to handle is the default mapping.
744 if (OpdMapper.getInstrMapping().getID() == DefaultMappingID)
745 return applyDefaultMapping(OpdMapper);
746 // For other mapping, the target needs to do the right thing.
747 // If that means calling applyDefaultMapping, fine, but this
748 // must be explicitly stated.
749 applyMappingImpl(Builder, OpdMapper);
750 }
751
752 /// Get the size in bits of \p Reg.
753 /// Utility method to get the size of any registers. Unlike
754 /// MachineRegisterInfo::getSize, the register does not need to be a
755 /// virtual register.
756 ///
757 /// \pre \p Reg != 0 (NoRegister).
758 TypeSize getSizeInBits(Register Reg, const MachineRegisterInfo &MRI,
759 const TargetRegisterInfo &TRI) const;
760
761 /// Check that information hold by this instance make sense for the
762 /// given \p TRI.
763 ///
764 /// \note This method does not check anything when assertions are disabled.
765 ///
766 /// \return True is the check was successful.
767 bool verify(const TargetRegisterInfo &TRI) const;
768};
769
772 const RegisterBankInfo::PartialMapping &PartMapping) {
773 PartMapping.print(OS);
774 return OS;
775}
776
779 ValMapping.print(OS);
780 return OS;
781}
782
785 const RegisterBankInfo::InstructionMapping &InstrMapping) {
786 InstrMapping.print(OS);
787 return OS;
788}
789
792 OpdMapper.print(OS, /*ForDebug*/ false);
793 return OS;
794}
795
796/// Hashing function for PartialMapping.
797/// It is required for the hashing of ValueMapping.
798LLVM_ABI hash_code
799hash_value(const RegisterBankInfo::PartialMapping &PartMapping);
800
801} // end namespace llvm
802
803#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:213
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.
static const TargetRegisterClass * getMinimalPhysRegClass(const TargetRegisterInfo *TRI, MCRegister Reg, TypeT Ty)
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:558
hash_code hash_value(const FixedPointSemantics &Val)
InstructionCost Cost
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
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.