blob: 37bf9033a058438667b219fd3c6af8887424eddd [file] [log] [blame]
Silviu Baranga82dd6ac2013-03-15 18:28:25 +00001//=== A15SDOptimizerPass.cpp - Optimize DPR and SPR register accesses on A15==//
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//
10// The Cortex-A15 processor employs a tracking scheme in its register renaming
11// in order to process each instruction's micro-ops speculatively and
12// out-of-order with appropriate forwarding. The ARM architecture allows VFP
13// instructions to read and write 32-bit S-registers. Each S-register
14// corresponds to one half (upper or lower) of an overlaid 64-bit D-register.
15//
16// There are several instruction patterns which can be used to provide this
17// capability which can provide higher performance than other, potentially more
18// direct patterns, specifically around when one micro-op reads a D-register
19// operand that has recently been written as one or more S-register results.
20//
21// This file defines a pre-regalloc pass which looks for SPR producers which
22// are going to be used by a DPR (or QPR) consumers and creates the more
23// optimized access pattern.
24//
25//===----------------------------------------------------------------------===//
26
27#define DEBUG_TYPE "a15-sd-optimizer"
28#include "ARM.h"
29#include "ARMBaseInstrInfo.h"
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000030#include "ARMISelLowering.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000031#include "ARMSubtarget.h"
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000032#include "ARMTargetMachine.h"
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000033#include "llvm/ADT/SmallPtrSet.h"
34#include "llvm/ADT/Statistic.h"
35#include "llvm/CodeGen/MachineFunctionPass.h"
36#include "llvm/CodeGen/MachineInstr.h"
37#include "llvm/CodeGen/MachineInstrBuilder.h"
38#include "llvm/CodeGen/MachineRegisterInfo.h"
39#include "llvm/Support/CommandLine.h"
40#include "llvm/Support/Debug.h"
41#include "llvm/Support/raw_ostream.h"
42#include "llvm/Target/TargetRegisterInfo.h"
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000043#include <set>
44
45using namespace llvm;
46
47namespace {
48 struct A15SDOptimizer : public MachineFunctionPass {
49 static char ID;
50 A15SDOptimizer() : MachineFunctionPass(ID) {}
51
Craig Topper6bc27bf2014-03-10 02:09:33 +000052 bool runOnMachineFunction(MachineFunction &Fn) override;
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000053
Craig Topper6bc27bf2014-03-10 02:09:33 +000054 const char *getPassName() const override {
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000055 return "ARM A15 S->D optimizer";
56 }
57
58 private:
59 const ARMBaseInstrInfo *TII;
60 const TargetRegisterInfo *TRI;
61 MachineRegisterInfo *MRI;
62
63 bool runOnInstruction(MachineInstr *MI);
64
65 //
66 // Instruction builder helpers
67 //
68 unsigned createDupLane(MachineBasicBlock &MBB,
69 MachineBasicBlock::iterator InsertBefore,
70 DebugLoc DL,
71 unsigned Reg, unsigned Lane,
72 bool QPR=false);
73
74 unsigned createExtractSubreg(MachineBasicBlock &MBB,
75 MachineBasicBlock::iterator InsertBefore,
76 DebugLoc DL,
77 unsigned DReg, unsigned Lane,
78 const TargetRegisterClass *TRC);
79
80 unsigned createVExt(MachineBasicBlock &MBB,
81 MachineBasicBlock::iterator InsertBefore,
82 DebugLoc DL,
83 unsigned Ssub0, unsigned Ssub1);
84
85 unsigned createRegSequence(MachineBasicBlock &MBB,
86 MachineBasicBlock::iterator InsertBefore,
87 DebugLoc DL,
88 unsigned Reg1, unsigned Reg2);
89
90 unsigned createInsertSubreg(MachineBasicBlock &MBB,
91 MachineBasicBlock::iterator InsertBefore,
92 DebugLoc DL, unsigned DReg, unsigned Lane,
93 unsigned ToInsert);
94
95 unsigned createImplicitDef(MachineBasicBlock &MBB,
96 MachineBasicBlock::iterator InsertBefore,
97 DebugLoc DL);
98
99 //
100 // Various property checkers
101 //
102 bool usesRegClass(MachineOperand &MO, const TargetRegisterClass *TRC);
103 bool hasPartialWrite(MachineInstr *MI);
104 SmallVector<unsigned, 8> getReadDPRs(MachineInstr *MI);
105 unsigned getDPRLaneFromSPR(unsigned SReg);
106
107 //
108 // Methods used for getting the definitions of partial registers
109 //
110
111 MachineInstr *elideCopies(MachineInstr *MI);
112 void elideCopiesAndPHIs(MachineInstr *MI,
113 SmallVectorImpl<MachineInstr*> &Outs);
114
115 //
116 // Pattern optimization methods
117 //
118 unsigned optimizeAllLanesPattern(MachineInstr *MI, unsigned Reg);
119 unsigned optimizeSDPattern(MachineInstr *MI);
120 unsigned getPrefSPRLane(unsigned SReg);
121
122 //
123 // Sanitizing method - used to make sure if don't leave dead code around.
124 //
125 void eraseInstrWithNoUses(MachineInstr *MI);
126
127 //
128 // A map used to track the changes done by this pass.
129 //
130 std::map<MachineInstr*, unsigned> Replacements;
131 std::set<MachineInstr *> DeadInstr;
132 };
133 char A15SDOptimizer::ID = 0;
134} // end anonymous namespace
135
136// Returns true if this is a use of a SPR register.
137bool A15SDOptimizer::usesRegClass(MachineOperand &MO,
138 const TargetRegisterClass *TRC) {
139 if (!MO.isReg())
140 return false;
141 unsigned Reg = MO.getReg();
142
143 if (TargetRegisterInfo::isVirtualRegister(Reg))
144 return MRI->getRegClass(Reg)->hasSuperClassEq(TRC);
145 else
146 return TRC->contains(Reg);
147}
148
149unsigned A15SDOptimizer::getDPRLaneFromSPR(unsigned SReg) {
150 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1,
151 &ARM::DPRRegClass);
152 if (DReg != ARM::NoRegister) return ARM::ssub_1;
153 return ARM::ssub_0;
154}
155
156// Get the subreg type that is most likely to be coalesced
157// for an SPR register that will be used in VDUP32d pseudo.
158unsigned A15SDOptimizer::getPrefSPRLane(unsigned SReg) {
159 if (!TRI->isVirtualRegister(SReg))
160 return getDPRLaneFromSPR(SReg);
161
162 MachineInstr *MI = MRI->getVRegDef(SReg);
163 if (!MI) return ARM::ssub_0;
164 MachineOperand *MO = MI->findRegisterDefOperand(SReg);
165
Alp Tokerf907b892013-12-05 05:44:44 +0000166 assert(MO->isReg() && "Non-register operand found!");
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000167 if (!MO) return ARM::ssub_0;
168
169 if (MI->isCopy() && usesRegClass(MI->getOperand(1),
170 &ARM::SPRRegClass)) {
171 SReg = MI->getOperand(1).getReg();
172 }
173
174 if (TargetRegisterInfo::isVirtualRegister(SReg)) {
175 if (MO->getSubReg() == ARM::ssub_1) return ARM::ssub_1;
176 return ARM::ssub_0;
177 }
178 return getDPRLaneFromSPR(SReg);
179}
180
181// MI is known to be dead. Figure out what instructions
182// are also made dead by this and mark them for removal.
183void A15SDOptimizer::eraseInstrWithNoUses(MachineInstr *MI) {
184 SmallVector<MachineInstr *, 8> Front;
185 DeadInstr.insert(MI);
186
187 DEBUG(dbgs() << "Deleting base instruction " << *MI << "\n");
188 Front.push_back(MI);
189
190 while (Front.size() != 0) {
191 MI = Front.back();
192 Front.pop_back();
193
194 // MI is already known to be dead. We need to see
195 // if other instructions can also be removed.
196 for (unsigned int i = 0; i < MI->getNumOperands(); ++i) {
197 MachineOperand &MO = MI->getOperand(i);
198 if ((!MO.isReg()) || (!MO.isUse()))
199 continue;
200 unsigned Reg = MO.getReg();
201 if (!TRI->isVirtualRegister(Reg))
202 continue;
203 MachineOperand *Op = MI->findRegisterDefOperand(Reg);
204
205 if (!Op)
206 continue;
207
208 MachineInstr *Def = Op->getParent();
209
210 // We don't need to do anything if we have already marked
211 // this instruction as being dead.
212 if (DeadInstr.find(Def) != DeadInstr.end())
213 continue;
214
215 // Check if all the uses of this instruction are marked as
216 // dead. If so, we can also mark this instruction as being
217 // dead.
218 bool IsDead = true;
219 for (unsigned int j = 0; j < Def->getNumOperands(); ++j) {
220 MachineOperand &MODef = Def->getOperand(j);
221 if ((!MODef.isReg()) || (!MODef.isDef()))
222 continue;
223 unsigned DefReg = MODef.getReg();
224 if (!TRI->isVirtualRegister(DefReg)) {
225 IsDead = false;
226 break;
227 }
Owen Anderson16c6bf42014-03-13 23:12:04 +0000228 for (MachineRegisterInfo::use_instr_iterator
229 II = MRI->use_instr_begin(Reg), EE = MRI->use_instr_end();
230 II != EE; ++II) {
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000231 // We don't care about self references.
232 if (&*II == Def)
233 continue;
234 if (DeadInstr.find(&*II) == DeadInstr.end()) {
235 IsDead = false;
236 break;
237 }
238 }
239 }
240
241 if (!IsDead) continue;
242
243 DEBUG(dbgs() << "Deleting instruction " << *Def << "\n");
244 DeadInstr.insert(Def);
245 }
246 }
247}
248
249// Creates the more optimized patterns and generally does all the code
250// transformations in this pass.
251unsigned A15SDOptimizer::optimizeSDPattern(MachineInstr *MI) {
252 if (MI->isCopy()) {
253 return optimizeAllLanesPattern(MI, MI->getOperand(1).getReg());
254 }
255
256 if (MI->isInsertSubreg()) {
257 unsigned DPRReg = MI->getOperand(1).getReg();
258 unsigned SPRReg = MI->getOperand(2).getReg();
259
260 if (TRI->isVirtualRegister(DPRReg) && TRI->isVirtualRegister(SPRReg)) {
261 MachineInstr *DPRMI = MRI->getVRegDef(MI->getOperand(1).getReg());
262 MachineInstr *SPRMI = MRI->getVRegDef(MI->getOperand(2).getReg());
263
264 if (DPRMI && SPRMI) {
265 // See if the first operand of this insert_subreg is IMPLICIT_DEF
266 MachineInstr *ECDef = elideCopies(DPRMI);
267 if (ECDef != 0 && ECDef->isImplicitDef()) {
268 // Another corner case - if we're inserting something that is purely
269 // a subreg copy of a DPR, just use that DPR.
270
271 MachineInstr *EC = elideCopies(SPRMI);
272 // Is it a subreg copy of ssub_0?
273 if (EC && EC->isCopy() &&
274 EC->getOperand(1).getSubReg() == ARM::ssub_0) {
275 DEBUG(dbgs() << "Found a subreg copy: " << *SPRMI);
276
277 // Find the thing we're subreg copying out of - is it of the same
278 // regclass as DPRMI? (i.e. a DPR or QPR).
279 unsigned FullReg = SPRMI->getOperand(1).getReg();
280 const TargetRegisterClass *TRC =
281 MRI->getRegClass(MI->getOperand(1).getReg());
282 if (TRC->hasSuperClassEq(MRI->getRegClass(FullReg))) {
283 DEBUG(dbgs() << "Subreg copy is compatible - returning ");
284 DEBUG(dbgs() << PrintReg(FullReg) << "\n");
285 eraseInstrWithNoUses(MI);
286 return FullReg;
287 }
288 }
289
290 return optimizeAllLanesPattern(MI, MI->getOperand(2).getReg());
291 }
292 }
293 }
294 return optimizeAllLanesPattern(MI, MI->getOperand(0).getReg());
295 }
296
297 if (MI->isRegSequence() && usesRegClass(MI->getOperand(1),
298 &ARM::SPRRegClass)) {
299 // See if all bar one of the operands are IMPLICIT_DEF and insert the
300 // optimizer pattern accordingly.
301 unsigned NumImplicit = 0, NumTotal = 0;
302 unsigned NonImplicitReg = ~0U;
303
304 for (unsigned I = 1; I < MI->getNumExplicitOperands(); ++I) {
305 if (!MI->getOperand(I).isReg())
306 continue;
307 ++NumTotal;
308 unsigned OpReg = MI->getOperand(I).getReg();
309
310 if (!TRI->isVirtualRegister(OpReg))
311 break;
312
313 MachineInstr *Def = MRI->getVRegDef(OpReg);
314 if (!Def)
315 break;
316 if (Def->isImplicitDef())
317 ++NumImplicit;
318 else
319 NonImplicitReg = MI->getOperand(I).getReg();
320 }
321
322 if (NumImplicit == NumTotal - 1)
323 return optimizeAllLanesPattern(MI, NonImplicitReg);
324 else
325 return optimizeAllLanesPattern(MI, MI->getOperand(0).getReg());
326 }
327
328 assert(0 && "Unhandled update pattern!");
329 return 0;
330}
331
332// Return true if this MachineInstr inserts a scalar (SPR) value into
333// a D or Q register.
334bool A15SDOptimizer::hasPartialWrite(MachineInstr *MI) {
335 // The only way we can do a partial register update is through a COPY,
336 // INSERT_SUBREG or REG_SEQUENCE.
337 if (MI->isCopy() && usesRegClass(MI->getOperand(1), &ARM::SPRRegClass))
338 return true;
339
340 if (MI->isInsertSubreg() && usesRegClass(MI->getOperand(2),
341 &ARM::SPRRegClass))
342 return true;
343
344 if (MI->isRegSequence() && usesRegClass(MI->getOperand(1), &ARM::SPRRegClass))
345 return true;
346
347 return false;
348}
349
350// Looks through full copies to get the instruction that defines the input
351// operand for MI.
352MachineInstr *A15SDOptimizer::elideCopies(MachineInstr *MI) {
353 if (!MI->isFullCopy())
354 return MI;
355 if (!TRI->isVirtualRegister(MI->getOperand(1).getReg()))
356 return NULL;
357 MachineInstr *Def = MRI->getVRegDef(MI->getOperand(1).getReg());
358 if (!Def)
359 return NULL;
360 return elideCopies(Def);
361}
362
363// Look through full copies and PHIs to get the set of non-copy MachineInstrs
364// that can produce MI.
365void A15SDOptimizer::elideCopiesAndPHIs(MachineInstr *MI,
366 SmallVectorImpl<MachineInstr*> &Outs) {
367 // Looking through PHIs may create loops so we need to track what
368 // instructions we have visited before.
369 std::set<MachineInstr *> Reached;
370 SmallVector<MachineInstr *, 8> Front;
371 Front.push_back(MI);
372 while (Front.size() != 0) {
373 MI = Front.back();
374 Front.pop_back();
375
376 // If we have already explored this MachineInstr, ignore it.
377 if (Reached.find(MI) != Reached.end())
378 continue;
379 Reached.insert(MI);
380 if (MI->isPHI()) {
381 for (unsigned I = 1, E = MI->getNumOperands(); I != E; I += 2) {
382 unsigned Reg = MI->getOperand(I).getReg();
383 if (!TRI->isVirtualRegister(Reg)) {
384 continue;
385 }
386 MachineInstr *NewMI = MRI->getVRegDef(Reg);
387 if (!NewMI)
388 continue;
389 Front.push_back(NewMI);
390 }
391 } else if (MI->isFullCopy()) {
392 if (!TRI->isVirtualRegister(MI->getOperand(1).getReg()))
393 continue;
394 MachineInstr *NewMI = MRI->getVRegDef(MI->getOperand(1).getReg());
395 if (!NewMI)
396 continue;
397 Front.push_back(NewMI);
398 } else {
399 DEBUG(dbgs() << "Found partial copy" << *MI <<"\n");
400 Outs.push_back(MI);
401 }
402 }
403}
404
405// Return the DPR virtual registers that are read by this machine instruction
406// (if any).
407SmallVector<unsigned, 8> A15SDOptimizer::getReadDPRs(MachineInstr *MI) {
408 if (MI->isCopyLike() || MI->isInsertSubreg() || MI->isRegSequence() ||
409 MI->isKill())
410 return SmallVector<unsigned, 8>();
411
412 SmallVector<unsigned, 8> Defs;
413 for (unsigned i = 0; i < MI->getNumOperands(); ++i) {
414 MachineOperand &MO = MI->getOperand(i);
415
416 if (!MO.isReg() || !MO.isUse())
417 continue;
418 if (!usesRegClass(MO, &ARM::DPRRegClass) &&
Hao Liu40b5ab82014-03-20 05:36:59 +0000419 !usesRegClass(MO, &ARM::QPRRegClass) &&
420 !usesRegClass(MO, &ARM::DPairRegClass)) // Treat DPair as QPR
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000421 continue;
422
423 Defs.push_back(MO.getReg());
424 }
425 return Defs;
426}
427
428// Creates a DPR register from an SPR one by using a VDUP.
429unsigned
430A15SDOptimizer::createDupLane(MachineBasicBlock &MBB,
431 MachineBasicBlock::iterator InsertBefore,
432 DebugLoc DL,
433 unsigned Reg, unsigned Lane, bool QPR) {
434 unsigned Out = MRI->createVirtualRegister(QPR ? &ARM::QPRRegClass :
435 &ARM::DPRRegClass);
436 AddDefaultPred(BuildMI(MBB,
437 InsertBefore,
438 DL,
439 TII->get(QPR ? ARM::VDUPLN32q : ARM::VDUPLN32d),
440 Out)
441 .addReg(Reg)
442 .addImm(Lane));
443
444 return Out;
445}
446
447// Creates a SPR register from a DPR by copying the value in lane 0.
448unsigned
449A15SDOptimizer::createExtractSubreg(MachineBasicBlock &MBB,
450 MachineBasicBlock::iterator InsertBefore,
451 DebugLoc DL,
452 unsigned DReg, unsigned Lane,
453 const TargetRegisterClass *TRC) {
454 unsigned Out = MRI->createVirtualRegister(TRC);
455 BuildMI(MBB,
456 InsertBefore,
457 DL,
458 TII->get(TargetOpcode::COPY), Out)
459 .addReg(DReg, 0, Lane);
460
461 return Out;
462}
463
464// Takes two SPR registers and creates a DPR by using a REG_SEQUENCE.
465unsigned
466A15SDOptimizer::createRegSequence(MachineBasicBlock &MBB,
467 MachineBasicBlock::iterator InsertBefore,
468 DebugLoc DL,
469 unsigned Reg1, unsigned Reg2) {
470 unsigned Out = MRI->createVirtualRegister(&ARM::QPRRegClass);
471 BuildMI(MBB,
472 InsertBefore,
473 DL,
474 TII->get(TargetOpcode::REG_SEQUENCE), Out)
475 .addReg(Reg1)
476 .addImm(ARM::dsub_0)
477 .addReg(Reg2)
478 .addImm(ARM::dsub_1);
479 return Out;
480}
481
482// Takes two DPR registers that have previously been VDUPed (Ssub0 and Ssub1)
483// and merges them into one DPR register.
484unsigned
485A15SDOptimizer::createVExt(MachineBasicBlock &MBB,
486 MachineBasicBlock::iterator InsertBefore,
487 DebugLoc DL,
488 unsigned Ssub0, unsigned Ssub1) {
489 unsigned Out = MRI->createVirtualRegister(&ARM::DPRRegClass);
490 AddDefaultPred(BuildMI(MBB,
491 InsertBefore,
492 DL,
493 TII->get(ARM::VEXTd32), Out)
494 .addReg(Ssub0)
495 .addReg(Ssub1)
496 .addImm(1));
497 return Out;
498}
499
500unsigned
501A15SDOptimizer::createInsertSubreg(MachineBasicBlock &MBB,
502 MachineBasicBlock::iterator InsertBefore,
503 DebugLoc DL, unsigned DReg, unsigned Lane,
504 unsigned ToInsert) {
505 unsigned Out = MRI->createVirtualRegister(&ARM::DPR_VFP2RegClass);
506 BuildMI(MBB,
507 InsertBefore,
508 DL,
509 TII->get(TargetOpcode::INSERT_SUBREG), Out)
510 .addReg(DReg)
511 .addReg(ToInsert)
512 .addImm(Lane);
513
514 return Out;
515}
516
517unsigned
518A15SDOptimizer::createImplicitDef(MachineBasicBlock &MBB,
519 MachineBasicBlock::iterator InsertBefore,
520 DebugLoc DL) {
521 unsigned Out = MRI->createVirtualRegister(&ARM::DPRRegClass);
522 BuildMI(MBB,
523 InsertBefore,
524 DL,
525 TII->get(TargetOpcode::IMPLICIT_DEF), Out);
526 return Out;
527}
528
529// This function inserts instructions in order to optimize interactions between
530// SPR registers and DPR/QPR registers. It does so by performing VDUPs on all
531// lanes, and the using VEXT instructions to recompose the result.
532unsigned
533A15SDOptimizer::optimizeAllLanesPattern(MachineInstr *MI, unsigned Reg) {
534 MachineBasicBlock::iterator InsertPt(MI);
535 DebugLoc DL = MI->getDebugLoc();
536 MachineBasicBlock &MBB = *MI->getParent();
537 InsertPt++;
538 unsigned Out;
539
Hao Liu40b5ab82014-03-20 05:36:59 +0000540 // DPair has the same length as QPR and also has two DPRs as subreg.
541 // Treat DPair as QPR.
542 if (MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::QPRRegClass) ||
543 MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::DPairRegClass)) {
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000544 unsigned DSub0 = createExtractSubreg(MBB, InsertPt, DL, Reg,
545 ARM::dsub_0, &ARM::DPRRegClass);
546 unsigned DSub1 = createExtractSubreg(MBB, InsertPt, DL, Reg,
547 ARM::dsub_1, &ARM::DPRRegClass);
548
549 unsigned Out1 = createDupLane(MBB, InsertPt, DL, DSub0, 0);
550 unsigned Out2 = createDupLane(MBB, InsertPt, DL, DSub0, 1);
551 Out = createVExt(MBB, InsertPt, DL, Out1, Out2);
552
553 unsigned Out3 = createDupLane(MBB, InsertPt, DL, DSub1, 0);
554 unsigned Out4 = createDupLane(MBB, InsertPt, DL, DSub1, 1);
555 Out2 = createVExt(MBB, InsertPt, DL, Out3, Out4);
556
557 Out = createRegSequence(MBB, InsertPt, DL, Out, Out2);
558
559 } else if (MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::DPRRegClass)) {
560 unsigned Out1 = createDupLane(MBB, InsertPt, DL, Reg, 0);
561 unsigned Out2 = createDupLane(MBB, InsertPt, DL, Reg, 1);
562 Out = createVExt(MBB, InsertPt, DL, Out1, Out2);
563
564 } else {
565 assert(MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::SPRRegClass) &&
566 "Found unexpected regclass!");
567
568 unsigned PrefLane = getPrefSPRLane(Reg);
569 unsigned Lane;
570 switch (PrefLane) {
571 case ARM::ssub_0: Lane = 0; break;
572 case ARM::ssub_1: Lane = 1; break;
573 default: llvm_unreachable("Unknown preferred lane!");
574 }
575
Hao Liu40b5ab82014-03-20 05:36:59 +0000576 // Treat DPair as QPR
577 bool UsesQPR = usesRegClass(MI->getOperand(0), &ARM::QPRRegClass) ||
578 usesRegClass(MI->getOperand(0), &ARM::DPairRegClass);
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000579
580 Out = createImplicitDef(MBB, InsertPt, DL);
581 Out = createInsertSubreg(MBB, InsertPt, DL, Out, PrefLane, Reg);
582 Out = createDupLane(MBB, InsertPt, DL, Out, Lane, UsesQPR);
583 eraseInstrWithNoUses(MI);
584 }
585 return Out;
586}
587
588bool A15SDOptimizer::runOnInstruction(MachineInstr *MI) {
589 // We look for instructions that write S registers that are then read as
590 // D/Q registers. These can only be caused by COPY, INSERT_SUBREG and
591 // REG_SEQUENCE pseudos that insert an SPR value into a DPR register or
592 // merge two SPR values to form a DPR register. In order avoid false
593 // positives we make sure that there is an SPR producer so we look past
594 // COPY and PHI nodes to find it.
595 //
596 // The best code pattern for when an SPR producer is going to be used by a
597 // DPR or QPR consumer depends on whether the other lanes of the
598 // corresponding DPR/QPR are currently defined.
599 //
600 // We can handle these efficiently, depending on the type of
601 // pseudo-instruction that is producing the pattern
602 //
603 // * COPY: * VDUP all lanes and merge the results together
604 // using VEXTs.
605 //
606 // * INSERT_SUBREG: * If the SPR value was originally in another DPR/QPR
607 // lane, and the other lane(s) of the DPR/QPR register
608 // that we are inserting in are undefined, use the
609 // original DPR/QPR value.
610 // * Otherwise, fall back on the same stategy as COPY.
611 //
612 // * REG_SEQUENCE: * If all except one of the input operands are
613 // IMPLICIT_DEFs, insert the VDUP pattern for just the
614 // defined input operand
615 // * Otherwise, fall back on the same stategy as COPY.
616 //
617
618 // First, get all the reads of D-registers done by this instruction.
619 SmallVector<unsigned, 8> Defs = getReadDPRs(MI);
620 bool Modified = false;
621
Craig Topper31ee5862013-07-03 15:07:05 +0000622 for (SmallVectorImpl<unsigned>::iterator I = Defs.begin(), E = Defs.end();
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000623 I != E; ++I) {
624 // Follow the def-use chain for this DPR through COPYs, and also through
625 // PHIs (which are essentially multi-way COPYs). It is because of PHIs that
626 // we can end up with multiple defs of this DPR.
627
628 SmallVector<MachineInstr *, 8> DefSrcs;
629 if (!TRI->isVirtualRegister(*I))
630 continue;
631 MachineInstr *Def = MRI->getVRegDef(*I);
632 if (!Def)
633 continue;
634
635 elideCopiesAndPHIs(Def, DefSrcs);
636
Craig Topper31ee5862013-07-03 15:07:05 +0000637 for (SmallVectorImpl<MachineInstr *>::iterator II = DefSrcs.begin(),
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000638 EE = DefSrcs.end(); II != EE; ++II) {
639 MachineInstr *MI = *II;
640
641 // If we've already analyzed and replaced this operand, don't do
642 // anything.
643 if (Replacements.find(MI) != Replacements.end())
644 continue;
645
646 // Now, work out if the instruction causes a SPR->DPR dependency.
647 if (!hasPartialWrite(MI))
648 continue;
649
650 // Collect all the uses of this MI's DPR def for updating later.
651 SmallVector<MachineOperand*, 8> Uses;
652 unsigned DPRDefReg = MI->getOperand(0).getReg();
653 for (MachineRegisterInfo::use_iterator I = MRI->use_begin(DPRDefReg),
654 E = MRI->use_end(); I != E; ++I)
Owen Anderson16c6bf42014-03-13 23:12:04 +0000655 Uses.push_back(&*I);
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000656
657 // We can optimize this.
658 unsigned NewReg = optimizeSDPattern(MI);
659
660 if (NewReg != 0) {
661 Modified = true;
Craig Topper31ee5862013-07-03 15:07:05 +0000662 for (SmallVectorImpl<MachineOperand *>::const_iterator I = Uses.begin(),
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000663 E = Uses.end(); I != E; ++I) {
Jim Grosbach13654dd2013-09-04 19:08:44 +0000664 // Make sure to constrain the register class of the new register to
665 // match what we're replacing. Otherwise we can optimize a DPR_VFP2
666 // reference into a plain DPR, and that will end poorly. NewReg is
667 // always virtual here, so there will always be a matching subclass
668 // to find.
669 MRI->constrainRegClass(NewReg, MRI->getRegClass((*I)->getReg()));
670
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000671 DEBUG(dbgs() << "Replacing operand "
672 << **I << " with "
673 << PrintReg(NewReg) << "\n");
674 (*I)->substVirtReg(NewReg, 0, *TRI);
675 }
676 }
677 Replacements[MI] = NewReg;
678 }
679 }
680 return Modified;
681}
682
683bool A15SDOptimizer::runOnMachineFunction(MachineFunction &Fn) {
684 TII = static_cast<const ARMBaseInstrInfo*>(Fn.getTarget().getInstrInfo());
685 TRI = Fn.getTarget().getRegisterInfo();
686 MRI = &Fn.getRegInfo();
687 bool Modified = false;
688
689 DEBUG(dbgs() << "Running on function " << Fn.getName()<< "\n");
690
691 DeadInstr.clear();
692 Replacements.clear();
693
694 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
695 ++MFI) {
696
697 for (MachineBasicBlock::iterator MI = MFI->begin(), ME = MFI->end();
698 MI != ME;) {
699 Modified |= runOnInstruction(MI++);
700 }
701
702 }
703
704 for (std::set<MachineInstr *>::iterator I = DeadInstr.begin(),
705 E = DeadInstr.end();
706 I != E; ++I) {
707 (*I)->eraseFromParent();
708 }
709
710 return Modified;
711}
712
713FunctionPass *llvm::createA15SDOptimizerPass() {
714 return new A15SDOptimizer();
715}