blob: cf4d110911f11c253319b202540e9ac6830ad991 [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 "HexagonInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "HexagonMachineFunctionInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000016#include "HexagonRegisterInfo.h"
17#include "HexagonSubtarget.h"
18#include "HexagonTargetMachine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000019#include "llvm/ADT/BitVector.h"
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +000020#include "llvm/ADT/PostOrderIterator.h"
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +000021#include "llvm/CodeGen/MachineDominators.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000022#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +000025#include "llvm/CodeGen/MachineInstrBuilder.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +000027#include "llvm/CodeGen/MachinePostDominators.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
29#include "llvm/CodeGen/RegisterScavenging.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/Function.h"
31#include "llvm/IR/Type.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/CommandLine.h"
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000033#include "llvm/Support/Debug.h"
34#include "llvm/Support/raw_ostream.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000035#include "llvm/Target/TargetInstrInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000036#include "llvm/Target/TargetMachine.h"
37#include "llvm/Target/TargetOptions.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000038
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000039// Hexagon stack frame layout as defined by the ABI:
40//
41// Incoming arguments
42// passed via stack
43// |
44// |
45// SP during function's FP during function's |
46// +-- runtime (top of stack) runtime (bottom) --+ |
47// | | |
48// --++---------------------+------------------+-----------------++-+-------
49// | parameter area for | variable-size | fixed-size |LR| arg
50// | called functions | local objects | local objects |FP|
51// --+----------------------+------------------+-----------------+--+-------
52// <- size known -> <- size unknown -> <- size known ->
53//
54// Low address High address
55//
56// <--- stack growth
57//
58//
59// - In any circumstances, the outgoing function arguments are always accessi-
60// ble using the SP, and the incoming arguments are accessible using the FP.
61// - If the local objects are not aligned, they can always be accessed using
62// the FP.
63// - If there are no variable-sized objects, the local objects can always be
64// accessed using the SP, regardless whether they are aligned or not. (The
65// alignment padding will be at the bottom of the stack (highest address),
66// and so the offset with respect to the SP will be known at the compile-
67// -time.)
68//
69// The only complication occurs if there are both, local aligned objects, and
70// dynamically allocated (variable-sized) objects. The alignment pad will be
71// placed between the FP and the local objects, thus preventing the use of the
72// FP to access the local objects. At the same time, the variable-sized objects
73// will be between the SP and the local objects, thus introducing an unknown
74// distance from the SP to the locals.
75//
76// To avoid this problem, a new register is created that holds the aligned
77// address of the bottom of the stack, referred in the sources as AP (aligned
78// pointer). The AP will be equal to "FP-p", where "p" is the smallest pad
79// that aligns AP to the required boundary (a maximum of the alignments of
80// all stack objects, fixed- and variable-sized). All local objects[1] will
81// then use AP as the base pointer.
82// [1] The exception is with "fixed" stack objects. "Fixed" stack objects get
83// their name from being allocated at fixed locations on the stack, relative
84// to the FP. In the presence of dynamic allocation and local alignment, such
85// objects can only be accessed through the FP.
86//
87// Illustration of the AP:
88// FP --+
89// |
90// ---------------+---------------------+-----+-----------------------++-+--
91// Rest of the | Local stack objects | Pad | Fixed stack objects |LR|
92// stack frame | (aligned) | | (CSR, spills, etc.) |FP|
93// ---------------+---------------------+-----+-----------------+-----+--+--
94// |<-- Multiple of the -->|
95// stack alignment +-- AP
96//
97// The AP is set up at the beginning of the function. Since it is not a dedi-
98// cated (reserved) register, it needs to be kept live throughout the function
99// to be available as the base register for local object accesses.
100// Normally, an address of a stack objects is obtained by a pseudo-instruction
101// TFR_FI. To access local objects with the AP register present, a different
102// pseudo-instruction needs to be used: TFR_FIA. The TFR_FIA takes one extra
103// argument compared to TFR_FI: the first input register is the AP register.
104// This keeps the register live between its definition and its uses.
105
106// The AP register is originally set up using pseudo-instruction ALIGNA:
107// AP = ALIGNA A
108// where
109// A - required stack alignment
110// The alignment value must be the maximum of all alignments required by
111// any stack object.
112
113// The dynamic allocation uses a pseudo-instruction ALLOCA:
114// Rd = ALLOCA Rs, A
115// where
116// Rd - address of the allocated space
117// Rs - minimum size (the actual allocated can be larger to accommodate
118// alignment)
119// A - required alignment
120
121
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000122using namespace llvm;
123
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000124static cl::opt<bool> DisableDeallocRet("disable-hexagon-dealloc-ret",
125 cl::Hidden, cl::desc("Disable Dealloc Return for Hexagon target"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000126
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000127static cl::opt<int> NumberScavengerSlots("number-scavenger-slots",
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000128 cl::Hidden, cl::desc("Set the number of scavenger slots"), cl::init(2),
129 cl::ZeroOrMore);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000130
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000131static cl::opt<int> SpillFuncThreshold("spill-func-threshold",
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000132 cl::Hidden, cl::desc("Specify O2(not Os) spill func threshold"),
133 cl::init(6), cl::ZeroOrMore);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000134
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000135static cl::opt<int> SpillFuncThresholdOs("spill-func-threshold-Os",
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000136 cl::Hidden, cl::desc("Specify Os spill func threshold"),
137 cl::init(1), cl::ZeroOrMore);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000138
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000139static cl::opt<bool> EnableShrinkWrapping("hexagon-shrink-frame",
140 cl::init(true), cl::Hidden, cl::ZeroOrMore,
141 cl::desc("Enable stack frame shrink wrapping"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000142
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000143static cl::opt<unsigned> ShrinkLimit("shrink-frame-limit", cl::init(UINT_MAX),
144 cl::Hidden, cl::ZeroOrMore, cl::desc("Max count of stack frame "
145 "shrink-wraps"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000146
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000147static cl::opt<bool> UseAllocframe("use-allocframe", cl::init(true),
148 cl::Hidden, cl::desc("Use allocframe more conservatively"));
149
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000150
151namespace llvm {
152 void initializeHexagonCallFrameInformationPass(PassRegistry&);
153 FunctionPass *createHexagonCallFrameInformation();
154}
155
156namespace {
157 class HexagonCallFrameInformation : public MachineFunctionPass {
158 public:
159 static char ID;
160 HexagonCallFrameInformation() : MachineFunctionPass(ID) {
161 PassRegistry &PR = *PassRegistry::getPassRegistry();
162 initializeHexagonCallFrameInformationPass(PR);
163 }
164 bool runOnMachineFunction(MachineFunction &MF) override;
165 };
166
167 char HexagonCallFrameInformation::ID = 0;
168}
169
170bool HexagonCallFrameInformation::runOnMachineFunction(MachineFunction &MF) {
171 auto &HFI = *MF.getSubtarget<HexagonSubtarget>().getFrameLowering();
172 bool NeedCFI = MF.getMMI().hasDebugInfo() ||
173 MF.getFunction()->needsUnwindTableEntry();
174
175 if (!NeedCFI)
176 return false;
177 HFI.insertCFIInstructions(MF);
178 return true;
179}
180
181INITIALIZE_PASS(HexagonCallFrameInformation, "hexagon-cfi",
182 "Hexagon call frame information", false, false)
183
184FunctionPass *llvm::createHexagonCallFrameInformation() {
185 return new HexagonCallFrameInformation();
186}
187
188
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000189namespace {
190 /// Map a register pair Reg to the subregister that has the greater "number",
191 /// i.e. D3 (aka R7:6) will be mapped to R7, etc.
192 unsigned getMax32BitSubRegister(unsigned Reg, const TargetRegisterInfo &TRI,
193 bool hireg = true) {
194 if (Reg < Hexagon::D0 || Reg > Hexagon::D15)
195 return Reg;
196
197 unsigned RegNo = 0;
198 for (MCSubRegIterator SubRegs(Reg, &TRI); SubRegs.isValid(); ++SubRegs) {
199 if (hireg) {
200 if (*SubRegs > RegNo)
201 RegNo = *SubRegs;
202 } else {
203 if (!RegNo || *SubRegs < RegNo)
204 RegNo = *SubRegs;
205 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000206 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000207 return RegNo;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000208 }
209
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000210 /// Returns the callee saved register with the largest id in the vector.
211 unsigned getMaxCalleeSavedReg(const std::vector<CalleeSavedInfo> &CSI,
212 const TargetRegisterInfo &TRI) {
213 assert(Hexagon::R1 > 0 &&
214 "Assume physical registers are encoded as positive integers");
215 if (CSI.empty())
216 return 0;
217
218 unsigned Max = getMax32BitSubRegister(CSI[0].getReg(), TRI);
219 for (unsigned I = 1, E = CSI.size(); I < E; ++I) {
220 unsigned Reg = getMax32BitSubRegister(CSI[I].getReg(), TRI);
221 if (Reg > Max)
222 Max = Reg;
223 }
224 return Max;
225 }
226
227 /// Checks if the basic block contains any instruction that needs a stack
228 /// frame to be already in place.
229 bool needsStackFrame(const MachineBasicBlock &MBB, const BitVector &CSR) {
230 for (auto &I : MBB) {
231 const MachineInstr *MI = &I;
232 if (MI->isCall())
233 return true;
234 unsigned Opc = MI->getOpcode();
235 switch (Opc) {
236 case Hexagon::ALLOCA:
237 case Hexagon::ALIGNA:
238 return true;
239 default:
240 break;
241 }
242 // Check individual operands.
Matthias Braune41e1462015-05-29 02:56:46 +0000243 for (const MachineOperand &MO : MI->operands()) {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000244 // While the presence of a frame index does not prove that a stack
245 // frame will be required, all frame indexes should be within alloc-
246 // frame/deallocframe. Otherwise, the code that translates a frame
247 // index into an offset would have to be aware of the placement of
248 // the frame creation/destruction instructions.
Matthias Braune41e1462015-05-29 02:56:46 +0000249 if (MO.isFI())
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000250 return true;
Matthias Braune41e1462015-05-29 02:56:46 +0000251 if (!MO.isReg())
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000252 continue;
Matthias Braune41e1462015-05-29 02:56:46 +0000253 unsigned R = MO.getReg();
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000254 // Virtual registers will need scavenging, which then may require
255 // a stack slot.
256 if (TargetRegisterInfo::isVirtualRegister(R))
257 return true;
258 if (CSR[R])
259 return true;
260 }
261 }
262 return false;
263 }
264
265 /// Returns true if MBB has a machine instructions that indicates a tail call
266 /// in the block.
267 bool hasTailCall(const MachineBasicBlock &MBB) {
268 MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr();
269 unsigned RetOpc = I->getOpcode();
270 return RetOpc == Hexagon::TCRETURNi || RetOpc == Hexagon::TCRETURNr;
271 }
272
273 /// Returns true if MBB contains an instruction that returns.
274 bool hasReturn(const MachineBasicBlock &MBB) {
275 for (auto I = MBB.getFirstTerminator(), E = MBB.end(); I != E; ++I)
276 if (I->isReturn())
277 return true;
278 return false;
279 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000280}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000281
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000282
283/// Implements shrink-wrapping of the stack frame. By default, stack frame
284/// is created in the function entry block, and is cleaned up in every block
285/// that returns. This function finds alternate blocks: one for the frame
286/// setup (prolog) and one for the cleanup (epilog).
287void HexagonFrameLowering::findShrunkPrologEpilog(MachineFunction &MF,
288 MachineBasicBlock *&PrologB, MachineBasicBlock *&EpilogB) const {
289 static unsigned ShrinkCounter = 0;
290
291 if (ShrinkLimit.getPosition()) {
292 if (ShrinkCounter >= ShrinkLimit)
293 return;
294 ShrinkCounter++;
295 }
296
297 auto &HST = static_cast<const HexagonSubtarget&>(MF.getSubtarget());
298 auto &HRI = *HST.getRegisterInfo();
299
300 MachineDominatorTree MDT;
301 MDT.runOnMachineFunction(MF);
302 MachinePostDominatorTree MPT;
303 MPT.runOnMachineFunction(MF);
304
305 typedef DenseMap<unsigned,unsigned> UnsignedMap;
306 UnsignedMap RPO;
307 typedef ReversePostOrderTraversal<const MachineFunction*> RPOTType;
308 RPOTType RPOT(&MF);
309 unsigned RPON = 0;
310 for (RPOTType::rpo_iterator I = RPOT.begin(), E = RPOT.end(); I != E; ++I)
311 RPO[(*I)->getNumber()] = RPON++;
312
313 // Don't process functions that have loops, at least for now. Placement
314 // of prolog and epilog must take loop structure into account. For simpli-
315 // city don't do it right now.
316 for (auto &I : MF) {
317 unsigned BN = RPO[I.getNumber()];
318 for (auto SI = I.succ_begin(), SE = I.succ_end(); SI != SE; ++SI) {
319 // If found a back-edge, return.
320 if (RPO[(*SI)->getNumber()] <= BN)
321 return;
322 }
323 }
324
325 // Collect the set of blocks that need a stack frame to execute. Scan
326 // each block for uses/defs of callee-saved registers, calls, etc.
327 SmallVector<MachineBasicBlock*,16> SFBlocks;
328 BitVector CSR(Hexagon::NUM_TARGET_REGS);
329 for (const MCPhysReg *P = HRI.getCalleeSavedRegs(&MF); *P; ++P)
330 CSR[*P] = true;
331
332 for (auto &I : MF)
333 if (needsStackFrame(I, CSR))
334 SFBlocks.push_back(&I);
335
336 DEBUG({
337 dbgs() << "Blocks needing SF: {";
338 for (auto &B : SFBlocks)
339 dbgs() << " BB#" << B->getNumber();
340 dbgs() << " }\n";
341 });
342 // No frame needed?
343 if (SFBlocks.empty())
344 return;
345
346 // Pick a common dominator and a common post-dominator.
347 MachineBasicBlock *DomB = SFBlocks[0];
348 for (unsigned i = 1, n = SFBlocks.size(); i < n; ++i) {
349 DomB = MDT.findNearestCommonDominator(DomB, SFBlocks[i]);
350 if (!DomB)
351 break;
352 }
353 MachineBasicBlock *PDomB = SFBlocks[0];
354 for (unsigned i = 1, n = SFBlocks.size(); i < n; ++i) {
355 PDomB = MPT.findNearestCommonDominator(PDomB, SFBlocks[i]);
356 if (!PDomB)
357 break;
358 }
359 DEBUG({
360 dbgs() << "Computed dom block: BB#";
361 if (DomB) dbgs() << DomB->getNumber();
362 else dbgs() << "<null>";
363 dbgs() << ", computed pdom block: BB#";
364 if (PDomB) dbgs() << PDomB->getNumber();
365 else dbgs() << "<null>";
366 dbgs() << "\n";
367 });
368 if (!DomB || !PDomB)
369 return;
370
371 // Make sure that DomB dominates PDomB and PDomB post-dominates DomB.
372 if (!MDT.dominates(DomB, PDomB)) {
373 DEBUG(dbgs() << "Dom block does not dominate pdom block\n");
374 return;
375 }
376 if (!MPT.dominates(PDomB, DomB)) {
377 DEBUG(dbgs() << "PDom block does not post-dominate dom block\n");
378 return;
379 }
380
381 // Finally, everything seems right.
382 PrologB = DomB;
383 EpilogB = PDomB;
384}
385
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000386/// Perform most of the PEI work here:
387/// - saving/restoring of the callee-saved registers,
388/// - stack frame creation and destruction.
389/// Normally, this work is distributed among various functions, but doing it
390/// in one place allows shrink-wrapping of the stack frame.
Quentin Colombet61b305e2015-05-05 17:38:16 +0000391void HexagonFrameLowering::emitPrologue(MachineFunction &MF,
392 MachineBasicBlock &MBB) const {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000393 auto &HST = static_cast<const HexagonSubtarget&>(MF.getSubtarget());
394 auto &HRI = *HST.getRegisterInfo();
395
396 MachineFrameInfo *MFI = MF.getFrameInfo();
397 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
398
399 MachineBasicBlock *PrologB = &MF.front(), *EpilogB = nullptr;
400 if (EnableShrinkWrapping)
401 findShrunkPrologEpilog(MF, PrologB, EpilogB);
402
403 insertCSRSpillsInBlock(*PrologB, CSI, HRI);
404 insertPrologueInBlock(*PrologB);
405
406 if (EpilogB) {
407 insertCSRRestoresInBlock(*EpilogB, CSI, HRI);
408 insertEpilogueInBlock(*EpilogB);
409 } else {
410 for (auto &B : MF)
Matthias Braunc2d4bef2015-09-25 21:25:19 +0000411 if (B.isReturnBlock())
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000412 insertCSRRestoresInBlock(B, CSI, HRI);
413
414 for (auto &B : MF)
Matthias Braunc2d4bef2015-09-25 21:25:19 +0000415 if (B.isReturnBlock())
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000416 insertEpilogueInBlock(B);
417 }
418}
419
420
421void HexagonFrameLowering::insertPrologueInBlock(MachineBasicBlock &MBB) const {
422 MachineFunction &MF = *MBB.getParent();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000423 MachineFrameInfo *MFI = MF.getFrameInfo();
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000424 auto &HST = MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000425 auto &HII = *HST.getInstrInfo();
426 auto &HRI = *HST.getRegisterInfo();
427 DebugLoc dl;
428
429 unsigned MaxAlign = std::max(MFI->getMaxAlignment(), getStackAlignment());
430
431 // Calculate the total stack frame size.
432 // Get the number of bytes to allocate from the FrameInfo.
433 unsigned FrameSize = MFI->getStackSize();
434 // Round up the max call frame size to the max alignment on the stack.
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000435 unsigned MaxCFA = alignTo(MFI->getMaxCallFrameSize(), MaxAlign);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000436 MFI->setMaxCallFrameSize(MaxCFA);
437
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000438 FrameSize = MaxCFA + alignTo(FrameSize, MaxAlign);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000439 MFI->setStackSize(FrameSize);
440
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000441 bool AlignStack = (MaxAlign > getStackAlignment());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000442
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000443 // Get the number of bytes to allocate from the FrameInfo.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000444 unsigned NumBytes = MFI->getStackSize();
445 unsigned SP = HRI.getStackRegister();
446 unsigned MaxCF = MFI->getMaxCallFrameSize();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000447 MachineBasicBlock::iterator InsertPt = MBB.begin();
448
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000449 auto *FuncInfo = MF.getInfo<HexagonMachineFunctionInfo>();
450 auto &AdjustRegs = FuncInfo->getAllocaAdjustInsts();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000451
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000452 for (auto MI : AdjustRegs) {
453 assert((MI->getOpcode() == Hexagon::ALLOCA) && "Expected alloca");
454 expandAlloca(MI, HII, SP, MaxCF);
455 MI->eraseFromParent();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000456 }
457
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000458 if (!hasFP(MF))
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000459 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000460
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000461 // Check for overflow.
462 // Hexagon_TODO: Ugh! hardcoding. Is there an API that can be used?
463 const unsigned int ALLOCFRAME_MAX = 16384;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000464
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000465 // Create a dummy memory operand to avoid allocframe from being treated as
466 // a volatile memory reference.
467 MachineMemOperand *MMO =
468 MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOStore,
469 4, 4);
470
471 if (NumBytes >= ALLOCFRAME_MAX) {
472 // Emit allocframe(#0).
473 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::S2_allocframe))
474 .addImm(0)
475 .addMemOperand(MMO);
476
477 // Subtract offset from frame pointer.
478 // We use a caller-saved non-parameter register for that.
479 unsigned CallerSavedReg = HRI.getFirstCallerSavedNonParamReg();
480 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::CONST32_Int_Real),
481 CallerSavedReg).addImm(NumBytes);
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000482 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_sub), SP)
483 .addReg(SP)
484 .addReg(CallerSavedReg);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000485 } else {
486 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::S2_allocframe))
487 .addImm(NumBytes)
488 .addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000489 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000490
491 if (AlignStack) {
492 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_andir), SP)
493 .addReg(SP)
494 .addImm(-int64_t(MaxAlign));
495 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000496}
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000497
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000498void HexagonFrameLowering::insertEpilogueInBlock(MachineBasicBlock &MBB) const {
499 MachineFunction &MF = *MBB.getParent();
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000500 if (!hasFP(MF))
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000501 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000502
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000503 auto &HST = static_cast<const HexagonSubtarget&>(MF.getSubtarget());
504 auto &HII = *HST.getInstrInfo();
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000505 auto &HRI = *HST.getRegisterInfo();
506 unsigned SP = HRI.getStackRegister();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000507
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000508 MachineInstr *RetI = nullptr;
509 for (auto &I : MBB) {
510 if (!I.isReturn())
511 continue;
512 RetI = &I;
513 break;
514 }
515 unsigned RetOpc = RetI ? RetI->getOpcode() : 0;
516
517 MachineBasicBlock::iterator InsertPt = MBB.getFirstTerminator();
518 DebugLoc DL;
519 if (InsertPt != MBB.end())
520 DL = InsertPt->getDebugLoc();
521 else if (!MBB.empty())
522 DL = std::prev(MBB.end())->getDebugLoc();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000523
524 // Handle EH_RETURN.
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000525 if (RetOpc == Hexagon::EH_RETURN_JMPR) {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000526 BuildMI(MBB, InsertPt, DL, HII.get(Hexagon::L2_deallocframe));
527 BuildMI(MBB, InsertPt, DL, HII.get(Hexagon::A2_add), SP)
528 .addReg(SP)
529 .addReg(Hexagon::R28);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000530 return;
531 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000532
533 // Check for RESTORE_DEALLOC_RET* tail call. Don't emit an extra dealloc-
534 // frame instruction if we encounter it.
535 if (RetOpc == Hexagon::RESTORE_DEALLOC_RET_JMP_V4) {
536 MachineBasicBlock::iterator It = RetI;
537 ++It;
538 // Delete all instructions after the RESTORE (except labels).
539 while (It != MBB.end()) {
540 if (!It->isLabel())
541 It = MBB.erase(It);
542 else
543 ++It;
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000544 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000545 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000546 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000547
548 // It is possible that the restoring code is a call to a library function.
549 // All of the restore* functions include "deallocframe", so we need to make
550 // sure that we don't add an extra one.
551 bool NeedsDeallocframe = true;
552 if (!MBB.empty() && InsertPt != MBB.begin()) {
553 MachineBasicBlock::iterator PrevIt = std::prev(InsertPt);
554 unsigned COpc = PrevIt->getOpcode();
555 if (COpc == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4)
556 NeedsDeallocframe = false;
557 }
558
559 if (!NeedsDeallocframe)
560 return;
561 // If the returning instruction is JMPret, replace it with dealloc_return,
562 // otherwise just add deallocframe. The function could be returning via a
563 // tail call.
564 if (RetOpc != Hexagon::JMPret || DisableDeallocRet) {
565 BuildMI(MBB, InsertPt, DL, HII.get(Hexagon::L2_deallocframe));
566 return;
567 }
568 unsigned NewOpc = Hexagon::L4_return;
569 MachineInstr *NewI = BuildMI(MBB, RetI, DL, HII.get(NewOpc));
570 // Transfer the function live-out registers.
571 NewI->copyImplicitOps(MF, RetI);
572 MBB.erase(RetI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000573}
574
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000575
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000576namespace {
577 bool IsAllocFrame(MachineBasicBlock::const_iterator It) {
578 if (!It->isBundle())
579 return It->getOpcode() == Hexagon::S2_allocframe;
580 auto End = It->getParent()->instr_end();
581 MachineBasicBlock::const_instr_iterator I = It.getInstrIterator();
582 while (++I != End && I->isBundled())
583 if (I->getOpcode() == Hexagon::S2_allocframe)
584 return true;
585 return false;
586 }
587
588 MachineBasicBlock::iterator FindAllocFrame(MachineBasicBlock &B) {
589 for (auto &I : B)
590 if (IsAllocFrame(I))
591 return I;
592 return B.end();
593 }
594}
595
596
597void HexagonFrameLowering::insertCFIInstructions(MachineFunction &MF) const {
598 for (auto &B : MF) {
599 auto AF = FindAllocFrame(B);
600 if (AF == B.end())
601 continue;
602 insertCFIInstructionsAt(B, ++AF);
603 }
604}
605
606
607void HexagonFrameLowering::insertCFIInstructionsAt(MachineBasicBlock &MBB,
608 MachineBasicBlock::iterator At) const {
609 MachineFunction &MF = *MBB.getParent();
610 MachineFrameInfo *MFI = MF.getFrameInfo();
611 MachineModuleInfo &MMI = MF.getMMI();
612 auto &HST = MF.getSubtarget<HexagonSubtarget>();
613 auto &HII = *HST.getInstrInfo();
614 auto &HRI = *HST.getRegisterInfo();
615
616 // If CFI instructions have debug information attached, something goes
617 // wrong with the final assembly generation: the prolog_end is placed
618 // in a wrong location.
619 DebugLoc DL;
620 const MCInstrDesc &CFID = HII.get(TargetOpcode::CFI_INSTRUCTION);
621
622 MCSymbol *FrameLabel = MMI.getContext().createTempSymbol();
623
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000624 if (hasFP(MF)) {
625 unsigned DwFPReg = HRI.getDwarfRegNum(HRI.getFrameRegister(), true);
626 unsigned DwRAReg = HRI.getDwarfRegNum(HRI.getRARegister(), true);
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000627
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000628 // Define CFA via an offset from the value of FP.
629 //
630 // -8 -4 0 (SP)
631 // --+----+----+---------------------
632 // | FP | LR | increasing addresses -->
633 // --+----+----+---------------------
634 // | +-- Old SP (before allocframe)
635 // +-- New FP (after allocframe)
636 //
637 // MCCFIInstruction::createDefCfa subtracts the offset from the register.
638 // MCCFIInstruction::createOffset takes the offset without sign change.
639 auto DefCfa = MCCFIInstruction::createDefCfa(FrameLabel, DwFPReg, -8);
640 BuildMI(MBB, At, DL, CFID)
641 .addCFIIndex(MMI.addFrameInst(DefCfa));
642 // R31 (return addr) = CFA - 4
643 auto OffR31 = MCCFIInstruction::createOffset(FrameLabel, DwRAReg, -4);
644 BuildMI(MBB, At, DL, CFID)
645 .addCFIIndex(MMI.addFrameInst(OffR31));
646 // R30 (frame ptr) = CFA - 8
647 auto OffR30 = MCCFIInstruction::createOffset(FrameLabel, DwFPReg, -8);
648 BuildMI(MBB, At, DL, CFID)
649 .addCFIIndex(MMI.addFrameInst(OffR30));
650 }
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000651
652 static unsigned int RegsToMove[] = {
653 Hexagon::R1, Hexagon::R0, Hexagon::R3, Hexagon::R2,
654 Hexagon::R17, Hexagon::R16, Hexagon::R19, Hexagon::R18,
655 Hexagon::R21, Hexagon::R20, Hexagon::R23, Hexagon::R22,
656 Hexagon::R25, Hexagon::R24, Hexagon::R27, Hexagon::R26,
657 Hexagon::D0, Hexagon::D1, Hexagon::D8, Hexagon::D9,
658 Hexagon::D10, Hexagon::D11, Hexagon::D12, Hexagon::D13,
659 Hexagon::NoRegister
660 };
661
662 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
663
664 for (unsigned i = 0; RegsToMove[i] != Hexagon::NoRegister; ++i) {
665 unsigned Reg = RegsToMove[i];
666 auto IfR = [Reg] (const CalleeSavedInfo &C) -> bool {
667 return C.getReg() == Reg;
668 };
669 auto F = std::find_if(CSI.begin(), CSI.end(), IfR);
670 if (F == CSI.end())
671 continue;
672
673 // Subtract 8 to make room for R30 and R31, which are added above.
674 unsigned FrameReg;
675 int64_t Offset = getFrameIndexReference(MF, F->getFrameIdx(), FrameReg) - 8;
676
677 if (Reg < Hexagon::D0 || Reg > Hexagon::D15) {
678 unsigned DwarfReg = HRI.getDwarfRegNum(Reg, true);
679 auto OffReg = MCCFIInstruction::createOffset(FrameLabel, DwarfReg,
680 Offset);
681 BuildMI(MBB, At, DL, CFID)
682 .addCFIIndex(MMI.addFrameInst(OffReg));
683 } else {
684 // Split the double regs into subregs, and generate appropriate
685 // cfi_offsets.
686 // The only reason, we are split double regs is, llvm-mc does not
687 // understand paired registers for cfi_offset.
688 // Eg .cfi_offset r1:0, -64
689
690 unsigned HiReg = HRI.getSubReg(Reg, Hexagon::subreg_hireg);
691 unsigned LoReg = HRI.getSubReg(Reg, Hexagon::subreg_loreg);
692 unsigned HiDwarfReg = HRI.getDwarfRegNum(HiReg, true);
693 unsigned LoDwarfReg = HRI.getDwarfRegNum(LoReg, true);
694 auto OffHi = MCCFIInstruction::createOffset(FrameLabel, HiDwarfReg,
695 Offset+4);
696 BuildMI(MBB, At, DL, CFID)
697 .addCFIIndex(MMI.addFrameInst(OffHi));
698 auto OffLo = MCCFIInstruction::createOffset(FrameLabel, LoDwarfReg,
699 Offset);
700 BuildMI(MBB, At, DL, CFID)
701 .addCFIIndex(MMI.addFrameInst(OffLo));
702 }
703 }
704}
705
706
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000707bool HexagonFrameLowering::hasFP(const MachineFunction &MF) const {
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000708 auto &MFI = *MF.getFrameInfo();
709 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
710
711 bool HasFixed = MFI.getNumFixedObjects();
712 bool HasPrealloc = const_cast<MachineFrameInfo&>(MFI)
713 .getLocalFrameObjectCount();
714 bool HasExtraAlign = HRI.needsStackRealignment(MF);
715 bool HasAlloca = MFI.hasVarSizedObjects();
716
717 // Insert ALLOCFRAME if we need to or at -O0 for the debugger. Think
718 // that this shouldn't be required, but doing so now because gcc does and
719 // gdb can't break at the start of the function without it. Will remove if
720 // this turns out to be a gdb bug.
721 //
722 if (MF.getTarget().getOptLevel() == CodeGenOpt::None)
723 return true;
724
725 // By default we want to use SP (since it's always there). FP requires
726 // some setup (i.e. ALLOCFRAME).
727 // Fixed and preallocated objects need FP if the distance from them to
728 // the SP is unknown (as is with alloca or aligna).
729 if ((HasFixed || HasPrealloc) && (HasAlloca || HasExtraAlign))
730 return true;
731
732 if (MFI.getStackSize() > 0) {
733 if (UseAllocframe)
734 return true;
735 }
736
737 if (MFI.hasCalls() ||
738 MF.getInfo<HexagonMachineFunctionInfo>()->hasClobberLR())
739 return true;
740
741 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000742}
743
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000744
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000745enum SpillKind {
746 SK_ToMem,
747 SK_FromMem,
748 SK_FromMemTailcall
749};
750
751static const char *
752getSpillFunctionFor(unsigned MaxReg, SpillKind SpillType) {
753 const char * V4SpillToMemoryFunctions[] = {
754 "__save_r16_through_r17",
755 "__save_r16_through_r19",
756 "__save_r16_through_r21",
757 "__save_r16_through_r23",
758 "__save_r16_through_r25",
759 "__save_r16_through_r27" };
760
761 const char * V4SpillFromMemoryFunctions[] = {
762 "__restore_r16_through_r17_and_deallocframe",
763 "__restore_r16_through_r19_and_deallocframe",
764 "__restore_r16_through_r21_and_deallocframe",
765 "__restore_r16_through_r23_and_deallocframe",
766 "__restore_r16_through_r25_and_deallocframe",
767 "__restore_r16_through_r27_and_deallocframe" };
768
769 const char * V4SpillFromMemoryTailcallFunctions[] = {
770 "__restore_r16_through_r17_and_deallocframe_before_tailcall",
771 "__restore_r16_through_r19_and_deallocframe_before_tailcall",
772 "__restore_r16_through_r21_and_deallocframe_before_tailcall",
773 "__restore_r16_through_r23_and_deallocframe_before_tailcall",
774 "__restore_r16_through_r25_and_deallocframe_before_tailcall",
775 "__restore_r16_through_r27_and_deallocframe_before_tailcall"
776 };
777
778 const char **SpillFunc = nullptr;
779
780 switch(SpillType) {
781 case SK_ToMem:
782 SpillFunc = V4SpillToMemoryFunctions;
783 break;
784 case SK_FromMem:
785 SpillFunc = V4SpillFromMemoryFunctions;
786 break;
787 case SK_FromMemTailcall:
788 SpillFunc = V4SpillFromMemoryTailcallFunctions;
789 break;
790 }
791 assert(SpillFunc && "Unknown spill kind");
792
793 // Spill all callee-saved registers up to the highest register used.
794 switch (MaxReg) {
795 case Hexagon::R17:
796 return SpillFunc[0];
797 case Hexagon::R19:
798 return SpillFunc[1];
799 case Hexagon::R21:
800 return SpillFunc[2];
801 case Hexagon::R23:
802 return SpillFunc[3];
803 case Hexagon::R25:
804 return SpillFunc[4];
805 case Hexagon::R27:
806 return SpillFunc[5];
807 default:
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000808 llvm_unreachable("Unhandled maximum callee save register");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000809 }
810 return 0;
811}
812
813/// Adds all callee-saved registers up to MaxReg to the instruction.
814static void addCalleeSaveRegistersAsImpOperand(MachineInstr *Inst,
815 unsigned MaxReg, bool IsDef) {
816 // Add the callee-saved registers as implicit uses.
817 for (unsigned R = Hexagon::R16; R <= MaxReg; ++R) {
818 MachineOperand ImpUse = MachineOperand::CreateReg(R, IsDef, true);
819 Inst->addOperand(ImpUse);
820 }
821}
822
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000823
James Y Knight5567baf2015-08-15 02:32:35 +0000824int HexagonFrameLowering::getFrameIndexReference(const MachineFunction &MF,
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000825 int FI, unsigned &FrameReg) const {
826 auto &MFI = *MF.getFrameInfo();
827 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000828
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000829 int Offset = MFI.getObjectOffset(FI);
830 bool HasAlloca = MFI.hasVarSizedObjects();
831 bool HasExtraAlign = HRI.needsStackRealignment(MF);
832 bool NoOpt = MF.getTarget().getOptLevel() == CodeGenOpt::None;
James Y Knight5567baf2015-08-15 02:32:35 +0000833
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000834 unsigned SP = HRI.getStackRegister(), FP = HRI.getFrameRegister();
835 unsigned AP = 0;
836 if (const MachineInstr *AI = getAlignaInstr(MF))
837 AP = AI->getOperand(0).getReg();
838 unsigned FrameSize = MFI.getStackSize();
839
840 bool UseFP = false, UseAP = false; // Default: use SP (except at -O0).
841 // Use FP at -O0, except when there are objects with extra alignment.
842 // That additional alignment requirement may cause a pad to be inserted,
843 // which will make it impossible to use FP to access objects located
844 // past the pad.
845 if (NoOpt && !HasExtraAlign)
846 UseFP = true;
847 if (MFI.isFixedObjectIndex(FI) || MFI.isObjectPreAllocated(FI)) {
848 // Fixed and preallocated objects will be located before any padding
849 // so FP must be used to access them.
850 UseFP |= (HasAlloca || HasExtraAlign);
851 } else {
852 if (HasAlloca) {
853 if (HasExtraAlign)
854 UseAP = true;
855 else
856 UseFP = true;
857 }
858 }
859
860 // If FP was picked, then there had better be FP.
861 bool HasFP = hasFP(MF);
862 assert((HasFP || !UseFP) && "This function must have frame pointer");
863
864 // Having FP implies allocframe. Allocframe will store extra 8 bytes:
865 // FP/LR. If the base register is used to access an object across these
866 // 8 bytes, then the offset will need to be adjusted by 8.
867 //
868 // After allocframe:
869 // HexagonISelLowering adds 8 to ---+
870 // the offsets of all stack-based |
871 // arguments (*) |
872 // |
873 // getObjectOffset < 0 0 8 getObjectOffset >= 8
874 // ------------------------+-----+------------------------> increasing
875 // <local objects> |FP/LR| <input arguments> addresses
876 // -----------------+------+-----+------------------------>
877 // | |
878 // SP/AP point --+ +-- FP points here (**)
879 // somewhere on
880 // this side of FP/LR
881 //
882 // (*) See LowerFormalArguments. The FP/LR is assumed to be present.
883 // (**) *FP == old-FP. FP+0..7 are the bytes of FP/LR.
884
885 // The lowering assumes that FP/LR is present, and so the offsets of
886 // the formal arguments start at 8. If FP/LR is not there we need to
887 // reduce the offset by 8.
888 if (Offset > 0 && !HasFP)
889 Offset -= 8;
890
891 if (UseFP)
892 FrameReg = FP;
893 else if (UseAP)
894 FrameReg = AP;
895 else
896 FrameReg = SP;
897
898 // Calculate the actual offset in the instruction. If there is no FP
899 // (in other words, no allocframe), then SP will not be adjusted (i.e.
900 // there will be no SP -= FrameSize), so the frame size should not be
901 // added to the calculated offset.
902 int RealOffset = Offset;
903 if (!UseFP && !UseAP && HasFP)
904 RealOffset = FrameSize+Offset;
905 return RealOffset;
Jakob Stoklund Olesen0b97dbc2012-05-30 22:40:03 +0000906}
907
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000908
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000909bool HexagonFrameLowering::insertCSRSpillsInBlock(MachineBasicBlock &MBB,
910 const CSIVect &CSI, const HexagonRegisterInfo &HRI) const {
911 if (CSI.empty())
912 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000913
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000914 MachineBasicBlock::iterator MI = MBB.begin();
915 MachineFunction &MF = *MBB.getParent();
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000916 auto &HII = *MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000917
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000918 if (useSpillFunction(MF, CSI)) {
919 unsigned MaxReg = getMaxCalleeSavedReg(CSI, HRI);
920 const char *SpillFun = getSpillFunctionFor(MaxReg, SK_ToMem);
921 // Call spill function.
922 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
923 MachineInstr *SaveRegsCall =
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000924 BuildMI(MBB, MI, DL, HII.get(Hexagon::SAVE_REGISTERS_CALL_V4))
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000925 .addExternalSymbol(SpillFun);
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000926 // Add callee-saved registers as use.
927 addCalleeSaveRegistersAsImpOperand(SaveRegsCall, MaxReg, false);
928 // Add live in registers.
929 for (unsigned I = 0; I < CSI.size(); ++I)
930 MBB.addLiveIn(CSI[I].getReg());
931 return true;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000932 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000933
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000934 for (unsigned i = 0, n = CSI.size(); i < n; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000935 unsigned Reg = CSI[i].getReg();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000936 // Add live in registers. We treat eh_return callee saved register r0 - r3
937 // specially. They are not really callee saved registers as they are not
938 // supposed to be killed.
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000939 bool IsKill = !HRI.isEHReturnCalleeSaveReg(Reg);
940 int FI = CSI[i].getFrameIdx();
941 const TargetRegisterClass *RC = HRI.getMinimalPhysRegClass(Reg);
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000942 HII.storeRegToStackSlot(MBB, MI, Reg, IsKill, FI, RC, &HRI);
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000943 if (IsKill)
944 MBB.addLiveIn(Reg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000945 }
946 return true;
947}
948
949
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000950bool HexagonFrameLowering::insertCSRRestoresInBlock(MachineBasicBlock &MBB,
951 const CSIVect &CSI, const HexagonRegisterInfo &HRI) const {
952 if (CSI.empty())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000953 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000954
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000955 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
956 MachineFunction &MF = *MBB.getParent();
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000957 auto &HII = *MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000958
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000959 if (useRestoreFunction(MF, CSI)) {
960 bool HasTC = hasTailCall(MBB) || !hasReturn(MBB);
961 unsigned MaxR = getMaxCalleeSavedReg(CSI, HRI);
962 SpillKind Kind = HasTC ? SK_FromMemTailcall : SK_FromMem;
963 const char *RestoreFn = getSpillFunctionFor(MaxR, Kind);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000964
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000965 // Call spill function.
966 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc()
967 : MBB.getLastNonDebugInstr()->getDebugLoc();
968 MachineInstr *DeallocCall = nullptr;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000969
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000970 if (HasTC) {
971 unsigned ROpc = Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4;
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000972 DeallocCall = BuildMI(MBB, MI, DL, HII.get(ROpc))
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000973 .addExternalSymbol(RestoreFn);
974 } else {
975 // The block has a return.
976 MachineBasicBlock::iterator It = MBB.getFirstTerminator();
977 assert(It->isReturn() && std::next(It) == MBB.end());
978 unsigned ROpc = Hexagon::RESTORE_DEALLOC_RET_JMP_V4;
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000979 DeallocCall = BuildMI(MBB, It, DL, HII.get(ROpc))
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000980 .addExternalSymbol(RestoreFn);
981 // Transfer the function live-out registers.
982 DeallocCall->copyImplicitOps(MF, It);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000983 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000984 addCalleeSaveRegistersAsImpOperand(DeallocCall, MaxR, true);
985 return true;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000986 }
987
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000988 for (unsigned i = 0; i < CSI.size(); ++i) {
989 unsigned Reg = CSI[i].getReg();
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000990 const TargetRegisterClass *RC = HRI.getMinimalPhysRegClass(Reg);
991 int FI = CSI[i].getFrameIdx();
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000992 HII.loadRegFromStackSlot(MBB, MI, Reg, FI, RC, &HRI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000993 }
994 return true;
995}
996
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000997
998void HexagonFrameLowering::eliminateCallFramePseudoInstr(MachineFunction &MF,
999 MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const {
Eli Bendersky8da87162013-02-21 20:05:00 +00001000 MachineInstr &MI = *I;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001001 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszeke5689672015-04-23 20:26:21 +00001002 (void)Opc; // Silence compiler warning.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001003 assert((Opc == Hexagon::ADJCALLSTACKDOWN || Opc == Hexagon::ADJCALLSTACKUP) &&
1004 "Cannot handle this call frame pseudo instruction");
Eli Bendersky8da87162013-02-21 20:05:00 +00001005 MBB.erase(I);
1006}
1007
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001008
1009void HexagonFrameLowering::processFunctionBeforeFrameFinalized(
1010 MachineFunction &MF, RegScavenger *RS) const {
1011 // If this function has uses aligned stack and also has variable sized stack
1012 // objects, then we need to map all spill slots to fixed positions, so that
1013 // they can be accessed through FP. Otherwise they would have to be accessed
1014 // via AP, which may not be available at the particular place in the program.
1015 MachineFrameInfo *MFI = MF.getFrameInfo();
1016 bool HasAlloca = MFI->hasVarSizedObjects();
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001017 bool NeedsAlign = (MFI->getMaxAlignment() > getStackAlignment());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001018
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001019 if (!HasAlloca || !NeedsAlign)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001020 return;
1021
1022 unsigned LFS = MFI->getLocalFrameSize();
1023 int Offset = -LFS;
1024 for (int i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
1025 if (!MFI->isSpillSlotObjectIndex(i) || MFI->isDeadObjectIndex(i))
1026 continue;
1027 int S = MFI->getObjectSize(i);
1028 LFS += S;
1029 Offset -= S;
1030 MFI->mapLocalFrameObject(i, Offset);
1031 }
1032
1033 MFI->setLocalFrameSize(LFS);
1034 unsigned A = MFI->getLocalFrameMaxAlign();
1035 assert(A <= 8 && "Unexpected local frame alignment");
1036 if (A == 0)
1037 MFI->setLocalFrameMaxAlign(8);
1038 MFI->setUseLocalStackAllocationBlock(true);
1039}
1040
1041/// Returns true if there is no caller saved registers available.
1042static bool needToReserveScavengingSpillSlots(MachineFunction &MF,
1043 const HexagonRegisterInfo &HRI) {
1044 MachineRegisterInfo &MRI = MF.getRegInfo();
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001045 const MCPhysReg *CallerSavedRegs = HRI.getCallerSavedRegs(&MF);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001046 // Check for an unused caller-saved register.
1047 for ( ; *CallerSavedRegs; ++CallerSavedRegs) {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001048 MCPhysReg FreeReg = *CallerSavedRegs;
Matthias Braun9912bb82015-07-14 17:52:07 +00001049 if (!MRI.reg_nodbg_empty(FreeReg))
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001050 continue;
1051
1052 // Check aliased register usage.
1053 bool IsCurrentRegUsed = false;
1054 for (MCRegAliasIterator AI(FreeReg, &HRI, false); AI.isValid(); ++AI)
Matthias Braun9912bb82015-07-14 17:52:07 +00001055 if (!MRI.reg_nodbg_empty(*AI)) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001056 IsCurrentRegUsed = true;
1057 break;
1058 }
1059 if (IsCurrentRegUsed)
1060 continue;
1061
1062 // Neither directly used nor used through an aliased register.
1063 return false;
1064 }
1065 // All caller-saved registers are used.
1066 return true;
1067}
1068
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001069
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001070#ifndef NDEBUG
Krzysztof Parzyszeke5689672015-04-23 20:26:21 +00001071static void dump_registers(BitVector &Regs, const TargetRegisterInfo &TRI) {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001072 dbgs() << '{';
1073 for (int x = Regs.find_first(); x >= 0; x = Regs.find_next(x)) {
1074 unsigned R = x;
1075 dbgs() << ' ' << PrintReg(R, &TRI);
1076 }
1077 dbgs() << " }";
1078}
1079#endif
1080
1081
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001082bool HexagonFrameLowering::assignCalleeSavedSpillSlots(MachineFunction &MF,
1083 const TargetRegisterInfo *TRI, std::vector<CalleeSavedInfo> &CSI) const {
Krzysztof Parzyszek27ba19a12015-04-23 20:42:20 +00001084 DEBUG(dbgs() << LLVM_FUNCTION_NAME << " on "
Krzysztof Parzyszeked75e7a2015-04-23 20:57:39 +00001085 << MF.getFunction()->getName() << '\n');
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001086 MachineFrameInfo *MFI = MF.getFrameInfo();
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001087 BitVector SRegs(Hexagon::NUM_TARGET_REGS);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001088
1089 // Generate a set of unique, callee-saved registers (SRegs), where each
1090 // register in the set is maximal in terms of sub-/super-register relation,
1091 // i.e. for each R in SRegs, no proper super-register of R is also in SRegs.
1092
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001093 // (1) For each callee-saved register, add that register and all of its
1094 // sub-registers to SRegs.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001095 DEBUG(dbgs() << "Initial CS registers: {");
1096 for (unsigned i = 0, n = CSI.size(); i < n; ++i) {
1097 unsigned R = CSI[i].getReg();
1098 DEBUG(dbgs() << ' ' << PrintReg(R, TRI));
1099 for (MCSubRegIterator SR(R, TRI, true); SR.isValid(); ++SR)
1100 SRegs[*SR] = true;
1101 }
1102 DEBUG(dbgs() << " }\n");
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001103 DEBUG(dbgs() << "SRegs.1: "; dump_registers(SRegs, *TRI); dbgs() << "\n");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001104
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001105 // (2) For each reserved register, remove that register and all of its
1106 // sub- and super-registers from SRegs.
1107 BitVector Reserved = TRI->getReservedRegs(MF);
1108 for (int x = Reserved.find_first(); x >= 0; x = Reserved.find_next(x)) {
1109 unsigned R = x;
1110 for (MCSuperRegIterator SR(R, TRI, true); SR.isValid(); ++SR)
1111 SRegs[*SR] = false;
1112 }
1113 DEBUG(dbgs() << "Res: "; dump_registers(Reserved, *TRI); dbgs() << "\n");
1114 DEBUG(dbgs() << "SRegs.2: "; dump_registers(SRegs, *TRI); dbgs() << "\n");
1115
1116 // (3) Collect all registers that have at least one sub-register in SRegs,
1117 // and also have no sub-registers that are reserved. These will be the can-
1118 // didates for saving as a whole instead of their individual sub-registers.
1119 // (Saving R17:16 instead of R16 is fine, but only if R17 was not reserved.)
1120 BitVector TmpSup(Hexagon::NUM_TARGET_REGS);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001121 for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) {
1122 unsigned R = x;
1123 for (MCSuperRegIterator SR(R, TRI); SR.isValid(); ++SR)
1124 TmpSup[*SR] = true;
1125 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001126 for (int x = TmpSup.find_first(); x >= 0; x = TmpSup.find_next(x)) {
1127 unsigned R = x;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001128 for (MCSubRegIterator SR(R, TRI, true); SR.isValid(); ++SR) {
1129 if (!Reserved[*SR])
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001130 continue;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001131 TmpSup[R] = false;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001132 break;
1133 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001134 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001135 DEBUG(dbgs() << "TmpSup: "; dump_registers(TmpSup, *TRI); dbgs() << "\n");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001136
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001137 // (4) Include all super-registers found in (3) into SRegs.
1138 SRegs |= TmpSup;
1139 DEBUG(dbgs() << "SRegs.4: "; dump_registers(SRegs, *TRI); dbgs() << "\n");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001140
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001141 // (5) For each register R in SRegs, if any super-register of R is in SRegs,
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001142 // remove R from SRegs.
1143 for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) {
1144 unsigned R = x;
1145 for (MCSuperRegIterator SR(R, TRI); SR.isValid(); ++SR) {
1146 if (!SRegs[*SR])
1147 continue;
1148 SRegs[R] = false;
1149 break;
1150 }
1151 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001152 DEBUG(dbgs() << "SRegs.5: "; dump_registers(SRegs, *TRI); dbgs() << "\n");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001153
1154 // Now, for each register that has a fixed stack slot, create the stack
1155 // object for it.
1156 CSI.clear();
1157
1158 typedef TargetFrameLowering::SpillSlot SpillSlot;
1159 unsigned NumFixed;
1160 int MinOffset = 0; // CS offsets are negative.
1161 const SpillSlot *FixedSlots = getCalleeSavedSpillSlots(NumFixed);
1162 for (const SpillSlot *S = FixedSlots; S != FixedSlots+NumFixed; ++S) {
1163 if (!SRegs[S->Reg])
1164 continue;
1165 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(S->Reg);
1166 int FI = MFI->CreateFixedSpillStackObject(RC->getSize(), S->Offset);
1167 MinOffset = std::min(MinOffset, S->Offset);
1168 CSI.push_back(CalleeSavedInfo(S->Reg, FI));
1169 SRegs[S->Reg] = false;
1170 }
1171
1172 // There can be some registers that don't have fixed slots. For example,
1173 // we need to store R0-R3 in functions with exception handling. For each
1174 // such register, create a non-fixed stack object.
1175 for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) {
1176 unsigned R = x;
1177 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(R);
1178 int Off = MinOffset - RC->getSize();
1179 unsigned Align = std::min(RC->getAlignment(), getStackAlignment());
1180 assert(isPowerOf2_32(Align));
1181 Off &= -Align;
1182 int FI = MFI->CreateFixedSpillStackObject(RC->getSize(), Off);
1183 MinOffset = std::min(MinOffset, Off);
1184 CSI.push_back(CalleeSavedInfo(R, FI));
1185 SRegs[R] = false;
1186 }
1187
1188 DEBUG({
1189 dbgs() << "CS information: {";
1190 for (unsigned i = 0, n = CSI.size(); i < n; ++i) {
1191 int FI = CSI[i].getFrameIdx();
1192 int Off = MFI->getObjectOffset(FI);
1193 dbgs() << ' ' << PrintReg(CSI[i].getReg(), TRI) << ":fi#" << FI << ":sp";
1194 if (Off >= 0)
1195 dbgs() << '+';
1196 dbgs() << Off;
1197 }
1198 dbgs() << " }\n";
1199 });
1200
1201#ifndef NDEBUG
1202 // Verify that all registers were handled.
1203 bool MissedReg = false;
1204 for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) {
1205 unsigned R = x;
1206 dbgs() << PrintReg(R, TRI) << ' ';
1207 MissedReg = true;
1208 }
1209 if (MissedReg)
1210 llvm_unreachable("...there are unhandled callee-saved registers!");
1211#endif
1212
1213 return true;
1214}
1215
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001216
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001217bool HexagonFrameLowering::expandCopy(MachineBasicBlock &B,
1218 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
1219 const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const {
1220 MachineInstr *MI = &*It;
1221 DebugLoc DL = MI->getDebugLoc();
1222 unsigned DstR = MI->getOperand(0).getReg();
1223 unsigned SrcR = MI->getOperand(1).getReg();
1224 if (!Hexagon::ModRegsRegClass.contains(DstR) ||
1225 !Hexagon::ModRegsRegClass.contains(SrcR))
1226 return false;
1227
1228 unsigned TmpR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
1229 BuildMI(B, It, DL, HII.get(TargetOpcode::COPY), TmpR)
1230 .addOperand(MI->getOperand(1));
1231 BuildMI(B, It, DL, HII.get(TargetOpcode::COPY), DstR)
1232 .addReg(TmpR, RegState::Kill);
1233
1234 NewRegs.push_back(TmpR);
1235 B.erase(It);
1236 return true;
1237}
1238
1239bool HexagonFrameLowering::expandStoreInt(MachineBasicBlock &B,
1240 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
1241 const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const {
1242 MachineInstr *MI = &*It;
1243 DebugLoc DL = MI->getDebugLoc();
1244 unsigned Opc = MI->getOpcode();
1245 unsigned SrcR = MI->getOperand(2).getReg();
1246 bool IsKill = MI->getOperand(2).isKill();
1247
1248 assert(MI->getOperand(0).isFI() && "Expect a frame index");
1249 int FI = MI->getOperand(0).getIndex();
1250
1251 // TmpR = C2_tfrpr SrcR if SrcR is a predicate register
1252 // TmpR = A2_tfrcrr SrcR if SrcR is a modifier register
1253 unsigned TmpR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
1254 unsigned TfrOpc = (Opc == Hexagon::STriw_pred) ? Hexagon::C2_tfrpr
1255 : Hexagon::A2_tfrcrr;
1256 BuildMI(B, It, DL, HII.get(TfrOpc), TmpR)
1257 .addReg(SrcR, getKillRegState(IsKill));
1258
1259 // S2_storeri_io FI, 0, TmpR
1260 BuildMI(B, It, DL, HII.get(Hexagon::S2_storeri_io))
1261 .addFrameIndex(FI)
1262 .addImm(0)
1263 .addReg(TmpR, RegState::Kill)
1264 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
1265
1266 NewRegs.push_back(TmpR);
1267 B.erase(It);
1268 return true;
1269}
1270
1271bool HexagonFrameLowering::expandLoadInt(MachineBasicBlock &B,
1272 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
1273 const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const {
1274 MachineInstr *MI = &*It;
1275 DebugLoc DL = MI->getDebugLoc();
1276 unsigned Opc = MI->getOpcode();
1277 unsigned DstR = MI->getOperand(0).getReg();
1278
1279 assert(MI->getOperand(1).isFI() && "Expect a frame index");
1280 int FI = MI->getOperand(1).getIndex();
1281
1282 // TmpR = L2_loadri_io FI, 0
1283 unsigned TmpR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
1284 BuildMI(B, It, DL, HII.get(Hexagon::L2_loadri_io), TmpR)
1285 .addFrameIndex(FI)
1286 .addImm(0)
1287 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
1288
1289 // DstR = C2_tfrrp TmpR if DstR is a predicate register
1290 // DstR = A2_tfrrcr TmpR if DstR is a modifier register
1291 unsigned TfrOpc = (Opc == Hexagon::LDriw_pred) ? Hexagon::C2_tfrrp
1292 : Hexagon::A2_tfrrcr;
1293 BuildMI(B, It, DL, HII.get(TfrOpc), DstR)
1294 .addReg(TmpR, RegState::Kill);
1295
1296 NewRegs.push_back(TmpR);
1297 B.erase(It);
1298 return true;
1299}
1300
1301
1302bool HexagonFrameLowering::expandStoreVecPred(MachineBasicBlock &B,
1303 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
1304 const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const {
1305 auto &HST = B.getParent()->getSubtarget<HexagonSubtarget>();
1306 MachineInstr *MI = &*It;
1307 DebugLoc DL = MI->getDebugLoc();
1308 unsigned SrcR = MI->getOperand(2).getReg();
1309 bool IsKill = MI->getOperand(2).isKill();
1310
1311 assert(MI->getOperand(0).isFI() && "Expect a frame index");
1312 int FI = MI->getOperand(0).getIndex();
1313
1314 bool Is128B = HST.useHVXDblOps();
1315 auto *RC = !Is128B ? &Hexagon::VectorRegsRegClass
1316 : &Hexagon::VectorRegs128BRegClass;
1317
1318 // Insert transfer to general vector register.
1319 // TmpR0 = A2_tfrsi 0x01010101
1320 // TmpR1 = V6_vandqrt Qx, TmpR0
1321 // store FI, 0, TmpR1
1322 unsigned TmpR0 = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
1323 unsigned TmpR1 = MRI.createVirtualRegister(RC);
1324
1325 BuildMI(B, It, DL, HII.get(Hexagon::A2_tfrsi), TmpR0)
1326 .addImm(0x01010101);
1327
1328 unsigned VandOpc = !Is128B ? Hexagon::V6_vandqrt : Hexagon::V6_vandqrt_128B;
1329 BuildMI(B, It, DL, HII.get(VandOpc), TmpR1)
1330 .addReg(SrcR, getKillRegState(IsKill))
1331 .addReg(TmpR0, RegState::Kill);
1332
1333 auto *HRI = B.getParent()->getSubtarget<HexagonSubtarget>().getRegisterInfo();
1334 HII.storeRegToStackSlot(B, It, TmpR1, true, FI, RC, HRI);
1335 expandStoreVec(B, std::prev(It), MRI, HII, NewRegs);
1336
1337 NewRegs.push_back(TmpR0);
1338 NewRegs.push_back(TmpR1);
1339 B.erase(It);
1340 return true;
1341}
1342
1343bool HexagonFrameLowering::expandLoadVecPred(MachineBasicBlock &B,
1344 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
1345 const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const {
1346 auto &HST = B.getParent()->getSubtarget<HexagonSubtarget>();
1347 MachineInstr *MI = &*It;
1348 DebugLoc DL = MI->getDebugLoc();
1349 unsigned DstR = MI->getOperand(0).getReg();
1350
1351 assert(MI->getOperand(1).isFI() && "Expect a frame index");
1352 int FI = MI->getOperand(1).getIndex();
1353
1354 bool Is128B = HST.useHVXDblOps();
1355 auto *RC = !Is128B ? &Hexagon::VectorRegsRegClass
1356 : &Hexagon::VectorRegs128BRegClass;
1357
1358 // TmpR0 = A2_tfrsi 0x01010101
1359 // TmpR1 = load FI, 0
1360 // DstR = V6_vandvrt TmpR1, TmpR0
1361 unsigned TmpR0 = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
1362 unsigned TmpR1 = MRI.createVirtualRegister(RC);
1363
1364 BuildMI(B, It, DL, HII.get(Hexagon::A2_tfrsi), TmpR0)
1365 .addImm(0x01010101);
1366 auto *HRI = B.getParent()->getSubtarget<HexagonSubtarget>().getRegisterInfo();
1367 HII.loadRegFromStackSlot(B, It, TmpR1, FI, RC, HRI);
1368 expandLoadVec(B, std::prev(It), MRI, HII, NewRegs);
1369
1370 unsigned VandOpc = !Is128B ? Hexagon::V6_vandvrt : Hexagon::V6_vandvrt_128B;
1371 BuildMI(B, It, DL, HII.get(VandOpc), DstR)
1372 .addReg(TmpR1, RegState::Kill)
1373 .addReg(TmpR0, RegState::Kill);
1374
1375 NewRegs.push_back(TmpR0);
1376 NewRegs.push_back(TmpR1);
1377 B.erase(It);
1378 return true;
1379}
1380
1381bool HexagonFrameLowering::expandStoreVec2(MachineBasicBlock &B,
1382 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
1383 const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const {
1384 MachineFunction &MF = *B.getParent();
1385 auto &HST = MF.getSubtarget<HexagonSubtarget>();
1386 auto &MFI = *MF.getFrameInfo();
1387 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
1388 MachineInstr *MI = &*It;
1389 DebugLoc DL = MI->getDebugLoc();
1390
1391 unsigned SrcR = MI->getOperand(2).getReg();
1392 unsigned SrcLo = HRI.getSubReg(SrcR, Hexagon::subreg_loreg);
1393 unsigned SrcHi = HRI.getSubReg(SrcR, Hexagon::subreg_hireg);
1394 bool IsKill = MI->getOperand(2).isKill();
1395
1396 assert(MI->getOperand(0).isFI() && "Expect a frame index");
1397 int FI = MI->getOperand(0).getIndex();
1398
1399 bool Is128B = HST.useHVXDblOps();
1400 auto *RC = !Is128B ? &Hexagon::VectorRegsRegClass
1401 : &Hexagon::VectorRegs128BRegClass;
1402 unsigned Size = RC->getSize();
1403 unsigned NeedAlign = RC->getAlignment();
1404 unsigned HasAlign = MFI.getObjectAlignment(FI);
1405 unsigned StoreOpc;
1406
1407 // Store low part.
1408 if (NeedAlign <= HasAlign)
1409 StoreOpc = !Is128B ? Hexagon::V6_vS32b_ai : Hexagon::V6_vS32b_ai_128B;
1410 else
1411 StoreOpc = !Is128B ? Hexagon::V6_vS32Ub_ai : Hexagon::V6_vS32Ub_ai_128B;
1412
1413 BuildMI(B, It, DL, HII.get(StoreOpc))
1414 .addFrameIndex(FI)
1415 .addImm(0)
1416 .addReg(SrcLo, getKillRegState(IsKill))
1417 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
1418
1419 // Load high part.
1420 if (NeedAlign <= MinAlign(HasAlign, Size))
1421 StoreOpc = !Is128B ? Hexagon::V6_vS32b_ai : Hexagon::V6_vS32b_ai_128B;
1422 else
1423 StoreOpc = !Is128B ? Hexagon::V6_vS32Ub_ai : Hexagon::V6_vS32Ub_ai_128B;
1424
1425 BuildMI(B, It, DL, HII.get(StoreOpc))
1426 .addFrameIndex(FI)
1427 .addImm(Size)
1428 .addReg(SrcHi, getKillRegState(IsKill))
1429 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
1430
1431 B.erase(It);
1432 return true;
1433}
1434
1435bool HexagonFrameLowering::expandLoadVec2(MachineBasicBlock &B,
1436 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
1437 const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const {
1438 MachineFunction &MF = *B.getParent();
1439 auto &HST = MF.getSubtarget<HexagonSubtarget>();
1440 auto &MFI = *MF.getFrameInfo();
1441 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
1442 MachineInstr *MI = &*It;
1443 DebugLoc DL = MI->getDebugLoc();
1444
1445 unsigned DstR = MI->getOperand(0).getReg();
1446 unsigned DstHi = HRI.getSubReg(DstR, Hexagon::subreg_hireg);
1447 unsigned DstLo = HRI.getSubReg(DstR, Hexagon::subreg_loreg);
1448
1449 assert(MI->getOperand(1).isFI() && "Expect a frame index");
1450 int FI = MI->getOperand(1).getIndex();
1451
1452 bool Is128B = HST.useHVXDblOps();
1453 auto *RC = !Is128B ? &Hexagon::VectorRegsRegClass
1454 : &Hexagon::VectorRegs128BRegClass;
1455 unsigned Size = RC->getSize();
1456 unsigned NeedAlign = RC->getAlignment();
1457 unsigned HasAlign = MFI.getObjectAlignment(FI);
1458 unsigned LoadOpc;
1459
1460 // Load low part.
1461 if (NeedAlign <= HasAlign)
1462 LoadOpc = !Is128B ? Hexagon::V6_vL32b_ai : Hexagon::V6_vL32b_ai_128B;
1463 else
1464 LoadOpc = !Is128B ? Hexagon::V6_vL32Ub_ai : Hexagon::V6_vL32Ub_ai_128B;
1465
1466 BuildMI(B, It, DL, HII.get(LoadOpc), DstLo)
1467 .addFrameIndex(FI)
1468 .addImm(0)
1469 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
1470
1471 // Load high part.
1472 if (NeedAlign <= MinAlign(HasAlign, Size))
1473 LoadOpc = !Is128B ? Hexagon::V6_vL32b_ai : Hexagon::V6_vL32b_ai_128B;
1474 else
1475 LoadOpc = !Is128B ? Hexagon::V6_vL32Ub_ai : Hexagon::V6_vL32Ub_ai_128B;
1476
1477 BuildMI(B, It, DL, HII.get(LoadOpc), DstHi)
1478 .addFrameIndex(FI)
1479 .addImm(Size)
1480 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
1481
1482 B.erase(It);
1483 return true;
1484}
1485
1486bool HexagonFrameLowering::expandStoreVec(MachineBasicBlock &B,
1487 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
1488 const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const {
1489 MachineFunction &MF = *B.getParent();
1490 auto &HST = MF.getSubtarget<HexagonSubtarget>();
1491 auto &MFI = *MF.getFrameInfo();
1492 MachineInstr *MI = &*It;
1493 DebugLoc DL = MI->getDebugLoc();
1494
1495 unsigned SrcR = MI->getOperand(2).getReg();
1496 bool IsKill = MI->getOperand(2).isKill();
1497
1498 assert(MI->getOperand(0).isFI() && "Expect a frame index");
1499 int FI = MI->getOperand(0).getIndex();
1500
1501 bool Is128B = HST.useHVXDblOps();
1502 auto *RC = !Is128B ? &Hexagon::VectorRegsRegClass
1503 : &Hexagon::VectorRegs128BRegClass;
1504
1505 unsigned NeedAlign = RC->getAlignment();
1506 unsigned HasAlign = MFI.getObjectAlignment(FI);
1507 unsigned StoreOpc;
1508
1509 if (NeedAlign <= HasAlign)
1510 StoreOpc = !Is128B ? Hexagon::V6_vS32b_ai : Hexagon::V6_vS32b_ai_128B;
1511 else
1512 StoreOpc = !Is128B ? Hexagon::V6_vS32Ub_ai : Hexagon::V6_vS32Ub_ai_128B;
1513
1514 BuildMI(B, It, DL, HII.get(StoreOpc))
1515 .addFrameIndex(FI)
1516 .addImm(0)
1517 .addReg(SrcR, getKillRegState(IsKill))
1518 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
1519
1520 B.erase(It);
1521 return true;
1522}
1523
1524bool HexagonFrameLowering::expandLoadVec(MachineBasicBlock &B,
1525 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
1526 const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const {
1527 MachineFunction &MF = *B.getParent();
1528 auto &HST = MF.getSubtarget<HexagonSubtarget>();
1529 auto &MFI = *MF.getFrameInfo();
1530 MachineInstr *MI = &*It;
1531 DebugLoc DL = MI->getDebugLoc();
1532
1533 unsigned DstR = MI->getOperand(0).getReg();
1534
1535 assert(MI->getOperand(1).isFI() && "Expect a frame index");
1536 int FI = MI->getOperand(1).getIndex();
1537
1538 bool Is128B = HST.useHVXDblOps();
1539 auto *RC = !Is128B ? &Hexagon::VectorRegsRegClass
1540 : &Hexagon::VectorRegs128BRegClass;
1541
1542 unsigned NeedAlign = RC->getAlignment();
1543 unsigned HasAlign = MFI.getObjectAlignment(FI);
1544 unsigned LoadOpc;
1545
1546 if (NeedAlign <= HasAlign)
1547 LoadOpc = !Is128B ? Hexagon::V6_vL32b_ai : Hexagon::V6_vL32b_ai_128B;
1548 else
1549 LoadOpc = !Is128B ? Hexagon::V6_vL32Ub_ai : Hexagon::V6_vL32Ub_ai_128B;
1550
1551 BuildMI(B, It, DL, HII.get(LoadOpc), DstR)
1552 .addFrameIndex(FI)
1553 .addImm(0)
1554 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
1555
1556 B.erase(It);
1557 return true;
1558}
1559
1560
1561bool HexagonFrameLowering::expandSpillMacros(MachineFunction &MF,
1562 SmallVectorImpl<unsigned> &NewRegs) const {
1563 auto &HST = MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00001564 auto &HII = *HST.getInstrInfo();
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00001565 MachineRegisterInfo &MRI = MF.getRegInfo();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001566 bool Changed = false;
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00001567
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001568 for (auto &B : MF) {
1569 // Traverse the basic block.
1570 MachineBasicBlock::iterator NextI;
1571 for (auto I = B.begin(), E = B.end(); I != E; I = NextI) {
1572 MachineInstr *MI = &*I;
1573 NextI = std::next(I);
1574 unsigned Opc = MI->getOpcode();
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00001575
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001576 switch (Opc) {
1577 case TargetOpcode::COPY:
1578 Changed = expandCopy(B, I, MRI, HII, NewRegs);
1579 break;
1580 case Hexagon::STriw_pred:
1581 case Hexagon::STriw_mod:
1582 Changed = expandStoreInt(B, I, MRI, HII, NewRegs);
1583 break;
1584 case Hexagon::LDriw_pred:
1585 case Hexagon::LDriw_mod:
1586 Changed = expandLoadInt(B, I, MRI, HII, NewRegs);
1587 break;
1588 case Hexagon::STriq_pred_V6:
1589 case Hexagon::STriq_pred_V6_128B:
1590 Changed = expandStoreVecPred(B, I, MRI, HII, NewRegs);
1591 break;
1592 case Hexagon::LDriq_pred_V6:
1593 case Hexagon::LDriq_pred_V6_128B:
1594 Changed = expandLoadVecPred(B, I, MRI, HII, NewRegs);
1595 break;
1596 case Hexagon::LDrivv_pseudo_V6:
1597 case Hexagon::LDrivv_pseudo_V6_128B:
1598 Changed = expandLoadVec2(B, I, MRI, HII, NewRegs);
1599 break;
1600 case Hexagon::STrivv_pseudo_V6:
1601 case Hexagon::STrivv_pseudo_V6_128B:
1602 Changed = expandStoreVec2(B, I, MRI, HII, NewRegs);
1603 break;
1604 case Hexagon::STriv_pseudo_V6:
1605 case Hexagon::STriv_pseudo_V6_128B:
1606 Changed = expandStoreVec(B, I, MRI, HII, NewRegs);
1607 break;
1608 case Hexagon::LDriv_pseudo_V6:
1609 case Hexagon::LDriv_pseudo_V6_128B:
1610 Changed = expandLoadVec(B, I, MRI, HII, NewRegs);
1611 break;
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00001612 }
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00001613 }
1614 }
1615
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001616 return Changed;
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00001617}
1618
1619
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001620void HexagonFrameLowering::determineCalleeSaves(MachineFunction &MF,
1621 BitVector &SavedRegs,
1622 RegScavenger *RS) const {
1623 auto &HST = MF.getSubtarget<HexagonSubtarget>();
1624 auto &HRI = *HST.getRegisterInfo();
1625
1626 SavedRegs.resize(HRI.getNumRegs());
1627
1628 // If we have a function containing __builtin_eh_return we want to spill and
1629 // restore all callee saved registers. Pretend that they are used.
1630 if (MF.getInfo<HexagonMachineFunctionInfo>()->hasEHReturn())
1631 for (const MCPhysReg *R = HRI.getCalleeSavedRegs(&MF); *R; ++R)
1632 SavedRegs.set(*R);
1633
1634 // Replace predicate register pseudo spill code.
1635 SmallVector<unsigned,8> NewRegs;
1636 expandSpillMacros(MF, NewRegs);
1637
1638
1639 // We need to reserve a a spill slot if scavenging could potentially require
1640 // spilling a scavenged register.
1641 if (!NewRegs.empty() && needToReserveScavengingSpillSlots(MF, HRI)) {
1642 MachineRegisterInfo &MRI = MF.getRegInfo();
1643 SetVector<const TargetRegisterClass*> SpillRCs;
1644 for (unsigned VR : NewRegs)
1645 SpillRCs.insert(MRI.getRegClass(VR));
1646
1647 MachineFrameInfo &MFI = *MF.getFrameInfo();
1648 const TargetRegisterClass &IntRC = Hexagon::IntRegsRegClass;
1649 if (SpillRCs.count(&IntRC)) {
1650 for (int i = 0; i < NumberScavengerSlots; i++) {
1651 int NewFI = MFI.CreateSpillStackObject(IntRC.getSize(),
1652 IntRC.getAlignment());
1653 RS->addScavengingFrameIndex(NewFI);
1654 }
1655 }
1656 for (auto *RC : SpillRCs) {
1657 if (RC == &IntRC)
1658 continue;
1659 int NewFI = MFI.CreateSpillStackObject(RC->getSize(), RC->getAlignment());
1660 RS->addScavengingFrameIndex(NewFI);
1661 }
1662 }
1663
1664 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1665}
1666
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00001667
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001668void HexagonFrameLowering::expandAlloca(MachineInstr *AI,
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001669 const HexagonInstrInfo &HII, unsigned SP, unsigned CF) const {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001670 MachineBasicBlock &MB = *AI->getParent();
1671 DebugLoc DL = AI->getDebugLoc();
1672 unsigned A = AI->getOperand(2).getImm();
1673
1674 // Have
1675 // Rd = alloca Rs, #A
1676 //
1677 // If Rs and Rd are different registers, use this sequence:
1678 // Rd = sub(r29, Rs)
1679 // r29 = sub(r29, Rs)
1680 // Rd = and(Rd, #-A) ; if necessary
1681 // r29 = and(r29, #-A) ; if necessary
1682 // Rd = add(Rd, #CF) ; CF size aligned to at most A
1683 // otherwise, do
1684 // Rd = sub(r29, Rs)
1685 // Rd = and(Rd, #-A) ; if necessary
1686 // r29 = Rd
1687 // Rd = add(Rd, #CF) ; CF size aligned to at most A
1688
1689 MachineOperand &RdOp = AI->getOperand(0);
1690 MachineOperand &RsOp = AI->getOperand(1);
1691 unsigned Rd = RdOp.getReg(), Rs = RsOp.getReg();
1692
1693 // Rd = sub(r29, Rs)
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001694 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_sub), Rd)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001695 .addReg(SP)
1696 .addReg(Rs);
1697 if (Rs != Rd) {
1698 // r29 = sub(r29, Rs)
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001699 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_sub), SP)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001700 .addReg(SP)
1701 .addReg(Rs);
1702 }
1703 if (A > 8) {
1704 // Rd = and(Rd, #-A)
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001705 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_andir), Rd)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001706 .addReg(Rd)
1707 .addImm(-int64_t(A));
1708 if (Rs != Rd)
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001709 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_andir), SP)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001710 .addReg(SP)
1711 .addImm(-int64_t(A));
1712 }
1713 if (Rs == Rd) {
1714 // r29 = Rd
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001715 BuildMI(MB, AI, DL, HII.get(TargetOpcode::COPY), SP)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001716 .addReg(Rd);
1717 }
1718 if (CF > 0) {
1719 // Rd = add(Rd, #CF)
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001720 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_addi), Rd)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001721 .addReg(Rd)
1722 .addImm(CF);
1723 }
1724}
1725
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001726
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001727bool HexagonFrameLowering::needsAligna(const MachineFunction &MF) const {
1728 const MachineFrameInfo *MFI = MF.getFrameInfo();
1729 if (!MFI->hasVarSizedObjects())
1730 return false;
1731 unsigned MaxA = MFI->getMaxAlignment();
1732 if (MaxA <= getStackAlignment())
1733 return false;
1734 return true;
1735}
1736
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001737
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001738const MachineInstr *HexagonFrameLowering::getAlignaInstr(
1739 const MachineFunction &MF) const {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001740 for (auto &B : MF)
1741 for (auto &I : B)
1742 if (I.getOpcode() == Hexagon::ALIGNA)
1743 return &I;
1744 return nullptr;
1745}
1746
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001747
Sanjay Patel924879a2015-08-04 15:49:57 +00001748// FIXME: Use Function::optForSize().
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001749inline static bool isOptSize(const MachineFunction &MF) {
1750 AttributeSet AF = MF.getFunction()->getAttributes();
1751 return AF.hasAttribute(AttributeSet::FunctionIndex,
1752 Attribute::OptimizeForSize);
1753}
1754
1755inline static bool isMinSize(const MachineFunction &MF) {
Sanjay Patel924879a2015-08-04 15:49:57 +00001756 return MF.getFunction()->optForMinSize();
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001757}
1758
1759
1760/// Determine whether the callee-saved register saves and restores should
1761/// be generated via inline code. If this function returns "true", inline
1762/// code will be generated. If this function returns "false", additional
1763/// checks are performed, which may still lead to the inline code.
1764bool HexagonFrameLowering::shouldInlineCSR(MachineFunction &MF,
1765 const CSIVect &CSI) const {
1766 if (MF.getInfo<HexagonMachineFunctionInfo>()->hasEHReturn())
1767 return true;
1768 if (!isOptSize(MF) && !isMinSize(MF))
1769 if (MF.getTarget().getOptLevel() > CodeGenOpt::Default)
1770 return true;
1771
1772 // Check if CSI only has double registers, and if the registers form
1773 // a contiguous block starting from D8.
1774 BitVector Regs(Hexagon::NUM_TARGET_REGS);
1775 for (unsigned i = 0, n = CSI.size(); i < n; ++i) {
1776 unsigned R = CSI[i].getReg();
1777 if (!Hexagon::DoubleRegsRegClass.contains(R))
1778 return true;
1779 Regs[R] = true;
1780 }
1781 int F = Regs.find_first();
1782 if (F != Hexagon::D8)
1783 return true;
1784 while (F >= 0) {
1785 int N = Regs.find_next(F);
1786 if (N >= 0 && N != F+1)
1787 return true;
1788 F = N;
1789 }
1790
1791 return false;
1792}
1793
1794
1795bool HexagonFrameLowering::useSpillFunction(MachineFunction &MF,
1796 const CSIVect &CSI) const {
1797 if (shouldInlineCSR(MF, CSI))
1798 return false;
1799 unsigned NumCSI = CSI.size();
1800 if (NumCSI <= 1)
1801 return false;
1802
1803 unsigned Threshold = isOptSize(MF) ? SpillFuncThresholdOs
1804 : SpillFuncThreshold;
1805 return Threshold < NumCSI;
1806}
1807
1808
1809bool HexagonFrameLowering::useRestoreFunction(MachineFunction &MF,
1810 const CSIVect &CSI) const {
1811 if (shouldInlineCSR(MF, CSI))
1812 return false;
1813 unsigned NumCSI = CSI.size();
1814 unsigned Threshold = isOptSize(MF) ? SpillFuncThresholdOs-1
1815 : SpillFuncThreshold;
1816 return Threshold < NumCSI;
1817}