Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 1 | //===- HexagonSplitDouble.cpp ---------------------------------------------===// |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #define DEBUG_TYPE "hsdr" |
| 10 | |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 11 | #include "HexagonInstrInfo.h" |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 12 | #include "HexagonRegisterInfo.h" |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 13 | #include "HexagonSubtarget.h" |
| 14 | #include "llvm/ADT/BitVector.h" |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallVector.h" |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringRef.h" |
| 18 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFunction.h" |
| 20 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineInstr.h" |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 23 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineMemOperand.h" |
| 25 | #include "llvm/CodeGen/MachineOperand.h" |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 28 | #include "llvm/Config/llvm-config.h" |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 29 | #include "llvm/IR/DebugLoc.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 30 | #include "llvm/Pass.h" |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 31 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Compiler.h" |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ErrorHandling.h" |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 36 | #include <algorithm> |
| 37 | #include <cassert> |
| 38 | #include <cstdint> |
| 39 | #include <limits> |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 40 | #include <map> |
| 41 | #include <set> |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 42 | #include <utility> |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 43 | #include <vector> |
| 44 | |
| 45 | using namespace llvm; |
| 46 | |
| 47 | namespace llvm { |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 48 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 49 | FunctionPass *createHexagonSplitDoubleRegs(); |
| 50 | void initializeHexagonSplitDoubleRegsPass(PassRegistry&); |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 51 | |
| 52 | } // end namespace llvm |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 53 | |
Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 54 | static cl::opt<int> MaxHSDR("max-hsdr", cl::Hidden, cl::init(-1), |
| 55 | cl::desc("Maximum number of split partitions")); |
| 56 | static cl::opt<bool> MemRefsFixed("hsdr-no-mem", cl::Hidden, cl::init(true), |
| 57 | cl::desc("Do not split loads or stores")); |
Krzysztof Parzyszek | bcf0a96 | 2018-03-23 20:11:00 +0000 | [diff] [blame] | 58 | static cl::opt<bool> SplitAll("hsdr-split-all", cl::Hidden, cl::init(false), |
| 59 | cl::desc("Split all partitions")); |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 60 | |
Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 61 | namespace { |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 62 | |
| 63 | class HexagonSplitDoubleRegs : public MachineFunctionPass { |
| 64 | public: |
| 65 | static char ID; |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 66 | |
Krzysztof Parzyszek | effcc2f | 2018-05-04 15:04:48 +0000 | [diff] [blame] | 67 | HexagonSplitDoubleRegs() : MachineFunctionPass(ID) {} |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 68 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 69 | StringRef getPassName() const override { |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 70 | return "Hexagon Split Double Registers"; |
| 71 | } |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 72 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 73 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 74 | AU.addRequired<MachineLoopInfo>(); |
| 75 | AU.addPreserved<MachineLoopInfo>(); |
| 76 | MachineFunctionPass::getAnalysisUsage(AU); |
| 77 | } |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 78 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 79 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 80 | |
| 81 | private: |
| 82 | static const TargetRegisterClass *const DoubleRC; |
| 83 | |
Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 84 | const HexagonRegisterInfo *TRI = nullptr; |
| 85 | const HexagonInstrInfo *TII = nullptr; |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 86 | const MachineLoopInfo *MLI; |
| 87 | MachineRegisterInfo *MRI; |
| 88 | |
Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 89 | using USet = std::set<unsigned>; |
| 90 | using UUSetMap = std::map<unsigned, USet>; |
| 91 | using UUPair = std::pair<unsigned, unsigned>; |
| 92 | using UUPairMap = std::map<unsigned, UUPair>; |
| 93 | using LoopRegMap = std::map<const MachineLoop *, USet>; |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 94 | |
| 95 | bool isInduction(unsigned Reg, LoopRegMap &IRM) const; |
| 96 | bool isVolatileInstr(const MachineInstr *MI) const; |
| 97 | bool isFixedInstr(const MachineInstr *MI) const; |
| 98 | void partitionRegisters(UUSetMap &P2Rs); |
| 99 | int32_t profit(const MachineInstr *MI) const; |
Krzysztof Parzyszek | bcf0a96 | 2018-03-23 20:11:00 +0000 | [diff] [blame] | 100 | int32_t profit(unsigned Reg) const; |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 101 | bool isProfitable(const USet &Part, LoopRegMap &IRM) const; |
| 102 | |
| 103 | void collectIndRegsForLoop(const MachineLoop *L, USet &Rs); |
| 104 | void collectIndRegs(LoopRegMap &IRM); |
| 105 | |
| 106 | void createHalfInstr(unsigned Opc, MachineInstr *MI, |
| 107 | const UUPairMap &PairMap, unsigned SubR); |
| 108 | void splitMemRef(MachineInstr *MI, const UUPairMap &PairMap); |
| 109 | void splitImmediate(MachineInstr *MI, const UUPairMap &PairMap); |
| 110 | void splitCombine(MachineInstr *MI, const UUPairMap &PairMap); |
| 111 | void splitExt(MachineInstr *MI, const UUPairMap &PairMap); |
| 112 | void splitShift(MachineInstr *MI, const UUPairMap &PairMap); |
| 113 | void splitAslOr(MachineInstr *MI, const UUPairMap &PairMap); |
| 114 | bool splitInstr(MachineInstr *MI, const UUPairMap &PairMap); |
| 115 | void replaceSubregUses(MachineInstr *MI, const UUPairMap &PairMap); |
| 116 | void collapseRegPairs(MachineInstr *MI, const UUPairMap &PairMap); |
| 117 | bool splitPartition(const USet &Part); |
| 118 | |
| 119 | static int Counter; |
Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 120 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 121 | static void dump_partition(raw_ostream&, const USet&, |
| 122 | const TargetRegisterInfo&); |
| 123 | }; |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 124 | |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 125 | } // end anonymous namespace |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 126 | |
Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 127 | char HexagonSplitDoubleRegs::ID; |
| 128 | int HexagonSplitDoubleRegs::Counter = 0; |
| 129 | const TargetRegisterClass *const HexagonSplitDoubleRegs::DoubleRC = |
| 130 | &Hexagon::DoubleRegsRegClass; |
| 131 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 132 | INITIALIZE_PASS(HexagonSplitDoubleRegs, "hexagon-split-double", |
| 133 | "Hexagon Split Double Registers", false, false) |
| 134 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 135 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 136 | LLVM_DUMP_METHOD void HexagonSplitDoubleRegs::dump_partition(raw_ostream &os, |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 137 | const USet &Part, const TargetRegisterInfo &TRI) { |
| 138 | dbgs() << '{'; |
| 139 | for (auto I : Part) |
Francis Visoiu Mistrih | 9d419d3 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 140 | dbgs() << ' ' << printReg(I, &TRI); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 141 | dbgs() << " }"; |
| 142 | } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 143 | #endif |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 144 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 145 | bool HexagonSplitDoubleRegs::isInduction(unsigned Reg, LoopRegMap &IRM) const { |
| 146 | for (auto I : IRM) { |
| 147 | const USet &Rs = I.second; |
| 148 | if (Rs.find(Reg) != Rs.end()) |
| 149 | return true; |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 154 | bool HexagonSplitDoubleRegs::isVolatileInstr(const MachineInstr *MI) const { |
Philip Reames | 33d7e49 | 2019-02-24 00:45:09 +0000 | [diff] [blame] | 155 | for (auto &MO : MI->memoperands()) |
| 156 | if (MO->isVolatile() || MO->isAtomic()) |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 157 | return true; |
| 158 | return false; |
| 159 | } |
| 160 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 161 | bool HexagonSplitDoubleRegs::isFixedInstr(const MachineInstr *MI) const { |
Jay Foad | c5c935a | 2019-12-19 11:19:36 +0000 | [diff] [blame] | 162 | if (MI->mayLoadOrStore()) |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 163 | if (MemRefsFixed || isVolatileInstr(MI)) |
| 164 | return true; |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 165 | if (MI->isDebugInstr()) |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 166 | return false; |
| 167 | |
| 168 | unsigned Opc = MI->getOpcode(); |
| 169 | switch (Opc) { |
| 170 | default: |
| 171 | return true; |
| 172 | |
| 173 | case TargetOpcode::PHI: |
| 174 | case TargetOpcode::COPY: |
| 175 | break; |
| 176 | |
| 177 | case Hexagon::L2_loadrd_io: |
| 178 | // Not handling stack stores (only reg-based addresses). |
| 179 | if (MI->getOperand(1).isReg()) |
| 180 | break; |
| 181 | return true; |
| 182 | case Hexagon::S2_storerd_io: |
| 183 | // Not handling stack stores (only reg-based addresses). |
| 184 | if (MI->getOperand(0).isReg()) |
| 185 | break; |
| 186 | return true; |
| 187 | case Hexagon::L2_loadrd_pi: |
| 188 | case Hexagon::S2_storerd_pi: |
| 189 | |
| 190 | case Hexagon::A2_tfrpi: |
| 191 | case Hexagon::A2_combineii: |
| 192 | case Hexagon::A4_combineir: |
| 193 | case Hexagon::A4_combineii: |
| 194 | case Hexagon::A4_combineri: |
| 195 | case Hexagon::A2_combinew: |
Krzysztof Parzyszek | a338650 | 2016-08-10 16:46:36 +0000 | [diff] [blame] | 196 | case Hexagon::CONST64: |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 197 | |
| 198 | case Hexagon::A2_sxtw: |
| 199 | |
| 200 | case Hexagon::A2_andp: |
| 201 | case Hexagon::A2_orp: |
| 202 | case Hexagon::A2_xorp: |
| 203 | case Hexagon::S2_asl_i_p_or: |
| 204 | case Hexagon::S2_asl_i_p: |
| 205 | case Hexagon::S2_asr_i_p: |
| 206 | case Hexagon::S2_lsr_i_p: |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | for (auto &Op : MI->operands()) { |
| 211 | if (!Op.isReg()) |
| 212 | continue; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 213 | Register R = Op.getReg(); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 214 | if (!Register::isVirtualRegister(R)) |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 215 | return true; |
| 216 | } |
| 217 | return false; |
| 218 | } |
| 219 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 220 | void HexagonSplitDoubleRegs::partitionRegisters(UUSetMap &P2Rs) { |
Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 221 | using UUMap = std::map<unsigned, unsigned>; |
| 222 | using UVect = std::vector<unsigned>; |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 223 | |
| 224 | unsigned NumRegs = MRI->getNumVirtRegs(); |
| 225 | BitVector DoubleRegs(NumRegs); |
| 226 | for (unsigned i = 0; i < NumRegs; ++i) { |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 227 | unsigned R = Register::index2VirtReg(i); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 228 | if (MRI->getRegClass(R) == DoubleRC) |
| 229 | DoubleRegs.set(i); |
| 230 | } |
| 231 | |
| 232 | BitVector FixedRegs(NumRegs); |
| 233 | for (int x = DoubleRegs.find_first(); x >= 0; x = DoubleRegs.find_next(x)) { |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 234 | unsigned R = Register::index2VirtReg(x); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 235 | MachineInstr *DefI = MRI->getVRegDef(R); |
| 236 | // In some cases a register may exist, but never be defined or used. |
| 237 | // It should never appear anywhere, but mark it as "fixed", just to be |
| 238 | // safe. |
| 239 | if (!DefI || isFixedInstr(DefI)) |
| 240 | FixedRegs.set(x); |
| 241 | } |
| 242 | |
| 243 | UUSetMap AssocMap; |
| 244 | for (int x = DoubleRegs.find_first(); x >= 0; x = DoubleRegs.find_next(x)) { |
| 245 | if (FixedRegs[x]) |
| 246 | continue; |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 247 | unsigned R = Register::index2VirtReg(x); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 248 | LLVM_DEBUG(dbgs() << printReg(R, TRI) << " ~~"); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 249 | USet &Asc = AssocMap[R]; |
| 250 | for (auto U = MRI->use_nodbg_begin(R), Z = MRI->use_nodbg_end(); |
| 251 | U != Z; ++U) { |
| 252 | MachineOperand &Op = *U; |
| 253 | MachineInstr *UseI = Op.getParent(); |
| 254 | if (isFixedInstr(UseI)) |
| 255 | continue; |
| 256 | for (unsigned i = 0, n = UseI->getNumOperands(); i < n; ++i) { |
| 257 | MachineOperand &MO = UseI->getOperand(i); |
| 258 | // Skip non-registers or registers with subregisters. |
| 259 | if (&MO == &Op || !MO.isReg() || MO.getSubReg()) |
| 260 | continue; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 261 | Register T = MO.getReg(); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 262 | if (!Register::isVirtualRegister(T)) { |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 263 | FixedRegs.set(x); |
| 264 | continue; |
| 265 | } |
| 266 | if (MRI->getRegClass(T) != DoubleRC) |
| 267 | continue; |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 268 | unsigned u = Register::virtReg2Index(T); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 269 | if (FixedRegs[u]) |
| 270 | continue; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 271 | LLVM_DEBUG(dbgs() << ' ' << printReg(T, TRI)); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 272 | Asc.insert(T); |
| 273 | // Make it symmetric. |
| 274 | AssocMap[T].insert(R); |
| 275 | } |
| 276 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 277 | LLVM_DEBUG(dbgs() << '\n'); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | UUMap R2P; |
| 281 | unsigned NextP = 1; |
| 282 | USet Visited; |
| 283 | for (int x = DoubleRegs.find_first(); x >= 0; x = DoubleRegs.find_next(x)) { |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 284 | unsigned R = Register::index2VirtReg(x); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 285 | if (Visited.count(R)) |
| 286 | continue; |
| 287 | // Create a new partition for R. |
| 288 | unsigned ThisP = FixedRegs[x] ? 0 : NextP++; |
| 289 | UVect WorkQ; |
| 290 | WorkQ.push_back(R); |
| 291 | for (unsigned i = 0; i < WorkQ.size(); ++i) { |
| 292 | unsigned T = WorkQ[i]; |
| 293 | if (Visited.count(T)) |
| 294 | continue; |
| 295 | R2P[T] = ThisP; |
| 296 | Visited.insert(T); |
| 297 | // Add all registers associated with T. |
| 298 | USet &Asc = AssocMap[T]; |
| 299 | for (USet::iterator J = Asc.begin(), F = Asc.end(); J != F; ++J) |
| 300 | WorkQ.push_back(*J); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | for (auto I : R2P) |
| 305 | P2Rs[I.second].insert(I.first); |
| 306 | } |
| 307 | |
Krzysztof Parzyszek | effcc2f | 2018-05-04 15:04:48 +0000 | [diff] [blame] | 308 | static inline int32_t profitImm(unsigned Imm) { |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 309 | int32_t P = 0; |
Krzysztof Parzyszek | effcc2f | 2018-05-04 15:04:48 +0000 | [diff] [blame] | 310 | if (Imm == 0 || Imm == 0xFFFFFFFF) |
Krzysztof Parzyszek | bcf0a96 | 2018-03-23 20:11:00 +0000 | [diff] [blame] | 311 | P += 10; |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 312 | return P; |
| 313 | } |
| 314 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 315 | int32_t HexagonSplitDoubleRegs::profit(const MachineInstr *MI) const { |
| 316 | unsigned ImmX = 0; |
| 317 | unsigned Opc = MI->getOpcode(); |
| 318 | switch (Opc) { |
| 319 | case TargetOpcode::PHI: |
| 320 | for (const auto &Op : MI->operands()) |
| 321 | if (!Op.getSubReg()) |
| 322 | return 0; |
| 323 | return 10; |
| 324 | case TargetOpcode::COPY: |
| 325 | if (MI->getOperand(1).getSubReg() != 0) |
| 326 | return 10; |
| 327 | return 0; |
| 328 | |
| 329 | case Hexagon::L2_loadrd_io: |
| 330 | case Hexagon::S2_storerd_io: |
| 331 | return -1; |
| 332 | case Hexagon::L2_loadrd_pi: |
| 333 | case Hexagon::S2_storerd_pi: |
| 334 | return 2; |
| 335 | |
| 336 | case Hexagon::A2_tfrpi: |
Krzysztof Parzyszek | a338650 | 2016-08-10 16:46:36 +0000 | [diff] [blame] | 337 | case Hexagon::CONST64: { |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 338 | uint64_t D = MI->getOperand(1).getImm(); |
| 339 | unsigned Lo = D & 0xFFFFFFFFULL; |
| 340 | unsigned Hi = D >> 32; |
Krzysztof Parzyszek | effcc2f | 2018-05-04 15:04:48 +0000 | [diff] [blame] | 341 | return profitImm(Lo) + profitImm(Hi); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 342 | } |
| 343 | case Hexagon::A2_combineii: |
Krzysztof Parzyszek | effcc2f | 2018-05-04 15:04:48 +0000 | [diff] [blame] | 344 | case Hexagon::A4_combineii: { |
| 345 | const MachineOperand &Op1 = MI->getOperand(1); |
| 346 | const MachineOperand &Op2 = MI->getOperand(2); |
| 347 | int32_t Prof1 = Op1.isImm() ? profitImm(Op1.getImm()) : 0; |
| 348 | int32_t Prof2 = Op2.isImm() ? profitImm(Op2.getImm()) : 0; |
| 349 | return Prof1 + Prof2; |
| 350 | } |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 351 | case Hexagon::A4_combineri: |
| 352 | ImmX++; |
Simon Pilgrim | 087e87d | 2017-07-07 13:21:43 +0000 | [diff] [blame] | 353 | // Fall through into A4_combineir. |
| 354 | LLVM_FALLTHROUGH; |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 355 | case Hexagon::A4_combineir: { |
| 356 | ImmX++; |
Krzysztof Parzyszek | effcc2f | 2018-05-04 15:04:48 +0000 | [diff] [blame] | 357 | const MachineOperand &OpX = MI->getOperand(ImmX); |
| 358 | if (OpX.isImm()) { |
| 359 | int64_t V = OpX.getImm(); |
| 360 | if (V == 0 || V == -1) |
| 361 | return 10; |
| 362 | } |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 363 | // Fall through into A2_combinew. |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 364 | LLVM_FALLTHROUGH; |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 365 | } |
| 366 | case Hexagon::A2_combinew: |
| 367 | return 2; |
| 368 | |
| 369 | case Hexagon::A2_sxtw: |
| 370 | return 3; |
| 371 | |
| 372 | case Hexagon::A2_andp: |
| 373 | case Hexagon::A2_orp: |
Krzysztof Parzyszek | bcf0a96 | 2018-03-23 20:11:00 +0000 | [diff] [blame] | 374 | case Hexagon::A2_xorp: { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 375 | Register Rs = MI->getOperand(1).getReg(); |
| 376 | Register Rt = MI->getOperand(2).getReg(); |
Krzysztof Parzyszek | bcf0a96 | 2018-03-23 20:11:00 +0000 | [diff] [blame] | 377 | return profit(Rs) + profit(Rt); |
| 378 | } |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 379 | |
| 380 | case Hexagon::S2_asl_i_p_or: { |
| 381 | unsigned S = MI->getOperand(3).getImm(); |
| 382 | if (S == 0 || S == 32) |
| 383 | return 10; |
| 384 | return -1; |
| 385 | } |
| 386 | case Hexagon::S2_asl_i_p: |
| 387 | case Hexagon::S2_asr_i_p: |
| 388 | case Hexagon::S2_lsr_i_p: |
| 389 | unsigned S = MI->getOperand(2).getImm(); |
| 390 | if (S == 0 || S == 32) |
| 391 | return 10; |
| 392 | if (S == 16) |
| 393 | return 5; |
| 394 | if (S == 48) |
| 395 | return 7; |
| 396 | return -10; |
| 397 | } |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
Krzysztof Parzyszek | bcf0a96 | 2018-03-23 20:11:00 +0000 | [diff] [blame] | 402 | int32_t HexagonSplitDoubleRegs::profit(unsigned Reg) const { |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 403 | assert(Register::isVirtualRegister(Reg)); |
Krzysztof Parzyszek | bcf0a96 | 2018-03-23 20:11:00 +0000 | [diff] [blame] | 404 | |
| 405 | const MachineInstr *DefI = MRI->getVRegDef(Reg); |
| 406 | switch (DefI->getOpcode()) { |
| 407 | case Hexagon::A2_tfrpi: |
| 408 | case Hexagon::CONST64: |
| 409 | case Hexagon::A2_combineii: |
| 410 | case Hexagon::A4_combineii: |
| 411 | case Hexagon::A4_combineri: |
| 412 | case Hexagon::A4_combineir: |
| 413 | case Hexagon::A2_combinew: |
| 414 | return profit(DefI); |
| 415 | default: |
| 416 | break; |
| 417 | } |
| 418 | return 0; |
| 419 | } |
| 420 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 421 | bool HexagonSplitDoubleRegs::isProfitable(const USet &Part, LoopRegMap &IRM) |
| 422 | const { |
Krzysztof Parzyszek | d04c9b9 | 2017-03-31 21:03:59 +0000 | [diff] [blame] | 423 | unsigned FixedNum = 0, LoopPhiNum = 0; |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 424 | int32_t TotalP = 0; |
| 425 | |
| 426 | for (unsigned DR : Part) { |
| 427 | MachineInstr *DefI = MRI->getVRegDef(DR); |
| 428 | int32_t P = profit(DefI); |
Malcolm Parsons | 14bb1f5 | 2016-12-19 12:37:26 +0000 | [diff] [blame] | 429 | if (P == std::numeric_limits<int>::min()) |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 430 | return false; |
| 431 | TotalP += P; |
| 432 | // Reduce the profitability of splitting induction registers. |
| 433 | if (isInduction(DR, IRM)) |
| 434 | TotalP -= 30; |
| 435 | |
| 436 | for (auto U = MRI->use_nodbg_begin(DR), W = MRI->use_nodbg_end(); |
| 437 | U != W; ++U) { |
| 438 | MachineInstr *UseI = U->getParent(); |
| 439 | if (isFixedInstr(UseI)) { |
| 440 | FixedNum++; |
| 441 | // Calculate the cost of generating REG_SEQUENCE instructions. |
| 442 | for (auto &Op : UseI->operands()) { |
| 443 | if (Op.isReg() && Part.count(Op.getReg())) |
| 444 | if (Op.getSubReg()) |
| 445 | TotalP -= 2; |
| 446 | } |
| 447 | continue; |
| 448 | } |
| 449 | // If a register from this partition is used in a fixed instruction, |
| 450 | // and there is also a register in this partition that is used in |
| 451 | // a loop phi node, then decrease the splitting profit as this can |
| 452 | // confuse the modulo scheduler. |
| 453 | if (UseI->isPHI()) { |
| 454 | const MachineBasicBlock *PB = UseI->getParent(); |
| 455 | const MachineLoop *L = MLI->getLoopFor(PB); |
| 456 | if (L && L->getHeader() == PB) |
| 457 | LoopPhiNum++; |
| 458 | } |
| 459 | // Splittable instruction. |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 460 | int32_t P = profit(UseI); |
Malcolm Parsons | 14bb1f5 | 2016-12-19 12:37:26 +0000 | [diff] [blame] | 461 | if (P == std::numeric_limits<int>::min()) |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 462 | return false; |
| 463 | TotalP += P; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | if (FixedNum > 0 && LoopPhiNum > 0) |
| 468 | TotalP -= 20*LoopPhiNum; |
| 469 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 470 | LLVM_DEBUG(dbgs() << "Partition profit: " << TotalP << '\n'); |
Krzysztof Parzyszek | bcf0a96 | 2018-03-23 20:11:00 +0000 | [diff] [blame] | 471 | if (SplitAll) |
| 472 | return true; |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 473 | return TotalP > 0; |
| 474 | } |
| 475 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 476 | void HexagonSplitDoubleRegs::collectIndRegsForLoop(const MachineLoop *L, |
| 477 | USet &Rs) { |
| 478 | const MachineBasicBlock *HB = L->getHeader(); |
| 479 | const MachineBasicBlock *LB = L->getLoopLatch(); |
| 480 | if (!HB || !LB) |
| 481 | return; |
| 482 | |
| 483 | // Examine the latch branch. Expect it to be a conditional branch to |
| 484 | // the header (either "br-cond header" or "br-cond exit; br header"). |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 485 | MachineBasicBlock *TB = nullptr, *FB = nullptr; |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 486 | MachineBasicBlock *TmpLB = const_cast<MachineBasicBlock*>(LB); |
| 487 | SmallVector<MachineOperand,2> Cond; |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 488 | bool BadLB = TII->analyzeBranch(*TmpLB, TB, FB, Cond, false); |
Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 489 | // Only analyzable conditional branches. HII::analyzeBranch will put |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 490 | // the branch opcode as the first element of Cond, and the predicate |
| 491 | // operand as the second. |
| 492 | if (BadLB || Cond.size() != 2) |
| 493 | return; |
| 494 | // Only simple jump-conditional (with or without negation). |
| 495 | if (!TII->PredOpcodeHasJMP_c(Cond[0].getImm())) |
| 496 | return; |
| 497 | // Must go to the header. |
| 498 | if (TB != HB && FB != HB) |
| 499 | return; |
Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 500 | assert(Cond[1].isReg() && "Unexpected Cond vector from analyzeBranch"); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 501 | // Expect a predicate register. |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 502 | Register PR = Cond[1].getReg(); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 503 | assert(MRI->getRegClass(PR) == &Hexagon::PredRegsRegClass); |
| 504 | |
| 505 | // Get the registers on which the loop controlling compare instruction |
| 506 | // depends. |
Matt Arsenault | 30ebafa | 2020-04-03 13:22:51 -0400 | [diff] [blame^] | 507 | Register CmpR1, CmpR2; |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 508 | const MachineInstr *CmpI = MRI->getVRegDef(PR); |
| 509 | while (CmpI->getOpcode() == Hexagon::C2_not) |
| 510 | CmpI = MRI->getVRegDef(CmpI->getOperand(1).getReg()); |
| 511 | |
| 512 | int Mask = 0, Val = 0; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 513 | bool OkCI = TII->analyzeCompare(*CmpI, CmpR1, CmpR2, Mask, Val); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 514 | if (!OkCI) |
| 515 | return; |
| 516 | // Eliminate non-double input registers. |
| 517 | if (CmpR1 && MRI->getRegClass(CmpR1) != DoubleRC) |
| 518 | CmpR1 = 0; |
| 519 | if (CmpR2 && MRI->getRegClass(CmpR2) != DoubleRC) |
| 520 | CmpR2 = 0; |
| 521 | if (!CmpR1 && !CmpR2) |
| 522 | return; |
| 523 | |
| 524 | // Now examine the top of the loop: the phi nodes that could poten- |
| 525 | // tially define loop induction registers. The registers defined by |
| 526 | // such a phi node would be used in a 64-bit add, which then would |
| 527 | // be used in the loop compare instruction. |
| 528 | |
| 529 | // Get the set of all double registers defined by phi nodes in the |
| 530 | // loop header. |
Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 531 | using UVect = std::vector<unsigned>; |
| 532 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 533 | UVect DP; |
| 534 | for (auto &MI : *HB) { |
| 535 | if (!MI.isPHI()) |
| 536 | break; |
| 537 | const MachineOperand &MD = MI.getOperand(0); |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 538 | Register R = MD.getReg(); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 539 | if (MRI->getRegClass(R) == DoubleRC) |
| 540 | DP.push_back(R); |
| 541 | } |
| 542 | if (DP.empty()) |
| 543 | return; |
| 544 | |
| 545 | auto NoIndOp = [this, CmpR1, CmpR2] (unsigned R) -> bool { |
| 546 | for (auto I = MRI->use_nodbg_begin(R), E = MRI->use_nodbg_end(); |
| 547 | I != E; ++I) { |
| 548 | const MachineInstr *UseI = I->getParent(); |
| 549 | if (UseI->getOpcode() != Hexagon::A2_addp) |
| 550 | continue; |
| 551 | // Get the output from the add. If it is one of the inputs to the |
| 552 | // loop-controlling compare instruction, then R is likely an induc- |
| 553 | // tion register. |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 554 | Register T = UseI->getOperand(0).getReg(); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 555 | if (T == CmpR1 || T == CmpR2) |
| 556 | return false; |
| 557 | } |
| 558 | return true; |
| 559 | }; |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 560 | UVect::iterator End = llvm::remove_if(DP, NoIndOp); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 561 | Rs.insert(DP.begin(), End); |
| 562 | Rs.insert(CmpR1); |
| 563 | Rs.insert(CmpR2); |
| 564 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 565 | LLVM_DEBUG({ |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 566 | dbgs() << "For loop at " << printMBBReference(*HB) << " ind regs: "; |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 567 | dump_partition(dbgs(), Rs, *TRI); |
| 568 | dbgs() << '\n'; |
| 569 | }); |
| 570 | } |
| 571 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 572 | void HexagonSplitDoubleRegs::collectIndRegs(LoopRegMap &IRM) { |
Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 573 | using LoopVector = std::vector<MachineLoop *>; |
| 574 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 575 | LoopVector WorkQ; |
| 576 | |
| 577 | for (auto I : *MLI) |
| 578 | WorkQ.push_back(I); |
| 579 | for (unsigned i = 0; i < WorkQ.size(); ++i) { |
| 580 | for (auto I : *WorkQ[i]) |
| 581 | WorkQ.push_back(I); |
| 582 | } |
| 583 | |
| 584 | USet Rs; |
| 585 | for (unsigned i = 0, n = WorkQ.size(); i < n; ++i) { |
| 586 | MachineLoop *L = WorkQ[i]; |
| 587 | Rs.clear(); |
| 588 | collectIndRegsForLoop(L, Rs); |
| 589 | if (!Rs.empty()) |
| 590 | IRM.insert(std::make_pair(L, Rs)); |
| 591 | } |
| 592 | } |
| 593 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 594 | void HexagonSplitDoubleRegs::createHalfInstr(unsigned Opc, MachineInstr *MI, |
| 595 | const UUPairMap &PairMap, unsigned SubR) { |
| 596 | MachineBasicBlock &B = *MI->getParent(); |
| 597 | DebugLoc DL = MI->getDebugLoc(); |
| 598 | MachineInstr *NewI = BuildMI(B, MI, DL, TII->get(Opc)); |
| 599 | |
| 600 | for (auto &Op : MI->operands()) { |
| 601 | if (!Op.isReg()) { |
| 602 | NewI->addOperand(Op); |
| 603 | continue; |
| 604 | } |
| 605 | // For register operands, set the subregister. |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 606 | Register R = Op.getReg(); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 607 | unsigned SR = Op.getSubReg(); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 608 | bool isVirtReg = Register::isVirtualRegister(R); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 609 | bool isKill = Op.isKill(); |
| 610 | if (isVirtReg && MRI->getRegClass(R) == DoubleRC) { |
| 611 | isKill = false; |
| 612 | UUPairMap::const_iterator F = PairMap.find(R); |
| 613 | if (F == PairMap.end()) { |
| 614 | SR = SubR; |
| 615 | } else { |
| 616 | const UUPair &P = F->second; |
Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 617 | R = (SubR == Hexagon::isub_lo) ? P.first : P.second; |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 618 | SR = 0; |
| 619 | } |
| 620 | } |
| 621 | auto CO = MachineOperand::CreateReg(R, Op.isDef(), Op.isImplicit(), isKill, |
| 622 | Op.isDead(), Op.isUndef(), Op.isEarlyClobber(), SR, Op.isDebug(), |
| 623 | Op.isInternalRead()); |
| 624 | NewI->addOperand(CO); |
| 625 | } |
| 626 | } |
| 627 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 628 | void HexagonSplitDoubleRegs::splitMemRef(MachineInstr *MI, |
| 629 | const UUPairMap &PairMap) { |
| 630 | bool Load = MI->mayLoad(); |
| 631 | unsigned OrigOpc = MI->getOpcode(); |
| 632 | bool PostInc = (OrigOpc == Hexagon::L2_loadrd_pi || |
| 633 | OrigOpc == Hexagon::S2_storerd_pi); |
| 634 | MachineInstr *LowI, *HighI; |
| 635 | MachineBasicBlock &B = *MI->getParent(); |
| 636 | DebugLoc DL = MI->getDebugLoc(); |
| 637 | |
| 638 | // Index of the base-address-register operand. |
| 639 | unsigned AdrX = PostInc ? (Load ? 2 : 1) |
| 640 | : (Load ? 1 : 0); |
| 641 | MachineOperand &AdrOp = MI->getOperand(AdrX); |
| 642 | unsigned RSA = getRegState(AdrOp); |
| 643 | MachineOperand &ValOp = Load ? MI->getOperand(0) |
| 644 | : (PostInc ? MI->getOperand(3) |
| 645 | : MI->getOperand(2)); |
| 646 | UUPairMap::const_iterator F = PairMap.find(ValOp.getReg()); |
| 647 | assert(F != PairMap.end()); |
| 648 | |
| 649 | if (Load) { |
| 650 | const UUPair &P = F->second; |
| 651 | int64_t Off = PostInc ? 0 : MI->getOperand(2).getImm(); |
| 652 | LowI = BuildMI(B, MI, DL, TII->get(Hexagon::L2_loadri_io), P.first) |
| 653 | .addReg(AdrOp.getReg(), RSA & ~RegState::Kill, AdrOp.getSubReg()) |
| 654 | .addImm(Off); |
| 655 | HighI = BuildMI(B, MI, DL, TII->get(Hexagon::L2_loadri_io), P.second) |
| 656 | .addReg(AdrOp.getReg(), RSA & ~RegState::Kill, AdrOp.getSubReg()) |
| 657 | .addImm(Off+4); |
| 658 | } else { |
| 659 | const UUPair &P = F->second; |
| 660 | int64_t Off = PostInc ? 0 : MI->getOperand(1).getImm(); |
| 661 | LowI = BuildMI(B, MI, DL, TII->get(Hexagon::S2_storeri_io)) |
| 662 | .addReg(AdrOp.getReg(), RSA & ~RegState::Kill, AdrOp.getSubReg()) |
| 663 | .addImm(Off) |
| 664 | .addReg(P.first); |
| 665 | HighI = BuildMI(B, MI, DL, TII->get(Hexagon::S2_storeri_io)) |
| 666 | .addReg(AdrOp.getReg(), RSA & ~RegState::Kill, AdrOp.getSubReg()) |
| 667 | .addImm(Off+4) |
| 668 | .addReg(P.second); |
| 669 | } |
| 670 | |
| 671 | if (PostInc) { |
| 672 | // Create the increment of the address register. |
| 673 | int64_t Inc = Load ? MI->getOperand(3).getImm() |
| 674 | : MI->getOperand(2).getImm(); |
| 675 | MachineOperand &UpdOp = Load ? MI->getOperand(1) : MI->getOperand(0); |
| 676 | const TargetRegisterClass *RC = MRI->getRegClass(UpdOp.getReg()); |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 677 | Register NewR = MRI->createVirtualRegister(RC); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 678 | assert(!UpdOp.getSubReg() && "Def operand with subreg"); |
| 679 | BuildMI(B, MI, DL, TII->get(Hexagon::A2_addi), NewR) |
| 680 | .addReg(AdrOp.getReg(), RSA) |
| 681 | .addImm(Inc); |
| 682 | MRI->replaceRegWith(UpdOp.getReg(), NewR); |
| 683 | // The original instruction will be deleted later. |
| 684 | } |
| 685 | |
| 686 | // Generate a new pair of memory-operands. |
| 687 | MachineFunction &MF = *B.getParent(); |
| 688 | for (auto &MO : MI->memoperands()) { |
| 689 | const MachinePointerInfo &Ptr = MO->getPointerInfo(); |
Justin Lebar | 0af80cd | 2016-07-15 18:26:59 +0000 | [diff] [blame] | 690 | MachineMemOperand::Flags F = MO->getFlags(); |
Guillaume Chatelet | 74eac90 | 2020-03-27 13:51:59 +0000 | [diff] [blame] | 691 | Align A = MO->getAlign(); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 692 | |
Guillaume Chatelet | bdf7720 | 2020-03-30 14:45:57 +0000 | [diff] [blame] | 693 | auto *Tmp1 = MF.getMachineMemOperand(Ptr, F, 4 /*size*/, A); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 694 | LowI->addMemOperand(MF, Tmp1); |
Guillaume Chatelet | bdf7720 | 2020-03-30 14:45:57 +0000 | [diff] [blame] | 695 | auto *Tmp2 = |
| 696 | MF.getMachineMemOperand(Ptr, F, 4 /*size*/, std::min(A, Align(4))); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 697 | HighI->addMemOperand(MF, Tmp2); |
| 698 | } |
| 699 | } |
| 700 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 701 | void HexagonSplitDoubleRegs::splitImmediate(MachineInstr *MI, |
| 702 | const UUPairMap &PairMap) { |
| 703 | MachineOperand &Op0 = MI->getOperand(0); |
| 704 | MachineOperand &Op1 = MI->getOperand(1); |
| 705 | assert(Op0.isReg() && Op1.isImm()); |
| 706 | uint64_t V = Op1.getImm(); |
| 707 | |
| 708 | MachineBasicBlock &B = *MI->getParent(); |
| 709 | DebugLoc DL = MI->getDebugLoc(); |
| 710 | UUPairMap::const_iterator F = PairMap.find(Op0.getReg()); |
| 711 | assert(F != PairMap.end()); |
| 712 | const UUPair &P = F->second; |
| 713 | |
| 714 | // The operand to A2_tfrsi can only have 32 significant bits. Immediate |
| 715 | // values in MachineOperand are stored as 64-bit integers, and so the |
| 716 | // value -1 may be represented either as 64-bit -1, or 4294967295. Both |
| 717 | // will have the 32 higher bits truncated in the end, but -1 will remain |
| 718 | // as -1, while the latter may appear to be a large unsigned value |
| 719 | // requiring a constant extender. The casting to int32_t will select the |
| 720 | // former representation. (The same reasoning applies to all 32-bit |
| 721 | // values.) |
| 722 | BuildMI(B, MI, DL, TII->get(Hexagon::A2_tfrsi), P.first) |
| 723 | .addImm(int32_t(V & 0xFFFFFFFFULL)); |
| 724 | BuildMI(B, MI, DL, TII->get(Hexagon::A2_tfrsi), P.second) |
| 725 | .addImm(int32_t(V >> 32)); |
| 726 | } |
| 727 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 728 | void HexagonSplitDoubleRegs::splitCombine(MachineInstr *MI, |
| 729 | const UUPairMap &PairMap) { |
| 730 | MachineOperand &Op0 = MI->getOperand(0); |
| 731 | MachineOperand &Op1 = MI->getOperand(1); |
| 732 | MachineOperand &Op2 = MI->getOperand(2); |
| 733 | assert(Op0.isReg()); |
| 734 | |
| 735 | MachineBasicBlock &B = *MI->getParent(); |
| 736 | DebugLoc DL = MI->getDebugLoc(); |
| 737 | UUPairMap::const_iterator F = PairMap.find(Op0.getReg()); |
| 738 | assert(F != PairMap.end()); |
| 739 | const UUPair &P = F->second; |
| 740 | |
Krzysztof Parzyszek | effcc2f | 2018-05-04 15:04:48 +0000 | [diff] [blame] | 741 | if (!Op1.isReg()) { |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 742 | BuildMI(B, MI, DL, TII->get(Hexagon::A2_tfrsi), P.second) |
Krzysztof Parzyszek | effcc2f | 2018-05-04 15:04:48 +0000 | [diff] [blame] | 743 | .add(Op1); |
| 744 | } else { |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 745 | BuildMI(B, MI, DL, TII->get(TargetOpcode::COPY), P.second) |
| 746 | .addReg(Op1.getReg(), getRegState(Op1), Op1.getSubReg()); |
Krzysztof Parzyszek | effcc2f | 2018-05-04 15:04:48 +0000 | [diff] [blame] | 747 | } |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 748 | |
Krzysztof Parzyszek | effcc2f | 2018-05-04 15:04:48 +0000 | [diff] [blame] | 749 | if (!Op2.isReg()) { |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 750 | BuildMI(B, MI, DL, TII->get(Hexagon::A2_tfrsi), P.first) |
Krzysztof Parzyszek | effcc2f | 2018-05-04 15:04:48 +0000 | [diff] [blame] | 751 | .add(Op2); |
| 752 | } else { |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 753 | BuildMI(B, MI, DL, TII->get(TargetOpcode::COPY), P.first) |
| 754 | .addReg(Op2.getReg(), getRegState(Op2), Op2.getSubReg()); |
Krzysztof Parzyszek | effcc2f | 2018-05-04 15:04:48 +0000 | [diff] [blame] | 755 | } |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 758 | void HexagonSplitDoubleRegs::splitExt(MachineInstr *MI, |
| 759 | const UUPairMap &PairMap) { |
| 760 | MachineOperand &Op0 = MI->getOperand(0); |
| 761 | MachineOperand &Op1 = MI->getOperand(1); |
| 762 | assert(Op0.isReg() && Op1.isReg()); |
| 763 | |
| 764 | MachineBasicBlock &B = *MI->getParent(); |
| 765 | DebugLoc DL = MI->getDebugLoc(); |
| 766 | UUPairMap::const_iterator F = PairMap.find(Op0.getReg()); |
| 767 | assert(F != PairMap.end()); |
| 768 | const UUPair &P = F->second; |
| 769 | unsigned RS = getRegState(Op1); |
| 770 | |
| 771 | BuildMI(B, MI, DL, TII->get(TargetOpcode::COPY), P.first) |
| 772 | .addReg(Op1.getReg(), RS & ~RegState::Kill, Op1.getSubReg()); |
| 773 | BuildMI(B, MI, DL, TII->get(Hexagon::S2_asr_i_r), P.second) |
| 774 | .addReg(Op1.getReg(), RS, Op1.getSubReg()) |
| 775 | .addImm(31); |
| 776 | } |
| 777 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 778 | void HexagonSplitDoubleRegs::splitShift(MachineInstr *MI, |
| 779 | const UUPairMap &PairMap) { |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 780 | using namespace Hexagon; |
| 781 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 782 | MachineOperand &Op0 = MI->getOperand(0); |
| 783 | MachineOperand &Op1 = MI->getOperand(1); |
| 784 | MachineOperand &Op2 = MI->getOperand(2); |
| 785 | assert(Op0.isReg() && Op1.isReg() && Op2.isImm()); |
| 786 | int64_t Sh64 = Op2.getImm(); |
| 787 | assert(Sh64 >= 0 && Sh64 < 64); |
| 788 | unsigned S = Sh64; |
| 789 | |
| 790 | UUPairMap::const_iterator F = PairMap.find(Op0.getReg()); |
| 791 | assert(F != PairMap.end()); |
| 792 | const UUPair &P = F->second; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 793 | Register LoR = P.first; |
| 794 | Register HiR = P.second; |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 795 | |
| 796 | unsigned Opc = MI->getOpcode(); |
| 797 | bool Right = (Opc == S2_lsr_i_p || Opc == S2_asr_i_p); |
| 798 | bool Left = !Right; |
| 799 | bool Signed = (Opc == S2_asr_i_p); |
| 800 | |
| 801 | MachineBasicBlock &B = *MI->getParent(); |
| 802 | DebugLoc DL = MI->getDebugLoc(); |
| 803 | unsigned RS = getRegState(Op1); |
| 804 | unsigned ShiftOpc = Left ? S2_asl_i_r |
| 805 | : (Signed ? S2_asr_i_r : S2_lsr_i_r); |
Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 806 | unsigned LoSR = isub_lo; |
| 807 | unsigned HiSR = isub_hi; |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 808 | |
| 809 | if (S == 0) { |
| 810 | // No shift, subregister copy. |
| 811 | BuildMI(B, MI, DL, TII->get(TargetOpcode::COPY), LoR) |
| 812 | .addReg(Op1.getReg(), RS & ~RegState::Kill, LoSR); |
| 813 | BuildMI(B, MI, DL, TII->get(TargetOpcode::COPY), HiR) |
| 814 | .addReg(Op1.getReg(), RS, HiSR); |
| 815 | } else if (S < 32) { |
| 816 | const TargetRegisterClass *IntRC = &IntRegsRegClass; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 817 | Register TmpR = MRI->createVirtualRegister(IntRC); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 818 | // Expansion: |
| 819 | // Shift left: DR = shl R, #s |
| 820 | // LoR = shl R.lo, #s |
| 821 | // TmpR = extractu R.lo, #s, #32-s |
| 822 | // HiR = or (TmpR, asl(R.hi, #s)) |
| 823 | // Shift right: DR = shr R, #s |
| 824 | // HiR = shr R.hi, #s |
| 825 | // TmpR = shr R.lo, #s |
| 826 | // LoR = insert TmpR, R.hi, #s, #32-s |
| 827 | |
| 828 | // Shift left: |
| 829 | // LoR = shl R.lo, #s |
| 830 | // Shift right: |
| 831 | // TmpR = shr R.lo, #s |
| 832 | |
| 833 | // Make a special case for A2_aslh and A2_asrh (they are predicable as |
| 834 | // opposed to S2_asl_i_r/S2_asr_i_r). |
| 835 | if (S == 16 && Left) |
| 836 | BuildMI(B, MI, DL, TII->get(A2_aslh), LoR) |
| 837 | .addReg(Op1.getReg(), RS & ~RegState::Kill, LoSR); |
| 838 | else if (S == 16 && Signed) |
| 839 | BuildMI(B, MI, DL, TII->get(A2_asrh), TmpR) |
| 840 | .addReg(Op1.getReg(), RS & ~RegState::Kill, LoSR); |
| 841 | else |
| 842 | BuildMI(B, MI, DL, TII->get(ShiftOpc), (Left ? LoR : TmpR)) |
| 843 | .addReg(Op1.getReg(), RS & ~RegState::Kill, LoSR) |
| 844 | .addImm(S); |
| 845 | |
| 846 | if (Left) { |
| 847 | // TmpR = extractu R.lo, #s, #32-s |
| 848 | BuildMI(B, MI, DL, TII->get(S2_extractu), TmpR) |
| 849 | .addReg(Op1.getReg(), RS & ~RegState::Kill, LoSR) |
| 850 | .addImm(S) |
| 851 | .addImm(32-S); |
| 852 | // HiR = or (TmpR, asl(R.hi, #s)) |
| 853 | BuildMI(B, MI, DL, TII->get(S2_asl_i_r_or), HiR) |
| 854 | .addReg(TmpR) |
| 855 | .addReg(Op1.getReg(), RS, HiSR) |
| 856 | .addImm(S); |
| 857 | } else { |
| 858 | // HiR = shr R.hi, #s |
| 859 | BuildMI(B, MI, DL, TII->get(ShiftOpc), HiR) |
| 860 | .addReg(Op1.getReg(), RS & ~RegState::Kill, HiSR) |
| 861 | .addImm(S); |
| 862 | // LoR = insert TmpR, R.hi, #s, #32-s |
| 863 | BuildMI(B, MI, DL, TII->get(S2_insert), LoR) |
| 864 | .addReg(TmpR) |
| 865 | .addReg(Op1.getReg(), RS, HiSR) |
| 866 | .addImm(S) |
| 867 | .addImm(32-S); |
| 868 | } |
| 869 | } else if (S == 32) { |
| 870 | BuildMI(B, MI, DL, TII->get(TargetOpcode::COPY), (Left ? HiR : LoR)) |
| 871 | .addReg(Op1.getReg(), RS & ~RegState::Kill, (Left ? LoSR : HiSR)); |
| 872 | if (!Signed) |
| 873 | BuildMI(B, MI, DL, TII->get(A2_tfrsi), (Left ? LoR : HiR)) |
| 874 | .addImm(0); |
| 875 | else // Must be right shift. |
| 876 | BuildMI(B, MI, DL, TII->get(S2_asr_i_r), HiR) |
| 877 | .addReg(Op1.getReg(), RS, HiSR) |
| 878 | .addImm(31); |
| 879 | } else if (S < 64) { |
| 880 | S -= 32; |
| 881 | if (S == 16 && Left) |
| 882 | BuildMI(B, MI, DL, TII->get(A2_aslh), HiR) |
| 883 | .addReg(Op1.getReg(), RS & ~RegState::Kill, LoSR); |
| 884 | else if (S == 16 && Signed) |
| 885 | BuildMI(B, MI, DL, TII->get(A2_asrh), LoR) |
| 886 | .addReg(Op1.getReg(), RS & ~RegState::Kill, HiSR); |
| 887 | else |
| 888 | BuildMI(B, MI, DL, TII->get(ShiftOpc), (Left ? HiR : LoR)) |
| 889 | .addReg(Op1.getReg(), RS & ~RegState::Kill, (Left ? LoSR : HiSR)) |
| 890 | .addImm(S); |
| 891 | |
| 892 | if (Signed) |
| 893 | BuildMI(B, MI, DL, TII->get(S2_asr_i_r), HiR) |
| 894 | .addReg(Op1.getReg(), RS, HiSR) |
| 895 | .addImm(31); |
| 896 | else |
| 897 | BuildMI(B, MI, DL, TII->get(A2_tfrsi), (Left ? LoR : HiR)) |
| 898 | .addImm(0); |
| 899 | } |
| 900 | } |
| 901 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 902 | void HexagonSplitDoubleRegs::splitAslOr(MachineInstr *MI, |
| 903 | const UUPairMap &PairMap) { |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 904 | using namespace Hexagon; |
| 905 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 906 | MachineOperand &Op0 = MI->getOperand(0); |
| 907 | MachineOperand &Op1 = MI->getOperand(1); |
| 908 | MachineOperand &Op2 = MI->getOperand(2); |
| 909 | MachineOperand &Op3 = MI->getOperand(3); |
| 910 | assert(Op0.isReg() && Op1.isReg() && Op2.isReg() && Op3.isImm()); |
| 911 | int64_t Sh64 = Op3.getImm(); |
| 912 | assert(Sh64 >= 0 && Sh64 < 64); |
| 913 | unsigned S = Sh64; |
| 914 | |
| 915 | UUPairMap::const_iterator F = PairMap.find(Op0.getReg()); |
| 916 | assert(F != PairMap.end()); |
| 917 | const UUPair &P = F->second; |
| 918 | unsigned LoR = P.first; |
| 919 | unsigned HiR = P.second; |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 920 | |
| 921 | MachineBasicBlock &B = *MI->getParent(); |
| 922 | DebugLoc DL = MI->getDebugLoc(); |
| 923 | unsigned RS1 = getRegState(Op1); |
| 924 | unsigned RS2 = getRegState(Op2); |
| 925 | const TargetRegisterClass *IntRC = &IntRegsRegClass; |
| 926 | |
Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 927 | unsigned LoSR = isub_lo; |
| 928 | unsigned HiSR = isub_hi; |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 929 | |
| 930 | // Op0 = S2_asl_i_p_or Op1, Op2, Op3 |
| 931 | // means: Op0 = or (Op1, asl(Op2, Op3)) |
| 932 | |
| 933 | // Expansion of |
| 934 | // DR = or (R1, asl(R2, #s)) |
| 935 | // |
| 936 | // LoR = or (R1.lo, asl(R2.lo, #s)) |
| 937 | // Tmp1 = extractu R2.lo, #s, #32-s |
| 938 | // Tmp2 = or R1.hi, Tmp1 |
| 939 | // HiR = or (Tmp2, asl(R2.hi, #s)) |
| 940 | |
| 941 | if (S == 0) { |
| 942 | // DR = or (R1, asl(R2, #0)) |
| 943 | // -> or (R1, R2) |
| 944 | // i.e. LoR = or R1.lo, R2.lo |
| 945 | // HiR = or R1.hi, R2.hi |
| 946 | BuildMI(B, MI, DL, TII->get(A2_or), LoR) |
| 947 | .addReg(Op1.getReg(), RS1 & ~RegState::Kill, LoSR) |
| 948 | .addReg(Op2.getReg(), RS2 & ~RegState::Kill, LoSR); |
| 949 | BuildMI(B, MI, DL, TII->get(A2_or), HiR) |
| 950 | .addReg(Op1.getReg(), RS1, HiSR) |
| 951 | .addReg(Op2.getReg(), RS2, HiSR); |
| 952 | } else if (S < 32) { |
| 953 | BuildMI(B, MI, DL, TII->get(S2_asl_i_r_or), LoR) |
| 954 | .addReg(Op1.getReg(), RS1 & ~RegState::Kill, LoSR) |
| 955 | .addReg(Op2.getReg(), RS2 & ~RegState::Kill, LoSR) |
| 956 | .addImm(S); |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 957 | Register TmpR1 = MRI->createVirtualRegister(IntRC); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 958 | BuildMI(B, MI, DL, TII->get(S2_extractu), TmpR1) |
| 959 | .addReg(Op2.getReg(), RS2 & ~RegState::Kill, LoSR) |
| 960 | .addImm(S) |
| 961 | .addImm(32-S); |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 962 | Register TmpR2 = MRI->createVirtualRegister(IntRC); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 963 | BuildMI(B, MI, DL, TII->get(A2_or), TmpR2) |
| 964 | .addReg(Op1.getReg(), RS1, HiSR) |
| 965 | .addReg(TmpR1); |
| 966 | BuildMI(B, MI, DL, TII->get(S2_asl_i_r_or), HiR) |
| 967 | .addReg(TmpR2) |
| 968 | .addReg(Op2.getReg(), RS2, HiSR) |
| 969 | .addImm(S); |
| 970 | } else if (S == 32) { |
| 971 | // DR = or (R1, asl(R2, #32)) |
| 972 | // -> or R1, R2.lo |
| 973 | // LoR = R1.lo |
| 974 | // HiR = or R1.hi, R2.lo |
| 975 | BuildMI(B, MI, DL, TII->get(TargetOpcode::COPY), LoR) |
| 976 | .addReg(Op1.getReg(), RS1 & ~RegState::Kill, LoSR); |
| 977 | BuildMI(B, MI, DL, TII->get(A2_or), HiR) |
| 978 | .addReg(Op1.getReg(), RS1, HiSR) |
| 979 | .addReg(Op2.getReg(), RS2, LoSR); |
| 980 | } else if (S < 64) { |
| 981 | // DR = or (R1, asl(R2, #s)) |
| 982 | // |
| 983 | // LoR = R1:lo |
| 984 | // HiR = or (R1:hi, asl(R2:lo, #s-32)) |
| 985 | S -= 32; |
| 986 | BuildMI(B, MI, DL, TII->get(TargetOpcode::COPY), LoR) |
| 987 | .addReg(Op1.getReg(), RS1 & ~RegState::Kill, LoSR); |
| 988 | BuildMI(B, MI, DL, TII->get(S2_asl_i_r_or), HiR) |
| 989 | .addReg(Op1.getReg(), RS1, HiSR) |
| 990 | .addReg(Op2.getReg(), RS2, LoSR) |
| 991 | .addImm(S); |
| 992 | } |
| 993 | } |
| 994 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 995 | bool HexagonSplitDoubleRegs::splitInstr(MachineInstr *MI, |
| 996 | const UUPairMap &PairMap) { |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 997 | using namespace Hexagon; |
| 998 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 999 | LLVM_DEBUG(dbgs() << "Splitting: " << *MI); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1000 | bool Split = false; |
| 1001 | unsigned Opc = MI->getOpcode(); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1002 | |
| 1003 | switch (Opc) { |
| 1004 | case TargetOpcode::PHI: |
| 1005 | case TargetOpcode::COPY: { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 1006 | Register DstR = MI->getOperand(0).getReg(); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1007 | if (MRI->getRegClass(DstR) == DoubleRC) { |
Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 1008 | createHalfInstr(Opc, MI, PairMap, isub_lo); |
| 1009 | createHalfInstr(Opc, MI, PairMap, isub_hi); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1010 | Split = true; |
| 1011 | } |
| 1012 | break; |
| 1013 | } |
| 1014 | case A2_andp: |
Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 1015 | createHalfInstr(A2_and, MI, PairMap, isub_lo); |
| 1016 | createHalfInstr(A2_and, MI, PairMap, isub_hi); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1017 | Split = true; |
| 1018 | break; |
| 1019 | case A2_orp: |
Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 1020 | createHalfInstr(A2_or, MI, PairMap, isub_lo); |
| 1021 | createHalfInstr(A2_or, MI, PairMap, isub_hi); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1022 | Split = true; |
| 1023 | break; |
| 1024 | case A2_xorp: |
Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 1025 | createHalfInstr(A2_xor, MI, PairMap, isub_lo); |
| 1026 | createHalfInstr(A2_xor, MI, PairMap, isub_hi); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1027 | Split = true; |
| 1028 | break; |
| 1029 | |
| 1030 | case L2_loadrd_io: |
| 1031 | case L2_loadrd_pi: |
| 1032 | case S2_storerd_io: |
| 1033 | case S2_storerd_pi: |
| 1034 | splitMemRef(MI, PairMap); |
| 1035 | Split = true; |
| 1036 | break; |
| 1037 | |
| 1038 | case A2_tfrpi: |
Krzysztof Parzyszek | a338650 | 2016-08-10 16:46:36 +0000 | [diff] [blame] | 1039 | case CONST64: |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1040 | splitImmediate(MI, PairMap); |
| 1041 | Split = true; |
| 1042 | break; |
| 1043 | |
| 1044 | case A2_combineii: |
| 1045 | case A4_combineir: |
| 1046 | case A4_combineii: |
| 1047 | case A4_combineri: |
| 1048 | case A2_combinew: |
| 1049 | splitCombine(MI, PairMap); |
| 1050 | Split = true; |
| 1051 | break; |
| 1052 | |
| 1053 | case A2_sxtw: |
| 1054 | splitExt(MI, PairMap); |
| 1055 | Split = true; |
| 1056 | break; |
| 1057 | |
| 1058 | case S2_asl_i_p: |
| 1059 | case S2_asr_i_p: |
| 1060 | case S2_lsr_i_p: |
| 1061 | splitShift(MI, PairMap); |
| 1062 | Split = true; |
| 1063 | break; |
| 1064 | |
| 1065 | case S2_asl_i_p_or: |
| 1066 | splitAslOr(MI, PairMap); |
| 1067 | Split = true; |
| 1068 | break; |
| 1069 | |
| 1070 | default: |
| 1071 | llvm_unreachable("Instruction not splitable"); |
| 1072 | return false; |
| 1073 | } |
| 1074 | |
| 1075 | return Split; |
| 1076 | } |
| 1077 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1078 | void HexagonSplitDoubleRegs::replaceSubregUses(MachineInstr *MI, |
| 1079 | const UUPairMap &PairMap) { |
| 1080 | for (auto &Op : MI->operands()) { |
| 1081 | if (!Op.isReg() || !Op.isUse() || !Op.getSubReg()) |
| 1082 | continue; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 1083 | Register R = Op.getReg(); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1084 | UUPairMap::const_iterator F = PairMap.find(R); |
| 1085 | if (F == PairMap.end()) |
| 1086 | continue; |
| 1087 | const UUPair &P = F->second; |
| 1088 | switch (Op.getSubReg()) { |
Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 1089 | case Hexagon::isub_lo: |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1090 | Op.setReg(P.first); |
| 1091 | break; |
Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 1092 | case Hexagon::isub_hi: |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1093 | Op.setReg(P.second); |
| 1094 | break; |
| 1095 | } |
| 1096 | Op.setSubReg(0); |
| 1097 | } |
| 1098 | } |
| 1099 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1100 | void HexagonSplitDoubleRegs::collapseRegPairs(MachineInstr *MI, |
| 1101 | const UUPairMap &PairMap) { |
| 1102 | MachineBasicBlock &B = *MI->getParent(); |
| 1103 | DebugLoc DL = MI->getDebugLoc(); |
| 1104 | |
| 1105 | for (auto &Op : MI->operands()) { |
| 1106 | if (!Op.isReg() || !Op.isUse()) |
| 1107 | continue; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 1108 | Register R = Op.getReg(); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 1109 | if (!Register::isVirtualRegister(R)) |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1110 | continue; |
| 1111 | if (MRI->getRegClass(R) != DoubleRC || Op.getSubReg()) |
| 1112 | continue; |
| 1113 | UUPairMap::const_iterator F = PairMap.find(R); |
| 1114 | if (F == PairMap.end()) |
| 1115 | continue; |
| 1116 | const UUPair &Pr = F->second; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 1117 | Register NewDR = MRI->createVirtualRegister(DoubleRC); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1118 | BuildMI(B, MI, DL, TII->get(TargetOpcode::REG_SEQUENCE), NewDR) |
| 1119 | .addReg(Pr.first) |
Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 1120 | .addImm(Hexagon::isub_lo) |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1121 | .addReg(Pr.second) |
Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 1122 | .addImm(Hexagon::isub_hi); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1123 | Op.setReg(NewDR); |
| 1124 | } |
| 1125 | } |
| 1126 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1127 | bool HexagonSplitDoubleRegs::splitPartition(const USet &Part) { |
Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 1128 | using MISet = std::set<MachineInstr *>; |
| 1129 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1130 | const TargetRegisterClass *IntRC = &Hexagon::IntRegsRegClass; |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1131 | bool Changed = false; |
| 1132 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1133 | LLVM_DEBUG(dbgs() << "Splitting partition: "; |
| 1134 | dump_partition(dbgs(), Part, *TRI); dbgs() << '\n'); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1135 | |
| 1136 | UUPairMap PairMap; |
| 1137 | |
| 1138 | MISet SplitIns; |
| 1139 | for (unsigned DR : Part) { |
| 1140 | MachineInstr *DefI = MRI->getVRegDef(DR); |
| 1141 | SplitIns.insert(DefI); |
| 1142 | |
| 1143 | // Collect all instructions, including fixed ones. We won't split them, |
| 1144 | // but we need to visit them again to insert the REG_SEQUENCE instructions. |
| 1145 | for (auto U = MRI->use_nodbg_begin(DR), W = MRI->use_nodbg_end(); |
| 1146 | U != W; ++U) |
| 1147 | SplitIns.insert(U->getParent()); |
| 1148 | |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 1149 | Register LoR = MRI->createVirtualRegister(IntRC); |
| 1150 | Register HiR = MRI->createVirtualRegister(IntRC); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1151 | LLVM_DEBUG(dbgs() << "Created mapping: " << printReg(DR, TRI) << " -> " |
| 1152 | << printReg(HiR, TRI) << ':' << printReg(LoR, TRI) |
| 1153 | << '\n'); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1154 | PairMap.insert(std::make_pair(DR, UUPair(LoR, HiR))); |
| 1155 | } |
| 1156 | |
| 1157 | MISet Erase; |
| 1158 | for (auto MI : SplitIns) { |
| 1159 | if (isFixedInstr(MI)) { |
| 1160 | collapseRegPairs(MI, PairMap); |
| 1161 | } else { |
| 1162 | bool Done = splitInstr(MI, PairMap); |
| 1163 | if (Done) |
| 1164 | Erase.insert(MI); |
| 1165 | Changed |= Done; |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | for (unsigned DR : Part) { |
| 1170 | // Before erasing "double" instructions, revisit all uses of the double |
| 1171 | // registers in this partition, and replace all uses of them with subre- |
| 1172 | // gisters, with the corresponding single registers. |
| 1173 | MISet Uses; |
| 1174 | for (auto U = MRI->use_nodbg_begin(DR), W = MRI->use_nodbg_end(); |
| 1175 | U != W; ++U) |
| 1176 | Uses.insert(U->getParent()); |
| 1177 | for (auto M : Uses) |
| 1178 | replaceSubregUses(M, PairMap); |
| 1179 | } |
| 1180 | |
| 1181 | for (auto MI : Erase) { |
| 1182 | MachineBasicBlock *B = MI->getParent(); |
| 1183 | B->erase(MI); |
| 1184 | } |
| 1185 | |
| 1186 | return Changed; |
| 1187 | } |
| 1188 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1189 | bool HexagonSplitDoubleRegs::runOnMachineFunction(MachineFunction &MF) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1190 | if (skipFunction(MF.getFunction())) |
Andrew Kaylor | 5b444a2 | 2016-04-26 19:46:28 +0000 | [diff] [blame] | 1191 | return false; |
| 1192 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1193 | LLVM_DEBUG(dbgs() << "Splitting double registers in function: " |
| 1194 | << MF.getName() << '\n'); |
Krzysztof Parzyszek | bcf0a96 | 2018-03-23 20:11:00 +0000 | [diff] [blame] | 1195 | |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1196 | auto &ST = MF.getSubtarget<HexagonSubtarget>(); |
| 1197 | TRI = ST.getRegisterInfo(); |
| 1198 | TII = ST.getInstrInfo(); |
| 1199 | MRI = &MF.getRegInfo(); |
| 1200 | MLI = &getAnalysis<MachineLoopInfo>(); |
| 1201 | |
| 1202 | UUSetMap P2Rs; |
| 1203 | LoopRegMap IRM; |
| 1204 | |
| 1205 | collectIndRegs(IRM); |
| 1206 | partitionRegisters(P2Rs); |
| 1207 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1208 | LLVM_DEBUG({ |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1209 | dbgs() << "Register partitioning: (partition #0 is fixed)\n"; |
| 1210 | for (UUSetMap::iterator I = P2Rs.begin(), E = P2Rs.end(); I != E; ++I) { |
| 1211 | dbgs() << '#' << I->first << " -> "; |
| 1212 | dump_partition(dbgs(), I->second, *TRI); |
| 1213 | dbgs() << '\n'; |
| 1214 | } |
| 1215 | }); |
| 1216 | |
| 1217 | bool Changed = false; |
| 1218 | int Limit = MaxHSDR; |
| 1219 | |
| 1220 | for (UUSetMap::iterator I = P2Rs.begin(), E = P2Rs.end(); I != E; ++I) { |
| 1221 | if (I->first == 0) |
| 1222 | continue; |
| 1223 | if (Limit >= 0 && Counter >= Limit) |
| 1224 | break; |
| 1225 | USet &Part = I->second; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1226 | LLVM_DEBUG(dbgs() << "Calculating profit for partition #" << I->first |
| 1227 | << '\n'); |
Krzysztof Parzyszek | a7c5f04 | 2015-10-16 20:38:54 +0000 | [diff] [blame] | 1228 | if (!isProfitable(Part, IRM)) |
| 1229 | continue; |
| 1230 | Counter++; |
| 1231 | Changed |= splitPartition(Part); |
| 1232 | } |
| 1233 | |
| 1234 | return Changed; |
| 1235 | } |
| 1236 | |
| 1237 | FunctionPass *llvm::createHexagonSplitDoubleRegs() { |
| 1238 | return new HexagonSplitDoubleRegs(); |
| 1239 | } |