LLVM 24.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 /// Create a RegisterBankInfo that can accommodate up to \p NumRegBanks
425 /// RegisterBank instances.
427 const unsigned *Sizes, unsigned HwMode);
428
429 /// This constructor is meaningless.
430 /// It just provides a default constructor that can be used at link time
431 /// when GlobalISel is not built.
432 /// That way, targets can still inherit from this class without doing
433 /// crazy gymnastic to avoid link time failures.
434 /// \note That works because the constructor is inlined.
436 llvm_unreachable("This constructor should not be executed");
437 }
438
441
442 /// Get the register bank identified by \p ID.
443 const RegisterBank &getRegBank(unsigned ID) {
444 assert(ID < getNumRegBanks() && "Accessing an unknown register bank");
445 return *RegBanks[ID];
446 }
447
448 /// Try to get the mapping of \p MI.
449 /// See getInstrMapping for more details on what a mapping represents.
450 ///
451 /// Unlike getInstrMapping the returned InstructionMapping may be invalid
452 /// (isValid() == false).
453 /// This means that the target independent code is not smart enough
454 /// to get the mapping of \p MI and thus, the target has to provide the
455 /// information for \p MI.
456 ///
457 /// This implementation is able to get the mapping of:
458 /// - Target specific instructions by looking at the encoding constraints.
459 /// - Any instruction if all the register operands have already been assigned
460 /// a register, a register class, or a register bank.
461 /// - Copies and phis if at least one of the operands has been assigned a
462 /// register, a register class, or a register bank.
463 /// In other words, this method will likely fail to find a mapping for
464 /// any generic opcode that has not been lowered by target specific code.
465 const InstructionMapping &getInstrMappingImpl(const MachineInstr &MI) const;
466
467 /// Get the uniquely generated PartialMapping for the
468 /// given arguments.
469 const PartialMapping &getPartialMapping(unsigned StartIdx, unsigned Length,
470 const RegisterBank &RegBank) const;
471
472 /// \name Methods to get a uniquely generated ValueMapping.
473 /// @{
474
475 /// The most common ValueMapping consists of a single PartialMapping.
476 /// Feature a method for that.
477 const ValueMapping &getValueMapping(unsigned StartIdx, unsigned Length,
478 const RegisterBank &RegBank) const;
479
480 /// Get the ValueMapping for the given arguments.
481 const ValueMapping &getValueMapping(const PartialMapping *BreakDown,
482 unsigned NumBreakDowns) const;
483 /// @}
484
485 /// \name Methods to get a uniquely generated array of ValueMapping.
486 /// @{
487
488 /// Get the uniquely generated array of ValueMapping for the
489 /// elements of between \p Begin and \p End.
490 ///
491 /// Elements that are nullptr will be replaced by
492 /// invalid ValueMapping (ValueMapping::isValid == false).
493 ///
494 /// \pre The pointers on ValueMapping between \p Begin and \p End
495 /// must uniquely identify a ValueMapping. Otherwise, there is no
496 /// guarantee that the return instance will be unique, i.e., another
497 /// OperandsMapping could have the same content.
498 template <typename Iterator>
499 const ValueMapping *getOperandsMapping(Iterator Begin, Iterator End) const;
500
501 /// Get the uniquely generated array of ValueMapping for the
502 /// elements of \p OpdsMapping.
503 ///
504 /// Elements of \p OpdsMapping that are nullptr will be replaced by
505 /// invalid ValueMapping (ValueMapping::isValid == false).
506 const ValueMapping *getOperandsMapping(
507 const SmallVectorImpl<const ValueMapping *> &OpdsMapping) const;
508
509 /// Get the uniquely generated array of ValueMapping for the
510 /// given arguments.
511 ///
512 /// Arguments that are nullptr will be replaced by invalid
513 /// ValueMapping (ValueMapping::isValid == false).
515 std::initializer_list<const ValueMapping *> OpdsMapping) const;
516 /// @}
517
518 /// \name Methods to get a uniquely generated InstructionMapping.
519 /// @{
520
521private:
522 /// Method to get a uniquely generated InstructionMapping.
523 const InstructionMapping &
524 getInstructionMappingImpl(bool IsInvalid, unsigned ID = InvalidMappingID,
525 unsigned Cost = 0,
526 const ValueMapping *OperandsMapping = nullptr,
527 unsigned NumOperands = 0) const;
528
529public:
530 /// Method to get a uniquely generated InstructionMapping.
531 const InstructionMapping &
532 getInstructionMapping(unsigned ID, unsigned Cost,
533 const ValueMapping *OperandsMapping,
534 unsigned NumOperands) const {
535 return getInstructionMappingImpl(/*IsInvalid*/ false, ID, Cost,
536 OperandsMapping, NumOperands);
537 }
538
539 /// Method to get a uniquely generated invalid InstructionMapping.
541 return getInstructionMappingImpl(/*IsInvalid*/ true);
542 }
543 /// @}
544
545 /// Get the register bank for the \p OpIdx-th operand of \p MI form
546 /// the encoding constraints, if any.
547 ///
548 /// \return A register bank that covers the register class of the
549 /// related encoding constraints or nullptr if \p MI did not provide
550 /// enough information to deduce it.
551 const RegisterBank *
552 getRegBankFromConstraints(const MachineInstr &MI, unsigned OpIdx,
553 const TargetInstrInfo &TII,
554 const MachineRegisterInfo &MRI) const;
555
556 /// Helper method to apply something that is like the default mapping.
557 /// Basically, that means that \p OpdMapper.getMI() is left untouched
558 /// aside from the reassignment of the register operand that have been
559 /// remapped.
560 ///
561 /// The type of all the new registers that have been created by the
562 /// mapper are properly remapped to the type of the original registers
563 /// they replace. In other words, the semantic of the instruction does
564 /// not change, only the register banks.
565 ///
566 /// If the mapping of one of the operand spans several registers, this
567 /// method will abort as this is not like a default mapping anymore.
568 ///
569 /// \pre For OpIdx in {0..\p OpdMapper.getMI().getNumOperands())
570 /// the range OpdMapper.getVRegs(OpIdx) is empty or of size 1.
571 static void applyDefaultMapping(const OperandsMapper &OpdMapper);
572
573 /// See ::applyMapping.
574 virtual void applyMappingImpl(MachineIRBuilder &Builder,
575 const OperandsMapper &OpdMapper) const {
576 llvm_unreachable("The target has to implement this");
577 }
578
579public:
580 virtual ~RegisterBankInfo() = default;
581
582 /// Get the register bank identified by \p ID.
583 const RegisterBank &getRegBank(unsigned ID) const {
584 return const_cast<RegisterBankInfo *>(this)->getRegBank(ID);
585 }
586
587 /// Get the maximum size in bits that fits in the given register bank.
588 unsigned getMaximumSize(unsigned RegBankID) const {
589 return Sizes[RegBankID + HwMode * NumRegBanks];
590 }
591
592 /// Get the register bank of \p Reg.
593 /// If Reg has not been assigned a register, a register class,
594 /// or a register bank, then this returns nullptr.
595 ///
596 /// \pre Reg != 0 (NoRegister)
597 const RegisterBank *getRegBank(Register Reg, const MachineRegisterInfo &MRI,
598 const TargetRegisterInfo &TRI) const;
599
600 /// Get the total number of register banks.
601 unsigned getNumRegBanks() const { return NumRegBanks; }
602
603 /// Returns true if the register bank is considered divergent.
604 virtual bool isDivergentRegBank(const RegisterBank *RB) const {
605 return false;
606 }
607
608 /// Get a register bank that covers \p RC.
609 ///
610 /// \pre \p RC is a user-defined register class (as opposed as one
611 /// generated by TableGen).
612 ///
613 /// \note The mapping RC -> RegBank could be built while adding the
614 /// coverage for the register banks. However, we do not do it, because,
615 /// at least for now, we only need this information for register classes
616 /// that are used in the description of instruction. In other words,
617 /// there are just a handful of them and we do not want to waste space.
618 ///
619 /// \todo This should be TableGen'ed.
620 virtual const RegisterBank &
622 llvm_unreachable("The target must override this method");
623 }
624
625 /// Get the cost of a copy from \p B to \p A, or put differently,
626 /// get the cost of A = COPY B. Since register banks may cover
627 /// different size, \p Size specifies what will be the size in bits
628 /// that will be copied around.
629 ///
630 /// \note Since this is a copy, both registers have the same size.
631 virtual unsigned copyCost(const RegisterBank &A, const RegisterBank &B,
632 TypeSize Size) const {
633 // Optimistically assume that copies are coalesced. I.e., when
634 // they are on the same bank, they are free.
635 // Otherwise assume a non-zero cost of 1. The targets are supposed
636 // to override that properly anyway if they care.
637 return &A != &B;
638 }
639
640 /// \returns true if emitting a copy from \p Src to \p Dst is impossible.
641 bool cannotCopy(const RegisterBank &Dst, const RegisterBank &Src,
642 TypeSize Size) const {
643 return copyCost(Dst, Src, Size) == std::numeric_limits<unsigned>::max();
644 }
645
646 /// Get the cost of using \p ValMapping to decompose a register. This is
647 /// similar to ::copyCost, except for cases where multiple copy-like
648 /// operations need to be inserted. If the register is used as a source
649 /// operand and already has a bank assigned, \p CurBank is non-null.
650 virtual unsigned
652 const RegisterBank *CurBank = nullptr) const {
653 return std::numeric_limits<unsigned>::max();
654 }
655
656 /// Constrain the (possibly generic) virtual register \p Reg to \p RC.
657 ///
658 /// \pre \p Reg is a virtual register that either has a bank or a class.
659 /// \returns The constrained register class, or nullptr if there is none.
660 /// \note This is a generic variant of MachineRegisterInfo::constrainRegClass
661 /// \note Use MachineRegisterInfo::constrainRegAttrs instead for any non-isel
662 /// purpose, including non-select passes of GlobalISel
663 static const TargetRegisterClass *
664 constrainGenericRegister(Register Reg, const TargetRegisterClass &RC,
666
667 /// Identifier used when the related instruction mapping instance
668 /// is generated by target independent code.
669 /// Make sure not to use that identifier to avoid possible collision.
670 static const unsigned DefaultMappingID;
671
672 /// Identifier used when the related instruction mapping instance
673 /// is generated by the default constructor.
674 /// Make sure not to use that identifier.
675 static const unsigned InvalidMappingID;
676
677 /// Get the mapping of the different operands of \p MI
678 /// on the register bank.
679 /// This mapping should be the direct translation of \p MI.
680 /// In other words, when \p MI is mapped with the returned mapping,
681 /// only the register banks of the operands of \p MI need to be updated.
682 /// In particular, neither the opcode nor the type of \p MI needs to be
683 /// updated for this direct mapping.
684 ///
685 /// The target independent implementation gives a mapping based on
686 /// the register classes for the target specific opcode.
687 /// It uses the ID RegisterBankInfo::DefaultMappingID for that mapping.
688 /// Make sure you do not use that ID for the alternative mapping
689 /// for MI. See getInstrAlternativeMappings for the alternative
690 /// mappings.
691 ///
692 /// For instance, if \p MI is a vector add, the mapping should
693 /// not be a scalarization of the add.
694 ///
695 /// \post returnedVal.verify(MI).
696 ///
697 /// \note If returnedVal does not verify MI, this would probably mean
698 /// that the target does not support that instruction.
699 virtual const InstructionMapping &
700 getInstrMapping(const MachineInstr &MI) const;
701
702 /// Get the alternative mappings for \p MI.
703 /// Alternative in the sense different from getInstrMapping.
704 virtual InstructionMappings
706
707 /// Get the possible mapping for \p MI.
708 /// A mapping defines where the different operands may live and at what cost.
709 /// For instance, let us consider:
710 /// v0(16) = G_ADD <2 x i8> v1, v2
711 /// The possible mapping could be:
712 ///
713 /// {/*ID*/VectorAdd, /*Cost*/1, /*v0*/{(0xFFFF, VPR)}, /*v1*/{(0xFFFF, VPR)},
714 /// /*v2*/{(0xFFFF, VPR)}}
715 /// {/*ID*/ScalarAddx2, /*Cost*/2, /*v0*/{(0x00FF, GPR),(0xFF00, GPR)},
716 /// /*v1*/{(0x00FF, GPR),(0xFF00, GPR)},
717 /// /*v2*/{(0x00FF, GPR),(0xFF00, GPR)}}
718 ///
719 /// \note The first alternative of the returned mapping should be the
720 /// direct translation of \p MI current form.
721 ///
722 /// \post !returnedVal.empty().
724
725 /// Apply \p OpdMapper.getInstrMapping() to \p OpdMapper.getMI().
726 /// After this call \p OpdMapper.getMI() may not be valid anymore.
727 /// \p OpdMapper.getInstrMapping().getID() carries the information of
728 /// what has been chosen to map \p OpdMapper.getMI(). This ID is set
729 /// by the various getInstrXXXMapping method.
730 ///
731 /// Therefore, getting the mapping and applying it should be kept in
732 /// sync.
734 const OperandsMapper &OpdMapper) const {
735 // The only mapping we know how to handle is the default mapping.
736 if (OpdMapper.getInstrMapping().getID() == DefaultMappingID)
737 return applyDefaultMapping(OpdMapper);
738 // For other mapping, the target needs to do the right thing.
739 // If that means calling applyDefaultMapping, fine, but this
740 // must be explicitly stated.
741 applyMappingImpl(Builder, OpdMapper);
742 }
743
744 /// Get the size in bits of \p Reg.
745 /// Utility method to get the size of any registers. Unlike
746 /// MachineRegisterInfo::getSize, the register does not need to be a
747 /// virtual register.
748 ///
749 /// \pre \p Reg != 0 (NoRegister).
750 TypeSize getSizeInBits(Register Reg, const MachineRegisterInfo &MRI,
751 const TargetRegisterInfo &TRI) const;
752
753 /// Check that information hold by this instance make sense for the
754 /// given \p TRI.
755 ///
756 /// \note This method does not check anything when assertions are disabled.
757 ///
758 /// \return True is the check was successful.
759 bool verify(const TargetRegisterInfo &TRI) const;
760};
761
764 const RegisterBankInfo::PartialMapping &PartMapping) {
765 PartMapping.print(OS);
766 return OS;
767}
768
771 ValMapping.print(OS);
772 return OS;
773}
774
777 const RegisterBankInfo::InstructionMapping &InstrMapping) {
778 InstrMapping.print(OS);
779 return OS;
780}
781
784 OpdMapper.print(OS, /*ForDebug*/ false);
785 return OS;
786}
787
788/// Hashing function for PartialMapping.
789/// It is required for the hashing of ValueMapping.
790LLVM_ABI hash_code
791hash_value(const RegisterBankInfo::PartialMapping &PartMapping);
792
793} // end namespace llvm
794
795#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:211
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
ppc ctr loops verify
MachineRegisterInfo & MRI
This file defines the SmallVector class.
MCRegisterClass - Base class of TargetRegisterClass.
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...
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.
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
@ Length
Definition DWP.cpp:577
hash_code hash_value(const FixedPointSemantics &Val)
InstructionCost Cost
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
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.