blob: f8a86a70c0771a0043e0ad9060ca578d98344097 [file] [log] [blame]
Silviu Baranga82dd6ac2013-03-15 18:28:25 +00001//=== A15SDOptimizerPass.cpp - Optimize DPR and SPR register accesses on A15==//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Silviu Baranga82dd6ac2013-03-15 18:28:25 +00006//
7//===----------------------------------------------------------------------===//
8//
9// The Cortex-A15 processor employs a tracking scheme in its register renaming
10// in order to process each instruction's micro-ops speculatively and
11// out-of-order with appropriate forwarding. The ARM architecture allows VFP
12// instructions to read and write 32-bit S-registers. Each S-register
13// corresponds to one half (upper or lower) of an overlaid 64-bit D-register.
14//
15// There are several instruction patterns which can be used to provide this
16// capability which can provide higher performance than other, potentially more
17// direct patterns, specifically around when one micro-op reads a D-register
18// operand that has recently been written as one or more S-register results.
19//
20// This file defines a pre-regalloc pass which looks for SPR producers which
21// are going to be used by a DPR (or QPR) consumers and creates the more
22// optimized access pattern.
23//
24//===----------------------------------------------------------------------===//
25
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000026#include "ARM.h"
27#include "ARMBaseInstrInfo.h"
Craig Toppera9253262014-03-22 23:51:00 +000028#include "ARMBaseRegisterInfo.h"
Eric Christopher63b44882015-03-05 00:23:40 +000029#include "ARMSubtarget.h"
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000030#include "llvm/ADT/Statistic.h"
Eric Christopher63b44882015-03-05 00:23:40 +000031#include "llvm/CodeGen/MachineFunction.h"
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000032#include "llvm/CodeGen/MachineFunctionPass.h"
33#include "llvm/CodeGen/MachineInstr.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
35#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000036#include "llvm/CodeGen/TargetRegisterInfo.h"
37#include "llvm/CodeGen/TargetSubtargetInfo.h"
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000038#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000039#include "llvm/Support/raw_ostream.h"
Eric Christopher4c67d5a2014-10-14 01:13:51 +000040#include <map>
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000041#include <set>
42
43using namespace llvm;
44
Chandler Carruth84e68b22014-04-22 02:41:26 +000045#define DEBUG_TYPE "a15-sd-optimizer"
46
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000047namespace {
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
Mehdi Amini117296c2016-10-01 02:56:57 +000054 StringRef getPassName() const override { return "ARM A15 S->D optimizer"; }
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000055
56 private:
57 const ARMBaseInstrInfo *TII;
58 const TargetRegisterInfo *TRI;
59 MachineRegisterInfo *MRI;
60
61 bool runOnInstruction(MachineInstr *MI);
62
63 //
64 // Instruction builder helpers
65 //
66 unsigned createDupLane(MachineBasicBlock &MBB,
67 MachineBasicBlock::iterator InsertBefore,
Benjamin Kramerbdc49562016-06-12 15:39:02 +000068 const DebugLoc &DL, unsigned Reg, unsigned Lane,
69 bool QPR = false);
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000070
71 unsigned createExtractSubreg(MachineBasicBlock &MBB,
72 MachineBasicBlock::iterator InsertBefore,
Benjamin Kramerbdc49562016-06-12 15:39:02 +000073 const DebugLoc &DL, unsigned DReg,
74 unsigned Lane, const TargetRegisterClass *TRC);
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000075
76 unsigned createVExt(MachineBasicBlock &MBB,
77 MachineBasicBlock::iterator InsertBefore,
Benjamin Kramerbdc49562016-06-12 15:39:02 +000078 const DebugLoc &DL, unsigned Ssub0, unsigned Ssub1);
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000079
80 unsigned createRegSequence(MachineBasicBlock &MBB,
81 MachineBasicBlock::iterator InsertBefore,
Benjamin Kramerbdc49562016-06-12 15:39:02 +000082 const DebugLoc &DL, unsigned Reg1,
83 unsigned Reg2);
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000084
85 unsigned createInsertSubreg(MachineBasicBlock &MBB,
86 MachineBasicBlock::iterator InsertBefore,
Benjamin Kramerbdc49562016-06-12 15:39:02 +000087 const DebugLoc &DL, unsigned DReg,
88 unsigned Lane, unsigned ToInsert);
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000089
90 unsigned createImplicitDef(MachineBasicBlock &MBB,
91 MachineBasicBlock::iterator InsertBefore,
Benjamin Kramerbdc49562016-06-12 15:39:02 +000092 const DebugLoc &DL);
Jim Grosbach1a597112014-04-03 23:43:18 +000093
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000094 //
95 // Various property checkers
96 //
97 bool usesRegClass(MachineOperand &MO, const TargetRegisterClass *TRC);
98 bool hasPartialWrite(MachineInstr *MI);
99 SmallVector<unsigned, 8> getReadDPRs(MachineInstr *MI);
100 unsigned getDPRLaneFromSPR(unsigned SReg);
101
102 //
103 // Methods used for getting the definitions of partial registers
104 //
105
106 MachineInstr *elideCopies(MachineInstr *MI);
107 void elideCopiesAndPHIs(MachineInstr *MI,
108 SmallVectorImpl<MachineInstr*> &Outs);
109
110 //
111 // Pattern optimization methods
112 //
113 unsigned optimizeAllLanesPattern(MachineInstr *MI, unsigned Reg);
114 unsigned optimizeSDPattern(MachineInstr *MI);
115 unsigned getPrefSPRLane(unsigned SReg);
116
117 //
118 // Sanitizing method - used to make sure if don't leave dead code around.
119 //
120 void eraseInstrWithNoUses(MachineInstr *MI);
121
122 //
123 // A map used to track the changes done by this pass.
124 //
125 std::map<MachineInstr*, unsigned> Replacements;
126 std::set<MachineInstr *> DeadInstr;
127 };
128 char A15SDOptimizer::ID = 0;
129} // end anonymous namespace
130
131// Returns true if this is a use of a SPR register.
132bool A15SDOptimizer::usesRegClass(MachineOperand &MO,
133 const TargetRegisterClass *TRC) {
134 if (!MO.isReg())
135 return false;
Daniel Sanders0c476112019-08-15 19:22:08 +0000136 Register Reg = MO.getReg();
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000137
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000138 if (Register::isVirtualRegister(Reg))
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000139 return MRI->getRegClass(Reg)->hasSuperClassEq(TRC);
140 else
141 return TRC->contains(Reg);
142}
143
144unsigned A15SDOptimizer::getDPRLaneFromSPR(unsigned SReg) {
145 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1,
146 &ARM::DPRRegClass);
147 if (DReg != ARM::NoRegister) return ARM::ssub_1;
148 return ARM::ssub_0;
149}
150
151// Get the subreg type that is most likely to be coalesced
152// for an SPR register that will be used in VDUP32d pseudo.
153unsigned A15SDOptimizer::getPrefSPRLane(unsigned SReg) {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000154 if (!Register::isVirtualRegister(SReg))
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000155 return getDPRLaneFromSPR(SReg);
156
157 MachineInstr *MI = MRI->getVRegDef(SReg);
158 if (!MI) return ARM::ssub_0;
159 MachineOperand *MO = MI->findRegisterDefOperand(SReg);
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000160 if (!MO) return ARM::ssub_0;
Simon Pilgrimd397e292019-11-02 21:49:12 +0000161 assert(MO->isReg() && "Non-register operand found!");
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000162
163 if (MI->isCopy() && usesRegClass(MI->getOperand(1),
164 &ARM::SPRRegClass)) {
165 SReg = MI->getOperand(1).getReg();
166 }
167
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000168 if (Register::isVirtualRegister(SReg)) {
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000169 if (MO->getSubReg() == ARM::ssub_1) return ARM::ssub_1;
170 return ARM::ssub_0;
171 }
172 return getDPRLaneFromSPR(SReg);
173}
174
175// MI is known to be dead. Figure out what instructions
176// are also made dead by this and mark them for removal.
177void A15SDOptimizer::eraseInstrWithNoUses(MachineInstr *MI) {
178 SmallVector<MachineInstr *, 8> Front;
179 DeadInstr.insert(MI);
180
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000181 LLVM_DEBUG(dbgs() << "Deleting base instruction " << *MI << "\n");
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000182 Front.push_back(MI);
183
184 while (Front.size() != 0) {
185 MI = Front.back();
186 Front.pop_back();
187
188 // MI is already known to be dead. We need to see
189 // if other instructions can also be removed.
Javed Absar37b22862017-08-14 01:38:01 +0000190 for (MachineOperand &MO : MI->operands()) {
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000191 if ((!MO.isReg()) || (!MO.isUse()))
192 continue;
Daniel Sanders0c476112019-08-15 19:22:08 +0000193 Register Reg = MO.getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000194 if (!Register::isVirtualRegister(Reg))
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000195 continue;
196 MachineOperand *Op = MI->findRegisterDefOperand(Reg);
197
198 if (!Op)
199 continue;
200
201 MachineInstr *Def = Op->getParent();
202
203 // We don't need to do anything if we have already marked
204 // this instruction as being dead.
205 if (DeadInstr.find(Def) != DeadInstr.end())
206 continue;
207
208 // Check if all the uses of this instruction are marked as
209 // dead. If so, we can also mark this instruction as being
210 // dead.
211 bool IsDead = true;
Javed Absar37b22862017-08-14 01:38:01 +0000212 for (MachineOperand &MODef : Def->operands()) {
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000213 if ((!MODef.isReg()) || (!MODef.isDef()))
214 continue;
Daniel Sanders0c476112019-08-15 19:22:08 +0000215 Register DefReg = MODef.getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000216 if (!Register::isVirtualRegister(DefReg)) {
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000217 IsDead = false;
218 break;
219 }
Javed Absar37b22862017-08-14 01:38:01 +0000220 for (MachineInstr &Use : MRI->use_instructions(Reg)) {
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000221 // We don't care about self references.
Javed Absar37b22862017-08-14 01:38:01 +0000222 if (&Use == Def)
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000223 continue;
Javed Absar37b22862017-08-14 01:38:01 +0000224 if (DeadInstr.find(&Use) == DeadInstr.end()) {
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000225 IsDead = false;
226 break;
227 }
228 }
229 }
230
231 if (!IsDead) continue;
232
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000233 LLVM_DEBUG(dbgs() << "Deleting instruction " << *Def << "\n");
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000234 DeadInstr.insert(Def);
235 }
236 }
237}
238
239// Creates the more optimized patterns and generally does all the code
240// transformations in this pass.
241unsigned A15SDOptimizer::optimizeSDPattern(MachineInstr *MI) {
242 if (MI->isCopy()) {
243 return optimizeAllLanesPattern(MI, MI->getOperand(1).getReg());
244 }
245
246 if (MI->isInsertSubreg()) {
Daniel Sanders0c476112019-08-15 19:22:08 +0000247 Register DPRReg = MI->getOperand(1).getReg();
248 Register SPRReg = MI->getOperand(2).getReg();
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000249
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000250 if (Register::isVirtualRegister(DPRReg) && Register::isVirtualRegister(SPRReg)) {
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000251 MachineInstr *DPRMI = MRI->getVRegDef(MI->getOperand(1).getReg());
252 MachineInstr *SPRMI = MRI->getVRegDef(MI->getOperand(2).getReg());
253
254 if (DPRMI && SPRMI) {
255 // See if the first operand of this insert_subreg is IMPLICIT_DEF
256 MachineInstr *ECDef = elideCopies(DPRMI);
Craig Topper062a2ba2014-04-25 05:30:21 +0000257 if (ECDef && ECDef->isImplicitDef()) {
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000258 // Another corner case - if we're inserting something that is purely
259 // a subreg copy of a DPR, just use that DPR.
260
261 MachineInstr *EC = elideCopies(SPRMI);
262 // Is it a subreg copy of ssub_0?
263 if (EC && EC->isCopy() &&
264 EC->getOperand(1).getSubReg() == ARM::ssub_0) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000265 LLVM_DEBUG(dbgs() << "Found a subreg copy: " << *SPRMI);
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000266
267 // Find the thing we're subreg copying out of - is it of the same
268 // regclass as DPRMI? (i.e. a DPR or QPR).
Daniel Sanders0c476112019-08-15 19:22:08 +0000269 Register FullReg = SPRMI->getOperand(1).getReg();
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000270 const TargetRegisterClass *TRC =
271 MRI->getRegClass(MI->getOperand(1).getReg());
272 if (TRC->hasSuperClassEq(MRI->getRegClass(FullReg))) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000273 LLVM_DEBUG(dbgs() << "Subreg copy is compatible - returning ");
274 LLVM_DEBUG(dbgs() << printReg(FullReg) << "\n");
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000275 eraseInstrWithNoUses(MI);
276 return FullReg;
277 }
278 }
279
280 return optimizeAllLanesPattern(MI, MI->getOperand(2).getReg());
281 }
282 }
283 }
284 return optimizeAllLanesPattern(MI, MI->getOperand(0).getReg());
285 }
286
287 if (MI->isRegSequence() && usesRegClass(MI->getOperand(1),
288 &ARM::SPRRegClass)) {
289 // See if all bar one of the operands are IMPLICIT_DEF and insert the
290 // optimizer pattern accordingly.
291 unsigned NumImplicit = 0, NumTotal = 0;
292 unsigned NonImplicitReg = ~0U;
293
294 for (unsigned I = 1; I < MI->getNumExplicitOperands(); ++I) {
295 if (!MI->getOperand(I).isReg())
296 continue;
297 ++NumTotal;
Daniel Sanders0c476112019-08-15 19:22:08 +0000298 Register OpReg = MI->getOperand(I).getReg();
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000299
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000300 if (!Register::isVirtualRegister(OpReg))
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000301 break;
302
303 MachineInstr *Def = MRI->getVRegDef(OpReg);
304 if (!Def)
305 break;
306 if (Def->isImplicitDef())
307 ++NumImplicit;
308 else
309 NonImplicitReg = MI->getOperand(I).getReg();
310 }
311
312 if (NumImplicit == NumTotal - 1)
313 return optimizeAllLanesPattern(MI, NonImplicitReg);
314 else
315 return optimizeAllLanesPattern(MI, MI->getOperand(0).getReg());
316 }
317
Craig Topper35b2f752014-06-19 06:10:58 +0000318 llvm_unreachable("Unhandled update pattern!");
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000319}
320
321// Return true if this MachineInstr inserts a scalar (SPR) value into
322// a D or Q register.
323bool A15SDOptimizer::hasPartialWrite(MachineInstr *MI) {
324 // The only way we can do a partial register update is through a COPY,
325 // INSERT_SUBREG or REG_SEQUENCE.
326 if (MI->isCopy() && usesRegClass(MI->getOperand(1), &ARM::SPRRegClass))
327 return true;
328
329 if (MI->isInsertSubreg() && usesRegClass(MI->getOperand(2),
330 &ARM::SPRRegClass))
331 return true;
332
333 if (MI->isRegSequence() && usesRegClass(MI->getOperand(1), &ARM::SPRRegClass))
334 return true;
335
336 return false;
337}
338
339// Looks through full copies to get the instruction that defines the input
340// operand for MI.
341MachineInstr *A15SDOptimizer::elideCopies(MachineInstr *MI) {
342 if (!MI->isFullCopy())
343 return MI;
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000344 if (!Register::isVirtualRegister(MI->getOperand(1).getReg()))
Craig Topper062a2ba2014-04-25 05:30:21 +0000345 return nullptr;
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000346 MachineInstr *Def = MRI->getVRegDef(MI->getOperand(1).getReg());
347 if (!Def)
Craig Topper062a2ba2014-04-25 05:30:21 +0000348 return nullptr;
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000349 return elideCopies(Def);
350}
351
352// Look through full copies and PHIs to get the set of non-copy MachineInstrs
353// that can produce MI.
354void A15SDOptimizer::elideCopiesAndPHIs(MachineInstr *MI,
355 SmallVectorImpl<MachineInstr*> &Outs) {
356 // Looking through PHIs may create loops so we need to track what
357 // instructions we have visited before.
358 std::set<MachineInstr *> Reached;
359 SmallVector<MachineInstr *, 8> Front;
360 Front.push_back(MI);
361 while (Front.size() != 0) {
362 MI = Front.back();
363 Front.pop_back();
364
365 // If we have already explored this MachineInstr, ignore it.
366 if (Reached.find(MI) != Reached.end())
367 continue;
368 Reached.insert(MI);
369 if (MI->isPHI()) {
370 for (unsigned I = 1, E = MI->getNumOperands(); I != E; I += 2) {
Daniel Sanders0c476112019-08-15 19:22:08 +0000371 Register Reg = MI->getOperand(I).getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000372 if (!Register::isVirtualRegister(Reg)) {
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000373 continue;
374 }
375 MachineInstr *NewMI = MRI->getVRegDef(Reg);
376 if (!NewMI)
377 continue;
378 Front.push_back(NewMI);
379 }
380 } else if (MI->isFullCopy()) {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000381 if (!Register::isVirtualRegister(MI->getOperand(1).getReg()))
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000382 continue;
383 MachineInstr *NewMI = MRI->getVRegDef(MI->getOperand(1).getReg());
384 if (!NewMI)
385 continue;
386 Front.push_back(NewMI);
387 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000388 LLVM_DEBUG(dbgs() << "Found partial copy" << *MI << "\n");
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000389 Outs.push_back(MI);
390 }
391 }
392}
393
394// Return the DPR virtual registers that are read by this machine instruction
395// (if any).
396SmallVector<unsigned, 8> A15SDOptimizer::getReadDPRs(MachineInstr *MI) {
397 if (MI->isCopyLike() || MI->isInsertSubreg() || MI->isRegSequence() ||
398 MI->isKill())
399 return SmallVector<unsigned, 8>();
400
401 SmallVector<unsigned, 8> Defs;
Javed Absar37b22862017-08-14 01:38:01 +0000402 for (MachineOperand &MO : MI->operands()) {
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000403 if (!MO.isReg() || !MO.isUse())
404 continue;
405 if (!usesRegClass(MO, &ARM::DPRRegClass) &&
Hao Liu40b5ab82014-03-20 05:36:59 +0000406 !usesRegClass(MO, &ARM::QPRRegClass) &&
407 !usesRegClass(MO, &ARM::DPairRegClass)) // Treat DPair as QPR
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000408 continue;
409
410 Defs.push_back(MO.getReg());
411 }
412 return Defs;
413}
414
415// Creates a DPR register from an SPR one by using a VDUP.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000416unsigned A15SDOptimizer::createDupLane(MachineBasicBlock &MBB,
417 MachineBasicBlock::iterator InsertBefore,
418 const DebugLoc &DL, unsigned Reg,
419 unsigned Lane, bool QPR) {
Daniel Sanders0c476112019-08-15 19:22:08 +0000420 Register Out =
421 MRI->createVirtualRegister(QPR ? &ARM::QPRRegClass : &ARM::DPRRegClass);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000422 BuildMI(MBB, InsertBefore, DL,
423 TII->get(QPR ? ARM::VDUPLN32q : ARM::VDUPLN32d), Out)
424 .addReg(Reg)
425 .addImm(Lane)
426 .add(predOps(ARMCC::AL));
Jim Grosbach1a597112014-04-03 23:43:18 +0000427
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000428 return Out;
429}
430
431// Creates a SPR register from a DPR by copying the value in lane 0.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000432unsigned A15SDOptimizer::createExtractSubreg(
433 MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
434 const DebugLoc &DL, unsigned DReg, unsigned Lane,
435 const TargetRegisterClass *TRC) {
Daniel Sanders0c476112019-08-15 19:22:08 +0000436 Register Out = MRI->createVirtualRegister(TRC);
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000437 BuildMI(MBB,
438 InsertBefore,
439 DL,
440 TII->get(TargetOpcode::COPY), Out)
441 .addReg(DReg, 0, Lane);
442
443 return Out;
444}
445
446// Takes two SPR registers and creates a DPR by using a REG_SEQUENCE.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000447unsigned A15SDOptimizer::createRegSequence(
448 MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
449 const DebugLoc &DL, unsigned Reg1, unsigned Reg2) {
Daniel Sanders0c476112019-08-15 19:22:08 +0000450 Register Out = MRI->createVirtualRegister(&ARM::QPRRegClass);
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000451 BuildMI(MBB,
452 InsertBefore,
453 DL,
454 TII->get(TargetOpcode::REG_SEQUENCE), Out)
455 .addReg(Reg1)
456 .addImm(ARM::dsub_0)
457 .addReg(Reg2)
458 .addImm(ARM::dsub_1);
459 return Out;
460}
461
462// Takes two DPR registers that have previously been VDUPed (Ssub0 and Ssub1)
463// and merges them into one DPR register.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000464unsigned A15SDOptimizer::createVExt(MachineBasicBlock &MBB,
465 MachineBasicBlock::iterator InsertBefore,
466 const DebugLoc &DL, unsigned Ssub0,
467 unsigned Ssub1) {
Daniel Sanders0c476112019-08-15 19:22:08 +0000468 Register Out = MRI->createVirtualRegister(&ARM::DPRRegClass);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000469 BuildMI(MBB, InsertBefore, DL, TII->get(ARM::VEXTd32), Out)
470 .addReg(Ssub0)
471 .addReg(Ssub1)
472 .addImm(1)
473 .add(predOps(ARMCC::AL));
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000474 return Out;
475}
476
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000477unsigned A15SDOptimizer::createInsertSubreg(
478 MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
479 const DebugLoc &DL, unsigned DReg, unsigned Lane, unsigned ToInsert) {
Daniel Sanders0c476112019-08-15 19:22:08 +0000480 Register Out = MRI->createVirtualRegister(&ARM::DPR_VFP2RegClass);
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000481 BuildMI(MBB,
482 InsertBefore,
483 DL,
484 TII->get(TargetOpcode::INSERT_SUBREG), Out)
485 .addReg(DReg)
486 .addReg(ToInsert)
487 .addImm(Lane);
488
489 return Out;
490}
491
492unsigned
493A15SDOptimizer::createImplicitDef(MachineBasicBlock &MBB,
494 MachineBasicBlock::iterator InsertBefore,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000495 const DebugLoc &DL) {
Daniel Sanders0c476112019-08-15 19:22:08 +0000496 Register Out = MRI->createVirtualRegister(&ARM::DPRRegClass);
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000497 BuildMI(MBB,
498 InsertBefore,
499 DL,
500 TII->get(TargetOpcode::IMPLICIT_DEF), Out);
501 return Out;
502}
503
504// This function inserts instructions in order to optimize interactions between
505// SPR registers and DPR/QPR registers. It does so by performing VDUPs on all
506// lanes, and the using VEXT instructions to recompose the result.
507unsigned
508A15SDOptimizer::optimizeAllLanesPattern(MachineInstr *MI, unsigned Reg) {
509 MachineBasicBlock::iterator InsertPt(MI);
510 DebugLoc DL = MI->getDebugLoc();
511 MachineBasicBlock &MBB = *MI->getParent();
512 InsertPt++;
513 unsigned Out;
514
Hao Liu40b5ab82014-03-20 05:36:59 +0000515 // DPair has the same length as QPR and also has two DPRs as subreg.
516 // Treat DPair as QPR.
517 if (MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::QPRRegClass) ||
518 MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::DPairRegClass)) {
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000519 unsigned DSub0 = createExtractSubreg(MBB, InsertPt, DL, Reg,
520 ARM::dsub_0, &ARM::DPRRegClass);
521 unsigned DSub1 = createExtractSubreg(MBB, InsertPt, DL, Reg,
522 ARM::dsub_1, &ARM::DPRRegClass);
523
524 unsigned Out1 = createDupLane(MBB, InsertPt, DL, DSub0, 0);
525 unsigned Out2 = createDupLane(MBB, InsertPt, DL, DSub0, 1);
526 Out = createVExt(MBB, InsertPt, DL, Out1, Out2);
527
528 unsigned Out3 = createDupLane(MBB, InsertPt, DL, DSub1, 0);
529 unsigned Out4 = createDupLane(MBB, InsertPt, DL, DSub1, 1);
530 Out2 = createVExt(MBB, InsertPt, DL, Out3, Out4);
531
532 Out = createRegSequence(MBB, InsertPt, DL, Out, Out2);
533
534 } else if (MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::DPRRegClass)) {
535 unsigned Out1 = createDupLane(MBB, InsertPt, DL, Reg, 0);
536 unsigned Out2 = createDupLane(MBB, InsertPt, DL, Reg, 1);
537 Out = createVExt(MBB, InsertPt, DL, Out1, Out2);
538
539 } else {
540 assert(MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::SPRRegClass) &&
541 "Found unexpected regclass!");
542
543 unsigned PrefLane = getPrefSPRLane(Reg);
544 unsigned Lane;
545 switch (PrefLane) {
546 case ARM::ssub_0: Lane = 0; break;
547 case ARM::ssub_1: Lane = 1; break;
548 default: llvm_unreachable("Unknown preferred lane!");
549 }
550
Hao Liu40b5ab82014-03-20 05:36:59 +0000551 // Treat DPair as QPR
552 bool UsesQPR = usesRegClass(MI->getOperand(0), &ARM::QPRRegClass) ||
553 usesRegClass(MI->getOperand(0), &ARM::DPairRegClass);
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000554
555 Out = createImplicitDef(MBB, InsertPt, DL);
556 Out = createInsertSubreg(MBB, InsertPt, DL, Out, PrefLane, Reg);
557 Out = createDupLane(MBB, InsertPt, DL, Out, Lane, UsesQPR);
558 eraseInstrWithNoUses(MI);
559 }
560 return Out;
561}
562
563bool A15SDOptimizer::runOnInstruction(MachineInstr *MI) {
564 // We look for instructions that write S registers that are then read as
565 // D/Q registers. These can only be caused by COPY, INSERT_SUBREG and
566 // REG_SEQUENCE pseudos that insert an SPR value into a DPR register or
567 // merge two SPR values to form a DPR register. In order avoid false
568 // positives we make sure that there is an SPR producer so we look past
569 // COPY and PHI nodes to find it.
570 //
571 // The best code pattern for when an SPR producer is going to be used by a
572 // DPR or QPR consumer depends on whether the other lanes of the
573 // corresponding DPR/QPR are currently defined.
574 //
575 // We can handle these efficiently, depending on the type of
576 // pseudo-instruction that is producing the pattern
577 //
578 // * COPY: * VDUP all lanes and merge the results together
579 // using VEXTs.
580 //
581 // * INSERT_SUBREG: * If the SPR value was originally in another DPR/QPR
582 // lane, and the other lane(s) of the DPR/QPR register
583 // that we are inserting in are undefined, use the
Jim Grosbach1a597112014-04-03 23:43:18 +0000584 // original DPR/QPR value.
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000585 // * Otherwise, fall back on the same stategy as COPY.
586 //
587 // * REG_SEQUENCE: * If all except one of the input operands are
588 // IMPLICIT_DEFs, insert the VDUP pattern for just the
589 // defined input operand
590 // * Otherwise, fall back on the same stategy as COPY.
591 //
592
593 // First, get all the reads of D-registers done by this instruction.
594 SmallVector<unsigned, 8> Defs = getReadDPRs(MI);
595 bool Modified = false;
596
Craig Topper31ee5862013-07-03 15:07:05 +0000597 for (SmallVectorImpl<unsigned>::iterator I = Defs.begin(), E = Defs.end();
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000598 I != E; ++I) {
599 // Follow the def-use chain for this DPR through COPYs, and also through
600 // PHIs (which are essentially multi-way COPYs). It is because of PHIs that
601 // we can end up with multiple defs of this DPR.
602
603 SmallVector<MachineInstr *, 8> DefSrcs;
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000604 if (!Register::isVirtualRegister(*I))
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000605 continue;
606 MachineInstr *Def = MRI->getVRegDef(*I);
607 if (!Def)
608 continue;
609
610 elideCopiesAndPHIs(Def, DefSrcs);
611
Javed Absar37b22862017-08-14 01:38:01 +0000612 for (MachineInstr *MI : DefSrcs) {
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000613 // If we've already analyzed and replaced this operand, don't do
614 // anything.
615 if (Replacements.find(MI) != Replacements.end())
616 continue;
617
618 // Now, work out if the instruction causes a SPR->DPR dependency.
619 if (!hasPartialWrite(MI))
620 continue;
621
622 // Collect all the uses of this MI's DPR def for updating later.
623 SmallVector<MachineOperand*, 8> Uses;
Daniel Sanders0c476112019-08-15 19:22:08 +0000624 Register DPRDefReg = MI->getOperand(0).getReg();
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000625 for (MachineRegisterInfo::use_iterator I = MRI->use_begin(DPRDefReg),
626 E = MRI->use_end(); I != E; ++I)
Owen Anderson16c6bf42014-03-13 23:12:04 +0000627 Uses.push_back(&*I);
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000628
629 // We can optimize this.
630 unsigned NewReg = optimizeSDPattern(MI);
631
632 if (NewReg != 0) {
633 Modified = true;
Craig Topper31ee5862013-07-03 15:07:05 +0000634 for (SmallVectorImpl<MachineOperand *>::const_iterator I = Uses.begin(),
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000635 E = Uses.end(); I != E; ++I) {
Jim Grosbach13654dd2013-09-04 19:08:44 +0000636 // Make sure to constrain the register class of the new register to
637 // match what we're replacing. Otherwise we can optimize a DPR_VFP2
638 // reference into a plain DPR, and that will end poorly. NewReg is
639 // always virtual here, so there will always be a matching subclass
640 // to find.
641 MRI->constrainRegClass(NewReg, MRI->getRegClass((*I)->getReg()));
642
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000643 LLVM_DEBUG(dbgs() << "Replacing operand " << **I << " with "
644 << printReg(NewReg) << "\n");
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000645 (*I)->substVirtReg(NewReg, 0, *TRI);
646 }
647 }
648 Replacements[MI] = NewReg;
649 }
650 }
651 return Modified;
652}
653
654bool A15SDOptimizer::runOnMachineFunction(MachineFunction &Fn) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000655 if (skipFunction(Fn.getFunction()))
Andrew Kaylora2b91112016-04-25 22:01:04 +0000656 return false;
657
Eric Christopher63b44882015-03-05 00:23:40 +0000658 const ARMSubtarget &STI = Fn.getSubtarget<ARMSubtarget>();
659 // Since the A15SDOptimizer pass can insert VDUP instructions, it can only be
660 // enabled when NEON is available.
Evandro Menezesfffa9b52018-07-20 16:49:28 +0000661 if (!(STI.useSplatVFPToNeon() && STI.hasNEON()))
Eric Christopher63b44882015-03-05 00:23:40 +0000662 return false;
Evandro Menezesfffa9b52018-07-20 16:49:28 +0000663
Eric Christopher63b44882015-03-05 00:23:40 +0000664 TII = STI.getInstrInfo();
665 TRI = STI.getRegisterInfo();
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000666 MRI = &Fn.getRegInfo();
667 bool Modified = false;
668
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000669 LLVM_DEBUG(dbgs() << "Running on function " << Fn.getName() << "\n");
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000670
671 DeadInstr.clear();
672 Replacements.clear();
673
Javed Absar37b22862017-08-14 01:38:01 +0000674 for (MachineBasicBlock &MBB : Fn) {
675 for (MachineInstr &MI : MBB) {
676 Modified |= runOnInstruction(&MI);
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000677 }
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000678 }
679
Javed Absar37b22862017-08-14 01:38:01 +0000680 for (MachineInstr *MI : DeadInstr) {
681 MI->eraseFromParent();
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000682 }
683
684 return Modified;
685}
686
687FunctionPass *llvm::createA15SDOptimizerPass() {
688 return new A15SDOptimizer();
689}