2481176019553bdbf808320447a145fd0ca89610
[oota-llvm.git] / lib / Target / AArch64 / AArch64RegisterInfo.cpp
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 class.
11 //
12 //===----------------------------------------------------------------------===//
13
14
15 #include "AArch64RegisterInfo.h"
16 #include "AArch64FrameLowering.h"
17 #include "AArch64MachineFunctionInfo.h"
18 #include "AArch64TargetMachine.h"
19 #include "MCTargetDesc/AArch64MCTargetDesc.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/RegisterScavenging.h"
24 #include "llvm/ADT/BitVector.h"
25
26 #define GET_REGINFO_TARGET_DESC
27 #include "AArch64GenRegisterInfo.inc"
28
29 using namespace llvm;
30
31 AArch64RegisterInfo::AArch64RegisterInfo(const AArch64InstrInfo &tii,
32                                          const AArch64Subtarget &sti)
33   : AArch64GenRegisterInfo(AArch64::X30), TII(tii) {
34 }
35
36 const uint16_t *
37 AArch64RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
38   return CSR_PCS_SaveList;
39 }
40
41 const uint32_t*
42 AArch64RegisterInfo::getCallPreservedMask(CallingConv::ID) const {
43   return CSR_PCS_RegMask;
44 }
45
46 const uint32_t *AArch64RegisterInfo::getTLSDescCallPreservedMask() const {
47   return TLSDesc_RegMask;
48 }
49
50 const TargetRegisterClass *
51 AArch64RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
52   if (RC == &AArch64::FlagClassRegClass)
53     return &AArch64::GPR64RegClass;
54
55   return RC;
56 }
57
58
59
60 BitVector
61 AArch64RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
62   BitVector Reserved(getNumRegs());
63   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
64
65   Reserved.set(AArch64::XSP);
66   Reserved.set(AArch64::WSP);
67
68   Reserved.set(AArch64::XZR);
69   Reserved.set(AArch64::WZR);
70
71   if (TFI->hasFP(MF)) {
72     Reserved.set(AArch64::X29);
73     Reserved.set(AArch64::W29);
74   }
75
76   return Reserved;
77 }
78
79 void
80 AArch64RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator MBBI,
81                                          int SPAdj,
82                                          unsigned FIOperandNum,
83                                          RegScavenger *RS) const {
84   assert(SPAdj == 0 && "Cannot deal with nonzero SPAdj yet");
85   MachineInstr &MI = *MBBI;
86   MachineBasicBlock &MBB = *MI.getParent();
87   MachineFunction &MF = *MBB.getParent();
88   MachineFrameInfo *MFI = MF.getFrameInfo();
89   const AArch64FrameLowering *TFI =
90     static_cast<const AArch64FrameLowering *>(MF.getTarget().getFrameLowering());
91
92   // In order to work out the base and offset for addressing, the FrameLowering
93   // code needs to know (sometimes) whether the instruction is storing/loading a
94   // callee-saved register, or whether it's a more generic
95   // operation. Fortunately the frame indices are used *only* for that purpose
96   // and are contiguous, so we can check here.
97   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
98   int MinCSFI = 0;
99   int MaxCSFI = -1;
100
101   if (CSI.size()) {
102     MinCSFI = CSI[0].getFrameIdx();
103     MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
104   }
105
106   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
107   bool IsCalleeSaveOp = FrameIndex >= MinCSFI && FrameIndex <= MaxCSFI;
108
109   unsigned FrameReg;
110   int64_t Offset;
111   Offset = TFI->resolveFrameIndexReference(MF, FrameIndex, FrameReg, SPAdj,
112                                            IsCalleeSaveOp);
113
114   Offset += MI.getOperand(FIOperandNum + 1).getImm();
115
116   // DBG_VALUE instructions have no real restrictions so they can be handled
117   // easily.
118   if (MI.isDebugValue()) {
119     MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, /*isDef=*/ false);
120     MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
121     return;
122   }
123
124   int MinOffset, MaxOffset, OffsetScale;
125   if (MI.getOpcode() == AArch64::ADDxxi_lsl0_s) {
126     MinOffset = 0;
127     MaxOffset = 0xfff;
128     OffsetScale = 1;
129   } else {
130     // Load/store of a stack object
131     TII.getAddressConstraints(MI, OffsetScale, MinOffset, MaxOffset);
132   }
133
134   // The frame lowering has told us a base and offset it thinks we should use to
135   // access this variable, but it's still up to us to make sure the values are
136   // legal for the instruction in question.
137   if (Offset % OffsetScale != 0 || Offset < MinOffset || Offset > MaxOffset) {
138     unsigned BaseReg =
139       MF.getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass);
140     emitRegUpdate(MBB, MBBI, MBBI->getDebugLoc(), TII,
141                   BaseReg, FrameReg, BaseReg, Offset);
142     FrameReg = BaseReg;
143     Offset = 0;
144   }
145
146   // Negative offsets are expected if we address from FP, but for
147   // now this checks nothing has gone horribly wrong.
148   assert(Offset >= 0 && "Unexpected negative offset from SP");
149
150   MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false, false, true);
151   MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset / OffsetScale);
152 }
153
154 void
155 AArch64RegisterInfo::eliminateCallFramePseudoInstr(MachineFunction &MF,
156                                          MachineBasicBlock &MBB,
157                                          MachineBasicBlock::iterator MI) const {
158   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
159   DebugLoc dl = MI->getDebugLoc();
160   int Opcode = MI->getOpcode();
161   bool IsDestroy = Opcode == TII.getCallFrameDestroyOpcode();
162   uint64_t CalleePopAmount = IsDestroy ? MI->getOperand(1).getImm() : 0;
163
164   if (!TFI->hasReservedCallFrame(MF)) {
165     unsigned Align = TFI->getStackAlignment();
166
167     uint64_t Amount = MI->getOperand(0).getImm();
168     Amount = (Amount + Align - 1)/Align * Align;
169     if (!IsDestroy) Amount = -Amount;
170
171     // N.b. if CalleePopAmount is valid but zero (i.e. callee would pop, but it
172     // doesn't have to pop anything), then the first operand will be zero too so
173     // this adjustment is a no-op.
174     if (CalleePopAmount == 0) {
175       // FIXME: in-function stack adjustment for calls is limited to 12-bits
176       // because there's no guaranteed temporary register available. Mostly call
177       // frames will be allocated at the start of a function so this is OK, but
178       // it is a limitation that needs dealing with.
179       assert(abs(Amount) < 0xfff && "call frame too large");
180       emitSPUpdate(MBB, MI, dl, TII, AArch64::NoRegister, Amount);
181     }
182   } else if (CalleePopAmount != 0) {
183     // If the calling convention demands that the callee pops arguments from the
184     // stack, we want to add it back if we have a reserved call frame.
185     assert(CalleePopAmount < 0xfff && "call frame too large");
186     emitSPUpdate(MBB, MI, dl, TII, AArch64::NoRegister, -CalleePopAmount);
187   }
188
189   MBB.erase(MI);
190 }
191
192 unsigned
193 AArch64RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
194   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
195
196   if (TFI->hasFP(MF))
197     return AArch64::X29;
198   else
199     return AArch64::XSP;
200 }
201
202 bool
203 AArch64RegisterInfo::useFPForScavengingIndex(const MachineFunction &MF) const {
204   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
205   const AArch64FrameLowering *AFI = static_cast<const AArch64FrameLowering*>(TFI);
206   return AFI->useFPForAddressing(MF);
207 }