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