LLVM API Documentation

SystemZInstrInfo.cpp
Go to the documentation of this file.
00001 //===-- SystemZInstrInfo.cpp - SystemZ 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 SystemZ implementation of the TargetInstrInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "SystemZInstrInfo.h"
00015 #include "SystemZInstrBuilder.h"
00016 #include "SystemZTargetMachine.h"
00017 #include "llvm/CodeGen/LiveVariables.h"
00018 #include "llvm/CodeGen/MachineRegisterInfo.h"
00019 
00020 using namespace llvm;
00021 
00022 #define GET_INSTRINFO_CTOR_DTOR
00023 #define GET_INSTRMAP_INFO
00024 #include "SystemZGenInstrInfo.inc"
00025 
00026 // Return a mask with Count low bits set.
00027 static uint64_t allOnes(unsigned int Count) {
00028   return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1;
00029 }
00030 
00031 // Reg should be a 32-bit GPR.  Return true if it is a high register rather
00032 // than a low register.
00033 static bool isHighReg(unsigned int Reg) {
00034   if (SystemZ::GRH32BitRegClass.contains(Reg))
00035     return true;
00036   assert(SystemZ::GR32BitRegClass.contains(Reg) && "Invalid GRX32");
00037   return false;
00038 }
00039 
00040 // Pin the vtable to this file.
00041 void SystemZInstrInfo::anchor() {}
00042 
00043 SystemZInstrInfo::SystemZInstrInfo(SystemZSubtarget &sti)
00044   : SystemZGenInstrInfo(SystemZ::ADJCALLSTACKDOWN, SystemZ::ADJCALLSTACKUP),
00045     RI(), STI(sti) {
00046 }
00047 
00048 // MI is a 128-bit load or store.  Split it into two 64-bit loads or stores,
00049 // each having the opcode given by NewOpcode.
00050 void SystemZInstrInfo::splitMove(MachineBasicBlock::iterator MI,
00051                                  unsigned NewOpcode) const {
00052   MachineBasicBlock *MBB = MI->getParent();
00053   MachineFunction &MF = *MBB->getParent();
00054 
00055   // Get two load or store instructions.  Use the original instruction for one
00056   // of them (arbitrarily the second here) and create a clone for the other.
00057   MachineInstr *EarlierMI = MF.CloneMachineInstr(MI);
00058   MBB->insert(MI, EarlierMI);
00059 
00060   // Set up the two 64-bit registers.
00061   MachineOperand &HighRegOp = EarlierMI->getOperand(0);
00062   MachineOperand &LowRegOp = MI->getOperand(0);
00063   HighRegOp.setReg(RI.getSubReg(HighRegOp.getReg(), SystemZ::subreg_h64));
00064   LowRegOp.setReg(RI.getSubReg(LowRegOp.getReg(), SystemZ::subreg_l64));
00065 
00066   // The address in the first (high) instruction is already correct.
00067   // Adjust the offset in the second (low) instruction.
00068   MachineOperand &HighOffsetOp = EarlierMI->getOperand(2);
00069   MachineOperand &LowOffsetOp = MI->getOperand(2);
00070   LowOffsetOp.setImm(LowOffsetOp.getImm() + 8);
00071 
00072   // Set the opcodes.
00073   unsigned HighOpcode = getOpcodeForOffset(NewOpcode, HighOffsetOp.getImm());
00074   unsigned LowOpcode = getOpcodeForOffset(NewOpcode, LowOffsetOp.getImm());
00075   assert(HighOpcode && LowOpcode && "Both offsets should be in range");
00076 
00077   EarlierMI->setDesc(get(HighOpcode));
00078   MI->setDesc(get(LowOpcode));
00079 }
00080 
00081 // Split ADJDYNALLOC instruction MI.
00082 void SystemZInstrInfo::splitAdjDynAlloc(MachineBasicBlock::iterator MI) const {
00083   MachineBasicBlock *MBB = MI->getParent();
00084   MachineFunction &MF = *MBB->getParent();
00085   MachineFrameInfo *MFFrame = MF.getFrameInfo();
00086   MachineOperand &OffsetMO = MI->getOperand(2);
00087 
00088   uint64_t Offset = (MFFrame->getMaxCallFrameSize() +
00089                      SystemZMC::CallFrameSize +
00090                      OffsetMO.getImm());
00091   unsigned NewOpcode = getOpcodeForOffset(SystemZ::LA, Offset);
00092   assert(NewOpcode && "No support for huge argument lists yet");
00093   MI->setDesc(get(NewOpcode));
00094   OffsetMO.setImm(Offset);
00095 }
00096 
00097 // MI is an RI-style pseudo instruction.  Replace it with LowOpcode
00098 // if the first operand is a low GR32 and HighOpcode if the first operand
00099 // is a high GR32.  ConvertHigh is true if LowOpcode takes a signed operand
00100 // and HighOpcode takes an unsigned 32-bit operand.  In those cases,
00101 // MI has the same kind of operand as LowOpcode, so needs to be converted
00102 // if HighOpcode is used.
00103 void SystemZInstrInfo::expandRIPseudo(MachineInstr *MI, unsigned LowOpcode,
00104                                       unsigned HighOpcode,
00105                                       bool ConvertHigh) const {
00106   unsigned Reg = MI->getOperand(0).getReg();
00107   bool IsHigh = isHighReg(Reg);
00108   MI->setDesc(get(IsHigh ? HighOpcode : LowOpcode));
00109   if (IsHigh && ConvertHigh)
00110     MI->getOperand(1).setImm(uint32_t(MI->getOperand(1).getImm()));
00111 }
00112 
00113 // MI is a three-operand RIE-style pseudo instruction.  Replace it with
00114 // LowOpcode3 if the registers are both low GR32s, otherwise use a move
00115 // followed by HighOpcode or LowOpcode, depending on whether the target
00116 // is a high or low GR32.
00117 void SystemZInstrInfo::expandRIEPseudo(MachineInstr *MI, unsigned LowOpcode,
00118                                        unsigned LowOpcodeK,
00119                                        unsigned HighOpcode) const {
00120   unsigned DestReg = MI->getOperand(0).getReg();
00121   unsigned SrcReg = MI->getOperand(1).getReg();
00122   bool DestIsHigh = isHighReg(DestReg);
00123   bool SrcIsHigh = isHighReg(SrcReg);
00124   if (!DestIsHigh && !SrcIsHigh)
00125     MI->setDesc(get(LowOpcodeK));
00126   else {
00127     emitGRX32Move(*MI->getParent(), MI, MI->getDebugLoc(),
00128                   DestReg, SrcReg, SystemZ::LR, 32,
00129                   MI->getOperand(1).isKill());
00130     MI->setDesc(get(DestIsHigh ? HighOpcode : LowOpcode));
00131     MI->getOperand(1).setReg(DestReg);
00132   }
00133 }
00134 
00135 // MI is an RXY-style pseudo instruction.  Replace it with LowOpcode
00136 // if the first operand is a low GR32 and HighOpcode if the first operand
00137 // is a high GR32.
00138 void SystemZInstrInfo::expandRXYPseudo(MachineInstr *MI, unsigned LowOpcode,
00139                                        unsigned HighOpcode) const {
00140   unsigned Reg = MI->getOperand(0).getReg();
00141   unsigned Opcode = getOpcodeForOffset(isHighReg(Reg) ? HighOpcode : LowOpcode,
00142                                        MI->getOperand(2).getImm());
00143   MI->setDesc(get(Opcode));
00144 }
00145 
00146 // MI is an RR-style pseudo instruction that zero-extends the low Size bits
00147 // of one GRX32 into another.  Replace it with LowOpcode if both operands
00148 // are low registers, otherwise use RISB[LH]G.
00149 void SystemZInstrInfo::expandZExtPseudo(MachineInstr *MI, unsigned LowOpcode,
00150                                         unsigned Size) const {
00151   emitGRX32Move(*MI->getParent(), MI, MI->getDebugLoc(),
00152                 MI->getOperand(0).getReg(), MI->getOperand(1).getReg(),
00153                 LowOpcode, Size, MI->getOperand(1).isKill());
00154   MI->eraseFromParent();
00155 }
00156 
00157 // Emit a zero-extending move from 32-bit GPR SrcReg to 32-bit GPR
00158 // DestReg before MBBI in MBB.  Use LowLowOpcode when both DestReg and SrcReg
00159 // are low registers, otherwise use RISB[LH]G.  Size is the number of bits
00160 // taken from the low end of SrcReg (8 for LLCR, 16 for LLHR and 32 for LR).
00161 // KillSrc is true if this move is the last use of SrcReg.
00162 void SystemZInstrInfo::emitGRX32Move(MachineBasicBlock &MBB,
00163                                      MachineBasicBlock::iterator MBBI,
00164                                      DebugLoc DL, unsigned DestReg,
00165                                      unsigned SrcReg, unsigned LowLowOpcode,
00166                                      unsigned Size, bool KillSrc) const {
00167   unsigned Opcode;
00168   bool DestIsHigh = isHighReg(DestReg);
00169   bool SrcIsHigh = isHighReg(SrcReg);
00170   if (DestIsHigh && SrcIsHigh)
00171     Opcode = SystemZ::RISBHH;
00172   else if (DestIsHigh && !SrcIsHigh)
00173     Opcode = SystemZ::RISBHL;
00174   else if (!DestIsHigh && SrcIsHigh)
00175     Opcode = SystemZ::RISBLH;
00176   else {
00177     BuildMI(MBB, MBBI, DL, get(LowLowOpcode), DestReg)
00178       .addReg(SrcReg, getKillRegState(KillSrc));
00179     return;
00180   }
00181   unsigned Rotate = (DestIsHigh != SrcIsHigh ? 32 : 0);
00182   BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
00183     .addReg(DestReg, RegState::Undef)
00184     .addReg(SrcReg, getKillRegState(KillSrc))
00185     .addImm(32 - Size).addImm(128 + 31).addImm(Rotate);
00186 }
00187 
00188 // If MI is a simple load or store for a frame object, return the register
00189 // it loads or stores and set FrameIndex to the index of the frame object.
00190 // Return 0 otherwise.
00191 //
00192 // Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
00193 static int isSimpleMove(const MachineInstr *MI, int &FrameIndex,
00194                         unsigned Flag) {
00195   const MCInstrDesc &MCID = MI->getDesc();
00196   if ((MCID.TSFlags & Flag) &&
00197       MI->getOperand(1).isFI() &&
00198       MI->getOperand(2).getImm() == 0 &&
00199       MI->getOperand(3).getReg() == 0) {
00200     FrameIndex = MI->getOperand(1).getIndex();
00201     return MI->getOperand(0).getReg();
00202   }
00203   return 0;
00204 }
00205 
00206 unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
00207                                                int &FrameIndex) const {
00208   return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad);
00209 }
00210 
00211 unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
00212                                               int &FrameIndex) const {
00213   return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore);
00214 }
00215 
00216 bool SystemZInstrInfo::isStackSlotCopy(const MachineInstr *MI,
00217                                        int &DestFrameIndex,
00218                                        int &SrcFrameIndex) const {
00219   // Check for MVC 0(Length,FI1),0(FI2)
00220   const MachineFrameInfo *MFI = MI->getParent()->getParent()->getFrameInfo();
00221   if (MI->getOpcode() != SystemZ::MVC ||
00222       !MI->getOperand(0).isFI() ||
00223       MI->getOperand(1).getImm() != 0 ||
00224       !MI->getOperand(3).isFI() ||
00225       MI->getOperand(4).getImm() != 0)
00226     return false;
00227 
00228   // Check that Length covers the full slots.
00229   int64_t Length = MI->getOperand(2).getImm();
00230   unsigned FI1 = MI->getOperand(0).getIndex();
00231   unsigned FI2 = MI->getOperand(3).getIndex();
00232   if (MFI->getObjectSize(FI1) != Length ||
00233       MFI->getObjectSize(FI2) != Length)
00234     return false;
00235 
00236   DestFrameIndex = FI1;
00237   SrcFrameIndex = FI2;
00238   return true;
00239 }
00240 
00241 bool SystemZInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
00242                                      MachineBasicBlock *&TBB,
00243                                      MachineBasicBlock *&FBB,
00244                                      SmallVectorImpl<MachineOperand> &Cond,
00245                                      bool AllowModify) const {
00246   // Most of the code and comments here are boilerplate.
00247 
00248   // Start from the bottom of the block and work up, examining the
00249   // terminator instructions.
00250   MachineBasicBlock::iterator I = MBB.end();
00251   while (I != MBB.begin()) {
00252     --I;
00253     if (I->isDebugValue())
00254       continue;
00255 
00256     // Working from the bottom, when we see a non-terminator instruction, we're
00257     // done.
00258     if (!isUnpredicatedTerminator(I))
00259       break;
00260 
00261     // A terminator that isn't a branch can't easily be handled by this
00262     // analysis.
00263     if (!I->isBranch())
00264       return true;
00265 
00266     // Can't handle indirect branches.
00267     SystemZII::Branch Branch(getBranchInfo(I));
00268     if (!Branch.Target->isMBB())
00269       return true;
00270 
00271     // Punt on compound branches.
00272     if (Branch.Type != SystemZII::BranchNormal)
00273       return true;
00274 
00275     if (Branch.CCMask == SystemZ::CCMASK_ANY) {
00276       // Handle unconditional branches.
00277       if (!AllowModify) {
00278         TBB = Branch.Target->getMBB();
00279         continue;
00280       }
00281 
00282       // If the block has any instructions after a JMP, delete them.
00283       while (std::next(I) != MBB.end())
00284         std::next(I)->eraseFromParent();
00285 
00286       Cond.clear();
00287       FBB = nullptr;
00288 
00289       // Delete the JMP if it's equivalent to a fall-through.
00290       if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) {
00291         TBB = nullptr;
00292         I->eraseFromParent();
00293         I = MBB.end();
00294         continue;
00295       }
00296 
00297       // TBB is used to indicate the unconditinal destination.
00298       TBB = Branch.Target->getMBB();
00299       continue;
00300     }
00301 
00302     // Working from the bottom, handle the first conditional branch.
00303     if (Cond.empty()) {
00304       // FIXME: add X86-style branch swap
00305       FBB = TBB;
00306       TBB = Branch.Target->getMBB();
00307       Cond.push_back(MachineOperand::CreateImm(Branch.CCValid));
00308       Cond.push_back(MachineOperand::CreateImm(Branch.CCMask));
00309       continue;
00310     }
00311 
00312     // Handle subsequent conditional branches.
00313     assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch");
00314 
00315     // Only handle the case where all conditional branches branch to the same
00316     // destination.
00317     if (TBB != Branch.Target->getMBB())
00318       return true;
00319 
00320     // If the conditions are the same, we can leave them alone.
00321     unsigned OldCCValid = Cond[0].getImm();
00322     unsigned OldCCMask = Cond[1].getImm();
00323     if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask)
00324       continue;
00325 
00326     // FIXME: Try combining conditions like X86 does.  Should be easy on Z!
00327     return false;
00328   }
00329 
00330   return false;
00331 }
00332 
00333 unsigned SystemZInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
00334   // Most of the code and comments here are boilerplate.
00335   MachineBasicBlock::iterator I = MBB.end();
00336   unsigned Count = 0;
00337 
00338   while (I != MBB.begin()) {
00339     --I;
00340     if (I->isDebugValue())
00341       continue;
00342     if (!I->isBranch())
00343       break;
00344     if (!getBranchInfo(I).Target->isMBB())
00345       break;
00346     // Remove the branch.
00347     I->eraseFromParent();
00348     I = MBB.end();
00349     ++Count;
00350   }
00351 
00352   return Count;
00353 }
00354 
00355 bool SystemZInstrInfo::
00356 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
00357   assert(Cond.size() == 2 && "Invalid condition");
00358   Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm());
00359   return false;
00360 }
00361 
00362 unsigned
00363 SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
00364                                MachineBasicBlock *FBB,
00365                                const SmallVectorImpl<MachineOperand> &Cond,
00366                                DebugLoc DL) const {
00367   // In this function we output 32-bit branches, which should always
00368   // have enough range.  They can be shortened and relaxed by later code
00369   // in the pipeline, if desired.
00370 
00371   // Shouldn't be a fall through.
00372   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
00373   assert((Cond.size() == 2 || Cond.size() == 0) &&
00374          "SystemZ branch conditions have one component!");
00375 
00376   if (Cond.empty()) {
00377     // Unconditional branch?
00378     assert(!FBB && "Unconditional branch with multiple successors!");
00379     BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
00380     return 1;
00381   }
00382 
00383   // Conditional branch.
00384   unsigned Count = 0;
00385   unsigned CCValid = Cond[0].getImm();
00386   unsigned CCMask = Cond[1].getImm();
00387   BuildMI(&MBB, DL, get(SystemZ::BRC))
00388     .addImm(CCValid).addImm(CCMask).addMBB(TBB);
00389   ++Count;
00390 
00391   if (FBB) {
00392     // Two-way Conditional branch. Insert the second branch.
00393     BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
00394     ++Count;
00395   }
00396   return Count;
00397 }
00398 
00399 bool SystemZInstrInfo::analyzeCompare(const MachineInstr *MI,
00400                                       unsigned &SrcReg, unsigned &SrcReg2,
00401                                       int &Mask, int &Value) const {
00402   assert(MI->isCompare() && "Caller should have checked for a comparison");
00403 
00404   if (MI->getNumExplicitOperands() == 2 &&
00405       MI->getOperand(0).isReg() &&
00406       MI->getOperand(1).isImm()) {
00407     SrcReg = MI->getOperand(0).getReg();
00408     SrcReg2 = 0;
00409     Value = MI->getOperand(1).getImm();
00410     Mask = ~0;
00411     return true;
00412   }
00413 
00414   return false;
00415 }
00416 
00417 // If Reg is a virtual register, return its definition, otherwise return null.
00418 static MachineInstr *getDef(unsigned Reg,
00419                             const MachineRegisterInfo *MRI) {
00420   if (TargetRegisterInfo::isPhysicalRegister(Reg))
00421     return nullptr;
00422   return MRI->getUniqueVRegDef(Reg);
00423 }
00424 
00425 // Return true if MI is a shift of type Opcode by Imm bits.
00426 static bool isShift(MachineInstr *MI, int Opcode, int64_t Imm) {
00427   return (MI->getOpcode() == Opcode &&
00428           !MI->getOperand(2).getReg() &&
00429           MI->getOperand(3).getImm() == Imm);
00430 }
00431 
00432 // If the destination of MI has no uses, delete it as dead.
00433 static void eraseIfDead(MachineInstr *MI, const MachineRegisterInfo *MRI) {
00434   if (MRI->use_nodbg_empty(MI->getOperand(0).getReg()))
00435     MI->eraseFromParent();
00436 }
00437 
00438 // Compare compares SrcReg against zero.  Check whether SrcReg contains
00439 // the result of an IPM sequence whose input CC survives until Compare,
00440 // and whether Compare is therefore redundant.  Delete it and return
00441 // true if so.
00442 static bool removeIPMBasedCompare(MachineInstr *Compare, unsigned SrcReg,
00443                                   const MachineRegisterInfo *MRI,
00444                                   const TargetRegisterInfo *TRI) {
00445   MachineInstr *LGFR = nullptr;
00446   MachineInstr *RLL = getDef(SrcReg, MRI);
00447   if (RLL && RLL->getOpcode() == SystemZ::LGFR) {
00448     LGFR = RLL;
00449     RLL = getDef(LGFR->getOperand(1).getReg(), MRI);
00450   }
00451   if (!RLL || !isShift(RLL, SystemZ::RLL, 31))
00452     return false;
00453 
00454   MachineInstr *SRL = getDef(RLL->getOperand(1).getReg(), MRI);
00455   if (!SRL || !isShift(SRL, SystemZ::SRL, SystemZ::IPM_CC))
00456     return false;
00457 
00458   MachineInstr *IPM = getDef(SRL->getOperand(1).getReg(), MRI);
00459   if (!IPM || IPM->getOpcode() != SystemZ::IPM)
00460     return false;
00461 
00462   // Check that there are no assignments to CC between the IPM and Compare,
00463   if (IPM->getParent() != Compare->getParent())
00464     return false;
00465   MachineBasicBlock::iterator MBBI = IPM, MBBE = Compare;
00466   for (++MBBI; MBBI != MBBE; ++MBBI) {
00467     MachineInstr *MI = MBBI;
00468     if (MI->modifiesRegister(SystemZ::CC, TRI))
00469       return false;
00470   }
00471 
00472   Compare->eraseFromParent();
00473   if (LGFR)
00474     eraseIfDead(LGFR, MRI);
00475   eraseIfDead(RLL, MRI);
00476   eraseIfDead(SRL, MRI);
00477   eraseIfDead(IPM, MRI);
00478 
00479   return true;
00480 }
00481 
00482 bool
00483 SystemZInstrInfo::optimizeCompareInstr(MachineInstr *Compare,
00484                                        unsigned SrcReg, unsigned SrcReg2,
00485                                        int Mask, int Value,
00486                                        const MachineRegisterInfo *MRI) const {
00487   assert(!SrcReg2 && "Only optimizing constant comparisons so far");
00488   bool IsLogical = (Compare->getDesc().TSFlags & SystemZII::IsLogical) != 0;
00489   if (Value == 0 &&
00490       !IsLogical &&
00491       removeIPMBasedCompare(Compare, SrcReg, MRI, &RI))
00492     return true;
00493   return false;
00494 }
00495 
00496 // If Opcode is a move that has a conditional variant, return that variant,
00497 // otherwise return 0.
00498 static unsigned getConditionalMove(unsigned Opcode) {
00499   switch (Opcode) {
00500   case SystemZ::LR:  return SystemZ::LOCR;
00501   case SystemZ::LGR: return SystemZ::LOCGR;
00502   default:           return 0;
00503   }
00504 }
00505 
00506 bool SystemZInstrInfo::isPredicable(MachineInstr *MI) const {
00507   unsigned Opcode = MI->getOpcode();
00508   if (STI.hasLoadStoreOnCond() &&
00509       getConditionalMove(Opcode))
00510     return true;
00511   return false;
00512 }
00513 
00514 bool SystemZInstrInfo::
00515 isProfitableToIfCvt(MachineBasicBlock &MBB,
00516                     unsigned NumCycles, unsigned ExtraPredCycles,
00517                     const BranchProbability &Probability) const {
00518   // For now only convert single instructions.
00519   return NumCycles == 1;
00520 }
00521 
00522 bool SystemZInstrInfo::
00523 isProfitableToIfCvt(MachineBasicBlock &TMBB,
00524                     unsigned NumCyclesT, unsigned ExtraPredCyclesT,
00525                     MachineBasicBlock &FMBB,
00526                     unsigned NumCyclesF, unsigned ExtraPredCyclesF,
00527                     const BranchProbability &Probability) const {
00528   // For now avoid converting mutually-exclusive cases.
00529   return false;
00530 }
00531 
00532 bool SystemZInstrInfo::
00533 PredicateInstruction(MachineInstr *MI,
00534                      const SmallVectorImpl<MachineOperand> &Pred) const {
00535   assert(Pred.size() == 2 && "Invalid condition");
00536   unsigned CCValid = Pred[0].getImm();
00537   unsigned CCMask = Pred[1].getImm();
00538   assert(CCMask > 0 && CCMask < 15 && "Invalid predicate");
00539   unsigned Opcode = MI->getOpcode();
00540   if (STI.hasLoadStoreOnCond()) {
00541     if (unsigned CondOpcode = getConditionalMove(Opcode)) {
00542       MI->setDesc(get(CondOpcode));
00543       MachineInstrBuilder(*MI->getParent()->getParent(), MI)
00544         .addImm(CCValid).addImm(CCMask)
00545         .addReg(SystemZ::CC, RegState::Implicit);
00546       return true;
00547     }
00548   }
00549   return false;
00550 }
00551 
00552 void
00553 SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
00554             MachineBasicBlock::iterator MBBI, DebugLoc DL,
00555             unsigned DestReg, unsigned SrcReg,
00556             bool KillSrc) const {
00557   // Split 128-bit GPR moves into two 64-bit moves.  This handles ADDR128 too.
00558   if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
00559     copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64),
00560                 RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc);
00561     copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64),
00562                 RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc);
00563     return;
00564   }
00565 
00566   if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) {
00567     emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc);
00568     return;
00569   }
00570 
00571   // Everything else needs only one instruction.
00572   unsigned Opcode;
00573   if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
00574     Opcode = SystemZ::LGR;
00575   else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
00576     Opcode = SystemZ::LER;
00577   else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
00578     Opcode = SystemZ::LDR;
00579   else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
00580     Opcode = SystemZ::LXR;
00581   else
00582     llvm_unreachable("Impossible reg-to-reg copy");
00583 
00584   BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
00585     .addReg(SrcReg, getKillRegState(KillSrc));
00586 }
00587 
00588 void
00589 SystemZInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
00590               MachineBasicBlock::iterator MBBI,
00591               unsigned SrcReg, bool isKill,
00592               int FrameIdx,
00593               const TargetRegisterClass *RC,
00594               const TargetRegisterInfo *TRI) const {
00595   DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
00596 
00597   // Callers may expect a single instruction, so keep 128-bit moves
00598   // together for now and lower them after register allocation.
00599   unsigned LoadOpcode, StoreOpcode;
00600   getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
00601   addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
00602         .addReg(SrcReg, getKillRegState(isKill)), FrameIdx);
00603 }
00604 
00605 void
00606 SystemZInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
00607                MachineBasicBlock::iterator MBBI,
00608                unsigned DestReg, int FrameIdx,
00609                const TargetRegisterClass *RC,
00610                const TargetRegisterInfo *TRI) const {
00611   DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
00612 
00613   // Callers may expect a single instruction, so keep 128-bit moves
00614   // together for now and lower them after register allocation.
00615   unsigned LoadOpcode, StoreOpcode;
00616   getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
00617   addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
00618                     FrameIdx);
00619 }
00620 
00621 // Return true if MI is a simple load or store with a 12-bit displacement
00622 // and no index.  Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
00623 static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) {
00624   const MCInstrDesc &MCID = MI->getDesc();
00625   return ((MCID.TSFlags & Flag) &&
00626           isUInt<12>(MI->getOperand(2).getImm()) &&
00627           MI->getOperand(3).getReg() == 0);
00628 }
00629 
00630 namespace {
00631 struct LogicOp {
00632   LogicOp() : RegSize(0), ImmLSB(0), ImmSize(0) {}
00633   LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize)
00634     : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {}
00635 
00636   operator bool() const { return RegSize; }
00637 
00638   unsigned RegSize, ImmLSB, ImmSize;
00639 };
00640 } // end anonymous namespace
00641 
00642 static LogicOp interpretAndImmediate(unsigned Opcode) {
00643   switch (Opcode) {
00644   case SystemZ::NILMux: return LogicOp(32,  0, 16);
00645   case SystemZ::NIHMux: return LogicOp(32, 16, 16);
00646   case SystemZ::NILL64: return LogicOp(64,  0, 16);
00647   case SystemZ::NILH64: return LogicOp(64, 16, 16);
00648   case SystemZ::NIHL64: return LogicOp(64, 32, 16);
00649   case SystemZ::NIHH64: return LogicOp(64, 48, 16);
00650   case SystemZ::NIFMux: return LogicOp(32,  0, 32);
00651   case SystemZ::NILF64: return LogicOp(64,  0, 32);
00652   case SystemZ::NIHF64: return LogicOp(64, 32, 32);
00653   default:              return LogicOp();
00654   }
00655 }
00656 
00657 // Used to return from convertToThreeAddress after replacing two-address
00658 // instruction OldMI with three-address instruction NewMI.
00659 static MachineInstr *finishConvertToThreeAddress(MachineInstr *OldMI,
00660                                                  MachineInstr *NewMI,
00661                                                  LiveVariables *LV) {
00662   if (LV) {
00663     unsigned NumOps = OldMI->getNumOperands();
00664     for (unsigned I = 1; I < NumOps; ++I) {
00665       MachineOperand &Op = OldMI->getOperand(I);
00666       if (Op.isReg() && Op.isKill())
00667         LV->replaceKillInstruction(Op.getReg(), OldMI, NewMI);
00668     }
00669   }
00670   return NewMI;
00671 }
00672 
00673 MachineInstr *
00674 SystemZInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
00675                                         MachineBasicBlock::iterator &MBBI,
00676                                         LiveVariables *LV) const {
00677   MachineInstr *MI = MBBI;
00678   MachineBasicBlock *MBB = MI->getParent();
00679   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
00680 
00681   unsigned Opcode = MI->getOpcode();
00682   unsigned NumOps = MI->getNumOperands();
00683 
00684   // Try to convert something like SLL into SLLK, if supported.
00685   // We prefer to keep the two-operand form where possible both
00686   // because it tends to be shorter and because some instructions
00687   // have memory forms that can be used during spilling.
00688   if (STI.hasDistinctOps()) {
00689     MachineOperand &Dest = MI->getOperand(0);
00690     MachineOperand &Src = MI->getOperand(1);
00691     unsigned DestReg = Dest.getReg();
00692     unsigned SrcReg = Src.getReg();
00693     // AHIMux is only really a three-operand instruction when both operands
00694     // are low registers.  Try to constrain both operands to be low if
00695     // possible.
00696     if (Opcode == SystemZ::AHIMux &&
00697         TargetRegisterInfo::isVirtualRegister(DestReg) &&
00698         TargetRegisterInfo::isVirtualRegister(SrcReg) &&
00699         MRI.getRegClass(DestReg)->contains(SystemZ::R1L) &&
00700         MRI.getRegClass(SrcReg)->contains(SystemZ::R1L)) {
00701       MRI.constrainRegClass(DestReg, &SystemZ::GR32BitRegClass);
00702       MRI.constrainRegClass(SrcReg, &SystemZ::GR32BitRegClass);
00703     }
00704     int ThreeOperandOpcode = SystemZ::getThreeOperandOpcode(Opcode);
00705     if (ThreeOperandOpcode >= 0) {
00706       MachineInstrBuilder MIB =
00707         BuildMI(*MBB, MBBI, MI->getDebugLoc(), get(ThreeOperandOpcode))
00708         .addOperand(Dest);
00709       // Keep the kill state, but drop the tied flag.
00710       MIB.addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg());
00711       // Keep the remaining operands as-is.
00712       for (unsigned I = 2; I < NumOps; ++I)
00713         MIB.addOperand(MI->getOperand(I));
00714       return finishConvertToThreeAddress(MI, MIB, LV);
00715     }
00716   }
00717 
00718   // Try to convert an AND into an RISBG-type instruction.
00719   if (LogicOp And = interpretAndImmediate(Opcode)) {
00720     uint64_t Imm = MI->getOperand(2).getImm() << And.ImmLSB;
00721     // AND IMMEDIATE leaves the other bits of the register unchanged.
00722     Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB);
00723     unsigned Start, End;
00724     if (isRxSBGMask(Imm, And.RegSize, Start, End)) {
00725       unsigned NewOpcode;
00726       if (And.RegSize == 64)
00727         NewOpcode = SystemZ::RISBG;
00728       else {
00729         NewOpcode = SystemZ::RISBMux;
00730         Start &= 31;
00731         End &= 31;
00732       }
00733       MachineOperand &Dest = MI->getOperand(0);
00734       MachineOperand &Src = MI->getOperand(1);
00735       MachineInstrBuilder MIB =
00736         BuildMI(*MBB, MI, MI->getDebugLoc(), get(NewOpcode))
00737         .addOperand(Dest).addReg(0)
00738         .addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg())
00739         .addImm(Start).addImm(End + 128).addImm(0);
00740       return finishConvertToThreeAddress(MI, MIB, LV);
00741     }
00742   }
00743   return nullptr;
00744 }
00745 
00746 MachineInstr *
00747 SystemZInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
00748                                         MachineInstr *MI,
00749                                         const SmallVectorImpl<unsigned> &Ops,
00750                                         int FrameIndex) const {
00751   const MachineFrameInfo *MFI = MF.getFrameInfo();
00752   unsigned Size = MFI->getObjectSize(FrameIndex);
00753   unsigned Opcode = MI->getOpcode();
00754 
00755   if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
00756     if ((Opcode == SystemZ::LA || Opcode == SystemZ::LAY) &&
00757         isInt<8>(MI->getOperand(2).getImm()) &&
00758         !MI->getOperand(3).getReg()) {
00759       // LA(Y) %reg, CONST(%reg) -> AGSI %mem, CONST
00760       return BuildMI(MF, MI->getDebugLoc(), get(SystemZ::AGSI))
00761         .addFrameIndex(FrameIndex).addImm(0)
00762         .addImm(MI->getOperand(2).getImm());
00763     }
00764     return nullptr;
00765   }
00766 
00767   // All other cases require a single operand.
00768   if (Ops.size() != 1)
00769     return nullptr;
00770 
00771   unsigned OpNum = Ops[0];
00772   assert(Size == MF.getRegInfo()
00773          .getRegClass(MI->getOperand(OpNum).getReg())->getSize() &&
00774          "Invalid size combination");
00775 
00776   if ((Opcode == SystemZ::AHI || Opcode == SystemZ::AGHI) &&
00777       OpNum == 0 &&
00778       isInt<8>(MI->getOperand(2).getImm())) {
00779     // A(G)HI %reg, CONST -> A(G)SI %mem, CONST
00780     Opcode = (Opcode == SystemZ::AHI ? SystemZ::ASI : SystemZ::AGSI);
00781     return BuildMI(MF, MI->getDebugLoc(), get(Opcode))
00782       .addFrameIndex(FrameIndex).addImm(0)
00783       .addImm(MI->getOperand(2).getImm());
00784   }
00785 
00786   if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) {
00787     bool Op0IsGPR = (Opcode == SystemZ::LGDR);
00788     bool Op1IsGPR = (Opcode == SystemZ::LDGR);
00789     // If we're spilling the destination of an LDGR or LGDR, store the
00790     // source register instead.
00791     if (OpNum == 0) {
00792       unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD;
00793       return BuildMI(MF, MI->getDebugLoc(), get(StoreOpcode))
00794         .addOperand(MI->getOperand(1)).addFrameIndex(FrameIndex)
00795         .addImm(0).addReg(0);
00796     }
00797     // If we're spilling the source of an LDGR or LGDR, load the
00798     // destination register instead.
00799     if (OpNum == 1) {
00800       unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD;
00801       unsigned Dest = MI->getOperand(0).getReg();
00802       return BuildMI(MF, MI->getDebugLoc(), get(LoadOpcode), Dest)
00803         .addFrameIndex(FrameIndex).addImm(0).addReg(0);
00804     }
00805   }
00806 
00807   // Look for cases where the source of a simple store or the destination
00808   // of a simple load is being spilled.  Try to use MVC instead.
00809   //
00810   // Although MVC is in practice a fast choice in these cases, it is still
00811   // logically a bytewise copy.  This means that we cannot use it if the
00812   // load or store is volatile.  We also wouldn't be able to use MVC if
00813   // the two memories partially overlap, but that case cannot occur here,
00814   // because we know that one of the memories is a full frame index.
00815   //
00816   // For performance reasons, we also want to avoid using MVC if the addresses
00817   // might be equal.  We don't worry about that case here, because spill slot
00818   // coloring happens later, and because we have special code to remove
00819   // MVCs that turn out to be redundant.
00820   if (OpNum == 0 && MI->hasOneMemOperand()) {
00821     MachineMemOperand *MMO = *MI->memoperands_begin();
00822     if (MMO->getSize() == Size && !MMO->isVolatile()) {
00823       // Handle conversion of loads.
00824       if (isSimpleBD12Move(MI, SystemZII::SimpleBDXLoad)) {
00825         return BuildMI(MF, MI->getDebugLoc(), get(SystemZ::MVC))
00826           .addFrameIndex(FrameIndex).addImm(0).addImm(Size)
00827           .addOperand(MI->getOperand(1)).addImm(MI->getOperand(2).getImm())
00828           .addMemOperand(MMO);
00829       }
00830       // Handle conversion of stores.
00831       if (isSimpleBD12Move(MI, SystemZII::SimpleBDXStore)) {
00832         return BuildMI(MF, MI->getDebugLoc(), get(SystemZ::MVC))
00833           .addOperand(MI->getOperand(1)).addImm(MI->getOperand(2).getImm())
00834           .addImm(Size).addFrameIndex(FrameIndex).addImm(0)
00835           .addMemOperand(MMO);
00836       }
00837     }
00838   }
00839 
00840   // If the spilled operand is the final one, try to change <INSN>R
00841   // into <INSN>.
00842   int MemOpcode = SystemZ::getMemOpcode(Opcode);
00843   if (MemOpcode >= 0) {
00844     unsigned NumOps = MI->getNumExplicitOperands();
00845     if (OpNum == NumOps - 1) {
00846       const MCInstrDesc &MemDesc = get(MemOpcode);
00847       uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags);
00848       assert(AccessBytes != 0 && "Size of access should be known");
00849       assert(AccessBytes <= Size && "Access outside the frame index");
00850       uint64_t Offset = Size - AccessBytes;
00851       MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), get(MemOpcode));
00852       for (unsigned I = 0; I < OpNum; ++I)
00853         MIB.addOperand(MI->getOperand(I));
00854       MIB.addFrameIndex(FrameIndex).addImm(Offset);
00855       if (MemDesc.TSFlags & SystemZII::HasIndex)
00856         MIB.addReg(0);
00857       return MIB;
00858     }
00859   }
00860 
00861   return nullptr;
00862 }
00863 
00864 MachineInstr *
00865 SystemZInstrInfo::foldMemoryOperandImpl(MachineFunction &MF, MachineInstr* MI,
00866                                         const SmallVectorImpl<unsigned> &Ops,
00867                                         MachineInstr* LoadMI) const {
00868   return nullptr;
00869 }
00870 
00871 bool
00872 SystemZInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
00873   switch (MI->getOpcode()) {
00874   case SystemZ::L128:
00875     splitMove(MI, SystemZ::LG);
00876     return true;
00877 
00878   case SystemZ::ST128:
00879     splitMove(MI, SystemZ::STG);
00880     return true;
00881 
00882   case SystemZ::LX:
00883     splitMove(MI, SystemZ::LD);
00884     return true;
00885 
00886   case SystemZ::STX:
00887     splitMove(MI, SystemZ::STD);
00888     return true;
00889 
00890   case SystemZ::LBMux:
00891     expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH);
00892     return true;
00893 
00894   case SystemZ::LHMux:
00895     expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH);
00896     return true;
00897 
00898   case SystemZ::LLCRMux:
00899     expandZExtPseudo(MI, SystemZ::LLCR, 8);
00900     return true;
00901 
00902   case SystemZ::LLHRMux:
00903     expandZExtPseudo(MI, SystemZ::LLHR, 16);
00904     return true;
00905 
00906   case SystemZ::LLCMux:
00907     expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH);
00908     return true;
00909 
00910   case SystemZ::LLHMux:
00911     expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH);
00912     return true;
00913 
00914   case SystemZ::LMux:
00915     expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH);
00916     return true;
00917 
00918   case SystemZ::STCMux:
00919     expandRXYPseudo(MI, SystemZ::STC, SystemZ::STCH);
00920     return true;
00921 
00922   case SystemZ::STHMux:
00923     expandRXYPseudo(MI, SystemZ::STH, SystemZ::STHH);
00924     return true;
00925 
00926   case SystemZ::STMux:
00927     expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH);
00928     return true;
00929 
00930   case SystemZ::LHIMux:
00931     expandRIPseudo(MI, SystemZ::LHI, SystemZ::IIHF, true);
00932     return true;
00933 
00934   case SystemZ::IIFMux:
00935     expandRIPseudo(MI, SystemZ::IILF, SystemZ::IIHF, false);
00936     return true;
00937 
00938   case SystemZ::IILMux:
00939     expandRIPseudo(MI, SystemZ::IILL, SystemZ::IIHL, false);
00940     return true;
00941 
00942   case SystemZ::IIHMux:
00943     expandRIPseudo(MI, SystemZ::IILH, SystemZ::IIHH, false);
00944     return true;
00945 
00946   case SystemZ::NIFMux:
00947     expandRIPseudo(MI, SystemZ::NILF, SystemZ::NIHF, false);
00948     return true;
00949 
00950   case SystemZ::NILMux:
00951     expandRIPseudo(MI, SystemZ::NILL, SystemZ::NIHL, false);
00952     return true;
00953 
00954   case SystemZ::NIHMux:
00955     expandRIPseudo(MI, SystemZ::NILH, SystemZ::NIHH, false);
00956     return true;
00957 
00958   case SystemZ::OIFMux:
00959     expandRIPseudo(MI, SystemZ::OILF, SystemZ::OIHF, false);
00960     return true;
00961 
00962   case SystemZ::OILMux:
00963     expandRIPseudo(MI, SystemZ::OILL, SystemZ::OIHL, false);
00964     return true;
00965 
00966   case SystemZ::OIHMux:
00967     expandRIPseudo(MI, SystemZ::OILH, SystemZ::OIHH, false);
00968     return true;
00969 
00970   case SystemZ::XIFMux:
00971     expandRIPseudo(MI, SystemZ::XILF, SystemZ::XIHF, false);
00972     return true;
00973 
00974   case SystemZ::TMLMux:
00975     expandRIPseudo(MI, SystemZ::TMLL, SystemZ::TMHL, false);
00976     return true;
00977 
00978   case SystemZ::TMHMux:
00979     expandRIPseudo(MI, SystemZ::TMLH, SystemZ::TMHH, false);
00980     return true;
00981 
00982   case SystemZ::AHIMux:
00983     expandRIPseudo(MI, SystemZ::AHI, SystemZ::AIH, false);
00984     return true;
00985 
00986   case SystemZ::AHIMuxK:
00987     expandRIEPseudo(MI, SystemZ::AHI, SystemZ::AHIK, SystemZ::AIH);
00988     return true;
00989 
00990   case SystemZ::AFIMux:
00991     expandRIPseudo(MI, SystemZ::AFI, SystemZ::AIH, false);
00992     return true;
00993 
00994   case SystemZ::CFIMux:
00995     expandRIPseudo(MI, SystemZ::CFI, SystemZ::CIH, false);
00996     return true;
00997 
00998   case SystemZ::CLFIMux:
00999     expandRIPseudo(MI, SystemZ::CLFI, SystemZ::CLIH, false);
01000     return true;
01001 
01002   case SystemZ::CMux:
01003     expandRXYPseudo(MI, SystemZ::C, SystemZ::CHF);
01004     return true;
01005 
01006   case SystemZ::CLMux:
01007     expandRXYPseudo(MI, SystemZ::CL, SystemZ::CLHF);
01008     return true;
01009 
01010   case SystemZ::RISBMux: {
01011     bool DestIsHigh = isHighReg(MI->getOperand(0).getReg());
01012     bool SrcIsHigh = isHighReg(MI->getOperand(2).getReg());
01013     if (SrcIsHigh == DestIsHigh)
01014       MI->setDesc(get(DestIsHigh ? SystemZ::RISBHH : SystemZ::RISBLL));
01015     else {
01016       MI->setDesc(get(DestIsHigh ? SystemZ::RISBHL : SystemZ::RISBLH));
01017       MI->getOperand(5).setImm(MI->getOperand(5).getImm() ^ 32);
01018     }
01019     return true;
01020   }
01021 
01022   case SystemZ::ADJDYNALLOC:
01023     splitAdjDynAlloc(MI);
01024     return true;
01025 
01026   default:
01027     return false;
01028   }
01029 }
01030 
01031 uint64_t SystemZInstrInfo::getInstSizeInBytes(const MachineInstr *MI) const {
01032   if (MI->getOpcode() == TargetOpcode::INLINEASM) {
01033     const MachineFunction *MF = MI->getParent()->getParent();
01034     const char *AsmStr = MI->getOperand(0).getSymbolName();
01035     return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
01036   }
01037   return MI->getDesc().getSize();
01038 }
01039 
01040 SystemZII::Branch
01041 SystemZInstrInfo::getBranchInfo(const MachineInstr *MI) const {
01042   switch (MI->getOpcode()) {
01043   case SystemZ::BR:
01044   case SystemZ::J:
01045   case SystemZ::JG:
01046     return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY,
01047                              SystemZ::CCMASK_ANY, &MI->getOperand(0));
01048 
01049   case SystemZ::BRC:
01050   case SystemZ::BRCL:
01051     return SystemZII::Branch(SystemZII::BranchNormal,
01052                              MI->getOperand(0).getImm(),
01053                              MI->getOperand(1).getImm(), &MI->getOperand(2));
01054 
01055   case SystemZ::BRCT:
01056     return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP,
01057                              SystemZ::CCMASK_CMP_NE, &MI->getOperand(2));
01058 
01059   case SystemZ::BRCTG:
01060     return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP,
01061                              SystemZ::CCMASK_CMP_NE, &MI->getOperand(2));
01062 
01063   case SystemZ::CIJ:
01064   case SystemZ::CRJ:
01065     return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP,
01066                              MI->getOperand(2).getImm(), &MI->getOperand(3));
01067 
01068   case SystemZ::CLIJ:
01069   case SystemZ::CLRJ:
01070     return SystemZII::Branch(SystemZII::BranchCL, SystemZ::CCMASK_ICMP,
01071                              MI->getOperand(2).getImm(), &MI->getOperand(3));
01072 
01073   case SystemZ::CGIJ:
01074   case SystemZ::CGRJ:
01075     return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP,
01076                              MI->getOperand(2).getImm(), &MI->getOperand(3));
01077 
01078   case SystemZ::CLGIJ:
01079   case SystemZ::CLGRJ:
01080     return SystemZII::Branch(SystemZII::BranchCLG, SystemZ::CCMASK_ICMP,
01081                              MI->getOperand(2).getImm(), &MI->getOperand(3));
01082 
01083   default:
01084     llvm_unreachable("Unrecognized branch opcode");
01085   }
01086 }
01087 
01088 void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC,
01089                                            unsigned &LoadOpcode,
01090                                            unsigned &StoreOpcode) const {
01091   if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
01092     LoadOpcode = SystemZ::L;
01093     StoreOpcode = SystemZ::ST;
01094   } else if (RC == &SystemZ::GRH32BitRegClass) {
01095     LoadOpcode = SystemZ::LFH;
01096     StoreOpcode = SystemZ::STFH;
01097   } else if (RC == &SystemZ::GRX32BitRegClass) {
01098     LoadOpcode = SystemZ::LMux;
01099     StoreOpcode = SystemZ::STMux;
01100   } else if (RC == &SystemZ::GR64BitRegClass ||
01101              RC == &SystemZ::ADDR64BitRegClass) {
01102     LoadOpcode = SystemZ::LG;
01103     StoreOpcode = SystemZ::STG;
01104   } else if (RC == &SystemZ::GR128BitRegClass ||
01105              RC == &SystemZ::ADDR128BitRegClass) {
01106     LoadOpcode = SystemZ::L128;
01107     StoreOpcode = SystemZ::ST128;
01108   } else if (RC == &SystemZ::FP32BitRegClass) {
01109     LoadOpcode = SystemZ::LE;
01110     StoreOpcode = SystemZ::STE;
01111   } else if (RC == &SystemZ::FP64BitRegClass) {
01112     LoadOpcode = SystemZ::LD;
01113     StoreOpcode = SystemZ::STD;
01114   } else if (RC == &SystemZ::FP128BitRegClass) {
01115     LoadOpcode = SystemZ::LX;
01116     StoreOpcode = SystemZ::STX;
01117   } else
01118     llvm_unreachable("Unsupported regclass to load or store");
01119 }
01120 
01121 unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode,
01122                                               int64_t Offset) const {
01123   const MCInstrDesc &MCID = get(Opcode);
01124   int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
01125   if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
01126     // Get the instruction to use for unsigned 12-bit displacements.
01127     int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
01128     if (Disp12Opcode >= 0)
01129       return Disp12Opcode;
01130 
01131     // All address-related instructions can use unsigned 12-bit
01132     // displacements.
01133     return Opcode;
01134   }
01135   if (isInt<20>(Offset) && isInt<20>(Offset2)) {
01136     // Get the instruction to use for signed 20-bit displacements.
01137     int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
01138     if (Disp20Opcode >= 0)
01139       return Disp20Opcode;
01140 
01141     // Check whether Opcode allows signed 20-bit displacements.
01142     if (MCID.TSFlags & SystemZII::Has20BitOffset)
01143       return Opcode;
01144   }
01145   return 0;
01146 }
01147 
01148 unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const {
01149   switch (Opcode) {
01150   case SystemZ::L:    return SystemZ::LT;
01151   case SystemZ::LY:   return SystemZ::LT;
01152   case SystemZ::LG:   return SystemZ::LTG;
01153   case SystemZ::LGF:  return SystemZ::LTGF;
01154   case SystemZ::LR:   return SystemZ::LTR;
01155   case SystemZ::LGFR: return SystemZ::LTGFR;
01156   case SystemZ::LGR:  return SystemZ::LTGR;
01157   case SystemZ::LER:  return SystemZ::LTEBR;
01158   case SystemZ::LDR:  return SystemZ::LTDBR;
01159   case SystemZ::LXR:  return SystemZ::LTXBR;
01160   default:            return 0;
01161   }
01162 }
01163 
01164 // Return true if Mask matches the regexp 0*1+0*, given that zero masks
01165 // have already been filtered out.  Store the first set bit in LSB and
01166 // the number of set bits in Length if so.
01167 static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) {
01168   unsigned First = findFirstSet(Mask);
01169   uint64_t Top = (Mask >> First) + 1;
01170   if ((Top & -Top) == Top) {
01171     LSB = First;
01172     Length = findFirstSet(Top);
01173     return true;
01174   }
01175   return false;
01176 }
01177 
01178 bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize,
01179                                    unsigned &Start, unsigned &End) const {
01180   // Reject trivial all-zero masks.
01181   if (Mask == 0)
01182     return false;
01183 
01184   // Handle the 1+0+ or 0+1+0* cases.  Start then specifies the index of
01185   // the msb and End specifies the index of the lsb.
01186   unsigned LSB, Length;
01187   if (isStringOfOnes(Mask, LSB, Length)) {
01188     Start = 63 - (LSB + Length - 1);
01189     End = 63 - LSB;
01190     return true;
01191   }
01192 
01193   // Handle the wrap-around 1+0+1+ cases.  Start then specifies the msb
01194   // of the low 1s and End specifies the lsb of the high 1s.
01195   if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) {
01196     assert(LSB > 0 && "Bottom bit must be set");
01197     assert(LSB + Length < BitSize && "Top bit must be set");
01198     Start = 63 - (LSB - 1);
01199     End = 63 - (LSB + Length);
01200     return true;
01201   }
01202 
01203   return false;
01204 }
01205 
01206 unsigned SystemZInstrInfo::getCompareAndBranch(unsigned Opcode,
01207                                                const MachineInstr *MI) const {
01208   switch (Opcode) {
01209   case SystemZ::CR:
01210     return SystemZ::CRJ;
01211   case SystemZ::CGR:
01212     return SystemZ::CGRJ;
01213   case SystemZ::CHI:
01214     return MI && isInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CIJ : 0;
01215   case SystemZ::CGHI:
01216     return MI && isInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CGIJ : 0;
01217   case SystemZ::CLR:
01218     return SystemZ::CLRJ;
01219   case SystemZ::CLGR:
01220     return SystemZ::CLGRJ;
01221   case SystemZ::CLFI:
01222     return MI && isUInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CLIJ : 0;
01223   case SystemZ::CLGFI:
01224     return MI && isUInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CLGIJ : 0;
01225   default:
01226     return 0;
01227   }
01228 }
01229 
01230 void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB,
01231                                      MachineBasicBlock::iterator MBBI,
01232                                      unsigned Reg, uint64_t Value) const {
01233   DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
01234   unsigned Opcode;
01235   if (isInt<16>(Value))
01236     Opcode = SystemZ::LGHI;
01237   else if (SystemZ::isImmLL(Value))
01238     Opcode = SystemZ::LLILL;
01239   else if (SystemZ::isImmLH(Value)) {
01240     Opcode = SystemZ::LLILH;
01241     Value >>= 16;
01242   } else {
01243     assert(isInt<32>(Value) && "Huge values not handled yet");
01244     Opcode = SystemZ::LGFI;
01245   }
01246   BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
01247 }