blob: 7a52a1c9eaecbb8d0ab0b2ca482447d5c4e216db [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonFrameLowering.cpp - Define frame lowering ------------------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +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//===----------------------------------------------------------------------===//
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000010
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000011#define DEBUG_TYPE "hexagon-pei"
12
Craig Topperb25fda92012-03-17 18:46:09 +000013#include "HexagonFrameLowering.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000014#include "Hexagon.h"
15#include "HexagonInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "HexagonMachineFunctionInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000017#include "HexagonRegisterInfo.h"
18#include "HexagonSubtarget.h"
19#include "HexagonTargetMachine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000020#include "llvm/ADT/BitVector.h"
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +000021#include "llvm/ADT/PostOrderIterator.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000022#include "llvm/ADT/STLExtras.h"
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +000023#include "llvm/CodeGen/MachineDominators.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000025#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/CodeGen/MachineInstrBuilder.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000028#include "llvm/CodeGen/MachineModuleInfo.h"
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +000029#include "llvm/CodeGen/MachinePostDominators.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
31#include "llvm/CodeGen/RegisterScavenging.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Function.h"
33#include "llvm/IR/Type.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Support/CommandLine.h"
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000035#include "llvm/Support/Debug.h"
36#include "llvm/Support/raw_ostream.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000037#include "llvm/Target/TargetInstrInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000038#include "llvm/Target/TargetMachine.h"
39#include "llvm/Target/TargetOptions.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000040
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000041// Hexagon stack frame layout as defined by the ABI:
42//
43// Incoming arguments
44// passed via stack
45// |
46// |
47// SP during function's FP during function's |
48// +-- runtime (top of stack) runtime (bottom) --+ |
49// | | |
50// --++---------------------+------------------+-----------------++-+-------
51// | parameter area for | variable-size | fixed-size |LR| arg
52// | called functions | local objects | local objects |FP|
53// --+----------------------+------------------+-----------------+--+-------
54// <- size known -> <- size unknown -> <- size known ->
55//
56// Low address High address
57//
58// <--- stack growth
59//
60//
61// - In any circumstances, the outgoing function arguments are always accessi-
62// ble using the SP, and the incoming arguments are accessible using the FP.
63// - If the local objects are not aligned, they can always be accessed using
64// the FP.
65// - If there are no variable-sized objects, the local objects can always be
66// accessed using the SP, regardless whether they are aligned or not. (The
67// alignment padding will be at the bottom of the stack (highest address),
68// and so the offset with respect to the SP will be known at the compile-
69// -time.)
70//
71// The only complication occurs if there are both, local aligned objects, and
72// dynamically allocated (variable-sized) objects. The alignment pad will be
73// placed between the FP and the local objects, thus preventing the use of the
74// FP to access the local objects. At the same time, the variable-sized objects
75// will be between the SP and the local objects, thus introducing an unknown
76// distance from the SP to the locals.
77//
78// To avoid this problem, a new register is created that holds the aligned
79// address of the bottom of the stack, referred in the sources as AP (aligned
80// pointer). The AP will be equal to "FP-p", where "p" is the smallest pad
81// that aligns AP to the required boundary (a maximum of the alignments of
82// all stack objects, fixed- and variable-sized). All local objects[1] will
83// then use AP as the base pointer.
84// [1] The exception is with "fixed" stack objects. "Fixed" stack objects get
85// their name from being allocated at fixed locations on the stack, relative
86// to the FP. In the presence of dynamic allocation and local alignment, such
87// objects can only be accessed through the FP.
88//
89// Illustration of the AP:
90// FP --+
91// |
92// ---------------+---------------------+-----+-----------------------++-+--
93// Rest of the | Local stack objects | Pad | Fixed stack objects |LR|
94// stack frame | (aligned) | | (CSR, spills, etc.) |FP|
95// ---------------+---------------------+-----+-----------------+-----+--+--
96// |<-- Multiple of the -->|
97// stack alignment +-- AP
98//
99// The AP is set up at the beginning of the function. Since it is not a dedi-
100// cated (reserved) register, it needs to be kept live throughout the function
101// to be available as the base register for local object accesses.
102// Normally, an address of a stack objects is obtained by a pseudo-instruction
103// TFR_FI. To access local objects with the AP register present, a different
104// pseudo-instruction needs to be used: TFR_FIA. The TFR_FIA takes one extra
105// argument compared to TFR_FI: the first input register is the AP register.
106// This keeps the register live between its definition and its uses.
107
108// The AP register is originally set up using pseudo-instruction ALIGNA:
109// AP = ALIGNA A
110// where
111// A - required stack alignment
112// The alignment value must be the maximum of all alignments required by
113// any stack object.
114
115// The dynamic allocation uses a pseudo-instruction ALLOCA:
116// Rd = ALLOCA Rs, A
117// where
118// Rd - address of the allocated space
119// Rs - minimum size (the actual allocated can be larger to accommodate
120// alignment)
121// A - required alignment
122
123
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000124using namespace llvm;
125
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000126static cl::opt<bool> DisableDeallocRet("disable-hexagon-dealloc-ret",
127 cl::Hidden, cl::desc("Disable Dealloc Return for Hexagon target"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000128
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000129
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000130static cl::opt<int> NumberScavengerSlots("number-scavenger-slots",
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000131 cl::Hidden, cl::desc("Set the number of scavenger slots"), cl::init(2),
132 cl::ZeroOrMore);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000133
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000134static cl::opt<int> SpillFuncThreshold("spill-func-threshold",
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000135 cl::Hidden, cl::desc("Specify O2(not Os) spill func threshold"),
136 cl::init(6), cl::ZeroOrMore);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000137
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000138static cl::opt<int> SpillFuncThresholdOs("spill-func-threshold-Os",
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000139 cl::Hidden, cl::desc("Specify Os spill func threshold"),
140 cl::init(1), cl::ZeroOrMore);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000141
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000142static cl::opt<bool> EnableShrinkWrapping("hexagon-shrink-frame",
143 cl::init(true), cl::Hidden, cl::ZeroOrMore,
144 cl::desc("Enable stack frame shrink wrapping"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000145
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000146static cl::opt<unsigned> ShrinkLimit("shrink-frame-limit", cl::init(UINT_MAX),
147 cl::Hidden, cl::ZeroOrMore, cl::desc("Max count of stack frame "
148 "shrink-wraps"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000149
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000150static cl::opt<bool> UseAllocframe("use-allocframe", cl::init(true),
151 cl::Hidden, cl::desc("Use allocframe more conservatively"));
152
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000153
154namespace llvm {
155 void initializeHexagonCallFrameInformationPass(PassRegistry&);
156 FunctionPass *createHexagonCallFrameInformation();
157}
158
159namespace {
160 class HexagonCallFrameInformation : public MachineFunctionPass {
161 public:
162 static char ID;
163 HexagonCallFrameInformation() : MachineFunctionPass(ID) {
164 PassRegistry &PR = *PassRegistry::getPassRegistry();
165 initializeHexagonCallFrameInformationPass(PR);
166 }
167 bool runOnMachineFunction(MachineFunction &MF) override;
168 };
169
170 char HexagonCallFrameInformation::ID = 0;
171}
172
173bool HexagonCallFrameInformation::runOnMachineFunction(MachineFunction &MF) {
174 auto &HFI = *MF.getSubtarget<HexagonSubtarget>().getFrameLowering();
175 bool NeedCFI = MF.getMMI().hasDebugInfo() ||
176 MF.getFunction()->needsUnwindTableEntry();
177
178 if (!NeedCFI)
179 return false;
180 HFI.insertCFIInstructions(MF);
181 return true;
182}
183
184INITIALIZE_PASS(HexagonCallFrameInformation, "hexagon-cfi",
185 "Hexagon call frame information", false, false)
186
187FunctionPass *llvm::createHexagonCallFrameInformation() {
188 return new HexagonCallFrameInformation();
189}
190
191
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000192namespace {
193 /// Map a register pair Reg to the subregister that has the greater "number",
194 /// i.e. D3 (aka R7:6) will be mapped to R7, etc.
195 unsigned getMax32BitSubRegister(unsigned Reg, const TargetRegisterInfo &TRI,
196 bool hireg = true) {
197 if (Reg < Hexagon::D0 || Reg > Hexagon::D15)
198 return Reg;
199
200 unsigned RegNo = 0;
201 for (MCSubRegIterator SubRegs(Reg, &TRI); SubRegs.isValid(); ++SubRegs) {
202 if (hireg) {
203 if (*SubRegs > RegNo)
204 RegNo = *SubRegs;
205 } else {
206 if (!RegNo || *SubRegs < RegNo)
207 RegNo = *SubRegs;
208 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000209 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000210 return RegNo;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000211 }
212
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000213 /// Returns the callee saved register with the largest id in the vector.
214 unsigned getMaxCalleeSavedReg(const std::vector<CalleeSavedInfo> &CSI,
215 const TargetRegisterInfo &TRI) {
216 assert(Hexagon::R1 > 0 &&
217 "Assume physical registers are encoded as positive integers");
218 if (CSI.empty())
219 return 0;
220
221 unsigned Max = getMax32BitSubRegister(CSI[0].getReg(), TRI);
222 for (unsigned I = 1, E = CSI.size(); I < E; ++I) {
223 unsigned Reg = getMax32BitSubRegister(CSI[I].getReg(), TRI);
224 if (Reg > Max)
225 Max = Reg;
226 }
227 return Max;
228 }
229
230 /// Checks if the basic block contains any instruction that needs a stack
231 /// frame to be already in place.
232 bool needsStackFrame(const MachineBasicBlock &MBB, const BitVector &CSR) {
233 for (auto &I : MBB) {
234 const MachineInstr *MI = &I;
235 if (MI->isCall())
236 return true;
237 unsigned Opc = MI->getOpcode();
238 switch (Opc) {
239 case Hexagon::ALLOCA:
240 case Hexagon::ALIGNA:
241 return true;
242 default:
243 break;
244 }
245 // Check individual operands.
Matthias Braune41e1462015-05-29 02:56:46 +0000246 for (const MachineOperand &MO : MI->operands()) {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000247 // While the presence of a frame index does not prove that a stack
248 // frame will be required, all frame indexes should be within alloc-
249 // frame/deallocframe. Otherwise, the code that translates a frame
250 // index into an offset would have to be aware of the placement of
251 // the frame creation/destruction instructions.
Matthias Braune41e1462015-05-29 02:56:46 +0000252 if (MO.isFI())
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000253 return true;
Matthias Braune41e1462015-05-29 02:56:46 +0000254 if (!MO.isReg())
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000255 continue;
Matthias Braune41e1462015-05-29 02:56:46 +0000256 unsigned R = MO.getReg();
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000257 // Virtual registers will need scavenging, which then may require
258 // a stack slot.
259 if (TargetRegisterInfo::isVirtualRegister(R))
260 return true;
261 if (CSR[R])
262 return true;
263 }
264 }
265 return false;
266 }
267
268 /// Returns true if MBB has a machine instructions that indicates a tail call
269 /// in the block.
270 bool hasTailCall(const MachineBasicBlock &MBB) {
271 MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr();
272 unsigned RetOpc = I->getOpcode();
273 return RetOpc == Hexagon::TCRETURNi || RetOpc == Hexagon::TCRETURNr;
274 }
275
276 /// Returns true if MBB contains an instruction that returns.
277 bool hasReturn(const MachineBasicBlock &MBB) {
278 for (auto I = MBB.getFirstTerminator(), E = MBB.end(); I != E; ++I)
279 if (I->isReturn())
280 return true;
281 return false;
282 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000283}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000284
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000285
286/// Implements shrink-wrapping of the stack frame. By default, stack frame
287/// is created in the function entry block, and is cleaned up in every block
288/// that returns. This function finds alternate blocks: one for the frame
289/// setup (prolog) and one for the cleanup (epilog).
290void HexagonFrameLowering::findShrunkPrologEpilog(MachineFunction &MF,
291 MachineBasicBlock *&PrologB, MachineBasicBlock *&EpilogB) const {
292 static unsigned ShrinkCounter = 0;
293
294 if (ShrinkLimit.getPosition()) {
295 if (ShrinkCounter >= ShrinkLimit)
296 return;
297 ShrinkCounter++;
298 }
299
300 auto &HST = static_cast<const HexagonSubtarget&>(MF.getSubtarget());
301 auto &HRI = *HST.getRegisterInfo();
302
303 MachineDominatorTree MDT;
304 MDT.runOnMachineFunction(MF);
305 MachinePostDominatorTree MPT;
306 MPT.runOnMachineFunction(MF);
307
308 typedef DenseMap<unsigned,unsigned> UnsignedMap;
309 UnsignedMap RPO;
310 typedef ReversePostOrderTraversal<const MachineFunction*> RPOTType;
311 RPOTType RPOT(&MF);
312 unsigned RPON = 0;
313 for (RPOTType::rpo_iterator I = RPOT.begin(), E = RPOT.end(); I != E; ++I)
314 RPO[(*I)->getNumber()] = RPON++;
315
316 // Don't process functions that have loops, at least for now. Placement
317 // of prolog and epilog must take loop structure into account. For simpli-
318 // city don't do it right now.
319 for (auto &I : MF) {
320 unsigned BN = RPO[I.getNumber()];
321 for (auto SI = I.succ_begin(), SE = I.succ_end(); SI != SE; ++SI) {
322 // If found a back-edge, return.
323 if (RPO[(*SI)->getNumber()] <= BN)
324 return;
325 }
326 }
327
328 // Collect the set of blocks that need a stack frame to execute. Scan
329 // each block for uses/defs of callee-saved registers, calls, etc.
330 SmallVector<MachineBasicBlock*,16> SFBlocks;
331 BitVector CSR(Hexagon::NUM_TARGET_REGS);
332 for (const MCPhysReg *P = HRI.getCalleeSavedRegs(&MF); *P; ++P)
333 CSR[*P] = true;
334
335 for (auto &I : MF)
336 if (needsStackFrame(I, CSR))
337 SFBlocks.push_back(&I);
338
339 DEBUG({
340 dbgs() << "Blocks needing SF: {";
341 for (auto &B : SFBlocks)
342 dbgs() << " BB#" << B->getNumber();
343 dbgs() << " }\n";
344 });
345 // No frame needed?
346 if (SFBlocks.empty())
347 return;
348
349 // Pick a common dominator and a common post-dominator.
350 MachineBasicBlock *DomB = SFBlocks[0];
351 for (unsigned i = 1, n = SFBlocks.size(); i < n; ++i) {
352 DomB = MDT.findNearestCommonDominator(DomB, SFBlocks[i]);
353 if (!DomB)
354 break;
355 }
356 MachineBasicBlock *PDomB = SFBlocks[0];
357 for (unsigned i = 1, n = SFBlocks.size(); i < n; ++i) {
358 PDomB = MPT.findNearestCommonDominator(PDomB, SFBlocks[i]);
359 if (!PDomB)
360 break;
361 }
362 DEBUG({
363 dbgs() << "Computed dom block: BB#";
364 if (DomB) dbgs() << DomB->getNumber();
365 else dbgs() << "<null>";
366 dbgs() << ", computed pdom block: BB#";
367 if (PDomB) dbgs() << PDomB->getNumber();
368 else dbgs() << "<null>";
369 dbgs() << "\n";
370 });
371 if (!DomB || !PDomB)
372 return;
373
374 // Make sure that DomB dominates PDomB and PDomB post-dominates DomB.
375 if (!MDT.dominates(DomB, PDomB)) {
376 DEBUG(dbgs() << "Dom block does not dominate pdom block\n");
377 return;
378 }
379 if (!MPT.dominates(PDomB, DomB)) {
380 DEBUG(dbgs() << "PDom block does not post-dominate dom block\n");
381 return;
382 }
383
384 // Finally, everything seems right.
385 PrologB = DomB;
386 EpilogB = PDomB;
387}
388
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000389/// Perform most of the PEI work here:
390/// - saving/restoring of the callee-saved registers,
391/// - stack frame creation and destruction.
392/// Normally, this work is distributed among various functions, but doing it
393/// in one place allows shrink-wrapping of the stack frame.
Quentin Colombet61b305e2015-05-05 17:38:16 +0000394void HexagonFrameLowering::emitPrologue(MachineFunction &MF,
395 MachineBasicBlock &MBB) const {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000396 auto &HST = static_cast<const HexagonSubtarget&>(MF.getSubtarget());
397 auto &HRI = *HST.getRegisterInfo();
398
Quentin Colombet61b305e2015-05-05 17:38:16 +0000399 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000400 MachineFrameInfo *MFI = MF.getFrameInfo();
401 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
402
403 MachineBasicBlock *PrologB = &MF.front(), *EpilogB = nullptr;
404 if (EnableShrinkWrapping)
405 findShrunkPrologEpilog(MF, PrologB, EpilogB);
406
407 insertCSRSpillsInBlock(*PrologB, CSI, HRI);
408 insertPrologueInBlock(*PrologB);
409
410 if (EpilogB) {
411 insertCSRRestoresInBlock(*EpilogB, CSI, HRI);
412 insertEpilogueInBlock(*EpilogB);
413 } else {
414 for (auto &B : MF)
Matthias Braunc2d4bef2015-09-25 21:25:19 +0000415 if (B.isReturnBlock())
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000416 insertCSRRestoresInBlock(B, CSI, HRI);
417
418 for (auto &B : MF)
Matthias Braunc2d4bef2015-09-25 21:25:19 +0000419 if (B.isReturnBlock())
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000420 insertEpilogueInBlock(B);
421 }
422}
423
424
425void HexagonFrameLowering::insertPrologueInBlock(MachineBasicBlock &MBB) const {
426 MachineFunction &MF = *MBB.getParent();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000427 MachineFrameInfo *MFI = MF.getFrameInfo();
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000428 auto &HST = MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000429 auto &HII = *HST.getInstrInfo();
430 auto &HRI = *HST.getRegisterInfo();
431 DebugLoc dl;
432
433 unsigned MaxAlign = std::max(MFI->getMaxAlignment(), getStackAlignment());
434
435 // Calculate the total stack frame size.
436 // Get the number of bytes to allocate from the FrameInfo.
437 unsigned FrameSize = MFI->getStackSize();
438 // Round up the max call frame size to the max alignment on the stack.
439 unsigned MaxCFA = RoundUpToAlignment(MFI->getMaxCallFrameSize(), MaxAlign);
440 MFI->setMaxCallFrameSize(MaxCFA);
441
442 FrameSize = MaxCFA + RoundUpToAlignment(FrameSize, MaxAlign);
443 MFI->setStackSize(FrameSize);
444
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000445 bool AlignStack = (MaxAlign > getStackAlignment());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000446
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000447 // Get the number of bytes to allocate from the FrameInfo.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000448 unsigned NumBytes = MFI->getStackSize();
449 unsigned SP = HRI.getStackRegister();
450 unsigned MaxCF = MFI->getMaxCallFrameSize();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000451 MachineBasicBlock::iterator InsertPt = MBB.begin();
452
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000453 auto *FuncInfo = MF.getInfo<HexagonMachineFunctionInfo>();
454 auto &AdjustRegs = FuncInfo->getAllocaAdjustInsts();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000455
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000456 for (auto MI : AdjustRegs) {
457 assert((MI->getOpcode() == Hexagon::ALLOCA) && "Expected alloca");
458 expandAlloca(MI, HII, SP, MaxCF);
459 MI->eraseFromParent();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000460 }
461
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000462 if (!hasFP(MF))
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000463 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000464
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000465 // Check for overflow.
466 // Hexagon_TODO: Ugh! hardcoding. Is there an API that can be used?
467 const unsigned int ALLOCFRAME_MAX = 16384;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000468
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000469 // Create a dummy memory operand to avoid allocframe from being treated as
470 // a volatile memory reference.
471 MachineMemOperand *MMO =
472 MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOStore,
473 4, 4);
474
475 if (NumBytes >= ALLOCFRAME_MAX) {
476 // Emit allocframe(#0).
477 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::S2_allocframe))
478 .addImm(0)
479 .addMemOperand(MMO);
480
481 // Subtract offset from frame pointer.
482 // We use a caller-saved non-parameter register for that.
483 unsigned CallerSavedReg = HRI.getFirstCallerSavedNonParamReg();
484 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::CONST32_Int_Real),
485 CallerSavedReg).addImm(NumBytes);
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000486 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_sub), SP)
487 .addReg(SP)
488 .addReg(CallerSavedReg);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000489 } else {
490 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::S2_allocframe))
491 .addImm(NumBytes)
492 .addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000493 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000494
495 if (AlignStack) {
496 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_andir), SP)
497 .addReg(SP)
498 .addImm(-int64_t(MaxAlign));
499 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000500}
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000501
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000502void HexagonFrameLowering::insertEpilogueInBlock(MachineBasicBlock &MBB) const {
503 MachineFunction &MF = *MBB.getParent();
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000504 if (!hasFP(MF))
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000505 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000506
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000507 auto &HST = static_cast<const HexagonSubtarget&>(MF.getSubtarget());
508 auto &HII = *HST.getInstrInfo();
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000509 auto &HRI = *HST.getRegisterInfo();
510 unsigned SP = HRI.getStackRegister();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000511
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000512 MachineInstr *RetI = nullptr;
513 for (auto &I : MBB) {
514 if (!I.isReturn())
515 continue;
516 RetI = &I;
517 break;
518 }
519 unsigned RetOpc = RetI ? RetI->getOpcode() : 0;
520
521 MachineBasicBlock::iterator InsertPt = MBB.getFirstTerminator();
522 DebugLoc DL;
523 if (InsertPt != MBB.end())
524 DL = InsertPt->getDebugLoc();
525 else if (!MBB.empty())
526 DL = std::prev(MBB.end())->getDebugLoc();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000527
528 // Handle EH_RETURN.
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000529 if (RetOpc == Hexagon::EH_RETURN_JMPR) {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000530 BuildMI(MBB, InsertPt, DL, HII.get(Hexagon::L2_deallocframe));
531 BuildMI(MBB, InsertPt, DL, HII.get(Hexagon::A2_add), SP)
532 .addReg(SP)
533 .addReg(Hexagon::R28);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000534 return;
535 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000536
537 // Check for RESTORE_DEALLOC_RET* tail call. Don't emit an extra dealloc-
538 // frame instruction if we encounter it.
539 if (RetOpc == Hexagon::RESTORE_DEALLOC_RET_JMP_V4) {
540 MachineBasicBlock::iterator It = RetI;
541 ++It;
542 // Delete all instructions after the RESTORE (except labels).
543 while (It != MBB.end()) {
544 if (!It->isLabel())
545 It = MBB.erase(It);
546 else
547 ++It;
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000548 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000549 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000550 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000551
552 // It is possible that the restoring code is a call to a library function.
553 // All of the restore* functions include "deallocframe", so we need to make
554 // sure that we don't add an extra one.
555 bool NeedsDeallocframe = true;
556 if (!MBB.empty() && InsertPt != MBB.begin()) {
557 MachineBasicBlock::iterator PrevIt = std::prev(InsertPt);
558 unsigned COpc = PrevIt->getOpcode();
559 if (COpc == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4)
560 NeedsDeallocframe = false;
561 }
562
563 if (!NeedsDeallocframe)
564 return;
565 // If the returning instruction is JMPret, replace it with dealloc_return,
566 // otherwise just add deallocframe. The function could be returning via a
567 // tail call.
568 if (RetOpc != Hexagon::JMPret || DisableDeallocRet) {
569 BuildMI(MBB, InsertPt, DL, HII.get(Hexagon::L2_deallocframe));
570 return;
571 }
572 unsigned NewOpc = Hexagon::L4_return;
573 MachineInstr *NewI = BuildMI(MBB, RetI, DL, HII.get(NewOpc));
574 // Transfer the function live-out registers.
575 NewI->copyImplicitOps(MF, RetI);
576 MBB.erase(RetI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000577}
578
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000579
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000580namespace {
581 bool IsAllocFrame(MachineBasicBlock::const_iterator It) {
582 if (!It->isBundle())
583 return It->getOpcode() == Hexagon::S2_allocframe;
584 auto End = It->getParent()->instr_end();
585 MachineBasicBlock::const_instr_iterator I = It.getInstrIterator();
586 while (++I != End && I->isBundled())
587 if (I->getOpcode() == Hexagon::S2_allocframe)
588 return true;
589 return false;
590 }
591
592 MachineBasicBlock::iterator FindAllocFrame(MachineBasicBlock &B) {
593 for (auto &I : B)
594 if (IsAllocFrame(I))
595 return I;
596 return B.end();
597 }
598}
599
600
601void HexagonFrameLowering::insertCFIInstructions(MachineFunction &MF) const {
602 for (auto &B : MF) {
603 auto AF = FindAllocFrame(B);
604 if (AF == B.end())
605 continue;
606 insertCFIInstructionsAt(B, ++AF);
607 }
608}
609
610
611void HexagonFrameLowering::insertCFIInstructionsAt(MachineBasicBlock &MBB,
612 MachineBasicBlock::iterator At) const {
613 MachineFunction &MF = *MBB.getParent();
614 MachineFrameInfo *MFI = MF.getFrameInfo();
615 MachineModuleInfo &MMI = MF.getMMI();
616 auto &HST = MF.getSubtarget<HexagonSubtarget>();
617 auto &HII = *HST.getInstrInfo();
618 auto &HRI = *HST.getRegisterInfo();
619
620 // If CFI instructions have debug information attached, something goes
621 // wrong with the final assembly generation: the prolog_end is placed
622 // in a wrong location.
623 DebugLoc DL;
624 const MCInstrDesc &CFID = HII.get(TargetOpcode::CFI_INSTRUCTION);
625
626 MCSymbol *FrameLabel = MMI.getContext().createTempSymbol();
627
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000628 if (hasFP(MF)) {
629 unsigned DwFPReg = HRI.getDwarfRegNum(HRI.getFrameRegister(), true);
630 unsigned DwRAReg = HRI.getDwarfRegNum(HRI.getRARegister(), true);
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000631
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000632 // Define CFA via an offset from the value of FP.
633 //
634 // -8 -4 0 (SP)
635 // --+----+----+---------------------
636 // | FP | LR | increasing addresses -->
637 // --+----+----+---------------------
638 // | +-- Old SP (before allocframe)
639 // +-- New FP (after allocframe)
640 //
641 // MCCFIInstruction::createDefCfa subtracts the offset from the register.
642 // MCCFIInstruction::createOffset takes the offset without sign change.
643 auto DefCfa = MCCFIInstruction::createDefCfa(FrameLabel, DwFPReg, -8);
644 BuildMI(MBB, At, DL, CFID)
645 .addCFIIndex(MMI.addFrameInst(DefCfa));
646 // R31 (return addr) = CFA - 4
647 auto OffR31 = MCCFIInstruction::createOffset(FrameLabel, DwRAReg, -4);
648 BuildMI(MBB, At, DL, CFID)
649 .addCFIIndex(MMI.addFrameInst(OffR31));
650 // R30 (frame ptr) = CFA - 8
651 auto OffR30 = MCCFIInstruction::createOffset(FrameLabel, DwFPReg, -8);
652 BuildMI(MBB, At, DL, CFID)
653 .addCFIIndex(MMI.addFrameInst(OffR30));
654 }
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000655
656 static unsigned int RegsToMove[] = {
657 Hexagon::R1, Hexagon::R0, Hexagon::R3, Hexagon::R2,
658 Hexagon::R17, Hexagon::R16, Hexagon::R19, Hexagon::R18,
659 Hexagon::R21, Hexagon::R20, Hexagon::R23, Hexagon::R22,
660 Hexagon::R25, Hexagon::R24, Hexagon::R27, Hexagon::R26,
661 Hexagon::D0, Hexagon::D1, Hexagon::D8, Hexagon::D9,
662 Hexagon::D10, Hexagon::D11, Hexagon::D12, Hexagon::D13,
663 Hexagon::NoRegister
664 };
665
666 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
667
668 for (unsigned i = 0; RegsToMove[i] != Hexagon::NoRegister; ++i) {
669 unsigned Reg = RegsToMove[i];
670 auto IfR = [Reg] (const CalleeSavedInfo &C) -> bool {
671 return C.getReg() == Reg;
672 };
673 auto F = std::find_if(CSI.begin(), CSI.end(), IfR);
674 if (F == CSI.end())
675 continue;
676
677 // Subtract 8 to make room for R30 and R31, which are added above.
678 unsigned FrameReg;
679 int64_t Offset = getFrameIndexReference(MF, F->getFrameIdx(), FrameReg) - 8;
680
681 if (Reg < Hexagon::D0 || Reg > Hexagon::D15) {
682 unsigned DwarfReg = HRI.getDwarfRegNum(Reg, true);
683 auto OffReg = MCCFIInstruction::createOffset(FrameLabel, DwarfReg,
684 Offset);
685 BuildMI(MBB, At, DL, CFID)
686 .addCFIIndex(MMI.addFrameInst(OffReg));
687 } else {
688 // Split the double regs into subregs, and generate appropriate
689 // cfi_offsets.
690 // The only reason, we are split double regs is, llvm-mc does not
691 // understand paired registers for cfi_offset.
692 // Eg .cfi_offset r1:0, -64
693
694 unsigned HiReg = HRI.getSubReg(Reg, Hexagon::subreg_hireg);
695 unsigned LoReg = HRI.getSubReg(Reg, Hexagon::subreg_loreg);
696 unsigned HiDwarfReg = HRI.getDwarfRegNum(HiReg, true);
697 unsigned LoDwarfReg = HRI.getDwarfRegNum(LoReg, true);
698 auto OffHi = MCCFIInstruction::createOffset(FrameLabel, HiDwarfReg,
699 Offset+4);
700 BuildMI(MBB, At, DL, CFID)
701 .addCFIIndex(MMI.addFrameInst(OffHi));
702 auto OffLo = MCCFIInstruction::createOffset(FrameLabel, LoDwarfReg,
703 Offset);
704 BuildMI(MBB, At, DL, CFID)
705 .addCFIIndex(MMI.addFrameInst(OffLo));
706 }
707 }
708}
709
710
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000711bool HexagonFrameLowering::hasFP(const MachineFunction &MF) const {
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000712 auto &MFI = *MF.getFrameInfo();
713 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
714
715 bool HasFixed = MFI.getNumFixedObjects();
716 bool HasPrealloc = const_cast<MachineFrameInfo&>(MFI)
717 .getLocalFrameObjectCount();
718 bool HasExtraAlign = HRI.needsStackRealignment(MF);
719 bool HasAlloca = MFI.hasVarSizedObjects();
720
721 // Insert ALLOCFRAME if we need to or at -O0 for the debugger. Think
722 // that this shouldn't be required, but doing so now because gcc does and
723 // gdb can't break at the start of the function without it. Will remove if
724 // this turns out to be a gdb bug.
725 //
726 if (MF.getTarget().getOptLevel() == CodeGenOpt::None)
727 return true;
728
729 // By default we want to use SP (since it's always there). FP requires
730 // some setup (i.e. ALLOCFRAME).
731 // Fixed and preallocated objects need FP if the distance from them to
732 // the SP is unknown (as is with alloca or aligna).
733 if ((HasFixed || HasPrealloc) && (HasAlloca || HasExtraAlign))
734 return true;
735
736 if (MFI.getStackSize() > 0) {
737 if (UseAllocframe)
738 return true;
739 }
740
741 if (MFI.hasCalls() ||
742 MF.getInfo<HexagonMachineFunctionInfo>()->hasClobberLR())
743 return true;
744
745 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000746}
747
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000748
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000749enum SpillKind {
750 SK_ToMem,
751 SK_FromMem,
752 SK_FromMemTailcall
753};
754
755static const char *
756getSpillFunctionFor(unsigned MaxReg, SpillKind SpillType) {
757 const char * V4SpillToMemoryFunctions[] = {
758 "__save_r16_through_r17",
759 "__save_r16_through_r19",
760 "__save_r16_through_r21",
761 "__save_r16_through_r23",
762 "__save_r16_through_r25",
763 "__save_r16_through_r27" };
764
765 const char * V4SpillFromMemoryFunctions[] = {
766 "__restore_r16_through_r17_and_deallocframe",
767 "__restore_r16_through_r19_and_deallocframe",
768 "__restore_r16_through_r21_and_deallocframe",
769 "__restore_r16_through_r23_and_deallocframe",
770 "__restore_r16_through_r25_and_deallocframe",
771 "__restore_r16_through_r27_and_deallocframe" };
772
773 const char * V4SpillFromMemoryTailcallFunctions[] = {
774 "__restore_r16_through_r17_and_deallocframe_before_tailcall",
775 "__restore_r16_through_r19_and_deallocframe_before_tailcall",
776 "__restore_r16_through_r21_and_deallocframe_before_tailcall",
777 "__restore_r16_through_r23_and_deallocframe_before_tailcall",
778 "__restore_r16_through_r25_and_deallocframe_before_tailcall",
779 "__restore_r16_through_r27_and_deallocframe_before_tailcall"
780 };
781
782 const char **SpillFunc = nullptr;
783
784 switch(SpillType) {
785 case SK_ToMem:
786 SpillFunc = V4SpillToMemoryFunctions;
787 break;
788 case SK_FromMem:
789 SpillFunc = V4SpillFromMemoryFunctions;
790 break;
791 case SK_FromMemTailcall:
792 SpillFunc = V4SpillFromMemoryTailcallFunctions;
793 break;
794 }
795 assert(SpillFunc && "Unknown spill kind");
796
797 // Spill all callee-saved registers up to the highest register used.
798 switch (MaxReg) {
799 case Hexagon::R17:
800 return SpillFunc[0];
801 case Hexagon::R19:
802 return SpillFunc[1];
803 case Hexagon::R21:
804 return SpillFunc[2];
805 case Hexagon::R23:
806 return SpillFunc[3];
807 case Hexagon::R25:
808 return SpillFunc[4];
809 case Hexagon::R27:
810 return SpillFunc[5];
811 default:
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000812 llvm_unreachable("Unhandled maximum callee save register");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000813 }
814 return 0;
815}
816
817/// Adds all callee-saved registers up to MaxReg to the instruction.
818static void addCalleeSaveRegistersAsImpOperand(MachineInstr *Inst,
819 unsigned MaxReg, bool IsDef) {
820 // Add the callee-saved registers as implicit uses.
821 for (unsigned R = Hexagon::R16; R <= MaxReg; ++R) {
822 MachineOperand ImpUse = MachineOperand::CreateReg(R, IsDef, true);
823 Inst->addOperand(ImpUse);
824 }
825}
826
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000827
James Y Knight5567baf2015-08-15 02:32:35 +0000828int HexagonFrameLowering::getFrameIndexReference(const MachineFunction &MF,
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000829 int FI, unsigned &FrameReg) const {
830 auto &MFI = *MF.getFrameInfo();
831 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000832
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000833 // Large parts of this code are shared with HRI::eliminateFrameIndex.
834 int Offset = MFI.getObjectOffset(FI);
835 bool HasAlloca = MFI.hasVarSizedObjects();
836 bool HasExtraAlign = HRI.needsStackRealignment(MF);
837 bool NoOpt = MF.getTarget().getOptLevel() == CodeGenOpt::None;
James Y Knight5567baf2015-08-15 02:32:35 +0000838
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000839 unsigned SP = HRI.getStackRegister(), FP = HRI.getFrameRegister();
840 unsigned AP = 0;
841 if (const MachineInstr *AI = getAlignaInstr(MF))
842 AP = AI->getOperand(0).getReg();
843 unsigned FrameSize = MFI.getStackSize();
844
845 bool UseFP = false, UseAP = false; // Default: use SP (except at -O0).
846 // Use FP at -O0, except when there are objects with extra alignment.
847 // That additional alignment requirement may cause a pad to be inserted,
848 // which will make it impossible to use FP to access objects located
849 // past the pad.
850 if (NoOpt && !HasExtraAlign)
851 UseFP = true;
852 if (MFI.isFixedObjectIndex(FI) || MFI.isObjectPreAllocated(FI)) {
853 // Fixed and preallocated objects will be located before any padding
854 // so FP must be used to access them.
855 UseFP |= (HasAlloca || HasExtraAlign);
856 } else {
857 if (HasAlloca) {
858 if (HasExtraAlign)
859 UseAP = true;
860 else
861 UseFP = true;
862 }
863 }
864
865 // If FP was picked, then there had better be FP.
866 bool HasFP = hasFP(MF);
867 assert((HasFP || !UseFP) && "This function must have frame pointer");
868
869 // Having FP implies allocframe. Allocframe will store extra 8 bytes:
870 // FP/LR. If the base register is used to access an object across these
871 // 8 bytes, then the offset will need to be adjusted by 8.
872 //
873 // After allocframe:
874 // HexagonISelLowering adds 8 to ---+
875 // the offsets of all stack-based |
876 // arguments (*) |
877 // |
878 // getObjectOffset < 0 0 8 getObjectOffset >= 8
879 // ------------------------+-----+------------------------> increasing
880 // <local objects> |FP/LR| <input arguments> addresses
881 // -----------------+------+-----+------------------------>
882 // | |
883 // SP/AP point --+ +-- FP points here (**)
884 // somewhere on
885 // this side of FP/LR
886 //
887 // (*) See LowerFormalArguments. The FP/LR is assumed to be present.
888 // (**) *FP == old-FP. FP+0..7 are the bytes of FP/LR.
889
890 // The lowering assumes that FP/LR is present, and so the offsets of
891 // the formal arguments start at 8. If FP/LR is not there we need to
892 // reduce the offset by 8.
893 if (Offset > 0 && !HasFP)
894 Offset -= 8;
895
896 if (UseFP)
897 FrameReg = FP;
898 else if (UseAP)
899 FrameReg = AP;
900 else
901 FrameReg = SP;
902
903 // Calculate the actual offset in the instruction. If there is no FP
904 // (in other words, no allocframe), then SP will not be adjusted (i.e.
905 // there will be no SP -= FrameSize), so the frame size should not be
906 // added to the calculated offset.
907 int RealOffset = Offset;
908 if (!UseFP && !UseAP && HasFP)
909 RealOffset = FrameSize+Offset;
910 return RealOffset;
Jakob Stoklund Olesen0b97dbc2012-05-30 22:40:03 +0000911}
912
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000913
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000914bool HexagonFrameLowering::insertCSRSpillsInBlock(MachineBasicBlock &MBB,
915 const CSIVect &CSI, const HexagonRegisterInfo &HRI) const {
916 if (CSI.empty())
917 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000918
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000919 MachineBasicBlock::iterator MI = MBB.begin();
920 MachineFunction &MF = *MBB.getParent();
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000921 auto &HII = *MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000922
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000923 if (useSpillFunction(MF, CSI)) {
924 unsigned MaxReg = getMaxCalleeSavedReg(CSI, HRI);
925 const char *SpillFun = getSpillFunctionFor(MaxReg, SK_ToMem);
926 // Call spill function.
927 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
928 MachineInstr *SaveRegsCall =
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000929 BuildMI(MBB, MI, DL, HII.get(Hexagon::SAVE_REGISTERS_CALL_V4))
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000930 .addExternalSymbol(SpillFun);
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000931 // Add callee-saved registers as use.
932 addCalleeSaveRegistersAsImpOperand(SaveRegsCall, MaxReg, false);
933 // Add live in registers.
934 for (unsigned I = 0; I < CSI.size(); ++I)
935 MBB.addLiveIn(CSI[I].getReg());
936 return true;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000937 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000938
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000939 for (unsigned i = 0, n = CSI.size(); i < n; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000940 unsigned Reg = CSI[i].getReg();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000941 // Add live in registers. We treat eh_return callee saved register r0 - r3
942 // specially. They are not really callee saved registers as they are not
943 // supposed to be killed.
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000944 bool IsKill = !HRI.isEHReturnCalleeSaveReg(Reg);
945 int FI = CSI[i].getFrameIdx();
946 const TargetRegisterClass *RC = HRI.getMinimalPhysRegClass(Reg);
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000947 HII.storeRegToStackSlot(MBB, MI, Reg, IsKill, FI, RC, &HRI);
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000948 if (IsKill)
949 MBB.addLiveIn(Reg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000950 }
951 return true;
952}
953
954
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000955bool HexagonFrameLowering::insertCSRRestoresInBlock(MachineBasicBlock &MBB,
956 const CSIVect &CSI, const HexagonRegisterInfo &HRI) const {
957 if (CSI.empty())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000958 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000959
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000960 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
961 MachineFunction &MF = *MBB.getParent();
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000962 auto &HII = *MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000963
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000964 if (useRestoreFunction(MF, CSI)) {
965 bool HasTC = hasTailCall(MBB) || !hasReturn(MBB);
966 unsigned MaxR = getMaxCalleeSavedReg(CSI, HRI);
967 SpillKind Kind = HasTC ? SK_FromMemTailcall : SK_FromMem;
968 const char *RestoreFn = getSpillFunctionFor(MaxR, Kind);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000969
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000970 // Call spill function.
971 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc()
972 : MBB.getLastNonDebugInstr()->getDebugLoc();
973 MachineInstr *DeallocCall = nullptr;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000974
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000975 if (HasTC) {
976 unsigned ROpc = Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4;
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000977 DeallocCall = BuildMI(MBB, MI, DL, HII.get(ROpc))
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000978 .addExternalSymbol(RestoreFn);
979 } else {
980 // The block has a return.
981 MachineBasicBlock::iterator It = MBB.getFirstTerminator();
982 assert(It->isReturn() && std::next(It) == MBB.end());
983 unsigned ROpc = Hexagon::RESTORE_DEALLOC_RET_JMP_V4;
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000984 DeallocCall = BuildMI(MBB, It, DL, HII.get(ROpc))
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000985 .addExternalSymbol(RestoreFn);
986 // Transfer the function live-out registers.
987 DeallocCall->copyImplicitOps(MF, It);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000988 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000989 addCalleeSaveRegistersAsImpOperand(DeallocCall, MaxR, true);
990 return true;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000991 }
992
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000993 for (unsigned i = 0; i < CSI.size(); ++i) {
994 unsigned Reg = CSI[i].getReg();
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000995 const TargetRegisterClass *RC = HRI.getMinimalPhysRegClass(Reg);
996 int FI = CSI[i].getFrameIdx();
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000997 HII.loadRegFromStackSlot(MBB, MI, Reg, FI, RC, &HRI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000998 }
999 return true;
1000}
1001
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001002
1003void HexagonFrameLowering::eliminateCallFramePseudoInstr(MachineFunction &MF,
1004 MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const {
Eli Bendersky8da87162013-02-21 20:05:00 +00001005 MachineInstr &MI = *I;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001006 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszeke5689672015-04-23 20:26:21 +00001007 (void)Opc; // Silence compiler warning.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001008 assert((Opc == Hexagon::ADJCALLSTACKDOWN || Opc == Hexagon::ADJCALLSTACKUP) &&
1009 "Cannot handle this call frame pseudo instruction");
Eli Bendersky8da87162013-02-21 20:05:00 +00001010 MBB.erase(I);
1011}
1012
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001013
1014void HexagonFrameLowering::processFunctionBeforeFrameFinalized(
1015 MachineFunction &MF, RegScavenger *RS) const {
1016 // If this function has uses aligned stack and also has variable sized stack
1017 // objects, then we need to map all spill slots to fixed positions, so that
1018 // they can be accessed through FP. Otherwise they would have to be accessed
1019 // via AP, which may not be available at the particular place in the program.
1020 MachineFrameInfo *MFI = MF.getFrameInfo();
1021 bool HasAlloca = MFI->hasVarSizedObjects();
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001022 bool NeedsAlign = (MFI->getMaxAlignment() > getStackAlignment());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001023
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001024 if (!HasAlloca || !NeedsAlign)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001025 return;
1026
1027 unsigned LFS = MFI->getLocalFrameSize();
1028 int Offset = -LFS;
1029 for (int i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
1030 if (!MFI->isSpillSlotObjectIndex(i) || MFI->isDeadObjectIndex(i))
1031 continue;
1032 int S = MFI->getObjectSize(i);
1033 LFS += S;
1034 Offset -= S;
1035 MFI->mapLocalFrameObject(i, Offset);
1036 }
1037
1038 MFI->setLocalFrameSize(LFS);
1039 unsigned A = MFI->getLocalFrameMaxAlign();
1040 assert(A <= 8 && "Unexpected local frame alignment");
1041 if (A == 0)
1042 MFI->setLocalFrameMaxAlign(8);
1043 MFI->setUseLocalStackAllocationBlock(true);
1044}
1045
1046/// Returns true if there is no caller saved registers available.
1047static bool needToReserveScavengingSpillSlots(MachineFunction &MF,
1048 const HexagonRegisterInfo &HRI) {
1049 MachineRegisterInfo &MRI = MF.getRegInfo();
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001050 const MCPhysReg *CallerSavedRegs = HRI.getCallerSavedRegs(&MF);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001051 // Check for an unused caller-saved register.
1052 for ( ; *CallerSavedRegs; ++CallerSavedRegs) {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001053 MCPhysReg FreeReg = *CallerSavedRegs;
Matthias Braun9912bb82015-07-14 17:52:07 +00001054 if (!MRI.reg_nodbg_empty(FreeReg))
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001055 continue;
1056
1057 // Check aliased register usage.
1058 bool IsCurrentRegUsed = false;
1059 for (MCRegAliasIterator AI(FreeReg, &HRI, false); AI.isValid(); ++AI)
Matthias Braun9912bb82015-07-14 17:52:07 +00001060 if (!MRI.reg_nodbg_empty(*AI)) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001061 IsCurrentRegUsed = true;
1062 break;
1063 }
1064 if (IsCurrentRegUsed)
1065 continue;
1066
1067 // Neither directly used nor used through an aliased register.
1068 return false;
1069 }
1070 // All caller-saved registers are used.
1071 return true;
1072}
1073
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001074
1075/// Replaces the predicate spill code pseudo instructions by valid instructions.
1076bool HexagonFrameLowering::replacePredRegPseudoSpillCode(MachineFunction &MF)
1077 const {
1078 auto &HST = static_cast<const HexagonSubtarget&>(MF.getSubtarget());
1079 auto &HII = *HST.getInstrInfo();
1080 MachineRegisterInfo &MRI = MF.getRegInfo();
1081 bool HasReplacedPseudoInst = false;
1082 // Replace predicate spill pseudo instructions by real code.
1083 // Loop over all of the basic blocks.
1084 for (MachineFunction::iterator MBBb = MF.begin(), MBBe = MF.end();
1085 MBBb != MBBe; ++MBBb) {
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +00001086 MachineBasicBlock *MBB = &*MBBb;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001087 // Traverse the basic block.
1088 MachineBasicBlock::iterator NextII;
1089 for (MachineBasicBlock::iterator MII = MBB->begin(); MII != MBB->end();
1090 MII = NextII) {
1091 MachineInstr *MI = MII;
1092 NextII = std::next(MII);
1093 int Opc = MI->getOpcode();
1094 if (Opc == Hexagon::STriw_pred) {
1095 HasReplacedPseudoInst = true;
1096 // STriw_pred FI, 0, SrcReg;
1097 unsigned VirtReg = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
1098 unsigned SrcReg = MI->getOperand(2).getReg();
1099 bool IsOrigSrcRegKilled = MI->getOperand(2).isKill();
1100
1101 assert(MI->getOperand(0).isFI() && "Expect a frame index");
1102 assert(Hexagon::PredRegsRegClass.contains(SrcReg) &&
1103 "Not a predicate register");
1104
1105 // Insert transfer to general purpose register.
1106 // VirtReg = C2_tfrpr SrcPredReg
1107 BuildMI(*MBB, MII, MI->getDebugLoc(), HII.get(Hexagon::C2_tfrpr),
1108 VirtReg).addReg(SrcReg, getKillRegState(IsOrigSrcRegKilled));
1109
1110 // Change instruction to S2_storeri_io.
1111 // S2_storeri_io FI, 0, VirtReg
1112 MI->setDesc(HII.get(Hexagon::S2_storeri_io));
1113 MI->getOperand(2).setReg(VirtReg);
1114 MI->getOperand(2).setIsKill();
1115
1116 } else if (Opc == Hexagon::LDriw_pred) {
1117 // DstReg = LDriw_pred FI, 0
1118 MachineOperand &M0 = MI->getOperand(0);
1119 if (M0.isDead()) {
1120 MBB->erase(MII);
1121 continue;
1122 }
1123
1124 unsigned VirtReg = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
1125 unsigned DestReg = MI->getOperand(0).getReg();
1126
1127 assert(MI->getOperand(1).isFI() && "Expect a frame index");
1128 assert(Hexagon::PredRegsRegClass.contains(DestReg) &&
1129 "Not a predicate register");
1130
1131 // Change instruction to L2_loadri_io.
1132 // VirtReg = L2_loadri_io FI, 0
1133 MI->setDesc(HII.get(Hexagon::L2_loadri_io));
1134 MI->getOperand(0).setReg(VirtReg);
1135
1136 // Insert transfer to general purpose register.
1137 // DestReg = C2_tfrrp VirtReg
1138 const MCInstrDesc &D = HII.get(Hexagon::C2_tfrrp);
1139 BuildMI(*MBB, std::next(MII), MI->getDebugLoc(), D, DestReg)
1140 .addReg(VirtReg, getKillRegState(true));
1141 HasReplacedPseudoInst = true;
1142 }
1143 }
1144 }
1145 return HasReplacedPseudoInst;
1146}
1147
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001148
Matthias Braun02564862015-07-14 17:17:13 +00001149void HexagonFrameLowering::determineCalleeSaves(MachineFunction &MF,
1150 BitVector &SavedRegs,
1151 RegScavenger *RS) const {
1152 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1153
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001154 auto &HST = static_cast<const HexagonSubtarget&>(MF.getSubtarget());
1155 auto &HRI = *HST.getRegisterInfo();
1156
1157 bool HasEHReturn = MF.getInfo<HexagonMachineFunctionInfo>()->hasEHReturn();
1158
1159 // If we have a function containing __builtin_eh_return we want to spill and
1160 // restore all callee saved registers. Pretend that they are used.
1161 if (HasEHReturn) {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001162 for (const MCPhysReg *CSRegs = HRI.getCalleeSavedRegs(&MF); *CSRegs;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001163 ++CSRegs)
Matthias Braun02564862015-07-14 17:17:13 +00001164 SavedRegs.set(*CSRegs);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001165 }
1166
1167 const TargetRegisterClass &RC = Hexagon::IntRegsRegClass;
1168
1169 // Replace predicate register pseudo spill code.
1170 bool HasReplacedPseudoInst = replacePredRegPseudoSpillCode(MF);
1171
1172 // We need to reserve a a spill slot if scavenging could potentially require
1173 // spilling a scavenged register.
1174 if (HasReplacedPseudoInst && needToReserveScavengingSpillSlots(MF, HRI)) {
1175 MachineFrameInfo *MFI = MF.getFrameInfo();
1176 for (int i=0; i < NumberScavengerSlots; i++)
1177 RS->addScavengingFrameIndex(
1178 MFI->CreateSpillStackObject(RC.getSize(), RC.getAlignment()));
1179 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001180}
1181
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001182
1183#ifndef NDEBUG
Krzysztof Parzyszeke5689672015-04-23 20:26:21 +00001184static void dump_registers(BitVector &Regs, const TargetRegisterInfo &TRI) {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001185 dbgs() << '{';
1186 for (int x = Regs.find_first(); x >= 0; x = Regs.find_next(x)) {
1187 unsigned R = x;
1188 dbgs() << ' ' << PrintReg(R, &TRI);
1189 }
1190 dbgs() << " }";
1191}
1192#endif
1193
1194
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001195bool HexagonFrameLowering::assignCalleeSavedSpillSlots(MachineFunction &MF,
1196 const TargetRegisterInfo *TRI, std::vector<CalleeSavedInfo> &CSI) const {
Krzysztof Parzyszek27ba19a12015-04-23 20:42:20 +00001197 DEBUG(dbgs() << LLVM_FUNCTION_NAME << " on "
Krzysztof Parzyszeked75e7a2015-04-23 20:57:39 +00001198 << MF.getFunction()->getName() << '\n');
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001199 MachineFrameInfo *MFI = MF.getFrameInfo();
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001200 BitVector SRegs(Hexagon::NUM_TARGET_REGS);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001201
1202 // Generate a set of unique, callee-saved registers (SRegs), where each
1203 // register in the set is maximal in terms of sub-/super-register relation,
1204 // i.e. for each R in SRegs, no proper super-register of R is also in SRegs.
1205
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001206 // (1) For each callee-saved register, add that register and all of its
1207 // sub-registers to SRegs.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001208 DEBUG(dbgs() << "Initial CS registers: {");
1209 for (unsigned i = 0, n = CSI.size(); i < n; ++i) {
1210 unsigned R = CSI[i].getReg();
1211 DEBUG(dbgs() << ' ' << PrintReg(R, TRI));
1212 for (MCSubRegIterator SR(R, TRI, true); SR.isValid(); ++SR)
1213 SRegs[*SR] = true;
1214 }
1215 DEBUG(dbgs() << " }\n");
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001216 DEBUG(dbgs() << "SRegs.1: "; dump_registers(SRegs, *TRI); dbgs() << "\n");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001217
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001218 // (2) For each reserved register, remove that register and all of its
1219 // sub- and super-registers from SRegs.
1220 BitVector Reserved = TRI->getReservedRegs(MF);
1221 for (int x = Reserved.find_first(); x >= 0; x = Reserved.find_next(x)) {
1222 unsigned R = x;
1223 for (MCSuperRegIterator SR(R, TRI, true); SR.isValid(); ++SR)
1224 SRegs[*SR] = false;
1225 }
1226 DEBUG(dbgs() << "Res: "; dump_registers(Reserved, *TRI); dbgs() << "\n");
1227 DEBUG(dbgs() << "SRegs.2: "; dump_registers(SRegs, *TRI); dbgs() << "\n");
1228
1229 // (3) Collect all registers that have at least one sub-register in SRegs,
1230 // and also have no sub-registers that are reserved. These will be the can-
1231 // didates for saving as a whole instead of their individual sub-registers.
1232 // (Saving R17:16 instead of R16 is fine, but only if R17 was not reserved.)
1233 BitVector TmpSup(Hexagon::NUM_TARGET_REGS);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001234 for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) {
1235 unsigned R = x;
1236 for (MCSuperRegIterator SR(R, TRI); SR.isValid(); ++SR)
1237 TmpSup[*SR] = true;
1238 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001239 for (int x = TmpSup.find_first(); x >= 0; x = TmpSup.find_next(x)) {
1240 unsigned R = x;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001241 for (MCSubRegIterator SR(R, TRI, true); SR.isValid(); ++SR) {
1242 if (!Reserved[*SR])
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001243 continue;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001244 TmpSup[R] = false;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001245 break;
1246 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001247 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001248 DEBUG(dbgs() << "TmpSup: "; dump_registers(TmpSup, *TRI); dbgs() << "\n");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001249
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001250 // (4) Include all super-registers found in (3) into SRegs.
1251 SRegs |= TmpSup;
1252 DEBUG(dbgs() << "SRegs.4: "; dump_registers(SRegs, *TRI); dbgs() << "\n");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001253
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001254 // (5) For each register R in SRegs, if any super-register of R is in SRegs,
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001255 // remove R from SRegs.
1256 for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) {
1257 unsigned R = x;
1258 for (MCSuperRegIterator SR(R, TRI); SR.isValid(); ++SR) {
1259 if (!SRegs[*SR])
1260 continue;
1261 SRegs[R] = false;
1262 break;
1263 }
1264 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001265 DEBUG(dbgs() << "SRegs.5: "; dump_registers(SRegs, *TRI); dbgs() << "\n");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001266
1267 // Now, for each register that has a fixed stack slot, create the stack
1268 // object for it.
1269 CSI.clear();
1270
1271 typedef TargetFrameLowering::SpillSlot SpillSlot;
1272 unsigned NumFixed;
1273 int MinOffset = 0; // CS offsets are negative.
1274 const SpillSlot *FixedSlots = getCalleeSavedSpillSlots(NumFixed);
1275 for (const SpillSlot *S = FixedSlots; S != FixedSlots+NumFixed; ++S) {
1276 if (!SRegs[S->Reg])
1277 continue;
1278 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(S->Reg);
1279 int FI = MFI->CreateFixedSpillStackObject(RC->getSize(), S->Offset);
1280 MinOffset = std::min(MinOffset, S->Offset);
1281 CSI.push_back(CalleeSavedInfo(S->Reg, FI));
1282 SRegs[S->Reg] = false;
1283 }
1284
1285 // There can be some registers that don't have fixed slots. For example,
1286 // we need to store R0-R3 in functions with exception handling. For each
1287 // such register, create a non-fixed stack object.
1288 for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) {
1289 unsigned R = x;
1290 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(R);
1291 int Off = MinOffset - RC->getSize();
1292 unsigned Align = std::min(RC->getAlignment(), getStackAlignment());
1293 assert(isPowerOf2_32(Align));
1294 Off &= -Align;
1295 int FI = MFI->CreateFixedSpillStackObject(RC->getSize(), Off);
1296 MinOffset = std::min(MinOffset, Off);
1297 CSI.push_back(CalleeSavedInfo(R, FI));
1298 SRegs[R] = false;
1299 }
1300
1301 DEBUG({
1302 dbgs() << "CS information: {";
1303 for (unsigned i = 0, n = CSI.size(); i < n; ++i) {
1304 int FI = CSI[i].getFrameIdx();
1305 int Off = MFI->getObjectOffset(FI);
1306 dbgs() << ' ' << PrintReg(CSI[i].getReg(), TRI) << ":fi#" << FI << ":sp";
1307 if (Off >= 0)
1308 dbgs() << '+';
1309 dbgs() << Off;
1310 }
1311 dbgs() << " }\n";
1312 });
1313
1314#ifndef NDEBUG
1315 // Verify that all registers were handled.
1316 bool MissedReg = false;
1317 for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) {
1318 unsigned R = x;
1319 dbgs() << PrintReg(R, TRI) << ' ';
1320 MissedReg = true;
1321 }
1322 if (MissedReg)
1323 llvm_unreachable("...there are unhandled callee-saved registers!");
1324#endif
1325
1326 return true;
1327}
1328
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001329
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001330void HexagonFrameLowering::expandAlloca(MachineInstr *AI,
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001331 const HexagonInstrInfo &HII, unsigned SP, unsigned CF) const {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001332 MachineBasicBlock &MB = *AI->getParent();
1333 DebugLoc DL = AI->getDebugLoc();
1334 unsigned A = AI->getOperand(2).getImm();
1335
1336 // Have
1337 // Rd = alloca Rs, #A
1338 //
1339 // If Rs and Rd are different registers, use this sequence:
1340 // Rd = sub(r29, Rs)
1341 // r29 = sub(r29, Rs)
1342 // Rd = and(Rd, #-A) ; if necessary
1343 // r29 = and(r29, #-A) ; if necessary
1344 // Rd = add(Rd, #CF) ; CF size aligned to at most A
1345 // otherwise, do
1346 // Rd = sub(r29, Rs)
1347 // Rd = and(Rd, #-A) ; if necessary
1348 // r29 = Rd
1349 // Rd = add(Rd, #CF) ; CF size aligned to at most A
1350
1351 MachineOperand &RdOp = AI->getOperand(0);
1352 MachineOperand &RsOp = AI->getOperand(1);
1353 unsigned Rd = RdOp.getReg(), Rs = RsOp.getReg();
1354
1355 // Rd = sub(r29, Rs)
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001356 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_sub), Rd)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001357 .addReg(SP)
1358 .addReg(Rs);
1359 if (Rs != Rd) {
1360 // r29 = sub(r29, Rs)
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001361 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_sub), SP)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001362 .addReg(SP)
1363 .addReg(Rs);
1364 }
1365 if (A > 8) {
1366 // Rd = and(Rd, #-A)
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001367 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_andir), Rd)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001368 .addReg(Rd)
1369 .addImm(-int64_t(A));
1370 if (Rs != Rd)
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001371 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_andir), SP)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001372 .addReg(SP)
1373 .addImm(-int64_t(A));
1374 }
1375 if (Rs == Rd) {
1376 // r29 = Rd
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001377 BuildMI(MB, AI, DL, HII.get(TargetOpcode::COPY), SP)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001378 .addReg(Rd);
1379 }
1380 if (CF > 0) {
1381 // Rd = add(Rd, #CF)
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001382 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_addi), Rd)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001383 .addReg(Rd)
1384 .addImm(CF);
1385 }
1386}
1387
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001388
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001389bool HexagonFrameLowering::needsAligna(const MachineFunction &MF) const {
1390 const MachineFrameInfo *MFI = MF.getFrameInfo();
1391 if (!MFI->hasVarSizedObjects())
1392 return false;
1393 unsigned MaxA = MFI->getMaxAlignment();
1394 if (MaxA <= getStackAlignment())
1395 return false;
1396 return true;
1397}
1398
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001399
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001400const MachineInstr *HexagonFrameLowering::getAlignaInstr(
1401 const MachineFunction &MF) const {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001402 for (auto &B : MF)
1403 for (auto &I : B)
1404 if (I.getOpcode() == Hexagon::ALIGNA)
1405 return &I;
1406 return nullptr;
1407}
1408
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001409
Sanjay Patel924879a2015-08-04 15:49:57 +00001410// FIXME: Use Function::optForSize().
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001411inline static bool isOptSize(const MachineFunction &MF) {
1412 AttributeSet AF = MF.getFunction()->getAttributes();
1413 return AF.hasAttribute(AttributeSet::FunctionIndex,
1414 Attribute::OptimizeForSize);
1415}
1416
1417inline static bool isMinSize(const MachineFunction &MF) {
Sanjay Patel924879a2015-08-04 15:49:57 +00001418 return MF.getFunction()->optForMinSize();
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001419}
1420
1421
1422/// Determine whether the callee-saved register saves and restores should
1423/// be generated via inline code. If this function returns "true", inline
1424/// code will be generated. If this function returns "false", additional
1425/// checks are performed, which may still lead to the inline code.
1426bool HexagonFrameLowering::shouldInlineCSR(MachineFunction &MF,
1427 const CSIVect &CSI) const {
1428 if (MF.getInfo<HexagonMachineFunctionInfo>()->hasEHReturn())
1429 return true;
1430 if (!isOptSize(MF) && !isMinSize(MF))
1431 if (MF.getTarget().getOptLevel() > CodeGenOpt::Default)
1432 return true;
1433
1434 // Check if CSI only has double registers, and if the registers form
1435 // a contiguous block starting from D8.
1436 BitVector Regs(Hexagon::NUM_TARGET_REGS);
1437 for (unsigned i = 0, n = CSI.size(); i < n; ++i) {
1438 unsigned R = CSI[i].getReg();
1439 if (!Hexagon::DoubleRegsRegClass.contains(R))
1440 return true;
1441 Regs[R] = true;
1442 }
1443 int F = Regs.find_first();
1444 if (F != Hexagon::D8)
1445 return true;
1446 while (F >= 0) {
1447 int N = Regs.find_next(F);
1448 if (N >= 0 && N != F+1)
1449 return true;
1450 F = N;
1451 }
1452
1453 return false;
1454}
1455
1456
1457bool HexagonFrameLowering::useSpillFunction(MachineFunction &MF,
1458 const CSIVect &CSI) const {
1459 if (shouldInlineCSR(MF, CSI))
1460 return false;
1461 unsigned NumCSI = CSI.size();
1462 if (NumCSI <= 1)
1463 return false;
1464
1465 unsigned Threshold = isOptSize(MF) ? SpillFuncThresholdOs
1466 : SpillFuncThreshold;
1467 return Threshold < NumCSI;
1468}
1469
1470
1471bool HexagonFrameLowering::useRestoreFunction(MachineFunction &MF,
1472 const CSIVect &CSI) const {
1473 if (shouldInlineCSR(MF, CSI))
1474 return false;
1475 unsigned NumCSI = CSI.size();
1476 unsigned Threshold = isOptSize(MF) ? SpillFuncThresholdOs-1
1477 : SpillFuncThreshold;
1478 return Threshold < NumCSI;
1479}