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