Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 1 | //===------- HexagonCopyToCombine.cpp - Hexagon Copy-To-Combine Pass ------===// |
| 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 | // This pass replaces transfer instructions by combine instructions. |
| 10 | // We walk along a basic block and look for two combinable instructions and try |
| 11 | // to move them together. If we can move them next to each other we do so and |
| 12 | // replace them with a combine instruction. |
| 13 | //===----------------------------------------------------------------------===// |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 14 | #include "llvm/PassSupport.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 15 | #include "Hexagon.h" |
| 16 | #include "HexagonInstrInfo.h" |
| 17 | #include "HexagonMachineFunctionInfo.h" |
| 18 | #include "HexagonRegisterInfo.h" |
| 19 | #include "HexagonSubtarget.h" |
| 20 | #include "HexagonTargetMachine.h" |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/DenseSet.h" |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 24 | #include "llvm/CodeGen/MachineFunction.h" |
| 25 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 26 | #include "llvm/CodeGen/MachineInstr.h" |
| 27 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/Passes.h" |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CodeGen.h" |
| 30 | #include "llvm/Support/CommandLine.h" |
| 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetRegisterInfo.h" |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace llvm; |
| 36 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 37 | #define DEBUG_TYPE "hexagon-copy-combine" |
| 38 | |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 39 | static |
| 40 | cl::opt<bool> IsCombinesDisabled("disable-merge-into-combines", |
| 41 | cl::Hidden, cl::ZeroOrMore, |
| 42 | cl::init(false), |
| 43 | cl::desc("Disable merging into combines")); |
| 44 | static |
Krzysztof Parzyszek | 2a3b2f9 | 2016-01-15 14:08:31 +0000 | [diff] [blame] | 45 | cl::opt<bool> IsConst64Disabled("disable-const64", |
| 46 | cl::Hidden, cl::ZeroOrMore, |
| 47 | cl::init(false), |
| 48 | cl::desc("Disable generation of const64")); |
| 49 | static |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 50 | cl::opt<unsigned> |
| 51 | MaxNumOfInstsBetweenNewValueStoreAndTFR("max-num-inst-between-tfr-and-nv-store", |
| 52 | cl::Hidden, cl::init(4), |
| 53 | cl::desc("Maximum distance between a tfr feeding a store we " |
| 54 | "consider the store still to be newifiable")); |
| 55 | |
| 56 | namespace llvm { |
Colin LeMahieu | 56efafc | 2015-06-15 19:05:35 +0000 | [diff] [blame] | 57 | FunctionPass *createHexagonCopyToCombine(); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 58 | void initializeHexagonCopyToCombinePass(PassRegistry&); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | namespace { |
| 63 | |
| 64 | class HexagonCopyToCombine : public MachineFunctionPass { |
| 65 | const HexagonInstrInfo *TII; |
| 66 | const TargetRegisterInfo *TRI; |
| 67 | bool ShouldCombineAggressively; |
| 68 | |
| 69 | DenseSet<MachineInstr *> PotentiallyNewifiableTFR; |
Krzysztof Parzyszek | 9b7320e | 2016-01-15 13:55:57 +0000 | [diff] [blame] | 70 | SmallVector<MachineInstr *, 8> DbgMItoMove; |
| 71 | |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 72 | public: |
| 73 | static char ID; |
| 74 | |
| 75 | HexagonCopyToCombine() : MachineFunctionPass(ID) { |
| 76 | initializeHexagonCopyToCombinePass(*PassRegistry::getPassRegistry()); |
| 77 | } |
| 78 | |
Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 79 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 80 | MachineFunctionPass::getAnalysisUsage(AU); |
| 81 | } |
| 82 | |
Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 83 | const char *getPassName() const override { |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 84 | return "Hexagon Copy-To-Combine Pass"; |
| 85 | } |
| 86 | |
Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 87 | bool runOnMachineFunction(MachineFunction &Fn) override; |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 88 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 89 | MachineFunctionProperties getRequiredProperties() const override { |
| 90 | return MachineFunctionProperties().set( |
| 91 | MachineFunctionProperties::Property::AllVRegsAllocated); |
| 92 | } |
| 93 | |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 94 | private: |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 95 | MachineInstr *findPairable(MachineInstr &I1, bool &DoInsertAtI1, |
Krzysztof Parzyszek | 2a3b2f9 | 2016-01-15 14:08:31 +0000 | [diff] [blame] | 96 | bool AllowC64); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 97 | |
| 98 | void findPotentialNewifiableTFRs(MachineBasicBlock &); |
| 99 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 100 | void combine(MachineInstr &I1, MachineInstr &I2, |
Krzysztof Parzyszek | 2a3b2f9 | 2016-01-15 14:08:31 +0000 | [diff] [blame] | 101 | MachineBasicBlock::iterator &MI, bool DoInsertAtI1, |
| 102 | bool OptForSize); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 103 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 104 | bool isSafeToMoveTogether(MachineInstr &I1, MachineInstr &I2, |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 105 | unsigned I1DestReg, unsigned I2DestReg, |
| 106 | bool &DoInsertAtI1); |
| 107 | |
| 108 | void emitCombineRR(MachineBasicBlock::iterator &Before, unsigned DestReg, |
| 109 | MachineOperand &HiOperand, MachineOperand &LoOperand); |
| 110 | |
| 111 | void emitCombineRI(MachineBasicBlock::iterator &Before, unsigned DestReg, |
| 112 | MachineOperand &HiOperand, MachineOperand &LoOperand); |
| 113 | |
| 114 | void emitCombineIR(MachineBasicBlock::iterator &Before, unsigned DestReg, |
| 115 | MachineOperand &HiOperand, MachineOperand &LoOperand); |
| 116 | |
| 117 | void emitCombineII(MachineBasicBlock::iterator &Before, unsigned DestReg, |
| 118 | MachineOperand &HiOperand, MachineOperand &LoOperand); |
Krzysztof Parzyszek | 2a3b2f9 | 2016-01-15 14:08:31 +0000 | [diff] [blame] | 119 | |
| 120 | void emitConst64(MachineBasicBlock::iterator &Before, unsigned DestReg, |
| 121 | MachineOperand &HiOperand, MachineOperand &LoOperand); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | } // End anonymous namespace. |
| 125 | |
| 126 | char HexagonCopyToCombine::ID = 0; |
| 127 | |
| 128 | INITIALIZE_PASS(HexagonCopyToCombine, "hexagon-copy-combine", |
| 129 | "Hexagon Copy-To-Combine Pass", false, false) |
| 130 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 131 | static bool isCombinableInstType(MachineInstr &MI, const HexagonInstrInfo *TII, |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 132 | bool ShouldCombineAggressively) { |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 133 | switch (MI.getOpcode()) { |
Colin LeMahieu | 4af437f | 2014-12-09 20:23:30 +0000 | [diff] [blame] | 134 | case Hexagon::A2_tfr: { |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 135 | // A COPY instruction can be combined if its arguments are IntRegs (32bit). |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 136 | const MachineOperand &Op0 = MI.getOperand(0); |
| 137 | const MachineOperand &Op1 = MI.getOperand(1); |
Krzysztof Parzyszek | cd97c98 | 2015-04-22 18:25:53 +0000 | [diff] [blame] | 138 | assert(Op0.isReg() && Op1.isReg()); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 139 | |
Krzysztof Parzyszek | cd97c98 | 2015-04-22 18:25:53 +0000 | [diff] [blame] | 140 | unsigned DestReg = Op0.getReg(); |
| 141 | unsigned SrcReg = Op1.getReg(); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 142 | return Hexagon::IntRegsRegClass.contains(DestReg) && |
Krzysztof Parzyszek | cd97c98 | 2015-04-22 18:25:53 +0000 | [diff] [blame] | 143 | Hexagon::IntRegsRegClass.contains(SrcReg); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Colin LeMahieu | 4af437f | 2014-12-09 20:23:30 +0000 | [diff] [blame] | 146 | case Hexagon::A2_tfrsi: { |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 147 | // A transfer-immediate can be combined if its argument is a signed 8bit |
| 148 | // value. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 149 | const MachineOperand &Op0 = MI.getOperand(0); |
| 150 | const MachineOperand &Op1 = MI.getOperand(1); |
Colin LeMahieu | 2efa2d0 | 2015-03-09 21:48:13 +0000 | [diff] [blame] | 151 | assert(Op0.isReg()); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 152 | |
Colin LeMahieu | 2efa2d0 | 2015-03-09 21:48:13 +0000 | [diff] [blame] | 153 | unsigned DestReg = Op0.getReg(); |
| 154 | // Ensure that TargetFlags are MO_NO_FLAG for a global. This is a |
| 155 | // workaround for an ABI bug that prevents GOT relocations on combine |
| 156 | // instructions |
| 157 | if (!Op1.isImm() && Op1.getTargetFlags() != HexagonII::MO_NO_FLAG) |
| 158 | return false; |
| 159 | |
| 160 | // Only combine constant extended A2_tfrsi if we are in aggressive mode. |
| 161 | bool NotExt = Op1.isImm() && isInt<8>(Op1.getImm()); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 162 | return Hexagon::IntRegsRegClass.contains(DestReg) && |
Colin LeMahieu | 2efa2d0 | 2015-03-09 21:48:13 +0000 | [diff] [blame] | 163 | (ShouldCombineAggressively || NotExt); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 166 | default: |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | return false; |
| 171 | } |
| 172 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 173 | template <unsigned N> static bool isGreaterThanNBitTFRI(const MachineInstr &I) { |
| 174 | if (I.getOpcode() == Hexagon::TFRI64_V4 || |
| 175 | I.getOpcode() == Hexagon::A2_tfrsi) { |
| 176 | const MachineOperand &Op = I.getOperand(1); |
Krzysztof Parzyszek | cd97c98 | 2015-04-22 18:25:53 +0000 | [diff] [blame] | 177 | return !Op.isImm() || !isInt<N>(Op.getImm()); |
| 178 | } |
| 179 | return false; |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /// areCombinableOperations - Returns true if the two instruction can be merge |
| 183 | /// into a combine (ignoring register constraints). |
| 184 | static bool areCombinableOperations(const TargetRegisterInfo *TRI, |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 185 | MachineInstr &HighRegInst, |
| 186 | MachineInstr &LowRegInst, bool AllowC64) { |
| 187 | unsigned HiOpc = HighRegInst.getOpcode(); |
| 188 | unsigned LoOpc = LowRegInst.getOpcode(); |
Krzysztof Parzyszek | e568967 | 2015-04-23 20:26:21 +0000 | [diff] [blame] | 189 | (void)HiOpc; // Fix compiler warning |
| 190 | (void)LoOpc; // Fix compiler warning |
Krzysztof Parzyszek | cd97c98 | 2015-04-22 18:25:53 +0000 | [diff] [blame] | 191 | assert((HiOpc == Hexagon::A2_tfr || HiOpc == Hexagon::A2_tfrsi) && |
| 192 | (LoOpc == Hexagon::A2_tfr || LoOpc == Hexagon::A2_tfrsi) && |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 193 | "Assume individual instructions are of a combinable type"); |
| 194 | |
Krzysztof Parzyszek | 2a3b2f9 | 2016-01-15 14:08:31 +0000 | [diff] [blame] | 195 | if (!AllowC64) { |
| 196 | // There is no combine of two constant extended values. |
| 197 | if (isGreaterThanNBitTFRI<8>(HighRegInst) && |
| 198 | isGreaterThanNBitTFRI<6>(LowRegInst)) |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | // There is a combine of two constant extended values into CONST64, |
| 203 | // provided both constants are true immediates. |
| 204 | if (isGreaterThanNBitTFRI<16>(HighRegInst) && |
| 205 | isGreaterThanNBitTFRI<16>(LowRegInst)) |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 206 | return (HighRegInst.getOperand(1).isImm() && |
| 207 | LowRegInst.getOperand(1).isImm()); |
Krzysztof Parzyszek | 2a3b2f9 | 2016-01-15 14:08:31 +0000 | [diff] [blame] | 208 | |
| 209 | // There is no combine of two constant extended values, unless handled above |
| 210 | // Make both 8-bit size checks to allow both combine (#,##) and combine(##,#) |
Krzysztof Parzyszek | cd97c98 | 2015-04-22 18:25:53 +0000 | [diff] [blame] | 211 | if (isGreaterThanNBitTFRI<8>(HighRegInst) && |
Krzysztof Parzyszek | 2a3b2f9 | 2016-01-15 14:08:31 +0000 | [diff] [blame] | 212 | isGreaterThanNBitTFRI<8>(LowRegInst)) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 213 | return false; |
| 214 | |
| 215 | return true; |
| 216 | } |
| 217 | |
| 218 | static bool isEvenReg(unsigned Reg) { |
| 219 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && |
| 220 | Hexagon::IntRegsRegClass.contains(Reg)); |
| 221 | return (Reg - Hexagon::R0) % 2 == 0; |
| 222 | } |
| 223 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 224 | static void removeKillInfo(MachineInstr &MI, unsigned RegNotKilled) { |
| 225 | for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) { |
| 226 | MachineOperand &Op = MI.getOperand(I); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 227 | if (!Op.isReg() || Op.getReg() != RegNotKilled || !Op.isKill()) |
| 228 | continue; |
| 229 | Op.setIsKill(false); |
| 230 | } |
| 231 | } |
| 232 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 233 | /// Returns true if it is unsafe to move a copy instruction from \p UseReg to |
| 234 | /// \p DestReg over the instruction \p MI. |
| 235 | static bool isUnsafeToMoveAcross(MachineInstr &MI, unsigned UseReg, |
| 236 | unsigned DestReg, |
| 237 | const TargetRegisterInfo *TRI) { |
| 238 | return (UseReg && (MI.modifiesRegister(UseReg, TRI))) || |
| 239 | MI.modifiesRegister(DestReg, TRI) || MI.readsRegister(DestReg, TRI) || |
| 240 | MI.hasUnmodeledSideEffects() || MI.isInlineAsm() || MI.isDebugValue(); |
Krzysztof Parzyszek | cd97c98 | 2015-04-22 18:25:53 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static unsigned UseReg(const MachineOperand& MO) { |
| 244 | return MO.isReg() ? MO.getReg() : 0; |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /// isSafeToMoveTogether - Returns true if it is safe to move I1 next to I2 such |
| 248 | /// that the two instructions can be paired in a combine. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 249 | bool HexagonCopyToCombine::isSafeToMoveTogether(MachineInstr &I1, |
| 250 | MachineInstr &I2, |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 251 | unsigned I1DestReg, |
| 252 | unsigned I2DestReg, |
| 253 | bool &DoInsertAtI1) { |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 254 | unsigned I2UseReg = UseReg(I2.getOperand(1)); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 255 | |
| 256 | // It is not safe to move I1 and I2 into one combine if I2 has a true |
| 257 | // dependence on I1. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 258 | if (I2UseReg && I1.modifiesRegister(I2UseReg, TRI)) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 259 | return false; |
| 260 | |
| 261 | bool isSafe = true; |
| 262 | |
| 263 | // First try to move I2 towards I1. |
| 264 | { |
| 265 | // A reverse_iterator instantiated like below starts before I2, and I1 |
| 266 | // respectively. |
| 267 | // Look at instructions I in between I2 and (excluding) I1. |
| 268 | MachineBasicBlock::reverse_iterator I(I2), |
| 269 | End = --(MachineBasicBlock::reverse_iterator(I1)); |
| 270 | // At 03 we got better results (dhrystone!) by being more conservative. |
| 271 | if (!ShouldCombineAggressively) |
| 272 | End = MachineBasicBlock::reverse_iterator(I1); |
| 273 | // If I2 kills its operand and we move I2 over an instruction that also |
| 274 | // uses I2's use reg we need to modify that (first) instruction to now kill |
| 275 | // this reg. |
| 276 | unsigned KilledOperand = 0; |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 277 | if (I2.killsRegister(I2UseReg)) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 278 | KilledOperand = I2UseReg; |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 279 | MachineInstr *KillingInstr = nullptr; |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 280 | |
| 281 | for (; I != End; ++I) { |
| 282 | // If the intervening instruction I: |
| 283 | // * modifies I2's use reg |
| 284 | // * modifies I2's def reg |
| 285 | // * reads I2's def reg |
| 286 | // * or has unmodelled side effects |
| 287 | // we can't move I2 across it. |
Krzysztof Parzyszek | 9b7320e | 2016-01-15 13:55:57 +0000 | [diff] [blame] | 288 | if (I->isDebugValue()) |
| 289 | continue; |
| 290 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 291 | if (isUnsafeToMoveAcross(*I, I2UseReg, I2DestReg, TRI)) { |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 292 | isSafe = false; |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | // Update first use of the killed operand. |
| 297 | if (!KillingInstr && KilledOperand && |
| 298 | I->readsRegister(KilledOperand, TRI)) |
| 299 | KillingInstr = &*I; |
| 300 | } |
| 301 | if (isSafe) { |
| 302 | // Update the intermediate instruction to with the kill flag. |
| 303 | if (KillingInstr) { |
| 304 | bool Added = KillingInstr->addRegisterKilled(KilledOperand, TRI, true); |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 305 | (void)Added; // suppress compiler warning |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 306 | assert(Added && "Must successfully update kill flag"); |
| 307 | removeKillInfo(I2, KilledOperand); |
| 308 | } |
| 309 | DoInsertAtI1 = true; |
| 310 | return true; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // Try to move I1 towards I2. |
| 315 | { |
| 316 | // Look at instructions I in between I1 and (excluding) I2. |
| 317 | MachineBasicBlock::iterator I(I1), End(I2); |
| 318 | // At O3 we got better results (dhrystone) by being more conservative here. |
| 319 | if (!ShouldCombineAggressively) |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 320 | End = std::next(MachineBasicBlock::iterator(I2)); |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 321 | unsigned I1UseReg = UseReg(I1.getOperand(1)); |
Jyotsna Verma | cceafb2 | 2013-05-28 19:01:45 +0000 | [diff] [blame] | 322 | // Track killed operands. If we move across an instruction that kills our |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 323 | // operand, we need to update the kill information on the moved I1. It kills |
| 324 | // the operand now. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 325 | MachineInstr *KillingInstr = nullptr; |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 326 | unsigned KilledOperand = 0; |
| 327 | |
| 328 | while(++I != End) { |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 329 | MachineInstr &MI = *I; |
| 330 | // If the intervening instruction MI: |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 331 | // * modifies I1's use reg |
| 332 | // * modifies I1's def reg |
| 333 | // * reads I1's def reg |
| 334 | // * or has unmodelled side effects |
| 335 | // We introduce this special case because llvm has no api to remove a |
| 336 | // kill flag for a register (a removeRegisterKilled() analogous to |
| 337 | // addRegisterKilled) that handles aliased register correctly. |
| 338 | // * or has a killed aliased register use of I1's use reg |
Krzysztof Parzyszek | 9b7320e | 2016-01-15 13:55:57 +0000 | [diff] [blame] | 339 | // %D4<def> = A2_tfrpi 16 |
| 340 | // %R6<def> = A2_tfr %R9 |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 341 | // %R8<def> = KILL %R8, %D4<imp-use,kill> |
| 342 | // If we want to move R6 = across the KILL instruction we would have |
| 343 | // to remove the %D4<imp-use,kill> operand. For now, we are |
| 344 | // conservative and disallow the move. |
| 345 | // we can't move I1 across it. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 346 | if (MI.isDebugValue()) { |
| 347 | if (MI.readsRegister(I1DestReg, TRI)) // Move this instruction after I2. |
| 348 | DbgMItoMove.push_back(&MI); |
Krzysztof Parzyszek | 9b7320e | 2016-01-15 13:55:57 +0000 | [diff] [blame] | 349 | continue; |
| 350 | } |
| 351 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 352 | if (isUnsafeToMoveAcross(MI, I1UseReg, I1DestReg, TRI) || |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 353 | // Check for an aliased register kill. Bail out if we see one. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 354 | (!MI.killsRegister(I1UseReg) && MI.killsRegister(I1UseReg, TRI))) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 355 | return false; |
| 356 | |
| 357 | // Check for an exact kill (registers match). |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 358 | if (I1UseReg && MI.killsRegister(I1UseReg)) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 359 | assert(!KillingInstr && "Should only see one killing instruction"); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 360 | KilledOperand = I1UseReg; |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 361 | KillingInstr = &MI; |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | if (KillingInstr) { |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 365 | removeKillInfo(*KillingInstr, KilledOperand); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 366 | // Update I1 to set the kill flag. This flag will later be picked up by |
| 367 | // the new COMBINE instruction. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 368 | bool Added = I1.addRegisterKilled(KilledOperand, TRI); |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 369 | (void)Added; // suppress compiler warning |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 370 | assert(Added && "Must successfully update kill flag"); |
| 371 | } |
| 372 | DoInsertAtI1 = false; |
| 373 | } |
| 374 | |
| 375 | return true; |
| 376 | } |
| 377 | |
| 378 | /// findPotentialNewifiableTFRs - Finds tranfers that feed stores that could be |
| 379 | /// newified. (A use of a 64 bit register define can not be newified) |
| 380 | void |
| 381 | HexagonCopyToCombine::findPotentialNewifiableTFRs(MachineBasicBlock &BB) { |
| 382 | DenseMap<unsigned, MachineInstr *> LastDef; |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 383 | for (MachineInstr &MI : BB) { |
| 384 | if (MI.isDebugValue()) |
Krzysztof Parzyszek | 9b7320e | 2016-01-15 13:55:57 +0000 | [diff] [blame] | 385 | continue; |
| 386 | |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 387 | // Mark TFRs that feed a potential new value store as such. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 388 | if (TII->mayBeNewStore(&MI)) { |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 389 | // Look for uses of TFR instructions. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 390 | for (unsigned OpdIdx = 0, OpdE = MI.getNumOperands(); OpdIdx != OpdE; |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 391 | ++OpdIdx) { |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 392 | MachineOperand &Op = MI.getOperand(OpdIdx); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 393 | |
| 394 | // Skip over anything except register uses. |
| 395 | if (!Op.isReg() || !Op.isUse() || !Op.getReg()) |
| 396 | continue; |
| 397 | |
| 398 | // Look for the defining instruction. |
| 399 | unsigned Reg = Op.getReg(); |
| 400 | MachineInstr *DefInst = LastDef[Reg]; |
| 401 | if (!DefInst) |
| 402 | continue; |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 403 | if (!isCombinableInstType(*DefInst, TII, ShouldCombineAggressively)) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 404 | continue; |
| 405 | |
| 406 | // Only close newifiable stores should influence the decision. |
Krzysztof Parzyszek | 9b7320e | 2016-01-15 13:55:57 +0000 | [diff] [blame] | 407 | // Ignore the debug instructions in between. |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 408 | MachineBasicBlock::iterator It(DefInst); |
| 409 | unsigned NumInstsToDef = 0; |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 410 | while (&*It != &MI) { |
Krzysztof Parzyszek | 9b7320e | 2016-01-15 13:55:57 +0000 | [diff] [blame] | 411 | if (!It->isDebugValue()) |
| 412 | ++NumInstsToDef; |
Krzysztof Parzyszek | 14f9535e | 2016-01-21 12:45:17 +0000 | [diff] [blame] | 413 | ++It; |
Krzysztof Parzyszek | 9b7320e | 2016-01-15 13:55:57 +0000 | [diff] [blame] | 414 | } |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 415 | |
| 416 | if (NumInstsToDef > MaxNumOfInstsBetweenNewValueStoreAndTFR) |
| 417 | continue; |
| 418 | |
| 419 | PotentiallyNewifiableTFR.insert(DefInst); |
| 420 | } |
| 421 | // Skip to next instruction. |
| 422 | continue; |
| 423 | } |
| 424 | |
| 425 | // Put instructions that last defined integer or double registers into the |
| 426 | // map. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 427 | for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) { |
| 428 | MachineOperand &Op = MI.getOperand(I); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 429 | if (!Op.isReg() || !Op.isDef() || !Op.getReg()) |
| 430 | continue; |
| 431 | unsigned Reg = Op.getReg(); |
| 432 | if (Hexagon::DoubleRegsRegClass.contains(Reg)) { |
| 433 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 434 | LastDef[*SubRegs] = &MI; |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 435 | } |
| 436 | } else if (Hexagon::IntRegsRegClass.contains(Reg)) |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 437 | LastDef[Reg] = &MI; |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | bool HexagonCopyToCombine::runOnMachineFunction(MachineFunction &MF) { |
| 443 | |
| 444 | if (IsCombinesDisabled) return false; |
| 445 | |
| 446 | bool HasChanged = false; |
| 447 | |
| 448 | // Get target info. |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 449 | TRI = MF.getSubtarget().getRegisterInfo(); |
Eric Christopher | 12a5c0d | 2015-02-02 18:46:29 +0000 | [diff] [blame] | 450 | TII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo(); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 451 | |
Krzysztof Parzyszek | 2a3b2f9 | 2016-01-15 14:08:31 +0000 | [diff] [blame] | 452 | const Function *F = MF.getFunction(); |
| 453 | bool OptForSize = F->hasFnAttribute(Attribute::OptimizeForSize); |
| 454 | |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 455 | // Combine aggressively (for code size) |
| 456 | ShouldCombineAggressively = |
| 457 | MF.getTarget().getOptLevel() <= CodeGenOpt::Default; |
| 458 | |
| 459 | // Traverse basic blocks. |
| 460 | for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); BI != BE; |
| 461 | ++BI) { |
| 462 | PotentiallyNewifiableTFR.clear(); |
| 463 | findPotentialNewifiableTFRs(*BI); |
| 464 | |
| 465 | // Traverse instructions in basic block. |
| 466 | for(MachineBasicBlock::iterator MI = BI->begin(), End = BI->end(); |
| 467 | MI != End;) { |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 468 | MachineInstr &I1 = *MI++; |
Krzysztof Parzyszek | 9b7320e | 2016-01-15 13:55:57 +0000 | [diff] [blame] | 469 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 470 | if (I1.isDebugValue()) |
Krzysztof Parzyszek | 9b7320e | 2016-01-15 13:55:57 +0000 | [diff] [blame] | 471 | continue; |
| 472 | |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 473 | // Don't combine a TFR whose user could be newified (instructions that |
| 474 | // define double registers can not be newified - Programmer's Ref Manual |
| 475 | // 5.4.2 New-value stores). |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 476 | if (ShouldCombineAggressively && PotentiallyNewifiableTFR.count(&I1)) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 477 | continue; |
| 478 | |
| 479 | // Ignore instructions that are not combinable. |
| 480 | if (!isCombinableInstType(I1, TII, ShouldCombineAggressively)) |
| 481 | continue; |
| 482 | |
| 483 | // Find a second instruction that can be merged into a combine |
Krzysztof Parzyszek | 9b7320e | 2016-01-15 13:55:57 +0000 | [diff] [blame] | 484 | // instruction. In addition, also find all the debug instructions that |
| 485 | // need to be moved along with it. |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 486 | bool DoInsertAtI1 = false; |
Krzysztof Parzyszek | 9b7320e | 2016-01-15 13:55:57 +0000 | [diff] [blame] | 487 | DbgMItoMove.clear(); |
Krzysztof Parzyszek | 2a3b2f9 | 2016-01-15 14:08:31 +0000 | [diff] [blame] | 488 | MachineInstr *I2 = findPairable(I1, DoInsertAtI1, OptForSize); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 489 | if (I2) { |
| 490 | HasChanged = true; |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 491 | combine(I1, *I2, MI, DoInsertAtI1, OptForSize); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | return HasChanged; |
| 497 | } |
| 498 | |
| 499 | /// findPairable - Returns an instruction that can be merged with \p I1 into a |
| 500 | /// COMBINE instruction or 0 if no such instruction can be found. Returns true |
| 501 | /// in \p DoInsertAtI1 if the combine must be inserted at instruction \p I1 |
| 502 | /// false if the combine must be inserted at the returned instruction. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 503 | MachineInstr *HexagonCopyToCombine::findPairable(MachineInstr &I1, |
Krzysztof Parzyszek | 2a3b2f9 | 2016-01-15 14:08:31 +0000 | [diff] [blame] | 504 | bool &DoInsertAtI1, |
| 505 | bool AllowC64) { |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 506 | MachineBasicBlock::iterator I2 = std::next(MachineBasicBlock::iterator(I1)); |
Krzysztof Parzyszek | 9b7320e | 2016-01-15 13:55:57 +0000 | [diff] [blame] | 507 | |
| 508 | while (I2->isDebugValue()) |
| 509 | ++I2; |
| 510 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 511 | unsigned I1DestReg = I1.getOperand(0).getReg(); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 512 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 513 | for (MachineBasicBlock::iterator End = I1.getParent()->end(); I2 != End; |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 514 | ++I2) { |
| 515 | // Bail out early if we see a second definition of I1DestReg. |
| 516 | if (I2->modifiesRegister(I1DestReg, TRI)) |
| 517 | break; |
| 518 | |
| 519 | // Ignore non-combinable instructions. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 520 | if (!isCombinableInstType(*I2, TII, ShouldCombineAggressively)) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 521 | continue; |
| 522 | |
| 523 | // Don't combine a TFR whose user could be newified. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 524 | if (ShouldCombineAggressively && PotentiallyNewifiableTFR.count(&*I2)) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 525 | continue; |
| 526 | |
| 527 | unsigned I2DestReg = I2->getOperand(0).getReg(); |
| 528 | |
| 529 | // Check that registers are adjacent and that the first destination register |
| 530 | // is even. |
| 531 | bool IsI1LowReg = (I2DestReg - I1DestReg) == 1; |
| 532 | bool IsI2LowReg = (I1DestReg - I2DestReg) == 1; |
| 533 | unsigned FirstRegIndex = IsI1LowReg ? I1DestReg : I2DestReg; |
| 534 | if ((!IsI1LowReg && !IsI2LowReg) || !isEvenReg(FirstRegIndex)) |
| 535 | continue; |
| 536 | |
| 537 | // Check that the two instructions are combinable. V4 allows more |
| 538 | // instructions to be merged into a combine. |
Krzysztof Parzyszek | 9b7320e | 2016-01-15 13:55:57 +0000 | [diff] [blame] | 539 | // The order matters because in a A2_tfrsi we might can encode a int8 as |
| 540 | // the hi reg operand but only a uint6 as the low reg operand. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 541 | if ((IsI2LowReg && !areCombinableOperations(TRI, I1, *I2, AllowC64)) || |
| 542 | (IsI1LowReg && !areCombinableOperations(TRI, *I2, I1, AllowC64))) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 543 | break; |
| 544 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 545 | if (isSafeToMoveTogether(I1, *I2, I1DestReg, I2DestReg, DoInsertAtI1)) |
| 546 | return &*I2; |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 547 | |
| 548 | // Not safe. Stop searching. |
| 549 | break; |
| 550 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 551 | return nullptr; |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 554 | void HexagonCopyToCombine::combine(MachineInstr &I1, MachineInstr &I2, |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 555 | MachineBasicBlock::iterator &MI, |
Krzysztof Parzyszek | 2a3b2f9 | 2016-01-15 14:08:31 +0000 | [diff] [blame] | 556 | bool DoInsertAtI1, bool OptForSize) { |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 557 | // We are going to delete I2. If MI points to I2 advance it to the next |
| 558 | // instruction. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 559 | if (MI == I2.getIterator()) |
| 560 | ++MI; |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 561 | |
| 562 | // Figure out whether I1 or I2 goes into the lowreg part. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 563 | unsigned I1DestReg = I1.getOperand(0).getReg(); |
| 564 | unsigned I2DestReg = I2.getOperand(0).getReg(); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 565 | bool IsI1Loreg = (I2DestReg - I1DestReg) == 1; |
| 566 | unsigned LoRegDef = IsI1Loreg ? I1DestReg : I2DestReg; |
| 567 | |
| 568 | // Get the double word register. |
| 569 | unsigned DoubleRegDest = |
| 570 | TRI->getMatchingSuperReg(LoRegDef, Hexagon::subreg_loreg, |
| 571 | &Hexagon::DoubleRegsRegClass); |
| 572 | assert(DoubleRegDest != 0 && "Expect a valid register"); |
| 573 | |
| 574 | |
| 575 | // Setup source operands. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 576 | MachineOperand &LoOperand = IsI1Loreg ? I1.getOperand(1) : I2.getOperand(1); |
| 577 | MachineOperand &HiOperand = IsI1Loreg ? I2.getOperand(1) : I1.getOperand(1); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 578 | |
| 579 | // Figure out which source is a register and which a constant. |
| 580 | bool IsHiReg = HiOperand.isReg(); |
| 581 | bool IsLoReg = LoOperand.isReg(); |
| 582 | |
Krzysztof Parzyszek | 2a3b2f9 | 2016-01-15 14:08:31 +0000 | [diff] [blame] | 583 | // There is a combine of two constant extended values into CONST64. |
| 584 | bool IsC64 = OptForSize && LoOperand.isImm() && HiOperand.isImm() && |
| 585 | isGreaterThanNBitTFRI<16>(I1) && isGreaterThanNBitTFRI<16>(I2); |
| 586 | |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 587 | MachineBasicBlock::iterator InsertPt(DoInsertAtI1 ? I1 : I2); |
| 588 | // Emit combine. |
| 589 | if (IsHiReg && IsLoReg) |
| 590 | emitCombineRR(InsertPt, DoubleRegDest, HiOperand, LoOperand); |
| 591 | else if (IsHiReg) |
| 592 | emitCombineRI(InsertPt, DoubleRegDest, HiOperand, LoOperand); |
| 593 | else if (IsLoReg) |
| 594 | emitCombineIR(InsertPt, DoubleRegDest, HiOperand, LoOperand); |
Krzysztof Parzyszek | 2a3b2f9 | 2016-01-15 14:08:31 +0000 | [diff] [blame] | 595 | else if (IsC64 && !IsConst64Disabled) |
| 596 | emitConst64(InsertPt, DoubleRegDest, HiOperand, LoOperand); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 597 | else |
| 598 | emitCombineII(InsertPt, DoubleRegDest, HiOperand, LoOperand); |
| 599 | |
Krzysztof Parzyszek | 9b7320e | 2016-01-15 13:55:57 +0000 | [diff] [blame] | 600 | // Move debug instructions along with I1 if it's being |
| 601 | // moved towards I2. |
| 602 | if (!DoInsertAtI1 && DbgMItoMove.size() != 0) { |
| 603 | // Insert debug instructions at the new location before I2. |
| 604 | MachineBasicBlock *BB = InsertPt->getParent(); |
| 605 | for (auto NewMI : DbgMItoMove) { |
| 606 | // If iterator MI is pointing to DEBUG_VAL, make sure |
| 607 | // MI now points to next relevant instruction. |
| 608 | if (NewMI == (MachineInstr*)MI) |
| 609 | ++MI; |
| 610 | BB->splice(InsertPt, BB, NewMI); |
| 611 | } |
| 612 | } |
| 613 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 614 | I1.eraseFromParent(); |
| 615 | I2.eraseFromParent(); |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Krzysztof Parzyszek | 2a3b2f9 | 2016-01-15 14:08:31 +0000 | [diff] [blame] | 618 | void HexagonCopyToCombine::emitConst64(MachineBasicBlock::iterator &InsertPt, |
| 619 | unsigned DoubleDestReg, |
| 620 | MachineOperand &HiOperand, |
| 621 | MachineOperand &LoOperand) { |
| 622 | DEBUG(dbgs() << "Found a CONST64\n"); |
| 623 | |
| 624 | DebugLoc DL = InsertPt->getDebugLoc(); |
| 625 | MachineBasicBlock *BB = InsertPt->getParent(); |
| 626 | assert(LoOperand.isImm() && HiOperand.isImm() && |
| 627 | "Both operands must be immediate"); |
| 628 | |
| 629 | int64_t V = HiOperand.getImm(); |
| 630 | V = (V << 32) | (0x0ffffffffLL & LoOperand.getImm()); |
| 631 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::CONST64_Int_Real), |
| 632 | DoubleDestReg) |
| 633 | .addImm(V); |
| 634 | } |
| 635 | |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 636 | void HexagonCopyToCombine::emitCombineII(MachineBasicBlock::iterator &InsertPt, |
| 637 | unsigned DoubleDestReg, |
| 638 | MachineOperand &HiOperand, |
| 639 | MachineOperand &LoOperand) { |
| 640 | DebugLoc DL = InsertPt->getDebugLoc(); |
| 641 | MachineBasicBlock *BB = InsertPt->getParent(); |
| 642 | |
Krzysztof Parzyszek | cd97c98 | 2015-04-22 18:25:53 +0000 | [diff] [blame] | 643 | // Handle globals. |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 644 | if (HiOperand.isGlobal()) { |
Colin LeMahieu | b580d7d | 2014-12-09 19:23:45 +0000 | [diff] [blame] | 645 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 646 | .addGlobalAddress(HiOperand.getGlobal(), HiOperand.getOffset(), |
| 647 | HiOperand.getTargetFlags()) |
| 648 | .addImm(LoOperand.getImm()); |
| 649 | return; |
| 650 | } |
| 651 | if (LoOperand.isGlobal()) { |
Colin LeMahieu | 82fb8cb | 2014-12-30 17:53:54 +0000 | [diff] [blame] | 652 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 653 | .addImm(HiOperand.getImm()) |
| 654 | .addGlobalAddress(LoOperand.getGlobal(), LoOperand.getOffset(), |
| 655 | LoOperand.getTargetFlags()); |
| 656 | return; |
| 657 | } |
| 658 | |
Krzysztof Parzyszek | cd97c98 | 2015-04-22 18:25:53 +0000 | [diff] [blame] | 659 | // Handle block addresses. |
| 660 | if (HiOperand.isBlockAddress()) { |
Colin LeMahieu | b580d7d | 2014-12-09 19:23:45 +0000 | [diff] [blame] | 661 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg) |
Krzysztof Parzyszek | cd97c98 | 2015-04-22 18:25:53 +0000 | [diff] [blame] | 662 | .addBlockAddress(HiOperand.getBlockAddress(), HiOperand.getOffset(), |
| 663 | HiOperand.getTargetFlags()) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 664 | .addImm(LoOperand.getImm()); |
| 665 | return; |
| 666 | } |
Krzysztof Parzyszek | cd97c98 | 2015-04-22 18:25:53 +0000 | [diff] [blame] | 667 | if (LoOperand.isBlockAddress()) { |
| 668 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg) |
| 669 | .addImm(HiOperand.getImm()) |
| 670 | .addBlockAddress(LoOperand.getBlockAddress(), LoOperand.getOffset(), |
| 671 | LoOperand.getTargetFlags()); |
| 672 | return; |
| 673 | } |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 674 | |
Krzysztof Parzyszek | cd97c98 | 2015-04-22 18:25:53 +0000 | [diff] [blame] | 675 | // Handle jump tables. |
| 676 | if (HiOperand.isJTI()) { |
| 677 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg) |
| 678 | .addJumpTableIndex(HiOperand.getIndex(), HiOperand.getTargetFlags()) |
| 679 | .addImm(LoOperand.getImm()); |
| 680 | return; |
| 681 | } |
| 682 | if (LoOperand.isJTI()) { |
| 683 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg) |
| 684 | .addImm(HiOperand.getImm()) |
| 685 | .addJumpTableIndex(LoOperand.getIndex(), LoOperand.getTargetFlags()); |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | // Handle constant pools. |
| 690 | if (HiOperand.isCPI()) { |
| 691 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg) |
| 692 | .addConstantPoolIndex(HiOperand.getIndex(), HiOperand.getOffset(), |
| 693 | HiOperand.getTargetFlags()) |
| 694 | .addImm(LoOperand.getImm()); |
| 695 | return; |
| 696 | } |
| 697 | if (LoOperand.isCPI()) { |
| 698 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg) |
| 699 | .addImm(HiOperand.getImm()) |
| 700 | .addConstantPoolIndex(LoOperand.getIndex(), LoOperand.getOffset(), |
| 701 | LoOperand.getTargetFlags()); |
| 702 | return; |
| 703 | } |
| 704 | |
| 705 | // First preference should be given to Hexagon::A2_combineii instruction |
| 706 | // as it can include U6 (in Hexagon::A4_combineii) as well. |
| 707 | // In this instruction, HiOperand is const extended, if required. |
| 708 | if (isInt<8>(LoOperand.getImm())) { |
| 709 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg) |
| 710 | .addImm(HiOperand.getImm()) |
| 711 | .addImm(LoOperand.getImm()); |
| 712 | return; |
| 713 | } |
| 714 | |
| 715 | // In this instruction, LoOperand is const extended, if required. |
| 716 | if (isInt<8>(HiOperand.getImm())) { |
Colin LeMahieu | 82fb8cb | 2014-12-30 17:53:54 +0000 | [diff] [blame] | 717 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 718 | .addImm(HiOperand.getImm()) |
| 719 | .addImm(LoOperand.getImm()); |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | // Insert new combine instruction. |
| 724 | // DoubleRegDest = combine #HiImm, #LoImm |
Colin LeMahieu | b580d7d | 2014-12-09 19:23:45 +0000 | [diff] [blame] | 725 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 726 | .addImm(HiOperand.getImm()) |
| 727 | .addImm(LoOperand.getImm()); |
| 728 | } |
| 729 | |
| 730 | void HexagonCopyToCombine::emitCombineIR(MachineBasicBlock::iterator &InsertPt, |
| 731 | unsigned DoubleDestReg, |
| 732 | MachineOperand &HiOperand, |
| 733 | MachineOperand &LoOperand) { |
| 734 | unsigned LoReg = LoOperand.getReg(); |
| 735 | unsigned LoRegKillFlag = getKillRegState(LoOperand.isKill()); |
| 736 | |
| 737 | DebugLoc DL = InsertPt->getDebugLoc(); |
| 738 | MachineBasicBlock *BB = InsertPt->getParent(); |
| 739 | |
Krzysztof Parzyszek | cd97c98 | 2015-04-22 18:25:53 +0000 | [diff] [blame] | 740 | // Handle globals. |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 741 | if (HiOperand.isGlobal()) { |
Colin LeMahieu | 82fb8cb | 2014-12-30 17:53:54 +0000 | [diff] [blame] | 742 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 743 | .addGlobalAddress(HiOperand.getGlobal(), HiOperand.getOffset(), |
| 744 | HiOperand.getTargetFlags()) |
| 745 | .addReg(LoReg, LoRegKillFlag); |
| 746 | return; |
| 747 | } |
Krzysztof Parzyszek | cd97c98 | 2015-04-22 18:25:53 +0000 | [diff] [blame] | 748 | // Handle block addresses. |
| 749 | if (HiOperand.isBlockAddress()) { |
| 750 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg) |
| 751 | .addBlockAddress(HiOperand.getBlockAddress(), HiOperand.getOffset(), |
| 752 | HiOperand.getTargetFlags()) |
| 753 | .addReg(LoReg, LoRegKillFlag); |
| 754 | return; |
| 755 | } |
| 756 | // Handle jump tables. |
| 757 | if (HiOperand.isJTI()) { |
| 758 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg) |
| 759 | .addJumpTableIndex(HiOperand.getIndex(), HiOperand.getTargetFlags()) |
| 760 | .addReg(LoReg, LoRegKillFlag); |
| 761 | return; |
| 762 | } |
| 763 | // Handle constant pools. |
| 764 | if (HiOperand.isCPI()) { |
| 765 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg) |
| 766 | .addConstantPoolIndex(HiOperand.getIndex(), HiOperand.getOffset(), |
| 767 | HiOperand.getTargetFlags()) |
| 768 | .addReg(LoReg, LoRegKillFlag); |
| 769 | return; |
| 770 | } |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 771 | // Insert new combine instruction. |
| 772 | // DoubleRegDest = combine #HiImm, LoReg |
Colin LeMahieu | 82fb8cb | 2014-12-30 17:53:54 +0000 | [diff] [blame] | 773 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 774 | .addImm(HiOperand.getImm()) |
| 775 | .addReg(LoReg, LoRegKillFlag); |
| 776 | } |
| 777 | |
| 778 | void HexagonCopyToCombine::emitCombineRI(MachineBasicBlock::iterator &InsertPt, |
| 779 | unsigned DoubleDestReg, |
| 780 | MachineOperand &HiOperand, |
| 781 | MachineOperand &LoOperand) { |
| 782 | unsigned HiRegKillFlag = getKillRegState(HiOperand.isKill()); |
| 783 | unsigned HiReg = HiOperand.getReg(); |
| 784 | |
| 785 | DebugLoc DL = InsertPt->getDebugLoc(); |
| 786 | MachineBasicBlock *BB = InsertPt->getParent(); |
| 787 | |
| 788 | // Handle global. |
| 789 | if (LoOperand.isGlobal()) { |
Colin LeMahieu | 82fb8cb | 2014-12-30 17:53:54 +0000 | [diff] [blame] | 790 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 791 | .addReg(HiReg, HiRegKillFlag) |
| 792 | .addGlobalAddress(LoOperand.getGlobal(), LoOperand.getOffset(), |
| 793 | LoOperand.getTargetFlags()); |
| 794 | return; |
| 795 | } |
Krzysztof Parzyszek | cd97c98 | 2015-04-22 18:25:53 +0000 | [diff] [blame] | 796 | // Handle block addresses. |
| 797 | if (LoOperand.isBlockAddress()) { |
| 798 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg) |
| 799 | .addReg(HiReg, HiRegKillFlag) |
| 800 | .addBlockAddress(LoOperand.getBlockAddress(), LoOperand.getOffset(), |
| 801 | LoOperand.getTargetFlags()); |
| 802 | return; |
| 803 | } |
| 804 | // Handle jump tables. |
| 805 | if (LoOperand.isJTI()) { |
| 806 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg) |
| 807 | .addReg(HiOperand.getReg(), HiRegKillFlag) |
| 808 | .addJumpTableIndex(LoOperand.getIndex(), LoOperand.getTargetFlags()); |
| 809 | return; |
| 810 | } |
| 811 | // Handle constant pools. |
| 812 | if (LoOperand.isCPI()) { |
| 813 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg) |
| 814 | .addReg(HiOperand.getReg(), HiRegKillFlag) |
| 815 | .addConstantPoolIndex(LoOperand.getIndex(), LoOperand.getOffset(), |
| 816 | LoOperand.getTargetFlags()); |
| 817 | return; |
| 818 | } |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 819 | |
| 820 | // Insert new combine instruction. |
| 821 | // DoubleRegDest = combine HiReg, #LoImm |
Colin LeMahieu | 82fb8cb | 2014-12-30 17:53:54 +0000 | [diff] [blame] | 822 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 823 | .addReg(HiReg, HiRegKillFlag) |
| 824 | .addImm(LoOperand.getImm()); |
| 825 | } |
| 826 | |
| 827 | void HexagonCopyToCombine::emitCombineRR(MachineBasicBlock::iterator &InsertPt, |
| 828 | unsigned DoubleDestReg, |
| 829 | MachineOperand &HiOperand, |
| 830 | MachineOperand &LoOperand) { |
| 831 | unsigned LoRegKillFlag = getKillRegState(LoOperand.isKill()); |
| 832 | unsigned HiRegKillFlag = getKillRegState(HiOperand.isKill()); |
| 833 | unsigned LoReg = LoOperand.getReg(); |
| 834 | unsigned HiReg = HiOperand.getReg(); |
| 835 | |
| 836 | DebugLoc DL = InsertPt->getDebugLoc(); |
| 837 | MachineBasicBlock *BB = InsertPt->getParent(); |
| 838 | |
| 839 | // Insert new combine instruction. |
| 840 | // DoubleRegDest = combine HiReg, LoReg |
Colin LeMahieu | b580d7d | 2014-12-09 19:23:45 +0000 | [diff] [blame] | 841 | BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combinew), DoubleDestReg) |
Jyotsna Verma | 803e506 | 2013-05-14 18:54:06 +0000 | [diff] [blame] | 842 | .addReg(HiReg, HiRegKillFlag) |
| 843 | .addReg(LoReg, LoRegKillFlag); |
| 844 | } |
| 845 | |
| 846 | FunctionPass *llvm::createHexagonCopyToCombine() { |
| 847 | return new HexagonCopyToCombine(); |
| 848 | } |