LLVM API Documentation

HexagonCopyToCombine.cpp
Go to the documentation of this file.
00001 //===------- HexagonCopyToCombine.cpp - Hexagon Copy-To-Combine Pass ------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 // This pass replaces transfer instructions by combine instructions.
00010 // We walk along a basic block and look for two combinable instructions and try
00011 // to move them together. If we can move them next to each other we do so and
00012 // replace them with a combine instruction.
00013 //===----------------------------------------------------------------------===//
00014 #include "llvm/PassSupport.h"
00015 #include "Hexagon.h"
00016 #include "HexagonInstrInfo.h"
00017 #include "HexagonMachineFunctionInfo.h"
00018 #include "HexagonRegisterInfo.h"
00019 #include "HexagonSubtarget.h"
00020 #include "HexagonTargetMachine.h"
00021 #include "llvm/ADT/DenseMap.h"
00022 #include "llvm/ADT/DenseSet.h"
00023 #include "llvm/CodeGen/MachineBasicBlock.h"
00024 #include "llvm/CodeGen/MachineFunction.h"
00025 #include "llvm/CodeGen/MachineFunctionPass.h"
00026 #include "llvm/CodeGen/MachineInstr.h"
00027 #include "llvm/CodeGen/MachineInstrBuilder.h"
00028 #include "llvm/CodeGen/Passes.h"
00029 #include "llvm/Support/CodeGen.h"
00030 #include "llvm/Support/CommandLine.h"
00031 #include "llvm/Support/Debug.h"
00032 #include "llvm/Support/raw_ostream.h"
00033 #include "llvm/Target/TargetRegisterInfo.h"
00034 
00035 using namespace llvm;
00036 
00037 #define DEBUG_TYPE "hexagon-copy-combine"
00038 
00039 static
00040 cl::opt<bool> IsCombinesDisabled("disable-merge-into-combines",
00041                                  cl::Hidden, cl::ZeroOrMore,
00042                                  cl::init(false),
00043                                  cl::desc("Disable merging into combines"));
00044 static
00045 cl::opt<unsigned>
00046 MaxNumOfInstsBetweenNewValueStoreAndTFR("max-num-inst-between-tfr-and-nv-store",
00047                    cl::Hidden, cl::init(4),
00048                    cl::desc("Maximum distance between a tfr feeding a store we "
00049                             "consider the store still to be newifiable"));
00050 
00051 namespace llvm {
00052   void initializeHexagonCopyToCombinePass(PassRegistry&);
00053 }
00054 
00055 
00056 namespace {
00057 
00058 class HexagonCopyToCombine : public MachineFunctionPass  {
00059   const HexagonInstrInfo *TII;
00060   const TargetRegisterInfo *TRI;
00061   bool ShouldCombineAggressively;
00062 
00063   DenseSet<MachineInstr *> PotentiallyNewifiableTFR;
00064 public:
00065   static char ID;
00066 
00067   HexagonCopyToCombine() : MachineFunctionPass(ID) {
00068     initializeHexagonCopyToCombinePass(*PassRegistry::getPassRegistry());
00069   }
00070 
00071   void getAnalysisUsage(AnalysisUsage &AU) const override {
00072     MachineFunctionPass::getAnalysisUsage(AU);
00073   }
00074 
00075   const char *getPassName() const override {
00076     return "Hexagon Copy-To-Combine Pass";
00077   }
00078 
00079   bool runOnMachineFunction(MachineFunction &Fn) override;
00080 
00081 private:
00082   MachineInstr *findPairable(MachineInstr *I1, bool &DoInsertAtI1);
00083 
00084   void findPotentialNewifiableTFRs(MachineBasicBlock &);
00085 
00086   void combine(MachineInstr *I1, MachineInstr *I2,
00087                MachineBasicBlock::iterator &MI, bool DoInsertAtI1);
00088 
00089   bool isSafeToMoveTogether(MachineInstr *I1, MachineInstr *I2,
00090                             unsigned I1DestReg, unsigned I2DestReg,
00091                             bool &DoInsertAtI1);
00092 
00093   void emitCombineRR(MachineBasicBlock::iterator &Before, unsigned DestReg,
00094                      MachineOperand &HiOperand, MachineOperand &LoOperand);
00095 
00096   void emitCombineRI(MachineBasicBlock::iterator &Before, unsigned DestReg,
00097                      MachineOperand &HiOperand, MachineOperand &LoOperand);
00098 
00099   void emitCombineIR(MachineBasicBlock::iterator &Before, unsigned DestReg,
00100                      MachineOperand &HiOperand, MachineOperand &LoOperand);
00101 
00102   void emitCombineII(MachineBasicBlock::iterator &Before, unsigned DestReg,
00103                      MachineOperand &HiOperand, MachineOperand &LoOperand);
00104 };
00105 
00106 } // End anonymous namespace.
00107 
00108 char HexagonCopyToCombine::ID = 0;
00109 
00110 INITIALIZE_PASS(HexagonCopyToCombine, "hexagon-copy-combine",
00111                 "Hexagon Copy-To-Combine Pass", false, false)
00112 
00113 static bool isCombinableInstType(MachineInstr *MI,
00114                                  const HexagonInstrInfo *TII,
00115                                  bool ShouldCombineAggressively) {
00116   switch(MI->getOpcode()) {
00117   case Hexagon::TFR: {
00118     // A COPY instruction can be combined if its arguments are IntRegs (32bit).
00119     assert(MI->getOperand(0).isReg() && MI->getOperand(1).isReg());
00120 
00121     unsigned DestReg = MI->getOperand(0).getReg();
00122     unsigned SrcReg = MI->getOperand(1).getReg();
00123     return Hexagon::IntRegsRegClass.contains(DestReg) &&
00124       Hexagon::IntRegsRegClass.contains(SrcReg);
00125   }
00126 
00127   case Hexagon::TFRI: {
00128     // A transfer-immediate can be combined if its argument is a signed 8bit
00129     // value.
00130     assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
00131     unsigned DestReg = MI->getOperand(0).getReg();
00132 
00133     // Only combine constant extended TFRI if we are in aggressive mode.
00134     return Hexagon::IntRegsRegClass.contains(DestReg) &&
00135       (ShouldCombineAggressively || isInt<8>(MI->getOperand(1).getImm()));
00136   }
00137 
00138   case Hexagon::TFRI_V4: {
00139     if (!ShouldCombineAggressively)
00140       return false;
00141     assert(MI->getOperand(0).isReg() && MI->getOperand(1).isGlobal());
00142 
00143     // Ensure that TargetFlags are MO_NO_FLAG for a global. This is a
00144     // workaround for an ABI bug that prevents GOT relocations on combine
00145     // instructions
00146     if (MI->getOperand(1).getTargetFlags() != HexagonII::MO_NO_FLAG)
00147       return false;
00148 
00149     unsigned DestReg = MI->getOperand(0).getReg();
00150     return Hexagon::IntRegsRegClass.contains(DestReg);
00151   }
00152 
00153   default:
00154     break;
00155   }
00156 
00157   return false;
00158 }
00159 
00160 static bool isGreaterThan8BitTFRI(MachineInstr *I) {
00161   return I->getOpcode() == Hexagon::TFRI &&
00162     !isInt<8>(I->getOperand(1).getImm());
00163 }
00164 static bool isGreaterThan6BitTFRI(MachineInstr *I) {
00165   return I->getOpcode() == Hexagon::TFRI &&
00166     !isUInt<6>(I->getOperand(1).getImm());
00167 }
00168 
00169 /// areCombinableOperations - Returns true if the two instruction can be merge
00170 /// into a combine (ignoring register constraints).
00171 static bool areCombinableOperations(const TargetRegisterInfo *TRI,
00172                                     MachineInstr *HighRegInst,
00173                                     MachineInstr *LowRegInst) {
00174   assert((HighRegInst->getOpcode() == Hexagon::TFR ||
00175           HighRegInst->getOpcode() == Hexagon::TFRI ||
00176           HighRegInst->getOpcode() == Hexagon::TFRI_V4) &&
00177          (LowRegInst->getOpcode() == Hexagon::TFR ||
00178           LowRegInst->getOpcode() == Hexagon::TFRI ||
00179           LowRegInst->getOpcode() == Hexagon::TFRI_V4) &&
00180          "Assume individual instructions are of a combinable type");
00181 
00182   const HexagonRegisterInfo *QRI =
00183     static_cast<const HexagonRegisterInfo *>(TRI);
00184 
00185   // V4 added some combine variations (mixed immediate and register source
00186   // operands), if we are on < V4 we can only combine 2 register-to-register
00187   // moves and 2 immediate-to-register moves. We also don't have
00188   // constant-extenders.
00189   if (!QRI->Subtarget.hasV4TOps())
00190     return HighRegInst->getOpcode() == LowRegInst->getOpcode() &&
00191       !isGreaterThan8BitTFRI(HighRegInst) &&
00192       !isGreaterThan6BitTFRI(LowRegInst);
00193 
00194   // There is no combine of two constant extended values.
00195   if ((HighRegInst->getOpcode() == Hexagon::TFRI_V4 ||
00196        isGreaterThan8BitTFRI(HighRegInst)) &&
00197       (LowRegInst->getOpcode() == Hexagon::TFRI_V4 ||
00198        isGreaterThan6BitTFRI(LowRegInst)))
00199     return false;
00200 
00201   return true;
00202 }
00203 
00204 static bool isEvenReg(unsigned Reg) {
00205   assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
00206          Hexagon::IntRegsRegClass.contains(Reg));
00207   return (Reg - Hexagon::R0) % 2 == 0;
00208 }
00209 
00210 static void removeKillInfo(MachineInstr *MI, unsigned RegNotKilled) {
00211   for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
00212     MachineOperand &Op = MI->getOperand(I);
00213     if (!Op.isReg() || Op.getReg() != RegNotKilled || !Op.isKill())
00214       continue;
00215     Op.setIsKill(false);
00216   }
00217 }
00218 
00219 /// isUnsafeToMoveAcross - Returns true if it is unsafe to move a copy
00220 /// instruction from \p UseReg to \p DestReg over the instruction \p I.
00221 static bool isUnsafeToMoveAcross(MachineInstr *I, unsigned UseReg,
00222                                   unsigned DestReg,
00223                                   const TargetRegisterInfo *TRI) {
00224   return (UseReg && (I->modifiesRegister(UseReg, TRI))) ||
00225           I->modifiesRegister(DestReg, TRI) ||
00226           I->readsRegister(DestReg, TRI) ||
00227           I->hasUnmodeledSideEffects() ||
00228           I->isInlineAsm() || I->isDebugValue();
00229 }
00230 
00231 /// isSafeToMoveTogether - Returns true if it is safe to move I1 next to I2 such
00232 /// that the two instructions can be paired in a combine.
00233 bool HexagonCopyToCombine::isSafeToMoveTogether(MachineInstr *I1,
00234                                                 MachineInstr *I2,
00235                                                 unsigned I1DestReg,
00236                                                 unsigned I2DestReg,
00237                                                 bool &DoInsertAtI1) {
00238 
00239   bool IsImmUseReg = I2->getOperand(1).isImm() || I2->getOperand(1).isGlobal();
00240   unsigned I2UseReg = IsImmUseReg ? 0 : I2->getOperand(1).getReg();
00241 
00242   // It is not safe to move I1 and I2 into one combine if I2 has a true
00243   // dependence on I1.
00244   if (I2UseReg && I1->modifiesRegister(I2UseReg, TRI))
00245     return false;
00246 
00247   bool isSafe = true;
00248 
00249   // First try to move I2 towards I1.
00250   {
00251     // A reverse_iterator instantiated like below starts before I2, and I1
00252     // respectively.
00253     // Look at instructions I in between I2 and (excluding) I1.
00254     MachineBasicBlock::reverse_iterator I(I2),
00255       End = --(MachineBasicBlock::reverse_iterator(I1));
00256     // At 03 we got better results (dhrystone!) by being more conservative.
00257     if (!ShouldCombineAggressively)
00258       End = MachineBasicBlock::reverse_iterator(I1);
00259     // If I2 kills its operand and we move I2 over an instruction that also
00260     // uses I2's use reg we need to modify that (first) instruction to now kill
00261     // this reg.
00262     unsigned KilledOperand = 0;
00263     if (I2->killsRegister(I2UseReg))
00264       KilledOperand = I2UseReg;
00265     MachineInstr *KillingInstr = nullptr;
00266 
00267     for (; I != End; ++I) {
00268       // If the intervening instruction I:
00269       //   * modifies I2's use reg
00270       //   * modifies I2's def reg
00271       //   * reads I2's def reg
00272       //   * or has unmodelled side effects
00273       // we can't move I2 across it.
00274       if (isUnsafeToMoveAcross(&*I, I2UseReg, I2DestReg, TRI)) {
00275         isSafe = false;
00276         break;
00277       }
00278 
00279       // Update first use of the killed operand.
00280       if (!KillingInstr && KilledOperand &&
00281           I->readsRegister(KilledOperand, TRI))
00282         KillingInstr = &*I;
00283     }
00284     if (isSafe) {
00285       // Update the intermediate instruction to with the kill flag.
00286       if (KillingInstr) {
00287         bool Added = KillingInstr->addRegisterKilled(KilledOperand, TRI, true);
00288         (void)Added; // suppress compiler warning
00289         assert(Added && "Must successfully update kill flag");
00290         removeKillInfo(I2, KilledOperand);
00291       }
00292       DoInsertAtI1 = true;
00293       return true;
00294     }
00295   }
00296 
00297   // Try to move I1 towards I2.
00298   {
00299     // Look at instructions I in between I1 and (excluding) I2.
00300     MachineBasicBlock::iterator I(I1), End(I2);
00301     // At O3 we got better results (dhrystone) by being more conservative here.
00302     if (!ShouldCombineAggressively)
00303       End = std::next(MachineBasicBlock::iterator(I2));
00304     IsImmUseReg = I1->getOperand(1).isImm() || I1->getOperand(1).isGlobal();
00305     unsigned I1UseReg = IsImmUseReg ? 0 : I1->getOperand(1).getReg();
00306     // Track killed operands. If we move across an instruction that kills our
00307     // operand, we need to update the kill information on the moved I1. It kills
00308     // the operand now.
00309     MachineInstr *KillingInstr = nullptr;
00310     unsigned KilledOperand = 0;
00311 
00312     while(++I != End) {
00313       // If the intervening instruction I:
00314       //   * modifies I1's use reg
00315       //   * modifies I1's def reg
00316       //   * reads I1's def reg
00317       //   * or has unmodelled side effects
00318       //   We introduce this special case because llvm has no api to remove a
00319       //   kill flag for a register (a removeRegisterKilled() analogous to
00320       //   addRegisterKilled) that handles aliased register correctly.
00321       //   * or has a killed aliased register use of I1's use reg
00322       //           %D4<def> = TFRI64 16
00323       //           %R6<def> = TFR %R9
00324       //           %R8<def> = KILL %R8, %D4<imp-use,kill>
00325       //      If we want to move R6 = across the KILL instruction we would have
00326       //      to remove the %D4<imp-use,kill> operand. For now, we are
00327       //      conservative and disallow the move.
00328       // we can't move I1 across it.
00329       if (isUnsafeToMoveAcross(I, I1UseReg, I1DestReg, TRI) ||
00330           // Check for an aliased register kill. Bail out if we see one.
00331           (!I->killsRegister(I1UseReg) && I->killsRegister(I1UseReg, TRI)))
00332         return false;
00333 
00334       // Check for an exact kill (registers match).
00335       if (I1UseReg && I->killsRegister(I1UseReg)) {
00336         assert(!KillingInstr && "Should only see one killing instruction");
00337         KilledOperand = I1UseReg;
00338         KillingInstr = &*I;
00339       }
00340     }
00341     if (KillingInstr) {
00342       removeKillInfo(KillingInstr, KilledOperand);
00343       // Update I1 to set the kill flag. This flag will later be picked up by
00344       // the new COMBINE instruction.
00345       bool Added = I1->addRegisterKilled(KilledOperand, TRI);
00346       (void)Added; // suppress compiler warning
00347       assert(Added && "Must successfully update kill flag");
00348     }
00349     DoInsertAtI1 = false;
00350   }
00351 
00352   return true;
00353 }
00354 
00355 /// findPotentialNewifiableTFRs - Finds tranfers that feed stores that could be
00356 /// newified. (A use of a 64 bit register define can not be newified)
00357 void
00358 HexagonCopyToCombine::findPotentialNewifiableTFRs(MachineBasicBlock &BB) {
00359   DenseMap<unsigned, MachineInstr *> LastDef;
00360   for (MachineBasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
00361     MachineInstr *MI = I;
00362     // Mark TFRs that feed a potential new value store as such.
00363     if(TII->mayBeNewStore(MI)) {
00364       // Look for uses of TFR instructions.
00365       for (unsigned OpdIdx = 0, OpdE = MI->getNumOperands(); OpdIdx != OpdE;
00366            ++OpdIdx) {
00367         MachineOperand &Op = MI->getOperand(OpdIdx);
00368 
00369         // Skip over anything except register uses.
00370         if (!Op.isReg() || !Op.isUse() || !Op.getReg())
00371           continue;
00372 
00373         // Look for the defining instruction.
00374         unsigned Reg = Op.getReg();
00375         MachineInstr *DefInst = LastDef[Reg];
00376         if (!DefInst)
00377           continue;
00378         if (!isCombinableInstType(DefInst, TII, ShouldCombineAggressively))
00379           continue;
00380 
00381         // Only close newifiable stores should influence the decision.
00382         MachineBasicBlock::iterator It(DefInst);
00383         unsigned NumInstsToDef = 0;
00384         while (&*It++ != MI)
00385           ++NumInstsToDef;
00386 
00387         if (NumInstsToDef > MaxNumOfInstsBetweenNewValueStoreAndTFR)
00388           continue;
00389 
00390         PotentiallyNewifiableTFR.insert(DefInst);
00391       }
00392       // Skip to next instruction.
00393       continue;
00394     }
00395 
00396     // Put instructions that last defined integer or double registers into the
00397     // map.
00398     for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
00399       MachineOperand &Op = MI->getOperand(I);
00400       if (!Op.isReg() || !Op.isDef() || !Op.getReg())
00401         continue;
00402       unsigned Reg = Op.getReg();
00403       if (Hexagon::DoubleRegsRegClass.contains(Reg)) {
00404         for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
00405           LastDef[*SubRegs] = MI;
00406         }
00407       } else if (Hexagon::IntRegsRegClass.contains(Reg))
00408         LastDef[Reg] = MI;
00409     }
00410   }
00411 }
00412 
00413 bool HexagonCopyToCombine::runOnMachineFunction(MachineFunction &MF) {
00414 
00415   if (IsCombinesDisabled) return false;
00416 
00417   bool HasChanged = false;
00418 
00419   // Get target info.
00420   TRI = MF.getSubtarget().getRegisterInfo();
00421   TII = static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo());
00422 
00423   // Combine aggressively (for code size)
00424   ShouldCombineAggressively =
00425     MF.getTarget().getOptLevel() <= CodeGenOpt::Default;
00426 
00427   // Traverse basic blocks.
00428   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); BI != BE;
00429        ++BI) {
00430     PotentiallyNewifiableTFR.clear();
00431     findPotentialNewifiableTFRs(*BI);
00432 
00433     // Traverse instructions in basic block.
00434     for(MachineBasicBlock::iterator MI = BI->begin(), End = BI->end();
00435         MI != End;) {
00436       MachineInstr *I1 = MI++;
00437       // Don't combine a TFR whose user could be newified (instructions that
00438       // define double registers can not be newified - Programmer's Ref Manual
00439       // 5.4.2 New-value stores).
00440       if (ShouldCombineAggressively && PotentiallyNewifiableTFR.count(I1))
00441         continue;
00442 
00443       // Ignore instructions that are not combinable.
00444       if (!isCombinableInstType(I1, TII, ShouldCombineAggressively))
00445         continue;
00446 
00447       // Find a second instruction that can be merged into a combine
00448       // instruction.
00449       bool DoInsertAtI1 = false;
00450       MachineInstr *I2 = findPairable(I1, DoInsertAtI1);
00451       if (I2) {
00452         HasChanged = true;
00453         combine(I1, I2, MI, DoInsertAtI1);
00454       }
00455     }
00456   }
00457 
00458   return HasChanged;
00459 }
00460 
00461 /// findPairable - Returns an instruction that can be merged with \p I1 into a
00462 /// COMBINE instruction or 0 if no such instruction can be found. Returns true
00463 /// in \p DoInsertAtI1 if the combine must be inserted at instruction \p I1
00464 /// false if the combine must be inserted at the returned instruction.
00465 MachineInstr *HexagonCopyToCombine::findPairable(MachineInstr *I1,
00466                                                  bool &DoInsertAtI1) {
00467   MachineBasicBlock::iterator I2 = std::next(MachineBasicBlock::iterator(I1));
00468   unsigned I1DestReg = I1->getOperand(0).getReg();
00469 
00470   for (MachineBasicBlock::iterator End = I1->getParent()->end(); I2 != End;
00471        ++I2) {
00472     // Bail out early if we see a second definition of I1DestReg.
00473     if (I2->modifiesRegister(I1DestReg, TRI))
00474       break;
00475 
00476     // Ignore non-combinable instructions.
00477     if (!isCombinableInstType(I2, TII, ShouldCombineAggressively))
00478       continue;
00479 
00480     // Don't combine a TFR whose user could be newified.
00481     if (ShouldCombineAggressively && PotentiallyNewifiableTFR.count(I2))
00482       continue;
00483 
00484     unsigned I2DestReg = I2->getOperand(0).getReg();
00485 
00486     // Check that registers are adjacent and that the first destination register
00487     // is even.
00488     bool IsI1LowReg = (I2DestReg - I1DestReg) == 1;
00489     bool IsI2LowReg = (I1DestReg - I2DestReg) == 1;
00490     unsigned FirstRegIndex = IsI1LowReg ? I1DestReg : I2DestReg;
00491     if ((!IsI1LowReg && !IsI2LowReg) || !isEvenReg(FirstRegIndex))
00492       continue;
00493 
00494     // Check that the two instructions are combinable. V4 allows more
00495     // instructions to be merged into a combine.
00496     // The order matters because in a TFRI we might can encode a int8 as the
00497     // hi reg operand but only a uint6 as the low reg operand.
00498     if ((IsI2LowReg && !areCombinableOperations(TRI, I1, I2)) ||
00499         (IsI1LowReg && !areCombinableOperations(TRI, I2, I1)))
00500       break;
00501 
00502     if (isSafeToMoveTogether(I1, I2, I1DestReg, I2DestReg,
00503                              DoInsertAtI1))
00504       return I2;
00505 
00506     // Not safe. Stop searching.
00507     break;
00508   }
00509   return nullptr;
00510 }
00511 
00512 void HexagonCopyToCombine::combine(MachineInstr *I1, MachineInstr *I2,
00513                                    MachineBasicBlock::iterator &MI,
00514                                    bool DoInsertAtI1) {
00515   // We are going to delete I2. If MI points to I2 advance it to the next
00516   // instruction.
00517   if ((MachineInstr *)MI == I2) ++MI;
00518 
00519   // Figure out whether I1 or I2 goes into the lowreg part.
00520   unsigned I1DestReg = I1->getOperand(0).getReg();
00521   unsigned I2DestReg = I2->getOperand(0).getReg();
00522   bool IsI1Loreg = (I2DestReg - I1DestReg) == 1;
00523   unsigned LoRegDef = IsI1Loreg ? I1DestReg : I2DestReg;
00524 
00525   // Get the double word register.
00526   unsigned DoubleRegDest =
00527     TRI->getMatchingSuperReg(LoRegDef, Hexagon::subreg_loreg,
00528                              &Hexagon::DoubleRegsRegClass);
00529   assert(DoubleRegDest != 0 && "Expect a valid register");
00530 
00531 
00532   // Setup source operands.
00533   MachineOperand &LoOperand = IsI1Loreg ? I1->getOperand(1) :
00534     I2->getOperand(1);
00535   MachineOperand &HiOperand = IsI1Loreg ? I2->getOperand(1) :
00536     I1->getOperand(1);
00537 
00538   // Figure out which source is a register and which a constant.
00539   bool IsHiReg = HiOperand.isReg();
00540   bool IsLoReg = LoOperand.isReg();
00541 
00542   MachineBasicBlock::iterator InsertPt(DoInsertAtI1 ? I1 : I2);
00543   // Emit combine.
00544   if (IsHiReg && IsLoReg)
00545     emitCombineRR(InsertPt, DoubleRegDest, HiOperand, LoOperand);
00546   else if (IsHiReg)
00547     emitCombineRI(InsertPt, DoubleRegDest, HiOperand, LoOperand);
00548   else if (IsLoReg)
00549     emitCombineIR(InsertPt, DoubleRegDest, HiOperand, LoOperand);
00550   else
00551     emitCombineII(InsertPt, DoubleRegDest, HiOperand, LoOperand);
00552 
00553   I1->eraseFromParent();
00554   I2->eraseFromParent();
00555 }
00556 
00557 void HexagonCopyToCombine::emitCombineII(MachineBasicBlock::iterator &InsertPt,
00558                                          unsigned DoubleDestReg,
00559                                          MachineOperand &HiOperand,
00560                                          MachineOperand &LoOperand) {
00561   DebugLoc DL = InsertPt->getDebugLoc();
00562   MachineBasicBlock *BB = InsertPt->getParent();
00563 
00564   // Handle  globals.
00565   if (HiOperand.isGlobal()) {
00566     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::COMBINE_Ii), DoubleDestReg)
00567       .addGlobalAddress(HiOperand.getGlobal(), HiOperand.getOffset(),
00568                         HiOperand.getTargetFlags())
00569       .addImm(LoOperand.getImm());
00570     return;
00571   }
00572   if (LoOperand.isGlobal()) {
00573     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::COMBINE_iI_V4), DoubleDestReg)
00574       .addImm(HiOperand.getImm())
00575       .addGlobalAddress(LoOperand.getGlobal(), LoOperand.getOffset(),
00576                         LoOperand.getTargetFlags());
00577     return;
00578   }
00579 
00580   // Handle constant extended immediates.
00581   if (!isInt<8>(HiOperand.getImm())) {
00582     assert(isInt<8>(LoOperand.getImm()));
00583     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::COMBINE_Ii), DoubleDestReg)
00584       .addImm(HiOperand.getImm())
00585       .addImm(LoOperand.getImm());
00586     return;
00587   }
00588 
00589   if (!isUInt<6>(LoOperand.getImm())) {
00590     assert(isInt<8>(HiOperand.getImm()));
00591     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::COMBINE_iI_V4), DoubleDestReg)
00592       .addImm(HiOperand.getImm())
00593       .addImm(LoOperand.getImm());
00594     return;
00595   }
00596 
00597   // Insert new combine instruction.
00598   //  DoubleRegDest = combine #HiImm, #LoImm
00599   BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::COMBINE_Ii), DoubleDestReg)
00600     .addImm(HiOperand.getImm())
00601     .addImm(LoOperand.getImm());
00602 }
00603 
00604 void HexagonCopyToCombine::emitCombineIR(MachineBasicBlock::iterator &InsertPt,
00605                                          unsigned DoubleDestReg,
00606                                          MachineOperand &HiOperand,
00607                                          MachineOperand &LoOperand) {
00608   unsigned LoReg = LoOperand.getReg();
00609   unsigned LoRegKillFlag = getKillRegState(LoOperand.isKill());
00610 
00611   DebugLoc DL = InsertPt->getDebugLoc();
00612   MachineBasicBlock *BB = InsertPt->getParent();
00613 
00614   // Handle global.
00615   if (HiOperand.isGlobal()) {
00616     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::COMBINE_Ir_V4), DoubleDestReg)
00617       .addGlobalAddress(HiOperand.getGlobal(), HiOperand.getOffset(),
00618                         HiOperand.getTargetFlags())
00619       .addReg(LoReg, LoRegKillFlag);
00620     return;
00621   }
00622   // Insert new combine instruction.
00623   //  DoubleRegDest = combine #HiImm, LoReg
00624   BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::COMBINE_Ir_V4), DoubleDestReg)
00625     .addImm(HiOperand.getImm())
00626     .addReg(LoReg, LoRegKillFlag);
00627 }
00628 
00629 void HexagonCopyToCombine::emitCombineRI(MachineBasicBlock::iterator &InsertPt,
00630                                          unsigned DoubleDestReg,
00631                                          MachineOperand &HiOperand,
00632                                          MachineOperand &LoOperand) {
00633   unsigned HiRegKillFlag = getKillRegState(HiOperand.isKill());
00634   unsigned HiReg = HiOperand.getReg();
00635 
00636   DebugLoc DL = InsertPt->getDebugLoc();
00637   MachineBasicBlock *BB = InsertPt->getParent();
00638 
00639   // Handle global.
00640   if (LoOperand.isGlobal()) {
00641     BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::COMBINE_rI_V4), DoubleDestReg)
00642       .addReg(HiReg, HiRegKillFlag)
00643       .addGlobalAddress(LoOperand.getGlobal(), LoOperand.getOffset(),
00644                         LoOperand.getTargetFlags());
00645     return;
00646   }
00647 
00648   // Insert new combine instruction.
00649   //  DoubleRegDest = combine HiReg, #LoImm
00650   BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::COMBINE_rI_V4), DoubleDestReg)
00651     .addReg(HiReg, HiRegKillFlag)
00652     .addImm(LoOperand.getImm());
00653 }
00654 
00655 void HexagonCopyToCombine::emitCombineRR(MachineBasicBlock::iterator &InsertPt,
00656                                          unsigned DoubleDestReg,
00657                                          MachineOperand &HiOperand,
00658                                          MachineOperand &LoOperand) {
00659   unsigned LoRegKillFlag = getKillRegState(LoOperand.isKill());
00660   unsigned HiRegKillFlag = getKillRegState(HiOperand.isKill());
00661   unsigned LoReg = LoOperand.getReg();
00662   unsigned HiReg = HiOperand.getReg();
00663 
00664   DebugLoc DL = InsertPt->getDebugLoc();
00665   MachineBasicBlock *BB = InsertPt->getParent();
00666 
00667   // Insert new combine instruction.
00668   //  DoubleRegDest = combine HiReg, LoReg
00669   BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::COMBINE_rr), DoubleDestReg)
00670     .addReg(HiReg, HiRegKillFlag)
00671     .addReg(LoReg, LoRegKillFlag);
00672 }
00673 
00674 FunctionPass *llvm::createHexagonCopyToCombine() {
00675   return new HexagonCopyToCombine();
00676 }