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