blob: 28965b69e284161d48b4f87421573c7f6ffca617 [file] [log] [blame]
Jyotsna Verma803e5062013-05-14 18:54:06 +00001//===------- 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//===----------------------------------------------------------------------===//
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000014#include "HexagonInstrInfo.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000015#include "HexagonSubtarget.h"
Jyotsna Verma803e5062013-05-14 18:54:06 +000016#include "llvm/ADT/DenseMap.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000017#include "llvm/ADT/DenseSet.h"
Jyotsna Verma803e5062013-05-14 18:54:06 +000018#include "llvm/CodeGen/MachineBasicBlock.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/CodeGen/MachineInstr.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000023#include "llvm/CodeGen/Passes.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000024#include "llvm/CodeGen/TargetRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000025#include "llvm/PassSupport.h"
Jyotsna Verma803e5062013-05-14 18:54:06 +000026#include "llvm/Support/CodeGen.h"
27#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/raw_ostream.h"
Jyotsna Verma803e5062013-05-14 18:54:06 +000030
31using namespace llvm;
32
Chandler Carruth84e68b22014-04-22 02:41:26 +000033#define DEBUG_TYPE "hexagon-copy-combine"
34
Jyotsna Verma803e5062013-05-14 18:54:06 +000035static
36cl::opt<bool> IsCombinesDisabled("disable-merge-into-combines",
37 cl::Hidden, cl::ZeroOrMore,
38 cl::init(false),
39 cl::desc("Disable merging into combines"));
40static
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +000041cl::opt<bool> IsConst64Disabled("disable-const64",
42 cl::Hidden, cl::ZeroOrMore,
43 cl::init(false),
44 cl::desc("Disable generation of const64"));
45static
Jyotsna Verma803e5062013-05-14 18:54:06 +000046cl::opt<unsigned>
47MaxNumOfInstsBetweenNewValueStoreAndTFR("max-num-inst-between-tfr-and-nv-store",
48 cl::Hidden, cl::init(4),
49 cl::desc("Maximum distance between a tfr feeding a store we "
50 "consider the store still to be newifiable"));
51
52namespace llvm {
Colin LeMahieu56efafc2015-06-15 19:05:35 +000053 FunctionPass *createHexagonCopyToCombine();
Jyotsna Verma803e5062013-05-14 18:54:06 +000054 void initializeHexagonCopyToCombinePass(PassRegistry&);
55}
56
57
58namespace {
59
60class HexagonCopyToCombine : public MachineFunctionPass {
61 const HexagonInstrInfo *TII;
62 const TargetRegisterInfo *TRI;
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +000063 const HexagonSubtarget *ST;
Jyotsna Verma803e5062013-05-14 18:54:06 +000064 bool ShouldCombineAggressively;
65
66 DenseSet<MachineInstr *> PotentiallyNewifiableTFR;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +000067 SmallVector<MachineInstr *, 8> DbgMItoMove;
68
Jyotsna Verma803e5062013-05-14 18:54:06 +000069public:
70 static char ID;
71
72 HexagonCopyToCombine() : MachineFunctionPass(ID) {
73 initializeHexagonCopyToCombinePass(*PassRegistry::getPassRegistry());
74 }
75
Craig Topper906c2cd2014-04-29 07:58:16 +000076 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jyotsna Verma803e5062013-05-14 18:54:06 +000077 MachineFunctionPass::getAnalysisUsage(AU);
78 }
79
Mehdi Amini117296c2016-10-01 02:56:57 +000080 StringRef getPassName() const override {
Jyotsna Verma803e5062013-05-14 18:54:06 +000081 return "Hexagon Copy-To-Combine Pass";
82 }
83
Craig Topper906c2cd2014-04-29 07:58:16 +000084 bool runOnMachineFunction(MachineFunction &Fn) override;
Jyotsna Verma803e5062013-05-14 18:54:06 +000085
Derek Schuff1dbf7a52016-04-04 17:09:25 +000086 MachineFunctionProperties getRequiredProperties() const override {
87 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000088 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000089 }
90
Jyotsna Verma803e5062013-05-14 18:54:06 +000091private:
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000092 MachineInstr *findPairable(MachineInstr &I1, bool &DoInsertAtI1,
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +000093 bool AllowC64);
Jyotsna Verma803e5062013-05-14 18:54:06 +000094
95 void findPotentialNewifiableTFRs(MachineBasicBlock &);
96
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000097 void combine(MachineInstr &I1, MachineInstr &I2,
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +000098 MachineBasicBlock::iterator &MI, bool DoInsertAtI1,
99 bool OptForSize);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000100
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000101 bool isSafeToMoveTogether(MachineInstr &I1, MachineInstr &I2,
Jyotsna Verma803e5062013-05-14 18:54:06 +0000102 unsigned I1DestReg, unsigned I2DestReg,
103 bool &DoInsertAtI1);
104
105 void emitCombineRR(MachineBasicBlock::iterator &Before, unsigned DestReg,
106 MachineOperand &HiOperand, MachineOperand &LoOperand);
107
108 void emitCombineRI(MachineBasicBlock::iterator &Before, unsigned DestReg,
109 MachineOperand &HiOperand, MachineOperand &LoOperand);
110
111 void emitCombineIR(MachineBasicBlock::iterator &Before, unsigned DestReg,
112 MachineOperand &HiOperand, MachineOperand &LoOperand);
113
114 void emitCombineII(MachineBasicBlock::iterator &Before, unsigned DestReg,
115 MachineOperand &HiOperand, MachineOperand &LoOperand);
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000116
117 void emitConst64(MachineBasicBlock::iterator &Before, unsigned DestReg,
118 MachineOperand &HiOperand, MachineOperand &LoOperand);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000119};
120
121} // End anonymous namespace.
122
123char HexagonCopyToCombine::ID = 0;
124
125INITIALIZE_PASS(HexagonCopyToCombine, "hexagon-copy-combine",
126 "Hexagon Copy-To-Combine Pass", false, false)
127
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000128static bool isCombinableInstType(MachineInstr &MI, const HexagonInstrInfo *TII,
Jyotsna Verma803e5062013-05-14 18:54:06 +0000129 bool ShouldCombineAggressively) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000130 switch (MI.getOpcode()) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000131 case Hexagon::A2_tfr: {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000132 // A COPY instruction can be combined if its arguments are IntRegs (32bit).
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000133 const MachineOperand &Op0 = MI.getOperand(0);
134 const MachineOperand &Op1 = MI.getOperand(1);
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000135 assert(Op0.isReg() && Op1.isReg());
Jyotsna Verma803e5062013-05-14 18:54:06 +0000136
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000137 unsigned DestReg = Op0.getReg();
138 unsigned SrcReg = Op1.getReg();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000139 return Hexagon::IntRegsRegClass.contains(DestReg) &&
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000140 Hexagon::IntRegsRegClass.contains(SrcReg);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000141 }
142
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000143 case Hexagon::A2_tfrsi: {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000144 // A transfer-immediate can be combined if its argument is a signed 8bit
145 // value.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000146 const MachineOperand &Op0 = MI.getOperand(0);
147 const MachineOperand &Op1 = MI.getOperand(1);
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000148 assert(Op0.isReg());
Jyotsna Verma803e5062013-05-14 18:54:06 +0000149
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000150 unsigned DestReg = Op0.getReg();
151 // Ensure that TargetFlags are MO_NO_FLAG for a global. This is a
152 // workaround for an ABI bug that prevents GOT relocations on combine
153 // instructions
154 if (!Op1.isImm() && Op1.getTargetFlags() != HexagonII::MO_NO_FLAG)
155 return false;
156
157 // Only combine constant extended A2_tfrsi if we are in aggressive mode.
158 bool NotExt = Op1.isImm() && isInt<8>(Op1.getImm());
Jyotsna Verma803e5062013-05-14 18:54:06 +0000159 return Hexagon::IntRegsRegClass.contains(DestReg) &&
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000160 (ShouldCombineAggressively || NotExt);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000161 }
162
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000163 case Hexagon::V6_vassign:
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000164 return true;
165
Jyotsna Verma803e5062013-05-14 18:54:06 +0000166 default:
167 break;
168 }
169
170 return false;
171}
172
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000173template <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 Parzyszekcd97c982015-04-22 18:25:53 +0000177 return !Op.isImm() || !isInt<N>(Op.getImm());
178 }
179 return false;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000180}
181
182/// areCombinableOperations - Returns true if the two instruction can be merge
183/// into a combine (ignoring register constraints).
184static bool areCombinableOperations(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000185 MachineInstr &HighRegInst,
186 MachineInstr &LowRegInst, bool AllowC64) {
187 unsigned HiOpc = HighRegInst.getOpcode();
188 unsigned LoOpc = LowRegInst.getOpcode();
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000189
190 auto verifyOpc = [](unsigned Opc) -> void {
191 switch (Opc) {
192 case Hexagon::A2_tfr:
193 case Hexagon::A2_tfrsi:
194 case Hexagon::V6_vassign:
195 break;
196 default:
197 llvm_unreachable("Unexpected opcode");
198 }
199 };
200 verifyOpc(HiOpc);
201 verifyOpc(LoOpc);
202
203 if (HiOpc == Hexagon::V6_vassign || LoOpc == Hexagon::V6_vassign)
204 return HiOpc == LoOpc;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000205
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000206 if (!AllowC64) {
207 // There is no combine of two constant extended values.
208 if (isGreaterThanNBitTFRI<8>(HighRegInst) &&
209 isGreaterThanNBitTFRI<6>(LowRegInst))
210 return false;
211 }
212
213 // There is a combine of two constant extended values into CONST64,
214 // provided both constants are true immediates.
215 if (isGreaterThanNBitTFRI<16>(HighRegInst) &&
216 isGreaterThanNBitTFRI<16>(LowRegInst))
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000217 return (HighRegInst.getOperand(1).isImm() &&
218 LowRegInst.getOperand(1).isImm());
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000219
220 // There is no combine of two constant extended values, unless handled above
221 // Make both 8-bit size checks to allow both combine (#,##) and combine(##,#)
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000222 if (isGreaterThanNBitTFRI<8>(HighRegInst) &&
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000223 isGreaterThanNBitTFRI<8>(LowRegInst))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000224 return false;
225
226 return true;
227}
228
229static bool isEvenReg(unsigned Reg) {
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000230 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
231 if (Hexagon::IntRegsRegClass.contains(Reg))
232 return (Reg - Hexagon::R0) % 2 == 0;
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000233 if (Hexagon::HvxVRRegClass.contains(Reg))
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000234 return (Reg - Hexagon::V0) % 2 == 0;
235 llvm_unreachable("Invalid register");
Jyotsna Verma803e5062013-05-14 18:54:06 +0000236}
237
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000238static void removeKillInfo(MachineInstr &MI, unsigned RegNotKilled) {
239 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
240 MachineOperand &Op = MI.getOperand(I);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000241 if (!Op.isReg() || Op.getReg() != RegNotKilled || !Op.isKill())
242 continue;
243 Op.setIsKill(false);
244 }
245}
246
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000247/// Returns true if it is unsafe to move a copy instruction from \p UseReg to
248/// \p DestReg over the instruction \p MI.
249static bool isUnsafeToMoveAcross(MachineInstr &MI, unsigned UseReg,
250 unsigned DestReg,
251 const TargetRegisterInfo *TRI) {
252 return (UseReg && (MI.modifiesRegister(UseReg, TRI))) ||
253 MI.modifiesRegister(DestReg, TRI) || MI.readsRegister(DestReg, TRI) ||
Krzysztof Parzyszek709e4f92017-08-10 15:00:30 +0000254 MI.hasUnmodeledSideEffects() || MI.isInlineAsm() ||
255 MI.isMetaInstruction();
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000256}
257
258static unsigned UseReg(const MachineOperand& MO) {
259 return MO.isReg() ? MO.getReg() : 0;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000260}
261
262/// isSafeToMoveTogether - Returns true if it is safe to move I1 next to I2 such
263/// that the two instructions can be paired in a combine.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000264bool HexagonCopyToCombine::isSafeToMoveTogether(MachineInstr &I1,
265 MachineInstr &I2,
Jyotsna Verma803e5062013-05-14 18:54:06 +0000266 unsigned I1DestReg,
267 unsigned I2DestReg,
268 bool &DoInsertAtI1) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000269 unsigned I2UseReg = UseReg(I2.getOperand(1));
Jyotsna Verma803e5062013-05-14 18:54:06 +0000270
271 // It is not safe to move I1 and I2 into one combine if I2 has a true
272 // dependence on I1.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000273 if (I2UseReg && I1.modifiesRegister(I2UseReg, TRI))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000274 return false;
275
276 bool isSafe = true;
277
278 // First try to move I2 towards I1.
279 {
280 // A reverse_iterator instantiated like below starts before I2, and I1
281 // respectively.
282 // Look at instructions I in between I2 and (excluding) I1.
283 MachineBasicBlock::reverse_iterator I(I2),
284 End = --(MachineBasicBlock::reverse_iterator(I1));
285 // At 03 we got better results (dhrystone!) by being more conservative.
286 if (!ShouldCombineAggressively)
287 End = MachineBasicBlock::reverse_iterator(I1);
288 // If I2 kills its operand and we move I2 over an instruction that also
289 // uses I2's use reg we need to modify that (first) instruction to now kill
290 // this reg.
291 unsigned KilledOperand = 0;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000292 if (I2.killsRegister(I2UseReg))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000293 KilledOperand = I2UseReg;
Craig Topper062a2ba2014-04-25 05:30:21 +0000294 MachineInstr *KillingInstr = nullptr;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000295
296 for (; I != End; ++I) {
297 // If the intervening instruction I:
298 // * modifies I2's use reg
299 // * modifies I2's def reg
300 // * reads I2's def reg
301 // * or has unmodelled side effects
302 // we can't move I2 across it.
Shiva Chen801bf7e2018-05-09 02:42:00 +0000303 if (I->isDebugInstr())
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000304 continue;
305
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000306 if (isUnsafeToMoveAcross(*I, I2UseReg, I2DestReg, TRI)) {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000307 isSafe = false;
308 break;
309 }
310
311 // Update first use of the killed operand.
312 if (!KillingInstr && KilledOperand &&
313 I->readsRegister(KilledOperand, TRI))
314 KillingInstr = &*I;
315 }
316 if (isSafe) {
317 // Update the intermediate instruction to with the kill flag.
318 if (KillingInstr) {
319 bool Added = KillingInstr->addRegisterKilled(KilledOperand, TRI, true);
Alp Tokercb402912014-01-24 17:20:08 +0000320 (void)Added; // suppress compiler warning
Jyotsna Verma803e5062013-05-14 18:54:06 +0000321 assert(Added && "Must successfully update kill flag");
322 removeKillInfo(I2, KilledOperand);
323 }
324 DoInsertAtI1 = true;
325 return true;
326 }
327 }
328
329 // Try to move I1 towards I2.
330 {
331 // Look at instructions I in between I1 and (excluding) I2.
332 MachineBasicBlock::iterator I(I1), End(I2);
333 // At O3 we got better results (dhrystone) by being more conservative here.
334 if (!ShouldCombineAggressively)
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000335 End = std::next(MachineBasicBlock::iterator(I2));
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000336 unsigned I1UseReg = UseReg(I1.getOperand(1));
Jyotsna Vermacceafb22013-05-28 19:01:45 +0000337 // Track killed operands. If we move across an instruction that kills our
Jyotsna Verma803e5062013-05-14 18:54:06 +0000338 // operand, we need to update the kill information on the moved I1. It kills
339 // the operand now.
Craig Topper062a2ba2014-04-25 05:30:21 +0000340 MachineInstr *KillingInstr = nullptr;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000341 unsigned KilledOperand = 0;
342
343 while(++I != End) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000344 MachineInstr &MI = *I;
345 // If the intervening instruction MI:
Jyotsna Verma803e5062013-05-14 18:54:06 +0000346 // * modifies I1's use reg
347 // * modifies I1's def reg
348 // * reads I1's def reg
349 // * or has unmodelled side effects
350 // We introduce this special case because llvm has no api to remove a
351 // kill flag for a register (a removeRegisterKilled() analogous to
352 // addRegisterKilled) that handles aliased register correctly.
353 // * or has a killed aliased register use of I1's use reg
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000354 // %d4 = A2_tfrpi 16
355 // %r6 = A2_tfr %r9
356 // %r8 = KILL %r8, implicit killed %d4
Jyotsna Verma803e5062013-05-14 18:54:06 +0000357 // If we want to move R6 = across the KILL instruction we would have
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000358 // to remove the implicit killed %d4 operand. For now, we are
Jyotsna Verma803e5062013-05-14 18:54:06 +0000359 // conservative and disallow the move.
360 // we can't move I1 across it.
Shiva Chen801bf7e2018-05-09 02:42:00 +0000361 if (MI.isDebugInstr()) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000362 if (MI.readsRegister(I1DestReg, TRI)) // Move this instruction after I2.
363 DbgMItoMove.push_back(&MI);
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000364 continue;
365 }
366
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000367 if (isUnsafeToMoveAcross(MI, I1UseReg, I1DestReg, TRI) ||
Jyotsna Verma803e5062013-05-14 18:54:06 +0000368 // Check for an aliased register kill. Bail out if we see one.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000369 (!MI.killsRegister(I1UseReg) && MI.killsRegister(I1UseReg, TRI)))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000370 return false;
371
372 // Check for an exact kill (registers match).
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000373 if (I1UseReg && MI.killsRegister(I1UseReg)) {
Craig Toppere73658d2014-04-28 04:05:08 +0000374 assert(!KillingInstr && "Should only see one killing instruction");
Jyotsna Verma803e5062013-05-14 18:54:06 +0000375 KilledOperand = I1UseReg;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000376 KillingInstr = &MI;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000377 }
378 }
379 if (KillingInstr) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000380 removeKillInfo(*KillingInstr, KilledOperand);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000381 // Update I1 to set the kill flag. This flag will later be picked up by
382 // the new COMBINE instruction.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000383 bool Added = I1.addRegisterKilled(KilledOperand, TRI);
Alp Tokercb402912014-01-24 17:20:08 +0000384 (void)Added; // suppress compiler warning
Jyotsna Verma803e5062013-05-14 18:54:06 +0000385 assert(Added && "Must successfully update kill flag");
386 }
387 DoInsertAtI1 = false;
388 }
389
390 return true;
391}
392
393/// findPotentialNewifiableTFRs - Finds tranfers that feed stores that could be
394/// newified. (A use of a 64 bit register define can not be newified)
395void
396HexagonCopyToCombine::findPotentialNewifiableTFRs(MachineBasicBlock &BB) {
397 DenseMap<unsigned, MachineInstr *> LastDef;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000398 for (MachineInstr &MI : BB) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000399 if (MI.isDebugInstr())
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000400 continue;
401
Jyotsna Verma803e5062013-05-14 18:54:06 +0000402 // Mark TFRs that feed a potential new value store as such.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000403 if (TII->mayBeNewStore(MI)) {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000404 // Look for uses of TFR instructions.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000405 for (unsigned OpdIdx = 0, OpdE = MI.getNumOperands(); OpdIdx != OpdE;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000406 ++OpdIdx) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000407 MachineOperand &Op = MI.getOperand(OpdIdx);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000408
409 // Skip over anything except register uses.
410 if (!Op.isReg() || !Op.isUse() || !Op.getReg())
411 continue;
412
413 // Look for the defining instruction.
414 unsigned Reg = Op.getReg();
415 MachineInstr *DefInst = LastDef[Reg];
416 if (!DefInst)
417 continue;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000418 if (!isCombinableInstType(*DefInst, TII, ShouldCombineAggressively))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000419 continue;
420
421 // Only close newifiable stores should influence the decision.
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000422 // Ignore the debug instructions in between.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000423 MachineBasicBlock::iterator It(DefInst);
424 unsigned NumInstsToDef = 0;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000425 while (&*It != &MI) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000426 if (!It->isDebugInstr())
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000427 ++NumInstsToDef;
Krzysztof Parzyszek14f9535e2016-01-21 12:45:17 +0000428 ++It;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000429 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000430
431 if (NumInstsToDef > MaxNumOfInstsBetweenNewValueStoreAndTFR)
432 continue;
433
434 PotentiallyNewifiableTFR.insert(DefInst);
435 }
436 // Skip to next instruction.
437 continue;
438 }
439
440 // Put instructions that last defined integer or double registers into the
441 // map.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000442 for (MachineOperand &Op : MI.operands()) {
443 if (Op.isReg()) {
444 if (!Op.isDef() || !Op.getReg())
445 continue;
446 unsigned Reg = Op.getReg();
447 if (Hexagon::DoubleRegsRegClass.contains(Reg)) {
448 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
449 LastDef[*SubRegs] = &MI;
450 } else if (Hexagon::IntRegsRegClass.contains(Reg))
451 LastDef[Reg] = &MI;
452 } else if (Op.isRegMask()) {
453 for (unsigned Reg : Hexagon::IntRegsRegClass)
454 if (Op.clobbersPhysReg(Reg))
455 LastDef[Reg] = &MI;
456 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000457 }
458 }
459}
460
461bool HexagonCopyToCombine::runOnMachineFunction(MachineFunction &MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000462 if (skipFunction(MF.getFunction()))
Krzysztof Parzyszek643aaea2017-04-14 15:26:34 +0000463 return false;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000464
465 if (IsCombinesDisabled) return false;
466
467 bool HasChanged = false;
468
469 // Get target info.
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000470 ST = &MF.getSubtarget<HexagonSubtarget>();
471 TRI = ST->getRegisterInfo();
472 TII = ST->getInstrInfo();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000473
Matthias Braunf1caa282017-12-15 22:22:58 +0000474 const Function &F = MF.getFunction();
475 bool OptForSize = F.hasFnAttribute(Attribute::OptimizeForSize);
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000476
Jyotsna Verma803e5062013-05-14 18:54:06 +0000477 // Combine aggressively (for code size)
478 ShouldCombineAggressively =
479 MF.getTarget().getOptLevel() <= CodeGenOpt::Default;
480
481 // Traverse basic blocks.
482 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); BI != BE;
483 ++BI) {
484 PotentiallyNewifiableTFR.clear();
485 findPotentialNewifiableTFRs(*BI);
486
487 // Traverse instructions in basic block.
488 for(MachineBasicBlock::iterator MI = BI->begin(), End = BI->end();
489 MI != End;) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000490 MachineInstr &I1 = *MI++;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000491
Shiva Chen801bf7e2018-05-09 02:42:00 +0000492 if (I1.isDebugInstr())
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000493 continue;
494
Jyotsna Verma803e5062013-05-14 18:54:06 +0000495 // Don't combine a TFR whose user could be newified (instructions that
496 // define double registers can not be newified - Programmer's Ref Manual
497 // 5.4.2 New-value stores).
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000498 if (ShouldCombineAggressively && PotentiallyNewifiableTFR.count(&I1))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000499 continue;
500
501 // Ignore instructions that are not combinable.
502 if (!isCombinableInstType(I1, TII, ShouldCombineAggressively))
503 continue;
504
505 // Find a second instruction that can be merged into a combine
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000506 // instruction. In addition, also find all the debug instructions that
507 // need to be moved along with it.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000508 bool DoInsertAtI1 = false;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000509 DbgMItoMove.clear();
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000510 MachineInstr *I2 = findPairable(I1, DoInsertAtI1, OptForSize);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000511 if (I2) {
512 HasChanged = true;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000513 combine(I1, *I2, MI, DoInsertAtI1, OptForSize);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000514 }
515 }
516 }
517
518 return HasChanged;
519}
520
521/// findPairable - Returns an instruction that can be merged with \p I1 into a
522/// COMBINE instruction or 0 if no such instruction can be found. Returns true
523/// in \p DoInsertAtI1 if the combine must be inserted at instruction \p I1
524/// false if the combine must be inserted at the returned instruction.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000525MachineInstr *HexagonCopyToCombine::findPairable(MachineInstr &I1,
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000526 bool &DoInsertAtI1,
527 bool AllowC64) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000528 MachineBasicBlock::iterator I2 = std::next(MachineBasicBlock::iterator(I1));
Shiva Chen801bf7e2018-05-09 02:42:00 +0000529 while (I2 != I1.getParent()->end() && I2->isDebugInstr())
Krzysztof Parzyszek6dff3362016-08-24 22:36:35 +0000530 ++I2;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000531
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000532 unsigned I1DestReg = I1.getOperand(0).getReg();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000533
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000534 for (MachineBasicBlock::iterator End = I1.getParent()->end(); I2 != End;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000535 ++I2) {
536 // Bail out early if we see a second definition of I1DestReg.
537 if (I2->modifiesRegister(I1DestReg, TRI))
538 break;
539
540 // Ignore non-combinable instructions.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000541 if (!isCombinableInstType(*I2, TII, ShouldCombineAggressively))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000542 continue;
543
544 // Don't combine a TFR whose user could be newified.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000545 if (ShouldCombineAggressively && PotentiallyNewifiableTFR.count(&*I2))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000546 continue;
547
548 unsigned I2DestReg = I2->getOperand(0).getReg();
549
550 // Check that registers are adjacent and that the first destination register
551 // is even.
552 bool IsI1LowReg = (I2DestReg - I1DestReg) == 1;
553 bool IsI2LowReg = (I1DestReg - I2DestReg) == 1;
554 unsigned FirstRegIndex = IsI1LowReg ? I1DestReg : I2DestReg;
555 if ((!IsI1LowReg && !IsI2LowReg) || !isEvenReg(FirstRegIndex))
556 continue;
557
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +0000558 // Check that the two instructions are combinable.
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000559 // The order matters because in a A2_tfrsi we might can encode a int8 as
560 // the hi reg operand but only a uint6 as the low reg operand.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000561 if ((IsI2LowReg && !areCombinableOperations(TRI, I1, *I2, AllowC64)) ||
562 (IsI1LowReg && !areCombinableOperations(TRI, *I2, I1, AllowC64)))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000563 break;
564
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000565 if (isSafeToMoveTogether(I1, *I2, I1DestReg, I2DestReg, DoInsertAtI1))
566 return &*I2;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000567
568 // Not safe. Stop searching.
569 break;
570 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000571 return nullptr;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000572}
573
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000574void HexagonCopyToCombine::combine(MachineInstr &I1, MachineInstr &I2,
Jyotsna Verma803e5062013-05-14 18:54:06 +0000575 MachineBasicBlock::iterator &MI,
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000576 bool DoInsertAtI1, bool OptForSize) {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000577 // We are going to delete I2. If MI points to I2 advance it to the next
578 // instruction.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000579 if (MI == I2.getIterator())
580 ++MI;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000581
582 // Figure out whether I1 or I2 goes into the lowreg part.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000583 unsigned I1DestReg = I1.getOperand(0).getReg();
584 unsigned I2DestReg = I2.getOperand(0).getReg();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000585 bool IsI1Loreg = (I2DestReg - I1DestReg) == 1;
586 unsigned LoRegDef = IsI1Loreg ? I1DestReg : I2DestReg;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000587 unsigned SubLo;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000588
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000589 const TargetRegisterClass *SuperRC = nullptr;
590 if (Hexagon::IntRegsRegClass.contains(LoRegDef)) {
591 SuperRC = &Hexagon::DoubleRegsRegClass;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000592 SubLo = Hexagon::isub_lo;
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000593 } else if (Hexagon::HvxVRRegClass.contains(LoRegDef)) {
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000594 assert(ST->useHVXOps());
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000595 SuperRC = &Hexagon::HvxWRRegClass;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000596 SubLo = Hexagon::vsub_lo;
Krzysztof Parzyszekf817efb2016-11-09 17:50:46 +0000597 } else
598 llvm_unreachable("Unexpected register class");
599
Jyotsna Verma803e5062013-05-14 18:54:06 +0000600 // Get the double word register.
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000601 unsigned DoubleRegDest = TRI->getMatchingSuperReg(LoRegDef, SubLo, SuperRC);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000602 assert(DoubleRegDest != 0 && "Expect a valid register");
603
Jyotsna Verma803e5062013-05-14 18:54:06 +0000604 // Setup source operands.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000605 MachineOperand &LoOperand = IsI1Loreg ? I1.getOperand(1) : I2.getOperand(1);
606 MachineOperand &HiOperand = IsI1Loreg ? I2.getOperand(1) : I1.getOperand(1);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000607
608 // Figure out which source is a register and which a constant.
609 bool IsHiReg = HiOperand.isReg();
610 bool IsLoReg = LoOperand.isReg();
611
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000612 // There is a combine of two constant extended values into CONST64.
613 bool IsC64 = OptForSize && LoOperand.isImm() && HiOperand.isImm() &&
614 isGreaterThanNBitTFRI<16>(I1) && isGreaterThanNBitTFRI<16>(I2);
615
Jyotsna Verma803e5062013-05-14 18:54:06 +0000616 MachineBasicBlock::iterator InsertPt(DoInsertAtI1 ? I1 : I2);
617 // Emit combine.
618 if (IsHiReg && IsLoReg)
619 emitCombineRR(InsertPt, DoubleRegDest, HiOperand, LoOperand);
620 else if (IsHiReg)
621 emitCombineRI(InsertPt, DoubleRegDest, HiOperand, LoOperand);
622 else if (IsLoReg)
623 emitCombineIR(InsertPt, DoubleRegDest, HiOperand, LoOperand);
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000624 else if (IsC64 && !IsConst64Disabled)
625 emitConst64(InsertPt, DoubleRegDest, HiOperand, LoOperand);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000626 else
627 emitCombineII(InsertPt, DoubleRegDest, HiOperand, LoOperand);
628
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000629 // Move debug instructions along with I1 if it's being
630 // moved towards I2.
631 if (!DoInsertAtI1 && DbgMItoMove.size() != 0) {
632 // Insert debug instructions at the new location before I2.
633 MachineBasicBlock *BB = InsertPt->getParent();
634 for (auto NewMI : DbgMItoMove) {
635 // If iterator MI is pointing to DEBUG_VAL, make sure
636 // MI now points to next relevant instruction.
Duncan P. N. Exon Smithf197b1f2016-08-12 05:05:36 +0000637 if (NewMI == MI)
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000638 ++MI;
639 BB->splice(InsertPt, BB, NewMI);
640 }
641 }
642
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000643 I1.eraseFromParent();
644 I2.eraseFromParent();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000645}
646
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000647void HexagonCopyToCombine::emitConst64(MachineBasicBlock::iterator &InsertPt,
648 unsigned DoubleDestReg,
649 MachineOperand &HiOperand,
650 MachineOperand &LoOperand) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000651 LLVM_DEBUG(dbgs() << "Found a CONST64\n");
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000652
653 DebugLoc DL = InsertPt->getDebugLoc();
654 MachineBasicBlock *BB = InsertPt->getParent();
655 assert(LoOperand.isImm() && HiOperand.isImm() &&
656 "Both operands must be immediate");
657
658 int64_t V = HiOperand.getImm();
659 V = (V << 32) | (0x0ffffffffLL & LoOperand.getImm());
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +0000660 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::CONST64), DoubleDestReg)
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000661 .addImm(V);
662}
663
Jyotsna Verma803e5062013-05-14 18:54:06 +0000664void HexagonCopyToCombine::emitCombineII(MachineBasicBlock::iterator &InsertPt,
665 unsigned DoubleDestReg,
666 MachineOperand &HiOperand,
667 MachineOperand &LoOperand) {
668 DebugLoc DL = InsertPt->getDebugLoc();
669 MachineBasicBlock *BB = InsertPt->getParent();
670
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000671 // Handle globals.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000672 if (HiOperand.isGlobal()) {
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000673 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000674 .addGlobalAddress(HiOperand.getGlobal(), HiOperand.getOffset(),
675 HiOperand.getTargetFlags())
676 .addImm(LoOperand.getImm());
677 return;
678 }
679 if (LoOperand.isGlobal()) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000680 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000681 .addImm(HiOperand.getImm())
682 .addGlobalAddress(LoOperand.getGlobal(), LoOperand.getOffset(),
683 LoOperand.getTargetFlags());
684 return;
685 }
686
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000687 // Handle block addresses.
688 if (HiOperand.isBlockAddress()) {
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000689 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000690 .addBlockAddress(HiOperand.getBlockAddress(), HiOperand.getOffset(),
691 HiOperand.getTargetFlags())
Jyotsna Verma803e5062013-05-14 18:54:06 +0000692 .addImm(LoOperand.getImm());
693 return;
694 }
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000695 if (LoOperand.isBlockAddress()) {
696 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
697 .addImm(HiOperand.getImm())
698 .addBlockAddress(LoOperand.getBlockAddress(), LoOperand.getOffset(),
699 LoOperand.getTargetFlags());
700 return;
701 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000702
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000703 // Handle jump tables.
704 if (HiOperand.isJTI()) {
705 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
706 .addJumpTableIndex(HiOperand.getIndex(), HiOperand.getTargetFlags())
707 .addImm(LoOperand.getImm());
708 return;
709 }
710 if (LoOperand.isJTI()) {
711 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
712 .addImm(HiOperand.getImm())
713 .addJumpTableIndex(LoOperand.getIndex(), LoOperand.getTargetFlags());
714 return;
715 }
716
717 // Handle constant pools.
718 if (HiOperand.isCPI()) {
719 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
720 .addConstantPoolIndex(HiOperand.getIndex(), HiOperand.getOffset(),
721 HiOperand.getTargetFlags())
722 .addImm(LoOperand.getImm());
723 return;
724 }
725 if (LoOperand.isCPI()) {
726 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
727 .addImm(HiOperand.getImm())
728 .addConstantPoolIndex(LoOperand.getIndex(), LoOperand.getOffset(),
729 LoOperand.getTargetFlags());
730 return;
731 }
732
733 // First preference should be given to Hexagon::A2_combineii instruction
734 // as it can include U6 (in Hexagon::A4_combineii) as well.
735 // In this instruction, HiOperand is const extended, if required.
736 if (isInt<8>(LoOperand.getImm())) {
737 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
738 .addImm(HiOperand.getImm())
739 .addImm(LoOperand.getImm());
740 return;
741 }
742
743 // In this instruction, LoOperand is const extended, if required.
744 if (isInt<8>(HiOperand.getImm())) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000745 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000746 .addImm(HiOperand.getImm())
747 .addImm(LoOperand.getImm());
748 return;
749 }
750
751 // Insert new combine instruction.
752 // DoubleRegDest = combine #HiImm, #LoImm
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000753 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000754 .addImm(HiOperand.getImm())
755 .addImm(LoOperand.getImm());
756}
757
758void HexagonCopyToCombine::emitCombineIR(MachineBasicBlock::iterator &InsertPt,
759 unsigned DoubleDestReg,
760 MachineOperand &HiOperand,
761 MachineOperand &LoOperand) {
762 unsigned LoReg = LoOperand.getReg();
763 unsigned LoRegKillFlag = getKillRegState(LoOperand.isKill());
764
765 DebugLoc DL = InsertPt->getDebugLoc();
766 MachineBasicBlock *BB = InsertPt->getParent();
767
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000768 // Handle globals.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000769 if (HiOperand.isGlobal()) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000770 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000771 .addGlobalAddress(HiOperand.getGlobal(), HiOperand.getOffset(),
772 HiOperand.getTargetFlags())
773 .addReg(LoReg, LoRegKillFlag);
774 return;
775 }
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000776 // Handle block addresses.
777 if (HiOperand.isBlockAddress()) {
778 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
779 .addBlockAddress(HiOperand.getBlockAddress(), HiOperand.getOffset(),
780 HiOperand.getTargetFlags())
781 .addReg(LoReg, LoRegKillFlag);
782 return;
783 }
784 // Handle jump tables.
785 if (HiOperand.isJTI()) {
786 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
787 .addJumpTableIndex(HiOperand.getIndex(), HiOperand.getTargetFlags())
788 .addReg(LoReg, LoRegKillFlag);
789 return;
790 }
791 // Handle constant pools.
792 if (HiOperand.isCPI()) {
793 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
794 .addConstantPoolIndex(HiOperand.getIndex(), HiOperand.getOffset(),
795 HiOperand.getTargetFlags())
796 .addReg(LoReg, LoRegKillFlag);
797 return;
798 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000799 // Insert new combine instruction.
800 // DoubleRegDest = combine #HiImm, LoReg
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000801 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000802 .addImm(HiOperand.getImm())
803 .addReg(LoReg, LoRegKillFlag);
804}
805
806void HexagonCopyToCombine::emitCombineRI(MachineBasicBlock::iterator &InsertPt,
807 unsigned DoubleDestReg,
808 MachineOperand &HiOperand,
809 MachineOperand &LoOperand) {
810 unsigned HiRegKillFlag = getKillRegState(HiOperand.isKill());
811 unsigned HiReg = HiOperand.getReg();
812
813 DebugLoc DL = InsertPt->getDebugLoc();
814 MachineBasicBlock *BB = InsertPt->getParent();
815
816 // Handle global.
817 if (LoOperand.isGlobal()) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000818 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000819 .addReg(HiReg, HiRegKillFlag)
820 .addGlobalAddress(LoOperand.getGlobal(), LoOperand.getOffset(),
821 LoOperand.getTargetFlags());
822 return;
823 }
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000824 // Handle block addresses.
825 if (LoOperand.isBlockAddress()) {
826 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
827 .addReg(HiReg, HiRegKillFlag)
828 .addBlockAddress(LoOperand.getBlockAddress(), LoOperand.getOffset(),
829 LoOperand.getTargetFlags());
830 return;
831 }
832 // Handle jump tables.
833 if (LoOperand.isJTI()) {
834 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
835 .addReg(HiOperand.getReg(), HiRegKillFlag)
836 .addJumpTableIndex(LoOperand.getIndex(), LoOperand.getTargetFlags());
837 return;
838 }
839 // Handle constant pools.
840 if (LoOperand.isCPI()) {
841 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
842 .addReg(HiOperand.getReg(), HiRegKillFlag)
843 .addConstantPoolIndex(LoOperand.getIndex(), LoOperand.getOffset(),
844 LoOperand.getTargetFlags());
845 return;
846 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000847
848 // Insert new combine instruction.
849 // DoubleRegDest = combine HiReg, #LoImm
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000850 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000851 .addReg(HiReg, HiRegKillFlag)
852 .addImm(LoOperand.getImm());
853}
854
855void HexagonCopyToCombine::emitCombineRR(MachineBasicBlock::iterator &InsertPt,
856 unsigned DoubleDestReg,
857 MachineOperand &HiOperand,
858 MachineOperand &LoOperand) {
859 unsigned LoRegKillFlag = getKillRegState(LoOperand.isKill());
860 unsigned HiRegKillFlag = getKillRegState(HiOperand.isKill());
861 unsigned LoReg = LoOperand.getReg();
862 unsigned HiReg = HiOperand.getReg();
863
864 DebugLoc DL = InsertPt->getDebugLoc();
865 MachineBasicBlock *BB = InsertPt->getParent();
866
867 // Insert new combine instruction.
868 // DoubleRegDest = combine HiReg, LoReg
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000869 unsigned NewOpc;
870 if (Hexagon::DoubleRegsRegClass.contains(DoubleDestReg)) {
871 NewOpc = Hexagon::A2_combinew;
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000872 } else if (Hexagon::HvxWRRegClass.contains(DoubleDestReg)) {
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000873 assert(ST->useHVXOps());
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000874 NewOpc = Hexagon::V6_vcombine;
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000875 } else
876 llvm_unreachable("Unexpected register");
877
878 BuildMI(*BB, InsertPt, DL, TII->get(NewOpc), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000879 .addReg(HiReg, HiRegKillFlag)
880 .addReg(LoReg, LoRegKillFlag);
881}
882
883FunctionPass *llvm::createHexagonCopyToCombine() {
884 return new HexagonCopyToCombine();
885}