blob: a07cfa74f5e3070be7cf93e2296b876f9b5ab430 [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"
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +000016#include "llvm/PassSupport.h"
Jyotsna Verma803e5062013-05-14 18:54:06 +000017#include "llvm/ADT/DenseMap.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000018#include "llvm/ADT/DenseSet.h"
Jyotsna Verma803e5062013-05-14 18:54:06 +000019#include "llvm/CodeGen/MachineBasicBlock.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineInstr.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000024#include "llvm/CodeGen/Passes.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"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000029#include "llvm/Target/TargetRegisterInfo.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:
164 case Hexagon::V6_vassign_128B:
165 return true;
166
Jyotsna Verma803e5062013-05-14 18:54:06 +0000167 default:
168 break;
169 }
170
171 return false;
172}
173
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000174template <unsigned N> static bool isGreaterThanNBitTFRI(const MachineInstr &I) {
175 if (I.getOpcode() == Hexagon::TFRI64_V4 ||
176 I.getOpcode() == Hexagon::A2_tfrsi) {
177 const MachineOperand &Op = I.getOperand(1);
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000178 return !Op.isImm() || !isInt<N>(Op.getImm());
179 }
180 return false;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000181}
182
183/// areCombinableOperations - Returns true if the two instruction can be merge
184/// into a combine (ignoring register constraints).
185static bool areCombinableOperations(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000186 MachineInstr &HighRegInst,
187 MachineInstr &LowRegInst, bool AllowC64) {
188 unsigned HiOpc = HighRegInst.getOpcode();
189 unsigned LoOpc = LowRegInst.getOpcode();
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000190
191 auto verifyOpc = [](unsigned Opc) -> void {
192 switch (Opc) {
193 case Hexagon::A2_tfr:
194 case Hexagon::A2_tfrsi:
195 case Hexagon::V6_vassign:
196 break;
197 default:
198 llvm_unreachable("Unexpected opcode");
199 }
200 };
201 verifyOpc(HiOpc);
202 verifyOpc(LoOpc);
203
204 if (HiOpc == Hexagon::V6_vassign || LoOpc == Hexagon::V6_vassign)
205 return HiOpc == LoOpc;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000206
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000207 if (!AllowC64) {
208 // There is no combine of two constant extended values.
209 if (isGreaterThanNBitTFRI<8>(HighRegInst) &&
210 isGreaterThanNBitTFRI<6>(LowRegInst))
211 return false;
212 }
213
214 // There is a combine of two constant extended values into CONST64,
215 // provided both constants are true immediates.
216 if (isGreaterThanNBitTFRI<16>(HighRegInst) &&
217 isGreaterThanNBitTFRI<16>(LowRegInst))
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000218 return (HighRegInst.getOperand(1).isImm() &&
219 LowRegInst.getOperand(1).isImm());
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000220
221 // There is no combine of two constant extended values, unless handled above
222 // Make both 8-bit size checks to allow both combine (#,##) and combine(##,#)
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000223 if (isGreaterThanNBitTFRI<8>(HighRegInst) &&
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000224 isGreaterThanNBitTFRI<8>(LowRegInst))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000225 return false;
226
227 return true;
228}
229
230static bool isEvenReg(unsigned Reg) {
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000231 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
232 if (Hexagon::IntRegsRegClass.contains(Reg))
233 return (Reg - Hexagon::R0) % 2 == 0;
234 if (Hexagon::VectorRegsRegClass.contains(Reg) ||
235 Hexagon::VectorRegs128BRegClass.contains(Reg))
236 return (Reg - Hexagon::V0) % 2 == 0;
237 llvm_unreachable("Invalid register");
Jyotsna Verma803e5062013-05-14 18:54:06 +0000238}
239
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000240static void removeKillInfo(MachineInstr &MI, unsigned RegNotKilled) {
241 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
242 MachineOperand &Op = MI.getOperand(I);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000243 if (!Op.isReg() || Op.getReg() != RegNotKilled || !Op.isKill())
244 continue;
245 Op.setIsKill(false);
246 }
247}
248
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000249/// Returns true if it is unsafe to move a copy instruction from \p UseReg to
250/// \p DestReg over the instruction \p MI.
251static bool isUnsafeToMoveAcross(MachineInstr &MI, unsigned UseReg,
252 unsigned DestReg,
253 const TargetRegisterInfo *TRI) {
254 return (UseReg && (MI.modifiesRegister(UseReg, TRI))) ||
255 MI.modifiesRegister(DestReg, TRI) || MI.readsRegister(DestReg, TRI) ||
256 MI.hasUnmodeledSideEffects() || MI.isInlineAsm() || MI.isDebugValue();
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000257}
258
259static unsigned UseReg(const MachineOperand& MO) {
260 return MO.isReg() ? MO.getReg() : 0;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000261}
262
263/// isSafeToMoveTogether - Returns true if it is safe to move I1 next to I2 such
264/// that the two instructions can be paired in a combine.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000265bool HexagonCopyToCombine::isSafeToMoveTogether(MachineInstr &I1,
266 MachineInstr &I2,
Jyotsna Verma803e5062013-05-14 18:54:06 +0000267 unsigned I1DestReg,
268 unsigned I2DestReg,
269 bool &DoInsertAtI1) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000270 unsigned I2UseReg = UseReg(I2.getOperand(1));
Jyotsna Verma803e5062013-05-14 18:54:06 +0000271
272 // It is not safe to move I1 and I2 into one combine if I2 has a true
273 // dependence on I1.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000274 if (I2UseReg && I1.modifiesRegister(I2UseReg, TRI))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000275 return false;
276
277 bool isSafe = true;
278
279 // First try to move I2 towards I1.
280 {
281 // A reverse_iterator instantiated like below starts before I2, and I1
282 // respectively.
283 // Look at instructions I in between I2 and (excluding) I1.
284 MachineBasicBlock::reverse_iterator I(I2),
285 End = --(MachineBasicBlock::reverse_iterator(I1));
286 // At 03 we got better results (dhrystone!) by being more conservative.
287 if (!ShouldCombineAggressively)
288 End = MachineBasicBlock::reverse_iterator(I1);
289 // If I2 kills its operand and we move I2 over an instruction that also
290 // uses I2's use reg we need to modify that (first) instruction to now kill
291 // this reg.
292 unsigned KilledOperand = 0;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000293 if (I2.killsRegister(I2UseReg))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000294 KilledOperand = I2UseReg;
Craig Topper062a2ba2014-04-25 05:30:21 +0000295 MachineInstr *KillingInstr = nullptr;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000296
297 for (; I != End; ++I) {
298 // If the intervening instruction I:
299 // * modifies I2's use reg
300 // * modifies I2's def reg
301 // * reads I2's def reg
302 // * or has unmodelled side effects
303 // we can't move I2 across it.
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000304 if (I->isDebugValue())
305 continue;
306
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000307 if (isUnsafeToMoveAcross(*I, I2UseReg, I2DestReg, TRI)) {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000308 isSafe = false;
309 break;
310 }
311
312 // Update first use of the killed operand.
313 if (!KillingInstr && KilledOperand &&
314 I->readsRegister(KilledOperand, TRI))
315 KillingInstr = &*I;
316 }
317 if (isSafe) {
318 // Update the intermediate instruction to with the kill flag.
319 if (KillingInstr) {
320 bool Added = KillingInstr->addRegisterKilled(KilledOperand, TRI, true);
Alp Tokercb402912014-01-24 17:20:08 +0000321 (void)Added; // suppress compiler warning
Jyotsna Verma803e5062013-05-14 18:54:06 +0000322 assert(Added && "Must successfully update kill flag");
323 removeKillInfo(I2, KilledOperand);
324 }
325 DoInsertAtI1 = true;
326 return true;
327 }
328 }
329
330 // Try to move I1 towards I2.
331 {
332 // Look at instructions I in between I1 and (excluding) I2.
333 MachineBasicBlock::iterator I(I1), End(I2);
334 // At O3 we got better results (dhrystone) by being more conservative here.
335 if (!ShouldCombineAggressively)
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000336 End = std::next(MachineBasicBlock::iterator(I2));
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000337 unsigned I1UseReg = UseReg(I1.getOperand(1));
Jyotsna Vermacceafb22013-05-28 19:01:45 +0000338 // Track killed operands. If we move across an instruction that kills our
Jyotsna Verma803e5062013-05-14 18:54:06 +0000339 // operand, we need to update the kill information on the moved I1. It kills
340 // the operand now.
Craig Topper062a2ba2014-04-25 05:30:21 +0000341 MachineInstr *KillingInstr = nullptr;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000342 unsigned KilledOperand = 0;
343
344 while(++I != End) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000345 MachineInstr &MI = *I;
346 // If the intervening instruction MI:
Jyotsna Verma803e5062013-05-14 18:54:06 +0000347 // * modifies I1's use reg
348 // * modifies I1's def reg
349 // * reads I1's def reg
350 // * or has unmodelled side effects
351 // We introduce this special case because llvm has no api to remove a
352 // kill flag for a register (a removeRegisterKilled() analogous to
353 // addRegisterKilled) that handles aliased register correctly.
354 // * or has a killed aliased register use of I1's use reg
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000355 // %D4<def> = A2_tfrpi 16
356 // %R6<def> = A2_tfr %R9
Jyotsna Verma803e5062013-05-14 18:54:06 +0000357 // %R8<def> = KILL %R8, %D4<imp-use,kill>
358 // If we want to move R6 = across the KILL instruction we would have
359 // to remove the %D4<imp-use,kill> operand. For now, we are
360 // conservative and disallow the move.
361 // we can't move I1 across it.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000362 if (MI.isDebugValue()) {
363 if (MI.readsRegister(I1DestReg, TRI)) // Move this instruction after I2.
364 DbgMItoMove.push_back(&MI);
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000365 continue;
366 }
367
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000368 if (isUnsafeToMoveAcross(MI, I1UseReg, I1DestReg, TRI) ||
Jyotsna Verma803e5062013-05-14 18:54:06 +0000369 // Check for an aliased register kill. Bail out if we see one.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000370 (!MI.killsRegister(I1UseReg) && MI.killsRegister(I1UseReg, TRI)))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000371 return false;
372
373 // Check for an exact kill (registers match).
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000374 if (I1UseReg && MI.killsRegister(I1UseReg)) {
Craig Toppere73658d2014-04-28 04:05:08 +0000375 assert(!KillingInstr && "Should only see one killing instruction");
Jyotsna Verma803e5062013-05-14 18:54:06 +0000376 KilledOperand = I1UseReg;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000377 KillingInstr = &MI;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000378 }
379 }
380 if (KillingInstr) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000381 removeKillInfo(*KillingInstr, KilledOperand);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000382 // Update I1 to set the kill flag. This flag will later be picked up by
383 // the new COMBINE instruction.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000384 bool Added = I1.addRegisterKilled(KilledOperand, TRI);
Alp Tokercb402912014-01-24 17:20:08 +0000385 (void)Added; // suppress compiler warning
Jyotsna Verma803e5062013-05-14 18:54:06 +0000386 assert(Added && "Must successfully update kill flag");
387 }
388 DoInsertAtI1 = false;
389 }
390
391 return true;
392}
393
394/// findPotentialNewifiableTFRs - Finds tranfers that feed stores that could be
395/// newified. (A use of a 64 bit register define can not be newified)
396void
397HexagonCopyToCombine::findPotentialNewifiableTFRs(MachineBasicBlock &BB) {
398 DenseMap<unsigned, MachineInstr *> LastDef;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000399 for (MachineInstr &MI : BB) {
400 if (MI.isDebugValue())
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000401 continue;
402
Jyotsna Verma803e5062013-05-14 18:54:06 +0000403 // Mark TFRs that feed a potential new value store as such.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000404 if (TII->mayBeNewStore(MI)) {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000405 // Look for uses of TFR instructions.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000406 for (unsigned OpdIdx = 0, OpdE = MI.getNumOperands(); OpdIdx != OpdE;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000407 ++OpdIdx) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000408 MachineOperand &Op = MI.getOperand(OpdIdx);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000409
410 // Skip over anything except register uses.
411 if (!Op.isReg() || !Op.isUse() || !Op.getReg())
412 continue;
413
414 // Look for the defining instruction.
415 unsigned Reg = Op.getReg();
416 MachineInstr *DefInst = LastDef[Reg];
417 if (!DefInst)
418 continue;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000419 if (!isCombinableInstType(*DefInst, TII, ShouldCombineAggressively))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000420 continue;
421
422 // Only close newifiable stores should influence the decision.
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000423 // Ignore the debug instructions in between.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000424 MachineBasicBlock::iterator It(DefInst);
425 unsigned NumInstsToDef = 0;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000426 while (&*It != &MI) {
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000427 if (!It->isDebugValue())
428 ++NumInstsToDef;
Krzysztof Parzyszek14f9535e2016-01-21 12:45:17 +0000429 ++It;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000430 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000431
432 if (NumInstsToDef > MaxNumOfInstsBetweenNewValueStoreAndTFR)
433 continue;
434
435 PotentiallyNewifiableTFR.insert(DefInst);
436 }
437 // Skip to next instruction.
438 continue;
439 }
440
441 // Put instructions that last defined integer or double registers into the
442 // map.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000443 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
444 MachineOperand &Op = MI.getOperand(I);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000445 if (!Op.isReg() || !Op.isDef() || !Op.getReg())
446 continue;
447 unsigned Reg = Op.getReg();
448 if (Hexagon::DoubleRegsRegClass.contains(Reg)) {
449 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000450 LastDef[*SubRegs] = &MI;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000451 }
452 } else if (Hexagon::IntRegsRegClass.contains(Reg))
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000453 LastDef[Reg] = &MI;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000454 }
455 }
456}
457
458bool HexagonCopyToCombine::runOnMachineFunction(MachineFunction &MF) {
459
460 if (IsCombinesDisabled) return false;
461
462 bool HasChanged = false;
463
464 // Get target info.
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000465 ST = &MF.getSubtarget<HexagonSubtarget>();
466 TRI = ST->getRegisterInfo();
467 TII = ST->getInstrInfo();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000468
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000469 const Function *F = MF.getFunction();
470 bool OptForSize = F->hasFnAttribute(Attribute::OptimizeForSize);
471
Jyotsna Verma803e5062013-05-14 18:54:06 +0000472 // Combine aggressively (for code size)
473 ShouldCombineAggressively =
474 MF.getTarget().getOptLevel() <= CodeGenOpt::Default;
475
476 // Traverse basic blocks.
477 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); BI != BE;
478 ++BI) {
479 PotentiallyNewifiableTFR.clear();
480 findPotentialNewifiableTFRs(*BI);
481
482 // Traverse instructions in basic block.
483 for(MachineBasicBlock::iterator MI = BI->begin(), End = BI->end();
484 MI != End;) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000485 MachineInstr &I1 = *MI++;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000486
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000487 if (I1.isDebugValue())
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000488 continue;
489
Jyotsna Verma803e5062013-05-14 18:54:06 +0000490 // Don't combine a TFR whose user could be newified (instructions that
491 // define double registers can not be newified - Programmer's Ref Manual
492 // 5.4.2 New-value stores).
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000493 if (ShouldCombineAggressively && PotentiallyNewifiableTFR.count(&I1))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000494 continue;
495
496 // Ignore instructions that are not combinable.
497 if (!isCombinableInstType(I1, TII, ShouldCombineAggressively))
498 continue;
499
500 // Find a second instruction that can be merged into a combine
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000501 // instruction. In addition, also find all the debug instructions that
502 // need to be moved along with it.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000503 bool DoInsertAtI1 = false;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000504 DbgMItoMove.clear();
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000505 MachineInstr *I2 = findPairable(I1, DoInsertAtI1, OptForSize);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000506 if (I2) {
507 HasChanged = true;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000508 combine(I1, *I2, MI, DoInsertAtI1, OptForSize);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000509 }
510 }
511 }
512
513 return HasChanged;
514}
515
516/// findPairable - Returns an instruction that can be merged with \p I1 into a
517/// COMBINE instruction or 0 if no such instruction can be found. Returns true
518/// in \p DoInsertAtI1 if the combine must be inserted at instruction \p I1
519/// false if the combine must be inserted at the returned instruction.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000520MachineInstr *HexagonCopyToCombine::findPairable(MachineInstr &I1,
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000521 bool &DoInsertAtI1,
522 bool AllowC64) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000523 MachineBasicBlock::iterator I2 = std::next(MachineBasicBlock::iterator(I1));
Krzysztof Parzyszek6dff3362016-08-24 22:36:35 +0000524 while (I2 != I1.getParent()->end() && I2->isDebugValue())
525 ++I2;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000526
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000527 unsigned I1DestReg = I1.getOperand(0).getReg();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000528
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000529 for (MachineBasicBlock::iterator End = I1.getParent()->end(); I2 != End;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000530 ++I2) {
531 // Bail out early if we see a second definition of I1DestReg.
532 if (I2->modifiesRegister(I1DestReg, TRI))
533 break;
534
535 // Ignore non-combinable instructions.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000536 if (!isCombinableInstType(*I2, TII, ShouldCombineAggressively))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000537 continue;
538
539 // Don't combine a TFR whose user could be newified.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000540 if (ShouldCombineAggressively && PotentiallyNewifiableTFR.count(&*I2))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000541 continue;
542
543 unsigned I2DestReg = I2->getOperand(0).getReg();
544
545 // Check that registers are adjacent and that the first destination register
546 // is even.
547 bool IsI1LowReg = (I2DestReg - I1DestReg) == 1;
548 bool IsI2LowReg = (I1DestReg - I2DestReg) == 1;
549 unsigned FirstRegIndex = IsI1LowReg ? I1DestReg : I2DestReg;
550 if ((!IsI1LowReg && !IsI2LowReg) || !isEvenReg(FirstRegIndex))
551 continue;
552
553 // Check that the two instructions are combinable. V4 allows more
554 // instructions to be merged into a combine.
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000555 // The order matters because in a A2_tfrsi we might can encode a int8 as
556 // the hi reg operand but only a uint6 as the low reg operand.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000557 if ((IsI2LowReg && !areCombinableOperations(TRI, I1, *I2, AllowC64)) ||
558 (IsI1LowReg && !areCombinableOperations(TRI, *I2, I1, AllowC64)))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000559 break;
560
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000561 if (isSafeToMoveTogether(I1, *I2, I1DestReg, I2DestReg, DoInsertAtI1))
562 return &*I2;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000563
564 // Not safe. Stop searching.
565 break;
566 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000567 return nullptr;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000568}
569
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000570void HexagonCopyToCombine::combine(MachineInstr &I1, MachineInstr &I2,
Jyotsna Verma803e5062013-05-14 18:54:06 +0000571 MachineBasicBlock::iterator &MI,
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000572 bool DoInsertAtI1, bool OptForSize) {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000573 // We are going to delete I2. If MI points to I2 advance it to the next
574 // instruction.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000575 if (MI == I2.getIterator())
576 ++MI;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000577
578 // Figure out whether I1 or I2 goes into the lowreg part.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000579 unsigned I1DestReg = I1.getOperand(0).getReg();
580 unsigned I2DestReg = I2.getOperand(0).getReg();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000581 bool IsI1Loreg = (I2DestReg - I1DestReg) == 1;
582 unsigned LoRegDef = IsI1Loreg ? I1DestReg : I2DestReg;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000583 unsigned SubLo;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000584
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000585 const TargetRegisterClass *SuperRC = nullptr;
586 if (Hexagon::IntRegsRegClass.contains(LoRegDef)) {
587 SuperRC = &Hexagon::DoubleRegsRegClass;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000588 SubLo = Hexagon::isub_lo;
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000589 } else if (Hexagon::VectorRegsRegClass.contains(LoRegDef)) {
590 assert(ST->useHVXOps());
591 if (ST->useHVXSglOps())
592 SuperRC = &Hexagon::VecDblRegsRegClass;
593 else
594 SuperRC = &Hexagon::VecDblRegs128BRegClass;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000595 SubLo = Hexagon::vsub_lo;
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000596 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000597 // Get the double word register.
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000598 unsigned DoubleRegDest = TRI->getMatchingSuperReg(LoRegDef, SubLo, SuperRC);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000599 assert(DoubleRegDest != 0 && "Expect a valid register");
600
Jyotsna Verma803e5062013-05-14 18:54:06 +0000601 // Setup source operands.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000602 MachineOperand &LoOperand = IsI1Loreg ? I1.getOperand(1) : I2.getOperand(1);
603 MachineOperand &HiOperand = IsI1Loreg ? I2.getOperand(1) : I1.getOperand(1);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000604
605 // Figure out which source is a register and which a constant.
606 bool IsHiReg = HiOperand.isReg();
607 bool IsLoReg = LoOperand.isReg();
608
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000609 // There is a combine of two constant extended values into CONST64.
610 bool IsC64 = OptForSize && LoOperand.isImm() && HiOperand.isImm() &&
611 isGreaterThanNBitTFRI<16>(I1) && isGreaterThanNBitTFRI<16>(I2);
612
Jyotsna Verma803e5062013-05-14 18:54:06 +0000613 MachineBasicBlock::iterator InsertPt(DoInsertAtI1 ? I1 : I2);
614 // Emit combine.
615 if (IsHiReg && IsLoReg)
616 emitCombineRR(InsertPt, DoubleRegDest, HiOperand, LoOperand);
617 else if (IsHiReg)
618 emitCombineRI(InsertPt, DoubleRegDest, HiOperand, LoOperand);
619 else if (IsLoReg)
620 emitCombineIR(InsertPt, DoubleRegDest, HiOperand, LoOperand);
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000621 else if (IsC64 && !IsConst64Disabled)
622 emitConst64(InsertPt, DoubleRegDest, HiOperand, LoOperand);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000623 else
624 emitCombineII(InsertPt, DoubleRegDest, HiOperand, LoOperand);
625
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000626 // Move debug instructions along with I1 if it's being
627 // moved towards I2.
628 if (!DoInsertAtI1 && DbgMItoMove.size() != 0) {
629 // Insert debug instructions at the new location before I2.
630 MachineBasicBlock *BB = InsertPt->getParent();
631 for (auto NewMI : DbgMItoMove) {
632 // If iterator MI is pointing to DEBUG_VAL, make sure
633 // MI now points to next relevant instruction.
Duncan P. N. Exon Smithf197b1f2016-08-12 05:05:36 +0000634 if (NewMI == MI)
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000635 ++MI;
636 BB->splice(InsertPt, BB, NewMI);
637 }
638 }
639
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000640 I1.eraseFromParent();
641 I2.eraseFromParent();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000642}
643
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000644void HexagonCopyToCombine::emitConst64(MachineBasicBlock::iterator &InsertPt,
645 unsigned DoubleDestReg,
646 MachineOperand &HiOperand,
647 MachineOperand &LoOperand) {
648 DEBUG(dbgs() << "Found a CONST64\n");
649
650 DebugLoc DL = InsertPt->getDebugLoc();
651 MachineBasicBlock *BB = InsertPt->getParent();
652 assert(LoOperand.isImm() && HiOperand.isImm() &&
653 "Both operands must be immediate");
654
655 int64_t V = HiOperand.getImm();
656 V = (V << 32) | (0x0ffffffffLL & LoOperand.getImm());
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +0000657 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::CONST64), DoubleDestReg)
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000658 .addImm(V);
659}
660
Jyotsna Verma803e5062013-05-14 18:54:06 +0000661void HexagonCopyToCombine::emitCombineII(MachineBasicBlock::iterator &InsertPt,
662 unsigned DoubleDestReg,
663 MachineOperand &HiOperand,
664 MachineOperand &LoOperand) {
665 DebugLoc DL = InsertPt->getDebugLoc();
666 MachineBasicBlock *BB = InsertPt->getParent();
667
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000668 // Handle globals.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000669 if (HiOperand.isGlobal()) {
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000670 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000671 .addGlobalAddress(HiOperand.getGlobal(), HiOperand.getOffset(),
672 HiOperand.getTargetFlags())
673 .addImm(LoOperand.getImm());
674 return;
675 }
676 if (LoOperand.isGlobal()) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000677 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000678 .addImm(HiOperand.getImm())
679 .addGlobalAddress(LoOperand.getGlobal(), LoOperand.getOffset(),
680 LoOperand.getTargetFlags());
681 return;
682 }
683
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000684 // Handle block addresses.
685 if (HiOperand.isBlockAddress()) {
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000686 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000687 .addBlockAddress(HiOperand.getBlockAddress(), HiOperand.getOffset(),
688 HiOperand.getTargetFlags())
Jyotsna Verma803e5062013-05-14 18:54:06 +0000689 .addImm(LoOperand.getImm());
690 return;
691 }
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000692 if (LoOperand.isBlockAddress()) {
693 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
694 .addImm(HiOperand.getImm())
695 .addBlockAddress(LoOperand.getBlockAddress(), LoOperand.getOffset(),
696 LoOperand.getTargetFlags());
697 return;
698 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000699
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000700 // Handle jump tables.
701 if (HiOperand.isJTI()) {
702 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
703 .addJumpTableIndex(HiOperand.getIndex(), HiOperand.getTargetFlags())
704 .addImm(LoOperand.getImm());
705 return;
706 }
707 if (LoOperand.isJTI()) {
708 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
709 .addImm(HiOperand.getImm())
710 .addJumpTableIndex(LoOperand.getIndex(), LoOperand.getTargetFlags());
711 return;
712 }
713
714 // Handle constant pools.
715 if (HiOperand.isCPI()) {
716 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
717 .addConstantPoolIndex(HiOperand.getIndex(), HiOperand.getOffset(),
718 HiOperand.getTargetFlags())
719 .addImm(LoOperand.getImm());
720 return;
721 }
722 if (LoOperand.isCPI()) {
723 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
724 .addImm(HiOperand.getImm())
725 .addConstantPoolIndex(LoOperand.getIndex(), LoOperand.getOffset(),
726 LoOperand.getTargetFlags());
727 return;
728 }
729
730 // First preference should be given to Hexagon::A2_combineii instruction
731 // as it can include U6 (in Hexagon::A4_combineii) as well.
732 // In this instruction, HiOperand is const extended, if required.
733 if (isInt<8>(LoOperand.getImm())) {
734 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
735 .addImm(HiOperand.getImm())
736 .addImm(LoOperand.getImm());
737 return;
738 }
739
740 // In this instruction, LoOperand is const extended, if required.
741 if (isInt<8>(HiOperand.getImm())) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000742 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000743 .addImm(HiOperand.getImm())
744 .addImm(LoOperand.getImm());
745 return;
746 }
747
748 // Insert new combine instruction.
749 // DoubleRegDest = combine #HiImm, #LoImm
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000750 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000751 .addImm(HiOperand.getImm())
752 .addImm(LoOperand.getImm());
753}
754
755void HexagonCopyToCombine::emitCombineIR(MachineBasicBlock::iterator &InsertPt,
756 unsigned DoubleDestReg,
757 MachineOperand &HiOperand,
758 MachineOperand &LoOperand) {
759 unsigned LoReg = LoOperand.getReg();
760 unsigned LoRegKillFlag = getKillRegState(LoOperand.isKill());
761
762 DebugLoc DL = InsertPt->getDebugLoc();
763 MachineBasicBlock *BB = InsertPt->getParent();
764
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000765 // Handle globals.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000766 if (HiOperand.isGlobal()) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000767 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000768 .addGlobalAddress(HiOperand.getGlobal(), HiOperand.getOffset(),
769 HiOperand.getTargetFlags())
770 .addReg(LoReg, LoRegKillFlag);
771 return;
772 }
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000773 // Handle block addresses.
774 if (HiOperand.isBlockAddress()) {
775 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
776 .addBlockAddress(HiOperand.getBlockAddress(), HiOperand.getOffset(),
777 HiOperand.getTargetFlags())
778 .addReg(LoReg, LoRegKillFlag);
779 return;
780 }
781 // Handle jump tables.
782 if (HiOperand.isJTI()) {
783 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
784 .addJumpTableIndex(HiOperand.getIndex(), HiOperand.getTargetFlags())
785 .addReg(LoReg, LoRegKillFlag);
786 return;
787 }
788 // Handle constant pools.
789 if (HiOperand.isCPI()) {
790 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
791 .addConstantPoolIndex(HiOperand.getIndex(), HiOperand.getOffset(),
792 HiOperand.getTargetFlags())
793 .addReg(LoReg, LoRegKillFlag);
794 return;
795 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000796 // Insert new combine instruction.
797 // DoubleRegDest = combine #HiImm, LoReg
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000798 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000799 .addImm(HiOperand.getImm())
800 .addReg(LoReg, LoRegKillFlag);
801}
802
803void HexagonCopyToCombine::emitCombineRI(MachineBasicBlock::iterator &InsertPt,
804 unsigned DoubleDestReg,
805 MachineOperand &HiOperand,
806 MachineOperand &LoOperand) {
807 unsigned HiRegKillFlag = getKillRegState(HiOperand.isKill());
808 unsigned HiReg = HiOperand.getReg();
809
810 DebugLoc DL = InsertPt->getDebugLoc();
811 MachineBasicBlock *BB = InsertPt->getParent();
812
813 // Handle global.
814 if (LoOperand.isGlobal()) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000815 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000816 .addReg(HiReg, HiRegKillFlag)
817 .addGlobalAddress(LoOperand.getGlobal(), LoOperand.getOffset(),
818 LoOperand.getTargetFlags());
819 return;
820 }
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000821 // Handle block addresses.
822 if (LoOperand.isBlockAddress()) {
823 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
824 .addReg(HiReg, HiRegKillFlag)
825 .addBlockAddress(LoOperand.getBlockAddress(), LoOperand.getOffset(),
826 LoOperand.getTargetFlags());
827 return;
828 }
829 // Handle jump tables.
830 if (LoOperand.isJTI()) {
831 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
832 .addReg(HiOperand.getReg(), HiRegKillFlag)
833 .addJumpTableIndex(LoOperand.getIndex(), LoOperand.getTargetFlags());
834 return;
835 }
836 // Handle constant pools.
837 if (LoOperand.isCPI()) {
838 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
839 .addReg(HiOperand.getReg(), HiRegKillFlag)
840 .addConstantPoolIndex(LoOperand.getIndex(), LoOperand.getOffset(),
841 LoOperand.getTargetFlags());
842 return;
843 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000844
845 // Insert new combine instruction.
846 // DoubleRegDest = combine HiReg, #LoImm
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000847 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000848 .addReg(HiReg, HiRegKillFlag)
849 .addImm(LoOperand.getImm());
850}
851
852void HexagonCopyToCombine::emitCombineRR(MachineBasicBlock::iterator &InsertPt,
853 unsigned DoubleDestReg,
854 MachineOperand &HiOperand,
855 MachineOperand &LoOperand) {
856 unsigned LoRegKillFlag = getKillRegState(LoOperand.isKill());
857 unsigned HiRegKillFlag = getKillRegState(HiOperand.isKill());
858 unsigned LoReg = LoOperand.getReg();
859 unsigned HiReg = HiOperand.getReg();
860
861 DebugLoc DL = InsertPt->getDebugLoc();
862 MachineBasicBlock *BB = InsertPt->getParent();
863
864 // Insert new combine instruction.
865 // DoubleRegDest = combine HiReg, LoReg
Krzysztof Parzyszekb1b03722016-08-18 14:12:34 +0000866 unsigned NewOpc;
867 if (Hexagon::DoubleRegsRegClass.contains(DoubleDestReg)) {
868 NewOpc = Hexagon::A2_combinew;
869 } else if (Hexagon::VecDblRegsRegClass.contains(DoubleDestReg)) {
870 assert(ST->useHVXOps());
871 if (ST->useHVXSglOps())
872 NewOpc = Hexagon::V6_vcombine;
873 else
874 NewOpc = Hexagon::V6_vcombine_128B;
875 } 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}