[ORC] Add std::tuple support to SimplePackedSerialization.
[llvm-project.git] / llvm / lib / Target / ARM / ARMBaseRegisterInfo.cpp
blob5b2a1f40703b50d2220e05eef85d6435c2092743
1 //===-- ARMBaseRegisterInfo.cpp - ARM Register Information ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the base ARM implementation of TargetRegisterInfo class.
11 //===----------------------------------------------------------------------===//
13 #include "ARMBaseRegisterInfo.h"
14 #include "ARM.h"
15 #include "ARMBaseInstrInfo.h"
16 #include "ARMFrameLowering.h"
17 #include "ARMMachineFunctionInfo.h"
18 #include "ARMSubtarget.h"
19 #include "MCTargetDesc/ARMAddressingModes.h"
20 #include "MCTargetDesc/ARMBaseInfo.h"
21 #include "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/CodeGen/MachineBasicBlock.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineOperand.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/CodeGen/RegisterScavenging.h"
33 #include "llvm/CodeGen/TargetInstrInfo.h"
34 #include "llvm/CodeGen/TargetRegisterInfo.h"
35 #include "llvm/CodeGen/VirtRegMap.h"
36 #include "llvm/IR/Attributes.h"
37 #include "llvm/IR/Constants.h"
38 #include "llvm/IR/DebugLoc.h"
39 #include "llvm/IR/Function.h"
40 #include "llvm/IR/Type.h"
41 #include "llvm/MC/MCInstrDesc.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Support/ErrorHandling.h"
44 #include "llvm/Support/raw_ostream.h"
45 #include "llvm/Target/TargetMachine.h"
46 #include "llvm/Target/TargetOptions.h"
47 #include <cassert>
48 #include <utility>
50 #define DEBUG_TYPE "arm-register-info"
52 #define GET_REGINFO_TARGET_DESC
53 #include "ARMGenRegisterInfo.inc"
55 using namespace llvm;
57 ARMBaseRegisterInfo::ARMBaseRegisterInfo()
58 : ARMGenRegisterInfo(ARM::LR, 0, 0, ARM::PC) {
59 ARM_MC::initLLVMToCVRegMapping(this);
62 const MCPhysReg*
63 ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
64 const ARMSubtarget &STI = MF->getSubtarget<ARMSubtarget>();
65 bool UseSplitPush = STI.splitFramePushPop(*MF);
66 const MCPhysReg *RegList =
67 STI.isTargetDarwin()
68 ? CSR_iOS_SaveList
69 : (UseSplitPush ? CSR_AAPCS_SplitPush_SaveList : CSR_AAPCS_SaveList);
71 const Function &F = MF->getFunction();
72 if (F.getCallingConv() == CallingConv::GHC) {
73 // GHC set of callee saved regs is empty as all those regs are
74 // used for passing STG regs around
75 return CSR_NoRegs_SaveList;
76 } else if (F.getCallingConv() == CallingConv::CFGuard_Check) {
77 return CSR_Win_AAPCS_CFGuard_Check_SaveList;
78 } else if (F.getCallingConv() == CallingConv::SwiftTail) {
79 return STI.isTargetDarwin()
80 ? CSR_iOS_SwiftTail_SaveList
81 : (UseSplitPush ? CSR_AAPCS_SplitPush_SwiftTail_SaveList
82 : CSR_AAPCS_SwiftTail_SaveList);
83 } else if (F.hasFnAttribute("interrupt")) {
84 if (STI.isMClass()) {
85 // M-class CPUs have hardware which saves the registers needed to allow a
86 // function conforming to the AAPCS to function as a handler.
87 return UseSplitPush ? CSR_AAPCS_SplitPush_SaveList : CSR_AAPCS_SaveList;
88 } else if (F.getFnAttribute("interrupt").getValueAsString() == "FIQ") {
89 // Fast interrupt mode gives the handler a private copy of R8-R14, so less
90 // need to be saved to restore user-mode state.
91 return CSR_FIQ_SaveList;
92 } else {
93 // Generally only R13-R14 (i.e. SP, LR) are automatically preserved by
94 // exception handling.
95 return CSR_GenericInt_SaveList;
99 if (STI.getTargetLowering()->supportSwiftError() &&
100 F.getAttributes().hasAttrSomewhere(Attribute::SwiftError)) {
101 if (STI.isTargetDarwin())
102 return CSR_iOS_SwiftError_SaveList;
104 return UseSplitPush ? CSR_AAPCS_SplitPush_SwiftError_SaveList :
105 CSR_AAPCS_SwiftError_SaveList;
108 if (STI.isTargetDarwin() && F.getCallingConv() == CallingConv::CXX_FAST_TLS)
109 return MF->getInfo<ARMFunctionInfo>()->isSplitCSR()
110 ? CSR_iOS_CXX_TLS_PE_SaveList
111 : CSR_iOS_CXX_TLS_SaveList;
112 return RegList;
115 const MCPhysReg *ARMBaseRegisterInfo::getCalleeSavedRegsViaCopy(
116 const MachineFunction *MF) const {
117 assert(MF && "Invalid MachineFunction pointer.");
118 if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS &&
119 MF->getInfo<ARMFunctionInfo>()->isSplitCSR())
120 return CSR_iOS_CXX_TLS_ViaCopy_SaveList;
121 return nullptr;
124 const uint32_t *
125 ARMBaseRegisterInfo::getCallPreservedMask(const MachineFunction &MF,
126 CallingConv::ID CC) const {
127 const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
128 if (CC == CallingConv::GHC)
129 // This is academic because all GHC calls are (supposed to be) tail calls
130 return CSR_NoRegs_RegMask;
131 if (CC == CallingConv::CFGuard_Check)
132 return CSR_Win_AAPCS_CFGuard_Check_RegMask;
133 if (CC == CallingConv::SwiftTail) {
134 return STI.isTargetDarwin() ? CSR_iOS_SwiftTail_RegMask
135 : CSR_AAPCS_SwiftTail_RegMask;
137 if (STI.getTargetLowering()->supportSwiftError() &&
138 MF.getFunction().getAttributes().hasAttrSomewhere(Attribute::SwiftError))
139 return STI.isTargetDarwin() ? CSR_iOS_SwiftError_RegMask
140 : CSR_AAPCS_SwiftError_RegMask;
142 if (STI.isTargetDarwin() && CC == CallingConv::CXX_FAST_TLS)
143 return CSR_iOS_CXX_TLS_RegMask;
144 return STI.isTargetDarwin() ? CSR_iOS_RegMask : CSR_AAPCS_RegMask;
147 const uint32_t*
148 ARMBaseRegisterInfo::getNoPreservedMask() const {
149 return CSR_NoRegs_RegMask;
152 const uint32_t *
153 ARMBaseRegisterInfo::getTLSCallPreservedMask(const MachineFunction &MF) const {
154 assert(MF.getSubtarget<ARMSubtarget>().isTargetDarwin() &&
155 "only know about special TLS call on Darwin");
156 return CSR_iOS_TLSCall_RegMask;
159 const uint32_t *
160 ARMBaseRegisterInfo::getSjLjDispatchPreservedMask(const MachineFunction &MF) const {
161 const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
162 if (!STI.useSoftFloat() && STI.hasVFP2Base() && !STI.isThumb1Only())
163 return CSR_NoRegs_RegMask;
164 else
165 return CSR_FPRegs_RegMask;
168 const uint32_t *
169 ARMBaseRegisterInfo::getThisReturnPreservedMask(const MachineFunction &MF,
170 CallingConv::ID CC) const {
171 const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
172 // This should return a register mask that is the same as that returned by
173 // getCallPreservedMask but that additionally preserves the register used for
174 // the first i32 argument (which must also be the register used to return a
175 // single i32 return value)
177 // In case that the calling convention does not use the same register for
178 // both or otherwise does not want to enable this optimization, the function
179 // should return NULL
180 if (CC == CallingConv::GHC)
181 // This is academic because all GHC calls are (supposed to be) tail calls
182 return nullptr;
183 return STI.isTargetDarwin() ? CSR_iOS_ThisReturn_RegMask
184 : CSR_AAPCS_ThisReturn_RegMask;
187 ArrayRef<MCPhysReg> ARMBaseRegisterInfo::getIntraCallClobberedRegs(
188 const MachineFunction *MF) const {
189 static const MCPhysReg IntraCallClobberedRegs[] = {ARM::R12};
190 return ArrayRef<MCPhysReg>(IntraCallClobberedRegs);
193 BitVector ARMBaseRegisterInfo::
194 getReservedRegs(const MachineFunction &MF) const {
195 const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
196 const ARMFrameLowering *TFI = getFrameLowering(MF);
198 // FIXME: avoid re-calculating this every time.
199 BitVector Reserved(getNumRegs());
200 markSuperRegs(Reserved, ARM::SP);
201 markSuperRegs(Reserved, ARM::PC);
202 markSuperRegs(Reserved, ARM::FPSCR);
203 markSuperRegs(Reserved, ARM::APSR_NZCV);
204 if (TFI->hasFP(MF))
205 markSuperRegs(Reserved, STI.getFramePointerReg());
206 if (hasBasePointer(MF))
207 markSuperRegs(Reserved, BasePtr);
208 // Some targets reserve R9.
209 if (STI.isR9Reserved())
210 markSuperRegs(Reserved, ARM::R9);
211 // Reserve D16-D31 if the subtarget doesn't support them.
212 if (!STI.hasD32()) {
213 static_assert(ARM::D31 == ARM::D16 + 15, "Register list not consecutive!");
214 for (unsigned R = 0; R < 16; ++R)
215 markSuperRegs(Reserved, ARM::D16 + R);
217 const TargetRegisterClass &RC = ARM::GPRPairRegClass;
218 for (unsigned Reg : RC)
219 for (MCSubRegIterator SI(Reg, this); SI.isValid(); ++SI)
220 if (Reserved.test(*SI))
221 markSuperRegs(Reserved, Reg);
222 // For v8.1m architecture
223 markSuperRegs(Reserved, ARM::ZR);
225 assert(checkAllSuperRegsMarked(Reserved));
226 return Reserved;
229 bool ARMBaseRegisterInfo::
230 isAsmClobberable(const MachineFunction &MF, MCRegister PhysReg) const {
231 return !getReservedRegs(MF).test(PhysReg);
234 bool ARMBaseRegisterInfo::isInlineAsmReadOnlyReg(const MachineFunction &MF,
235 unsigned PhysReg) const {
236 const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
237 const ARMFrameLowering *TFI = getFrameLowering(MF);
239 BitVector Reserved(getNumRegs());
240 markSuperRegs(Reserved, ARM::PC);
241 if (TFI->hasFP(MF))
242 markSuperRegs(Reserved, STI.getFramePointerReg());
243 if (hasBasePointer(MF))
244 markSuperRegs(Reserved, BasePtr);
245 assert(checkAllSuperRegsMarked(Reserved));
246 return Reserved.test(PhysReg);
249 const TargetRegisterClass *
250 ARMBaseRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,
251 const MachineFunction &MF) const {
252 const TargetRegisterClass *Super = RC;
253 TargetRegisterClass::sc_iterator I = RC->getSuperClasses();
254 do {
255 switch (Super->getID()) {
256 case ARM::GPRRegClassID:
257 case ARM::SPRRegClassID:
258 case ARM::DPRRegClassID:
259 case ARM::GPRPairRegClassID:
260 return Super;
261 case ARM::QPRRegClassID:
262 case ARM::QQPRRegClassID:
263 case ARM::QQQQPRRegClassID:
264 if (MF.getSubtarget<ARMSubtarget>().hasNEON())
265 return Super;
266 break;
267 case ARM::MQPRRegClassID:
268 case ARM::MQQPRRegClassID:
269 case ARM::MQQQQPRRegClassID:
270 if (MF.getSubtarget<ARMSubtarget>().hasMVEIntegerOps())
271 return Super;
272 break;
274 Super = *I++;
275 } while (Super);
276 return RC;
279 const TargetRegisterClass *
280 ARMBaseRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
281 const {
282 return &ARM::GPRRegClass;
285 const TargetRegisterClass *
286 ARMBaseRegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
287 if (RC == &ARM::CCRRegClass)
288 return &ARM::rGPRRegClass; // Can't copy CCR registers.
289 return RC;
292 unsigned
293 ARMBaseRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
294 MachineFunction &MF) const {
295 const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
296 const ARMFrameLowering *TFI = getFrameLowering(MF);
298 switch (RC->getID()) {
299 default:
300 return 0;
301 case ARM::tGPRRegClassID: {
302 // hasFP ends up calling getMaxCallFrameComputed() which may not be
303 // available when getPressureLimit() is called as part of
304 // ScheduleDAGRRList.
305 bool HasFP = MF.getFrameInfo().isMaxCallFrameSizeComputed()
306 ? TFI->hasFP(MF) : true;
307 return 5 - HasFP;
309 case ARM::GPRRegClassID: {
310 bool HasFP = MF.getFrameInfo().isMaxCallFrameSizeComputed()
311 ? TFI->hasFP(MF) : true;
312 return 10 - HasFP - (STI.isR9Reserved() ? 1 : 0);
314 case ARM::SPRRegClassID: // Currently not used as 'rep' register class.
315 case ARM::DPRRegClassID:
316 return 32 - 10;
320 // Get the other register in a GPRPair.
321 static MCPhysReg getPairedGPR(MCPhysReg Reg, bool Odd,
322 const MCRegisterInfo *RI) {
323 for (MCSuperRegIterator Supers(Reg, RI); Supers.isValid(); ++Supers)
324 if (ARM::GPRPairRegClass.contains(*Supers))
325 return RI->getSubReg(*Supers, Odd ? ARM::gsub_1 : ARM::gsub_0);
326 return 0;
329 // Resolve the RegPairEven / RegPairOdd register allocator hints.
330 bool ARMBaseRegisterInfo::getRegAllocationHints(
331 Register VirtReg, ArrayRef<MCPhysReg> Order,
332 SmallVectorImpl<MCPhysReg> &Hints, const MachineFunction &MF,
333 const VirtRegMap *VRM, const LiveRegMatrix *Matrix) const {
334 const MachineRegisterInfo &MRI = MF.getRegInfo();
335 std::pair<Register, Register> Hint = MRI.getRegAllocationHint(VirtReg);
337 unsigned Odd;
338 switch (Hint.first) {
339 case ARMRI::RegPairEven:
340 Odd = 0;
341 break;
342 case ARMRI::RegPairOdd:
343 Odd = 1;
344 break;
345 case ARMRI::RegLR:
346 TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints, MF, VRM);
347 if (MRI.getRegClass(VirtReg)->contains(ARM::LR))
348 Hints.push_back(ARM::LR);
349 return false;
350 default:
351 return TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints, MF, VRM);
354 // This register should preferably be even (Odd == 0) or odd (Odd == 1).
355 // Check if the other part of the pair has already been assigned, and provide
356 // the paired register as the first hint.
357 Register Paired = Hint.second;
358 if (!Paired)
359 return false;
361 Register PairedPhys;
362 if (Paired.isPhysical()) {
363 PairedPhys = Paired;
364 } else if (VRM && VRM->hasPhys(Paired)) {
365 PairedPhys = getPairedGPR(VRM->getPhys(Paired), Odd, this);
368 // First prefer the paired physreg.
369 if (PairedPhys && is_contained(Order, PairedPhys))
370 Hints.push_back(PairedPhys);
372 // Then prefer even or odd registers.
373 for (MCPhysReg Reg : Order) {
374 if (Reg == PairedPhys || (getEncodingValue(Reg) & 1) != Odd)
375 continue;
376 // Don't provide hints that are paired to a reserved register.
377 MCPhysReg Paired = getPairedGPR(Reg, !Odd, this);
378 if (!Paired || MRI.isReserved(Paired))
379 continue;
380 Hints.push_back(Reg);
382 return false;
385 void ARMBaseRegisterInfo::updateRegAllocHint(Register Reg, Register NewReg,
386 MachineFunction &MF) const {
387 MachineRegisterInfo *MRI = &MF.getRegInfo();
388 std::pair<Register, Register> Hint = MRI->getRegAllocationHint(Reg);
389 if ((Hint.first == ARMRI::RegPairOdd || Hint.first == ARMRI::RegPairEven) &&
390 Hint.second.isVirtual()) {
391 // If 'Reg' is one of the even / odd register pair and it's now changed
392 // (e.g. coalesced) into a different register. The other register of the
393 // pair allocation hint must be updated to reflect the relationship
394 // change.
395 Register OtherReg = Hint.second;
396 Hint = MRI->getRegAllocationHint(OtherReg);
397 // Make sure the pair has not already divorced.
398 if (Hint.second == Reg) {
399 MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
400 if (Register::isVirtualRegister(NewReg))
401 MRI->setRegAllocationHint(NewReg,
402 Hint.first == ARMRI::RegPairOdd
403 ? ARMRI::RegPairEven
404 : ARMRI::RegPairOdd,
405 OtherReg);
410 bool ARMBaseRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
411 const MachineFrameInfo &MFI = MF.getFrameInfo();
412 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
413 const ARMFrameLowering *TFI = getFrameLowering(MF);
415 // If we have stack realignment and VLAs, we have no pointer to use to
416 // access the stack. If we have stack realignment, and a large call frame,
417 // we have no place to allocate the emergency spill slot.
418 if (hasStackRealignment(MF) && !TFI->hasReservedCallFrame(MF))
419 return true;
421 // Thumb has trouble with negative offsets from the FP. Thumb2 has a limited
422 // negative range for ldr/str (255), and Thumb1 is positive offsets only.
424 // It's going to be better to use the SP or Base Pointer instead. When there
425 // are variable sized objects, we can't reference off of the SP, so we
426 // reserve a Base Pointer.
428 // For Thumb2, estimate whether a negative offset from the frame pointer
429 // will be sufficient to reach the whole stack frame. If a function has a
430 // smallish frame, it's less likely to have lots of spills and callee saved
431 // space, so it's all more likely to be within range of the frame pointer.
432 // If it's wrong, the scavenger will still enable access to work, it just
433 // won't be optimal. (We should always be able to reach the emergency
434 // spill slot from the frame pointer.)
435 if (AFI->isThumb2Function() && MFI.hasVarSizedObjects() &&
436 MFI.getLocalFrameSize() >= 128)
437 return true;
438 // For Thumb1, if sp moves, nothing is in range, so force a base pointer.
439 // This is necessary for correctness in cases where we need an emergency
440 // spill slot. (In Thumb1, we can't use a negative offset from the frame
441 // pointer.)
442 if (AFI->isThumb1OnlyFunction() && !TFI->hasReservedCallFrame(MF))
443 return true;
444 return false;
447 bool ARMBaseRegisterInfo::canRealignStack(const MachineFunction &MF) const {
448 const MachineRegisterInfo *MRI = &MF.getRegInfo();
449 const ARMFrameLowering *TFI = getFrameLowering(MF);
450 const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
451 // We can't realign the stack if:
452 // 1. Dynamic stack realignment is explicitly disabled,
453 // 2. There are VLAs in the function and the base pointer is disabled.
454 if (!TargetRegisterInfo::canRealignStack(MF))
455 return false;
456 // Stack realignment requires a frame pointer. If we already started
457 // register allocation with frame pointer elimination, it is too late now.
458 if (!MRI->canReserveReg(STI.getFramePointerReg()))
459 return false;
460 // We may also need a base pointer if there are dynamic allocas or stack
461 // pointer adjustments around calls.
462 if (TFI->hasReservedCallFrame(MF))
463 return true;
464 // A base pointer is required and allowed. Check that it isn't too late to
465 // reserve it.
466 return MRI->canReserveReg(BasePtr);
469 bool ARMBaseRegisterInfo::
470 cannotEliminateFrame(const MachineFunction &MF) const {
471 const MachineFrameInfo &MFI = MF.getFrameInfo();
472 if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI.adjustsStack())
473 return true;
474 return MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken() ||
475 hasStackRealignment(MF);
478 Register
479 ARMBaseRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
480 const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
481 const ARMFrameLowering *TFI = getFrameLowering(MF);
483 if (TFI->hasFP(MF))
484 return STI.getFramePointerReg();
485 return ARM::SP;
488 /// emitLoadConstPool - Emits a load from constpool to materialize the
489 /// specified immediate.
490 void ARMBaseRegisterInfo::emitLoadConstPool(
491 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
492 const DebugLoc &dl, Register DestReg, unsigned SubIdx, int Val,
493 ARMCC::CondCodes Pred, Register PredReg, unsigned MIFlags) const {
494 MachineFunction &MF = *MBB.getParent();
495 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
496 MachineConstantPool *ConstantPool = MF.getConstantPool();
497 const Constant *C =
498 ConstantInt::get(Type::getInt32Ty(MF.getFunction().getContext()), Val);
499 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align(4));
501 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
502 .addReg(DestReg, getDefRegState(true), SubIdx)
503 .addConstantPoolIndex(Idx)
504 .addImm(0)
505 .add(predOps(Pred, PredReg))
506 .setMIFlags(MIFlags);
509 bool ARMBaseRegisterInfo::
510 requiresRegisterScavenging(const MachineFunction &MF) const {
511 return true;
514 bool ARMBaseRegisterInfo::
515 requiresFrameIndexScavenging(const MachineFunction &MF) const {
516 return true;
519 bool ARMBaseRegisterInfo::
520 requiresVirtualBaseRegisters(const MachineFunction &MF) const {
521 return true;
524 int64_t ARMBaseRegisterInfo::
525 getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const {
526 const MCInstrDesc &Desc = MI->getDesc();
527 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
528 int64_t InstrOffs = 0;
529 int Scale = 1;
530 unsigned ImmIdx = 0;
531 switch (AddrMode) {
532 case ARMII::AddrModeT2_i8:
533 case ARMII::AddrModeT2_i12:
534 case ARMII::AddrMode_i12:
535 InstrOffs = MI->getOperand(Idx+1).getImm();
536 Scale = 1;
537 break;
538 case ARMII::AddrMode5: {
539 // VFP address mode.
540 const MachineOperand &OffOp = MI->getOperand(Idx+1);
541 InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm());
542 if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub)
543 InstrOffs = -InstrOffs;
544 Scale = 4;
545 break;
547 case ARMII::AddrMode2:
548 ImmIdx = Idx+2;
549 InstrOffs = ARM_AM::getAM2Offset(MI->getOperand(ImmIdx).getImm());
550 if (ARM_AM::getAM2Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
551 InstrOffs = -InstrOffs;
552 break;
553 case ARMII::AddrMode3:
554 ImmIdx = Idx+2;
555 InstrOffs = ARM_AM::getAM3Offset(MI->getOperand(ImmIdx).getImm());
556 if (ARM_AM::getAM3Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
557 InstrOffs = -InstrOffs;
558 break;
559 case ARMII::AddrModeT1_s:
560 ImmIdx = Idx+1;
561 InstrOffs = MI->getOperand(ImmIdx).getImm();
562 Scale = 4;
563 break;
564 default:
565 llvm_unreachable("Unsupported addressing mode!");
568 return InstrOffs * Scale;
571 /// needsFrameBaseReg - Returns true if the instruction's frame index
572 /// reference would be better served by a base register other than FP
573 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
574 /// references it should create new base registers for.
575 bool ARMBaseRegisterInfo::
576 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
577 for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) {
578 assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
581 // It's the load/store FI references that cause issues, as it can be difficult
582 // to materialize the offset if it won't fit in the literal field. Estimate
583 // based on the size of the local frame and some conservative assumptions
584 // about the rest of the stack frame (note, this is pre-regalloc, so
585 // we don't know everything for certain yet) whether this offset is likely
586 // to be out of range of the immediate. Return true if so.
588 // We only generate virtual base registers for loads and stores, so
589 // return false for everything else.
590 unsigned Opc = MI->getOpcode();
591 switch (Opc) {
592 case ARM::LDRi12: case ARM::LDRH: case ARM::LDRBi12:
593 case ARM::STRi12: case ARM::STRH: case ARM::STRBi12:
594 case ARM::t2LDRi12: case ARM::t2LDRi8:
595 case ARM::t2STRi12: case ARM::t2STRi8:
596 case ARM::VLDRS: case ARM::VLDRD:
597 case ARM::VSTRS: case ARM::VSTRD:
598 case ARM::tSTRspi: case ARM::tLDRspi:
599 break;
600 default:
601 return false;
604 // Without a virtual base register, if the function has variable sized
605 // objects, all fixed-size local references will be via the frame pointer,
606 // Approximate the offset and see if it's legal for the instruction.
607 // Note that the incoming offset is based on the SP value at function entry,
608 // so it'll be negative.
609 MachineFunction &MF = *MI->getParent()->getParent();
610 const ARMFrameLowering *TFI = getFrameLowering(MF);
611 MachineFrameInfo &MFI = MF.getFrameInfo();
612 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
614 // Estimate an offset from the frame pointer.
615 // Conservatively assume all callee-saved registers get pushed. R4-R6
616 // will be earlier than the FP, so we ignore those.
617 // R7, LR
618 int64_t FPOffset = Offset - 8;
619 // ARM and Thumb2 functions also need to consider R8-R11 and D8-D15
620 if (!AFI->isThumbFunction() || !AFI->isThumb1OnlyFunction())
621 FPOffset -= 80;
622 // Estimate an offset from the stack pointer.
623 // The incoming offset is relating to the SP at the start of the function,
624 // but when we access the local it'll be relative to the SP after local
625 // allocation, so adjust our SP-relative offset by that allocation size.
626 Offset += MFI.getLocalFrameSize();
627 // Assume that we'll have at least some spill slots allocated.
628 // FIXME: This is a total SWAG number. We should run some statistics
629 // and pick a real one.
630 Offset += 128; // 128 bytes of spill slots
632 // If there's a frame pointer and the addressing mode allows it, try using it.
633 // The FP is only available if there is no dynamic realignment. We
634 // don't know for sure yet whether we'll need that, so we guess based
635 // on whether there are any local variables that would trigger it.
636 if (TFI->hasFP(MF) &&
637 !((MFI.getLocalFrameMaxAlign() > TFI->getStackAlign()) &&
638 canRealignStack(MF))) {
639 if (isFrameOffsetLegal(MI, getFrameRegister(MF), FPOffset))
640 return false;
642 // If we can reference via the stack pointer, try that.
643 // FIXME: This (and the code that resolves the references) can be improved
644 // to only disallow SP relative references in the live range of
645 // the VLA(s). In practice, it's unclear how much difference that
646 // would make, but it may be worth doing.
647 if (!MFI.hasVarSizedObjects() && isFrameOffsetLegal(MI, ARM::SP, Offset))
648 return false;
650 // The offset likely isn't legal, we want to allocate a virtual base register.
651 return true;
654 /// materializeFrameBaseRegister - Insert defining instruction(s) for BaseReg to
655 /// be a pointer to FrameIdx at the beginning of the basic block.
656 Register
657 ARMBaseRegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB,
658 int FrameIdx,
659 int64_t Offset) const {
660 ARMFunctionInfo *AFI = MBB->getParent()->getInfo<ARMFunctionInfo>();
661 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri :
662 (AFI->isThumb1OnlyFunction() ? ARM::tADDframe : ARM::t2ADDri);
664 MachineBasicBlock::iterator Ins = MBB->begin();
665 DebugLoc DL; // Defaults to "unknown"
666 if (Ins != MBB->end())
667 DL = Ins->getDebugLoc();
669 const MachineFunction &MF = *MBB->getParent();
670 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
671 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
672 const MCInstrDesc &MCID = TII.get(ADDriOpc);
673 Register BaseReg = MRI.createVirtualRegister(&ARM::GPRRegClass);
674 MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
676 MachineInstrBuilder MIB = BuildMI(*MBB, Ins, DL, MCID, BaseReg)
677 .addFrameIndex(FrameIdx).addImm(Offset);
679 if (!AFI->isThumb1OnlyFunction())
680 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
682 return BaseReg;
685 void ARMBaseRegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg,
686 int64_t Offset) const {
687 MachineBasicBlock &MBB = *MI.getParent();
688 MachineFunction &MF = *MBB.getParent();
689 const ARMBaseInstrInfo &TII =
690 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
691 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
692 int Off = Offset; // ARM doesn't need the general 64-bit offsets
693 unsigned i = 0;
695 assert(!AFI->isThumb1OnlyFunction() &&
696 "This resolveFrameIndex does not support Thumb1!");
698 while (!MI.getOperand(i).isFI()) {
699 ++i;
700 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
702 bool Done = false;
703 if (!AFI->isThumbFunction())
704 Done = rewriteARMFrameIndex(MI, i, BaseReg, Off, TII);
705 else {
706 assert(AFI->isThumb2Function());
707 Done = rewriteT2FrameIndex(MI, i, BaseReg, Off, TII, this);
709 assert(Done && "Unable to resolve frame index!");
710 (void)Done;
713 bool ARMBaseRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
714 Register BaseReg,
715 int64_t Offset) const {
716 const MCInstrDesc &Desc = MI->getDesc();
717 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
718 unsigned i = 0;
719 for (; !MI->getOperand(i).isFI(); ++i)
720 assert(i+1 < MI->getNumOperands() && "Instr doesn't have FrameIndex operand!");
722 // AddrMode4 and AddrMode6 cannot handle any offset.
723 if (AddrMode == ARMII::AddrMode4 || AddrMode == ARMII::AddrMode6)
724 return Offset == 0;
726 unsigned NumBits = 0;
727 unsigned Scale = 1;
728 bool isSigned = true;
729 switch (AddrMode) {
730 case ARMII::AddrModeT2_i8:
731 case ARMII::AddrModeT2_i12:
732 // i8 supports only negative, and i12 supports only positive, so
733 // based on Offset sign, consider the appropriate instruction
734 Scale = 1;
735 if (Offset < 0) {
736 NumBits = 8;
737 Offset = -Offset;
738 } else {
739 NumBits = 12;
741 break;
742 case ARMII::AddrMode5:
743 // VFP address mode.
744 NumBits = 8;
745 Scale = 4;
746 break;
747 case ARMII::AddrMode_i12:
748 case ARMII::AddrMode2:
749 NumBits = 12;
750 break;
751 case ARMII::AddrMode3:
752 NumBits = 8;
753 break;
754 case ARMII::AddrModeT1_s:
755 NumBits = (BaseReg == ARM::SP ? 8 : 5);
756 Scale = 4;
757 isSigned = false;
758 break;
759 default:
760 llvm_unreachable("Unsupported addressing mode!");
763 Offset += getFrameIndexInstrOffset(MI, i);
764 // Make sure the offset is encodable for instructions that scale the
765 // immediate.
766 if ((Offset & (Scale-1)) != 0)
767 return false;
769 if (isSigned && Offset < 0)
770 Offset = -Offset;
772 unsigned Mask = (1 << NumBits) - 1;
773 if ((unsigned)Offset <= Mask * Scale)
774 return true;
776 return false;
779 void
780 ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
781 int SPAdj, unsigned FIOperandNum,
782 RegScavenger *RS) const {
783 MachineInstr &MI = *II;
784 MachineBasicBlock &MBB = *MI.getParent();
785 MachineFunction &MF = *MBB.getParent();
786 const ARMBaseInstrInfo &TII =
787 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
788 const ARMFrameLowering *TFI = getFrameLowering(MF);
789 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
790 assert(!AFI->isThumb1OnlyFunction() &&
791 "This eliminateFrameIndex does not support Thumb1!");
792 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
793 Register FrameReg;
795 int Offset = TFI->ResolveFrameIndexReference(MF, FrameIndex, FrameReg, SPAdj);
797 // PEI::scavengeFrameVirtualRegs() cannot accurately track SPAdj because the
798 // call frame setup/destroy instructions have already been eliminated. That
799 // means the stack pointer cannot be used to access the emergency spill slot
800 // when !hasReservedCallFrame().
801 #ifndef NDEBUG
802 if (RS && FrameReg == ARM::SP && RS->isScavengingFrameIndex(FrameIndex)){
803 assert(TFI->hasReservedCallFrame(MF) &&
804 "Cannot use SP to access the emergency spill slot in "
805 "functions without a reserved call frame");
806 assert(!MF.getFrameInfo().hasVarSizedObjects() &&
807 "Cannot use SP to access the emergency spill slot in "
808 "functions with variable sized frame objects");
810 #endif // NDEBUG
812 assert(!MI.isDebugValue() && "DBG_VALUEs should be handled in target-independent code");
814 // Modify MI as necessary to handle as much of 'Offset' as possible
815 bool Done = false;
816 if (!AFI->isThumbFunction())
817 Done = rewriteARMFrameIndex(MI, FIOperandNum, FrameReg, Offset, TII);
818 else {
819 assert(AFI->isThumb2Function());
820 Done = rewriteT2FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII, this);
822 if (Done)
823 return;
825 // If we get here, the immediate doesn't fit into the instruction. We folded
826 // as much as possible above, handle the rest, providing a register that is
827 // SP+LargeImm.
828 assert(
829 (Offset ||
830 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4 ||
831 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode6 ||
832 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrModeT2_i7 ||
833 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrModeT2_i7s2 ||
834 (MI.getDesc().TSFlags & ARMII::AddrModeMask) ==
835 ARMII::AddrModeT2_i7s4) &&
836 "This code isn't needed if offset already handled!");
838 unsigned ScratchReg = 0;
839 int PIdx = MI.findFirstPredOperandIdx();
840 ARMCC::CondCodes Pred = (PIdx == -1)
841 ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
842 Register PredReg = (PIdx == -1) ? Register() : MI.getOperand(PIdx+1).getReg();
844 const MCInstrDesc &MCID = MI.getDesc();
845 const TargetRegisterClass *RegClass =
846 TII.getRegClass(MCID, FIOperandNum, this, *MI.getParent()->getParent());
848 if (Offset == 0 &&
849 (Register::isVirtualRegister(FrameReg) || RegClass->contains(FrameReg)))
850 // Must be addrmode4/6.
851 MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false, false, false);
852 else {
853 ScratchReg = MF.getRegInfo().createVirtualRegister(RegClass);
854 if (!AFI->isThumbFunction())
855 emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
856 Offset, Pred, PredReg, TII);
857 else {
858 assert(AFI->isThumb2Function());
859 emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
860 Offset, Pred, PredReg, TII);
862 // Update the original instruction to use the scratch register.
863 MI.getOperand(FIOperandNum).ChangeToRegister(ScratchReg, false, false,true);
867 bool ARMBaseRegisterInfo::shouldCoalesce(MachineInstr *MI,
868 const TargetRegisterClass *SrcRC,
869 unsigned SubReg,
870 const TargetRegisterClass *DstRC,
871 unsigned DstSubReg,
872 const TargetRegisterClass *NewRC,
873 LiveIntervals &LIS) const {
874 auto MBB = MI->getParent();
875 auto MF = MBB->getParent();
876 const MachineRegisterInfo &MRI = MF->getRegInfo();
877 // If not copying into a sub-register this should be ok because we shouldn't
878 // need to split the reg.
879 if (!DstSubReg)
880 return true;
881 // Small registers don't frequently cause a problem, so we can coalesce them.
882 if (getRegSizeInBits(*NewRC) < 256 && getRegSizeInBits(*DstRC) < 256 &&
883 getRegSizeInBits(*SrcRC) < 256)
884 return true;
886 auto NewRCWeight =
887 MRI.getTargetRegisterInfo()->getRegClassWeight(NewRC);
888 auto SrcRCWeight =
889 MRI.getTargetRegisterInfo()->getRegClassWeight(SrcRC);
890 auto DstRCWeight =
891 MRI.getTargetRegisterInfo()->getRegClassWeight(DstRC);
892 // If the source register class is more expensive than the destination, the
893 // coalescing is probably profitable.
894 if (SrcRCWeight.RegWeight > NewRCWeight.RegWeight)
895 return true;
896 if (DstRCWeight.RegWeight > NewRCWeight.RegWeight)
897 return true;
899 // If the register allocator isn't constrained, we can always allow coalescing
900 // unfortunately we don't know yet if we will be constrained.
901 // The goal of this heuristic is to restrict how many expensive registers
902 // we allow to coalesce in a given basic block.
903 auto AFI = MF->getInfo<ARMFunctionInfo>();
904 auto It = AFI->getCoalescedWeight(MBB);
906 LLVM_DEBUG(dbgs() << "\tARM::shouldCoalesce - Coalesced Weight: "
907 << It->second << "\n");
908 LLVM_DEBUG(dbgs() << "\tARM::shouldCoalesce - Reg Weight: "
909 << NewRCWeight.RegWeight << "\n");
911 // This number is the largest round number that which meets the criteria:
912 // (1) addresses PR18825
913 // (2) generates better code in some test cases (like vldm-shed-a9.ll)
914 // (3) Doesn't regress any test cases (in-tree, test-suite, and SPEC)
915 // In practice the SizeMultiplier will only factor in for straight line code
916 // that uses a lot of NEON vectors, which isn't terribly common.
917 unsigned SizeMultiplier = MBB->size()/100;
918 SizeMultiplier = SizeMultiplier ? SizeMultiplier : 1;
919 if (It->second < NewRCWeight.WeightLimit * SizeMultiplier) {
920 It->second += NewRCWeight.RegWeight;
921 return true;
923 return false;
926 bool ARMBaseRegisterInfo::shouldRewriteCopySrc(const TargetRegisterClass *DefRC,
927 unsigned DefSubReg,
928 const TargetRegisterClass *SrcRC,
929 unsigned SrcSubReg) const {
930 // We can't extract an SPR from an arbitary DPR (as opposed to a DPR_VFP2).
931 if (DefRC == &ARM::SPRRegClass && DefSubReg == 0 &&
932 SrcRC == &ARM::DPRRegClass &&
933 (SrcSubReg == ARM::ssub_0 || SrcSubReg == ARM::ssub_1))
934 return false;
936 return TargetRegisterInfo::shouldRewriteCopySrc(DefRC, DefSubReg,
937 SrcRC, SrcSubReg);