LLVM 22.0.0git
ARMLowOverheadLoops.cpp
Go to the documentation of this file.
1//===-- ARMLowOverheadLoops.cpp - CodeGen Low-overhead Loops ---*- C++ -*-===//
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/// \file
9/// Finalize v8.1-m low-overhead loops by converting the associated pseudo
10/// instructions into machine operations.
11/// The expectation is that the loop contains three pseudo instructions:
12/// - t2*LoopStart - placed in the preheader or pre-preheader. The do-loop
13/// form should be in the preheader, whereas the while form should be in the
14/// preheaders only predecessor.
15/// - t2LoopDec - placed within in the loop body.
16/// - t2LoopEnd - the loop latch terminator.
17///
18/// In addition to this, we also look for the presence of the VCTP instruction,
19/// which determines whether we can generated the tail-predicated low-overhead
20/// loop form.
21///
22/// Assumptions and Dependencies:
23/// Low-overhead loops are constructed and executed using a setup instruction:
24/// DLS, WLS, DLSTP or WLSTP and an instruction that loops back: LE or LETP.
25/// WLS(TP) and LE(TP) are branching instructions with a (large) limited range
26/// but fixed polarity: WLS can only branch forwards and LE can only branch
27/// backwards. These restrictions mean that this pass is dependent upon block
28/// layout and block sizes, which is why it's the last pass to run. The same is
29/// true for ConstantIslands, but this pass does not increase the size of the
30/// basic blocks, nor does it change the CFG. Instructions are mainly removed
31/// during the transform and pseudo instructions are replaced by real ones. In
32/// some cases, when we have to revert to a 'normal' loop, we have to introduce
33/// multiple instructions for a single pseudo (see RevertWhile and
34/// RevertLoopEnd). To handle this situation, t2WhileLoopStartLR and t2LoopEnd
35/// are defined to be as large as this maximum sequence of replacement
36/// instructions.
37///
38/// A note on VPR.P0 (the lane mask):
39/// VPT, VCMP, VPNOT and VCTP won't overwrite VPR.P0 when they update it in a
40/// "VPT Active" context (which includes low-overhead loops and vpt blocks).
41/// They will simply "and" the result of their calculation with the current
42/// value of VPR.P0. You can think of it like this:
43/// \verbatim
44/// if VPT active: ; Between a DLSTP/LETP, or for predicated instrs
45/// VPR.P0 &= Value
46/// else
47/// VPR.P0 = Value
48/// \endverbatim
49/// When we're inside the low-overhead loop (between DLSTP and LETP), we always
50/// fall in the "VPT active" case, so we can consider that all VPR writes by
51/// one of those instruction is actually a "and".
52//===----------------------------------------------------------------------===//
53
54#include "ARM.h"
55#include "ARMBaseInstrInfo.h"
56#include "ARMBasicBlockInfo.h"
57#include "ARMSubtarget.h"
58#include "MVETailPredUtils.h"
59#include "Thumb2InstrInfo.h"
60#include "llvm/ADT/SetVector.h"
67#include "llvm/CodeGen/Passes.h"
69#include "llvm/MC/MCInstrDesc.h"
70
71using namespace llvm;
72
73#define DEBUG_TYPE "arm-low-overhead-loops"
74#define ARM_LOW_OVERHEAD_LOOPS_NAME "ARM Low Overhead Loops pass"
75
76static cl::opt<bool>
77DisableTailPredication("arm-loloops-disable-tailpred", cl::Hidden,
78 cl::desc("Disable tail-predication in the ARM LowOverheadLoop pass"),
79 cl::init(false));
80
81static cl::opt<bool>
82 DisableOmitDLS("arm-disable-omit-dls", cl::Hidden,
83 cl::desc("Disable omitting 'dls lr, lr' instructions"),
84 cl::init(false));
85
88 return PIdx != -1 && MI->getOperand(PIdx + 1).getReg() == ARM::VPR;
89}
90
92 return MI->findRegisterDefOperandIdx(ARM::VPR, /*TRI=*/nullptr) != -1;
93}
94
95static bool hasVPRUse(MachineInstr &MI) {
96 return MI.findRegisterUseOperandIdx(ARM::VPR, /*TRI=*/nullptr) != -1;
97}
98
100 uint64_t Domain = MI->getDesc().TSFlags & ARMII::DomainMask;
101 return Domain == ARMII::DomainMVE;
102}
103
104static int getVecSize(const MachineInstr &MI) {
105 const MCInstrDesc &MCID = MI.getDesc();
106 uint64_t Flags = MCID.TSFlags;
107 return (Flags & ARMII::VecSize) >> ARMII::VecSizeShift;
108}
109
111 if (MI.isDebugInstr())
112 return false;
113 return isDomainMVE(&MI) || isVectorPredicate(&MI) || hasVPRUse(MI);
114}
115
117 const MCInstrDesc &MCID = MI.getDesc();
118 uint64_t Flags = MCID.TSFlags;
119 return (Flags & ARMII::HorizontalReduction) != 0;
120}
121
122namespace {
123
124 using InstSet = SmallPtrSetImpl<MachineInstr *>;
125
126 class PostOrderLoopTraversal {
127 MachineLoop &ML;
128 MachineLoopInfo &MLI;
129 SmallPtrSet<MachineBasicBlock*, 4> Visited;
130 SmallVector<MachineBasicBlock*, 4> Order;
131
132 public:
133 PostOrderLoopTraversal(MachineLoop &ML, MachineLoopInfo &MLI)
134 : ML(ML), MLI(MLI) { }
135
136 const SmallVectorImpl<MachineBasicBlock*> &getOrder() const {
137 return Order;
138 }
139
140 // Visit all the blocks within the loop, as well as exit blocks and any
141 // blocks properly dominating the header.
142 void ProcessLoop() {
143 std::function<void(MachineBasicBlock *)> Search =
144 [this, &Search](MachineBasicBlock *MBB) -> void {
145 if (!Visited.insert(MBB).second)
146 return;
147
148 for (auto *Succ : MBB->successors()) {
149 if (!ML.contains(Succ))
150 continue;
151 Search(Succ);
152 }
153 Order.push_back(MBB);
154 };
155
156 // Insert exit blocks.
158 ML.getExitBlocks(ExitBlocks);
159 append_range(Order, ExitBlocks);
160
161 // Then add the loop body.
162 Search(ML.getHeader());
163
164 // Then try the preheader and its predecessors.
165 std::function<void(MachineBasicBlock*)> GetPredecessor =
166 [this, &GetPredecessor] (MachineBasicBlock *MBB) -> void {
167 Order.push_back(MBB);
168 if (MBB->pred_size() == 1)
169 GetPredecessor(*MBB->pred_begin());
170 };
171
172 if (auto *Preheader = ML.getLoopPreheader())
173 GetPredecessor(Preheader);
174 else if (auto *Preheader = MLI.findLoopPreheader(&ML, true, true))
175 GetPredecessor(Preheader);
176 }
177 };
178
179 class VPTBlock {
180 SmallVector<MachineInstr *, 4> Insts;
181
182 public:
183 VPTBlock(MachineInstr *MI) { Insts.push_back(MI); }
184
185 // Have we found an instruction within the block which defines the vpr? If
186 // so, not all the instructions in the block will have the same predicate.
187 bool hasUniformPredicate() { return getDivergent() == nullptr; }
188
189 // If it exists, return the first internal instruction which modifies the
190 // VPR.
191 MachineInstr *getDivergent() {
192 SmallVectorImpl<MachineInstr *> &Insts = getInsts();
193 for (unsigned i = 1; i < Insts.size(); ++i) {
194 MachineInstr *Next = Insts[i];
196 return Next; // Found an instruction altering the vpr.
197 }
198 return nullptr;
199 }
200
201 void insert(MachineInstr *MI) {
202 Insts.push_back(MI);
203 // VPT/VPST + 4 predicated instructions.
204 assert(Insts.size() <= 5 && "Too many instructions in VPT block!");
205 }
206
207 bool containsVCTP() const { return llvm::any_of(Insts, isVCTP); }
208
209 unsigned size() const { return Insts.size(); }
210 SmallVectorImpl<MachineInstr *> &getInsts() { return Insts; }
211 };
212
213 // Represent the current state of the VPR and hold all instances which
214 // represent a VPT block, which is a list of instructions that begins with a
215 // VPT/VPST and has a maximum of four proceeding instructions. All
216 // instructions within the block are predicated upon the vpr and we allow
217 // instructions to define the vpr within in the block too.
218 class VPTState {
219 friend struct LowOverheadLoop;
220
222 SetVector<MachineInstr *> CurrentPredicates;
223 std::map<MachineInstr *, SetVector<MachineInstr *>> PredicatedInsts;
224
225 void CreateVPTBlock(MachineInstr *MI) {
226 assert((CurrentPredicates.size() || MI->getParent()->isLiveIn(ARM::VPR))
227 && "Can't begin VPT without predicate");
228 Blocks.emplace_back(MI);
229 // The execution of MI is predicated upon the current set of instructions
230 // that are AND'ed together to form the VPR predicate value. In the case
231 // that MI is a VPT, CurrentPredicates will also just be MI.
232 PredicatedInsts[MI] = CurrentPredicates;
233 }
234
235 void addInst(MachineInstr *MI) {
236 Blocks.back().insert(MI);
237 PredicatedInsts[MI] = CurrentPredicates;
238 }
239
240 void addPredicate(MachineInstr *MI) {
241 LLVM_DEBUG(dbgs() << "ARM Loops: Adding VPT Predicate: " << *MI);
242 CurrentPredicates.insert(MI);
243 }
244
245 void resetPredicate(MachineInstr *MI) {
246 LLVM_DEBUG(dbgs() << "ARM Loops: Resetting VPT Predicate: " << *MI);
247 CurrentPredicates.clear();
248 CurrentPredicates.insert(MI);
249 }
250
251 public:
252 // Return whether the given instruction is predicated upon a VCTP.
253 bool isPredicatedOnVCTP(MachineInstr *MI, bool Exclusive = false) {
254 SetVector<MachineInstr *> &Predicates = PredicatedInsts[MI];
255 if (Exclusive && Predicates.size() != 1)
256 return false;
257 // We do not know how to convert an else predicate of a VCTP.
259 return false;
260 return llvm::any_of(Predicates, isVCTP);
261 }
262
263 // Is the VPST, controlling the block entry, predicated upon a VCTP.
264 bool isEntryPredicatedOnVCTP(VPTBlock &Block, bool Exclusive = false) {
265 SmallVectorImpl<MachineInstr *> &Insts = Block.getInsts();
266 return isPredicatedOnVCTP(Insts.front(), Exclusive);
267 }
268
269 // If this block begins with a VPT, we can check whether it's using
270 // at least one predicated input(s), as well as possible loop invariant
271 // which would result in it being implicitly predicated.
272 bool hasImplicitlyValidVPT(VPTBlock &Block, ReachingDefInfo &RDI) {
273 SmallVectorImpl<MachineInstr *> &Insts = Block.getInsts();
274 MachineInstr *VPT = Insts.front();
275 assert(isVPTOpcode(VPT->getOpcode()) &&
276 "Expected VPT block to begin with VPT/VPST");
277
278 if (VPT->getOpcode() == ARM::MVE_VPST)
279 return false;
280
281 // If the VPT block does not define something that is an "output", then
282 // the tail-predicated version will just perform a subset of the original
283 // vpt block, where the last lanes should not be used.
284 if (isVPTOpcode(VPT->getOpcode()) &&
285 all_of(Block.getInsts(), [](const MachineInstr *MI) {
286 return !MI->mayStore() && !MI->mayLoad() &&
287 !isHorizontalReduction(*MI) && !isVCTP(MI);
288 }))
289 return true;
290
291 auto IsOperandPredicated = [&](MachineInstr *MI, unsigned Idx) {
292 MachineInstr *Op = RDI.getMIOperand(MI, MI->getOperand(Idx));
293 return Op && PredicatedInsts.count(Op) && isPredicatedOnVCTP(Op);
294 };
295
296 auto IsOperandInvariant = [&](MachineInstr *MI, unsigned Idx) {
297 MachineOperand &MO = MI->getOperand(Idx);
298 if (!MO.isReg() || !MO.getReg())
299 return true;
300
301 SmallPtrSet<MachineInstr *, 2> Defs;
302 RDI.getGlobalReachingDefs(MI, MO.getReg(), Defs);
303 if (Defs.empty())
304 return true;
305
306 for (auto *Def : Defs)
307 if (Def->getParent() == VPT->getParent())
308 return false;
309 return true;
310 };
311
312 // Check that at least one of the operands is directly predicated on a
313 // vctp and allow an invariant value too.
314 return (IsOperandPredicated(VPT, 1) || IsOperandPredicated(VPT, 2)) &&
315 (IsOperandPredicated(VPT, 1) || IsOperandInvariant(VPT, 1)) &&
316 (IsOperandPredicated(VPT, 2) || IsOperandInvariant(VPT, 2));
317 }
318
319 bool isValid(ReachingDefInfo &RDI) {
320 // All predication within the loop should be based on vctp. If the block
321 // isn't predicated on entry, check whether the vctp is within the block
322 // and that all other instructions are then predicated on it.
323 for (auto &Block : Blocks) {
324 if (isEntryPredicatedOnVCTP(Block, false) &&
325 !any_of(drop_begin(Block.getInsts()), [](const MachineInstr *MI) {
326 return getVPTInstrPredicate(*MI) == ARMVCC::Else;
327 }))
328 continue;
329 if (hasImplicitlyValidVPT(Block, RDI))
330 continue;
331
332 SmallVectorImpl<MachineInstr *> &Insts = Block.getInsts();
333 // We don't know how to convert a block with just a VPT;VCTP into
334 // anything valid once we remove the VCTP. For now just bail out.
335 assert(isVPTOpcode(Insts.front()->getOpcode()) &&
336 "Expected VPT block to start with a VPST or VPT!");
337 if (Insts.size() == 2 && Insts.front()->getOpcode() != ARM::MVE_VPST &&
338 isVCTP(Insts.back()))
339 return false;
340
341 for (auto *MI : Insts) {
342 // Check that any internal VCTPs are 'Then' predicated.
344 return false;
345 // Skip other instructions that build up the predicate.
346 if (MI->getOpcode() == ARM::MVE_VPST || isVectorPredicate(MI))
347 continue;
348 // Check that any other instructions are predicated upon a vctp.
349 // TODO: We could infer when VPTs are implicitly predicated on the
350 // vctp (when the operands are predicated).
351 if (!isPredicatedOnVCTP(MI)) {
352 LLVM_DEBUG(dbgs() << "ARM Loops: Can't convert: " << *MI);
353 return false;
354 }
355 }
356 }
357 return true;
358 }
359 };
360
361 struct LowOverheadLoop {
362
363 MachineLoop &ML;
364 MachineBasicBlock *Preheader = nullptr;
365 MachineLoopInfo &MLI;
366 ReachingDefInfo &RDI;
367 const TargetRegisterInfo &TRI;
368 const ARMBaseInstrInfo &TII;
369 MachineFunction *MF = nullptr;
370 MachineBasicBlock::iterator StartInsertPt;
371 MachineBasicBlock *StartInsertBB = nullptr;
372 MachineInstr *Start = nullptr;
373 MachineInstr *Dec = nullptr;
374 MachineInstr *End = nullptr;
375 MachineOperand TPNumElements;
376 SmallVector<MachineInstr *, 4> VCTPs;
377 SmallPtrSet<MachineInstr *, 4> ToRemove;
378 SmallPtrSet<MachineInstr *, 4> BlockMasksToRecompute;
379 SmallPtrSet<MachineInstr *, 4> DoubleWidthResultInstrs;
380 SmallPtrSet<MachineInstr *, 4> VMOVCopies;
381 bool Revert = false;
382 bool CannotTailPredicate = false;
383 VPTState VPTstate;
384
385 LowOverheadLoop(MachineLoop &ML, MachineLoopInfo &MLI, ReachingDefInfo &RDI,
386 const TargetRegisterInfo &TRI, const ARMBaseInstrInfo &TII)
387 : ML(ML), MLI(MLI), RDI(RDI), TRI(TRI), TII(TII),
388 TPNumElements(MachineOperand::CreateImm(0)) {
389 MF = ML.getHeader()->getParent();
390 if (auto *MBB = ML.getLoopPreheader())
391 Preheader = MBB;
392 else if (auto *MBB = MLI.findLoopPreheader(&ML, true, true))
393 Preheader = MBB;
394 }
395
396 // If this is an MVE instruction, check that we know how to use tail
397 // predication with it. Record VPT blocks and return whether the
398 // instruction is valid for tail predication.
399 bool ValidateMVEInst(MachineInstr *MI);
400
401 void AnalyseMVEInst(MachineInstr *MI) {
402 CannotTailPredicate = !ValidateMVEInst(MI);
403 }
404
405 bool IsTailPredicationLegal() const {
406 // For now, let's keep things really simple and only support a single
407 // block for tail predication.
408 return !Revert && FoundAllComponents() && !VCTPs.empty() &&
409 !CannotTailPredicate && ML.getNumBlocks() == 1;
410 }
411
412 // Given that MI is a VCTP, check that is equivalent to any other VCTPs
413 // found.
414 bool AddVCTP(MachineInstr *MI);
415
416 // Check that the predication in the loop will be equivalent once we
417 // perform the conversion. Also ensure that we can provide the number
418 // of elements to the loop start instruction.
419 bool ValidateTailPredicate();
420
421 // Check that any values available outside of the loop will be the same
422 // after tail predication conversion.
423 bool ValidateLiveOuts();
424
425 // Check the branch targets are within range and we satisfy our
426 // restrictions.
427 void Validate(ARMBasicBlockUtils *BBUtils);
428
429 bool FoundAllComponents() const {
430 return Start && Dec && End;
431 }
432
433 SmallVectorImpl<VPTBlock> &getVPTBlocks() { return VPTstate.Blocks; }
434
435 // Return the operand for the loop start instruction. This will be the loop
436 // iteration count, or the number of elements if we're tail predicating.
437 MachineOperand &getLoopStartOperand() {
438 if (IsTailPredicationLegal())
439 return TPNumElements;
440 return Start->getOperand(1);
441 }
442
443 unsigned getStartOpcode() const {
444 bool IsDo = isDoLoopStart(*Start);
445 if (!IsTailPredicationLegal())
446 return IsDo ? ARM::t2DLS : ARM::t2WLS;
447
448 return VCTPOpcodeToLSTP(VCTPs.back()->getOpcode(), IsDo);
449 }
450
451 void dump() const {
452 if (Start) dbgs() << "ARM Loops: Found Loop Start: " << *Start;
453 if (Dec) dbgs() << "ARM Loops: Found Loop Dec: " << *Dec;
454 if (End) dbgs() << "ARM Loops: Found Loop End: " << *End;
455 if (!VCTPs.empty()) {
456 dbgs() << "ARM Loops: Found VCTP(s):\n";
457 for (auto *MI : VCTPs)
458 dbgs() << " - " << *MI;
459 }
460 if (!FoundAllComponents())
461 dbgs() << "ARM Loops: Not a low-overhead loop.\n";
462 else if (!(Start && Dec && End))
463 dbgs() << "ARM Loops: Failed to find all loop components.\n";
464 }
465 };
466
467 class ARMLowOverheadLoops : public MachineFunctionPass {
468 MachineFunction *MF = nullptr;
469 MachineLoopInfo *MLI = nullptr;
470 ReachingDefInfo *RDI = nullptr;
471 const ARMBaseInstrInfo *TII = nullptr;
472 MachineRegisterInfo *MRI = nullptr;
473 const TargetRegisterInfo *TRI = nullptr;
474 std::unique_ptr<ARMBasicBlockUtils> BBUtils = nullptr;
475
476 public:
477 static char ID;
478
479 ARMLowOverheadLoops() : MachineFunctionPass(ID) { }
480
481 void getAnalysisUsage(AnalysisUsage &AU) const override {
482 AU.setPreservesCFG();
483 AU.addRequired<MachineLoopInfoWrapperPass>();
484 AU.addRequired<ReachingDefInfoWrapperPass>();
486 }
487
488 bool runOnMachineFunction(MachineFunction &MF) override;
489
490 MachineFunctionProperties getRequiredProperties() const override {
491 return MachineFunctionProperties().setNoVRegs().setTracksLiveness();
492 }
493
494 StringRef getPassName() const override {
496 }
497
498 private:
499 bool ProcessLoop(MachineLoop *ML);
500
501 bool RevertNonLoops();
502
503 void RevertWhile(MachineInstr *MI) const;
504 void RevertDo(MachineInstr *MI) const;
505
506 bool RevertLoopDec(MachineInstr *MI) const;
507
508 void RevertLoopEnd(MachineInstr *MI, bool SkipCmp = false) const;
509
510 void RevertLoopEndDec(MachineInstr *MI) const;
511
512 void ConvertVPTBlocks(LowOverheadLoop &LoLoop);
513
514 MachineInstr *ExpandLoopStart(LowOverheadLoop &LoLoop);
515
516 void Expand(LowOverheadLoop &LoLoop);
517
518 void IterationCountDCE(LowOverheadLoop &LoLoop);
519 };
520}
521
522char ARMLowOverheadLoops::ID = 0;
523
525 false, false)
526
527static bool TryRemove(MachineInstr *MI, ReachingDefInfo &RDI, InstSet &ToRemove,
528 InstSet &Ignore) {
529
530 // Check that we can remove all of Killed without having to modify any IT
531 // blocks.
532 auto WontCorruptITs = [](InstSet &Killed, ReachingDefInfo &RDI) {
533 // Collect the dead code and the MBBs in which they reside.
535 for (auto *Dead : Killed)
536 BasicBlocks.insert(Dead->getParent());
537
538 // Collect IT blocks in all affected basic blocks.
539 std::map<MachineInstr *, SmallPtrSet<MachineInstr *, 2>> ITBlocks;
540 for (auto *MBB : BasicBlocks) {
541 for (auto &IT : *MBB) {
542 if (IT.getOpcode() != ARM::t2IT)
543 continue;
544 RDI.getReachingLocalUses(&IT, MCRegister::from(ARM::ITSTATE),
545 ITBlocks[&IT]);
546 }
547 }
548
549 // If we're removing all of the instructions within an IT block, then
550 // also remove the IT instruction.
553 for (auto *Dead : Killed) {
554 if (MachineOperand *MO =
555 Dead->findRegisterUseOperand(ARM::ITSTATE, /*TRI=*/nullptr)) {
556 MachineInstr *IT = RDI.getMIOperand(Dead, *MO);
557 RemoveITs.insert(IT);
558 auto &CurrentBlock = ITBlocks[IT];
559 CurrentBlock.erase(Dead);
560 if (CurrentBlock.empty())
561 ModifiedITs.erase(IT);
562 else
563 ModifiedITs.insert(IT);
564 }
565 }
566 if (!ModifiedITs.empty())
567 return false;
568 Killed.insert_range(RemoveITs);
569 return true;
570 };
571
573 if (!RDI.isSafeToRemove(MI, Uses, Ignore))
574 return false;
575
576 if (WontCorruptITs(Uses, RDI)) {
577 ToRemove.insert_range(Uses);
578 LLVM_DEBUG(dbgs() << "ARM Loops: Able to remove: " << *MI
579 << " - can also remove:\n";
580 for (auto *Use : Uses)
581 dbgs() << " - " << *Use);
582
584 RDI.collectKilledOperands(MI, Killed);
585 if (WontCorruptITs(Killed, RDI)) {
586 ToRemove.insert_range(Killed);
587 LLVM_DEBUG(for (auto *Dead : Killed)
588 dbgs() << " - " << *Dead);
589 }
590 return true;
591 }
592 return false;
593}
594
595bool LowOverheadLoop::ValidateTailPredicate() {
596 if (!IsTailPredicationLegal()) {
597 LLVM_DEBUG(if (VCTPs.empty())
598 dbgs() << "ARM Loops: Didn't find a VCTP instruction.\n";
599 dbgs() << "ARM Loops: Tail-predication is not valid.\n");
600 return false;
601 }
602
603 assert(!VCTPs.empty() && "VCTP instruction expected but is not set");
604 assert(ML.getBlocks().size() == 1 &&
605 "Shouldn't be processing a loop with more than one block");
606
608 LLVM_DEBUG(dbgs() << "ARM Loops: tail-predication is disabled\n");
609 return false;
610 }
611
612 if (!VPTstate.isValid(RDI)) {
613 LLVM_DEBUG(dbgs() << "ARM Loops: Invalid VPT state.\n");
614 return false;
615 }
616
617 if (!ValidateLiveOuts()) {
618 LLVM_DEBUG(dbgs() << "ARM Loops: Invalid live outs.\n");
619 return false;
620 }
621
622 // For tail predication, we need to provide the number of elements, instead
623 // of the iteration count, to the loop start instruction. The number of
624 // elements is provided to the vctp instruction, so we need to check that
625 // we can use this register at InsertPt.
626 MachineInstr *VCTP = VCTPs.back();
627 if (Start->getOpcode() == ARM::t2DoLoopStartTP ||
628 Start->getOpcode() == ARM::t2WhileLoopStartTP) {
629 TPNumElements = Start->getOperand(2);
630 StartInsertPt = Start;
631 StartInsertBB = Start->getParent();
632 } else {
633 TPNumElements = VCTP->getOperand(1);
634 MCRegister NumElements = TPNumElements.getReg().asMCReg();
635
636 // If the register is defined within loop, then we can't perform TP.
637 // TODO: Check whether this is just a mov of a register that would be
638 // available.
639 if (RDI.hasLocalDefBefore(VCTP, NumElements)) {
640 LLVM_DEBUG(dbgs() << "ARM Loops: VCTP operand is defined in the loop.\n");
641 return false;
642 }
643
644 // The element count register maybe defined after InsertPt, in which case we
645 // need to try to move either InsertPt or the def so that the [w|d]lstp can
646 // use the value.
647
648 if (StartInsertPt != StartInsertBB->end() &&
649 !RDI.isReachingDefLiveOut(&*StartInsertPt, NumElements)) {
650 if (auto *ElemDef =
651 RDI.getLocalLiveOutMIDef(StartInsertBB, NumElements)) {
652 if (RDI.isSafeToMoveForwards(ElemDef, &*StartInsertPt)) {
653 ElemDef->removeFromParent();
654 StartInsertBB->insert(StartInsertPt, ElemDef);
656 << "ARM Loops: Moved element count def: " << *ElemDef);
657 } else if (RDI.isSafeToMoveBackwards(&*StartInsertPt, ElemDef)) {
658 StartInsertPt->removeFromParent();
659 StartInsertBB->insertAfter(MachineBasicBlock::iterator(ElemDef),
660 &*StartInsertPt);
661 LLVM_DEBUG(dbgs() << "ARM Loops: Moved start past: " << *ElemDef);
662 } else {
663 // If we fail to move an instruction and the element count is provided
664 // by a mov, use the mov operand if it will have the same value at the
665 // insertion point
666 MachineOperand Operand = ElemDef->getOperand(1);
667 if (isMovRegOpcode(ElemDef->getOpcode()) &&
668 RDI.getUniqueReachingMIDef(ElemDef, Operand.getReg().asMCReg()) ==
669 RDI.getUniqueReachingMIDef(&*StartInsertPt,
670 Operand.getReg().asMCReg())) {
671 TPNumElements = Operand;
672 NumElements = TPNumElements.getReg();
673 } else {
675 << "ARM Loops: Unable to move element count to loop "
676 << "start instruction.\n");
677 return false;
678 }
679 }
680 }
681 }
682
683 // Especially in the case of while loops, InsertBB may not be the
684 // preheader, so we need to check that the register isn't redefined
685 // before entering the loop.
686 auto CannotProvideElements = [this](MachineBasicBlock *MBB,
687 MCRegister NumElements) {
688 if (MBB->empty())
689 return false;
690 // NumElements is redefined in this block.
691 if (RDI.hasLocalDefBefore(&MBB->back(), NumElements))
692 return true;
693
694 // Don't continue searching up through multiple predecessors.
695 if (MBB->pred_size() > 1)
696 return true;
697
698 return false;
699 };
700
701 // Search backwards for a def, until we get to InsertBB.
702 MachineBasicBlock *MBB = Preheader;
703 while (MBB && MBB != StartInsertBB) {
704 if (CannotProvideElements(MBB, NumElements)) {
705 LLVM_DEBUG(dbgs() << "ARM Loops: Unable to provide element count.\n");
706 return false;
707 }
708 MBB = *MBB->pred_begin();
709 }
710 }
711
712 // Could inserting the [W|D]LSTP cause some unintended affects? In a perfect
713 // world the [w|d]lstp instruction would be last instruction in the preheader
714 // and so it would only affect instructions within the loop body. But due to
715 // scheduling, and/or the logic in this pass (above), the insertion point can
716 // be moved earlier. So if the Loop Start isn't the last instruction in the
717 // preheader, and if the initial element count is smaller than the vector
718 // width, the Loop Start instruction will immediately generate one or more
719 // false lane mask which can, incorrectly, affect the proceeding MVE
720 // instructions in the preheader.
721 if (std::any_of(StartInsertPt, StartInsertBB->end(), shouldInspect)) {
722 LLVM_DEBUG(dbgs() << "ARM Loops: Instruction blocks [W|D]LSTP\n");
723 return false;
724 }
725
726 // For any DoubleWidthResultInstrs we found whilst scanning instructions, they
727 // need to compute an output size that is smaller than the VCTP mask operates
728 // on. The VecSize of the DoubleWidthResult is the larger vector size - the
729 // size it extends into, so any VCTP VecSize <= is valid.
730 unsigned VCTPVecSize = getVecSize(*VCTP);
731 for (MachineInstr *MI : DoubleWidthResultInstrs) {
732 unsigned InstrVecSize = getVecSize(*MI);
733 if (InstrVecSize > VCTPVecSize) {
734 LLVM_DEBUG(dbgs() << "ARM Loops: Double width result larger than VCTP "
735 << "VecSize:\n" << *MI);
736 return false;
737 }
738 }
739
740 // Check that the value change of the element count is what we expect and
741 // that the predication will be equivalent. For this we need:
742 // NumElements = NumElements - VectorWidth. The sub will be a sub immediate
743 // and we can also allow register copies within the chain too.
744 auto IsValidSub = [](MachineInstr *MI, int ExpectedVecWidth) {
745 return -getAddSubImmediate(*MI) == ExpectedVecWidth;
746 };
747
748 MachineBasicBlock *MBB = VCTP->getParent();
749 // Remove modifications to the element count since they have no purpose in a
750 // tail predicated loop. Explicitly refer to the vctp operand no matter which
751 // register NumElements has been assigned to, since that is what the
752 // modifications will be using
753 if (auto *Def = RDI.getUniqueReachingMIDef(
754 &MBB->back(), VCTP->getOperand(1).getReg().asMCReg())) {
755 SmallPtrSet<MachineInstr*, 2> ElementChain;
756 SmallPtrSet<MachineInstr*, 2> Ignore;
757 unsigned ExpectedVectorWidth = getTailPredVectorWidth(VCTP->getOpcode());
758
759 Ignore.insert_range(VCTPs);
760
761 if (TryRemove(Def, RDI, ElementChain, Ignore)) {
762 bool FoundSub = false;
763
764 for (auto *MI : ElementChain) {
765 if (isMovRegOpcode(MI->getOpcode()))
766 continue;
767
768 if (isSubImmOpcode(MI->getOpcode())) {
769 if (FoundSub || !IsValidSub(MI, ExpectedVectorWidth)) {
770 LLVM_DEBUG(dbgs() << "ARM Loops: Unexpected instruction in element"
771 " count: " << *MI);
772 return false;
773 }
774 FoundSub = true;
775 } else {
776 LLVM_DEBUG(dbgs() << "ARM Loops: Unexpected instruction in element"
777 " count: " << *MI);
778 return false;
779 }
780 }
781 ToRemove.insert_range(ElementChain);
782 }
783 }
784
785 // If we converted the LoopStart to a t2DoLoopStartTP/t2WhileLoopStartTP, we
786 // can also remove any extra instructions in the preheader, which often
787 // includes a now unused MOV.
788 if ((Start->getOpcode() == ARM::t2DoLoopStartTP ||
789 Start->getOpcode() == ARM::t2WhileLoopStartTP) &&
790 Preheader && !Preheader->empty() &&
791 !RDI.hasLocalDefBefore(VCTP, VCTP->getOperand(1).getReg())) {
792 if (auto *Def = RDI.getUniqueReachingMIDef(
793 &Preheader->back(), VCTP->getOperand(1).getReg().asMCReg())) {
794 SmallPtrSet<MachineInstr *, 2> Ignore(llvm::from_range, VCTPs);
795 TryRemove(Def, RDI, ToRemove, Ignore);
796 }
797 }
798
799 return true;
800}
801
802static bool isRegInClass(const MachineOperand &MO,
803 const TargetRegisterClass *Class) {
804 return MO.isReg() && MO.getReg() && Class->contains(MO.getReg());
805}
806
807// MVE 'narrowing' operate on half a lane, reading from half and writing
808// to half, which are referred to has the top and bottom half. The other
809// half retains its previous value.
811 const MCInstrDesc &MCID = MI.getDesc();
812 uint64_t Flags = MCID.TSFlags;
813 return (Flags & ARMII::RetainsPreviousHalfElement) != 0;
814}
815
816// Some MVE instructions read from the top/bottom halves of their operand(s)
817// and generate a vector result with result elements that are double the
818// width of the input.
820 const MCInstrDesc &MCID = MI.getDesc();
821 uint64_t Flags = MCID.TSFlags;
822 return (Flags & ARMII::DoubleWidthResult) != 0;
823}
824
825// Can this instruction generate a non-zero result when given only zeroed
826// operands? This allows us to know that, given operands with false bytes
827// zeroed by masked loads, that the result will also contain zeros in those
828// bytes.
830
831 // Check for instructions which can write into a larger element size,
832 // possibly writing into a previous zero'd lane.
834 return true;
835
836 switch (MI.getOpcode()) {
837 default:
838 break;
839 // FIXME: VNEG FP and -0? I think we'll need to handle this once we allow
840 // fp16 -> fp32 vector conversions.
841 // Instructions that perform a NOT will generate 1s from 0s.
842 case ARM::MVE_VMVN:
843 case ARM::MVE_VORN:
844 // Count leading zeros will do just that!
845 case ARM::MVE_VCLZs8:
846 case ARM::MVE_VCLZs16:
847 case ARM::MVE_VCLZs32:
848 return true;
849 }
850 return false;
851}
852
853// Look at its register uses to see if it only can only receive zeros
854// into its false lanes which would then produce zeros. Also check that
855// the output register is also defined by an FalseLanesZero instruction
856// so that if tail-predication happens, the lanes that aren't updated will
857// still be zeros.
859 const TargetRegisterClass *QPRs,
860 const ReachingDefInfo &RDI,
861 InstSet &FalseLanesZero) {
863 return false;
864
865 bool isPredicated = isVectorPredicated(&MI);
866 // Predicated loads will write zeros to the falsely predicated bytes of the
867 // destination register.
868 if (MI.mayLoad())
869 return isPredicated;
870
871 auto IsZeroInit = [](MachineInstr *Def) {
872 return !isVectorPredicated(Def) &&
873 Def->getOpcode() == ARM::MVE_VMOVimmi32 &&
874 Def->getOperand(1).getImm() == 0;
875 };
876
877 bool AllowScalars = isHorizontalReduction(MI);
878 for (auto &MO : MI.operands()) {
879 if (!MO.isReg() || !MO.getReg())
880 continue;
881 if (!isRegInClass(MO, QPRs) && AllowScalars)
882 continue;
883 // Skip the lr predicate reg
885 if (PIdx != -1 && (int)MO.getOperandNo() == PIdx + 2)
886 continue;
887
888 // Check that this instruction will produce zeros in its false lanes:
889 // - If it only consumes false lanes zero or constant 0 (vmov #0)
890 // - If it's predicated, it only matters that it's def register already has
891 // false lane zeros, so we can ignore the uses.
893 RDI.getGlobalReachingDefs(&MI, MO.getReg(), Defs);
894 if (Defs.empty())
895 return false;
896 for (auto *Def : Defs) {
897 if (Def == &MI || FalseLanesZero.count(Def) || IsZeroInit(Def))
898 continue;
899 if (MO.isUse() && isPredicated)
900 continue;
901 return false;
902 }
903 }
904 LLVM_DEBUG(dbgs() << "ARM Loops: Always False Zeros: " << MI);
905 return true;
906}
907
908bool LowOverheadLoop::ValidateLiveOuts() {
909 // We want to find out if the tail-predicated version of this loop will
910 // produce the same values as the loop in its original form. For this to
911 // be true, the newly inserted implicit predication must not change the
912 // the (observable) results.
913 // We're doing this because many instructions in the loop will not be
914 // predicated and so the conversion from VPT predication to tail-predication
915 // can result in different values being produced; due to the tail-predication
916 // preventing many instructions from updating their falsely predicated
917 // lanes. This analysis assumes that all the instructions perform lane-wise
918 // operations and don't perform any exchanges.
919 // A masked load, whether through VPT or tail predication, will write zeros
920 // to any of the falsely predicated bytes. So, from the loads, we know that
921 // the false lanes are zeroed and here we're trying to track that those false
922 // lanes remain zero, or where they change, the differences are masked away
923 // by their user(s).
924 // All MVE stores have to be predicated, so we know that any predicate load
925 // operands, or stored results are equivalent already. Other explicitly
926 // predicated instructions will perform the same operation in the original
927 // loop and the tail-predicated form too. Because of this, we can insert
928 // loads, stores and other predicated instructions into our Predicated
929 // set and build from there.
930 const TargetRegisterClass *QPRs = TRI.getRegClass(ARM::MQPRRegClassID);
931 SetVector<MachineInstr *> FalseLanesUnknown;
932 SmallPtrSet<MachineInstr *, 4> FalseLanesZero;
933 SmallPtrSet<MachineInstr *, 4> Predicated;
934 MachineBasicBlock *Header = ML.getHeader();
935
936 LLVM_DEBUG(dbgs() << "ARM Loops: Validating Live outs\n");
937
938 for (auto &MI : *Header) {
939 if (!shouldInspect(MI))
940 continue;
941
942 if (isVCTP(&MI) || isVPTOpcode(MI.getOpcode()))
943 continue;
944
946 bool retainsOrReduces =
948
949 if (isPredicated)
950 Predicated.insert(&MI);
951 if (producesFalseLanesZero(MI, QPRs, RDI, FalseLanesZero))
952 FalseLanesZero.insert(&MI);
953 else if (MI.getNumDefs() == 0)
954 continue;
955 else if (!isPredicated && retainsOrReduces) {
956 LLVM_DEBUG(dbgs() << " Unpredicated instruction that retainsOrReduces: " << MI);
957 return false;
958 } else if (!isPredicated && MI.getOpcode() != ARM::MQPRCopy)
959 FalseLanesUnknown.insert(&MI);
960 }
961
962 LLVM_DEBUG({
963 dbgs() << " Predicated:\n";
964 for (auto *I : Predicated)
965 dbgs() << " " << *I;
966 dbgs() << " FalseLanesZero:\n";
967 for (auto *I : FalseLanesZero)
968 dbgs() << " " << *I;
969 dbgs() << " FalseLanesUnknown:\n";
970 for (auto *I : FalseLanesUnknown)
971 dbgs() << " " << *I;
972 });
973
974 auto HasPredicatedUsers = [this](MachineInstr *MI, const MachineOperand &MO,
975 SmallPtrSetImpl<MachineInstr *> &Predicated) {
976 SmallPtrSet<MachineInstr *, 2> Uses;
977 RDI.getGlobalUses(MI, MO.getReg().asMCReg(), Uses);
978 for (auto *Use : Uses) {
979 if (Use != MI && !Predicated.count(Use))
980 return false;
981 }
982 return true;
983 };
984
985 // Visit the unknowns in reverse so that we can start at the values being
986 // stored and then we can work towards the leaves, hopefully adding more
987 // instructions to Predicated. Successfully terminating the loop means that
988 // all the unknown values have to found to be masked by predicated user(s).
989 // For any unpredicated values, we store them in NonPredicated so that we
990 // can later check whether these form a reduction.
991 SmallPtrSet<MachineInstr*, 2> NonPredicated;
992 for (auto *MI : reverse(FalseLanesUnknown)) {
993 for (auto &MO : MI->operands()) {
994 if (!isRegInClass(MO, QPRs) || !MO.isDef())
995 continue;
996 if (!HasPredicatedUsers(MI, MO, Predicated)) {
997 LLVM_DEBUG(dbgs() << " Found an unknown def of : "
998 << TRI.getRegAsmName(MO.getReg()) << " at " << *MI);
999 NonPredicated.insert(MI);
1000 break;
1001 }
1002 }
1003 // Any unknown false lanes have been masked away by the user(s).
1004 if (!NonPredicated.contains(MI))
1005 Predicated.insert(MI);
1006 }
1007
1008 SmallPtrSet<MachineInstr *, 2> LiveOutMIs;
1010 ML.getExitBlocks(ExitBlocks);
1011 assert(ML.getNumBlocks() == 1 && "Expected single block loop!");
1012 assert(ExitBlocks.size() == 1 && "Expected a single exit block");
1013 MachineBasicBlock *ExitBB = ExitBlocks.front();
1014 for (const MachineBasicBlock::RegisterMaskPair &RegMask : ExitBB->liveins()) {
1015 // TODO: Instead of blocking predication, we could move the vctp to the exit
1016 // block and calculate it's operand there in or the preheader.
1017 if (RegMask.PhysReg == ARM::VPR) {
1018 LLVM_DEBUG(dbgs() << " VPR is live in to the exit block.");
1019 return false;
1020 }
1021 // Check Q-regs that are live in the exit blocks. We don't collect scalars
1022 // because they won't be affected by lane predication.
1023 if (QPRs->contains(RegMask.PhysReg))
1024 if (auto *MI = RDI.getLocalLiveOutMIDef(Header, RegMask.PhysReg))
1025 LiveOutMIs.insert(MI);
1026 }
1027
1028 // We've already validated that any VPT predication within the loop will be
1029 // equivalent when we perform the predication transformation; so we know that
1030 // any VPT predicated instruction is predicated upon VCTP. Any live-out
1031 // instruction needs to be predicated, so check this here. The instructions
1032 // in NonPredicated have been found to be a reduction that we can ensure its
1033 // legality. Any MQPRCopy found will need to validate its input as if it was
1034 // live out.
1035 SmallVector<MachineInstr *> Worklist(LiveOutMIs.begin(), LiveOutMIs.end());
1036 while (!Worklist.empty()) {
1037 MachineInstr *MI = Worklist.pop_back_val();
1038 if (MI->getOpcode() == ARM::MQPRCopy) {
1039 VMOVCopies.insert(MI);
1040 MachineInstr *CopySrc =
1041 RDI.getUniqueReachingMIDef(MI, MI->getOperand(1).getReg());
1042 if (CopySrc)
1043 Worklist.push_back(CopySrc);
1044 } else if (NonPredicated.count(MI) && FalseLanesUnknown.contains(MI)) {
1045 LLVM_DEBUG(dbgs() << " Unable to handle live out: " << *MI);
1046 VMOVCopies.clear();
1047 return false;
1048 }
1049 }
1050
1051 return true;
1052}
1053
1054void LowOverheadLoop::Validate(ARMBasicBlockUtils *BBUtils) {
1055 if (Revert)
1056 return;
1057
1058 // Check branch target ranges: WLS[TP] can only branch forwards and LE[TP]
1059 // can only jump back.
1060 auto ValidateRanges = [](MachineInstr *Start, MachineInstr *End,
1061 ARMBasicBlockUtils *BBUtils, MachineLoop &ML) {
1062 MachineBasicBlock *TgtBB = End->getOpcode() == ARM::t2LoopEnd
1063 ? End->getOperand(1).getMBB()
1064 : End->getOperand(2).getMBB();
1065 // TODO Maybe there's cases where the target doesn't have to be the header,
1066 // but for now be safe and revert.
1067 if (TgtBB != ML.getHeader()) {
1068 LLVM_DEBUG(dbgs() << "ARM Loops: LoopEnd is not targeting header.\n");
1069 return false;
1070 }
1071
1072 // The WLS and LE instructions have 12-bits for the label offset. WLS
1073 // requires a positive offset, while LE uses negative.
1074 if (BBUtils->getOffsetOf(End) < BBUtils->getOffsetOf(ML.getHeader()) ||
1075 !BBUtils->isBBInRange(End, ML.getHeader(), 4094)) {
1076 LLVM_DEBUG(dbgs() << "ARM Loops: LE offset is out-of-range\n");
1077 return false;
1078 }
1079
1080 if (isWhileLoopStart(*Start)) {
1081 MachineBasicBlock *TargetBB = getWhileLoopStartTargetBB(*Start);
1082 if (BBUtils->getOffsetOf(Start) > BBUtils->getOffsetOf(TargetBB) ||
1083 !BBUtils->isBBInRange(Start, TargetBB, 4094)) {
1084 LLVM_DEBUG(dbgs() << "ARM Loops: WLS offset is out-of-range!\n");
1085 return false;
1086 }
1087 }
1088 return true;
1089 };
1090
1091 StartInsertPt = MachineBasicBlock::iterator(Start);
1092 StartInsertBB = Start->getParent();
1093 LLVM_DEBUG(dbgs() << "ARM Loops: Will insert LoopStart at "
1094 << *StartInsertPt);
1095
1096 Revert = !ValidateRanges(Start, End, BBUtils, ML);
1097 CannotTailPredicate = !ValidateTailPredicate();
1098}
1099
1100bool LowOverheadLoop::AddVCTP(MachineInstr *MI) {
1101 LLVM_DEBUG(dbgs() << "ARM Loops: Adding VCTP: " << *MI);
1102 if (VCTPs.empty()) {
1103 VCTPs.push_back(MI);
1104 return true;
1105 }
1106
1107 // If we find another VCTP, check whether it uses the same value as the main VCTP.
1108 // If it does, store it in the VCTPs set, else refuse it.
1109 MachineInstr *Prev = VCTPs.back();
1110 if (!Prev->getOperand(1).isIdenticalTo(MI->getOperand(1)) ||
1111 !RDI.hasSameReachingDef(Prev, MI, MI->getOperand(1).getReg().asMCReg())) {
1112 LLVM_DEBUG(dbgs() << "ARM Loops: Found VCTP with a different reaching "
1113 "definition from the main VCTP");
1114 return false;
1115 }
1116 VCTPs.push_back(MI);
1117 return true;
1118}
1119
1121
1122 auto GetFrameIndex = [](MachineMemOperand *Operand) {
1123 const PseudoSourceValue *PseudoValue = Operand->getPseudoValue();
1124 if (PseudoValue && PseudoValue->kind() == PseudoSourceValue::FixedStack) {
1125 if (const auto *FS = dyn_cast<FixedStackPseudoSourceValue>(PseudoValue)) {
1126 return FS->getFrameIndex();
1127 }
1128 }
1129 return -1;
1130 };
1131
1132 auto IsStackOp = [GetFrameIndex](MachineInstr *I) {
1133 switch (I->getOpcode()) {
1134 case ARM::MVE_VSTRWU32:
1135 case ARM::MVE_VLDRWU32: {
1136 return I->getOperand(1).getReg() == ARM::SP &&
1137 I->memoperands().size() == 1 &&
1138 GetFrameIndex(I->memoperands().front()) >= 0;
1139 }
1140 default:
1141 return false;
1142 }
1143 };
1144
1145 // An unpredicated vector register spill is allowed if all of the uses of the
1146 // stack slot are within the loop
1147 if (MI->getOpcode() != ARM::MVE_VSTRWU32 || !IsStackOp(MI))
1148 return false;
1149
1150 // Search all blocks after the loop for accesses to the same stack slot.
1151 // ReachingDefAnalysis doesn't work for sp as it relies on registers being
1152 // live-out (which sp never is) to know what blocks to look in
1153 if (MI->memoperands().size() == 0)
1154 return false;
1155 int FI = GetFrameIndex(MI->memoperands().front());
1156
1157 auto &FrameInfo = MI->getParent()->getParent()->getFrameInfo();
1158 if (FI == -1 || !FrameInfo.isSpillSlotObjectIndex(FI))
1159 return false;
1160
1162 ML->getExitBlocks(Frontier);
1163 SmallPtrSet<MachineBasicBlock *, 4> Visited{MI->getParent()};
1164 unsigned Idx = 0;
1165 while (Idx < Frontier.size()) {
1166 MachineBasicBlock *BB = Frontier[Idx];
1167 bool LookAtSuccessors = true;
1168 for (auto &I : *BB) {
1169 if (!IsStackOp(&I) || I.memoperands().size() == 0)
1170 continue;
1171 if (GetFrameIndex(I.memoperands().front()) != FI)
1172 continue;
1173 // If this block has a store to the stack slot before any loads then we
1174 // can ignore the block
1175 if (I.getOpcode() == ARM::MVE_VSTRWU32) {
1176 LookAtSuccessors = false;
1177 break;
1178 }
1179 // If the store and the load are using the same stack slot then the
1180 // store isn't valid for tail predication
1181 if (I.getOpcode() == ARM::MVE_VLDRWU32)
1182 return false;
1183 }
1184
1185 if (LookAtSuccessors) {
1186 for (auto *Succ : BB->successors()) {
1187 if (!Visited.contains(Succ) && !is_contained(Frontier, Succ))
1188 Frontier.push_back(Succ);
1189 }
1190 }
1191 Visited.insert(BB);
1192 Idx++;
1193 }
1194
1195 return true;
1196}
1197
1198bool LowOverheadLoop::ValidateMVEInst(MachineInstr *MI) {
1199 if (CannotTailPredicate)
1200 return false;
1201
1202 if (!shouldInspect(*MI))
1203 return true;
1204
1205 if (MI->getOpcode() == ARM::MVE_VPSEL ||
1206 MI->getOpcode() == ARM::MVE_VPNOT) {
1207 // TODO: Allow VPSEL and VPNOT, we currently cannot because:
1208 // 1) It will use the VPR as a predicate operand, but doesn't have to be
1209 // instead a VPT block, which means we can assert while building up
1210 // the VPT block because we don't find another VPT or VPST to being a new
1211 // one.
1212 // 2) VPSEL still requires a VPR operand even after tail predicating,
1213 // which means we can't remove it unless there is another
1214 // instruction, such as vcmp, that can provide the VPR def.
1215 return false;
1216 }
1217
1218 // Record all VCTPs and check that they're equivalent to one another.
1219 if (isVCTP(MI) && !AddVCTP(MI))
1220 return false;
1221
1222 // Inspect uses first so that any instructions that alter the VPR don't
1223 // alter the predicate upon themselves.
1224 const MCInstrDesc &MCID = MI->getDesc();
1225 bool IsUse = false;
1226 unsigned LastOpIdx = MI->getNumOperands() - 1;
1227 for (const auto &Op : enumerate(reverse(MCID.operands()))) {
1228 const MachineOperand &MO = MI->getOperand(LastOpIdx - Op.index());
1229 if (!MO.isReg() || !MO.isUse() || MO.getReg() != ARM::VPR)
1230 continue;
1231
1232 if (ARM::isVpred(Op.value().OperandType)) {
1233 VPTstate.addInst(MI);
1234 IsUse = true;
1235 } else if (MI->getOpcode() != ARM::MVE_VPST) {
1236 LLVM_DEBUG(dbgs() << "ARM Loops: Found instruction using vpr: " << *MI);
1237 return false;
1238 }
1239 }
1240
1241 // If we find an instruction that has been marked as not valid for tail
1242 // predication, only allow the instruction if it's contained within a valid
1243 // VPT block.
1244 bool RequiresExplicitPredication =
1246 if (isDomainMVE(MI) && RequiresExplicitPredication) {
1247 if (MI->getOpcode() == ARM::MQPRCopy)
1248 return true;
1249 if (!IsUse && producesDoubleWidthResult(*MI)) {
1250 DoubleWidthResultInstrs.insert(MI);
1251 return true;
1252 }
1253
1254 LLVM_DEBUG(if (!IsUse) dbgs()
1255 << "ARM Loops: Can't tail predicate: " << *MI);
1256 return IsUse;
1257 }
1258
1259 // If the instruction is already explicitly predicated, then the conversion
1260 // will be fine, but ensure that all store operations are predicated.
1261 if (MI->mayStore() && !ValidateMVEStore(MI, &ML))
1262 return IsUse;
1263
1264 // If this instruction defines the VPR, update the predicate for the
1265 // proceeding instructions.
1266 if (isVectorPredicate(MI)) {
1267 // Clear the existing predicate when we're not in VPT Active state,
1268 // otherwise we add to it.
1269 if (!isVectorPredicated(MI))
1270 VPTstate.resetPredicate(MI);
1271 else
1272 VPTstate.addPredicate(MI);
1273 }
1274
1275 // Finally once the predicate has been modified, we can start a new VPT
1276 // block if necessary.
1277 if (isVPTOpcode(MI->getOpcode()))
1278 VPTstate.CreateVPTBlock(MI);
1279
1280 return true;
1281}
1282
1283bool ARMLowOverheadLoops::runOnMachineFunction(MachineFunction &mf) {
1284 const ARMSubtarget &ST = mf.getSubtarget<ARMSubtarget>();
1285 if (!ST.hasLOB())
1286 return false;
1287
1288 MF = &mf;
1289 LLVM_DEBUG(dbgs() << "ARM Loops on " << MF->getName() << " ------------- \n");
1290
1291 MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
1292 RDI = &getAnalysis<ReachingDefInfoWrapperPass>().getRDI();
1293 MF->getProperties().setTracksLiveness();
1294 MRI = &MF->getRegInfo();
1295 TII = ST.getInstrInfo();
1296 TRI = ST.getRegisterInfo();
1297 BBUtils = std::make_unique<ARMBasicBlockUtils>(*MF);
1298 BBUtils->computeAllBlockSizes();
1299 BBUtils->adjustBBOffsetsAfter(&MF->front());
1300
1301 bool Changed = false;
1302 for (auto *ML : *MLI) {
1303 if (ML->isOutermost())
1304 Changed |= ProcessLoop(ML);
1305 }
1306 Changed |= RevertNonLoops();
1307 return Changed;
1308}
1309
1310bool ARMLowOverheadLoops::ProcessLoop(MachineLoop *ML) {
1311 bool Changed = false;
1312
1313 // Process inner loops first.
1314 for (MachineLoop *L : *ML)
1315 Changed |= ProcessLoop(L);
1316
1317 LLVM_DEBUG({
1318 dbgs() << "ARM Loops: Processing loop containing:\n";
1319 if (auto *Preheader = ML->getLoopPreheader())
1320 dbgs() << " - Preheader: " << printMBBReference(*Preheader) << "\n";
1321 else if (auto *Preheader = MLI->findLoopPreheader(ML, true, true))
1322 dbgs() << " - Preheader: " << printMBBReference(*Preheader) << "\n";
1323 for (auto *MBB : ML->getBlocks())
1324 dbgs() << " - Block: " << printMBBReference(*MBB) << "\n";
1325 });
1326
1327 // Search the given block for a loop start instruction. If one isn't found,
1328 // and there's only one predecessor block, search that one too.
1329 std::function<MachineInstr*(MachineBasicBlock*)> SearchForStart =
1330 [&SearchForStart](MachineBasicBlock *MBB) -> MachineInstr* {
1331 for (auto &MI : *MBB) {
1332 if (isLoopStart(MI))
1333 return &MI;
1334 }
1335 if (MBB->pred_size() == 1)
1336 return SearchForStart(*MBB->pred_begin());
1337 return nullptr;
1338 };
1339
1340 LowOverheadLoop LoLoop(*ML, *MLI, *RDI, *TRI, *TII);
1341 // Search the preheader for the start intrinsic.
1342 // FIXME: I don't see why we shouldn't be supporting multiple predecessors
1343 // with potentially multiple set.loop.iterations, so we need to enable this.
1344 if (LoLoop.Preheader)
1345 LoLoop.Start = SearchForStart(LoLoop.Preheader);
1346 else
1347 return Changed;
1348
1349 // Find the low-overhead loop components and decide whether or not to fall
1350 // back to a normal loop. Also look for a vctp instructions and decide
1351 // whether we can convert that predicate using tail predication.
1352 for (auto *MBB : reverse(ML->getBlocks())) {
1353 for (auto &MI : *MBB) {
1354 if (MI.isDebugValue())
1355 continue;
1356 else if (MI.getOpcode() == ARM::t2LoopDec)
1357 LoLoop.Dec = &MI;
1358 else if (MI.getOpcode() == ARM::t2LoopEnd)
1359 LoLoop.End = &MI;
1360 else if (MI.getOpcode() == ARM::t2LoopEndDec)
1361 LoLoop.End = LoLoop.Dec = &MI;
1362 else if (isLoopStart(MI))
1363 LoLoop.Start = &MI;
1364 else if (MI.getDesc().isCall()) {
1365 // TODO: Though the call will require LE to execute again, does this
1366 // mean we should revert? Always executing LE hopefully should be
1367 // faster than performing a sub,cmp,br or even subs,br.
1368 LoLoop.Revert = true;
1369 LLVM_DEBUG(dbgs() << "ARM Loops: Found call.\n");
1370 } else {
1371 // Record VPR defs and build up their corresponding vpt blocks.
1372 // Check we know how to tail predicate any mve instructions.
1373 LoLoop.AnalyseMVEInst(&MI);
1374 }
1375 }
1376 }
1377
1378 LLVM_DEBUG(LoLoop.dump());
1379 if (!LoLoop.FoundAllComponents()) {
1380 LLVM_DEBUG(dbgs() << "ARM Loops: Didn't find loop start, update, end\n");
1381 return Changed;
1382 }
1383
1384 assert(LoLoop.Start->getOpcode() != ARM::t2WhileLoopStart &&
1385 "Expected t2WhileLoopStart to be removed before regalloc!");
1386
1387 // Check that the only instruction using LoopDec is LoopEnd. This can only
1388 // happen when the Dec and End are separate, not a single t2LoopEndDec.
1389 // TODO: Check for copy chains that really have no effect.
1390 if (LoLoop.Dec != LoLoop.End) {
1391 SmallPtrSet<MachineInstr *, 2> Uses;
1392 RDI->getReachingLocalUses(LoLoop.Dec, MCRegister::from(ARM::LR), Uses);
1393 if (Uses.size() > 1 || !Uses.count(LoLoop.End)) {
1394 LLVM_DEBUG(dbgs() << "ARM Loops: Unable to remove LoopDec.\n");
1395 LoLoop.Revert = true;
1396 }
1397 }
1398 LoLoop.Validate(BBUtils.get());
1399 Expand(LoLoop);
1400 return true;
1401}
1402
1403// WhileLoopStart holds the exit block, so produce a cmp lr, 0 and then a
1404// beq that branches to the exit branch.
1405// TODO: We could also try to generate a cbz if the value in LR is also in
1406// another low register.
1407void ARMLowOverheadLoops::RevertWhile(MachineInstr *MI) const {
1408 LLVM_DEBUG(dbgs() << "ARM Loops: Reverting to cmp: " << *MI);
1409 MachineBasicBlock *DestBB = getWhileLoopStartTargetBB(*MI);
1410 unsigned BrOpc = BBUtils->isBBInRange(MI, DestBB, 254) ?
1411 ARM::tBcc : ARM::t2Bcc;
1412
1413 RevertWhileLoopStartLR(MI, TII, BrOpc);
1414}
1415
1416void ARMLowOverheadLoops::RevertDo(MachineInstr *MI) const {
1417 LLVM_DEBUG(dbgs() << "ARM Loops: Reverting to mov: " << *MI);
1419}
1420
1421bool ARMLowOverheadLoops::RevertLoopDec(MachineInstr *MI) const {
1422 LLVM_DEBUG(dbgs() << "ARM Loops: Reverting to sub: " << *MI);
1423 MachineBasicBlock *MBB = MI->getParent();
1424 SmallPtrSet<MachineInstr*, 1> Ignore;
1425 for (auto I = MachineBasicBlock::iterator(MI), E = MBB->end(); I != E; ++I) {
1426 if (I->getOpcode() == ARM::t2LoopEnd) {
1427 Ignore.insert(&*I);
1428 break;
1429 }
1430 }
1431
1432 // If nothing defines CPSR between LoopDec and LoopEnd, use a t2SUBS.
1433 bool SetFlags =
1435
1436 llvm::RevertLoopDec(MI, TII, SetFlags);
1437 return SetFlags;
1438}
1439
1440// Generate a subs, or sub and cmp, and a branch instead of an LE.
1441void ARMLowOverheadLoops::RevertLoopEnd(MachineInstr *MI, bool SkipCmp) const {
1442 LLVM_DEBUG(dbgs() << "ARM Loops: Reverting to cmp, br: " << *MI);
1443
1444 MachineBasicBlock *DestBB = MI->getOperand(1).getMBB();
1445 unsigned BrOpc = BBUtils->isBBInRange(MI, DestBB, 254) ?
1446 ARM::tBcc : ARM::t2Bcc;
1447
1448 llvm::RevertLoopEnd(MI, TII, BrOpc, SkipCmp);
1449}
1450
1451// Generate a subs, or sub and cmp, and a branch instead of an LE.
1452void ARMLowOverheadLoops::RevertLoopEndDec(MachineInstr *MI) const {
1453 LLVM_DEBUG(dbgs() << "ARM Loops: Reverting to subs, br: " << *MI);
1454 assert(MI->getOpcode() == ARM::t2LoopEndDec && "Expected a t2LoopEndDec!");
1455 MachineBasicBlock *MBB = MI->getParent();
1456
1457 MachineInstrBuilder MIB =
1458 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(ARM::t2SUBri));
1459 MIB.addDef(ARM::LR);
1460 MIB.add(MI->getOperand(1));
1461 MIB.addImm(1);
1462 MIB.addImm(ARMCC::AL);
1463 MIB.addReg(ARM::NoRegister);
1464 MIB.addReg(ARM::CPSR);
1465 MIB->getOperand(5).setIsDef(true);
1466
1467 MachineBasicBlock *DestBB = MI->getOperand(2).getMBB();
1468 unsigned BrOpc =
1469 BBUtils->isBBInRange(MI, DestBB, 254) ? ARM::tBcc : ARM::t2Bcc;
1470
1471 // Create bne
1472 MIB = BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(BrOpc));
1473 MIB.add(MI->getOperand(2)); // branch target
1474 MIB.addImm(ARMCC::NE); // condition code
1475 MIB.addReg(ARM::CPSR);
1476
1477 MI->eraseFromParent();
1478}
1479
1480// Perform dead code elimation on the loop iteration count setup expression.
1481// If we are tail-predicating, the number of elements to be processed is the
1482// operand of the VCTP instruction in the vector body, see getCount(), which is
1483// register $r3 in this example:
1484//
1485// $lr = big-itercount-expression
1486// ..
1487// $lr = t2DoLoopStart renamable $lr
1488// vector.body:
1489// ..
1490// $vpr = MVE_VCTP32 renamable $r3
1491// renamable $lr = t2LoopDec killed renamable $lr, 1
1492// t2LoopEnd renamable $lr, %vector.body
1493// tB %end
1494//
1495// What we would like achieve here is to replace the do-loop start pseudo
1496// instruction t2DoLoopStart with:
1497//
1498// $lr = MVE_DLSTP_32 killed renamable $r3
1499//
1500// Thus, $r3 which defines the number of elements, is written to $lr,
1501// and then we want to delete the whole chain that used to define $lr,
1502// see the comment below how this chain could look like.
1503//
1504void ARMLowOverheadLoops::IterationCountDCE(LowOverheadLoop &LoLoop) {
1505 if (!LoLoop.IsTailPredicationLegal())
1506 return;
1507
1508 LLVM_DEBUG(dbgs() << "ARM Loops: Trying DCE on loop iteration count.\n");
1509
1510 MachineInstr *Def = RDI->getMIOperand(LoLoop.Start, 1);
1511 if (!Def) {
1512 LLVM_DEBUG(dbgs() << "ARM Loops: Couldn't find iteration count.\n");
1513 return;
1514 }
1515
1516 // Collect and remove the users of iteration count.
1517 SmallPtrSet<MachineInstr*, 4> Killed = { LoLoop.Start, LoLoop.Dec,
1518 LoLoop.End };
1519 if (!TryRemove(Def, *RDI, LoLoop.ToRemove, Killed))
1520 LLVM_DEBUG(dbgs() << "ARM Loops: Unsafe to remove loop iteration count.\n");
1521}
1522
1523MachineInstr* ARMLowOverheadLoops::ExpandLoopStart(LowOverheadLoop &LoLoop) {
1524 LLVM_DEBUG(dbgs() << "ARM Loops: Expanding LoopStart.\n");
1525 // When using tail-predication, try to delete the dead code that was used to
1526 // calculate the number of loop iterations.
1527 IterationCountDCE(LoLoop);
1528
1529 MachineBasicBlock::iterator InsertPt = LoLoop.StartInsertPt;
1530 MachineInstr *Start = LoLoop.Start;
1531 MachineBasicBlock *MBB = LoLoop.StartInsertBB;
1532 unsigned Opc = LoLoop.getStartOpcode();
1533 MachineOperand &Count = LoLoop.getLoopStartOperand();
1534
1535 // A DLS lr, lr we needn't emit
1536 MachineInstr* NewStart;
1537 if (!DisableOmitDLS && Opc == ARM::t2DLS && Count.isReg() &&
1538 Count.getReg() == ARM::LR) {
1539 LLVM_DEBUG(dbgs() << "ARM Loops: Didn't insert start: DLS lr, lr");
1540 NewStart = nullptr;
1541 } else {
1542 MachineInstrBuilder MIB =
1543 BuildMI(*MBB, InsertPt, Start->getDebugLoc(), TII->get(Opc));
1544
1545 MIB.addDef(ARM::LR);
1546 MIB.add(Count);
1547 if (isWhileLoopStart(*Start))
1548 MIB.addMBB(getWhileLoopStartTargetBB(*Start));
1549
1550 LLVM_DEBUG(dbgs() << "ARM Loops: Inserted start: " << *MIB);
1551 NewStart = &*MIB;
1552 }
1553
1554 LoLoop.ToRemove.insert(Start);
1555 return NewStart;
1556}
1557
1558void ARMLowOverheadLoops::ConvertVPTBlocks(LowOverheadLoop &LoLoop) {
1559 auto RemovePredicate = [](MachineInstr *MI) {
1560 if (MI->isDebugInstr())
1561 return;
1562 LLVM_DEBUG(dbgs() << "ARM Loops: Removing predicate from: " << *MI);
1564 assert(PIdx >= 1 && "Trying to unpredicate a non-predicated instruction");
1565 assert(MI->getOperand(PIdx).getImm() == ARMVCC::Then &&
1566 "Expected Then predicate!");
1567 MI->getOperand(PIdx).setImm(ARMVCC::None);
1568 MI->getOperand(PIdx + 1).setReg(0);
1569 };
1570
1571 for (auto &Block : LoLoop.getVPTBlocks()) {
1572 SmallVectorImpl<MachineInstr *> &Insts = Block.getInsts();
1573
1574 auto ReplaceVCMPWithVPT = [&](MachineInstr *&TheVCMP, MachineInstr *At) {
1575 assert(TheVCMP && "Replacing a removed or non-existent VCMP");
1576 // Replace the VCMP with a VPT
1577 MachineInstrBuilder MIB =
1578 BuildMI(*At->getParent(), At, At->getDebugLoc(),
1579 TII->get(VCMPOpcodeToVPT(TheVCMP->getOpcode())));
1580 MIB.addImm(ARMVCC::Then);
1581 // Register one
1582 MIB.add(TheVCMP->getOperand(1));
1583 // Register two
1584 MIB.add(TheVCMP->getOperand(2));
1585 // The comparison code, e.g. ge, eq, lt
1586 MIB.add(TheVCMP->getOperand(3));
1587 LLVM_DEBUG(dbgs() << "ARM Loops: Combining with VCMP to VPT: " << *MIB);
1588 LoLoop.BlockMasksToRecompute.insert(MIB.getInstr());
1589 LoLoop.ToRemove.insert(TheVCMP);
1590 TheVCMP = nullptr;
1591 };
1592
1593 if (LoLoop.VPTstate.isEntryPredicatedOnVCTP(Block, /*exclusive*/ true)) {
1594 MachineInstr *VPST = Insts.front();
1595 if (Block.hasUniformPredicate()) {
1596 // A vpt block starting with VPST, is only predicated upon vctp and has no
1597 // internal vpr defs:
1598 // - Remove vpst.
1599 // - Unpredicate the remaining instructions.
1600 LLVM_DEBUG(dbgs() << "ARM Loops: Removing VPST: " << *VPST);
1601 for (unsigned i = 1; i < Insts.size(); ++i)
1602 RemovePredicate(Insts[i]);
1603 } else {
1604 // The VPT block has a non-uniform predicate but it uses a vpst and its
1605 // entry is guarded only by a vctp, which means we:
1606 // - Need to remove the original vpst.
1607 // - Then need to unpredicate any following instructions, until
1608 // we come across the divergent vpr def.
1609 // - Insert a new vpst to predicate the instruction(s) that following
1610 // the divergent vpr def.
1611 MachineInstr *Divergent = Block.getDivergent();
1612 MachineBasicBlock *MBB = Divergent->getParent();
1613 auto DivergentNext = ++MachineBasicBlock::iterator(Divergent);
1614 while (DivergentNext != MBB->end() && DivergentNext->isDebugInstr())
1615 ++DivergentNext;
1616
1617 bool DivergentNextIsPredicated =
1618 DivergentNext != MBB->end() &&
1619 getVPTInstrPredicate(*DivergentNext) != ARMVCC::None;
1620
1621 for (auto I = ++MachineBasicBlock::iterator(VPST), E = DivergentNext;
1622 I != E; ++I)
1623 RemovePredicate(&*I);
1624
1625 // Check if the instruction defining vpr is a vcmp so it can be combined
1626 // with the VPST This should be the divergent instruction
1627 MachineInstr *VCMP =
1628 VCMPOpcodeToVPT(Divergent->getOpcode()) != 0 ? Divergent : nullptr;
1629
1630 if (DivergentNextIsPredicated) {
1631 // Insert a VPST at the divergent only if the next instruction
1632 // would actually use it. A VCMP following a VPST can be
1633 // merged into a VPT so do that instead if the VCMP exists.
1634 if (!VCMP) {
1635 // Create a VPST (with a null mask for now, we'll recompute it
1636 // later)
1637 MachineInstrBuilder MIB =
1638 BuildMI(*Divergent->getParent(), Divergent,
1639 Divergent->getDebugLoc(), TII->get(ARM::MVE_VPST));
1640 MIB.addImm(0);
1641 LLVM_DEBUG(dbgs() << "ARM Loops: Created VPST: " << *MIB);
1642 LoLoop.BlockMasksToRecompute.insert(MIB.getInstr());
1643 } else {
1644 // No RDI checks are necessary here since the VPST would have been
1645 // directly after the VCMP
1646 ReplaceVCMPWithVPT(VCMP, VCMP);
1647 }
1648 }
1649 }
1650 LLVM_DEBUG(dbgs() << "ARM Loops: Removing VPST: " << *VPST);
1651 LoLoop.ToRemove.insert(VPST);
1652 } else if (Block.containsVCTP()) {
1653 // The vctp will be removed, so either the entire block will be dead or
1654 // the block mask of the vp(s)t will need to be recomputed.
1655 MachineInstr *VPST = Insts.front();
1656 if (Block.size() == 2) {
1657 assert(VPST->getOpcode() == ARM::MVE_VPST &&
1658 "Found a VPST in an otherwise empty vpt block");
1659 LoLoop.ToRemove.insert(VPST);
1660 } else
1661 LoLoop.BlockMasksToRecompute.insert(VPST);
1662 } else if (Insts.front()->getOpcode() == ARM::MVE_VPST) {
1663 // If this block starts with a VPST then attempt to merge it with the
1664 // preceeding un-merged VCMP into a VPT. This VCMP comes from a VPT
1665 // block that no longer exists
1666 MachineInstr *VPST = Insts.front();
1667 auto Next = ++MachineBasicBlock::iterator(VPST);
1669 "The instruction after a VPST must be predicated");
1670 (void)Next;
1671 MachineInstr *VprDef = RDI->getUniqueReachingMIDef(VPST, ARM::VPR);
1672 if (VprDef && VCMPOpcodeToVPT(VprDef->getOpcode()) &&
1673 !LoLoop.ToRemove.contains(VprDef)) {
1674 MachineInstr *VCMP = VprDef;
1675 // The VCMP and VPST can only be merged if the VCMP's operands will have
1676 // the same values at the VPST.
1677 // If any of the instructions between the VCMP and VPST are predicated
1678 // then a different code path is expected to have merged the VCMP and
1679 // VPST already.
1680 if (std::none_of(++MachineBasicBlock::iterator(VCMP),
1682 RDI->hasSameReachingDef(VCMP, VPST, VCMP->getOperand(1).getReg()) &&
1683 RDI->hasSameReachingDef(VCMP, VPST, VCMP->getOperand(2).getReg())) {
1684 ReplaceVCMPWithVPT(VCMP, VPST);
1685 LLVM_DEBUG(dbgs() << "ARM Loops: Removing VPST: " << *VPST);
1686 LoLoop.ToRemove.insert(VPST);
1687 }
1688 }
1689 }
1690 }
1691
1692 LoLoop.ToRemove.insert_range(LoLoop.VCTPs);
1693}
1694
1695void ARMLowOverheadLoops::Expand(LowOverheadLoop &LoLoop) {
1696
1697 // Combine the LoopDec and LoopEnd instructions into LE(TP).
1698 auto ExpandLoopEnd = [this](LowOverheadLoop &LoLoop) {
1699 MachineInstr *End = LoLoop.End;
1700 MachineBasicBlock *MBB = End->getParent();
1701 unsigned Opc = LoLoop.IsTailPredicationLegal() ?
1702 ARM::MVE_LETP : ARM::t2LEUpdate;
1703 MachineInstrBuilder MIB = BuildMI(*MBB, End, End->getDebugLoc(),
1704 TII->get(Opc));
1705 MIB.addDef(ARM::LR);
1706 unsigned Off = LoLoop.Dec == LoLoop.End ? 1 : 0;
1707 MIB.add(End->getOperand(Off + 0));
1708 MIB.add(End->getOperand(Off + 1));
1709 LLVM_DEBUG(dbgs() << "ARM Loops: Inserted LE: " << *MIB);
1710 LoLoop.ToRemove.insert(LoLoop.Dec);
1711 LoLoop.ToRemove.insert(End);
1712 return &*MIB;
1713 };
1714
1715 // TODO: We should be able to automatically remove these branches before we
1716 // get here - probably by teaching analyzeBranch about the pseudo
1717 // instructions.
1718 // If there is an unconditional branch, after I, that just branches to the
1719 // next block, remove it.
1720 auto RemoveDeadBranch = [](MachineInstr *I) {
1721 MachineBasicBlock *BB = I->getParent();
1722 MachineInstr *Terminator = &BB->instr_back();
1723 if (Terminator->isUnconditionalBranch() && I != Terminator) {
1724 MachineBasicBlock *Succ = Terminator->getOperand(0).getMBB();
1725 if (BB->isLayoutSuccessor(Succ)) {
1726 LLVM_DEBUG(dbgs() << "ARM Loops: Removing branch: " << *Terminator);
1727 Terminator->eraseFromParent();
1728 }
1729 }
1730 };
1731
1732 // And VMOVCopies need to become 2xVMOVD for tail predication to be valid.
1733 // Anything other MQPRCopy can be converted to MVE_VORR later on.
1734 auto ExpandVMOVCopies = [this](SmallPtrSet<MachineInstr *, 4> &VMOVCopies) {
1735 for (auto *MI : VMOVCopies) {
1736 LLVM_DEBUG(dbgs() << "Converting copy to VMOVD: " << *MI);
1737 assert(MI->getOpcode() == ARM::MQPRCopy && "Only expected MQPRCOPY!");
1738 MachineBasicBlock *MBB = MI->getParent();
1739 Register Dst = MI->getOperand(0).getReg();
1740 Register Src = MI->getOperand(1).getReg();
1741 auto MIB1 = BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(ARM::VMOVD),
1742 ARM::D0 + (Dst - ARM::Q0) * 2)
1743 .addReg(ARM::D0 + (Src - ARM::Q0) * 2)
1745 (void)MIB1;
1746 LLVM_DEBUG(dbgs() << " into " << *MIB1);
1747 auto MIB2 = BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(ARM::VMOVD),
1748 ARM::D0 + (Dst - ARM::Q0) * 2 + 1)
1749 .addReg(ARM::D0 + (Src - ARM::Q0) * 2 + 1)
1751 LLVM_DEBUG(dbgs() << " and " << *MIB2);
1752 (void)MIB2;
1753 MI->eraseFromParent();
1754 }
1755 };
1756
1757 if (LoLoop.Revert) {
1758 if (isWhileLoopStart(*LoLoop.Start))
1759 RevertWhile(LoLoop.Start);
1760 else
1761 RevertDo(LoLoop.Start);
1762 if (LoLoop.Dec == LoLoop.End)
1763 RevertLoopEndDec(LoLoop.End);
1764 else
1765 RevertLoopEnd(LoLoop.End, RevertLoopDec(LoLoop.Dec));
1766 } else {
1767 ExpandVMOVCopies(LoLoop.VMOVCopies);
1768 LoLoop.Start = ExpandLoopStart(LoLoop);
1769 if (LoLoop.Start)
1770 RemoveDeadBranch(LoLoop.Start);
1771 LoLoop.End = ExpandLoopEnd(LoLoop);
1772 RemoveDeadBranch(LoLoop.End);
1773 if (LoLoop.IsTailPredicationLegal())
1774 ConvertVPTBlocks(LoLoop);
1775 for (auto *I : LoLoop.ToRemove) {
1776 LLVM_DEBUG(dbgs() << "ARM Loops: Erasing " << *I);
1777 I->eraseFromParent();
1778 }
1779 for (auto *I : LoLoop.BlockMasksToRecompute) {
1780 LLVM_DEBUG(dbgs() << "ARM Loops: Recomputing VPT/VPST Block Mask: " << *I);
1782 LLVM_DEBUG(dbgs() << " ... done: " << *I);
1783 }
1784 }
1785
1786 PostOrderLoopTraversal DFS(LoLoop.ML, *MLI);
1787 DFS.ProcessLoop();
1788 const SmallVectorImpl<MachineBasicBlock*> &PostOrder = DFS.getOrder();
1789 fullyRecomputeLiveIns(PostOrder);
1790
1791 for (auto *MBB : reverse(PostOrder))
1793
1794 // We've moved, removed and inserted new instructions, so update RDI.
1795 RDI->reset();
1796}
1797
1798bool ARMLowOverheadLoops::RevertNonLoops() {
1799 LLVM_DEBUG(dbgs() << "ARM Loops: Reverting any remaining pseudos...\n");
1800 bool Changed = false;
1801
1802 for (auto &MBB : *MF) {
1803 SmallVector<MachineInstr*, 4> Starts;
1804 SmallVector<MachineInstr*, 4> Decs;
1805 SmallVector<MachineInstr*, 4> Ends;
1806 SmallVector<MachineInstr *, 4> EndDecs;
1807 SmallVector<MachineInstr *, 4> MQPRCopies;
1808
1809 for (auto &I : MBB) {
1810 if (isLoopStart(I))
1811 Starts.push_back(&I);
1812 else if (I.getOpcode() == ARM::t2LoopDec)
1813 Decs.push_back(&I);
1814 else if (I.getOpcode() == ARM::t2LoopEnd)
1815 Ends.push_back(&I);
1816 else if (I.getOpcode() == ARM::t2LoopEndDec)
1817 EndDecs.push_back(&I);
1818 else if (I.getOpcode() == ARM::MQPRCopy)
1819 MQPRCopies.push_back(&I);
1820 }
1821
1822 if (Starts.empty() && Decs.empty() && Ends.empty() && EndDecs.empty() &&
1823 MQPRCopies.empty())
1824 continue;
1825
1826 Changed = true;
1827
1828 for (auto *Start : Starts) {
1829 if (isWhileLoopStart(*Start))
1830 RevertWhile(Start);
1831 else
1832 RevertDo(Start);
1833 }
1834 for (auto *Dec : Decs)
1835 RevertLoopDec(Dec);
1836
1837 for (auto *End : Ends)
1838 RevertLoopEnd(End);
1839 for (auto *End : EndDecs)
1840 RevertLoopEndDec(End);
1841 for (auto *MI : MQPRCopies) {
1842 LLVM_DEBUG(dbgs() << "Converting copy to VORR: " << *MI);
1843 assert(MI->getOpcode() == ARM::MQPRCopy && "Only expected MQPRCOPY!");
1844 MachineBasicBlock *MBB = MI->getParent();
1845 auto MIB = BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(ARM::MVE_VORR),
1846 MI->getOperand(0).getReg())
1847 .add(MI->getOperand(1))
1848 .add(MI->getOperand(1));
1849 addUnpredicatedMveVpredROp(MIB, MI->getOperand(0).getReg());
1850 MI->eraseFromParent();
1851 }
1852 }
1853 return Changed;
1854}
1855
1857 return new ARMLowOverheadLoops();
1858}
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static int getVecSize(const AMDGPULibFunc &FInfo)
static bool isDomainMVE(MachineInstr *MI)
static bool isVectorPredicated(MachineInstr *MI)
ReachingDefInfo & RDI
static bool canGenerateNonZeros(const MachineInstr &MI)
static bool isHorizontalReduction(const MachineInstr &MI)
static bool producesDoubleWidthResult(const MachineInstr &MI)
static bool hasVPRUse(MachineInstr &MI)
static bool isRegInClass(const MachineOperand &MO, const TargetRegisterClass *Class)
static bool ValidateMVEStore(MachineInstr *MI, MachineLoop *ML)
static bool isVectorPredicate(MachineInstr *MI)
static bool retainsPreviousHalfElement(const MachineInstr &MI)
static bool shouldInspect(MachineInstr &MI)
static bool producesFalseLanesZero(MachineInstr &MI, const TargetRegisterClass *QPRs, const ReachingDefInfo &RDI, InstSet &FalseLanesZero)
static cl::opt< bool > DisableTailPredication("arm-loloops-disable-tailpred", cl::Hidden, cl::desc("Disable tail-predication in the ARM LowOverheadLoop pass"), cl::init(false))
ReachingDefInfo InstSet InstSet & Ignore
if(!RDI.isSafeToRemove(MI, Uses, Ignore)) return false
static int getVecSize(const MachineInstr &MI)
#define ARM_LOW_OVERHEAD_LOOPS_NAME
static cl::opt< bool > DisableOmitDLS("arm-disable-omit-dls", cl::Hidden, cl::desc("Disable omitting 'dls lr, lr' instructions"), cl::init(false))
ReachingDefInfo InstSet & ToRemove
MachineBasicBlock & MBB
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate any type of IT block"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow complex IT blocks")))
static const Function * getParent(const Value *V)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
#define I(x, y, z)
Definition MD5.cpp:58
static ARM::PredBlockMask CreateVPTBlock(MachineBasicBlock::instr_iterator &Iter, MachineBasicBlock::instr_iterator EndIter, SmallVectorImpl< MachineInstr * > &DeadInstructions)
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
if(PassOpts->AAPipeline)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
Remove Loads Into Fake Uses
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
This file implements a set that has insertion order iteration characteristics.
#define LLVM_DEBUG(...)
Definition Debug.h:114
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:270
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
Describe properties that are true of each instruction in the target description file.
ArrayRef< MCOperandInfo > operands() const
static MCRegister from(unsigned Val)
Check the provided unsigned value is a valid MCRegister.
Definition MCRegister.h:69
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
iterator_range< livein_iterator > liveins() const
LLVM_ABI bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
iterator_range< succ_iterator > successors()
iterator insertAfter(iterator I, MachineInstr *MI)
Insert MI into the instruction list after I.
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.
const MachineFunctionProperties & getProperties() const
Get the function properties.
const MachineBasicBlock & front() const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
A description of a memory reference used in the backend.
MachineOperand class - Representation of each machine instruction operand.
LLVM_ABI unsigned getOperandNo() const
Returns the index of this operand in the instruction that it belongs to.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
LLVM_ABI bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
LLVM_ABI void setIsDef(bool Val=true)
Change a def to a use, or a use to a def.
Special value supplied for machine level alias analysis.
This class provides the reaching def analysis.
MachineInstr * getUniqueReachingMIDef(MachineInstr *MI, Register Reg) const
If a single MachineInstr creates the reaching definition, then return it.
bool isReachingDefLiveOut(MachineInstr *MI, Register Reg) const
Return whether the reaching def for MI also is live out of its parent block.
bool isSafeToMoveForwards(MachineInstr *From, MachineInstr *To) const
Return whether From can be moved forwards to just before To.
void getReachingLocalUses(MachineInstr *MI, Register Reg, InstSet &Uses) const
Provides the uses, in the same block as MI, of register that MI defines.
bool hasLocalDefBefore(MachineInstr *MI, Register Reg) const
Provide whether the register has been defined in the same basic block as, and before,...
void reset()
Re-run the analysis.
void getGlobalUses(MachineInstr *MI, Register Reg, InstSet &Uses) const
Collect the users of the value stored in Reg, which is defined by MI.
MachineInstr * getMIOperand(MachineInstr *MI, unsigned Idx) const
If a single MachineInstr creates the reaching definition, for MIs operand at Idx, then return it.
bool isSafeToMoveBackwards(MachineInstr *From, MachineInstr *To) const
Return whether From can be moved backwards to just after To.
bool hasSameReachingDef(MachineInstr *A, MachineInstr *B, Register Reg) const
Return whether A and B use the same def of Reg.
void getGlobalReachingDefs(MachineInstr *MI, Register Reg, InstSet &Defs) const
Collect all possible definitions of the value stored in Reg, which is used by MI.
MachineInstr * getLocalLiveOutMIDef(MachineBasicBlock *MBB, Register Reg) const
Return the local MI that produces the live out value for Reg, or nullptr for a non-live out or non-lo...
bool isSafeToDefRegAt(MachineInstr *MI, Register Reg) const
Return whether a MachineInstr could be inserted at MI and safely define the given register without af...
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition Register.h:102
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:104
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:168
bool contains(const key_type &key) const
Check if the SetVector contains the given key.
Definition SetVector.h:269
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
iterator end() const
void insert_range(Range &&R)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
iterator begin() const
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
bool contains(Register Reg) const
Return true if the specified register is included in this register class.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
Changed
@ ValidForTailPredication
@ RetainsPreviousHalfElement
bool isPredicated(const MCInst &MI, const MCInstrInfo *MCII)
bool isVpred(OperandType op)
initializer< Ty > init(const Ty &Val)
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
This is an optimization pass for GlobalISel generic memory operations.
static bool isDoLoopStart(const MachineInstr &MI)
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:318
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
int findFirstVPTPredOperandIdx(const MachineInstr &MI)
ARMVCC::VPTCodes getVPTInstrPredicate(const MachineInstr &MI, Register &PredReg)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1705
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1657
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
static bool isVCTP(const MachineInstr *MI)
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2452
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
constexpr from_range_t from_range
static bool isVPTOpcode(int Opc)
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2116
static unsigned getTailPredVectorWidth(unsigned Opcode)
static std::array< MachineOperand, 2 > predOps(ARMCC::CondCodes Pred, unsigned PredReg=0)
Get the operands corresponding to the given Pred value.
FunctionPass * createARMLowOverheadLoopsPass()
static bool isMovRegOpcode(int Opc)
static bool isSubImmOpcode(int Opc)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1712
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
static bool isLoopStart(const MachineInstr &MI)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
void RevertWhileLoopStartLR(MachineInstr *MI, const TargetInstrInfo *TII, unsigned BrOpc=ARM::t2Bcc, bool UseCmp=false)
void recomputeLivenessFlags(MachineBasicBlock &MBB)
Recomputes dead and kill flags in MBB.
static unsigned VCTPOpcodeToLSTP(unsigned Opcode, bool IsDoLoop)
void addUnpredicatedMveVpredROp(MachineInstrBuilder &MIB, Register DestReg)
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
DWARFExpression::Operation Op
void RevertLoopEnd(MachineInstr *MI, const TargetInstrInfo *TII, unsigned BrOpc=ARM::t2Bcc, bool SkipCmp=false)
void RevertLoopDec(MachineInstr *MI, const TargetInstrInfo *TII, bool SetFlags=false)
MachineBasicBlock * getWhileLoopStartTargetBB(const MachineInstr &MI)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1877
static bool isWhileLoopStart(const MachineInstr &MI)
static unsigned VCMPOpcodeToVPT(unsigned Opcode)
void RevertDoLoopStart(MachineInstr *MI, const TargetInstrInfo *TII)
int getAddSubImmediate(MachineInstr &MI)
void recomputeVPTBlockMask(MachineInstr &Instr)
void fullyRecomputeLiveIns(ArrayRef< MachineBasicBlock * > MBBs)
Convenience function for recomputing live-in's for a set of MBBs until the computation converges.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.