LLVM API Documentation

HexagonInstrInfo.cpp
Go to the documentation of this file.
00001 //===-- HexagonInstrInfo.cpp - Hexagon Instruction Information ------------===//
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 //
00010 // This file contains the Hexagon implementation of the TargetInstrInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "HexagonInstrInfo.h"
00015 #include "Hexagon.h"
00016 #include "HexagonRegisterInfo.h"
00017 #include "HexagonSubtarget.h"
00018 #include "llvm/ADT/STLExtras.h"
00019 #include "llvm/ADT/SmallVector.h"
00020 #include "llvm/CodeGen/DFAPacketizer.h"
00021 #include "llvm/CodeGen/MachineFrameInfo.h"
00022 #include "llvm/CodeGen/MachineInstrBuilder.h"
00023 #include "llvm/CodeGen/MachineMemOperand.h"
00024 #include "llvm/CodeGen/MachineRegisterInfo.h"
00025 #include "llvm/CodeGen/PseudoSourceValue.h"
00026 #include "llvm/Support/Debug.h"
00027 #include "llvm/Support/MathExtras.h"
00028 #include "llvm/Support/raw_ostream.h"
00029 #define GET_INSTRINFO_CTOR
00030 #define GET_INSTRMAP_INFO
00031 #include "HexagonGenInstrInfo.inc"
00032 #include "HexagonGenDFAPacketizer.inc"
00033 
00034 using namespace llvm;
00035 
00036 ///
00037 /// Constants for Hexagon instructions.
00038 ///
00039 const int Hexagon_MEMW_OFFSET_MAX = 4095;
00040 const int Hexagon_MEMW_OFFSET_MIN = -4096;
00041 const int Hexagon_MEMD_OFFSET_MAX = 8191;
00042 const int Hexagon_MEMD_OFFSET_MIN = -8192;
00043 const int Hexagon_MEMH_OFFSET_MAX = 2047;
00044 const int Hexagon_MEMH_OFFSET_MIN = -2048;
00045 const int Hexagon_MEMB_OFFSET_MAX = 1023;
00046 const int Hexagon_MEMB_OFFSET_MIN = -1024;
00047 const int Hexagon_ADDI_OFFSET_MAX = 32767;
00048 const int Hexagon_ADDI_OFFSET_MIN = -32768;
00049 const int Hexagon_MEMD_AUTOINC_MAX = 56;
00050 const int Hexagon_MEMD_AUTOINC_MIN = -64;
00051 const int Hexagon_MEMW_AUTOINC_MAX = 28;
00052 const int Hexagon_MEMW_AUTOINC_MIN = -32;
00053 const int Hexagon_MEMH_AUTOINC_MAX = 14;
00054 const int Hexagon_MEMH_AUTOINC_MIN = -16;
00055 const int Hexagon_MEMB_AUTOINC_MAX = 7;
00056 const int Hexagon_MEMB_AUTOINC_MIN = -8;
00057 
00058 
00059 HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
00060   : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
00061     RI(ST, *this), Subtarget(ST) {
00062 }
00063 
00064 
00065 /// isLoadFromStackSlot - If the specified machine instruction is a direct
00066 /// load from a stack slot, return the virtual or physical register number of
00067 /// the destination along with the FrameIndex of the loaded stack slot.  If
00068 /// not, return 0.  This predicate must return 0 if the instruction has
00069 /// any side effects other than loading from the stack slot.
00070 unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
00071                                              int &FrameIndex) const {
00072 
00073 
00074   switch (MI->getOpcode()) {
00075   default: break;
00076   case Hexagon::LDriw:
00077   case Hexagon::LDrid:
00078   case Hexagon::LDrih:
00079   case Hexagon::LDrib:
00080   case Hexagon::LDriub:
00081     if (MI->getOperand(2).isFI() &&
00082         MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
00083       FrameIndex = MI->getOperand(2).getIndex();
00084       return MI->getOperand(0).getReg();
00085     }
00086     break;
00087   }
00088   return 0;
00089 }
00090 
00091 
00092 /// isStoreToStackSlot - If the specified machine instruction is a direct
00093 /// store to a stack slot, return the virtual or physical register number of
00094 /// the source reg along with the FrameIndex of the loaded stack slot.  If
00095 /// not, return 0.  This predicate must return 0 if the instruction has
00096 /// any side effects other than storing to the stack slot.
00097 unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
00098                                             int &FrameIndex) const {
00099   switch (MI->getOpcode()) {
00100   default: break;
00101   case Hexagon::STriw:
00102   case Hexagon::STrid:
00103   case Hexagon::STrih:
00104   case Hexagon::STrib:
00105     if (MI->getOperand(2).isFI() &&
00106         MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
00107       FrameIndex = MI->getOperand(0).getIndex();
00108       return MI->getOperand(2).getReg();
00109     }
00110     break;
00111   }
00112   return 0;
00113 }
00114 
00115 
00116 unsigned
00117 HexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
00118                              MachineBasicBlock *FBB,
00119                              const SmallVectorImpl<MachineOperand> &Cond,
00120                              DebugLoc DL) const{
00121 
00122     int BOpc   = Hexagon::JMP;
00123     int BccOpc = Hexagon::JMP_t;
00124 
00125     assert(TBB && "InsertBranch must not be told to insert a fallthrough");
00126 
00127     int regPos = 0;
00128     // Check if ReverseBranchCondition has asked to reverse this branch
00129     // If we want to reverse the branch an odd number of times, we want
00130     // JMP_f.
00131     if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
00132       BccOpc = Hexagon::JMP_f;
00133       regPos = 1;
00134     }
00135 
00136     if (FBB == 0) {
00137       if (Cond.empty()) {
00138         // Due to a bug in TailMerging/CFG Optimization, we need to add a
00139         // special case handling of a predicated jump followed by an
00140         // unconditional jump. If not, Tail Merging and CFG Optimization go
00141         // into an infinite loop.
00142         MachineBasicBlock *NewTBB, *NewFBB;
00143         SmallVector<MachineOperand, 4> Cond;
00144         MachineInstr *Term = MBB.getFirstTerminator();
00145         if (isPredicated(Term) && !AnalyzeBranch(MBB, NewTBB, NewFBB, Cond,
00146                                                  false)) {
00147           MachineBasicBlock *NextBB =
00148             llvm::next(MachineFunction::iterator(&MBB));
00149           if (NewTBB == NextBB) {
00150             ReverseBranchCondition(Cond);
00151             RemoveBranch(MBB);
00152             return InsertBranch(MBB, TBB, 0, Cond, DL);
00153           }
00154         }
00155         BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
00156       } else {
00157         BuildMI(&MBB, DL,
00158                 get(BccOpc)).addReg(Cond[regPos].getReg()).addMBB(TBB);
00159       }
00160       return 1;
00161     }
00162 
00163     BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[regPos].getReg()).addMBB(TBB);
00164     BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
00165 
00166     return 2;
00167 }
00168 
00169 
00170 bool HexagonInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
00171                                      MachineBasicBlock *&TBB,
00172                                  MachineBasicBlock *&FBB,
00173                                  SmallVectorImpl<MachineOperand> &Cond,
00174                                  bool AllowModify) const {
00175   TBB = NULL;
00176   FBB = NULL;
00177 
00178   // If the block has no terminators, it just falls into the block after it.
00179   MachineBasicBlock::instr_iterator I = MBB.instr_end();
00180   if (I == MBB.instr_begin())
00181     return false;
00182 
00183   // A basic block may looks like this:
00184   //
00185   //  [   insn
00186   //     EH_LABEL
00187   //      insn
00188   //      insn
00189   //      insn
00190   //     EH_LABEL
00191   //      insn     ]
00192   //
00193   // It has two succs but does not have a terminator
00194   // Don't know how to handle it.
00195   do {
00196     --I;
00197     if (I->isEHLabel())
00198       return true;
00199   } while (I != MBB.instr_begin());
00200 
00201   I = MBB.instr_end();
00202   --I;
00203 
00204   while (I->isDebugValue()) {
00205     if (I == MBB.instr_begin())
00206       return false;
00207     --I;
00208   }
00209 
00210   // Delete the JMP if it's equivalent to a fall-through.
00211   if (AllowModify && I->getOpcode() == Hexagon::JMP &&
00212       MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
00213     DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
00214     I->eraseFromParent();
00215     I = MBB.instr_end();
00216     if (I == MBB.instr_begin())
00217       return false;
00218     --I;
00219   }
00220   if (!isUnpredicatedTerminator(I))
00221     return false;
00222 
00223   // Get the last instruction in the block.
00224   MachineInstr *LastInst = I;
00225   MachineInstr *SecondLastInst = NULL;
00226   // Find one more terminator if present.
00227   do {
00228     if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(I)) {
00229       if (!SecondLastInst)
00230         SecondLastInst = I;
00231       else
00232         // This is a third branch.
00233         return true;
00234     }
00235     if (I == MBB.instr_begin())
00236       break;
00237     --I;
00238   } while(I);
00239 
00240   int LastOpcode = LastInst->getOpcode();
00241 
00242   bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
00243   bool LastOpcodeHasNot = PredOpcodeHasNot(LastOpcode);
00244 
00245   // If there is only one terminator instruction, process it.
00246   if (LastInst && !SecondLastInst) {
00247     if (LastOpcode == Hexagon::JMP) {
00248       TBB = LastInst->getOperand(0).getMBB();
00249       return false;
00250     }
00251     if (LastOpcode == Hexagon::ENDLOOP0) {
00252       TBB = LastInst->getOperand(0).getMBB();
00253       Cond.push_back(LastInst->getOperand(0));
00254       return false;
00255     }
00256     if (LastOpcodeHasJMP_c) {
00257       TBB = LastInst->getOperand(1).getMBB();
00258       if (LastOpcodeHasNot) {
00259         Cond.push_back(MachineOperand::CreateImm(0));
00260       }
00261       Cond.push_back(LastInst->getOperand(0));
00262       return false;
00263     }
00264     // Otherwise, don't know what this is.
00265     return true;
00266   }
00267 
00268   int SecLastOpcode = SecondLastInst->getOpcode();
00269 
00270   bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
00271   bool SecLastOpcodeHasNot = PredOpcodeHasNot(SecLastOpcode);
00272   if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::JMP)) {
00273     TBB =  SecondLastInst->getOperand(1).getMBB();
00274     if (SecLastOpcodeHasNot)
00275       Cond.push_back(MachineOperand::CreateImm(0));
00276     Cond.push_back(SecondLastInst->getOperand(0));
00277     FBB = LastInst->getOperand(0).getMBB();
00278     return false;
00279   }
00280 
00281   // If the block ends with two Hexagon:JMPs, handle it.  The second one is not
00282   // executed, so remove it.
00283   if (SecLastOpcode == Hexagon::JMP && LastOpcode == Hexagon::JMP) {
00284     TBB = SecondLastInst->getOperand(0).getMBB();
00285     I = LastInst;
00286     if (AllowModify)
00287       I->eraseFromParent();
00288     return false;
00289   }
00290 
00291   // If the block ends with an ENDLOOP, and JMP, handle it.
00292   if (SecLastOpcode == Hexagon::ENDLOOP0 &&
00293       LastOpcode == Hexagon::JMP) {
00294     TBB = SecondLastInst->getOperand(0).getMBB();
00295     Cond.push_back(SecondLastInst->getOperand(0));
00296     FBB = LastInst->getOperand(0).getMBB();
00297     return false;
00298   }
00299 
00300   // Otherwise, can't handle this.
00301   return true;
00302 }
00303 
00304 
00305 unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
00306   int BOpc   = Hexagon::JMP;
00307   int BccOpc = Hexagon::JMP_t;
00308   int BccOpcNot = Hexagon::JMP_f;
00309 
00310   MachineBasicBlock::iterator I = MBB.end();
00311   if (I == MBB.begin()) return 0;
00312   --I;
00313   if (I->getOpcode() != BOpc && I->getOpcode() != BccOpc &&
00314       I->getOpcode() != BccOpcNot)
00315     return 0;
00316 
00317   // Remove the branch.
00318   I->eraseFromParent();
00319 
00320   I = MBB.end();
00321 
00322   if (I == MBB.begin()) return 1;
00323   --I;
00324   if (I->getOpcode() != BccOpc && I->getOpcode() != BccOpcNot)
00325     return 1;
00326 
00327   // Remove the branch.
00328   I->eraseFromParent();
00329   return 2;
00330 }
00331 
00332 
00333 /// \brief For a comparison instruction, return the source registers in
00334 /// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
00335 /// compares against in CmpValue. Return true if the comparison instruction
00336 /// can be analyzed.
00337 bool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
00338                                       unsigned &SrcReg, unsigned &SrcReg2,
00339                                       int &Mask, int &Value) const {
00340   unsigned Opc = MI->getOpcode();
00341 
00342   // Set mask and the first source register.
00343   switch (Opc) {
00344     case Hexagon::CMPEHexagon4rr:
00345     case Hexagon::CMPEQri:
00346     case Hexagon::CMPEQrr:
00347     case Hexagon::CMPGT64rr:
00348     case Hexagon::CMPGTU64rr:
00349     case Hexagon::CMPGTUri:
00350     case Hexagon::CMPGTUrr:
00351     case Hexagon::CMPGTri:
00352     case Hexagon::CMPGTrr:
00353       SrcReg = MI->getOperand(1).getReg();
00354       Mask = ~0;
00355       break;
00356     case Hexagon::CMPbEQri_V4:
00357     case Hexagon::CMPbEQrr_sbsb_V4:
00358     case Hexagon::CMPbEQrr_ubub_V4:
00359     case Hexagon::CMPbGTUri_V4:
00360     case Hexagon::CMPbGTUrr_V4:
00361     case Hexagon::CMPbGTrr_V4:
00362       SrcReg = MI->getOperand(1).getReg();
00363       Mask = 0xFF;
00364       break;
00365     case Hexagon::CMPhEQri_V4:
00366     case Hexagon::CMPhEQrr_shl_V4:
00367     case Hexagon::CMPhEQrr_xor_V4:
00368     case Hexagon::CMPhGTUri_V4:
00369     case Hexagon::CMPhGTUrr_V4:
00370     case Hexagon::CMPhGTrr_shl_V4:
00371       SrcReg = MI->getOperand(1).getReg();
00372       Mask = 0xFFFF;
00373       break;
00374   }
00375 
00376   // Set the value/second source register.
00377   switch (Opc) {
00378     case Hexagon::CMPEHexagon4rr:
00379     case Hexagon::CMPEQrr:
00380     case Hexagon::CMPGT64rr:
00381     case Hexagon::CMPGTU64rr:
00382     case Hexagon::CMPGTUrr:
00383     case Hexagon::CMPGTrr:
00384     case Hexagon::CMPbEQrr_sbsb_V4:
00385     case Hexagon::CMPbEQrr_ubub_V4:
00386     case Hexagon::CMPbGTUrr_V4:
00387     case Hexagon::CMPbGTrr_V4:
00388     case Hexagon::CMPhEQrr_shl_V4:
00389     case Hexagon::CMPhEQrr_xor_V4:
00390     case Hexagon::CMPhGTUrr_V4:
00391     case Hexagon::CMPhGTrr_shl_V4:
00392       SrcReg2 = MI->getOperand(2).getReg();
00393       return true;
00394 
00395     case Hexagon::CMPEQri:
00396     case Hexagon::CMPGTUri:
00397     case Hexagon::CMPGTri:
00398     case Hexagon::CMPbEQri_V4:
00399     case Hexagon::CMPbGTUri_V4:
00400     case Hexagon::CMPhEQri_V4:
00401     case Hexagon::CMPhGTUri_V4:
00402       SrcReg2 = 0;
00403       Value = MI->getOperand(2).getImm();
00404       return true;
00405   }
00406 
00407   return false;
00408 }
00409 
00410 
00411 void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
00412                                  MachineBasicBlock::iterator I, DebugLoc DL,
00413                                  unsigned DestReg, unsigned SrcReg,
00414                                  bool KillSrc) const {
00415   if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
00416     BuildMI(MBB, I, DL, get(Hexagon::TFR), DestReg).addReg(SrcReg);
00417     return;
00418   }
00419   if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
00420     BuildMI(MBB, I, DL, get(Hexagon::TFR64), DestReg).addReg(SrcReg);
00421     return;
00422   }
00423   if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
00424     // Map Pd = Ps to Pd = or(Ps, Ps).
00425     BuildMI(MBB, I, DL, get(Hexagon::OR_pp),
00426             DestReg).addReg(SrcReg).addReg(SrcReg);
00427     return;
00428   }
00429   if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
00430       Hexagon::IntRegsRegClass.contains(SrcReg)) {
00431     // We can have an overlap between single and double reg: r1:0 = r0.
00432     if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
00433         // r1:0 = r0
00434         BuildMI(MBB, I, DL, get(Hexagon::TFRI), (RI.getSubReg(DestReg,
00435                 Hexagon::subreg_hireg))).addImm(0);
00436     } else {
00437         // r1:0 = r1 or no overlap.
00438         BuildMI(MBB, I, DL, get(Hexagon::TFR), (RI.getSubReg(DestReg,
00439                 Hexagon::subreg_loreg))).addReg(SrcReg);
00440         BuildMI(MBB, I, DL, get(Hexagon::TFRI), (RI.getSubReg(DestReg,
00441                 Hexagon::subreg_hireg))).addImm(0);
00442     }
00443     return;
00444   }
00445   if (Hexagon::CRRegsRegClass.contains(DestReg) &&
00446       Hexagon::IntRegsRegClass.contains(SrcReg)) {
00447     BuildMI(MBB, I, DL, get(Hexagon::TFCR), DestReg).addReg(SrcReg);
00448     return;
00449   }
00450   if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
00451       Hexagon::IntRegsRegClass.contains(DestReg)) {
00452     BuildMI(MBB, I, DL, get(Hexagon::TFR_RsPd), DestReg).
00453       addReg(SrcReg, getKillRegState(KillSrc));
00454     return;
00455   }
00456   if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
00457       Hexagon::PredRegsRegClass.contains(DestReg)) {
00458     BuildMI(MBB, I, DL, get(Hexagon::TFR_PdRs), DestReg).
00459       addReg(SrcReg, getKillRegState(KillSrc));
00460     return;
00461   }
00462 
00463   llvm_unreachable("Unimplemented");
00464 }
00465 
00466 
00467 void HexagonInstrInfo::
00468 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
00469                     unsigned SrcReg, bool isKill, int FI,
00470                     const TargetRegisterClass *RC,
00471                     const TargetRegisterInfo *TRI) const {
00472 
00473   DebugLoc DL = MBB.findDebugLoc(I);
00474   MachineFunction &MF = *MBB.getParent();
00475   MachineFrameInfo &MFI = *MF.getFrameInfo();
00476   unsigned Align = MFI.getObjectAlignment(FI);
00477 
00478   MachineMemOperand *MMO =
00479       MF.getMachineMemOperand(
00480                       MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
00481                       MachineMemOperand::MOStore,
00482                       MFI.getObjectSize(FI),
00483                       Align);
00484 
00485   if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
00486     BuildMI(MBB, I, DL, get(Hexagon::STriw))
00487           .addFrameIndex(FI).addImm(0)
00488           .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
00489   } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
00490     BuildMI(MBB, I, DL, get(Hexagon::STrid))
00491           .addFrameIndex(FI).addImm(0)
00492           .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
00493   } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
00494     BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
00495           .addFrameIndex(FI).addImm(0)
00496           .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
00497   } else {
00498     llvm_unreachable("Unimplemented");
00499   }
00500 }
00501 
00502 
00503 void HexagonInstrInfo::storeRegToAddr(
00504                                  MachineFunction &MF, unsigned SrcReg,
00505                                  bool isKill,
00506                                  SmallVectorImpl<MachineOperand> &Addr,
00507                                  const TargetRegisterClass *RC,
00508                                  SmallVectorImpl<MachineInstr*> &NewMIs) const
00509 {
00510   llvm_unreachable("Unimplemented");
00511 }
00512 
00513 
00514 void HexagonInstrInfo::
00515 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
00516                      unsigned DestReg, int FI,
00517                      const TargetRegisterClass *RC,
00518                      const TargetRegisterInfo *TRI) const {
00519   DebugLoc DL = MBB.findDebugLoc(I);
00520   MachineFunction &MF = *MBB.getParent();
00521   MachineFrameInfo &MFI = *MF.getFrameInfo();
00522   unsigned Align = MFI.getObjectAlignment(FI);
00523 
00524   MachineMemOperand *MMO =
00525       MF.getMachineMemOperand(
00526                       MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
00527                       MachineMemOperand::MOLoad,
00528                       MFI.getObjectSize(FI),
00529                       Align);
00530   if (RC == &Hexagon::IntRegsRegClass) {
00531     BuildMI(MBB, I, DL, get(Hexagon::LDriw), DestReg)
00532           .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
00533   } else if (RC == &Hexagon::DoubleRegsRegClass) {
00534     BuildMI(MBB, I, DL, get(Hexagon::LDrid), DestReg)
00535           .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
00536   } else if (RC == &Hexagon::PredRegsRegClass) {
00537     BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
00538           .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
00539   } else {
00540     llvm_unreachable("Can't store this register to stack slot");
00541   }
00542 }
00543 
00544 
00545 void HexagonInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
00546                                         SmallVectorImpl<MachineOperand> &Addr,
00547                                         const TargetRegisterClass *RC,
00548                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
00549   llvm_unreachable("Unimplemented");
00550 }
00551 
00552 
00553 MachineInstr *HexagonInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
00554                                                     MachineInstr* MI,
00555                                           const SmallVectorImpl<unsigned> &Ops,
00556                                                     int FI) const {
00557   // Hexagon_TODO: Implement.
00558   return(0);
00559 }
00560 
00561 MachineInstr*
00562 HexagonInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
00563                                            int FrameIx, uint64_t Offset,
00564                                            const MDNode *MDPtr,
00565                                            DebugLoc DL) const {
00566   MachineInstrBuilder MIB = BuildMI(MF, DL, get(Hexagon::DBG_VALUE))
00567     .addImm(0).addImm(Offset).addMetadata(MDPtr);
00568   return &*MIB;
00569 }
00570 
00571 unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
00572 
00573   MachineRegisterInfo &RegInfo = MF->getRegInfo();
00574   const TargetRegisterClass *TRC;
00575   if (VT == MVT::i1) {
00576     TRC = &Hexagon::PredRegsRegClass;
00577   } else if (VT == MVT::i32 || VT == MVT::f32) {
00578     TRC = &Hexagon::IntRegsRegClass;
00579   } else if (VT == MVT::i64 || VT == MVT::f64) {
00580     TRC = &Hexagon::DoubleRegsRegClass;
00581   } else {
00582     llvm_unreachable("Cannot handle this register class");
00583   }
00584 
00585   unsigned NewReg = RegInfo.createVirtualRegister(TRC);
00586   return NewReg;
00587 }
00588 
00589 bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
00590   // Constant extenders are allowed only for V4 and above.
00591   if (!Subtarget.hasV4TOps())
00592     return false;
00593 
00594   const MCInstrDesc &MID = MI->getDesc();
00595   const uint64_t F = MID.TSFlags;
00596   if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
00597     return true;
00598 
00599   // TODO: This is largely obsolete now. Will need to be removed
00600   // in consecutive patches.
00601   switch(MI->getOpcode()) {
00602     // TFR_FI Remains a special case.
00603     case Hexagon::TFR_FI:
00604       return true;
00605     default:
00606       return false;
00607   }
00608   return  false;
00609 }
00610 
00611 // This returns true in two cases:
00612 // - The OP code itself indicates that this is an extended instruction.
00613 // - One of MOs has been marked with HMOTF_ConstExtended flag.
00614 bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
00615   // First check if this is permanently extended op code.
00616   const uint64_t F = MI->getDesc().TSFlags;
00617   if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
00618     return true;
00619   // Use MO operand flags to determine if one of MI's operands
00620   // has HMOTF_ConstExtended flag set.
00621   for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
00622        E = MI->operands_end(); I != E; ++I) {
00623     if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
00624       return true;
00625   }
00626   return  false;
00627 }
00628 
00629 bool HexagonInstrInfo::isBranch (const MachineInstr *MI) const {
00630   return MI->getDesc().isBranch();
00631 }
00632 
00633 bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
00634   if (isNewValueJump(MI))
00635     return true;
00636 
00637   if (isNewValueStore(MI))
00638     return true;
00639 
00640   return false;
00641 }
00642 
00643 bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
00644   return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4;
00645 }
00646 
00647 bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
00648   bool isPred = MI->getDesc().isPredicable();
00649 
00650   if (!isPred)
00651     return false;
00652 
00653   const int Opc = MI->getOpcode();
00654 
00655   switch(Opc) {
00656   case Hexagon::TFRI:
00657     return isInt<12>(MI->getOperand(1).getImm());
00658 
00659   case Hexagon::STrid:
00660   case Hexagon::STrid_indexed:
00661     return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
00662 
00663   case Hexagon::STriw:
00664   case Hexagon::STriw_indexed:
00665   case Hexagon::STriw_nv_V4:
00666     return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
00667 
00668   case Hexagon::STrih:
00669   case Hexagon::STrih_indexed:
00670   case Hexagon::STrih_nv_V4:
00671     return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
00672 
00673   case Hexagon::STrib:
00674   case Hexagon::STrib_indexed:
00675   case Hexagon::STrib_nv_V4:
00676     return isUInt<6>(MI->getOperand(1).getImm());
00677 
00678   case Hexagon::LDrid:
00679   case Hexagon::LDrid_indexed:
00680     return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
00681 
00682   case Hexagon::LDriw:
00683   case Hexagon::LDriw_indexed:
00684     return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
00685 
00686   case Hexagon::LDrih:
00687   case Hexagon::LDriuh:
00688   case Hexagon::LDrih_indexed:
00689   case Hexagon::LDriuh_indexed:
00690     return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
00691 
00692   case Hexagon::LDrib:
00693   case Hexagon::LDriub:
00694   case Hexagon::LDrib_indexed:
00695   case Hexagon::LDriub_indexed:
00696     return isUInt<6>(MI->getOperand(2).getImm());
00697 
00698   case Hexagon::POST_LDrid:
00699     return isShiftedInt<4,3>(MI->getOperand(3).getImm());
00700 
00701   case Hexagon::POST_LDriw:
00702     return isShiftedInt<4,2>(MI->getOperand(3).getImm());
00703 
00704   case Hexagon::POST_LDrih:
00705   case Hexagon::POST_LDriuh:
00706     return isShiftedInt<4,1>(MI->getOperand(3).getImm());
00707 
00708   case Hexagon::POST_LDrib:
00709   case Hexagon::POST_LDriub:
00710     return isInt<4>(MI->getOperand(3).getImm());
00711 
00712   case Hexagon::STrib_imm_V4:
00713   case Hexagon::STrih_imm_V4:
00714   case Hexagon::STriw_imm_V4:
00715     return (isUInt<6>(MI->getOperand(1).getImm()) &&
00716             isInt<6>(MI->getOperand(2).getImm()));
00717 
00718   case Hexagon::ADD_ri:
00719     return isInt<8>(MI->getOperand(2).getImm());
00720 
00721   case Hexagon::ASLH:
00722   case Hexagon::ASRH:
00723   case Hexagon::SXTB:
00724   case Hexagon::SXTH:
00725   case Hexagon::ZXTB:
00726   case Hexagon::ZXTH:
00727     return Subtarget.hasV4TOps();
00728   }
00729 
00730   return true;
00731 }
00732 
00733 // This function performs the following inversiones:
00734 //
00735 //  cPt    ---> cNotPt
00736 //  cNotPt ---> cPt
00737 //
00738 unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
00739   int InvPredOpcode;
00740   InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
00741                                         : Hexagon::getTruePredOpcode(Opc);
00742   if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
00743     return InvPredOpcode;
00744 
00745   switch(Opc) {
00746     default: llvm_unreachable("Unexpected predicated instruction");
00747     case Hexagon::COMBINE_rr_cPt:
00748       return Hexagon::COMBINE_rr_cNotPt;
00749     case Hexagon::COMBINE_rr_cNotPt:
00750       return Hexagon::COMBINE_rr_cPt;
00751 
00752       // Dealloc_return.
00753     case Hexagon::DEALLOC_RET_cPt_V4:
00754       return Hexagon::DEALLOC_RET_cNotPt_V4;
00755     case Hexagon::DEALLOC_RET_cNotPt_V4:
00756       return Hexagon::DEALLOC_RET_cPt_V4;
00757   }
00758 }
00759 
00760 // New Value Store instructions.
00761 bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
00762   const uint64_t F = MI->getDesc().TSFlags;
00763 
00764   return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
00765 }
00766 
00767 bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
00768   const uint64_t F = get(Opcode).TSFlags;
00769 
00770   return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
00771 }
00772 
00773 int HexagonInstrInfo::
00774 getMatchingCondBranchOpcode(int Opc, bool invertPredicate) const {
00775   enum Hexagon::PredSense inPredSense;
00776   inPredSense = invertPredicate ? Hexagon::PredSense_false :
00777                                   Hexagon::PredSense_true;
00778   int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
00779   if (CondOpcode >= 0) // Valid Conditional opcode/instruction
00780     return CondOpcode;
00781 
00782   // This switch case will be removed once all the instructions have been
00783   // modified to use relation maps.
00784   switch(Opc) {
00785   case Hexagon::TFRI_f:
00786     return !invertPredicate ? Hexagon::TFRI_cPt_f :
00787                               Hexagon::TFRI_cNotPt_f;
00788   case Hexagon::COMBINE_rr:
00789     return !invertPredicate ? Hexagon::COMBINE_rr_cPt :
00790                               Hexagon::COMBINE_rr_cNotPt;
00791 
00792   // Word.
00793   case Hexagon::STriw_f:
00794     return !invertPredicate ? Hexagon::STriw_cPt :
00795                               Hexagon::STriw_cNotPt;
00796   case Hexagon::STriw_indexed_f:
00797     return !invertPredicate ? Hexagon::STriw_indexed_cPt :
00798                               Hexagon::STriw_indexed_cNotPt;
00799 
00800   // DEALLOC_RETURN.
00801   case Hexagon::DEALLOC_RET_V4:
00802     return !invertPredicate ? Hexagon::DEALLOC_RET_cPt_V4 :
00803                               Hexagon::DEALLOC_RET_cNotPt_V4;
00804   }
00805   llvm_unreachable("Unexpected predicable instruction");
00806 }
00807 
00808 
00809 bool HexagonInstrInfo::
00810 PredicateInstruction(MachineInstr *MI,
00811                      const SmallVectorImpl<MachineOperand> &Cond) const {
00812   int Opc = MI->getOpcode();
00813   assert (isPredicable(MI) && "Expected predicable instruction");
00814   bool invertJump = (!Cond.empty() && Cond[0].isImm() &&
00815                      (Cond[0].getImm() == 0));
00816 
00817   // This will change MI's opcode to its predicate version.
00818   // However, its operand list is still the old one, i.e. the
00819   // non-predicate one.
00820   MI->setDesc(get(getMatchingCondBranchOpcode(Opc, invertJump)));
00821 
00822   int oper = -1;
00823   unsigned int GAIdx = 0;
00824 
00825   // Indicates whether the current MI has a GlobalAddress operand
00826   bool hasGAOpnd = false;
00827   std::vector<MachineOperand> tmpOpnds;
00828 
00829   // Indicates whether we need to shift operands to right.
00830   bool needShift = true;
00831 
00832   // The predicate is ALWAYS the FIRST input operand !!!
00833   if (MI->getNumOperands() == 0) {
00834     // The non-predicate version of MI does not take any operands,
00835     // i.e. no outs and no ins. In this condition, the predicate
00836     // operand will be directly placed at Operands[0]. No operand
00837     // shift is needed.
00838     // Example: BARRIER
00839     needShift = false;
00840     oper = -1;
00841   }
00842   else if (   MI->getOperand(MI->getNumOperands()-1).isReg()
00843            && MI->getOperand(MI->getNumOperands()-1).isDef()
00844            && !MI->getOperand(MI->getNumOperands()-1).isImplicit()) {
00845     // The non-predicate version of MI does not have any input operands.
00846     // In this condition, we extend the length of Operands[] by one and
00847     // copy the original last operand to the newly allocated slot.
00848     // At this moment, it is just a place holder. Later, we will put
00849     // predicate operand directly into it. No operand shift is needed.
00850     // Example: r0=BARRIER (this is a faked insn used here for illustration)
00851     MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
00852     needShift = false;
00853     oper = MI->getNumOperands() - 2;
00854   }
00855   else {
00856     // We need to right shift all input operands by one. Duplicate the
00857     // last operand into the newly allocated slot.
00858     MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
00859   }
00860 
00861   if (needShift)
00862   {
00863     // Operands[ MI->getNumOperands() - 2 ] has been copied into
00864     // Operands[ MI->getNumOperands() - 1 ], so we start from
00865     // Operands[ MI->getNumOperands() - 3 ].
00866     // oper is a signed int.
00867     // It is ok if "MI->getNumOperands()-3" is -3, -2, or -1.
00868     for (oper = MI->getNumOperands() - 3; oper >= 0; --oper)
00869     {
00870       MachineOperand &MO = MI->getOperand(oper);
00871 
00872       // Opnd[0] Opnd[1] Opnd[2] Opnd[3] Opnd[4]   Opnd[5]   Opnd[6]   Opnd[7]
00873       // <Def0>  <Def1>  <Use0>  <Use1>  <ImpDef0> <ImpDef1> <ImpUse0> <ImpUse1>
00874       //               /\~
00875       //              /||\~
00876       //               ||
00877       //        Predicate Operand here
00878       if (MO.isReg() && !MO.isUse() && !MO.isImplicit()) {
00879         break;
00880       }
00881       if (MO.isReg()) {
00882         MI->getOperand(oper+1).ChangeToRegister(MO.getReg(), MO.isDef(),
00883                                                 MO.isImplicit(), MO.isKill(),
00884                                                 MO.isDead(), MO.isUndef(),
00885                                                 MO.isDebug());
00886       }
00887       else if (MO.isImm()) {
00888         MI->getOperand(oper+1).ChangeToImmediate(MO.getImm());
00889       }
00890       else if (MO.isGlobal()) {
00891         // MI can not have more than one GlobalAddress operand.
00892         assert(hasGAOpnd == false && "MI can only have one GlobalAddress opnd");
00893 
00894         // There is no member function called "ChangeToGlobalAddress" in the
00895         // MachineOperand class (not like "ChangeToRegister" and
00896         // "ChangeToImmediate"). So we have to remove them from Operands[] list
00897         // first, and then add them back after we have inserted the predicate
00898         // operand. tmpOpnds[] is to remember these operands before we remove
00899         // them.
00900         tmpOpnds.push_back(MO);
00901 
00902         // Operands[oper] is a GlobalAddress operand;
00903         // Operands[oper+1] has been copied into Operands[oper+2];
00904         hasGAOpnd = true;
00905         GAIdx = oper;
00906         continue;
00907       }
00908       else {
00909         assert(false && "Unexpected operand type");
00910       }
00911     }
00912   }
00913 
00914   int regPos = invertJump ? 1 : 0;
00915   MachineOperand PredMO = Cond[regPos];
00916 
00917   // [oper] now points to the last explicit Def. Predicate operand must be
00918   // located at [oper+1]. See diagram above.
00919   // This assumes that the predicate is always the first operand,
00920   // i.e. Operands[0+numResults], in the set of inputs
00921   // It is better to have an assert here to check this. But I don't know how
00922   // to write this assert because findFirstPredOperandIdx() would return -1
00923   if (oper < -1) oper = -1;
00924 
00925   MI->getOperand(oper+1).ChangeToRegister(PredMO.getReg(), PredMO.isDef(),
00926                                           PredMO.isImplicit(), false,
00927                                           PredMO.isDead(), PredMO.isUndef(),
00928                                           PredMO.isDebug());
00929 
00930   MachineRegisterInfo &RegInfo = MI->getParent()->getParent()->getRegInfo();
00931   RegInfo.clearKillFlags(PredMO.getReg());
00932 
00933   if (hasGAOpnd)
00934   {
00935     unsigned int i;
00936 
00937     // Operands[GAIdx] is the original GlobalAddress operand, which is
00938     // already copied into tmpOpnds[0].
00939     // Operands[GAIdx] now stores a copy of Operands[GAIdx-1]
00940     // Operands[GAIdx+1] has already been copied into Operands[GAIdx+2],
00941     // so we start from [GAIdx+2]
00942     for (i = GAIdx + 2; i < MI->getNumOperands(); ++i)
00943       tmpOpnds.push_back(MI->getOperand(i));
00944 
00945     // Remove all operands in range [ (GAIdx+1) ... (MI->getNumOperands()-1) ]
00946     // It is very important that we always remove from the end of Operands[]
00947     // MI->getNumOperands() is at least 2 if program goes to here.
00948     for (i = MI->getNumOperands() - 1; i > GAIdx; --i)
00949       MI->RemoveOperand(i);
00950 
00951     for (i = 0; i < tmpOpnds.size(); ++i)
00952       MI->addOperand(tmpOpnds[i]);
00953   }
00954 
00955   return true;
00956 }
00957 
00958 
00959 bool
00960 HexagonInstrInfo::
00961 isProfitableToIfCvt(MachineBasicBlock &MBB,
00962                     unsigned NumCycles,
00963                     unsigned ExtraPredCycles,
00964                     const BranchProbability &Probability) const {
00965   return true;
00966 }
00967 
00968 
00969 bool
00970 HexagonInstrInfo::
00971 isProfitableToIfCvt(MachineBasicBlock &TMBB,
00972                     unsigned NumTCycles,
00973                     unsigned ExtraTCycles,
00974                     MachineBasicBlock &FMBB,
00975                     unsigned NumFCycles,
00976                     unsigned ExtraFCycles,
00977                     const BranchProbability &Probability) const {
00978   return true;
00979 }
00980 
00981 // Returns true if an instruction is predicated irrespective of the predicate
00982 // sense. For example, all of the following will return true.
00983 // if (p0) R1 = add(R2, R3)
00984 // if (!p0) R1 = add(R2, R3)
00985 // if (p0.new) R1 = add(R2, R3)
00986 // if (!p0.new) R1 = add(R2, R3)
00987 bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
00988   const uint64_t F = MI->getDesc().TSFlags;
00989 
00990   return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
00991 }
00992 
00993 bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
00994   const uint64_t F = get(Opcode).TSFlags;
00995 
00996   return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
00997 }
00998 
00999 bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
01000   const uint64_t F = MI->getDesc().TSFlags;
01001 
01002   assert(isPredicated(MI));
01003   return (!((F >> HexagonII::PredicatedFalsePos) &
01004             HexagonII::PredicatedFalseMask));
01005 }
01006 
01007 bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
01008   const uint64_t F = get(Opcode).TSFlags;
01009 
01010   // Make sure that the instruction is predicated.
01011   assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
01012   return (!((F >> HexagonII::PredicatedFalsePos) &
01013             HexagonII::PredicatedFalseMask));
01014 }
01015 
01016 bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
01017   const uint64_t F = MI->getDesc().TSFlags;
01018 
01019   assert(isPredicated(MI));
01020   return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
01021 }
01022 
01023 bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
01024   const uint64_t F = get(Opcode).TSFlags;
01025 
01026   assert(isPredicated(Opcode));
01027   return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
01028 }
01029 
01030 // Returns true, if a ST insn can be promoted to a new-value store.
01031 bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
01032   const HexagonRegisterInfo& QRI = getRegisterInfo();
01033   const uint64_t F = MI->getDesc().TSFlags;
01034 
01035   return ((F >> HexagonII::mayNVStorePos) &
01036            HexagonII::mayNVStoreMask &
01037            QRI.Subtarget.hasV4TOps());
01038 }
01039 
01040 bool
01041 HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
01042                                    std::vector<MachineOperand> &Pred) const {
01043   for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
01044     MachineOperand MO = MI->getOperand(oper);
01045     if (MO.isReg() && MO.isDef()) {
01046       const TargetRegisterClass* RC = RI.getMinimalPhysRegClass(MO.getReg());
01047       if (RC == &Hexagon::PredRegsRegClass) {
01048         Pred.push_back(MO);
01049         return true;
01050       }
01051     }
01052   }
01053   return false;
01054 }
01055 
01056 
01057 bool
01058 HexagonInstrInfo::
01059 SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
01060                   const SmallVectorImpl<MachineOperand> &Pred2) const {
01061   // TODO: Fix this
01062   return false;
01063 }
01064 
01065 
01066 //
01067 // We indicate that we want to reverse the branch by
01068 // inserting a 0 at the beginning of the Cond vector.
01069 //
01070 bool HexagonInstrInfo::
01071 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
01072   if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
01073     Cond.erase(Cond.begin());
01074   } else {
01075     Cond.insert(Cond.begin(), MachineOperand::CreateImm(0));
01076   }
01077   return false;
01078 }
01079 
01080 
01081 bool HexagonInstrInfo::
01082 isProfitableToDupForIfCvt(MachineBasicBlock &MBB,unsigned NumInstrs,
01083                           const BranchProbability &Probability) const {
01084   return (NumInstrs <= 4);
01085 }
01086 
01087 bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
01088   switch (MI->getOpcode()) {
01089   default: return false;
01090   case Hexagon::DEALLOC_RET_V4 :
01091   case Hexagon::DEALLOC_RET_cPt_V4 :
01092   case Hexagon::DEALLOC_RET_cNotPt_V4 :
01093   case Hexagon::DEALLOC_RET_cdnPnt_V4 :
01094   case Hexagon::DEALLOC_RET_cNotdnPnt_V4 :
01095   case Hexagon::DEALLOC_RET_cdnPt_V4 :
01096   case Hexagon::DEALLOC_RET_cNotdnPt_V4 :
01097    return true;
01098   }
01099 }
01100 
01101 
01102 bool HexagonInstrInfo::
01103 isValidOffset(const int Opcode, const int Offset) const {
01104   // This function is to check whether the "Offset" is in the correct range of
01105   // the given "Opcode". If "Offset" is not in the correct range, "ADD_ri" is
01106   // inserted to calculate the final address. Due to this reason, the function
01107   // assumes that the "Offset" has correct alignment.
01108   // We used to assert if the offset was not properly aligned, however,
01109   // there are cases where a misaligned pointer recast can cause this
01110   // problem, and we need to allow for it. The front end warns of such
01111   // misaligns with respect to load size.
01112 
01113   switch(Opcode) {
01114 
01115   case Hexagon::LDriw:
01116   case Hexagon::LDriw_indexed:
01117   case Hexagon::LDriw_f:
01118   case Hexagon::STriw_indexed:
01119   case Hexagon::STriw:
01120   case Hexagon::STriw_f:
01121     return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
01122       (Offset <= Hexagon_MEMW_OFFSET_MAX);
01123 
01124   case Hexagon::LDrid:
01125   case Hexagon::LDrid_indexed:
01126   case Hexagon::LDrid_f:
01127   case Hexagon::STrid:
01128   case Hexagon::STrid_indexed:
01129   case Hexagon::STrid_f:
01130     return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
01131       (Offset <= Hexagon_MEMD_OFFSET_MAX);
01132 
01133   case Hexagon::LDrih:
01134   case Hexagon::LDriuh:
01135   case Hexagon::STrih:
01136     return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
01137       (Offset <= Hexagon_MEMH_OFFSET_MAX);
01138 
01139   case Hexagon::LDrib:
01140   case Hexagon::STrib:
01141   case Hexagon::LDriub:
01142     return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
01143       (Offset <= Hexagon_MEMB_OFFSET_MAX);
01144 
01145   case Hexagon::ADD_ri:
01146   case Hexagon::TFR_FI:
01147     return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
01148       (Offset <= Hexagon_ADDI_OFFSET_MAX);
01149 
01150   case Hexagon::MemOPw_ADDi_V4 :
01151   case Hexagon::MemOPw_SUBi_V4 :
01152   case Hexagon::MemOPw_ADDr_V4 :
01153   case Hexagon::MemOPw_SUBr_V4 :
01154   case Hexagon::MemOPw_ANDr_V4 :
01155   case Hexagon::MemOPw_ORr_V4 :
01156     return (0 <= Offset && Offset <= 255);
01157 
01158   case Hexagon::MemOPh_ADDi_V4 :
01159   case Hexagon::MemOPh_SUBi_V4 :
01160   case Hexagon::MemOPh_ADDr_V4 :
01161   case Hexagon::MemOPh_SUBr_V4 :
01162   case Hexagon::MemOPh_ANDr_V4 :
01163   case Hexagon::MemOPh_ORr_V4 :
01164     return (0 <= Offset && Offset <= 127);
01165 
01166   case Hexagon::MemOPb_ADDi_V4 :
01167   case Hexagon::MemOPb_SUBi_V4 :
01168   case Hexagon::MemOPb_ADDr_V4 :
01169   case Hexagon::MemOPb_SUBr_V4 :
01170   case Hexagon::MemOPb_ANDr_V4 :
01171   case Hexagon::MemOPb_ORr_V4 :
01172     return (0 <= Offset && Offset <= 63);
01173 
01174   // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
01175   // any size. Later pass knows how to handle it.
01176   case Hexagon::STriw_pred:
01177   case Hexagon::LDriw_pred:
01178     return true;
01179 
01180   case Hexagon::LOOP0_i:
01181     return isUInt<10>(Offset);
01182 
01183   // INLINEASM is very special.
01184   case Hexagon::INLINEASM:
01185     return true;
01186   }
01187 
01188   llvm_unreachable("No offset range is defined for this opcode. "
01189                    "Please define it in the above switch statement!");
01190 }
01191 
01192 
01193 //
01194 // Check if the Offset is a valid auto-inc imm by Load/Store Type.
01195 //
01196 bool HexagonInstrInfo::
01197 isValidAutoIncImm(const EVT VT, const int Offset) const {
01198 
01199   if (VT == MVT::i64) {
01200       return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
01201               Offset <= Hexagon_MEMD_AUTOINC_MAX &&
01202               (Offset & 0x7) == 0);
01203   }
01204   if (VT == MVT::i32) {
01205       return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
01206               Offset <= Hexagon_MEMW_AUTOINC_MAX &&
01207               (Offset & 0x3) == 0);
01208   }
01209   if (VT == MVT::i16) {
01210       return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
01211               Offset <= Hexagon_MEMH_AUTOINC_MAX &&
01212               (Offset & 0x1) == 0);
01213   }
01214   if (VT == MVT::i8) {
01215       return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
01216               Offset <= Hexagon_MEMB_AUTOINC_MAX);
01217   }
01218   llvm_unreachable("Not an auto-inc opc!");
01219 }
01220 
01221 
01222 bool HexagonInstrInfo::
01223 isMemOp(const MachineInstr *MI) const {
01224 //  return MI->getDesc().mayLoad() && MI->getDesc().mayStore();
01225 
01226   switch (MI->getOpcode())
01227   {
01228     default: return false;
01229     case Hexagon::MemOPw_ADDi_V4 :
01230     case Hexagon::MemOPw_SUBi_V4 :
01231     case Hexagon::MemOPw_ADDr_V4 :
01232     case Hexagon::MemOPw_SUBr_V4 :
01233     case Hexagon::MemOPw_ANDr_V4 :
01234     case Hexagon::MemOPw_ORr_V4 :
01235     case Hexagon::MemOPh_ADDi_V4 :
01236     case Hexagon::MemOPh_SUBi_V4 :
01237     case Hexagon::MemOPh_ADDr_V4 :
01238     case Hexagon::MemOPh_SUBr_V4 :
01239     case Hexagon::MemOPh_ANDr_V4 :
01240     case Hexagon::MemOPh_ORr_V4 :
01241     case Hexagon::MemOPb_ADDi_V4 :
01242     case Hexagon::MemOPb_SUBi_V4 :
01243     case Hexagon::MemOPb_ADDr_V4 :
01244     case Hexagon::MemOPb_SUBr_V4 :
01245     case Hexagon::MemOPb_ANDr_V4 :
01246     case Hexagon::MemOPb_ORr_V4 :
01247     case Hexagon::MemOPb_SETBITi_V4:
01248     case Hexagon::MemOPh_SETBITi_V4:
01249     case Hexagon::MemOPw_SETBITi_V4:
01250     case Hexagon::MemOPb_CLRBITi_V4:
01251     case Hexagon::MemOPh_CLRBITi_V4:
01252     case Hexagon::MemOPw_CLRBITi_V4:
01253     return true;
01254   }
01255   return false;
01256 }
01257 
01258 
01259 bool HexagonInstrInfo::
01260 isSpillPredRegOp(const MachineInstr *MI) const {
01261   switch (MI->getOpcode()) {
01262     default: return false;
01263     case Hexagon::STriw_pred :
01264     case Hexagon::LDriw_pred :
01265       return true;
01266   }
01267 }
01268 
01269 bool HexagonInstrInfo::isNewValueJumpCandidate(const MachineInstr *MI) const {
01270   switch (MI->getOpcode()) {
01271     default: return false;
01272     case Hexagon::CMPEQrr:
01273     case Hexagon::CMPEQri:
01274     case Hexagon::CMPGTrr:
01275     case Hexagon::CMPGTri:
01276     case Hexagon::CMPGTUrr:
01277     case Hexagon::CMPGTUri:
01278       return true;
01279   }
01280 }
01281 
01282 bool HexagonInstrInfo::
01283 isConditionalTransfer (const MachineInstr *MI) const {
01284   switch (MI->getOpcode()) {
01285     default: return false;
01286     case Hexagon::TFR_cPt:
01287     case Hexagon::TFR_cNotPt:
01288     case Hexagon::TFRI_cPt:
01289     case Hexagon::TFRI_cNotPt:
01290     case Hexagon::TFR_cdnPt:
01291     case Hexagon::TFR_cdnNotPt:
01292     case Hexagon::TFRI_cdnPt:
01293     case Hexagon::TFRI_cdnNotPt:
01294       return true;
01295   }
01296 }
01297 
01298 bool HexagonInstrInfo::isConditionalALU32 (const MachineInstr* MI) const {
01299   const HexagonRegisterInfo& QRI = getRegisterInfo();
01300   switch (MI->getOpcode())
01301   {
01302     default: return false;
01303     case Hexagon::ADD_ri_cPt:
01304     case Hexagon::ADD_ri_cNotPt:
01305     case Hexagon::ADD_rr_cPt:
01306     case Hexagon::ADD_rr_cNotPt:
01307     case Hexagon::XOR_rr_cPt:
01308     case Hexagon::XOR_rr_cNotPt:
01309     case Hexagon::AND_rr_cPt:
01310     case Hexagon::AND_rr_cNotPt:
01311     case Hexagon::OR_rr_cPt:
01312     case Hexagon::OR_rr_cNotPt:
01313     case Hexagon::SUB_rr_cPt:
01314     case Hexagon::SUB_rr_cNotPt:
01315     case Hexagon::COMBINE_rr_cPt:
01316     case Hexagon::COMBINE_rr_cNotPt:
01317       return true;
01318     case Hexagon::ASLH_cPt_V4:
01319     case Hexagon::ASLH_cNotPt_V4:
01320     case Hexagon::ASRH_cPt_V4:
01321     case Hexagon::ASRH_cNotPt_V4:
01322     case Hexagon::SXTB_cPt_V4:
01323     case Hexagon::SXTB_cNotPt_V4:
01324     case Hexagon::SXTH_cPt_V4:
01325     case Hexagon::SXTH_cNotPt_V4:
01326     case Hexagon::ZXTB_cPt_V4:
01327     case Hexagon::ZXTB_cNotPt_V4:
01328     case Hexagon::ZXTH_cPt_V4:
01329     case Hexagon::ZXTH_cNotPt_V4:
01330       return QRI.Subtarget.hasV4TOps();
01331   }
01332 }
01333 
01334 bool HexagonInstrInfo::
01335 isConditionalLoad (const MachineInstr* MI) const {
01336   const HexagonRegisterInfo& QRI = getRegisterInfo();
01337   switch (MI->getOpcode())
01338   {
01339     default: return false;
01340     case Hexagon::LDrid_cPt :
01341     case Hexagon::LDrid_cNotPt :
01342     case Hexagon::LDrid_indexed_cPt :
01343     case Hexagon::LDrid_indexed_cNotPt :
01344     case Hexagon::LDriw_cPt :
01345     case Hexagon::LDriw_cNotPt :
01346     case Hexagon::LDriw_indexed_cPt :
01347     case Hexagon::LDriw_indexed_cNotPt :
01348     case Hexagon::LDrih_cPt :
01349     case Hexagon::LDrih_cNotPt :
01350     case Hexagon::LDrih_indexed_cPt :
01351     case Hexagon::LDrih_indexed_cNotPt :
01352     case Hexagon::LDrib_cPt :
01353     case Hexagon::LDrib_cNotPt :
01354     case Hexagon::LDrib_indexed_cPt :
01355     case Hexagon::LDrib_indexed_cNotPt :
01356     case Hexagon::LDriuh_cPt :
01357     case Hexagon::LDriuh_cNotPt :
01358     case Hexagon::LDriuh_indexed_cPt :
01359     case Hexagon::LDriuh_indexed_cNotPt :
01360     case Hexagon::LDriub_cPt :
01361     case Hexagon::LDriub_cNotPt :
01362     case Hexagon::LDriub_indexed_cPt :
01363     case Hexagon::LDriub_indexed_cNotPt :
01364       return true;
01365     case Hexagon::POST_LDrid_cPt :
01366     case Hexagon::POST_LDrid_cNotPt :
01367     case Hexagon::POST_LDriw_cPt :
01368     case Hexagon::POST_LDriw_cNotPt :
01369     case Hexagon::POST_LDrih_cPt :
01370     case Hexagon::POST_LDrih_cNotPt :
01371     case Hexagon::POST_LDrib_cPt :
01372     case Hexagon::POST_LDrib_cNotPt :
01373     case Hexagon::POST_LDriuh_cPt :
01374     case Hexagon::POST_LDriuh_cNotPt :
01375     case Hexagon::POST_LDriub_cPt :
01376     case Hexagon::POST_LDriub_cNotPt :
01377       return QRI.Subtarget.hasV4TOps();
01378     case Hexagon::LDrid_indexed_shl_cPt_V4 :
01379     case Hexagon::LDrid_indexed_shl_cNotPt_V4 :
01380     case Hexagon::LDrib_indexed_shl_cPt_V4 :
01381     case Hexagon::LDrib_indexed_shl_cNotPt_V4 :
01382     case Hexagon::LDriub_indexed_shl_cPt_V4 :
01383     case Hexagon::LDriub_indexed_shl_cNotPt_V4 :
01384     case Hexagon::LDrih_indexed_shl_cPt_V4 :
01385     case Hexagon::LDrih_indexed_shl_cNotPt_V4 :
01386     case Hexagon::LDriuh_indexed_shl_cPt_V4 :
01387     case Hexagon::LDriuh_indexed_shl_cNotPt_V4 :
01388     case Hexagon::LDriw_indexed_shl_cPt_V4 :
01389     case Hexagon::LDriw_indexed_shl_cNotPt_V4 :
01390       return QRI.Subtarget.hasV4TOps();
01391   }
01392 }
01393 
01394 // Returns true if an instruction is a conditional store.
01395 //
01396 // Note: It doesn't include conditional new-value stores as they can't be
01397 // converted to .new predicate.
01398 //
01399 //               p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
01400 //                ^           ^
01401 //               /             \ (not OK. it will cause new-value store to be
01402 //              /               X conditional on p0.new while R2 producer is
01403 //             /                 \ on p0)
01404 //            /                   \.
01405 //     p.new store                 p.old NV store
01406 // [if(p0.new)memw(R0+#0)=R2]    [if(p0)memw(R0+#0)=R2.new]
01407 //            ^                  ^
01408 //             \                /
01409 //              \              /
01410 //               \            /
01411 //                 p.old store
01412 //             [if (p0)memw(R0+#0)=R2]
01413 //
01414 // The above diagram shows the steps involoved in the conversion of a predicated
01415 // store instruction to its .new predicated new-value form.
01416 //
01417 // The following set of instructions further explains the scenario where
01418 // conditional new-value store becomes invalid when promoted to .new predicate
01419 // form.
01420 //
01421 // { 1) if (p0) r0 = add(r1, r2)
01422 //   2) p0 = cmp.eq(r3, #0) }
01423 //
01424 //   3) if (p0) memb(r1+#0) = r0  --> this instruction can't be grouped with
01425 // the first two instructions because in instr 1, r0 is conditional on old value
01426 // of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
01427 // is not valid for new-value stores.
01428 bool HexagonInstrInfo::
01429 isConditionalStore (const MachineInstr* MI) const {
01430   const HexagonRegisterInfo& QRI = getRegisterInfo();
01431   switch (MI->getOpcode())
01432   {
01433     default: return false;
01434     case Hexagon::STrib_imm_cPt_V4 :
01435     case Hexagon::STrib_imm_cNotPt_V4 :
01436     case Hexagon::STrib_indexed_shl_cPt_V4 :
01437     case Hexagon::STrib_indexed_shl_cNotPt_V4 :
01438     case Hexagon::STrib_cPt :
01439     case Hexagon::STrib_cNotPt :
01440     case Hexagon::POST_STbri_cPt :
01441     case Hexagon::POST_STbri_cNotPt :
01442     case Hexagon::STrid_indexed_cPt :
01443     case Hexagon::STrid_indexed_cNotPt :
01444     case Hexagon::STrid_indexed_shl_cPt_V4 :
01445     case Hexagon::POST_STdri_cPt :
01446     case Hexagon::POST_STdri_cNotPt :
01447     case Hexagon::STrih_cPt :
01448     case Hexagon::STrih_cNotPt :
01449     case Hexagon::STrih_indexed_cPt :
01450     case Hexagon::STrih_indexed_cNotPt :
01451     case Hexagon::STrih_imm_cPt_V4 :
01452     case Hexagon::STrih_imm_cNotPt_V4 :
01453     case Hexagon::STrih_indexed_shl_cPt_V4 :
01454     case Hexagon::STrih_indexed_shl_cNotPt_V4 :
01455     case Hexagon::POST_SThri_cPt :
01456     case Hexagon::POST_SThri_cNotPt :
01457     case Hexagon::STriw_cPt :
01458     case Hexagon::STriw_cNotPt :
01459     case Hexagon::STriw_indexed_cPt :
01460     case Hexagon::STriw_indexed_cNotPt :
01461     case Hexagon::STriw_imm_cPt_V4 :
01462     case Hexagon::STriw_imm_cNotPt_V4 :
01463     case Hexagon::STriw_indexed_shl_cPt_V4 :
01464     case Hexagon::STriw_indexed_shl_cNotPt_V4 :
01465     case Hexagon::POST_STwri_cPt :
01466     case Hexagon::POST_STwri_cNotPt :
01467       return QRI.Subtarget.hasV4TOps();
01468 
01469     // V4 global address store before promoting to dot new.
01470     case Hexagon::STd_GP_cPt_V4 :
01471     case Hexagon::STd_GP_cNotPt_V4 :
01472     case Hexagon::STb_GP_cPt_V4 :
01473     case Hexagon::STb_GP_cNotPt_V4 :
01474     case Hexagon::STh_GP_cPt_V4 :
01475     case Hexagon::STh_GP_cNotPt_V4 :
01476     case Hexagon::STw_GP_cPt_V4 :
01477     case Hexagon::STw_GP_cNotPt_V4 :
01478       return QRI.Subtarget.hasV4TOps();
01479 
01480     // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
01481     // from the "Conditional Store" list. Because a predicated new value store
01482     // would NOT be promoted to a double dot new store. See diagram below:
01483     // This function returns yes for those stores that are predicated but not
01484     // yet promoted to predicate dot new instructions.
01485     //
01486     //                          +---------------------+
01487     //                    /-----| if (p0) memw(..)=r0 |---------\~
01488     //                   ||     +---------------------+         ||
01489     //          promote  ||       /\       /\                   ||  promote
01490     //                   ||      /||\     /||\                  ||
01491     //                  \||/    demote     ||                  \||/
01492     //                   \/       ||       ||                   \/
01493     //       +-------------------------+   ||   +-------------------------+
01494     //       | if (p0.new) memw(..)=r0 |   ||   | if (p0) memw(..)=r0.new |
01495     //       +-------------------------+   ||   +-------------------------+
01496     //                        ||           ||         ||
01497     //                        ||         demote      \||/
01498     //                      promote        ||         \/ NOT possible
01499     //                        ||           ||         /\~
01500     //                       \||/          ||        /||\~
01501     //                        \/           ||         ||
01502     //                      +-----------------------------+
01503     //                      | if (p0.new) memw(..)=r0.new |
01504     //                      +-----------------------------+
01505     //                           Double Dot New Store
01506     //
01507   }
01508 }
01509 
01510 
01511 bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
01512   if (isNewValue(MI) && isBranch(MI))
01513     return true;
01514   return false;
01515 }
01516 
01517 bool HexagonInstrInfo::isPostIncrement (const MachineInstr* MI) const {
01518   return (getAddrMode(MI) == HexagonII::PostInc);
01519 }
01520 
01521 bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
01522   const uint64_t F = MI->getDesc().TSFlags;
01523   return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
01524 }
01525 
01526 // Returns true, if any one of the operands is a dot new
01527 // insn, whether it is predicated dot new or register dot new.
01528 bool HexagonInstrInfo::isDotNewInst (const MachineInstr* MI) const {
01529   return (isNewValueInst(MI) ||
01530      (isPredicated(MI) && isPredicatedNew(MI)));
01531 }
01532 
01533 // Returns the most basic instruction for the .new predicated instructions and
01534 // new-value stores.
01535 // For example, all of the following instructions will be converted back to the
01536 // same instruction:
01537 // 1) if (p0.new) memw(R0+#0) = R1.new  --->
01538 // 2) if (p0) memw(R0+#0)= R1.new      -------> if (p0) memw(R0+#0) = R1
01539 // 3) if (p0.new) memw(R0+#0) = R1      --->
01540 //
01541 
01542 int HexagonInstrInfo::GetDotOldOp(const int opc) const {
01543   int NewOp = opc;
01544   if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
01545     NewOp = Hexagon::getPredOldOpcode(NewOp);
01546     if (NewOp < 0)
01547       assert(0 && "Couldn't change predicate new instruction to its old form.");
01548   }
01549 
01550   if (isNewValueStore(NewOp)) { // Convert into non new-value format
01551     NewOp = Hexagon::getNonNVStore(NewOp);
01552     if (NewOp < 0)
01553       assert(0 && "Couldn't change new-value store to its old form.");
01554   }
01555   return NewOp;
01556 }
01557 
01558 // Return the new value instruction for a given store.
01559 int HexagonInstrInfo::GetDotNewOp(const MachineInstr* MI) const {
01560   int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
01561   if (NVOpcode >= 0) // Valid new-value store instruction.
01562     return NVOpcode;
01563 
01564   switch (MI->getOpcode()) {
01565   default: llvm_unreachable("Unknown .new type");
01566   // store new value byte
01567   case Hexagon::STrib_shl_V4:
01568     return Hexagon::STrib_shl_nv_V4;
01569 
01570   case Hexagon::STrih_shl_V4:
01571     return Hexagon::STrih_shl_nv_V4;
01572 
01573   case Hexagon::STriw_f:
01574     return Hexagon::STriw_nv_V4;
01575 
01576   case Hexagon::STriw_indexed_f:
01577     return Hexagon::STriw_indexed_nv_V4;
01578 
01579   case Hexagon::STriw_shl_V4:
01580     return Hexagon::STriw_shl_nv_V4;
01581 
01582   }
01583   return 0;
01584 }
01585 
01586 // Return .new predicate version for an instruction.
01587 int HexagonInstrInfo::GetDotNewPredOp(MachineInstr *MI,
01588                                       const MachineBranchProbabilityInfo
01589                                       *MBPI) const {
01590 
01591   int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
01592   if (NewOpcode >= 0) // Valid predicate new instruction
01593     return NewOpcode;
01594 
01595   switch (MI->getOpcode()) {
01596   default: llvm_unreachable("Unknown .new type");
01597   // Condtional Jumps
01598   case Hexagon::JMP_t:
01599   case Hexagon::JMP_f:
01600     return getDotNewPredJumpOp(MI, MBPI);
01601 
01602   case Hexagon::JMPR_t:
01603     return Hexagon::JMPR_tnew_tV3;
01604 
01605   case Hexagon::JMPR_f:
01606     return Hexagon::JMPR_fnew_tV3;
01607 
01608   case Hexagon::JMPret_t:
01609     return Hexagon::JMPret_tnew_tV3;
01610 
01611   case Hexagon::JMPret_f:
01612     return Hexagon::JMPret_fnew_tV3;
01613 
01614 
01615   // Conditional combine
01616   case Hexagon::COMBINE_rr_cPt :
01617     return Hexagon::COMBINE_rr_cdnPt;
01618   case Hexagon::COMBINE_rr_cNotPt :
01619     return Hexagon::COMBINE_rr_cdnNotPt;
01620   }
01621 }
01622 
01623 
01624 unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
01625   const uint64_t F = MI->getDesc().TSFlags;
01626 
01627   return((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
01628 }
01629 
01630 /// immediateExtend - Changes the instruction in place to one using an immediate
01631 /// extender.
01632 void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
01633   assert((isExtendable(MI)||isConstExtended(MI)) &&
01634                                "Instruction must be extendable");
01635   // Find which operand is extendable.
01636   short ExtOpNum = getCExtOpNum(MI);
01637   MachineOperand &MO = MI->getOperand(ExtOpNum);
01638   // This needs to be something we understand.
01639   assert((MO.isMBB() || MO.isImm()) &&
01640          "Branch with unknown extendable field type");
01641   // Mark given operand as extended.
01642   MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
01643 }
01644 
01645 DFAPacketizer *HexagonInstrInfo::
01646 CreateTargetScheduleState(const TargetMachine *TM,
01647                            const ScheduleDAG *DAG) const {
01648   const InstrItineraryData *II = TM->getInstrItineraryData();
01649   return TM->getSubtarget<HexagonGenSubtargetInfo>().createDFAPacketizer(II);
01650 }
01651 
01652 bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
01653                                             const MachineBasicBlock *MBB,
01654                                             const MachineFunction &MF) const {
01655   // Debug info is never a scheduling boundary. It's necessary to be explicit
01656   // due to the special treatment of IT instructions below, otherwise a
01657   // dbg_value followed by an IT will result in the IT instruction being
01658   // considered a scheduling hazard, which is wrong. It should be the actual
01659   // instruction preceding the dbg_value instruction(s), just like it is
01660   // when debug info is not present.
01661   if (MI->isDebugValue())
01662     return false;
01663 
01664   // Terminators and labels can't be scheduled around.
01665   if (MI->getDesc().isTerminator() || MI->isLabel() || MI->isInlineAsm())
01666     return true;
01667 
01668   return false;
01669 }
01670 
01671 bool HexagonInstrInfo::isConstExtended(MachineInstr *MI) const {
01672 
01673   // Constant extenders are allowed only for V4 and above.
01674   if (!Subtarget.hasV4TOps())
01675     return false;
01676 
01677   const uint64_t F = MI->getDesc().TSFlags;
01678   unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
01679   if (isExtended) // Instruction must be extended.
01680     return true;
01681 
01682   unsigned isExtendable = (F >> HexagonII::ExtendablePos)
01683                           & HexagonII::ExtendableMask;
01684   if (!isExtendable)
01685     return false;
01686 
01687   short ExtOpNum = getCExtOpNum(MI);
01688   const MachineOperand &MO = MI->getOperand(ExtOpNum);
01689   // Use MO operand flags to determine if MO
01690   // has the HMOTF_ConstExtended flag set.
01691   if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
01692     return true;
01693   // If this is a Machine BB address we are talking about, and it is
01694   // not marked as extended, say so.
01695   if (MO.isMBB())
01696     return false;
01697 
01698   // We could be using an instruction with an extendable immediate and shoehorn
01699   // a global address into it. If it is a global address it will be constant
01700   // extended. We do this for COMBINE.
01701   // We currently only handle isGlobal() because it is the only kind of
01702   // object we are going to end up with here for now.
01703   // In the future we probably should add isSymbol(), etc.
01704   if (MO.isGlobal() || MO.isSymbol())
01705     return true;
01706 
01707   // If the extendable operand is not 'Immediate' type, the instruction should
01708   // have 'isExtended' flag set.
01709   assert(MO.isImm() && "Extendable operand must be Immediate type");
01710 
01711   int MinValue = getMinValue(MI);
01712   int MaxValue = getMaxValue(MI);
01713   int ImmValue = MO.getImm();
01714 
01715   return (ImmValue < MinValue || ImmValue > MaxValue);
01716 }
01717 
01718 // Returns the opcode to use when converting MI, which is a conditional jump,
01719 // into a conditional instruction which uses the .new value of the predicate.
01720 // We also use branch probabilities to add a hint to the jump.
01721 int
01722 HexagonInstrInfo::getDotNewPredJumpOp(MachineInstr *MI,
01723                                   const
01724                                   MachineBranchProbabilityInfo *MBPI) const {
01725 
01726   // We assume that block can have at most two successors.
01727   bool taken = false;
01728   MachineBasicBlock *Src = MI->getParent();
01729   MachineOperand *BrTarget = &MI->getOperand(1);
01730   MachineBasicBlock *Dst = BrTarget->getMBB();
01731 
01732   const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
01733   if (Prediction >= BranchProbability(1,2))
01734     taken = true;
01735 
01736   switch (MI->getOpcode()) {
01737   case Hexagon::JMP_t:
01738     return taken ? Hexagon::JMP_tnew_t : Hexagon::JMP_tnew_nt;
01739   case Hexagon::JMP_f:
01740     return taken ? Hexagon::JMP_fnew_t : Hexagon::JMP_fnew_nt;
01741 
01742   default:
01743     llvm_unreachable("Unexpected jump instruction.");
01744   }
01745 }
01746 // Returns true if a particular operand is extendable for an instruction.
01747 bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
01748                                          unsigned short OperandNum) const {
01749   // Constant extenders are allowed only for V4 and above.
01750   if (!Subtarget.hasV4TOps())
01751     return false;
01752 
01753   const uint64_t F = MI->getDesc().TSFlags;
01754 
01755   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
01756           == OperandNum;
01757 }
01758 
01759 // Returns Operand Index for the constant extended instruction.
01760 unsigned short HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
01761   const uint64_t F = MI->getDesc().TSFlags;
01762   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
01763 }
01764 
01765 // Returns the min value that doesn't need to be extended.
01766 int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
01767   const uint64_t F = MI->getDesc().TSFlags;
01768   unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
01769                     & HexagonII::ExtentSignedMask;
01770   unsigned bits =  (F >> HexagonII::ExtentBitsPos)
01771                     & HexagonII::ExtentBitsMask;
01772 
01773   if (isSigned) // if value is signed
01774     return -1 << (bits - 1);
01775   else
01776     return 0;
01777 }
01778 
01779 // Returns the max value that doesn't need to be extended.
01780 int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
01781   const uint64_t F = MI->getDesc().TSFlags;
01782   unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
01783                     & HexagonII::ExtentSignedMask;
01784   unsigned bits =  (F >> HexagonII::ExtentBitsPos)
01785                     & HexagonII::ExtentBitsMask;
01786 
01787   if (isSigned) // if value is signed
01788     return ~(-1 << (bits - 1));
01789   else
01790     return ~(-1 << bits);
01791 }
01792 
01793 // Returns true if an instruction can be converted into a non-extended
01794 // equivalent instruction.
01795 bool HexagonInstrInfo::NonExtEquivalentExists (const MachineInstr *MI) const {
01796 
01797   short NonExtOpcode;
01798   // Check if the instruction has a register form that uses register in place
01799   // of the extended operand, if so return that as the non-extended form.
01800   if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
01801     return true;
01802 
01803   if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
01804     // Check addressing mode and retreive non-ext equivalent instruction.
01805 
01806     switch (getAddrMode(MI)) {
01807     case HexagonII::Absolute :
01808       // Load/store with absolute addressing mode can be converted into
01809       // base+offset mode.
01810       NonExtOpcode = Hexagon::getBasedWithImmOffset(MI->getOpcode());
01811       break;
01812     case HexagonII::BaseImmOffset :
01813       // Load/store with base+offset addressing mode can be converted into
01814       // base+register offset addressing mode. However left shift operand should
01815       // be set to 0.
01816       NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
01817       break;
01818     default:
01819       return false;
01820     }
01821     if (NonExtOpcode < 0)
01822       return false;
01823     return true;
01824   }
01825   return false;
01826 }
01827 
01828 // Returns opcode of the non-extended equivalent instruction.
01829 short HexagonInstrInfo::getNonExtOpcode (const MachineInstr *MI) const {
01830 
01831   // Check if the instruction has a register form that uses register in place
01832   // of the extended operand, if so return that as the non-extended form.
01833   short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
01834     if (NonExtOpcode >= 0)
01835       return NonExtOpcode;
01836 
01837   if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
01838     // Check addressing mode and retreive non-ext equivalent instruction.
01839     switch (getAddrMode(MI)) {
01840     case HexagonII::Absolute :
01841       return Hexagon::getBasedWithImmOffset(MI->getOpcode());
01842     case HexagonII::BaseImmOffset :
01843       return Hexagon::getBaseWithRegOffset(MI->getOpcode());
01844     default:
01845       return -1;
01846     }
01847   }
01848   return -1;
01849 }
01850 
01851 bool HexagonInstrInfo::PredOpcodeHasJMP_c(Opcode_t Opcode) const {
01852   return (Opcode == Hexagon::JMP_t) ||
01853          (Opcode == Hexagon::JMP_f) ||
01854          (Opcode == Hexagon::JMP_tnew_t) ||
01855          (Opcode == Hexagon::JMP_fnew_t) ||
01856          (Opcode == Hexagon::JMP_tnew_nt) ||
01857          (Opcode == Hexagon::JMP_fnew_nt);
01858 }
01859 
01860 bool HexagonInstrInfo::PredOpcodeHasNot(Opcode_t Opcode) const {
01861   return (Opcode == Hexagon::JMP_f) ||
01862          (Opcode == Hexagon::JMP_fnew_t) ||
01863          (Opcode == Hexagon::JMP_fnew_nt);
01864 }