blob: face0f3f64b4cde6d8affae4d4bd564d6d77c45f [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//===----------------------------------------------------------------------===//
Jyotsna Verma803e5062013-05-14 18:54:06 +000014#include "llvm/PassSupport.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000015#include "Hexagon.h"
16#include "HexagonInstrInfo.h"
17#include "HexagonMachineFunctionInfo.h"
18#include "HexagonRegisterInfo.h"
19#include "HexagonSubtarget.h"
20#include "HexagonTargetMachine.h"
Jyotsna Verma803e5062013-05-14 18:54:06 +000021#include "llvm/ADT/DenseMap.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000022#include "llvm/ADT/DenseSet.h"
Jyotsna Verma803e5062013-05-14 18:54:06 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineInstr.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000028#include "llvm/CodeGen/Passes.h"
Jyotsna Verma803e5062013-05-14 18:54:06 +000029#include "llvm/Support/CodeGen.h"
30#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/raw_ostream.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000033#include "llvm/Target/TargetRegisterInfo.h"
Jyotsna Verma803e5062013-05-14 18:54:06 +000034
35using namespace llvm;
36
Chandler Carruth84e68b22014-04-22 02:41:26 +000037#define DEBUG_TYPE "hexagon-copy-combine"
38
Jyotsna Verma803e5062013-05-14 18:54:06 +000039static
40cl::opt<bool> IsCombinesDisabled("disable-merge-into-combines",
41 cl::Hidden, cl::ZeroOrMore,
42 cl::init(false),
43 cl::desc("Disable merging into combines"));
44static
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +000045cl::opt<bool> IsConst64Disabled("disable-const64",
46 cl::Hidden, cl::ZeroOrMore,
47 cl::init(false),
48 cl::desc("Disable generation of const64"));
49static
Jyotsna Verma803e5062013-05-14 18:54:06 +000050cl::opt<unsigned>
51MaxNumOfInstsBetweenNewValueStoreAndTFR("max-num-inst-between-tfr-and-nv-store",
52 cl::Hidden, cl::init(4),
53 cl::desc("Maximum distance between a tfr feeding a store we "
54 "consider the store still to be newifiable"));
55
56namespace llvm {
Colin LeMahieu56efafc2015-06-15 19:05:35 +000057 FunctionPass *createHexagonCopyToCombine();
Jyotsna Verma803e5062013-05-14 18:54:06 +000058 void initializeHexagonCopyToCombinePass(PassRegistry&);
59}
60
61
62namespace {
63
64class HexagonCopyToCombine : public MachineFunctionPass {
65 const HexagonInstrInfo *TII;
66 const TargetRegisterInfo *TRI;
67 bool ShouldCombineAggressively;
68
69 DenseSet<MachineInstr *> PotentiallyNewifiableTFR;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +000070 SmallVector<MachineInstr *, 8> DbgMItoMove;
71
Jyotsna Verma803e5062013-05-14 18:54:06 +000072public:
73 static char ID;
74
75 HexagonCopyToCombine() : MachineFunctionPass(ID) {
76 initializeHexagonCopyToCombinePass(*PassRegistry::getPassRegistry());
77 }
78
Craig Topper906c2cd2014-04-29 07:58:16 +000079 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jyotsna Verma803e5062013-05-14 18:54:06 +000080 MachineFunctionPass::getAnalysisUsage(AU);
81 }
82
Craig Topper906c2cd2014-04-29 07:58:16 +000083 const char *getPassName() const override {
Jyotsna Verma803e5062013-05-14 18:54:06 +000084 return "Hexagon Copy-To-Combine Pass";
85 }
86
Craig Topper906c2cd2014-04-29 07:58:16 +000087 bool runOnMachineFunction(MachineFunction &Fn) override;
Jyotsna Verma803e5062013-05-14 18:54:06 +000088
Derek Schuff1dbf7a52016-04-04 17:09:25 +000089 MachineFunctionProperties getRequiredProperties() const override {
90 return MachineFunctionProperties().set(
91 MachineFunctionProperties::Property::AllVRegsAllocated);
92 }
93
Jyotsna Verma803e5062013-05-14 18:54:06 +000094private:
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000095 MachineInstr *findPairable(MachineInstr &I1, bool &DoInsertAtI1,
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +000096 bool AllowC64);
Jyotsna Verma803e5062013-05-14 18:54:06 +000097
98 void findPotentialNewifiableTFRs(MachineBasicBlock &);
99
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000100 void combine(MachineInstr &I1, MachineInstr &I2,
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000101 MachineBasicBlock::iterator &MI, bool DoInsertAtI1,
102 bool OptForSize);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000103
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000104 bool isSafeToMoveTogether(MachineInstr &I1, MachineInstr &I2,
Jyotsna Verma803e5062013-05-14 18:54:06 +0000105 unsigned I1DestReg, unsigned I2DestReg,
106 bool &DoInsertAtI1);
107
108 void emitCombineRR(MachineBasicBlock::iterator &Before, unsigned DestReg,
109 MachineOperand &HiOperand, MachineOperand &LoOperand);
110
111 void emitCombineRI(MachineBasicBlock::iterator &Before, unsigned DestReg,
112 MachineOperand &HiOperand, MachineOperand &LoOperand);
113
114 void emitCombineIR(MachineBasicBlock::iterator &Before, unsigned DestReg,
115 MachineOperand &HiOperand, MachineOperand &LoOperand);
116
117 void emitCombineII(MachineBasicBlock::iterator &Before, unsigned DestReg,
118 MachineOperand &HiOperand, MachineOperand &LoOperand);
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000119
120 void emitConst64(MachineBasicBlock::iterator &Before, unsigned DestReg,
121 MachineOperand &HiOperand, MachineOperand &LoOperand);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000122};
123
124} // End anonymous namespace.
125
126char HexagonCopyToCombine::ID = 0;
127
128INITIALIZE_PASS(HexagonCopyToCombine, "hexagon-copy-combine",
129 "Hexagon Copy-To-Combine Pass", false, false)
130
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000131static bool isCombinableInstType(MachineInstr &MI, const HexagonInstrInfo *TII,
Jyotsna Verma803e5062013-05-14 18:54:06 +0000132 bool ShouldCombineAggressively) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000133 switch (MI.getOpcode()) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000134 case Hexagon::A2_tfr: {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000135 // A COPY instruction can be combined if its arguments are IntRegs (32bit).
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000136 const MachineOperand &Op0 = MI.getOperand(0);
137 const MachineOperand &Op1 = MI.getOperand(1);
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000138 assert(Op0.isReg() && Op1.isReg());
Jyotsna Verma803e5062013-05-14 18:54:06 +0000139
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000140 unsigned DestReg = Op0.getReg();
141 unsigned SrcReg = Op1.getReg();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000142 return Hexagon::IntRegsRegClass.contains(DestReg) &&
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000143 Hexagon::IntRegsRegClass.contains(SrcReg);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000144 }
145
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000146 case Hexagon::A2_tfrsi: {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000147 // A transfer-immediate can be combined if its argument is a signed 8bit
148 // value.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000149 const MachineOperand &Op0 = MI.getOperand(0);
150 const MachineOperand &Op1 = MI.getOperand(1);
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000151 assert(Op0.isReg());
Jyotsna Verma803e5062013-05-14 18:54:06 +0000152
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000153 unsigned DestReg = Op0.getReg();
154 // Ensure that TargetFlags are MO_NO_FLAG for a global. This is a
155 // workaround for an ABI bug that prevents GOT relocations on combine
156 // instructions
157 if (!Op1.isImm() && Op1.getTargetFlags() != HexagonII::MO_NO_FLAG)
158 return false;
159
160 // Only combine constant extended A2_tfrsi if we are in aggressive mode.
161 bool NotExt = Op1.isImm() && isInt<8>(Op1.getImm());
Jyotsna Verma803e5062013-05-14 18:54:06 +0000162 return Hexagon::IntRegsRegClass.contains(DestReg) &&
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000163 (ShouldCombineAggressively || NotExt);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000164 }
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 Parzyszeke5689672015-04-23 20:26:21 +0000189 (void)HiOpc; // Fix compiler warning
190 (void)LoOpc; // Fix compiler warning
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000191 assert((HiOpc == Hexagon::A2_tfr || HiOpc == Hexagon::A2_tfrsi) &&
192 (LoOpc == Hexagon::A2_tfr || LoOpc == Hexagon::A2_tfrsi) &&
Jyotsna Verma803e5062013-05-14 18:54:06 +0000193 "Assume individual instructions are of a combinable type");
194
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000195 if (!AllowC64) {
196 // There is no combine of two constant extended values.
197 if (isGreaterThanNBitTFRI<8>(HighRegInst) &&
198 isGreaterThanNBitTFRI<6>(LowRegInst))
199 return false;
200 }
201
202 // There is a combine of two constant extended values into CONST64,
203 // provided both constants are true immediates.
204 if (isGreaterThanNBitTFRI<16>(HighRegInst) &&
205 isGreaterThanNBitTFRI<16>(LowRegInst))
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000206 return (HighRegInst.getOperand(1).isImm() &&
207 LowRegInst.getOperand(1).isImm());
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000208
209 // There is no combine of two constant extended values, unless handled above
210 // Make both 8-bit size checks to allow both combine (#,##) and combine(##,#)
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000211 if (isGreaterThanNBitTFRI<8>(HighRegInst) &&
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000212 isGreaterThanNBitTFRI<8>(LowRegInst))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000213 return false;
214
215 return true;
216}
217
218static bool isEvenReg(unsigned Reg) {
219 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
220 Hexagon::IntRegsRegClass.contains(Reg));
221 return (Reg - Hexagon::R0) % 2 == 0;
222}
223
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000224static void removeKillInfo(MachineInstr &MI, unsigned RegNotKilled) {
225 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
226 MachineOperand &Op = MI.getOperand(I);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000227 if (!Op.isReg() || Op.getReg() != RegNotKilled || !Op.isKill())
228 continue;
229 Op.setIsKill(false);
230 }
231}
232
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000233/// Returns true if it is unsafe to move a copy instruction from \p UseReg to
234/// \p DestReg over the instruction \p MI.
235static bool isUnsafeToMoveAcross(MachineInstr &MI, unsigned UseReg,
236 unsigned DestReg,
237 const TargetRegisterInfo *TRI) {
238 return (UseReg && (MI.modifiesRegister(UseReg, TRI))) ||
239 MI.modifiesRegister(DestReg, TRI) || MI.readsRegister(DestReg, TRI) ||
240 MI.hasUnmodeledSideEffects() || MI.isInlineAsm() || MI.isDebugValue();
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000241}
242
243static unsigned UseReg(const MachineOperand& MO) {
244 return MO.isReg() ? MO.getReg() : 0;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000245}
246
247/// isSafeToMoveTogether - Returns true if it is safe to move I1 next to I2 such
248/// that the two instructions can be paired in a combine.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000249bool HexagonCopyToCombine::isSafeToMoveTogether(MachineInstr &I1,
250 MachineInstr &I2,
Jyotsna Verma803e5062013-05-14 18:54:06 +0000251 unsigned I1DestReg,
252 unsigned I2DestReg,
253 bool &DoInsertAtI1) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000254 unsigned I2UseReg = UseReg(I2.getOperand(1));
Jyotsna Verma803e5062013-05-14 18:54:06 +0000255
256 // It is not safe to move I1 and I2 into one combine if I2 has a true
257 // dependence on I1.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000258 if (I2UseReg && I1.modifiesRegister(I2UseReg, TRI))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000259 return false;
260
261 bool isSafe = true;
262
263 // First try to move I2 towards I1.
264 {
265 // A reverse_iterator instantiated like below starts before I2, and I1
266 // respectively.
267 // Look at instructions I in between I2 and (excluding) I1.
268 MachineBasicBlock::reverse_iterator I(I2),
269 End = --(MachineBasicBlock::reverse_iterator(I1));
270 // At 03 we got better results (dhrystone!) by being more conservative.
271 if (!ShouldCombineAggressively)
272 End = MachineBasicBlock::reverse_iterator(I1);
273 // If I2 kills its operand and we move I2 over an instruction that also
274 // uses I2's use reg we need to modify that (first) instruction to now kill
275 // this reg.
276 unsigned KilledOperand = 0;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000277 if (I2.killsRegister(I2UseReg))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000278 KilledOperand = I2UseReg;
Craig Topper062a2ba2014-04-25 05:30:21 +0000279 MachineInstr *KillingInstr = nullptr;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000280
281 for (; I != End; ++I) {
282 // If the intervening instruction I:
283 // * modifies I2's use reg
284 // * modifies I2's def reg
285 // * reads I2's def reg
286 // * or has unmodelled side effects
287 // we can't move I2 across it.
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000288 if (I->isDebugValue())
289 continue;
290
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000291 if (isUnsafeToMoveAcross(*I, I2UseReg, I2DestReg, TRI)) {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000292 isSafe = false;
293 break;
294 }
295
296 // Update first use of the killed operand.
297 if (!KillingInstr && KilledOperand &&
298 I->readsRegister(KilledOperand, TRI))
299 KillingInstr = &*I;
300 }
301 if (isSafe) {
302 // Update the intermediate instruction to with the kill flag.
303 if (KillingInstr) {
304 bool Added = KillingInstr->addRegisterKilled(KilledOperand, TRI, true);
Alp Tokercb402912014-01-24 17:20:08 +0000305 (void)Added; // suppress compiler warning
Jyotsna Verma803e5062013-05-14 18:54:06 +0000306 assert(Added && "Must successfully update kill flag");
307 removeKillInfo(I2, KilledOperand);
308 }
309 DoInsertAtI1 = true;
310 return true;
311 }
312 }
313
314 // Try to move I1 towards I2.
315 {
316 // Look at instructions I in between I1 and (excluding) I2.
317 MachineBasicBlock::iterator I(I1), End(I2);
318 // At O3 we got better results (dhrystone) by being more conservative here.
319 if (!ShouldCombineAggressively)
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000320 End = std::next(MachineBasicBlock::iterator(I2));
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000321 unsigned I1UseReg = UseReg(I1.getOperand(1));
Jyotsna Vermacceafb22013-05-28 19:01:45 +0000322 // Track killed operands. If we move across an instruction that kills our
Jyotsna Verma803e5062013-05-14 18:54:06 +0000323 // operand, we need to update the kill information on the moved I1. It kills
324 // the operand now.
Craig Topper062a2ba2014-04-25 05:30:21 +0000325 MachineInstr *KillingInstr = nullptr;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000326 unsigned KilledOperand = 0;
327
328 while(++I != End) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000329 MachineInstr &MI = *I;
330 // If the intervening instruction MI:
Jyotsna Verma803e5062013-05-14 18:54:06 +0000331 // * modifies I1's use reg
332 // * modifies I1's def reg
333 // * reads I1's def reg
334 // * or has unmodelled side effects
335 // We introduce this special case because llvm has no api to remove a
336 // kill flag for a register (a removeRegisterKilled() analogous to
337 // addRegisterKilled) that handles aliased register correctly.
338 // * or has a killed aliased register use of I1's use reg
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000339 // %D4<def> = A2_tfrpi 16
340 // %R6<def> = A2_tfr %R9
Jyotsna Verma803e5062013-05-14 18:54:06 +0000341 // %R8<def> = KILL %R8, %D4<imp-use,kill>
342 // If we want to move R6 = across the KILL instruction we would have
343 // to remove the %D4<imp-use,kill> operand. For now, we are
344 // conservative and disallow the move.
345 // we can't move I1 across it.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000346 if (MI.isDebugValue()) {
347 if (MI.readsRegister(I1DestReg, TRI)) // Move this instruction after I2.
348 DbgMItoMove.push_back(&MI);
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000349 continue;
350 }
351
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000352 if (isUnsafeToMoveAcross(MI, I1UseReg, I1DestReg, TRI) ||
Jyotsna Verma803e5062013-05-14 18:54:06 +0000353 // Check for an aliased register kill. Bail out if we see one.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000354 (!MI.killsRegister(I1UseReg) && MI.killsRegister(I1UseReg, TRI)))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000355 return false;
356
357 // Check for an exact kill (registers match).
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000358 if (I1UseReg && MI.killsRegister(I1UseReg)) {
Craig Toppere73658d2014-04-28 04:05:08 +0000359 assert(!KillingInstr && "Should only see one killing instruction");
Jyotsna Verma803e5062013-05-14 18:54:06 +0000360 KilledOperand = I1UseReg;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000361 KillingInstr = &MI;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000362 }
363 }
364 if (KillingInstr) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000365 removeKillInfo(*KillingInstr, KilledOperand);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000366 // Update I1 to set the kill flag. This flag will later be picked up by
367 // the new COMBINE instruction.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000368 bool Added = I1.addRegisterKilled(KilledOperand, TRI);
Alp Tokercb402912014-01-24 17:20:08 +0000369 (void)Added; // suppress compiler warning
Jyotsna Verma803e5062013-05-14 18:54:06 +0000370 assert(Added && "Must successfully update kill flag");
371 }
372 DoInsertAtI1 = false;
373 }
374
375 return true;
376}
377
378/// findPotentialNewifiableTFRs - Finds tranfers that feed stores that could be
379/// newified. (A use of a 64 bit register define can not be newified)
380void
381HexagonCopyToCombine::findPotentialNewifiableTFRs(MachineBasicBlock &BB) {
382 DenseMap<unsigned, MachineInstr *> LastDef;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000383 for (MachineInstr &MI : BB) {
384 if (MI.isDebugValue())
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000385 continue;
386
Jyotsna Verma803e5062013-05-14 18:54:06 +0000387 // Mark TFRs that feed a potential new value store as such.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000388 if (TII->mayBeNewStore(&MI)) {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000389 // Look for uses of TFR instructions.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000390 for (unsigned OpdIdx = 0, OpdE = MI.getNumOperands(); OpdIdx != OpdE;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000391 ++OpdIdx) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000392 MachineOperand &Op = MI.getOperand(OpdIdx);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000393
394 // Skip over anything except register uses.
395 if (!Op.isReg() || !Op.isUse() || !Op.getReg())
396 continue;
397
398 // Look for the defining instruction.
399 unsigned Reg = Op.getReg();
400 MachineInstr *DefInst = LastDef[Reg];
401 if (!DefInst)
402 continue;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000403 if (!isCombinableInstType(*DefInst, TII, ShouldCombineAggressively))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000404 continue;
405
406 // Only close newifiable stores should influence the decision.
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000407 // Ignore the debug instructions in between.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000408 MachineBasicBlock::iterator It(DefInst);
409 unsigned NumInstsToDef = 0;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000410 while (&*It != &MI) {
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000411 if (!It->isDebugValue())
412 ++NumInstsToDef;
Krzysztof Parzyszek14f9535e2016-01-21 12:45:17 +0000413 ++It;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000414 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000415
416 if (NumInstsToDef > MaxNumOfInstsBetweenNewValueStoreAndTFR)
417 continue;
418
419 PotentiallyNewifiableTFR.insert(DefInst);
420 }
421 // Skip to next instruction.
422 continue;
423 }
424
425 // Put instructions that last defined integer or double registers into the
426 // map.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000427 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
428 MachineOperand &Op = MI.getOperand(I);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000429 if (!Op.isReg() || !Op.isDef() || !Op.getReg())
430 continue;
431 unsigned Reg = Op.getReg();
432 if (Hexagon::DoubleRegsRegClass.contains(Reg)) {
433 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000434 LastDef[*SubRegs] = &MI;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000435 }
436 } else if (Hexagon::IntRegsRegClass.contains(Reg))
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000437 LastDef[Reg] = &MI;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000438 }
439 }
440}
441
442bool HexagonCopyToCombine::runOnMachineFunction(MachineFunction &MF) {
443
444 if (IsCombinesDisabled) return false;
445
446 bool HasChanged = false;
447
448 // Get target info.
Eric Christopherfc6de422014-08-05 02:39:49 +0000449 TRI = MF.getSubtarget().getRegisterInfo();
Eric Christopher12a5c0d2015-02-02 18:46:29 +0000450 TII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000451
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000452 const Function *F = MF.getFunction();
453 bool OptForSize = F->hasFnAttribute(Attribute::OptimizeForSize);
454
Jyotsna Verma803e5062013-05-14 18:54:06 +0000455 // Combine aggressively (for code size)
456 ShouldCombineAggressively =
457 MF.getTarget().getOptLevel() <= CodeGenOpt::Default;
458
459 // Traverse basic blocks.
460 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); BI != BE;
461 ++BI) {
462 PotentiallyNewifiableTFR.clear();
463 findPotentialNewifiableTFRs(*BI);
464
465 // Traverse instructions in basic block.
466 for(MachineBasicBlock::iterator MI = BI->begin(), End = BI->end();
467 MI != End;) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000468 MachineInstr &I1 = *MI++;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000469
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000470 if (I1.isDebugValue())
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000471 continue;
472
Jyotsna Verma803e5062013-05-14 18:54:06 +0000473 // Don't combine a TFR whose user could be newified (instructions that
474 // define double registers can not be newified - Programmer's Ref Manual
475 // 5.4.2 New-value stores).
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000476 if (ShouldCombineAggressively && PotentiallyNewifiableTFR.count(&I1))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000477 continue;
478
479 // Ignore instructions that are not combinable.
480 if (!isCombinableInstType(I1, TII, ShouldCombineAggressively))
481 continue;
482
483 // Find a second instruction that can be merged into a combine
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000484 // instruction. In addition, also find all the debug instructions that
485 // need to be moved along with it.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000486 bool DoInsertAtI1 = false;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000487 DbgMItoMove.clear();
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000488 MachineInstr *I2 = findPairable(I1, DoInsertAtI1, OptForSize);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000489 if (I2) {
490 HasChanged = true;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000491 combine(I1, *I2, MI, DoInsertAtI1, OptForSize);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000492 }
493 }
494 }
495
496 return HasChanged;
497}
498
499/// findPairable - Returns an instruction that can be merged with \p I1 into a
500/// COMBINE instruction or 0 if no such instruction can be found. Returns true
501/// in \p DoInsertAtI1 if the combine must be inserted at instruction \p I1
502/// false if the combine must be inserted at the returned instruction.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000503MachineInstr *HexagonCopyToCombine::findPairable(MachineInstr &I1,
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000504 bool &DoInsertAtI1,
505 bool AllowC64) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000506 MachineBasicBlock::iterator I2 = std::next(MachineBasicBlock::iterator(I1));
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000507
508 while (I2->isDebugValue())
509 ++I2;
510
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000511 unsigned I1DestReg = I1.getOperand(0).getReg();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000512
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000513 for (MachineBasicBlock::iterator End = I1.getParent()->end(); I2 != End;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000514 ++I2) {
515 // Bail out early if we see a second definition of I1DestReg.
516 if (I2->modifiesRegister(I1DestReg, TRI))
517 break;
518
519 // Ignore non-combinable instructions.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000520 if (!isCombinableInstType(*I2, TII, ShouldCombineAggressively))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000521 continue;
522
523 // Don't combine a TFR whose user could be newified.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000524 if (ShouldCombineAggressively && PotentiallyNewifiableTFR.count(&*I2))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000525 continue;
526
527 unsigned I2DestReg = I2->getOperand(0).getReg();
528
529 // Check that registers are adjacent and that the first destination register
530 // is even.
531 bool IsI1LowReg = (I2DestReg - I1DestReg) == 1;
532 bool IsI2LowReg = (I1DestReg - I2DestReg) == 1;
533 unsigned FirstRegIndex = IsI1LowReg ? I1DestReg : I2DestReg;
534 if ((!IsI1LowReg && !IsI2LowReg) || !isEvenReg(FirstRegIndex))
535 continue;
536
537 // Check that the two instructions are combinable. V4 allows more
538 // instructions to be merged into a combine.
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000539 // The order matters because in a A2_tfrsi we might can encode a int8 as
540 // the hi reg operand but only a uint6 as the low reg operand.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000541 if ((IsI2LowReg && !areCombinableOperations(TRI, I1, *I2, AllowC64)) ||
542 (IsI1LowReg && !areCombinableOperations(TRI, *I2, I1, AllowC64)))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000543 break;
544
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000545 if (isSafeToMoveTogether(I1, *I2, I1DestReg, I2DestReg, DoInsertAtI1))
546 return &*I2;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000547
548 // Not safe. Stop searching.
549 break;
550 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000551 return nullptr;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000552}
553
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000554void HexagonCopyToCombine::combine(MachineInstr &I1, MachineInstr &I2,
Jyotsna Verma803e5062013-05-14 18:54:06 +0000555 MachineBasicBlock::iterator &MI,
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000556 bool DoInsertAtI1, bool OptForSize) {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000557 // We are going to delete I2. If MI points to I2 advance it to the next
558 // instruction.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000559 if (MI == I2.getIterator())
560 ++MI;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000561
562 // Figure out whether I1 or I2 goes into the lowreg part.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000563 unsigned I1DestReg = I1.getOperand(0).getReg();
564 unsigned I2DestReg = I2.getOperand(0).getReg();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000565 bool IsI1Loreg = (I2DestReg - I1DestReg) == 1;
566 unsigned LoRegDef = IsI1Loreg ? I1DestReg : I2DestReg;
567
568 // Get the double word register.
569 unsigned DoubleRegDest =
570 TRI->getMatchingSuperReg(LoRegDef, Hexagon::subreg_loreg,
571 &Hexagon::DoubleRegsRegClass);
572 assert(DoubleRegDest != 0 && "Expect a valid register");
573
574
575 // Setup source operands.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000576 MachineOperand &LoOperand = IsI1Loreg ? I1.getOperand(1) : I2.getOperand(1);
577 MachineOperand &HiOperand = IsI1Loreg ? I2.getOperand(1) : I1.getOperand(1);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000578
579 // Figure out which source is a register and which a constant.
580 bool IsHiReg = HiOperand.isReg();
581 bool IsLoReg = LoOperand.isReg();
582
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000583 // There is a combine of two constant extended values into CONST64.
584 bool IsC64 = OptForSize && LoOperand.isImm() && HiOperand.isImm() &&
585 isGreaterThanNBitTFRI<16>(I1) && isGreaterThanNBitTFRI<16>(I2);
586
Jyotsna Verma803e5062013-05-14 18:54:06 +0000587 MachineBasicBlock::iterator InsertPt(DoInsertAtI1 ? I1 : I2);
588 // Emit combine.
589 if (IsHiReg && IsLoReg)
590 emitCombineRR(InsertPt, DoubleRegDest, HiOperand, LoOperand);
591 else if (IsHiReg)
592 emitCombineRI(InsertPt, DoubleRegDest, HiOperand, LoOperand);
593 else if (IsLoReg)
594 emitCombineIR(InsertPt, DoubleRegDest, HiOperand, LoOperand);
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000595 else if (IsC64 && !IsConst64Disabled)
596 emitConst64(InsertPt, DoubleRegDest, HiOperand, LoOperand);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000597 else
598 emitCombineII(InsertPt, DoubleRegDest, HiOperand, LoOperand);
599
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000600 // Move debug instructions along with I1 if it's being
601 // moved towards I2.
602 if (!DoInsertAtI1 && DbgMItoMove.size() != 0) {
603 // Insert debug instructions at the new location before I2.
604 MachineBasicBlock *BB = InsertPt->getParent();
605 for (auto NewMI : DbgMItoMove) {
606 // If iterator MI is pointing to DEBUG_VAL, make sure
607 // MI now points to next relevant instruction.
608 if (NewMI == (MachineInstr*)MI)
609 ++MI;
610 BB->splice(InsertPt, BB, NewMI);
611 }
612 }
613
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000614 I1.eraseFromParent();
615 I2.eraseFromParent();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000616}
617
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000618void HexagonCopyToCombine::emitConst64(MachineBasicBlock::iterator &InsertPt,
619 unsigned DoubleDestReg,
620 MachineOperand &HiOperand,
621 MachineOperand &LoOperand) {
622 DEBUG(dbgs() << "Found a CONST64\n");
623
624 DebugLoc DL = InsertPt->getDebugLoc();
625 MachineBasicBlock *BB = InsertPt->getParent();
626 assert(LoOperand.isImm() && HiOperand.isImm() &&
627 "Both operands must be immediate");
628
629 int64_t V = HiOperand.getImm();
630 V = (V << 32) | (0x0ffffffffLL & LoOperand.getImm());
631 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::CONST64_Int_Real),
632 DoubleDestReg)
633 .addImm(V);
634}
635
Jyotsna Verma803e5062013-05-14 18:54:06 +0000636void HexagonCopyToCombine::emitCombineII(MachineBasicBlock::iterator &InsertPt,
637 unsigned DoubleDestReg,
638 MachineOperand &HiOperand,
639 MachineOperand &LoOperand) {
640 DebugLoc DL = InsertPt->getDebugLoc();
641 MachineBasicBlock *BB = InsertPt->getParent();
642
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000643 // Handle globals.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000644 if (HiOperand.isGlobal()) {
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000645 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000646 .addGlobalAddress(HiOperand.getGlobal(), HiOperand.getOffset(),
647 HiOperand.getTargetFlags())
648 .addImm(LoOperand.getImm());
649 return;
650 }
651 if (LoOperand.isGlobal()) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000652 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000653 .addImm(HiOperand.getImm())
654 .addGlobalAddress(LoOperand.getGlobal(), LoOperand.getOffset(),
655 LoOperand.getTargetFlags());
656 return;
657 }
658
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000659 // Handle block addresses.
660 if (HiOperand.isBlockAddress()) {
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000661 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000662 .addBlockAddress(HiOperand.getBlockAddress(), HiOperand.getOffset(),
663 HiOperand.getTargetFlags())
Jyotsna Verma803e5062013-05-14 18:54:06 +0000664 .addImm(LoOperand.getImm());
665 return;
666 }
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000667 if (LoOperand.isBlockAddress()) {
668 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
669 .addImm(HiOperand.getImm())
670 .addBlockAddress(LoOperand.getBlockAddress(), LoOperand.getOffset(),
671 LoOperand.getTargetFlags());
672 return;
673 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000674
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000675 // Handle jump tables.
676 if (HiOperand.isJTI()) {
677 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
678 .addJumpTableIndex(HiOperand.getIndex(), HiOperand.getTargetFlags())
679 .addImm(LoOperand.getImm());
680 return;
681 }
682 if (LoOperand.isJTI()) {
683 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
684 .addImm(HiOperand.getImm())
685 .addJumpTableIndex(LoOperand.getIndex(), LoOperand.getTargetFlags());
686 return;
687 }
688
689 // Handle constant pools.
690 if (HiOperand.isCPI()) {
691 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
692 .addConstantPoolIndex(HiOperand.getIndex(), HiOperand.getOffset(),
693 HiOperand.getTargetFlags())
694 .addImm(LoOperand.getImm());
695 return;
696 }
697 if (LoOperand.isCPI()) {
698 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
699 .addImm(HiOperand.getImm())
700 .addConstantPoolIndex(LoOperand.getIndex(), LoOperand.getOffset(),
701 LoOperand.getTargetFlags());
702 return;
703 }
704
705 // First preference should be given to Hexagon::A2_combineii instruction
706 // as it can include U6 (in Hexagon::A4_combineii) as well.
707 // In this instruction, HiOperand is const extended, if required.
708 if (isInt<8>(LoOperand.getImm())) {
709 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
710 .addImm(HiOperand.getImm())
711 .addImm(LoOperand.getImm());
712 return;
713 }
714
715 // In this instruction, LoOperand is const extended, if required.
716 if (isInt<8>(HiOperand.getImm())) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000717 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000718 .addImm(HiOperand.getImm())
719 .addImm(LoOperand.getImm());
720 return;
721 }
722
723 // Insert new combine instruction.
724 // DoubleRegDest = combine #HiImm, #LoImm
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000725 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000726 .addImm(HiOperand.getImm())
727 .addImm(LoOperand.getImm());
728}
729
730void HexagonCopyToCombine::emitCombineIR(MachineBasicBlock::iterator &InsertPt,
731 unsigned DoubleDestReg,
732 MachineOperand &HiOperand,
733 MachineOperand &LoOperand) {
734 unsigned LoReg = LoOperand.getReg();
735 unsigned LoRegKillFlag = getKillRegState(LoOperand.isKill());
736
737 DebugLoc DL = InsertPt->getDebugLoc();
738 MachineBasicBlock *BB = InsertPt->getParent();
739
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000740 // Handle globals.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000741 if (HiOperand.isGlobal()) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000742 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000743 .addGlobalAddress(HiOperand.getGlobal(), HiOperand.getOffset(),
744 HiOperand.getTargetFlags())
745 .addReg(LoReg, LoRegKillFlag);
746 return;
747 }
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000748 // Handle block addresses.
749 if (HiOperand.isBlockAddress()) {
750 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
751 .addBlockAddress(HiOperand.getBlockAddress(), HiOperand.getOffset(),
752 HiOperand.getTargetFlags())
753 .addReg(LoReg, LoRegKillFlag);
754 return;
755 }
756 // Handle jump tables.
757 if (HiOperand.isJTI()) {
758 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
759 .addJumpTableIndex(HiOperand.getIndex(), HiOperand.getTargetFlags())
760 .addReg(LoReg, LoRegKillFlag);
761 return;
762 }
763 // Handle constant pools.
764 if (HiOperand.isCPI()) {
765 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
766 .addConstantPoolIndex(HiOperand.getIndex(), HiOperand.getOffset(),
767 HiOperand.getTargetFlags())
768 .addReg(LoReg, LoRegKillFlag);
769 return;
770 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000771 // Insert new combine instruction.
772 // DoubleRegDest = combine #HiImm, LoReg
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000773 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000774 .addImm(HiOperand.getImm())
775 .addReg(LoReg, LoRegKillFlag);
776}
777
778void HexagonCopyToCombine::emitCombineRI(MachineBasicBlock::iterator &InsertPt,
779 unsigned DoubleDestReg,
780 MachineOperand &HiOperand,
781 MachineOperand &LoOperand) {
782 unsigned HiRegKillFlag = getKillRegState(HiOperand.isKill());
783 unsigned HiReg = HiOperand.getReg();
784
785 DebugLoc DL = InsertPt->getDebugLoc();
786 MachineBasicBlock *BB = InsertPt->getParent();
787
788 // Handle global.
789 if (LoOperand.isGlobal()) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000790 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000791 .addReg(HiReg, HiRegKillFlag)
792 .addGlobalAddress(LoOperand.getGlobal(), LoOperand.getOffset(),
793 LoOperand.getTargetFlags());
794 return;
795 }
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000796 // Handle block addresses.
797 if (LoOperand.isBlockAddress()) {
798 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
799 .addReg(HiReg, HiRegKillFlag)
800 .addBlockAddress(LoOperand.getBlockAddress(), LoOperand.getOffset(),
801 LoOperand.getTargetFlags());
802 return;
803 }
804 // Handle jump tables.
805 if (LoOperand.isJTI()) {
806 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
807 .addReg(HiOperand.getReg(), HiRegKillFlag)
808 .addJumpTableIndex(LoOperand.getIndex(), LoOperand.getTargetFlags());
809 return;
810 }
811 // Handle constant pools.
812 if (LoOperand.isCPI()) {
813 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
814 .addReg(HiOperand.getReg(), HiRegKillFlag)
815 .addConstantPoolIndex(LoOperand.getIndex(), LoOperand.getOffset(),
816 LoOperand.getTargetFlags());
817 return;
818 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000819
820 // Insert new combine instruction.
821 // DoubleRegDest = combine HiReg, #LoImm
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000822 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000823 .addReg(HiReg, HiRegKillFlag)
824 .addImm(LoOperand.getImm());
825}
826
827void HexagonCopyToCombine::emitCombineRR(MachineBasicBlock::iterator &InsertPt,
828 unsigned DoubleDestReg,
829 MachineOperand &HiOperand,
830 MachineOperand &LoOperand) {
831 unsigned LoRegKillFlag = getKillRegState(LoOperand.isKill());
832 unsigned HiRegKillFlag = getKillRegState(HiOperand.isKill());
833 unsigned LoReg = LoOperand.getReg();
834 unsigned HiReg = HiOperand.getReg();
835
836 DebugLoc DL = InsertPt->getDebugLoc();
837 MachineBasicBlock *BB = InsertPt->getParent();
838
839 // Insert new combine instruction.
840 // DoubleRegDest = combine HiReg, LoReg
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000841 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combinew), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000842 .addReg(HiReg, HiRegKillFlag)
843 .addReg(LoReg, LoRegKillFlag);
844}
845
846FunctionPass *llvm::createHexagonCopyToCombine() {
847 return new HexagonCopyToCombine();
848}