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 "SystemZ.h"
00015 #include "SystemZInstrBuilder.h"
00016 #include "SystemZInstrInfo.h"
00017 #include "SystemZMachineFunctionInfo.h"
00018 #include "SystemZTargetMachine.h"
00019 #include "SystemZGenInstrInfo.inc"
00020 #include "llvm/Function.h"
00021 #include "llvm/CodeGen/MachineFrameInfo.h"
00022 #include "llvm/CodeGen/MachineInstrBuilder.h"
00023 #include "llvm/CodeGen/MachineRegisterInfo.h"
00024 #include "llvm/CodeGen/PseudoSourceValue.h"
00025 
00026 using namespace llvm;
00027 
00028 SystemZInstrInfo::SystemZInstrInfo(SystemZTargetMachine &tm)
00029   : TargetInstrInfoImpl(SystemZInsts, array_lengthof(SystemZInsts)),
00030     RI(tm, *this), TM(tm) {
00031   // Fill the spill offsets map
00032   static const unsigned SpillOffsTab[][2] = {
00033     { SystemZ::R2D,  0x10 },
00034     { SystemZ::R3D,  0x18 },
00035     { SystemZ::R4D,  0x20 },
00036     { SystemZ::R5D,  0x28 },
00037     { SystemZ::R6D,  0x30 },
00038     { SystemZ::R7D,  0x38 },
00039     { SystemZ::R8D,  0x40 },
00040     { SystemZ::R9D,  0x48 },
00041     { SystemZ::R10D, 0x50 },
00042     { SystemZ::R11D, 0x58 },
00043     { SystemZ::R12D, 0x60 },
00044     { SystemZ::R13D, 0x68 },
00045     { SystemZ::R14D, 0x70 },
00046     { SystemZ::R15D, 0x78 }
00047   };
00048 
00049   RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS);
00050 
00051   for (unsigned i = 0, e = array_lengthof(SpillOffsTab); i != e; ++i)
00052     RegSpillOffsets[SpillOffsTab[i][0]] = SpillOffsTab[i][1];
00053 }
00054 
00055 /// isGVStub - Return true if the GV requires an extra load to get the
00056 /// real address.
00057 static inline bool isGVStub(GlobalValue *GV, SystemZTargetMachine &TM) {
00058   return TM.getSubtarget<SystemZSubtarget>().GVRequiresExtraLoad(GV, TM, false);
00059 }
00060 
00061 void SystemZInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
00062                                           MachineBasicBlock::iterator MI,
00063                                     unsigned SrcReg, bool isKill, int FrameIdx,
00064                                     const TargetRegisterClass *RC) const {
00065   DebugLoc DL = DebugLoc::getUnknownLoc();
00066   if (MI != MBB.end()) DL = MI->getDebugLoc();
00067 
00068   unsigned Opc = 0;
00069   if (RC == &SystemZ::GR32RegClass ||
00070       RC == &SystemZ::ADDR32RegClass)
00071     Opc = SystemZ::MOV32mr;
00072   else if (RC == &SystemZ::GR64RegClass ||
00073            RC == &SystemZ::ADDR64RegClass) {
00074     Opc = SystemZ::MOV64mr;
00075   } else if (RC == &SystemZ::FP32RegClass) {
00076     Opc = SystemZ::FMOV32mr;
00077   } else if (RC == &SystemZ::FP64RegClass) {
00078     Opc = SystemZ::FMOV64mr;
00079   } else if (RC == &SystemZ::GR64PRegClass) {
00080     Opc = SystemZ::MOV64Pmr;
00081   } else if (RC == &SystemZ::GR128RegClass) {
00082     Opc = SystemZ::MOV128mr;
00083   } else
00084     llvm_unreachable("Unsupported regclass to store");
00085 
00086   addFrameReference(BuildMI(MBB, MI, DL, get(Opc)), FrameIdx)
00087     .addReg(SrcReg, getKillRegState(isKill));
00088 }
00089 
00090 void SystemZInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
00091                                            MachineBasicBlock::iterator MI,
00092                                            unsigned DestReg, int FrameIdx,
00093                                            const TargetRegisterClass *RC) const{
00094   DebugLoc DL = DebugLoc::getUnknownLoc();
00095   if (MI != MBB.end()) DL = MI->getDebugLoc();
00096 
00097   unsigned Opc = 0;
00098   if (RC == &SystemZ::GR32RegClass ||
00099       RC == &SystemZ::ADDR32RegClass)
00100     Opc = SystemZ::MOV32rm;
00101   else if (RC == &SystemZ::GR64RegClass ||
00102            RC == &SystemZ::ADDR64RegClass) {
00103     Opc = SystemZ::MOV64rm;
00104   } else if (RC == &SystemZ::FP32RegClass) {
00105     Opc = SystemZ::FMOV32rm;
00106   } else if (RC == &SystemZ::FP64RegClass) {
00107     Opc = SystemZ::FMOV64rm;
00108   } else if (RC == &SystemZ::GR64PRegClass) {
00109     Opc = SystemZ::MOV64Prm;
00110   } else if (RC == &SystemZ::GR128RegClass) {
00111     Opc = SystemZ::MOV128rm;
00112   } else
00113     llvm_unreachable("Unsupported regclass to load");
00114 
00115   addFrameReference(BuildMI(MBB, MI, DL, get(Opc), DestReg), FrameIdx);
00116 }
00117 
00118 bool SystemZInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
00119                                     MachineBasicBlock::iterator I,
00120                                     unsigned DestReg, unsigned SrcReg,
00121                                     const TargetRegisterClass *DestRC,
00122                                     const TargetRegisterClass *SrcRC) const {
00123   DebugLoc DL = DebugLoc::getUnknownLoc();
00124   if (I != MBB.end()) DL = I->getDebugLoc();
00125 
00126   // Determine if DstRC and SrcRC have a common superclass.
00127   const TargetRegisterClass *CommonRC = DestRC;
00128   if (DestRC == SrcRC)
00129     /* Same regclass for source and dest */;
00130   else if (CommonRC->hasSuperClass(SrcRC))
00131     CommonRC = SrcRC;
00132   else if (!CommonRC->hasSubClass(SrcRC))
00133     CommonRC = 0;
00134 
00135   if (CommonRC) {
00136     if (CommonRC == &SystemZ::GR64RegClass ||
00137         CommonRC == &SystemZ::ADDR64RegClass) {
00138       BuildMI(MBB, I, DL, get(SystemZ::MOV64rr), DestReg).addReg(SrcReg);
00139     } else if (CommonRC == &SystemZ::GR32RegClass ||
00140                CommonRC == &SystemZ::ADDR32RegClass) {
00141       BuildMI(MBB, I, DL, get(SystemZ::MOV32rr), DestReg).addReg(SrcReg);
00142     } else if (CommonRC == &SystemZ::GR64PRegClass) {
00143       BuildMI(MBB, I, DL, get(SystemZ::MOV64rrP), DestReg).addReg(SrcReg);
00144     } else if (CommonRC == &SystemZ::GR128RegClass) {
00145       BuildMI(MBB, I, DL, get(SystemZ::MOV128rr), DestReg).addReg(SrcReg);
00146     } else if (CommonRC == &SystemZ::FP32RegClass) {
00147       BuildMI(MBB, I, DL, get(SystemZ::FMOV32rr), DestReg).addReg(SrcReg);
00148     } else if (CommonRC == &SystemZ::FP64RegClass) {
00149       BuildMI(MBB, I, DL, get(SystemZ::FMOV64rr), DestReg).addReg(SrcReg);
00150     } else {
00151       return false;
00152     }
00153 
00154     return true;
00155   }
00156 
00157   if ((SrcRC == &SystemZ::GR64RegClass &&
00158        DestRC == &SystemZ::ADDR64RegClass) ||
00159       (DestRC == &SystemZ::GR64RegClass &&
00160        SrcRC == &SystemZ::ADDR64RegClass)) {
00161     BuildMI(MBB, I, DL, get(SystemZ::MOV64rr), DestReg).addReg(SrcReg);
00162     return true;
00163   } else if ((SrcRC == &SystemZ::GR32RegClass &&
00164               DestRC == &SystemZ::ADDR32RegClass) ||
00165              (DestRC == &SystemZ::GR32RegClass &&
00166               SrcRC == &SystemZ::ADDR32RegClass)) {
00167     BuildMI(MBB, I, DL, get(SystemZ::MOV32rr), DestReg).addReg(SrcReg);
00168     return true;
00169   }
00170 
00171   return false;
00172 }
00173 
00174 bool
00175 SystemZInstrInfo::isMoveInstr(const MachineInstr& MI,
00176                               unsigned &SrcReg, unsigned &DstReg,
00177                               unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
00178   switch (MI.getOpcode()) {
00179   default:
00180     return false;
00181   case SystemZ::MOV32rr:
00182   case SystemZ::MOV64rr:
00183   case SystemZ::MOV64rrP:
00184   case SystemZ::MOV128rr:
00185   case SystemZ::FMOV32rr:
00186   case SystemZ::FMOV64rr:
00187     assert(MI.getNumOperands() >= 2 &&
00188            MI.getOperand(0).isReg() &&
00189            MI.getOperand(1).isReg() &&
00190            "invalid register-register move instruction");
00191     SrcReg = MI.getOperand(1).getReg();
00192     DstReg = MI.getOperand(0).getReg();
00193     SrcSubIdx = MI.getOperand(1).getSubReg();
00194     DstSubIdx = MI.getOperand(0).getSubReg();
00195     return true;
00196   }
00197 }
00198 
00199 unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
00200                                                int &FrameIndex) const {
00201   switch (MI->getOpcode()) {
00202   default: break;
00203   case SystemZ::MOV32rm:
00204   case SystemZ::MOV32rmy:
00205   case SystemZ::MOV64rm:
00206   case SystemZ::MOVSX32rm8:
00207   case SystemZ::MOVSX32rm16y:
00208   case SystemZ::MOVSX64rm8:
00209   case SystemZ::MOVSX64rm16:
00210   case SystemZ::MOVSX64rm32:
00211   case SystemZ::MOVZX32rm8:
00212   case SystemZ::MOVZX32rm16:
00213   case SystemZ::MOVZX64rm8:
00214   case SystemZ::MOVZX64rm16:
00215   case SystemZ::MOVZX64rm32:
00216   case SystemZ::FMOV32rm:
00217   case SystemZ::FMOV32rmy:
00218   case SystemZ::FMOV64rm:
00219   case SystemZ::FMOV64rmy:
00220   case SystemZ::MOV64Prm:
00221   case SystemZ::MOV64Prmy:
00222   case SystemZ::MOV128rm:
00223     if (MI->getOperand(1).isFI() &&
00224         MI->getOperand(2).isImm() && MI->getOperand(3).isReg() &&
00225         MI->getOperand(2).getImm() == 0 && MI->getOperand(3).getReg() == 0) {
00226       FrameIndex = MI->getOperand(1).getIndex();
00227       return MI->getOperand(0).getReg();
00228     }
00229     break;
00230   }
00231   return 0;
00232 }
00233 
00234 unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
00235                                               int &FrameIndex) const {
00236   switch (MI->getOpcode()) {
00237   default: break;
00238   case SystemZ::MOV32mr:
00239   case SystemZ::MOV32mry:
00240   case SystemZ::MOV64mr:
00241   case SystemZ::MOV32m8r:
00242   case SystemZ::MOV32m8ry:
00243   case SystemZ::MOV32m16r:
00244   case SystemZ::MOV32m16ry:
00245   case SystemZ::MOV64m8r:
00246   case SystemZ::MOV64m8ry:
00247   case SystemZ::MOV64m16r:
00248   case SystemZ::MOV64m16ry:
00249   case SystemZ::MOV64m32r:
00250   case SystemZ::MOV64m32ry:
00251   case SystemZ::FMOV32mr:
00252   case SystemZ::FMOV32mry:
00253   case SystemZ::FMOV64mr:
00254   case SystemZ::FMOV64mry:
00255   case SystemZ::MOV64Pmr:
00256   case SystemZ::MOV64Pmry:
00257   case SystemZ::MOV128mr:
00258     if (MI->getOperand(0).isFI() &&
00259         MI->getOperand(1).isImm() && MI->getOperand(2).isReg() &&
00260         MI->getOperand(1).getImm() == 0 && MI->getOperand(2).getReg() == 0) {
00261       FrameIndex = MI->getOperand(0).getIndex();
00262       return MI->getOperand(3).getReg();
00263     }
00264     break;
00265   }
00266   return 0;
00267 }
00268 
00269 bool SystemZInstrInfo::isInvariantLoad(const MachineInstr *MI) const {
00270   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00271     const MachineOperand &MO = MI->getOperand(i);
00272     // Loads from constant pools are trivially invariant.
00273     if (MO.isCPI())
00274       return true;
00275 
00276     if (MO.isGlobal())
00277       return isGVStub(MO.getGlobal(), TM);
00278 
00279     // If this is a load from an invariant stack slot, the load is a constant.
00280     if (MO.isFI()) {
00281       const MachineFrameInfo &MFI =
00282         *MI->getParent()->getParent()->getFrameInfo();
00283       int Idx = MO.getIndex();
00284       return MFI.isFixedObjectIndex(Idx) && MFI.isImmutableObjectIndex(Idx);
00285     }
00286   }
00287 
00288   // All other instances of these instructions are presumed to have other
00289   // issues.
00290   return false;
00291 }
00292 
00293 bool
00294 SystemZInstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
00295                                            MachineBasicBlock::iterator MI,
00296                                 const std::vector<CalleeSavedInfo> &CSI) const {
00297   if (CSI.empty())
00298     return false;
00299 
00300   DebugLoc DL = DebugLoc::getUnknownLoc();
00301   if (MI != MBB.end()) DL = MI->getDebugLoc();
00302 
00303   MachineFunction &MF = *MBB.getParent();
00304   SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
00305   unsigned CalleeFrameSize = 0;
00306 
00307   // Scan the callee-saved and find the bounds of register spill area.
00308   unsigned LowReg = 0, HighReg = 0, StartOffset = -1U, EndOffset = 0;
00309   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
00310     unsigned Reg = CSI[i].getReg();
00311     const TargetRegisterClass *RegClass = CSI[i].getRegClass();
00312     if (RegClass != &SystemZ::FP64RegClass) {
00313       unsigned Offset = RegSpillOffsets[Reg];
00314       CalleeFrameSize += 8;
00315       if (StartOffset > Offset) {
00316         LowReg = Reg; StartOffset = Offset;
00317       }
00318       if (EndOffset < Offset) {
00319         HighReg = Reg; EndOffset = RegSpillOffsets[Reg];
00320       }
00321     }
00322   }
00323 
00324   // Save information for epilogue inserter.
00325   MFI->setCalleeSavedFrameSize(CalleeFrameSize);
00326   MFI->setLowReg(LowReg); MFI->setHighReg(HighReg);
00327 
00328   // Save GPRs
00329   if (StartOffset) {
00330     // Build a store instruction. Use STORE MULTIPLE instruction if there are many
00331     // registers to store, otherwise - just STORE.
00332     MachineInstrBuilder MIB =
00333       BuildMI(MBB, MI, DL, get((LowReg == HighReg ?
00334                                 SystemZ::MOV64mr : SystemZ::MOV64mrm)));
00335 
00336     // Add store operands.
00337     MIB.addReg(SystemZ::R15D).addImm(StartOffset);
00338     if (LowReg == HighReg)
00339       MIB.addReg(0);
00340     MIB.addReg(LowReg, RegState::Kill);
00341     if (LowReg != HighReg)
00342       MIB.addReg(HighReg, RegState::Kill);
00343 
00344     // Do a second scan adding regs as being killed by instruction
00345     for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
00346       unsigned Reg = CSI[i].getReg();
00347       // Add the callee-saved register as live-in. It's killed at the spill.
00348       MBB.addLiveIn(Reg);
00349       if (Reg != LowReg && Reg != HighReg)
00350         MIB.addReg(Reg, RegState::ImplicitKill);
00351     }
00352   }
00353 
00354   // Save FPRs
00355   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
00356     unsigned Reg = CSI[i].getReg();
00357     const TargetRegisterClass *RegClass = CSI[i].getRegClass();
00358     if (RegClass == &SystemZ::FP64RegClass) {
00359       MBB.addLiveIn(Reg);
00360       storeRegToStackSlot(MBB, MI, Reg, true, CSI[i].getFrameIdx(), RegClass);
00361     }
00362   }
00363 
00364   return true;
00365 }
00366 
00367 bool
00368 SystemZInstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
00369                                              MachineBasicBlock::iterator MI,
00370                                 const std::vector<CalleeSavedInfo> &CSI) const {
00371   if (CSI.empty())
00372     return false;
00373 
00374   DebugLoc DL = DebugLoc::getUnknownLoc();
00375   if (MI != MBB.end()) DL = MI->getDebugLoc();
00376 
00377   MachineFunction &MF = *MBB.getParent();
00378   const TargetRegisterInfo *RegInfo= MF.getTarget().getRegisterInfo();
00379   SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
00380 
00381   // Restore FP registers
00382   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
00383     unsigned Reg = CSI[i].getReg();
00384     const TargetRegisterClass *RegClass = CSI[i].getRegClass();
00385     if (RegClass == &SystemZ::FP64RegClass)
00386       loadRegFromStackSlot(MBB, MI, Reg, CSI[i].getFrameIdx(), RegClass);
00387   }
00388 
00389   // Restore GP registers
00390   unsigned LowReg = MFI->getLowReg(), HighReg = MFI->getHighReg();
00391   unsigned StartOffset = RegSpillOffsets[LowReg];
00392 
00393   if (StartOffset) {
00394     // Build a load instruction. Use LOAD MULTIPLE instruction if there are many
00395     // registers to load, otherwise - just LOAD.
00396     MachineInstrBuilder MIB =
00397       BuildMI(MBB, MI, DL, get((LowReg == HighReg ?
00398                                 SystemZ::MOV64rm : SystemZ::MOV64rmm)));
00399     // Add store operands.
00400     MIB.addReg(LowReg, RegState::Define);
00401     if (LowReg != HighReg)
00402       MIB.addReg(HighReg, RegState::Define);
00403 
00404     MIB.addReg((RegInfo->hasFP(MF) ? SystemZ::R11D : SystemZ::R15D));
00405     MIB.addImm(StartOffset);
00406     if (LowReg == HighReg)
00407       MIB.addReg(0);
00408 
00409     // Do a second scan adding regs as being defined by instruction
00410     for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
00411       unsigned Reg = CSI[i].getReg();
00412       if (Reg != LowReg && Reg != HighReg)
00413         MIB.addReg(Reg, RegState::ImplicitDefine);
00414     }
00415   }
00416 
00417   return true;
00418 }
00419 
00420 bool SystemZInstrInfo::
00421 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
00422   assert(Cond.size() == 1 && "Invalid Xbranch condition!");
00423 
00424   SystemZCC::CondCodes CC = static_cast<SystemZCC::CondCodes>(Cond[0].getImm());
00425   Cond[0].setImm(getOppositeCondition(CC));
00426   return false;
00427 }
00428 
00429 bool SystemZInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB)const{
00430   if (MBB.empty()) return false;
00431 
00432   switch (MBB.back().getOpcode()) {
00433   case SystemZ::RET:   // Return.
00434   case SystemZ::JMP:   // Uncond branch.
00435   case SystemZ::JMPr:  // Indirect branch.
00436     return true;
00437   default: return false;
00438   }
00439 }
00440 
00441 bool SystemZInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
00442   const TargetInstrDesc &TID = MI->getDesc();
00443   if (!TID.isTerminator()) return false;
00444 
00445   // Conditional branch is a special case.
00446   if (TID.isBranch() && !TID.isBarrier())
00447     return true;
00448   if (!TID.isPredicable())
00449     return true;
00450   return !isPredicated(MI);
00451 }
00452 
00453 bool SystemZInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
00454                                      MachineBasicBlock *&TBB,
00455                                      MachineBasicBlock *&FBB,
00456                                      SmallVectorImpl<MachineOperand> &Cond,
00457                                      bool AllowModify) const {
00458   // Start from the bottom of the block and work up, examining the
00459   // terminator instructions.
00460   MachineBasicBlock::iterator I = MBB.end();
00461   while (I != MBB.begin()) {
00462     --I;
00463     // Working from the bottom, when we see a non-terminator
00464     // instruction, we're done.
00465     if (!isUnpredicatedTerminator(I))
00466       break;
00467 
00468     // A terminator that isn't a branch can't easily be handled
00469     // by this analysis.
00470     if (!I->getDesc().isBranch())
00471       return true;
00472 
00473     // Handle unconditional branches.
00474     if (I->getOpcode() == SystemZ::JMP) {
00475       if (!AllowModify) {
00476         TBB = I->getOperand(0).getMBB();
00477         continue;
00478       }
00479 
00480       // If the block has any instructions after a JMP, delete them.
00481       while (next(I) != MBB.end())
00482         next(I)->eraseFromParent();
00483       Cond.clear();
00484       FBB = 0;
00485 
00486       // Delete the JMP if it's equivalent to a fall-through.
00487       if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
00488         TBB = 0;
00489         I->eraseFromParent();
00490         I = MBB.end();
00491         continue;
00492       }
00493 
00494       // TBB is used to indicate the unconditinal destination.
00495       TBB = I->getOperand(0).getMBB();
00496       continue;
00497     }
00498 
00499     // Handle conditional branches.
00500     SystemZCC::CondCodes BranchCode = getCondFromBranchOpc(I->getOpcode());
00501     if (BranchCode == SystemZCC::INVALID)
00502       return true;  // Can't handle indirect branch.
00503 
00504     // Working from the bottom, handle the first conditional branch.
00505     if (Cond.empty()) {
00506       FBB = TBB;
00507       TBB = I->getOperand(0).getMBB();
00508       Cond.push_back(MachineOperand::CreateImm(BranchCode));
00509       continue;
00510     }
00511 
00512     // Handle subsequent conditional branches. Only handle the case where all
00513     // conditional branches branch to the same destination.
00514     assert(Cond.size() == 1);
00515     assert(TBB);
00516 
00517     // Only handle the case where all conditional branches branch to
00518     // the same destination.
00519     if (TBB != I->getOperand(0).getMBB())
00520       return true;
00521 
00522     SystemZCC::CondCodes OldBranchCode = (SystemZCC::CondCodes)Cond[0].getImm();
00523     // If the conditions are the same, we can leave them alone.
00524     if (OldBranchCode == BranchCode)
00525       continue;
00526 
00527     return true;
00528   }
00529 
00530   return false;
00531 }
00532 
00533 unsigned SystemZInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
00534   MachineBasicBlock::iterator I = MBB.end();
00535   unsigned Count = 0;
00536 
00537   while (I != MBB.begin()) {
00538     --I;
00539     if (I->getOpcode() != SystemZ::JMP &&
00540         getCondFromBranchOpc(I->getOpcode()) == SystemZCC::INVALID)
00541       break;
00542     // Remove the branch.
00543     I->eraseFromParent();
00544     I = MBB.end();
00545     ++Count;
00546   }
00547 
00548   return Count;
00549 }
00550 
00551 unsigned
00552 SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
00553                                MachineBasicBlock *FBB,
00554                             const SmallVectorImpl<MachineOperand> &Cond) const {
00555   // FIXME: this should probably have a DebugLoc operand
00556   DebugLoc dl = DebugLoc::getUnknownLoc();
00557   // Shouldn't be a fall through.
00558   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
00559   assert((Cond.size() == 1 || Cond.size() == 0) &&
00560          "SystemZ branch conditions have one component!");
00561 
00562   if (Cond.empty()) {
00563     // Unconditional branch?
00564     assert(!FBB && "Unconditional branch with multiple successors!");
00565     BuildMI(&MBB, dl, get(SystemZ::JMP)).addMBB(TBB);
00566     return 1;
00567   }
00568 
00569   // Conditional branch.
00570   unsigned Count = 0;
00571   SystemZCC::CondCodes CC = (SystemZCC::CondCodes)Cond[0].getImm();
00572   BuildMI(&MBB, dl, getBrCond(CC)).addMBB(TBB);
00573   ++Count;
00574 
00575   if (FBB) {
00576     // Two-way Conditional branch. Insert the second branch.
00577     BuildMI(&MBB, dl, get(SystemZ::JMP)).addMBB(FBB);
00578     ++Count;
00579   }
00580   return Count;
00581 }
00582 
00583 const TargetInstrDesc&
00584 SystemZInstrInfo::getBrCond(SystemZCC::CondCodes CC) const {
00585   switch (CC) {
00586   default:
00587    llvm_unreachable("Unknown condition code!");
00588   case SystemZCC::O:   return get(SystemZ::JO);
00589   case SystemZCC::H:   return get(SystemZ::JH);
00590   case SystemZCC::NLE: return get(SystemZ::JNLE);
00591   case SystemZCC::L:   return get(SystemZ::JL);
00592   case SystemZCC::NHE: return get(SystemZ::JNHE);
00593   case SystemZCC::LH:  return get(SystemZ::JLH);
00594   case SystemZCC::NE:  return get(SystemZ::JNE);
00595   case SystemZCC::E:   return get(SystemZ::JE);
00596   case SystemZCC::NLH: return get(SystemZ::JNLH);
00597   case SystemZCC::HE:  return get(SystemZ::JHE);
00598   case SystemZCC::NL:  return get(SystemZ::JNL);
00599   case SystemZCC::LE:  return get(SystemZ::JLE);
00600   case SystemZCC::NH:  return get(SystemZ::JNH);
00601   case SystemZCC::NO:  return get(SystemZ::JNO);
00602   }
00603 }
00604 
00605 SystemZCC::CondCodes
00606 SystemZInstrInfo::getCondFromBranchOpc(unsigned Opc) const {
00607   switch (Opc) {
00608   default:            return SystemZCC::INVALID;
00609   case SystemZ::JO:   return SystemZCC::O;
00610   case SystemZ::JH:   return SystemZCC::H;
00611   case SystemZ::JNLE: return SystemZCC::NLE;
00612   case SystemZ::JL:   return SystemZCC::L;
00613   case SystemZ::JNHE: return SystemZCC::NHE;
00614   case SystemZ::JLH:  return SystemZCC::LH;
00615   case SystemZ::JNE:  return SystemZCC::NE;
00616   case SystemZ::JE:   return SystemZCC::E;
00617   case SystemZ::JNLH: return SystemZCC::NLH;
00618   case SystemZ::JHE:  return SystemZCC::HE;
00619   case SystemZ::JNL:  return SystemZCC::NL;
00620   case SystemZ::JLE:  return SystemZCC::LE;
00621   case SystemZ::JNH:  return SystemZCC::NH;
00622   case SystemZ::JNO:  return SystemZCC::NO;
00623   }
00624 }
00625 
00626 SystemZCC::CondCodes
00627 SystemZInstrInfo::getOppositeCondition(SystemZCC::CondCodes CC) const {
00628   switch (CC) {
00629   default:
00630     llvm_unreachable("Invalid condition!");
00631   case SystemZCC::O:   return SystemZCC::NO;
00632   case SystemZCC::H:   return SystemZCC::NH;
00633   case SystemZCC::NLE: return SystemZCC::LE;
00634   case SystemZCC::L:   return SystemZCC::NL;
00635   case SystemZCC::NHE: return SystemZCC::HE;
00636   case SystemZCC::LH:  return SystemZCC::NLH;
00637   case SystemZCC::NE:  return SystemZCC::E;
00638   case SystemZCC::E:   return SystemZCC::NE;
00639   case SystemZCC::NLH: return SystemZCC::LH;
00640   case SystemZCC::HE:  return SystemZCC::NHE;
00641   case SystemZCC::NL:  return SystemZCC::L;
00642   case SystemZCC::LE:  return SystemZCC::NLE;
00643   case SystemZCC::NH:  return SystemZCC::H;
00644   case SystemZCC::NO:  return SystemZCC::O;
00645   }
00646 }
00647 
00648 const TargetInstrDesc&
00649 SystemZInstrInfo::getLongDispOpc(unsigned Opc) const {
00650   switch (Opc) {
00651   default:
00652     llvm_unreachable("Don't have long disp version of this instruction");
00653   case SystemZ::MOV32mr:   return get(SystemZ::MOV32mry);
00654   case SystemZ::MOV32rm:   return get(SystemZ::MOV32rmy);
00655   case SystemZ::MOVSX32rm16: return get(SystemZ::MOVSX32rm16y);
00656   case SystemZ::MOV32m8r:  return get(SystemZ::MOV32m8ry);
00657   case SystemZ::MOV32m16r: return get(SystemZ::MOV32m16ry);
00658   case SystemZ::MOV64m8r:  return get(SystemZ::MOV64m8ry);
00659   case SystemZ::MOV64m16r: return get(SystemZ::MOV64m16ry);
00660   case SystemZ::MOV64m32r: return get(SystemZ::MOV64m32ry);
00661   case SystemZ::MOV8mi:    return get(SystemZ::MOV8miy);
00662   case SystemZ::MUL32rm:   return get(SystemZ::MUL32rmy);
00663   case SystemZ::CMP32rm:   return get(SystemZ::CMP32rmy);
00664   case SystemZ::UCMP32rm:  return get(SystemZ::UCMP32rmy);
00665   case SystemZ::FMOV32mr:  return get(SystemZ::FMOV32mry);
00666   case SystemZ::FMOV64mr:  return get(SystemZ::FMOV64mry);
00667   case SystemZ::FMOV32rm:  return get(SystemZ::FMOV32rmy);
00668   case SystemZ::FMOV64rm:  return get(SystemZ::FMOV64rmy);
00669   case SystemZ::MOV64Pmr:  return get(SystemZ::MOV64Pmry);
00670   case SystemZ::MOV64Prm:  return get(SystemZ::MOV64Prmy);
00671   }
00672 }