LLVM API Documentation
00001 //===-- Thumb1RegisterInfo.cpp - Thumb-1 Register 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-1 implementation of the TargetRegisterInfo 00011 // class. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "Thumb1RegisterInfo.h" 00016 #include "ARMBaseInstrInfo.h" 00017 #include "ARMMachineFunctionInfo.h" 00018 #include "ARMSubtarget.h" 00019 #include "MCTargetDesc/ARMAddressingModes.h" 00020 #include "llvm/CodeGen/MachineConstantPool.h" 00021 #include "llvm/CodeGen/MachineFrameInfo.h" 00022 #include "llvm/CodeGen/MachineFunction.h" 00023 #include "llvm/CodeGen/MachineInstrBuilder.h" 00024 #include "llvm/CodeGen/MachineRegisterInfo.h" 00025 #include "llvm/CodeGen/RegisterScavenging.h" 00026 #include "llvm/IR/Constants.h" 00027 #include "llvm/IR/DerivedTypes.h" 00028 #include "llvm/IR/Function.h" 00029 #include "llvm/IR/LLVMContext.h" 00030 #include "llvm/Support/CommandLine.h" 00031 #include "llvm/Support/ErrorHandling.h" 00032 #include "llvm/Target/TargetFrameLowering.h" 00033 #include "llvm/Target/TargetMachine.h" 00034 00035 namespace llvm { 00036 extern cl::opt<bool> ReuseFrameIndexVals; 00037 } 00038 00039 using namespace llvm; 00040 00041 Thumb1RegisterInfo::Thumb1RegisterInfo(const ARMSubtarget &sti) 00042 : ARMBaseRegisterInfo(sti) { 00043 } 00044 00045 const TargetRegisterClass* 00046 Thumb1RegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC) 00047 const { 00048 if (ARM::tGPRRegClass.hasSubClassEq(RC)) 00049 return &ARM::tGPRRegClass; 00050 return ARMBaseRegisterInfo::getLargestLegalSuperClass(RC); 00051 } 00052 00053 const TargetRegisterClass * 00054 Thumb1RegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind) 00055 const { 00056 return &ARM::tGPRRegClass; 00057 } 00058 00059 /// emitLoadConstPool - Emits a load from constpool to materialize the 00060 /// specified immediate. 00061 void 00062 Thumb1RegisterInfo::emitLoadConstPool(MachineBasicBlock &MBB, 00063 MachineBasicBlock::iterator &MBBI, 00064 DebugLoc dl, 00065 unsigned DestReg, unsigned SubIdx, 00066 int Val, 00067 ARMCC::CondCodes Pred, unsigned PredReg, 00068 unsigned MIFlags) const { 00069 MachineFunction &MF = *MBB.getParent(); 00070 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); 00071 MachineConstantPool *ConstantPool = MF.getConstantPool(); 00072 const Constant *C = ConstantInt::get( 00073 Type::getInt32Ty(MBB.getParent()->getFunction()->getContext()), Val); 00074 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4); 00075 00076 BuildMI(MBB, MBBI, dl, TII.get(ARM::tLDRpci)) 00077 .addReg(DestReg, getDefRegState(true), SubIdx) 00078 .addConstantPoolIndex(Idx).addImm(Pred).addReg(PredReg) 00079 .setMIFlags(MIFlags); 00080 } 00081 00082 00083 /// emitThumbRegPlusImmInReg - Emits a series of instructions to materialize 00084 /// a destreg = basereg + immediate in Thumb code. Materialize the immediate 00085 /// in a register using mov / mvn sequences or load the immediate from a 00086 /// constpool entry. 00087 static 00088 void emitThumbRegPlusImmInReg(MachineBasicBlock &MBB, 00089 MachineBasicBlock::iterator &MBBI, 00090 DebugLoc dl, 00091 unsigned DestReg, unsigned BaseReg, 00092 int NumBytes, bool CanChangeCC, 00093 const TargetInstrInfo &TII, 00094 const ARMBaseRegisterInfo& MRI, 00095 unsigned MIFlags = MachineInstr::NoFlags) { 00096 MachineFunction &MF = *MBB.getParent(); 00097 bool isHigh = !isARMLowRegister(DestReg) || 00098 (BaseReg != 0 && !isARMLowRegister(BaseReg)); 00099 bool isSub = false; 00100 // Subtract doesn't have high register version. Load the negative value 00101 // if either base or dest register is a high register. Also, if do not 00102 // issue sub as part of the sequence if condition register is to be 00103 // preserved. 00104 if (NumBytes < 0 && !isHigh && CanChangeCC) { 00105 isSub = true; 00106 NumBytes = -NumBytes; 00107 } 00108 unsigned LdReg = DestReg; 00109 if (DestReg == ARM::SP) { 00110 assert(BaseReg == ARM::SP && "Unexpected!"); 00111 LdReg = MF.getRegInfo().createVirtualRegister(&ARM::tGPRRegClass); 00112 } 00113 00114 if (NumBytes <= 255 && NumBytes >= 0) 00115 AddDefaultT1CC(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVi8), LdReg)) 00116 .addImm(NumBytes).setMIFlags(MIFlags); 00117 else if (NumBytes < 0 && NumBytes >= -255) { 00118 AddDefaultT1CC(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVi8), LdReg)) 00119 .addImm(NumBytes).setMIFlags(MIFlags); 00120 AddDefaultT1CC(BuildMI(MBB, MBBI, dl, TII.get(ARM::tRSB), LdReg)) 00121 .addReg(LdReg, RegState::Kill).setMIFlags(MIFlags); 00122 } else 00123 MRI.emitLoadConstPool(MBB, MBBI, dl, LdReg, 0, NumBytes, 00124 ARMCC::AL, 0, MIFlags); 00125 00126 // Emit add / sub. 00127 int Opc = (isSub) ? ARM::tSUBrr : (isHigh ? ARM::tADDhirr : ARM::tADDrr); 00128 MachineInstrBuilder MIB = 00129 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg); 00130 if (Opc != ARM::tADDhirr) 00131 MIB = AddDefaultT1CC(MIB); 00132 if (DestReg == ARM::SP || isSub) 00133 MIB.addReg(BaseReg).addReg(LdReg, RegState::Kill); 00134 else 00135 MIB.addReg(LdReg).addReg(BaseReg, RegState::Kill); 00136 AddDefaultPred(MIB); 00137 } 00138 00139 /// calcNumMI - Returns the number of instructions required to materialize 00140 /// the specific add / sub r, c instruction. 00141 static unsigned calcNumMI(int Opc, int ExtraOpc, unsigned Bytes, 00142 unsigned NumBits, unsigned Scale) { 00143 unsigned NumMIs = 0; 00144 unsigned Chunk = ((1 << NumBits) - 1) * Scale; 00145 00146 if (Opc == ARM::tADDrSPi) { 00147 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes; 00148 Bytes -= ThisVal; 00149 NumMIs++; 00150 NumBits = 8; 00151 Scale = 1; // Followed by a number of tADDi8. 00152 Chunk = ((1 << NumBits) - 1) * Scale; 00153 } 00154 00155 NumMIs += Bytes / Chunk; 00156 if ((Bytes % Chunk) != 0) 00157 NumMIs++; 00158 if (ExtraOpc) 00159 NumMIs++; 00160 return NumMIs; 00161 } 00162 00163 /// emitThumbRegPlusImmediate - Emits a series of instructions to materialize 00164 /// a destreg = basereg + immediate in Thumb code. 00165 void llvm::emitThumbRegPlusImmediate(MachineBasicBlock &MBB, 00166 MachineBasicBlock::iterator &MBBI, 00167 DebugLoc dl, 00168 unsigned DestReg, unsigned BaseReg, 00169 int NumBytes, const TargetInstrInfo &TII, 00170 const ARMBaseRegisterInfo& MRI, 00171 unsigned MIFlags) { 00172 bool isSub = NumBytes < 0; 00173 unsigned Bytes = (unsigned)NumBytes; 00174 if (isSub) Bytes = -NumBytes; 00175 bool isMul4 = (Bytes & 3) == 0; 00176 bool isTwoAddr = false; 00177 bool DstNotEqBase = false; 00178 unsigned NumBits = 1; 00179 unsigned Scale = 1; 00180 int Opc = 0; 00181 int ExtraOpc = 0; 00182 bool NeedCC = false; 00183 00184 if (DestReg == BaseReg && BaseReg == ARM::SP) { 00185 assert(isMul4 && "Thumb sp inc / dec size must be multiple of 4!"); 00186 NumBits = 7; 00187 Scale = 4; 00188 Opc = isSub ? ARM::tSUBspi : ARM::tADDspi; 00189 isTwoAddr = true; 00190 } else if (!isSub && BaseReg == ARM::SP) { 00191 // r1 = add sp, 403 00192 // => 00193 // r1 = add sp, 100 * 4 00194 // r1 = add r1, 3 00195 if (!isMul4) { 00196 Bytes &= ~3; 00197 ExtraOpc = ARM::tADDi3; 00198 } 00199 NumBits = 8; 00200 Scale = 4; 00201 Opc = ARM::tADDrSPi; 00202 } else { 00203 // sp = sub sp, c 00204 // r1 = sub sp, c 00205 // r8 = sub sp, c 00206 if (DestReg != BaseReg) 00207 DstNotEqBase = true; 00208 NumBits = 8; 00209 if (DestReg == ARM::SP) { 00210 Opc = isSub ? ARM::tSUBspi : ARM::tADDspi; 00211 assert(isMul4 && "Thumb sp inc / dec size must be multiple of 4!"); 00212 NumBits = 7; 00213 Scale = 4; 00214 } else { 00215 Opc = isSub ? ARM::tSUBi8 : ARM::tADDi8; 00216 NumBits = 8; 00217 NeedCC = true; 00218 } 00219 isTwoAddr = true; 00220 } 00221 00222 unsigned NumMIs = calcNumMI(Opc, ExtraOpc, Bytes, NumBits, Scale); 00223 unsigned Threshold = (DestReg == ARM::SP) ? 3 : 2; 00224 if (NumMIs > Threshold) { 00225 // This will expand into too many instructions. Load the immediate from a 00226 // constpool entry. 00227 emitThumbRegPlusImmInReg(MBB, MBBI, dl, 00228 DestReg, BaseReg, NumBytes, true, 00229 TII, MRI, MIFlags); 00230 return; 00231 } 00232 00233 if (DstNotEqBase) { 00234 if (isARMLowRegister(DestReg) && isARMLowRegister(BaseReg)) { 00235 // If both are low registers, emit DestReg = add BaseReg, max(Imm, 7) 00236 unsigned Chunk = (1 << 3) - 1; 00237 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes; 00238 Bytes -= ThisVal; 00239 const MCInstrDesc &MCID = TII.get(isSub ? ARM::tSUBi3 : ARM::tADDi3); 00240 const MachineInstrBuilder MIB = 00241 AddDefaultT1CC(BuildMI(MBB, MBBI, dl, MCID, DestReg) 00242 .setMIFlags(MIFlags)); 00243 AddDefaultPred(MIB.addReg(BaseReg, RegState::Kill).addImm(ThisVal)); 00244 } else { 00245 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), DestReg) 00246 .addReg(BaseReg, RegState::Kill)) 00247 .setMIFlags(MIFlags); 00248 } 00249 BaseReg = DestReg; 00250 } 00251 00252 unsigned Chunk = ((1 << NumBits) - 1) * Scale; 00253 while (Bytes) { 00254 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes; 00255 Bytes -= ThisVal; 00256 ThisVal /= Scale; 00257 // Build the new tADD / tSUB. 00258 if (isTwoAddr) { 00259 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg); 00260 if (NeedCC) 00261 MIB = AddDefaultT1CC(MIB); 00262 MIB.addReg(DestReg).addImm(ThisVal); 00263 MIB = AddDefaultPred(MIB); 00264 MIB.setMIFlags(MIFlags); 00265 } else { 00266 bool isKill = BaseReg != ARM::SP; 00267 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg); 00268 if (NeedCC) 00269 MIB = AddDefaultT1CC(MIB); 00270 MIB.addReg(BaseReg, getKillRegState(isKill)).addImm(ThisVal); 00271 MIB = AddDefaultPred(MIB); 00272 MIB.setMIFlags(MIFlags); 00273 00274 BaseReg = DestReg; 00275 if (Opc == ARM::tADDrSPi) { 00276 // r4 = add sp, imm 00277 // r4 = add r4, imm 00278 // ... 00279 NumBits = 8; 00280 Scale = 1; 00281 Chunk = ((1 << NumBits) - 1) * Scale; 00282 Opc = isSub ? ARM::tSUBi8 : ARM::tADDi8; 00283 NeedCC = isTwoAddr = true; 00284 } 00285 } 00286 } 00287 00288 if (ExtraOpc) { 00289 const MCInstrDesc &MCID = TII.get(ExtraOpc); 00290 AddDefaultPred(AddDefaultT1CC(BuildMI(MBB, MBBI, dl, MCID, DestReg)) 00291 .addReg(DestReg, RegState::Kill) 00292 .addImm(((unsigned)NumBytes) & 3) 00293 .setMIFlags(MIFlags)); 00294 } 00295 } 00296 00297 /// emitThumbConstant - Emit a series of instructions to materialize a 00298 /// constant. 00299 static void emitThumbConstant(MachineBasicBlock &MBB, 00300 MachineBasicBlock::iterator &MBBI, 00301 unsigned DestReg, int Imm, 00302 const TargetInstrInfo &TII, 00303 const Thumb1RegisterInfo& MRI, 00304 DebugLoc dl) { 00305 bool isSub = Imm < 0; 00306 if (isSub) Imm = -Imm; 00307 00308 int Chunk = (1 << 8) - 1; 00309 int ThisVal = (Imm > Chunk) ? Chunk : Imm; 00310 Imm -= ThisVal; 00311 AddDefaultPred(AddDefaultT1CC(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVi8), 00312 DestReg)) 00313 .addImm(ThisVal)); 00314 if (Imm > 0) 00315 emitThumbRegPlusImmediate(MBB, MBBI, dl, DestReg, DestReg, Imm, TII, MRI); 00316 if (isSub) { 00317 const MCInstrDesc &MCID = TII.get(ARM::tRSB); 00318 AddDefaultPred(AddDefaultT1CC(BuildMI(MBB, MBBI, dl, MCID, DestReg)) 00319 .addReg(DestReg, RegState::Kill)); 00320 } 00321 } 00322 00323 static void removeOperands(MachineInstr &MI, unsigned i) { 00324 unsigned Op = i; 00325 for (unsigned e = MI.getNumOperands(); i != e; ++i) 00326 MI.RemoveOperand(Op); 00327 } 00328 00329 /// convertToNonSPOpcode - Change the opcode to the non-SP version, because 00330 /// we're replacing the frame index with a non-SP register. 00331 static unsigned convertToNonSPOpcode(unsigned Opcode) { 00332 switch (Opcode) { 00333 case ARM::tLDRspi: 00334 return ARM::tLDRi; 00335 00336 case ARM::tSTRspi: 00337 return ARM::tSTRi; 00338 } 00339 00340 return Opcode; 00341 } 00342 00343 bool Thumb1RegisterInfo:: 00344 rewriteFrameIndex(MachineBasicBlock::iterator II, unsigned FrameRegIdx, 00345 unsigned FrameReg, int &Offset, 00346 const ARMBaseInstrInfo &TII) const { 00347 MachineInstr &MI = *II; 00348 MachineBasicBlock &MBB = *MI.getParent(); 00349 DebugLoc dl = MI.getDebugLoc(); 00350 MachineInstrBuilder MIB(*MBB.getParent(), &MI); 00351 unsigned Opcode = MI.getOpcode(); 00352 const MCInstrDesc &Desc = MI.getDesc(); 00353 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask); 00354 00355 if (Opcode == ARM::tADDrSPi) { 00356 Offset += MI.getOperand(FrameRegIdx+1).getImm(); 00357 00358 // Can't use tADDrSPi if it's based off the frame pointer. 00359 unsigned NumBits = 0; 00360 unsigned Scale = 1; 00361 if (FrameReg != ARM::SP) { 00362 Opcode = ARM::tADDi3; 00363 NumBits = 3; 00364 } else { 00365 NumBits = 8; 00366 Scale = 4; 00367 assert((Offset & 3) == 0 && 00368 "Thumb add/sub sp, #imm immediate must be multiple of 4!"); 00369 } 00370 00371 unsigned PredReg; 00372 if (Offset == 0 && getInstrPredicate(&MI, PredReg) == ARMCC::AL) { 00373 // Turn it into a move. 00374 MI.setDesc(TII.get(ARM::tMOVr)); 00375 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); 00376 // Remove offset 00377 MI.RemoveOperand(FrameRegIdx+1); 00378 return true; 00379 } 00380 00381 // Common case: small offset, fits into instruction. 00382 unsigned Mask = (1 << NumBits) - 1; 00383 if (((Offset / Scale) & ~Mask) == 0) { 00384 // Replace the FrameIndex with sp / fp 00385 if (Opcode == ARM::tADDi3) { 00386 MI.setDesc(TII.get(Opcode)); 00387 removeOperands(MI, FrameRegIdx); 00388 AddDefaultPred(AddDefaultT1CC(MIB).addReg(FrameReg) 00389 .addImm(Offset / Scale)); 00390 } else { 00391 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); 00392 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset / Scale); 00393 } 00394 return true; 00395 } 00396 00397 unsigned DestReg = MI.getOperand(0).getReg(); 00398 unsigned Bytes = (Offset > 0) ? Offset : -Offset; 00399 unsigned NumMIs = calcNumMI(Opcode, 0, Bytes, NumBits, Scale); 00400 // MI would expand into a large number of instructions. Don't try to 00401 // simplify the immediate. 00402 if (NumMIs > 2) { 00403 emitThumbRegPlusImmediate(MBB, II, dl, DestReg, FrameReg, Offset, TII, 00404 *this); 00405 MBB.erase(II); 00406 return true; 00407 } 00408 00409 if (Offset > 0) { 00410 // Translate r0 = add sp, imm to 00411 // r0 = add sp, 255*4 00412 // r0 = add r0, (imm - 255*4) 00413 if (Opcode == ARM::tADDi3) { 00414 MI.setDesc(TII.get(Opcode)); 00415 removeOperands(MI, FrameRegIdx); 00416 AddDefaultPred(AddDefaultT1CC(MIB).addReg(FrameReg).addImm(Mask)); 00417 } else { 00418 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); 00419 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Mask); 00420 } 00421 Offset = (Offset - Mask * Scale); 00422 MachineBasicBlock::iterator NII = std::next(II); 00423 emitThumbRegPlusImmediate(MBB, NII, dl, DestReg, DestReg, Offset, TII, 00424 *this); 00425 } else { 00426 // Translate r0 = add sp, -imm to 00427 // r0 = -imm (this is then translated into a series of instructions) 00428 // r0 = add r0, sp 00429 emitThumbConstant(MBB, II, DestReg, Offset, TII, *this, dl); 00430 00431 MI.setDesc(TII.get(ARM::tADDhirr)); 00432 MI.getOperand(FrameRegIdx).ChangeToRegister(DestReg, false, false, true); 00433 MI.getOperand(FrameRegIdx+1).ChangeToRegister(FrameReg, false); 00434 } 00435 return true; 00436 } else { 00437 if (AddrMode != ARMII::AddrModeT1_s) 00438 llvm_unreachable("Unsupported addressing mode!"); 00439 00440 unsigned ImmIdx = FrameRegIdx + 1; 00441 int InstrOffs = MI.getOperand(ImmIdx).getImm(); 00442 unsigned NumBits = (FrameReg == ARM::SP) ? 8 : 5; 00443 unsigned Scale = 4; 00444 00445 Offset += InstrOffs * Scale; 00446 assert((Offset & (Scale - 1)) == 0 && "Can't encode this offset!"); 00447 00448 // Common case: small offset, fits into instruction. 00449 MachineOperand &ImmOp = MI.getOperand(ImmIdx); 00450 int ImmedOffset = Offset / Scale; 00451 unsigned Mask = (1 << NumBits) - 1; 00452 00453 if ((unsigned)Offset <= Mask * Scale) { 00454 // Replace the FrameIndex with the frame register (e.g., sp). 00455 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); 00456 ImmOp.ChangeToImmediate(ImmedOffset); 00457 00458 // If we're using a register where sp was stored, convert the instruction 00459 // to the non-SP version. 00460 unsigned NewOpc = convertToNonSPOpcode(Opcode); 00461 if (NewOpc != Opcode && FrameReg != ARM::SP) 00462 MI.setDesc(TII.get(NewOpc)); 00463 00464 return true; 00465 } 00466 00467 NumBits = 5; 00468 Mask = (1 << NumBits) - 1; 00469 00470 // If this is a thumb spill / restore, we will be using a constpool load to 00471 // materialize the offset. 00472 if (Opcode == ARM::tLDRspi || Opcode == ARM::tSTRspi) { 00473 ImmOp.ChangeToImmediate(0); 00474 } else { 00475 // Otherwise, it didn't fit. Pull in what we can to simplify the immed. 00476 ImmedOffset = ImmedOffset & Mask; 00477 ImmOp.ChangeToImmediate(ImmedOffset); 00478 Offset &= ~(Mask * Scale); 00479 } 00480 } 00481 00482 return Offset == 0; 00483 } 00484 00485 void Thumb1RegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg, 00486 int64_t Offset) const { 00487 const ARMBaseInstrInfo &TII = 00488 *static_cast<const ARMBaseInstrInfo *>(MI.getParent() 00489 ->getParent() 00490 ->getTarget() 00491 .getSubtargetImpl() 00492 ->getInstrInfo()); 00493 int Off = Offset; // ARM doesn't need the general 64-bit offsets 00494 unsigned i = 0; 00495 00496 while (!MI.getOperand(i).isFI()) { 00497 ++i; 00498 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!"); 00499 } 00500 bool Done = rewriteFrameIndex(MI, i, BaseReg, Off, TII); 00501 assert (Done && "Unable to resolve frame index!"); 00502 (void)Done; 00503 } 00504 00505 /// saveScavengerRegister - Spill the register so it can be used by the 00506 /// register scavenger. Return true. 00507 bool 00508 Thumb1RegisterInfo::saveScavengerRegister(MachineBasicBlock &MBB, 00509 MachineBasicBlock::iterator I, 00510 MachineBasicBlock::iterator &UseMI, 00511 const TargetRegisterClass *RC, 00512 unsigned Reg) const { 00513 // Thumb1 can't use the emergency spill slot on the stack because 00514 // ldr/str immediate offsets must be positive, and if we're referencing 00515 // off the frame pointer (if, for example, there are alloca() calls in 00516 // the function, the offset will be negative. Use R12 instead since that's 00517 // a call clobbered register that we know won't be used in Thumb1 mode. 00518 const TargetInstrInfo &TII = *MBB.getParent()->getSubtarget().getInstrInfo(); 00519 DebugLoc DL; 00520 AddDefaultPred(BuildMI(MBB, I, DL, TII.get(ARM::tMOVr)) 00521 .addReg(ARM::R12, RegState::Define) 00522 .addReg(Reg, RegState::Kill)); 00523 00524 // The UseMI is where we would like to restore the register. If there's 00525 // interference with R12 before then, however, we'll need to restore it 00526 // before that instead and adjust the UseMI. 00527 bool done = false; 00528 for (MachineBasicBlock::iterator II = I; !done && II != UseMI ; ++II) { 00529 if (II->isDebugValue()) 00530 continue; 00531 // If this instruction affects R12, adjust our restore point. 00532 for (unsigned i = 0, e = II->getNumOperands(); i != e; ++i) { 00533 const MachineOperand &MO = II->getOperand(i); 00534 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::R12)) { 00535 UseMI = II; 00536 done = true; 00537 break; 00538 } 00539 if (!MO.isReg() || MO.isUndef() || !MO.getReg() || 00540 TargetRegisterInfo::isVirtualRegister(MO.getReg())) 00541 continue; 00542 if (MO.getReg() == ARM::R12) { 00543 UseMI = II; 00544 done = true; 00545 break; 00546 } 00547 } 00548 } 00549 // Restore the register from R12 00550 AddDefaultPred(BuildMI(MBB, UseMI, DL, TII.get(ARM::tMOVr)). 00551 addReg(Reg, RegState::Define).addReg(ARM::R12, RegState::Kill)); 00552 00553 return true; 00554 } 00555 00556 void 00557 Thumb1RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 00558 int SPAdj, unsigned FIOperandNum, 00559 RegScavenger *RS) const { 00560 unsigned VReg = 0; 00561 MachineInstr &MI = *II; 00562 MachineBasicBlock &MBB = *MI.getParent(); 00563 MachineFunction &MF = *MBB.getParent(); 00564 const ARMBaseInstrInfo &TII = 00565 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); 00566 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 00567 DebugLoc dl = MI.getDebugLoc(); 00568 MachineInstrBuilder MIB(*MBB.getParent(), &MI); 00569 00570 unsigned FrameReg = ARM::SP; 00571 int FrameIndex = MI.getOperand(FIOperandNum).getIndex(); 00572 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) + 00573 MF.getFrameInfo()->getStackSize() + SPAdj; 00574 00575 if (MF.getFrameInfo()->hasVarSizedObjects()) { 00576 assert(SPAdj == 0 && MF.getSubtarget().getFrameLowering()->hasFP(MF) && 00577 "Unexpected"); 00578 // There are alloca()'s in this function, must reference off the frame 00579 // pointer or base pointer instead. 00580 if (!hasBasePointer(MF)) { 00581 FrameReg = getFrameRegister(MF); 00582 Offset -= AFI->getFramePtrSpillOffset(); 00583 } else 00584 FrameReg = BasePtr; 00585 } 00586 00587 // PEI::scavengeFrameVirtualRegs() cannot accurately track SPAdj because the 00588 // call frame setup/destroy instructions have already been eliminated. That 00589 // means the stack pointer cannot be used to access the emergency spill slot 00590 // when !hasReservedCallFrame(). 00591 #ifndef NDEBUG 00592 if (RS && FrameReg == ARM::SP && RS->isScavengingFrameIndex(FrameIndex)){ 00593 assert(MF.getTarget() 00594 .getSubtargetImpl() 00595 ->getFrameLowering() 00596 ->hasReservedCallFrame(MF) && 00597 "Cannot use SP to access the emergency spill slot in " 00598 "functions without a reserved call frame"); 00599 assert(!MF.getFrameInfo()->hasVarSizedObjects() && 00600 "Cannot use SP to access the emergency spill slot in " 00601 "functions with variable sized frame objects"); 00602 } 00603 #endif // NDEBUG 00604 00605 // Special handling of dbg_value instructions. 00606 if (MI.isDebugValue()) { 00607 MI.getOperand(FIOperandNum). ChangeToRegister(FrameReg, false /*isDef*/); 00608 MI.getOperand(FIOperandNum+1).ChangeToImmediate(Offset); 00609 return; 00610 } 00611 00612 // Modify MI as necessary to handle as much of 'Offset' as possible 00613 assert(AFI->isThumbFunction() && 00614 "This eliminateFrameIndex only supports Thumb1!"); 00615 if (rewriteFrameIndex(MI, FIOperandNum, FrameReg, Offset, TII)) 00616 return; 00617 00618 // If we get here, the immediate doesn't fit into the instruction. We folded 00619 // as much as possible above, handle the rest, providing a register that is 00620 // SP+LargeImm. 00621 assert(Offset && "This code isn't needed if offset already handled!"); 00622 00623 unsigned Opcode = MI.getOpcode(); 00624 00625 // Remove predicate first. 00626 int PIdx = MI.findFirstPredOperandIdx(); 00627 if (PIdx != -1) 00628 removeOperands(MI, PIdx); 00629 00630 if (MI.mayLoad()) { 00631 // Use the destination register to materialize sp + offset. 00632 unsigned TmpReg = MI.getOperand(0).getReg(); 00633 bool UseRR = false; 00634 if (Opcode == ARM::tLDRspi) { 00635 if (FrameReg == ARM::SP) 00636 emitThumbRegPlusImmInReg(MBB, II, dl, TmpReg, FrameReg, 00637 Offset, false, TII, *this); 00638 else { 00639 emitLoadConstPool(MBB, II, dl, TmpReg, 0, Offset); 00640 UseRR = true; 00641 } 00642 } else { 00643 emitThumbRegPlusImmediate(MBB, II, dl, TmpReg, FrameReg, Offset, TII, 00644 *this); 00645 } 00646 00647 MI.setDesc(TII.get(UseRR ? ARM::tLDRr : ARM::tLDRi)); 00648 MI.getOperand(FIOperandNum).ChangeToRegister(TmpReg, false, false, true); 00649 if (UseRR) 00650 // Use [reg, reg] addrmode. Replace the immediate operand w/ the frame 00651 // register. The offset is already handled in the vreg value. 00652 MI.getOperand(FIOperandNum+1).ChangeToRegister(FrameReg, false, false, 00653 false); 00654 } else if (MI.mayStore()) { 00655 VReg = MF.getRegInfo().createVirtualRegister(&ARM::tGPRRegClass); 00656 bool UseRR = false; 00657 00658 if (Opcode == ARM::tSTRspi) { 00659 if (FrameReg == ARM::SP) 00660 emitThumbRegPlusImmInReg(MBB, II, dl, VReg, FrameReg, 00661 Offset, false, TII, *this); 00662 else { 00663 emitLoadConstPool(MBB, II, dl, VReg, 0, Offset); 00664 UseRR = true; 00665 } 00666 } else 00667 emitThumbRegPlusImmediate(MBB, II, dl, VReg, FrameReg, Offset, TII, 00668 *this); 00669 MI.setDesc(TII.get(UseRR ? ARM::tSTRr : ARM::tSTRi)); 00670 MI.getOperand(FIOperandNum).ChangeToRegister(VReg, false, false, true); 00671 if (UseRR) 00672 // Use [reg, reg] addrmode. Replace the immediate operand w/ the frame 00673 // register. The offset is already handled in the vreg value. 00674 MI.getOperand(FIOperandNum+1).ChangeToRegister(FrameReg, false, false, 00675 false); 00676 } else { 00677 llvm_unreachable("Unexpected opcode!"); 00678 } 00679 00680 // Add predicate back if it's needed. 00681 if (MI.isPredicable()) 00682 AddDefaultPred(MIB); 00683 }