xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Hexagon/HexagonCopyToCombine.cpp (revision 0eae32dcef82f6f06de6419a0d623d7def0cc8f6)
1 //===------- HexagonCopyToCombine.cpp - Hexagon Copy-To-Combine Pass ------===//
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 // This pass replaces transfer instructions by combine instructions.
9 // We walk along a basic block and look for two combinable instructions and try
10 // to move them together. If we can move them next to each other we do so and
11 // replace them with a combine instruction.
12 //===----------------------------------------------------------------------===//
13 
14 #include "HexagonInstrInfo.h"
15 #include "HexagonSubtarget.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/MachineInstr.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/CodeGen/TargetRegisterInfo.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Support/CodeGen.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/Target/TargetMachine.h"
31 
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "hexagon-copy-combine"
35 
36 static
37 cl::opt<bool> IsCombinesDisabled("disable-merge-into-combines",
38                                  cl::Hidden, cl::ZeroOrMore,
39                                  cl::init(false),
40                                  cl::desc("Disable merging into combines"));
41 static
42 cl::opt<bool> IsConst64Disabled("disable-const64",
43                                  cl::Hidden, cl::ZeroOrMore,
44                                  cl::init(false),
45                                  cl::desc("Disable generation of const64"));
46 static
47 cl::opt<unsigned>
48 MaxNumOfInstsBetweenNewValueStoreAndTFR("max-num-inst-between-tfr-and-nv-store",
49                    cl::Hidden, cl::init(4),
50                    cl::desc("Maximum distance between a tfr feeding a store we "
51                             "consider the store still to be newifiable"));
52 
53 namespace llvm {
54   FunctionPass *createHexagonCopyToCombine();
55   void initializeHexagonCopyToCombinePass(PassRegistry&);
56 }
57 
58 
59 namespace {
60 
61 class HexagonCopyToCombine : public MachineFunctionPass  {
62   const HexagonInstrInfo *TII;
63   const TargetRegisterInfo *TRI;
64   const HexagonSubtarget *ST;
65   bool ShouldCombineAggressively;
66 
67   DenseSet<MachineInstr *> PotentiallyNewifiableTFR;
68   SmallVector<MachineInstr *, 8> DbgMItoMove;
69 
70 public:
71   static char ID;
72 
73   HexagonCopyToCombine() : MachineFunctionPass(ID) {
74     initializeHexagonCopyToCombinePass(*PassRegistry::getPassRegistry());
75   }
76 
77   void getAnalysisUsage(AnalysisUsage &AU) const override {
78     MachineFunctionPass::getAnalysisUsage(AU);
79   }
80 
81   StringRef getPassName() const override {
82     return "Hexagon Copy-To-Combine Pass";
83   }
84 
85   bool runOnMachineFunction(MachineFunction &Fn) override;
86 
87   MachineFunctionProperties getRequiredProperties() const override {
88     return MachineFunctionProperties().set(
89         MachineFunctionProperties::Property::NoVRegs);
90   }
91 
92 private:
93   MachineInstr *findPairable(MachineInstr &I1, bool &DoInsertAtI1,
94                              bool AllowC64);
95 
96   void findPotentialNewifiableTFRs(MachineBasicBlock &);
97 
98   void combine(MachineInstr &I1, MachineInstr &I2,
99                MachineBasicBlock::iterator &MI, bool DoInsertAtI1,
100                bool OptForSize);
101 
102   bool isSafeToMoveTogether(MachineInstr &I1, MachineInstr &I2,
103                             unsigned I1DestReg, unsigned I2DestReg,
104                             bool &DoInsertAtI1);
105 
106   void emitCombineRR(MachineBasicBlock::iterator &Before, unsigned DestReg,
107                      MachineOperand &HiOperand, MachineOperand &LoOperand);
108 
109   void emitCombineRI(MachineBasicBlock::iterator &Before, unsigned DestReg,
110                      MachineOperand &HiOperand, MachineOperand &LoOperand);
111 
112   void emitCombineIR(MachineBasicBlock::iterator &Before, unsigned DestReg,
113                      MachineOperand &HiOperand, MachineOperand &LoOperand);
114 
115   void emitCombineII(MachineBasicBlock::iterator &Before, unsigned DestReg,
116                      MachineOperand &HiOperand, MachineOperand &LoOperand);
117 
118   void emitConst64(MachineBasicBlock::iterator &Before, unsigned DestReg,
119                    MachineOperand &HiOperand, MachineOperand &LoOperand);
120 };
121 
122 } // End anonymous namespace.
123 
124 char HexagonCopyToCombine::ID = 0;
125 
126 INITIALIZE_PASS(HexagonCopyToCombine, "hexagon-copy-combine",
127                 "Hexagon Copy-To-Combine Pass", false, false)
128 
129 static bool isCombinableInstType(MachineInstr &MI, const HexagonInstrInfo *TII,
130                                  bool ShouldCombineAggressively) {
131   switch (MI.getOpcode()) {
132   case Hexagon::A2_tfr: {
133     // A COPY instruction can be combined if its arguments are IntRegs (32bit).
134     const MachineOperand &Op0 = MI.getOperand(0);
135     const MachineOperand &Op1 = MI.getOperand(1);
136     assert(Op0.isReg() && Op1.isReg());
137 
138     Register DestReg = Op0.getReg();
139     Register SrcReg = Op1.getReg();
140     return Hexagon::IntRegsRegClass.contains(DestReg) &&
141            Hexagon::IntRegsRegClass.contains(SrcReg);
142   }
143 
144   case Hexagon::A2_tfrsi: {
145     // A transfer-immediate can be combined if its argument is a signed 8bit
146     // value.
147     const MachineOperand &Op0 = MI.getOperand(0);
148     const MachineOperand &Op1 = MI.getOperand(1);
149     assert(Op0.isReg());
150 
151     Register DestReg = Op0.getReg();
152     // Ensure that TargetFlags are MO_NO_FLAG for a global. This is a
153     // workaround for an ABI bug that prevents GOT relocations on combine
154     // instructions
155     if (!Op1.isImm() && Op1.getTargetFlags() != HexagonII::MO_NO_FLAG)
156       return false;
157 
158     // Only combine constant extended A2_tfrsi if we are in aggressive mode.
159     bool NotExt = Op1.isImm() && isInt<8>(Op1.getImm());
160     return Hexagon::IntRegsRegClass.contains(DestReg) &&
161            (ShouldCombineAggressively || NotExt);
162   }
163 
164   case Hexagon::V6_vassign:
165     return true;
166 
167   default:
168     break;
169   }
170 
171   return false;
172 }
173 
174 template <unsigned N> static bool isGreaterThanNBitTFRI(const MachineInstr &I) {
175   if (I.getOpcode() == Hexagon::TFRI64_V4 ||
176       I.getOpcode() == Hexagon::A2_tfrsi) {
177     const MachineOperand &Op = I.getOperand(1);
178     return !Op.isImm() || !isInt<N>(Op.getImm());
179   }
180   return false;
181 }
182 
183 /// areCombinableOperations - Returns true if the two instruction can be merge
184 /// into a combine (ignoring register constraints).
185 static bool areCombinableOperations(const TargetRegisterInfo *TRI,
186                                     MachineInstr &HighRegInst,
187                                     MachineInstr &LowRegInst, bool AllowC64) {
188   unsigned HiOpc = HighRegInst.getOpcode();
189   unsigned LoOpc = LowRegInst.getOpcode();
190 
191   auto verifyOpc = [](unsigned Opc) -> void {
192     switch (Opc) {
193       case Hexagon::A2_tfr:
194       case Hexagon::A2_tfrsi:
195       case Hexagon::V6_vassign:
196         break;
197       default:
198         llvm_unreachable("Unexpected opcode");
199     }
200   };
201   verifyOpc(HiOpc);
202   verifyOpc(LoOpc);
203 
204   if (HiOpc == Hexagon::V6_vassign || LoOpc == Hexagon::V6_vassign)
205     return HiOpc == LoOpc;
206 
207   if (!AllowC64) {
208     // There is no combine of two constant extended values.
209     if (isGreaterThanNBitTFRI<8>(HighRegInst) &&
210         isGreaterThanNBitTFRI<6>(LowRegInst))
211       return false;
212   }
213 
214   // There is a combine of two constant extended values into CONST64,
215   // provided both constants are true immediates.
216   if (isGreaterThanNBitTFRI<16>(HighRegInst) &&
217       isGreaterThanNBitTFRI<16>(LowRegInst) && !IsConst64Disabled)
218     return (HighRegInst.getOperand(1).isImm() &&
219             LowRegInst.getOperand(1).isImm());
220 
221   // There is no combine of two constant extended values, unless handled above
222   // Make both 8-bit size checks to allow both combine (#,##) and combine(##,#)
223   if (isGreaterThanNBitTFRI<8>(HighRegInst) &&
224       isGreaterThanNBitTFRI<8>(LowRegInst))
225     return false;
226 
227   return true;
228 }
229 
230 static bool isEvenReg(unsigned Reg) {
231   assert(Register::isPhysicalRegister(Reg));
232   if (Hexagon::IntRegsRegClass.contains(Reg))
233     return (Reg - Hexagon::R0) % 2 == 0;
234   if (Hexagon::HvxVRRegClass.contains(Reg))
235     return (Reg - Hexagon::V0) % 2 == 0;
236   llvm_unreachable("Invalid register");
237 }
238 
239 static void removeKillInfo(MachineInstr &MI, unsigned RegNotKilled) {
240   for (MachineOperand &Op : MI.operands())
241     if (Op.isReg() && Op.getReg() == RegNotKilled && Op.isKill())
242       Op.setIsKill(false);
243 }
244 
245 /// Returns true if it is unsafe to move a copy instruction from \p UseReg to
246 /// \p DestReg over the instruction \p MI.
247 static bool isUnsafeToMoveAcross(MachineInstr &MI, unsigned UseReg,
248                                  unsigned DestReg,
249                                  const TargetRegisterInfo *TRI) {
250   return (UseReg && (MI.modifiesRegister(UseReg, TRI))) ||
251          MI.modifiesRegister(DestReg, TRI) || MI.readsRegister(DestReg, TRI) ||
252          MI.hasUnmodeledSideEffects() || MI.isInlineAsm() ||
253          MI.isMetaInstruction();
254 }
255 
256 static Register UseReg(const MachineOperand& MO) {
257   return MO.isReg() ? MO.getReg() : Register();
258 }
259 
260 /// isSafeToMoveTogether - Returns true if it is safe to move I1 next to I2 such
261 /// that the two instructions can be paired in a combine.
262 bool HexagonCopyToCombine::isSafeToMoveTogether(MachineInstr &I1,
263                                                 MachineInstr &I2,
264                                                 unsigned I1DestReg,
265                                                 unsigned I2DestReg,
266                                                 bool &DoInsertAtI1) {
267   Register I2UseReg = UseReg(I2.getOperand(1));
268 
269   // It is not safe to move I1 and I2 into one combine if I2 has a true
270   // dependence on I1.
271   if (I2UseReg && I1.modifiesRegister(I2UseReg, TRI))
272     return false;
273 
274   bool isSafe = true;
275 
276   // First try to move I2 towards I1.
277   {
278     // A reverse_iterator instantiated like below starts before I2, and I1
279     // respectively.
280     // Look at instructions I in between I2 and (excluding) I1.
281     MachineBasicBlock::reverse_iterator I = ++I2.getIterator().getReverse();
282     MachineBasicBlock::reverse_iterator End = I1.getIterator().getReverse();
283     // At 03 we got better results (dhrystone!) by being more conservative.
284     if (!ShouldCombineAggressively)
285       End = ++I1.getIterator().getReverse();
286     // If I2 kills its operand and we move I2 over an instruction that also
287     // uses I2's use reg we need to modify that (first) instruction to now kill
288     // this reg.
289     unsigned KilledOperand = 0;
290     if (I2.killsRegister(I2UseReg))
291       KilledOperand = I2UseReg;
292     MachineInstr *KillingInstr = nullptr;
293 
294     for (; I != End; ++I) {
295       // If the intervening instruction I:
296       //   * modifies I2's use reg
297       //   * modifies I2's def reg
298       //   * reads I2's def reg
299       //   * or has unmodelled side effects
300       // we can't move I2 across it.
301       if (I->isDebugInstr())
302         continue;
303 
304       if (isUnsafeToMoveAcross(*I, I2UseReg, I2DestReg, TRI)) {
305         isSafe = false;
306         break;
307       }
308 
309       // Update first use of the killed operand.
310       if (!KillingInstr && KilledOperand &&
311           I->readsRegister(KilledOperand, TRI))
312         KillingInstr = &*I;
313     }
314     if (isSafe) {
315       // Update the intermediate instruction to with the kill flag.
316       if (KillingInstr) {
317         bool Added = KillingInstr->addRegisterKilled(KilledOperand, TRI, true);
318         (void)Added; // suppress compiler warning
319         assert(Added && "Must successfully update kill flag");
320         removeKillInfo(I2, KilledOperand);
321       }
322       DoInsertAtI1 = true;
323       return true;
324     }
325   }
326 
327   // Try to move I1 towards I2.
328   {
329     // Look at instructions I in between I1 and (excluding) I2.
330     MachineBasicBlock::iterator I(I1), End(I2);
331     // At O3 we got better results (dhrystone) by being more conservative here.
332     if (!ShouldCombineAggressively)
333       End = std::next(MachineBasicBlock::iterator(I2));
334     Register I1UseReg = UseReg(I1.getOperand(1));
335     // Track killed operands. If we move across an instruction that kills our
336     // operand, we need to update the kill information on the moved I1. It kills
337     // the operand now.
338     MachineInstr *KillingInstr = nullptr;
339     unsigned KilledOperand = 0;
340 
341     while(++I != End) {
342       MachineInstr &MI = *I;
343       // If the intervening instruction MI:
344       //   * modifies I1's use reg
345       //   * modifies I1's def reg
346       //   * reads I1's def reg
347       //   * or has unmodelled side effects
348       //   We introduce this special case because llvm has no api to remove a
349       //   kill flag for a register (a removeRegisterKilled() analogous to
350       //   addRegisterKilled) that handles aliased register correctly.
351       //   * or has a killed aliased register use of I1's use reg
352       //           %d4 = A2_tfrpi 16
353       //           %r6 = A2_tfr %r9
354       //           %r8 = KILL %r8, implicit killed %d4
355       //      If we want to move R6 = across the KILL instruction we would have
356       //      to remove the implicit killed %d4 operand. For now, we are
357       //      conservative and disallow the move.
358       // we can't move I1 across it.
359       if (MI.isDebugInstr()) {
360         if (MI.readsRegister(I1DestReg, TRI)) // Move this instruction after I2.
361           DbgMItoMove.push_back(&MI);
362         continue;
363       }
364 
365       if (isUnsafeToMoveAcross(MI, I1UseReg, I1DestReg, TRI) ||
366           // Check for an aliased register kill. Bail out if we see one.
367           (!MI.killsRegister(I1UseReg) && MI.killsRegister(I1UseReg, TRI)))
368         return false;
369 
370       // Check for an exact kill (registers match).
371       if (I1UseReg && MI.killsRegister(I1UseReg)) {
372         assert(!KillingInstr && "Should only see one killing instruction");
373         KilledOperand = I1UseReg;
374         KillingInstr = &MI;
375       }
376     }
377     if (KillingInstr) {
378       removeKillInfo(*KillingInstr, KilledOperand);
379       // Update I1 to set the kill flag. This flag will later be picked up by
380       // the new COMBINE instruction.
381       bool Added = I1.addRegisterKilled(KilledOperand, TRI);
382       (void)Added; // suppress compiler warning
383       assert(Added && "Must successfully update kill flag");
384     }
385     DoInsertAtI1 = false;
386   }
387 
388   return true;
389 }
390 
391 /// findPotentialNewifiableTFRs - Finds tranfers that feed stores that could be
392 /// newified. (A use of a 64 bit register define can not be newified)
393 void
394 HexagonCopyToCombine::findPotentialNewifiableTFRs(MachineBasicBlock &BB) {
395   DenseMap<unsigned, MachineInstr *> LastDef;
396   for (MachineInstr &MI : BB) {
397     if (MI.isDebugInstr())
398       continue;
399 
400     // Mark TFRs that feed a potential new value store as such.
401     if (TII->mayBeNewStore(MI)) {
402       // Look for uses of TFR instructions.
403       for (const MachineOperand &Op : MI.operands()) {
404         // Skip over anything except register uses.
405         if (!Op.isReg() || !Op.isUse() || !Op.getReg())
406           continue;
407 
408         // Look for the defining instruction.
409         Register Reg = Op.getReg();
410         MachineInstr *DefInst = LastDef[Reg];
411         if (!DefInst)
412           continue;
413         if (!isCombinableInstType(*DefInst, TII, ShouldCombineAggressively))
414           continue;
415 
416         // Only close newifiable stores should influence the decision.
417         // Ignore the debug instructions in between.
418         MachineBasicBlock::iterator It(DefInst);
419         unsigned NumInstsToDef = 0;
420         while (&*It != &MI) {
421           if (!It->isDebugInstr())
422             ++NumInstsToDef;
423           ++It;
424         }
425 
426         if (NumInstsToDef > MaxNumOfInstsBetweenNewValueStoreAndTFR)
427           continue;
428 
429         PotentiallyNewifiableTFR.insert(DefInst);
430       }
431       // Skip to next instruction.
432       continue;
433     }
434 
435     // Put instructions that last defined integer or double registers into the
436     // map.
437     for (MachineOperand &Op : MI.operands()) {
438       if (Op.isReg()) {
439         if (!Op.isDef() || !Op.getReg())
440           continue;
441         Register Reg = Op.getReg();
442         if (Hexagon::DoubleRegsRegClass.contains(Reg)) {
443           for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
444             LastDef[*SubRegs] = &MI;
445         } else if (Hexagon::IntRegsRegClass.contains(Reg))
446           LastDef[Reg] = &MI;
447       } else if (Op.isRegMask()) {
448         for (unsigned Reg : Hexagon::IntRegsRegClass)
449           if (Op.clobbersPhysReg(Reg))
450             LastDef[Reg] = &MI;
451       }
452     }
453   }
454 }
455 
456 bool HexagonCopyToCombine::runOnMachineFunction(MachineFunction &MF) {
457   if (skipFunction(MF.getFunction()))
458     return false;
459 
460   if (IsCombinesDisabled) return false;
461 
462   bool HasChanged = false;
463 
464   // Get target info.
465   ST = &MF.getSubtarget<HexagonSubtarget>();
466   TRI = ST->getRegisterInfo();
467   TII = ST->getInstrInfo();
468 
469   const Function &F = MF.getFunction();
470   bool OptForSize = F.hasFnAttribute(Attribute::OptimizeForSize);
471 
472   // Combine aggressively (for code size)
473   ShouldCombineAggressively =
474     MF.getTarget().getOptLevel() <= CodeGenOpt::Default;
475 
476   // Disable CONST64 for tiny core since it takes a LD resource.
477   if (!OptForSize && ST->isTinyCore())
478     IsConst64Disabled = true;
479 
480   // Traverse basic blocks.
481   for (MachineBasicBlock &MBB : MF) {
482     PotentiallyNewifiableTFR.clear();
483     findPotentialNewifiableTFRs(MBB);
484 
485     // Traverse instructions in basic block.
486     for (MachineBasicBlock::iterator MI = MBB.begin(), End = MBB.end();
487          MI != End;) {
488       MachineInstr &I1 = *MI++;
489 
490       if (I1.isDebugInstr())
491         continue;
492 
493       // Don't combine a TFR whose user could be newified (instructions that
494       // define double registers can not be newified - Programmer's Ref Manual
495       // 5.4.2 New-value stores).
496       if (ShouldCombineAggressively && PotentiallyNewifiableTFR.count(&I1))
497         continue;
498 
499       // Ignore instructions that are not combinable.
500       if (!isCombinableInstType(I1, TII, ShouldCombineAggressively))
501         continue;
502 
503       // Find a second instruction that can be merged into a combine
504       // instruction. In addition, also find all the debug instructions that
505       // need to be moved along with it.
506       bool DoInsertAtI1 = false;
507       DbgMItoMove.clear();
508       MachineInstr *I2 = findPairable(I1, DoInsertAtI1, OptForSize);
509       if (I2) {
510         HasChanged = true;
511         combine(I1, *I2, MI, DoInsertAtI1, OptForSize);
512       }
513     }
514   }
515 
516   return HasChanged;
517 }
518 
519 /// findPairable - Returns an instruction that can be merged with \p I1 into a
520 /// COMBINE instruction or 0 if no such instruction can be found. Returns true
521 /// in \p DoInsertAtI1 if the combine must be inserted at instruction \p I1
522 /// false if the combine must be inserted at the returned instruction.
523 MachineInstr *HexagonCopyToCombine::findPairable(MachineInstr &I1,
524                                                  bool &DoInsertAtI1,
525                                                  bool AllowC64) {
526   MachineBasicBlock::iterator I2 = std::next(MachineBasicBlock::iterator(I1));
527   while (I2 != I1.getParent()->end() && I2->isDebugInstr())
528     ++I2;
529 
530   Register I1DestReg = I1.getOperand(0).getReg();
531 
532   for (MachineBasicBlock::iterator End = I1.getParent()->end(); I2 != End;
533        ++I2) {
534     // Bail out early if we see a second definition of I1DestReg.
535     if (I2->modifiesRegister(I1DestReg, TRI))
536       break;
537 
538     // Ignore non-combinable instructions.
539     if (!isCombinableInstType(*I2, TII, ShouldCombineAggressively))
540       continue;
541 
542     // Don't combine a TFR whose user could be newified.
543     if (ShouldCombineAggressively && PotentiallyNewifiableTFR.count(&*I2))
544       continue;
545 
546     Register I2DestReg = I2->getOperand(0).getReg();
547 
548     // Check that registers are adjacent and that the first destination register
549     // is even.
550     bool IsI1LowReg = (I2DestReg - I1DestReg) == 1;
551     bool IsI2LowReg = (I1DestReg - I2DestReg) == 1;
552     unsigned FirstRegIndex = IsI1LowReg ? I1DestReg : I2DestReg;
553     if ((!IsI1LowReg && !IsI2LowReg) || !isEvenReg(FirstRegIndex))
554       continue;
555 
556     // Check that the two instructions are combinable.
557     // The order matters because in a A2_tfrsi we might can encode a int8 as
558     // the hi reg operand but only a uint6 as the low reg operand.
559     if ((IsI2LowReg && !areCombinableOperations(TRI, I1, *I2, AllowC64)) ||
560         (IsI1LowReg && !areCombinableOperations(TRI, *I2, I1, AllowC64)))
561       break;
562 
563     if (isSafeToMoveTogether(I1, *I2, I1DestReg, I2DestReg, DoInsertAtI1))
564       return &*I2;
565 
566     // Not safe. Stop searching.
567     break;
568   }
569   return nullptr;
570 }
571 
572 void HexagonCopyToCombine::combine(MachineInstr &I1, MachineInstr &I2,
573                                    MachineBasicBlock::iterator &MI,
574                                    bool DoInsertAtI1, bool OptForSize) {
575   // We are going to delete I2. If MI points to I2 advance it to the next
576   // instruction.
577   if (MI == I2.getIterator())
578     ++MI;
579 
580   // Figure out whether I1 or I2 goes into the lowreg part.
581   Register I1DestReg = I1.getOperand(0).getReg();
582   Register I2DestReg = I2.getOperand(0).getReg();
583   bool IsI1Loreg = (I2DestReg - I1DestReg) == 1;
584   unsigned LoRegDef = IsI1Loreg ? I1DestReg : I2DestReg;
585   unsigned SubLo;
586 
587   const TargetRegisterClass *SuperRC = nullptr;
588   if (Hexagon::IntRegsRegClass.contains(LoRegDef)) {
589     SuperRC = &Hexagon::DoubleRegsRegClass;
590     SubLo = Hexagon::isub_lo;
591   } else if (Hexagon::HvxVRRegClass.contains(LoRegDef)) {
592     assert(ST->useHVXOps());
593     SuperRC = &Hexagon::HvxWRRegClass;
594     SubLo = Hexagon::vsub_lo;
595   } else
596     llvm_unreachable("Unexpected register class");
597 
598   // Get the double word register.
599   unsigned DoubleRegDest = TRI->getMatchingSuperReg(LoRegDef, SubLo, SuperRC);
600   assert(DoubleRegDest != 0 && "Expect a valid register");
601 
602   // Setup source operands.
603   MachineOperand &LoOperand = IsI1Loreg ? I1.getOperand(1) : I2.getOperand(1);
604   MachineOperand &HiOperand = IsI1Loreg ? I2.getOperand(1) : I1.getOperand(1);
605 
606   // Figure out which source is a register and which a constant.
607   bool IsHiReg = HiOperand.isReg();
608   bool IsLoReg = LoOperand.isReg();
609 
610   // There is a combine of two constant extended values into CONST64.
611   bool IsC64 = OptForSize && LoOperand.isImm() && HiOperand.isImm() &&
612                isGreaterThanNBitTFRI<16>(I1) && isGreaterThanNBitTFRI<16>(I2);
613 
614   MachineBasicBlock::iterator InsertPt(DoInsertAtI1 ? I1 : I2);
615   // Emit combine.
616   if (IsHiReg && IsLoReg)
617     emitCombineRR(InsertPt, DoubleRegDest, HiOperand, LoOperand);
618   else if (IsHiReg)
619     emitCombineRI(InsertPt, DoubleRegDest, HiOperand, LoOperand);
620   else if (IsLoReg)
621     emitCombineIR(InsertPt, DoubleRegDest, HiOperand, LoOperand);
622   else if (IsC64 && !IsConst64Disabled)
623     emitConst64(InsertPt, DoubleRegDest, HiOperand, LoOperand);
624   else
625     emitCombineII(InsertPt, DoubleRegDest, HiOperand, LoOperand);
626 
627   // Move debug instructions along with I1 if it's being
628   // moved towards I2.
629   if (!DoInsertAtI1 && DbgMItoMove.size() != 0) {
630     // Insert debug instructions at the new location before I2.
631     MachineBasicBlock *BB = InsertPt->getParent();
632     for (auto NewMI : DbgMItoMove) {
633       // If iterator MI is pointing to DEBUG_VAL, make sure
634       // MI now points to next relevant instruction.
635       if (NewMI == MI)
636         ++MI;
637       BB->splice(InsertPt, BB, NewMI);
638     }
639   }
640 
641   I1.eraseFromParent();
642   I2.eraseFromParent();
643 }
644 
645 void HexagonCopyToCombine::emitConst64(MachineBasicBlock::iterator &InsertPt,
646                                        unsigned DoubleDestReg,
647                                        MachineOperand &HiOperand,
648                                        MachineOperand &LoOperand) {
649   LLVM_DEBUG(dbgs() << "Found a CONST64\n");
650 
651   DebugLoc DL = InsertPt->getDebugLoc();
652   MachineBasicBlock *BB = InsertPt->getParent();
653   assert(LoOperand.isImm() && HiOperand.isImm() &&
654          "Both operands must be immediate");
655 
656   int64_t V = HiOperand.getImm();
657   V = (V << 32) | (0x0ffffffffLL & LoOperand.getImm());
658   BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::CONST64), DoubleDestReg)
659     .addImm(V);
660 }
661 
662 void HexagonCopyToCombine::emitCombineII(MachineBasicBlock::iterator &InsertPt,
663                                          unsigned DoubleDestReg,
664                                          MachineOperand &HiOperand,
665                                          MachineOperand &LoOperand) {
666   DebugLoc DL = InsertPt->getDebugLoc();
667   MachineBasicBlock *BB = InsertPt->getParent();
668 
669   // Handle globals.
670   if (HiOperand.isGlobal()) {
671     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
672       .addGlobalAddress(HiOperand.getGlobal(), HiOperand.getOffset(),
673                         HiOperand.getTargetFlags())
674       .addImm(LoOperand.getImm());
675     return;
676   }
677   if (LoOperand.isGlobal()) {
678     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
679       .addImm(HiOperand.getImm())
680       .addGlobalAddress(LoOperand.getGlobal(), LoOperand.getOffset(),
681                         LoOperand.getTargetFlags());
682     return;
683   }
684 
685   // Handle block addresses.
686   if (HiOperand.isBlockAddress()) {
687     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
688       .addBlockAddress(HiOperand.getBlockAddress(), HiOperand.getOffset(),
689                        HiOperand.getTargetFlags())
690       .addImm(LoOperand.getImm());
691     return;
692   }
693   if (LoOperand.isBlockAddress()) {
694     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
695       .addImm(HiOperand.getImm())
696       .addBlockAddress(LoOperand.getBlockAddress(), LoOperand.getOffset(),
697                        LoOperand.getTargetFlags());
698     return;
699   }
700 
701   // Handle jump tables.
702   if (HiOperand.isJTI()) {
703     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
704       .addJumpTableIndex(HiOperand.getIndex(), HiOperand.getTargetFlags())
705       .addImm(LoOperand.getImm());
706     return;
707   }
708   if (LoOperand.isJTI()) {
709     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
710       .addImm(HiOperand.getImm())
711       .addJumpTableIndex(LoOperand.getIndex(), LoOperand.getTargetFlags());
712     return;
713   }
714 
715   // Handle constant pools.
716   if (HiOperand.isCPI()) {
717     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
718       .addConstantPoolIndex(HiOperand.getIndex(), HiOperand.getOffset(),
719                             HiOperand.getTargetFlags())
720       .addImm(LoOperand.getImm());
721     return;
722   }
723   if (LoOperand.isCPI()) {
724     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
725       .addImm(HiOperand.getImm())
726       .addConstantPoolIndex(LoOperand.getIndex(), LoOperand.getOffset(),
727                             LoOperand.getTargetFlags());
728     return;
729   }
730 
731   // First preference should be given to Hexagon::A2_combineii instruction
732   // as it can include U6 (in Hexagon::A4_combineii) as well.
733   // In this instruction, HiOperand is const extended, if required.
734   if (isInt<8>(LoOperand.getImm())) {
735     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
736       .addImm(HiOperand.getImm())
737       .addImm(LoOperand.getImm());
738       return;
739   }
740 
741   // In this instruction, LoOperand is const extended, if required.
742   if (isInt<8>(HiOperand.getImm())) {
743     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
744       .addImm(HiOperand.getImm())
745       .addImm(LoOperand.getImm());
746     return;
747   }
748 
749   // Insert new combine instruction.
750   //  DoubleRegDest = combine #HiImm, #LoImm
751   BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
752     .addImm(HiOperand.getImm())
753     .addImm(LoOperand.getImm());
754 }
755 
756 void HexagonCopyToCombine::emitCombineIR(MachineBasicBlock::iterator &InsertPt,
757                                          unsigned DoubleDestReg,
758                                          MachineOperand &HiOperand,
759                                          MachineOperand &LoOperand) {
760   Register LoReg = LoOperand.getReg();
761   unsigned LoRegKillFlag = getKillRegState(LoOperand.isKill());
762 
763   DebugLoc DL = InsertPt->getDebugLoc();
764   MachineBasicBlock *BB = InsertPt->getParent();
765 
766   // Handle globals.
767   if (HiOperand.isGlobal()) {
768     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
769       .addGlobalAddress(HiOperand.getGlobal(), HiOperand.getOffset(),
770                         HiOperand.getTargetFlags())
771       .addReg(LoReg, LoRegKillFlag);
772     return;
773   }
774   // Handle block addresses.
775   if (HiOperand.isBlockAddress()) {
776     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
777       .addBlockAddress(HiOperand.getBlockAddress(), HiOperand.getOffset(),
778                        HiOperand.getTargetFlags())
779       .addReg(LoReg, LoRegKillFlag);
780     return;
781   }
782   // Handle jump tables.
783   if (HiOperand.isJTI()) {
784     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
785       .addJumpTableIndex(HiOperand.getIndex(), HiOperand.getTargetFlags())
786       .addReg(LoReg, LoRegKillFlag);
787     return;
788   }
789   // Handle constant pools.
790   if (HiOperand.isCPI()) {
791     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
792       .addConstantPoolIndex(HiOperand.getIndex(), HiOperand.getOffset(),
793                             HiOperand.getTargetFlags())
794       .addReg(LoReg, LoRegKillFlag);
795     return;
796   }
797   // Insert new combine instruction.
798   //  DoubleRegDest = combine #HiImm, LoReg
799   BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
800     .addImm(HiOperand.getImm())
801     .addReg(LoReg, LoRegKillFlag);
802 }
803 
804 void HexagonCopyToCombine::emitCombineRI(MachineBasicBlock::iterator &InsertPt,
805                                          unsigned DoubleDestReg,
806                                          MachineOperand &HiOperand,
807                                          MachineOperand &LoOperand) {
808   unsigned HiRegKillFlag = getKillRegState(HiOperand.isKill());
809   Register HiReg = HiOperand.getReg();
810 
811   DebugLoc DL = InsertPt->getDebugLoc();
812   MachineBasicBlock *BB = InsertPt->getParent();
813 
814   // Handle global.
815   if (LoOperand.isGlobal()) {
816     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
817       .addReg(HiReg, HiRegKillFlag)
818       .addGlobalAddress(LoOperand.getGlobal(), LoOperand.getOffset(),
819                         LoOperand.getTargetFlags());
820     return;
821   }
822   // Handle block addresses.
823   if (LoOperand.isBlockAddress()) {
824     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
825       .addReg(HiReg, HiRegKillFlag)
826       .addBlockAddress(LoOperand.getBlockAddress(), LoOperand.getOffset(),
827                        LoOperand.getTargetFlags());
828     return;
829   }
830   // Handle jump tables.
831   if (LoOperand.isJTI()) {
832     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
833       .addReg(HiOperand.getReg(), HiRegKillFlag)
834       .addJumpTableIndex(LoOperand.getIndex(), LoOperand.getTargetFlags());
835     return;
836   }
837   // Handle constant pools.
838   if (LoOperand.isCPI()) {
839     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
840       .addReg(HiOperand.getReg(), HiRegKillFlag)
841       .addConstantPoolIndex(LoOperand.getIndex(), LoOperand.getOffset(),
842                             LoOperand.getTargetFlags());
843     return;
844   }
845 
846   // Insert new combine instruction.
847   //  DoubleRegDest = combine HiReg, #LoImm
848   BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
849     .addReg(HiReg, HiRegKillFlag)
850     .addImm(LoOperand.getImm());
851 }
852 
853 void HexagonCopyToCombine::emitCombineRR(MachineBasicBlock::iterator &InsertPt,
854                                          unsigned DoubleDestReg,
855                                          MachineOperand &HiOperand,
856                                          MachineOperand &LoOperand) {
857   unsigned LoRegKillFlag = getKillRegState(LoOperand.isKill());
858   unsigned HiRegKillFlag = getKillRegState(HiOperand.isKill());
859   Register LoReg = LoOperand.getReg();
860   Register HiReg = HiOperand.getReg();
861 
862   DebugLoc DL = InsertPt->getDebugLoc();
863   MachineBasicBlock *BB = InsertPt->getParent();
864 
865   // Insert new combine instruction.
866   //  DoubleRegDest = combine HiReg, LoReg
867   unsigned NewOpc;
868   if (Hexagon::DoubleRegsRegClass.contains(DoubleDestReg)) {
869     NewOpc = Hexagon::A2_combinew;
870   } else if (Hexagon::HvxWRRegClass.contains(DoubleDestReg)) {
871     assert(ST->useHVXOps());
872     NewOpc = Hexagon::V6_vcombine;
873   } else
874     llvm_unreachable("Unexpected register");
875 
876   BuildMI(*BB, InsertPt, DL, TII->get(NewOpc), DoubleDestReg)
877     .addReg(HiReg, HiRegKillFlag)
878     .addReg(LoReg, LoRegKillFlag);
879 }
880 
881 FunctionPass *llvm::createHexagonCopyToCombine() {
882   return new HexagonCopyToCombine();
883 }
884