LLVM API Documentation

ARMBaseInstrInfo.cpp

Go to the documentation of this file.
00001 //===- ARMBaseInstrInfo.cpp - ARM Instruction Information -----------*- C++ -*-===//
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 Base ARM implementation of the TargetInstrInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "ARMBaseInstrInfo.h"
00015 #include "ARM.h"
00016 #include "ARMAddressingModes.h"
00017 #include "ARMGenInstrInfo.inc"
00018 #include "ARMMachineFunctionInfo.h"
00019 #include "llvm/ADT/STLExtras.h"
00020 #include "llvm/CodeGen/LiveVariables.h"
00021 #include "llvm/CodeGen/MachineFrameInfo.h"
00022 #include "llvm/CodeGen/MachineInstrBuilder.h"
00023 #include "llvm/CodeGen/MachineJumpTableInfo.h"
00024 #include "llvm/Target/TargetAsmInfo.h"
00025 #include "llvm/Support/CommandLine.h"
00026 #include "llvm/Support/ErrorHandling.h"
00027 using namespace llvm;
00028 
00029 static cl::opt<bool>
00030 EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
00031                cl::desc("Enable ARM 2-addr to 3-addr conv"));
00032 
00033 ARMBaseInstrInfo::ARMBaseInstrInfo()
00034   : TargetInstrInfoImpl(ARMInsts, array_lengthof(ARMInsts)) {
00035 }
00036 
00037 MachineInstr *
00038 ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
00039                                         MachineBasicBlock::iterator &MBBI,
00040                                         LiveVariables *LV) const {
00041   // FIXME: Thumb2 support.
00042 
00043   if (!EnableARM3Addr)
00044     return NULL;
00045 
00046   MachineInstr *MI = MBBI;
00047   MachineFunction &MF = *MI->getParent()->getParent();
00048   unsigned TSFlags = MI->getDesc().TSFlags;
00049   bool isPre = false;
00050   switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
00051   default: return NULL;
00052   case ARMII::IndexModePre:
00053     isPre = true;
00054     break;
00055   case ARMII::IndexModePost:
00056     break;
00057   }
00058 
00059   // Try splitting an indexed load/store to an un-indexed one plus an add/sub
00060   // operation.
00061   unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
00062   if (MemOpc == 0)
00063     return NULL;
00064 
00065   MachineInstr *UpdateMI = NULL;
00066   MachineInstr *MemMI = NULL;
00067   unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
00068   const TargetInstrDesc &TID = MI->getDesc();
00069   unsigned NumOps = TID.getNumOperands();
00070   bool isLoad = !TID.mayStore();
00071   const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
00072   const MachineOperand &Base = MI->getOperand(2);
00073   const MachineOperand &Offset = MI->getOperand(NumOps-3);
00074   unsigned WBReg = WB.getReg();
00075   unsigned BaseReg = Base.getReg();
00076   unsigned OffReg = Offset.getReg();
00077   unsigned OffImm = MI->getOperand(NumOps-2).getImm();
00078   ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
00079   switch (AddrMode) {
00080   default:
00081     assert(false && "Unknown indexed op!");
00082     return NULL;
00083   case ARMII::AddrMode2: {
00084     bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
00085     unsigned Amt = ARM_AM::getAM2Offset(OffImm);
00086     if (OffReg == 0) {
00087       if (ARM_AM::getSOImmVal(Amt) == -1)
00088         // Can't encode it in a so_imm operand. This transformation will
00089         // add more than 1 instruction. Abandon!
00090         return NULL;
00091       UpdateMI = BuildMI(MF, MI->getDebugLoc(),
00092                          get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
00093         .addReg(BaseReg).addImm(Amt)
00094         .addImm(Pred).addReg(0).addReg(0);
00095     } else if (Amt != 0) {
00096       ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
00097       unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
00098       UpdateMI = BuildMI(MF, MI->getDebugLoc(),
00099                          get(isSub ? ARM::SUBrs : ARM::ADDrs), WBReg)
00100         .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
00101         .addImm(Pred).addReg(0).addReg(0);
00102     } else
00103       UpdateMI = BuildMI(MF, MI->getDebugLoc(),
00104                          get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
00105         .addReg(BaseReg).addReg(OffReg)
00106         .addImm(Pred).addReg(0).addReg(0);
00107     break;
00108   }
00109   case ARMII::AddrMode3 : {
00110     bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
00111     unsigned Amt = ARM_AM::getAM3Offset(OffImm);
00112     if (OffReg == 0)
00113       // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
00114       UpdateMI = BuildMI(MF, MI->getDebugLoc(),
00115                          get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
00116         .addReg(BaseReg).addImm(Amt)
00117         .addImm(Pred).addReg(0).addReg(0);
00118     else
00119       UpdateMI = BuildMI(MF, MI->getDebugLoc(),
00120                          get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
00121         .addReg(BaseReg).addReg(OffReg)
00122         .addImm(Pred).addReg(0).addReg(0);
00123     break;
00124   }
00125   }
00126 
00127   std::vector<MachineInstr*> NewMIs;
00128   if (isPre) {
00129     if (isLoad)
00130       MemMI = BuildMI(MF, MI->getDebugLoc(),
00131                       get(MemOpc), MI->getOperand(0).getReg())
00132         .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
00133     else
00134       MemMI = BuildMI(MF, MI->getDebugLoc(),
00135                       get(MemOpc)).addReg(MI->getOperand(1).getReg())
00136         .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
00137     NewMIs.push_back(MemMI);
00138     NewMIs.push_back(UpdateMI);
00139   } else {
00140     if (isLoad)
00141       MemMI = BuildMI(MF, MI->getDebugLoc(),
00142                       get(MemOpc), MI->getOperand(0).getReg())
00143         .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
00144     else
00145       MemMI = BuildMI(MF, MI->getDebugLoc(),
00146                       get(MemOpc)).addReg(MI->getOperand(1).getReg())
00147         .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
00148     if (WB.isDead())
00149       UpdateMI->getOperand(0).setIsDead();
00150     NewMIs.push_back(UpdateMI);
00151     NewMIs.push_back(MemMI);
00152   }
00153 
00154   // Transfer LiveVariables states, kill / dead info.
00155   if (LV) {
00156     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00157       MachineOperand &MO = MI->getOperand(i);
00158       if (MO.isReg() && MO.getReg() &&
00159           TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
00160         unsigned Reg = MO.getReg();
00161 
00162         LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
00163         if (MO.isDef()) {
00164           MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
00165           if (MO.isDead())
00166             LV->addVirtualRegisterDead(Reg, NewMI);
00167         }
00168         if (MO.isUse() && MO.isKill()) {
00169           for (unsigned j = 0; j < 2; ++j) {
00170             // Look at the two new MI's in reverse order.
00171             MachineInstr *NewMI = NewMIs[j];
00172             if (!NewMI->readsRegister(Reg))
00173               continue;
00174             LV->addVirtualRegisterKilled(Reg, NewMI);
00175             if (VI.removeKill(MI))
00176               VI.Kills.push_back(NewMI);
00177             break;
00178           }
00179         }
00180       }
00181     }
00182   }
00183 
00184   MFI->insert(MBBI, NewMIs[1]);
00185   MFI->insert(MBBI, NewMIs[0]);
00186   return NewMIs[0];
00187 }
00188 
00189 // Branch analysis.
00190 bool
00191 ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
00192                                 MachineBasicBlock *&FBB,
00193                                 SmallVectorImpl<MachineOperand> &Cond,
00194                                 bool AllowModify) const {
00195   // If the block has no terminators, it just falls into the block after it.
00196   MachineBasicBlock::iterator I = MBB.end();
00197   if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
00198     return false;
00199 
00200   // Get the last instruction in the block.
00201   MachineInstr *LastInst = I;
00202 
00203   // If there is only one terminator instruction, process it.
00204   unsigned LastOpc = LastInst->getOpcode();
00205   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
00206     if (isUncondBranchOpcode(LastOpc)) {
00207       TBB = LastInst->getOperand(0).getMBB();
00208       return false;
00209     }
00210     if (isCondBranchOpcode(LastOpc)) {
00211       // Block ends with fall-through condbranch.
00212       TBB = LastInst->getOperand(0).getMBB();
00213       Cond.push_back(LastInst->getOperand(1));
00214       Cond.push_back(LastInst->getOperand(2));
00215       return false;
00216     }
00217     return true;  // Can't handle indirect branch.
00218   }
00219 
00220   // Get the instruction before it if it is a terminator.
00221   MachineInstr *SecondLastInst = I;
00222 
00223   // If there are three terminators, we don't know what sort of block this is.
00224   if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
00225     return true;
00226 
00227   // If the block ends with a B and a Bcc, handle it.
00228   unsigned SecondLastOpc = SecondLastInst->getOpcode();
00229   if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
00230     TBB =  SecondLastInst->getOperand(0).getMBB();
00231     Cond.push_back(SecondLastInst->getOperand(1));
00232     Cond.push_back(SecondLastInst->getOperand(2));
00233     FBB = LastInst->getOperand(0).getMBB();
00234     return false;
00235   }
00236 
00237   // If the block ends with two unconditional branches, handle it.  The second
00238   // one is not executed, so remove it.
00239   if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
00240     TBB = SecondLastInst->getOperand(0).getMBB();
00241     I = LastInst;
00242     if (AllowModify)
00243       I->eraseFromParent();
00244     return false;
00245   }
00246 
00247   // ...likewise if it ends with a branch table followed by an unconditional
00248   // branch. The branch folder can create these, and we must get rid of them for
00249   // correctness of Thumb constant islands.
00250   if (isJumpTableBranchOpcode(SecondLastOpc) &&
00251       isUncondBranchOpcode(LastOpc)) {
00252     I = LastInst;
00253     if (AllowModify)
00254       I->eraseFromParent();
00255     return true;
00256   }
00257 
00258   // Otherwise, can't handle this.
00259   return true;
00260 }
00261 
00262 
00263 unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
00264   MachineBasicBlock::iterator I = MBB.end();
00265   if (I == MBB.begin()) return 0;
00266   --I;
00267   if (!isUncondBranchOpcode(I->getOpcode()) &&
00268       !isCondBranchOpcode(I->getOpcode()))
00269     return 0;
00270 
00271   // Remove the branch.
00272   I->eraseFromParent();
00273 
00274   I = MBB.end();
00275 
00276   if (I == MBB.begin()) return 1;
00277   --I;
00278   if (!isCondBranchOpcode(I->getOpcode()))
00279     return 1;
00280 
00281   // Remove the branch.
00282   I->eraseFromParent();
00283   return 2;
00284 }
00285 
00286 unsigned
00287 ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
00288                                MachineBasicBlock *FBB,
00289                              const SmallVectorImpl<MachineOperand> &Cond) const {
00290   // FIXME this should probably have a DebugLoc argument
00291   DebugLoc dl = DebugLoc::getUnknownLoc();
00292 
00293   ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
00294   int BOpc   = !AFI->isThumbFunction()
00295     ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
00296   int BccOpc = !AFI->isThumbFunction()
00297     ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
00298 
00299   // Shouldn't be a fall through.
00300   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
00301   assert((Cond.size() == 2 || Cond.size() == 0) &&
00302          "ARM branch conditions have two components!");
00303 
00304   if (FBB == 0) {
00305     if (Cond.empty()) // Unconditional branch?
00306       BuildMI(&MBB, dl, get(BOpc)).addMBB(TBB);
00307     else
00308       BuildMI(&MBB, dl, get(BccOpc)).addMBB(TBB)
00309         .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
00310     return 1;
00311   }
00312 
00313   // Two-way conditional branch.
00314   BuildMI(&MBB, dl, get(BccOpc)).addMBB(TBB)
00315     .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
00316   BuildMI(&MBB, dl, get(BOpc)).addMBB(FBB);
00317   return 2;
00318 }
00319 
00320 bool ARMBaseInstrInfo::
00321 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
00322   ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
00323   Cond[0].setImm(ARMCC::getOppositeCondition(CC));
00324   return false;
00325 }
00326 
00327 bool ARMBaseInstrInfo::
00328 PredicateInstruction(MachineInstr *MI,
00329                      const SmallVectorImpl<MachineOperand> &Pred) const {
00330   unsigned Opc = MI->getOpcode();
00331   if (isUncondBranchOpcode(Opc)) {
00332     MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
00333     MI->addOperand(MachineOperand::CreateImm(Pred[0].getImm()));
00334     MI->addOperand(MachineOperand::CreateReg(Pred[1].getReg(), false));
00335     return true;
00336   }
00337 
00338   int PIdx = MI->findFirstPredOperandIdx();
00339   if (PIdx != -1) {
00340     MachineOperand &PMO = MI->getOperand(PIdx);
00341     PMO.setImm(Pred[0].getImm());
00342     MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
00343     return true;
00344   }
00345   return false;
00346 }
00347 
00348 bool ARMBaseInstrInfo::
00349 SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
00350                   const SmallVectorImpl<MachineOperand> &Pred2) const {
00351   if (Pred1.size() > 2 || Pred2.size() > 2)
00352     return false;
00353 
00354   ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
00355   ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
00356   if (CC1 == CC2)
00357     return true;
00358 
00359   switch (CC1) {
00360   default:
00361     return false;
00362   case ARMCC::AL:
00363     return true;
00364   case ARMCC::HS:
00365     return CC2 == ARMCC::HI;
00366   case ARMCC::LS:
00367     return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
00368   case ARMCC::GE:
00369     return CC2 == ARMCC::GT;
00370   case ARMCC::LE:
00371     return CC2 == ARMCC::LT;
00372   }
00373 }
00374 
00375 bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
00376                                     std::vector<MachineOperand> &Pred) const {
00377   // FIXME: This confuses implicit_def with optional CPSR def.
00378   const TargetInstrDesc &TID = MI->getDesc();
00379   if (!TID.getImplicitDefs() && !TID.hasOptionalDef())
00380     return false;
00381 
00382   bool Found = false;
00383   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00384     const MachineOperand &MO = MI->getOperand(i);
00385     if (MO.isReg() && MO.getReg() == ARM::CPSR) {
00386       Pred.push_back(MO);
00387       Found = true;
00388     }
00389   }
00390 
00391   return Found;
00392 }
00393 
00394 
00395 /// FIXME: Works around a gcc miscompilation with -fstrict-aliasing
00396 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
00397                                 unsigned JTI) DISABLE_INLINE;
00398 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
00399                                 unsigned JTI) {
00400   return JT[JTI].MBBs.size();
00401 }
00402 
00403 /// GetInstSize - Return the size of the specified MachineInstr.
00404 ///
00405 unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
00406   const MachineBasicBlock &MBB = *MI->getParent();
00407   const MachineFunction *MF = MBB.getParent();
00408   const TargetAsmInfo *TAI = MF->getTarget().getTargetAsmInfo();
00409 
00410   // Basic size info comes from the TSFlags field.
00411   const TargetInstrDesc &TID = MI->getDesc();
00412   unsigned TSFlags = TID.TSFlags;
00413 
00414   unsigned Opc = MI->getOpcode();
00415   switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
00416   default: {
00417     // If this machine instr is an inline asm, measure it.
00418     if (MI->getOpcode() == ARM::INLINEASM)
00419       return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *TAI);
00420     if (MI->isLabel())
00421       return 0;
00422     switch (Opc) {
00423     default:
00424       llvm_unreachable("Unknown or unset size field for instr!");
00425     case TargetInstrInfo::IMPLICIT_DEF:
00426     case TargetInstrInfo::DECLARE:
00427     case TargetInstrInfo::DBG_LABEL:
00428     case TargetInstrInfo::EH_LABEL:
00429       return 0;
00430     }
00431     break;
00432   }
00433   case ARMII::Size8Bytes: return 8;          // ARM instruction x 2.
00434   case ARMII::Size4Bytes: return 4;          // ARM / Thumb2 instruction.
00435   case ARMII::Size2Bytes: return 2;          // Thumb1 instruction.
00436   case ARMII::SizeSpecial: {
00437     switch (Opc) {
00438     case ARM::CONSTPOOL_ENTRY:
00439       // If this machine instr is a constant pool entry, its size is recorded as
00440       // operand #2.
00441       return MI->getOperand(2).getImm();
00442     case ARM::Int_eh_sjlj_setjmp:
00443       return 24;
00444     case ARM::t2Int_eh_sjlj_setjmp:
00445       return 20;
00446     case ARM::BR_JTr:
00447     case ARM::BR_JTm:
00448     case ARM::BR_JTadd:
00449     case ARM::tBR_JTr:
00450     case ARM::t2BR_JT:
00451     case ARM::t2TBB:
00452     case ARM::t2TBH: {
00453       // These are jumptable branches, i.e. a branch followed by an inlined
00454       // jumptable. The size is 4 + 4 * number of entries. For TBB, each
00455       // entry is one byte; TBH two byte each.
00456       unsigned EntrySize = (Opc == ARM::t2TBB)
00457         ? 1 : ((Opc == ARM::t2TBH) ? 2 : 4);
00458       unsigned NumOps = TID.getNumOperands();
00459       MachineOperand JTOP =
00460         MI->getOperand(NumOps - (TID.isPredicable() ? 3 : 2));
00461       unsigned JTI = JTOP.getIndex();
00462       const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
00463       const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
00464       assert(JTI < JT.size());
00465       // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
00466       // 4 aligned. The assembler / linker may add 2 byte padding just before
00467       // the JT entries.  The size does not include this padding; the
00468       // constant islands pass does separate bookkeeping for it.
00469       // FIXME: If we know the size of the function is less than (1 << 16) *2
00470       // bytes, we can use 16-bit entries instead. Then there won't be an
00471       // alignment issue.
00472       unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
00473       unsigned NumEntries = getNumJTEntries(JT, JTI);
00474       if (Opc == ARM::t2TBB && (NumEntries & 1))
00475         // Make sure the instruction that follows TBB is 2-byte aligned.
00476         // FIXME: Constant island pass should insert an "ALIGN" instruction
00477         // instead.
00478         ++NumEntries;
00479       return NumEntries * EntrySize + InstSize;
00480     }
00481     default:
00482       // Otherwise, pseudo-instruction sizes are zero.
00483       return 0;
00484     }
00485   }
00486   }
00487   return 0; // Not reached
00488 }
00489 
00490 /// Return true if the instruction is a register to register move and
00491 /// leave the source and dest operands in the passed parameters.
00492 ///
00493 bool
00494 ARMBaseInstrInfo::isMoveInstr(const MachineInstr &MI,
00495                               unsigned &SrcReg, unsigned &DstReg,
00496                               unsigned& SrcSubIdx, unsigned& DstSubIdx) const {
00497   SrcSubIdx = DstSubIdx = 0; // No sub-registers.
00498 
00499   switch (MI.getOpcode()) {
00500   default: break;
00501   case ARM::FCPYS:
00502   case ARM::FCPYD:
00503   case ARM::VMOVD:
00504   case  ARM::VMOVQ: {
00505     SrcReg = MI.getOperand(1).getReg();
00506     DstReg = MI.getOperand(0).getReg();
00507     return true;
00508   }
00509   case ARM::MOVr:
00510   case ARM::tMOVr:
00511   case ARM::tMOVgpr2tgpr:
00512   case ARM::tMOVtgpr2gpr:
00513   case ARM::tMOVgpr2gpr:
00514   case ARM::t2MOVr: {
00515     assert(MI.getDesc().getNumOperands() >= 2 &&
00516            MI.getOperand(0).isReg() &&
00517            MI.getOperand(1).isReg() &&
00518            "Invalid ARM MOV instruction");
00519     SrcReg = MI.getOperand(1).getReg();
00520     DstReg = MI.getOperand(0).getReg();
00521     return true;
00522   }
00523   }
00524 
00525   return false;
00526 }
00527 
00528 unsigned
00529 ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
00530                                       int &FrameIndex) const {
00531   switch (MI->getOpcode()) {
00532   default: break;
00533   case ARM::LDR:
00534   case ARM::t2LDRs:  // FIXME: don't use t2LDRs to access frame.
00535     if (MI->getOperand(1).isFI() &&
00536         MI->getOperand(2).isReg() &&
00537         MI->getOperand(3).isImm() &&
00538         MI->getOperand(2).getReg() == 0 &&
00539         MI->getOperand(3).getImm() == 0) {
00540       FrameIndex = MI->getOperand(1).getIndex();
00541       return MI->getOperand(0).getReg();
00542     }
00543     break;
00544   case ARM::t2LDRi12:
00545   case ARM::tRestore:
00546     if (MI->getOperand(1).isFI() &&
00547         MI->getOperand(2).isImm() &&
00548         MI->getOperand(2).getImm() == 0) {
00549       FrameIndex = MI->getOperand(1).getIndex();
00550       return MI->getOperand(0).getReg();
00551     }
00552     break;
00553   case ARM::FLDD:
00554   case ARM::FLDS:
00555     if (MI->getOperand(1).isFI() &&
00556         MI->getOperand(2).isImm() &&
00557         MI->getOperand(2).getImm() == 0) {
00558       FrameIndex = MI->getOperand(1).getIndex();
00559       return MI->getOperand(0).getReg();
00560     }
00561     break;
00562   }
00563 
00564   return 0;
00565 }
00566 
00567 unsigned
00568 ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
00569                                      int &FrameIndex) const {
00570   switch (MI->getOpcode()) {
00571   default: break;
00572   case ARM::STR:
00573   case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
00574     if (MI->getOperand(1).isFI() &&
00575         MI->getOperand(2).isReg() &&
00576         MI->getOperand(3).isImm() &&
00577         MI->getOperand(2).getReg() == 0 &&
00578         MI->getOperand(3).getImm() == 0) {
00579       FrameIndex = MI->getOperand(1).getIndex();
00580       return MI->getOperand(0).getReg();
00581     }
00582     break;
00583   case ARM::t2STRi12:
00584   case ARM::tSpill:
00585     if (MI->getOperand(1).isFI() &&
00586         MI->getOperand(2).isImm() &&
00587         MI->getOperand(2).getImm() == 0) {
00588       FrameIndex = MI->getOperand(1).getIndex();
00589       return MI->getOperand(0).getReg();
00590     }
00591     break;
00592   case ARM::FSTD:
00593   case ARM::FSTS:
00594     if (MI->getOperand(1).isFI() &&
00595         MI->getOperand(2).isImm() &&
00596         MI->getOperand(2).getImm() == 0) {
00597       FrameIndex = MI->getOperand(1).getIndex();
00598       return MI->getOperand(0).getReg();
00599     }
00600     break;
00601   }
00602 
00603   return 0;
00604 }
00605 
00606 bool
00607 ARMBaseInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
00608                                MachineBasicBlock::iterator I,
00609                                unsigned DestReg, unsigned SrcReg,
00610                                const TargetRegisterClass *DestRC,
00611                                const TargetRegisterClass *SrcRC) const {
00612   DebugLoc DL = DebugLoc::getUnknownLoc();
00613   if (I != MBB.end()) DL = I->getDebugLoc();
00614 
00615   if (DestRC != SrcRC) {
00616     if (((DestRC == ARM::DPRRegisterClass) &&
00617          (SrcRC == ARM::DPR_VFP2RegisterClass)) ||
00618         ((SrcRC == ARM::DPRRegisterClass) &&
00619          (DestRC == ARM::DPR_VFP2RegisterClass))) {
00620       // Allow copy between DPR and DPR_VFP2.
00621     } else {
00622       return false;
00623     }
00624   }
00625 
00626   if (DestRC == ARM::GPRRegisterClass) {
00627     AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr),
00628                                         DestReg).addReg(SrcReg)));
00629   } else if (DestRC == ARM::SPRRegisterClass) {
00630     AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::FCPYS), DestReg)
00631                    .addReg(SrcReg));
00632   } else if ((DestRC == ARM::DPRRegisterClass) ||
00633              (DestRC == ARM::DPR_VFP2RegisterClass)) {
00634     AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::FCPYD), DestReg)
00635                    .addReg(SrcReg));
00636   } else if (DestRC == ARM::QPRRegisterClass) {
00637     BuildMI(MBB, I, DL, get(ARM::VMOVQ), DestReg).addReg(SrcReg);
00638   } else {
00639     return false;
00640   }
00641 
00642   return true;
00643 }
00644 
00645 void ARMBaseInstrInfo::
00646 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
00647                     unsigned SrcReg, bool isKill, int FI,
00648                     const TargetRegisterClass *RC) const {
00649   DebugLoc DL = DebugLoc::getUnknownLoc();
00650   if (I != MBB.end()) DL = I->getDebugLoc();
00651 
00652   if (RC == ARM::GPRRegisterClass) {
00653     AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STR))
00654                    .addReg(SrcReg, getKillRegState(isKill))
00655                    .addFrameIndex(FI).addReg(0).addImm(0));
00656   } else if (RC == ARM::DPRRegisterClass || RC == ARM::DPR_VFP2RegisterClass) {
00657     AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::FSTD))
00658                    .addReg(SrcReg, getKillRegState(isKill))
00659                    .addFrameIndex(FI).addImm(0));
00660   } else if (RC == ARM::SPRRegisterClass) {
00661     AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::FSTS))
00662                    .addReg(SrcReg, getKillRegState(isKill))
00663                    .addFrameIndex(FI).addImm(0));
00664   } else {
00665     assert(RC == ARM::QPRRegisterClass && "Unknown regclass!");
00666     // FIXME: Neon instructions should support predicates
00667     BuildMI(MBB, I, DL, get(ARM::VSTRQ)).addReg(SrcReg, getKillRegState(isKill))
00668       .addFrameIndex(FI).addImm(0);
00669   }
00670 }
00671 
00672 void ARMBaseInstrInfo::
00673 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
00674                      unsigned DestReg, int FI,
00675                      const TargetRegisterClass *RC) const {
00676   DebugLoc DL = DebugLoc::getUnknownLoc();
00677   if (I != MBB.end()) DL = I->getDebugLoc();
00678 
00679   if (RC == ARM::GPRRegisterClass) {
00680     AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDR), DestReg)
00681                    .addFrameIndex(FI).addReg(0).addImm(0));
00682   } else if (RC == ARM::DPRRegisterClass || RC == ARM::DPR_VFP2RegisterClass) {
00683     AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::FLDD), DestReg)
00684                    .addFrameIndex(FI).addImm(0));
00685   } else if (RC == ARM::SPRRegisterClass) {
00686     AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::FLDS), DestReg)
00687                    .addFrameIndex(FI).addImm(0));
00688   } else {
00689     assert(RC == ARM::QPRRegisterClass && "Unknown regclass!");
00690     // FIXME: Neon instructions should support predicates
00691     BuildMI(MBB, I, DL, get(ARM::VLDRQ), DestReg).addFrameIndex(FI).addImm(0);
00692   }
00693 }
00694 
00695 MachineInstr *ARMBaseInstrInfo::
00696 foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
00697                       const SmallVectorImpl<unsigned> &Ops, int FI) const {
00698   if (Ops.size() != 1) return NULL;
00699 
00700   unsigned OpNum = Ops[0];
00701   unsigned Opc = MI->getOpcode();
00702   MachineInstr *NewMI = NULL;
00703   if (Opc == ARM::MOVr || Opc == ARM::t2MOVr) {
00704     // If it is updating CPSR, then it cannot be folded.
00705     if (MI->getOperand(4).getReg() == ARM::CPSR && !MI->getOperand(4).isDead())
00706       return NULL;
00707     unsigned Pred = MI->getOperand(2).getImm();
00708     unsigned PredReg = MI->getOperand(3).getReg();
00709     if (OpNum == 0) { // move -> store
00710       unsigned SrcReg = MI->getOperand(1).getReg();
00711       bool isKill = MI->getOperand(1).isKill();
00712       bool isUndef = MI->getOperand(1).isUndef();
00713       if (Opc == ARM::MOVr)
00714         NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::STR))
00715           .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef))
00716           .addFrameIndex(FI).addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
00717       else // ARM::t2MOVr
00718         NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2STRi12))
00719           .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef))
00720           .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
00721     } else {          // move -> load
00722       unsigned DstReg = MI->getOperand(0).getReg();
00723       bool isDead = MI->getOperand(0).isDead();
00724       bool isUndef = MI->getOperand(0).isUndef();
00725       if (Opc == ARM::MOVr)
00726         NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::LDR))
00727           .addReg(DstReg,
00728                   RegState::Define |
00729                   getDeadRegState(isDead) |
00730                   getUndefRegState(isUndef))
00731           .addFrameIndex(FI).addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
00732       else // ARM::t2MOVr
00733         NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2LDRi12))
00734           .addReg(DstReg,
00735                   RegState::Define |
00736                   getDeadRegState(isDead) |
00737                   getUndefRegState(isUndef))
00738           .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
00739     }
00740   } else if (Opc == ARM::tMOVgpr2gpr ||
00741              Opc == ARM::tMOVtgpr2gpr ||
00742              Opc == ARM::tMOVgpr2tgpr) {
00743     if (OpNum == 0) { // move -> store
00744       unsigned SrcReg = MI->getOperand(1).getReg();
00745       bool isKill = MI->getOperand(1).isKill();
00746       bool isUndef = MI->getOperand(1).isUndef();
00747       NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2STRi12))
00748         .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef))
00749         .addFrameIndex(FI).addImm(0).addImm(ARMCC::AL).addReg(0);
00750     } else {          // move -> load
00751       unsigned DstReg = MI->getOperand(0).getReg();
00752       bool isDead = MI->getOperand(0).isDead();
00753       bool isUndef = MI->getOperand(0).isUndef();
00754       NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2LDRi12))
00755         .addReg(DstReg,
00756                 RegState::Define |
00757                 getDeadRegState(isDead) |
00758                 getUndefRegState(isUndef))
00759         .addFrameIndex(FI).addImm(0).addImm(ARMCC::AL).addReg(0);
00760     }
00761   } else if (Opc == ARM::FCPYS) {
00762     unsigned Pred = MI->getOperand(2).getImm();
00763     unsigned PredReg = MI->getOperand(3).getReg();
00764     if (OpNum == 0) { // move -> store
00765       unsigned SrcReg = MI->getOperand(1).getReg();
00766       bool isKill = MI->getOperand(1).isKill();
00767       bool isUndef = MI->getOperand(1).isUndef();
00768       NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::FSTS))
00769         .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef))
00770         .addFrameIndex(FI)
00771         .addImm(0).addImm(Pred).addReg(PredReg);
00772     } else {          // move -> load
00773       unsigned DstReg = MI->getOperand(0).getReg();
00774       bool isDead = MI->getOperand(0).isDead();
00775       bool isUndef = MI->getOperand(0).isUndef();
00776       NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::FLDS))
00777         .addReg(DstReg,
00778                 RegState::Define |
00779                 getDeadRegState(isDead) |
00780                 getUndefRegState(isUndef))
00781         .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
00782     }
00783   }
00784   else if (Opc == ARM::FCPYD) {
00785     unsigned Pred = MI->getOperand(2).getImm();
00786     unsigned PredReg = MI->getOperand(3).getReg();
00787     if (OpNum == 0) { // move -> store
00788       unsigned SrcReg = MI->getOperand(1).getReg();
00789       bool isKill = MI->getOperand(1).isKill();
00790       bool isUndef = MI->getOperand(1).isUndef();
00791       NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::FSTD))
00792         .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef))
00793         .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
00794     } else {          // move -> load
00795       unsigned DstReg = MI->getOperand(0).getReg();
00796       bool isDead = MI->getOperand(0).isDead();
00797       bool isUndef = MI->getOperand(0).isUndef();
00798       NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::FLDD))
00799         .addReg(DstReg,
00800                 RegState::Define |
00801                 getDeadRegState(isDead) |
00802                 getUndefRegState(isUndef))
00803         .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
00804     }
00805   }
00806 
00807   return NewMI;
00808 }
00809 
00810 MachineInstr*
00811 ARMBaseInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
00812                                         MachineInstr* MI,
00813                                         const SmallVectorImpl<unsigned> &Ops,
00814                                         MachineInstr* LoadMI) const {
00815   // FIXME
00816   return 0;
00817 }
00818 
00819 bool
00820 ARMBaseInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
00821                                    const SmallVectorImpl<unsigned> &Ops) const {
00822   if (Ops.size() != 1) return false;
00823 
00824   unsigned Opc = MI->getOpcode();
00825   if (Opc == ARM::MOVr || Opc == ARM::t2MOVr) {
00826     // If it is updating CPSR, then it cannot be folded.
00827     return MI->getOperand(4).getReg() != ARM::CPSR ||
00828       MI->getOperand(4).isDead();
00829   } else if (Opc == ARM::tMOVgpr2gpr ||
00830              Opc == ARM::tMOVtgpr2gpr ||
00831              Opc == ARM::tMOVgpr2tgpr) {
00832     return true;
00833   } else if (Opc == ARM::FCPYS || Opc == ARM::FCPYD) {
00834     return true;
00835   } else if (Opc == ARM::VMOVD || Opc == ARM::VMOVQ) {
00836     return false; // FIXME
00837   }
00838 
00839   return false;
00840 }
00841 
00842 /// getInstrPredicate - If instruction is predicated, returns its predicate
00843 /// condition, otherwise returns AL. It also returns the condition code
00844 /// register by reference.
00845 ARMCC::CondCodes llvm::getInstrPredicate(MachineInstr *MI, unsigned &PredReg) {
00846   int PIdx = MI->findFirstPredOperandIdx();
00847   if (PIdx == -1) {
00848     PredReg = 0;
00849     return ARMCC::AL;
00850   }
00851 
00852   PredReg = MI->getOperand(PIdx+1).getReg();
00853   return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
00854 }
00855 
00856 
00857 int llvm::getMatchingCondBranchOpcode(int Opc) {
00858   if (Opc == ARM::B)
00859     return ARM::Bcc;
00860   else if (Opc == ARM::tB)
00861     return ARM::tBcc;
00862   else if (Opc == ARM::t2B)
00863       return ARM::t2Bcc;
00864 
00865   llvm_unreachable("Unknown unconditional branch opcode!");
00866   return 0;
00867 }
00868 
00869 
00870 void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
00871                                MachineBasicBlock::iterator &MBBI, DebugLoc dl,
00872                                unsigned DestReg, unsigned BaseReg, int NumBytes,
00873                                ARMCC::CondCodes Pred, unsigned PredReg,
00874                                const ARMBaseInstrInfo &TII) {
00875   bool isSub = NumBytes < 0;
00876   if (isSub) NumBytes = -NumBytes;
00877 
00878   while (NumBytes) {
00879     unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
00880     unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
00881     assert(ThisVal && "Didn't extract field correctly");
00882 
00883     // We will handle these bits from offset, clear them.
00884     NumBytes &= ~ThisVal;
00885 
00886     assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
00887 
00888     // Build the new ADD / SUB.
00889     unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
00890     BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
00891       .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
00892       .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
00893     BaseReg = DestReg;
00894   }
00895 }
00896 
00897 int llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
00898                                unsigned FrameReg, int Offset,
00899                                const ARMBaseInstrInfo &TII) {
00900   unsigned Opcode = MI.getOpcode();
00901   const TargetInstrDesc &Desc = MI.getDesc();
00902   unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
00903   bool isSub = false;
00904 
00905   // Memory operands in inline assembly always use AddrMode2.
00906   if (Opcode == ARM::INLINEASM)
00907     AddrMode = ARMII::AddrMode2;
00908 
00909   if (Opcode == ARM::ADDri) {
00910     Offset += MI.getOperand(FrameRegIdx+1).getImm();
00911     if (Offset == 0) {
00912       // Turn it into a move.
00913       MI.setDesc(TII.get(ARM::MOVr));
00914       MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
00915       MI.RemoveOperand(FrameRegIdx+1);
00916       return 0;
00917     } else if (Offset < 0) {
00918       Offset = -Offset;
00919       isSub = true;
00920       MI.setDesc(TII.get(ARM::SUBri));
00921     }
00922 
00923     // Common case: small offset, fits into instruction.
00924     if (ARM_AM::getSOImmVal(Offset) != -1) {
00925       // Replace the FrameIndex with sp / fp
00926       MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
00927       MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
00928       return 0;
00929     }
00930 
00931     // Otherwise, pull as much of the immedidate into this ADDri/SUBri
00932     // as possible.
00933     unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
00934     unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
00935 
00936     // We will handle these bits from offset, clear them.
00937     Offset &= ~ThisImmVal;
00938 
00939     // Get the properly encoded SOImmVal field.
00940     assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
00941            "Bit extraction didn't work?");
00942     MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
00943  } else {
00944     unsigned ImmIdx = 0;
00945     int InstrOffs = 0;
00946     unsigned NumBits = 0;
00947     unsigned Scale = 1;
00948     switch (AddrMode) {
00949     case ARMII::AddrMode2: {
00950       ImmIdx = FrameRegIdx+2;
00951       InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
00952       if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
00953         InstrOffs *= -1;
00954       NumBits = 12;
00955       break;
00956     }
00957     case ARMII::AddrMode3: {
00958       ImmIdx = FrameRegIdx+2;
00959       InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
00960       if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
00961         InstrOffs *= -1;
00962       NumBits = 8;
00963       break;
00964     }
00965     case ARMII::AddrMode4:
00966      break;
00967     case ARMII::AddrMode5: {
00968       ImmIdx = FrameRegIdx+1;
00969       InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
00970       if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
00971         InstrOffs *= -1;
00972       NumBits = 8;
00973       Scale = 4;
00974       break;
00975     }
00976     default:
00977       llvm_unreachable("Unsupported addressing mode!");
00978       break;
00979     }
00980 
00981     Offset += InstrOffs * Scale;
00982     assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
00983     if (Offset < 0) {
00984       Offset = -Offset;
00985       isSub = true;
00986     }
00987 
00988     // Attempt to fold address comp. if opcode has offset bits
00989     if (NumBits > 0) {
00990       // Common case: small offset, fits into instruction.
00991       MachineOperand &ImmOp = MI.getOperand(ImmIdx);
00992       int ImmedOffset = Offset / Scale;
00993       unsigned Mask = (1 << NumBits) - 1;
00994       if ((unsigned)Offset <= Mask * Scale) {
00995         // Replace the FrameIndex with sp
00996         MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
00997         if (isSub)
00998           ImmedOffset |= 1 << NumBits;
00999         ImmOp.ChangeToImmediate(ImmedOffset);
01000         return 0;
01001       }
01002 
01003       // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
01004       ImmedOffset = ImmedOffset & Mask;
01005       if (isSub)
01006         ImmedOffset |= 1 << NumBits;
01007       ImmOp.ChangeToImmediate(ImmedOffset);
01008       Offset &= ~(Mask*Scale);
01009     }
01010   }
01011 
01012   return (isSub) ? -Offset : Offset;
01013 }