1 //===- AArch64RegisterInfo.cpp - AArch64 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 AArch64 implementation of the TargetRegisterInfo
11 // class.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "AArch64RegisterInfo.h"
16 #include "AArch64FrameLowering.h"
17 #include "AArch64InstrInfo.h"
18 #include "AArch64Subtarget.h"
19 #include "MCTargetDesc/AArch64AddressingModes.h"
20 #include "llvm/ADT/BitVector.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/RegisterScavenging.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Target/TargetFrameLowering.h"
30 #include "llvm/Target/TargetOptions.h"
31
32 using namespace llvm;
33
34 #define GET_REGINFO_TARGET_DESC
35 #include "AArch64GenRegisterInfo.inc"
36
37 static cl::opt<bool>
38 ReserveX18("aarch64-reserve-x18", cl::Hidden,
39 cl::desc("Reserve X18, making it unavailable as GPR"));
40
AArch64RegisterInfo(const Triple & TT)41 AArch64RegisterInfo::AArch64RegisterInfo(const Triple &TT)
42 : AArch64GenRegisterInfo(AArch64::LR), TT(TT) {}
43
44 const MCPhysReg *
getCalleeSavedRegs(const MachineFunction * MF) const45 AArch64RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
46 assert(MF && "Invalid MachineFunction pointer.");
47 if (MF->getFunction()->getCallingConv() == CallingConv::GHC)
48 // GHC set of callee saved regs is empty as all those regs are
49 // used for passing STG regs around
50 return CSR_AArch64_NoRegs_SaveList;
51 if (MF->getFunction()->getCallingConv() == CallingConv::AnyReg)
52 return CSR_AArch64_AllRegs_SaveList;
53 else
54 return CSR_AArch64_AAPCS_SaveList;
55 }
56
57 const uint32_t *
getCallPreservedMask(const MachineFunction & MF,CallingConv::ID CC) const58 AArch64RegisterInfo::getCallPreservedMask(const MachineFunction &MF,
59 CallingConv::ID CC) const {
60 if (CC == CallingConv::GHC)
61 // This is academic becase all GHC calls are (supposed to be) tail calls
62 return CSR_AArch64_NoRegs_RegMask;
63 if (CC == CallingConv::AnyReg)
64 return CSR_AArch64_AllRegs_RegMask;
65 else
66 return CSR_AArch64_AAPCS_RegMask;
67 }
68
getTLSCallPreservedMask() const69 const uint32_t *AArch64RegisterInfo::getTLSCallPreservedMask() const {
70 if (TT.isOSDarwin())
71 return CSR_AArch64_TLS_Darwin_RegMask;
72
73 assert(TT.isOSBinFormatELF() && "only expect Darwin or ELF TLS");
74 return CSR_AArch64_TLS_ELF_RegMask;
75 }
76
77 const uint32_t *
getThisReturnPreservedMask(const MachineFunction & MF,CallingConv::ID CC) const78 AArch64RegisterInfo::getThisReturnPreservedMask(const MachineFunction &MF,
79 CallingConv::ID CC) const {
80 // This should return a register mask that is the same as that returned by
81 // getCallPreservedMask but that additionally preserves the register used for
82 // the first i64 argument (which must also be the register used to return a
83 // single i64 return value)
84 //
85 // In case that the calling convention does not use the same register for
86 // both, the function should return NULL (does not currently apply)
87 assert(CC != CallingConv::GHC && "should not be GHC calling convention.");
88 return CSR_AArch64_AAPCS_ThisReturn_RegMask;
89 }
90
91 BitVector
getReservedRegs(const MachineFunction & MF) const92 AArch64RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
93 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
94
95 // FIXME: avoid re-calculating this every time.
96 BitVector Reserved(getNumRegs());
97 Reserved.set(AArch64::SP);
98 Reserved.set(AArch64::XZR);
99 Reserved.set(AArch64::WSP);
100 Reserved.set(AArch64::WZR);
101
102 if (TFI->hasFP(MF) || TT.isOSDarwin()) {
103 Reserved.set(AArch64::FP);
104 Reserved.set(AArch64::W29);
105 }
106
107 if (TT.isOSDarwin() || ReserveX18) {
108 Reserved.set(AArch64::X18); // Platform register
109 Reserved.set(AArch64::W18);
110 }
111
112 if (hasBasePointer(MF)) {
113 Reserved.set(AArch64::X19);
114 Reserved.set(AArch64::W19);
115 }
116
117 return Reserved;
118 }
119
isReservedReg(const MachineFunction & MF,unsigned Reg) const120 bool AArch64RegisterInfo::isReservedReg(const MachineFunction &MF,
121 unsigned Reg) const {
122 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
123
124 switch (Reg) {
125 default:
126 break;
127 case AArch64::SP:
128 case AArch64::XZR:
129 case AArch64::WSP:
130 case AArch64::WZR:
131 return true;
132 case AArch64::X18:
133 case AArch64::W18:
134 return TT.isOSDarwin() || ReserveX18;
135 case AArch64::FP:
136 case AArch64::W29:
137 return TFI->hasFP(MF) || TT.isOSDarwin();
138 case AArch64::W19:
139 case AArch64::X19:
140 return hasBasePointer(MF);
141 }
142
143 return false;
144 }
145
146 const TargetRegisterClass *
getPointerRegClass(const MachineFunction & MF,unsigned Kind) const147 AArch64RegisterInfo::getPointerRegClass(const MachineFunction &MF,
148 unsigned Kind) const {
149 return &AArch64::GPR64RegClass;
150 }
151
152 const TargetRegisterClass *
getCrossCopyRegClass(const TargetRegisterClass * RC) const153 AArch64RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
154 if (RC == &AArch64::CCRRegClass)
155 return &AArch64::GPR64RegClass; // Only MSR & MRS copy NZCV.
156 return RC;
157 }
158
getBaseRegister() const159 unsigned AArch64RegisterInfo::getBaseRegister() const { return AArch64::X19; }
160
hasBasePointer(const MachineFunction & MF) const161 bool AArch64RegisterInfo::hasBasePointer(const MachineFunction &MF) const {
162 const MachineFrameInfo *MFI = MF.getFrameInfo();
163
164 // In the presence of variable sized objects, if the fixed stack size is
165 // large enough that referencing from the FP won't result in things being
166 // in range relatively often, we can use a base pointer to allow access
167 // from the other direction like the SP normally works.
168 // Furthermore, if both variable sized objects are present, and the
169 // stack needs to be dynamically re-aligned, the base pointer is the only
170 // reliable way to reference the locals.
171 if (MFI->hasVarSizedObjects()) {
172 if (needsStackRealignment(MF))
173 return true;
174 // Conservatively estimate whether the negative offset from the frame
175 // pointer will be sufficient to reach. If a function has a smallish
176 // frame, it's less likely to have lots of spills and callee saved
177 // space, so it's all more likely to be within range of the frame pointer.
178 // If it's wrong, we'll materialize the constant and still get to the
179 // object; it's just suboptimal. Negative offsets use the unscaled
180 // load/store instructions, which have a 9-bit signed immediate.
181 if (MFI->getLocalFrameSize() < 256)
182 return false;
183 return true;
184 }
185
186 return false;
187 }
188
canRealignStack(const MachineFunction & MF) const189 bool AArch64RegisterInfo::canRealignStack(const MachineFunction &MF) const {
190
191 if (MF.getFunction()->hasFnAttribute("no-realign-stack"))
192 return false;
193
194 return true;
195 }
196
197 // FIXME: share this with other backends with identical implementation?
198 bool
needsStackRealignment(const MachineFunction & MF) const199 AArch64RegisterInfo::needsStackRealignment(const MachineFunction &MF) const {
200 const MachineFrameInfo *MFI = MF.getFrameInfo();
201 const Function *F = MF.getFunction();
202 unsigned StackAlign = MF.getTarget()
203 .getSubtargetImpl(*MF.getFunction())
204 ->getFrameLowering()
205 ->getStackAlignment();
206 bool requiresRealignment =
207 ((MFI->getMaxAlignment() > StackAlign) ||
208 F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
209 Attribute::StackAlignment));
210
211 return requiresRealignment && canRealignStack(MF);
212 }
213
214 unsigned
getFrameRegister(const MachineFunction & MF) const215 AArch64RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
216 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
217
218 return TFI->hasFP(MF) ? AArch64::FP : AArch64::SP;
219 }
220
requiresRegisterScavenging(const MachineFunction & MF) const221 bool AArch64RegisterInfo::requiresRegisterScavenging(
222 const MachineFunction &MF) const {
223 return true;
224 }
225
requiresVirtualBaseRegisters(const MachineFunction & MF) const226 bool AArch64RegisterInfo::requiresVirtualBaseRegisters(
227 const MachineFunction &MF) const {
228 return true;
229 }
230
231 bool
useFPForScavengingIndex(const MachineFunction & MF) const232 AArch64RegisterInfo::useFPForScavengingIndex(const MachineFunction &MF) const {
233 const MachineFrameInfo *MFI = MF.getFrameInfo();
234 // AArch64FrameLowering::resolveFrameIndexReference() can always fall back
235 // to the stack pointer, so only put the emergency spill slot next to the
236 // FP when there's no better way to access it (SP or base pointer).
237 return MFI->hasVarSizedObjects() && !hasBasePointer(MF);
238 }
239
requiresFrameIndexScavenging(const MachineFunction & MF) const240 bool AArch64RegisterInfo::requiresFrameIndexScavenging(
241 const MachineFunction &MF) const {
242 return true;
243 }
244
245 bool
cannotEliminateFrame(const MachineFunction & MF) const246 AArch64RegisterInfo::cannotEliminateFrame(const MachineFunction &MF) const {
247 const MachineFrameInfo *MFI = MF.getFrameInfo();
248 // Only consider eliminating leaf frames.
249 if (MFI->hasCalls() || (MF.getTarget().Options.DisableFramePointerElim(MF) &&
250 MFI->adjustsStack()))
251 return true;
252 return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken();
253 }
254
255 /// needsFrameBaseReg - Returns true if the instruction's frame index
256 /// reference would be better served by a base register other than FP
257 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
258 /// references it should create new base registers for.
needsFrameBaseReg(MachineInstr * MI,int64_t Offset) const259 bool AArch64RegisterInfo::needsFrameBaseReg(MachineInstr *MI,
260 int64_t Offset) const {
261 for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i)
262 assert(i < MI->getNumOperands() &&
263 "Instr doesn't have FrameIndex operand!");
264
265 // It's the load/store FI references that cause issues, as it can be difficult
266 // to materialize the offset if it won't fit in the literal field. Estimate
267 // based on the size of the local frame and some conservative assumptions
268 // about the rest of the stack frame (note, this is pre-regalloc, so
269 // we don't know everything for certain yet) whether this offset is likely
270 // to be out of range of the immediate. Return true if so.
271
272 // We only generate virtual base registers for loads and stores, so
273 // return false for everything else.
274 if (!MI->mayLoad() && !MI->mayStore())
275 return false;
276
277 // Without a virtual base register, if the function has variable sized
278 // objects, all fixed-size local references will be via the frame pointer,
279 // Approximate the offset and see if it's legal for the instruction.
280 // Note that the incoming offset is based on the SP value at function entry,
281 // so it'll be negative.
282 MachineFunction &MF = *MI->getParent()->getParent();
283 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
284 MachineFrameInfo *MFI = MF.getFrameInfo();
285
286 // Estimate an offset from the frame pointer.
287 // Conservatively assume all GPR callee-saved registers get pushed.
288 // FP, LR, X19-X28, D8-D15. 64-bits each.
289 int64_t FPOffset = Offset - 16 * 20;
290 // Estimate an offset from the stack pointer.
291 // The incoming offset is relating to the SP at the start of the function,
292 // but when we access the local it'll be relative to the SP after local
293 // allocation, so adjust our SP-relative offset by that allocation size.
294 Offset += MFI->getLocalFrameSize();
295 // Assume that we'll have at least some spill slots allocated.
296 // FIXME: This is a total SWAG number. We should run some statistics
297 // and pick a real one.
298 Offset += 128; // 128 bytes of spill slots
299
300 // If there is a frame pointer, try using it.
301 // The FP is only available if there is no dynamic realignment. We
302 // don't know for sure yet whether we'll need that, so we guess based
303 // on whether there are any local variables that would trigger it.
304 if (TFI->hasFP(MF) && isFrameOffsetLegal(MI, AArch64::FP, FPOffset))
305 return false;
306
307 // If we can reference via the stack pointer or base pointer, try that.
308 // FIXME: This (and the code that resolves the references) can be improved
309 // to only disallow SP relative references in the live range of
310 // the VLA(s). In practice, it's unclear how much difference that
311 // would make, but it may be worth doing.
312 if (isFrameOffsetLegal(MI, AArch64::SP, Offset))
313 return false;
314
315 // The offset likely isn't legal; we want to allocate a virtual base register.
316 return true;
317 }
318
isFrameOffsetLegal(const MachineInstr * MI,unsigned BaseReg,int64_t Offset) const319 bool AArch64RegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
320 unsigned BaseReg,
321 int64_t Offset) const {
322 assert(Offset <= INT_MAX && "Offset too big to fit in int.");
323 assert(MI && "Unable to get the legal offset for nil instruction.");
324 int SaveOffset = Offset;
325 return isAArch64FrameOffsetLegal(*MI, SaveOffset) & AArch64FrameOffsetIsLegal;
326 }
327
328 /// Insert defining instruction(s) for BaseReg to be a pointer to FrameIdx
329 /// at the beginning of the basic block.
materializeFrameBaseRegister(MachineBasicBlock * MBB,unsigned BaseReg,int FrameIdx,int64_t Offset) const330 void AArch64RegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB,
331 unsigned BaseReg,
332 int FrameIdx,
333 int64_t Offset) const {
334 MachineBasicBlock::iterator Ins = MBB->begin();
335 DebugLoc DL; // Defaults to "unknown"
336 if (Ins != MBB->end())
337 DL = Ins->getDebugLoc();
338 const MachineFunction &MF = *MBB->getParent();
339 const AArch64InstrInfo *TII =
340 MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
341 const MCInstrDesc &MCID = TII->get(AArch64::ADDXri);
342 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
343 MRI.constrainRegClass(BaseReg, TII->getRegClass(MCID, 0, this, MF));
344 unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0);
345
346 BuildMI(*MBB, Ins, DL, MCID, BaseReg)
347 .addFrameIndex(FrameIdx)
348 .addImm(Offset)
349 .addImm(Shifter);
350 }
351
resolveFrameIndex(MachineInstr & MI,unsigned BaseReg,int64_t Offset) const352 void AArch64RegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg,
353 int64_t Offset) const {
354 int Off = Offset; // ARM doesn't need the general 64-bit offsets
355 unsigned i = 0;
356
357 while (!MI.getOperand(i).isFI()) {
358 ++i;
359 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
360 }
361 const MachineFunction *MF = MI.getParent()->getParent();
362 const AArch64InstrInfo *TII =
363 MF->getSubtarget<AArch64Subtarget>().getInstrInfo();
364 bool Done = rewriteAArch64FrameIndex(MI, i, BaseReg, Off, TII);
365 assert(Done && "Unable to resolve frame index!");
366 (void)Done;
367 }
368
eliminateFrameIndex(MachineBasicBlock::iterator II,int SPAdj,unsigned FIOperandNum,RegScavenger * RS) const369 void AArch64RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
370 int SPAdj, unsigned FIOperandNum,
371 RegScavenger *RS) const {
372 assert(SPAdj == 0 && "Unexpected");
373
374 MachineInstr &MI = *II;
375 MachineBasicBlock &MBB = *MI.getParent();
376 MachineFunction &MF = *MBB.getParent();
377 const AArch64InstrInfo *TII =
378 MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
379 const AArch64FrameLowering *TFI = static_cast<const AArch64FrameLowering *>(
380 MF.getSubtarget().getFrameLowering());
381
382 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
383 unsigned FrameReg;
384 int Offset;
385
386 // Special handling of dbg_value, stackmap and patchpoint instructions.
387 if (MI.isDebugValue() || MI.getOpcode() == TargetOpcode::STACKMAP ||
388 MI.getOpcode() == TargetOpcode::PATCHPOINT) {
389 Offset = TFI->resolveFrameIndexReference(MF, FrameIndex, FrameReg,
390 /*PreferFP=*/true);
391 Offset += MI.getOperand(FIOperandNum + 1).getImm();
392 MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false /*isDef*/);
393 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
394 return;
395 }
396
397 // Modify MI as necessary to handle as much of 'Offset' as possible
398 Offset = TFI->resolveFrameIndexReference(MF, FrameIndex, FrameReg);
399 if (rewriteAArch64FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII))
400 return;
401
402 assert((!RS || !RS->isScavengingFrameIndex(FrameIndex)) &&
403 "Emergency spill slot is out of reach");
404
405 // If we get here, the immediate doesn't fit into the instruction. We folded
406 // as much as possible above. Handle the rest, providing a register that is
407 // SP+LargeImm.
408 unsigned ScratchReg =
409 MF.getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass);
410 emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset, TII);
411 MI.getOperand(FIOperandNum).ChangeToRegister(ScratchReg, false, false, true);
412 }
413
414 namespace llvm {
415
getRegPressureLimit(const TargetRegisterClass * RC,MachineFunction & MF) const416 unsigned AArch64RegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
417 MachineFunction &MF) const {
418 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
419
420 switch (RC->getID()) {
421 default:
422 return 0;
423 case AArch64::GPR32RegClassID:
424 case AArch64::GPR32spRegClassID:
425 case AArch64::GPR32allRegClassID:
426 case AArch64::GPR64spRegClassID:
427 case AArch64::GPR64allRegClassID:
428 case AArch64::GPR64RegClassID:
429 case AArch64::GPR32commonRegClassID:
430 case AArch64::GPR64commonRegClassID:
431 return 32 - 1 // XZR/SP
432 - (TFI->hasFP(MF) || TT.isOSDarwin()) // FP
433 - (TT.isOSDarwin() || ReserveX18) // X18 reserved as platform register
434 - hasBasePointer(MF); // X19
435 case AArch64::FPR8RegClassID:
436 case AArch64::FPR16RegClassID:
437 case AArch64::FPR32RegClassID:
438 case AArch64::FPR64RegClassID:
439 case AArch64::FPR128RegClassID:
440 return 32;
441
442 case AArch64::DDRegClassID:
443 case AArch64::DDDRegClassID:
444 case AArch64::DDDDRegClassID:
445 case AArch64::QQRegClassID:
446 case AArch64::QQQRegClassID:
447 case AArch64::QQQQRegClassID:
448 return 32;
449
450 case AArch64::FPR128_loRegClassID:
451 return 16;
452 }
453 }
454
455 } // namespace llvm
456