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