1 //===- HexagonGenInsert.cpp -----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "BitTracker.h"
10 #include "HexagonBitTracker.h"
11 #include "HexagonInstrInfo.h"
12 #include "HexagonRegisterInfo.h"
13 #include "HexagonSubtarget.h"
14 #include "llvm/ADT/BitVector.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/GraphTraits.h"
17 #include "llvm/ADT/PostOrderIterator.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/CodeGen/MachineBasicBlock.h"
23 #include "llvm/CodeGen/MachineDominators.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineFunctionPass.h"
26 #include "llvm/CodeGen/MachineInstr.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineOperand.h"
29 #include "llvm/CodeGen/MachineRegisterInfo.h"
30 #include "llvm/CodeGen/TargetRegisterInfo.h"
31 #include "llvm/IR/DebugLoc.h"
32 #include "llvm/InitializePasses.h"
33 #include "llvm/Pass.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/MathExtras.h"
37 #include "llvm/Support/Timer.h"
38 #include "llvm/Support/raw_ostream.h"
46 #define DEBUG_TYPE "hexinsert"
50 static cl::opt
<unsigned>
51 VRegIndexCutoff("insert-vreg-cutoff", cl::init(~0U), cl::Hidden
,
52 cl::desc("Vreg# cutoff for insert generation."));
53 // The distance cutoff is selected based on the precheckin-perf results:
54 // cutoffs 20, 25, 35, and 40 are worse than 30.
55 static cl::opt
<unsigned>
56 VRegDistCutoff("insert-dist-cutoff", cl::init(30U), cl::Hidden
,
57 cl::desc("Vreg distance cutoff for insert "
60 // Limit the container sizes for extreme cases where we run out of memory.
61 static cl::opt
<unsigned>
62 MaxORLSize("insert-max-orl", cl::init(4096), cl::Hidden
,
63 cl::desc("Maximum size of OrderedRegisterList"));
64 static cl::opt
<unsigned> MaxIFMSize("insert-max-ifmap", cl::init(1024),
66 cl::desc("Maximum size of IFMap"));
68 static cl::opt
<bool> OptTiming("insert-timing", cl::Hidden
,
69 cl::desc("Enable timing of insert generation"));
71 OptTimingDetail("insert-timing-detail", cl::Hidden
,
72 cl::desc("Enable detailed timing of insert "
75 static cl::opt
<bool> OptSelectAll0("insert-all0", cl::init(false), cl::Hidden
);
76 static cl::opt
<bool> OptSelectHas0("insert-has0", cl::init(false), cl::Hidden
);
77 // Whether to construct constant values via "insert". Could eliminate constant
78 // extenders, but often not practical.
79 static cl::opt
<bool> OptConst("insert-const", cl::init(false), cl::Hidden
);
81 // The preprocessor gets confused when the DEBUG macro is passed larger
82 // chunks of code. Use this function to detect debugging.
83 inline static bool isDebug() {
85 return DebugFlag
&& isCurrentDebugType(DEBUG_TYPE
);
93 // Set of virtual registers, based on BitVector.
94 struct RegisterSet
: private BitVector
{
95 RegisterSet() = default;
96 explicit RegisterSet(unsigned s
, bool t
= false) : BitVector(s
, t
) {}
97 RegisterSet(const RegisterSet
&RS
) = default;
98 RegisterSet
&operator=(const RegisterSet
&RS
) = default;
100 using BitVector::clear
;
102 unsigned find_first() const {
103 int First
= BitVector::find_first();
109 unsigned find_next(unsigned Prev
) const {
110 int Next
= BitVector::find_next(v2x(Prev
));
116 RegisterSet
&insert(unsigned R
) {
117 unsigned Idx
= v2x(R
);
119 return static_cast<RegisterSet
&>(BitVector::set(Idx
));
121 RegisterSet
&remove(unsigned R
) {
122 unsigned Idx
= v2x(R
);
125 return static_cast<RegisterSet
&>(BitVector::reset(Idx
));
128 RegisterSet
&insert(const RegisterSet
&Rs
) {
129 return static_cast<RegisterSet
&>(BitVector::operator|=(Rs
));
131 RegisterSet
&remove(const RegisterSet
&Rs
) {
132 return static_cast<RegisterSet
&>(BitVector::reset(Rs
));
135 reference
operator[](unsigned R
) {
136 unsigned Idx
= v2x(R
);
138 return BitVector::operator[](Idx
);
140 bool operator[](unsigned R
) const {
141 unsigned Idx
= v2x(R
);
142 assert(Idx
< size());
143 return BitVector::operator[](Idx
);
145 bool has(unsigned R
) const {
146 unsigned Idx
= v2x(R
);
149 return BitVector::test(Idx
);
153 return !BitVector::any();
155 bool includes(const RegisterSet
&Rs
) const {
156 // A.BitVector::test(B) <=> A-B != {}
157 return !Rs
.BitVector::test(*this);
159 bool intersects(const RegisterSet
&Rs
) const {
160 return BitVector::anyCommon(Rs
);
164 void ensure(unsigned Idx
) {
166 resize(std::max(Idx
+1, 32U));
169 static inline unsigned v2x(unsigned v
) {
170 return Register::virtReg2Index(v
);
173 static inline unsigned x2v(unsigned x
) {
174 return Register::index2VirtReg(x
);
179 PrintRegSet(const RegisterSet
&S
, const TargetRegisterInfo
*RI
)
182 friend raw_ostream
&operator<< (raw_ostream
&OS
,
183 const PrintRegSet
&P
);
186 const RegisterSet
&RS
;
187 const TargetRegisterInfo
*TRI
;
190 raw_ostream
&operator<< (raw_ostream
&OS
, const PrintRegSet
&P
) {
192 for (unsigned R
= P
.RS
.find_first(); R
; R
= P
.RS
.find_next(R
))
193 OS
<< ' ' << printReg(R
, P
.TRI
);
198 // A convenience class to associate unsigned numbers (such as virtual
199 // registers) with unsigned numbers.
200 struct UnsignedMap
: public DenseMap
<unsigned,unsigned> {
201 UnsignedMap() = default;
204 using BaseType
= DenseMap
<unsigned, unsigned>;
207 // A utility to establish an ordering between virtual registers:
208 // VRegA < VRegB <=> RegisterOrdering[VRegA] < RegisterOrdering[VRegB]
209 // This is meant as a cache for the ordering of virtual registers defined
210 // by a potentially expensive comparison function, or obtained by a proce-
211 // dure that should not be repeated each time two registers are compared.
212 struct RegisterOrdering
: public UnsignedMap
{
213 RegisterOrdering() = default;
215 unsigned operator[](unsigned VR
) const {
216 const_iterator F
= find(VR
);
221 // Add operator(), so that objects of this class can be used as
222 // comparators in std::sort et al.
223 bool operator() (unsigned VR1
, unsigned VR2
) const {
224 return operator[](VR1
) < operator[](VR2
);
228 // Ordering of bit values. This class does not have operator[], but
229 // is supplies a comparison operator() for use in std:: algorithms.
230 // The order is as follows:
232 // - ref1 < ref2, if ord(ref1.Reg) < ord(ref2.Reg),
233 // or ord(ref1.Reg) == ord(ref2.Reg), and ref1.Pos < ref2.Pos.
234 struct BitValueOrdering
{
235 BitValueOrdering(const RegisterOrdering
&RB
) : BaseOrd(RB
) {}
237 bool operator() (const BitTracker::BitValue
&V1
,
238 const BitTracker::BitValue
&V2
) const;
240 const RegisterOrdering
&BaseOrd
;
243 } // end anonymous namespace
245 bool BitValueOrdering::operator() (const BitTracker::BitValue
&V1
,
246 const BitTracker::BitValue
&V2
) const {
249 // V1==0 => true, V2==0 => false
250 if (V1
.is(0) || V2
.is(0))
252 // Neither of V1,V2 is 0, and V1!=V2.
253 // V2==1 => false, V1==1 => true
254 if (V2
.is(1) || V1
.is(1))
256 // Both V1,V2 are refs.
257 unsigned Ind1
= BaseOrd
[V1
.RefI
.Reg
], Ind2
= BaseOrd
[V2
.RefI
.Reg
];
261 assert(V1
.RefI
.Pos
!= V2
.RefI
.Pos
&& "Bit values should be different");
262 return V1
.RefI
.Pos
< V2
.RefI
.Pos
;
267 // Cache for the BitTracker's cell map. Map lookup has a logarithmic
268 // complexity, this class will memoize the lookup results to reduce
269 // the access time for repeated lookups of the same cell.
270 struct CellMapShadow
{
271 CellMapShadow(const BitTracker
&T
) : BT(T
) {}
273 const BitTracker::RegisterCell
&lookup(unsigned VR
) {
274 unsigned RInd
= Register::virtReg2Index(VR
);
275 // Grow the vector to at least 32 elements.
276 if (RInd
>= CVect
.size())
277 CVect
.resize(std::max(RInd
+16, 32U), nullptr);
278 const BitTracker::RegisterCell
*CP
= CVect
[RInd
];
280 CP
= CVect
[RInd
] = &BT
.lookup(VR
);
284 const BitTracker
&BT
;
287 using CellVectType
= std::vector
<const BitTracker::RegisterCell
*>;
292 // Comparator class for lexicographic ordering of virtual registers
293 // according to the corresponding BitTracker::RegisterCell objects.
294 struct RegisterCellLexCompare
{
295 RegisterCellLexCompare(const BitValueOrdering
&BO
, CellMapShadow
&M
)
296 : BitOrd(BO
), CM(M
) {}
298 bool operator() (unsigned VR1
, unsigned VR2
) const;
301 const BitValueOrdering
&BitOrd
;
305 // Comparator class for lexicographic ordering of virtual registers
306 // according to the specified bits of the corresponding BitTracker::
307 // RegisterCell objects.
308 // Specifically, this class will be used to compare bit B of a register
309 // cell for a selected virtual register R with bit N of any register
311 struct RegisterCellBitCompareSel
{
312 RegisterCellBitCompareSel(unsigned R
, unsigned B
, unsigned N
,
313 const BitValueOrdering
&BO
, CellMapShadow
&M
)
314 : SelR(R
), SelB(B
), BitN(N
), BitOrd(BO
), CM(M
) {}
316 bool operator() (unsigned VR1
, unsigned VR2
) const;
319 const unsigned SelR
, SelB
;
321 const BitValueOrdering
&BitOrd
;
325 } // end anonymous namespace
327 bool RegisterCellLexCompare::operator() (unsigned VR1
, unsigned VR2
) const {
328 // Ordering of registers, made up from two given orderings:
329 // - the ordering of the register numbers, and
330 // - the ordering of register cells.
332 // - cell(R1) < cell(R2), or
333 // - cell(R1) == cell(R2), and index(R1) < index(R2).
335 // For register cells, the ordering is lexicographic, with index 0 being
336 // the most significant.
340 const BitTracker::RegisterCell
&RC1
= CM
.lookup(VR1
), &RC2
= CM
.lookup(VR2
);
341 uint16_t W1
= RC1
.width(), W2
= RC2
.width();
342 for (uint16_t i
= 0, w
= std::min(W1
, W2
); i
< w
; ++i
) {
343 const BitTracker::BitValue
&V1
= RC1
[i
], &V2
= RC2
[i
];
345 return BitOrd(V1
, V2
);
347 // Cells are equal up until the common length.
351 return BitOrd
.BaseOrd
[VR1
] < BitOrd
.BaseOrd
[VR2
];
354 bool RegisterCellBitCompareSel::operator() (unsigned VR1
, unsigned VR2
) const {
357 const BitTracker::RegisterCell
&RC1
= CM
.lookup(VR1
);
358 const BitTracker::RegisterCell
&RC2
= CM
.lookup(VR2
);
359 uint16_t W1
= RC1
.width(), W2
= RC2
.width();
360 uint16_t Bit1
= (VR1
== SelR
) ? SelB
: BitN
;
361 uint16_t Bit2
= (VR2
== SelR
) ? SelB
: BitN
;
362 // If Bit1 exceeds the width of VR1, then:
363 // - return false, if at the same time Bit2 exceeds VR2, or
364 // - return true, otherwise.
365 // (I.e. "a bit value that does not exist is less than any bit value
366 // that does exist".)
369 // If Bit1 is within VR1, but Bit2 is not within VR2, return false.
373 const BitTracker::BitValue
&V1
= RC1
[Bit1
], V2
= RC2
[Bit2
];
375 return BitOrd(V1
, V2
);
381 class OrderedRegisterList
{
382 using ListType
= std::vector
<unsigned>;
383 const unsigned MaxSize
;
386 OrderedRegisterList(const RegisterOrdering
&RO
)
387 : MaxSize(MaxORLSize
), Ord(RO
) {}
389 void insert(unsigned VR
);
390 void remove(unsigned VR
);
392 unsigned operator[](unsigned Idx
) const {
393 assert(Idx
< Seq
.size());
397 unsigned size() const {
401 using iterator
= ListType::iterator
;
402 using const_iterator
= ListType::const_iterator
;
404 iterator
begin() { return Seq
.begin(); }
405 iterator
end() { return Seq
.end(); }
406 const_iterator
begin() const { return Seq
.begin(); }
407 const_iterator
end() const { return Seq
.end(); }
409 // Convenience function to convert an iterator to the corresponding index.
410 unsigned idx(iterator It
) const { return It
-begin(); }
414 const RegisterOrdering
&Ord
;
418 PrintORL(const OrderedRegisterList
&L
, const TargetRegisterInfo
*RI
)
421 friend raw_ostream
&operator<< (raw_ostream
&OS
, const PrintORL
&P
);
424 const OrderedRegisterList
&RL
;
425 const TargetRegisterInfo
*TRI
;
428 raw_ostream
&operator<< (raw_ostream
&OS
, const PrintORL
&P
) {
430 OrderedRegisterList::const_iterator B
= P
.RL
.begin(), E
= P
.RL
.end();
431 for (OrderedRegisterList::const_iterator I
= B
; I
!= E
; ++I
) {
434 OS
<< printReg(*I
, P
.TRI
);
440 } // end anonymous namespace
442 void OrderedRegisterList::insert(unsigned VR
) {
443 iterator L
= llvm::lower_bound(Seq
, VR
, Ord
);
449 unsigned S
= Seq
.size();
452 assert(Seq
.size() <= MaxSize
);
455 void OrderedRegisterList::remove(unsigned VR
) {
456 iterator L
= llvm::lower_bound(Seq
, VR
, Ord
);
463 // A record of the insert form. The fields correspond to the operands
464 // of the "insert" instruction:
465 // ... = insert(SrcR, InsR, #Wdh, #Off)
467 IFRecord(unsigned SR
= 0, unsigned IR
= 0, uint16_t W
= 0, uint16_t O
= 0)
468 : SrcR(SR
), InsR(IR
), Wdh(W
), Off(O
) {}
475 PrintIFR(const IFRecord
&R
, const TargetRegisterInfo
*RI
)
479 friend raw_ostream
&operator<< (raw_ostream
&OS
, const PrintIFR
&P
);
482 const TargetRegisterInfo
*TRI
;
485 raw_ostream
&operator<< (raw_ostream
&OS
, const PrintIFR
&P
) {
486 unsigned SrcR
= P
.IFR
.SrcR
, InsR
= P
.IFR
.InsR
;
487 OS
<< '(' << printReg(SrcR
, P
.TRI
) << ',' << printReg(InsR
, P
.TRI
)
488 << ",#" << P
.IFR
.Wdh
<< ",#" << P
.IFR
.Off
<< ')';
492 using IFRecordWithRegSet
= std::pair
<IFRecord
, RegisterSet
>;
494 } // end anonymous namespace
498 void initializeHexagonGenInsertPass(PassRegistry
&);
499 FunctionPass
*createHexagonGenInsert();
501 } // end namespace llvm
505 class HexagonGenInsert
: public MachineFunctionPass
{
509 HexagonGenInsert() : MachineFunctionPass(ID
) {
510 initializeHexagonGenInsertPass(*PassRegistry::getPassRegistry());
513 StringRef
getPassName() const override
{
514 return "Hexagon generate \"insert\" instructions";
517 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
518 AU
.addRequired
<MachineDominatorTreeWrapperPass
>();
519 AU
.addPreserved
<MachineDominatorTreeWrapperPass
>();
520 MachineFunctionPass::getAnalysisUsage(AU
);
523 bool runOnMachineFunction(MachineFunction
&MF
) override
;
526 using PairMapType
= DenseMap
<std::pair
<unsigned, unsigned>, unsigned>;
528 void buildOrderingMF(RegisterOrdering
&RO
) const;
529 void buildOrderingBT(RegisterOrdering
&RB
, RegisterOrdering
&RO
) const;
530 bool isIntClass(const TargetRegisterClass
*RC
) const;
531 bool isConstant(unsigned VR
) const;
532 bool isSmallConstant(unsigned VR
) const;
533 bool isValidInsertForm(unsigned DstR
, unsigned SrcR
, unsigned InsR
,
534 uint16_t L
, uint16_t S
) const;
535 bool findSelfReference(unsigned VR
) const;
536 bool findNonSelfReference(unsigned VR
) const;
537 void getInstrDefs(const MachineInstr
*MI
, RegisterSet
&Defs
) const;
538 void getInstrUses(const MachineInstr
*MI
, RegisterSet
&Uses
) const;
539 unsigned distance(const MachineBasicBlock
*FromB
,
540 const MachineBasicBlock
*ToB
, const UnsignedMap
&RPO
,
541 PairMapType
&M
) const;
542 unsigned distance(MachineBasicBlock::const_iterator FromI
,
543 MachineBasicBlock::const_iterator ToI
, const UnsignedMap
&RPO
,
544 PairMapType
&M
) const;
545 bool findRecordInsertForms(unsigned VR
, OrderedRegisterList
&AVs
);
546 void collectInBlock(MachineBasicBlock
*B
, OrderedRegisterList
&AVs
);
547 void findRemovableRegisters(unsigned VR
, IFRecord IF
,
548 RegisterSet
&RMs
) const;
549 void computeRemovableRegisters();
551 void pruneEmptyLists();
552 void pruneCoveredSets(unsigned VR
);
553 void pruneUsesTooFar(unsigned VR
, const UnsignedMap
&RPO
, PairMapType
&M
);
554 void pruneRegCopies(unsigned VR
);
555 void pruneCandidates();
556 void selectCandidates();
557 bool generateInserts();
559 bool removeDeadCode(MachineDomTreeNode
*N
);
561 // IFRecord coupled with a set of potentially removable registers:
562 using IFListType
= std::vector
<IFRecordWithRegSet
>;
563 using IFMapType
= DenseMap
<unsigned, IFListType
>; // vreg -> IFListType
565 void dump_map() const;
567 const HexagonInstrInfo
*HII
= nullptr;
568 const HexagonRegisterInfo
*HRI
= nullptr;
570 MachineFunction
*MFN
;
571 MachineRegisterInfo
*MRI
;
572 MachineDominatorTree
*MDT
;
575 RegisterOrdering BaseOrd
;
576 RegisterOrdering CellOrd
;
580 } // end anonymous namespace
582 char HexagonGenInsert::ID
= 0;
584 void HexagonGenInsert::dump_map() const {
585 for (const auto &I
: IFMap
) {
586 dbgs() << " " << printReg(I
.first
, HRI
) << ":\n";
587 const IFListType
&LL
= I
.second
;
588 for (const auto &J
: LL
)
589 dbgs() << " " << PrintIFR(J
.first
, HRI
) << ", "
590 << PrintRegSet(J
.second
, HRI
) << '\n';
594 void HexagonGenInsert::buildOrderingMF(RegisterOrdering
&RO
) const {
597 for (const MachineBasicBlock
&B
: *MFN
) {
598 if (!CMS
->BT
.reached(&B
))
601 for (const MachineInstr
&MI
: B
) {
602 for (const MachineOperand
&MO
: MI
.operands()) {
603 if (MO
.isReg() && MO
.isDef()) {
604 Register R
= MO
.getReg();
605 assert(MO
.getSubReg() == 0 && "Unexpected subregister in definition");
607 RO
.insert(std::make_pair(R
, Index
++));
612 // Since some virtual registers may have had their def and uses eliminated,
613 // they are no longer referenced in the code, and so they will not appear
617 void HexagonGenInsert::buildOrderingBT(RegisterOrdering
&RB
,
618 RegisterOrdering
&RO
) const {
619 // Create a vector of all virtual registers (collect them from the base
620 // ordering RB), and then sort it using the RegisterCell comparator.
621 BitValueOrdering
BVO(RB
);
622 RegisterCellLexCompare
LexCmp(BVO
, *CMS
);
624 using SortableVectorType
= std::vector
<unsigned>;
626 SortableVectorType VRs
;
628 VRs
.push_back(I
.first
);
629 llvm::sort(VRs
, LexCmp
);
630 // Transfer the results to the outgoing register ordering.
631 for (unsigned i
= 0, n
= VRs
.size(); i
< n
; ++i
)
632 RO
.insert(std::make_pair(VRs
[i
], i
));
635 inline bool HexagonGenInsert::isIntClass(const TargetRegisterClass
*RC
) const {
636 return RC
== &Hexagon::IntRegsRegClass
|| RC
== &Hexagon::DoubleRegsRegClass
;
639 bool HexagonGenInsert::isConstant(unsigned VR
) const {
640 const BitTracker::RegisterCell
&RC
= CMS
->lookup(VR
);
641 uint16_t W
= RC
.width();
642 for (uint16_t i
= 0; i
< W
; ++i
) {
643 const BitTracker::BitValue
&BV
= RC
[i
];
644 if (BV
.is(0) || BV
.is(1))
651 bool HexagonGenInsert::isSmallConstant(unsigned VR
) const {
652 const BitTracker::RegisterCell
&RC
= CMS
->lookup(VR
);
653 uint16_t W
= RC
.width();
656 uint64_t V
= 0, B
= 1;
657 for (uint16_t i
= 0; i
< W
; ++i
) {
658 const BitTracker::BitValue
&BV
= RC
[i
];
666 // For 32-bit registers, consider: Rd = #s16.
670 // For 64-bit registers, it's Rdd = #s8 or Rdd = combine(#s8,#s8)
671 return isInt
<8>(Lo_32(V
)) && isInt
<8>(Hi_32(V
));
674 bool HexagonGenInsert::isValidInsertForm(unsigned DstR
, unsigned SrcR
,
675 unsigned InsR
, uint16_t L
, uint16_t S
) const {
676 const TargetRegisterClass
*DstRC
= MRI
->getRegClass(DstR
);
677 const TargetRegisterClass
*SrcRC
= MRI
->getRegClass(SrcR
);
678 const TargetRegisterClass
*InsRC
= MRI
->getRegClass(InsR
);
679 // Only integet (32-/64-bit) register classes.
680 if (!isIntClass(DstRC
) || !isIntClass(SrcRC
) || !isIntClass(InsRC
))
682 // The "source" register must be of the same class as DstR.
687 // A 64-bit register can only be generated from other 64-bit registers.
688 if (DstRC
== &Hexagon::DoubleRegsRegClass
)
690 // Otherwise, the L and S cannot span 32-bit word boundary.
691 if (S
< 32 && S
+L
> 32)
696 bool HexagonGenInsert::findSelfReference(unsigned VR
) const {
697 const BitTracker::RegisterCell
&RC
= CMS
->lookup(VR
);
698 for (uint16_t i
= 0, w
= RC
.width(); i
< w
; ++i
) {
699 const BitTracker::BitValue
&V
= RC
[i
];
700 if (V
.Type
== BitTracker::BitValue::Ref
&& V
.RefI
.Reg
== VR
)
706 bool HexagonGenInsert::findNonSelfReference(unsigned VR
) const {
707 BitTracker::RegisterCell RC
= CMS
->lookup(VR
);
708 for (uint16_t i
= 0, w
= RC
.width(); i
< w
; ++i
) {
709 const BitTracker::BitValue
&V
= RC
[i
];
710 if (V
.Type
== BitTracker::BitValue::Ref
&& V
.RefI
.Reg
!= VR
)
716 void HexagonGenInsert::getInstrDefs(const MachineInstr
*MI
,
717 RegisterSet
&Defs
) const {
718 for (const MachineOperand
&MO
: MI
->operands()) {
719 if (!MO
.isReg() || !MO
.isDef())
721 Register R
= MO
.getReg();
728 void HexagonGenInsert::getInstrUses(const MachineInstr
*MI
,
729 RegisterSet
&Uses
) const {
730 for (const MachineOperand
&MO
: MI
->operands()) {
731 if (!MO
.isReg() || !MO
.isUse())
733 Register R
= MO
.getReg();
740 unsigned HexagonGenInsert::distance(const MachineBasicBlock
*FromB
,
741 const MachineBasicBlock
*ToB
, const UnsignedMap
&RPO
,
742 PairMapType
&M
) const {
743 // Forward distance from the end of a block to the beginning of it does
744 // not make sense. This function should not be called with FromB == ToB.
745 assert(FromB
!= ToB
);
747 unsigned FromN
= FromB
->getNumber(), ToN
= ToB
->getNumber();
748 // If we have already computed it, return the cached result.
749 PairMapType::iterator F
= M
.find(std::make_pair(FromN
, ToN
));
752 unsigned ToRPO
= RPO
.lookup(ToN
);
756 for (const MachineBasicBlock
*PB
: ToB
->predecessors()) {
757 // Skip back edges. Also, if FromB is a predecessor of ToB, the distance
758 // along that path will be 0, and we don't need to do any calculations
760 if (PB
== FromB
|| RPO
.lookup(PB
->getNumber()) >= ToRPO
)
762 unsigned D
= PB
->size() + distance(FromB
, PB
, RPO
, M
);
767 // Memoize the result for later lookup.
768 M
.insert(std::make_pair(std::make_pair(FromN
, ToN
), MaxD
));
772 unsigned HexagonGenInsert::distance(MachineBasicBlock::const_iterator FromI
,
773 MachineBasicBlock::const_iterator ToI
, const UnsignedMap
&RPO
,
774 PairMapType
&M
) const {
775 const MachineBasicBlock
*FB
= FromI
->getParent(), *TB
= ToI
->getParent();
777 return std::distance(FromI
, ToI
);
778 unsigned D1
= std::distance(TB
->begin(), ToI
);
779 unsigned D2
= distance(FB
, TB
, RPO
, M
);
780 unsigned D3
= std::distance(FromI
, FB
->end());
784 bool HexagonGenInsert::findRecordInsertForms(unsigned VR
,
785 OrderedRegisterList
&AVs
) {
787 dbgs() << __func__
<< ": " << printReg(VR
, HRI
)
788 << " AVs: " << PrintORL(AVs
, HRI
) << "\n";
793 using iterator
= OrderedRegisterList::iterator
;
795 BitValueOrdering
BVO(BaseOrd
);
796 const BitTracker::RegisterCell
&RC
= CMS
->lookup(VR
);
797 uint16_t W
= RC
.width();
799 using RSRecord
= std::pair
<unsigned, uint16_t>; // (reg,shift)
800 using RSListType
= std::vector
<RSRecord
>;
801 // Have a map, with key being the matching prefix length, and the value
802 // being the list of pairs (R,S), where R's prefix matches VR at S.
803 // (DenseMap<uint16_t,RSListType> fails to instantiate.)
804 using LRSMapType
= DenseMap
<unsigned, RSListType
>;
807 // Conceptually, rotate the cell RC right (i.e. towards the LSB) by S,
808 // and find matching prefixes from AVs with the rotated RC. Such a prefix
809 // would match a string of bits (of length L) in RC starting at S.
810 for (uint16_t S
= 0; S
< W
; ++S
) {
811 iterator B
= AVs
.begin(), E
= AVs
.end();
812 // The registers in AVs are ordered according to the lexical order of
813 // the corresponding register cells. This means that the range of regis-
814 // ters in AVs that match a prefix of length L+1 will be contained in
815 // the range that matches a prefix of length L. This means that we can
816 // keep narrowing the search space as the prefix length goes up. This
817 // helps reduce the overall complexity of the search.
819 for (L
= 0; L
< W
-S
; ++L
) {
820 // Compare against VR's bits starting at S, which emulates rotation
822 RegisterCellBitCompareSel
RCB(VR
, S
+L
, L
, BVO
, *CMS
);
823 iterator NewB
= std::lower_bound(B
, E
, VR
, RCB
);
824 iterator NewE
= std::upper_bound(NewB
, E
, VR
, RCB
);
825 // For the registers that are eliminated from the next range, L is
826 // the longest prefix matching VR at position S (their prefixes
827 // differ from VR at S+L). If L>0, record this information for later
830 for (iterator I
= B
; I
!= NewB
; ++I
)
831 LM
[L
].push_back(std::make_pair(*I
, S
));
832 for (iterator I
= NewE
; I
!= E
; ++I
)
833 LM
[L
].push_back(std::make_pair(*I
, S
));
839 // Record the final register range. If this range is non-empty, then
841 assert(B
== E
|| L
== W
-S
);
843 for (iterator I
= B
; I
!= E
; ++I
)
844 LM
[L
].push_back(std::make_pair(*I
, S
));
845 // If B!=E, then we found a range of registers whose prefixes cover the
846 // rest of VR from position S. There is no need to further advance S.
852 dbgs() << "Prefixes matching register " << printReg(VR
, HRI
) << "\n";
853 for (const auto &I
: LM
) {
854 dbgs() << " L=" << I
.first
<< ':';
855 const RSListType
&LL
= I
.second
;
856 for (const auto &J
: LL
)
857 dbgs() << " (" << printReg(J
.first
, HRI
) << ",@" << J
.second
<< ')';
862 bool Recorded
= false;
864 for (unsigned SrcR
: AVs
) {
865 int FDi
= -1, LDi
= -1; // First/last different bit.
866 const BitTracker::RegisterCell
&AC
= CMS
->lookup(SrcR
);
867 uint16_t AW
= AC
.width();
868 for (uint16_t i
= 0, w
= std::min(W
, AW
); i
< w
; ++i
) {
876 continue; // TODO (future): Record identical registers.
877 // Look for a register whose prefix could patch the range [FD..LD]
878 // where VR and SrcR differ.
879 uint16_t FD
= FDi
, LD
= LDi
; // Switch to unsigned type.
880 uint16_t MinL
= LD
-FD
+1;
881 for (uint16_t L
= MinL
; L
< W
; ++L
) {
882 LRSMapType::iterator F
= LM
.find(L
);
885 RSListType
&LL
= F
->second
;
886 for (const auto &I
: LL
) {
887 uint16_t S
= I
.second
;
888 // MinL is the minimum length of the prefix. Any length above MinL
889 // allows some flexibility as to where the prefix can start:
890 // given the extra length EL=L-MinL, the prefix must start between
891 // max(0,FD-EL) and FD.
892 if (S
> FD
) // Starts too late.
894 uint16_t EL
= L
-MinL
;
895 uint16_t LowS
= (EL
< FD
) ? FD
-EL
: 0;
896 if (S
< LowS
) // Starts too early.
898 unsigned InsR
= I
.first
;
899 if (!isValidInsertForm(VR
, SrcR
, InsR
, L
, S
))
902 dbgs() << printReg(VR
, HRI
) << " = insert(" << printReg(SrcR
, HRI
)
903 << ',' << printReg(InsR
, HRI
) << ",#" << L
<< ",#"
906 IFRecordWithRegSet
RR(IFRecord(SrcR
, InsR
, L
, S
), RegisterSet());
907 IFMap
[VR
].push_back(RR
);
916 void HexagonGenInsert::collectInBlock(MachineBasicBlock
*B
,
917 OrderedRegisterList
&AVs
) {
919 dbgs() << "visiting block " << printMBBReference(*B
) << "\n";
921 // First, check if this block is reachable at all. If not, the bit tracker
922 // will not have any information about registers in it.
923 if (!CMS
->BT
.reached(B
))
926 bool DoConst
= OptConst
;
927 // Keep a separate set of registers defined in this block, so that we
928 // can remove them from the list of available registers once all DT
929 // successors have been processed.
930 RegisterSet BlockDefs
, InsDefs
;
931 for (MachineInstr
&MI
: *B
) {
933 getInstrDefs(&MI
, InsDefs
);
934 // Leave those alone. They are more transparent than "insert".
935 bool Skip
= MI
.isCopy() || MI
.isRegSequence();
938 // Visit all defined registers, and attempt to find the corresponding
939 // "insert" representations.
940 for (unsigned VR
= InsDefs
.find_first(); VR
; VR
= InsDefs
.find_next(VR
)) {
941 // Do not collect registers that are known to be compile-time cons-
942 // tants, unless requested.
943 if (!DoConst
&& isConstant(VR
))
945 // If VR's cell contains a reference to VR, then VR cannot be defined
946 // via "insert". If VR is a constant that can be generated in a single
947 // instruction (without constant extenders), generating it via insert
949 if (findSelfReference(VR
) || isSmallConstant(VR
))
952 findRecordInsertForms(VR
, AVs
);
953 // Stop if the map size is too large.
954 if (IFMap
.size() > MaxIFMSize
)
959 // Insert the defined registers into the list of available registers
960 // after they have been processed.
961 for (unsigned VR
= InsDefs
.find_first(); VR
; VR
= InsDefs
.find_next(VR
))
963 BlockDefs
.insert(InsDefs
);
966 for (auto *DTN
: children
<MachineDomTreeNode
*>(MDT
->getNode(B
))) {
967 MachineBasicBlock
*SB
= DTN
->getBlock();
968 collectInBlock(SB
, AVs
);
971 for (unsigned VR
= BlockDefs
.find_first(); VR
; VR
= BlockDefs
.find_next(VR
))
975 void HexagonGenInsert::findRemovableRegisters(unsigned VR
, IFRecord IF
,
976 RegisterSet
&RMs
) const {
977 // For a given register VR and a insert form, find the registers that are
978 // used by the current definition of VR, and which would no longer be
979 // needed for it after the definition of VR is replaced with the insert
980 // form. These are the registers that could potentially become dead.
983 unsigned S
= 0; // Register set selector.
986 while (!Regs
[S
].empty()) {
987 // Breadth-first search.
988 unsigned OtherS
= 1-S
;
989 Regs
[OtherS
].clear();
990 for (unsigned R
= Regs
[S
].find_first(); R
; R
= Regs
[S
].find_next(R
)) {
992 if (R
== IF
.SrcR
|| R
== IF
.InsR
)
994 // Check if a given register has bits that are references to any other
995 // registers. This is to detect situations where the instruction that
996 // defines register R takes register Q as an operand, but R itself does
997 // not contain any bits from Q. Loads are examples of how this could
1000 // In this case (assuming we do not have any knowledge about the loaded
1001 // value), we must not treat R as a "conveyance" of the bits from Q.
1002 // (The information in BT about R's bits would have them as constants,
1003 // in case of zero-extending loads, or refs to R.)
1004 if (!findNonSelfReference(R
))
1007 const MachineInstr
*DefI
= MRI
->getVRegDef(R
);
1009 // Do not iterate past PHI nodes to avoid infinite loops. This can
1010 // make the final set a bit less accurate, but the removable register
1011 // sets are an approximation anyway.
1014 getInstrUses(DefI
, Regs
[OtherS
]);
1018 // The register VR is added to the list as a side-effect of the algorithm,
1019 // but it is not "potentially removable". A potentially removable register
1020 // is one that may become unused (dead) after conversion to the insert form
1021 // IF, and obviously VR (or its replacement) will not become dead by apply-
1026 void HexagonGenInsert::computeRemovableRegisters() {
1027 for (auto &I
: IFMap
) {
1028 IFListType
&LL
= I
.second
;
1030 findRemovableRegisters(I
.first
, J
.first
, J
.second
);
1034 void HexagonGenInsert::pruneEmptyLists() {
1035 // Remove all entries from the map, where the register has no insert forms
1036 // associated with it.
1037 using IterListType
= SmallVector
<IFMapType::iterator
, 16>;
1039 for (IFMapType::iterator I
= IFMap
.begin(), E
= IFMap
.end(); I
!= E
; ++I
) {
1040 if (I
->second
.empty())
1043 for (const auto &It
: Prune
)
1047 void HexagonGenInsert::pruneCoveredSets(unsigned VR
) {
1048 IFMapType::iterator F
= IFMap
.find(VR
);
1049 assert(F
!= IFMap
.end());
1050 IFListType
&LL
= F
->second
;
1052 // First, examine the IF candidates for register VR whose removable-regis-
1053 // ter sets are empty. This means that a given candidate will not help eli-
1054 // minate any registers, but since "insert" is not a constant-extendable
1055 // instruction, using such a candidate may reduce code size if the defini-
1056 // tion of VR is constant-extended.
1057 // If there exists a candidate with a non-empty set, the ones with empty
1058 // sets will not be used and can be removed.
1059 MachineInstr
*DefVR
= MRI
->getVRegDef(VR
);
1060 bool DefEx
= HII
->isConstExtended(*DefVR
);
1062 for (const auto &I
: LL
) {
1063 if (I
.second
.empty())
1068 if (!DefEx
|| HasNE
) {
1069 // The definition of VR is not constant-extended, or there is a candidate
1070 // with a non-empty set. Remove all candidates with empty sets.
1071 auto IsEmpty
= [] (const IFRecordWithRegSet
&IR
) -> bool {
1072 return IR
.second
.empty();
1074 llvm::erase_if(LL
, IsEmpty
);
1076 // The definition of VR is constant-extended, and all candidates have
1077 // empty removable-register sets. Pick the maximum candidate, and remove
1078 // all others. The "maximum" does not have any special meaning here, it
1079 // is only so that the candidate that will remain on the list is selec-
1080 // ted deterministically.
1081 IFRecord MaxIF
= LL
[0].first
;
1082 for (unsigned i
= 1, n
= LL
.size(); i
< n
; ++i
) {
1083 // If LL[MaxI] < LL[i], then MaxI = i.
1084 const IFRecord
&IF
= LL
[i
].first
;
1085 unsigned M0
= BaseOrd
[MaxIF
.SrcR
], M1
= BaseOrd
[MaxIF
.InsR
];
1086 unsigned R0
= BaseOrd
[IF
.SrcR
], R1
= BaseOrd
[IF
.InsR
];
1093 if (MaxIF
.Wdh
> IF
.Wdh
)
1095 if (MaxIF
.Wdh
== IF
.Wdh
&& MaxIF
.Off
>= IF
.Off
)
1102 // Remove everything except the maximum candidate. All register sets
1103 // are empty, so no need to preserve anything.
1105 LL
.push_back(std::make_pair(MaxIF
, RegisterSet()));
1108 // Now, remove those whose sets of potentially removable registers are
1109 // contained in another IF candidate for VR. For example, given these
1110 // candidates for %45,
1112 // (%44,%41,#9,#8), { %42 }
1113 // (%43,%41,#9,#8), { %42 %44 }
1114 // remove the first one, since it is contained in the second one.
1115 for (unsigned i
= 0, n
= LL
.size(); i
< n
; ) {
1116 const RegisterSet
&RMi
= LL
[i
].second
;
1119 if (j
!= i
&& LL
[j
].second
.includes(RMi
))
1123 if (j
== n
) { // RMi not contained in anything else.
1127 LL
.erase(LL
.begin()+i
);
1132 void HexagonGenInsert::pruneUsesTooFar(unsigned VR
, const UnsignedMap
&RPO
,
1134 IFMapType::iterator F
= IFMap
.find(VR
);
1135 assert(F
!= IFMap
.end());
1136 IFListType
&LL
= F
->second
;
1137 unsigned Cutoff
= VRegDistCutoff
;
1138 const MachineInstr
*DefV
= MRI
->getVRegDef(VR
);
1140 for (unsigned i
= LL
.size(); i
> 0; --i
) {
1141 unsigned SR
= LL
[i
-1].first
.SrcR
, IR
= LL
[i
-1].first
.InsR
;
1142 const MachineInstr
*DefS
= MRI
->getVRegDef(SR
);
1143 const MachineInstr
*DefI
= MRI
->getVRegDef(IR
);
1144 unsigned DSV
= distance(DefS
, DefV
, RPO
, M
);
1146 unsigned DIV
= distance(DefI
, DefV
, RPO
, M
);
1150 LL
.erase(LL
.begin()+(i
-1));
1154 void HexagonGenInsert::pruneRegCopies(unsigned VR
) {
1155 IFMapType::iterator F
= IFMap
.find(VR
);
1156 assert(F
!= IFMap
.end());
1157 IFListType
&LL
= F
->second
;
1159 auto IsCopy
= [] (const IFRecordWithRegSet
&IR
) -> bool {
1160 return IR
.first
.Wdh
== 32 && (IR
.first
.Off
== 0 || IR
.first
.Off
== 32);
1162 llvm::erase_if(LL
, IsCopy
);
1165 void HexagonGenInsert::pruneCandidates() {
1166 // Remove candidates that are not beneficial, regardless of the final
1167 // selection method.
1168 // First, remove candidates whose potentially removable set is a subset
1169 // of another candidate's set.
1170 for (const auto &I
: IFMap
)
1171 pruneCoveredSets(I
.first
);
1175 using RPOTType
= ReversePostOrderTraversal
<const MachineFunction
*>;
1179 for (const auto &I
: RPOT
)
1180 RPO
[I
->getNumber()] = RPON
++;
1182 PairMapType Memo
; // Memoization map for distance calculation.
1183 // Remove candidates that would use registers defined too far away.
1184 for (const auto &I
: IFMap
)
1185 pruneUsesTooFar(I
.first
, RPO
, Memo
);
1189 for (const auto &I
: IFMap
)
1190 pruneRegCopies(I
.first
);
1195 // Class for comparing IF candidates for registers that have multiple of
1196 // them. The smaller the candidate, according to this ordering, the better.
1197 // First, compare the number of zeros in the associated potentially remova-
1198 // ble register sets. "Zero" indicates that the register is very likely to
1199 // become dead after this transformation.
1200 // Second, compare "averages", i.e. use-count per size. The lower wins.
1201 // After that, it does not really matter which one is smaller. Resolve
1202 // the tie in some deterministic way.
1204 IFOrdering(const UnsignedMap
&UC
, const RegisterOrdering
&BO
)
1205 : UseC(UC
), BaseOrd(BO
) {}
1207 bool operator() (const IFRecordWithRegSet
&A
,
1208 const IFRecordWithRegSet
&B
) const;
1211 void stats(const RegisterSet
&Rs
, unsigned &Size
, unsigned &Zero
,
1212 unsigned &Sum
) const;
1214 const UnsignedMap
&UseC
;
1215 const RegisterOrdering
&BaseOrd
;
1218 } // end anonymous namespace
1220 bool IFOrdering::operator() (const IFRecordWithRegSet
&A
,
1221 const IFRecordWithRegSet
&B
) const {
1222 unsigned SizeA
= 0, ZeroA
= 0, SumA
= 0;
1223 unsigned SizeB
= 0, ZeroB
= 0, SumB
= 0;
1224 stats(A
.second
, SizeA
, ZeroA
, SumA
);
1225 stats(B
.second
, SizeB
, ZeroB
, SumB
);
1227 // We will pick the minimum element. The more zeros, the better.
1229 return ZeroA
> ZeroB
;
1230 // Compare SumA/SizeA with SumB/SizeB, lower is better.
1231 uint64_t AvgA
= SumA
*SizeB
, AvgB
= SumB
*SizeA
;
1235 // The sets compare identical so far. Resort to comparing the IF records.
1236 // The actual values don't matter, this is only for determinism.
1237 unsigned OSA
= BaseOrd
[A
.first
.SrcR
], OSB
= BaseOrd
[B
.first
.SrcR
];
1240 unsigned OIA
= BaseOrd
[A
.first
.InsR
], OIB
= BaseOrd
[B
.first
.InsR
];
1243 if (A
.first
.Wdh
!= B
.first
.Wdh
)
1244 return A
.first
.Wdh
< B
.first
.Wdh
;
1245 return A
.first
.Off
< B
.first
.Off
;
1248 void IFOrdering::stats(const RegisterSet
&Rs
, unsigned &Size
, unsigned &Zero
,
1249 unsigned &Sum
) const {
1250 for (unsigned R
= Rs
.find_first(); R
; R
= Rs
.find_next(R
)) {
1251 UnsignedMap::const_iterator F
= UseC
.find(R
);
1252 assert(F
!= UseC
.end());
1253 unsigned UC
= F
->second
;
1261 void HexagonGenInsert::selectCandidates() {
1262 // Some registers may have multiple valid candidates. Pick the best one
1263 // (or decide not to use any).
1265 // Compute the "removability" measure of R:
1266 // For each potentially removable register R, record the number of regis-
1267 // ters with IF candidates, where R appears in at least one set.
1269 UnsignedMap UseC
, RemC
;
1270 IFMapType::iterator End
= IFMap
.end();
1272 for (IFMapType::iterator I
= IFMap
.begin(); I
!= End
; ++I
) {
1273 const IFListType
&LL
= I
->second
;
1275 for (const auto &J
: LL
)
1276 TT
.insert(J
.second
);
1277 for (unsigned R
= TT
.find_first(); R
; R
= TT
.find_next(R
))
1282 for (unsigned R
= AllRMs
.find_first(); R
; R
= AllRMs
.find_next(R
)) {
1283 using use_iterator
= MachineRegisterInfo::use_nodbg_iterator
;
1284 using InstrSet
= SmallSet
<const MachineInstr
*, 16>;
1287 // Count as the number of instructions in which R is used, not the
1288 // number of operands.
1289 use_iterator E
= MRI
->use_nodbg_end();
1290 for (use_iterator I
= MRI
->use_nodbg_begin(R
); I
!= E
; ++I
)
1291 UIs
.insert(I
->getParent());
1292 unsigned C
= UIs
.size();
1293 // Calculate a measure, which is the number of instructions using R,
1294 // minus the "removability" count computed earlier.
1295 unsigned D
= RemC
[R
];
1296 UseC
[R
] = (C
> D
) ? C
-D
: 0; // doz
1299 bool SelectAll0
= OptSelectAll0
, SelectHas0
= OptSelectHas0
;
1300 if (!SelectAll0
&& !SelectHas0
)
1303 // The smaller the number UseC for a given register R, the "less used"
1304 // R is aside from the opportunities for removal offered by generating
1305 // "insert" instructions.
1306 // Iterate over the IF map, and for those registers that have multiple
1307 // candidates, pick the minimum one according to IFOrdering.
1308 IFOrdering
IFO(UseC
, BaseOrd
);
1309 for (IFMapType::iterator I
= IFMap
.begin(); I
!= End
; ++I
) {
1310 IFListType
&LL
= I
->second
;
1313 // Get the minimum element, remember it and clear the list. If the
1314 // element found is adequate, we will put it back on the list, other-
1315 // wise the list will remain empty, and the entry for this register
1316 // will be removed (i.e. this register will not be replaced by insert).
1317 IFListType::iterator MinI
= llvm::min_element(LL
, IFO
);
1318 assert(MinI
!= LL
.end());
1319 IFRecordWithRegSet M
= *MinI
;
1322 // We want to make sure that this replacement will have a chance to be
1323 // beneficial, and that means that we want to have indication that some
1324 // register will be removed. The most likely registers to be eliminated
1325 // are the use operands in the definition of I->first. Accept/reject a
1326 // candidate based on how many of its uses it can potentially eliminate.
1329 const MachineInstr
*DefI
= MRI
->getVRegDef(I
->first
);
1330 getInstrUses(DefI
, Us
);
1331 bool Accept
= false;
1335 for (unsigned R
= Us
.find_first(); R
; R
= Us
.find_next(R
)) {
1342 } else if (SelectHas0
) {
1344 for (unsigned R
= Us
.find_first(); R
; R
= Us
.find_next(R
)) {
1356 // Remove candidates that add uses of removable registers, unless the
1357 // removable registers are among replacement candidates.
1358 // Recompute the removable registers, since some candidates may have
1361 for (IFMapType::iterator I
= IFMap
.begin(); I
!= End
; ++I
) {
1362 const IFListType
&LL
= I
->second
;
1364 AllRMs
.insert(LL
[0].second
);
1366 for (IFMapType::iterator I
= IFMap
.begin(); I
!= End
; ++I
) {
1367 IFListType
&LL
= I
->second
;
1370 unsigned SR
= LL
[0].first
.SrcR
, IR
= LL
[0].first
.InsR
;
1371 if (AllRMs
[SR
] || AllRMs
[IR
])
1378 bool HexagonGenInsert::generateInserts() {
1379 // Create a new register for each one from IFMap, and store them in the
1382 for (auto &I
: IFMap
) {
1383 unsigned VR
= I
.first
;
1384 const TargetRegisterClass
*RC
= MRI
->getRegClass(VR
);
1385 Register NewVR
= MRI
->createVirtualRegister(RC
);
1389 // We can generate the "insert" instructions using potentially stale re-
1390 // gisters: SrcR and InsR for a given VR may be among other registers that
1391 // are also replaced. This is fine, we will do the mass "rauw" a bit later.
1392 for (auto &I
: IFMap
) {
1393 MachineInstr
*MI
= MRI
->getVRegDef(I
.first
);
1394 MachineBasicBlock
&B
= *MI
->getParent();
1395 DebugLoc DL
= MI
->getDebugLoc();
1396 unsigned NewR
= RegMap
[I
.first
];
1397 bool R32
= MRI
->getRegClass(NewR
) == &Hexagon::IntRegsRegClass
;
1398 const MCInstrDesc
&D
= R32
? HII
->get(Hexagon::S2_insert
)
1399 : HII
->get(Hexagon::S2_insertp
);
1400 IFRecord IF
= I
.second
[0].first
;
1401 unsigned Wdh
= IF
.Wdh
, Off
= IF
.Off
;
1403 if (R32
&& MRI
->getRegClass(IF
.InsR
) == &Hexagon::DoubleRegsRegClass
) {
1404 InsS
= Hexagon::isub_lo
;
1406 InsS
= Hexagon::isub_hi
;
1410 // Advance to the proper location for inserting instructions. This could
1412 MachineBasicBlock::iterator At
= MI
;
1414 At
= B
.getFirstNonPHI();
1416 BuildMI(B
, At
, DL
, D
, NewR
)
1418 .addReg(IF
.InsR
, 0, InsS
)
1422 MRI
->clearKillFlags(IF
.SrcR
);
1423 MRI
->clearKillFlags(IF
.InsR
);
1426 for (const auto &I
: IFMap
) {
1427 MachineInstr
*DefI
= MRI
->getVRegDef(I
.first
);
1428 MRI
->replaceRegWith(I
.first
, RegMap
[I
.first
]);
1429 DefI
->eraseFromParent();
1435 bool HexagonGenInsert::removeDeadCode(MachineDomTreeNode
*N
) {
1436 bool Changed
= false;
1438 for (auto *DTN
: children
<MachineDomTreeNode
*>(N
))
1439 Changed
|= removeDeadCode(DTN
);
1441 MachineBasicBlock
*B
= N
->getBlock();
1442 std::vector
<MachineInstr
*> Instrs
;
1443 for (MachineInstr
&MI
: llvm::reverse(*B
))
1444 Instrs
.push_back(&MI
);
1446 for (MachineInstr
*MI
: Instrs
) {
1447 unsigned Opc
= MI
->getOpcode();
1448 // Do not touch lifetime markers. This is why the target-independent DCE
1450 if (Opc
== TargetOpcode::LIFETIME_START
||
1451 Opc
== TargetOpcode::LIFETIME_END
)
1454 if (MI
->isInlineAsm() || !MI
->isSafeToMove(Store
))
1457 bool AllDead
= true;
1458 SmallVector
<unsigned,2> Regs
;
1459 for (const MachineOperand
&MO
: MI
->operands()) {
1460 if (!MO
.isReg() || !MO
.isDef())
1462 Register R
= MO
.getReg();
1463 if (!R
.isVirtual() || !MRI
->use_nodbg_empty(R
)) {
1473 for (unsigned Reg
: Regs
)
1474 MRI
->markUsesInDebugValueAsUndef(Reg
);
1481 bool HexagonGenInsert::runOnMachineFunction(MachineFunction
&MF
) {
1482 if (skipFunction(MF
.getFunction()))
1485 bool Timing
= OptTiming
, TimingDetail
= Timing
&& OptTimingDetail
;
1486 bool Changed
= false;
1488 // Verify: one, but not both.
1489 assert(!OptSelectAll0
|| !OptSelectHas0
);
1495 const auto &ST
= MF
.getSubtarget
<HexagonSubtarget
>();
1496 HII
= ST
.getInstrInfo();
1497 HRI
= ST
.getRegisterInfo();
1499 MRI
= &MF
.getRegInfo();
1500 MDT
= &getAnalysis
<MachineDominatorTreeWrapperPass
>().getDomTree();
1502 // Clean up before any further processing, so that dead code does not
1503 // get used in a newly generated "insert" instruction. Have a custom
1504 // version of DCE that preserves lifetime markers. Without it, merging
1505 // of stack objects can fail to recognize and merge disjoint objects
1506 // leading to unnecessary stack growth.
1507 Changed
= removeDeadCode(MDT
->getRootNode());
1509 const HexagonEvaluator
HE(*HRI
, *MRI
, *HII
, MF
);
1510 BitTracker
BTLoc(HE
, MF
);
1511 BTLoc
.trace(isDebug());
1513 CellMapShadow
MS(BTLoc
);
1516 buildOrderingMF(BaseOrd
);
1517 buildOrderingBT(BaseOrd
, CellOrd
);
1520 dbgs() << "Cell ordering:\n";
1521 for (const auto &I
: CellOrd
) {
1522 unsigned VR
= I
.first
, Pos
= I
.second
;
1523 dbgs() << printReg(VR
, HRI
) << " -> " << Pos
<< "\n";
1527 // Collect candidates for conversion into the insert forms.
1528 MachineBasicBlock
*RootB
= MDT
->getRoot();
1529 OrderedRegisterList
AvailR(CellOrd
);
1531 const char *const TGName
= "hexinsert";
1532 const char *const TGDesc
= "Generate Insert Instructions";
1535 NamedRegionTimer
_T("collection", "collection", TGName
, TGDesc
,
1537 collectInBlock(RootB
, AvailR
);
1538 // Complete the information gathered in IFMap.
1539 computeRemovableRegisters();
1543 dbgs() << "Candidates after collection:\n";
1551 NamedRegionTimer
_T("pruning", "pruning", TGName
, TGDesc
, TimingDetail
);
1556 dbgs() << "Candidates after pruning:\n";
1564 NamedRegionTimer
_T("selection", "selection", TGName
, TGDesc
, TimingDetail
);
1569 dbgs() << "Candidates after selection:\n";
1573 // Filter out vregs beyond the cutoff.
1574 if (VRegIndexCutoff
.getPosition()) {
1575 unsigned Cutoff
= VRegIndexCutoff
;
1577 using IterListType
= SmallVector
<IFMapType::iterator
, 16>;
1580 for (IFMapType::iterator I
= IFMap
.begin(), E
= IFMap
.end(); I
!= E
; ++I
) {
1581 unsigned Idx
= Register::virtReg2Index(I
->first
);
1585 for (const auto &It
: Out
)
1592 NamedRegionTimer
_T("generation", "generation", TGName
, TGDesc
,
1600 FunctionPass
*llvm::createHexagonGenInsert() {
1601 return new HexagonGenInsert();
1604 //===----------------------------------------------------------------------===//
1605 // Public Constructor Functions
1606 //===----------------------------------------------------------------------===//
1608 INITIALIZE_PASS_BEGIN(HexagonGenInsert
, "hexinsert",
1609 "Hexagon generate \"insert\" instructions", false, false)
1610 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass
)
1611 INITIALIZE_PASS_END(HexagonGenInsert
, "hexinsert",
1612 "Hexagon generate \"insert\" instructions", false, false)