blob: 61af3e7df8734f41c62ec7d8d1cd31c954103bcb [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
89private:
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +000090 MachineInstr *findPairable(MachineInstr *I1, bool &DoInsertAtI1,
91 bool AllowC64);
Jyotsna Verma803e5062013-05-14 18:54:06 +000092
93 void findPotentialNewifiableTFRs(MachineBasicBlock &);
94
95 void combine(MachineInstr *I1, MachineInstr *I2,
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +000096 MachineBasicBlock::iterator &MI, bool DoInsertAtI1,
97 bool OptForSize);
Jyotsna Verma803e5062013-05-14 18:54:06 +000098
99 bool isSafeToMoveTogether(MachineInstr *I1, MachineInstr *I2,
100 unsigned I1DestReg, unsigned I2DestReg,
101 bool &DoInsertAtI1);
102
103 void emitCombineRR(MachineBasicBlock::iterator &Before, unsigned DestReg,
104 MachineOperand &HiOperand, MachineOperand &LoOperand);
105
106 void emitCombineRI(MachineBasicBlock::iterator &Before, unsigned DestReg,
107 MachineOperand &HiOperand, MachineOperand &LoOperand);
108
109 void emitCombineIR(MachineBasicBlock::iterator &Before, unsigned DestReg,
110 MachineOperand &HiOperand, MachineOperand &LoOperand);
111
112 void emitCombineII(MachineBasicBlock::iterator &Before, unsigned DestReg,
113 MachineOperand &HiOperand, MachineOperand &LoOperand);
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000114
115 void emitConst64(MachineBasicBlock::iterator &Before, unsigned DestReg,
116 MachineOperand &HiOperand, MachineOperand &LoOperand);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000117};
118
119} // End anonymous namespace.
120
121char HexagonCopyToCombine::ID = 0;
122
123INITIALIZE_PASS(HexagonCopyToCombine, "hexagon-copy-combine",
124 "Hexagon Copy-To-Combine Pass", false, false)
125
126static bool isCombinableInstType(MachineInstr *MI,
127 const HexagonInstrInfo *TII,
128 bool ShouldCombineAggressively) {
129 switch(MI->getOpcode()) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000130 case Hexagon::A2_tfr: {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000131 // A COPY instruction can be combined if its arguments are IntRegs (32bit).
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000132 const MachineOperand &Op0 = MI->getOperand(0);
133 const MachineOperand &Op1 = MI->getOperand(1);
134 assert(Op0.isReg() && Op1.isReg());
Jyotsna Verma803e5062013-05-14 18:54:06 +0000135
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000136 unsigned DestReg = Op0.getReg();
137 unsigned SrcReg = Op1.getReg();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000138 return Hexagon::IntRegsRegClass.contains(DestReg) &&
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000139 Hexagon::IntRegsRegClass.contains(SrcReg);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000140 }
141
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000142 case Hexagon::A2_tfrsi: {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000143 // A transfer-immediate can be combined if its argument is a signed 8bit
144 // value.
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000145 const MachineOperand &Op0 = MI->getOperand(0);
146 const MachineOperand &Op1 = MI->getOperand(1);
147 assert(Op0.isReg());
Jyotsna Verma803e5062013-05-14 18:54:06 +0000148
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000149 unsigned DestReg = Op0.getReg();
150 // Ensure that TargetFlags are MO_NO_FLAG for a global. This is a
151 // workaround for an ABI bug that prevents GOT relocations on combine
152 // instructions
153 if (!Op1.isImm() && Op1.getTargetFlags() != HexagonII::MO_NO_FLAG)
154 return false;
155
156 // Only combine constant extended A2_tfrsi if we are in aggressive mode.
157 bool NotExt = Op1.isImm() && isInt<8>(Op1.getImm());
Jyotsna Verma803e5062013-05-14 18:54:06 +0000158 return Hexagon::IntRegsRegClass.contains(DestReg) &&
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000159 (ShouldCombineAggressively || NotExt);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000160 }
161
Jyotsna Verma803e5062013-05-14 18:54:06 +0000162 default:
163 break;
164 }
165
166 return false;
167}
168
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000169template <unsigned N>
170static bool isGreaterThanNBitTFRI(const MachineInstr *I) {
171 if (I->getOpcode() == Hexagon::TFRI64_V4 ||
172 I->getOpcode() == Hexagon::A2_tfrsi) {
173 const MachineOperand &Op = I->getOperand(1);
174 return !Op.isImm() || !isInt<N>(Op.getImm());
175 }
176 return false;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000177}
178
179/// areCombinableOperations - Returns true if the two instruction can be merge
180/// into a combine (ignoring register constraints).
181static bool areCombinableOperations(const TargetRegisterInfo *TRI,
182 MachineInstr *HighRegInst,
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000183 MachineInstr *LowRegInst, bool AllowC64) {
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000184 unsigned HiOpc = HighRegInst->getOpcode();
185 unsigned LoOpc = LowRegInst->getOpcode();
Krzysztof Parzyszeke5689672015-04-23 20:26:21 +0000186 (void)HiOpc; // Fix compiler warning
187 (void)LoOpc; // Fix compiler warning
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000188 assert((HiOpc == Hexagon::A2_tfr || HiOpc == Hexagon::A2_tfrsi) &&
189 (LoOpc == Hexagon::A2_tfr || LoOpc == Hexagon::A2_tfrsi) &&
Jyotsna Verma803e5062013-05-14 18:54:06 +0000190 "Assume individual instructions are of a combinable type");
191
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000192 if (!AllowC64) {
193 // There is no combine of two constant extended values.
194 if (isGreaterThanNBitTFRI<8>(HighRegInst) &&
195 isGreaterThanNBitTFRI<6>(LowRegInst))
196 return false;
197 }
198
199 // There is a combine of two constant extended values into CONST64,
200 // provided both constants are true immediates.
201 if (isGreaterThanNBitTFRI<16>(HighRegInst) &&
202 isGreaterThanNBitTFRI<16>(LowRegInst))
203 return (HighRegInst->getOperand(1).isImm() &&
204 LowRegInst->getOperand(1).isImm());
205
206 // There is no combine of two constant extended values, unless handled above
207 // Make both 8-bit size checks to allow both combine (#,##) and combine(##,#)
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000208 if (isGreaterThanNBitTFRI<8>(HighRegInst) &&
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000209 isGreaterThanNBitTFRI<8>(LowRegInst))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000210 return false;
211
212 return true;
213}
214
215static bool isEvenReg(unsigned Reg) {
216 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
217 Hexagon::IntRegsRegClass.contains(Reg));
218 return (Reg - Hexagon::R0) % 2 == 0;
219}
220
221static void removeKillInfo(MachineInstr *MI, unsigned RegNotKilled) {
222 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
223 MachineOperand &Op = MI->getOperand(I);
224 if (!Op.isReg() || Op.getReg() != RegNotKilled || !Op.isKill())
225 continue;
226 Op.setIsKill(false);
227 }
228}
229
Jyotsna Vermacceafb22013-05-28 19:01:45 +0000230/// isUnsafeToMoveAcross - Returns true if it is unsafe to move a copy
Jyotsna Verma803e5062013-05-14 18:54:06 +0000231/// instruction from \p UseReg to \p DestReg over the instruction \p I.
Jyotsna Vermacceafb22013-05-28 19:01:45 +0000232static bool isUnsafeToMoveAcross(MachineInstr *I, unsigned UseReg,
Benjamin Kramere79beac2013-05-23 15:43:11 +0000233 unsigned DestReg,
234 const TargetRegisterInfo *TRI) {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000235 return (UseReg && (I->modifiesRegister(UseReg, TRI))) ||
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000236 I->modifiesRegister(DestReg, TRI) ||
237 I->readsRegister(DestReg, TRI) ||
238 I->hasUnmodeledSideEffects() ||
239 I->isInlineAsm() || I->isDebugValue();
240}
241
242static unsigned UseReg(const MachineOperand& MO) {
243 return MO.isReg() ? MO.getReg() : 0;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000244}
245
246/// isSafeToMoveTogether - Returns true if it is safe to move I1 next to I2 such
247/// that the two instructions can be paired in a combine.
248bool HexagonCopyToCombine::isSafeToMoveTogether(MachineInstr *I1,
249 MachineInstr *I2,
250 unsigned I1DestReg,
251 unsigned I2DestReg,
252 bool &DoInsertAtI1) {
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000253 unsigned I2UseReg = UseReg(I2->getOperand(1));
Jyotsna Verma803e5062013-05-14 18:54:06 +0000254
255 // It is not safe to move I1 and I2 into one combine if I2 has a true
256 // dependence on I1.
257 if (I2UseReg && I1->modifiesRegister(I2UseReg, TRI))
258 return false;
259
260 bool isSafe = true;
261
262 // First try to move I2 towards I1.
263 {
264 // A reverse_iterator instantiated like below starts before I2, and I1
265 // respectively.
266 // Look at instructions I in between I2 and (excluding) I1.
267 MachineBasicBlock::reverse_iterator I(I2),
268 End = --(MachineBasicBlock::reverse_iterator(I1));
269 // At 03 we got better results (dhrystone!) by being more conservative.
270 if (!ShouldCombineAggressively)
271 End = MachineBasicBlock::reverse_iterator(I1);
272 // If I2 kills its operand and we move I2 over an instruction that also
273 // uses I2's use reg we need to modify that (first) instruction to now kill
274 // this reg.
275 unsigned KilledOperand = 0;
276 if (I2->killsRegister(I2UseReg))
277 KilledOperand = I2UseReg;
Craig Topper062a2ba2014-04-25 05:30:21 +0000278 MachineInstr *KillingInstr = nullptr;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000279
280 for (; I != End; ++I) {
281 // If the intervening instruction I:
282 // * modifies I2's use reg
283 // * modifies I2's def reg
284 // * reads I2's def reg
285 // * or has unmodelled side effects
286 // we can't move I2 across it.
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000287 if (I->isDebugValue())
288 continue;
289
Jyotsna Vermacceafb22013-05-28 19:01:45 +0000290 if (isUnsafeToMoveAcross(&*I, I2UseReg, I2DestReg, TRI)) {
Jyotsna Verma803e5062013-05-14 18:54:06 +0000291 isSafe = false;
292 break;
293 }
294
295 // Update first use of the killed operand.
296 if (!KillingInstr && KilledOperand &&
297 I->readsRegister(KilledOperand, TRI))
298 KillingInstr = &*I;
299 }
300 if (isSafe) {
301 // Update the intermediate instruction to with the kill flag.
302 if (KillingInstr) {
303 bool Added = KillingInstr->addRegisterKilled(KilledOperand, TRI, true);
Alp Tokercb402912014-01-24 17:20:08 +0000304 (void)Added; // suppress compiler warning
Jyotsna Verma803e5062013-05-14 18:54:06 +0000305 assert(Added && "Must successfully update kill flag");
306 removeKillInfo(I2, KilledOperand);
307 }
308 DoInsertAtI1 = true;
309 return true;
310 }
311 }
312
313 // Try to move I1 towards I2.
314 {
315 // Look at instructions I in between I1 and (excluding) I2.
316 MachineBasicBlock::iterator I(I1), End(I2);
317 // At O3 we got better results (dhrystone) by being more conservative here.
318 if (!ShouldCombineAggressively)
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000319 End = std::next(MachineBasicBlock::iterator(I2));
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000320 unsigned I1UseReg = UseReg(I1->getOperand(1));
Jyotsna Vermacceafb22013-05-28 19:01:45 +0000321 // Track killed operands. If we move across an instruction that kills our
Jyotsna Verma803e5062013-05-14 18:54:06 +0000322 // operand, we need to update the kill information on the moved I1. It kills
323 // the operand now.
Craig Topper062a2ba2014-04-25 05:30:21 +0000324 MachineInstr *KillingInstr = nullptr;
Jyotsna Verma803e5062013-05-14 18:54:06 +0000325 unsigned KilledOperand = 0;
326
327 while(++I != End) {
328 // If the intervening instruction I:
329 // * modifies I1's use reg
330 // * modifies I1's def reg
331 // * reads I1's def reg
332 // * or has unmodelled side effects
333 // We introduce this special case because llvm has no api to remove a
334 // kill flag for a register (a removeRegisterKilled() analogous to
335 // addRegisterKilled) that handles aliased register correctly.
336 // * or has a killed aliased register use of I1's use reg
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000337 // %D4<def> = A2_tfrpi 16
338 // %R6<def> = A2_tfr %R9
Jyotsna Verma803e5062013-05-14 18:54:06 +0000339 // %R8<def> = KILL %R8, %D4<imp-use,kill>
340 // If we want to move R6 = across the KILL instruction we would have
341 // to remove the %D4<imp-use,kill> operand. For now, we are
342 // conservative and disallow the move.
343 // we can't move I1 across it.
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000344 if (I->isDebugValue()) {
345 if (I->readsRegister(I1DestReg, TRI)) // Move this instruction after I2.
346 DbgMItoMove.push_back(I);
347 continue;
348 }
349
Jyotsna Vermacceafb22013-05-28 19:01:45 +0000350 if (isUnsafeToMoveAcross(I, I1UseReg, I1DestReg, TRI) ||
Jyotsna Verma803e5062013-05-14 18:54:06 +0000351 // Check for an aliased register kill. Bail out if we see one.
352 (!I->killsRegister(I1UseReg) && I->killsRegister(I1UseReg, TRI)))
353 return false;
354
355 // Check for an exact kill (registers match).
356 if (I1UseReg && I->killsRegister(I1UseReg)) {
Craig Toppere73658d2014-04-28 04:05:08 +0000357 assert(!KillingInstr && "Should only see one killing instruction");
Jyotsna Verma803e5062013-05-14 18:54:06 +0000358 KilledOperand = I1UseReg;
359 KillingInstr = &*I;
360 }
361 }
362 if (KillingInstr) {
363 removeKillInfo(KillingInstr, KilledOperand);
364 // Update I1 to set the kill flag. This flag will later be picked up by
365 // the new COMBINE instruction.
366 bool Added = I1->addRegisterKilled(KilledOperand, TRI);
Alp Tokercb402912014-01-24 17:20:08 +0000367 (void)Added; // suppress compiler warning
Jyotsna Verma803e5062013-05-14 18:54:06 +0000368 assert(Added && "Must successfully update kill flag");
369 }
370 DoInsertAtI1 = false;
371 }
372
373 return true;
374}
375
376/// findPotentialNewifiableTFRs - Finds tranfers that feed stores that could be
377/// newified. (A use of a 64 bit register define can not be newified)
378void
379HexagonCopyToCombine::findPotentialNewifiableTFRs(MachineBasicBlock &BB) {
380 DenseMap<unsigned, MachineInstr *> LastDef;
381 for (MachineBasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
382 MachineInstr *MI = I;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000383 if (MI->isDebugValue())
384 continue;
385
Jyotsna Verma803e5062013-05-14 18:54:06 +0000386 // Mark TFRs that feed a potential new value store as such.
387 if(TII->mayBeNewStore(MI)) {
388 // Look for uses of TFR instructions.
389 for (unsigned OpdIdx = 0, OpdE = MI->getNumOperands(); OpdIdx != OpdE;
390 ++OpdIdx) {
391 MachineOperand &Op = MI->getOperand(OpdIdx);
392
393 // Skip over anything except register uses.
394 if (!Op.isReg() || !Op.isUse() || !Op.getReg())
395 continue;
396
397 // Look for the defining instruction.
398 unsigned Reg = Op.getReg();
399 MachineInstr *DefInst = LastDef[Reg];
400 if (!DefInst)
401 continue;
402 if (!isCombinableInstType(DefInst, TII, ShouldCombineAggressively))
403 continue;
404
405 // Only close newifiable stores should influence the decision.
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000406 // Ignore the debug instructions in between.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000407 MachineBasicBlock::iterator It(DefInst);
408 unsigned NumInstsToDef = 0;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000409 while (&*It != MI) {
410 if (!It->isDebugValue())
411 ++NumInstsToDef;
Krzysztof Parzyszek14f9535e2016-01-21 12:45:17 +0000412 ++It;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000413 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000414
415 if (NumInstsToDef > MaxNumOfInstsBetweenNewValueStoreAndTFR)
416 continue;
417
418 PotentiallyNewifiableTFR.insert(DefInst);
419 }
420 // Skip to next instruction.
421 continue;
422 }
423
424 // Put instructions that last defined integer or double registers into the
425 // map.
426 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
427 MachineOperand &Op = MI->getOperand(I);
428 if (!Op.isReg() || !Op.isDef() || !Op.getReg())
429 continue;
430 unsigned Reg = Op.getReg();
431 if (Hexagon::DoubleRegsRegClass.contains(Reg)) {
432 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
433 LastDef[*SubRegs] = MI;
434 }
435 } else if (Hexagon::IntRegsRegClass.contains(Reg))
436 LastDef[Reg] = MI;
437 }
438 }
439}
440
441bool HexagonCopyToCombine::runOnMachineFunction(MachineFunction &MF) {
442
443 if (IsCombinesDisabled) return false;
444
445 bool HasChanged = false;
446
447 // Get target info.
Eric Christopherfc6de422014-08-05 02:39:49 +0000448 TRI = MF.getSubtarget().getRegisterInfo();
Eric Christopher12a5c0d2015-02-02 18:46:29 +0000449 TII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
Jyotsna Verma803e5062013-05-14 18:54:06 +0000450
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000451 const Function *F = MF.getFunction();
452 bool OptForSize = F->hasFnAttribute(Attribute::OptimizeForSize);
453
Jyotsna Verma803e5062013-05-14 18:54:06 +0000454 // Combine aggressively (for code size)
455 ShouldCombineAggressively =
456 MF.getTarget().getOptLevel() <= CodeGenOpt::Default;
457
458 // Traverse basic blocks.
459 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); BI != BE;
460 ++BI) {
461 PotentiallyNewifiableTFR.clear();
462 findPotentialNewifiableTFRs(*BI);
463
464 // Traverse instructions in basic block.
465 for(MachineBasicBlock::iterator MI = BI->begin(), End = BI->end();
466 MI != End;) {
467 MachineInstr *I1 = MI++;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000468
469 if (I1->isDebugValue())
470 continue;
471
Jyotsna Verma803e5062013-05-14 18:54:06 +0000472 // Don't combine a TFR whose user could be newified (instructions that
473 // define double registers can not be newified - Programmer's Ref Manual
474 // 5.4.2 New-value stores).
475 if (ShouldCombineAggressively && PotentiallyNewifiableTFR.count(I1))
476 continue;
477
478 // Ignore instructions that are not combinable.
479 if (!isCombinableInstType(I1, TII, ShouldCombineAggressively))
480 continue;
481
482 // Find a second instruction that can be merged into a combine
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000483 // instruction. In addition, also find all the debug instructions that
484 // need to be moved along with it.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000485 bool DoInsertAtI1 = false;
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000486 DbgMItoMove.clear();
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000487 MachineInstr *I2 = findPairable(I1, DoInsertAtI1, OptForSize);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000488 if (I2) {
489 HasChanged = true;
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000490 combine(I1, I2, MI, DoInsertAtI1, OptForSize);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000491 }
492 }
493 }
494
495 return HasChanged;
496}
497
498/// findPairable - Returns an instruction that can be merged with \p I1 into a
499/// COMBINE instruction or 0 if no such instruction can be found. Returns true
500/// in \p DoInsertAtI1 if the combine must be inserted at instruction \p I1
501/// false if the combine must be inserted at the returned instruction.
502MachineInstr *HexagonCopyToCombine::findPairable(MachineInstr *I1,
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000503 bool &DoInsertAtI1,
504 bool AllowC64) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000505 MachineBasicBlock::iterator I2 = std::next(MachineBasicBlock::iterator(I1));
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000506
507 while (I2->isDebugValue())
508 ++I2;
509
Jyotsna Verma803e5062013-05-14 18:54:06 +0000510 unsigned I1DestReg = I1->getOperand(0).getReg();
511
512 for (MachineBasicBlock::iterator End = I1->getParent()->end(); I2 != End;
513 ++I2) {
514 // Bail out early if we see a second definition of I1DestReg.
515 if (I2->modifiesRegister(I1DestReg, TRI))
516 break;
517
518 // Ignore non-combinable instructions.
519 if (!isCombinableInstType(I2, TII, ShouldCombineAggressively))
520 continue;
521
522 // Don't combine a TFR whose user could be newified.
523 if (ShouldCombineAggressively && PotentiallyNewifiableTFR.count(I2))
524 continue;
525
526 unsigned I2DestReg = I2->getOperand(0).getReg();
527
528 // Check that registers are adjacent and that the first destination register
529 // is even.
530 bool IsI1LowReg = (I2DestReg - I1DestReg) == 1;
531 bool IsI2LowReg = (I1DestReg - I2DestReg) == 1;
532 unsigned FirstRegIndex = IsI1LowReg ? I1DestReg : I2DestReg;
533 if ((!IsI1LowReg && !IsI2LowReg) || !isEvenReg(FirstRegIndex))
534 continue;
535
536 // Check that the two instructions are combinable. V4 allows more
537 // instructions to be merged into a combine.
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000538 // The order matters because in a A2_tfrsi we might can encode a int8 as
539 // the hi reg operand but only a uint6 as the low reg operand.
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000540 if ((IsI2LowReg && !areCombinableOperations(TRI, I1, I2, AllowC64)) ||
541 (IsI1LowReg && !areCombinableOperations(TRI, I2, I1, AllowC64)))
Jyotsna Verma803e5062013-05-14 18:54:06 +0000542 break;
543
544 if (isSafeToMoveTogether(I1, I2, I1DestReg, I2DestReg,
545 DoInsertAtI1))
546 return I2;
547
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
554void HexagonCopyToCombine::combine(MachineInstr *I1, MachineInstr *I2,
555 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.
559 if ((MachineInstr *)MI == I2) ++MI;
560
561 // Figure out whether I1 or I2 goes into the lowreg part.
562 unsigned I1DestReg = I1->getOperand(0).getReg();
563 unsigned I2DestReg = I2->getOperand(0).getReg();
564 bool IsI1Loreg = (I2DestReg - I1DestReg) == 1;
565 unsigned LoRegDef = IsI1Loreg ? I1DestReg : I2DestReg;
566
567 // Get the double word register.
568 unsigned DoubleRegDest =
569 TRI->getMatchingSuperReg(LoRegDef, Hexagon::subreg_loreg,
570 &Hexagon::DoubleRegsRegClass);
571 assert(DoubleRegDest != 0 && "Expect a valid register");
572
573
574 // Setup source operands.
575 MachineOperand &LoOperand = IsI1Loreg ? I1->getOperand(1) :
576 I2->getOperand(1);
577 MachineOperand &HiOperand = IsI1Loreg ? I2->getOperand(1) :
578 I1->getOperand(1);
579
580 // Figure out which source is a register and which a constant.
581 bool IsHiReg = HiOperand.isReg();
582 bool IsLoReg = LoOperand.isReg();
583
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000584 // There is a combine of two constant extended values into CONST64.
585 bool IsC64 = OptForSize && LoOperand.isImm() && HiOperand.isImm() &&
586 isGreaterThanNBitTFRI<16>(I1) && isGreaterThanNBitTFRI<16>(I2);
587
Jyotsna Verma803e5062013-05-14 18:54:06 +0000588 MachineBasicBlock::iterator InsertPt(DoInsertAtI1 ? I1 : I2);
589 // Emit combine.
590 if (IsHiReg && IsLoReg)
591 emitCombineRR(InsertPt, DoubleRegDest, HiOperand, LoOperand);
592 else if (IsHiReg)
593 emitCombineRI(InsertPt, DoubleRegDest, HiOperand, LoOperand);
594 else if (IsLoReg)
595 emitCombineIR(InsertPt, DoubleRegDest, HiOperand, LoOperand);
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000596 else if (IsC64 && !IsConst64Disabled)
597 emitConst64(InsertPt, DoubleRegDest, HiOperand, LoOperand);
Jyotsna Verma803e5062013-05-14 18:54:06 +0000598 else
599 emitCombineII(InsertPt, DoubleRegDest, HiOperand, LoOperand);
600
Krzysztof Parzyszek9b7320e2016-01-15 13:55:57 +0000601 // Move debug instructions along with I1 if it's being
602 // moved towards I2.
603 if (!DoInsertAtI1 && DbgMItoMove.size() != 0) {
604 // Insert debug instructions at the new location before I2.
605 MachineBasicBlock *BB = InsertPt->getParent();
606 for (auto NewMI : DbgMItoMove) {
607 // If iterator MI is pointing to DEBUG_VAL, make sure
608 // MI now points to next relevant instruction.
609 if (NewMI == (MachineInstr*)MI)
610 ++MI;
611 BB->splice(InsertPt, BB, NewMI);
612 }
613 }
614
Jyotsna Verma803e5062013-05-14 18:54:06 +0000615 I1->eraseFromParent();
616 I2->eraseFromParent();
617}
618
Krzysztof Parzyszek2a3b2f92016-01-15 14:08:31 +0000619void HexagonCopyToCombine::emitConst64(MachineBasicBlock::iterator &InsertPt,
620 unsigned DoubleDestReg,
621 MachineOperand &HiOperand,
622 MachineOperand &LoOperand) {
623 DEBUG(dbgs() << "Found a CONST64\n");
624
625 DebugLoc DL = InsertPt->getDebugLoc();
626 MachineBasicBlock *BB = InsertPt->getParent();
627 assert(LoOperand.isImm() && HiOperand.isImm() &&
628 "Both operands must be immediate");
629
630 int64_t V = HiOperand.getImm();
631 V = (V << 32) | (0x0ffffffffLL & LoOperand.getImm());
632 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::CONST64_Int_Real),
633 DoubleDestReg)
634 .addImm(V);
635}
636
Jyotsna Verma803e5062013-05-14 18:54:06 +0000637void HexagonCopyToCombine::emitCombineII(MachineBasicBlock::iterator &InsertPt,
638 unsigned DoubleDestReg,
639 MachineOperand &HiOperand,
640 MachineOperand &LoOperand) {
641 DebugLoc DL = InsertPt->getDebugLoc();
642 MachineBasicBlock *BB = InsertPt->getParent();
643
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000644 // Handle globals.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000645 if (HiOperand.isGlobal()) {
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000646 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000647 .addGlobalAddress(HiOperand.getGlobal(), HiOperand.getOffset(),
648 HiOperand.getTargetFlags())
649 .addImm(LoOperand.getImm());
650 return;
651 }
652 if (LoOperand.isGlobal()) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000653 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000654 .addImm(HiOperand.getImm())
655 .addGlobalAddress(LoOperand.getGlobal(), LoOperand.getOffset(),
656 LoOperand.getTargetFlags());
657 return;
658 }
659
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000660 // Handle block addresses.
661 if (HiOperand.isBlockAddress()) {
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000662 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000663 .addBlockAddress(HiOperand.getBlockAddress(), HiOperand.getOffset(),
664 HiOperand.getTargetFlags())
Jyotsna Verma803e5062013-05-14 18:54:06 +0000665 .addImm(LoOperand.getImm());
666 return;
667 }
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000668 if (LoOperand.isBlockAddress()) {
669 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
670 .addImm(HiOperand.getImm())
671 .addBlockAddress(LoOperand.getBlockAddress(), LoOperand.getOffset(),
672 LoOperand.getTargetFlags());
673 return;
674 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000675
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000676 // Handle jump tables.
677 if (HiOperand.isJTI()) {
678 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
679 .addJumpTableIndex(HiOperand.getIndex(), HiOperand.getTargetFlags())
680 .addImm(LoOperand.getImm());
681 return;
682 }
683 if (LoOperand.isJTI()) {
684 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
685 .addImm(HiOperand.getImm())
686 .addJumpTableIndex(LoOperand.getIndex(), LoOperand.getTargetFlags());
687 return;
688 }
689
690 // Handle constant pools.
691 if (HiOperand.isCPI()) {
692 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
693 .addConstantPoolIndex(HiOperand.getIndex(), HiOperand.getOffset(),
694 HiOperand.getTargetFlags())
695 .addImm(LoOperand.getImm());
696 return;
697 }
698 if (LoOperand.isCPI()) {
699 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
700 .addImm(HiOperand.getImm())
701 .addConstantPoolIndex(LoOperand.getIndex(), LoOperand.getOffset(),
702 LoOperand.getTargetFlags());
703 return;
704 }
705
706 // First preference should be given to Hexagon::A2_combineii instruction
707 // as it can include U6 (in Hexagon::A4_combineii) as well.
708 // In this instruction, HiOperand is const extended, if required.
709 if (isInt<8>(LoOperand.getImm())) {
710 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
711 .addImm(HiOperand.getImm())
712 .addImm(LoOperand.getImm());
713 return;
714 }
715
716 // In this instruction, LoOperand is const extended, if required.
717 if (isInt<8>(HiOperand.getImm())) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000718 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000719 .addImm(HiOperand.getImm())
720 .addImm(LoOperand.getImm());
721 return;
722 }
723
724 // Insert new combine instruction.
725 // DoubleRegDest = combine #HiImm, #LoImm
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000726 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000727 .addImm(HiOperand.getImm())
728 .addImm(LoOperand.getImm());
729}
730
731void HexagonCopyToCombine::emitCombineIR(MachineBasicBlock::iterator &InsertPt,
732 unsigned DoubleDestReg,
733 MachineOperand &HiOperand,
734 MachineOperand &LoOperand) {
735 unsigned LoReg = LoOperand.getReg();
736 unsigned LoRegKillFlag = getKillRegState(LoOperand.isKill());
737
738 DebugLoc DL = InsertPt->getDebugLoc();
739 MachineBasicBlock *BB = InsertPt->getParent();
740
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000741 // Handle globals.
Jyotsna Verma803e5062013-05-14 18:54:06 +0000742 if (HiOperand.isGlobal()) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000743 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000744 .addGlobalAddress(HiOperand.getGlobal(), HiOperand.getOffset(),
745 HiOperand.getTargetFlags())
746 .addReg(LoReg, LoRegKillFlag);
747 return;
748 }
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000749 // Handle block addresses.
750 if (HiOperand.isBlockAddress()) {
751 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
752 .addBlockAddress(HiOperand.getBlockAddress(), HiOperand.getOffset(),
753 HiOperand.getTargetFlags())
754 .addReg(LoReg, LoRegKillFlag);
755 return;
756 }
757 // Handle jump tables.
758 if (HiOperand.isJTI()) {
759 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
760 .addJumpTableIndex(HiOperand.getIndex(), HiOperand.getTargetFlags())
761 .addReg(LoReg, LoRegKillFlag);
762 return;
763 }
764 // Handle constant pools.
765 if (HiOperand.isCPI()) {
766 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
767 .addConstantPoolIndex(HiOperand.getIndex(), HiOperand.getOffset(),
768 HiOperand.getTargetFlags())
769 .addReg(LoReg, LoRegKillFlag);
770 return;
771 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000772 // Insert new combine instruction.
773 // DoubleRegDest = combine #HiImm, LoReg
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000774 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000775 .addImm(HiOperand.getImm())
776 .addReg(LoReg, LoRegKillFlag);
777}
778
779void HexagonCopyToCombine::emitCombineRI(MachineBasicBlock::iterator &InsertPt,
780 unsigned DoubleDestReg,
781 MachineOperand &HiOperand,
782 MachineOperand &LoOperand) {
783 unsigned HiRegKillFlag = getKillRegState(HiOperand.isKill());
784 unsigned HiReg = HiOperand.getReg();
785
786 DebugLoc DL = InsertPt->getDebugLoc();
787 MachineBasicBlock *BB = InsertPt->getParent();
788
789 // Handle global.
790 if (LoOperand.isGlobal()) {
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000791 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000792 .addReg(HiReg, HiRegKillFlag)
793 .addGlobalAddress(LoOperand.getGlobal(), LoOperand.getOffset(),
794 LoOperand.getTargetFlags());
795 return;
796 }
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +0000797 // Handle block addresses.
798 if (LoOperand.isBlockAddress()) {
799 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
800 .addReg(HiReg, HiRegKillFlag)
801 .addBlockAddress(LoOperand.getBlockAddress(), LoOperand.getOffset(),
802 LoOperand.getTargetFlags());
803 return;
804 }
805 // Handle jump tables.
806 if (LoOperand.isJTI()) {
807 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
808 .addReg(HiOperand.getReg(), HiRegKillFlag)
809 .addJumpTableIndex(LoOperand.getIndex(), LoOperand.getTargetFlags());
810 return;
811 }
812 // Handle constant pools.
813 if (LoOperand.isCPI()) {
814 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
815 .addReg(HiOperand.getReg(), HiRegKillFlag)
816 .addConstantPoolIndex(LoOperand.getIndex(), LoOperand.getOffset(),
817 LoOperand.getTargetFlags());
818 return;
819 }
Jyotsna Verma803e5062013-05-14 18:54:06 +0000820
821 // Insert new combine instruction.
822 // DoubleRegDest = combine HiReg, #LoImm
Colin LeMahieu82fb8cb2014-12-30 17:53:54 +0000823 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000824 .addReg(HiReg, HiRegKillFlag)
825 .addImm(LoOperand.getImm());
826}
827
828void HexagonCopyToCombine::emitCombineRR(MachineBasicBlock::iterator &InsertPt,
829 unsigned DoubleDestReg,
830 MachineOperand &HiOperand,
831 MachineOperand &LoOperand) {
832 unsigned LoRegKillFlag = getKillRegState(LoOperand.isKill());
833 unsigned HiRegKillFlag = getKillRegState(HiOperand.isKill());
834 unsigned LoReg = LoOperand.getReg();
835 unsigned HiReg = HiOperand.getReg();
836
837 DebugLoc DL = InsertPt->getDebugLoc();
838 MachineBasicBlock *BB = InsertPt->getParent();
839
840 // Insert new combine instruction.
841 // DoubleRegDest = combine HiReg, LoReg
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000842 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combinew), DoubleDestReg)
Jyotsna Verma803e5062013-05-14 18:54:06 +0000843 .addReg(HiReg, HiRegKillFlag)
844 .addReg(LoReg, LoRegKillFlag);
845}
846
847FunctionPass *llvm::createHexagonCopyToCombine() {
848 return new HexagonCopyToCombine();
849}