xref: /llvm-project/llvm/lib/Target/Hexagon/HexagonSplitDouble.cpp (revision 8c209aa8779de57771b64896f805e14cc0016dcd)
1 //===--- HexagonSplitDouble.cpp -------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #define DEBUG_TYPE "hsdr"
11 
12 #include "HexagonInstrInfo.h"
13 #include "HexagonRegisterInfo.h"
14 #include "HexagonSubtarget.h"
15 #include "llvm/ADT/BitVector.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineInstr.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineLoopInfo.h"
25 #include "llvm/CodeGen/MachineMemOperand.h"
26 #include "llvm/CodeGen/MachineOperand.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/IR/DebugLoc.h"
29 #include "llvm/Pass.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Target/TargetRegisterInfo.h"
36 #include <algorithm>
37 #include <cassert>
38 #include <cstdint>
39 #include <limits>
40 #include <map>
41 #include <set>
42 #include <utility>
43 #include <vector>
44 
45 using namespace llvm;
46 
47 namespace llvm {
48 
49   FunctionPass *createHexagonSplitDoubleRegs();
50   void initializeHexagonSplitDoubleRegsPass(PassRegistry&);
51 
52 } // end namespace llvm
53 
54 namespace {
55 
56   static cl::opt<int> MaxHSDR("max-hsdr", cl::Hidden, cl::init(-1),
57       cl::desc("Maximum number of split partitions"));
58   static cl::opt<bool> MemRefsFixed("hsdr-no-mem", cl::Hidden, cl::init(true),
59       cl::desc("Do not split loads or stores"));
60 
61   class HexagonSplitDoubleRegs : public MachineFunctionPass {
62   public:
63     static char ID;
64 
65     HexagonSplitDoubleRegs() : MachineFunctionPass(ID), TRI(nullptr),
66         TII(nullptr) {
67       initializeHexagonSplitDoubleRegsPass(*PassRegistry::getPassRegistry());
68     }
69 
70     StringRef getPassName() const override {
71       return "Hexagon Split Double Registers";
72     }
73 
74     void getAnalysisUsage(AnalysisUsage &AU) const override {
75       AU.addRequired<MachineLoopInfo>();
76       AU.addPreserved<MachineLoopInfo>();
77       MachineFunctionPass::getAnalysisUsage(AU);
78     }
79 
80     bool runOnMachineFunction(MachineFunction &MF) override;
81 
82   private:
83     static const TargetRegisterClass *const DoubleRC;
84 
85     const HexagonRegisterInfo *TRI;
86     const HexagonInstrInfo *TII;
87     const MachineLoopInfo *MLI;
88     MachineRegisterInfo *MRI;
89 
90     typedef std::set<unsigned> USet;
91     typedef std::map<unsigned,USet> UUSetMap;
92     typedef std::pair<unsigned,unsigned> UUPair;
93     typedef std::map<unsigned,UUPair> UUPairMap;
94     typedef std::map<const MachineLoop*,USet> LoopRegMap;
95 
96     bool isInduction(unsigned Reg, LoopRegMap &IRM) const;
97     bool isVolatileInstr(const MachineInstr *MI) const;
98     bool isFixedInstr(const MachineInstr *MI) const;
99     void partitionRegisters(UUSetMap &P2Rs);
100     int32_t profit(const MachineInstr *MI) const;
101     bool isProfitable(const USet &Part, LoopRegMap &IRM) const;
102 
103     void collectIndRegsForLoop(const MachineLoop *L, USet &Rs);
104     void collectIndRegs(LoopRegMap &IRM);
105 
106     void createHalfInstr(unsigned Opc, MachineInstr *MI,
107         const UUPairMap &PairMap, unsigned SubR);
108     void splitMemRef(MachineInstr *MI, const UUPairMap &PairMap);
109     void splitImmediate(MachineInstr *MI, const UUPairMap &PairMap);
110     void splitCombine(MachineInstr *MI, const UUPairMap &PairMap);
111     void splitExt(MachineInstr *MI, const UUPairMap &PairMap);
112     void splitShift(MachineInstr *MI, const UUPairMap &PairMap);
113     void splitAslOr(MachineInstr *MI, const UUPairMap &PairMap);
114     bool splitInstr(MachineInstr *MI, const UUPairMap &PairMap);
115     void replaceSubregUses(MachineInstr *MI, const UUPairMap &PairMap);
116     void collapseRegPairs(MachineInstr *MI, const UUPairMap &PairMap);
117     bool splitPartition(const USet &Part);
118 
119     static int Counter;
120     static void dump_partition(raw_ostream&, const USet&,
121        const TargetRegisterInfo&);
122   };
123 
124   char HexagonSplitDoubleRegs::ID;
125   int HexagonSplitDoubleRegs::Counter = 0;
126   const TargetRegisterClass *const HexagonSplitDoubleRegs::DoubleRC
127       = &Hexagon::DoubleRegsRegClass;
128 
129 } // end anonymous namespace
130 
131 INITIALIZE_PASS(HexagonSplitDoubleRegs, "hexagon-split-double",
132   "Hexagon Split Double Registers", false, false)
133 
134 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
135 LLVM_DUMP_METHOD void HexagonSplitDoubleRegs::dump_partition(raw_ostream &os,
136       const USet &Part, const TargetRegisterInfo &TRI) {
137   dbgs() << '{';
138   for (auto I : Part)
139     dbgs() << ' ' << PrintReg(I, &TRI);
140   dbgs() << " }";
141 }
142 #endif
143 
144 bool HexagonSplitDoubleRegs::isInduction(unsigned Reg, LoopRegMap &IRM) const {
145   for (auto I : IRM) {
146     const USet &Rs = I.second;
147     if (Rs.find(Reg) != Rs.end())
148       return true;
149   }
150   return false;
151 }
152 
153 bool HexagonSplitDoubleRegs::isVolatileInstr(const MachineInstr *MI) const {
154   for (auto &I : MI->memoperands())
155     if (I->isVolatile())
156       return true;
157   return false;
158 }
159 
160 bool HexagonSplitDoubleRegs::isFixedInstr(const MachineInstr *MI) const {
161   if (MI->mayLoad() || MI->mayStore())
162     if (MemRefsFixed || isVolatileInstr(MI))
163       return true;
164   if (MI->isDebugValue())
165     return false;
166 
167   unsigned Opc = MI->getOpcode();
168   switch (Opc) {
169     default:
170       return true;
171 
172     case TargetOpcode::PHI:
173     case TargetOpcode::COPY:
174       break;
175 
176     case Hexagon::L2_loadrd_io:
177       // Not handling stack stores (only reg-based addresses).
178       if (MI->getOperand(1).isReg())
179         break;
180       return true;
181     case Hexagon::S2_storerd_io:
182       // Not handling stack stores (only reg-based addresses).
183       if (MI->getOperand(0).isReg())
184         break;
185       return true;
186     case Hexagon::L2_loadrd_pi:
187     case Hexagon::S2_storerd_pi:
188 
189     case Hexagon::A2_tfrpi:
190     case Hexagon::A2_combineii:
191     case Hexagon::A4_combineir:
192     case Hexagon::A4_combineii:
193     case Hexagon::A4_combineri:
194     case Hexagon::A2_combinew:
195     case Hexagon::CONST64:
196 
197     case Hexagon::A2_sxtw:
198 
199     case Hexagon::A2_andp:
200     case Hexagon::A2_orp:
201     case Hexagon::A2_xorp:
202     case Hexagon::S2_asl_i_p_or:
203     case Hexagon::S2_asl_i_p:
204     case Hexagon::S2_asr_i_p:
205     case Hexagon::S2_lsr_i_p:
206       break;
207   }
208 
209   for (auto &Op : MI->operands()) {
210     if (!Op.isReg())
211       continue;
212     unsigned R = Op.getReg();
213     if (!TargetRegisterInfo::isVirtualRegister(R))
214       return true;
215   }
216   return false;
217 }
218 
219 void HexagonSplitDoubleRegs::partitionRegisters(UUSetMap &P2Rs) {
220   typedef std::map<unsigned,unsigned> UUMap;
221   typedef std::vector<unsigned> UVect;
222 
223   unsigned NumRegs = MRI->getNumVirtRegs();
224   BitVector DoubleRegs(NumRegs);
225   for (unsigned i = 0; i < NumRegs; ++i) {
226     unsigned R = TargetRegisterInfo::index2VirtReg(i);
227     if (MRI->getRegClass(R) == DoubleRC)
228       DoubleRegs.set(i);
229   }
230 
231   BitVector FixedRegs(NumRegs);
232   for (int x = DoubleRegs.find_first(); x >= 0; x = DoubleRegs.find_next(x)) {
233     unsigned R = TargetRegisterInfo::index2VirtReg(x);
234     MachineInstr *DefI = MRI->getVRegDef(R);
235     // In some cases a register may exist, but never be defined or used.
236     // It should never appear anywhere, but mark it as "fixed", just to be
237     // safe.
238     if (!DefI || isFixedInstr(DefI))
239       FixedRegs.set(x);
240   }
241 
242   UUSetMap AssocMap;
243   for (int x = DoubleRegs.find_first(); x >= 0; x = DoubleRegs.find_next(x)) {
244     if (FixedRegs[x])
245       continue;
246     unsigned R = TargetRegisterInfo::index2VirtReg(x);
247     DEBUG(dbgs() << PrintReg(R, TRI) << " ~~");
248     USet &Asc = AssocMap[R];
249     for (auto U = MRI->use_nodbg_begin(R), Z = MRI->use_nodbg_end();
250          U != Z; ++U) {
251       MachineOperand &Op = *U;
252       MachineInstr *UseI = Op.getParent();
253       if (isFixedInstr(UseI))
254         continue;
255       for (unsigned i = 0, n = UseI->getNumOperands(); i < n; ++i) {
256         MachineOperand &MO = UseI->getOperand(i);
257         // Skip non-registers or registers with subregisters.
258         if (&MO == &Op || !MO.isReg() || MO.getSubReg())
259           continue;
260         unsigned T = MO.getReg();
261         if (!TargetRegisterInfo::isVirtualRegister(T)) {
262           FixedRegs.set(x);
263           continue;
264         }
265         if (MRI->getRegClass(T) != DoubleRC)
266           continue;
267         unsigned u = TargetRegisterInfo::virtReg2Index(T);
268         if (FixedRegs[u])
269           continue;
270         DEBUG(dbgs() << ' ' << PrintReg(T, TRI));
271         Asc.insert(T);
272         // Make it symmetric.
273         AssocMap[T].insert(R);
274       }
275     }
276     DEBUG(dbgs() << '\n');
277   }
278 
279   UUMap R2P;
280   unsigned NextP = 1;
281   USet Visited;
282   for (int x = DoubleRegs.find_first(); x >= 0; x = DoubleRegs.find_next(x)) {
283     unsigned R = TargetRegisterInfo::index2VirtReg(x);
284     if (Visited.count(R))
285       continue;
286     // Create a new partition for R.
287     unsigned ThisP = FixedRegs[x] ? 0 : NextP++;
288     UVect WorkQ;
289     WorkQ.push_back(R);
290     for (unsigned i = 0; i < WorkQ.size(); ++i) {
291       unsigned T = WorkQ[i];
292       if (Visited.count(T))
293         continue;
294       R2P[T] = ThisP;
295       Visited.insert(T);
296       // Add all registers associated with T.
297       USet &Asc = AssocMap[T];
298       for (USet::iterator J = Asc.begin(), F = Asc.end(); J != F; ++J)
299         WorkQ.push_back(*J);
300     }
301   }
302 
303   for (auto I : R2P)
304     P2Rs[I.second].insert(I.first);
305 }
306 
307 static inline int32_t profitImm(unsigned Lo, unsigned Hi) {
308   int32_t P = 0;
309   bool LoZ1 = false, HiZ1 = false;
310   if (Lo == 0 || Lo == 0xFFFFFFFF)
311     P += 10, LoZ1 = true;
312   if (Hi == 0 || Hi == 0xFFFFFFFF)
313     P += 10, HiZ1 = true;
314   if (!LoZ1 && !HiZ1 && Lo == Hi)
315     P += 3;
316   return P;
317 }
318 
319 int32_t HexagonSplitDoubleRegs::profit(const MachineInstr *MI) const {
320   unsigned ImmX = 0;
321   unsigned Opc = MI->getOpcode();
322   switch (Opc) {
323     case TargetOpcode::PHI:
324       for (const auto &Op : MI->operands())
325         if (!Op.getSubReg())
326           return 0;
327       return 10;
328     case TargetOpcode::COPY:
329       if (MI->getOperand(1).getSubReg() != 0)
330         return 10;
331       return 0;
332 
333     case Hexagon::L2_loadrd_io:
334     case Hexagon::S2_storerd_io:
335       return -1;
336     case Hexagon::L2_loadrd_pi:
337     case Hexagon::S2_storerd_pi:
338       return 2;
339 
340     case Hexagon::A2_tfrpi:
341     case Hexagon::CONST64: {
342       uint64_t D = MI->getOperand(1).getImm();
343       unsigned Lo = D & 0xFFFFFFFFULL;
344       unsigned Hi = D >> 32;
345       return profitImm(Lo, Hi);
346     }
347     case Hexagon::A2_combineii:
348     case Hexagon::A4_combineii:
349       return profitImm(MI->getOperand(1).getImm(),
350                        MI->getOperand(2).getImm());
351     case Hexagon::A4_combineri:
352       ImmX++;
353     case Hexagon::A4_combineir: {
354       ImmX++;
355       int64_t V = MI->getOperand(ImmX).getImm();
356       if (V == 0 || V == -1)
357         return 10;
358       // Fall through into A2_combinew.
359       LLVM_FALLTHROUGH;
360     }
361     case Hexagon::A2_combinew:
362       return 2;
363 
364     case Hexagon::A2_sxtw:
365       return 3;
366 
367     case Hexagon::A2_andp:
368     case Hexagon::A2_orp:
369     case Hexagon::A2_xorp:
370       return 1;
371 
372     case Hexagon::S2_asl_i_p_or: {
373       unsigned S = MI->getOperand(3).getImm();
374       if (S == 0 || S == 32)
375         return 10;
376       return -1;
377     }
378     case Hexagon::S2_asl_i_p:
379     case Hexagon::S2_asr_i_p:
380     case Hexagon::S2_lsr_i_p:
381       unsigned S = MI->getOperand(2).getImm();
382       if (S == 0 || S == 32)
383         return 10;
384       if (S == 16)
385         return 5;
386       if (S == 48)
387         return 7;
388       return -10;
389   }
390 
391   return 0;
392 }
393 
394 bool HexagonSplitDoubleRegs::isProfitable(const USet &Part, LoopRegMap &IRM)
395       const {
396   unsigned FixedNum = 0, SplitNum = 0, LoopPhiNum = 0;
397   int32_t TotalP = 0;
398 
399   for (unsigned DR : Part) {
400     MachineInstr *DefI = MRI->getVRegDef(DR);
401     int32_t P = profit(DefI);
402     if (P == std::numeric_limits<int>::min())
403       return false;
404     TotalP += P;
405     // Reduce the profitability of splitting induction registers.
406     if (isInduction(DR, IRM))
407       TotalP -= 30;
408 
409     for (auto U = MRI->use_nodbg_begin(DR), W = MRI->use_nodbg_end();
410          U != W; ++U) {
411       MachineInstr *UseI = U->getParent();
412       if (isFixedInstr(UseI)) {
413         FixedNum++;
414         // Calculate the cost of generating REG_SEQUENCE instructions.
415         for (auto &Op : UseI->operands()) {
416           if (Op.isReg() && Part.count(Op.getReg()))
417             if (Op.getSubReg())
418               TotalP -= 2;
419         }
420         continue;
421       }
422       // If a register from this partition is used in a fixed instruction,
423       // and there is also a register in this partition that is used in
424       // a loop phi node, then decrease the splitting profit as this can
425       // confuse the modulo scheduler.
426       if (UseI->isPHI()) {
427         const MachineBasicBlock *PB = UseI->getParent();
428         const MachineLoop *L = MLI->getLoopFor(PB);
429         if (L && L->getHeader() == PB)
430           LoopPhiNum++;
431       }
432       // Splittable instruction.
433       SplitNum++;
434       int32_t P = profit(UseI);
435       if (P == std::numeric_limits<int>::min())
436         return false;
437       TotalP += P;
438     }
439   }
440 
441   if (FixedNum > 0 && LoopPhiNum > 0)
442     TotalP -= 20*LoopPhiNum;
443 
444   DEBUG(dbgs() << "Partition profit: " << TotalP << '\n');
445   return TotalP > 0;
446 }
447 
448 void HexagonSplitDoubleRegs::collectIndRegsForLoop(const MachineLoop *L,
449       USet &Rs) {
450   const MachineBasicBlock *HB = L->getHeader();
451   const MachineBasicBlock *LB = L->getLoopLatch();
452   if (!HB || !LB)
453     return;
454 
455   // Examine the latch branch. Expect it to be a conditional branch to
456   // the header (either "br-cond header" or "br-cond exit; br header").
457   MachineBasicBlock *TB = nullptr, *FB = nullptr;
458   MachineBasicBlock *TmpLB = const_cast<MachineBasicBlock*>(LB);
459   SmallVector<MachineOperand,2> Cond;
460   bool BadLB = TII->analyzeBranch(*TmpLB, TB, FB, Cond, false);
461   // Only analyzable conditional branches. HII::analyzeBranch will put
462   // the branch opcode as the first element of Cond, and the predicate
463   // operand as the second.
464   if (BadLB || Cond.size() != 2)
465     return;
466   // Only simple jump-conditional (with or without negation).
467   if (!TII->PredOpcodeHasJMP_c(Cond[0].getImm()))
468     return;
469   // Must go to the header.
470   if (TB != HB && FB != HB)
471     return;
472   assert(Cond[1].isReg() && "Unexpected Cond vector from analyzeBranch");
473   // Expect a predicate register.
474   unsigned PR = Cond[1].getReg();
475   assert(MRI->getRegClass(PR) == &Hexagon::PredRegsRegClass);
476 
477   // Get the registers on which the loop controlling compare instruction
478   // depends.
479   unsigned CmpR1 = 0, CmpR2 = 0;
480   const MachineInstr *CmpI = MRI->getVRegDef(PR);
481   while (CmpI->getOpcode() == Hexagon::C2_not)
482     CmpI = MRI->getVRegDef(CmpI->getOperand(1).getReg());
483 
484   int Mask = 0, Val = 0;
485   bool OkCI = TII->analyzeCompare(*CmpI, CmpR1, CmpR2, Mask, Val);
486   if (!OkCI)
487     return;
488   // Eliminate non-double input registers.
489   if (CmpR1 && MRI->getRegClass(CmpR1) != DoubleRC)
490     CmpR1 = 0;
491   if (CmpR2 && MRI->getRegClass(CmpR2) != DoubleRC)
492     CmpR2 = 0;
493   if (!CmpR1 && !CmpR2)
494     return;
495 
496   // Now examine the top of the loop: the phi nodes that could poten-
497   // tially define loop induction registers. The registers defined by
498   // such a phi node would be used in a 64-bit add, which then would
499   // be used in the loop compare instruction.
500 
501   // Get the set of all double registers defined by phi nodes in the
502   // loop header.
503   typedef std::vector<unsigned> UVect;
504   UVect DP;
505   for (auto &MI : *HB) {
506     if (!MI.isPHI())
507       break;
508     const MachineOperand &MD = MI.getOperand(0);
509     unsigned R = MD.getReg();
510     if (MRI->getRegClass(R) == DoubleRC)
511       DP.push_back(R);
512   }
513   if (DP.empty())
514     return;
515 
516   auto NoIndOp = [this, CmpR1, CmpR2] (unsigned R) -> bool {
517     for (auto I = MRI->use_nodbg_begin(R), E = MRI->use_nodbg_end();
518          I != E; ++I) {
519       const MachineInstr *UseI = I->getParent();
520       if (UseI->getOpcode() != Hexagon::A2_addp)
521         continue;
522       // Get the output from the add. If it is one of the inputs to the
523       // loop-controlling compare instruction, then R is likely an induc-
524       // tion register.
525       unsigned T = UseI->getOperand(0).getReg();
526       if (T == CmpR1 || T == CmpR2)
527         return false;
528     }
529     return true;
530   };
531   UVect::iterator End = llvm::remove_if(DP, NoIndOp);
532   Rs.insert(DP.begin(), End);
533   Rs.insert(CmpR1);
534   Rs.insert(CmpR2);
535 
536   DEBUG({
537     dbgs() << "For loop at BB#" << HB->getNumber() << " ind regs: ";
538     dump_partition(dbgs(), Rs, *TRI);
539     dbgs() << '\n';
540   });
541 }
542 
543 void HexagonSplitDoubleRegs::collectIndRegs(LoopRegMap &IRM) {
544   typedef std::vector<MachineLoop*> LoopVector;
545   LoopVector WorkQ;
546 
547   for (auto I : *MLI)
548     WorkQ.push_back(I);
549   for (unsigned i = 0; i < WorkQ.size(); ++i) {
550     for (auto I : *WorkQ[i])
551       WorkQ.push_back(I);
552   }
553 
554   USet Rs;
555   for (unsigned i = 0, n = WorkQ.size(); i < n; ++i) {
556     MachineLoop *L = WorkQ[i];
557     Rs.clear();
558     collectIndRegsForLoop(L, Rs);
559     if (!Rs.empty())
560       IRM.insert(std::make_pair(L, Rs));
561   }
562 }
563 
564 void HexagonSplitDoubleRegs::createHalfInstr(unsigned Opc, MachineInstr *MI,
565       const UUPairMap &PairMap, unsigned SubR) {
566   MachineBasicBlock &B = *MI->getParent();
567   DebugLoc DL = MI->getDebugLoc();
568   MachineInstr *NewI = BuildMI(B, MI, DL, TII->get(Opc));
569 
570   for (auto &Op : MI->operands()) {
571     if (!Op.isReg()) {
572       NewI->addOperand(Op);
573       continue;
574     }
575     // For register operands, set the subregister.
576     unsigned R = Op.getReg();
577     unsigned SR = Op.getSubReg();
578     bool isVirtReg = TargetRegisterInfo::isVirtualRegister(R);
579     bool isKill = Op.isKill();
580     if (isVirtReg && MRI->getRegClass(R) == DoubleRC) {
581       isKill = false;
582       UUPairMap::const_iterator F = PairMap.find(R);
583       if (F == PairMap.end()) {
584         SR = SubR;
585       } else {
586         const UUPair &P = F->second;
587         R = (SubR == Hexagon::isub_lo) ? P.first : P.second;
588         SR = 0;
589       }
590     }
591     auto CO = MachineOperand::CreateReg(R, Op.isDef(), Op.isImplicit(), isKill,
592           Op.isDead(), Op.isUndef(), Op.isEarlyClobber(), SR, Op.isDebug(),
593           Op.isInternalRead());
594     NewI->addOperand(CO);
595   }
596 }
597 
598 void HexagonSplitDoubleRegs::splitMemRef(MachineInstr *MI,
599       const UUPairMap &PairMap) {
600   bool Load = MI->mayLoad();
601   unsigned OrigOpc = MI->getOpcode();
602   bool PostInc = (OrigOpc == Hexagon::L2_loadrd_pi ||
603                   OrigOpc == Hexagon::S2_storerd_pi);
604   MachineInstr *LowI, *HighI;
605   MachineBasicBlock &B = *MI->getParent();
606   DebugLoc DL = MI->getDebugLoc();
607 
608   // Index of the base-address-register operand.
609   unsigned AdrX = PostInc ? (Load ? 2 : 1)
610                           : (Load ? 1 : 0);
611   MachineOperand &AdrOp = MI->getOperand(AdrX);
612   unsigned RSA = getRegState(AdrOp);
613   MachineOperand &ValOp = Load ? MI->getOperand(0)
614                                : (PostInc ? MI->getOperand(3)
615                                           : MI->getOperand(2));
616   UUPairMap::const_iterator F = PairMap.find(ValOp.getReg());
617   assert(F != PairMap.end());
618 
619   if (Load) {
620     const UUPair &P = F->second;
621     int64_t Off = PostInc ? 0 : MI->getOperand(2).getImm();
622     LowI = BuildMI(B, MI, DL, TII->get(Hexagon::L2_loadri_io), P.first)
623              .addReg(AdrOp.getReg(), RSA & ~RegState::Kill, AdrOp.getSubReg())
624              .addImm(Off);
625     HighI = BuildMI(B, MI, DL, TII->get(Hexagon::L2_loadri_io), P.second)
626               .addReg(AdrOp.getReg(), RSA & ~RegState::Kill, AdrOp.getSubReg())
627               .addImm(Off+4);
628   } else {
629     const UUPair &P = F->second;
630     int64_t Off = PostInc ? 0 : MI->getOperand(1).getImm();
631     LowI = BuildMI(B, MI, DL, TII->get(Hexagon::S2_storeri_io))
632              .addReg(AdrOp.getReg(), RSA & ~RegState::Kill, AdrOp.getSubReg())
633              .addImm(Off)
634              .addReg(P.first);
635     HighI = BuildMI(B, MI, DL, TII->get(Hexagon::S2_storeri_io))
636               .addReg(AdrOp.getReg(), RSA & ~RegState::Kill, AdrOp.getSubReg())
637               .addImm(Off+4)
638               .addReg(P.second);
639   }
640 
641   if (PostInc) {
642     // Create the increment of the address register.
643     int64_t Inc = Load ? MI->getOperand(3).getImm()
644                        : MI->getOperand(2).getImm();
645     MachineOperand &UpdOp = Load ? MI->getOperand(1) : MI->getOperand(0);
646     const TargetRegisterClass *RC = MRI->getRegClass(UpdOp.getReg());
647     unsigned NewR = MRI->createVirtualRegister(RC);
648     assert(!UpdOp.getSubReg() && "Def operand with subreg");
649     BuildMI(B, MI, DL, TII->get(Hexagon::A2_addi), NewR)
650       .addReg(AdrOp.getReg(), RSA)
651       .addImm(Inc);
652     MRI->replaceRegWith(UpdOp.getReg(), NewR);
653     // The original instruction will be deleted later.
654   }
655 
656   // Generate a new pair of memory-operands.
657   MachineFunction &MF = *B.getParent();
658   for (auto &MO : MI->memoperands()) {
659     const MachinePointerInfo &Ptr = MO->getPointerInfo();
660     MachineMemOperand::Flags F = MO->getFlags();
661     int A = MO->getAlignment();
662 
663     auto *Tmp1 = MF.getMachineMemOperand(Ptr, F, 4/*size*/, A);
664     LowI->addMemOperand(MF, Tmp1);
665     auto *Tmp2 = MF.getMachineMemOperand(Ptr, F, 4/*size*/, std::min(A, 4));
666     HighI->addMemOperand(MF, Tmp2);
667   }
668 }
669 
670 void HexagonSplitDoubleRegs::splitImmediate(MachineInstr *MI,
671       const UUPairMap &PairMap) {
672   MachineOperand &Op0 = MI->getOperand(0);
673   MachineOperand &Op1 = MI->getOperand(1);
674   assert(Op0.isReg() && Op1.isImm());
675   uint64_t V = Op1.getImm();
676 
677   MachineBasicBlock &B = *MI->getParent();
678   DebugLoc DL = MI->getDebugLoc();
679   UUPairMap::const_iterator F = PairMap.find(Op0.getReg());
680   assert(F != PairMap.end());
681   const UUPair &P = F->second;
682 
683   // The operand to A2_tfrsi can only have 32 significant bits. Immediate
684   // values in MachineOperand are stored as 64-bit integers, and so the
685   // value -1 may be represented either as 64-bit -1, or 4294967295. Both
686   // will have the 32 higher bits truncated in the end, but -1 will remain
687   // as -1, while the latter may appear to be a large unsigned value
688   // requiring a constant extender. The casting to int32_t will select the
689   // former representation. (The same reasoning applies to all 32-bit
690   // values.)
691   BuildMI(B, MI, DL, TII->get(Hexagon::A2_tfrsi), P.first)
692     .addImm(int32_t(V & 0xFFFFFFFFULL));
693   BuildMI(B, MI, DL, TII->get(Hexagon::A2_tfrsi), P.second)
694     .addImm(int32_t(V >> 32));
695 }
696 
697 void HexagonSplitDoubleRegs::splitCombine(MachineInstr *MI,
698       const UUPairMap &PairMap) {
699   MachineOperand &Op0 = MI->getOperand(0);
700   MachineOperand &Op1 = MI->getOperand(1);
701   MachineOperand &Op2 = MI->getOperand(2);
702   assert(Op0.isReg());
703 
704   MachineBasicBlock &B = *MI->getParent();
705   DebugLoc DL = MI->getDebugLoc();
706   UUPairMap::const_iterator F = PairMap.find(Op0.getReg());
707   assert(F != PairMap.end());
708   const UUPair &P = F->second;
709 
710   if (Op1.isImm()) {
711     BuildMI(B, MI, DL, TII->get(Hexagon::A2_tfrsi), P.second)
712       .addImm(Op1.getImm());
713   } else if (Op1.isReg()) {
714     BuildMI(B, MI, DL, TII->get(TargetOpcode::COPY), P.second)
715       .addReg(Op1.getReg(), getRegState(Op1), Op1.getSubReg());
716   } else
717     llvm_unreachable("Unexpected operand");
718 
719   if (Op2.isImm()) {
720     BuildMI(B, MI, DL, TII->get(Hexagon::A2_tfrsi), P.first)
721       .addImm(Op2.getImm());
722   } else if (Op2.isReg()) {
723     BuildMI(B, MI, DL, TII->get(TargetOpcode::COPY), P.first)
724       .addReg(Op2.getReg(), getRegState(Op2), Op2.getSubReg());
725   } else
726     llvm_unreachable("Unexpected operand");
727 }
728 
729 void HexagonSplitDoubleRegs::splitExt(MachineInstr *MI,
730       const UUPairMap &PairMap) {
731   MachineOperand &Op0 = MI->getOperand(0);
732   MachineOperand &Op1 = MI->getOperand(1);
733   assert(Op0.isReg() && Op1.isReg());
734 
735   MachineBasicBlock &B = *MI->getParent();
736   DebugLoc DL = MI->getDebugLoc();
737   UUPairMap::const_iterator F = PairMap.find(Op0.getReg());
738   assert(F != PairMap.end());
739   const UUPair &P = F->second;
740   unsigned RS = getRegState(Op1);
741 
742   BuildMI(B, MI, DL, TII->get(TargetOpcode::COPY), P.first)
743     .addReg(Op1.getReg(), RS & ~RegState::Kill, Op1.getSubReg());
744   BuildMI(B, MI, DL, TII->get(Hexagon::S2_asr_i_r), P.second)
745     .addReg(Op1.getReg(), RS, Op1.getSubReg())
746     .addImm(31);
747 }
748 
749 void HexagonSplitDoubleRegs::splitShift(MachineInstr *MI,
750       const UUPairMap &PairMap) {
751   using namespace Hexagon;
752 
753   MachineOperand &Op0 = MI->getOperand(0);
754   MachineOperand &Op1 = MI->getOperand(1);
755   MachineOperand &Op2 = MI->getOperand(2);
756   assert(Op0.isReg() && Op1.isReg() && Op2.isImm());
757   int64_t Sh64 = Op2.getImm();
758   assert(Sh64 >= 0 && Sh64 < 64);
759   unsigned S = Sh64;
760 
761   UUPairMap::const_iterator F = PairMap.find(Op0.getReg());
762   assert(F != PairMap.end());
763   const UUPair &P = F->second;
764   unsigned LoR = P.first;
765   unsigned HiR = P.second;
766 
767   unsigned Opc = MI->getOpcode();
768   bool Right = (Opc == S2_lsr_i_p || Opc == S2_asr_i_p);
769   bool Left = !Right;
770   bool Signed = (Opc == S2_asr_i_p);
771 
772   MachineBasicBlock &B = *MI->getParent();
773   DebugLoc DL = MI->getDebugLoc();
774   unsigned RS = getRegState(Op1);
775   unsigned ShiftOpc = Left ? S2_asl_i_r
776                            : (Signed ? S2_asr_i_r : S2_lsr_i_r);
777   unsigned LoSR = isub_lo;
778   unsigned HiSR = isub_hi;
779 
780   if (S == 0) {
781     // No shift, subregister copy.
782     BuildMI(B, MI, DL, TII->get(TargetOpcode::COPY), LoR)
783       .addReg(Op1.getReg(), RS & ~RegState::Kill, LoSR);
784     BuildMI(B, MI, DL, TII->get(TargetOpcode::COPY), HiR)
785       .addReg(Op1.getReg(), RS, HiSR);
786   } else if (S < 32) {
787     const TargetRegisterClass *IntRC = &IntRegsRegClass;
788     unsigned TmpR = MRI->createVirtualRegister(IntRC);
789     // Expansion:
790     // Shift left:    DR = shl R, #s
791     //   LoR  = shl R.lo, #s
792     //   TmpR = extractu R.lo, #s, #32-s
793     //   HiR  = or (TmpR, asl(R.hi, #s))
794     // Shift right:   DR = shr R, #s
795     //   HiR  = shr R.hi, #s
796     //   TmpR = shr R.lo, #s
797     //   LoR  = insert TmpR, R.hi, #s, #32-s
798 
799     // Shift left:
800     //   LoR  = shl R.lo, #s
801     // Shift right:
802     //   TmpR = shr R.lo, #s
803 
804     // Make a special case for A2_aslh and A2_asrh (they are predicable as
805     // opposed to S2_asl_i_r/S2_asr_i_r).
806     if (S == 16 && Left)
807       BuildMI(B, MI, DL, TII->get(A2_aslh), LoR)
808         .addReg(Op1.getReg(), RS & ~RegState::Kill, LoSR);
809     else if (S == 16 && Signed)
810       BuildMI(B, MI, DL, TII->get(A2_asrh), TmpR)
811         .addReg(Op1.getReg(), RS & ~RegState::Kill, LoSR);
812     else
813       BuildMI(B, MI, DL, TII->get(ShiftOpc), (Left ? LoR : TmpR))
814         .addReg(Op1.getReg(), RS & ~RegState::Kill, LoSR)
815         .addImm(S);
816 
817     if (Left) {
818       // TmpR = extractu R.lo, #s, #32-s
819       BuildMI(B, MI, DL, TII->get(S2_extractu), TmpR)
820         .addReg(Op1.getReg(), RS & ~RegState::Kill, LoSR)
821         .addImm(S)
822         .addImm(32-S);
823       // HiR  = or (TmpR, asl(R.hi, #s))
824       BuildMI(B, MI, DL, TII->get(S2_asl_i_r_or), HiR)
825         .addReg(TmpR)
826         .addReg(Op1.getReg(), RS, HiSR)
827         .addImm(S);
828     } else {
829       // HiR  = shr R.hi, #s
830       BuildMI(B, MI, DL, TII->get(ShiftOpc), HiR)
831         .addReg(Op1.getReg(), RS & ~RegState::Kill, HiSR)
832         .addImm(S);
833       // LoR  = insert TmpR, R.hi, #s, #32-s
834       BuildMI(B, MI, DL, TII->get(S2_insert), LoR)
835         .addReg(TmpR)
836         .addReg(Op1.getReg(), RS, HiSR)
837         .addImm(S)
838         .addImm(32-S);
839     }
840   } else if (S == 32) {
841     BuildMI(B, MI, DL, TII->get(TargetOpcode::COPY), (Left ? HiR : LoR))
842       .addReg(Op1.getReg(), RS & ~RegState::Kill, (Left ? LoSR : HiSR));
843     if (!Signed)
844       BuildMI(B, MI, DL, TII->get(A2_tfrsi), (Left ? LoR : HiR))
845         .addImm(0);
846     else  // Must be right shift.
847       BuildMI(B, MI, DL, TII->get(S2_asr_i_r), HiR)
848         .addReg(Op1.getReg(), RS, HiSR)
849         .addImm(31);
850   } else if (S < 64) {
851     S -= 32;
852     if (S == 16 && Left)
853       BuildMI(B, MI, DL, TII->get(A2_aslh), HiR)
854         .addReg(Op1.getReg(), RS & ~RegState::Kill, LoSR);
855     else if (S == 16 && Signed)
856       BuildMI(B, MI, DL, TII->get(A2_asrh), LoR)
857         .addReg(Op1.getReg(), RS & ~RegState::Kill, HiSR);
858     else
859       BuildMI(B, MI, DL, TII->get(ShiftOpc), (Left ? HiR : LoR))
860         .addReg(Op1.getReg(), RS & ~RegState::Kill, (Left ? LoSR : HiSR))
861         .addImm(S);
862 
863     if (Signed)
864       BuildMI(B, MI, DL, TII->get(S2_asr_i_r), HiR)
865         .addReg(Op1.getReg(), RS, HiSR)
866         .addImm(31);
867     else
868       BuildMI(B, MI, DL, TII->get(A2_tfrsi), (Left ? LoR : HiR))
869         .addImm(0);
870   }
871 }
872 
873 void HexagonSplitDoubleRegs::splitAslOr(MachineInstr *MI,
874       const UUPairMap &PairMap) {
875   using namespace Hexagon;
876 
877   MachineOperand &Op0 = MI->getOperand(0);
878   MachineOperand &Op1 = MI->getOperand(1);
879   MachineOperand &Op2 = MI->getOperand(2);
880   MachineOperand &Op3 = MI->getOperand(3);
881   assert(Op0.isReg() && Op1.isReg() && Op2.isReg() && Op3.isImm());
882   int64_t Sh64 = Op3.getImm();
883   assert(Sh64 >= 0 && Sh64 < 64);
884   unsigned S = Sh64;
885 
886   UUPairMap::const_iterator F = PairMap.find(Op0.getReg());
887   assert(F != PairMap.end());
888   const UUPair &P = F->second;
889   unsigned LoR = P.first;
890   unsigned HiR = P.second;
891 
892   MachineBasicBlock &B = *MI->getParent();
893   DebugLoc DL = MI->getDebugLoc();
894   unsigned RS1 = getRegState(Op1);
895   unsigned RS2 = getRegState(Op2);
896   const TargetRegisterClass *IntRC = &IntRegsRegClass;
897 
898   unsigned LoSR = isub_lo;
899   unsigned HiSR = isub_hi;
900 
901   // Op0 = S2_asl_i_p_or Op1, Op2, Op3
902   // means:  Op0 = or (Op1, asl(Op2, Op3))
903 
904   // Expansion of
905   //   DR = or (R1, asl(R2, #s))
906   //
907   //   LoR  = or (R1.lo, asl(R2.lo, #s))
908   //   Tmp1 = extractu R2.lo, #s, #32-s
909   //   Tmp2 = or R1.hi, Tmp1
910   //   HiR  = or (Tmp2, asl(R2.hi, #s))
911 
912   if (S == 0) {
913     // DR  = or (R1, asl(R2, #0))
914     //    -> or (R1, R2)
915     // i.e. LoR = or R1.lo, R2.lo
916     //      HiR = or R1.hi, R2.hi
917     BuildMI(B, MI, DL, TII->get(A2_or), LoR)
918       .addReg(Op1.getReg(), RS1 & ~RegState::Kill, LoSR)
919       .addReg(Op2.getReg(), RS2 & ~RegState::Kill, LoSR);
920     BuildMI(B, MI, DL, TII->get(A2_or), HiR)
921       .addReg(Op1.getReg(), RS1, HiSR)
922       .addReg(Op2.getReg(), RS2, HiSR);
923   } else if (S < 32) {
924     BuildMI(B, MI, DL, TII->get(S2_asl_i_r_or), LoR)
925       .addReg(Op1.getReg(), RS1 & ~RegState::Kill, LoSR)
926       .addReg(Op2.getReg(), RS2 & ~RegState::Kill, LoSR)
927       .addImm(S);
928     unsigned TmpR1 = MRI->createVirtualRegister(IntRC);
929     BuildMI(B, MI, DL, TII->get(S2_extractu), TmpR1)
930       .addReg(Op2.getReg(), RS2 & ~RegState::Kill, LoSR)
931       .addImm(S)
932       .addImm(32-S);
933     unsigned TmpR2 = MRI->createVirtualRegister(IntRC);
934     BuildMI(B, MI, DL, TII->get(A2_or), TmpR2)
935       .addReg(Op1.getReg(), RS1, HiSR)
936       .addReg(TmpR1);
937     BuildMI(B, MI, DL, TII->get(S2_asl_i_r_or), HiR)
938       .addReg(TmpR2)
939       .addReg(Op2.getReg(), RS2, HiSR)
940       .addImm(S);
941   } else if (S == 32) {
942     // DR  = or (R1, asl(R2, #32))
943     //    -> or R1, R2.lo
944     // LoR = R1.lo
945     // HiR = or R1.hi, R2.lo
946     BuildMI(B, MI, DL, TII->get(TargetOpcode::COPY), LoR)
947       .addReg(Op1.getReg(), RS1 & ~RegState::Kill, LoSR);
948     BuildMI(B, MI, DL, TII->get(A2_or), HiR)
949       .addReg(Op1.getReg(), RS1, HiSR)
950       .addReg(Op2.getReg(), RS2, LoSR);
951   } else if (S < 64) {
952     // DR  = or (R1, asl(R2, #s))
953     //
954     // LoR = R1:lo
955     // HiR = or (R1:hi, asl(R2:lo, #s-32))
956     S -= 32;
957     BuildMI(B, MI, DL, TII->get(TargetOpcode::COPY), LoR)
958       .addReg(Op1.getReg(), RS1 & ~RegState::Kill, LoSR);
959     BuildMI(B, MI, DL, TII->get(S2_asl_i_r_or), HiR)
960       .addReg(Op1.getReg(), RS1, HiSR)
961       .addReg(Op2.getReg(), RS2, LoSR)
962       .addImm(S);
963   }
964 }
965 
966 bool HexagonSplitDoubleRegs::splitInstr(MachineInstr *MI,
967       const UUPairMap &PairMap) {
968   using namespace Hexagon;
969 
970   DEBUG(dbgs() << "Splitting: " << *MI);
971   bool Split = false;
972   unsigned Opc = MI->getOpcode();
973 
974   switch (Opc) {
975     case TargetOpcode::PHI:
976     case TargetOpcode::COPY: {
977       unsigned DstR = MI->getOperand(0).getReg();
978       if (MRI->getRegClass(DstR) == DoubleRC) {
979         createHalfInstr(Opc, MI, PairMap, isub_lo);
980         createHalfInstr(Opc, MI, PairMap, isub_hi);
981         Split = true;
982       }
983       break;
984     }
985     case A2_andp:
986       createHalfInstr(A2_and, MI, PairMap, isub_lo);
987       createHalfInstr(A2_and, MI, PairMap, isub_hi);
988       Split = true;
989       break;
990     case A2_orp:
991       createHalfInstr(A2_or, MI, PairMap, isub_lo);
992       createHalfInstr(A2_or, MI, PairMap, isub_hi);
993       Split = true;
994       break;
995     case A2_xorp:
996       createHalfInstr(A2_xor, MI, PairMap, isub_lo);
997       createHalfInstr(A2_xor, MI, PairMap, isub_hi);
998       Split = true;
999       break;
1000 
1001     case L2_loadrd_io:
1002     case L2_loadrd_pi:
1003     case S2_storerd_io:
1004     case S2_storerd_pi:
1005       splitMemRef(MI, PairMap);
1006       Split = true;
1007       break;
1008 
1009     case A2_tfrpi:
1010     case CONST64:
1011       splitImmediate(MI, PairMap);
1012       Split = true;
1013       break;
1014 
1015     case A2_combineii:
1016     case A4_combineir:
1017     case A4_combineii:
1018     case A4_combineri:
1019     case A2_combinew:
1020       splitCombine(MI, PairMap);
1021       Split = true;
1022       break;
1023 
1024     case A2_sxtw:
1025       splitExt(MI, PairMap);
1026       Split = true;
1027       break;
1028 
1029     case S2_asl_i_p:
1030     case S2_asr_i_p:
1031     case S2_lsr_i_p:
1032       splitShift(MI, PairMap);
1033       Split = true;
1034       break;
1035 
1036     case S2_asl_i_p_or:
1037       splitAslOr(MI, PairMap);
1038       Split = true;
1039       break;
1040 
1041     default:
1042       llvm_unreachable("Instruction not splitable");
1043       return false;
1044   }
1045 
1046   return Split;
1047 }
1048 
1049 void HexagonSplitDoubleRegs::replaceSubregUses(MachineInstr *MI,
1050       const UUPairMap &PairMap) {
1051   for (auto &Op : MI->operands()) {
1052     if (!Op.isReg() || !Op.isUse() || !Op.getSubReg())
1053       continue;
1054     unsigned R = Op.getReg();
1055     UUPairMap::const_iterator F = PairMap.find(R);
1056     if (F == PairMap.end())
1057       continue;
1058     const UUPair &P = F->second;
1059     switch (Op.getSubReg()) {
1060       case Hexagon::isub_lo:
1061         Op.setReg(P.first);
1062         break;
1063       case Hexagon::isub_hi:
1064         Op.setReg(P.second);
1065         break;
1066     }
1067     Op.setSubReg(0);
1068   }
1069 }
1070 
1071 void HexagonSplitDoubleRegs::collapseRegPairs(MachineInstr *MI,
1072       const UUPairMap &PairMap) {
1073   MachineBasicBlock &B = *MI->getParent();
1074   DebugLoc DL = MI->getDebugLoc();
1075 
1076   for (auto &Op : MI->operands()) {
1077     if (!Op.isReg() || !Op.isUse())
1078       continue;
1079     unsigned R = Op.getReg();
1080     if (!TargetRegisterInfo::isVirtualRegister(R))
1081       continue;
1082     if (MRI->getRegClass(R) != DoubleRC || Op.getSubReg())
1083       continue;
1084     UUPairMap::const_iterator F = PairMap.find(R);
1085     if (F == PairMap.end())
1086       continue;
1087     const UUPair &Pr = F->second;
1088     unsigned NewDR = MRI->createVirtualRegister(DoubleRC);
1089     BuildMI(B, MI, DL, TII->get(TargetOpcode::REG_SEQUENCE), NewDR)
1090       .addReg(Pr.first)
1091       .addImm(Hexagon::isub_lo)
1092       .addReg(Pr.second)
1093       .addImm(Hexagon::isub_hi);
1094     Op.setReg(NewDR);
1095   }
1096 }
1097 
1098 bool HexagonSplitDoubleRegs::splitPartition(const USet &Part) {
1099   const TargetRegisterClass *IntRC = &Hexagon::IntRegsRegClass;
1100   typedef std::set<MachineInstr*> MISet;
1101   bool Changed = false;
1102 
1103   DEBUG(dbgs() << "Splitting partition: "; dump_partition(dbgs(), Part, *TRI);
1104         dbgs() << '\n');
1105 
1106   UUPairMap PairMap;
1107 
1108   MISet SplitIns;
1109   for (unsigned DR : Part) {
1110     MachineInstr *DefI = MRI->getVRegDef(DR);
1111     SplitIns.insert(DefI);
1112 
1113     // Collect all instructions, including fixed ones.  We won't split them,
1114     // but we need to visit them again to insert the REG_SEQUENCE instructions.
1115     for (auto U = MRI->use_nodbg_begin(DR), W = MRI->use_nodbg_end();
1116          U != W; ++U)
1117       SplitIns.insert(U->getParent());
1118 
1119     unsigned LoR = MRI->createVirtualRegister(IntRC);
1120     unsigned HiR = MRI->createVirtualRegister(IntRC);
1121     DEBUG(dbgs() << "Created mapping: " << PrintReg(DR, TRI) << " -> "
1122                  << PrintReg(HiR, TRI) << ':' << PrintReg(LoR, TRI) << '\n');
1123     PairMap.insert(std::make_pair(DR, UUPair(LoR, HiR)));
1124   }
1125 
1126   MISet Erase;
1127   for (auto MI : SplitIns) {
1128     if (isFixedInstr(MI)) {
1129       collapseRegPairs(MI, PairMap);
1130     } else {
1131       bool Done = splitInstr(MI, PairMap);
1132       if (Done)
1133         Erase.insert(MI);
1134       Changed |= Done;
1135     }
1136   }
1137 
1138   for (unsigned DR : Part) {
1139     // Before erasing "double" instructions, revisit all uses of the double
1140     // registers in this partition, and replace all uses of them with subre-
1141     // gisters, with the corresponding single registers.
1142     MISet Uses;
1143     for (auto U = MRI->use_nodbg_begin(DR), W = MRI->use_nodbg_end();
1144          U != W; ++U)
1145       Uses.insert(U->getParent());
1146     for (auto M : Uses)
1147       replaceSubregUses(M, PairMap);
1148   }
1149 
1150   for (auto MI : Erase) {
1151     MachineBasicBlock *B = MI->getParent();
1152     B->erase(MI);
1153   }
1154 
1155   return Changed;
1156 }
1157 
1158 bool HexagonSplitDoubleRegs::runOnMachineFunction(MachineFunction &MF) {
1159   DEBUG(dbgs() << "Splitting double registers in function: "
1160         << MF.getName() << '\n');
1161 
1162   if (skipFunction(*MF.getFunction()))
1163     return false;
1164 
1165   auto &ST = MF.getSubtarget<HexagonSubtarget>();
1166   TRI = ST.getRegisterInfo();
1167   TII = ST.getInstrInfo();
1168   MRI = &MF.getRegInfo();
1169   MLI = &getAnalysis<MachineLoopInfo>();
1170 
1171   UUSetMap P2Rs;
1172   LoopRegMap IRM;
1173 
1174   collectIndRegs(IRM);
1175   partitionRegisters(P2Rs);
1176 
1177   DEBUG({
1178     dbgs() << "Register partitioning: (partition #0 is fixed)\n";
1179     for (UUSetMap::iterator I = P2Rs.begin(), E = P2Rs.end(); I != E; ++I) {
1180       dbgs() << '#' << I->first << " -> ";
1181       dump_partition(dbgs(), I->second, *TRI);
1182       dbgs() << '\n';
1183     }
1184   });
1185 
1186   bool Changed = false;
1187   int Limit = MaxHSDR;
1188 
1189   for (UUSetMap::iterator I = P2Rs.begin(), E = P2Rs.end(); I != E; ++I) {
1190     if (I->first == 0)
1191       continue;
1192     if (Limit >= 0 && Counter >= Limit)
1193       break;
1194     USet &Part = I->second;
1195     DEBUG(dbgs() << "Calculating profit for partition #" << I->first << '\n');
1196     if (!isProfitable(Part, IRM))
1197       continue;
1198     Counter++;
1199     Changed |= splitPartition(Part);
1200   }
1201 
1202   return Changed;
1203 }
1204 
1205 FunctionPass *llvm::createHexagonSplitDoubleRegs() {
1206   return new HexagonSplitDoubleRegs();
1207 }
1208