LLVM 24.0.0git
A15SDOptimizer.cpp
Go to the documentation of this file.
1//=== A15SDOptimizerPass.cpp - Optimize DPR and SPR register accesses on A15==//
2//
3// 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
6//
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
26#include "ARM.h"
27#include "ARMBaseInstrInfo.h"
28#include "ARMBaseRegisterInfo.h"
29#include "ARMSubtarget.h"
30#include "llvm/ADT/Statistic.h"
39#include "llvm/Support/Debug.h"
41#include <map>
42#include <set>
43
44using namespace llvm;
45
46#define DEBUG_TYPE "a15-sd-optimizer"
47
48namespace {
49 struct A15SDOptimizer : public MachineFunctionPass {
50 static char ID;
51 A15SDOptimizer() : MachineFunctionPass(ID) {}
52
53 bool runOnMachineFunction(MachineFunction &Fn) override;
54
55 StringRef getPassName() const override { return "ARM A15 S->D optimizer"; }
56
57 void getAnalysisUsage(AnalysisUsage &AU) const override {
60 }
61
62 private:
63 const ARMBaseInstrInfo *TII;
66
67 bool runOnInstruction(MachineInstr *MI);
68
69 //
70 // Instruction builder helpers
71 //
72 unsigned createDupLane(MachineBasicBlock &MBB,
73 MachineBasicBlock::iterator InsertBefore,
74 const DebugLoc &DL, unsigned Reg, unsigned Lane,
75 bool QPR = false);
76
77 unsigned createExtractSubreg(MachineBasicBlock &MBB,
78 MachineBasicBlock::iterator InsertBefore,
79 const DebugLoc &DL, unsigned DReg,
80 unsigned Lane, const TargetRegisterClass *TRC);
81
82 unsigned createVExt(MachineBasicBlock &MBB,
83 MachineBasicBlock::iterator InsertBefore,
84 const DebugLoc &DL, unsigned Ssub0, unsigned Ssub1);
85
86 unsigned createRegSequence(MachineBasicBlock &MBB,
87 MachineBasicBlock::iterator InsertBefore,
88 const DebugLoc &DL, unsigned Reg1,
89 unsigned Reg2);
90
91 unsigned createInsertSubreg(MachineBasicBlock &MBB,
92 MachineBasicBlock::iterator InsertBefore,
93 const DebugLoc &DL, unsigned DReg,
94 unsigned Lane, unsigned ToInsert);
95
96 unsigned createImplicitDef(MachineBasicBlock &MBB,
97 MachineBasicBlock::iterator InsertBefore,
98 const DebugLoc &DL);
99
100 //
101 // Various property checkers
102 //
103 bool usesRegClass(MachineOperand &MO, const TargetRegisterClass *TRC);
104 bool hasPartialWrite(MachineInstr *MI);
106 unsigned getDPRLaneFromSPR(unsigned SReg);
107
108 //
109 // Methods used for getting the definitions of partial registers
110 //
111
112 MachineInstr *elideCopies(MachineInstr *MI);
113 void elideCopiesAndPHIs(MachineInstr *MI,
115
116 //
117 // Pattern optimization methods
118 //
119 unsigned optimizeAllLanesPattern(MachineInstr *MI, unsigned Reg);
120 unsigned optimizeSDPattern(MachineInstr *MI);
121 unsigned getPrefSPRLane(unsigned SReg);
122
123 //
124 // Sanitizing method - used to make sure if don't leave dead code around.
125 //
126 void eraseInstrWithNoUses(MachineInstr *MI);
127
128 //
129 // A map used to track the changes done by this pass.
130 //
131 std::map<MachineInstr*, unsigned> Replacements;
132 std::set<MachineInstr *> DeadInstr;
133 };
134 char A15SDOptimizer::ID = 0;
135} // end anonymous namespace
136
137// Returns true if this is a use of a SPR register.
138bool A15SDOptimizer::usesRegClass(MachineOperand &MO,
139 const TargetRegisterClass *TRC) {
140 if (!MO.isReg())
141 return false;
142 Register Reg = MO.getReg();
143
144 if (Reg.isVirtual())
145 return MRI->getRegClass(Reg)->hasSuperClassEq(TRC);
146 else
147 return TRC->contains(Reg);
148}
149
150unsigned A15SDOptimizer::getDPRLaneFromSPR(unsigned SReg) {
151 MCRegister DReg =
152 TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
153 if (DReg)
154 return ARM::ssub_1;
155 return ARM::ssub_0;
156}
157
158// Get the subreg type that is most likely to be coalesced
159// for an SPR register that will be used in VDUP32d pseudo.
160unsigned A15SDOptimizer::getPrefSPRLane(unsigned SReg) {
161 if (!Register::isVirtualRegister(SReg))
162 return getDPRLaneFromSPR(SReg);
163
164 MachineInstr *MI = MRI->getVRegDef(SReg);
165 if (!MI) return ARM::ssub_0;
166 MachineOperand *MO = MI->findRegisterDefOperand(SReg, /*TRI=*/nullptr);
167 if (!MO) return ARM::ssub_0;
168 assert(MO->isReg() && "Non-register operand found!");
169
170 if (MI->isCopy() && usesRegClass(MI->getOperand(1),
171 &ARM::SPRRegClass)) {
172 SReg = MI->getOperand(1).getReg();
173 }
174
175 if (Register::isVirtualRegister(SReg)) {
176 if (MO->getSubReg() == ARM::ssub_1) return ARM::ssub_1;
177 return ARM::ssub_0;
178 }
179 return getDPRLaneFromSPR(SReg);
180}
181
182// MI is known to be dead. Figure out what instructions
183// are also made dead by this and mark them for removal.
184void A15SDOptimizer::eraseInstrWithNoUses(MachineInstr *MI) {
185 SmallVector<MachineInstr *, 8> Front;
186 DeadInstr.insert(MI);
187
188 LLVM_DEBUG(dbgs() << "Deleting base instruction " << *MI << "\n");
189 Front.push_back(MI);
190
191 while (Front.size() != 0) {
192 MI = Front.pop_back_val();
193
194 // MI is already known to be dead. We need to see
195 // if other instructions can also be removed.
196 for (MachineOperand &MO : MI->operands()) {
197 if ((!MO.isReg()) || (!MO.isUse()))
198 continue;
199 Register Reg = MO.getReg();
200 if (!Reg.isVirtual())
201 continue;
202 MachineOperand *Op = MI->findRegisterDefOperand(Reg, /*TRI=*/nullptr);
203
204 if (!Op)
205 continue;
206
207 MachineInstr *Def = Op->getParent();
208
209 // We don't need to do anything if we have already marked
210 // this instruction as being dead.
211 if (DeadInstr.find(Def) != DeadInstr.end())
212 continue;
213
214 // Check if all the uses of this instruction are marked as
215 // dead. If so, we can also mark this instruction as being
216 // dead.
217 bool IsDead = true;
218 for (MachineOperand &MODef : Def->operands()) {
219 if ((!MODef.isReg()) || (!MODef.isDef()))
220 continue;
221 Register DefReg = MODef.getReg();
222 if (!DefReg.isVirtual()) {
223 IsDead = false;
224 break;
225 }
226 for (MachineInstr &Use : MRI->use_instructions(Reg)) {
227 // We don't care about self references.
228 if (&Use == Def)
229 continue;
230 if (DeadInstr.find(&Use) == DeadInstr.end()) {
231 IsDead = false;
232 break;
233 }
234 }
235 }
236
237 if (!IsDead) continue;
238
239 LLVM_DEBUG(dbgs() << "Deleting instruction " << *Def << "\n");
240 DeadInstr.insert(Def);
241 }
242 }
243}
244
245// Creates the more optimized patterns and generally does all the code
246// transformations in this pass.
247unsigned A15SDOptimizer::optimizeSDPattern(MachineInstr *MI) {
248 if (MI->isCopy()) {
249 return optimizeAllLanesPattern(MI, MI->getOperand(1).getReg());
250 }
251
252 if (MI->isInsertSubreg()) {
253 Register DPRReg = MI->getOperand(1).getReg();
254 Register SPRReg = MI->getOperand(2).getReg();
255
256 if (DPRReg.isVirtual() && SPRReg.isVirtual()) {
257 MachineInstr *DPRMI = MRI->getVRegDef(MI->getOperand(1).getReg());
258 MachineInstr *SPRMI = MRI->getVRegDef(MI->getOperand(2).getReg());
259
260 if (DPRMI && SPRMI) {
261 // See if the first operand of this insert_subreg is IMPLICIT_DEF
262 MachineInstr *ECDef = elideCopies(DPRMI);
263 if (ECDef && ECDef->isImplicitDef()) {
264 // Another corner case - if we're inserting something that is purely
265 // a subreg copy of a DPR, just use that DPR.
266
267 MachineInstr *EC = elideCopies(SPRMI);
268 // Is it a subreg copy of ssub_0?
269 if (EC && EC->isCopy() &&
270 EC->getOperand(1).getSubReg() == ARM::ssub_0) {
271 LLVM_DEBUG(dbgs() << "Found a subreg copy: " << *SPRMI);
272
273 // Find the thing we're subreg copying out of - is it of the same
274 // regclass as DPRMI? (i.e. a DPR or QPR).
275 Register FullReg = SPRMI->getOperand(1).getReg();
276 const TargetRegisterClass *TRC =
277 MRI->getRegClass(MI->getOperand(1).getReg());
278 if (TRC->hasSuperClassEq(MRI->getRegClass(FullReg))) {
279 LLVM_DEBUG(dbgs() << "Subreg copy is compatible - returning ");
280 LLVM_DEBUG(dbgs() << printReg(FullReg) << "\n");
281 eraseInstrWithNoUses(MI);
282 return FullReg;
283 }
284 }
285
286 return optimizeAllLanesPattern(MI, MI->getOperand(2).getReg());
287 }
288 }
289 }
290 return optimizeAllLanesPattern(MI, MI->getOperand(0).getReg());
291 }
292
293 if (MI->isRegSequence() && usesRegClass(MI->getOperand(1),
294 &ARM::SPRRegClass)) {
295 // See if all bar one of the operands are IMPLICIT_DEF and insert the
296 // optimizer pattern accordingly.
297 unsigned NumImplicit = 0, NumTotal = 0;
298 unsigned NonImplicitReg = ~0U;
299
300 for (MachineOperand &MO : llvm::drop_begin(MI->explicit_operands())) {
301 if (!MO.isReg())
302 continue;
303 ++NumTotal;
304 Register OpReg = MO.getReg();
305
306 if (!OpReg.isVirtual())
307 break;
308
309 MachineInstr *Def = MRI->getVRegDef(OpReg);
310 if (!Def)
311 break;
312 if (Def->isImplicitDef())
313 ++NumImplicit;
314 else
315 NonImplicitReg = MO.getReg();
316 }
317
318 if (NumImplicit == NumTotal - 1)
319 return optimizeAllLanesPattern(MI, NonImplicitReg);
320 else
321 return optimizeAllLanesPattern(MI, MI->getOperand(0).getReg());
322 }
323
324 llvm_unreachable("Unhandled update pattern!");
325}
326
327// Return true if this MachineInstr inserts a scalar (SPR) value into
328// a D or Q register.
329bool A15SDOptimizer::hasPartialWrite(MachineInstr *MI) {
330 // The only way we can do a partial register update is through a COPY,
331 // INSERT_SUBREG or REG_SEQUENCE.
332 if (MI->isCopy() && usesRegClass(MI->getOperand(1), &ARM::SPRRegClass))
333 return true;
334
335 if (MI->isInsertSubreg() && usesRegClass(MI->getOperand(2),
336 &ARM::SPRRegClass))
337 return true;
338
339 if (MI->isRegSequence() && usesRegClass(MI->getOperand(1), &ARM::SPRRegClass))
340 return true;
341
342 return false;
343}
344
345// Looks through full copies to get the instruction that defines the input
346// operand for MI.
347MachineInstr *A15SDOptimizer::elideCopies(MachineInstr *MI) {
348 if (!MI->isFullCopy())
349 return MI;
350 if (!MI->getOperand(1).getReg().isVirtual())
351 return nullptr;
352 MachineInstr *Def = MRI->getVRegDef(MI->getOperand(1).getReg());
353 if (!Def)
354 return nullptr;
355 return elideCopies(Def);
356}
357
358// Look through full copies and PHIs to get the set of non-copy MachineInstrs
359// that can produce MI.
360void A15SDOptimizer::elideCopiesAndPHIs(MachineInstr *MI,
361 SmallVectorImpl<MachineInstr*> &Outs) {
362 // Looking through PHIs may create loops so we need to track what
363 // instructions we have visited before.
364 std::set<MachineInstr *> Reached;
365 SmallVector<MachineInstr *, 8> Front;
366 Front.push_back(MI);
367 while (Front.size() != 0) {
368 MI = Front.pop_back_val();
369
370 // If we have already explored this MachineInstr, ignore it.
371 if (!Reached.insert(MI).second)
372 continue;
373 if (MI->isPHI()) {
374 for (unsigned I = 1, E = MI->getNumOperands(); I != E; I += 2) {
375 Register Reg = MI->getOperand(I).getReg();
376 if (!Reg.isVirtual()) {
377 continue;
378 }
379 MachineInstr *NewMI = MRI->getVRegDef(Reg);
380 if (!NewMI)
381 continue;
382 Front.push_back(NewMI);
383 }
384 } else if (MI->isFullCopy()) {
385 if (!MI->getOperand(1).getReg().isVirtual())
386 continue;
387 MachineInstr *NewMI = MRI->getVRegDef(MI->getOperand(1).getReg());
388 if (!NewMI)
389 continue;
390 Front.push_back(NewMI);
391 } else {
392 LLVM_DEBUG(dbgs() << "Found partial copy" << *MI << "\n");
393 Outs.push_back(MI);
394 }
395 }
396}
397
398// Return the DPR virtual registers that are read by this machine instruction
399// (if any).
400SmallVector<unsigned, 8> A15SDOptimizer::getReadDPRs(MachineInstr *MI) {
401 if (MI->isCopyLike() || MI->isInsertSubreg() || MI->isRegSequence() ||
402 MI->isKill())
403 return SmallVector<unsigned, 8>();
404
405 SmallVector<unsigned, 8> Defs;
406 for (MachineOperand &MO : MI->operands()) {
407 if (!MO.isReg() || !MO.isUse())
408 continue;
409 if (!usesRegClass(MO, &ARM::DPRRegClass) &&
410 !usesRegClass(MO, &ARM::QPRRegClass) &&
411 !usesRegClass(MO, &ARM::DPairRegClass)) // Treat DPair as QPR
412 continue;
413
414 Defs.push_back(MO.getReg());
415 }
416 return Defs;
417}
418
419// Creates a DPR register from an SPR one by using a VDUP.
420unsigned A15SDOptimizer::createDupLane(MachineBasicBlock &MBB,
421 MachineBasicBlock::iterator InsertBefore,
422 const DebugLoc &DL, unsigned Reg,
423 unsigned Lane, bool QPR) {
424 Register Out =
425 MRI->createVirtualRegister(QPR ? &ARM::QPRRegClass : &ARM::DPRRegClass);
426 BuildMI(MBB, InsertBefore, DL,
427 TII->get(QPR ? ARM::VDUPLN32q : ARM::VDUPLN32d), Out)
428 .addReg(Reg)
429 .addImm(Lane)
431
432 return Out;
433}
434
435// Creates a SPR register from a DPR by copying the value in lane 0.
436unsigned A15SDOptimizer::createExtractSubreg(
437 MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
438 const DebugLoc &DL, unsigned DReg, unsigned Lane,
439 const TargetRegisterClass *TRC) {
440 Register Out = MRI->createVirtualRegister(TRC);
441 BuildMI(MBB, InsertBefore, DL, TII->get(TargetOpcode::COPY), Out)
442 .addReg(DReg, {}, Lane);
443
444 return Out;
445}
446
447// Takes two SPR registers and creates a DPR by using a REG_SEQUENCE.
448unsigned A15SDOptimizer::createRegSequence(
449 MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
450 const DebugLoc &DL, unsigned Reg1, unsigned Reg2) {
451 Register Out = MRI->createVirtualRegister(&ARM::QPRRegClass);
452 BuildMI(MBB,
453 InsertBefore,
454 DL,
455 TII->get(TargetOpcode::REG_SEQUENCE), Out)
456 .addReg(Reg1)
457 .addImm(ARM::dsub_0)
458 .addReg(Reg2)
459 .addImm(ARM::dsub_1);
460 return Out;
461}
462
463// Takes two DPR registers that have previously been VDUPed (Ssub0 and Ssub1)
464// and merges them into one DPR register.
465unsigned A15SDOptimizer::createVExt(MachineBasicBlock &MBB,
466 MachineBasicBlock::iterator InsertBefore,
467 const DebugLoc &DL, unsigned Ssub0,
468 unsigned Ssub1) {
469 Register Out = MRI->createVirtualRegister(&ARM::DPRRegClass);
470 BuildMI(MBB, InsertBefore, DL, TII->get(ARM::VEXTd32), Out)
471 .addReg(Ssub0)
472 .addReg(Ssub1)
473 .addImm(1)
475 return Out;
476}
477
478unsigned A15SDOptimizer::createInsertSubreg(
479 MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
480 const DebugLoc &DL, unsigned DReg, unsigned Lane, unsigned ToInsert) {
481 Register Out = MRI->createVirtualRegister(&ARM::DPR_VFP2RegClass);
482 BuildMI(MBB,
483 InsertBefore,
484 DL,
485 TII->get(TargetOpcode::INSERT_SUBREG), Out)
486 .addReg(DReg)
487 .addReg(ToInsert)
488 .addImm(Lane);
489
490 return Out;
491}
492
493unsigned
494A15SDOptimizer::createImplicitDef(MachineBasicBlock &MBB,
495 MachineBasicBlock::iterator InsertBefore,
496 const DebugLoc &DL) {
497 Register Out = MRI->createVirtualRegister(&ARM::DPRRegClass);
498 BuildMI(MBB,
499 InsertBefore,
500 DL,
501 TII->get(TargetOpcode::IMPLICIT_DEF), Out);
502 return Out;
503}
504
505// This function inserts instructions in order to optimize interactions between
506// SPR registers and DPR/QPR registers. It does so by performing VDUPs on all
507// lanes, and the using VEXT instructions to recompose the result.
508unsigned
509A15SDOptimizer::optimizeAllLanesPattern(MachineInstr *MI, unsigned Reg) {
511 DebugLoc DL = MI->getDebugLoc();
512 MachineBasicBlock &MBB = *MI->getParent();
513 InsertPt++;
514 unsigned Out;
515
516 // DPair has the same length as QPR and also has two DPRs as subreg.
517 // Treat DPair as QPR.
518 if (MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::QPRRegClass) ||
519 MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::DPairRegClass)) {
520 unsigned DSub0 = createExtractSubreg(MBB, InsertPt, DL, Reg,
521 ARM::dsub_0, &ARM::DPRRegClass);
522 unsigned DSub1 = createExtractSubreg(MBB, InsertPt, DL, Reg,
523 ARM::dsub_1, &ARM::DPRRegClass);
524
525 unsigned Out1 = createDupLane(MBB, InsertPt, DL, DSub0, 0);
526 unsigned Out2 = createDupLane(MBB, InsertPt, DL, DSub0, 1);
527 Out = createVExt(MBB, InsertPt, DL, Out1, Out2);
528
529 unsigned Out3 = createDupLane(MBB, InsertPt, DL, DSub1, 0);
530 unsigned Out4 = createDupLane(MBB, InsertPt, DL, DSub1, 1);
531 Out2 = createVExt(MBB, InsertPt, DL, Out3, Out4);
532
533 Out = createRegSequence(MBB, InsertPt, DL, Out, Out2);
534
535 } else if (MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::DPRRegClass)) {
536 unsigned Out1 = createDupLane(MBB, InsertPt, DL, Reg, 0);
537 unsigned Out2 = createDupLane(MBB, InsertPt, DL, Reg, 1);
538 Out = createVExt(MBB, InsertPt, DL, Out1, Out2);
539
540 } else {
541 assert(MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::SPRRegClass) &&
542 "Found unexpected regclass!");
543
544 unsigned PrefLane = getPrefSPRLane(Reg);
545 unsigned Lane;
546 switch (PrefLane) {
547 case ARM::ssub_0: Lane = 0; break;
548 case ARM::ssub_1: Lane = 1; break;
549 default: llvm_unreachable("Unknown preferred lane!");
550 }
551
552 // Treat DPair as QPR
553 bool UsesQPR = usesRegClass(MI->getOperand(0), &ARM::QPRRegClass) ||
554 usesRegClass(MI->getOperand(0), &ARM::DPairRegClass);
555
556 Out = createImplicitDef(MBB, InsertPt, DL);
557 Out = createInsertSubreg(MBB, InsertPt, DL, Out, PrefLane, Reg);
558 Out = createDupLane(MBB, InsertPt, DL, Out, Lane, UsesQPR);
559 eraseInstrWithNoUses(MI);
560 }
561 return Out;
562}
563
564bool A15SDOptimizer::runOnInstruction(MachineInstr *MI) {
565 // We look for instructions that write S registers that are then read as
566 // D/Q registers. These can only be caused by COPY, INSERT_SUBREG and
567 // REG_SEQUENCE pseudos that insert an SPR value into a DPR register or
568 // merge two SPR values to form a DPR register. In order avoid false
569 // positives we make sure that there is an SPR producer so we look past
570 // COPY and PHI nodes to find it.
571 //
572 // The best code pattern for when an SPR producer is going to be used by a
573 // DPR or QPR consumer depends on whether the other lanes of the
574 // corresponding DPR/QPR are currently defined.
575 //
576 // We can handle these efficiently, depending on the type of
577 // pseudo-instruction that is producing the pattern
578 //
579 // * COPY: * VDUP all lanes and merge the results together
580 // using VEXTs.
581 //
582 // * INSERT_SUBREG: * If the SPR value was originally in another DPR/QPR
583 // lane, and the other lane(s) of the DPR/QPR register
584 // that we are inserting in are undefined, use the
585 // original DPR/QPR value.
586 // * Otherwise, fall back on the same strategy as COPY.
587 //
588 // * REG_SEQUENCE: * If all except one of the input operands are
589 // IMPLICIT_DEFs, insert the VDUP pattern for just the
590 // defined input operand
591 // * Otherwise, fall back on the same strategy as COPY.
592 //
593
594 // First, get all the reads of D-registers done by this instruction.
595 SmallVector<unsigned, 8> Defs = getReadDPRs(MI);
596 bool Modified = false;
597
598 for (unsigned I : Defs) {
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;
604 if (!Register::isVirtualRegister(I))
605 continue;
606 MachineInstr *Def = MRI->getVRegDef(I);
607 if (!Def)
608 continue;
609
610 elideCopiesAndPHIs(Def, DefSrcs);
611
612 for (MachineInstr *MI : DefSrcs) {
613 // 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 Register DPRDefReg = MI->getOperand(0).getReg();
625 llvm::make_pointer_range(MRI->use_operands(DPRDefReg)));
626
627 // We can optimize this.
628 unsigned NewReg = optimizeSDPattern(MI);
629
630 if (NewReg != 0) {
631 Modified = true;
632 for (MachineOperand *Use : Uses) {
633 // Make sure to constrain the register class of the new register to
634 // match what we're replacing. Otherwise we can optimize a DPR_VFP2
635 // reference into a plain DPR, and that will end poorly. NewReg is
636 // always virtual here, so there will always be a matching subclass
637 // to find.
638 MRI->constrainRegClass(NewReg, MRI->getRegClass(Use->getReg()));
639
640 LLVM_DEBUG(dbgs() << "Replacing operand " << *Use << " with "
641 << printReg(NewReg) << "\n");
642 Use->substVirtReg(NewReg, 0, *TRI);
643 }
644 }
645 Replacements[MI] = NewReg;
646 }
647 }
648 return Modified;
649}
650
651bool A15SDOptimizer::runOnMachineFunction(MachineFunction &Fn) {
652 if (skipFunction(Fn.getFunction()))
653 return false;
654
655 const ARMSubtarget &STI = Fn.getSubtarget<ARMSubtarget>();
656 // Since the A15SDOptimizer pass can insert VDUP instructions, it can only be
657 // enabled when NEON is available.
658 if (!(STI.useSplatVFPToNeon() && STI.hasNEON()))
659 return false;
660
661 TII = STI.getInstrInfo();
662 TRI = STI.getRegisterInfo();
663 MRI = &Fn.getRegInfo();
664 bool Modified = false;
665
666 LLVM_DEBUG(dbgs() << "Running on function " << Fn.getName() << "\n");
667
668 DeadInstr.clear();
669 Replacements.clear();
670
671 for (MachineBasicBlock &MBB : Fn) {
672 for (MachineInstr &MI : MBB) {
673 Modified |= runOnInstruction(&MI);
674 }
675 }
676
677 for (MachineInstr *MI : DeadInstr) {
678 MI->eraseFromParent();
679 }
680
681 return Modified;
682}
683
685 return new A15SDOptimizer();
686}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
Remove Loads Into Fake Uses
bool IsDead
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define LLVM_DEBUG(...)
Definition Debug.h:119
const ARMBaseInstrInfo * getInstrInfo() const override
const ARMBaseRegisterInfo * getRegisterInfo() const override
Represent the analysis usage information of a pass.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
A debug info location.
Definition DebugLoc.h:126
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
bool hasSuperClassEq(const MCRegisterClass *RC) const
Returns true if RC is a super-class of or equal to this class.
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
Representation of each machine instruction.
bool isImplicitDef() const
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
LLVM_ABI MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
iterator_range< use_instr_iterator > use_instructions(Register Reg) const
LLVM_ABI const TargetRegisterClass * constrainRegClass(Register Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
constrainRegClass - Constrain the register class of the specified virtual register to be a common sub...
iterator_range< use_iterator > use_operands(Register Reg) const
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:315
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
static std::array< MachineOperand, 2 > predOps(ARMCC::CondCodes Pred, unsigned PredReg=0)
Get the operands corresponding to the given Pred value.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
FunctionPass * createA15SDOptimizerPass()
DWARFExpression::Operation Op
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
Definition iterator.h:368
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58