LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ARMBaseRegisterInfo.cpp
Go to the documentation of this file.
1 //===-- ARMBaseRegisterInfo.cpp - ARM Register Information ----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the base ARM implementation of TargetRegisterInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "ARMBaseRegisterInfo.h"
15 #include "ARM.h"
16 #include "ARMBaseInstrInfo.h"
17 #include "ARMFrameLowering.h"
18 #include "ARMMachineFunctionInfo.h"
19 #include "ARMSubtarget.h"
21 #include "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/IR/Constants.h"
31 #include "llvm/IR/DerivedTypes.h"
32 #include "llvm/IR/Function.h"
33 #include "llvm/IR/LLVMContext.h"
34 #include "llvm/Support/Debug.h"
40 
41 #define GET_REGINFO_TARGET_DESC
42 #include "ARMGenRegisterInfo.inc"
43 
44 using namespace llvm;
45 
47  : ARMGenRegisterInfo(ARM::LR, 0, 0, ARM::PC), STI(sti),
48  FramePtr((STI.isTargetDarwin() || STI.isThumb()) ? ARM::R7 : ARM::R11),
49  BasePtr(ARM::R6) {
50 }
51 
52 const uint16_t*
54  const uint16_t *RegList = (STI.isTargetIOS() && !STI.isAAPCS_ABI())
55  ? CSR_iOS_SaveList
56  : CSR_AAPCS_SaveList;
57 
58  if (!MF) return RegList;
59 
60  const Function *F = MF->getFunction();
61  if (F->getCallingConv() == CallingConv::GHC) {
62  // GHC set of callee saved regs is empty as all those regs are
63  // used for passing STG regs around
64  return CSR_NoRegs_SaveList;
65  } else if (F->hasFnAttribute("interrupt")) {
66  if (STI.isMClass()) {
67  // M-class CPUs have hardware which saves the registers needed to allow a
68  // function conforming to the AAPCS to function as a handler.
69  return CSR_AAPCS_SaveList;
70  } else if (F->getFnAttribute("interrupt").getValueAsString() == "FIQ") {
71  // Fast interrupt mode gives the handler a private copy of R8-R14, so less
72  // need to be saved to restore user-mode state.
73  return CSR_FIQ_SaveList;
74  } else {
75  // Generally only R13-R14 (i.e. SP, LR) are automatically preserved by
76  // exception handling.
77  return CSR_GenericInt_SaveList;
78  }
79  }
80 
81  return RegList;
82 }
83 
84 const uint32_t*
86  if (CC == CallingConv::GHC)
87  // This is academic becase all GHC calls are (supposed to be) tail calls
88  return CSR_NoRegs_RegMask;
89  return (STI.isTargetIOS() && !STI.isAAPCS_ABI())
90  ? CSR_iOS_RegMask : CSR_AAPCS_RegMask;
91 }
92 
93 const uint32_t*
95  return CSR_NoRegs_RegMask;
96 }
97 
98 const uint32_t*
100  // This should return a register mask that is the same as that returned by
101  // getCallPreservedMask but that additionally preserves the register used for
102  // the first i32 argument (which must also be the register used to return a
103  // single i32 return value)
104  //
105  // In case that the calling convention does not use the same register for
106  // both or otherwise does not want to enable this optimization, the function
107  // should return NULL
108  if (CC == CallingConv::GHC)
109  // This is academic becase all GHC calls are (supposed to be) tail calls
110  return NULL;
111  return (STI.isTargetIOS() && !STI.isAAPCS_ABI())
112  ? CSR_iOS_ThisReturn_RegMask : CSR_AAPCS_ThisReturn_RegMask;
113 }
114 
117  const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
118 
119  // FIXME: avoid re-calculating this every time.
120  BitVector Reserved(getNumRegs());
121  Reserved.set(ARM::SP);
122  Reserved.set(ARM::PC);
123  Reserved.set(ARM::FPSCR);
124  Reserved.set(ARM::APSR_NZCV);
125  if (TFI->hasFP(MF))
126  Reserved.set(FramePtr);
127  if (hasBasePointer(MF))
128  Reserved.set(BasePtr);
129  // Some targets reserve R9.
130  if (STI.isR9Reserved())
131  Reserved.set(ARM::R9);
132  // Reserve D16-D31 if the subtarget doesn't support them.
133  if (!STI.hasVFP3() || STI.hasD16()) {
134  assert(ARM::D31 == ARM::D16 + 15);
135  for (unsigned i = 0; i != 16; ++i)
136  Reserved.set(ARM::D16 + i);
137  }
138  const TargetRegisterClass *RC = &ARM::GPRPairRegClass;
139  for(TargetRegisterClass::iterator I = RC->begin(), E = RC->end(); I!=E; ++I)
140  for (MCSubRegIterator SI(*I, this); SI.isValid(); ++SI)
141  if (Reserved.test(*SI)) Reserved.set(*I);
142 
143  return Reserved;
144 }
145 
146 const TargetRegisterClass*
148  const {
149  const TargetRegisterClass *Super = RC;
151  do {
152  switch (Super->getID()) {
153  case ARM::GPRRegClassID:
154  case ARM::SPRRegClassID:
155  case ARM::DPRRegClassID:
156  case ARM::QPRRegClassID:
157  case ARM::QQPRRegClassID:
158  case ARM::QQQQPRRegClassID:
159  case ARM::GPRPairRegClassID:
160  return Super;
161  }
162  Super = *I++;
163  } while (Super);
164  return RC;
165 }
166 
167 const TargetRegisterClass *
169  const {
170  return &ARM::GPRRegClass;
171 }
172 
173 const TargetRegisterClass *
175  if (RC == &ARM::CCRRegClass)
176  return 0; // Can't copy CCR registers.
177  return RC;
178 }
179 
180 unsigned
182  MachineFunction &MF) const {
183  const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
184 
185  switch (RC->getID()) {
186  default:
187  return 0;
188  case ARM::tGPRRegClassID:
189  return TFI->hasFP(MF) ? 4 : 5;
190  case ARM::GPRRegClassID: {
191  unsigned FP = TFI->hasFP(MF) ? 1 : 0;
192  return 10 - FP - (STI.isR9Reserved() ? 1 : 0);
193  }
194  case ARM::SPRRegClassID: // Currently not used as 'rep' register class.
195  case ARM::DPRRegClassID:
196  return 32 - 10;
197  }
198 }
199 
200 // Get the other register in a GPRPair.
201 static unsigned getPairedGPR(unsigned Reg, bool Odd, const MCRegisterInfo *RI) {
202  for (MCSuperRegIterator Supers(Reg, RI); Supers.isValid(); ++Supers)
203  if (ARM::GPRPairRegClass.contains(*Supers))
204  return RI->getSubReg(*Supers, Odd ? ARM::gsub_1 : ARM::gsub_0);
205  return 0;
206 }
207 
208 // Resolve the RegPairEven / RegPairOdd register allocator hints.
209 void
211  ArrayRef<MCPhysReg> Order,
213  const MachineFunction &MF,
214  const VirtRegMap *VRM) const {
215  const MachineRegisterInfo &MRI = MF.getRegInfo();
216  std::pair<unsigned, unsigned> Hint = MRI.getRegAllocationHint(VirtReg);
217 
218  unsigned Odd;
219  switch (Hint.first) {
220  case ARMRI::RegPairEven:
221  Odd = 0;
222  break;
223  case ARMRI::RegPairOdd:
224  Odd = 1;
225  break;
226  default:
227  TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints, MF, VRM);
228  return;
229  }
230 
231  // This register should preferably be even (Odd == 0) or odd (Odd == 1).
232  // Check if the other part of the pair has already been assigned, and provide
233  // the paired register as the first hint.
234  unsigned PairedPhys = 0;
235  if (VRM && VRM->hasPhys(Hint.second)) {
236  PairedPhys = getPairedGPR(VRM->getPhys(Hint.second), Odd, this);
237  if (PairedPhys && MRI.isReserved(PairedPhys))
238  PairedPhys = 0;
239  }
240 
241  // First prefer the paired physreg.
242  if (PairedPhys &&
243  std::find(Order.begin(), Order.end(), PairedPhys) != Order.end())
244  Hints.push_back(PairedPhys);
245 
246  // Then prefer even or odd registers.
247  for (unsigned I = 0, E = Order.size(); I != E; ++I) {
248  unsigned Reg = Order[I];
249  if (Reg == PairedPhys || (getEncodingValue(Reg) & 1) != Odd)
250  continue;
251  // Don't provide hints that are paired to a reserved register.
252  unsigned Paired = getPairedGPR(Reg, !Odd, this);
253  if (!Paired || MRI.isReserved(Paired))
254  continue;
255  Hints.push_back(Reg);
256  }
257 }
258 
259 void
261  MachineFunction &MF) const {
263  std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg);
264  if ((Hint.first == (unsigned)ARMRI::RegPairOdd ||
265  Hint.first == (unsigned)ARMRI::RegPairEven) &&
267  // If 'Reg' is one of the even / odd register pair and it's now changed
268  // (e.g. coalesced) into a different register. The other register of the
269  // pair allocation hint must be updated to reflect the relationship
270  // change.
271  unsigned OtherReg = Hint.second;
272  Hint = MRI->getRegAllocationHint(OtherReg);
273  if (Hint.second == Reg)
274  // Make sure the pair has not already divorced.
275  MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
276  }
277 }
278 
279 bool
281  // CortexA9 has a Write-after-write hazard for NEON registers.
282  if (!STI.isLikeA9())
283  return false;
284 
285  switch (RC->getID()) {
286  case ARM::DPRRegClassID:
287  case ARM::DPR_8RegClassID:
288  case ARM::DPR_VFP2RegClassID:
289  case ARM::QPRRegClassID:
290  case ARM::QPR_8RegClassID:
291  case ARM::QPR_VFP2RegClassID:
292  case ARM::SPRRegClassID:
293  case ARM::SPR_8RegClassID:
294  // Avoid reusing S, D, and Q registers.
295  // Don't increase register pressure for QQ and QQQQ.
296  return true;
297  default:
298  return false;
299  }
300 }
301 
303  const MachineFrameInfo *MFI = MF.getFrameInfo();
304  const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
305  const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
306 
307  // When outgoing call frames are so large that we adjust the stack pointer
308  // around the call, we can no longer use the stack pointer to reach the
309  // emergency spill slot.
310  if (needsStackRealignment(MF) && !TFI->hasReservedCallFrame(MF))
311  return true;
312 
313  // Thumb has trouble with negative offsets from the FP. Thumb2 has a limited
314  // negative range for ldr/str (255), and thumb1 is positive offsets only.
315  // It's going to be better to use the SP or Base Pointer instead. When there
316  // are variable sized objects, we can't reference off of the SP, so we
317  // reserve a Base Pointer.
318  if (AFI->isThumbFunction() && MFI->hasVarSizedObjects()) {
319  // Conservatively estimate whether the negative offset from the frame
320  // pointer will be sufficient to reach. If a function has a smallish
321  // frame, it's less likely to have lots of spills and callee saved
322  // space, so it's all more likely to be within range of the frame pointer.
323  // If it's wrong, the scavenger will still enable access to work, it just
324  // won't be optimal.
325  if (AFI->isThumb2Function() && MFI->getLocalFrameSize() < 128)
326  return false;
327  return true;
328  }
329 
330  return false;
331 }
332 
334  const MachineRegisterInfo *MRI = &MF.getRegInfo();
335  const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
336  // We can't realign the stack if:
337  // 1. Dynamic stack realignment is explicitly disabled,
338  // 2. This is a Thumb1 function (it's not useful, so we don't bother), or
339  // 3. There are VLAs in the function and the base pointer is disabled.
340  if (MF.getFunction()->hasFnAttribute("no-realign-stack"))
341  return false;
342  if (AFI->isThumb1OnlyFunction())
343  return false;
344  // Stack realignment requires a frame pointer. If we already started
345  // register allocation with frame pointer elimination, it is too late now.
346  if (!MRI->canReserveReg(FramePtr))
347  return false;
348  // We may also need a base pointer if there are dynamic allocas or stack
349  // pointer adjustments around calls.
351  return true;
352  // A base pointer is required and allowed. Check that it isn't too late to
353  // reserve it.
354  return MRI->canReserveReg(BasePtr);
355 }
356 
359  const MachineFrameInfo *MFI = MF.getFrameInfo();
360  const Function *F = MF.getFunction();
361  unsigned StackAlign = MF.getTarget().getFrameLowering()->getStackAlignment();
362  bool requiresRealignment =
363  ((MFI->getMaxAlignment() > StackAlign) ||
364  F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
366 
367  return requiresRealignment && canRealignStack(MF);
368 }
369 
372  const MachineFrameInfo *MFI = MF.getFrameInfo();
373  if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI->adjustsStack())
374  return true;
375  return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken()
376  || needsStackRealignment(MF);
377 }
378 
379 unsigned
381  const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
382 
383  if (TFI->hasFP(MF))
384  return FramePtr;
385  return ARM::SP;
386 }
387 
388 /// emitLoadConstPool - Emits a load from constpool to materialize the
389 /// specified immediate.
393  DebugLoc dl,
394  unsigned DestReg, unsigned SubIdx, int Val,
395  ARMCC::CondCodes Pred,
396  unsigned PredReg, unsigned MIFlags) const {
397  MachineFunction &MF = *MBB.getParent();
398  const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
400  const Constant *C =
402  unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
403 
404  BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
405  .addReg(DestReg, getDefRegState(true), SubIdx)
406  .addConstantPoolIndex(Idx)
407  .addImm(0).addImm(Pred).addReg(PredReg)
408  .setMIFlags(MIFlags);
409 }
410 
413  return true;
414 }
415 
418  return true;
419 }
420 
423  return true;
424 }
425 
428  return true;
429 }
430 
431 int64_t ARMBaseRegisterInfo::
432 getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const {
433  const MCInstrDesc &Desc = MI->getDesc();
434  unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
435  int64_t InstrOffs = 0;
436  int Scale = 1;
437  unsigned ImmIdx = 0;
438  switch (AddrMode) {
441  case ARMII::AddrMode_i12:
442  InstrOffs = MI->getOperand(Idx+1).getImm();
443  Scale = 1;
444  break;
445  case ARMII::AddrMode5: {
446  // VFP address mode.
447  const MachineOperand &OffOp = MI->getOperand(Idx+1);
448  InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm());
449  if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub)
450  InstrOffs = -InstrOffs;
451  Scale = 4;
452  break;
453  }
454  case ARMII::AddrMode2: {
455  ImmIdx = Idx+2;
456  InstrOffs = ARM_AM::getAM2Offset(MI->getOperand(ImmIdx).getImm());
457  if (ARM_AM::getAM2Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
458  InstrOffs = -InstrOffs;
459  break;
460  }
461  case ARMII::AddrMode3: {
462  ImmIdx = Idx+2;
463  InstrOffs = ARM_AM::getAM3Offset(MI->getOperand(ImmIdx).getImm());
464  if (ARM_AM::getAM3Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
465  InstrOffs = -InstrOffs;
466  break;
467  }
468  case ARMII::AddrModeT1_s: {
469  ImmIdx = Idx+1;
470  InstrOffs = MI->getOperand(ImmIdx).getImm();
471  Scale = 4;
472  break;
473  }
474  default:
475  llvm_unreachable("Unsupported addressing mode!");
476  }
477 
478  return InstrOffs * Scale;
479 }
480 
481 /// needsFrameBaseReg - Returns true if the instruction's frame index
482 /// reference would be better served by a base register other than FP
483 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
484 /// references it should create new base registers for.
486 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
487  for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) {
488  assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
489  }
490 
491  // It's the load/store FI references that cause issues, as it can be difficult
492  // to materialize the offset if it won't fit in the literal field. Estimate
493  // based on the size of the local frame and some conservative assumptions
494  // about the rest of the stack frame (note, this is pre-regalloc, so
495  // we don't know everything for certain yet) whether this offset is likely
496  // to be out of range of the immediate. Return true if so.
497 
498  // We only generate virtual base registers for loads and stores, so
499  // return false for everything else.
500  unsigned Opc = MI->getOpcode();
501  switch (Opc) {
502  case ARM::LDRi12: case ARM::LDRH: case ARM::LDRBi12:
503  case ARM::STRi12: case ARM::STRH: case ARM::STRBi12:
504  case ARM::t2LDRi12: case ARM::t2LDRi8:
505  case ARM::t2STRi12: case ARM::t2STRi8:
506  case ARM::VLDRS: case ARM::VLDRD:
507  case ARM::VSTRS: case ARM::VSTRD:
508  case ARM::tSTRspi: case ARM::tLDRspi:
509  break;
510  default:
511  return false;
512  }
513 
514  // Without a virtual base register, if the function has variable sized
515  // objects, all fixed-size local references will be via the frame pointer,
516  // Approximate the offset and see if it's legal for the instruction.
517  // Note that the incoming offset is based on the SP value at function entry,
518  // so it'll be negative.
519  MachineFunction &MF = *MI->getParent()->getParent();
520  const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
521  MachineFrameInfo *MFI = MF.getFrameInfo();
523 
524  // Estimate an offset from the frame pointer.
525  // Conservatively assume all callee-saved registers get pushed. R4-R6
526  // will be earlier than the FP, so we ignore those.
527  // R7, LR
528  int64_t FPOffset = Offset - 8;
529  // ARM and Thumb2 functions also need to consider R8-R11 and D8-D15
530  if (!AFI->isThumbFunction() || !AFI->isThumb1OnlyFunction())
531  FPOffset -= 80;
532  // Estimate an offset from the stack pointer.
533  // The incoming offset is relating to the SP at the start of the function,
534  // but when we access the local it'll be relative to the SP after local
535  // allocation, so adjust our SP-relative offset by that allocation size.
536  Offset = -Offset;
537  Offset += MFI->getLocalFrameSize();
538  // Assume that we'll have at least some spill slots allocated.
539  // FIXME: This is a total SWAG number. We should run some statistics
540  // and pick a real one.
541  Offset += 128; // 128 bytes of spill slots
542 
543  // If there is a frame pointer, try using it.
544  // The FP is only available if there is no dynamic realignment. We
545  // don't know for sure yet whether we'll need that, so we guess based
546  // on whether there are any local variables that would trigger it.
547  unsigned StackAlign = TFI->getStackAlignment();
548  if (TFI->hasFP(MF) &&
549  !((MFI->getLocalFrameMaxAlign() > StackAlign) && canRealignStack(MF))) {
550  if (isFrameOffsetLegal(MI, FPOffset))
551  return false;
552  }
553  // If we can reference via the stack pointer, try that.
554  // FIXME: This (and the code that resolves the references) can be improved
555  // to only disallow SP relative references in the live range of
556  // the VLA(s). In practice, it's unclear how much difference that
557  // would make, but it may be worth doing.
558  if (!MFI->hasVarSizedObjects() && isFrameOffsetLegal(MI, Offset))
559  return false;
560 
561  // The offset likely isn't legal, we want to allocate a virtual base register.
562  return true;
563 }
564 
565 /// materializeFrameBaseRegister - Insert defining instruction(s) for BaseReg to
566 /// be a pointer to FrameIdx at the beginning of the basic block.
569  unsigned BaseReg, int FrameIdx,
570  int64_t Offset) const {
572  unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri :
573  (AFI->isThumb1OnlyFunction() ? ARM::tADDrSPi : ARM::t2ADDri);
574 
576  DebugLoc DL; // Defaults to "unknown"
577  if (Ins != MBB->end())
578  DL = Ins->getDebugLoc();
579 
580  const MachineFunction &MF = *MBB->getParent();
582  const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
583  const MCInstrDesc &MCID = TII.get(ADDriOpc);
584  MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
585 
586  MachineInstrBuilder MIB = AddDefaultPred(BuildMI(*MBB, Ins, DL, MCID, BaseReg)
587  .addFrameIndex(FrameIdx).addImm(Offset));
588 
589  if (!AFI->isThumb1OnlyFunction())
590  AddDefaultCC(MIB);
591 }
592 
593 void
595  unsigned BaseReg, int64_t Offset) const {
596  MachineInstr &MI = *I;
597  MachineBasicBlock &MBB = *MI.getParent();
598  MachineFunction &MF = *MBB.getParent();
599  const ARMBaseInstrInfo &TII =
600  *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
602  int Off = Offset; // ARM doesn't need the general 64-bit offsets
603  unsigned i = 0;
604 
605  assert(!AFI->isThumb1OnlyFunction() &&
606  "This resolveFrameIndex does not support Thumb1!");
607 
608  while (!MI.getOperand(i).isFI()) {
609  ++i;
610  assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
611  }
612  bool Done = false;
613  if (!AFI->isThumbFunction())
614  Done = rewriteARMFrameIndex(MI, i, BaseReg, Off, TII);
615  else {
616  assert(AFI->isThumb2Function());
617  Done = rewriteT2FrameIndex(MI, i, BaseReg, Off, TII);
618  }
619  assert (Done && "Unable to resolve frame index!");
620  (void)Done;
621 }
622 
624  int64_t Offset) const {
625  const MCInstrDesc &Desc = MI->getDesc();
626  unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
627  unsigned i = 0;
628 
629  while (!MI->getOperand(i).isFI()) {
630  ++i;
631  assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
632  }
633 
634  // AddrMode4 and AddrMode6 cannot handle any offset.
635  if (AddrMode == ARMII::AddrMode4 || AddrMode == ARMII::AddrMode6)
636  return Offset == 0;
637 
638  unsigned NumBits = 0;
639  unsigned Scale = 1;
640  bool isSigned = true;
641  switch (AddrMode) {
644  // i8 supports only negative, and i12 supports only positive, so
645  // based on Offset sign, consider the appropriate instruction
646  Scale = 1;
647  if (Offset < 0) {
648  NumBits = 8;
649  Offset = -Offset;
650  } else {
651  NumBits = 12;
652  }
653  break;
654  case ARMII::AddrMode5:
655  // VFP address mode.
656  NumBits = 8;
657  Scale = 4;
658  break;
659  case ARMII::AddrMode_i12:
660  case ARMII::AddrMode2:
661  NumBits = 12;
662  break;
663  case ARMII::AddrMode3:
664  NumBits = 8;
665  break;
666  case ARMII::AddrModeT1_s:
667  NumBits = 5;
668  Scale = 4;
669  isSigned = false;
670  break;
671  default:
672  llvm_unreachable("Unsupported addressing mode!");
673  }
674 
675  Offset += getFrameIndexInstrOffset(MI, i);
676  // Make sure the offset is encodable for instructions that scale the
677  // immediate.
678  if ((Offset & (Scale-1)) != 0)
679  return false;
680 
681  if (isSigned && Offset < 0)
682  Offset = -Offset;
683 
684  unsigned Mask = (1 << NumBits) - 1;
685  if ((unsigned)Offset <= Mask * Scale)
686  return true;
687 
688  return false;
689 }
690 
691 void
693  int SPAdj, unsigned FIOperandNum,
694  RegScavenger *RS) const {
695  MachineInstr &MI = *II;
696  MachineBasicBlock &MBB = *MI.getParent();
697  MachineFunction &MF = *MBB.getParent();
698  const ARMBaseInstrInfo &TII =
699  *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
700  const ARMFrameLowering *TFI =
701  static_cast<const ARMFrameLowering*>(MF.getTarget().getFrameLowering());
703  assert(!AFI->isThumb1OnlyFunction() &&
704  "This eliminateFrameIndex does not support Thumb1!");
705  int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
706  unsigned FrameReg;
707 
708  int Offset = TFI->ResolveFrameIndexReference(MF, FrameIndex, FrameReg, SPAdj);
709 
710  // PEI::scavengeFrameVirtualRegs() cannot accurately track SPAdj because the
711  // call frame setup/destroy instructions have already been eliminated. That
712  // means the stack pointer cannot be used to access the emergency spill slot
713  // when !hasReservedCallFrame().
714 #ifndef NDEBUG
715  if (RS && FrameReg == ARM::SP && RS->isScavengingFrameIndex(FrameIndex)){
716  assert(TFI->hasReservedCallFrame(MF) &&
717  "Cannot use SP to access the emergency spill slot in "
718  "functions without a reserved call frame");
719  assert(!MF.getFrameInfo()->hasVarSizedObjects() &&
720  "Cannot use SP to access the emergency spill slot in "
721  "functions with variable sized frame objects");
722  }
723 #endif // NDEBUG
724 
725  assert(!MI.isDebugValue() && "DBG_VALUEs should be handled in target-independent code");
726 
727  // Modify MI as necessary to handle as much of 'Offset' as possible
728  bool Done = false;
729  if (!AFI->isThumbFunction())
730  Done = rewriteARMFrameIndex(MI, FIOperandNum, FrameReg, Offset, TII);
731  else {
732  assert(AFI->isThumb2Function());
733  Done = rewriteT2FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII);
734  }
735  if (Done)
736  return;
737 
738  // If we get here, the immediate doesn't fit into the instruction. We folded
739  // as much as possible above, handle the rest, providing a register that is
740  // SP+LargeImm.
741  assert((Offset ||
744  "This code isn't needed if offset already handled!");
745 
746  unsigned ScratchReg = 0;
747  int PIdx = MI.findFirstPredOperandIdx();
748  ARMCC::CondCodes Pred = (PIdx == -1)
750  unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
751  if (Offset == 0)
752  // Must be addrmode4/6.
753  MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false, false, false);
754  else {
755  ScratchReg = MF.getRegInfo().createVirtualRegister(&ARM::GPRRegClass);
756  if (!AFI->isThumbFunction())
757  emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
758  Offset, Pred, PredReg, TII);
759  else {
760  assert(AFI->isThumb2Function());
761  emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
762  Offset, Pred, PredReg, TII);
763  }
764  // Update the original instruction to use the scratch register.
765  MI.getOperand(FIOperandNum).ChangeToRegister(ScratchReg, false, false,true);
766  }
767 }
unsigned getFrameRegister(const MachineFunction &MF) const
bool hasPhys(unsigned virtReg) const
returns true if the specified virtual register is mapped to a physical register
Definition: VirtRegMap.h:92
unsigned getStackAlignment() const
void push_back(const T &Elt)
Definition: SmallVector.h:236
const MachineFunction * getParent() const
BitVector & set()
Definition: BitVector.h:236
The machine constant pool.
void resolveFrameIndex(MachineBasicBlock::iterator I, unsigned BaseReg, int64_t Offset) const
bool hasReservedCallFrame(const MachineFunction &MF) const
LLVMContext & getContext() const
Definition: Function.cpp:167
static unsigned char getAM3Offset(unsigned AM3Opc)
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
ARMBaseRegisterInfo(const ARMSubtarget &STI)
void ChangeToRegister(unsigned Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isDebug=false)
bool needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const
virtual void eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=NULL) const
unsigned createVirtualRegister(const TargetRegisterClass *RegClass)
iterator end() const
Definition: ArrayRef.h:98
static bool isVirtualRegister(unsigned Reg)
void emitT2RegPlusImmediate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, DebugLoc dl, unsigned DestReg, unsigned BaseReg, int NumBytes, ARMCC::CondCodes Pred, unsigned PredReg, const ARMBaseInstrInfo &TII, unsigned MIFlags=0)
int ResolveFrameIndexReference(const MachineFunction &MF, int FI, unsigned &FrameReg, int SPAdj) const
const MCInstrDesc & getDesc() const
Definition: MachineInstr.h:257
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:208
F(f)
const Function * getFunction() const
bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const
Return true if the attribute exists at the given index.
Definition: Attributes.cpp:818
virtual bool trackLivenessAfterRegAlloc(const MachineFunction &MF) const
bool canReserveReg(unsigned PhysReg) const
const TargetRegisterClass * getPointerRegClass(const MachineFunction &MF, unsigned Kind=0) const
CallingConv::ID getCallingConv() const
Definition: Function.h:161
virtual bool requiresRegisterScavenging(const MachineFunction &MF) const
Code Generation virtual methods...
unsigned getMaxAlignment() const
bool isR9Reserved() const
Definition: ARMSubtarget.h:327
int64_t getLocalFrameSize() const
getLocalFrameSize - Get the size of the local object blob.
BitVector getReservedRegs(const MachineFunction &MF) const
static const MachineInstrBuilder & AddDefaultPred(const MachineInstrBuilder &MIB)
static unsigned getPairedGPR(unsigned Reg, bool Odd, const MCRegisterInfo *RI)
bool hasVFP3() const
Definition: ARMSubtarget.h:265
const TargetRegisterClass * getLargestLegalSuperClass(const TargetRegisterClass *RC) const
const HexagonInstrInfo * TII
std::pair< unsigned, unsigned > getRegAllocationHint(unsigned Reg) const
virtual bool hasFP(const MachineFunction &MF) const =0
#define llvm_unreachable(msg)
const ARMSubtarget & STI
bool DisableFramePointerElim(const MachineFunction &MF) const
unsigned getRegPressureLimit(const TargetRegisterClass *RC, MachineFunction &MF) const
bool rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx, unsigned FrameReg, int &Offset, const ARMBaseInstrInfo &TII)
Abstract Stack Frame Information.
bool isFrameAddressTaken() const
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
bool rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx, unsigned FrameReg, int &Offset, const ARMBaseInstrInfo &TII)
void setRegAllocationHint(unsigned Reg, unsigned Type, unsigned PrefReg)
const MachineInstrBuilder & addImm(int64_t Val) const
virtual void getRegAllocationHints(unsigned VirtReg, ArrayRef< MCPhysReg > Order, SmallVectorImpl< MCPhysReg > &Hints, const MachineFunction &MF, const VirtRegMap *VRM=0) const
unsigned getNumOperands() const
Definition: MachineInstr.h:265
virtual bool avoidWriteAfterWrite(const TargetRegisterClass *RC) const
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
int getOpcode() const
Definition: MachineInstr.h:261
unsigned getLocalFrameMaxAlign() const
int64_t getImm() const
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:109
const TargetRegisterClass * constrainRegClass(unsigned Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
virtual bool requiresFrameIndexScavenging(const MachineFunction &MF) const
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:119
bool isDebugValue() const
Definition: MachineInstr.h:639
bool isTargetIOS() const
Definition: ARMSubtarget.h:302
unsigned getDefRegState(bool B)
bundle_iterator< MachineInstr, instr_iterator > iterator
const uint32_t * getCallPreservedMask(CallingConv::ID) const
void UpdateRegAllocHint(unsigned Reg, unsigned NewReg, MachineFunction &MF) const
LLVM Constant Representation.
Definition: Constant.h:41
bool isReserved(unsigned PhysReg) const
static unsigned char getAM5Offset(unsigned AM5Opc)
bool isMClass() const
Definition: ARMSubtarget.h:323
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:267
unsigned getSubReg(unsigned Reg, unsigned Idx) const
Returns the physical register number of sub-register "Index" for physical register RegNo...
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
iterator begin() const
Definition: ArrayRef.h:97
MachineConstantPool * getConstantPool()
MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL, const MCInstrDesc &MCID)
const TargetRegisterClass *const * sc_iterator
virtual const TargetFrameLowering * getFrameLowering() const
int64_t getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const
void emitARMRegPlusImmediate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, DebugLoc dl, unsigned DestReg, unsigned BaseReg, int NumBytes, ARMCC::CondCodes Pred, unsigned PredReg, const ARMBaseInstrInfo &TII, unsigned MIFlags=0)
const MCInstrDesc & get(unsigned Opcode) const
Definition: MCInstrInfo.h:48
virtual bool hasReservedCallFrame(const MachineFunction &MF) const
bool canRealignStack(const MachineFunction &MF) const
bool needsStackRealignment(const MachineFunction &MF) const
bool isScavengingFrameIndex(int FI) const
Query whether a frame index is a scavenging frame index.
const TargetRegisterClass * getCrossCopyRegClass(const TargetRegisterClass *RC) const
static AddrOpc getAM2Op(unsigned AM2Opc)
virtual const TargetInstrInfo * getInstrInfo() const
static const MachineInstrBuilder & AddDefaultCC(const MachineInstrBuilder &MIB)
#define R6(n)
static unsigned getAM2Offset(unsigned AM2Opc)
bool test(unsigned Idx) const
Definition: BitVector.h:337
const uint32_t * getNoPreservedMask() const
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:492
MachineFrameInfo * getFrameInfo()
AttributeSet getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:170
const uint16_t * getCalleeSavedRegs(const MachineFunction *MF=0) const
Code Generation virtual methods...
bool hasBasePointer(const MachineFunction &MF) const
AddrMode
ARM Addressing Modes.
Definition: ARMBaseInfo.h:234
void getRegAllocationHints(unsigned VirtReg, ArrayRef< MCPhysReg > Order, SmallVectorImpl< MCPhysReg > &Hints, const MachineFunction &MF, const VirtRegMap *VRM) const
static AddrOpc getAM3Op(unsigned AM3Opc)
int findFirstPredOperandIdx() const
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:200
MachineRegisterInfo & getRegInfo()
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:241
#define I(x, y, z)
Definition: MD5.cpp:54
const TargetMachine & getTarget() const
bool hasVarSizedObjects() const
static AddrOpc getAM5Op(unsigned AM5Opc)
virtual void emitLoadConstPool(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, DebugLoc dl, unsigned DestReg, unsigned SubIdx, int Val, ARMCC::CondCodes Pred=ARMCC::AL, unsigned PredReg=0, unsigned MIFlags=MachineInstr::NoFlags) const
unsigned getReg() const
getReg - Returns the register number.
StringRef getValueAsString() const
Return the attribute's value as a string. This requires the attribute to be a string attribute...
Definition: Attributes.cpp:127
bool isLikeA9() const
Definition: ARMSubtarget.h:259
bool cannotEliminateFrame(const MachineFunction &MF) const
const uint32_t * getThisReturnPreservedMask(CallingConv::ID) const
virtual bool requiresVirtualBaseRegisters(const MachineFunction &MF) const
bool isFrameOffsetLegal(const MachineInstr *MI, int64_t Offset) const
bool isAAPCS_ABI() const
Definition: ARMSubtarget.h:317
unsigned getPhys(unsigned virtReg) const
returns the physical register mapped to the specified virtual register
Definition: VirtRegMap.h:98
const TargetRegisterClass * getRegClass(const MCInstrDesc &TID, unsigned OpNum, const TargetRegisterInfo *TRI, const MachineFunction &MF) const
const MCRegisterInfo & MRI
bool hasD16() const
Definition: ARMSubtarget.h:298
void materializeFrameBaseRegister(MachineBasicBlock *MBB, unsigned BaseReg, int FrameIdx, int64_t Offset) const
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
sc_iterator getSuperClasses() const
unsigned getConstantPoolIndex(const Constant *C, unsigned Alignment)
DebugLoc getDebugLoc() const
Definition: MachineInstr.h:244
unsigned FramePtr
FramePtr - ARM physical register used as frame ptr.