Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +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 | |
Craig Topper | b25fda9 | 2012-03-17 18:46:09 +0000 | [diff] [blame] | 11 | #include "HexagonFrameLowering.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 12 | #include "HexagonBlockRanges.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 13 | #include "HexagonInstrInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include "HexagonMachineFunctionInfo.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 15 | #include "HexagonRegisterInfo.h" |
| 16 | #include "HexagonSubtarget.h" |
| 17 | #include "HexagonTargetMachine.h" |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 18 | #include "MCTargetDesc/HexagonBaseInfo.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/BitVector.h" |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseMap.h" |
| 21 | #include "llvm/ADT/None.h" |
| 22 | #include "llvm/ADT/Optional.h" |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/PostOrderIterator.h" |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SetVector.h" |
| 25 | #include "llvm/ADT/SmallSet.h" |
| 26 | #include "llvm/ADT/SmallVector.h" |
Krzysztof Parzyszek | b71085b | 2016-10-21 16:38:29 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/LivePhysRegs.h" |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineDominators.h" |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Benjamin Kramer | ae87d7b | 2012-02-06 10:19:29 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineFunction.h" |
| 32 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineInstr.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/MachineMemOperand.h" |
Benjamin Kramer | ae87d7b | 2012-02-06 10:19:29 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/MachineOperand.h" |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/MachinePostDominators.h" |
Benjamin Kramer | ae87d7b | 2012-02-06 10:19:29 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 40 | #include "llvm/CodeGen/RegisterScavenging.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 42 | #include "llvm/IR/Attributes.h" |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 43 | #include "llvm/IR/DebugLoc.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 44 | #include "llvm/IR/Function.h" |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 45 | #include "llvm/MC/MCDwarf.h" |
| 46 | #include "llvm/MC/MCRegisterInfo.h" |
| 47 | #include "llvm/Pass.h" |
| 48 | #include "llvm/Support/CodeGen.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 49 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Compiler.h" |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 52 | #include "llvm/Support/ErrorHandling.h" |
| 53 | #include "llvm/Support/MathExtras.h" |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 54 | #include "llvm/Support/raw_ostream.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 55 | #include "llvm/Target/TargetMachine.h" |
Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 56 | #include "llvm/Target/TargetOptions.h" |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 57 | #include <algorithm> |
| 58 | #include <cassert> |
| 59 | #include <cstdint> |
| 60 | #include <iterator> |
| 61 | #include <limits> |
| 62 | #include <map> |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 63 | #include <utility> |
| 64 | #include <vector> |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 65 | |
Jakub Kuderski | 34327d2 | 2017-07-13 20:26:45 +0000 | [diff] [blame] | 66 | #define DEBUG_TYPE "hexagon-pei" |
| 67 | |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 68 | // Hexagon stack frame layout as defined by the ABI: |
| 69 | // |
| 70 | // Incoming arguments |
| 71 | // passed via stack |
| 72 | // | |
| 73 | // | |
| 74 | // SP during function's FP during function's | |
| 75 | // +-- runtime (top of stack) runtime (bottom) --+ | |
| 76 | // | | | |
| 77 | // --++---------------------+------------------+-----------------++-+------- |
| 78 | // | parameter area for | variable-size | fixed-size |LR| arg |
| 79 | // | called functions | local objects | local objects |FP| |
| 80 | // --+----------------------+------------------+-----------------+--+------- |
| 81 | // <- size known -> <- size unknown -> <- size known -> |
| 82 | // |
| 83 | // Low address High address |
| 84 | // |
| 85 | // <--- stack growth |
| 86 | // |
| 87 | // |
| 88 | // - In any circumstances, the outgoing function arguments are always accessi- |
| 89 | // ble using the SP, and the incoming arguments are accessible using the FP. |
| 90 | // - If the local objects are not aligned, they can always be accessed using |
| 91 | // the FP. |
| 92 | // - If there are no variable-sized objects, the local objects can always be |
| 93 | // accessed using the SP, regardless whether they are aligned or not. (The |
| 94 | // alignment padding will be at the bottom of the stack (highest address), |
| 95 | // and so the offset with respect to the SP will be known at the compile- |
| 96 | // -time.) |
| 97 | // |
| 98 | // The only complication occurs if there are both, local aligned objects, and |
| 99 | // dynamically allocated (variable-sized) objects. The alignment pad will be |
| 100 | // placed between the FP and the local objects, thus preventing the use of the |
| 101 | // FP to access the local objects. At the same time, the variable-sized objects |
| 102 | // will be between the SP and the local objects, thus introducing an unknown |
| 103 | // distance from the SP to the locals. |
| 104 | // |
| 105 | // To avoid this problem, a new register is created that holds the aligned |
| 106 | // address of the bottom of the stack, referred in the sources as AP (aligned |
| 107 | // pointer). The AP will be equal to "FP-p", where "p" is the smallest pad |
| 108 | // that aligns AP to the required boundary (a maximum of the alignments of |
| 109 | // all stack objects, fixed- and variable-sized). All local objects[1] will |
| 110 | // then use AP as the base pointer. |
| 111 | // [1] The exception is with "fixed" stack objects. "Fixed" stack objects get |
| 112 | // their name from being allocated at fixed locations on the stack, relative |
| 113 | // to the FP. In the presence of dynamic allocation and local alignment, such |
| 114 | // objects can only be accessed through the FP. |
| 115 | // |
| 116 | // Illustration of the AP: |
| 117 | // FP --+ |
| 118 | // | |
| 119 | // ---------------+---------------------+-----+-----------------------++-+-- |
| 120 | // Rest of the | Local stack objects | Pad | Fixed stack objects |LR| |
| 121 | // stack frame | (aligned) | | (CSR, spills, etc.) |FP| |
| 122 | // ---------------+---------------------+-----+-----------------+-----+--+-- |
| 123 | // |<-- Multiple of the -->| |
| 124 | // stack alignment +-- AP |
| 125 | // |
| 126 | // The AP is set up at the beginning of the function. Since it is not a dedi- |
| 127 | // cated (reserved) register, it needs to be kept live throughout the function |
| 128 | // to be available as the base register for local object accesses. |
| 129 | // Normally, an address of a stack objects is obtained by a pseudo-instruction |
Krzysztof Parzyszek | 1d01a79 | 2016-08-16 18:08:40 +0000 | [diff] [blame] | 130 | // PS_fi. To access local objects with the AP register present, a different |
| 131 | // pseudo-instruction needs to be used: PS_fia. The PS_fia takes one extra |
| 132 | // argument compared to PS_fi: the first input register is the AP register. |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 133 | // This keeps the register live between its definition and its uses. |
| 134 | |
Krzysztof Parzyszek | 1d01a79 | 2016-08-16 18:08:40 +0000 | [diff] [blame] | 135 | // The AP register is originally set up using pseudo-instruction PS_aligna: |
| 136 | // AP = PS_aligna A |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 137 | // where |
| 138 | // A - required stack alignment |
| 139 | // The alignment value must be the maximum of all alignments required by |
| 140 | // any stack object. |
| 141 | |
Krzysztof Parzyszek | 1d01a79 | 2016-08-16 18:08:40 +0000 | [diff] [blame] | 142 | // The dynamic allocation uses a pseudo-instruction PS_alloca: |
| 143 | // Rd = PS_alloca Rs, A |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 144 | // where |
| 145 | // Rd - address of the allocated space |
| 146 | // Rs - minimum size (the actual allocated can be larger to accommodate |
| 147 | // alignment) |
| 148 | // A - required alignment |
| 149 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 150 | using namespace llvm; |
| 151 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 152 | static cl::opt<bool> DisableDeallocRet("disable-hexagon-dealloc-ret", |
| 153 | cl::Hidden, cl::desc("Disable Dealloc Return for Hexagon target")); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 154 | |
Krzysztof Parzyszek | a5bd2954 | 2016-05-16 18:02:28 +0000 | [diff] [blame] | 155 | static cl::opt<unsigned> NumberScavengerSlots("number-scavenger-slots", |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 156 | cl::Hidden, cl::desc("Set the number of scavenger slots"), cl::init(2), |
| 157 | cl::ZeroOrMore); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 158 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 159 | static cl::opt<int> SpillFuncThreshold("spill-func-threshold", |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 160 | cl::Hidden, cl::desc("Specify O2(not Os) spill func threshold"), |
| 161 | cl::init(6), cl::ZeroOrMore); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 162 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 163 | static cl::opt<int> SpillFuncThresholdOs("spill-func-threshold-Os", |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 164 | cl::Hidden, cl::desc("Specify Os spill func threshold"), |
| 165 | cl::init(1), cl::ZeroOrMore); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 166 | |
Krzysztof Parzyszek | c9d4caa | 2016-03-24 20:20:07 +0000 | [diff] [blame] | 167 | static cl::opt<bool> EnableStackOVFSanitizer("enable-stackovf-sanitizer", |
| 168 | cl::Hidden, cl::desc("Enable runtime checks for stack overflow."), |
| 169 | cl::init(false), cl::ZeroOrMore); |
| 170 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 171 | static cl::opt<bool> EnableShrinkWrapping("hexagon-shrink-frame", |
| 172 | cl::init(true), cl::Hidden, cl::ZeroOrMore, |
| 173 | cl::desc("Enable stack frame shrink wrapping")); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 174 | |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 175 | static cl::opt<unsigned> ShrinkLimit("shrink-frame-limit", |
| 176 | cl::init(std::numeric_limits<unsigned>::max()), cl::Hidden, cl::ZeroOrMore, |
| 177 | cl::desc("Max count of stack frame shrink-wraps")); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 178 | |
Krzysztof Parzyszek | 080bebd | 2016-07-25 14:42:11 +0000 | [diff] [blame] | 179 | static cl::opt<bool> EnableSaveRestoreLong("enable-save-restore-long", |
| 180 | cl::Hidden, cl::desc("Enable long calls for save-restore stubs."), |
| 181 | cl::init(false), cl::ZeroOrMore); |
| 182 | |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 183 | static cl::opt<bool> EliminateFramePointer("hexagon-fp-elim", cl::init(true), |
| 184 | cl::Hidden, cl::desc("Refrain from using FP whenever possible")); |
Krzysztof Parzyszek | 23920ec | 2015-10-19 18:30:27 +0000 | [diff] [blame] | 185 | |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 186 | static cl::opt<bool> OptimizeSpillSlots("hexagon-opt-spill", cl::Hidden, |
| 187 | cl::init(true), cl::desc("Optimize spill slots")); |
| 188 | |
Krzysztof Parzyszek | dc42164 | 2016-07-27 20:58:43 +0000 | [diff] [blame] | 189 | #ifndef NDEBUG |
| 190 | static cl::opt<unsigned> SpillOptMax("spill-opt-max", cl::Hidden, |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 191 | cl::init(std::numeric_limits<unsigned>::max())); |
Krzysztof Parzyszek | dc42164 | 2016-07-27 20:58:43 +0000 | [diff] [blame] | 192 | static unsigned SpillOptCount = 0; |
| 193 | #endif |
| 194 | |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 195 | namespace llvm { |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 196 | |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 197 | void initializeHexagonCallFrameInformationPass(PassRegistry&); |
| 198 | FunctionPass *createHexagonCallFrameInformation(); |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 199 | |
| 200 | } // end namespace llvm |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 201 | |
| 202 | namespace { |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 203 | |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 204 | class HexagonCallFrameInformation : public MachineFunctionPass { |
| 205 | public: |
| 206 | static char ID; |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 207 | |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 208 | HexagonCallFrameInformation() : MachineFunctionPass(ID) { |
| 209 | PassRegistry &PR = *PassRegistry::getPassRegistry(); |
| 210 | initializeHexagonCallFrameInformationPass(PR); |
| 211 | } |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 212 | |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 213 | bool runOnMachineFunction(MachineFunction &MF) override; |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 214 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 215 | MachineFunctionProperties getRequiredProperties() const override { |
| 216 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 217 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 218 | } |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | char HexagonCallFrameInformation::ID = 0; |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 222 | |
| 223 | } // end anonymous namespace |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 224 | |
| 225 | bool HexagonCallFrameInformation::runOnMachineFunction(MachineFunction &MF) { |
| 226 | auto &HFI = *MF.getSubtarget<HexagonSubtarget>().getFrameLowering(); |
| 227 | bool NeedCFI = MF.getMMI().hasDebugInfo() || |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 228 | MF.getFunction().needsUnwindTableEntry(); |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 229 | |
| 230 | if (!NeedCFI) |
| 231 | return false; |
| 232 | HFI.insertCFIInstructions(MF); |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | INITIALIZE_PASS(HexagonCallFrameInformation, "hexagon-cfi", |
| 237 | "Hexagon call frame information", false, false) |
| 238 | |
| 239 | FunctionPass *llvm::createHexagonCallFrameInformation() { |
| 240 | return new HexagonCallFrameInformation(); |
| 241 | } |
| 242 | |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 243 | /// Map a register pair Reg to the subregister that has the greater "number", |
| 244 | /// i.e. D3 (aka R7:6) will be mapped to R7, etc. |
| 245 | static unsigned getMax32BitSubRegister(unsigned Reg, |
| 246 | const TargetRegisterInfo &TRI, |
| 247 | bool hireg = true) { |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 248 | if (Reg < Hexagon::D0 || Reg > Hexagon::D15) |
| 249 | return Reg; |
| 250 | |
| 251 | unsigned RegNo = 0; |
| 252 | for (MCSubRegIterator SubRegs(Reg, &TRI); SubRegs.isValid(); ++SubRegs) { |
| 253 | if (hireg) { |
| 254 | if (*SubRegs > RegNo) |
| 255 | RegNo = *SubRegs; |
| 256 | } else { |
| 257 | if (!RegNo || *SubRegs < RegNo) |
| 258 | RegNo = *SubRegs; |
| 259 | } |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 260 | } |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 261 | return RegNo; |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 262 | } |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 263 | |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 264 | /// Returns the callee saved register with the largest id in the vector. |
| 265 | static unsigned getMaxCalleeSavedReg(const std::vector<CalleeSavedInfo> &CSI, |
| 266 | const TargetRegisterInfo &TRI) { |
Benjamin Kramer | 3e9a5d3 | 2016-05-27 11:36:04 +0000 | [diff] [blame] | 267 | static_assert(Hexagon::R1 > 0, |
| 268 | "Assume physical registers are encoded as positive integers"); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 269 | if (CSI.empty()) |
| 270 | return 0; |
| 271 | |
| 272 | unsigned Max = getMax32BitSubRegister(CSI[0].getReg(), TRI); |
| 273 | for (unsigned I = 1, E = CSI.size(); I < E; ++I) { |
| 274 | unsigned Reg = getMax32BitSubRegister(CSI[I].getReg(), TRI); |
| 275 | if (Reg > Max) |
| 276 | Max = Reg; |
| 277 | } |
| 278 | return Max; |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 279 | } |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 280 | |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 281 | /// Checks if the basic block contains any instruction that needs a stack |
| 282 | /// frame to be already in place. |
| 283 | static bool needsStackFrame(const MachineBasicBlock &MBB, const BitVector &CSR, |
| 284 | const HexagonRegisterInfo &HRI) { |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 285 | for (auto &I : MBB) { |
| 286 | const MachineInstr *MI = &I; |
| 287 | if (MI->isCall()) |
| 288 | return true; |
| 289 | unsigned Opc = MI->getOpcode(); |
| 290 | switch (Opc) { |
Krzysztof Parzyszek | 1d01a79 | 2016-08-16 18:08:40 +0000 | [diff] [blame] | 291 | case Hexagon::PS_alloca: |
| 292 | case Hexagon::PS_aligna: |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 293 | return true; |
| 294 | default: |
| 295 | break; |
| 296 | } |
| 297 | // Check individual operands. |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 298 | for (const MachineOperand &MO : MI->operands()) { |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 299 | // While the presence of a frame index does not prove that a stack |
| 300 | // frame will be required, all frame indexes should be within alloc- |
| 301 | // frame/deallocframe. Otherwise, the code that translates a frame |
| 302 | // index into an offset would have to be aware of the placement of |
| 303 | // the frame creation/destruction instructions. |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 304 | if (MO.isFI()) |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 305 | return true; |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 306 | if (MO.isReg()) { |
| 307 | unsigned R = MO.getReg(); |
| 308 | // Virtual registers will need scavenging, which then may require |
| 309 | // a stack slot. |
| 310 | if (TargetRegisterInfo::isVirtualRegister(R)) |
Rafael Espindola | 6eab404 | 2017-02-17 02:08:58 +0000 | [diff] [blame] | 311 | return true; |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 312 | for (MCSubRegIterator S(R, &HRI, true); S.isValid(); ++S) |
| 313 | if (CSR[*S]) |
| 314 | return true; |
| 315 | continue; |
| 316 | } |
| 317 | if (MO.isRegMask()) { |
| 318 | // A regmask would normally have all callee-saved registers marked |
| 319 | // as preserved, so this check would not be needed, but in case of |
| 320 | // ever having other regmasks (for other calling conventions), |
| 321 | // make sure they would be processed correctly. |
| 322 | const uint32_t *BM = MO.getRegMask(); |
| 323 | for (int x = CSR.find_first(); x >= 0; x = CSR.find_next(x)) { |
| 324 | unsigned R = x; |
| 325 | // If this regmask does not preserve a CSR, a frame will be needed. |
| 326 | if (!(BM[R/32] & (1u << (R%32)))) |
| 327 | return true; |
| 328 | } |
| 329 | } |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | return false; |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 333 | } |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 334 | |
| 335 | /// Returns true if MBB has a machine instructions that indicates a tail call |
| 336 | /// in the block. |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 337 | static bool hasTailCall(const MachineBasicBlock &MBB) { |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 338 | MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr(); |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 339 | if (I == MBB.end()) |
| 340 | return false; |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 341 | unsigned RetOpc = I->getOpcode(); |
Krzysztof Parzyszek | be976d4 | 2016-08-12 11:12:02 +0000 | [diff] [blame] | 342 | return RetOpc == Hexagon::PS_tailcall_i || RetOpc == Hexagon::PS_tailcall_r; |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 343 | } |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 344 | |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 345 | /// Returns true if MBB contains an instruction that returns. |
| 346 | static bool hasReturn(const MachineBasicBlock &MBB) { |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 347 | for (auto I = MBB.getFirstTerminator(), E = MBB.end(); I != E; ++I) |
| 348 | if (I->isReturn()) |
| 349 | return true; |
| 350 | return false; |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 351 | } |
Krzysztof Parzyszek | a34901a | 2016-03-28 14:42:03 +0000 | [diff] [blame] | 352 | |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 353 | /// Returns the "return" instruction from this block, or nullptr if there |
| 354 | /// isn't any. |
| 355 | static MachineInstr *getReturn(MachineBasicBlock &MBB) { |
Krzysztof Parzyszek | da0b9a9 | 2016-05-26 19:44:28 +0000 | [diff] [blame] | 356 | for (auto &I : MBB) |
| 357 | if (I.isReturn()) |
| 358 | return &I; |
| 359 | return nullptr; |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 360 | } |
Krzysztof Parzyszek | da0b9a9 | 2016-05-26 19:44:28 +0000 | [diff] [blame] | 361 | |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 362 | static bool isRestoreCall(unsigned Opc) { |
Krzysztof Parzyszek | da0b9a9 | 2016-05-26 19:44:28 +0000 | [diff] [blame] | 363 | switch (Opc) { |
| 364 | case Hexagon::RESTORE_DEALLOC_RET_JMP_V4: |
| 365 | case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC: |
Krzysztof Parzyszek | fae7986 | 2016-07-27 18:47:25 +0000 | [diff] [blame] | 366 | case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_EXT: |
| 367 | case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_EXT_PIC: |
| 368 | case Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT: |
| 369 | case Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT_PIC: |
Krzysztof Parzyszek | da0b9a9 | 2016-05-26 19:44:28 +0000 | [diff] [blame] | 370 | case Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4: |
| 371 | case Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_PIC: |
| 372 | return true; |
| 373 | } |
| 374 | return false; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 375 | } |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 376 | |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 377 | static inline bool isOptNone(const MachineFunction &MF) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 378 | return MF.getFunction().hasFnAttribute(Attribute::OptimizeNone) || |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 379 | MF.getTarget().getOptLevel() == CodeGenOpt::None; |
| 380 | } |
| 381 | |
| 382 | static inline bool isOptSize(const MachineFunction &MF) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 383 | const Function &F = MF.getFunction(); |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 384 | return F.optForSize() && !F.optForMinSize(); |
| 385 | } |
| 386 | |
| 387 | static inline bool isMinSize(const MachineFunction &MF) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 388 | return MF.getFunction().optForMinSize(); |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 389 | } |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 390 | |
| 391 | /// Implements shrink-wrapping of the stack frame. By default, stack frame |
| 392 | /// is created in the function entry block, and is cleaned up in every block |
| 393 | /// that returns. This function finds alternate blocks: one for the frame |
| 394 | /// setup (prolog) and one for the cleanup (epilog). |
| 395 | void HexagonFrameLowering::findShrunkPrologEpilog(MachineFunction &MF, |
| 396 | MachineBasicBlock *&PrologB, MachineBasicBlock *&EpilogB) const { |
| 397 | static unsigned ShrinkCounter = 0; |
| 398 | |
| 399 | if (ShrinkLimit.getPosition()) { |
| 400 | if (ShrinkCounter >= ShrinkLimit) |
| 401 | return; |
| 402 | ShrinkCounter++; |
| 403 | } |
| 404 | |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 405 | auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo(); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 406 | |
| 407 | MachineDominatorTree MDT; |
| 408 | MDT.runOnMachineFunction(MF); |
| 409 | MachinePostDominatorTree MPT; |
| 410 | MPT.runOnMachineFunction(MF); |
| 411 | |
Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 412 | using UnsignedMap = DenseMap<unsigned, unsigned>; |
| 413 | using RPOTType = ReversePostOrderTraversal<const MachineFunction *>; |
| 414 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 415 | UnsignedMap RPO; |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 416 | RPOTType RPOT(&MF); |
| 417 | unsigned RPON = 0; |
| 418 | for (RPOTType::rpo_iterator I = RPOT.begin(), E = RPOT.end(); I != E; ++I) |
| 419 | RPO[(*I)->getNumber()] = RPON++; |
| 420 | |
| 421 | // Don't process functions that have loops, at least for now. Placement |
| 422 | // of prolog and epilog must take loop structure into account. For simpli- |
| 423 | // city don't do it right now. |
| 424 | for (auto &I : MF) { |
| 425 | unsigned BN = RPO[I.getNumber()]; |
| 426 | for (auto SI = I.succ_begin(), SE = I.succ_end(); SI != SE; ++SI) { |
| 427 | // If found a back-edge, return. |
| 428 | if (RPO[(*SI)->getNumber()] <= BN) |
| 429 | return; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | // Collect the set of blocks that need a stack frame to execute. Scan |
| 434 | // each block for uses/defs of callee-saved registers, calls, etc. |
| 435 | SmallVector<MachineBasicBlock*,16> SFBlocks; |
| 436 | BitVector CSR(Hexagon::NUM_TARGET_REGS); |
| 437 | for (const MCPhysReg *P = HRI.getCalleeSavedRegs(&MF); *P; ++P) |
Krzysztof Parzyszek | 01598de | 2016-03-24 20:31:41 +0000 | [diff] [blame] | 438 | for (MCSubRegIterator S(*P, &HRI, true); S.isValid(); ++S) |
| 439 | CSR[*S] = true; |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 440 | |
| 441 | for (auto &I : MF) |
Krzysztof Parzyszek | 01598de | 2016-03-24 20:31:41 +0000 | [diff] [blame] | 442 | if (needsStackFrame(I, CSR, HRI)) |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 443 | SFBlocks.push_back(&I); |
| 444 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 445 | LLVM_DEBUG({ |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 446 | dbgs() << "Blocks needing SF: {"; |
| 447 | for (auto &B : SFBlocks) |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 448 | dbgs() << " " << printMBBReference(*B); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 449 | dbgs() << " }\n"; |
| 450 | }); |
| 451 | // No frame needed? |
| 452 | if (SFBlocks.empty()) |
| 453 | return; |
| 454 | |
| 455 | // Pick a common dominator and a common post-dominator. |
| 456 | MachineBasicBlock *DomB = SFBlocks[0]; |
| 457 | for (unsigned i = 1, n = SFBlocks.size(); i < n; ++i) { |
| 458 | DomB = MDT.findNearestCommonDominator(DomB, SFBlocks[i]); |
| 459 | if (!DomB) |
| 460 | break; |
| 461 | } |
| 462 | MachineBasicBlock *PDomB = SFBlocks[0]; |
| 463 | for (unsigned i = 1, n = SFBlocks.size(); i < n; ++i) { |
| 464 | PDomB = MPT.findNearestCommonDominator(PDomB, SFBlocks[i]); |
| 465 | if (!PDomB) |
| 466 | break; |
| 467 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 468 | LLVM_DEBUG({ |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 469 | dbgs() << "Computed dom block: "; |
| 470 | if (DomB) |
| 471 | dbgs() << printMBBReference(*DomB); |
| 472 | else |
| 473 | dbgs() << "<null>"; |
| 474 | dbgs() << ", computed pdom block: "; |
| 475 | if (PDomB) |
| 476 | dbgs() << printMBBReference(*PDomB); |
| 477 | else |
| 478 | dbgs() << "<null>"; |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 479 | dbgs() << "\n"; |
| 480 | }); |
| 481 | if (!DomB || !PDomB) |
| 482 | return; |
| 483 | |
| 484 | // Make sure that DomB dominates PDomB and PDomB post-dominates DomB. |
| 485 | if (!MDT.dominates(DomB, PDomB)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 486 | LLVM_DEBUG(dbgs() << "Dom block does not dominate pdom block\n"); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 487 | return; |
| 488 | } |
| 489 | if (!MPT.dominates(PDomB, DomB)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 490 | LLVM_DEBUG(dbgs() << "PDom block does not post-dominate dom block\n"); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 491 | return; |
| 492 | } |
| 493 | |
| 494 | // Finally, everything seems right. |
| 495 | PrologB = DomB; |
| 496 | EpilogB = PDomB; |
| 497 | } |
| 498 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 499 | /// Perform most of the PEI work here: |
| 500 | /// - saving/restoring of the callee-saved registers, |
| 501 | /// - stack frame creation and destruction. |
| 502 | /// Normally, this work is distributed among various functions, but doing it |
| 503 | /// in one place allows shrink-wrapping of the stack frame. |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 504 | void HexagonFrameLowering::emitPrologue(MachineFunction &MF, |
| 505 | MachineBasicBlock &MBB) const { |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 506 | auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo(); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 507 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 508 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 509 | const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo(); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 510 | |
| 511 | MachineBasicBlock *PrologB = &MF.front(), *EpilogB = nullptr; |
| 512 | if (EnableShrinkWrapping) |
| 513 | findShrunkPrologEpilog(MF, PrologB, EpilogB); |
| 514 | |
Krzysztof Parzyszek | c9d4caa | 2016-03-24 20:20:07 +0000 | [diff] [blame] | 515 | bool PrologueStubs = false; |
| 516 | insertCSRSpillsInBlock(*PrologB, CSI, HRI, PrologueStubs); |
| 517 | insertPrologueInBlock(*PrologB, PrologueStubs); |
Krzysztof Parzyszek | a34d639 | 2016-07-27 16:26:39 +0000 | [diff] [blame] | 518 | updateEntryPaths(MF, *PrologB); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 519 | |
| 520 | if (EpilogB) { |
| 521 | insertCSRRestoresInBlock(*EpilogB, CSI, HRI); |
| 522 | insertEpilogueInBlock(*EpilogB); |
| 523 | } else { |
| 524 | for (auto &B : MF) |
Matthias Braun | c2d4bef | 2015-09-25 21:25:19 +0000 | [diff] [blame] | 525 | if (B.isReturnBlock()) |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 526 | insertCSRRestoresInBlock(B, CSI, HRI); |
| 527 | |
| 528 | for (auto &B : MF) |
Matthias Braun | c2d4bef | 2015-09-25 21:25:19 +0000 | [diff] [blame] | 529 | if (B.isReturnBlock()) |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 530 | insertEpilogueInBlock(B); |
Krzysztof Parzyszek | da0b9a9 | 2016-05-26 19:44:28 +0000 | [diff] [blame] | 531 | |
| 532 | for (auto &B : MF) { |
| 533 | if (B.empty()) |
| 534 | continue; |
| 535 | MachineInstr *RetI = getReturn(B); |
| 536 | if (!RetI || isRestoreCall(RetI->getOpcode())) |
| 537 | continue; |
| 538 | for (auto &R : CSI) |
| 539 | RetI->addOperand(MachineOperand::CreateReg(R.getReg(), false, true)); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | if (EpilogB) { |
| 544 | // If there is an epilog block, it may not have a return instruction. |
| 545 | // In such case, we need to add the callee-saved registers as live-ins |
| 546 | // in all blocks on all paths from the epilog to any return block. |
Krzysztof Parzyszek | a34d639 | 2016-07-27 16:26:39 +0000 | [diff] [blame] | 547 | unsigned MaxBN = MF.getNumBlockIDs(); |
Krzysztof Parzyszek | da0b9a9 | 2016-05-26 19:44:28 +0000 | [diff] [blame] | 548 | BitVector DoneT(MaxBN+1), DoneF(MaxBN+1), Path(MaxBN+1); |
Krzysztof Parzyszek | a34d639 | 2016-07-27 16:26:39 +0000 | [diff] [blame] | 549 | updateExitPaths(*EpilogB, *EpilogB, DoneT, DoneF, Path); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
Brendon Cahoon | ac8fed6 | 2018-11-09 18:16:24 +0000 | [diff] [blame] | 553 | /// Returns true if the target can safely skip saving callee-saved registers |
| 554 | /// for noreturn nounwind functions. |
| 555 | bool HexagonFrameLowering::enableCalleeSaveSkip( |
| 556 | const MachineFunction &MF) const { |
| 557 | const auto &F = MF.getFunction(); |
| 558 | assert(F.hasFnAttribute(Attribute::NoReturn) && |
| 559 | F.getFunction().hasFnAttribute(Attribute::NoUnwind) && |
| 560 | !F.getFunction().hasFnAttribute(Attribute::UWTable)); |
Fangrui Song | 60b7fb4 | 2018-11-09 19:24:48 +0000 | [diff] [blame^] | 561 | (void)F; |
Brendon Cahoon | ac8fed6 | 2018-11-09 18:16:24 +0000 | [diff] [blame] | 562 | |
| 563 | // No need to save callee saved registers if the function does not return. |
| 564 | return MF.getSubtarget<HexagonSubtarget>().noreturnStackElim(); |
| 565 | } |
| 566 | |
| 567 | // Helper function used to determine when to eliminate the stack frame for |
| 568 | // functions marked as noreturn and when the noreturn-stack-elim options are |
| 569 | // specified. When both these conditions are true, then a FP may not be needed |
| 570 | // if the function makes a call. It is very similar to enableCalleeSaveSkip, |
| 571 | // but it used to check if the allocframe can be eliminated as well. |
| 572 | static bool enableAllocFrameElim(const MachineFunction &MF) { |
| 573 | const auto &F = MF.getFunction(); |
| 574 | const auto &MFI = MF.getFrameInfo(); |
| 575 | const auto &HST = MF.getSubtarget<HexagonSubtarget>(); |
| 576 | assert(!MFI.hasVarSizedObjects() && |
| 577 | !HST.getRegisterInfo()->needsStackRealignment(MF)); |
| 578 | return F.hasFnAttribute(Attribute::NoReturn) && |
| 579 | F.hasFnAttribute(Attribute::NoUnwind) && |
| 580 | !F.hasFnAttribute(Attribute::UWTable) && HST.noreturnStackElim() && |
| 581 | MFI.getStackSize() == 0; |
| 582 | } |
| 583 | |
Krzysztof Parzyszek | c9d4caa | 2016-03-24 20:20:07 +0000 | [diff] [blame] | 584 | void HexagonFrameLowering::insertPrologueInBlock(MachineBasicBlock &MBB, |
| 585 | bool PrologueStubs) const { |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 586 | MachineFunction &MF = *MBB.getParent(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 587 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 588 | auto &HST = MF.getSubtarget<HexagonSubtarget>(); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 589 | auto &HII = *HST.getInstrInfo(); |
| 590 | auto &HRI = *HST.getRegisterInfo(); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 591 | |
Krzysztof Parzyszek | 7170217 | 2017-06-23 19:47:04 +0000 | [diff] [blame] | 592 | unsigned MaxAlign = std::max(MFI.getMaxAlignment(), getStackAlignment()); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 593 | |
| 594 | // Calculate the total stack frame size. |
| 595 | // Get the number of bytes to allocate from the FrameInfo. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 596 | unsigned FrameSize = MFI.getStackSize(); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 597 | // Round up the max call frame size to the max alignment on the stack. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 598 | unsigned MaxCFA = alignTo(MFI.getMaxCallFrameSize(), MaxAlign); |
| 599 | MFI.setMaxCallFrameSize(MaxCFA); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 600 | |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 601 | FrameSize = MaxCFA + alignTo(FrameSize, MaxAlign); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 602 | MFI.setStackSize(FrameSize); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 603 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 604 | bool AlignStack = (MaxAlign > getStackAlignment()); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 605 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 606 | // Get the number of bytes to allocate from the FrameInfo. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 607 | unsigned NumBytes = MFI.getStackSize(); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 608 | unsigned SP = HRI.getStackRegister(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 609 | unsigned MaxCF = MFI.getMaxCallFrameSize(); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 610 | MachineBasicBlock::iterator InsertPt = MBB.begin(); |
| 611 | |
Krzysztof Parzyszek | 8849a51 | 2016-08-19 18:46:13 +0000 | [diff] [blame] | 612 | SmallVector<MachineInstr *, 4> AdjustRegs; |
| 613 | for (auto &MBB : MF) |
| 614 | for (auto &MI : MBB) |
| 615 | if (MI.getOpcode() == Hexagon::PS_alloca) |
| 616 | AdjustRegs.push_back(&MI); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 617 | |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 618 | for (auto MI : AdjustRegs) { |
Krzysztof Parzyszek | 1d01a79 | 2016-08-16 18:08:40 +0000 | [diff] [blame] | 619 | assert((MI->getOpcode() == Hexagon::PS_alloca) && "Expected alloca"); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 620 | expandAlloca(MI, HII, SP, MaxCF); |
| 621 | MI->eraseFromParent(); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 624 | DebugLoc dl = MBB.findDebugLoc(InsertPt); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 625 | |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 626 | if (hasFP(MF)) { |
| 627 | insertAllocframe(MBB, InsertPt, NumBytes); |
| 628 | if (AlignStack) { |
| 629 | BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_andir), SP) |
| 630 | .addReg(SP) |
| 631 | .addImm(-int64_t(MaxAlign)); |
| 632 | } |
| 633 | // If the stack-checking is enabled, and we spilled the callee-saved |
| 634 | // registers inline (i.e. did not use a spill function), then call |
| 635 | // the stack checker directly. |
| 636 | if (EnableStackOVFSanitizer && !PrologueStubs) |
| 637 | BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::PS_call_stk)) |
| 638 | .addExternalSymbol("__runtime_stack_check"); |
| 639 | } else if (NumBytes > 0) { |
| 640 | assert(alignTo(NumBytes, 8) == NumBytes); |
| 641 | BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_addi), SP) |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 642 | .addReg(SP) |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 643 | .addImm(-int(NumBytes)); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 646 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 647 | void HexagonFrameLowering::insertEpilogueInBlock(MachineBasicBlock &MBB) const { |
| 648 | MachineFunction &MF = *MBB.getParent(); |
Krzysztof Parzyszek | 080bebd | 2016-07-25 14:42:11 +0000 | [diff] [blame] | 649 | auto &HST = MF.getSubtarget<HexagonSubtarget>(); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 650 | auto &HII = *HST.getInstrInfo(); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 651 | auto &HRI = *HST.getRegisterInfo(); |
| 652 | unsigned SP = HRI.getStackRegister(); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 653 | |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 654 | MachineBasicBlock::iterator InsertPt = MBB.getFirstTerminator(); |
| 655 | DebugLoc dl = MBB.findDebugLoc(InsertPt); |
| 656 | |
| 657 | if (!hasFP(MF)) { |
| 658 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 659 | if (unsigned NumBytes = MFI.getStackSize()) { |
| 660 | BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_addi), SP) |
| 661 | .addReg(SP) |
| 662 | .addImm(NumBytes); |
| 663 | } |
| 664 | return; |
| 665 | } |
| 666 | |
Krzysztof Parzyszek | da0b9a9 | 2016-05-26 19:44:28 +0000 | [diff] [blame] | 667 | MachineInstr *RetI = getReturn(MBB); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 668 | unsigned RetOpc = RetI ? RetI->getOpcode() : 0; |
| 669 | |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 670 | // Handle EH_RETURN. |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 671 | if (RetOpc == Hexagon::EH_RETURN_JMPR) { |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 672 | BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::L2_deallocframe)) |
| 673 | .addDef(Hexagon::D15) |
| 674 | .addReg(Hexagon::R30); |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 675 | BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_add), SP) |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 676 | .addReg(SP) |
| 677 | .addReg(Hexagon::R28); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 678 | return; |
| 679 | } |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 680 | |
| 681 | // Check for RESTORE_DEALLOC_RET* tail call. Don't emit an extra dealloc- |
| 682 | // frame instruction if we encounter it. |
Krzysztof Parzyszek | 181fdbd | 2016-03-24 19:18:48 +0000 | [diff] [blame] | 683 | if (RetOpc == Hexagon::RESTORE_DEALLOC_RET_JMP_V4 || |
Krzysztof Parzyszek | fae7986 | 2016-07-27 18:47:25 +0000 | [diff] [blame] | 684 | RetOpc == Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC || |
| 685 | RetOpc == Hexagon::RESTORE_DEALLOC_RET_JMP_V4_EXT || |
| 686 | RetOpc == Hexagon::RESTORE_DEALLOC_RET_JMP_V4_EXT_PIC) { |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 687 | MachineBasicBlock::iterator It = RetI; |
| 688 | ++It; |
| 689 | // Delete all instructions after the RESTORE (except labels). |
| 690 | while (It != MBB.end()) { |
| 691 | if (!It->isLabel()) |
| 692 | It = MBB.erase(It); |
| 693 | else |
| 694 | ++It; |
Jyotsna Verma | 300f0b9 | 2013-05-10 20:27:34 +0000 | [diff] [blame] | 695 | } |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 696 | return; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 697 | } |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 698 | |
| 699 | // It is possible that the restoring code is a call to a library function. |
| 700 | // All of the restore* functions include "deallocframe", so we need to make |
| 701 | // sure that we don't add an extra one. |
| 702 | bool NeedsDeallocframe = true; |
| 703 | if (!MBB.empty() && InsertPt != MBB.begin()) { |
| 704 | MachineBasicBlock::iterator PrevIt = std::prev(InsertPt); |
| 705 | unsigned COpc = PrevIt->getOpcode(); |
Krzysztof Parzyszek | 181fdbd | 2016-03-24 19:18:48 +0000 | [diff] [blame] | 706 | if (COpc == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4 || |
Krzysztof Parzyszek | fae7986 | 2016-07-27 18:47:25 +0000 | [diff] [blame] | 707 | COpc == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_PIC || |
| 708 | COpc == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT || |
| 709 | COpc == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT_PIC || |
Krzysztof Parzyszek | be976d4 | 2016-08-12 11:12:02 +0000 | [diff] [blame] | 710 | COpc == Hexagon::PS_call_nr || COpc == Hexagon::PS_callr_nr) |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 711 | NeedsDeallocframe = false; |
| 712 | } |
| 713 | |
| 714 | if (!NeedsDeallocframe) |
| 715 | return; |
Krzysztof Parzyszek | be976d4 | 2016-08-12 11:12:02 +0000 | [diff] [blame] | 716 | // If the returning instruction is PS_jmpret, replace it with dealloc_return, |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 717 | // otherwise just add deallocframe. The function could be returning via a |
| 718 | // tail call. |
Krzysztof Parzyszek | be976d4 | 2016-08-12 11:12:02 +0000 | [diff] [blame] | 719 | if (RetOpc != Hexagon::PS_jmpret || DisableDeallocRet) { |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 720 | BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::L2_deallocframe)) |
| 721 | .addDef(Hexagon::D15) |
| 722 | .addReg(Hexagon::R30); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 723 | return; |
| 724 | } |
| 725 | unsigned NewOpc = Hexagon::L4_return; |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 726 | MachineInstr *NewI = BuildMI(MBB, RetI, dl, HII.get(NewOpc)) |
| 727 | .addDef(Hexagon::D15) |
| 728 | .addReg(Hexagon::R30); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 729 | // Transfer the function live-out registers. |
Duncan P. N. Exon Smith | fd8cc23 | 2016-02-27 20:01:33 +0000 | [diff] [blame] | 730 | NewI->copyImplicitOps(MF, *RetI); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 731 | MBB.erase(RetI); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 734 | void HexagonFrameLowering::insertAllocframe(MachineBasicBlock &MBB, |
| 735 | MachineBasicBlock::iterator InsertPt, unsigned NumBytes) const { |
| 736 | MachineFunction &MF = *MBB.getParent(); |
| 737 | auto &HST = MF.getSubtarget<HexagonSubtarget>(); |
| 738 | auto &HII = *HST.getInstrInfo(); |
| 739 | auto &HRI = *HST.getRegisterInfo(); |
| 740 | |
| 741 | // Check for overflow. |
| 742 | // Hexagon_TODO: Ugh! hardcoding. Is there an API that can be used? |
| 743 | const unsigned int ALLOCFRAME_MAX = 16384; |
| 744 | |
| 745 | // Create a dummy memory operand to avoid allocframe from being treated as |
| 746 | // a volatile memory reference. |
| 747 | auto *MMO = MF.getMachineMemOperand(MachinePointerInfo::getStack(MF, 0), |
| 748 | MachineMemOperand::MOStore, 4, 4); |
| 749 | |
| 750 | DebugLoc dl = MBB.findDebugLoc(InsertPt); |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 751 | unsigned SP = HRI.getStackRegister(); |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 752 | |
| 753 | if (NumBytes >= ALLOCFRAME_MAX) { |
| 754 | // Emit allocframe(#0). |
| 755 | BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::S2_allocframe)) |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 756 | .addDef(SP) |
| 757 | .addReg(SP) |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 758 | .addImm(0) |
| 759 | .addMemOperand(MMO); |
| 760 | |
| 761 | // Subtract the size from the stack pointer. |
| 762 | unsigned SP = HRI.getStackRegister(); |
| 763 | BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_addi), SP) |
| 764 | .addReg(SP) |
| 765 | .addImm(-int(NumBytes)); |
| 766 | } else { |
| 767 | BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::S2_allocframe)) |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 768 | .addDef(SP) |
| 769 | .addReg(SP) |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 770 | .addImm(NumBytes) |
| 771 | .addMemOperand(MMO); |
| 772 | } |
| 773 | } |
| 774 | |
Krzysztof Parzyszek | a34d639 | 2016-07-27 16:26:39 +0000 | [diff] [blame] | 775 | void HexagonFrameLowering::updateEntryPaths(MachineFunction &MF, |
| 776 | MachineBasicBlock &SaveB) const { |
| 777 | SetVector<unsigned> Worklist; |
| 778 | |
| 779 | MachineBasicBlock &EntryB = MF.front(); |
| 780 | Worklist.insert(EntryB.getNumber()); |
| 781 | |
| 782 | unsigned SaveN = SaveB.getNumber(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 783 | auto &CSI = MF.getFrameInfo().getCalleeSavedInfo(); |
Krzysztof Parzyszek | a34d639 | 2016-07-27 16:26:39 +0000 | [diff] [blame] | 784 | |
| 785 | for (unsigned i = 0; i < Worklist.size(); ++i) { |
| 786 | unsigned BN = Worklist[i]; |
| 787 | MachineBasicBlock &MBB = *MF.getBlockNumbered(BN); |
| 788 | for (auto &R : CSI) |
| 789 | if (!MBB.isLiveIn(R.getReg())) |
| 790 | MBB.addLiveIn(R.getReg()); |
| 791 | if (BN != SaveN) |
| 792 | for (auto &SB : MBB.successors()) |
| 793 | Worklist.insert(SB->getNumber()); |
| 794 | } |
| 795 | } |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 796 | |
Krzysztof Parzyszek | da0b9a9 | 2016-05-26 19:44:28 +0000 | [diff] [blame] | 797 | bool HexagonFrameLowering::updateExitPaths(MachineBasicBlock &MBB, |
Krzysztof Parzyszek | a34d639 | 2016-07-27 16:26:39 +0000 | [diff] [blame] | 798 | MachineBasicBlock &RestoreB, BitVector &DoneT, BitVector &DoneF, |
Krzysztof Parzyszek | da0b9a9 | 2016-05-26 19:44:28 +0000 | [diff] [blame] | 799 | BitVector &Path) const { |
| 800 | assert(MBB.getNumber() >= 0); |
| 801 | unsigned BN = MBB.getNumber(); |
| 802 | if (Path[BN] || DoneF[BN]) |
| 803 | return false; |
| 804 | if (DoneT[BN]) |
| 805 | return true; |
| 806 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 807 | auto &CSI = MBB.getParent()->getFrameInfo().getCalleeSavedInfo(); |
Krzysztof Parzyszek | da0b9a9 | 2016-05-26 19:44:28 +0000 | [diff] [blame] | 808 | |
| 809 | Path[BN] = true; |
| 810 | bool ReachedExit = false; |
| 811 | for (auto &SB : MBB.successors()) |
| 812 | ReachedExit |= updateExitPaths(*SB, RestoreB, DoneT, DoneF, Path); |
| 813 | |
| 814 | if (!MBB.empty() && MBB.back().isReturn()) { |
| 815 | // Add implicit uses of all callee-saved registers to the reached |
| 816 | // return instructions. This is to prevent the anti-dependency breaker |
| 817 | // from renaming these registers. |
| 818 | MachineInstr &RetI = MBB.back(); |
| 819 | if (!isRestoreCall(RetI.getOpcode())) |
| 820 | for (auto &R : CSI) |
| 821 | RetI.addOperand(MachineOperand::CreateReg(R.getReg(), false, true)); |
| 822 | ReachedExit = true; |
| 823 | } |
| 824 | |
| 825 | // We don't want to add unnecessary live-ins to the restore block: since |
| 826 | // the callee-saved registers are being defined in it, the entry of the |
| 827 | // restore block cannot be on the path from the definitions to any exit. |
Krzysztof Parzyszek | a34d639 | 2016-07-27 16:26:39 +0000 | [diff] [blame] | 828 | if (ReachedExit && &MBB != &RestoreB) { |
Krzysztof Parzyszek | da0b9a9 | 2016-05-26 19:44:28 +0000 | [diff] [blame] | 829 | for (auto &R : CSI) |
| 830 | if (!MBB.isLiveIn(R.getReg())) |
| 831 | MBB.addLiveIn(R.getReg()); |
| 832 | DoneT[BN] = true; |
| 833 | } |
| 834 | if (!ReachedExit) |
| 835 | DoneF[BN] = true; |
| 836 | |
| 837 | Path[BN] = false; |
| 838 | return ReachedExit; |
| 839 | } |
| 840 | |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 841 | static Optional<MachineBasicBlock::iterator> |
| 842 | findCFILocation(MachineBasicBlock &B) { |
Krzysztof Parzyszek | c43644d | 2016-07-28 19:13:46 +0000 | [diff] [blame] | 843 | // The CFI instructions need to be inserted right after allocframe. |
| 844 | // An exception to this is a situation where allocframe is bundled |
| 845 | // with a call: then the CFI instructions need to be inserted before |
| 846 | // the packet with the allocframe+call (in case the call throws an |
| 847 | // exception). |
| 848 | auto End = B.instr_end(); |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 849 | |
Krzysztof Parzyszek | c43644d | 2016-07-28 19:13:46 +0000 | [diff] [blame] | 850 | for (MachineInstr &I : B) { |
| 851 | MachineBasicBlock::iterator It = I.getIterator(); |
| 852 | if (!I.isBundle()) { |
| 853 | if (I.getOpcode() == Hexagon::S2_allocframe) |
| 854 | return std::next(It); |
| 855 | continue; |
| 856 | } |
| 857 | // I is a bundle. |
| 858 | bool HasCall = false, HasAllocFrame = false; |
| 859 | auto T = It.getInstrIterator(); |
| 860 | while (++T != End && T->isBundled()) { |
| 861 | if (T->getOpcode() == Hexagon::S2_allocframe) |
| 862 | HasAllocFrame = true; |
| 863 | else if (T->isCall()) |
| 864 | HasCall = true; |
| 865 | } |
| 866 | if (HasAllocFrame) |
| 867 | return HasCall ? It : std::next(It); |
| 868 | } |
| 869 | return None; |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 872 | void HexagonFrameLowering::insertCFIInstructions(MachineFunction &MF) const { |
| 873 | for (auto &B : MF) { |
Krzysztof Parzyszek | c43644d | 2016-07-28 19:13:46 +0000 | [diff] [blame] | 874 | auto At = findCFILocation(B); |
| 875 | if (At.hasValue()) |
| 876 | insertCFIInstructionsAt(B, At.getValue()); |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 880 | void HexagonFrameLowering::insertCFIInstructionsAt(MachineBasicBlock &MBB, |
| 881 | MachineBasicBlock::iterator At) const { |
| 882 | MachineFunction &MF = *MBB.getParent(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 883 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 884 | MachineModuleInfo &MMI = MF.getMMI(); |
| 885 | auto &HST = MF.getSubtarget<HexagonSubtarget>(); |
| 886 | auto &HII = *HST.getInstrInfo(); |
| 887 | auto &HRI = *HST.getRegisterInfo(); |
| 888 | |
| 889 | // If CFI instructions have debug information attached, something goes |
| 890 | // wrong with the final assembly generation: the prolog_end is placed |
| 891 | // in a wrong location. |
| 892 | DebugLoc DL; |
| 893 | const MCInstrDesc &CFID = HII.get(TargetOpcode::CFI_INSTRUCTION); |
| 894 | |
| 895 | MCSymbol *FrameLabel = MMI.getContext().createTempSymbol(); |
Krzysztof Parzyszek | c2c7868 | 2016-05-11 14:53:07 +0000 | [diff] [blame] | 896 | bool HasFP = hasFP(MF); |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 897 | |
Krzysztof Parzyszek | c2c7868 | 2016-05-11 14:53:07 +0000 | [diff] [blame] | 898 | if (HasFP) { |
Krzysztof Parzyszek | 23920ec | 2015-10-19 18:30:27 +0000 | [diff] [blame] | 899 | unsigned DwFPReg = HRI.getDwarfRegNum(HRI.getFrameRegister(), true); |
| 900 | unsigned DwRAReg = HRI.getDwarfRegNum(HRI.getRARegister(), true); |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 901 | |
Krzysztof Parzyszek | 23920ec | 2015-10-19 18:30:27 +0000 | [diff] [blame] | 902 | // Define CFA via an offset from the value of FP. |
| 903 | // |
| 904 | // -8 -4 0 (SP) |
| 905 | // --+----+----+--------------------- |
| 906 | // | FP | LR | increasing addresses --> |
| 907 | // --+----+----+--------------------- |
| 908 | // | +-- Old SP (before allocframe) |
| 909 | // +-- New FP (after allocframe) |
| 910 | // |
| 911 | // MCCFIInstruction::createDefCfa subtracts the offset from the register. |
| 912 | // MCCFIInstruction::createOffset takes the offset without sign change. |
| 913 | auto DefCfa = MCCFIInstruction::createDefCfa(FrameLabel, DwFPReg, -8); |
| 914 | BuildMI(MBB, At, DL, CFID) |
Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame] | 915 | .addCFIIndex(MF.addFrameInst(DefCfa)); |
Krzysztof Parzyszek | 23920ec | 2015-10-19 18:30:27 +0000 | [diff] [blame] | 916 | // R31 (return addr) = CFA - 4 |
| 917 | auto OffR31 = MCCFIInstruction::createOffset(FrameLabel, DwRAReg, -4); |
| 918 | BuildMI(MBB, At, DL, CFID) |
Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame] | 919 | .addCFIIndex(MF.addFrameInst(OffR31)); |
Krzysztof Parzyszek | 23920ec | 2015-10-19 18:30:27 +0000 | [diff] [blame] | 920 | // R30 (frame ptr) = CFA - 8 |
| 921 | auto OffR30 = MCCFIInstruction::createOffset(FrameLabel, DwFPReg, -8); |
| 922 | BuildMI(MBB, At, DL, CFID) |
Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame] | 923 | .addCFIIndex(MF.addFrameInst(OffR30)); |
Krzysztof Parzyszek | 23920ec | 2015-10-19 18:30:27 +0000 | [diff] [blame] | 924 | } |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 925 | |
| 926 | static unsigned int RegsToMove[] = { |
| 927 | Hexagon::R1, Hexagon::R0, Hexagon::R3, Hexagon::R2, |
| 928 | Hexagon::R17, Hexagon::R16, Hexagon::R19, Hexagon::R18, |
| 929 | Hexagon::R21, Hexagon::R20, Hexagon::R23, Hexagon::R22, |
| 930 | Hexagon::R25, Hexagon::R24, Hexagon::R27, Hexagon::R26, |
| 931 | Hexagon::D0, Hexagon::D1, Hexagon::D8, Hexagon::D9, |
| 932 | Hexagon::D10, Hexagon::D11, Hexagon::D12, Hexagon::D13, |
| 933 | Hexagon::NoRegister |
| 934 | }; |
| 935 | |
Krzysztof Parzyszek | c2c7868 | 2016-05-11 14:53:07 +0000 | [diff] [blame] | 936 | const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo(); |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 937 | |
| 938 | for (unsigned i = 0; RegsToMove[i] != Hexagon::NoRegister; ++i) { |
| 939 | unsigned Reg = RegsToMove[i]; |
| 940 | auto IfR = [Reg] (const CalleeSavedInfo &C) -> bool { |
| 941 | return C.getReg() == Reg; |
| 942 | }; |
David Majnemer | 562e829 | 2016-08-12 00:18:03 +0000 | [diff] [blame] | 943 | auto F = find_if(CSI, IfR); |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 944 | if (F == CSI.end()) |
| 945 | continue; |
| 946 | |
Krzysztof Parzyszek | c2c7868 | 2016-05-11 14:53:07 +0000 | [diff] [blame] | 947 | int64_t Offset; |
| 948 | if (HasFP) { |
| 949 | // If the function has a frame pointer (i.e. has an allocframe), |
| 950 | // then the CFA has been defined in terms of FP. Any offsets in |
| 951 | // the following CFI instructions have to be defined relative |
| 952 | // to FP, which points to the bottom of the stack frame. |
| 953 | // The function getFrameIndexReference can still choose to use SP |
| 954 | // for the offset calculation, so we cannot simply call it here. |
| 955 | // Instead, get the offset (relative to the FP) directly. |
| 956 | Offset = MFI.getObjectOffset(F->getFrameIdx()); |
| 957 | } else { |
| 958 | unsigned FrameReg; |
| 959 | Offset = getFrameIndexReference(MF, F->getFrameIdx(), FrameReg); |
| 960 | } |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 961 | // Subtract 8 to make room for R30 and R31, which are added above. |
Krzysztof Parzyszek | c2c7868 | 2016-05-11 14:53:07 +0000 | [diff] [blame] | 962 | Offset -= 8; |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 963 | |
| 964 | if (Reg < Hexagon::D0 || Reg > Hexagon::D15) { |
| 965 | unsigned DwarfReg = HRI.getDwarfRegNum(Reg, true); |
| 966 | auto OffReg = MCCFIInstruction::createOffset(FrameLabel, DwarfReg, |
| 967 | Offset); |
| 968 | BuildMI(MBB, At, DL, CFID) |
Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame] | 969 | .addCFIIndex(MF.addFrameInst(OffReg)); |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 970 | } else { |
| 971 | // Split the double regs into subregs, and generate appropriate |
| 972 | // cfi_offsets. |
| 973 | // The only reason, we are split double regs is, llvm-mc does not |
| 974 | // understand paired registers for cfi_offset. |
| 975 | // Eg .cfi_offset r1:0, -64 |
| 976 | |
Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 977 | unsigned HiReg = HRI.getSubReg(Reg, Hexagon::isub_hi); |
| 978 | unsigned LoReg = HRI.getSubReg(Reg, Hexagon::isub_lo); |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 979 | unsigned HiDwarfReg = HRI.getDwarfRegNum(HiReg, true); |
| 980 | unsigned LoDwarfReg = HRI.getDwarfRegNum(LoReg, true); |
| 981 | auto OffHi = MCCFIInstruction::createOffset(FrameLabel, HiDwarfReg, |
| 982 | Offset+4); |
| 983 | BuildMI(MBB, At, DL, CFID) |
Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame] | 984 | .addCFIIndex(MF.addFrameInst(OffHi)); |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 985 | auto OffLo = MCCFIInstruction::createOffset(FrameLabel, LoDwarfReg, |
| 986 | Offset); |
| 987 | BuildMI(MBB, At, DL, CFID) |
Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame] | 988 | .addCFIIndex(MF.addFrameInst(OffLo)); |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | } |
| 992 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 993 | bool HexagonFrameLowering::hasFP(const MachineFunction &MF) const { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 994 | if (MF.getFunction().hasFnAttribute(Attribute::Naked)) |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 995 | return false; |
| 996 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 997 | auto &MFI = MF.getFrameInfo(); |
Krzysztof Parzyszek | 23920ec | 2015-10-19 18:30:27 +0000 | [diff] [blame] | 998 | auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo(); |
Krzysztof Parzyszek | 23920ec | 2015-10-19 18:30:27 +0000 | [diff] [blame] | 999 | bool HasExtraAlign = HRI.needsStackRealignment(MF); |
| 1000 | bool HasAlloca = MFI.hasVarSizedObjects(); |
| 1001 | |
| 1002 | // Insert ALLOCFRAME if we need to or at -O0 for the debugger. Think |
| 1003 | // that this shouldn't be required, but doing so now because gcc does and |
| 1004 | // gdb can't break at the start of the function without it. Will remove if |
| 1005 | // this turns out to be a gdb bug. |
| 1006 | // |
| 1007 | if (MF.getTarget().getOptLevel() == CodeGenOpt::None) |
| 1008 | return true; |
| 1009 | |
| 1010 | // By default we want to use SP (since it's always there). FP requires |
| 1011 | // some setup (i.e. ALLOCFRAME). |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 1012 | // Both, alloca and stack alignment modify the stack pointer by an |
| 1013 | // undetermined value, so we need to save it at the entry to the function |
| 1014 | // (i.e. use allocframe). |
| 1015 | if (HasAlloca || HasExtraAlign) |
Krzysztof Parzyszek | 23920ec | 2015-10-19 18:30:27 +0000 | [diff] [blame] | 1016 | return true; |
| 1017 | |
| 1018 | if (MFI.getStackSize() > 0) { |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 1019 | // If FP-elimination is disabled, we have to use FP at this point. |
| 1020 | const TargetMachine &TM = MF.getTarget(); |
| 1021 | if (TM.Options.DisableFramePointerElim(MF) || !EliminateFramePointer) |
| 1022 | return true; |
| 1023 | if (EnableStackOVFSanitizer) |
Krzysztof Parzyszek | 23920ec | 2015-10-19 18:30:27 +0000 | [diff] [blame] | 1024 | return true; |
| 1025 | } |
| 1026 | |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 1027 | const auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>(); |
Brendon Cahoon | ac8fed6 | 2018-11-09 18:16:24 +0000 | [diff] [blame] | 1028 | if ((MFI.hasCalls() && !enableAllocFrameElim(MF)) || HMFI.hasClobberLR()) |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 1029 | return true; |
| 1030 | |
Krzysztof Parzyszek | 23920ec | 2015-10-19 18:30:27 +0000 | [diff] [blame] | 1031 | return false; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1034 | enum SpillKind { |
| 1035 | SK_ToMem, |
| 1036 | SK_FromMem, |
| 1037 | SK_FromMemTailcall |
| 1038 | }; |
| 1039 | |
Krzysztof Parzyszek | c9d4caa | 2016-03-24 20:20:07 +0000 | [diff] [blame] | 1040 | static const char *getSpillFunctionFor(unsigned MaxReg, SpillKind SpillType, |
| 1041 | bool Stkchk = false) { |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1042 | const char * V4SpillToMemoryFunctions[] = { |
| 1043 | "__save_r16_through_r17", |
| 1044 | "__save_r16_through_r19", |
| 1045 | "__save_r16_through_r21", |
| 1046 | "__save_r16_through_r23", |
| 1047 | "__save_r16_through_r25", |
| 1048 | "__save_r16_through_r27" }; |
| 1049 | |
Krzysztof Parzyszek | c9d4caa | 2016-03-24 20:20:07 +0000 | [diff] [blame] | 1050 | const char * V4SpillToMemoryStkchkFunctions[] = { |
| 1051 | "__save_r16_through_r17_stkchk", |
| 1052 | "__save_r16_through_r19_stkchk", |
| 1053 | "__save_r16_through_r21_stkchk", |
| 1054 | "__save_r16_through_r23_stkchk", |
| 1055 | "__save_r16_through_r25_stkchk", |
| 1056 | "__save_r16_through_r27_stkchk" }; |
| 1057 | |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1058 | const char * V4SpillFromMemoryFunctions[] = { |
| 1059 | "__restore_r16_through_r17_and_deallocframe", |
| 1060 | "__restore_r16_through_r19_and_deallocframe", |
| 1061 | "__restore_r16_through_r21_and_deallocframe", |
| 1062 | "__restore_r16_through_r23_and_deallocframe", |
| 1063 | "__restore_r16_through_r25_and_deallocframe", |
| 1064 | "__restore_r16_through_r27_and_deallocframe" }; |
| 1065 | |
| 1066 | const char * V4SpillFromMemoryTailcallFunctions[] = { |
| 1067 | "__restore_r16_through_r17_and_deallocframe_before_tailcall", |
| 1068 | "__restore_r16_through_r19_and_deallocframe_before_tailcall", |
| 1069 | "__restore_r16_through_r21_and_deallocframe_before_tailcall", |
| 1070 | "__restore_r16_through_r23_and_deallocframe_before_tailcall", |
| 1071 | "__restore_r16_through_r25_and_deallocframe_before_tailcall", |
| 1072 | "__restore_r16_through_r27_and_deallocframe_before_tailcall" |
| 1073 | }; |
| 1074 | |
| 1075 | const char **SpillFunc = nullptr; |
| 1076 | |
| 1077 | switch(SpillType) { |
| 1078 | case SK_ToMem: |
Krzysztof Parzyszek | c9d4caa | 2016-03-24 20:20:07 +0000 | [diff] [blame] | 1079 | SpillFunc = Stkchk ? V4SpillToMemoryStkchkFunctions |
| 1080 | : V4SpillToMemoryFunctions; |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1081 | break; |
| 1082 | case SK_FromMem: |
| 1083 | SpillFunc = V4SpillFromMemoryFunctions; |
| 1084 | break; |
| 1085 | case SK_FromMemTailcall: |
| 1086 | SpillFunc = V4SpillFromMemoryTailcallFunctions; |
| 1087 | break; |
| 1088 | } |
| 1089 | assert(SpillFunc && "Unknown spill kind"); |
| 1090 | |
| 1091 | // Spill all callee-saved registers up to the highest register used. |
| 1092 | switch (MaxReg) { |
| 1093 | case Hexagon::R17: |
| 1094 | return SpillFunc[0]; |
| 1095 | case Hexagon::R19: |
| 1096 | return SpillFunc[1]; |
| 1097 | case Hexagon::R21: |
| 1098 | return SpillFunc[2]; |
| 1099 | case Hexagon::R23: |
| 1100 | return SpillFunc[3]; |
| 1101 | case Hexagon::R25: |
| 1102 | return SpillFunc[4]; |
| 1103 | case Hexagon::R27: |
| 1104 | return SpillFunc[5]; |
| 1105 | default: |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1106 | llvm_unreachable("Unhandled maximum callee save register"); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1107 | } |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 1108 | return nullptr; |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
James Y Knight | 5567baf | 2015-08-15 02:32:35 +0000 | [diff] [blame] | 1111 | int HexagonFrameLowering::getFrameIndexReference(const MachineFunction &MF, |
Krzysztof Parzyszek | 23920ec | 2015-10-19 18:30:27 +0000 | [diff] [blame] | 1112 | int FI, unsigned &FrameReg) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1113 | auto &MFI = MF.getFrameInfo(); |
Krzysztof Parzyszek | 23920ec | 2015-10-19 18:30:27 +0000 | [diff] [blame] | 1114 | auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo(); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1115 | |
Krzysztof Parzyszek | 23920ec | 2015-10-19 18:30:27 +0000 | [diff] [blame] | 1116 | int Offset = MFI.getObjectOffset(FI); |
| 1117 | bool HasAlloca = MFI.hasVarSizedObjects(); |
| 1118 | bool HasExtraAlign = HRI.needsStackRealignment(MF); |
| 1119 | bool NoOpt = MF.getTarget().getOptLevel() == CodeGenOpt::None; |
James Y Knight | 5567baf | 2015-08-15 02:32:35 +0000 | [diff] [blame] | 1120 | |
Krzysztof Parzyszek | a34901a | 2016-03-28 14:42:03 +0000 | [diff] [blame] | 1121 | auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>(); |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 1122 | unsigned FrameSize = MFI.getStackSize(); |
| 1123 | unsigned SP = HRI.getStackRegister(); |
| 1124 | unsigned FP = HRI.getFrameRegister(); |
Krzysztof Parzyszek | a34901a | 2016-03-28 14:42:03 +0000 | [diff] [blame] | 1125 | unsigned AP = HMFI.getStackAlignBasePhysReg(); |
Krzysztof Parzyszek | 918e6d7 | 2017-06-26 14:17:58 +0000 | [diff] [blame] | 1126 | // It may happen that AP will be absent even HasAlloca && HasExtraAlign |
| 1127 | // is true. HasExtraAlign may be set because of vector spills, without |
| 1128 | // aligned locals or aligned outgoing function arguments. Since vector |
| 1129 | // spills will ultimately be "unaligned", it is safe to use FP as the |
| 1130 | // base register. |
| 1131 | // In fact, in such a scenario the stack is actually not required to be |
| 1132 | // aligned, although it may end up being aligned anyway, since this |
| 1133 | // particular case is not easily detectable. The alignment will be |
| 1134 | // unnecessary, but not incorrect. |
| 1135 | // Unfortunately there is no quick way to verify that the above is |
| 1136 | // indeed the case (and that it's not a result of an error), so just |
| 1137 | // assume that missing AP will be replaced by FP. |
| 1138 | // (A better fix would be to rematerialize AP from FP and always align |
| 1139 | // vector spills.) |
| 1140 | if (AP == 0) |
| 1141 | AP = FP; |
Krzysztof Parzyszek | 23920ec | 2015-10-19 18:30:27 +0000 | [diff] [blame] | 1142 | |
| 1143 | bool UseFP = false, UseAP = false; // Default: use SP (except at -O0). |
| 1144 | // Use FP at -O0, except when there are objects with extra alignment. |
| 1145 | // That additional alignment requirement may cause a pad to be inserted, |
| 1146 | // which will make it impossible to use FP to access objects located |
| 1147 | // past the pad. |
| 1148 | if (NoOpt && !HasExtraAlign) |
| 1149 | UseFP = true; |
| 1150 | if (MFI.isFixedObjectIndex(FI) || MFI.isObjectPreAllocated(FI)) { |
| 1151 | // Fixed and preallocated objects will be located before any padding |
| 1152 | // so FP must be used to access them. |
| 1153 | UseFP |= (HasAlloca || HasExtraAlign); |
| 1154 | } else { |
| 1155 | if (HasAlloca) { |
| 1156 | if (HasExtraAlign) |
| 1157 | UseAP = true; |
| 1158 | else |
| 1159 | UseFP = true; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | // If FP was picked, then there had better be FP. |
| 1164 | bool HasFP = hasFP(MF); |
| 1165 | assert((HasFP || !UseFP) && "This function must have frame pointer"); |
| 1166 | |
| 1167 | // Having FP implies allocframe. Allocframe will store extra 8 bytes: |
| 1168 | // FP/LR. If the base register is used to access an object across these |
| 1169 | // 8 bytes, then the offset will need to be adjusted by 8. |
| 1170 | // |
| 1171 | // After allocframe: |
| 1172 | // HexagonISelLowering adds 8 to ---+ |
| 1173 | // the offsets of all stack-based | |
| 1174 | // arguments (*) | |
| 1175 | // | |
| 1176 | // getObjectOffset < 0 0 8 getObjectOffset >= 8 |
| 1177 | // ------------------------+-----+------------------------> increasing |
| 1178 | // <local objects> |FP/LR| <input arguments> addresses |
| 1179 | // -----------------+------+-----+------------------------> |
| 1180 | // | | |
| 1181 | // SP/AP point --+ +-- FP points here (**) |
| 1182 | // somewhere on |
| 1183 | // this side of FP/LR |
| 1184 | // |
| 1185 | // (*) See LowerFormalArguments. The FP/LR is assumed to be present. |
| 1186 | // (**) *FP == old-FP. FP+0..7 are the bytes of FP/LR. |
| 1187 | |
| 1188 | // The lowering assumes that FP/LR is present, and so the offsets of |
| 1189 | // the formal arguments start at 8. If FP/LR is not there we need to |
| 1190 | // reduce the offset by 8. |
| 1191 | if (Offset > 0 && !HasFP) |
| 1192 | Offset -= 8; |
| 1193 | |
| 1194 | if (UseFP) |
| 1195 | FrameReg = FP; |
| 1196 | else if (UseAP) |
| 1197 | FrameReg = AP; |
| 1198 | else |
| 1199 | FrameReg = SP; |
| 1200 | |
| 1201 | // Calculate the actual offset in the instruction. If there is no FP |
| 1202 | // (in other words, no allocframe), then SP will not be adjusted (i.e. |
| 1203 | // there will be no SP -= FrameSize), so the frame size should not be |
| 1204 | // added to the calculated offset. |
| 1205 | int RealOffset = Offset; |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 1206 | if (!UseFP && !UseAP) |
Krzysztof Parzyszek | 23920ec | 2015-10-19 18:30:27 +0000 | [diff] [blame] | 1207 | RealOffset = FrameSize+Offset; |
| 1208 | return RealOffset; |
Jakob Stoklund Olesen | 0b97dbc | 2012-05-30 22:40:03 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1211 | bool HexagonFrameLowering::insertCSRSpillsInBlock(MachineBasicBlock &MBB, |
Krzysztof Parzyszek | c9d4caa | 2016-03-24 20:20:07 +0000 | [diff] [blame] | 1212 | const CSIVect &CSI, const HexagonRegisterInfo &HRI, |
| 1213 | bool &PrologueStubs) const { |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1214 | if (CSI.empty()) |
| 1215 | return true; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1216 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1217 | MachineBasicBlock::iterator MI = MBB.begin(); |
Krzysztof Parzyszek | c9d4caa | 2016-03-24 20:20:07 +0000 | [diff] [blame] | 1218 | PrologueStubs = false; |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1219 | MachineFunction &MF = *MBB.getParent(); |
Krzysztof Parzyszek | 080bebd | 2016-07-25 14:42:11 +0000 | [diff] [blame] | 1220 | auto &HST = MF.getSubtarget<HexagonSubtarget>(); |
| 1221 | auto &HII = *HST.getInstrInfo(); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1222 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1223 | if (useSpillFunction(MF, CSI)) { |
Krzysztof Parzyszek | c9d4caa | 2016-03-24 20:20:07 +0000 | [diff] [blame] | 1224 | PrologueStubs = true; |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1225 | unsigned MaxReg = getMaxCalleeSavedReg(CSI, HRI); |
Krzysztof Parzyszek | c9d4caa | 2016-03-24 20:20:07 +0000 | [diff] [blame] | 1226 | bool StkOvrFlowEnabled = EnableStackOVFSanitizer; |
| 1227 | const char *SpillFun = getSpillFunctionFor(MaxReg, SK_ToMem, |
| 1228 | StkOvrFlowEnabled); |
Krzysztof Parzyszek | 181fdbd | 2016-03-24 19:18:48 +0000 | [diff] [blame] | 1229 | auto &HTM = static_cast<const HexagonTargetMachine&>(MF.getTarget()); |
Rafael Espindola | b1556c4 | 2016-06-28 20:13:36 +0000 | [diff] [blame] | 1230 | bool IsPIC = HTM.isPositionIndependent(); |
Krzysztof Parzyszek | 080bebd | 2016-07-25 14:42:11 +0000 | [diff] [blame] | 1231 | bool LongCalls = HST.useLongCalls() || EnableSaveRestoreLong; |
Krzysztof Parzyszek | 181fdbd | 2016-03-24 19:18:48 +0000 | [diff] [blame] | 1232 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1233 | // Call spill function. |
| 1234 | DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc(); |
Krzysztof Parzyszek | c9d4caa | 2016-03-24 20:20:07 +0000 | [diff] [blame] | 1235 | unsigned SpillOpc; |
Krzysztof Parzyszek | 080bebd | 2016-07-25 14:42:11 +0000 | [diff] [blame] | 1236 | if (StkOvrFlowEnabled) { |
| 1237 | if (LongCalls) |
| 1238 | SpillOpc = IsPIC ? Hexagon::SAVE_REGISTERS_CALL_V4STK_EXT_PIC |
| 1239 | : Hexagon::SAVE_REGISTERS_CALL_V4STK_EXT; |
| 1240 | else |
| 1241 | SpillOpc = IsPIC ? Hexagon::SAVE_REGISTERS_CALL_V4STK_PIC |
| 1242 | : Hexagon::SAVE_REGISTERS_CALL_V4STK; |
| 1243 | } else { |
| 1244 | if (LongCalls) |
| 1245 | SpillOpc = IsPIC ? Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC |
| 1246 | : Hexagon::SAVE_REGISTERS_CALL_V4_EXT; |
| 1247 | else |
| 1248 | SpillOpc = IsPIC ? Hexagon::SAVE_REGISTERS_CALL_V4_PIC |
| 1249 | : Hexagon::SAVE_REGISTERS_CALL_V4; |
| 1250 | } |
Krzysztof Parzyszek | 181fdbd | 2016-03-24 19:18:48 +0000 | [diff] [blame] | 1251 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1252 | MachineInstr *SaveRegsCall = |
Krzysztof Parzyszek | 181fdbd | 2016-03-24 19:18:48 +0000 | [diff] [blame] | 1253 | BuildMI(MBB, MI, DL, HII.get(SpillOpc)) |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1254 | .addExternalSymbol(SpillFun); |
Krzysztof Parzyszek | 080bebd | 2016-07-25 14:42:11 +0000 | [diff] [blame] | 1255 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1256 | // Add callee-saved registers as use. |
Krzysztof Parzyszek | e8e754d | 2016-04-25 17:49:44 +0000 | [diff] [blame] | 1257 | addCalleeSaveRegistersAsImpOperand(SaveRegsCall, CSI, false, true); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1258 | // Add live in registers. |
| 1259 | for (unsigned I = 0; I < CSI.size(); ++I) |
| 1260 | MBB.addLiveIn(CSI[I].getReg()); |
| 1261 | return true; |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1262 | } |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1263 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1264 | for (unsigned i = 0, n = CSI.size(); i < n; ++i) { |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1265 | unsigned Reg = CSI[i].getReg(); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1266 | // Add live in registers. We treat eh_return callee saved register r0 - r3 |
| 1267 | // specially. They are not really callee saved registers as they are not |
| 1268 | // supposed to be killed. |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1269 | bool IsKill = !HRI.isEHReturnCalleeSaveReg(Reg); |
| 1270 | int FI = CSI[i].getFrameIdx(); |
| 1271 | const TargetRegisterClass *RC = HRI.getMinimalPhysRegClass(Reg); |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 1272 | HII.storeRegToStackSlot(MBB, MI, Reg, IsKill, FI, RC, &HRI); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1273 | if (IsKill) |
| 1274 | MBB.addLiveIn(Reg); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1275 | } |
| 1276 | return true; |
| 1277 | } |
| 1278 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1279 | bool HexagonFrameLowering::insertCSRRestoresInBlock(MachineBasicBlock &MBB, |
| 1280 | const CSIVect &CSI, const HexagonRegisterInfo &HRI) const { |
| 1281 | if (CSI.empty()) |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1282 | return false; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1283 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1284 | MachineBasicBlock::iterator MI = MBB.getFirstTerminator(); |
| 1285 | MachineFunction &MF = *MBB.getParent(); |
Krzysztof Parzyszek | 080bebd | 2016-07-25 14:42:11 +0000 | [diff] [blame] | 1286 | auto &HST = MF.getSubtarget<HexagonSubtarget>(); |
| 1287 | auto &HII = *HST.getInstrInfo(); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1288 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1289 | if (useRestoreFunction(MF, CSI)) { |
| 1290 | bool HasTC = hasTailCall(MBB) || !hasReturn(MBB); |
| 1291 | unsigned MaxR = getMaxCalleeSavedReg(CSI, HRI); |
| 1292 | SpillKind Kind = HasTC ? SK_FromMemTailcall : SK_FromMem; |
| 1293 | const char *RestoreFn = getSpillFunctionFor(MaxR, Kind); |
Krzysztof Parzyszek | 181fdbd | 2016-03-24 19:18:48 +0000 | [diff] [blame] | 1294 | auto &HTM = static_cast<const HexagonTargetMachine&>(MF.getTarget()); |
Rafael Espindola | b1556c4 | 2016-06-28 20:13:36 +0000 | [diff] [blame] | 1295 | bool IsPIC = HTM.isPositionIndependent(); |
Krzysztof Parzyszek | 080bebd | 2016-07-25 14:42:11 +0000 | [diff] [blame] | 1296 | bool LongCalls = HST.useLongCalls() || EnableSaveRestoreLong; |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1297 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1298 | // Call spill function. |
| 1299 | DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() |
Krzysztof Parzyszek | 4cef462 | 2018-08-31 22:10:04 +0000 | [diff] [blame] | 1300 | : MBB.findDebugLoc(MBB.end()); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1301 | MachineInstr *DeallocCall = nullptr; |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1302 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1303 | if (HasTC) { |
Krzysztof Parzyszek | 080bebd | 2016-07-25 14:42:11 +0000 | [diff] [blame] | 1304 | unsigned RetOpc; |
| 1305 | if (LongCalls) |
| 1306 | RetOpc = IsPIC ? Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT_PIC |
| 1307 | : Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT; |
| 1308 | else |
| 1309 | RetOpc = IsPIC ? Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_PIC |
| 1310 | : Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4; |
| 1311 | DeallocCall = BuildMI(MBB, MI, DL, HII.get(RetOpc)) |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1312 | .addExternalSymbol(RestoreFn); |
| 1313 | } else { |
| 1314 | // The block has a return. |
| 1315 | MachineBasicBlock::iterator It = MBB.getFirstTerminator(); |
| 1316 | assert(It->isReturn() && std::next(It) == MBB.end()); |
Krzysztof Parzyszek | 080bebd | 2016-07-25 14:42:11 +0000 | [diff] [blame] | 1317 | unsigned RetOpc; |
| 1318 | if (LongCalls) |
| 1319 | RetOpc = IsPIC ? Hexagon::RESTORE_DEALLOC_RET_JMP_V4_EXT_PIC |
| 1320 | : Hexagon::RESTORE_DEALLOC_RET_JMP_V4_EXT; |
| 1321 | else |
| 1322 | RetOpc = IsPIC ? Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC |
| 1323 | : Hexagon::RESTORE_DEALLOC_RET_JMP_V4; |
| 1324 | DeallocCall = BuildMI(MBB, It, DL, HII.get(RetOpc)) |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1325 | .addExternalSymbol(RestoreFn); |
| 1326 | // Transfer the function live-out registers. |
Duncan P. N. Exon Smith | fd8cc23 | 2016-02-27 20:01:33 +0000 | [diff] [blame] | 1327 | DeallocCall->copyImplicitOps(MF, *It); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1328 | } |
Krzysztof Parzyszek | e8e754d | 2016-04-25 17:49:44 +0000 | [diff] [blame] | 1329 | addCalleeSaveRegistersAsImpOperand(DeallocCall, CSI, true, false); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1330 | return true; |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1333 | for (unsigned i = 0; i < CSI.size(); ++i) { |
| 1334 | unsigned Reg = CSI[i].getReg(); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1335 | const TargetRegisterClass *RC = HRI.getMinimalPhysRegClass(Reg); |
| 1336 | int FI = CSI[i].getFrameIdx(); |
Krzysztof Parzyszek | db86770 | 2015-10-19 17:46:01 +0000 | [diff] [blame] | 1337 | HII.loadRegFromStackSlot(MBB, MI, Reg, FI, RC, &HRI); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1338 | } |
Krzysztof Parzyszek | c9d4caa | 2016-03-24 20:20:07 +0000 | [diff] [blame] | 1339 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1340 | return true; |
| 1341 | } |
| 1342 | |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 1343 | MachineBasicBlock::iterator HexagonFrameLowering::eliminateCallFramePseudoInstr( |
| 1344 | MachineFunction &MF, MachineBasicBlock &MBB, |
| 1345 | MachineBasicBlock::iterator I) const { |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 1346 | MachineInstr &MI = *I; |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1347 | unsigned Opc = MI.getOpcode(); |
Krzysztof Parzyszek | e568967 | 2015-04-23 20:26:21 +0000 | [diff] [blame] | 1348 | (void)Opc; // Silence compiler warning. |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1349 | assert((Opc == Hexagon::ADJCALLSTACKDOWN || Opc == Hexagon::ADJCALLSTACKUP) && |
| 1350 | "Cannot handle this call frame pseudo instruction"); |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 1351 | return MBB.erase(I); |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1354 | void HexagonFrameLowering::processFunctionBeforeFrameFinalized( |
| 1355 | MachineFunction &MF, RegScavenger *RS) const { |
| 1356 | // If this function has uses aligned stack and also has variable sized stack |
| 1357 | // objects, then we need to map all spill slots to fixed positions, so that |
| 1358 | // they can be accessed through FP. Otherwise they would have to be accessed |
| 1359 | // via AP, which may not be available at the particular place in the program. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1360 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 1361 | bool HasAlloca = MFI.hasVarSizedObjects(); |
| 1362 | bool NeedsAlign = (MFI.getMaxAlignment() > getStackAlignment()); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1363 | |
Krzysztof Parzyszek | 23920ec | 2015-10-19 18:30:27 +0000 | [diff] [blame] | 1364 | if (!HasAlloca || !NeedsAlign) |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1365 | return; |
| 1366 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1367 | unsigned LFS = MFI.getLocalFrameSize(); |
| 1368 | for (int i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) { |
| 1369 | if (!MFI.isSpillSlotObjectIndex(i) || MFI.isDeadObjectIndex(i)) |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1370 | continue; |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1371 | unsigned S = MFI.getObjectSize(i); |
Krzysztof Parzyszek | 2d65ea7 | 2016-03-28 15:43:03 +0000 | [diff] [blame] | 1372 | // Reduce the alignment to at most 8. This will require unaligned vector |
| 1373 | // stores if they happen here. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1374 | unsigned A = std::max(MFI.getObjectAlignment(i), 8U); |
| 1375 | MFI.setObjectAlignment(i, 8); |
Krzysztof Parzyszek | 2d65ea7 | 2016-03-28 15:43:03 +0000 | [diff] [blame] | 1376 | LFS = alignTo(LFS+S, A); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1377 | MFI.mapLocalFrameObject(i, -LFS); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1380 | MFI.setLocalFrameSize(LFS); |
| 1381 | unsigned A = MFI.getLocalFrameMaxAlign(); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1382 | assert(A <= 8 && "Unexpected local frame alignment"); |
| 1383 | if (A == 0) |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1384 | MFI.setLocalFrameMaxAlign(8); |
| 1385 | MFI.setUseLocalStackAllocationBlock(true); |
Krzysztof Parzyszek | a34901a | 2016-03-28 14:42:03 +0000 | [diff] [blame] | 1386 | |
| 1387 | // Set the physical aligned-stack base address register. |
| 1388 | unsigned AP = 0; |
| 1389 | if (const MachineInstr *AI = getAlignaInstr(MF)) |
| 1390 | AP = AI->getOperand(0).getReg(); |
| 1391 | auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>(); |
| 1392 | HMFI.setStackAlignBasePhysReg(AP); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Krzysztof Parzyszek | a5bd2954 | 2016-05-16 18:02:28 +0000 | [diff] [blame] | 1395 | /// Returns true if there are no caller-saved registers available in class RC. |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1396 | static bool needToReserveScavengingSpillSlots(MachineFunction &MF, |
Krzysztof Parzyszek | a5bd2954 | 2016-05-16 18:02:28 +0000 | [diff] [blame] | 1397 | const HexagonRegisterInfo &HRI, const TargetRegisterClass *RC) { |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1398 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1399 | |
Krzysztof Parzyszek | 6514a88 | 2016-03-21 19:57:08 +0000 | [diff] [blame] | 1400 | auto IsUsed = [&HRI,&MRI] (unsigned Reg) -> bool { |
| 1401 | for (MCRegAliasIterator AI(Reg, &HRI, true); AI.isValid(); ++AI) |
| 1402 | if (MRI.isPhysRegUsed(*AI)) |
| 1403 | return true; |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1404 | return false; |
Krzysztof Parzyszek | 6514a88 | 2016-03-21 19:57:08 +0000 | [diff] [blame] | 1405 | }; |
| 1406 | |
Krzysztof Parzyszek | e8e754d | 2016-04-25 17:49:44 +0000 | [diff] [blame] | 1407 | // Check for an unused caller-saved register. Callee-saved registers |
| 1408 | // have become pristine by now. |
Krzysztof Parzyszek | a5bd2954 | 2016-05-16 18:02:28 +0000 | [diff] [blame] | 1409 | for (const MCPhysReg *P = HRI.getCallerSavedRegs(&MF, RC); *P; ++P) |
Krzysztof Parzyszek | 6514a88 | 2016-03-21 19:57:08 +0000 | [diff] [blame] | 1410 | if (!IsUsed(*P)) |
| 1411 | return false; |
Krzysztof Parzyszek | e8e754d | 2016-04-25 17:49:44 +0000 | [diff] [blame] | 1412 | |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1413 | // All caller-saved registers are used. |
| 1414 | return true; |
| 1415 | } |
| 1416 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1417 | #ifndef NDEBUG |
Krzysztof Parzyszek | e568967 | 2015-04-23 20:26:21 +0000 | [diff] [blame] | 1418 | static void dump_registers(BitVector &Regs, const TargetRegisterInfo &TRI) { |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1419 | dbgs() << '{'; |
| 1420 | for (int x = Regs.find_first(); x >= 0; x = Regs.find_next(x)) { |
| 1421 | unsigned R = x; |
Francis Visoiu Mistrih | 9d419d3 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 1422 | dbgs() << ' ' << printReg(R, &TRI); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1423 | } |
| 1424 | dbgs() << " }"; |
| 1425 | } |
| 1426 | #endif |
| 1427 | |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1428 | bool HexagonFrameLowering::assignCalleeSavedSpillSlots(MachineFunction &MF, |
| 1429 | const TargetRegisterInfo *TRI, std::vector<CalleeSavedInfo> &CSI) const { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1430 | LLVM_DEBUG(dbgs() << __func__ << " on " << MF.getName() << '\n'); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1431 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1432 | BitVector SRegs(Hexagon::NUM_TARGET_REGS); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1433 | |
| 1434 | // Generate a set of unique, callee-saved registers (SRegs), where each |
| 1435 | // register in the set is maximal in terms of sub-/super-register relation, |
| 1436 | // i.e. for each R in SRegs, no proper super-register of R is also in SRegs. |
| 1437 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1438 | // (1) For each callee-saved register, add that register and all of its |
| 1439 | // sub-registers to SRegs. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1440 | LLVM_DEBUG(dbgs() << "Initial CS registers: {"); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1441 | for (unsigned i = 0, n = CSI.size(); i < n; ++i) { |
| 1442 | unsigned R = CSI[i].getReg(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1443 | LLVM_DEBUG(dbgs() << ' ' << printReg(R, TRI)); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1444 | for (MCSubRegIterator SR(R, TRI, true); SR.isValid(); ++SR) |
| 1445 | SRegs[*SR] = true; |
| 1446 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1447 | LLVM_DEBUG(dbgs() << " }\n"); |
| 1448 | LLVM_DEBUG(dbgs() << "SRegs.1: "; dump_registers(SRegs, *TRI); |
| 1449 | dbgs() << "\n"); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1450 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1451 | // (2) For each reserved register, remove that register and all of its |
| 1452 | // sub- and super-registers from SRegs. |
| 1453 | BitVector Reserved = TRI->getReservedRegs(MF); |
| 1454 | for (int x = Reserved.find_first(); x >= 0; x = Reserved.find_next(x)) { |
| 1455 | unsigned R = x; |
| 1456 | for (MCSuperRegIterator SR(R, TRI, true); SR.isValid(); ++SR) |
| 1457 | SRegs[*SR] = false; |
| 1458 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1459 | LLVM_DEBUG(dbgs() << "Res: "; dump_registers(Reserved, *TRI); |
| 1460 | dbgs() << "\n"); |
| 1461 | LLVM_DEBUG(dbgs() << "SRegs.2: "; dump_registers(SRegs, *TRI); |
| 1462 | dbgs() << "\n"); |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1463 | |
| 1464 | // (3) Collect all registers that have at least one sub-register in SRegs, |
| 1465 | // and also have no sub-registers that are reserved. These will be the can- |
| 1466 | // didates for saving as a whole instead of their individual sub-registers. |
| 1467 | // (Saving R17:16 instead of R16 is fine, but only if R17 was not reserved.) |
| 1468 | BitVector TmpSup(Hexagon::NUM_TARGET_REGS); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1469 | for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) { |
| 1470 | unsigned R = x; |
| 1471 | for (MCSuperRegIterator SR(R, TRI); SR.isValid(); ++SR) |
| 1472 | TmpSup[*SR] = true; |
| 1473 | } |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1474 | for (int x = TmpSup.find_first(); x >= 0; x = TmpSup.find_next(x)) { |
| 1475 | unsigned R = x; |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1476 | for (MCSubRegIterator SR(R, TRI, true); SR.isValid(); ++SR) { |
| 1477 | if (!Reserved[*SR]) |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1478 | continue; |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1479 | TmpSup[R] = false; |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1480 | break; |
| 1481 | } |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1482 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1483 | LLVM_DEBUG(dbgs() << "TmpSup: "; dump_registers(TmpSup, *TRI); |
| 1484 | dbgs() << "\n"); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1485 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1486 | // (4) Include all super-registers found in (3) into SRegs. |
| 1487 | SRegs |= TmpSup; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1488 | LLVM_DEBUG(dbgs() << "SRegs.4: "; dump_registers(SRegs, *TRI); |
| 1489 | dbgs() << "\n"); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1490 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 1491 | // (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] | 1492 | // remove R from SRegs. |
| 1493 | for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) { |
| 1494 | unsigned R = x; |
| 1495 | for (MCSuperRegIterator SR(R, TRI); SR.isValid(); ++SR) { |
| 1496 | if (!SRegs[*SR]) |
| 1497 | continue; |
| 1498 | SRegs[R] = false; |
| 1499 | break; |
| 1500 | } |
| 1501 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1502 | LLVM_DEBUG(dbgs() << "SRegs.5: "; dump_registers(SRegs, *TRI); |
| 1503 | dbgs() << "\n"); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1504 | |
| 1505 | // Now, for each register that has a fixed stack slot, create the stack |
| 1506 | // object for it. |
| 1507 | CSI.clear(); |
| 1508 | |
Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 1509 | using SpillSlot = TargetFrameLowering::SpillSlot; |
| 1510 | |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1511 | unsigned NumFixed; |
| 1512 | int MinOffset = 0; // CS offsets are negative. |
| 1513 | const SpillSlot *FixedSlots = getCalleeSavedSpillSlots(NumFixed); |
| 1514 | for (const SpillSlot *S = FixedSlots; S != FixedSlots+NumFixed; ++S) { |
| 1515 | if (!SRegs[S->Reg]) |
| 1516 | continue; |
| 1517 | const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(S->Reg); |
Krzysztof Parzyszek | 44e25f3 | 2017-04-24 18:55:33 +0000 | [diff] [blame] | 1518 | int FI = MFI.CreateFixedSpillStackObject(TRI->getSpillSize(*RC), S->Offset); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1519 | MinOffset = std::min(MinOffset, S->Offset); |
| 1520 | CSI.push_back(CalleeSavedInfo(S->Reg, FI)); |
| 1521 | SRegs[S->Reg] = false; |
| 1522 | } |
| 1523 | |
| 1524 | // There can be some registers that don't have fixed slots. For example, |
| 1525 | // we need to store R0-R3 in functions with exception handling. For each |
| 1526 | // such register, create a non-fixed stack object. |
| 1527 | for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) { |
| 1528 | unsigned R = x; |
| 1529 | const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(R); |
Krzysztof Parzyszek | 44e25f3 | 2017-04-24 18:55:33 +0000 | [diff] [blame] | 1530 | unsigned Size = TRI->getSpillSize(*RC); |
| 1531 | int Off = MinOffset - Size; |
| 1532 | unsigned Align = std::min(TRI->getSpillAlignment(*RC), getStackAlignment()); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1533 | assert(isPowerOf2_32(Align)); |
| 1534 | Off &= -Align; |
Krzysztof Parzyszek | 44e25f3 | 2017-04-24 18:55:33 +0000 | [diff] [blame] | 1535 | int FI = MFI.CreateFixedSpillStackObject(Size, Off); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1536 | MinOffset = std::min(MinOffset, Off); |
| 1537 | CSI.push_back(CalleeSavedInfo(R, FI)); |
| 1538 | SRegs[R] = false; |
| 1539 | } |
| 1540 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1541 | LLVM_DEBUG({ |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1542 | dbgs() << "CS information: {"; |
| 1543 | for (unsigned i = 0, n = CSI.size(); i < n; ++i) { |
| 1544 | int FI = CSI[i].getFrameIdx(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1545 | int Off = MFI.getObjectOffset(FI); |
Francis Visoiu Mistrih | 9d419d3 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 1546 | dbgs() << ' ' << printReg(CSI[i].getReg(), TRI) << ":fi#" << FI << ":sp"; |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1547 | if (Off >= 0) |
| 1548 | dbgs() << '+'; |
| 1549 | dbgs() << Off; |
| 1550 | } |
| 1551 | dbgs() << " }\n"; |
| 1552 | }); |
| 1553 | |
| 1554 | #ifndef NDEBUG |
| 1555 | // Verify that all registers were handled. |
| 1556 | bool MissedReg = false; |
| 1557 | for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) { |
| 1558 | unsigned R = x; |
Francis Visoiu Mistrih | 9d419d3 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 1559 | dbgs() << printReg(R, TRI) << ' '; |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1560 | MissedReg = true; |
| 1561 | } |
| 1562 | if (MissedReg) |
| 1563 | llvm_unreachable("...there are unhandled callee-saved registers!"); |
| 1564 | #endif |
| 1565 | |
| 1566 | return true; |
| 1567 | } |
| 1568 | |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1569 | bool HexagonFrameLowering::expandCopy(MachineBasicBlock &B, |
| 1570 | MachineBasicBlock::iterator It, MachineRegisterInfo &MRI, |
| 1571 | const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const { |
| 1572 | MachineInstr *MI = &*It; |
| 1573 | DebugLoc DL = MI->getDebugLoc(); |
| 1574 | unsigned DstR = MI->getOperand(0).getReg(); |
| 1575 | unsigned SrcR = MI->getOperand(1).getReg(); |
| 1576 | if (!Hexagon::ModRegsRegClass.contains(DstR) || |
| 1577 | !Hexagon::ModRegsRegClass.contains(SrcR)) |
| 1578 | return false; |
| 1579 | |
| 1580 | unsigned TmpR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass); |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 1581 | BuildMI(B, It, DL, HII.get(TargetOpcode::COPY), TmpR).add(MI->getOperand(1)); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1582 | BuildMI(B, It, DL, HII.get(TargetOpcode::COPY), DstR) |
| 1583 | .addReg(TmpR, RegState::Kill); |
| 1584 | |
| 1585 | NewRegs.push_back(TmpR); |
| 1586 | B.erase(It); |
| 1587 | return true; |
| 1588 | } |
| 1589 | |
| 1590 | bool HexagonFrameLowering::expandStoreInt(MachineBasicBlock &B, |
| 1591 | MachineBasicBlock::iterator It, MachineRegisterInfo &MRI, |
| 1592 | const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const { |
| 1593 | MachineInstr *MI = &*It; |
Krzysztof Parzyszek | f285963 | 2016-08-12 21:05:05 +0000 | [diff] [blame] | 1594 | if (!MI->getOperand(0).isFI()) |
| 1595 | return false; |
| 1596 | |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1597 | DebugLoc DL = MI->getDebugLoc(); |
| 1598 | unsigned Opc = MI->getOpcode(); |
| 1599 | unsigned SrcR = MI->getOperand(2).getReg(); |
| 1600 | bool IsKill = MI->getOperand(2).isKill(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1601 | int FI = MI->getOperand(0).getIndex(); |
| 1602 | |
| 1603 | // TmpR = C2_tfrpr SrcR if SrcR is a predicate register |
| 1604 | // TmpR = A2_tfrcrr SrcR if SrcR is a modifier register |
| 1605 | unsigned TmpR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass); |
| 1606 | unsigned TfrOpc = (Opc == Hexagon::STriw_pred) ? Hexagon::C2_tfrpr |
| 1607 | : Hexagon::A2_tfrcrr; |
| 1608 | BuildMI(B, It, DL, HII.get(TfrOpc), TmpR) |
| 1609 | .addReg(SrcR, getKillRegState(IsKill)); |
| 1610 | |
| 1611 | // S2_storeri_io FI, 0, TmpR |
| 1612 | BuildMI(B, It, DL, HII.get(Hexagon::S2_storeri_io)) |
Chandler Carruth | c73c030 | 2018-08-16 21:30:05 +0000 | [diff] [blame] | 1613 | .addFrameIndex(FI) |
| 1614 | .addImm(0) |
| 1615 | .addReg(TmpR, RegState::Kill) |
| 1616 | .cloneMemRefs(*MI); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1617 | |
| 1618 | NewRegs.push_back(TmpR); |
| 1619 | B.erase(It); |
| 1620 | return true; |
| 1621 | } |
| 1622 | |
| 1623 | bool HexagonFrameLowering::expandLoadInt(MachineBasicBlock &B, |
| 1624 | MachineBasicBlock::iterator It, MachineRegisterInfo &MRI, |
| 1625 | const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const { |
| 1626 | MachineInstr *MI = &*It; |
Krzysztof Parzyszek | f285963 | 2016-08-12 21:05:05 +0000 | [diff] [blame] | 1627 | if (!MI->getOperand(1).isFI()) |
| 1628 | return false; |
| 1629 | |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1630 | DebugLoc DL = MI->getDebugLoc(); |
| 1631 | unsigned Opc = MI->getOpcode(); |
| 1632 | unsigned DstR = MI->getOperand(0).getReg(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1633 | int FI = MI->getOperand(1).getIndex(); |
| 1634 | |
| 1635 | // TmpR = L2_loadri_io FI, 0 |
| 1636 | unsigned TmpR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass); |
| 1637 | BuildMI(B, It, DL, HII.get(Hexagon::L2_loadri_io), TmpR) |
Chandler Carruth | c73c030 | 2018-08-16 21:30:05 +0000 | [diff] [blame] | 1638 | .addFrameIndex(FI) |
| 1639 | .addImm(0) |
| 1640 | .cloneMemRefs(*MI); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1641 | |
| 1642 | // DstR = C2_tfrrp TmpR if DstR is a predicate register |
| 1643 | // DstR = A2_tfrrcr TmpR if DstR is a modifier register |
| 1644 | unsigned TfrOpc = (Opc == Hexagon::LDriw_pred) ? Hexagon::C2_tfrrp |
| 1645 | : Hexagon::A2_tfrrcr; |
| 1646 | BuildMI(B, It, DL, HII.get(TfrOpc), DstR) |
| 1647 | .addReg(TmpR, RegState::Kill); |
| 1648 | |
| 1649 | NewRegs.push_back(TmpR); |
| 1650 | B.erase(It); |
| 1651 | return true; |
| 1652 | } |
| 1653 | |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1654 | bool HexagonFrameLowering::expandStoreVecPred(MachineBasicBlock &B, |
| 1655 | MachineBasicBlock::iterator It, MachineRegisterInfo &MRI, |
| 1656 | const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const { |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1657 | MachineInstr *MI = &*It; |
Krzysztof Parzyszek | f285963 | 2016-08-12 21:05:05 +0000 | [diff] [blame] | 1658 | if (!MI->getOperand(0).isFI()) |
| 1659 | return false; |
| 1660 | |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1661 | DebugLoc DL = MI->getDebugLoc(); |
| 1662 | unsigned SrcR = MI->getOperand(2).getReg(); |
| 1663 | bool IsKill = MI->getOperand(2).isKill(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1664 | int FI = MI->getOperand(0).getIndex(); |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1665 | auto *RC = &Hexagon::HvxVRRegClass; |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1666 | |
| 1667 | // Insert transfer to general vector register. |
| 1668 | // TmpR0 = A2_tfrsi 0x01010101 |
| 1669 | // TmpR1 = V6_vandqrt Qx, TmpR0 |
| 1670 | // store FI, 0, TmpR1 |
| 1671 | unsigned TmpR0 = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass); |
| 1672 | unsigned TmpR1 = MRI.createVirtualRegister(RC); |
| 1673 | |
| 1674 | BuildMI(B, It, DL, HII.get(Hexagon::A2_tfrsi), TmpR0) |
| 1675 | .addImm(0x01010101); |
| 1676 | |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1677 | BuildMI(B, It, DL, HII.get(Hexagon::V6_vandqrt), TmpR1) |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1678 | .addReg(SrcR, getKillRegState(IsKill)) |
| 1679 | .addReg(TmpR0, RegState::Kill); |
| 1680 | |
| 1681 | auto *HRI = B.getParent()->getSubtarget<HexagonSubtarget>().getRegisterInfo(); |
| 1682 | HII.storeRegToStackSlot(B, It, TmpR1, true, FI, RC, HRI); |
| 1683 | expandStoreVec(B, std::prev(It), MRI, HII, NewRegs); |
| 1684 | |
| 1685 | NewRegs.push_back(TmpR0); |
| 1686 | NewRegs.push_back(TmpR1); |
| 1687 | B.erase(It); |
| 1688 | return true; |
| 1689 | } |
| 1690 | |
| 1691 | bool HexagonFrameLowering::expandLoadVecPred(MachineBasicBlock &B, |
| 1692 | MachineBasicBlock::iterator It, MachineRegisterInfo &MRI, |
| 1693 | const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const { |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1694 | MachineInstr *MI = &*It; |
Krzysztof Parzyszek | f285963 | 2016-08-12 21:05:05 +0000 | [diff] [blame] | 1695 | if (!MI->getOperand(1).isFI()) |
| 1696 | return false; |
| 1697 | |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1698 | DebugLoc DL = MI->getDebugLoc(); |
| 1699 | unsigned DstR = MI->getOperand(0).getReg(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1700 | int FI = MI->getOperand(1).getIndex(); |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1701 | auto *RC = &Hexagon::HvxVRRegClass; |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1702 | |
| 1703 | // TmpR0 = A2_tfrsi 0x01010101 |
| 1704 | // TmpR1 = load FI, 0 |
| 1705 | // DstR = V6_vandvrt TmpR1, TmpR0 |
| 1706 | unsigned TmpR0 = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass); |
| 1707 | unsigned TmpR1 = MRI.createVirtualRegister(RC); |
| 1708 | |
| 1709 | BuildMI(B, It, DL, HII.get(Hexagon::A2_tfrsi), TmpR0) |
| 1710 | .addImm(0x01010101); |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1711 | MachineFunction &MF = *B.getParent(); |
| 1712 | auto *HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1713 | HII.loadRegFromStackSlot(B, It, TmpR1, FI, RC, HRI); |
| 1714 | expandLoadVec(B, std::prev(It), MRI, HII, NewRegs); |
| 1715 | |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1716 | BuildMI(B, It, DL, HII.get(Hexagon::V6_vandvrt), DstR) |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1717 | .addReg(TmpR1, RegState::Kill) |
| 1718 | .addReg(TmpR0, RegState::Kill); |
| 1719 | |
| 1720 | NewRegs.push_back(TmpR0); |
| 1721 | NewRegs.push_back(TmpR1); |
| 1722 | B.erase(It); |
| 1723 | return true; |
| 1724 | } |
| 1725 | |
| 1726 | bool HexagonFrameLowering::expandStoreVec2(MachineBasicBlock &B, |
| 1727 | MachineBasicBlock::iterator It, MachineRegisterInfo &MRI, |
| 1728 | const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const { |
| 1729 | MachineFunction &MF = *B.getParent(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1730 | auto &MFI = MF.getFrameInfo(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1731 | auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo(); |
| 1732 | MachineInstr *MI = &*It; |
Krzysztof Parzyszek | f285963 | 2016-08-12 21:05:05 +0000 | [diff] [blame] | 1733 | if (!MI->getOperand(0).isFI()) |
| 1734 | return false; |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1735 | |
Krzysztof Parzyszek | b71085b | 2016-10-21 16:38:29 +0000 | [diff] [blame] | 1736 | // It is possible that the double vector being stored is only partially |
| 1737 | // defined. From the point of view of the liveness tracking, it is ok to |
| 1738 | // store it as a whole, but if we break it up we may end up storing a |
| 1739 | // register that is entirely undefined. |
Matthias Braun | ac4307c | 2017-05-26 21:51:00 +0000 | [diff] [blame] | 1740 | LivePhysRegs LPR(HRI); |
Krzysztof Parzyszek | b71085b | 2016-10-21 16:38:29 +0000 | [diff] [blame] | 1741 | LPR.addLiveIns(B); |
Matthias Braun | c661387 | 2018-11-06 19:00:11 +0000 | [diff] [blame] | 1742 | SmallVector<std::pair<MCPhysReg, const MachineOperand*>,2> Clobbers; |
Krzysztof Parzyszek | 954dd8d | 2017-01-18 23:11:40 +0000 | [diff] [blame] | 1743 | for (auto R = B.begin(); R != It; ++R) { |
| 1744 | Clobbers.clear(); |
Krzysztof Parzyszek | b71085b | 2016-10-21 16:38:29 +0000 | [diff] [blame] | 1745 | LPR.stepForward(*R, Clobbers); |
Krzysztof Parzyszek | 954dd8d | 2017-01-18 23:11:40 +0000 | [diff] [blame] | 1746 | } |
Krzysztof Parzyszek | b71085b | 2016-10-21 16:38:29 +0000 | [diff] [blame] | 1747 | |
Krzysztof Parzyszek | f285963 | 2016-08-12 21:05:05 +0000 | [diff] [blame] | 1748 | DebugLoc DL = MI->getDebugLoc(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1749 | unsigned SrcR = MI->getOperand(2).getReg(); |
Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 1750 | unsigned SrcLo = HRI.getSubReg(SrcR, Hexagon::vsub_lo); |
| 1751 | unsigned SrcHi = HRI.getSubReg(SrcR, Hexagon::vsub_hi); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1752 | bool IsKill = MI->getOperand(2).isKill(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1753 | int FI = MI->getOperand(0).getIndex(); |
| 1754 | |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1755 | unsigned Size = HRI.getSpillSize(Hexagon::HvxVRRegClass); |
| 1756 | unsigned NeedAlign = HRI.getSpillAlignment(Hexagon::HvxVRRegClass); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1757 | unsigned HasAlign = MFI.getObjectAlignment(FI); |
| 1758 | unsigned StoreOpc; |
| 1759 | |
| 1760 | // Store low part. |
Krzysztof Parzyszek | b71085b | 2016-10-21 16:38:29 +0000 | [diff] [blame] | 1761 | if (LPR.contains(SrcLo)) { |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1762 | StoreOpc = NeedAlign <= HasAlign ? Hexagon::V6_vS32b_ai |
| 1763 | : Hexagon::V6_vS32Ub_ai; |
Krzysztof Parzyszek | b71085b | 2016-10-21 16:38:29 +0000 | [diff] [blame] | 1764 | BuildMI(B, It, DL, HII.get(StoreOpc)) |
Chandler Carruth | c73c030 | 2018-08-16 21:30:05 +0000 | [diff] [blame] | 1765 | .addFrameIndex(FI) |
| 1766 | .addImm(0) |
| 1767 | .addReg(SrcLo, getKillRegState(IsKill)) |
| 1768 | .cloneMemRefs(*MI); |
Krzysztof Parzyszek | b71085b | 2016-10-21 16:38:29 +0000 | [diff] [blame] | 1769 | } |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1770 | |
Krzysztof Parzyszek | b71085b | 2016-10-21 16:38:29 +0000 | [diff] [blame] | 1771 | // Store high part. |
| 1772 | if (LPR.contains(SrcHi)) { |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1773 | StoreOpc = NeedAlign <= MinAlign(HasAlign, Size) ? Hexagon::V6_vS32b_ai |
| 1774 | : Hexagon::V6_vS32Ub_ai; |
Krzysztof Parzyszek | b71085b | 2016-10-21 16:38:29 +0000 | [diff] [blame] | 1775 | BuildMI(B, It, DL, HII.get(StoreOpc)) |
Chandler Carruth | c73c030 | 2018-08-16 21:30:05 +0000 | [diff] [blame] | 1776 | .addFrameIndex(FI) |
| 1777 | .addImm(Size) |
| 1778 | .addReg(SrcHi, getKillRegState(IsKill)) |
| 1779 | .cloneMemRefs(*MI); |
Krzysztof Parzyszek | b71085b | 2016-10-21 16:38:29 +0000 | [diff] [blame] | 1780 | } |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1781 | |
| 1782 | B.erase(It); |
| 1783 | return true; |
| 1784 | } |
| 1785 | |
| 1786 | bool HexagonFrameLowering::expandLoadVec2(MachineBasicBlock &B, |
| 1787 | MachineBasicBlock::iterator It, MachineRegisterInfo &MRI, |
| 1788 | const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const { |
| 1789 | MachineFunction &MF = *B.getParent(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1790 | auto &MFI = MF.getFrameInfo(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1791 | auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo(); |
| 1792 | MachineInstr *MI = &*It; |
Krzysztof Parzyszek | f285963 | 2016-08-12 21:05:05 +0000 | [diff] [blame] | 1793 | if (!MI->getOperand(1).isFI()) |
| 1794 | return false; |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1795 | |
Krzysztof Parzyszek | f285963 | 2016-08-12 21:05:05 +0000 | [diff] [blame] | 1796 | DebugLoc DL = MI->getDebugLoc(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1797 | unsigned DstR = MI->getOperand(0).getReg(); |
Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 1798 | unsigned DstHi = HRI.getSubReg(DstR, Hexagon::vsub_hi); |
| 1799 | unsigned DstLo = HRI.getSubReg(DstR, Hexagon::vsub_lo); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1800 | int FI = MI->getOperand(1).getIndex(); |
| 1801 | |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1802 | unsigned Size = HRI.getSpillSize(Hexagon::HvxVRRegClass); |
| 1803 | unsigned NeedAlign = HRI.getSpillAlignment(Hexagon::HvxVRRegClass); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1804 | unsigned HasAlign = MFI.getObjectAlignment(FI); |
| 1805 | unsigned LoadOpc; |
| 1806 | |
| 1807 | // Load low part. |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1808 | LoadOpc = NeedAlign <= HasAlign ? Hexagon::V6_vL32b_ai |
| 1809 | : Hexagon::V6_vL32Ub_ai; |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1810 | BuildMI(B, It, DL, HII.get(LoadOpc), DstLo) |
Chandler Carruth | c73c030 | 2018-08-16 21:30:05 +0000 | [diff] [blame] | 1811 | .addFrameIndex(FI) |
| 1812 | .addImm(0) |
| 1813 | .cloneMemRefs(*MI); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1814 | |
| 1815 | // Load high part. |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1816 | LoadOpc = NeedAlign <= MinAlign(HasAlign, Size) ? Hexagon::V6_vL32b_ai |
| 1817 | : Hexagon::V6_vL32Ub_ai; |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1818 | BuildMI(B, It, DL, HII.get(LoadOpc), DstHi) |
Chandler Carruth | c73c030 | 2018-08-16 21:30:05 +0000 | [diff] [blame] | 1819 | .addFrameIndex(FI) |
| 1820 | .addImm(Size) |
| 1821 | .cloneMemRefs(*MI); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1822 | |
| 1823 | B.erase(It); |
| 1824 | return true; |
| 1825 | } |
| 1826 | |
| 1827 | bool HexagonFrameLowering::expandStoreVec(MachineBasicBlock &B, |
| 1828 | MachineBasicBlock::iterator It, MachineRegisterInfo &MRI, |
| 1829 | const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const { |
| 1830 | MachineFunction &MF = *B.getParent(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1831 | auto &MFI = MF.getFrameInfo(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1832 | MachineInstr *MI = &*It; |
Krzysztof Parzyszek | f285963 | 2016-08-12 21:05:05 +0000 | [diff] [blame] | 1833 | if (!MI->getOperand(0).isFI()) |
| 1834 | return false; |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1835 | |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1836 | auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo(); |
Krzysztof Parzyszek | f285963 | 2016-08-12 21:05:05 +0000 | [diff] [blame] | 1837 | DebugLoc DL = MI->getDebugLoc(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1838 | unsigned SrcR = MI->getOperand(2).getReg(); |
| 1839 | bool IsKill = MI->getOperand(2).isKill(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1840 | int FI = MI->getOperand(0).getIndex(); |
| 1841 | |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1842 | unsigned NeedAlign = HRI.getSpillAlignment(Hexagon::HvxVRRegClass); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1843 | unsigned HasAlign = MFI.getObjectAlignment(FI); |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1844 | unsigned StoreOpc = NeedAlign <= HasAlign ? Hexagon::V6_vS32b_ai |
| 1845 | : Hexagon::V6_vS32Ub_ai; |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1846 | BuildMI(B, It, DL, HII.get(StoreOpc)) |
Chandler Carruth | c73c030 | 2018-08-16 21:30:05 +0000 | [diff] [blame] | 1847 | .addFrameIndex(FI) |
| 1848 | .addImm(0) |
| 1849 | .addReg(SrcR, getKillRegState(IsKill)) |
| 1850 | .cloneMemRefs(*MI); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1851 | |
| 1852 | B.erase(It); |
| 1853 | return true; |
| 1854 | } |
| 1855 | |
| 1856 | bool HexagonFrameLowering::expandLoadVec(MachineBasicBlock &B, |
| 1857 | MachineBasicBlock::iterator It, MachineRegisterInfo &MRI, |
| 1858 | const HexagonInstrInfo &HII, SmallVectorImpl<unsigned> &NewRegs) const { |
| 1859 | MachineFunction &MF = *B.getParent(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1860 | auto &MFI = MF.getFrameInfo(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1861 | MachineInstr *MI = &*It; |
Krzysztof Parzyszek | f285963 | 2016-08-12 21:05:05 +0000 | [diff] [blame] | 1862 | if (!MI->getOperand(1).isFI()) |
| 1863 | return false; |
| 1864 | |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1865 | auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1866 | DebugLoc DL = MI->getDebugLoc(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1867 | unsigned DstR = MI->getOperand(0).getReg(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1868 | int FI = MI->getOperand(1).getIndex(); |
| 1869 | |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1870 | unsigned NeedAlign = HRI.getSpillAlignment(Hexagon::HvxVRRegClass); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1871 | unsigned HasAlign = MFI.getObjectAlignment(FI); |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1872 | unsigned LoadOpc = NeedAlign <= HasAlign ? Hexagon::V6_vL32b_ai |
| 1873 | : Hexagon::V6_vL32Ub_ai; |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1874 | BuildMI(B, It, DL, HII.get(LoadOpc), DstR) |
Chandler Carruth | c73c030 | 2018-08-16 21:30:05 +0000 | [diff] [blame] | 1875 | .addFrameIndex(FI) |
| 1876 | .addImm(0) |
| 1877 | .cloneMemRefs(*MI); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1878 | |
| 1879 | B.erase(It); |
| 1880 | return true; |
| 1881 | } |
| 1882 | |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1883 | bool HexagonFrameLowering::expandSpillMacros(MachineFunction &MF, |
| 1884 | SmallVectorImpl<unsigned> &NewRegs) const { |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1885 | auto &HII = *MF.getSubtarget<HexagonSubtarget>().getInstrInfo(); |
Krzysztof Parzyszek | 7b413c6 | 2016-01-22 19:15:58 +0000 | [diff] [blame] | 1886 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1887 | bool Changed = false; |
Krzysztof Parzyszek | 7b413c6 | 2016-01-22 19:15:58 +0000 | [diff] [blame] | 1888 | |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1889 | for (auto &B : MF) { |
| 1890 | // Traverse the basic block. |
| 1891 | MachineBasicBlock::iterator NextI; |
| 1892 | for (auto I = B.begin(), E = B.end(); I != E; I = NextI) { |
| 1893 | MachineInstr *MI = &*I; |
| 1894 | NextI = std::next(I); |
| 1895 | unsigned Opc = MI->getOpcode(); |
Krzysztof Parzyszek | 7b413c6 | 2016-01-22 19:15:58 +0000 | [diff] [blame] | 1896 | |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1897 | switch (Opc) { |
| 1898 | case TargetOpcode::COPY: |
Krzysztof Parzyszek | e8e754d | 2016-04-25 17:49:44 +0000 | [diff] [blame] | 1899 | Changed |= expandCopy(B, I, MRI, HII, NewRegs); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1900 | break; |
| 1901 | case Hexagon::STriw_pred: |
Krzysztof Parzyszek | 440ba3a | 2018-03-28 19:38:29 +0000 | [diff] [blame] | 1902 | case Hexagon::STriw_ctr: |
Krzysztof Parzyszek | e8e754d | 2016-04-25 17:49:44 +0000 | [diff] [blame] | 1903 | Changed |= expandStoreInt(B, I, MRI, HII, NewRegs); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1904 | break; |
| 1905 | case Hexagon::LDriw_pred: |
Krzysztof Parzyszek | 440ba3a | 2018-03-28 19:38:29 +0000 | [diff] [blame] | 1906 | case Hexagon::LDriw_ctr: |
Krzysztof Parzyszek | e8e754d | 2016-04-25 17:49:44 +0000 | [diff] [blame] | 1907 | Changed |= expandLoadInt(B, I, MRI, HII, NewRegs); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1908 | break; |
Krzysztof Parzyszek | 17aa413 | 2016-08-16 15:43:54 +0000 | [diff] [blame] | 1909 | case Hexagon::PS_vstorerq_ai: |
Krzysztof Parzyszek | e8e754d | 2016-04-25 17:49:44 +0000 | [diff] [blame] | 1910 | Changed |= expandStoreVecPred(B, I, MRI, HII, NewRegs); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1911 | break; |
Krzysztof Parzyszek | 17aa413 | 2016-08-16 15:43:54 +0000 | [diff] [blame] | 1912 | case Hexagon::PS_vloadrq_ai: |
Krzysztof Parzyszek | e8e754d | 2016-04-25 17:49:44 +0000 | [diff] [blame] | 1913 | Changed |= expandLoadVecPred(B, I, MRI, HII, NewRegs); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1914 | break; |
Krzysztof Parzyszek | f285963 | 2016-08-12 21:05:05 +0000 | [diff] [blame] | 1915 | case Hexagon::PS_vloadrw_ai: |
| 1916 | case Hexagon::PS_vloadrwu_ai: |
Krzysztof Parzyszek | e8e754d | 2016-04-25 17:49:44 +0000 | [diff] [blame] | 1917 | Changed |= expandLoadVec2(B, I, MRI, HII, NewRegs); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1918 | break; |
Krzysztof Parzyszek | f285963 | 2016-08-12 21:05:05 +0000 | [diff] [blame] | 1919 | case Hexagon::PS_vstorerw_ai: |
| 1920 | case Hexagon::PS_vstorerwu_ai: |
Krzysztof Parzyszek | e8e754d | 2016-04-25 17:49:44 +0000 | [diff] [blame] | 1921 | Changed |= expandStoreVec2(B, I, MRI, HII, NewRegs); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1922 | break; |
Krzysztof Parzyszek | 7b413c6 | 2016-01-22 19:15:58 +0000 | [diff] [blame] | 1923 | } |
Krzysztof Parzyszek | 7b413c6 | 2016-01-22 19:15:58 +0000 | [diff] [blame] | 1924 | } |
| 1925 | } |
| 1926 | |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1927 | return Changed; |
Krzysztof Parzyszek | 7b413c6 | 2016-01-22 19:15:58 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1930 | void HexagonFrameLowering::determineCalleeSaves(MachineFunction &MF, |
| 1931 | BitVector &SavedRegs, |
| 1932 | RegScavenger *RS) const { |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1933 | auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1934 | |
| 1935 | SavedRegs.resize(HRI.getNumRegs()); |
| 1936 | |
| 1937 | // If we have a function containing __builtin_eh_return we want to spill and |
| 1938 | // restore all callee saved registers. Pretend that they are used. |
| 1939 | if (MF.getInfo<HexagonMachineFunctionInfo>()->hasEHReturn()) |
| 1940 | for (const MCPhysReg *R = HRI.getCalleeSavedRegs(&MF); *R; ++R) |
| 1941 | SavedRegs.set(*R); |
| 1942 | |
| 1943 | // Replace predicate register pseudo spill code. |
| 1944 | SmallVector<unsigned,8> NewRegs; |
| 1945 | expandSpillMacros(MF, NewRegs); |
Krzysztof Parzyszek | a34901a | 2016-03-28 14:42:03 +0000 | [diff] [blame] | 1946 | if (OptimizeSpillSlots && !isOptNone(MF)) |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 1947 | optimizeSpillSlots(MF, NewRegs); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1948 | |
Hiroshi Inoue | 7f9f92f | 2018-02-22 07:48:29 +0000 | [diff] [blame] | 1949 | // We need to reserve a spill slot if scavenging could potentially require |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1950 | // spilling a scavenged register. |
Krzysztof Parzyszek | ddafa2c | 2016-08-01 17:15:30 +0000 | [diff] [blame] | 1951 | if (!NewRegs.empty() || mayOverflowFrameOffset(MF)) { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1952 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1953 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 1954 | SetVector<const TargetRegisterClass*> SpillRCs; |
Krzysztof Parzyszek | a5bd2954 | 2016-05-16 18:02:28 +0000 | [diff] [blame] | 1955 | // Reserve an int register in any case, because it could be used to hold |
| 1956 | // the stack offset in case it does not fit into a spill instruction. |
| 1957 | SpillRCs.insert(&Hexagon::IntRegsRegClass); |
| 1958 | |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1959 | for (unsigned VR : NewRegs) |
| 1960 | SpillRCs.insert(MRI.getRegClass(VR)); |
| 1961 | |
Krzysztof Parzyszek | a5bd2954 | 2016-05-16 18:02:28 +0000 | [diff] [blame] | 1962 | for (auto *RC : SpillRCs) { |
| 1963 | if (!needToReserveScavengingSpillSlots(MF, HRI, RC)) |
| 1964 | continue; |
| 1965 | unsigned Num = RC == &Hexagon::IntRegsRegClass ? NumberScavengerSlots : 1; |
Krzysztof Parzyszek | 44e25f3 | 2017-04-24 18:55:33 +0000 | [diff] [blame] | 1966 | unsigned S = HRI.getSpillSize(*RC), A = HRI.getSpillAlignment(*RC); |
Krzysztof Parzyszek | a5bd2954 | 2016-05-16 18:02:28 +0000 | [diff] [blame] | 1967 | for (unsigned i = 0; i < Num; i++) { |
| 1968 | int NewFI = MFI.CreateSpillStackObject(S, A); |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1969 | RS->addScavengingFrameIndex(NewFI); |
| 1970 | } |
| 1971 | } |
Krzysztof Parzyszek | 996ad1f | 2016-02-12 18:19:53 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS); |
| 1975 | } |
| 1976 | |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 1977 | unsigned HexagonFrameLowering::findPhysReg(MachineFunction &MF, |
| 1978 | HexagonBlockRanges::IndexRange &FIR, |
| 1979 | HexagonBlockRanges::InstrIndexMap &IndexMap, |
| 1980 | HexagonBlockRanges::RegToRangeMap &DeadMap, |
| 1981 | const TargetRegisterClass *RC) const { |
| 1982 | auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo(); |
| 1983 | auto &MRI = MF.getRegInfo(); |
| 1984 | |
| 1985 | auto isDead = [&FIR,&DeadMap] (unsigned Reg) -> bool { |
| 1986 | auto F = DeadMap.find({Reg,0}); |
| 1987 | if (F == DeadMap.end()) |
| 1988 | return false; |
| 1989 | for (auto &DR : F->second) |
| 1990 | if (DR.contains(FIR)) |
| 1991 | return true; |
| 1992 | return false; |
| 1993 | }; |
| 1994 | |
| 1995 | for (unsigned Reg : RC->getRawAllocationOrder(MF)) { |
| 1996 | bool Dead = true; |
| 1997 | for (auto R : HexagonBlockRanges::expandToSubRegs({Reg,0}, MRI, HRI)) { |
| 1998 | if (isDead(R.Reg)) |
| 1999 | continue; |
| 2000 | Dead = false; |
| 2001 | break; |
| 2002 | } |
| 2003 | if (Dead) |
| 2004 | return Reg; |
| 2005 | } |
| 2006 | return 0; |
| 2007 | } |
| 2008 | |
| 2009 | void HexagonFrameLowering::optimizeSpillSlots(MachineFunction &MF, |
| 2010 | SmallVectorImpl<unsigned> &VRegs) const { |
| 2011 | auto &HST = MF.getSubtarget<HexagonSubtarget>(); |
| 2012 | auto &HII = *HST.getInstrInfo(); |
| 2013 | auto &HRI = *HST.getRegisterInfo(); |
| 2014 | auto &MRI = MF.getRegInfo(); |
| 2015 | HexagonBlockRanges HBR(MF); |
| 2016 | |
Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 2017 | using BlockIndexMap = |
| 2018 | std::map<MachineBasicBlock *, HexagonBlockRanges::InstrIndexMap>; |
| 2019 | using BlockRangeMap = |
| 2020 | std::map<MachineBasicBlock *, HexagonBlockRanges::RangeList>; |
| 2021 | using IndexType = HexagonBlockRanges::IndexType; |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2022 | |
| 2023 | struct SlotInfo { |
| 2024 | BlockRangeMap Map; |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 2025 | unsigned Size = 0; |
| 2026 | const TargetRegisterClass *RC = nullptr; |
NAKAMURA Takumi | c2cc870 | 2016-02-13 07:29:49 +0000 | [diff] [blame] | 2027 | |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 2028 | SlotInfo() = default; |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2029 | }; |
| 2030 | |
| 2031 | BlockIndexMap BlockIndexes; |
| 2032 | SmallSet<int,4> BadFIs; |
| 2033 | std::map<int,SlotInfo> FIRangeMap; |
| 2034 | |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2035 | // Accumulate register classes: get a common class for a pre-existing |
| 2036 | // class HaveRC and a new class NewRC. Return nullptr if a common class |
| 2037 | // cannot be found, otherwise return the resulting class. If HaveRC is |
| 2038 | // nullptr, assume that it is still unset. |
Malcolm Parsons | 17d266b | 2017-01-13 17:12:16 +0000 | [diff] [blame] | 2039 | auto getCommonRC = |
| 2040 | [](const TargetRegisterClass *HaveRC, |
| 2041 | const TargetRegisterClass *NewRC) -> const TargetRegisterClass * { |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2042 | if (HaveRC == nullptr || HaveRC == NewRC) |
| 2043 | return NewRC; |
| 2044 | // Different classes, both non-null. Pick the more general one. |
| 2045 | if (HaveRC->hasSubClassEq(NewRC)) |
| 2046 | return HaveRC; |
| 2047 | if (NewRC->hasSubClassEq(HaveRC)) |
| 2048 | return NewRC; |
| 2049 | return nullptr; |
| 2050 | }; |
| 2051 | |
| 2052 | // Scan all blocks in the function. Check all occurrences of frame indexes, |
| 2053 | // and collect relevant information. |
| 2054 | for (auto &B : MF) { |
| 2055 | std::map<int,IndexType> LastStore, LastLoad; |
Krzysztof Parzyszek | 280a50e | 2016-02-13 14:06:01 +0000 | [diff] [blame] | 2056 | // Emplace appears not to be supported in gcc 4.7.2-4. |
| 2057 | //auto P = BlockIndexes.emplace(&B, HexagonBlockRanges::InstrIndexMap(B)); |
Krzysztof Parzyszek | de697d4 | 2016-02-17 15:02:07 +0000 | [diff] [blame] | 2058 | auto P = BlockIndexes.insert( |
| 2059 | std::make_pair(&B, HexagonBlockRanges::InstrIndexMap(B))); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2060 | auto &IndexMap = P.first->second; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2061 | LLVM_DEBUG(dbgs() << "Index map for " << printMBBReference(B) << "\n" |
| 2062 | << IndexMap << '\n'); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2063 | |
| 2064 | for (auto &In : B) { |
| 2065 | int LFI, SFI; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 2066 | bool Load = HII.isLoadFromStackSlot(In, LFI) && !HII.isPredicated(In); |
| 2067 | bool Store = HII.isStoreToStackSlot(In, SFI) && !HII.isPredicated(In); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2068 | if (Load && Store) { |
| 2069 | // If it's both a load and a store, then we won't handle it. |
| 2070 | BadFIs.insert(LFI); |
| 2071 | BadFIs.insert(SFI); |
| 2072 | continue; |
| 2073 | } |
| 2074 | // Check for register classes of the register used as the source for |
| 2075 | // the store, and the register used as the destination for the load. |
| 2076 | // Also, only accept base+imm_offset addressing modes. Other addressing |
| 2077 | // modes can have side-effects (post-increments, etc.). For stack |
| 2078 | // slots they are very unlikely, so there is not much loss due to |
| 2079 | // this restriction. |
| 2080 | if (Load || Store) { |
| 2081 | int TFI = Load ? LFI : SFI; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 2082 | unsigned AM = HII.getAddrMode(In); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2083 | SlotInfo &SI = FIRangeMap[TFI]; |
| 2084 | bool Bad = (AM != HexagonII::BaseImmOffset); |
| 2085 | if (!Bad) { |
| 2086 | // If the addressing mode is ok, check the register class. |
Krzysztof Parzyszek | 5241b8e | 2016-07-27 20:50:42 +0000 | [diff] [blame] | 2087 | unsigned OpNum = Load ? 0 : 2; |
| 2088 | auto *RC = HII.getRegClass(In.getDesc(), OpNum, &HRI, MF); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2089 | RC = getCommonRC(SI.RC, RC); |
| 2090 | if (RC == nullptr) |
| 2091 | Bad = true; |
| 2092 | else |
| 2093 | SI.RC = RC; |
| 2094 | } |
| 2095 | if (!Bad) { |
| 2096 | // Check sizes. |
Krzysztof Parzyszek | 473d02d | 2017-09-14 12:06:40 +0000 | [diff] [blame] | 2097 | unsigned S = HII.getMemAccessSize(In); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2098 | if (SI.Size != 0 && SI.Size != S) |
| 2099 | Bad = true; |
| 2100 | else |
| 2101 | SI.Size = S; |
| 2102 | } |
Krzysztof Parzyszek | 5241b8e | 2016-07-27 20:50:42 +0000 | [diff] [blame] | 2103 | if (!Bad) { |
| 2104 | for (auto *Mo : In.memoperands()) { |
| 2105 | if (!Mo->isVolatile()) |
| 2106 | continue; |
| 2107 | Bad = true; |
| 2108 | break; |
| 2109 | } |
| 2110 | } |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2111 | if (Bad) |
| 2112 | BadFIs.insert(TFI); |
| 2113 | } |
| 2114 | |
| 2115 | // Locate uses of frame indices. |
| 2116 | for (unsigned i = 0, n = In.getNumOperands(); i < n; ++i) { |
| 2117 | const MachineOperand &Op = In.getOperand(i); |
| 2118 | if (!Op.isFI()) |
| 2119 | continue; |
| 2120 | int FI = Op.getIndex(); |
| 2121 | // Make sure that the following operand is an immediate and that |
| 2122 | // it is 0. This is the offset in the stack object. |
| 2123 | if (i+1 >= n || !In.getOperand(i+1).isImm() || |
| 2124 | In.getOperand(i+1).getImm() != 0) |
| 2125 | BadFIs.insert(FI); |
| 2126 | if (BadFIs.count(FI)) |
| 2127 | continue; |
| 2128 | |
| 2129 | IndexType Index = IndexMap.getIndex(&In); |
| 2130 | if (Load) { |
| 2131 | if (LastStore[FI] == IndexType::None) |
| 2132 | LastStore[FI] = IndexType::Entry; |
| 2133 | LastLoad[FI] = Index; |
| 2134 | } else if (Store) { |
| 2135 | HexagonBlockRanges::RangeList &RL = FIRangeMap[FI].Map[&B]; |
| 2136 | if (LastStore[FI] != IndexType::None) |
| 2137 | RL.add(LastStore[FI], LastLoad[FI], false, false); |
| 2138 | else if (LastLoad[FI] != IndexType::None) |
| 2139 | RL.add(IndexType::Entry, LastLoad[FI], false, false); |
| 2140 | LastLoad[FI] = IndexType::None; |
| 2141 | LastStore[FI] = Index; |
| 2142 | } else { |
| 2143 | BadFIs.insert(FI); |
| 2144 | } |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | for (auto &I : LastLoad) { |
| 2149 | IndexType LL = I.second; |
| 2150 | if (LL == IndexType::None) |
| 2151 | continue; |
| 2152 | auto &RL = FIRangeMap[I.first].Map[&B]; |
| 2153 | IndexType &LS = LastStore[I.first]; |
| 2154 | if (LS != IndexType::None) |
| 2155 | RL.add(LS, LL, false, false); |
| 2156 | else |
| 2157 | RL.add(IndexType::Entry, LL, false, false); |
| 2158 | LS = IndexType::None; |
| 2159 | } |
| 2160 | for (auto &I : LastStore) { |
| 2161 | IndexType LS = I.second; |
| 2162 | if (LS == IndexType::None) |
| 2163 | continue; |
| 2164 | auto &RL = FIRangeMap[I.first].Map[&B]; |
| 2165 | RL.add(LS, IndexType::None, false, false); |
| 2166 | } |
| 2167 | } |
| 2168 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2169 | LLVM_DEBUG({ |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2170 | for (auto &P : FIRangeMap) { |
| 2171 | dbgs() << "fi#" << P.first; |
| 2172 | if (BadFIs.count(P.first)) |
| 2173 | dbgs() << " (bad)"; |
| 2174 | dbgs() << " RC: "; |
| 2175 | if (P.second.RC != nullptr) |
| 2176 | dbgs() << HRI.getRegClassName(P.second.RC) << '\n'; |
| 2177 | else |
| 2178 | dbgs() << "<null>\n"; |
| 2179 | for (auto &R : P.second.Map) |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 2180 | dbgs() << " " << printMBBReference(*R.first) << " { " << R.second |
| 2181 | << "}\n"; |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2182 | } |
| 2183 | }); |
| 2184 | |
| 2185 | // When a slot is loaded from in a block without being stored to in the |
| 2186 | // same block, it is live-on-entry to this block. To avoid CFG analysis, |
| 2187 | // consider this slot to be live-on-exit from all blocks. |
| 2188 | SmallSet<int,4> LoxFIs; |
| 2189 | |
| 2190 | std::map<MachineBasicBlock*,std::vector<int>> BlockFIMap; |
| 2191 | |
| 2192 | for (auto &P : FIRangeMap) { |
| 2193 | // P = pair(FI, map: BB->RangeList) |
| 2194 | if (BadFIs.count(P.first)) |
| 2195 | continue; |
| 2196 | for (auto &B : MF) { |
| 2197 | auto F = P.second.Map.find(&B); |
| 2198 | // F = pair(BB, RangeList) |
| 2199 | if (F == P.second.Map.end() || F->second.empty()) |
| 2200 | continue; |
| 2201 | HexagonBlockRanges::IndexRange &IR = F->second.front(); |
| 2202 | if (IR.start() == IndexType::Entry) |
| 2203 | LoxFIs.insert(P.first); |
| 2204 | BlockFIMap[&B].push_back(P.first); |
| 2205 | } |
| 2206 | } |
| 2207 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2208 | LLVM_DEBUG({ |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2209 | dbgs() << "Block-to-FI map (* -- live-on-exit):\n"; |
| 2210 | for (auto &P : BlockFIMap) { |
| 2211 | auto &FIs = P.second; |
| 2212 | if (FIs.empty()) |
| 2213 | continue; |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 2214 | dbgs() << " " << printMBBReference(*P.first) << ": {"; |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2215 | for (auto I : FIs) { |
| 2216 | dbgs() << " fi#" << I; |
| 2217 | if (LoxFIs.count(I)) |
| 2218 | dbgs() << '*'; |
| 2219 | } |
| 2220 | dbgs() << " }\n"; |
| 2221 | } |
| 2222 | }); |
| 2223 | |
Krzysztof Parzyszek | dc42164 | 2016-07-27 20:58:43 +0000 | [diff] [blame] | 2224 | #ifndef NDEBUG |
| 2225 | bool HasOptLimit = SpillOptMax.getPosition(); |
| 2226 | #endif |
| 2227 | |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2228 | // eliminate loads, when all loads eliminated, eliminate all stores. |
| 2229 | for (auto &B : MF) { |
| 2230 | auto F = BlockIndexes.find(&B); |
| 2231 | assert(F != BlockIndexes.end()); |
| 2232 | HexagonBlockRanges::InstrIndexMap &IM = F->second; |
| 2233 | HexagonBlockRanges::RegToRangeMap LM = HBR.computeLiveMap(IM); |
| 2234 | HexagonBlockRanges::RegToRangeMap DM = HBR.computeDeadMap(IM, LM); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2235 | LLVM_DEBUG(dbgs() << printMBBReference(B) << " dead map\n" |
| 2236 | << HexagonBlockRanges::PrintRangeMap(DM, HRI)); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2237 | |
| 2238 | for (auto FI : BlockFIMap[&B]) { |
| 2239 | if (BadFIs.count(FI)) |
| 2240 | continue; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2241 | LLVM_DEBUG(dbgs() << "Working on fi#" << FI << '\n'); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2242 | HexagonBlockRanges::RangeList &RL = FIRangeMap[FI].Map[&B]; |
| 2243 | for (auto &Range : RL) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2244 | LLVM_DEBUG(dbgs() << "--Examining range:" << RL << '\n'); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2245 | if (!IndexType::isInstr(Range.start()) || |
| 2246 | !IndexType::isInstr(Range.end())) |
| 2247 | continue; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 2248 | MachineInstr &SI = *IM.getInstr(Range.start()); |
| 2249 | MachineInstr &EI = *IM.getInstr(Range.end()); |
| 2250 | assert(SI.mayStore() && "Unexpected start instruction"); |
| 2251 | assert(EI.mayLoad() && "Unexpected end instruction"); |
| 2252 | MachineOperand &SrcOp = SI.getOperand(2); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2253 | |
| 2254 | HexagonBlockRanges::RegisterRef SrcRR = { SrcOp.getReg(), |
| 2255 | SrcOp.getSubReg() }; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 2256 | auto *RC = HII.getRegClass(SI.getDesc(), 2, &HRI, MF); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2257 | // The this-> is needed to unconfuse MSVC. |
| 2258 | unsigned FoundR = this->findPhysReg(MF, Range, IM, DM, RC); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2259 | LLVM_DEBUG(dbgs() << "Replacement reg:" << printReg(FoundR, &HRI) |
| 2260 | << '\n'); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2261 | if (FoundR == 0) |
| 2262 | continue; |
Krzysztof Parzyszek | dc42164 | 2016-07-27 20:58:43 +0000 | [diff] [blame] | 2263 | #ifndef NDEBUG |
| 2264 | if (HasOptLimit) { |
| 2265 | if (SpillOptCount >= SpillOptMax) |
| 2266 | return; |
| 2267 | SpillOptCount++; |
| 2268 | } |
| 2269 | #endif |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2270 | |
| 2271 | // Generate the copy-in: "FoundR = COPY SrcR" at the store location. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 2272 | MachineBasicBlock::iterator StartIt = SI.getIterator(), NextIt; |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2273 | MachineInstr *CopyIn = nullptr; |
| 2274 | if (SrcRR.Reg != FoundR || SrcRR.Sub != 0) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 2275 | const DebugLoc &DL = SI.getDebugLoc(); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2276 | CopyIn = BuildMI(B, StartIt, DL, HII.get(TargetOpcode::COPY), FoundR) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 2277 | .add(SrcOp); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | ++StartIt; |
| 2281 | // Check if this is a last store and the FI is live-on-exit. |
| 2282 | if (LoxFIs.count(FI) && (&Range == &RL.back())) { |
| 2283 | // Update store's source register. |
| 2284 | if (unsigned SR = SrcOp.getSubReg()) |
| 2285 | SrcOp.setReg(HRI.getSubReg(FoundR, SR)); |
| 2286 | else |
| 2287 | SrcOp.setReg(FoundR); |
| 2288 | SrcOp.setSubReg(0); |
| 2289 | // We are keeping this register live. |
| 2290 | SrcOp.setIsKill(false); |
| 2291 | } else { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 2292 | B.erase(&SI); |
| 2293 | IM.replaceInstr(&SI, CopyIn); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 2296 | auto EndIt = std::next(EI.getIterator()); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2297 | for (auto It = StartIt; It != EndIt; It = NextIt) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 2298 | MachineInstr &MI = *It; |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2299 | NextIt = std::next(It); |
| 2300 | int TFI; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 2301 | if (!HII.isLoadFromStackSlot(MI, TFI) || TFI != FI) |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2302 | continue; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 2303 | unsigned DstR = MI.getOperand(0).getReg(); |
| 2304 | assert(MI.getOperand(0).getSubReg() == 0); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2305 | MachineInstr *CopyOut = nullptr; |
| 2306 | if (DstR != FoundR) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 2307 | DebugLoc DL = MI.getDebugLoc(); |
Krzysztof Parzyszek | 473d02d | 2017-09-14 12:06:40 +0000 | [diff] [blame] | 2308 | unsigned MemSize = HII.getMemAccessSize(MI); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2309 | assert(HII.getAddrMode(MI) == HexagonII::BaseImmOffset); |
| 2310 | unsigned CopyOpc = TargetOpcode::COPY; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 2311 | if (HII.isSignExtendingLoad(MI)) |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2312 | CopyOpc = (MemSize == 1) ? Hexagon::A2_sxtb : Hexagon::A2_sxth; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 2313 | else if (HII.isZeroExtendingLoad(MI)) |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2314 | CopyOpc = (MemSize == 1) ? Hexagon::A2_zxtb : Hexagon::A2_zxth; |
| 2315 | CopyOut = BuildMI(B, It, DL, HII.get(CopyOpc), DstR) |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 2316 | .addReg(FoundR, getKillRegState(&MI == &EI)); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2317 | } |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 2318 | IM.replaceInstr(&MI, CopyOut); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 2319 | B.erase(It); |
| 2320 | } |
| 2321 | |
| 2322 | // Update the dead map. |
| 2323 | HexagonBlockRanges::RegisterRef FoundRR = { FoundR, 0 }; |
| 2324 | for (auto RR : HexagonBlockRanges::expandToSubRegs(FoundRR, MRI, HRI)) |
| 2325 | DM[RR].subtract(Range); |
| 2326 | } // for Range in range list |
| 2327 | } |
| 2328 | } |
| 2329 | } |
| 2330 | |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 2331 | void HexagonFrameLowering::expandAlloca(MachineInstr *AI, |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 2332 | const HexagonInstrInfo &HII, unsigned SP, unsigned CF) const { |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 2333 | MachineBasicBlock &MB = *AI->getParent(); |
| 2334 | DebugLoc DL = AI->getDebugLoc(); |
| 2335 | unsigned A = AI->getOperand(2).getImm(); |
| 2336 | |
| 2337 | // Have |
| 2338 | // Rd = alloca Rs, #A |
| 2339 | // |
| 2340 | // If Rs and Rd are different registers, use this sequence: |
| 2341 | // Rd = sub(r29, Rs) |
| 2342 | // r29 = sub(r29, Rs) |
| 2343 | // Rd = and(Rd, #-A) ; if necessary |
| 2344 | // r29 = and(r29, #-A) ; if necessary |
| 2345 | // Rd = add(Rd, #CF) ; CF size aligned to at most A |
| 2346 | // otherwise, do |
| 2347 | // Rd = sub(r29, Rs) |
| 2348 | // Rd = and(Rd, #-A) ; if necessary |
| 2349 | // r29 = Rd |
| 2350 | // Rd = add(Rd, #CF) ; CF size aligned to at most A |
| 2351 | |
| 2352 | MachineOperand &RdOp = AI->getOperand(0); |
| 2353 | MachineOperand &RsOp = AI->getOperand(1); |
| 2354 | unsigned Rd = RdOp.getReg(), Rs = RsOp.getReg(); |
| 2355 | |
| 2356 | // Rd = sub(r29, Rs) |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 2357 | BuildMI(MB, AI, DL, HII.get(Hexagon::A2_sub), Rd) |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 2358 | .addReg(SP) |
| 2359 | .addReg(Rs); |
| 2360 | if (Rs != Rd) { |
| 2361 | // r29 = sub(r29, Rs) |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 2362 | BuildMI(MB, AI, DL, HII.get(Hexagon::A2_sub), SP) |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 2363 | .addReg(SP) |
| 2364 | .addReg(Rs); |
| 2365 | } |
| 2366 | if (A > 8) { |
| 2367 | // Rd = and(Rd, #-A) |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 2368 | BuildMI(MB, AI, DL, HII.get(Hexagon::A2_andir), Rd) |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 2369 | .addReg(Rd) |
| 2370 | .addImm(-int64_t(A)); |
| 2371 | if (Rs != Rd) |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 2372 | BuildMI(MB, AI, DL, HII.get(Hexagon::A2_andir), SP) |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 2373 | .addReg(SP) |
| 2374 | .addImm(-int64_t(A)); |
| 2375 | } |
| 2376 | if (Rs == Rd) { |
| 2377 | // r29 = Rd |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 2378 | BuildMI(MB, AI, DL, HII.get(TargetOpcode::COPY), SP) |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 2379 | .addReg(Rd); |
| 2380 | } |
| 2381 | if (CF > 0) { |
| 2382 | // Rd = add(Rd, #CF) |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 2383 | BuildMI(MB, AI, DL, HII.get(Hexagon::A2_addi), Rd) |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 2384 | .addReg(Rd) |
| 2385 | .addImm(CF); |
| 2386 | } |
| 2387 | } |
| 2388 | |
| 2389 | bool HexagonFrameLowering::needsAligna(const MachineFunction &MF) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 2390 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 2391 | if (!MFI.hasVarSizedObjects()) |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 2392 | return false; |
Krzysztof Parzyszek | 7170217 | 2017-06-23 19:47:04 +0000 | [diff] [blame] | 2393 | unsigned MaxA = MFI.getMaxAlignment(); |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 2394 | if (MaxA <= getStackAlignment()) |
| 2395 | return false; |
| 2396 | return true; |
| 2397 | } |
| 2398 | |
Krzysztof Parzyszek | 23920ec | 2015-10-19 18:30:27 +0000 | [diff] [blame] | 2399 | const MachineInstr *HexagonFrameLowering::getAlignaInstr( |
| 2400 | const MachineFunction &MF) const { |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 2401 | for (auto &B : MF) |
| 2402 | for (auto &I : B) |
Krzysztof Parzyszek | 1d01a79 | 2016-08-16 18:08:40 +0000 | [diff] [blame] | 2403 | if (I.getOpcode() == Hexagon::PS_aligna) |
Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 2404 | return &I; |
| 2405 | return nullptr; |
| 2406 | } |
| 2407 | |
Krzysztof Parzyszek | e8e754d | 2016-04-25 17:49:44 +0000 | [diff] [blame] | 2408 | /// Adds all callee-saved registers as implicit uses or defs to the |
| 2409 | /// instruction. |
| 2410 | void HexagonFrameLowering::addCalleeSaveRegistersAsImpOperand(MachineInstr *MI, |
| 2411 | const CSIVect &CSI, bool IsDef, bool IsKill) const { |
| 2412 | // Add the callee-saved registers as implicit uses. |
| 2413 | for (auto &R : CSI) |
| 2414 | MI->addOperand(MachineOperand::CreateReg(R.getReg(), IsDef, true, IsKill)); |
| 2415 | } |
| 2416 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 2417 | /// Determine whether the callee-saved register saves and restores should |
| 2418 | /// be generated via inline code. If this function returns "true", inline |
| 2419 | /// code will be generated. If this function returns "false", additional |
| 2420 | /// checks are performed, which may still lead to the inline code. |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 2421 | bool HexagonFrameLowering::shouldInlineCSR(const MachineFunction &MF, |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 2422 | const CSIVect &CSI) const { |
| 2423 | if (MF.getInfo<HexagonMachineFunctionInfo>()->hasEHReturn()) |
| 2424 | return true; |
Krzysztof Parzyszek | f67cd82 | 2017-07-11 17:11:54 +0000 | [diff] [blame] | 2425 | if (!hasFP(MF)) |
| 2426 | return true; |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 2427 | if (!isOptSize(MF) && !isMinSize(MF)) |
| 2428 | if (MF.getTarget().getOptLevel() > CodeGenOpt::Default) |
| 2429 | return true; |
| 2430 | |
| 2431 | // Check if CSI only has double registers, and if the registers form |
| 2432 | // a contiguous block starting from D8. |
| 2433 | BitVector Regs(Hexagon::NUM_TARGET_REGS); |
| 2434 | for (unsigned i = 0, n = CSI.size(); i < n; ++i) { |
| 2435 | unsigned R = CSI[i].getReg(); |
| 2436 | if (!Hexagon::DoubleRegsRegClass.contains(R)) |
| 2437 | return true; |
| 2438 | Regs[R] = true; |
| 2439 | } |
| 2440 | int F = Regs.find_first(); |
| 2441 | if (F != Hexagon::D8) |
| 2442 | return true; |
| 2443 | while (F >= 0) { |
| 2444 | int N = Regs.find_next(F); |
| 2445 | if (N >= 0 && N != F+1) |
| 2446 | return true; |
| 2447 | F = N; |
| 2448 | } |
| 2449 | |
| 2450 | return false; |
| 2451 | } |
| 2452 | |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 2453 | bool HexagonFrameLowering::useSpillFunction(const MachineFunction &MF, |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 2454 | const CSIVect &CSI) const { |
| 2455 | if (shouldInlineCSR(MF, CSI)) |
| 2456 | return false; |
| 2457 | unsigned NumCSI = CSI.size(); |
| 2458 | if (NumCSI <= 1) |
| 2459 | return false; |
| 2460 | |
| 2461 | unsigned Threshold = isOptSize(MF) ? SpillFuncThresholdOs |
| 2462 | : SpillFuncThreshold; |
| 2463 | return Threshold < NumCSI; |
| 2464 | } |
| 2465 | |
Krzysztof Parzyszek | 9eb75c4 | 2017-06-30 21:21:40 +0000 | [diff] [blame] | 2466 | bool HexagonFrameLowering::useRestoreFunction(const MachineFunction &MF, |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 2467 | const CSIVect &CSI) const { |
| 2468 | if (shouldInlineCSR(MF, CSI)) |
| 2469 | return false; |
Krzysztof Parzyszek | bb63f66 | 2016-03-28 14:52:21 +0000 | [diff] [blame] | 2470 | // The restore functions do a bit more than just restoring registers. |
| 2471 | // The non-returning versions will go back directly to the caller's |
| 2472 | // caller, others will clean up the stack frame in preparation for |
| 2473 | // a tail call. Using them can still save code size even if only one |
| 2474 | // register is getting restores. Make the decision based on -Oz: |
| 2475 | // using -Os will use inline restore for a single register. |
| 2476 | if (isMinSize(MF)) |
| 2477 | return true; |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 2478 | unsigned NumCSI = CSI.size(); |
Krzysztof Parzyszek | bb63f66 | 2016-03-28 14:52:21 +0000 | [diff] [blame] | 2479 | if (NumCSI <= 1) |
| 2480 | return false; |
| 2481 | |
Krzysztof Parzyszek | 876a19d | 2015-04-23 16:05:39 +0000 | [diff] [blame] | 2482 | unsigned Threshold = isOptSize(MF) ? SpillFuncThresholdOs-1 |
| 2483 | : SpillFuncThreshold; |
| 2484 | return Threshold < NumCSI; |
| 2485 | } |
Krzysztof Parzyszek | ddafa2c | 2016-08-01 17:15:30 +0000 | [diff] [blame] | 2486 | |
Krzysztof Parzyszek | ddafa2c | 2016-08-01 17:15:30 +0000 | [diff] [blame] | 2487 | bool HexagonFrameLowering::mayOverflowFrameOffset(MachineFunction &MF) const { |
| 2488 | unsigned StackSize = MF.getFrameInfo().estimateStackSize(MF); |
| 2489 | auto &HST = MF.getSubtarget<HexagonSubtarget>(); |
| 2490 | // A fairly simplistic guess as to whether a potential load/store to a |
Krzysztof Parzyszek | 69ffba4 | 2017-06-22 14:11:23 +0000 | [diff] [blame] | 2491 | // stack location could require an extra register. |
| 2492 | if (HST.useHVXOps() && StackSize > 256) |
| 2493 | return true; |
| 2494 | |
| 2495 | // Check if the function has store-immediate instructions that access |
| 2496 | // the stack. Since the offset field is not extendable, if the stack |
| 2497 | // size exceeds the offset limit (6 bits, shifted), the stores will |
| 2498 | // require a new base register. |
| 2499 | bool HasImmStack = false; |
| 2500 | unsigned MinLS = ~0u; // Log_2 of the memory access size. |
| 2501 | |
| 2502 | for (const MachineBasicBlock &B : MF) { |
| 2503 | for (const MachineInstr &MI : B) { |
| 2504 | unsigned LS = 0; |
| 2505 | switch (MI.getOpcode()) { |
| 2506 | case Hexagon::S4_storeirit_io: |
| 2507 | case Hexagon::S4_storeirif_io: |
| 2508 | case Hexagon::S4_storeiri_io: |
| 2509 | ++LS; |
| 2510 | LLVM_FALLTHROUGH; |
| 2511 | case Hexagon::S4_storeirht_io: |
| 2512 | case Hexagon::S4_storeirhf_io: |
| 2513 | case Hexagon::S4_storeirh_io: |
| 2514 | ++LS; |
| 2515 | LLVM_FALLTHROUGH; |
| 2516 | case Hexagon::S4_storeirbt_io: |
| 2517 | case Hexagon::S4_storeirbf_io: |
| 2518 | case Hexagon::S4_storeirb_io: |
| 2519 | if (MI.getOperand(0).isFI()) |
| 2520 | HasImmStack = true; |
| 2521 | MinLS = std::min(MinLS, LS); |
| 2522 | break; |
| 2523 | } |
| 2524 | } |
| 2525 | } |
| 2526 | |
| 2527 | if (HasImmStack) |
| 2528 | return !isUInt<6>(StackSize >> MinLS); |
| 2529 | |
Krzysztof Parzyszek | ddafa2c | 2016-08-01 17:15:30 +0000 | [diff] [blame] | 2530 | return false; |
| 2531 | } |