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