blob: 4a47231ddd82ba3a86e7ade635a91ee77feb4cee [file] [log] [blame]
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +00001//===- HexagonFrameLowering.cpp - Define frame lowering -------------------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Tony Linthicum1213a7a2011-12-12 21:14:40 +00006//
7//
8//===----------------------------------------------------------------------===//
Benjamin Kramerae87d7b2012-02-06 10:19:29 +00009
Craig Topperb25fda92012-03-17 18:46:09 +000010#include "HexagonFrameLowering.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000011#include "HexagonBlockRanges.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000012#include "HexagonInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "HexagonMachineFunctionInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000014#include "HexagonRegisterInfo.h"
15#include "HexagonSubtarget.h"
16#include "HexagonTargetMachine.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000017#include "MCTargetDesc/HexagonBaseInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000018#include "llvm/ADT/BitVector.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000019#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/None.h"
21#include "llvm/ADT/Optional.h"
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +000022#include "llvm/ADT/PostOrderIterator.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000023#include "llvm/ADT/SetVector.h"
24#include "llvm/ADT/SmallSet.h"
25#include "llvm/ADT/SmallVector.h"
Krzysztof Parzyszekb71085b2016-10-21 16:38:29 +000026#include "llvm/CodeGen/LivePhysRegs.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000027#include "llvm/CodeGen/MachineBasicBlock.h"
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +000028#include "llvm/CodeGen/MachineDominators.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000030#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineFunctionPass.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000032#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000034#include "llvm/CodeGen/MachineMemOperand.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000035#include "llvm/CodeGen/MachineModuleInfo.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000036#include "llvm/CodeGen/MachineOperand.h"
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +000037#include "llvm/CodeGen/MachinePostDominators.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000038#include "llvm/CodeGen/MachineRegisterInfo.h"
39#include "llvm/CodeGen/RegisterScavenging.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000040#include "llvm/CodeGen/TargetRegisterInfo.h"
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000041#include "llvm/IR/Attributes.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000042#include "llvm/IR/DebugLoc.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000043#include "llvm/IR/Function.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000044#include "llvm/MC/MCDwarf.h"
45#include "llvm/MC/MCRegisterInfo.h"
46#include "llvm/Pass.h"
47#include "llvm/Support/CodeGen.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000048#include "llvm/Support/CommandLine.h"
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000049#include "llvm/Support/Compiler.h"
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000050#include "llvm/Support/Debug.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000053#include "llvm/Support/raw_ostream.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000054#include "llvm/Target/TargetMachine.h"
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000055#include "llvm/Target/TargetOptions.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000056#include <algorithm>
57#include <cassert>
58#include <cstdint>
59#include <iterator>
60#include <limits>
61#include <map>
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000062#include <utility>
63#include <vector>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000064
Jakub Kuderski34327d22017-07-13 20:26:45 +000065#define DEBUG_TYPE "hexagon-pei"
66
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000067// Hexagon stack frame layout as defined by the ABI:
68//
69// Incoming arguments
70// passed via stack
71// |
72// |
73// SP during function's FP during function's |
74// +-- runtime (top of stack) runtime (bottom) --+ |
75// | | |
76// --++---------------------+------------------+-----------------++-+-------
77// | parameter area for | variable-size | fixed-size |LR| arg
78// | called functions | local objects | local objects |FP|
79// --+----------------------+------------------+-----------------+--+-------
80// <- size known -> <- size unknown -> <- size known ->
81//
82// Low address High address
83//
84// <--- stack growth
85//
86//
87// - In any circumstances, the outgoing function arguments are always accessi-
88// ble using the SP, and the incoming arguments are accessible using the FP.
89// - If the local objects are not aligned, they can always be accessed using
90// the FP.
91// - If there are no variable-sized objects, the local objects can always be
92// accessed using the SP, regardless whether they are aligned or not. (The
93// alignment padding will be at the bottom of the stack (highest address),
94// and so the offset with respect to the SP will be known at the compile-
95// -time.)
96//
97// The only complication occurs if there are both, local aligned objects, and
98// dynamically allocated (variable-sized) objects. The alignment pad will be
99// placed between the FP and the local objects, thus preventing the use of the
100// FP to access the local objects. At the same time, the variable-sized objects
101// will be between the SP and the local objects, thus introducing an unknown
102// distance from the SP to the locals.
103//
104// To avoid this problem, a new register is created that holds the aligned
105// address of the bottom of the stack, referred in the sources as AP (aligned
106// pointer). The AP will be equal to "FP-p", where "p" is the smallest pad
107// that aligns AP to the required boundary (a maximum of the alignments of
108// all stack objects, fixed- and variable-sized). All local objects[1] will
109// then use AP as the base pointer.
110// [1] The exception is with "fixed" stack objects. "Fixed" stack objects get
111// their name from being allocated at fixed locations on the stack, relative
112// to the FP. In the presence of dynamic allocation and local alignment, such
113// objects can only be accessed through the FP.
114//
115// Illustration of the AP:
116// FP --+
117// |
118// ---------------+---------------------+-----+-----------------------++-+--
119// Rest of the | Local stack objects | Pad | Fixed stack objects |LR|
120// stack frame | (aligned) | | (CSR, spills, etc.) |FP|
121// ---------------+---------------------+-----+-----------------+-----+--+--
122// |<-- Multiple of the -->|
123// stack alignment +-- AP
124//
125// The AP is set up at the beginning of the function. Since it is not a dedi-
126// cated (reserved) register, it needs to be kept live throughout the function
127// to be available as the base register for local object accesses.
128// Normally, an address of a stack objects is obtained by a pseudo-instruction
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +0000129// PS_fi. To access local objects with the AP register present, a different
130// pseudo-instruction needs to be used: PS_fia. The PS_fia takes one extra
131// argument compared to PS_fi: the first input register is the AP register.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000132// This keeps the register live between its definition and its uses.
133
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +0000134// The AP register is originally set up using pseudo-instruction PS_aligna:
135// AP = PS_aligna A
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000136// where
137// A - required stack alignment
138// The alignment value must be the maximum of all alignments required by
139// any stack object.
140
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +0000141// The dynamic allocation uses a pseudo-instruction PS_alloca:
142// Rd = PS_alloca Rs, A
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000143// where
144// Rd - address of the allocated space
145// Rs - minimum size (the actual allocated can be larger to accommodate
146// alignment)
147// A - required alignment
148
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000149using namespace llvm;
150
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000151static cl::opt<bool> DisableDeallocRet("disable-hexagon-dealloc-ret",
152 cl::Hidden, cl::desc("Disable Dealloc Return for Hexagon target"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000153
Krzysztof Parzyszeka5bd29542016-05-16 18:02:28 +0000154static cl::opt<unsigned> NumberScavengerSlots("number-scavenger-slots",
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000155 cl::Hidden, cl::desc("Set the number of scavenger slots"), cl::init(2),
156 cl::ZeroOrMore);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000157
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000158static cl::opt<int> SpillFuncThreshold("spill-func-threshold",
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000159 cl::Hidden, cl::desc("Specify O2(not Os) spill func threshold"),
160 cl::init(6), cl::ZeroOrMore);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000161
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000162static cl::opt<int> SpillFuncThresholdOs("spill-func-threshold-Os",
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000163 cl::Hidden, cl::desc("Specify Os spill func threshold"),
164 cl::init(1), cl::ZeroOrMore);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000165
Krzysztof Parzyszekc9d4caa2016-03-24 20:20:07 +0000166static cl::opt<bool> EnableStackOVFSanitizer("enable-stackovf-sanitizer",
167 cl::Hidden, cl::desc("Enable runtime checks for stack overflow."),
168 cl::init(false), cl::ZeroOrMore);
169
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000170static cl::opt<bool> EnableShrinkWrapping("hexagon-shrink-frame",
171 cl::init(true), cl::Hidden, cl::ZeroOrMore,
172 cl::desc("Enable stack frame shrink wrapping"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000173
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000174static cl::opt<unsigned> ShrinkLimit("shrink-frame-limit",
175 cl::init(std::numeric_limits<unsigned>::max()), cl::Hidden, cl::ZeroOrMore,
176 cl::desc("Max count of stack frame shrink-wraps"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000177
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000178static cl::opt<bool> EnableSaveRestoreLong("enable-save-restore-long",
179 cl::Hidden, cl::desc("Enable long calls for save-restore stubs."),
180 cl::init(false), cl::ZeroOrMore);
181
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000182static cl::opt<bool> EliminateFramePointer("hexagon-fp-elim", cl::init(true),
183 cl::Hidden, cl::desc("Refrain from using FP whenever possible"));
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000184
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +0000185static cl::opt<bool> OptimizeSpillSlots("hexagon-opt-spill", cl::Hidden,
186 cl::init(true), cl::desc("Optimize spill slots"));
187
Krzysztof Parzyszekdc421642016-07-27 20:58:43 +0000188#ifndef NDEBUG
189static cl::opt<unsigned> SpillOptMax("spill-opt-max", cl::Hidden,
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000190 cl::init(std::numeric_limits<unsigned>::max()));
Krzysztof Parzyszekdc421642016-07-27 20:58:43 +0000191static unsigned SpillOptCount = 0;
192#endif
193
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000194namespace llvm {
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000195
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000196 void initializeHexagonCallFrameInformationPass(PassRegistry&);
197 FunctionPass *createHexagonCallFrameInformation();
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000198
199} // end namespace llvm
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000200
201namespace {
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000202
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000203 class HexagonCallFrameInformation : public MachineFunctionPass {
204 public:
205 static char ID;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000206
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000207 HexagonCallFrameInformation() : MachineFunctionPass(ID) {
208 PassRegistry &PR = *PassRegistry::getPassRegistry();
209 initializeHexagonCallFrameInformationPass(PR);
210 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000211
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000212 bool runOnMachineFunction(MachineFunction &MF) override;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000213
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000214 MachineFunctionProperties getRequiredProperties() const override {
215 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000216 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000217 }
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000218 };
219
220 char HexagonCallFrameInformation::ID = 0;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000221
222} // end anonymous namespace
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000223
224bool HexagonCallFrameInformation::runOnMachineFunction(MachineFunction &MF) {
225 auto &HFI = *MF.getSubtarget<HexagonSubtarget>().getFrameLowering();
226 bool NeedCFI = MF.getMMI().hasDebugInfo() ||
Matthias Braunf1caa282017-12-15 22:22:58 +0000227 MF.getFunction().needsUnwindTableEntry();
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000228
229 if (!NeedCFI)
230 return false;
231 HFI.insertCFIInstructions(MF);
232 return true;
233}
234
235INITIALIZE_PASS(HexagonCallFrameInformation, "hexagon-cfi",
236 "Hexagon call frame information", false, false)
237
238FunctionPass *llvm::createHexagonCallFrameInformation() {
239 return new HexagonCallFrameInformation();
240}
241
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000242/// Map a register pair Reg to the subregister that has the greater "number",
243/// i.e. D3 (aka R7:6) will be mapped to R7, etc.
244static unsigned getMax32BitSubRegister(unsigned Reg,
245 const TargetRegisterInfo &TRI,
246 bool hireg = true) {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000247 if (Reg < Hexagon::D0 || Reg > Hexagon::D15)
248 return Reg;
249
250 unsigned RegNo = 0;
251 for (MCSubRegIterator SubRegs(Reg, &TRI); SubRegs.isValid(); ++SubRegs) {
252 if (hireg) {
253 if (*SubRegs > RegNo)
254 RegNo = *SubRegs;
255 } else {
256 if (!RegNo || *SubRegs < RegNo)
257 RegNo = *SubRegs;
258 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000259 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000260 return RegNo;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000261}
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000262
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000263/// Returns the callee saved register with the largest id in the vector.
264static unsigned getMaxCalleeSavedReg(const std::vector<CalleeSavedInfo> &CSI,
265 const TargetRegisterInfo &TRI) {
Benjamin Kramer3e9a5d32016-05-27 11:36:04 +0000266 static_assert(Hexagon::R1 > 0,
267 "Assume physical registers are encoded as positive integers");
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000268 if (CSI.empty())
269 return 0;
270
271 unsigned Max = getMax32BitSubRegister(CSI[0].getReg(), TRI);
272 for (unsigned I = 1, E = CSI.size(); I < E; ++I) {
273 unsigned Reg = getMax32BitSubRegister(CSI[I].getReg(), TRI);
274 if (Reg > Max)
275 Max = Reg;
276 }
277 return Max;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000278}
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000279
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000280/// Checks if the basic block contains any instruction that needs a stack
281/// frame to be already in place.
282static bool needsStackFrame(const MachineBasicBlock &MBB, const BitVector &CSR,
283 const HexagonRegisterInfo &HRI) {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000284 for (auto &I : MBB) {
285 const MachineInstr *MI = &I;
286 if (MI->isCall())
287 return true;
288 unsigned Opc = MI->getOpcode();
289 switch (Opc) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +0000290 case Hexagon::PS_alloca:
291 case Hexagon::PS_aligna:
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000292 return true;
293 default:
294 break;
295 }
296 // Check individual operands.
Matthias Braune41e1462015-05-29 02:56:46 +0000297 for (const MachineOperand &MO : MI->operands()) {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000298 // While the presence of a frame index does not prove that a stack
299 // frame will be required, all frame indexes should be within alloc-
300 // frame/deallocframe. Otherwise, the code that translates a frame
301 // index into an offset would have to be aware of the placement of
302 // the frame creation/destruction instructions.
Matthias Braune41e1462015-05-29 02:56:46 +0000303 if (MO.isFI())
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000304 return true;
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000305 if (MO.isReg()) {
306 unsigned R = MO.getReg();
307 // Virtual registers will need scavenging, which then may require
308 // a stack slot.
309 if (TargetRegisterInfo::isVirtualRegister(R))
Rafael Espindola6eab4042017-02-17 02:08:58 +0000310 return true;
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000311 for (MCSubRegIterator S(R, &HRI, true); S.isValid(); ++S)
312 if (CSR[*S])
313 return true;
314 continue;
315 }
316 if (MO.isRegMask()) {
317 // A regmask would normally have all callee-saved registers marked
318 // as preserved, so this check would not be needed, but in case of
319 // ever having other regmasks (for other calling conventions),
320 // make sure they would be processed correctly.
321 const uint32_t *BM = MO.getRegMask();
322 for (int x = CSR.find_first(); x >= 0; x = CSR.find_next(x)) {
323 unsigned R = x;
324 // If this regmask does not preserve a CSR, a frame will be needed.
325 if (!(BM[R/32] & (1u << (R%32))))
326 return true;
327 }
328 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000329 }
330 }
331 return false;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000332}
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000333
334 /// Returns true if MBB has a machine instructions that indicates a tail call
335 /// in the block.
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000336static bool hasTailCall(const MachineBasicBlock &MBB) {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000337 MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr();
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000338 if (I == MBB.end())
339 return false;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000340 unsigned RetOpc = I->getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +0000341 return RetOpc == Hexagon::PS_tailcall_i || RetOpc == Hexagon::PS_tailcall_r;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000342}
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000343
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000344/// Returns true if MBB contains an instruction that returns.
345static bool hasReturn(const MachineBasicBlock &MBB) {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000346 for (auto I = MBB.getFirstTerminator(), E = MBB.end(); I != E; ++I)
347 if (I->isReturn())
348 return true;
349 return false;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000350}
Krzysztof Parzyszeka34901a2016-03-28 14:42:03 +0000351
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000352/// Returns the "return" instruction from this block, or nullptr if there
353/// isn't any.
354static MachineInstr *getReturn(MachineBasicBlock &MBB) {
Krzysztof Parzyszekda0b9a92016-05-26 19:44:28 +0000355 for (auto &I : MBB)
356 if (I.isReturn())
357 return &I;
358 return nullptr;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000359}
Krzysztof Parzyszekda0b9a92016-05-26 19:44:28 +0000360
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000361static bool isRestoreCall(unsigned Opc) {
Krzysztof Parzyszekda0b9a92016-05-26 19:44:28 +0000362 switch (Opc) {
363 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
364 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekfae79862016-07-27 18:47:25 +0000365 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_EXT:
366 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_EXT_PIC:
367 case Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT:
368 case Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT_PIC:
Krzysztof Parzyszekda0b9a92016-05-26 19:44:28 +0000369 case Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4:
370 case Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_PIC:
371 return true;
372 }
373 return false;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000374}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000375
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000376static inline bool isOptNone(const MachineFunction &MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000377 return MF.getFunction().hasFnAttribute(Attribute::OptimizeNone) ||
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000378 MF.getTarget().getOptLevel() == CodeGenOpt::None;
379}
380
381static inline bool isOptSize(const MachineFunction &MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000382 const Function &F = MF.getFunction();
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000383 return F.optForSize() && !F.optForMinSize();
384}
385
386static inline bool isMinSize(const MachineFunction &MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000387 return MF.getFunction().optForMinSize();
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000388}
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000389
390/// Implements shrink-wrapping of the stack frame. By default, stack frame
391/// is created in the function entry block, and is cleaned up in every block
392/// that returns. This function finds alternate blocks: one for the frame
393/// setup (prolog) and one for the cleanup (epilog).
394void HexagonFrameLowering::findShrunkPrologEpilog(MachineFunction &MF,
395 MachineBasicBlock *&PrologB, MachineBasicBlock *&EpilogB) const {
396 static unsigned ShrinkCounter = 0;
397
398 if (ShrinkLimit.getPosition()) {
399 if (ShrinkCounter >= ShrinkLimit)
400 return;
401 ShrinkCounter++;
402 }
403
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000404 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000405
406 MachineDominatorTree MDT;
407 MDT.runOnMachineFunction(MF);
408 MachinePostDominatorTree MPT;
409 MPT.runOnMachineFunction(MF);
410
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000411 using UnsignedMap = DenseMap<unsigned, unsigned>;
412 using RPOTType = ReversePostOrderTraversal<const MachineFunction *>;
413
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000414 UnsignedMap RPO;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000415 RPOTType RPOT(&MF);
416 unsigned RPON = 0;
417 for (RPOTType::rpo_iterator I = RPOT.begin(), E = RPOT.end(); I != E; ++I)
418 RPO[(*I)->getNumber()] = RPON++;
419
420 // Don't process functions that have loops, at least for now. Placement
421 // of prolog and epilog must take loop structure into account. For simpli-
422 // city don't do it right now.
423 for (auto &I : MF) {
424 unsigned BN = RPO[I.getNumber()];
425 for (auto SI = I.succ_begin(), SE = I.succ_end(); SI != SE; ++SI) {
426 // If found a back-edge, return.
427 if (RPO[(*SI)->getNumber()] <= BN)
428 return;
429 }
430 }
431
432 // Collect the set of blocks that need a stack frame to execute. Scan
433 // each block for uses/defs of callee-saved registers, calls, etc.
434 SmallVector<MachineBasicBlock*,16> SFBlocks;
435 BitVector CSR(Hexagon::NUM_TARGET_REGS);
436 for (const MCPhysReg *P = HRI.getCalleeSavedRegs(&MF); *P; ++P)
Krzysztof Parzyszek01598de2016-03-24 20:31:41 +0000437 for (MCSubRegIterator S(*P, &HRI, true); S.isValid(); ++S)
438 CSR[*S] = true;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000439
440 for (auto &I : MF)
Krzysztof Parzyszek01598de2016-03-24 20:31:41 +0000441 if (needsStackFrame(I, CSR, HRI))
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000442 SFBlocks.push_back(&I);
443
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000444 LLVM_DEBUG({
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000445 dbgs() << "Blocks needing SF: {";
446 for (auto &B : SFBlocks)
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000447 dbgs() << " " << printMBBReference(*B);
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000448 dbgs() << " }\n";
449 });
450 // No frame needed?
451 if (SFBlocks.empty())
452 return;
453
454 // Pick a common dominator and a common post-dominator.
455 MachineBasicBlock *DomB = SFBlocks[0];
456 for (unsigned i = 1, n = SFBlocks.size(); i < n; ++i) {
457 DomB = MDT.findNearestCommonDominator(DomB, SFBlocks[i]);
458 if (!DomB)
459 break;
460 }
461 MachineBasicBlock *PDomB = SFBlocks[0];
462 for (unsigned i = 1, n = SFBlocks.size(); i < n; ++i) {
463 PDomB = MPT.findNearestCommonDominator(PDomB, SFBlocks[i]);
464 if (!PDomB)
465 break;
466 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000467 LLVM_DEBUG({
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000468 dbgs() << "Computed dom block: ";
469 if (DomB)
470 dbgs() << printMBBReference(*DomB);
471 else
472 dbgs() << "<null>";
473 dbgs() << ", computed pdom block: ";
474 if (PDomB)
475 dbgs() << printMBBReference(*PDomB);
476 else
477 dbgs() << "<null>";
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000478 dbgs() << "\n";
479 });
480 if (!DomB || !PDomB)
481 return;
482
483 // Make sure that DomB dominates PDomB and PDomB post-dominates DomB.
484 if (!MDT.dominates(DomB, PDomB)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000485 LLVM_DEBUG(dbgs() << "Dom block does not dominate pdom block\n");
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000486 return;
487 }
488 if (!MPT.dominates(PDomB, DomB)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000489 LLVM_DEBUG(dbgs() << "PDom block does not post-dominate dom block\n");
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000490 return;
491 }
492
493 // Finally, everything seems right.
494 PrologB = DomB;
495 EpilogB = PDomB;
496}
497
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000498/// Perform most of the PEI work here:
499/// - saving/restoring of the callee-saved registers,
500/// - stack frame creation and destruction.
501/// Normally, this work is distributed among various functions, but doing it
502/// in one place allows shrink-wrapping of the stack frame.
Quentin Colombet61b305e2015-05-05 17:38:16 +0000503void HexagonFrameLowering::emitPrologue(MachineFunction &MF,
504 MachineBasicBlock &MBB) const {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000505 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000506
Matthias Braun941a7052016-07-28 18:40:00 +0000507 MachineFrameInfo &MFI = MF.getFrameInfo();
508 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000509
510 MachineBasicBlock *PrologB = &MF.front(), *EpilogB = nullptr;
511 if (EnableShrinkWrapping)
512 findShrunkPrologEpilog(MF, PrologB, EpilogB);
513
Krzysztof Parzyszekc9d4caa2016-03-24 20:20:07 +0000514 bool PrologueStubs = false;
515 insertCSRSpillsInBlock(*PrologB, CSI, HRI, PrologueStubs);
516 insertPrologueInBlock(*PrologB, PrologueStubs);
Krzysztof Parzyszeka34d6392016-07-27 16:26:39 +0000517 updateEntryPaths(MF, *PrologB);
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000518
519 if (EpilogB) {
520 insertCSRRestoresInBlock(*EpilogB, CSI, HRI);
521 insertEpilogueInBlock(*EpilogB);
522 } else {
523 for (auto &B : MF)
Matthias Braunc2d4bef2015-09-25 21:25:19 +0000524 if (B.isReturnBlock())
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000525 insertCSRRestoresInBlock(B, CSI, HRI);
526
527 for (auto &B : MF)
Matthias Braunc2d4bef2015-09-25 21:25:19 +0000528 if (B.isReturnBlock())
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000529 insertEpilogueInBlock(B);
Krzysztof Parzyszekda0b9a92016-05-26 19:44:28 +0000530
531 for (auto &B : MF) {
532 if (B.empty())
533 continue;
534 MachineInstr *RetI = getReturn(B);
535 if (!RetI || isRestoreCall(RetI->getOpcode()))
536 continue;
537 for (auto &R : CSI)
538 RetI->addOperand(MachineOperand::CreateReg(R.getReg(), false, true));
539 }
540 }
541
542 if (EpilogB) {
543 // If there is an epilog block, it may not have a return instruction.
544 // In such case, we need to add the callee-saved registers as live-ins
545 // in all blocks on all paths from the epilog to any return block.
Krzysztof Parzyszeka34d6392016-07-27 16:26:39 +0000546 unsigned MaxBN = MF.getNumBlockIDs();
Krzysztof Parzyszekda0b9a92016-05-26 19:44:28 +0000547 BitVector DoneT(MaxBN+1), DoneF(MaxBN+1), Path(MaxBN+1);
Krzysztof Parzyszeka34d6392016-07-27 16:26:39 +0000548 updateExitPaths(*EpilogB, *EpilogB, DoneT, DoneF, Path);
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000549 }
550}
551
Brendon Cahoonac8fed62018-11-09 18:16:24 +0000552/// Returns true if the target can safely skip saving callee-saved registers
553/// for noreturn nounwind functions.
554bool HexagonFrameLowering::enableCalleeSaveSkip(
555 const MachineFunction &MF) const {
556 const auto &F = MF.getFunction();
557 assert(F.hasFnAttribute(Attribute::NoReturn) &&
558 F.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
559 !F.getFunction().hasFnAttribute(Attribute::UWTable));
Fangrui Song60b7fb42018-11-09 19:24:48 +0000560 (void)F;
Brendon Cahoonac8fed62018-11-09 18:16:24 +0000561
562 // No need to save callee saved registers if the function does not return.
563 return MF.getSubtarget<HexagonSubtarget>().noreturnStackElim();
564}
565
566// Helper function used to determine when to eliminate the stack frame for
567// functions marked as noreturn and when the noreturn-stack-elim options are
568// specified. When both these conditions are true, then a FP may not be needed
569// if the function makes a call. It is very similar to enableCalleeSaveSkip,
570// but it used to check if the allocframe can be eliminated as well.
571static bool enableAllocFrameElim(const MachineFunction &MF) {
572 const auto &F = MF.getFunction();
573 const auto &MFI = MF.getFrameInfo();
574 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
575 assert(!MFI.hasVarSizedObjects() &&
576 !HST.getRegisterInfo()->needsStackRealignment(MF));
577 return F.hasFnAttribute(Attribute::NoReturn) &&
578 F.hasFnAttribute(Attribute::NoUnwind) &&
579 !F.hasFnAttribute(Attribute::UWTable) && HST.noreturnStackElim() &&
580 MFI.getStackSize() == 0;
581}
582
Krzysztof Parzyszekc9d4caa2016-03-24 20:20:07 +0000583void HexagonFrameLowering::insertPrologueInBlock(MachineBasicBlock &MBB,
584 bool PrologueStubs) const {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000585 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000586 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000587 auto &HST = MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000588 auto &HII = *HST.getInstrInfo();
589 auto &HRI = *HST.getRegisterInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000590
Krzysztof Parzyszek71702172017-06-23 19:47:04 +0000591 unsigned MaxAlign = std::max(MFI.getMaxAlignment(), getStackAlignment());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000592
593 // Calculate the total stack frame size.
594 // Get the number of bytes to allocate from the FrameInfo.
Matthias Braun941a7052016-07-28 18:40:00 +0000595 unsigned FrameSize = MFI.getStackSize();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000596 // Round up the max call frame size to the max alignment on the stack.
Matthias Braun941a7052016-07-28 18:40:00 +0000597 unsigned MaxCFA = alignTo(MFI.getMaxCallFrameSize(), MaxAlign);
598 MFI.setMaxCallFrameSize(MaxCFA);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000599
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000600 FrameSize = MaxCFA + alignTo(FrameSize, MaxAlign);
Matthias Braun941a7052016-07-28 18:40:00 +0000601 MFI.setStackSize(FrameSize);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000602
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000603 bool AlignStack = (MaxAlign > getStackAlignment());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000604
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000605 // Get the number of bytes to allocate from the FrameInfo.
Matthias Braun941a7052016-07-28 18:40:00 +0000606 unsigned NumBytes = MFI.getStackSize();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000607 unsigned SP = HRI.getStackRegister();
Matthias Braun941a7052016-07-28 18:40:00 +0000608 unsigned MaxCF = MFI.getMaxCallFrameSize();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000609 MachineBasicBlock::iterator InsertPt = MBB.begin();
610
Krzysztof Parzyszek8849a512016-08-19 18:46:13 +0000611 SmallVector<MachineInstr *, 4> AdjustRegs;
612 for (auto &MBB : MF)
613 for (auto &MI : MBB)
614 if (MI.getOpcode() == Hexagon::PS_alloca)
615 AdjustRegs.push_back(&MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000616
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000617 for (auto MI : AdjustRegs) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +0000618 assert((MI->getOpcode() == Hexagon::PS_alloca) && "Expected alloca");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000619 expandAlloca(MI, HII, SP, MaxCF);
620 MI->eraseFromParent();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000621 }
622
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000623 DebugLoc dl = MBB.findDebugLoc(InsertPt);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000624
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000625 if (hasFP(MF)) {
626 insertAllocframe(MBB, InsertPt, NumBytes);
627 if (AlignStack) {
628 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_andir), SP)
629 .addReg(SP)
630 .addImm(-int64_t(MaxAlign));
631 }
632 // If the stack-checking is enabled, and we spilled the callee-saved
633 // registers inline (i.e. did not use a spill function), then call
634 // the stack checker directly.
635 if (EnableStackOVFSanitizer && !PrologueStubs)
636 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::PS_call_stk))
637 .addExternalSymbol("__runtime_stack_check");
638 } else if (NumBytes > 0) {
639 assert(alignTo(NumBytes, 8) == NumBytes);
640 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_addi), SP)
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000641 .addReg(SP)
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000642 .addImm(-int(NumBytes));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000643 }
644}
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000645
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000646void HexagonFrameLowering::insertEpilogueInBlock(MachineBasicBlock &MBB) const {
647 MachineFunction &MF = *MBB.getParent();
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000648 auto &HST = MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000649 auto &HII = *HST.getInstrInfo();
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000650 auto &HRI = *HST.getRegisterInfo();
651 unsigned SP = HRI.getStackRegister();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000652
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000653 MachineBasicBlock::iterator InsertPt = MBB.getFirstTerminator();
654 DebugLoc dl = MBB.findDebugLoc(InsertPt);
655
656 if (!hasFP(MF)) {
657 MachineFrameInfo &MFI = MF.getFrameInfo();
658 if (unsigned NumBytes = MFI.getStackSize()) {
659 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_addi), SP)
660 .addReg(SP)
661 .addImm(NumBytes);
662 }
663 return;
664 }
665
Krzysztof Parzyszekda0b9a92016-05-26 19:44:28 +0000666 MachineInstr *RetI = getReturn(MBB);
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000667 unsigned RetOpc = RetI ? RetI->getOpcode() : 0;
668
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000669 // Handle EH_RETURN.
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000670 if (RetOpc == Hexagon::EH_RETURN_JMPR) {
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000671 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::L2_deallocframe))
672 .addDef(Hexagon::D15)
673 .addReg(Hexagon::R30);
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000674 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_add), SP)
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000675 .addReg(SP)
676 .addReg(Hexagon::R28);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000677 return;
678 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000679
680 // Check for RESTORE_DEALLOC_RET* tail call. Don't emit an extra dealloc-
681 // frame instruction if we encounter it.
Krzysztof Parzyszek181fdbd2016-03-24 19:18:48 +0000682 if (RetOpc == Hexagon::RESTORE_DEALLOC_RET_JMP_V4 ||
Krzysztof Parzyszekfae79862016-07-27 18:47:25 +0000683 RetOpc == Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC ||
684 RetOpc == Hexagon::RESTORE_DEALLOC_RET_JMP_V4_EXT ||
685 RetOpc == Hexagon::RESTORE_DEALLOC_RET_JMP_V4_EXT_PIC) {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000686 MachineBasicBlock::iterator It = RetI;
687 ++It;
688 // Delete all instructions after the RESTORE (except labels).
689 while (It != MBB.end()) {
690 if (!It->isLabel())
691 It = MBB.erase(It);
692 else
693 ++It;
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000694 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000695 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000696 }
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000697
698 // It is possible that the restoring code is a call to a library function.
699 // All of the restore* functions include "deallocframe", so we need to make
700 // sure that we don't add an extra one.
701 bool NeedsDeallocframe = true;
702 if (!MBB.empty() && InsertPt != MBB.begin()) {
703 MachineBasicBlock::iterator PrevIt = std::prev(InsertPt);
704 unsigned COpc = PrevIt->getOpcode();
Krzysztof Parzyszek181fdbd2016-03-24 19:18:48 +0000705 if (COpc == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4 ||
Krzysztof Parzyszekfae79862016-07-27 18:47:25 +0000706 COpc == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_PIC ||
707 COpc == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT ||
708 COpc == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT_PIC ||
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +0000709 COpc == Hexagon::PS_call_nr || COpc == Hexagon::PS_callr_nr)
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000710 NeedsDeallocframe = false;
711 }
712
713 if (!NeedsDeallocframe)
714 return;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +0000715 // If the returning instruction is PS_jmpret, replace it with dealloc_return,
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000716 // otherwise just add deallocframe. The function could be returning via a
717 // tail call.
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +0000718 if (RetOpc != Hexagon::PS_jmpret || DisableDeallocRet) {
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000719 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::L2_deallocframe))
720 .addDef(Hexagon::D15)
721 .addReg(Hexagon::R30);
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000722 return;
723 }
724 unsigned NewOpc = Hexagon::L4_return;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000725 MachineInstr *NewI = BuildMI(MBB, RetI, dl, HII.get(NewOpc))
726 .addDef(Hexagon::D15)
727 .addReg(Hexagon::R30);
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000728 // Transfer the function live-out registers.
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000729 NewI->copyImplicitOps(MF, *RetI);
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000730 MBB.erase(RetI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000731}
732
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000733void HexagonFrameLowering::insertAllocframe(MachineBasicBlock &MBB,
734 MachineBasicBlock::iterator InsertPt, unsigned NumBytes) const {
735 MachineFunction &MF = *MBB.getParent();
736 auto &HST = MF.getSubtarget<HexagonSubtarget>();
737 auto &HII = *HST.getInstrInfo();
738 auto &HRI = *HST.getRegisterInfo();
739
740 // Check for overflow.
741 // Hexagon_TODO: Ugh! hardcoding. Is there an API that can be used?
742 const unsigned int ALLOCFRAME_MAX = 16384;
743
744 // Create a dummy memory operand to avoid allocframe from being treated as
745 // a volatile memory reference.
746 auto *MMO = MF.getMachineMemOperand(MachinePointerInfo::getStack(MF, 0),
747 MachineMemOperand::MOStore, 4, 4);
748
749 DebugLoc dl = MBB.findDebugLoc(InsertPt);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000750 unsigned SP = HRI.getStackRegister();
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000751
752 if (NumBytes >= ALLOCFRAME_MAX) {
753 // Emit allocframe(#0).
754 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::S2_allocframe))
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000755 .addDef(SP)
756 .addReg(SP)
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000757 .addImm(0)
758 .addMemOperand(MMO);
759
760 // Subtract the size from the stack pointer.
761 unsigned SP = HRI.getStackRegister();
762 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_addi), SP)
763 .addReg(SP)
764 .addImm(-int(NumBytes));
765 } else {
766 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::S2_allocframe))
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000767 .addDef(SP)
768 .addReg(SP)
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000769 .addImm(NumBytes)
770 .addMemOperand(MMO);
771 }
772}
773
Krzysztof Parzyszeka34d6392016-07-27 16:26:39 +0000774void HexagonFrameLowering::updateEntryPaths(MachineFunction &MF,
775 MachineBasicBlock &SaveB) const {
776 SetVector<unsigned> Worklist;
777
778 MachineBasicBlock &EntryB = MF.front();
779 Worklist.insert(EntryB.getNumber());
780
781 unsigned SaveN = SaveB.getNumber();
Matthias Braun941a7052016-07-28 18:40:00 +0000782 auto &CSI = MF.getFrameInfo().getCalleeSavedInfo();
Krzysztof Parzyszeka34d6392016-07-27 16:26:39 +0000783
784 for (unsigned i = 0; i < Worklist.size(); ++i) {
785 unsigned BN = Worklist[i];
786 MachineBasicBlock &MBB = *MF.getBlockNumbered(BN);
787 for (auto &R : CSI)
788 if (!MBB.isLiveIn(R.getReg()))
789 MBB.addLiveIn(R.getReg());
790 if (BN != SaveN)
791 for (auto &SB : MBB.successors())
792 Worklist.insert(SB->getNumber());
793 }
794}
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000795
Krzysztof Parzyszekda0b9a92016-05-26 19:44:28 +0000796bool HexagonFrameLowering::updateExitPaths(MachineBasicBlock &MBB,
Krzysztof Parzyszeka34d6392016-07-27 16:26:39 +0000797 MachineBasicBlock &RestoreB, BitVector &DoneT, BitVector &DoneF,
Krzysztof Parzyszekda0b9a92016-05-26 19:44:28 +0000798 BitVector &Path) const {
799 assert(MBB.getNumber() >= 0);
800 unsigned BN = MBB.getNumber();
801 if (Path[BN] || DoneF[BN])
802 return false;
803 if (DoneT[BN])
804 return true;
805
Matthias Braun941a7052016-07-28 18:40:00 +0000806 auto &CSI = MBB.getParent()->getFrameInfo().getCalleeSavedInfo();
Krzysztof Parzyszekda0b9a92016-05-26 19:44:28 +0000807
808 Path[BN] = true;
809 bool ReachedExit = false;
810 for (auto &SB : MBB.successors())
811 ReachedExit |= updateExitPaths(*SB, RestoreB, DoneT, DoneF, Path);
812
813 if (!MBB.empty() && MBB.back().isReturn()) {
814 // Add implicit uses of all callee-saved registers to the reached
815 // return instructions. This is to prevent the anti-dependency breaker
816 // from renaming these registers.
817 MachineInstr &RetI = MBB.back();
818 if (!isRestoreCall(RetI.getOpcode()))
819 for (auto &R : CSI)
820 RetI.addOperand(MachineOperand::CreateReg(R.getReg(), false, true));
821 ReachedExit = true;
822 }
823
824 // We don't want to add unnecessary live-ins to the restore block: since
825 // the callee-saved registers are being defined in it, the entry of the
826 // restore block cannot be on the path from the definitions to any exit.
Krzysztof Parzyszeka34d6392016-07-27 16:26:39 +0000827 if (ReachedExit && &MBB != &RestoreB) {
Krzysztof Parzyszekda0b9a92016-05-26 19:44:28 +0000828 for (auto &R : CSI)
829 if (!MBB.isLiveIn(R.getReg()))
830 MBB.addLiveIn(R.getReg());
831 DoneT[BN] = true;
832 }
833 if (!ReachedExit)
834 DoneF[BN] = true;
835
836 Path[BN] = false;
837 return ReachedExit;
838}
839
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000840static Optional<MachineBasicBlock::iterator>
841findCFILocation(MachineBasicBlock &B) {
Krzysztof Parzyszekc43644d2016-07-28 19:13:46 +0000842 // The CFI instructions need to be inserted right after allocframe.
843 // An exception to this is a situation where allocframe is bundled
844 // with a call: then the CFI instructions need to be inserted before
845 // the packet with the allocframe+call (in case the call throws an
846 // exception).
847 auto End = B.instr_end();
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000848
Krzysztof Parzyszekc43644d2016-07-28 19:13:46 +0000849 for (MachineInstr &I : B) {
850 MachineBasicBlock::iterator It = I.getIterator();
851 if (!I.isBundle()) {
852 if (I.getOpcode() == Hexagon::S2_allocframe)
853 return std::next(It);
854 continue;
855 }
856 // I is a bundle.
857 bool HasCall = false, HasAllocFrame = false;
858 auto T = It.getInstrIterator();
859 while (++T != End && T->isBundled()) {
860 if (T->getOpcode() == Hexagon::S2_allocframe)
861 HasAllocFrame = true;
862 else if (T->isCall())
863 HasCall = true;
864 }
865 if (HasAllocFrame)
866 return HasCall ? It : std::next(It);
867 }
868 return None;
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000869}
870
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000871void HexagonFrameLowering::insertCFIInstructions(MachineFunction &MF) const {
872 for (auto &B : MF) {
Krzysztof Parzyszekc43644d2016-07-28 19:13:46 +0000873 auto At = findCFILocation(B);
874 if (At.hasValue())
875 insertCFIInstructionsAt(B, At.getValue());
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000876 }
877}
878
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000879void HexagonFrameLowering::insertCFIInstructionsAt(MachineBasicBlock &MBB,
880 MachineBasicBlock::iterator At) const {
881 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000882 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000883 MachineModuleInfo &MMI = MF.getMMI();
884 auto &HST = MF.getSubtarget<HexagonSubtarget>();
885 auto &HII = *HST.getInstrInfo();
886 auto &HRI = *HST.getRegisterInfo();
887
888 // If CFI instructions have debug information attached, something goes
889 // wrong with the final assembly generation: the prolog_end is placed
890 // in a wrong location.
891 DebugLoc DL;
892 const MCInstrDesc &CFID = HII.get(TargetOpcode::CFI_INSTRUCTION);
893
894 MCSymbol *FrameLabel = MMI.getContext().createTempSymbol();
Krzysztof Parzyszekc2c78682016-05-11 14:53:07 +0000895 bool HasFP = hasFP(MF);
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000896
Krzysztof Parzyszekc2c78682016-05-11 14:53:07 +0000897 if (HasFP) {
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000898 unsigned DwFPReg = HRI.getDwarfRegNum(HRI.getFrameRegister(), true);
899 unsigned DwRAReg = HRI.getDwarfRegNum(HRI.getRARegister(), true);
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000900
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000901 // Define CFA via an offset from the value of FP.
902 //
903 // -8 -4 0 (SP)
904 // --+----+----+---------------------
905 // | FP | LR | increasing addresses -->
906 // --+----+----+---------------------
907 // | +-- Old SP (before allocframe)
908 // +-- New FP (after allocframe)
909 //
910 // MCCFIInstruction::createDefCfa subtracts the offset from the register.
911 // MCCFIInstruction::createOffset takes the offset without sign change.
912 auto DefCfa = MCCFIInstruction::createDefCfa(FrameLabel, DwFPReg, -8);
913 BuildMI(MBB, At, DL, CFID)
Matthias Braunf23ef432016-11-30 23:48:42 +0000914 .addCFIIndex(MF.addFrameInst(DefCfa));
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000915 // R31 (return addr) = CFA - 4
916 auto OffR31 = MCCFIInstruction::createOffset(FrameLabel, DwRAReg, -4);
917 BuildMI(MBB, At, DL, CFID)
Matthias Braunf23ef432016-11-30 23:48:42 +0000918 .addCFIIndex(MF.addFrameInst(OffR31));
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000919 // R30 (frame ptr) = CFA - 8
920 auto OffR30 = MCCFIInstruction::createOffset(FrameLabel, DwFPReg, -8);
921 BuildMI(MBB, At, DL, CFID)
Matthias Braunf23ef432016-11-30 23:48:42 +0000922 .addCFIIndex(MF.addFrameInst(OffR30));
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000923 }
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000924
925 static unsigned int RegsToMove[] = {
926 Hexagon::R1, Hexagon::R0, Hexagon::R3, Hexagon::R2,
927 Hexagon::R17, Hexagon::R16, Hexagon::R19, Hexagon::R18,
928 Hexagon::R21, Hexagon::R20, Hexagon::R23, Hexagon::R22,
929 Hexagon::R25, Hexagon::R24, Hexagon::R27, Hexagon::R26,
930 Hexagon::D0, Hexagon::D1, Hexagon::D8, Hexagon::D9,
931 Hexagon::D10, Hexagon::D11, Hexagon::D12, Hexagon::D13,
932 Hexagon::NoRegister
933 };
934
Krzysztof Parzyszekc2c78682016-05-11 14:53:07 +0000935 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000936
937 for (unsigned i = 0; RegsToMove[i] != Hexagon::NoRegister; ++i) {
938 unsigned Reg = RegsToMove[i];
939 auto IfR = [Reg] (const CalleeSavedInfo &C) -> bool {
940 return C.getReg() == Reg;
941 };
David Majnemer562e8292016-08-12 00:18:03 +0000942 auto F = find_if(CSI, IfR);
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000943 if (F == CSI.end())
944 continue;
945
Krzysztof Parzyszekc2c78682016-05-11 14:53:07 +0000946 int64_t Offset;
947 if (HasFP) {
948 // If the function has a frame pointer (i.e. has an allocframe),
949 // then the CFA has been defined in terms of FP. Any offsets in
950 // the following CFI instructions have to be defined relative
951 // to FP, which points to the bottom of the stack frame.
952 // The function getFrameIndexReference can still choose to use SP
953 // for the offset calculation, so we cannot simply call it here.
954 // Instead, get the offset (relative to the FP) directly.
955 Offset = MFI.getObjectOffset(F->getFrameIdx());
956 } else {
957 unsigned FrameReg;
958 Offset = getFrameIndexReference(MF, F->getFrameIdx(), FrameReg);
959 }
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000960 // Subtract 8 to make room for R30 and R31, which are added above.
Krzysztof Parzyszekc2c78682016-05-11 14:53:07 +0000961 Offset -= 8;
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000962
963 if (Reg < Hexagon::D0 || Reg > Hexagon::D15) {
964 unsigned DwarfReg = HRI.getDwarfRegNum(Reg, true);
965 auto OffReg = MCCFIInstruction::createOffset(FrameLabel, DwarfReg,
966 Offset);
967 BuildMI(MBB, At, DL, CFID)
Matthias Braunf23ef432016-11-30 23:48:42 +0000968 .addCFIIndex(MF.addFrameInst(OffReg));
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000969 } else {
970 // Split the double regs into subregs, and generate appropriate
971 // cfi_offsets.
972 // The only reason, we are split double regs is, llvm-mc does not
973 // understand paired registers for cfi_offset.
974 // Eg .cfi_offset r1:0, -64
975
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000976 unsigned HiReg = HRI.getSubReg(Reg, Hexagon::isub_hi);
977 unsigned LoReg = HRI.getSubReg(Reg, Hexagon::isub_lo);
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000978 unsigned HiDwarfReg = HRI.getDwarfRegNum(HiReg, true);
979 unsigned LoDwarfReg = HRI.getDwarfRegNum(LoReg, true);
980 auto OffHi = MCCFIInstruction::createOffset(FrameLabel, HiDwarfReg,
981 Offset+4);
982 BuildMI(MBB, At, DL, CFID)
Matthias Braunf23ef432016-11-30 23:48:42 +0000983 .addCFIIndex(MF.addFrameInst(OffHi));
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000984 auto OffLo = MCCFIInstruction::createOffset(FrameLabel, LoDwarfReg,
985 Offset);
986 BuildMI(MBB, At, DL, CFID)
Matthias Braunf23ef432016-11-30 23:48:42 +0000987 .addCFIIndex(MF.addFrameInst(OffLo));
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000988 }
989 }
990}
991
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000992bool HexagonFrameLowering::hasFP(const MachineFunction &MF) const {
Matthias Braunf1caa282017-12-15 22:22:58 +0000993 if (MF.getFunction().hasFnAttribute(Attribute::Naked))
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000994 return false;
995
Matthias Braun941a7052016-07-28 18:40:00 +0000996 auto &MFI = MF.getFrameInfo();
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000997 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000998 bool HasExtraAlign = HRI.needsStackRealignment(MF);
999 bool HasAlloca = MFI.hasVarSizedObjects();
1000
1001 // Insert ALLOCFRAME if we need to or at -O0 for the debugger. Think
1002 // that this shouldn't be required, but doing so now because gcc does and
1003 // gdb can't break at the start of the function without it. Will remove if
1004 // this turns out to be a gdb bug.
1005 //
1006 if (MF.getTarget().getOptLevel() == CodeGenOpt::None)
1007 return true;
1008
1009 // By default we want to use SP (since it's always there). FP requires
1010 // some setup (i.e. ALLOCFRAME).
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001011 // Both, alloca and stack alignment modify the stack pointer by an
1012 // undetermined value, so we need to save it at the entry to the function
1013 // (i.e. use allocframe).
1014 if (HasAlloca || HasExtraAlign)
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001015 return true;
1016
1017 if (MFI.getStackSize() > 0) {
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001018 // If FP-elimination is disabled, we have to use FP at this point.
1019 const TargetMachine &TM = MF.getTarget();
1020 if (TM.Options.DisableFramePointerElim(MF) || !EliminateFramePointer)
1021 return true;
1022 if (EnableStackOVFSanitizer)
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001023 return true;
1024 }
1025
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001026 const auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
Brendon Cahoonac8fed62018-11-09 18:16:24 +00001027 if ((MFI.hasCalls() && !enableAllocFrameElim(MF)) || HMFI.hasClobberLR())
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001028 return true;
1029
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001030 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001031}
1032
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001033enum SpillKind {
1034 SK_ToMem,
1035 SK_FromMem,
1036 SK_FromMemTailcall
1037};
1038
Krzysztof Parzyszekc9d4caa2016-03-24 20:20:07 +00001039static const char *getSpillFunctionFor(unsigned MaxReg, SpillKind SpillType,
1040 bool Stkchk = false) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001041 const char * V4SpillToMemoryFunctions[] = {
1042 "__save_r16_through_r17",
1043 "__save_r16_through_r19",
1044 "__save_r16_through_r21",
1045 "__save_r16_through_r23",
1046 "__save_r16_through_r25",
1047 "__save_r16_through_r27" };
1048
Krzysztof Parzyszekc9d4caa2016-03-24 20:20:07 +00001049 const char * V4SpillToMemoryStkchkFunctions[] = {
1050 "__save_r16_through_r17_stkchk",
1051 "__save_r16_through_r19_stkchk",
1052 "__save_r16_through_r21_stkchk",
1053 "__save_r16_through_r23_stkchk",
1054 "__save_r16_through_r25_stkchk",
1055 "__save_r16_through_r27_stkchk" };
1056
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001057 const char * V4SpillFromMemoryFunctions[] = {
1058 "__restore_r16_through_r17_and_deallocframe",
1059 "__restore_r16_through_r19_and_deallocframe",
1060 "__restore_r16_through_r21_and_deallocframe",
1061 "__restore_r16_through_r23_and_deallocframe",
1062 "__restore_r16_through_r25_and_deallocframe",
1063 "__restore_r16_through_r27_and_deallocframe" };
1064
1065 const char * V4SpillFromMemoryTailcallFunctions[] = {
1066 "__restore_r16_through_r17_and_deallocframe_before_tailcall",
1067 "__restore_r16_through_r19_and_deallocframe_before_tailcall",
1068 "__restore_r16_through_r21_and_deallocframe_before_tailcall",
1069 "__restore_r16_through_r23_and_deallocframe_before_tailcall",
1070 "__restore_r16_through_r25_and_deallocframe_before_tailcall",
1071 "__restore_r16_through_r27_and_deallocframe_before_tailcall"
1072 };
1073
1074 const char **SpillFunc = nullptr;
1075
1076 switch(SpillType) {
1077 case SK_ToMem:
Krzysztof Parzyszekc9d4caa2016-03-24 20:20:07 +00001078 SpillFunc = Stkchk ? V4SpillToMemoryStkchkFunctions
1079 : V4SpillToMemoryFunctions;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001080 break;
1081 case SK_FromMem:
1082 SpillFunc = V4SpillFromMemoryFunctions;
1083 break;
1084 case SK_FromMemTailcall:
1085 SpillFunc = V4SpillFromMemoryTailcallFunctions;
1086 break;
1087 }
1088 assert(SpillFunc && "Unknown spill kind");
1089
1090 // Spill all callee-saved registers up to the highest register used.
1091 switch (MaxReg) {
1092 case Hexagon::R17:
1093 return SpillFunc[0];
1094 case Hexagon::R19:
1095 return SpillFunc[1];
1096 case Hexagon::R21:
1097 return SpillFunc[2];
1098 case Hexagon::R23:
1099 return SpillFunc[3];
1100 case Hexagon::R25:
1101 return SpillFunc[4];
1102 case Hexagon::R27:
1103 return SpillFunc[5];
1104 default:
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001105 llvm_unreachable("Unhandled maximum callee save register");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001106 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +00001107 return nullptr;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001108}
1109
James Y Knight5567baf2015-08-15 02:32:35 +00001110int HexagonFrameLowering::getFrameIndexReference(const MachineFunction &MF,
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001111 int FI, unsigned &FrameReg) const {
Matthias Braun941a7052016-07-28 18:40:00 +00001112 auto &MFI = MF.getFrameInfo();
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001113 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001114
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001115 int Offset = MFI.getObjectOffset(FI);
1116 bool HasAlloca = MFI.hasVarSizedObjects();
1117 bool HasExtraAlign = HRI.needsStackRealignment(MF);
1118 bool NoOpt = MF.getTarget().getOptLevel() == CodeGenOpt::None;
James Y Knight5567baf2015-08-15 02:32:35 +00001119
Krzysztof Parzyszeka34901a2016-03-28 14:42:03 +00001120 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001121 unsigned FrameSize = MFI.getStackSize();
1122 unsigned SP = HRI.getStackRegister();
1123 unsigned FP = HRI.getFrameRegister();
Krzysztof Parzyszeka34901a2016-03-28 14:42:03 +00001124 unsigned AP = HMFI.getStackAlignBasePhysReg();
Krzysztof Parzyszek918e6d72017-06-26 14:17:58 +00001125 // It may happen that AP will be absent even HasAlloca && HasExtraAlign
1126 // is true. HasExtraAlign may be set because of vector spills, without
1127 // aligned locals or aligned outgoing function arguments. Since vector
1128 // spills will ultimately be "unaligned", it is safe to use FP as the
1129 // base register.
1130 // In fact, in such a scenario the stack is actually not required to be
1131 // aligned, although it may end up being aligned anyway, since this
1132 // particular case is not easily detectable. The alignment will be
1133 // unnecessary, but not incorrect.
1134 // Unfortunately there is no quick way to verify that the above is
1135 // indeed the case (and that it's not a result of an error), so just
1136 // assume that missing AP will be replaced by FP.
1137 // (A better fix would be to rematerialize AP from FP and always align
1138 // vector spills.)
1139 if (AP == 0)
1140 AP = FP;
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001141
1142 bool UseFP = false, UseAP = false; // Default: use SP (except at -O0).
1143 // Use FP at -O0, except when there are objects with extra alignment.
1144 // That additional alignment requirement may cause a pad to be inserted,
1145 // which will make it impossible to use FP to access objects located
1146 // past the pad.
1147 if (NoOpt && !HasExtraAlign)
1148 UseFP = true;
1149 if (MFI.isFixedObjectIndex(FI) || MFI.isObjectPreAllocated(FI)) {
1150 // Fixed and preallocated objects will be located before any padding
1151 // so FP must be used to access them.
1152 UseFP |= (HasAlloca || HasExtraAlign);
1153 } else {
1154 if (HasAlloca) {
1155 if (HasExtraAlign)
1156 UseAP = true;
1157 else
1158 UseFP = true;
1159 }
1160 }
1161
1162 // If FP was picked, then there had better be FP.
1163 bool HasFP = hasFP(MF);
1164 assert((HasFP || !UseFP) && "This function must have frame pointer");
1165
1166 // Having FP implies allocframe. Allocframe will store extra 8 bytes:
1167 // FP/LR. If the base register is used to access an object across these
1168 // 8 bytes, then the offset will need to be adjusted by 8.
1169 //
1170 // After allocframe:
1171 // HexagonISelLowering adds 8 to ---+
1172 // the offsets of all stack-based |
1173 // arguments (*) |
1174 // |
1175 // getObjectOffset < 0 0 8 getObjectOffset >= 8
1176 // ------------------------+-----+------------------------> increasing
1177 // <local objects> |FP/LR| <input arguments> addresses
1178 // -----------------+------+-----+------------------------>
1179 // | |
1180 // SP/AP point --+ +-- FP points here (**)
1181 // somewhere on
1182 // this side of FP/LR
1183 //
1184 // (*) See LowerFormalArguments. The FP/LR is assumed to be present.
1185 // (**) *FP == old-FP. FP+0..7 are the bytes of FP/LR.
1186
1187 // The lowering assumes that FP/LR is present, and so the offsets of
1188 // the formal arguments start at 8. If FP/LR is not there we need to
1189 // reduce the offset by 8.
1190 if (Offset > 0 && !HasFP)
1191 Offset -= 8;
1192
1193 if (UseFP)
1194 FrameReg = FP;
1195 else if (UseAP)
1196 FrameReg = AP;
1197 else
1198 FrameReg = SP;
1199
1200 // Calculate the actual offset in the instruction. If there is no FP
1201 // (in other words, no allocframe), then SP will not be adjusted (i.e.
1202 // there will be no SP -= FrameSize), so the frame size should not be
1203 // added to the calculated offset.
1204 int RealOffset = Offset;
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001205 if (!UseFP && !UseAP)
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001206 RealOffset = FrameSize+Offset;
1207 return RealOffset;
Jakob Stoklund Olesen0b97dbc2012-05-30 22:40:03 +00001208}
1209
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001210bool HexagonFrameLowering::insertCSRSpillsInBlock(MachineBasicBlock &MBB,
Krzysztof Parzyszekc9d4caa2016-03-24 20:20:07 +00001211 const CSIVect &CSI, const HexagonRegisterInfo &HRI,
1212 bool &PrologueStubs) const {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001213 if (CSI.empty())
1214 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001215
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001216 MachineBasicBlock::iterator MI = MBB.begin();
Krzysztof Parzyszekc9d4caa2016-03-24 20:20:07 +00001217 PrologueStubs = false;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001218 MachineFunction &MF = *MBB.getParent();
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +00001219 auto &HST = MF.getSubtarget<HexagonSubtarget>();
1220 auto &HII = *HST.getInstrInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001221
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001222 if (useSpillFunction(MF, CSI)) {
Krzysztof Parzyszekc9d4caa2016-03-24 20:20:07 +00001223 PrologueStubs = true;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001224 unsigned MaxReg = getMaxCalleeSavedReg(CSI, HRI);
Krzysztof Parzyszekc9d4caa2016-03-24 20:20:07 +00001225 bool StkOvrFlowEnabled = EnableStackOVFSanitizer;
1226 const char *SpillFun = getSpillFunctionFor(MaxReg, SK_ToMem,
1227 StkOvrFlowEnabled);
Krzysztof Parzyszek181fdbd2016-03-24 19:18:48 +00001228 auto &HTM = static_cast<const HexagonTargetMachine&>(MF.getTarget());
Rafael Espindolab1556c42016-06-28 20:13:36 +00001229 bool IsPIC = HTM.isPositionIndependent();
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +00001230 bool LongCalls = HST.useLongCalls() || EnableSaveRestoreLong;
Krzysztof Parzyszek181fdbd2016-03-24 19:18:48 +00001231
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001232 // Call spill function.
1233 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
Krzysztof Parzyszekc9d4caa2016-03-24 20:20:07 +00001234 unsigned SpillOpc;
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +00001235 if (StkOvrFlowEnabled) {
1236 if (LongCalls)
1237 SpillOpc = IsPIC ? Hexagon::SAVE_REGISTERS_CALL_V4STK_EXT_PIC
1238 : Hexagon::SAVE_REGISTERS_CALL_V4STK_EXT;
1239 else
1240 SpillOpc = IsPIC ? Hexagon::SAVE_REGISTERS_CALL_V4STK_PIC
1241 : Hexagon::SAVE_REGISTERS_CALL_V4STK;
1242 } else {
1243 if (LongCalls)
1244 SpillOpc = IsPIC ? Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC
1245 : Hexagon::SAVE_REGISTERS_CALL_V4_EXT;
1246 else
1247 SpillOpc = IsPIC ? Hexagon::SAVE_REGISTERS_CALL_V4_PIC
1248 : Hexagon::SAVE_REGISTERS_CALL_V4;
1249 }
Krzysztof Parzyszek181fdbd2016-03-24 19:18:48 +00001250
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001251 MachineInstr *SaveRegsCall =
Krzysztof Parzyszek181fdbd2016-03-24 19:18:48 +00001252 BuildMI(MBB, MI, DL, HII.get(SpillOpc))
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001253 .addExternalSymbol(SpillFun);
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +00001254
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001255 // Add callee-saved registers as use.
Krzysztof Parzyszeke8e754d2016-04-25 17:49:44 +00001256 addCalleeSaveRegistersAsImpOperand(SaveRegsCall, CSI, false, true);
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001257 // Add live in registers.
1258 for (unsigned I = 0; I < CSI.size(); ++I)
1259 MBB.addLiveIn(CSI[I].getReg());
1260 return true;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001261 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001262
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001263 for (unsigned i = 0, n = CSI.size(); i < n; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001264 unsigned Reg = CSI[i].getReg();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001265 // Add live in registers. We treat eh_return callee saved register r0 - r3
1266 // specially. They are not really callee saved registers as they are not
1267 // supposed to be killed.
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001268 bool IsKill = !HRI.isEHReturnCalleeSaveReg(Reg);
1269 int FI = CSI[i].getFrameIdx();
1270 const TargetRegisterClass *RC = HRI.getMinimalPhysRegClass(Reg);
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +00001271 HII.storeRegToStackSlot(MBB, MI, Reg, IsKill, FI, RC, &HRI);
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001272 if (IsKill)
1273 MBB.addLiveIn(Reg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001274 }
1275 return true;
1276}
1277
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001278bool HexagonFrameLowering::insertCSRRestoresInBlock(MachineBasicBlock &MBB,
1279 const CSIVect &CSI, const HexagonRegisterInfo &HRI) const {
1280 if (CSI.empty())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001281 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001282
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001283 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
1284 MachineFunction &MF = *MBB.getParent();
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +00001285 auto &HST = MF.getSubtarget<HexagonSubtarget>();
1286 auto &HII = *HST.getInstrInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001287
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001288 if (useRestoreFunction(MF, CSI)) {
1289 bool HasTC = hasTailCall(MBB) || !hasReturn(MBB);
1290 unsigned MaxR = getMaxCalleeSavedReg(CSI, HRI);
1291 SpillKind Kind = HasTC ? SK_FromMemTailcall : SK_FromMem;
1292 const char *RestoreFn = getSpillFunctionFor(MaxR, Kind);
Krzysztof Parzyszek181fdbd2016-03-24 19:18:48 +00001293 auto &HTM = static_cast<const HexagonTargetMachine&>(MF.getTarget());
Rafael Espindolab1556c42016-06-28 20:13:36 +00001294 bool IsPIC = HTM.isPositionIndependent();
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +00001295 bool LongCalls = HST.useLongCalls() || EnableSaveRestoreLong;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001296
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001297 // Call spill function.
1298 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc()
Krzysztof Parzyszek4cef4622018-08-31 22:10:04 +00001299 : MBB.findDebugLoc(MBB.end());
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001300 MachineInstr *DeallocCall = nullptr;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001301
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001302 if (HasTC) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +00001303 unsigned RetOpc;
1304 if (LongCalls)
1305 RetOpc = IsPIC ? Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT_PIC
1306 : Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT;
1307 else
1308 RetOpc = IsPIC ? Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_PIC
1309 : Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4;
1310 DeallocCall = BuildMI(MBB, MI, DL, HII.get(RetOpc))
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001311 .addExternalSymbol(RestoreFn);
1312 } else {
1313 // The block has a return.
1314 MachineBasicBlock::iterator It = MBB.getFirstTerminator();
1315 assert(It->isReturn() && std::next(It) == MBB.end());
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +00001316 unsigned RetOpc;
1317 if (LongCalls)
1318 RetOpc = IsPIC ? Hexagon::RESTORE_DEALLOC_RET_JMP_V4_EXT_PIC
1319 : Hexagon::RESTORE_DEALLOC_RET_JMP_V4_EXT;
1320 else
1321 RetOpc = IsPIC ? Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC
1322 : Hexagon::RESTORE_DEALLOC_RET_JMP_V4;
1323 DeallocCall = BuildMI(MBB, It, DL, HII.get(RetOpc))
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001324 .addExternalSymbol(RestoreFn);
1325 // Transfer the function live-out registers.
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001326 DeallocCall->copyImplicitOps(MF, *It);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001327 }
Krzysztof Parzyszeke8e754d2016-04-25 17:49:44 +00001328 addCalleeSaveRegistersAsImpOperand(DeallocCall, CSI, true, false);
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001329 return true;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001330 }
1331
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001332 for (unsigned i = 0; i < CSI.size(); ++i) {
1333 unsigned Reg = CSI[i].getReg();
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001334 const TargetRegisterClass *RC = HRI.getMinimalPhysRegClass(Reg);
1335 int FI = CSI[i].getFrameIdx();
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +00001336 HII.loadRegFromStackSlot(MBB, MI, Reg, FI, RC, &HRI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001337 }
Krzysztof Parzyszekc9d4caa2016-03-24 20:20:07 +00001338
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001339 return true;
1340}
1341
Hans Wennborge1a2e902016-03-31 18:33:38 +00001342MachineBasicBlock::iterator HexagonFrameLowering::eliminateCallFramePseudoInstr(
1343 MachineFunction &MF, MachineBasicBlock &MBB,
1344 MachineBasicBlock::iterator I) const {
Eli Bendersky8da87162013-02-21 20:05:00 +00001345 MachineInstr &MI = *I;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001346 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszeke5689672015-04-23 20:26:21 +00001347 (void)Opc; // Silence compiler warning.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001348 assert((Opc == Hexagon::ADJCALLSTACKDOWN || Opc == Hexagon::ADJCALLSTACKUP) &&
1349 "Cannot handle this call frame pseudo instruction");
Hans Wennborge1a2e902016-03-31 18:33:38 +00001350 return MBB.erase(I);
Eli Bendersky8da87162013-02-21 20:05:00 +00001351}
1352
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001353void HexagonFrameLowering::processFunctionBeforeFrameFinalized(
1354 MachineFunction &MF, RegScavenger *RS) const {
1355 // If this function has uses aligned stack and also has variable sized stack
1356 // objects, then we need to map all spill slots to fixed positions, so that
1357 // they can be accessed through FP. Otherwise they would have to be accessed
1358 // via AP, which may not be available at the particular place in the program.
Matthias Braun941a7052016-07-28 18:40:00 +00001359 MachineFrameInfo &MFI = MF.getFrameInfo();
1360 bool HasAlloca = MFI.hasVarSizedObjects();
1361 bool NeedsAlign = (MFI.getMaxAlignment() > getStackAlignment());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001362
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001363 if (!HasAlloca || !NeedsAlign)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001364 return;
1365
Matthias Braun941a7052016-07-28 18:40:00 +00001366 unsigned LFS = MFI.getLocalFrameSize();
1367 for (int i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
1368 if (!MFI.isSpillSlotObjectIndex(i) || MFI.isDeadObjectIndex(i))
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001369 continue;
Matthias Braun941a7052016-07-28 18:40:00 +00001370 unsigned S = MFI.getObjectSize(i);
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00001371 // Reduce the alignment to at most 8. This will require unaligned vector
1372 // stores if they happen here.
Matthias Braun941a7052016-07-28 18:40:00 +00001373 unsigned A = std::max(MFI.getObjectAlignment(i), 8U);
1374 MFI.setObjectAlignment(i, 8);
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00001375 LFS = alignTo(LFS+S, A);
Matthias Braun941a7052016-07-28 18:40:00 +00001376 MFI.mapLocalFrameObject(i, -LFS);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001377 }
1378
Matthias Braun941a7052016-07-28 18:40:00 +00001379 MFI.setLocalFrameSize(LFS);
1380 unsigned A = MFI.getLocalFrameMaxAlign();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001381 assert(A <= 8 && "Unexpected local frame alignment");
1382 if (A == 0)
Matthias Braun941a7052016-07-28 18:40:00 +00001383 MFI.setLocalFrameMaxAlign(8);
1384 MFI.setUseLocalStackAllocationBlock(true);
Krzysztof Parzyszeka34901a2016-03-28 14:42:03 +00001385
1386 // Set the physical aligned-stack base address register.
1387 unsigned AP = 0;
1388 if (const MachineInstr *AI = getAlignaInstr(MF))
1389 AP = AI->getOperand(0).getReg();
1390 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
1391 HMFI.setStackAlignBasePhysReg(AP);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001392}
1393
Krzysztof Parzyszeka5bd29542016-05-16 18:02:28 +00001394/// Returns true if there are no caller-saved registers available in class RC.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001395static bool needToReserveScavengingSpillSlots(MachineFunction &MF,
Krzysztof Parzyszeka5bd29542016-05-16 18:02:28 +00001396 const HexagonRegisterInfo &HRI, const TargetRegisterClass *RC) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001397 MachineRegisterInfo &MRI = MF.getRegInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001398
Krzysztof Parzyszek6514a882016-03-21 19:57:08 +00001399 auto IsUsed = [&HRI,&MRI] (unsigned Reg) -> bool {
1400 for (MCRegAliasIterator AI(Reg, &HRI, true); AI.isValid(); ++AI)
1401 if (MRI.isPhysRegUsed(*AI))
1402 return true;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001403 return false;
Krzysztof Parzyszek6514a882016-03-21 19:57:08 +00001404 };
1405
Krzysztof Parzyszeke8e754d2016-04-25 17:49:44 +00001406 // Check for an unused caller-saved register. Callee-saved registers
1407 // have become pristine by now.
Krzysztof Parzyszeka5bd29542016-05-16 18:02:28 +00001408 for (const MCPhysReg *P = HRI.getCallerSavedRegs(&MF, RC); *P; ++P)
Krzysztof Parzyszek6514a882016-03-21 19:57:08 +00001409 if (!IsUsed(*P))
1410 return false;
Krzysztof Parzyszeke8e754d2016-04-25 17:49:44 +00001411
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001412 // All caller-saved registers are used.
1413 return true;
1414}
1415
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001416#ifndef NDEBUG
Krzysztof Parzyszeke5689672015-04-23 20:26:21 +00001417static void dump_registers(BitVector &Regs, const TargetRegisterInfo &TRI) {
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001418 dbgs() << '{';
1419 for (int x = Regs.find_first(); x >= 0; x = Regs.find_next(x)) {
1420 unsigned R = x;
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001421 dbgs() << ' ' << printReg(R, &TRI);
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001422 }
1423 dbgs() << " }";
1424}
1425#endif
1426
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001427bool HexagonFrameLowering::assignCalleeSavedSpillSlots(MachineFunction &MF,
1428 const TargetRegisterInfo *TRI, std::vector<CalleeSavedInfo> &CSI) const {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001429 LLVM_DEBUG(dbgs() << __func__ << " on " << MF.getName() << '\n');
Matthias Braun941a7052016-07-28 18:40:00 +00001430 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001431 BitVector SRegs(Hexagon::NUM_TARGET_REGS);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001432
1433 // Generate a set of unique, callee-saved registers (SRegs), where each
1434 // register in the set is maximal in terms of sub-/super-register relation,
1435 // i.e. for each R in SRegs, no proper super-register of R is also in SRegs.
1436
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001437 // (1) For each callee-saved register, add that register and all of its
1438 // sub-registers to SRegs.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001439 LLVM_DEBUG(dbgs() << "Initial CS registers: {");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001440 for (unsigned i = 0, n = CSI.size(); i < n; ++i) {
1441 unsigned R = CSI[i].getReg();
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001442 LLVM_DEBUG(dbgs() << ' ' << printReg(R, TRI));
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001443 for (MCSubRegIterator SR(R, TRI, true); SR.isValid(); ++SR)
1444 SRegs[*SR] = true;
1445 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001446 LLVM_DEBUG(dbgs() << " }\n");
1447 LLVM_DEBUG(dbgs() << "SRegs.1: "; dump_registers(SRegs, *TRI);
1448 dbgs() << "\n");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001449
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001450 // (2) For each reserved register, remove that register and all of its
1451 // sub- and super-registers from SRegs.
1452 BitVector Reserved = TRI->getReservedRegs(MF);
1453 for (int x = Reserved.find_first(); x >= 0; x = Reserved.find_next(x)) {
1454 unsigned R = x;
1455 for (MCSuperRegIterator SR(R, TRI, true); SR.isValid(); ++SR)
1456 SRegs[*SR] = false;
1457 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001458 LLVM_DEBUG(dbgs() << "Res: "; dump_registers(Reserved, *TRI);
1459 dbgs() << "\n");
1460 LLVM_DEBUG(dbgs() << "SRegs.2: "; dump_registers(SRegs, *TRI);
1461 dbgs() << "\n");
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001462
1463 // (3) Collect all registers that have at least one sub-register in SRegs,
1464 // and also have no sub-registers that are reserved. These will be the can-
1465 // didates for saving as a whole instead of their individual sub-registers.
1466 // (Saving R17:16 instead of R16 is fine, but only if R17 was not reserved.)
1467 BitVector TmpSup(Hexagon::NUM_TARGET_REGS);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001468 for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) {
1469 unsigned R = x;
1470 for (MCSuperRegIterator SR(R, TRI); SR.isValid(); ++SR)
1471 TmpSup[*SR] = true;
1472 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001473 for (int x = TmpSup.find_first(); x >= 0; x = TmpSup.find_next(x)) {
1474 unsigned R = x;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001475 for (MCSubRegIterator SR(R, TRI, true); SR.isValid(); ++SR) {
1476 if (!Reserved[*SR])
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001477 continue;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001478 TmpSup[R] = false;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001479 break;
1480 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001481 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001482 LLVM_DEBUG(dbgs() << "TmpSup: "; dump_registers(TmpSup, *TRI);
1483 dbgs() << "\n");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001484
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001485 // (4) Include all super-registers found in (3) into SRegs.
1486 SRegs |= TmpSup;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001487 LLVM_DEBUG(dbgs() << "SRegs.4: "; dump_registers(SRegs, *TRI);
1488 dbgs() << "\n");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001489
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00001490 // (5) For each register R in SRegs, if any super-register of R is in SRegs,
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001491 // remove R from SRegs.
1492 for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) {
1493 unsigned R = x;
1494 for (MCSuperRegIterator SR(R, TRI); SR.isValid(); ++SR) {
1495 if (!SRegs[*SR])
1496 continue;
1497 SRegs[R] = false;
1498 break;
1499 }
1500 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001501 LLVM_DEBUG(dbgs() << "SRegs.5: "; dump_registers(SRegs, *TRI);
1502 dbgs() << "\n");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001503
1504 // Now, for each register that has a fixed stack slot, create the stack
1505 // object for it.
1506 CSI.clear();
1507
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +00001508 using SpillSlot = TargetFrameLowering::SpillSlot;
1509
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001510 unsigned NumFixed;
1511 int MinOffset = 0; // CS offsets are negative.
1512 const SpillSlot *FixedSlots = getCalleeSavedSpillSlots(NumFixed);
1513 for (const SpillSlot *S = FixedSlots; S != FixedSlots+NumFixed; ++S) {
1514 if (!SRegs[S->Reg])
1515 continue;
1516 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(S->Reg);
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001517 int FI = MFI.CreateFixedSpillStackObject(TRI->getSpillSize(*RC), S->Offset);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001518 MinOffset = std::min(MinOffset, S->Offset);
1519 CSI.push_back(CalleeSavedInfo(S->Reg, FI));
1520 SRegs[S->Reg] = false;
1521 }
1522
1523 // There can be some registers that don't have fixed slots. For example,
1524 // we need to store R0-R3 in functions with exception handling. For each
1525 // such register, create a non-fixed stack object.
1526 for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) {
1527 unsigned R = x;
1528 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(R);
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001529 unsigned Size = TRI->getSpillSize(*RC);
1530 int Off = MinOffset - Size;
1531 unsigned Align = std::min(TRI->getSpillAlignment(*RC), getStackAlignment());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001532 assert(isPowerOf2_32(Align));
1533 Off &= -Align;
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001534 int FI = MFI.CreateFixedSpillStackObject(Size, Off);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001535 MinOffset = std::min(MinOffset, Off);
1536 CSI.push_back(CalleeSavedInfo(R, FI));
1537 SRegs[R] = false;
1538 }
1539
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001540 LLVM_DEBUG({
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001541 dbgs() << "CS information: {";
1542 for (unsigned i = 0, n = CSI.size(); i < n; ++i) {
1543 int FI = CSI[i].getFrameIdx();
Matthias Braun941a7052016-07-28 18:40:00 +00001544 int Off = MFI.getObjectOffset(FI);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001545 dbgs() << ' ' << printReg(CSI[i].getReg(), TRI) << ":fi#" << FI << ":sp";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001546 if (Off >= 0)
1547 dbgs() << '+';
1548 dbgs() << Off;
1549 }
1550 dbgs() << " }\n";
1551 });
1552
1553#ifndef NDEBUG
1554 // Verify that all registers were handled.
1555 bool MissedReg = false;
1556 for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) {
1557 unsigned R = x;
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001558 dbgs() << printReg(R, TRI) << ' ';
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001559 MissedReg = true;
1560 }
1561 if (MissedReg)
1562 llvm_unreachable("...there are unhandled callee-saved registers!");
1563#endif
1564
1565 return true;
1566}
1567
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001568bool HexagonFrameLowering::expandCopy(MachineBasicBlock &B,
1569 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
1570 const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const {
1571 MachineInstr *MI = &*It;
1572 DebugLoc DL = MI->getDebugLoc();
1573 unsigned DstR = MI->getOperand(0).getReg();
1574 unsigned SrcR = MI->getOperand(1).getReg();
1575 if (!Hexagon::ModRegsRegClass.contains(DstR) ||
1576 !Hexagon::ModRegsRegClass.contains(SrcR))
1577 return false;
1578
1579 unsigned TmpR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
Diana Picus116bbab2017-01-13 09:58:52 +00001580 BuildMI(B, It, DL, HII.get(TargetOpcode::COPY), TmpR).add(MI->getOperand(1));
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001581 BuildMI(B, It, DL, HII.get(TargetOpcode::COPY), DstR)
1582 .addReg(TmpR, RegState::Kill);
1583
1584 NewRegs.push_back(TmpR);
1585 B.erase(It);
1586 return true;
1587}
1588
1589bool HexagonFrameLowering::expandStoreInt(MachineBasicBlock &B,
1590 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
1591 const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const {
1592 MachineInstr *MI = &*It;
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001593 if (!MI->getOperand(0).isFI())
1594 return false;
1595
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001596 DebugLoc DL = MI->getDebugLoc();
1597 unsigned Opc = MI->getOpcode();
1598 unsigned SrcR = MI->getOperand(2).getReg();
1599 bool IsKill = MI->getOperand(2).isKill();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001600 int FI = MI->getOperand(0).getIndex();
1601
1602 // TmpR = C2_tfrpr SrcR if SrcR is a predicate register
1603 // TmpR = A2_tfrcrr SrcR if SrcR is a modifier register
1604 unsigned TmpR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
1605 unsigned TfrOpc = (Opc == Hexagon::STriw_pred) ? Hexagon::C2_tfrpr
1606 : Hexagon::A2_tfrcrr;
1607 BuildMI(B, It, DL, HII.get(TfrOpc), TmpR)
1608 .addReg(SrcR, getKillRegState(IsKill));
1609
1610 // S2_storeri_io FI, 0, TmpR
1611 BuildMI(B, It, DL, HII.get(Hexagon::S2_storeri_io))
Chandler Carruthc73c0302018-08-16 21:30:05 +00001612 .addFrameIndex(FI)
1613 .addImm(0)
1614 .addReg(TmpR, RegState::Kill)
1615 .cloneMemRefs(*MI);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001616
1617 NewRegs.push_back(TmpR);
1618 B.erase(It);
1619 return true;
1620}
1621
1622bool HexagonFrameLowering::expandLoadInt(MachineBasicBlock &B,
1623 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
1624 const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const {
1625 MachineInstr *MI = &*It;
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001626 if (!MI->getOperand(1).isFI())
1627 return false;
1628
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001629 DebugLoc DL = MI->getDebugLoc();
1630 unsigned Opc = MI->getOpcode();
1631 unsigned DstR = MI->getOperand(0).getReg();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001632 int FI = MI->getOperand(1).getIndex();
1633
1634 // TmpR = L2_loadri_io FI, 0
1635 unsigned TmpR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
1636 BuildMI(B, It, DL, HII.get(Hexagon::L2_loadri_io), TmpR)
Chandler Carruthc73c0302018-08-16 21:30:05 +00001637 .addFrameIndex(FI)
1638 .addImm(0)
1639 .cloneMemRefs(*MI);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001640
1641 // DstR = C2_tfrrp TmpR if DstR is a predicate register
1642 // DstR = A2_tfrrcr TmpR if DstR is a modifier register
1643 unsigned TfrOpc = (Opc == Hexagon::LDriw_pred) ? Hexagon::C2_tfrrp
1644 : Hexagon::A2_tfrrcr;
1645 BuildMI(B, It, DL, HII.get(TfrOpc), DstR)
1646 .addReg(TmpR, RegState::Kill);
1647
1648 NewRegs.push_back(TmpR);
1649 B.erase(It);
1650 return true;
1651}
1652
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001653bool HexagonFrameLowering::expandStoreVecPred(MachineBasicBlock &B,
1654 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
1655 const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const {
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001656 MachineInstr *MI = &*It;
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001657 if (!MI->getOperand(0).isFI())
1658 return false;
1659
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001660 DebugLoc DL = MI->getDebugLoc();
1661 unsigned SrcR = MI->getOperand(2).getReg();
1662 bool IsKill = MI->getOperand(2).isKill();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001663 int FI = MI->getOperand(0).getIndex();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001664 auto *RC = &Hexagon::HvxVRRegClass;
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001665
1666 // Insert transfer to general vector register.
1667 // TmpR0 = A2_tfrsi 0x01010101
1668 // TmpR1 = V6_vandqrt Qx, TmpR0
1669 // store FI, 0, TmpR1
1670 unsigned TmpR0 = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
1671 unsigned TmpR1 = MRI.createVirtualRegister(RC);
1672
1673 BuildMI(B, It, DL, HII.get(Hexagon::A2_tfrsi), TmpR0)
1674 .addImm(0x01010101);
1675
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001676 BuildMI(B, It, DL, HII.get(Hexagon::V6_vandqrt), TmpR1)
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001677 .addReg(SrcR, getKillRegState(IsKill))
1678 .addReg(TmpR0, RegState::Kill);
1679
1680 auto *HRI = B.getParent()->getSubtarget<HexagonSubtarget>().getRegisterInfo();
1681 HII.storeRegToStackSlot(B, It, TmpR1, true, FI, RC, HRI);
1682 expandStoreVec(B, std::prev(It), MRI, HII, NewRegs);
1683
1684 NewRegs.push_back(TmpR0);
1685 NewRegs.push_back(TmpR1);
1686 B.erase(It);
1687 return true;
1688}
1689
1690bool HexagonFrameLowering::expandLoadVecPred(MachineBasicBlock &B,
1691 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
1692 const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const {
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001693 MachineInstr *MI = &*It;
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001694 if (!MI->getOperand(1).isFI())
1695 return false;
1696
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001697 DebugLoc DL = MI->getDebugLoc();
1698 unsigned DstR = MI->getOperand(0).getReg();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001699 int FI = MI->getOperand(1).getIndex();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001700 auto *RC = &Hexagon::HvxVRRegClass;
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001701
1702 // TmpR0 = A2_tfrsi 0x01010101
1703 // TmpR1 = load FI, 0
1704 // DstR = V6_vandvrt TmpR1, TmpR0
1705 unsigned TmpR0 = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
1706 unsigned TmpR1 = MRI.createVirtualRegister(RC);
1707
1708 BuildMI(B, It, DL, HII.get(Hexagon::A2_tfrsi), TmpR0)
1709 .addImm(0x01010101);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001710 MachineFunction &MF = *B.getParent();
1711 auto *HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001712 HII.loadRegFromStackSlot(B, It, TmpR1, FI, RC, HRI);
1713 expandLoadVec(B, std::prev(It), MRI, HII, NewRegs);
1714
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001715 BuildMI(B, It, DL, HII.get(Hexagon::V6_vandvrt), DstR)
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001716 .addReg(TmpR1, RegState::Kill)
1717 .addReg(TmpR0, RegState::Kill);
1718
1719 NewRegs.push_back(TmpR0);
1720 NewRegs.push_back(TmpR1);
1721 B.erase(It);
1722 return true;
1723}
1724
1725bool HexagonFrameLowering::expandStoreVec2(MachineBasicBlock &B,
1726 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
1727 const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const {
1728 MachineFunction &MF = *B.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +00001729 auto &MFI = MF.getFrameInfo();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001730 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
1731 MachineInstr *MI = &*It;
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001732 if (!MI->getOperand(0).isFI())
1733 return false;
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001734
Krzysztof Parzyszekb71085b2016-10-21 16:38:29 +00001735 // It is possible that the double vector being stored is only partially
1736 // defined. From the point of view of the liveness tracking, it is ok to
1737 // store it as a whole, but if we break it up we may end up storing a
1738 // register that is entirely undefined.
Matthias Braunac4307c2017-05-26 21:51:00 +00001739 LivePhysRegs LPR(HRI);
Krzysztof Parzyszekb71085b2016-10-21 16:38:29 +00001740 LPR.addLiveIns(B);
Matthias Braunc6613872018-11-06 19:00:11 +00001741 SmallVector<std::pair<MCPhysReg, const MachineOperand*>,2> Clobbers;
Krzysztof Parzyszek954dd8d2017-01-18 23:11:40 +00001742 for (auto R = B.begin(); R != It; ++R) {
1743 Clobbers.clear();
Krzysztof Parzyszekb71085b2016-10-21 16:38:29 +00001744 LPR.stepForward(*R, Clobbers);
Krzysztof Parzyszek954dd8d2017-01-18 23:11:40 +00001745 }
Krzysztof Parzyszekb71085b2016-10-21 16:38:29 +00001746
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001747 DebugLoc DL = MI->getDebugLoc();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001748 unsigned SrcR = MI->getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001749 unsigned SrcLo = HRI.getSubReg(SrcR, Hexagon::vsub_lo);
1750 unsigned SrcHi = HRI.getSubReg(SrcR, Hexagon::vsub_hi);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001751 bool IsKill = MI->getOperand(2).isKill();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001752 int FI = MI->getOperand(0).getIndex();
1753
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001754 unsigned Size = HRI.getSpillSize(Hexagon::HvxVRRegClass);
1755 unsigned NeedAlign = HRI.getSpillAlignment(Hexagon::HvxVRRegClass);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001756 unsigned HasAlign = MFI.getObjectAlignment(FI);
1757 unsigned StoreOpc;
1758
1759 // Store low part.
Krzysztof Parzyszekb71085b2016-10-21 16:38:29 +00001760 if (LPR.contains(SrcLo)) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001761 StoreOpc = NeedAlign <= HasAlign ? Hexagon::V6_vS32b_ai
1762 : Hexagon::V6_vS32Ub_ai;
Krzysztof Parzyszekb71085b2016-10-21 16:38:29 +00001763 BuildMI(B, It, DL, HII.get(StoreOpc))
Chandler Carruthc73c0302018-08-16 21:30:05 +00001764 .addFrameIndex(FI)
1765 .addImm(0)
1766 .addReg(SrcLo, getKillRegState(IsKill))
1767 .cloneMemRefs(*MI);
Krzysztof Parzyszekb71085b2016-10-21 16:38:29 +00001768 }
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001769
Krzysztof Parzyszekb71085b2016-10-21 16:38:29 +00001770 // Store high part.
1771 if (LPR.contains(SrcHi)) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001772 StoreOpc = NeedAlign <= MinAlign(HasAlign, Size) ? Hexagon::V6_vS32b_ai
1773 : Hexagon::V6_vS32Ub_ai;
Krzysztof Parzyszekb71085b2016-10-21 16:38:29 +00001774 BuildMI(B, It, DL, HII.get(StoreOpc))
Chandler Carruthc73c0302018-08-16 21:30:05 +00001775 .addFrameIndex(FI)
1776 .addImm(Size)
1777 .addReg(SrcHi, getKillRegState(IsKill))
1778 .cloneMemRefs(*MI);
Krzysztof Parzyszekb71085b2016-10-21 16:38:29 +00001779 }
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001780
1781 B.erase(It);
1782 return true;
1783}
1784
1785bool HexagonFrameLowering::expandLoadVec2(MachineBasicBlock &B,
1786 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
1787 const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const {
1788 MachineFunction &MF = *B.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +00001789 auto &MFI = MF.getFrameInfo();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001790 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
1791 MachineInstr *MI = &*It;
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001792 if (!MI->getOperand(1).isFI())
1793 return false;
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001794
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001795 DebugLoc DL = MI->getDebugLoc();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001796 unsigned DstR = MI->getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001797 unsigned DstHi = HRI.getSubReg(DstR, Hexagon::vsub_hi);
1798 unsigned DstLo = HRI.getSubReg(DstR, Hexagon::vsub_lo);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001799 int FI = MI->getOperand(1).getIndex();
1800
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001801 unsigned Size = HRI.getSpillSize(Hexagon::HvxVRRegClass);
1802 unsigned NeedAlign = HRI.getSpillAlignment(Hexagon::HvxVRRegClass);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001803 unsigned HasAlign = MFI.getObjectAlignment(FI);
1804 unsigned LoadOpc;
1805
1806 // Load low part.
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001807 LoadOpc = NeedAlign <= HasAlign ? Hexagon::V6_vL32b_ai
1808 : Hexagon::V6_vL32Ub_ai;
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001809 BuildMI(B, It, DL, HII.get(LoadOpc), DstLo)
Chandler Carruthc73c0302018-08-16 21:30:05 +00001810 .addFrameIndex(FI)
1811 .addImm(0)
1812 .cloneMemRefs(*MI);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001813
1814 // Load high part.
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001815 LoadOpc = NeedAlign <= MinAlign(HasAlign, Size) ? Hexagon::V6_vL32b_ai
1816 : Hexagon::V6_vL32Ub_ai;
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001817 BuildMI(B, It, DL, HII.get(LoadOpc), DstHi)
Chandler Carruthc73c0302018-08-16 21:30:05 +00001818 .addFrameIndex(FI)
1819 .addImm(Size)
1820 .cloneMemRefs(*MI);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001821
1822 B.erase(It);
1823 return true;
1824}
1825
1826bool HexagonFrameLowering::expandStoreVec(MachineBasicBlock &B,
1827 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
1828 const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const {
1829 MachineFunction &MF = *B.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +00001830 auto &MFI = MF.getFrameInfo();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001831 MachineInstr *MI = &*It;
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001832 if (!MI->getOperand(0).isFI())
1833 return false;
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001834
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001835 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001836 DebugLoc DL = MI->getDebugLoc();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001837 unsigned SrcR = MI->getOperand(2).getReg();
1838 bool IsKill = MI->getOperand(2).isKill();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001839 int FI = MI->getOperand(0).getIndex();
1840
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001841 unsigned NeedAlign = HRI.getSpillAlignment(Hexagon::HvxVRRegClass);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001842 unsigned HasAlign = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001843 unsigned StoreOpc = NeedAlign <= HasAlign ? Hexagon::V6_vS32b_ai
1844 : Hexagon::V6_vS32Ub_ai;
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001845 BuildMI(B, It, DL, HII.get(StoreOpc))
Chandler Carruthc73c0302018-08-16 21:30:05 +00001846 .addFrameIndex(FI)
1847 .addImm(0)
1848 .addReg(SrcR, getKillRegState(IsKill))
1849 .cloneMemRefs(*MI);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001850
1851 B.erase(It);
1852 return true;
1853}
1854
1855bool HexagonFrameLowering::expandLoadVec(MachineBasicBlock &B,
1856 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
1857 const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const {
1858 MachineFunction &MF = *B.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +00001859 auto &MFI = MF.getFrameInfo();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001860 MachineInstr *MI = &*It;
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001861 if (!MI->getOperand(1).isFI())
1862 return false;
1863
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001864 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001865 DebugLoc DL = MI->getDebugLoc();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001866 unsigned DstR = MI->getOperand(0).getReg();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001867 int FI = MI->getOperand(1).getIndex();
1868
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001869 unsigned NeedAlign = HRI.getSpillAlignment(Hexagon::HvxVRRegClass);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001870 unsigned HasAlign = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001871 unsigned LoadOpc = NeedAlign <= HasAlign ? Hexagon::V6_vL32b_ai
1872 : Hexagon::V6_vL32Ub_ai;
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001873 BuildMI(B, It, DL, HII.get(LoadOpc), DstR)
Chandler Carruthc73c0302018-08-16 21:30:05 +00001874 .addFrameIndex(FI)
1875 .addImm(0)
1876 .cloneMemRefs(*MI);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001877
1878 B.erase(It);
1879 return true;
1880}
1881
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001882bool HexagonFrameLowering::expandSpillMacros(MachineFunction &MF,
1883 SmallVectorImpl<unsigned> &NewRegs) const {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001884 auto &HII = *MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00001885 MachineRegisterInfo &MRI = MF.getRegInfo();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001886 bool Changed = false;
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00001887
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001888 for (auto &B : MF) {
1889 // Traverse the basic block.
1890 MachineBasicBlock::iterator NextI;
1891 for (auto I = B.begin(), E = B.end(); I != E; I = NextI) {
1892 MachineInstr *MI = &*I;
1893 NextI = std::next(I);
1894 unsigned Opc = MI->getOpcode();
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00001895
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001896 switch (Opc) {
1897 case TargetOpcode::COPY:
Krzysztof Parzyszeke8e754d2016-04-25 17:49:44 +00001898 Changed |= expandCopy(B, I, MRI, HII, NewRegs);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001899 break;
1900 case Hexagon::STriw_pred:
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001901 case Hexagon::STriw_ctr:
Krzysztof Parzyszeke8e754d2016-04-25 17:49:44 +00001902 Changed |= expandStoreInt(B, I, MRI, HII, NewRegs);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001903 break;
1904 case Hexagon::LDriw_pred:
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001905 case Hexagon::LDriw_ctr:
Krzysztof Parzyszeke8e754d2016-04-25 17:49:44 +00001906 Changed |= expandLoadInt(B, I, MRI, HII, NewRegs);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001907 break;
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00001908 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszeke8e754d2016-04-25 17:49:44 +00001909 Changed |= expandStoreVecPred(B, I, MRI, HII, NewRegs);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001910 break;
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00001911 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszeke8e754d2016-04-25 17:49:44 +00001912 Changed |= expandLoadVecPred(B, I, MRI, HII, NewRegs);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001913 break;
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001914 case Hexagon::PS_vloadrw_ai:
1915 case Hexagon::PS_vloadrwu_ai:
Krzysztof Parzyszeke8e754d2016-04-25 17:49:44 +00001916 Changed |= expandLoadVec2(B, I, MRI, HII, NewRegs);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001917 break;
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001918 case Hexagon::PS_vstorerw_ai:
1919 case Hexagon::PS_vstorerwu_ai:
Krzysztof Parzyszeke8e754d2016-04-25 17:49:44 +00001920 Changed |= expandStoreVec2(B, I, MRI, HII, NewRegs);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001921 break;
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00001922 }
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00001923 }
1924 }
1925
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001926 return Changed;
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00001927}
1928
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001929void HexagonFrameLowering::determineCalleeSaves(MachineFunction &MF,
1930 BitVector &SavedRegs,
1931 RegScavenger *RS) const {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001932 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001933
1934 SavedRegs.resize(HRI.getNumRegs());
1935
1936 // If we have a function containing __builtin_eh_return we want to spill and
1937 // restore all callee saved registers. Pretend that they are used.
1938 if (MF.getInfo<HexagonMachineFunctionInfo>()->hasEHReturn())
1939 for (const MCPhysReg *R = HRI.getCalleeSavedRegs(&MF); *R; ++R)
1940 SavedRegs.set(*R);
1941
1942 // Replace predicate register pseudo spill code.
1943 SmallVector<unsigned,8> NewRegs;
1944 expandSpillMacros(MF, NewRegs);
Krzysztof Parzyszeka34901a2016-03-28 14:42:03 +00001945 if (OptimizeSpillSlots && !isOptNone(MF))
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00001946 optimizeSpillSlots(MF, NewRegs);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001947
Hiroshi Inoue7f9f92f2018-02-22 07:48:29 +00001948 // We need to reserve a spill slot if scavenging could potentially require
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001949 // spilling a scavenged register.
Krzysztof Parzyszekddafa2c2016-08-01 17:15:30 +00001950 if (!NewRegs.empty() || mayOverflowFrameOffset(MF)) {
Matthias Braun941a7052016-07-28 18:40:00 +00001951 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001952 MachineRegisterInfo &MRI = MF.getRegInfo();
1953 SetVector<const TargetRegisterClass*> SpillRCs;
Krzysztof Parzyszeka5bd29542016-05-16 18:02:28 +00001954 // Reserve an int register in any case, because it could be used to hold
1955 // the stack offset in case it does not fit into a spill instruction.
1956 SpillRCs.insert(&Hexagon::IntRegsRegClass);
1957
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001958 for (unsigned VR : NewRegs)
1959 SpillRCs.insert(MRI.getRegClass(VR));
1960
Krzysztof Parzyszeka5bd29542016-05-16 18:02:28 +00001961 for (auto *RC : SpillRCs) {
1962 if (!needToReserveScavengingSpillSlots(MF, HRI, RC))
1963 continue;
1964 unsigned Num = RC == &Hexagon::IntRegsRegClass ? NumberScavengerSlots : 1;
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001965 unsigned S = HRI.getSpillSize(*RC), A = HRI.getSpillAlignment(*RC);
Krzysztof Parzyszeka5bd29542016-05-16 18:02:28 +00001966 for (unsigned i = 0; i < Num; i++) {
1967 int NewFI = MFI.CreateSpillStackObject(S, A);
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001968 RS->addScavengingFrameIndex(NewFI);
1969 }
1970 }
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +00001971 }
1972
1973 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1974}
1975
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00001976unsigned HexagonFrameLowering::findPhysReg(MachineFunction &MF,
1977 HexagonBlockRanges::IndexRange &FIR,
1978 HexagonBlockRanges::InstrIndexMap &IndexMap,
1979 HexagonBlockRanges::RegToRangeMap &DeadMap,
1980 const TargetRegisterClass *RC) const {
1981 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
1982 auto &MRI = MF.getRegInfo();
1983
1984 auto isDead = [&FIR,&DeadMap] (unsigned Reg) -> bool {
1985 auto F = DeadMap.find({Reg,0});
1986 if (F == DeadMap.end())
1987 return false;
1988 for (auto &DR : F->second)
1989 if (DR.contains(FIR))
1990 return true;
1991 return false;
1992 };
1993
1994 for (unsigned Reg : RC->getRawAllocationOrder(MF)) {
1995 bool Dead = true;
1996 for (auto R : HexagonBlockRanges::expandToSubRegs({Reg,0}, MRI, HRI)) {
1997 if (isDead(R.Reg))
1998 continue;
1999 Dead = false;
2000 break;
2001 }
2002 if (Dead)
2003 return Reg;
2004 }
2005 return 0;
2006}
2007
2008void HexagonFrameLowering::optimizeSpillSlots(MachineFunction &MF,
2009 SmallVectorImpl<unsigned> &VRegs) const {
2010 auto &HST = MF.getSubtarget<HexagonSubtarget>();
2011 auto &HII = *HST.getInstrInfo();
2012 auto &HRI = *HST.getRegisterInfo();
2013 auto &MRI = MF.getRegInfo();
2014 HexagonBlockRanges HBR(MF);
2015
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +00002016 using BlockIndexMap =
2017 std::map<MachineBasicBlock *, HexagonBlockRanges::InstrIndexMap>;
2018 using BlockRangeMap =
2019 std::map<MachineBasicBlock *, HexagonBlockRanges::RangeList>;
2020 using IndexType = HexagonBlockRanges::IndexType;
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002021
2022 struct SlotInfo {
2023 BlockRangeMap Map;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +00002024 unsigned Size = 0;
2025 const TargetRegisterClass *RC = nullptr;
NAKAMURA Takumic2cc8702016-02-13 07:29:49 +00002026
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +00002027 SlotInfo() = default;
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002028 };
2029
2030 BlockIndexMap BlockIndexes;
2031 SmallSet<int,4> BadFIs;
2032 std::map<int,SlotInfo> FIRangeMap;
2033
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002034 // Accumulate register classes: get a common class for a pre-existing
2035 // class HaveRC and a new class NewRC. Return nullptr if a common class
2036 // cannot be found, otherwise return the resulting class. If HaveRC is
2037 // nullptr, assume that it is still unset.
Malcolm Parsons17d266b2017-01-13 17:12:16 +00002038 auto getCommonRC =
2039 [](const TargetRegisterClass *HaveRC,
2040 const TargetRegisterClass *NewRC) -> const TargetRegisterClass * {
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002041 if (HaveRC == nullptr || HaveRC == NewRC)
2042 return NewRC;
2043 // Different classes, both non-null. Pick the more general one.
2044 if (HaveRC->hasSubClassEq(NewRC))
2045 return HaveRC;
2046 if (NewRC->hasSubClassEq(HaveRC))
2047 return NewRC;
2048 return nullptr;
2049 };
2050
2051 // Scan all blocks in the function. Check all occurrences of frame indexes,
2052 // and collect relevant information.
2053 for (auto &B : MF) {
2054 std::map<int,IndexType> LastStore, LastLoad;
Krzysztof Parzyszek280a50e2016-02-13 14:06:01 +00002055 // Emplace appears not to be supported in gcc 4.7.2-4.
2056 //auto P = BlockIndexes.emplace(&B, HexagonBlockRanges::InstrIndexMap(B));
Krzysztof Parzyszekde697d42016-02-17 15:02:07 +00002057 auto P = BlockIndexes.insert(
2058 std::make_pair(&B, HexagonBlockRanges::InstrIndexMap(B)));
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002059 auto &IndexMap = P.first->second;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002060 LLVM_DEBUG(dbgs() << "Index map for " << printMBBReference(B) << "\n"
2061 << IndexMap << '\n');
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002062
2063 for (auto &In : B) {
2064 int LFI, SFI;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002065 bool Load = HII.isLoadFromStackSlot(In, LFI) && !HII.isPredicated(In);
2066 bool Store = HII.isStoreToStackSlot(In, SFI) && !HII.isPredicated(In);
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002067 if (Load && Store) {
2068 // If it's both a load and a store, then we won't handle it.
2069 BadFIs.insert(LFI);
2070 BadFIs.insert(SFI);
2071 continue;
2072 }
2073 // Check for register classes of the register used as the source for
2074 // the store, and the register used as the destination for the load.
2075 // Also, only accept base+imm_offset addressing modes. Other addressing
2076 // modes can have side-effects (post-increments, etc.). For stack
2077 // slots they are very unlikely, so there is not much loss due to
2078 // this restriction.
2079 if (Load || Store) {
2080 int TFI = Load ? LFI : SFI;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002081 unsigned AM = HII.getAddrMode(In);
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002082 SlotInfo &SI = FIRangeMap[TFI];
2083 bool Bad = (AM != HexagonII::BaseImmOffset);
2084 if (!Bad) {
2085 // If the addressing mode is ok, check the register class.
Krzysztof Parzyszek5241b8e2016-07-27 20:50:42 +00002086 unsigned OpNum = Load ? 0 : 2;
2087 auto *RC = HII.getRegClass(In.getDesc(), OpNum, &HRI, MF);
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002088 RC = getCommonRC(SI.RC, RC);
2089 if (RC == nullptr)
2090 Bad = true;
2091 else
2092 SI.RC = RC;
2093 }
2094 if (!Bad) {
2095 // Check sizes.
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00002096 unsigned S = HII.getMemAccessSize(In);
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002097 if (SI.Size != 0 && SI.Size != S)
2098 Bad = true;
2099 else
2100 SI.Size = S;
2101 }
Krzysztof Parzyszek5241b8e2016-07-27 20:50:42 +00002102 if (!Bad) {
2103 for (auto *Mo : In.memoperands()) {
2104 if (!Mo->isVolatile())
2105 continue;
2106 Bad = true;
2107 break;
2108 }
2109 }
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002110 if (Bad)
2111 BadFIs.insert(TFI);
2112 }
2113
2114 // Locate uses of frame indices.
2115 for (unsigned i = 0, n = In.getNumOperands(); i < n; ++i) {
2116 const MachineOperand &Op = In.getOperand(i);
2117 if (!Op.isFI())
2118 continue;
2119 int FI = Op.getIndex();
2120 // Make sure that the following operand is an immediate and that
2121 // it is 0. This is the offset in the stack object.
2122 if (i+1 >= n || !In.getOperand(i+1).isImm() ||
2123 In.getOperand(i+1).getImm() != 0)
2124 BadFIs.insert(FI);
2125 if (BadFIs.count(FI))
2126 continue;
2127
2128 IndexType Index = IndexMap.getIndex(&In);
2129 if (Load) {
2130 if (LastStore[FI] == IndexType::None)
2131 LastStore[FI] = IndexType::Entry;
2132 LastLoad[FI] = Index;
2133 } else if (Store) {
2134 HexagonBlockRanges::RangeList &RL = FIRangeMap[FI].Map[&B];
2135 if (LastStore[FI] != IndexType::None)
2136 RL.add(LastStore[FI], LastLoad[FI], false, false);
2137 else if (LastLoad[FI] != IndexType::None)
2138 RL.add(IndexType::Entry, LastLoad[FI], false, false);
2139 LastLoad[FI] = IndexType::None;
2140 LastStore[FI] = Index;
2141 } else {
2142 BadFIs.insert(FI);
2143 }
2144 }
2145 }
2146
2147 for (auto &I : LastLoad) {
2148 IndexType LL = I.second;
2149 if (LL == IndexType::None)
2150 continue;
2151 auto &RL = FIRangeMap[I.first].Map[&B];
2152 IndexType &LS = LastStore[I.first];
2153 if (LS != IndexType::None)
2154 RL.add(LS, LL, false, false);
2155 else
2156 RL.add(IndexType::Entry, LL, false, false);
2157 LS = IndexType::None;
2158 }
2159 for (auto &I : LastStore) {
2160 IndexType LS = I.second;
2161 if (LS == IndexType::None)
2162 continue;
2163 auto &RL = FIRangeMap[I.first].Map[&B];
2164 RL.add(LS, IndexType::None, false, false);
2165 }
2166 }
2167
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002168 LLVM_DEBUG({
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002169 for (auto &P : FIRangeMap) {
2170 dbgs() << "fi#" << P.first;
2171 if (BadFIs.count(P.first))
2172 dbgs() << " (bad)";
2173 dbgs() << " RC: ";
2174 if (P.second.RC != nullptr)
2175 dbgs() << HRI.getRegClassName(P.second.RC) << '\n';
2176 else
2177 dbgs() << "<null>\n";
2178 for (auto &R : P.second.Map)
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00002179 dbgs() << " " << printMBBReference(*R.first) << " { " << R.second
2180 << "}\n";
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002181 }
2182 });
2183
2184 // When a slot is loaded from in a block without being stored to in the
2185 // same block, it is live-on-entry to this block. To avoid CFG analysis,
2186 // consider this slot to be live-on-exit from all blocks.
2187 SmallSet<int,4> LoxFIs;
2188
2189 std::map<MachineBasicBlock*,std::vector<int>> BlockFIMap;
2190
2191 for (auto &P : FIRangeMap) {
2192 // P = pair(FI, map: BB->RangeList)
2193 if (BadFIs.count(P.first))
2194 continue;
2195 for (auto &B : MF) {
2196 auto F = P.second.Map.find(&B);
2197 // F = pair(BB, RangeList)
2198 if (F == P.second.Map.end() || F->second.empty())
2199 continue;
2200 HexagonBlockRanges::IndexRange &IR = F->second.front();
2201 if (IR.start() == IndexType::Entry)
2202 LoxFIs.insert(P.first);
2203 BlockFIMap[&B].push_back(P.first);
2204 }
2205 }
2206
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002207 LLVM_DEBUG({
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002208 dbgs() << "Block-to-FI map (* -- live-on-exit):\n";
2209 for (auto &P : BlockFIMap) {
2210 auto &FIs = P.second;
2211 if (FIs.empty())
2212 continue;
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00002213 dbgs() << " " << printMBBReference(*P.first) << ": {";
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002214 for (auto I : FIs) {
2215 dbgs() << " fi#" << I;
2216 if (LoxFIs.count(I))
2217 dbgs() << '*';
2218 }
2219 dbgs() << " }\n";
2220 }
2221 });
2222
Krzysztof Parzyszekdc421642016-07-27 20:58:43 +00002223#ifndef NDEBUG
2224 bool HasOptLimit = SpillOptMax.getPosition();
2225#endif
2226
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002227 // eliminate loads, when all loads eliminated, eliminate all stores.
2228 for (auto &B : MF) {
2229 auto F = BlockIndexes.find(&B);
2230 assert(F != BlockIndexes.end());
2231 HexagonBlockRanges::InstrIndexMap &IM = F->second;
2232 HexagonBlockRanges::RegToRangeMap LM = HBR.computeLiveMap(IM);
2233 HexagonBlockRanges::RegToRangeMap DM = HBR.computeDeadMap(IM, LM);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002234 LLVM_DEBUG(dbgs() << printMBBReference(B) << " dead map\n"
2235 << HexagonBlockRanges::PrintRangeMap(DM, HRI));
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002236
2237 for (auto FI : BlockFIMap[&B]) {
2238 if (BadFIs.count(FI))
2239 continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002240 LLVM_DEBUG(dbgs() << "Working on fi#" << FI << '\n');
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002241 HexagonBlockRanges::RangeList &RL = FIRangeMap[FI].Map[&B];
2242 for (auto &Range : RL) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002243 LLVM_DEBUG(dbgs() << "--Examining range:" << RL << '\n');
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002244 if (!IndexType::isInstr(Range.start()) ||
2245 !IndexType::isInstr(Range.end()))
2246 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002247 MachineInstr &SI = *IM.getInstr(Range.start());
2248 MachineInstr &EI = *IM.getInstr(Range.end());
2249 assert(SI.mayStore() && "Unexpected start instruction");
2250 assert(EI.mayLoad() && "Unexpected end instruction");
2251 MachineOperand &SrcOp = SI.getOperand(2);
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002252
2253 HexagonBlockRanges::RegisterRef SrcRR = { SrcOp.getReg(),
2254 SrcOp.getSubReg() };
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002255 auto *RC = HII.getRegClass(SI.getDesc(), 2, &HRI, MF);
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002256 // The this-> is needed to unconfuse MSVC.
2257 unsigned FoundR = this->findPhysReg(MF, Range, IM, DM, RC);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002258 LLVM_DEBUG(dbgs() << "Replacement reg:" << printReg(FoundR, &HRI)
2259 << '\n');
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002260 if (FoundR == 0)
2261 continue;
Krzysztof Parzyszekdc421642016-07-27 20:58:43 +00002262#ifndef NDEBUG
2263 if (HasOptLimit) {
2264 if (SpillOptCount >= SpillOptMax)
2265 return;
2266 SpillOptCount++;
2267 }
2268#endif
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002269
2270 // Generate the copy-in: "FoundR = COPY SrcR" at the store location.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002271 MachineBasicBlock::iterator StartIt = SI.getIterator(), NextIt;
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002272 MachineInstr *CopyIn = nullptr;
2273 if (SrcRR.Reg != FoundR || SrcRR.Sub != 0) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002274 const DebugLoc &DL = SI.getDebugLoc();
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002275 CopyIn = BuildMI(B, StartIt, DL, HII.get(TargetOpcode::COPY), FoundR)
Diana Picus116bbab2017-01-13 09:58:52 +00002276 .add(SrcOp);
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002277 }
2278
2279 ++StartIt;
2280 // Check if this is a last store and the FI is live-on-exit.
2281 if (LoxFIs.count(FI) && (&Range == &RL.back())) {
2282 // Update store's source register.
2283 if (unsigned SR = SrcOp.getSubReg())
2284 SrcOp.setReg(HRI.getSubReg(FoundR, SR));
2285 else
2286 SrcOp.setReg(FoundR);
2287 SrcOp.setSubReg(0);
2288 // We are keeping this register live.
2289 SrcOp.setIsKill(false);
2290 } else {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002291 B.erase(&SI);
2292 IM.replaceInstr(&SI, CopyIn);
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002293 }
2294
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002295 auto EndIt = std::next(EI.getIterator());
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002296 for (auto It = StartIt; It != EndIt; It = NextIt) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002297 MachineInstr &MI = *It;
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002298 NextIt = std::next(It);
2299 int TFI;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002300 if (!HII.isLoadFromStackSlot(MI, TFI) || TFI != FI)
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002301 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002302 unsigned DstR = MI.getOperand(0).getReg();
2303 assert(MI.getOperand(0).getSubReg() == 0);
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002304 MachineInstr *CopyOut = nullptr;
2305 if (DstR != FoundR) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002306 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00002307 unsigned MemSize = HII.getMemAccessSize(MI);
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002308 assert(HII.getAddrMode(MI) == HexagonII::BaseImmOffset);
2309 unsigned CopyOpc = TargetOpcode::COPY;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002310 if (HII.isSignExtendingLoad(MI))
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002311 CopyOpc = (MemSize == 1) ? Hexagon::A2_sxtb : Hexagon::A2_sxth;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002312 else if (HII.isZeroExtendingLoad(MI))
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002313 CopyOpc = (MemSize == 1) ? Hexagon::A2_zxtb : Hexagon::A2_zxth;
2314 CopyOut = BuildMI(B, It, DL, HII.get(CopyOpc), DstR)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002315 .addReg(FoundR, getKillRegState(&MI == &EI));
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002316 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002317 IM.replaceInstr(&MI, CopyOut);
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00002318 B.erase(It);
2319 }
2320
2321 // Update the dead map.
2322 HexagonBlockRanges::RegisterRef FoundRR = { FoundR, 0 };
2323 for (auto RR : HexagonBlockRanges::expandToSubRegs(FoundRR, MRI, HRI))
2324 DM[RR].subtract(Range);
2325 } // for Range in range list
2326 }
2327 }
2328}
2329
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002330void HexagonFrameLowering::expandAlloca(MachineInstr *AI,
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00002331 const HexagonInstrInfo &HII, unsigned SP, unsigned CF) const {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002332 MachineBasicBlock &MB = *AI->getParent();
2333 DebugLoc DL = AI->getDebugLoc();
2334 unsigned A = AI->getOperand(2).getImm();
2335
2336 // Have
2337 // Rd = alloca Rs, #A
2338 //
2339 // If Rs and Rd are different registers, use this sequence:
2340 // Rd = sub(r29, Rs)
2341 // r29 = sub(r29, Rs)
2342 // Rd = and(Rd, #-A) ; if necessary
2343 // r29 = and(r29, #-A) ; if necessary
2344 // Rd = add(Rd, #CF) ; CF size aligned to at most A
2345 // otherwise, do
2346 // Rd = sub(r29, Rs)
2347 // Rd = and(Rd, #-A) ; if necessary
2348 // r29 = Rd
2349 // Rd = add(Rd, #CF) ; CF size aligned to at most A
2350
2351 MachineOperand &RdOp = AI->getOperand(0);
2352 MachineOperand &RsOp = AI->getOperand(1);
2353 unsigned Rd = RdOp.getReg(), Rs = RsOp.getReg();
2354
2355 // Rd = sub(r29, Rs)
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00002356 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_sub), Rd)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002357 .addReg(SP)
2358 .addReg(Rs);
2359 if (Rs != Rd) {
2360 // r29 = sub(r29, Rs)
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00002361 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_sub), SP)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002362 .addReg(SP)
2363 .addReg(Rs);
2364 }
2365 if (A > 8) {
2366 // Rd = and(Rd, #-A)
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00002367 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_andir), Rd)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002368 .addReg(Rd)
2369 .addImm(-int64_t(A));
2370 if (Rs != Rd)
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00002371 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_andir), SP)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002372 .addReg(SP)
2373 .addImm(-int64_t(A));
2374 }
2375 if (Rs == Rd) {
2376 // r29 = Rd
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00002377 BuildMI(MB, AI, DL, HII.get(TargetOpcode::COPY), SP)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002378 .addReg(Rd);
2379 }
2380 if (CF > 0) {
2381 // Rd = add(Rd, #CF)
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00002382 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_addi), Rd)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002383 .addReg(Rd)
2384 .addImm(CF);
2385 }
2386}
2387
2388bool HexagonFrameLowering::needsAligna(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +00002389 const MachineFrameInfo &MFI = MF.getFrameInfo();
2390 if (!MFI.hasVarSizedObjects())
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002391 return false;
Krzysztof Parzyszek71702172017-06-23 19:47:04 +00002392 unsigned MaxA = MFI.getMaxAlignment();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002393 if (MaxA <= getStackAlignment())
2394 return false;
2395 return true;
2396}
2397
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00002398const MachineInstr *HexagonFrameLowering::getAlignaInstr(
2399 const MachineFunction &MF) const {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002400 for (auto &B : MF)
2401 for (auto &I : B)
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002402 if (I.getOpcode() == Hexagon::PS_aligna)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002403 return &I;
2404 return nullptr;
2405}
2406
Krzysztof Parzyszeke8e754d2016-04-25 17:49:44 +00002407/// Adds all callee-saved registers as implicit uses or defs to the
2408/// instruction.
2409void HexagonFrameLowering::addCalleeSaveRegistersAsImpOperand(MachineInstr *MI,
2410 const CSIVect &CSI, bool IsDef, bool IsKill) const {
2411 // Add the callee-saved registers as implicit uses.
2412 for (auto &R : CSI)
2413 MI->addOperand(MachineOperand::CreateReg(R.getReg(), IsDef, true, IsKill));
2414}
2415
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00002416/// Determine whether the callee-saved register saves and restores should
2417/// be generated via inline code. If this function returns "true", inline
2418/// code will be generated. If this function returns "false", additional
2419/// checks are performed, which may still lead to the inline code.
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00002420bool HexagonFrameLowering::shouldInlineCSR(const MachineFunction &MF,
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00002421 const CSIVect &CSI) const {
2422 if (MF.getInfo<HexagonMachineFunctionInfo>()->hasEHReturn())
2423 return true;
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +00002424 if (!hasFP(MF))
2425 return true;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00002426 if (!isOptSize(MF) && !isMinSize(MF))
2427 if (MF.getTarget().getOptLevel() > CodeGenOpt::Default)
2428 return true;
2429
2430 // Check if CSI only has double registers, and if the registers form
2431 // a contiguous block starting from D8.
2432 BitVector Regs(Hexagon::NUM_TARGET_REGS);
2433 for (unsigned i = 0, n = CSI.size(); i < n; ++i) {
2434 unsigned R = CSI[i].getReg();
2435 if (!Hexagon::DoubleRegsRegClass.contains(R))
2436 return true;
2437 Regs[R] = true;
2438 }
2439 int F = Regs.find_first();
2440 if (F != Hexagon::D8)
2441 return true;
2442 while (F >= 0) {
2443 int N = Regs.find_next(F);
2444 if (N >= 0 && N != F+1)
2445 return true;
2446 F = N;
2447 }
2448
2449 return false;
2450}
2451
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00002452bool HexagonFrameLowering::useSpillFunction(const MachineFunction &MF,
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00002453 const CSIVect &CSI) const {
2454 if (shouldInlineCSR(MF, CSI))
2455 return false;
2456 unsigned NumCSI = CSI.size();
2457 if (NumCSI <= 1)
2458 return false;
2459
2460 unsigned Threshold = isOptSize(MF) ? SpillFuncThresholdOs
2461 : SpillFuncThreshold;
2462 return Threshold < NumCSI;
2463}
2464
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00002465bool HexagonFrameLowering::useRestoreFunction(const MachineFunction &MF,
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00002466 const CSIVect &CSI) const {
2467 if (shouldInlineCSR(MF, CSI))
2468 return false;
Krzysztof Parzyszekbb63f662016-03-28 14:52:21 +00002469 // The restore functions do a bit more than just restoring registers.
2470 // The non-returning versions will go back directly to the caller's
2471 // caller, others will clean up the stack frame in preparation for
2472 // a tail call. Using them can still save code size even if only one
2473 // register is getting restores. Make the decision based on -Oz:
2474 // using -Os will use inline restore for a single register.
2475 if (isMinSize(MF))
2476 return true;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00002477 unsigned NumCSI = CSI.size();
Krzysztof Parzyszekbb63f662016-03-28 14:52:21 +00002478 if (NumCSI <= 1)
2479 return false;
2480
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +00002481 unsigned Threshold = isOptSize(MF) ? SpillFuncThresholdOs-1
2482 : SpillFuncThreshold;
2483 return Threshold < NumCSI;
2484}
Krzysztof Parzyszekddafa2c2016-08-01 17:15:30 +00002485
Krzysztof Parzyszekddafa2c2016-08-01 17:15:30 +00002486bool HexagonFrameLowering::mayOverflowFrameOffset(MachineFunction &MF) const {
2487 unsigned StackSize = MF.getFrameInfo().estimateStackSize(MF);
2488 auto &HST = MF.getSubtarget<HexagonSubtarget>();
2489 // A fairly simplistic guess as to whether a potential load/store to a
Krzysztof Parzyszek69ffba42017-06-22 14:11:23 +00002490 // stack location could require an extra register.
2491 if (HST.useHVXOps() && StackSize > 256)
2492 return true;
2493
2494 // Check if the function has store-immediate instructions that access
2495 // the stack. Since the offset field is not extendable, if the stack
2496 // size exceeds the offset limit (6 bits, shifted), the stores will
2497 // require a new base register.
2498 bool HasImmStack = false;
2499 unsigned MinLS = ~0u; // Log_2 of the memory access size.
2500
2501 for (const MachineBasicBlock &B : MF) {
2502 for (const MachineInstr &MI : B) {
2503 unsigned LS = 0;
2504 switch (MI.getOpcode()) {
2505 case Hexagon::S4_storeirit_io:
2506 case Hexagon::S4_storeirif_io:
2507 case Hexagon::S4_storeiri_io:
2508 ++LS;
2509 LLVM_FALLTHROUGH;
2510 case Hexagon::S4_storeirht_io:
2511 case Hexagon::S4_storeirhf_io:
2512 case Hexagon::S4_storeirh_io:
2513 ++LS;
2514 LLVM_FALLTHROUGH;
2515 case Hexagon::S4_storeirbt_io:
2516 case Hexagon::S4_storeirbf_io:
2517 case Hexagon::S4_storeirb_io:
2518 if (MI.getOperand(0).isFI())
2519 HasImmStack = true;
2520 MinLS = std::min(MinLS, LS);
2521 break;
2522 }
2523 }
2524 }
2525
2526 if (HasImmStack)
2527 return !isUInt<6>(StackSize >> MinLS);
2528
Krzysztof Parzyszekddafa2c2016-08-01 17:15:30 +00002529 return false;
2530}