blob: b2bf4f07789d77cc6afbacdfcdc4e46aa7caf8b8 [file] [log] [blame]
Chris Lattner2cfd52c2009-07-29 20:31:52 +00001//===- ARMBaseRegisterInfo.cpp - ARM Register Information -------*- C++ -*-===//
David Goodwinc140c482009-07-08 17:28:55 +00002//
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 "ARM.h"
15#include "ARMAddressingModes.h"
David Goodwindb5a71a2009-07-08 18:31:39 +000016#include "ARMBaseInstrInfo.h"
David Goodwinc140c482009-07-08 17:28:55 +000017#include "ARMBaseRegisterInfo.h"
18#include "ARMInstrInfo.h"
19#include "ARMMachineFunctionInfo.h"
20#include "ARMSubtarget.h"
21#include "llvm/Constants.h"
22#include "llvm/DerivedTypes.h"
Owen Anderson9adc0ab2009-07-14 23:09:55 +000023#include "llvm/Function.h"
24#include "llvm/LLVMContext.h"
David Goodwinc140c482009-07-08 17:28:55 +000025#include "llvm/CodeGen/MachineConstantPool.h"
26#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
29#include "llvm/CodeGen/MachineLocation.h"
30#include "llvm/CodeGen/MachineRegisterInfo.h"
31#include "llvm/CodeGen/RegisterScavenging.h"
Jim Grosbach3dab2772009-10-27 22:45:39 +000032#include "llvm/Support/Debug.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000033#include "llvm/Support/ErrorHandling.h"
Torok Edwindac237e2009-07-08 20:53:28 +000034#include "llvm/Support/raw_ostream.h"
David Goodwinc140c482009-07-08 17:28:55 +000035#include "llvm/Target/TargetFrameInfo.h"
36#include "llvm/Target/TargetMachine.h"
37#include "llvm/Target/TargetOptions.h"
38#include "llvm/ADT/BitVector.h"
39#include "llvm/ADT/SmallVector.h"
Jim Grosbach18ed9c92009-10-20 20:19:50 +000040#include "llvm/Support/CommandLine.h"
David Goodwinc140c482009-07-08 17:28:55 +000041using namespace llvm;
42
Jim Grosbach18ed9c92009-10-20 20:19:50 +000043static cl::opt<bool>
Jim Grosbacha6a99b42009-10-27 22:52:29 +000044ReuseFrameIndexVals("arm-reuse-frame-index-vals", cl::Hidden, cl::init(true),
Jim Grosbach18ed9c92009-10-20 20:19:50 +000045 cl::desc("Reuse repeated frame index values"));
46
Jim Grosbach3dab2772009-10-27 22:45:39 +000047static cl::opt<bool>
48ARMDynamicStackAlign("arm-dynamic-stack-alignment", cl::Hidden, cl::init(false),
49 cl::desc("Dynamically re-align the stack as needed"));
50
David Goodwinc140c482009-07-08 17:28:55 +000051unsigned ARMBaseRegisterInfo::getRegisterNumbering(unsigned RegEnum,
Evan Cheng8295d992009-07-22 05:55:18 +000052 bool *isSPVFP) {
53 if (isSPVFP)
54 *isSPVFP = false;
David Goodwinc140c482009-07-08 17:28:55 +000055
56 using namespace ARM;
57 switch (RegEnum) {
58 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000059 llvm_unreachable("Unknown ARM register!");
Evan Cheng8295d992009-07-22 05:55:18 +000060 case R0: case D0: case Q0: return 0;
61 case R1: case D1: case Q1: return 1;
62 case R2: case D2: case Q2: return 2;
63 case R3: case D3: case Q3: return 3;
64 case R4: case D4: case Q4: return 4;
65 case R5: case D5: case Q5: return 5;
66 case R6: case D6: case Q6: return 6;
67 case R7: case D7: case Q7: return 7;
68 case R8: case D8: case Q8: return 8;
69 case R9: case D9: case Q9: return 9;
70 case R10: case D10: case Q10: return 10;
71 case R11: case D11: case Q11: return 11;
72 case R12: case D12: case Q12: return 12;
73 case SP: case D13: case Q13: return 13;
74 case LR: case D14: case Q14: return 14;
75 case PC: case D15: case Q15: return 15;
76
77 case D16: return 16;
78 case D17: return 17;
79 case D18: return 18;
80 case D19: return 19;
81 case D20: return 20;
82 case D21: return 21;
83 case D22: return 22;
84 case D23: return 23;
85 case D24: return 24;
86 case D25: return 25;
87 case D26: return 27;
88 case D27: return 27;
89 case D28: return 28;
90 case D29: return 29;
91 case D30: return 30;
92 case D31: return 31;
David Goodwinc140c482009-07-08 17:28:55 +000093
94 case S0: case S1: case S2: case S3:
95 case S4: case S5: case S6: case S7:
96 case S8: case S9: case S10: case S11:
97 case S12: case S13: case S14: case S15:
98 case S16: case S17: case S18: case S19:
99 case S20: case S21: case S22: case S23:
100 case S24: case S25: case S26: case S27:
Evan Cheng8295d992009-07-22 05:55:18 +0000101 case S28: case S29: case S30: case S31: {
102 if (isSPVFP)
103 *isSPVFP = true;
David Goodwinc140c482009-07-08 17:28:55 +0000104 switch (RegEnum) {
105 default: return 0; // Avoid compile time warning.
106 case S0: return 0;
107 case S1: return 1;
108 case S2: return 2;
109 case S3: return 3;
110 case S4: return 4;
111 case S5: return 5;
112 case S6: return 6;
113 case S7: return 7;
114 case S8: return 8;
115 case S9: return 9;
116 case S10: return 10;
117 case S11: return 11;
118 case S12: return 12;
119 case S13: return 13;
120 case S14: return 14;
121 case S15: return 15;
122 case S16: return 16;
123 case S17: return 17;
124 case S18: return 18;
125 case S19: return 19;
126 case S20: return 20;
127 case S21: return 21;
128 case S22: return 22;
129 case S23: return 23;
130 case S24: return 24;
131 case S25: return 25;
132 case S26: return 26;
133 case S27: return 27;
134 case S28: return 28;
135 case S29: return 29;
136 case S30: return 30;
137 case S31: return 31;
138 }
139 }
140 }
141}
142
David Goodwindb5a71a2009-07-08 18:31:39 +0000143ARMBaseRegisterInfo::ARMBaseRegisterInfo(const ARMBaseInstrInfo &tii,
David Goodwinc140c482009-07-08 17:28:55 +0000144 const ARMSubtarget &sti)
145 : ARMGenRegisterInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
146 TII(tii), STI(sti),
147 FramePtr((STI.isTargetDarwin() || STI.isThumb()) ? ARM::R7 : ARM::R11) {
148}
149
150const unsigned*
151ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
152 static const unsigned CalleeSavedRegs[] = {
153 ARM::LR, ARM::R11, ARM::R10, ARM::R9, ARM::R8,
154 ARM::R7, ARM::R6, ARM::R5, ARM::R4,
155
156 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
157 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
158 0
159 };
160
161 static const unsigned DarwinCalleeSavedRegs[] = {
162 // Darwin ABI deviates from ARM standard ABI. R9 is not a callee-saved
163 // register.
164 ARM::LR, ARM::R7, ARM::R6, ARM::R5, ARM::R4,
165 ARM::R11, ARM::R10, ARM::R8,
166
167 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
168 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
169 0
170 };
171 return STI.isTargetDarwin() ? DarwinCalleeSavedRegs : CalleeSavedRegs;
172}
173
174const TargetRegisterClass* const *
175ARMBaseRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
176 static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
Jim Grosbach82b3c2e2009-09-11 20:13:17 +0000177 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
178 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
179 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
David Goodwinc140c482009-07-08 17:28:55 +0000180
Jim Grosbach82b3c2e2009-09-11 20:13:17 +0000181 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
182 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
David Goodwinc140c482009-07-08 17:28:55 +0000183 0
184 };
185
186 static const TargetRegisterClass * const ThumbCalleeSavedRegClasses[] = {
Jim Grosbach82b3c2e2009-09-11 20:13:17 +0000187 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
188 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::tGPRRegClass,
189 &ARM::tGPRRegClass,&ARM::tGPRRegClass,&ARM::tGPRRegClass,
David Goodwinc140c482009-07-08 17:28:55 +0000190
Jim Grosbach82b3c2e2009-09-11 20:13:17 +0000191 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
192 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
David Goodwinc140c482009-07-08 17:28:55 +0000193 0
194 };
195
196 static const TargetRegisterClass * const DarwinCalleeSavedRegClasses[] = {
Jim Grosbach82b3c2e2009-09-11 20:13:17 +0000197 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
198 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
199 &ARM::GPRRegClass, &ARM::GPRRegClass,
David Goodwinc140c482009-07-08 17:28:55 +0000200
Jim Grosbach82b3c2e2009-09-11 20:13:17 +0000201 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
202 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
David Goodwinc140c482009-07-08 17:28:55 +0000203 0
204 };
205
206 static const TargetRegisterClass * const DarwinThumbCalleeSavedRegClasses[] ={
Jim Grosbach82b3c2e2009-09-11 20:13:17 +0000207 &ARM::GPRRegClass, &ARM::tGPRRegClass, &ARM::tGPRRegClass,
208 &ARM::tGPRRegClass, &ARM::tGPRRegClass, &ARM::GPRRegClass,
209 &ARM::GPRRegClass, &ARM::GPRRegClass,
David Goodwinc140c482009-07-08 17:28:55 +0000210
Jim Grosbach82b3c2e2009-09-11 20:13:17 +0000211 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
212 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
David Goodwinc140c482009-07-08 17:28:55 +0000213 0
214 };
215
David Goodwinf1daf7d2009-07-08 23:10:31 +0000216 if (STI.isThumb1Only()) {
David Goodwinc140c482009-07-08 17:28:55 +0000217 return STI.isTargetDarwin()
218 ? DarwinThumbCalleeSavedRegClasses : ThumbCalleeSavedRegClasses;
219 }
220 return STI.isTargetDarwin()
221 ? DarwinCalleeSavedRegClasses : CalleeSavedRegClasses;
222}
223
224BitVector ARMBaseRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
225 // FIXME: avoid re-calculating this everytime.
226 BitVector Reserved(getNumRegs());
227 Reserved.set(ARM::SP);
228 Reserved.set(ARM::PC);
229 if (STI.isTargetDarwin() || hasFP(MF))
230 Reserved.set(FramePtr);
231 // Some targets reserve R9.
232 if (STI.isR9Reserved())
233 Reserved.set(ARM::R9);
234 return Reserved;
235}
236
Chris Lattner2cfd52c2009-07-29 20:31:52 +0000237bool ARMBaseRegisterInfo::isReservedReg(const MachineFunction &MF,
238 unsigned Reg) const {
David Goodwinc140c482009-07-08 17:28:55 +0000239 switch (Reg) {
240 default: break;
241 case ARM::SP:
242 case ARM::PC:
243 return true;
244 case ARM::R7:
245 case ARM::R11:
246 if (FramePtr == Reg && (STI.isTargetDarwin() || hasFP(MF)))
247 return true;
248 break;
249 case ARM::R9:
250 return STI.isR9Reserved();
251 }
252
253 return false;
254}
255
Chris Lattner2cfd52c2009-07-29 20:31:52 +0000256const TargetRegisterClass *
Evan Cheng4f54c122009-10-25 07:53:28 +0000257ARMBaseRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
258 const TargetRegisterClass *B,
259 unsigned SubIdx) const {
260 switch (SubIdx) {
261 default: return 0;
262 case 1:
263 case 2:
264 case 3:
265 case 4:
266 // S sub-registers.
267 if (A->getSize() == 8) {
268 if (A == &ARM::DPR_8RegClass)
269 return A;
270 return &ARM::DPR_VFP2RegClass;
271 }
272
273 assert(A->getSize() == 16 && "Expecting a Q register class!");
274 return &ARM::QPR_VFP2RegClass;
275 case 5:
276 case 6:
277 // D sub-registers.
278 return A;
279 }
280 return 0;
281}
282
283const TargetRegisterClass *
Chris Lattner2cfd52c2009-07-29 20:31:52 +0000284ARMBaseRegisterInfo::getPointerRegClass(unsigned Kind) const {
Jim Grosbache11a8f52009-09-11 19:49:06 +0000285 return ARM::GPRRegisterClass;
David Goodwinc140c482009-07-08 17:28:55 +0000286}
287
288/// getAllocationOrder - Returns the register allocation order for a specified
289/// register class in the form of a pair of TargetRegisterClass iterators.
290std::pair<TargetRegisterClass::iterator,TargetRegisterClass::iterator>
291ARMBaseRegisterInfo::getAllocationOrder(const TargetRegisterClass *RC,
292 unsigned HintType, unsigned HintReg,
293 const MachineFunction &MF) const {
294 // Alternative register allocation orders when favoring even / odd registers
295 // of register pairs.
296
297 // No FP, R9 is available.
298 static const unsigned GPREven1[] = {
299 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8, ARM::R10,
300 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7,
301 ARM::R9, ARM::R11
302 };
303 static const unsigned GPROdd1[] = {
304 ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R9, ARM::R11,
305 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
306 ARM::R8, ARM::R10
307 };
308
309 // FP is R7, R9 is available.
310 static const unsigned GPREven2[] = {
311 ARM::R0, ARM::R2, ARM::R4, ARM::R8, ARM::R10,
312 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6,
313 ARM::R9, ARM::R11
314 };
315 static const unsigned GPROdd2[] = {
316 ARM::R1, ARM::R3, ARM::R5, ARM::R9, ARM::R11,
317 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
318 ARM::R8, ARM::R10
319 };
320
321 // FP is R11, R9 is available.
322 static const unsigned GPREven3[] = {
323 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8,
324 ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7,
325 ARM::R9
326 };
327 static const unsigned GPROdd3[] = {
328 ARM::R1, ARM::R3, ARM::R5, ARM::R6, ARM::R9,
329 ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R7,
330 ARM::R8
331 };
332
333 // No FP, R9 is not available.
334 static const unsigned GPREven4[] = {
335 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R10,
336 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8,
337 ARM::R11
338 };
339 static const unsigned GPROdd4[] = {
340 ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R11,
341 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
342 ARM::R10
343 };
344
345 // FP is R7, R9 is not available.
346 static const unsigned GPREven5[] = {
347 ARM::R0, ARM::R2, ARM::R4, ARM::R10,
348 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6, ARM::R8,
349 ARM::R11
350 };
351 static const unsigned GPROdd5[] = {
352 ARM::R1, ARM::R3, ARM::R5, ARM::R11,
353 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
354 ARM::R10
355 };
356
357 // FP is R11, R9 is not available.
358 static const unsigned GPREven6[] = {
359 ARM::R0, ARM::R2, ARM::R4, ARM::R6,
360 ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8
361 };
362 static const unsigned GPROdd6[] = {
363 ARM::R1, ARM::R3, ARM::R5, ARM::R7,
364 ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8
365 };
366
367
368 if (HintType == ARMRI::RegPairEven) {
369 if (isPhysicalRegister(HintReg) && getRegisterPairEven(HintReg, MF) == 0)
370 // It's no longer possible to fulfill this hint. Return the default
371 // allocation order.
372 return std::make_pair(RC->allocation_order_begin(MF),
373 RC->allocation_order_end(MF));
374
375 if (!STI.isTargetDarwin() && !hasFP(MF)) {
376 if (!STI.isR9Reserved())
377 return std::make_pair(GPREven1,
378 GPREven1 + (sizeof(GPREven1)/sizeof(unsigned)));
379 else
380 return std::make_pair(GPREven4,
381 GPREven4 + (sizeof(GPREven4)/sizeof(unsigned)));
382 } else if (FramePtr == ARM::R7) {
383 if (!STI.isR9Reserved())
384 return std::make_pair(GPREven2,
385 GPREven2 + (sizeof(GPREven2)/sizeof(unsigned)));
386 else
387 return std::make_pair(GPREven5,
388 GPREven5 + (sizeof(GPREven5)/sizeof(unsigned)));
389 } else { // FramePtr == ARM::R11
390 if (!STI.isR9Reserved())
391 return std::make_pair(GPREven3,
392 GPREven3 + (sizeof(GPREven3)/sizeof(unsigned)));
393 else
394 return std::make_pair(GPREven6,
395 GPREven6 + (sizeof(GPREven6)/sizeof(unsigned)));
396 }
397 } else if (HintType == ARMRI::RegPairOdd) {
398 if (isPhysicalRegister(HintReg) && getRegisterPairOdd(HintReg, MF) == 0)
399 // It's no longer possible to fulfill this hint. Return the default
400 // allocation order.
401 return std::make_pair(RC->allocation_order_begin(MF),
402 RC->allocation_order_end(MF));
403
404 if (!STI.isTargetDarwin() && !hasFP(MF)) {
405 if (!STI.isR9Reserved())
406 return std::make_pair(GPROdd1,
407 GPROdd1 + (sizeof(GPROdd1)/sizeof(unsigned)));
408 else
409 return std::make_pair(GPROdd4,
410 GPROdd4 + (sizeof(GPROdd4)/sizeof(unsigned)));
411 } else if (FramePtr == ARM::R7) {
412 if (!STI.isR9Reserved())
413 return std::make_pair(GPROdd2,
414 GPROdd2 + (sizeof(GPROdd2)/sizeof(unsigned)));
415 else
416 return std::make_pair(GPROdd5,
417 GPROdd5 + (sizeof(GPROdd5)/sizeof(unsigned)));
418 } else { // FramePtr == ARM::R11
419 if (!STI.isR9Reserved())
420 return std::make_pair(GPROdd3,
421 GPROdd3 + (sizeof(GPROdd3)/sizeof(unsigned)));
422 else
423 return std::make_pair(GPROdd6,
424 GPROdd6 + (sizeof(GPROdd6)/sizeof(unsigned)));
425 }
426 }
427 return std::make_pair(RC->allocation_order_begin(MF),
428 RC->allocation_order_end(MF));
429}
430
431/// ResolveRegAllocHint - Resolves the specified register allocation hint
432/// to a physical register. Returns the physical register if it is successful.
433unsigned
434ARMBaseRegisterInfo::ResolveRegAllocHint(unsigned Type, unsigned Reg,
435 const MachineFunction &MF) const {
436 if (Reg == 0 || !isPhysicalRegister(Reg))
437 return 0;
438 if (Type == 0)
439 return Reg;
440 else if (Type == (unsigned)ARMRI::RegPairOdd)
441 // Odd register.
442 return getRegisterPairOdd(Reg, MF);
443 else if (Type == (unsigned)ARMRI::RegPairEven)
444 // Even register.
445 return getRegisterPairEven(Reg, MF);
446 return 0;
447}
448
449void
450ARMBaseRegisterInfo::UpdateRegAllocHint(unsigned Reg, unsigned NewReg,
451 MachineFunction &MF) const {
452 MachineRegisterInfo *MRI = &MF.getRegInfo();
453 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg);
454 if ((Hint.first == (unsigned)ARMRI::RegPairOdd ||
455 Hint.first == (unsigned)ARMRI::RegPairEven) &&
456 Hint.second && TargetRegisterInfo::isVirtualRegister(Hint.second)) {
457 // If 'Reg' is one of the even / odd register pair and it's now changed
458 // (e.g. coalesced) into a different register. The other register of the
459 // pair allocation hint must be updated to reflect the relationship
460 // change.
461 unsigned OtherReg = Hint.second;
462 Hint = MRI->getRegAllocationHint(OtherReg);
463 if (Hint.second == Reg)
464 // Make sure the pair has not already divorced.
465 MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
466 }
467}
468
Jim Grosbach3dab2772009-10-27 22:45:39 +0000469static unsigned calculateMaxStackAlignment(const MachineFrameInfo *FFI) {
470 unsigned MaxAlign = 0;
471
472 for (int i = FFI->getObjectIndexBegin(),
473 e = FFI->getObjectIndexEnd(); i != e; ++i) {
474 if (FFI->isDeadObjectIndex(i))
475 continue;
476
477 unsigned Align = FFI->getObjectAlignment(i);
478 MaxAlign = std::max(MaxAlign, Align);
479 }
480
481 return MaxAlign;
482}
483
David Goodwinc140c482009-07-08 17:28:55 +0000484/// hasFP - Return true if the specified function should have a dedicated frame
485/// pointer register. This is true if the function has variable sized allocas
486/// or if frame pointer elimination is disabled.
487///
488bool ARMBaseRegisterInfo::hasFP(const MachineFunction &MF) const {
489 const MachineFrameInfo *MFI = MF.getFrameInfo();
490 return (NoFramePointerElim ||
Jim Grosbach3dab2772009-10-27 22:45:39 +0000491 needsStackRealignment(MF) ||
David Goodwinc140c482009-07-08 17:28:55 +0000492 MFI->hasVarSizedObjects() ||
493 MFI->isFrameAddressTaken());
494}
495
Jim Grosbach3dab2772009-10-27 22:45:39 +0000496bool ARMBaseRegisterInfo::
497needsStackRealignment(const MachineFunction &MF) const {
498 // Only do this for ARM if explicitly enabled
499 // FIXME: Once it's passing all the tests, enable by default
500 if (!ARMDynamicStackAlign)
501 return false;
502
503 const MachineFrameInfo *MFI = MF.getFrameInfo();
504 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
505 unsigned StackAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
506 return (RealignStack &&
507 !AFI->isThumb1OnlyFunction() &&
508 (MFI->getMaxAlignment() > StackAlign) &&
509 !MFI->hasVarSizedObjects());
510
511}
512
Evan Cheng010b1b92009-08-15 02:05:35 +0000513bool ARMBaseRegisterInfo::cannotEliminateFrame(const MachineFunction &MF) const {
Evan Cheng98a01042009-08-14 20:48:13 +0000514 const MachineFrameInfo *MFI = MF.getFrameInfo();
515 if (NoFramePointerElim && MFI->hasCalls())
516 return true;
517 return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken();
518}
519
Evan Cheng542383d2009-07-28 06:24:12 +0000520/// estimateStackSize - Estimate and return the size of the frame.
David Goodwinc140c482009-07-08 17:28:55 +0000521static unsigned estimateStackSize(MachineFunction &MF, MachineFrameInfo *MFI) {
522 const MachineFrameInfo *FFI = MF.getFrameInfo();
523 int Offset = 0;
524 for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
525 int FixedOff = -FFI->getObjectOffset(i);
526 if (FixedOff > Offset) Offset = FixedOff;
527 }
528 for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
529 if (FFI->isDeadObjectIndex(i))
530 continue;
531 Offset += FFI->getObjectSize(i);
532 unsigned Align = FFI->getObjectAlignment(i);
533 // Adjust to alignment boundary
534 Offset = (Offset+Align-1)/Align*Align;
535 }
536 return (unsigned)Offset;
537}
538
Evan Cheng542383d2009-07-28 06:24:12 +0000539/// estimateRSStackSizeLimit - Look at each instruction that references stack
540/// frames and return the stack size limit beyond which some of these
541/// instructions will require scratch register during their expansion later.
Evan Chengee42fd32009-07-30 23:29:25 +0000542unsigned
543ARMBaseRegisterInfo::estimateRSStackSizeLimit(MachineFunction &MF) const {
Evan Cheng542383d2009-07-28 06:24:12 +0000544 unsigned Limit = (1 << 12) - 1;
Chris Lattnerb180d992009-07-28 18:48:43 +0000545 for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) {
546 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
547 I != E; ++I) {
548 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
549 if (!I->getOperand(i).isFI()) continue;
Jim Grosbach764ab522009-08-11 15:33:49 +0000550
Chris Lattnerb180d992009-07-28 18:48:43 +0000551 const TargetInstrDesc &Desc = TII.get(I->getOpcode());
552 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
553 if (AddrMode == ARMII::AddrMode3 ||
554 AddrMode == ARMII::AddrModeT2_i8)
555 return (1 << 8) - 1;
Jim Grosbach764ab522009-08-11 15:33:49 +0000556
Chris Lattnerb180d992009-07-28 18:48:43 +0000557 if (AddrMode == ARMII::AddrMode5 ||
558 AddrMode == ARMII::AddrModeT2_i8s4)
559 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
Evan Chengee42fd32009-07-30 23:29:25 +0000560
561 if (AddrMode == ARMII::AddrModeT2_i12 && hasFP(MF))
562 // When the stack offset is negative, we will end up using
563 // the i8 instructions instead.
564 return (1 << 8) - 1;
Chris Lattnerb180d992009-07-28 18:48:43 +0000565 break; // At most one FI per instruction
566 }
Evan Cheng542383d2009-07-28 06:24:12 +0000567 }
568 }
569
570 return Limit;
571}
572
David Goodwinc140c482009-07-08 17:28:55 +0000573void
574ARMBaseRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
575 RegScavenger *RS) const {
576 // This tells PEI to spill the FP as if it is any other callee-save register
577 // to take advantage the eliminateFrameIndex machinery. This also ensures it
578 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
579 // to combine multiple loads / stores.
580 bool CanEliminateFrame = true;
581 bool CS1Spilled = false;
582 bool LRSpilled = false;
583 unsigned NumGPRSpills = 0;
584 SmallVector<unsigned, 4> UnspilledCS1GPRs;
585 SmallVector<unsigned, 4> UnspilledCS2GPRs;
586 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
587
Jim Grosbach3dab2772009-10-27 22:45:39 +0000588 MachineFrameInfo *MFI = MF.getFrameInfo();
589
590 // Calculate and set max stack object alignment early, so we can decide
591 // whether we will need stack realignment (and thus FP).
592 if (ARMDynamicStackAlign) {
593 unsigned MaxAlign = std::max(MFI->getMaxAlignment(),
594 calculateMaxStackAlignment(MFI));
595 MFI->setMaxAlignment(MaxAlign);
596 }
597
David Goodwinc140c482009-07-08 17:28:55 +0000598 // Don't spill FP if the frame can be eliminated. This is determined
599 // by scanning the callee-save registers to see if any is used.
600 const unsigned *CSRegs = getCalleeSavedRegs();
601 const TargetRegisterClass* const *CSRegClasses = getCalleeSavedRegClasses();
602 for (unsigned i = 0; CSRegs[i]; ++i) {
603 unsigned Reg = CSRegs[i];
604 bool Spilled = false;
605 if (MF.getRegInfo().isPhysRegUsed(Reg)) {
606 AFI->setCSRegisterIsSpilled(Reg);
607 Spilled = true;
608 CanEliminateFrame = false;
609 } else {
610 // Check alias registers too.
611 for (const unsigned *Aliases = getAliasSet(Reg); *Aliases; ++Aliases) {
612 if (MF.getRegInfo().isPhysRegUsed(*Aliases)) {
613 Spilled = true;
614 CanEliminateFrame = false;
615 }
616 }
617 }
618
Jim Grosbachec9eef42009-09-28 22:08:06 +0000619 if (CSRegClasses[i] == ARM::GPRRegisterClass ||
620 CSRegClasses[i] == ARM::tGPRRegisterClass) {
David Goodwinc140c482009-07-08 17:28:55 +0000621 if (Spilled) {
622 NumGPRSpills++;
623
624 if (!STI.isTargetDarwin()) {
625 if (Reg == ARM::LR)
626 LRSpilled = true;
627 CS1Spilled = true;
628 continue;
629 }
630
631 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
632 switch (Reg) {
633 case ARM::LR:
634 LRSpilled = true;
635 // Fallthrough
636 case ARM::R4:
637 case ARM::R5:
638 case ARM::R6:
639 case ARM::R7:
640 CS1Spilled = true;
641 break;
642 default:
643 break;
644 }
645 } else {
646 if (!STI.isTargetDarwin()) {
647 UnspilledCS1GPRs.push_back(Reg);
648 continue;
649 }
650
651 switch (Reg) {
652 case ARM::R4:
653 case ARM::R5:
654 case ARM::R6:
655 case ARM::R7:
656 case ARM::LR:
657 UnspilledCS1GPRs.push_back(Reg);
658 break;
659 default:
660 UnspilledCS2GPRs.push_back(Reg);
661 break;
662 }
663 }
664 }
665 }
666
667 bool ForceLRSpill = false;
David Goodwinf1daf7d2009-07-08 23:10:31 +0000668 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
David Goodwinc140c482009-07-08 17:28:55 +0000669 unsigned FnSize = TII.GetFunctionSizeInBytes(MF);
670 // Force LR to be spilled if the Thumb function size is > 2048. This enables
671 // use of BL to implement far jump. If it turns out that it's not needed
672 // then the branch fix up path will undo it.
673 if (FnSize >= (1 << 11)) {
674 CanEliminateFrame = false;
675 ForceLRSpill = true;
676 }
677 }
678
679 bool ExtraCSSpill = false;
Evan Cheng010b1b92009-08-15 02:05:35 +0000680 if (!CanEliminateFrame || cannotEliminateFrame(MF)) {
David Goodwinc140c482009-07-08 17:28:55 +0000681 AFI->setHasStackFrame(true);
682
683 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
684 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
685 if (!LRSpilled && CS1Spilled) {
686 MF.getRegInfo().setPhysRegUsed(ARM::LR);
687 AFI->setCSRegisterIsSpilled(ARM::LR);
688 NumGPRSpills++;
689 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
690 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
691 ForceLRSpill = false;
692 ExtraCSSpill = true;
693 }
694
695 // Darwin ABI requires FP to point to the stack slot that contains the
696 // previous FP.
697 if (STI.isTargetDarwin() || hasFP(MF)) {
698 MF.getRegInfo().setPhysRegUsed(FramePtr);
699 NumGPRSpills++;
700 }
701
702 // If stack and double are 8-byte aligned and we are spilling an odd number
703 // of GPRs. Spill one extra callee save GPR so we won't have to pad between
704 // the integer and double callee save areas.
705 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
706 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
707 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
708 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
709 unsigned Reg = UnspilledCS1GPRs[i];
David Goodwinf1daf7d2009-07-08 23:10:31 +0000710 // Don't spill high register if the function is thumb1
711 if (!AFI->isThumb1OnlyFunction() ||
David Goodwinc140c482009-07-08 17:28:55 +0000712 isARMLowRegister(Reg) || Reg == ARM::LR) {
713 MF.getRegInfo().setPhysRegUsed(Reg);
714 AFI->setCSRegisterIsSpilled(Reg);
715 if (!isReservedReg(MF, Reg))
716 ExtraCSSpill = true;
717 break;
718 }
719 }
720 } else if (!UnspilledCS2GPRs.empty() &&
David Goodwinf1daf7d2009-07-08 23:10:31 +0000721 !AFI->isThumb1OnlyFunction()) {
David Goodwinc140c482009-07-08 17:28:55 +0000722 unsigned Reg = UnspilledCS2GPRs.front();
723 MF.getRegInfo().setPhysRegUsed(Reg);
724 AFI->setCSRegisterIsSpilled(Reg);
725 if (!isReservedReg(MF, Reg))
726 ExtraCSSpill = true;
727 }
728 }
729
730 // Estimate if we might need to scavenge a register at some point in order
731 // to materialize a stack offset. If so, either spill one additional
732 // callee-saved register or reserve a special spill slot to facilitate
Jim Grosbach3d6cb882009-09-24 23:52:18 +0000733 // register scavenging. Thumb1 needs a spill slot for stack pointer
734 // adjustments also, even when the frame itself is small.
735 if (RS && !ExtraCSSpill) {
David Goodwinc140c482009-07-08 17:28:55 +0000736 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachd1a5ca62009-09-30 01:43:29 +0000737 // If any of the stack slot references may be out of range of an
738 // immediate offset, make sure a register (or a spill slot) is
739 // available for the register scavenger. Note that if we're indexing
740 // off the frame pointer, the effective stack size is 4 bytes larger
Jim Grosbach460c4822009-09-30 15:23:38 +0000741 // since the FP points to the stack slot of the previous FP.
Jim Grosbachd1a5ca62009-09-30 01:43:29 +0000742 if (estimateStackSize(MF, MFI) + (hasFP(MF) ? 4 : 0)
Jim Grosbach540b05d2009-10-05 22:30:23 +0000743 >= estimateRSStackSizeLimit(MF)) {
David Goodwinc140c482009-07-08 17:28:55 +0000744 // If any non-reserved CS register isn't spilled, just spill one or two
745 // extra. That should take care of it!
746 unsigned NumExtras = TargetAlign / 4;
747 SmallVector<unsigned, 2> Extras;
748 while (NumExtras && !UnspilledCS1GPRs.empty()) {
749 unsigned Reg = UnspilledCS1GPRs.back();
750 UnspilledCS1GPRs.pop_back();
751 if (!isReservedReg(MF, Reg)) {
752 Extras.push_back(Reg);
753 NumExtras--;
754 }
755 }
Jim Grosbach17487ba2009-09-29 23:17:20 +0000756 // For non-Thumb1 functions, also check for hi-reg CS registers
757 if (!AFI->isThumb1OnlyFunction()) {
758 while (NumExtras && !UnspilledCS2GPRs.empty()) {
759 unsigned Reg = UnspilledCS2GPRs.back();
760 UnspilledCS2GPRs.pop_back();
761 if (!isReservedReg(MF, Reg)) {
762 Extras.push_back(Reg);
763 NumExtras--;
764 }
David Goodwinc140c482009-07-08 17:28:55 +0000765 }
766 }
767 if (Extras.size() && NumExtras == 0) {
768 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
769 MF.getRegInfo().setPhysRegUsed(Extras[i]);
770 AFI->setCSRegisterIsSpilled(Extras[i]);
771 }
Jim Grosbach540b05d2009-10-05 22:30:23 +0000772 } else if (!AFI->isThumb1OnlyFunction()) {
773 // note: Thumb1 functions spill to R12, not the stack.
David Goodwinc140c482009-07-08 17:28:55 +0000774 // Reserve a slot closest to SP or frame pointer.
Jim Grosbache11a8f52009-09-11 19:49:06 +0000775 const TargetRegisterClass *RC = ARM::GPRRegisterClass;
David Goodwinc140c482009-07-08 17:28:55 +0000776 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
777 RC->getAlignment()));
778 }
779 }
780 }
781 }
782
783 if (ForceLRSpill) {
784 MF.getRegInfo().setPhysRegUsed(ARM::LR);
785 AFI->setCSRegisterIsSpilled(ARM::LR);
786 AFI->setLRIsSpilledForFarJump(true);
787 }
788}
789
790unsigned ARMBaseRegisterInfo::getRARegister() const {
791 return ARM::LR;
792}
793
794unsigned ARMBaseRegisterInfo::getFrameRegister(MachineFunction &MF) const {
795 if (STI.isTargetDarwin() || hasFP(MF))
796 return FramePtr;
797 return ARM::SP;
798}
799
800unsigned ARMBaseRegisterInfo::getEHExceptionRegister() const {
Torok Edwinc23197a2009-07-14 16:55:14 +0000801 llvm_unreachable("What is the exception register");
David Goodwinc140c482009-07-08 17:28:55 +0000802 return 0;
803}
804
805unsigned ARMBaseRegisterInfo::getEHHandlerRegister() const {
Torok Edwinc23197a2009-07-14 16:55:14 +0000806 llvm_unreachable("What is the exception handler register");
David Goodwinc140c482009-07-08 17:28:55 +0000807 return 0;
808}
809
810int ARMBaseRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
811 return ARMGenRegisterInfo::getDwarfRegNumFull(RegNum, 0);
812}
813
814unsigned ARMBaseRegisterInfo::getRegisterPairEven(unsigned Reg,
815 const MachineFunction &MF) const {
816 switch (Reg) {
817 default: break;
818 // Return 0 if either register of the pair is a special register.
819 // So no R12, etc.
820 case ARM::R1:
821 return ARM::R0;
822 case ARM::R3:
Jim Grosbach60097512009-10-19 22:57:03 +0000823 return ARM::R2;
David Goodwinc140c482009-07-08 17:28:55 +0000824 case ARM::R5:
825 return ARM::R4;
826 case ARM::R7:
827 return isReservedReg(MF, ARM::R7) ? 0 : ARM::R6;
828 case ARM::R9:
829 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R8;
830 case ARM::R11:
831 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R10;
832
833 case ARM::S1:
834 return ARM::S0;
835 case ARM::S3:
836 return ARM::S2;
837 case ARM::S5:
838 return ARM::S4;
839 case ARM::S7:
840 return ARM::S6;
841 case ARM::S9:
842 return ARM::S8;
843 case ARM::S11:
844 return ARM::S10;
845 case ARM::S13:
846 return ARM::S12;
847 case ARM::S15:
848 return ARM::S14;
849 case ARM::S17:
850 return ARM::S16;
851 case ARM::S19:
852 return ARM::S18;
853 case ARM::S21:
854 return ARM::S20;
855 case ARM::S23:
856 return ARM::S22;
857 case ARM::S25:
858 return ARM::S24;
859 case ARM::S27:
860 return ARM::S26;
861 case ARM::S29:
862 return ARM::S28;
863 case ARM::S31:
864 return ARM::S30;
865
866 case ARM::D1:
867 return ARM::D0;
868 case ARM::D3:
869 return ARM::D2;
870 case ARM::D5:
871 return ARM::D4;
872 case ARM::D7:
873 return ARM::D6;
874 case ARM::D9:
875 return ARM::D8;
876 case ARM::D11:
877 return ARM::D10;
878 case ARM::D13:
879 return ARM::D12;
880 case ARM::D15:
881 return ARM::D14;
Evan Cheng8295d992009-07-22 05:55:18 +0000882 case ARM::D17:
883 return ARM::D16;
884 case ARM::D19:
885 return ARM::D18;
886 case ARM::D21:
887 return ARM::D20;
888 case ARM::D23:
889 return ARM::D22;
890 case ARM::D25:
891 return ARM::D24;
892 case ARM::D27:
893 return ARM::D26;
894 case ARM::D29:
895 return ARM::D28;
896 case ARM::D31:
897 return ARM::D30;
David Goodwinc140c482009-07-08 17:28:55 +0000898 }
899
900 return 0;
901}
902
903unsigned ARMBaseRegisterInfo::getRegisterPairOdd(unsigned Reg,
904 const MachineFunction &MF) const {
905 switch (Reg) {
906 default: break;
907 // Return 0 if either register of the pair is a special register.
908 // So no R12, etc.
909 case ARM::R0:
910 return ARM::R1;
911 case ARM::R2:
Jim Grosbach60097512009-10-19 22:57:03 +0000912 return ARM::R3;
David Goodwinc140c482009-07-08 17:28:55 +0000913 case ARM::R4:
914 return ARM::R5;
915 case ARM::R6:
916 return isReservedReg(MF, ARM::R7) ? 0 : ARM::R7;
917 case ARM::R8:
918 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R9;
919 case ARM::R10:
920 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R11;
921
922 case ARM::S0:
923 return ARM::S1;
924 case ARM::S2:
925 return ARM::S3;
926 case ARM::S4:
927 return ARM::S5;
928 case ARM::S6:
929 return ARM::S7;
930 case ARM::S8:
931 return ARM::S9;
932 case ARM::S10:
933 return ARM::S11;
934 case ARM::S12:
935 return ARM::S13;
936 case ARM::S14:
937 return ARM::S15;
938 case ARM::S16:
939 return ARM::S17;
940 case ARM::S18:
941 return ARM::S19;
942 case ARM::S20:
943 return ARM::S21;
944 case ARM::S22:
945 return ARM::S23;
946 case ARM::S24:
947 return ARM::S25;
948 case ARM::S26:
949 return ARM::S27;
950 case ARM::S28:
951 return ARM::S29;
952 case ARM::S30:
953 return ARM::S31;
954
955 case ARM::D0:
956 return ARM::D1;
957 case ARM::D2:
958 return ARM::D3;
959 case ARM::D4:
960 return ARM::D5;
961 case ARM::D6:
962 return ARM::D7;
963 case ARM::D8:
964 return ARM::D9;
965 case ARM::D10:
966 return ARM::D11;
967 case ARM::D12:
968 return ARM::D13;
969 case ARM::D14:
970 return ARM::D15;
Evan Cheng8295d992009-07-22 05:55:18 +0000971 case ARM::D16:
972 return ARM::D17;
973 case ARM::D18:
974 return ARM::D19;
975 case ARM::D20:
976 return ARM::D21;
977 case ARM::D22:
978 return ARM::D23;
979 case ARM::D24:
980 return ARM::D25;
981 case ARM::D26:
982 return ARM::D27;
983 case ARM::D28:
984 return ARM::D29;
985 case ARM::D30:
986 return ARM::D31;
David Goodwinc140c482009-07-08 17:28:55 +0000987 }
988
989 return 0;
990}
991
David Goodwindb5a71a2009-07-08 18:31:39 +0000992/// emitLoadConstPool - Emits a load from constpool to materialize the
993/// specified immediate.
994void ARMBaseRegisterInfo::
995emitLoadConstPool(MachineBasicBlock &MBB,
996 MachineBasicBlock::iterator &MBBI,
David Goodwin77521f52009-07-08 20:28:28 +0000997 DebugLoc dl,
Evan Cheng37844532009-07-16 09:20:10 +0000998 unsigned DestReg, unsigned SubIdx, int Val,
David Goodwindb5a71a2009-07-08 18:31:39 +0000999 ARMCC::CondCodes Pred,
1000 unsigned PredReg) const {
1001 MachineFunction &MF = *MBB.getParent();
1002 MachineConstantPool *ConstantPool = MF.getConstantPool();
Owen Anderson1d0be152009-08-13 21:58:54 +00001003 Constant *C =
1004 ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val);
David Goodwindb5a71a2009-07-08 18:31:39 +00001005 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
1006
Evan Cheng37844532009-07-16 09:20:10 +00001007 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
1008 .addReg(DestReg, getDefRegState(true), SubIdx)
David Goodwindb5a71a2009-07-08 18:31:39 +00001009 .addConstantPoolIndex(Idx)
1010 .addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
1011}
1012
1013bool ARMBaseRegisterInfo::
1014requiresRegisterScavenging(const MachineFunction &MF) const {
1015 return true;
1016}
Jim Grosbach41fff8c2009-10-21 23:40:56 +00001017
Jim Grosbach7e831db2009-10-20 01:26:58 +00001018bool ARMBaseRegisterInfo::
1019requiresFrameIndexScavenging(const MachineFunction &MF) const {
Jim Grosbachca5dfb72009-10-28 17:33:28 +00001020 return true;
Jim Grosbach7e831db2009-10-20 01:26:58 +00001021}
David Goodwindb5a71a2009-07-08 18:31:39 +00001022
1023// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
1024// not required, we reserve argument space for call sites in the function
1025// immediately on entry to the current function. This eliminates the need for
1026// add/sub sp brackets around call sites. Returns true if the call frame is
1027// included as part of the stack frame.
1028bool ARMBaseRegisterInfo::
1029hasReservedCallFrame(MachineFunction &MF) const {
1030 const MachineFrameInfo *FFI = MF.getFrameInfo();
1031 unsigned CFSize = FFI->getMaxCallFrameSize();
1032 // It's not always a good idea to include the call frame as part of the
1033 // stack frame. ARM (especially Thumb) has small immediate offset to
1034 // address the stack frame. So a large call frame can cause poor codegen
1035 // and may even makes it impossible to scavenge a register.
1036 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
1037 return false;
1038
1039 return !MF.getFrameInfo()->hasVarSizedObjects();
1040}
1041
David Goodwindb5a71a2009-07-08 18:31:39 +00001042static void
Evan Cheng6495f632009-07-28 05:48:47 +00001043emitSPUpdate(bool isARM,
1044 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
1045 DebugLoc dl, const ARMBaseInstrInfo &TII,
David Goodwindb5a71a2009-07-08 18:31:39 +00001046 int NumBytes,
1047 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
Evan Cheng6495f632009-07-28 05:48:47 +00001048 if (isARM)
1049 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
1050 Pred, PredReg, TII);
1051 else
1052 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
1053 Pred, PredReg, TII);
David Goodwindb5a71a2009-07-08 18:31:39 +00001054}
1055
Evan Cheng6495f632009-07-28 05:48:47 +00001056
David Goodwindb5a71a2009-07-08 18:31:39 +00001057void ARMBaseRegisterInfo::
1058eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1059 MachineBasicBlock::iterator I) const {
1060 if (!hasReservedCallFrame(MF)) {
1061 // If we have alloca, convert as follows:
1062 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
1063 // ADJCALLSTACKUP -> add, sp, sp, amount
1064 MachineInstr *Old = I;
1065 DebugLoc dl = Old->getDebugLoc();
1066 unsigned Amount = Old->getOperand(0).getImm();
1067 if (Amount != 0) {
1068 // We need to keep the stack aligned properly. To do this, we round the
1069 // amount of space needed for the outgoing arguments up to the next
1070 // alignment boundary.
1071 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1072 Amount = (Amount+Align-1)/Align*Align;
1073
Evan Cheng6495f632009-07-28 05:48:47 +00001074 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1075 assert(!AFI->isThumb1OnlyFunction() &&
1076 "This eliminateCallFramePseudoInstr does not suppor Thumb1!");
1077 bool isARM = !AFI->isThumbFunction();
1078
David Goodwindb5a71a2009-07-08 18:31:39 +00001079 // Replace the pseudo instruction with a new instruction...
1080 unsigned Opc = Old->getOpcode();
1081 ARMCC::CondCodes Pred = (ARMCC::CondCodes)Old->getOperand(1).getImm();
Evan Cheng6495f632009-07-28 05:48:47 +00001082 // FIXME: Thumb2 version of ADJCALLSTACKUP and ADJCALLSTACKDOWN?
David Goodwindb5a71a2009-07-08 18:31:39 +00001083 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
1084 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
1085 unsigned PredReg = Old->getOperand(2).getReg();
Evan Cheng6495f632009-07-28 05:48:47 +00001086 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, Pred, PredReg);
David Goodwindb5a71a2009-07-08 18:31:39 +00001087 } else {
1088 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
1089 unsigned PredReg = Old->getOperand(3).getReg();
1090 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
Evan Cheng6495f632009-07-28 05:48:47 +00001091 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, Pred, PredReg);
David Goodwindb5a71a2009-07-08 18:31:39 +00001092 }
1093 }
1094 }
1095 MBB.erase(I);
1096}
1097
Jim Grosbachb58f4982009-10-07 17:12:56 +00001098unsigned
Evan Cheng6495f632009-07-28 05:48:47 +00001099ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
Jim Grosbachb58f4982009-10-07 17:12:56 +00001100 int SPAdj, int *Value,
1101 RegScavenger *RS) const {
David Goodwindb5a71a2009-07-08 18:31:39 +00001102 unsigned i = 0;
1103 MachineInstr &MI = *II;
1104 MachineBasicBlock &MBB = *MI.getParent();
1105 MachineFunction &MF = *MBB.getParent();
Evan Cheng010b1b92009-08-15 02:05:35 +00001106 const MachineFrameInfo *MFI = MF.getFrameInfo();
David Goodwindb5a71a2009-07-08 18:31:39 +00001107 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng6495f632009-07-28 05:48:47 +00001108 assert(!AFI->isThumb1OnlyFunction() &&
Bob Wilsona15de002009-09-18 21:42:44 +00001109 "This eliminateFrameIndex does not support Thumb1!");
David Goodwindb5a71a2009-07-08 18:31:39 +00001110
1111 while (!MI.getOperand(i).isFI()) {
1112 ++i;
1113 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1114 }
1115
1116 unsigned FrameReg = ARM::SP;
1117 int FrameIndex = MI.getOperand(i).getIndex();
Evan Cheng010b1b92009-08-15 02:05:35 +00001118 int Offset = MFI->getObjectOffset(FrameIndex) + MFI->getStackSize() + SPAdj;
David Goodwindb5a71a2009-07-08 18:31:39 +00001119
Jim Grosbach3dab2772009-10-27 22:45:39 +00001120 // When doing dynamic stack realignment, all of these need to change(?)
David Goodwindb5a71a2009-07-08 18:31:39 +00001121 if (AFI->isGPRCalleeSavedArea1Frame(FrameIndex))
1122 Offset -= AFI->getGPRCalleeSavedArea1Offset();
1123 else if (AFI->isGPRCalleeSavedArea2Frame(FrameIndex))
1124 Offset -= AFI->getGPRCalleeSavedArea2Offset();
1125 else if (AFI->isDPRCalleeSavedAreaFrame(FrameIndex))
1126 Offset -= AFI->getDPRCalleeSavedAreaOffset();
Jim Grosbach3dab2772009-10-27 22:45:39 +00001127 else if (needsStackRealignment(MF)) {
1128 // When dynamically realigning the stack, use the frame pointer for
1129 // parameters, and the stack pointer for locals.
1130 assert (hasFP(MF) && "dynamic stack realignment without a FP!");
1131 if (FrameIndex < 0) {
1132 FrameReg = getFrameRegister(MF);
1133 Offset -= AFI->getFramePtrSpillOffset();
1134 // When referencing from the frame pointer, stack pointer adjustments
1135 // don't matter.
1136 SPAdj = 0;
1137 }
1138 } else if (hasFP(MF) && AFI->hasStackFrame()) {
Evan Cheng010b1b92009-08-15 02:05:35 +00001139 assert(SPAdj == 0 && "Unexpected stack offset!");
1140 // Use frame pointer to reference fixed objects unless this is a
Jim Grosbach3dab2772009-10-27 22:45:39 +00001141 // frameless function.
David Goodwindb5a71a2009-07-08 18:31:39 +00001142 FrameReg = getFrameRegister(MF);
1143 Offset -= AFI->getFramePtrSpillOffset();
1144 }
1145
David Goodwin5ff58b52009-07-24 00:16:18 +00001146 // modify MI as necessary to handle as much of 'Offset' as possible
Evan Chengcdbb3f52009-08-27 01:23:50 +00001147 bool Done = false;
Evan Cheng6495f632009-07-28 05:48:47 +00001148 if (!AFI->isThumbFunction())
Evan Chengcdbb3f52009-08-27 01:23:50 +00001149 Done = rewriteARMFrameIndex(MI, i, FrameReg, Offset, TII);
Evan Cheng6495f632009-07-28 05:48:47 +00001150 else {
1151 assert(AFI->isThumb2Function());
Evan Chengcdbb3f52009-08-27 01:23:50 +00001152 Done = rewriteT2FrameIndex(MI, i, FrameReg, Offset, TII);
Evan Cheng6495f632009-07-28 05:48:47 +00001153 }
Evan Chengcdbb3f52009-08-27 01:23:50 +00001154 if (Done)
Jim Grosbachb58f4982009-10-07 17:12:56 +00001155 return 0;
David Goodwindb5a71a2009-07-08 18:31:39 +00001156
1157 // If we get here, the immediate doesn't fit into the instruction. We folded
1158 // as much as possible above, handle the rest, providing a register that is
1159 // SP+LargeImm.
Daniel Dunbar19bb87d2009-08-28 08:08:22 +00001160 assert((Offset ||
1161 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4) &&
Evan Chengcdbb3f52009-08-27 01:23:50 +00001162 "This code isn't needed if offset already handled!");
David Goodwindb5a71a2009-07-08 18:31:39 +00001163
Jim Grosbach7e831db2009-10-20 01:26:58 +00001164 unsigned ScratchReg = 0;
David Goodwindb5a71a2009-07-08 18:31:39 +00001165 int PIdx = MI.findFirstPredOperandIdx();
1166 ARMCC::CondCodes Pred = (PIdx == -1)
1167 ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
1168 unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
Evan Chengcdbb3f52009-08-27 01:23:50 +00001169 if (Offset == 0)
1170 // Must be addrmode4.
1171 MI.getOperand(i).ChangeToRegister(FrameReg, false, false, false);
Evan Cheng6495f632009-07-28 05:48:47 +00001172 else {
Jim Grosbachca5dfb72009-10-28 17:33:28 +00001173 ScratchReg = MF.getRegInfo().createVirtualRegister(ARM::GPRRegisterClass);
1174 if (Value) *Value = Offset;
Evan Chengcdbb3f52009-08-27 01:23:50 +00001175 if (!AFI->isThumbFunction())
1176 emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1177 Offset, Pred, PredReg, TII);
1178 else {
1179 assert(AFI->isThumb2Function());
1180 emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1181 Offset, Pred, PredReg, TII);
1182 }
1183 MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
Jim Grosbachca5dfb72009-10-28 17:33:28 +00001184 if (!ReuseFrameIndexVals)
Jim Grosbach18ed9c92009-10-20 20:19:50 +00001185 ScratchReg = 0;
Evan Cheng6495f632009-07-28 05:48:47 +00001186 }
Jim Grosbach7e831db2009-10-20 01:26:58 +00001187 return ScratchReg;
David Goodwindb5a71a2009-07-08 18:31:39 +00001188}
1189
1190/// Move iterator pass the next bunch of callee save load / store ops for
1191/// the particular spill area (1: integer area 1, 2: integer area 2,
1192/// 3: fp area, 0: don't care).
1193static void movePastCSLoadStoreOps(MachineBasicBlock &MBB,
1194 MachineBasicBlock::iterator &MBBI,
David Goodwin5ff58b52009-07-24 00:16:18 +00001195 int Opc1, int Opc2, unsigned Area,
David Goodwindb5a71a2009-07-08 18:31:39 +00001196 const ARMSubtarget &STI) {
1197 while (MBBI != MBB.end() &&
David Goodwin5ff58b52009-07-24 00:16:18 +00001198 ((MBBI->getOpcode() == Opc1) || (MBBI->getOpcode() == Opc2)) &&
1199 MBBI->getOperand(1).isFI()) {
David Goodwindb5a71a2009-07-08 18:31:39 +00001200 if (Area != 0) {
1201 bool Done = false;
1202 unsigned Category = 0;
1203 switch (MBBI->getOperand(0).getReg()) {
1204 case ARM::R4: case ARM::R5: case ARM::R6: case ARM::R7:
1205 case ARM::LR:
1206 Category = 1;
1207 break;
1208 case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11:
1209 Category = STI.isTargetDarwin() ? 2 : 1;
1210 break;
1211 case ARM::D8: case ARM::D9: case ARM::D10: case ARM::D11:
1212 case ARM::D12: case ARM::D13: case ARM::D14: case ARM::D15:
1213 Category = 3;
1214 break;
1215 default:
1216 Done = true;
1217 break;
1218 }
1219 if (Done || Category != Area)
1220 break;
1221 }
1222
1223 ++MBBI;
1224 }
1225}
1226
1227void ARMBaseRegisterInfo::
1228emitPrologue(MachineFunction &MF) const {
1229 MachineBasicBlock &MBB = MF.front();
1230 MachineBasicBlock::iterator MBBI = MBB.begin();
1231 MachineFrameInfo *MFI = MF.getFrameInfo();
1232 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng6495f632009-07-28 05:48:47 +00001233 assert(!AFI->isThumb1OnlyFunction() &&
1234 "This emitPrologue does not suppor Thumb1!");
1235 bool isARM = !AFI->isThumbFunction();
David Goodwindb5a71a2009-07-08 18:31:39 +00001236 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1237 unsigned NumBytes = MFI->getStackSize();
1238 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
1239 DebugLoc dl = (MBBI != MBB.end() ?
1240 MBBI->getDebugLoc() : DebugLoc::getUnknownLoc());
1241
1242 // Determine the sizes of each callee-save spill areas and record which frame
1243 // belongs to which callee-save spill areas.
1244 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
1245 int FramePtrSpillFI = 0;
1246
Bob Wilsonc8ce2d42009-09-25 16:34:46 +00001247 // Allocate the vararg register save area. This is not counted in NumBytes.
David Goodwindb5a71a2009-07-08 18:31:39 +00001248 if (VARegSaveSize)
Evan Cheng6495f632009-07-28 05:48:47 +00001249 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize);
David Goodwindb5a71a2009-07-08 18:31:39 +00001250
1251 if (!AFI->hasStackFrame()) {
1252 if (NumBytes != 0)
Evan Cheng6495f632009-07-28 05:48:47 +00001253 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
David Goodwindb5a71a2009-07-08 18:31:39 +00001254 return;
1255 }
1256
1257 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1258 unsigned Reg = CSI[i].getReg();
1259 int FI = CSI[i].getFrameIdx();
1260 switch (Reg) {
1261 case ARM::R4:
1262 case ARM::R5:
1263 case ARM::R6:
1264 case ARM::R7:
1265 case ARM::LR:
1266 if (Reg == FramePtr)
1267 FramePtrSpillFI = FI;
1268 AFI->addGPRCalleeSavedArea1Frame(FI);
1269 GPRCS1Size += 4;
1270 break;
1271 case ARM::R8:
1272 case ARM::R9:
1273 case ARM::R10:
1274 case ARM::R11:
1275 if (Reg == FramePtr)
1276 FramePtrSpillFI = FI;
1277 if (STI.isTargetDarwin()) {
1278 AFI->addGPRCalleeSavedArea2Frame(FI);
1279 GPRCS2Size += 4;
1280 } else {
1281 AFI->addGPRCalleeSavedArea1Frame(FI);
1282 GPRCS1Size += 4;
1283 }
1284 break;
1285 default:
1286 AFI->addDPRCalleeSavedAreaFrame(FI);
1287 DPRCSSize += 8;
1288 }
1289 }
1290
1291 // Build the new SUBri to adjust SP for integer callee-save spill area 1.
Evan Cheng6495f632009-07-28 05:48:47 +00001292 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS1Size);
Evan Cheng5732ca02009-07-27 03:14:20 +00001293 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 1, STI);
David Goodwindb5a71a2009-07-08 18:31:39 +00001294
Bob Wilsonc8ce2d42009-09-25 16:34:46 +00001295 // Set FP to point to the stack slot that contains the previous FP.
1296 // For Darwin, FP is R7, which has now been stored in spill area 1.
1297 // Otherwise, if this is not Darwin, all the callee-saved registers go
1298 // into spill area 1, including the FP in R11. In either case, it is
1299 // now safe to emit this assignment.
David Goodwindb5a71a2009-07-08 18:31:39 +00001300 if (STI.isTargetDarwin() || hasFP(MF)) {
Evan Cheng6495f632009-07-28 05:48:47 +00001301 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri;
David Goodwindb5a71a2009-07-08 18:31:39 +00001302 MachineInstrBuilder MIB =
Evan Cheng6495f632009-07-28 05:48:47 +00001303 BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr)
David Goodwindb5a71a2009-07-08 18:31:39 +00001304 .addFrameIndex(FramePtrSpillFI).addImm(0);
1305 AddDefaultCC(AddDefaultPred(MIB));
1306 }
1307
1308 // Build the new SUBri to adjust SP for integer callee-save spill area 2.
Evan Cheng6495f632009-07-28 05:48:47 +00001309 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS2Size);
David Goodwindb5a71a2009-07-08 18:31:39 +00001310
1311 // Build the new SUBri to adjust SP for FP callee-save spill area.
Evan Cheng5732ca02009-07-27 03:14:20 +00001312 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 2, STI);
Evan Cheng6495f632009-07-28 05:48:47 +00001313 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRCSSize);
David Goodwindb5a71a2009-07-08 18:31:39 +00001314
1315 // Determine starting offsets of spill areas.
1316 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
1317 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
1318 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
1319 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
1320 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
1321 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
1322 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
1323
1324 NumBytes = DPRCSOffset;
1325 if (NumBytes) {
1326 // Insert it after all the callee-save spills.
Evan Chengb74bb1a2009-07-24 00:53:56 +00001327 movePastCSLoadStoreOps(MBB, MBBI, ARM::FSTD, 0, 3, STI);
Evan Cheng6495f632009-07-28 05:48:47 +00001328 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
David Goodwindb5a71a2009-07-08 18:31:39 +00001329 }
1330
1331 if (STI.isTargetELF() && hasFP(MF)) {
1332 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
1333 AFI->getFramePtrSpillOffset());
1334 }
1335
1336 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
1337 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
1338 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
Jim Grosbach3dab2772009-10-27 22:45:39 +00001339
1340 // If we need dynamic stack realignment, do it here.
1341 if (needsStackRealignment(MF)) {
1342 unsigned Opc;
1343 unsigned MaxAlign = MFI->getMaxAlignment();
1344 assert (!AFI->isThumb1OnlyFunction());
1345 Opc = AFI->isThumbFunction() ? ARM::t2BICri : ARM::BICri;
1346
1347 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(Opc), ARM::SP)
1348 .addReg(ARM::SP, RegState::Kill)
1349 .addImm(MaxAlign-1)));
1350 }
David Goodwindb5a71a2009-07-08 18:31:39 +00001351}
1352
1353static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
1354 for (unsigned i = 0; CSRegs[i]; ++i)
1355 if (Reg == CSRegs[i])
1356 return true;
1357 return false;
1358}
1359
David Goodwin77521f52009-07-08 20:28:28 +00001360static bool isCSRestore(MachineInstr *MI,
Jim Grosbach764ab522009-08-11 15:33:49 +00001361 const ARMBaseInstrInfo &TII,
David Goodwin77521f52009-07-08 20:28:28 +00001362 const unsigned *CSRegs) {
Evan Chengb74bb1a2009-07-24 00:53:56 +00001363 return ((MI->getOpcode() == (int)ARM::FLDD ||
Evan Cheng5732ca02009-07-27 03:14:20 +00001364 MI->getOpcode() == (int)ARM::LDR ||
1365 MI->getOpcode() == (int)ARM::t2LDRi12) &&
David Goodwindb5a71a2009-07-08 18:31:39 +00001366 MI->getOperand(1).isFI() &&
1367 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs));
1368}
1369
1370void ARMBaseRegisterInfo::
Evan Cheng293f8d92009-07-27 18:31:40 +00001371emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const {
David Goodwindb5a71a2009-07-08 18:31:39 +00001372 MachineBasicBlock::iterator MBBI = prior(MBB.end());
Evan Cheng5ca53a72009-07-27 18:20:05 +00001373 assert(MBBI->getDesc().isReturn() &&
David Goodwindb5a71a2009-07-08 18:31:39 +00001374 "Can only insert epilog into returning blocks");
1375 DebugLoc dl = MBBI->getDebugLoc();
1376 MachineFrameInfo *MFI = MF.getFrameInfo();
1377 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng6495f632009-07-28 05:48:47 +00001378 assert(!AFI->isThumb1OnlyFunction() &&
1379 "This emitEpilogue does not suppor Thumb1!");
1380 bool isARM = !AFI->isThumbFunction();
1381
David Goodwindb5a71a2009-07-08 18:31:39 +00001382 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1383 int NumBytes = (int)MFI->getStackSize();
1384
1385 if (!AFI->hasStackFrame()) {
1386 if (NumBytes != 0)
Evan Cheng6495f632009-07-28 05:48:47 +00001387 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
David Goodwindb5a71a2009-07-08 18:31:39 +00001388 } else {
1389 // Unwind MBBI to point to first LDR / FLDD.
1390 const unsigned *CSRegs = getCalleeSavedRegs();
1391 if (MBBI != MBB.begin()) {
1392 do
1393 --MBBI;
David Goodwin77521f52009-07-08 20:28:28 +00001394 while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
1395 if (!isCSRestore(MBBI, TII, CSRegs))
David Goodwindb5a71a2009-07-08 18:31:39 +00001396 ++MBBI;
1397 }
1398
1399 // Move SP to start of FP callee save spill area.
1400 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
1401 AFI->getGPRCalleeSavedArea2Size() +
1402 AFI->getDPRCalleeSavedAreaSize());
1403
1404 // Darwin ABI requires FP to point to the stack slot that contains the
1405 // previous FP.
Evan Cheng010b1b92009-08-15 02:05:35 +00001406 bool HasFP = hasFP(MF);
1407 if ((STI.isTargetDarwin() && NumBytes) || HasFP) {
David Goodwindb5a71a2009-07-08 18:31:39 +00001408 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1409 // Reset SP based on frame pointer only if the stack frame extends beyond
1410 // frame pointer stack slot or target is ELF and the function has FP.
Evan Cheng010b1b92009-08-15 02:05:35 +00001411 if (HasFP ||
1412 AFI->getGPRCalleeSavedArea2Size() ||
David Goodwindb5a71a2009-07-08 18:31:39 +00001413 AFI->getDPRCalleeSavedAreaSize() ||
Evan Cheng010b1b92009-08-15 02:05:35 +00001414 AFI->getDPRCalleeSavedAreaOffset()) {
Evan Cheng6495f632009-07-28 05:48:47 +00001415 if (NumBytes) {
Evan Cheng86198642009-08-07 00:34:42 +00001416 if (isARM)
1417 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
1418 ARMCC::AL, 0, TII);
1419 else
1420 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
1421 ARMCC::AL, 0, TII);
Evan Cheng6495f632009-07-28 05:48:47 +00001422 } else {
1423 // Thumb2 or ARM.
Jim Grosbach764ab522009-08-11 15:33:49 +00001424 if (isARM)
Evan Cheng052053b2009-08-10 05:49:43 +00001425 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
1426 .addReg(FramePtr)
1427 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
1428 else
1429 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), ARM::SP)
1430 .addReg(FramePtr);
Evan Cheng6495f632009-07-28 05:48:47 +00001431 }
David Goodwindb5a71a2009-07-08 18:31:39 +00001432 }
Evan Cheng6495f632009-07-28 05:48:47 +00001433 } else if (NumBytes)
1434 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
David Goodwindb5a71a2009-07-08 18:31:39 +00001435
1436 // Move SP to start of integer callee save spill area 2.
Evan Chengb74bb1a2009-07-24 00:53:56 +00001437 movePastCSLoadStoreOps(MBB, MBBI, ARM::FLDD, 0, 3, STI);
Evan Cheng6495f632009-07-28 05:48:47 +00001438 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedAreaSize());
David Goodwindb5a71a2009-07-08 18:31:39 +00001439
1440 // Move SP to start of integer callee save spill area 1.
Evan Cheng5732ca02009-07-27 03:14:20 +00001441 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 2, STI);
Evan Cheng6495f632009-07-28 05:48:47 +00001442 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea2Size());
David Goodwindb5a71a2009-07-08 18:31:39 +00001443
1444 // Move SP to SP upon entry to the function.
Evan Cheng5732ca02009-07-27 03:14:20 +00001445 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 1, STI);
Evan Cheng6495f632009-07-28 05:48:47 +00001446 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea1Size());
David Goodwindb5a71a2009-07-08 18:31:39 +00001447 }
1448
1449 if (VARegSaveSize)
Evan Cheng6495f632009-07-28 05:48:47 +00001450 emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize);
David Goodwindb5a71a2009-07-08 18:31:39 +00001451}
1452
David Goodwinc140c482009-07-08 17:28:55 +00001453#include "ARMGenRegisterInfo.inc"