blob: a09ccab483cf22d328b3df235ebbad2fc4d545ad [file] [log] [blame]
Jyotsna Verma803e5062013-05-14 18:54:06 +00001//===------- HexagonCopyToCombine.cpp - Hexagon Copy-To-Combine Pass ------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jyotsna Verma803e5062013-05-14 18:54:06 +00006//
7//===----------------------------------------------------------------------===//
8// This pass replaces transfer instructions by combine instructions.
9// We walk along a basic block and look for two combinable instructions and try
10// to move them together. If we can move them next to each other we do so and
11// replace them with a combine instruction.
12//===----------------------------------------------------------------------===//
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000013#include "HexagonInstrInfo.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000014#include "HexagonSubtarget.h"
Jyotsna Verma803e5062013-05-14 18:54:06 +000015#include "llvm/ADT/DenseMap.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/ADT/DenseSet.h"
Jyotsna Verma803e5062013-05-14 18:54:06 +000017#include "llvm/CodeGen/MachineBasicBlock.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/MachineInstr.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000022#include "llvm/CodeGen/Passes.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000023#include "llvm/CodeGen/TargetRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000024#include "llvm/PassSupport.h"
Jyotsna Verma803e5062013-05-14 18:54:06 +000025#include "llvm/Support/CodeGen.h"
26#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/raw_ostream.h"
Jyotsna Verma803e5062013-05-14 18:54:06 +000029
30using namespace llvm;
31
Chandler Carruth84e68b22014-04-22 02:41:26 +000032#define DEBUG_TYPE "hexagon-copy-combine"
33
Jyotsna Verma803e5062013-05-14 18:54:06 +000034static
35cl::opt<bool> IsCombinesDisabled("disable-merge-into-combines",
36 cl::Hidden, cl::ZeroOrMore,
37 cl::init(false),
38 cl::desc("Disable merging into combines"));
39static
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +000040cl::opt<bool> IsConst64Disabled("disable-const64",
41 cl::Hidden, cl::ZeroOrMore,
42 cl::init(false),
43 cl::desc("Disable generation of const64"));
44static
Jyotsna Verma803e5062013-05-14 18:54:06 +000045cl::opt<unsigned>
46MaxNumOfInstsBetweenNewValueStoreAndTFR("max-num-inst-between-tfr-and-nv-store",
47 cl::Hidden, cl::init(4),
48 cl::desc("Maximum distance between a tfr feeding a store we "
49 "consider the store still to be newifiable"));
50
51namespace llvm {
Colin LeMahieu56efafc2015-06-15 19:05:35 +000052 FunctionPass *createHexagonCopyToCombine();
Jyotsna Verma803e5062013-05-14 18:54:06 +000053 void initializeHexagonCopyToCombinePass(PassRegistry&);
54}
55
56
57namespace {
58
59class HexagonCopyToCombine : public MachineFunctionPass {
60 const HexagonInstrInfo *TII;
61 const TargetRegisterInfo *TRI;
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +000062 const HexagonSubtarget *ST;
Jyotsna Verma803e5062013-05-14 18:54:06 +000063 bool ShouldCombineAggressively;
64
65 DenseSet<MachineInstr *> PotentiallyNewifiableTFR;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +000066 SmallVector<MachineInstr *, 8> DbgMItoMove;
67
Jyotsna Verma803e5062013-05-14 18:54:06 +000068public:
69 static char ID;
70
71 HexagonCopyToCombine() : MachineFunctionPass(ID) {
72 initializeHexagonCopyToCombinePass(*PassRegistry::getPassRegistry());
73 }
74
Craig Topper906c2cd2014-04-29 07:58:16 +000075 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jyotsna Verma803e5062013-05-14 18:54:06 +000076 MachineFunctionPass::getAnalysisUsage(AU);
77 }
78
Mehdi Amini117296c2016-10-01 02:56:57 +000079 StringRef getPassName() const override {
Jyotsna Verma803e5062013-05-14 18:54:06 +000080 return "Hexagon Copy-To-Combine Pass";
81 }
82
Craig Topper906c2cd2014-04-29 07:58:16 +000083 bool runOnMachineFunction(MachineFunction &Fn) override;
Jyotsna Verma803e5062013-05-14 18:54:06 +000084
Derek Schuff1dbf7a52016-04-04 17:09:25 +000085 MachineFunctionProperties getRequiredProperties() const override {
86 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000087 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000088 }
89
Jyotsna Verma803e5062013-05-14 18:54:06 +000090private:
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000091 MachineInstr *findPairable(MachineInstr &I1, bool &DoInsertAtI1,
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +000092 bool AllowC64);
Jyotsna Verma803e5062013-05-14 18:54:06 +000093
94 void findPotentialNewifiableTFRs(MachineBasicBlock &);
95
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000096 void combine(MachineInstr &I1, MachineInstr &I2,
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +000097 MachineBasicBlock::iterator &MI, bool DoInsertAtI1,
98 bool OptForSize);
Jyotsna Verma803e5062013-05-14 18:54:06 +000099
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000100 bool isSafeToMoveTogether(MachineInstr &I1, MachineInstr &I2,
Jyotsna Verma803e5062013-05-14 18:54:06 +0000101 unsigned I1DestReg, unsigned I2DestReg,
102 bool &DoInsertAtI1);
103
104 void emitCombineRR(MachineBasicBlock::iterator &Before, unsigned DestReg,
105 MachineOperand &HiOperand, MachineOperand &LoOperand);
106
107 void emitCombineRI(MachineBasicBlock::iterator &Before, unsigned DestReg,
108 MachineOperand &HiOperand, MachineOperand &LoOperand);
109
110 void emitCombineIR(MachineBasicBlock::iterator &Before, unsigned DestReg,
111 MachineOperand &HiOperand, MachineOperand &LoOperand);
112
113 void emitCombineII(MachineBasicBlock::iterator &Before, unsigned DestReg,
114 MachineOperand &HiOperand, MachineOperand &LoOperand);
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000115
116 void emitConst64(MachineBasicBlock::iterator &Before, unsigned DestReg,
117 MachineOperand &HiOperand, MachineOperand &LoOperand);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000118};
119
120} // End anonymous namespace.
121
122char HexagonCopyToCombine::ID = 0;
123
124INITIALIZE_PASS(HexagonCopyToCombine, "hexagon-copy-combine",
125 "Hexagon Copy-To-Combine Pass", false, false)
126
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000127static bool isCombinableInstType(MachineInstr &MI, const HexagonInstrInfo *TII,
Jyotsna Verma803e5062013-05-14 18:54:06 +0000128 bool ShouldCombineAggressively) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000129 switch (MI.getOpcode()) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000130 case Hexagon::A2_tfr: {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000131 // A COPY instruction can be combined if its arguments are IntRegs (32bit).
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000132 const MachineOperand &Op0 = MI.getOperand(0);
133 const MachineOperand &Op1 = MI.getOperand(1);
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000134 assert(Op0.isReg() && Op1.isReg());
Jyotsna Verma803e5062013-05-14 18:54:06 +0000135
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000136 unsigned DestReg = Op0.getReg();
137 unsigned SrcReg = Op1.getReg();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000138 return Hexagon::IntRegsRegClass.contains(DestReg) &&
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000139 Hexagon::IntRegsRegClass.contains(SrcReg);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000140 }
141
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000142 case Hexagon::A2_tfrsi: {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000143 // A transfer-immediate can be combined if its argument is a signed 8bit
144 // value.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000145 const MachineOperand &Op0 = MI.getOperand(0);
146 const MachineOperand &Op1 = MI.getOperand(1);
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000147 assert(Op0.isReg());
Jyotsna Verma803e5062013-05-14 18:54:06 +0000148
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000149 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 Verma803e5062013-05-14 18:54:06 +0000158 return Hexagon::IntRegsRegClass.contains(DestReg) &&
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000159 (ShouldCombineAggressively || NotExt);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000160 }
161
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000162 case Hexagon::V6_vassign:
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000163 return true;
164
Jyotsna Verma803e5062013-05-14 18:54:06 +0000165 default:
166 break;
167 }
168
169 return false;
170}
171
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000172template <unsigned N> static bool isGreaterThanNBitTFRI(const MachineInstr &I) {
173 if (I.getOpcode() == Hexagon::TFRI64_V4 ||
174 I.getOpcode() == Hexagon::A2_tfrsi) {
175 const MachineOperand &Op = I.getOperand(1);
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000176 return !Op.isImm() || !isInt<N>(Op.getImm());
177 }
178 return false;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000179}
180
181/// areCombinableOperations - Returns true if the two instruction can be merge
182/// into a combine (ignoring register constraints).
183static bool areCombinableOperations(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000184 MachineInstr &HighRegInst,
185 MachineInstr &LowRegInst, bool AllowC64) {
186 unsigned HiOpc = HighRegInst.getOpcode();
187 unsigned LoOpc = LowRegInst.getOpcode();
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000188
189 auto verifyOpc = [](unsigned Opc) -> void {
190 switch (Opc) {
191 case Hexagon::A2_tfr:
192 case Hexagon::A2_tfrsi:
193 case Hexagon::V6_vassign:
194 break;
195 default:
196 llvm_unreachable("Unexpected opcode");
197 }
198 };
199 verifyOpc(HiOpc);
200 verifyOpc(LoOpc);
201
202 if (HiOpc == Hexagon::V6_vassign || LoOpc == Hexagon::V6_vassign)
203 return HiOpc == LoOpc;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000204
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000205 if (!AllowC64) {
206 // There is no combine of two constant extended values.
207 if (isGreaterThanNBitTFRI<8>(HighRegInst) &&
208 isGreaterThanNBitTFRI<6>(LowRegInst))
209 return false;
210 }
211
212 // There is a combine of two constant extended values into CONST64,
213 // provided both constants are true immediates.
214 if (isGreaterThanNBitTFRI<16>(HighRegInst) &&
215 isGreaterThanNBitTFRI<16>(LowRegInst))
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000216 return (HighRegInst.getOperand(1).isImm() &&
217 LowRegInst.getOperand(1).isImm());
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000218
219 // There is no combine of two constant extended values, unless handled above
220 // Make both 8-bit size checks to allow both combine (#,##) and combine(##,#)
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000221 if (isGreaterThanNBitTFRI<8>(HighRegInst) &&
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000222 isGreaterThanNBitTFRI<8>(LowRegInst))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000223 return false;
224
225 return true;
226}
227
228static bool isEvenReg(unsigned Reg) {
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000229 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
230 if (Hexagon::IntRegsRegClass.contains(Reg))
231 return (Reg - Hexagon::R0) % 2 == 0;
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000232 if (Hexagon::HvxVRRegClass.contains(Reg))
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000233 return (Reg - Hexagon::V0) % 2 == 0;
234 llvm_unreachable("Invalid register");
Jyotsna Verma803e5062013-05-14 18:54:06 +0000235}
236
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000237static void removeKillInfo(MachineInstr &MI, unsigned RegNotKilled) {
238 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
239 MachineOperand &Op = MI.getOperand(I);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000240 if (!Op.isReg() || Op.getReg() != RegNotKilled || !Op.isKill())
241 continue;
242 Op.setIsKill(false);
243 }
244}
245
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000246/// Returns true if it is unsafe to move a copy instruction from \p UseReg to
247/// \p DestReg over the instruction \p MI.
248static bool isUnsafeToMoveAcross(MachineInstr &MI, unsigned UseReg,
249 unsigned DestReg,
250 const TargetRegisterInfo *TRI) {
251 return (UseReg && (MI.modifiesRegister(UseReg, TRI))) ||
252 MI.modifiesRegister(DestReg, TRI) || MI.readsRegister(DestReg, TRI) ||
Krzysztof Parzyszek709e4f92017-08-10 15:00:30 +0000253 MI.hasUnmodeledSideEffects() || MI.isInlineAsm() ||
254 MI.isMetaInstruction();
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000255}
256
Matt Arsenaulte3a676e2019-06-24 15:50:29 +0000257static Register UseReg(const MachineOperand& MO) {
258 return MO.isReg() ? MO.getReg() : Register();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000259}
260
261/// isSafeToMoveTogether - Returns true if it is safe to move I1 next to I2 such
262/// that the two instructions can be paired in a combine.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000263bool HexagonCopyToCombine::isSafeToMoveTogether(MachineInstr &I1,
264 MachineInstr &I2,
Jyotsna Verma803e5062013-05-14 18:54:06 +0000265 unsigned I1DestReg,
266 unsigned I2DestReg,
267 bool &DoInsertAtI1) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000268 unsigned I2UseReg = UseReg(I2.getOperand(1));
Jyotsna Verma803e5062013-05-14 18:54:06 +0000269
270 // It is not safe to move I1 and I2 into one combine if I2 has a true
271 // dependence on I1.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000272 if (I2UseReg && I1.modifiesRegister(I2UseReg, TRI))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000273 return false;
274
275 bool isSafe = true;
276
277 // First try to move I2 towards I1.
278 {
279 // A reverse_iterator instantiated like below starts before I2, and I1
280 // respectively.
281 // Look at instructions I in between I2 and (excluding) I1.
282 MachineBasicBlock::reverse_iterator I(I2),
283 End = --(MachineBasicBlock::reverse_iterator(I1));
284 // At 03 we got better results (dhrystone!) by being more conservative.
285 if (!ShouldCombineAggressively)
286 End = MachineBasicBlock::reverse_iterator(I1);
287 // If I2 kills its operand and we move I2 over an instruction that also
288 // uses I2's use reg we need to modify that (first) instruction to now kill
289 // this reg.
290 unsigned KilledOperand = 0;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000291 if (I2.killsRegister(I2UseReg))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000292 KilledOperand = I2UseReg;
Craig Topper062a2ba2014-04-25 05:30:21 +0000293 MachineInstr *KillingInstr = nullptr;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000294
295 for (; I != End; ++I) {
296 // If the intervening instruction I:
297 // * modifies I2's use reg
298 // * modifies I2's def reg
299 // * reads I2's def reg
300 // * or has unmodelled side effects
301 // we can't move I2 across it.
Shiva Chen801bf7e2018-05-09 02:42:00 +0000302 if (I->isDebugInstr())
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000303 continue;
304
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000305 if (isUnsafeToMoveAcross(*I, I2UseReg, I2DestReg, TRI)) {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000306 isSafe = false;
307 break;
308 }
309
310 // Update first use of the killed operand.
311 if (!KillingInstr && KilledOperand &&
312 I->readsRegister(KilledOperand, TRI))
313 KillingInstr = &*I;
314 }
315 if (isSafe) {
316 // Update the intermediate instruction to with the kill flag.
317 if (KillingInstr) {
318 bool Added = KillingInstr->addRegisterKilled(KilledOperand, TRI, true);
Alp Tokercb402912014-01-24 17:20:08 +0000319 (void)Added; // suppress compiler warning
Jyotsna Verma803e5062013-05-14 18:54:06 +0000320 assert(Added && "Must successfully update kill flag");
321 removeKillInfo(I2, KilledOperand);
322 }
323 DoInsertAtI1 = true;
324 return true;
325 }
326 }
327
328 // Try to move I1 towards I2.
329 {
330 // Look at instructions I in between I1 and (excluding) I2.
331 MachineBasicBlock::iterator I(I1), End(I2);
332 // At O3 we got better results (dhrystone) by being more conservative here.
333 if (!ShouldCombineAggressively)
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000334 End = std::next(MachineBasicBlock::iterator(I2));
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000335 unsigned I1UseReg = UseReg(I1.getOperand(1));
Jyotsna Vermacceafb22013-05-28 19:01:45 +0000336 // Track killed operands. If we move across an instruction that kills our
Jyotsna Verma803e5062013-05-14 18:54:06 +0000337 // operand, we need to update the kill information on the moved I1. It kills
338 // the operand now.
Craig Topper062a2ba2014-04-25 05:30:21 +0000339 MachineInstr *KillingInstr = nullptr;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000340 unsigned KilledOperand = 0;
341
342 while(++I != End) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000343 MachineInstr &MI = *I;
344 // If the intervening instruction MI:
Jyotsna Verma803e5062013-05-14 18:54:06 +0000345 // * modifies I1's use reg
346 // * modifies I1's def reg
347 // * reads I1's def reg
348 // * or has unmodelled side effects
349 // We introduce this special case because llvm has no api to remove a
350 // kill flag for a register (a removeRegisterKilled() analogous to
351 // addRegisterKilled) that handles aliased register correctly.
352 // * or has a killed aliased register use of I1's use reg
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000353 // %d4 = A2_tfrpi 16
354 // %r6 = A2_tfr %r9
355 // %r8 = KILL %r8, implicit killed %d4
Jyotsna Verma803e5062013-05-14 18:54:06 +0000356 // If we want to move R6 = across the KILL instruction we would have
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000357 // to remove the implicit killed %d4 operand. For now, we are
Jyotsna Verma803e5062013-05-14 18:54:06 +0000358 // conservative and disallow the move.
359 // we can't move I1 across it.
Shiva Chen801bf7e2018-05-09 02:42:00 +0000360 if (MI.isDebugInstr()) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000361 if (MI.readsRegister(I1DestReg, TRI)) // Move this instruction after I2.
362 DbgMItoMove.push_back(&MI);
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000363 continue;
364 }
365
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000366 if (isUnsafeToMoveAcross(MI, I1UseReg, I1DestReg, TRI) ||
Jyotsna Verma803e5062013-05-14 18:54:06 +0000367 // Check for an aliased register kill. Bail out if we see one.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000368 (!MI.killsRegister(I1UseReg) && MI.killsRegister(I1UseReg, TRI)))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000369 return false;
370
371 // Check for an exact kill (registers match).
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000372 if (I1UseReg && MI.killsRegister(I1UseReg)) {
Craig Toppere73658d2014-04-28 04:05:08 +0000373 assert(!KillingInstr && "Should only see one killing instruction");
Jyotsna Verma803e5062013-05-14 18:54:06 +0000374 KilledOperand = I1UseReg;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000375 KillingInstr = &MI;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000376 }
377 }
378 if (KillingInstr) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000379 removeKillInfo(*KillingInstr, KilledOperand);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000380 // Update I1 to set the kill flag. This flag will later be picked up by
381 // the new COMBINE instruction.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000382 bool Added = I1.addRegisterKilled(KilledOperand, TRI);
Alp Tokercb402912014-01-24 17:20:08 +0000383 (void)Added; // suppress compiler warning
Jyotsna Verma803e5062013-05-14 18:54:06 +0000384 assert(Added && "Must successfully update kill flag");
385 }
386 DoInsertAtI1 = false;
387 }
388
389 return true;
390}
391
392/// findPotentialNewifiableTFRs - Finds tranfers that feed stores that could be
393/// newified. (A use of a 64 bit register define can not be newified)
394void
395HexagonCopyToCombine::findPotentialNewifiableTFRs(MachineBasicBlock &BB) {
396 DenseMap<unsigned, MachineInstr *> LastDef;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000397 for (MachineInstr &MI : BB) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000398 if (MI.isDebugInstr())
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000399 continue;
400
Jyotsna Verma803e5062013-05-14 18:54:06 +0000401 // Mark TFRs that feed a potential new value store as such.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000402 if (TII->mayBeNewStore(MI)) {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000403 // Look for uses of TFR instructions.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000404 for (unsigned OpdIdx = 0, OpdE = MI.getNumOperands(); OpdIdx != OpdE;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000405 ++OpdIdx) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000406 MachineOperand &Op = MI.getOperand(OpdIdx);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000407
408 // Skip over anything except register uses.
409 if (!Op.isReg() || !Op.isUse() || !Op.getReg())
410 continue;
411
412 // Look for the defining instruction.
413 unsigned Reg = Op.getReg();
414 MachineInstr *DefInst = LastDef[Reg];
415 if (!DefInst)
416 continue;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000417 if (!isCombinableInstType(*DefInst, TII, ShouldCombineAggressively))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000418 continue;
419
420 // Only close newifiable stores should influence the decision.
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000421 // Ignore the debug instructions in between.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000422 MachineBasicBlock::iterator It(DefInst);
423 unsigned NumInstsToDef = 0;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000424 while (&*It != &MI) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000425 if (!It->isDebugInstr())
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000426 ++NumInstsToDef;
Krzysztof Parzyszek14f9535e2016-01-21 12:45:17 +0000427 ++It;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000428 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000429
430 if (NumInstsToDef > MaxNumOfInstsBetweenNewValueStoreAndTFR)
431 continue;
432
433 PotentiallyNewifiableTFR.insert(DefInst);
434 }
435 // Skip to next instruction.
436 continue;
437 }
438
439 // Put instructions that last defined integer or double registers into the
440 // map.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000441 for (MachineOperand &Op : MI.operands()) {
442 if (Op.isReg()) {
443 if (!Op.isDef() || !Op.getReg())
444 continue;
445 unsigned Reg = Op.getReg();
446 if (Hexagon::DoubleRegsRegClass.contains(Reg)) {
447 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
448 LastDef[*SubRegs] = &MI;
449 } else if (Hexagon::IntRegsRegClass.contains(Reg))
450 LastDef[Reg] = &MI;
451 } else if (Op.isRegMask()) {
452 for (unsigned Reg : Hexagon::IntRegsRegClass)
453 if (Op.clobbersPhysReg(Reg))
454 LastDef[Reg] = &MI;
455 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000456 }
457 }
458}
459
460bool HexagonCopyToCombine::runOnMachineFunction(MachineFunction &MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000461 if (skipFunction(MF.getFunction()))
Krzysztof Parzyszek643aaea2017-04-14 15:26:34 +0000462 return false;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000463
464 if (IsCombinesDisabled) return false;
465
466 bool HasChanged = false;
467
468 // Get target info.
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000469 ST = &MF.getSubtarget<HexagonSubtarget>();
470 TRI = ST->getRegisterInfo();
471 TII = ST->getInstrInfo();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000472
Matthias Braunf1caa282017-12-15 22:22:58 +0000473 const Function &F = MF.getFunction();
474 bool OptForSize = F.hasFnAttribute(Attribute::OptimizeForSize);
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000475
Jyotsna Verma803e5062013-05-14 18:54:06 +0000476 // Combine aggressively (for code size)
477 ShouldCombineAggressively =
478 MF.getTarget().getOptLevel() <= CodeGenOpt::Default;
479
480 // Traverse basic blocks.
481 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); BI != BE;
482 ++BI) {
483 PotentiallyNewifiableTFR.clear();
484 findPotentialNewifiableTFRs(*BI);
485
486 // Traverse instructions in basic block.
487 for(MachineBasicBlock::iterator MI = BI->begin(), End = BI->end();
488 MI != End;) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000489 MachineInstr &I1 = *MI++;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000490
Shiva Chen801bf7e2018-05-09 02:42:00 +0000491 if (I1.isDebugInstr())
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000492 continue;
493
Jyotsna Verma803e5062013-05-14 18:54:06 +0000494 // Don't combine a TFR whose user could be newified (instructions that
495 // define double registers can not be newified - Programmer's Ref Manual
496 // 5.4.2 New-value stores).
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000497 if (ShouldCombineAggressively && PotentiallyNewifiableTFR.count(&I1))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000498 continue;
499
500 // Ignore instructions that are not combinable.
501 if (!isCombinableInstType(I1, TII, ShouldCombineAggressively))
502 continue;
503
504 // Find a second instruction that can be merged into a combine
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000505 // instruction. In addition, also find all the debug instructions that
506 // need to be moved along with it.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000507 bool DoInsertAtI1 = false;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000508 DbgMItoMove.clear();
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000509 MachineInstr *I2 = findPairable(I1, DoInsertAtI1, OptForSize);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000510 if (I2) {
511 HasChanged = true;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000512 combine(I1, *I2, MI, DoInsertAtI1, OptForSize);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000513 }
514 }
515 }
516
517 return HasChanged;
518}
519
520/// findPairable - Returns an instruction that can be merged with \p I1 into a
521/// COMBINE instruction or 0 if no such instruction can be found. Returns true
522/// in \p DoInsertAtI1 if the combine must be inserted at instruction \p I1
523/// false if the combine must be inserted at the returned instruction.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000524MachineInstr *HexagonCopyToCombine::findPairable(MachineInstr &I1,
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000525 bool &DoInsertAtI1,
526 bool AllowC64) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000527 MachineBasicBlock::iterator I2 = std::next(MachineBasicBlock::iterator(I1));
Shiva Chen801bf7e2018-05-09 02:42:00 +0000528 while (I2 != I1.getParent()->end() && I2->isDebugInstr())
Krzysztof Parzyszek6dff3362016-08-24 22:36:35 +0000529 ++I2;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000530
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000531 unsigned I1DestReg = I1.getOperand(0).getReg();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000532
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000533 for (MachineBasicBlock::iterator End = I1.getParent()->end(); I2 != End;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000534 ++I2) {
535 // Bail out early if we see a second definition of I1DestReg.
536 if (I2->modifiesRegister(I1DestReg, TRI))
537 break;
538
539 // Ignore non-combinable instructions.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000540 if (!isCombinableInstType(*I2, TII, ShouldCombineAggressively))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000541 continue;
542
543 // Don't combine a TFR whose user could be newified.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000544 if (ShouldCombineAggressively && PotentiallyNewifiableTFR.count(&*I2))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000545 continue;
546
547 unsigned I2DestReg = I2->getOperand(0).getReg();
548
549 // Check that registers are adjacent and that the first destination register
550 // is even.
551 bool IsI1LowReg = (I2DestReg - I1DestReg) == 1;
552 bool IsI2LowReg = (I1DestReg - I2DestReg) == 1;
553 unsigned FirstRegIndex = IsI1LowReg ? I1DestReg : I2DestReg;
554 if ((!IsI1LowReg && !IsI2LowReg) || !isEvenReg(FirstRegIndex))
555 continue;
556
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +0000557 // Check that the two instructions are combinable.
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000558 // The order matters because in a A2_tfrsi we might can encode a int8 as
559 // the hi reg operand but only a uint6 as the low reg operand.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000560 if ((IsI2LowReg && !areCombinableOperations(TRI, I1, *I2, AllowC64)) ||
561 (IsI1LowReg && !areCombinableOperations(TRI, *I2, I1, AllowC64)))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000562 break;
563
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000564 if (isSafeToMoveTogether(I1, *I2, I1DestReg, I2DestReg, DoInsertAtI1))
565 return &*I2;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000566
567 // Not safe. Stop searching.
568 break;
569 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000570 return nullptr;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000571}
572
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000573void HexagonCopyToCombine::combine(MachineInstr &I1, MachineInstr &I2,
Jyotsna Verma803e5062013-05-14 18:54:06 +0000574 MachineBasicBlock::iterator &MI,
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000575 bool DoInsertAtI1, bool OptForSize) {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000576 // We are going to delete I2. If MI points to I2 advance it to the next
577 // instruction.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000578 if (MI == I2.getIterator())
579 ++MI;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000580
581 // Figure out whether I1 or I2 goes into the lowreg part.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000582 unsigned I1DestReg = I1.getOperand(0).getReg();
583 unsigned I2DestReg = I2.getOperand(0).getReg();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000584 bool IsI1Loreg = (I2DestReg - I1DestReg) == 1;
585 unsigned LoRegDef = IsI1Loreg ? I1DestReg : I2DestReg;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000586 unsigned SubLo;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000587
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000588 const TargetRegisterClass *SuperRC = nullptr;
589 if (Hexagon::IntRegsRegClass.contains(LoRegDef)) {
590 SuperRC = &Hexagon::DoubleRegsRegClass;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000591 SubLo = Hexagon::isub_lo;
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000592 } else if (Hexagon::HvxVRRegClass.contains(LoRegDef)) {
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000593 assert(ST->useHVXOps());
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000594 SuperRC = &Hexagon::HvxWRRegClass;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000595 SubLo = Hexagon::vsub_lo;
Krzysztof Parzyszekf817efb2016-11-09 17:50:46 +0000596 } else
597 llvm_unreachable("Unexpected register class");
598
Jyotsna Verma803e5062013-05-14 18:54:06 +0000599 // Get the double word register.
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000600 unsigned DoubleRegDest = TRI->getMatchingSuperReg(LoRegDef, SubLo, SuperRC);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000601 assert(DoubleRegDest != 0 && "Expect a valid register");
602
Jyotsna Verma803e5062013-05-14 18:54:06 +0000603 // Setup source operands.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000604 MachineOperand &LoOperand = IsI1Loreg ? I1.getOperand(1) : I2.getOperand(1);
605 MachineOperand &HiOperand = IsI1Loreg ? I2.getOperand(1) : I1.getOperand(1);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000606
607 // Figure out which source is a register and which a constant.
608 bool IsHiReg = HiOperand.isReg();
609 bool IsLoReg = LoOperand.isReg();
610
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000611 // There is a combine of two constant extended values into CONST64.
612 bool IsC64 = OptForSize && LoOperand.isImm() && HiOperand.isImm() &&
613 isGreaterThanNBitTFRI<16>(I1) && isGreaterThanNBitTFRI<16>(I2);
614
Jyotsna Verma803e5062013-05-14 18:54:06 +0000615 MachineBasicBlock::iterator InsertPt(DoInsertAtI1 ? I1 : I2);
616 // Emit combine.
617 if (IsHiReg && IsLoReg)
618 emitCombineRR(InsertPt, DoubleRegDest, HiOperand, LoOperand);
619 else if (IsHiReg)
620 emitCombineRI(InsertPt, DoubleRegDest, HiOperand, LoOperand);
621 else if (IsLoReg)
622 emitCombineIR(InsertPt, DoubleRegDest, HiOperand, LoOperand);
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000623 else if (IsC64 && !IsConst64Disabled)
624 emitConst64(InsertPt, DoubleRegDest, HiOperand, LoOperand);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000625 else
626 emitCombineII(InsertPt, DoubleRegDest, HiOperand, LoOperand);
627
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000628 // Move debug instructions along with I1 if it's being
629 // moved towards I2.
630 if (!DoInsertAtI1 && DbgMItoMove.size() != 0) {
631 // Insert debug instructions at the new location before I2.
632 MachineBasicBlock *BB = InsertPt->getParent();
633 for (auto NewMI : DbgMItoMove) {
634 // If iterator MI is pointing to DEBUG_VAL, make sure
635 // MI now points to next relevant instruction.
Duncan P. N. Exon Smithf197b1f2016-08-12 05:05:36 +0000636 if (NewMI == MI)
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000637 ++MI;
638 BB->splice(InsertPt, BB, NewMI);
639 }
640 }
641
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000642 I1.eraseFromParent();
643 I2.eraseFromParent();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000644}
645
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000646void HexagonCopyToCombine::emitConst64(MachineBasicBlock::iterator &InsertPt,
647 unsigned DoubleDestReg,
648 MachineOperand &HiOperand,
649 MachineOperand &LoOperand) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000650 LLVM_DEBUG(dbgs() << "Found a CONST64\n");
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000651
652 DebugLoc DL = InsertPt->getDebugLoc();
653 MachineBasicBlock *BB = InsertPt->getParent();
654 assert(LoOperand.isImm() && HiOperand.isImm() &&
655 "Both operands must be immediate");
656
657 int64_t V = HiOperand.getImm();
658 V = (V << 32) | (0x0ffffffffLL & LoOperand.getImm());
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +0000659 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::CONST64), DoubleDestReg)
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000660 .addImm(V);
661}
662
Jyotsna Verma803e5062013-05-14 18:54:06 +0000663void HexagonCopyToCombine::emitCombineII(MachineBasicBlock::iterator &InsertPt,
664 unsigned DoubleDestReg,
665 MachineOperand &HiOperand,
666 MachineOperand &LoOperand) {
667 DebugLoc DL = InsertPt->getDebugLoc();
668 MachineBasicBlock *BB = InsertPt->getParent();
669
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000670 // Handle globals.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000671 if (HiOperand.isGlobal()) {
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000672 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000673 .addGlobalAddress(HiOperand.getGlobal(), HiOperand.getOffset(),
674 HiOperand.getTargetFlags())
675 .addImm(LoOperand.getImm());
676 return;
677 }
678 if (LoOperand.isGlobal()) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000679 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000680 .addImm(HiOperand.getImm())
681 .addGlobalAddress(LoOperand.getGlobal(), LoOperand.getOffset(),
682 LoOperand.getTargetFlags());
683 return;
684 }
685
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000686 // Handle block addresses.
687 if (HiOperand.isBlockAddress()) {
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000688 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000689 .addBlockAddress(HiOperand.getBlockAddress(), HiOperand.getOffset(),
690 HiOperand.getTargetFlags())
Jyotsna Verma803e5062013-05-14 18:54:06 +0000691 .addImm(LoOperand.getImm());
692 return;
693 }
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000694 if (LoOperand.isBlockAddress()) {
695 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
696 .addImm(HiOperand.getImm())
697 .addBlockAddress(LoOperand.getBlockAddress(), LoOperand.getOffset(),
698 LoOperand.getTargetFlags());
699 return;
700 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000701
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000702 // Handle jump tables.
703 if (HiOperand.isJTI()) {
704 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
705 .addJumpTableIndex(HiOperand.getIndex(), HiOperand.getTargetFlags())
706 .addImm(LoOperand.getImm());
707 return;
708 }
709 if (LoOperand.isJTI()) {
710 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
711 .addImm(HiOperand.getImm())
712 .addJumpTableIndex(LoOperand.getIndex(), LoOperand.getTargetFlags());
713 return;
714 }
715
716 // Handle constant pools.
717 if (HiOperand.isCPI()) {
718 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
719 .addConstantPoolIndex(HiOperand.getIndex(), HiOperand.getOffset(),
720 HiOperand.getTargetFlags())
721 .addImm(LoOperand.getImm());
722 return;
723 }
724 if (LoOperand.isCPI()) {
725 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
726 .addImm(HiOperand.getImm())
727 .addConstantPoolIndex(LoOperand.getIndex(), LoOperand.getOffset(),
728 LoOperand.getTargetFlags());
729 return;
730 }
731
732 // First preference should be given to Hexagon::A2_combineii instruction
733 // as it can include U6 (in Hexagon::A4_combineii) as well.
734 // In this instruction, HiOperand is const extended, if required.
735 if (isInt<8>(LoOperand.getImm())) {
736 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
737 .addImm(HiOperand.getImm())
738 .addImm(LoOperand.getImm());
739 return;
740 }
741
742 // In this instruction, LoOperand is const extended, if required.
743 if (isInt<8>(HiOperand.getImm())) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000744 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000745 .addImm(HiOperand.getImm())
746 .addImm(LoOperand.getImm());
747 return;
748 }
749
750 // Insert new combine instruction.
751 // DoubleRegDest = combine #HiImm, #LoImm
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000752 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000753 .addImm(HiOperand.getImm())
754 .addImm(LoOperand.getImm());
755}
756
757void HexagonCopyToCombine::emitCombineIR(MachineBasicBlock::iterator &InsertPt,
758 unsigned DoubleDestReg,
759 MachineOperand &HiOperand,
760 MachineOperand &LoOperand) {
761 unsigned LoReg = LoOperand.getReg();
762 unsigned LoRegKillFlag = getKillRegState(LoOperand.isKill());
763
764 DebugLoc DL = InsertPt->getDebugLoc();
765 MachineBasicBlock *BB = InsertPt->getParent();
766
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000767 // Handle globals.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000768 if (HiOperand.isGlobal()) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000769 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000770 .addGlobalAddress(HiOperand.getGlobal(), HiOperand.getOffset(),
771 HiOperand.getTargetFlags())
772 .addReg(LoReg, LoRegKillFlag);
773 return;
774 }
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000775 // Handle block addresses.
776 if (HiOperand.isBlockAddress()) {
777 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
778 .addBlockAddress(HiOperand.getBlockAddress(), HiOperand.getOffset(),
779 HiOperand.getTargetFlags())
780 .addReg(LoReg, LoRegKillFlag);
781 return;
782 }
783 // Handle jump tables.
784 if (HiOperand.isJTI()) {
785 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
786 .addJumpTableIndex(HiOperand.getIndex(), HiOperand.getTargetFlags())
787 .addReg(LoReg, LoRegKillFlag);
788 return;
789 }
790 // Handle constant pools.
791 if (HiOperand.isCPI()) {
792 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
793 .addConstantPoolIndex(HiOperand.getIndex(), HiOperand.getOffset(),
794 HiOperand.getTargetFlags())
795 .addReg(LoReg, LoRegKillFlag);
796 return;
797 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000798 // Insert new combine instruction.
799 // DoubleRegDest = combine #HiImm, LoReg
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000800 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000801 .addImm(HiOperand.getImm())
802 .addReg(LoReg, LoRegKillFlag);
803}
804
805void HexagonCopyToCombine::emitCombineRI(MachineBasicBlock::iterator &InsertPt,
806 unsigned DoubleDestReg,
807 MachineOperand &HiOperand,
808 MachineOperand &LoOperand) {
809 unsigned HiRegKillFlag = getKillRegState(HiOperand.isKill());
810 unsigned HiReg = HiOperand.getReg();
811
812 DebugLoc DL = InsertPt->getDebugLoc();
813 MachineBasicBlock *BB = InsertPt->getParent();
814
815 // Handle global.
816 if (LoOperand.isGlobal()) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000817 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000818 .addReg(HiReg, HiRegKillFlag)
819 .addGlobalAddress(LoOperand.getGlobal(), LoOperand.getOffset(),
820 LoOperand.getTargetFlags());
821 return;
822 }
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000823 // Handle block addresses.
824 if (LoOperand.isBlockAddress()) {
825 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
826 .addReg(HiReg, HiRegKillFlag)
827 .addBlockAddress(LoOperand.getBlockAddress(), LoOperand.getOffset(),
828 LoOperand.getTargetFlags());
829 return;
830 }
831 // Handle jump tables.
832 if (LoOperand.isJTI()) {
833 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
834 .addReg(HiOperand.getReg(), HiRegKillFlag)
835 .addJumpTableIndex(LoOperand.getIndex(), LoOperand.getTargetFlags());
836 return;
837 }
838 // Handle constant pools.
839 if (LoOperand.isCPI()) {
840 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
841 .addReg(HiOperand.getReg(), HiRegKillFlag)
842 .addConstantPoolIndex(LoOperand.getIndex(), LoOperand.getOffset(),
843 LoOperand.getTargetFlags());
844 return;
845 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000846
847 // Insert new combine instruction.
848 // DoubleRegDest = combine HiReg, #LoImm
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000849 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000850 .addReg(HiReg, HiRegKillFlag)
851 .addImm(LoOperand.getImm());
852}
853
854void HexagonCopyToCombine::emitCombineRR(MachineBasicBlock::iterator &InsertPt,
855 unsigned DoubleDestReg,
856 MachineOperand &HiOperand,
857 MachineOperand &LoOperand) {
858 unsigned LoRegKillFlag = getKillRegState(LoOperand.isKill());
859 unsigned HiRegKillFlag = getKillRegState(HiOperand.isKill());
860 unsigned LoReg = LoOperand.getReg();
861 unsigned HiReg = HiOperand.getReg();
862
863 DebugLoc DL = InsertPt->getDebugLoc();
864 MachineBasicBlock *BB = InsertPt->getParent();
865
866 // Insert new combine instruction.
867 // DoubleRegDest = combine HiReg, LoReg
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000868 unsigned NewOpc;
869 if (Hexagon::DoubleRegsRegClass.contains(DoubleDestReg)) {
870 NewOpc = Hexagon::A2_combinew;
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000871 } else if (Hexagon::HvxWRRegClass.contains(DoubleDestReg)) {
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000872 assert(ST->useHVXOps());
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000873 NewOpc = Hexagon::V6_vcombine;
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000874 } else
875 llvm_unreachable("Unexpected register");
876
877 BuildMI(*BB, InsertPt, DL, TII->get(NewOpc), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000878 .addReg(HiReg, HiRegKillFlag)
879 .addReg(LoReg, LoRegKillFlag);
880}
881
882FunctionPass *llvm::createHexagonCopyToCombine() {
883 return new HexagonCopyToCombine();
884}