LLVM API Documentation

Thumb2InstrInfo.cpp
Go to the documentation of this file.
00001 //===-- Thumb2InstrInfo.cpp - Thumb-2 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 Thumb-2 implementation of the TargetInstrInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "Thumb2InstrInfo.h"
00015 #include "ARMConstantPoolValue.h"
00016 #include "ARMMachineFunctionInfo.h"
00017 #include "MCTargetDesc/ARMAddressingModes.h"
00018 #include "llvm/CodeGen/MachineFrameInfo.h"
00019 #include "llvm/CodeGen/MachineInstrBuilder.h"
00020 #include "llvm/CodeGen/MachineMemOperand.h"
00021 #include "llvm/CodeGen/MachineRegisterInfo.h"
00022 #include "llvm/MC/MCInst.h"
00023 #include "llvm/Support/CommandLine.h"
00024 
00025 using namespace llvm;
00026 
00027 static cl::opt<bool>
00028 OldT2IfCvt("old-thumb2-ifcvt", cl::Hidden,
00029            cl::desc("Use old-style Thumb2 if-conversion heuristics"),
00030            cl::init(false));
00031 
00032 Thumb2InstrInfo::Thumb2InstrInfo(const ARMSubtarget &STI)
00033   : ARMBaseInstrInfo(STI), RI(STI) {
00034 }
00035 
00036 /// getNoopForMachoTarget - Return the noop instruction to use for a noop.
00037 void Thumb2InstrInfo::getNoopForMachoTarget(MCInst &NopInst) const {
00038   NopInst.setOpcode(ARM::tHINT);
00039   NopInst.addOperand(MCOperand::CreateImm(0));
00040   NopInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
00041   NopInst.addOperand(MCOperand::CreateReg(0));
00042 }
00043 
00044 unsigned Thumb2InstrInfo::getUnindexedOpcode(unsigned Opc) const {
00045   // FIXME
00046   return 0;
00047 }
00048 
00049 void
00050 Thumb2InstrInfo::ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail,
00051                                          MachineBasicBlock *NewDest) const {
00052   MachineBasicBlock *MBB = Tail->getParent();
00053   ARMFunctionInfo *AFI = MBB->getParent()->getInfo<ARMFunctionInfo>();
00054   if (!AFI->hasITBlocks()) {
00055     TargetInstrInfo::ReplaceTailWithBranchTo(Tail, NewDest);
00056     return;
00057   }
00058 
00059   // If the first instruction of Tail is predicated, we may have to update
00060   // the IT instruction.
00061   unsigned PredReg = 0;
00062   ARMCC::CondCodes CC = getInstrPredicate(Tail, PredReg);
00063   MachineBasicBlock::iterator MBBI = Tail;
00064   if (CC != ARMCC::AL)
00065     // Expecting at least the t2IT instruction before it.
00066     --MBBI;
00067 
00068   // Actually replace the tail.
00069   TargetInstrInfo::ReplaceTailWithBranchTo(Tail, NewDest);
00070 
00071   // Fix up IT.
00072   if (CC != ARMCC::AL) {
00073     MachineBasicBlock::iterator E = MBB->begin();
00074     unsigned Count = 4; // At most 4 instructions in an IT block.
00075     while (Count && MBBI != E) {
00076       if (MBBI->isDebugValue()) {
00077         --MBBI;
00078         continue;
00079       }
00080       if (MBBI->getOpcode() == ARM::t2IT) {
00081         unsigned Mask = MBBI->getOperand(1).getImm();
00082         if (Count == 4)
00083           MBBI->eraseFromParent();
00084         else {
00085           unsigned MaskOn = 1 << Count;
00086           unsigned MaskOff = ~(MaskOn - 1);
00087           MBBI->getOperand(1).setImm((Mask & MaskOff) | MaskOn);
00088         }
00089         return;
00090       }
00091       --MBBI;
00092       --Count;
00093     }
00094 
00095     // Ctrl flow can reach here if branch folding is run before IT block
00096     // formation pass.
00097   }
00098 }
00099 
00100 bool
00101 Thumb2InstrInfo::isLegalToSplitMBBAt(MachineBasicBlock &MBB,
00102                                      MachineBasicBlock::iterator MBBI) const {
00103   while (MBBI->isDebugValue()) {
00104     ++MBBI;
00105     if (MBBI == MBB.end())
00106       return false;
00107   }
00108 
00109   unsigned PredReg = 0;
00110   return getITInstrPredicate(MBBI, PredReg) == ARMCC::AL;
00111 }
00112 
00113 void Thumb2InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
00114                                   MachineBasicBlock::iterator I, DebugLoc DL,
00115                                   unsigned DestReg, unsigned SrcReg,
00116                                   bool KillSrc) const {
00117   // Handle SPR, DPR, and QPR copies.
00118   if (!ARM::GPRRegClass.contains(DestReg, SrcReg))
00119     return ARMBaseInstrInfo::copyPhysReg(MBB, I, DL, DestReg, SrcReg, KillSrc);
00120 
00121   AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::tMOVr), DestReg)
00122     .addReg(SrcReg, getKillRegState(KillSrc)));
00123 }
00124 
00125 void Thumb2InstrInfo::
00126 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
00127                     unsigned SrcReg, bool isKill, int FI,
00128                     const TargetRegisterClass *RC,
00129                     const TargetRegisterInfo *TRI) const {
00130   DebugLoc DL;
00131   if (I != MBB.end()) DL = I->getDebugLoc();
00132 
00133   MachineFunction &MF = *MBB.getParent();
00134   MachineFrameInfo &MFI = *MF.getFrameInfo();
00135   MachineMemOperand *MMO =
00136     MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
00137                             MachineMemOperand::MOStore,
00138                             MFI.getObjectSize(FI),
00139                             MFI.getObjectAlignment(FI));
00140 
00141   if (RC == &ARM::GPRRegClass   || RC == &ARM::tGPRRegClass ||
00142       RC == &ARM::tcGPRRegClass || RC == &ARM::rGPRRegClass ||
00143       RC == &ARM::GPRnopcRegClass) {
00144     AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::t2STRi12))
00145                    .addReg(SrcReg, getKillRegState(isKill))
00146                    .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
00147     return;
00148   }
00149 
00150   if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
00151     // Thumb2 STRD expects its dest-registers to be in rGPR. Not a problem for
00152     // gsub_0, but needs an extra constraint for gsub_1 (which could be sp
00153     // otherwise).
00154     MachineRegisterInfo *MRI = &MF.getRegInfo();
00155     MRI->constrainRegClass(SrcReg, &ARM::GPRPair_with_gsub_1_in_rGPRRegClass);
00156 
00157     MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::t2STRDi8));
00158     AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
00159     AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
00160     MIB.addFrameIndex(FI).addImm(0).addMemOperand(MMO);
00161     AddDefaultPred(MIB);
00162     return;
00163   }
00164 
00165   ARMBaseInstrInfo::storeRegToStackSlot(MBB, I, SrcReg, isKill, FI, RC, TRI);
00166 }
00167 
00168 void Thumb2InstrInfo::
00169 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
00170                      unsigned DestReg, int FI,
00171                      const TargetRegisterClass *RC,
00172                      const TargetRegisterInfo *TRI) const {
00173   MachineFunction &MF = *MBB.getParent();
00174   MachineFrameInfo &MFI = *MF.getFrameInfo();
00175   MachineMemOperand *MMO =
00176     MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
00177                             MachineMemOperand::MOLoad,
00178                             MFI.getObjectSize(FI),
00179                             MFI.getObjectAlignment(FI));
00180   DebugLoc DL;
00181   if (I != MBB.end()) DL = I->getDebugLoc();
00182 
00183   if (RC == &ARM::GPRRegClass   || RC == &ARM::tGPRRegClass ||
00184       RC == &ARM::tcGPRRegClass || RC == &ARM::rGPRRegClass ||
00185       RC == &ARM::GPRnopcRegClass) {
00186     AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::t2LDRi12), DestReg)
00187                    .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
00188     return;
00189   }
00190 
00191   if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
00192     // Thumb2 LDRD expects its dest-registers to be in rGPR. Not a problem for
00193     // gsub_0, but needs an extra constraint for gsub_1 (which could be sp
00194     // otherwise).
00195     MachineRegisterInfo *MRI = &MF.getRegInfo();
00196     MRI->constrainRegClass(DestReg, &ARM::GPRPair_with_gsub_1_in_rGPRRegClass);
00197 
00198     MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::t2LDRDi8));
00199     AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
00200     AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
00201     MIB.addFrameIndex(FI).addImm(0).addMemOperand(MMO);
00202     AddDefaultPred(MIB);
00203 
00204     if (TargetRegisterInfo::isPhysicalRegister(DestReg))
00205       MIB.addReg(DestReg, RegState::ImplicitDefine);
00206     return;
00207   }
00208 
00209   ARMBaseInstrInfo::loadRegFromStackSlot(MBB, I, DestReg, FI, RC, TRI);
00210 }
00211 
00212 void
00213 Thumb2InstrInfo::expandLoadStackGuard(MachineBasicBlock::iterator MI,
00214                                       Reloc::Model RM) const {
00215   if (RM == Reloc::PIC_)
00216     expandLoadStackGuardBase(MI, ARM::t2MOV_ga_pcrel, ARM::t2LDRi12, RM);
00217   else
00218     expandLoadStackGuardBase(MI, ARM::t2MOVi32imm, ARM::t2LDRi12, RM);
00219 }
00220 
00221 void llvm::emitT2RegPlusImmediate(MachineBasicBlock &MBB,
00222                                MachineBasicBlock::iterator &MBBI, DebugLoc dl,
00223                                unsigned DestReg, unsigned BaseReg, int NumBytes,
00224                                ARMCC::CondCodes Pred, unsigned PredReg,
00225                                const ARMBaseInstrInfo &TII, unsigned MIFlags) {
00226   if (NumBytes == 0 && DestReg != BaseReg) {
00227     BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), DestReg)
00228       .addReg(BaseReg, RegState::Kill)
00229       .addImm((unsigned)Pred).addReg(PredReg).setMIFlags(MIFlags);
00230     return;
00231   }
00232 
00233   bool isSub = NumBytes < 0;
00234   if (isSub) NumBytes = -NumBytes;
00235 
00236   // If profitable, use a movw or movt to materialize the offset.
00237   // FIXME: Use the scavenger to grab a scratch register.
00238   if (DestReg != ARM::SP && DestReg != BaseReg &&
00239       NumBytes >= 4096 &&
00240       ARM_AM::getT2SOImmVal(NumBytes) == -1) {
00241     bool Fits = false;
00242     if (NumBytes < 65536) {
00243       // Use a movw to materialize the 16-bit constant.
00244       BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), DestReg)
00245         .addImm(NumBytes)
00246         .addImm((unsigned)Pred).addReg(PredReg).setMIFlags(MIFlags);
00247       Fits = true;
00248     } else if ((NumBytes & 0xffff) == 0) {
00249       // Use a movt to materialize the 32-bit constant.
00250       BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVTi16), DestReg)
00251         .addReg(DestReg)
00252         .addImm(NumBytes >> 16)
00253         .addImm((unsigned)Pred).addReg(PredReg).setMIFlags(MIFlags);
00254       Fits = true;
00255     }
00256 
00257     if (Fits) {
00258       if (isSub) {
00259         BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr), DestReg)
00260           .addReg(BaseReg, RegState::Kill)
00261           .addReg(DestReg, RegState::Kill)
00262           .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
00263           .setMIFlags(MIFlags);
00264       } else {
00265         BuildMI(MBB, MBBI, dl, TII.get(ARM::t2ADDrr), DestReg)
00266           .addReg(DestReg, RegState::Kill)
00267           .addReg(BaseReg, RegState::Kill)
00268           .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
00269           .setMIFlags(MIFlags);
00270       }
00271       return;
00272     }
00273   }
00274 
00275   while (NumBytes) {
00276     unsigned ThisVal = NumBytes;
00277     unsigned Opc = 0;
00278     if (DestReg == ARM::SP && BaseReg != ARM::SP) {
00279       // mov sp, rn. Note t2MOVr cannot be used.
00280       AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),DestReg)
00281         .addReg(BaseReg).setMIFlags(MIFlags));
00282       BaseReg = ARM::SP;
00283       continue;
00284     }
00285 
00286     bool HasCCOut = true;
00287     if (BaseReg == ARM::SP) {
00288       // sub sp, sp, #imm7
00289       if (DestReg == ARM::SP && (ThisVal < ((1 << 7)-1) * 4)) {
00290         assert((ThisVal & 3) == 0 && "Stack update is not multiple of 4?");
00291         Opc = isSub ? ARM::tSUBspi : ARM::tADDspi;
00292         AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
00293           .addReg(BaseReg).addImm(ThisVal/4).setMIFlags(MIFlags));
00294         NumBytes = 0;
00295         continue;
00296       }
00297 
00298       // sub rd, sp, so_imm
00299       Opc = isSub ? ARM::t2SUBri : ARM::t2ADDri;
00300       if (ARM_AM::getT2SOImmVal(NumBytes) != -1) {
00301         NumBytes = 0;
00302       } else {
00303         // FIXME: Move this to ARMAddressingModes.h?
00304         unsigned RotAmt = countLeadingZeros(ThisVal);
00305         ThisVal = ThisVal & ARM_AM::rotr32(0xff000000U, RotAmt);
00306         NumBytes &= ~ThisVal;
00307         assert(ARM_AM::getT2SOImmVal(ThisVal) != -1 &&
00308                "Bit extraction didn't work?");
00309       }
00310     } else {
00311       assert(DestReg != ARM::SP && BaseReg != ARM::SP);
00312       Opc = isSub ? ARM::t2SUBri : ARM::t2ADDri;
00313       if (ARM_AM::getT2SOImmVal(NumBytes) != -1) {
00314         NumBytes = 0;
00315       } else if (ThisVal < 4096) {
00316         Opc = isSub ? ARM::t2SUBri12 : ARM::t2ADDri12;
00317         HasCCOut = false;
00318         NumBytes = 0;
00319       } else {
00320         // FIXME: Move this to ARMAddressingModes.h?
00321         unsigned RotAmt = countLeadingZeros(ThisVal);
00322         ThisVal = ThisVal & ARM_AM::rotr32(0xff000000U, RotAmt);
00323         NumBytes &= ~ThisVal;
00324         assert(ARM_AM::getT2SOImmVal(ThisVal) != -1 &&
00325                "Bit extraction didn't work?");
00326       }
00327     }
00328 
00329     // Build the new ADD / SUB.
00330     MachineInstrBuilder MIB =
00331       AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
00332                      .addReg(BaseReg, RegState::Kill)
00333                      .addImm(ThisVal)).setMIFlags(MIFlags);
00334     if (HasCCOut)
00335       AddDefaultCC(MIB);
00336 
00337     BaseReg = DestReg;
00338   }
00339 }
00340 
00341 static unsigned
00342 negativeOffsetOpcode(unsigned opcode)
00343 {
00344   switch (opcode) {
00345   case ARM::t2LDRi12:   return ARM::t2LDRi8;
00346   case ARM::t2LDRHi12:  return ARM::t2LDRHi8;
00347   case ARM::t2LDRBi12:  return ARM::t2LDRBi8;
00348   case ARM::t2LDRSHi12: return ARM::t2LDRSHi8;
00349   case ARM::t2LDRSBi12: return ARM::t2LDRSBi8;
00350   case ARM::t2STRi12:   return ARM::t2STRi8;
00351   case ARM::t2STRBi12:  return ARM::t2STRBi8;
00352   case ARM::t2STRHi12:  return ARM::t2STRHi8;
00353   case ARM::t2PLDi12:   return ARM::t2PLDi8;
00354 
00355   case ARM::t2LDRi8:
00356   case ARM::t2LDRHi8:
00357   case ARM::t2LDRBi8:
00358   case ARM::t2LDRSHi8:
00359   case ARM::t2LDRSBi8:
00360   case ARM::t2STRi8:
00361   case ARM::t2STRBi8:
00362   case ARM::t2STRHi8:
00363   case ARM::t2PLDi8:
00364     return opcode;
00365 
00366   default:
00367     break;
00368   }
00369 
00370   return 0;
00371 }
00372 
00373 static unsigned
00374 positiveOffsetOpcode(unsigned opcode)
00375 {
00376   switch (opcode) {
00377   case ARM::t2LDRi8:   return ARM::t2LDRi12;
00378   case ARM::t2LDRHi8:  return ARM::t2LDRHi12;
00379   case ARM::t2LDRBi8:  return ARM::t2LDRBi12;
00380   case ARM::t2LDRSHi8: return ARM::t2LDRSHi12;
00381   case ARM::t2LDRSBi8: return ARM::t2LDRSBi12;
00382   case ARM::t2STRi8:   return ARM::t2STRi12;
00383   case ARM::t2STRBi8:  return ARM::t2STRBi12;
00384   case ARM::t2STRHi8:  return ARM::t2STRHi12;
00385   case ARM::t2PLDi8:   return ARM::t2PLDi12;
00386 
00387   case ARM::t2LDRi12:
00388   case ARM::t2LDRHi12:
00389   case ARM::t2LDRBi12:
00390   case ARM::t2LDRSHi12:
00391   case ARM::t2LDRSBi12:
00392   case ARM::t2STRi12:
00393   case ARM::t2STRBi12:
00394   case ARM::t2STRHi12:
00395   case ARM::t2PLDi12:
00396     return opcode;
00397 
00398   default:
00399     break;
00400   }
00401 
00402   return 0;
00403 }
00404 
00405 static unsigned
00406 immediateOffsetOpcode(unsigned opcode)
00407 {
00408   switch (opcode) {
00409   case ARM::t2LDRs:   return ARM::t2LDRi12;
00410   case ARM::t2LDRHs:  return ARM::t2LDRHi12;
00411   case ARM::t2LDRBs:  return ARM::t2LDRBi12;
00412   case ARM::t2LDRSHs: return ARM::t2LDRSHi12;
00413   case ARM::t2LDRSBs: return ARM::t2LDRSBi12;
00414   case ARM::t2STRs:   return ARM::t2STRi12;
00415   case ARM::t2STRBs:  return ARM::t2STRBi12;
00416   case ARM::t2STRHs:  return ARM::t2STRHi12;
00417   case ARM::t2PLDs:   return ARM::t2PLDi12;
00418 
00419   case ARM::t2LDRi12:
00420   case ARM::t2LDRHi12:
00421   case ARM::t2LDRBi12:
00422   case ARM::t2LDRSHi12:
00423   case ARM::t2LDRSBi12:
00424   case ARM::t2STRi12:
00425   case ARM::t2STRBi12:
00426   case ARM::t2STRHi12:
00427   case ARM::t2PLDi12:
00428   case ARM::t2LDRi8:
00429   case ARM::t2LDRHi8:
00430   case ARM::t2LDRBi8:
00431   case ARM::t2LDRSHi8:
00432   case ARM::t2LDRSBi8:
00433   case ARM::t2STRi8:
00434   case ARM::t2STRBi8:
00435   case ARM::t2STRHi8:
00436   case ARM::t2PLDi8:
00437     return opcode;
00438 
00439   default:
00440     break;
00441   }
00442 
00443   return 0;
00444 }
00445 
00446 bool llvm::rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
00447                                unsigned FrameReg, int &Offset,
00448                                const ARMBaseInstrInfo &TII) {
00449   unsigned Opcode = MI.getOpcode();
00450   const MCInstrDesc &Desc = MI.getDesc();
00451   unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
00452   bool isSub = false;
00453 
00454   // Memory operands in inline assembly always use AddrModeT2_i12.
00455   if (Opcode == ARM::INLINEASM)
00456     AddrMode = ARMII::AddrModeT2_i12; // FIXME. mode for thumb2?
00457 
00458   if (Opcode == ARM::t2ADDri || Opcode == ARM::t2ADDri12) {
00459     Offset += MI.getOperand(FrameRegIdx+1).getImm();
00460 
00461     unsigned PredReg;
00462     if (Offset == 0 && getInstrPredicate(&MI, PredReg) == ARMCC::AL) {
00463       // Turn it into a move.
00464       MI.setDesc(TII.get(ARM::tMOVr));
00465       MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
00466       // Remove offset and remaining explicit predicate operands.
00467       do MI.RemoveOperand(FrameRegIdx+1);
00468       while (MI.getNumOperands() > FrameRegIdx+1);
00469       MachineInstrBuilder MIB(*MI.getParent()->getParent(), &MI);
00470       AddDefaultPred(MIB);
00471       return true;
00472     }
00473 
00474     bool HasCCOut = Opcode != ARM::t2ADDri12;
00475 
00476     if (Offset < 0) {
00477       Offset = -Offset;
00478       isSub = true;
00479       MI.setDesc(TII.get(ARM::t2SUBri));
00480     } else {
00481       MI.setDesc(TII.get(ARM::t2ADDri));
00482     }
00483 
00484     // Common case: small offset, fits into instruction.
00485     if (ARM_AM::getT2SOImmVal(Offset) != -1) {
00486       MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
00487       MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
00488       // Add cc_out operand if the original instruction did not have one.
00489       if (!HasCCOut)
00490         MI.addOperand(MachineOperand::CreateReg(0, false));
00491       Offset = 0;
00492       return true;
00493     }
00494     // Another common case: imm12.
00495     if (Offset < 4096 &&
00496         (!HasCCOut || MI.getOperand(MI.getNumOperands()-1).getReg() == 0)) {
00497       unsigned NewOpc = isSub ? ARM::t2SUBri12 : ARM::t2ADDri12;
00498       MI.setDesc(TII.get(NewOpc));
00499       MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
00500       MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
00501       // Remove the cc_out operand.
00502       if (HasCCOut)
00503         MI.RemoveOperand(MI.getNumOperands()-1);
00504       Offset = 0;
00505       return true;
00506     }
00507 
00508     // Otherwise, extract 8 adjacent bits from the immediate into this
00509     // t2ADDri/t2SUBri.
00510     unsigned RotAmt = countLeadingZeros<unsigned>(Offset);
00511     unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xff000000U, RotAmt);
00512 
00513     // We will handle these bits from offset, clear them.
00514     Offset &= ~ThisImmVal;
00515 
00516     assert(ARM_AM::getT2SOImmVal(ThisImmVal) != -1 &&
00517            "Bit extraction didn't work?");
00518     MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
00519     // Add cc_out operand if the original instruction did not have one.
00520     if (!HasCCOut)
00521       MI.addOperand(MachineOperand::CreateReg(0, false));
00522 
00523   } else {
00524 
00525     // AddrMode4 and AddrMode6 cannot handle any offset.
00526     if (AddrMode == ARMII::AddrMode4 || AddrMode == ARMII::AddrMode6)
00527       return false;
00528 
00529     // AddrModeT2_so cannot handle any offset. If there is no offset
00530     // register then we change to an immediate version.
00531     unsigned NewOpc = Opcode;
00532     if (AddrMode == ARMII::AddrModeT2_so) {
00533       unsigned OffsetReg = MI.getOperand(FrameRegIdx+1).getReg();
00534       if (OffsetReg != 0) {
00535         MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
00536         return Offset == 0;
00537       }
00538 
00539       MI.RemoveOperand(FrameRegIdx+1);
00540       MI.getOperand(FrameRegIdx+1).ChangeToImmediate(0);
00541       NewOpc = immediateOffsetOpcode(Opcode);
00542       AddrMode = ARMII::AddrModeT2_i12;
00543     }
00544 
00545     unsigned NumBits = 0;
00546     unsigned Scale = 1;
00547     if (AddrMode == ARMII::AddrModeT2_i8 || AddrMode == ARMII::AddrModeT2_i12) {
00548       // i8 supports only negative, and i12 supports only positive, so
00549       // based on Offset sign convert Opcode to the appropriate
00550       // instruction
00551       Offset += MI.getOperand(FrameRegIdx+1).getImm();
00552       if (Offset < 0) {
00553         NewOpc = negativeOffsetOpcode(Opcode);
00554         NumBits = 8;
00555         isSub = true;
00556         Offset = -Offset;
00557       } else {
00558         NewOpc = positiveOffsetOpcode(Opcode);
00559         NumBits = 12;
00560       }
00561     } else if (AddrMode == ARMII::AddrMode5) {
00562       // VFP address mode.
00563       const MachineOperand &OffOp = MI.getOperand(FrameRegIdx+1);
00564       int InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm());
00565       if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub)
00566         InstrOffs *= -1;
00567       NumBits = 8;
00568       Scale = 4;
00569       Offset += InstrOffs * 4;
00570       assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
00571       if (Offset < 0) {
00572         Offset = -Offset;
00573         isSub = true;
00574       }
00575     } else if (AddrMode == ARMII::AddrModeT2_i8s4) {
00576       Offset += MI.getOperand(FrameRegIdx + 1).getImm() * 4;
00577       NumBits = 8;
00578       // MCInst operand has already scaled value.
00579       Scale = 1;
00580       if (Offset < 0) {
00581         isSub = true;
00582         Offset = -Offset;
00583       }
00584     } else {
00585       llvm_unreachable("Unsupported addressing mode!");
00586     }
00587 
00588     if (NewOpc != Opcode)
00589       MI.setDesc(TII.get(NewOpc));
00590 
00591     MachineOperand &ImmOp = MI.getOperand(FrameRegIdx+1);
00592 
00593     // Attempt to fold address computation
00594     // Common case: small offset, fits into instruction.
00595     int ImmedOffset = Offset / Scale;
00596     unsigned Mask = (1 << NumBits) - 1;
00597     if ((unsigned)Offset <= Mask * Scale) {
00598       // Replace the FrameIndex with fp/sp
00599       MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
00600       if (isSub) {
00601         if (AddrMode == ARMII::AddrMode5)
00602           // FIXME: Not consistent.
00603           ImmedOffset |= 1 << NumBits;
00604         else
00605           ImmedOffset = -ImmedOffset;
00606       }
00607       ImmOp.ChangeToImmediate(ImmedOffset);
00608       Offset = 0;
00609       return true;
00610     }
00611 
00612     // Otherwise, offset doesn't fit. Pull in what we can to simplify
00613     ImmedOffset = ImmedOffset & Mask;
00614     if (isSub) {
00615       if (AddrMode == ARMII::AddrMode5)
00616         // FIXME: Not consistent.
00617         ImmedOffset |= 1 << NumBits;
00618       else {
00619         ImmedOffset = -ImmedOffset;
00620         if (ImmedOffset == 0)
00621           // Change the opcode back if the encoded offset is zero.
00622           MI.setDesc(TII.get(positiveOffsetOpcode(NewOpc)));
00623       }
00624     }
00625     ImmOp.ChangeToImmediate(ImmedOffset);
00626     Offset &= ~(Mask*Scale);
00627   }
00628 
00629   Offset = (isSub) ? -Offset : Offset;
00630   return Offset == 0;
00631 }
00632 
00633 ARMCC::CondCodes
00634 llvm::getITInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
00635   unsigned Opc = MI->getOpcode();
00636   if (Opc == ARM::tBcc || Opc == ARM::t2Bcc)
00637     return ARMCC::AL;
00638   return getInstrPredicate(MI, PredReg);
00639 }