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, t2WhileLoopStart and t2LoopEnd
35 /// are defined to be as large as this maximum sequence of replacement
36 /// instructions.
37 ///
38 //===----------------------------------------------------------------------===//
39 
40 #include "ARM.h"
41 #include "ARMBaseInstrInfo.h"
42 #include "ARMBaseRegisterInfo.h"
43 #include "ARMBasicBlockInfo.h"
44 #include "ARMSubtarget.h"
45 #include "Thumb2InstrInfo.h"
46 #include "llvm/ADT/SetOperations.h"
47 #include "llvm/ADT/SmallSet.h"
48 #include "llvm/CodeGen/MachineFunctionPass.h"
49 #include "llvm/CodeGen/MachineLoopInfo.h"
50 #include "llvm/CodeGen/MachineLoopUtils.h"
51 #include "llvm/CodeGen/MachineRegisterInfo.h"
52 #include "llvm/CodeGen/Passes.h"
53 #include "llvm/CodeGen/ReachingDefAnalysis.h"
54 #include "llvm/MC/MCInstrDesc.h"
55 
56 using namespace llvm;
57 
58 #define DEBUG_TYPE "arm-low-overhead-loops"
59 #define ARM_LOW_OVERHEAD_LOOPS_NAME "ARM Low Overhead Loops pass"
60 
61 namespace {
62 
63   struct PredicatedMI {
64     MachineInstr *MI = nullptr;
65     SetVector<MachineInstr*> Predicates;
66 
67   public:
68     PredicatedMI(MachineInstr *I, SetVector<MachineInstr*> &Preds) :
69     MI(I) {
70       Predicates.insert(Preds.begin(), Preds.end());
71     }
72   };
73 
74   // Represent a VPT block, a list of instructions that begins with a VPST and
75   // has a maximum of four proceeding instructions. All instructions within the
76   // block are predicated upon the vpr and we allow instructions to define the
77   // vpr within in the block too.
78   class VPTBlock {
79     std::unique_ptr<PredicatedMI> VPST;
80     PredicatedMI *Divergent = nullptr;
81     SmallVector<PredicatedMI, 4> Insts;
82 
83   public:
84     VPTBlock(MachineInstr *MI, SetVector<MachineInstr*> &Preds) {
85       VPST = std::make_unique<PredicatedMI>(MI, Preds);
86     }
87 
88     void addInst(MachineInstr *MI, SetVector<MachineInstr*> &Preds) {
89       LLVM_DEBUG(dbgs() << "ARM Loops: Adding predicated MI: " << *MI);
90       if (!Divergent && !set_difference(Preds, VPST->Predicates).empty()) {
91         Divergent = &Insts.back();
92         LLVM_DEBUG(dbgs() << " - has divergent predicate: " << *Divergent->MI);
93       }
94       Insts.emplace_back(MI, Preds);
95       assert(Insts.size() <= 4 && "Too many instructions in VPT block!");
96     }
97 
98     // Have we found an instruction within the block which defines the vpr? If
99     // so, not all the instructions in the block will have the same predicate.
100     bool HasNonUniformPredicate() const {
101       return Divergent != nullptr;
102     }
103 
104     // Is the given instruction part of the predicate set controlling the entry
105     // to the block.
106     bool IsPredicatedOn(MachineInstr *MI) const {
107       return VPST->Predicates.count(MI);
108     }
109 
110     // Is the given instruction the only predicate which controls the entry to
111     // the block.
112     bool IsOnlyPredicatedOn(MachineInstr *MI) const {
113       return IsPredicatedOn(MI) && VPST->Predicates.size() == 1;
114     }
115 
116     unsigned size() const { return Insts.size(); }
117     SmallVectorImpl<PredicatedMI> &getInsts() { return Insts; }
118     MachineInstr *getVPST() const { return VPST->MI; }
119     PredicatedMI *getDivergent() const { return Divergent; }
120   };
121 
122   struct LowOverheadLoop {
123 
124     MachineLoop *ML = nullptr;
125     MachineFunction *MF = nullptr;
126     MachineInstr *InsertPt = nullptr;
127     MachineInstr *Start = nullptr;
128     MachineInstr *Dec = nullptr;
129     MachineInstr *End = nullptr;
130     MachineInstr *VCTP = nullptr;
131     VPTBlock *CurrentBlock = nullptr;
132     SetVector<MachineInstr*> CurrentPredicate;
133     SmallVector<VPTBlock, 4> VPTBlocks;
134     bool Revert = false;
135     bool CannotTailPredicate = false;
136 
137     LowOverheadLoop(MachineLoop *ML) : ML(ML) {
138       MF = ML->getHeader()->getParent();
139     }
140 
141     // If this is an MVE instruction, check that we know how to use tail
142     // predication with it. Record VPT blocks and return whether the
143     // instruction is valid for tail predication.
144     bool ValidateMVEInst(MachineInstr *MI);
145 
146     void AnalyseMVEInst(MachineInstr *MI) {
147       CannotTailPredicate = !ValidateMVEInst(MI);
148     }
149 
150     bool IsTailPredicationLegal() const {
151       // For now, let's keep things really simple and only support a single
152       // block for tail predication.
153       return !Revert && FoundAllComponents() && VCTP &&
154              !CannotTailPredicate && ML->getNumBlocks() == 1;
155     }
156 
157     bool ValidateTailPredicate(MachineInstr *StartInsertPt,
158                                ReachingDefAnalysis *RDA,
159                                MachineLoopInfo *MLI);
160 
161     // Is it safe to define LR with DLS/WLS?
162     // LR can be defined if it is the operand to start, because it's the same
163     // value, or if it's going to be equivalent to the operand to Start.
164     MachineInstr *IsSafeToDefineLR(ReachingDefAnalysis *RDA);
165 
166     // Check the branch targets are within range and we satisfy our
167     // restrictions.
168     void CheckLegality(ARMBasicBlockUtils *BBUtils, ReachingDefAnalysis *RDA,
169                        MachineLoopInfo *MLI);
170 
171     bool FoundAllComponents() const {
172       return Start && Dec && End;
173     }
174 
175     SmallVectorImpl<VPTBlock> &getVPTBlocks() { return VPTBlocks; }
176 
177     // Return the loop iteration count, or the number of elements if we're tail
178     // predicating.
179     MachineOperand &getCount() {
180       return IsTailPredicationLegal() ?
181         VCTP->getOperand(1) : Start->getOperand(0);
182     }
183 
184     unsigned getStartOpcode() const {
185       bool IsDo = Start->getOpcode() == ARM::t2DoLoopStart;
186       if (!IsTailPredicationLegal())
187         return IsDo ? ARM::t2DLS : ARM::t2WLS;
188 
189       return VCTPOpcodeToLSTP(VCTP->getOpcode(), IsDo);
190     }
191 
192     void dump() const {
193       if (Start) dbgs() << "ARM Loops: Found Loop Start: " << *Start;
194       if (Dec) dbgs() << "ARM Loops: Found Loop Dec: " << *Dec;
195       if (End) dbgs() << "ARM Loops: Found Loop End: " << *End;
196       if (VCTP) dbgs() << "ARM Loops: Found VCTP: " << *VCTP;
197       if (!FoundAllComponents())
198         dbgs() << "ARM Loops: Not a low-overhead loop.\n";
199       else if (!(Start && Dec && End))
200         dbgs() << "ARM Loops: Failed to find all loop components.\n";
201     }
202   };
203 
204   class ARMLowOverheadLoops : public MachineFunctionPass {
205     MachineFunction           *MF = nullptr;
206     MachineLoopInfo           *MLI = nullptr;
207     ReachingDefAnalysis       *RDA = nullptr;
208     const ARMBaseInstrInfo    *TII = nullptr;
209     MachineRegisterInfo       *MRI = nullptr;
210     const TargetRegisterInfo  *TRI = nullptr;
211     std::unique_ptr<ARMBasicBlockUtils> BBUtils = nullptr;
212 
213   public:
214     static char ID;
215 
216     ARMLowOverheadLoops() : MachineFunctionPass(ID) { }
217 
218     void getAnalysisUsage(AnalysisUsage &AU) const override {
219       AU.setPreservesCFG();
220       AU.addRequired<MachineLoopInfo>();
221       AU.addRequired<ReachingDefAnalysis>();
222       MachineFunctionPass::getAnalysisUsage(AU);
223     }
224 
225     bool runOnMachineFunction(MachineFunction &MF) override;
226 
227     MachineFunctionProperties getRequiredProperties() const override {
228       return MachineFunctionProperties().set(
229           MachineFunctionProperties::Property::NoVRegs).set(
230           MachineFunctionProperties::Property::TracksLiveness);
231     }
232 
233     StringRef getPassName() const override {
234       return ARM_LOW_OVERHEAD_LOOPS_NAME;
235     }
236 
237   private:
238     bool ProcessLoop(MachineLoop *ML);
239 
240     bool RevertNonLoops();
241 
242     void RevertWhile(MachineInstr *MI) const;
243 
244     bool RevertLoopDec(MachineInstr *MI, bool AllowFlags = false) const;
245 
246     void RevertLoopEnd(MachineInstr *MI, bool SkipCmp = false) const;
247 
248     void RemoveLoopUpdate(LowOverheadLoop &LoLoop);
249 
250     void ConvertVPTBlocks(LowOverheadLoop &LoLoop);
251 
252     MachineInstr *ExpandLoopStart(LowOverheadLoop &LoLoop);
253 
254     void Expand(LowOverheadLoop &LoLoop);
255 
256   };
257 }
258 
259 char ARMLowOverheadLoops::ID = 0;
260 
261 INITIALIZE_PASS(ARMLowOverheadLoops, DEBUG_TYPE, ARM_LOW_OVERHEAD_LOOPS_NAME,
262                 false, false)
263 
264 MachineInstr *LowOverheadLoop::IsSafeToDefineLR(ReachingDefAnalysis *RDA) {
265   // We can define LR because LR already contains the same value.
266   if (Start->getOperand(0).getReg() == ARM::LR)
267     return Start;
268 
269   unsigned CountReg = Start->getOperand(0).getReg();
270   auto IsMoveLR = [&CountReg](MachineInstr *MI) {
271     return MI->getOpcode() == ARM::tMOVr &&
272            MI->getOperand(0).getReg() == ARM::LR &&
273            MI->getOperand(1).getReg() == CountReg &&
274            MI->getOperand(2).getImm() == ARMCC::AL;
275    };
276 
277   MachineBasicBlock *MBB = Start->getParent();
278 
279   // Find an insertion point:
280   // - Is there a (mov lr, Count) before Start? If so, and nothing else writes
281   //   to Count before Start, we can insert at that mov.
282   if (auto *LRDef = RDA->getReachingMIDef(Start, ARM::LR))
283     if (IsMoveLR(LRDef) && RDA->hasSameReachingDef(Start, LRDef, CountReg))
284       return LRDef;
285 
286   // - Is there a (mov lr, Count) after Start? If so, and nothing else writes
287   //   to Count after Start, we can insert at that mov.
288   if (auto *LRDef = RDA->getLocalLiveOutMIDef(MBB, ARM::LR))
289     if (IsMoveLR(LRDef) && RDA->hasSameReachingDef(Start, LRDef, CountReg))
290       return LRDef;
291 
292   // We've found no suitable LR def and Start doesn't use LR directly. Can we
293   // just define LR anyway?
294   if (!RDA->isRegUsedAfter(Start, ARM::LR))
295     return Start;
296 
297   return nullptr;
298 }
299 
300 // Can we safely move 'From' to just before 'To'? To satisfy this, 'From' must
301 // not define a register that is used by any instructions, after and including,
302 // 'To'. These instructions also must not redefine any of Froms operands.
303 template<typename Iterator>
304 static bool IsSafeToMove(MachineInstr *From, MachineInstr *To, ReachingDefAnalysis *RDA) {
305   SmallSet<int, 2> Defs;
306   // First check that From would compute the same value if moved.
307   for (auto &MO : From->operands()) {
308     if (!MO.isReg() || MO.isUndef() || !MO.getReg())
309       continue;
310     if (MO.isDef())
311       Defs.insert(MO.getReg());
312     else if (!RDA->hasSameReachingDef(From, To, MO.getReg()))
313       return false;
314   }
315 
316   // Now walk checking that the rest of the instructions will compute the same
317   // value.
318   for (auto I = ++Iterator(From), E = Iterator(To); I != E; ++I) {
319     for (auto &MO : I->operands())
320       if (MO.isReg() && MO.getReg() && MO.isUse() && Defs.count(MO.getReg()))
321         return false;
322   }
323   return true;
324 }
325 
326 bool LowOverheadLoop::ValidateTailPredicate(MachineInstr *StartInsertPt,
327     ReachingDefAnalysis *RDA, MachineLoopInfo *MLI) {
328   assert(VCTP && "VCTP instruction expected but is not set");
329   // All predication within the loop should be based on vctp. If the block
330   // isn't predicated on entry, check whether the vctp is within the block
331   // and that all other instructions are then predicated on it.
332   for (auto &Block : VPTBlocks) {
333     if (Block.IsPredicatedOn(VCTP))
334       continue;
335     if (!Block.HasNonUniformPredicate() || !isVCTP(Block.getDivergent()->MI)) {
336       LLVM_DEBUG(dbgs() << "ARM Loops: Found unsupported diverging predicate: "
337                  << *Block.getDivergent()->MI);
338       return false;
339     }
340     SmallVectorImpl<PredicatedMI> &Insts = Block.getInsts();
341     for (auto &PredMI : Insts) {
342       if (PredMI.Predicates.count(VCTP) || isVCTP(PredMI.MI))
343         continue;
344       LLVM_DEBUG(dbgs() << "ARM Loops: Can't convert: " << *PredMI.MI
345                         << " - which is predicated on:\n";
346                         for (auto *MI : PredMI.Predicates)
347                           dbgs() << "   - " << *MI;
348                  );
349       return false;
350     }
351   }
352 
353   // For tail predication, we need to provide the number of elements, instead
354   // of the iteration count, to the loop start instruction. The number of
355   // elements is provided to the vctp instruction, so we need to check that
356   // we can use this register at InsertPt.
357   Register NumElements = VCTP->getOperand(1).getReg();
358 
359   // If the register is defined within loop, then we can't perform TP.
360   // TODO: Check whether this is just a mov of a register that would be
361   // available.
362   if (RDA->getReachingDef(VCTP, NumElements) >= 0) {
363     LLVM_DEBUG(dbgs() << "ARM Loops: VCTP operand is defined in the loop.\n");
364     return false;
365   }
366 
367   // The element count register maybe defined after InsertPt, in which case we
368   // need to try to move either InsertPt or the def so that the [w|d]lstp can
369   // use the value.
370   MachineBasicBlock *InsertBB = InsertPt->getParent();
371   if (!RDA->isReachingDefLiveOut(InsertPt, NumElements)) {
372     if (auto *ElemDef = RDA->getLocalLiveOutMIDef(InsertBB, NumElements)) {
373       if (IsSafeToMove<MachineBasicBlock::reverse_iterator>(ElemDef, InsertPt, RDA)) {
374         ElemDef->removeFromParent();
375         InsertBB->insert(MachineBasicBlock::iterator(InsertPt), ElemDef);
376         LLVM_DEBUG(dbgs() << "ARM Loops: Moved element count def: "
377                    << *ElemDef);
378       } else if (IsSafeToMove<MachineBasicBlock::iterator>(InsertPt, ElemDef, RDA)) {
379         InsertPt->removeFromParent();
380         InsertBB->insertAfter(MachineBasicBlock::iterator(ElemDef), InsertPt);
381         LLVM_DEBUG(dbgs() << "ARM Loops: Moved start past: " << *ElemDef);
382       } else {
383         LLVM_DEBUG(dbgs() << "ARM Loops: Unable to move element count to loop "
384                    << "start instruction.\n");
385         return false;
386       }
387     }
388   }
389 
390   // Especially in the case of while loops, InsertBB may not be the
391   // preheader, so we need to check that the register isn't redefined
392   // before entering the loop.
393   auto CannotProvideElements = [&RDA](MachineBasicBlock *MBB,
394                                       Register NumElements) {
395     // NumElements is redefined in this block.
396     if (RDA->getReachingDef(&MBB->back(), NumElements) >= 0)
397       return true;
398 
399     // Don't continue searching up through multiple predecessors.
400     if (MBB->pred_size() > 1)
401       return true;
402 
403     return false;
404   };
405 
406   // First, find the block that looks like the preheader.
407   MachineBasicBlock *MBB = MLI->findLoopPreheader(ML, true);
408   if (!MBB) {
409     LLVM_DEBUG(dbgs() << "ARM Loops: Didn't find preheader.\n");
410     return false;
411   }
412 
413   // Then search backwards for a def, until we get to InsertBB.
414   while (MBB != InsertBB) {
415     if (CannotProvideElements(MBB, NumElements)) {
416       LLVM_DEBUG(dbgs() << "ARM Loops: Unable to provide element count.\n");
417       return false;
418     }
419     MBB = *MBB->pred_begin();
420   }
421 
422   LLVM_DEBUG(dbgs() << "ARM Loops: Will use tail predication.\n");
423   return true;
424 }
425 
426 void LowOverheadLoop::CheckLegality(ARMBasicBlockUtils *BBUtils,
427                                     ReachingDefAnalysis *RDA,
428                                     MachineLoopInfo *MLI) {
429   if (Revert)
430     return;
431 
432   if (!End->getOperand(1).isMBB())
433     report_fatal_error("Expected LoopEnd to target basic block");
434 
435   // TODO Maybe there's cases where the target doesn't have to be the header,
436   // but for now be safe and revert.
437   if (End->getOperand(1).getMBB() != ML->getHeader()) {
438     LLVM_DEBUG(dbgs() << "ARM Loops: LoopEnd is not targetting header.\n");
439     Revert = true;
440     return;
441   }
442 
443   // The WLS and LE instructions have 12-bits for the label offset. WLS
444   // requires a positive offset, while LE uses negative.
445   if (BBUtils->getOffsetOf(End) < BBUtils->getOffsetOf(ML->getHeader()) ||
446       !BBUtils->isBBInRange(End, ML->getHeader(), 4094)) {
447     LLVM_DEBUG(dbgs() << "ARM Loops: LE offset is out-of-range\n");
448     Revert = true;
449     return;
450   }
451 
452   if (Start->getOpcode() == ARM::t2WhileLoopStart &&
453       (BBUtils->getOffsetOf(Start) >
454        BBUtils->getOffsetOf(Start->getOperand(1).getMBB()) ||
455        !BBUtils->isBBInRange(Start, Start->getOperand(1).getMBB(), 4094))) {
456     LLVM_DEBUG(dbgs() << "ARM Loops: WLS offset is out-of-range!\n");
457     Revert = true;
458     return;
459   }
460 
461   InsertPt = Revert ? nullptr : IsSafeToDefineLR(RDA);
462   if (!InsertPt) {
463     LLVM_DEBUG(dbgs() << "ARM Loops: Unable to find safe insertion point.\n");
464     Revert = true;
465     return;
466   } else
467     LLVM_DEBUG(dbgs() << "ARM Loops: Start insertion point: " << *InsertPt);
468 
469   if (!IsTailPredicationLegal()) {
470     LLVM_DEBUG(if (!VCTP)
471                  dbgs() << "ARM Loops: Didn't find a VCTP instruction.\n";
472                dbgs() << "ARM Loops: Tail-predication is not valid.\n");
473     return;
474   }
475 
476   assert(ML->getBlocks().size() == 1 &&
477          "Shouldn't be processing a loop with more than one block");
478   CannotTailPredicate = !ValidateTailPredicate(InsertPt, RDA, MLI);
479   LLVM_DEBUG(if (CannotTailPredicate)
480              dbgs() << "ARM Loops: Couldn't validate tail predicate.\n");
481 }
482 
483 bool LowOverheadLoop::ValidateMVEInst(MachineInstr* MI) {
484   if (CannotTailPredicate)
485     return false;
486 
487   // Only support a single vctp.
488   if (isVCTP(MI) && VCTP)
489     return false;
490 
491   // Start a new vpt block when we discover a vpt.
492   if (MI->getOpcode() == ARM::MVE_VPST) {
493     VPTBlocks.emplace_back(MI, CurrentPredicate);
494     CurrentBlock = &VPTBlocks.back();
495     return true;
496   } else if (isVCTP(MI))
497     VCTP = MI;
498   else if (MI->getOpcode() == ARM::MVE_VPSEL ||
499            MI->getOpcode() == ARM::MVE_VPNOT)
500     return false;
501 
502   // TODO: Allow VPSEL and VPNOT, we currently cannot because:
503   // 1) It will use the VPR as a predicate operand, but doesn't have to be
504   //    instead a VPT block, which means we can assert while building up
505   //    the VPT block because we don't find another VPST to being a new
506   //    one.
507   // 2) VPSEL still requires a VPR operand even after tail predicating,
508   //    which means we can't remove it unless there is another
509   //    instruction, such as vcmp, that can provide the VPR def.
510 
511   bool IsUse = false;
512   bool IsDef = false;
513   const MCInstrDesc &MCID = MI->getDesc();
514   for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
515     const MachineOperand &MO = MI->getOperand(i);
516     if (!MO.isReg() || MO.getReg() != ARM::VPR)
517       continue;
518 
519     if (MO.isDef()) {
520       CurrentPredicate.insert(MI);
521       IsDef = true;
522     } else if (ARM::isVpred(MCID.OpInfo[i].OperandType)) {
523       CurrentBlock->addInst(MI, CurrentPredicate);
524       IsUse = true;
525     } else {
526       LLVM_DEBUG(dbgs() << "ARM Loops: Found instruction using vpr: " << *MI);
527       return false;
528     }
529   }
530 
531   // If we find a vpr def that is not already predicated on the vctp, we've
532   // got disjoint predicates that may not be equivalent when we do the
533   // conversion.
534   if (IsDef && !IsUse && VCTP && !isVCTP(MI)) {
535     LLVM_DEBUG(dbgs() << "ARM Loops: Found disjoint vpr def: " << *MI);
536     return false;
537   }
538 
539   uint64_t Flags = MCID.TSFlags;
540   if ((Flags & ARMII::DomainMask) != ARMII::DomainMVE)
541     return true;
542 
543   // If we find an instruction that has been marked as not valid for tail
544   // predication, only allow the instruction if it's contained within a valid
545   // VPT block.
546   if ((Flags & ARMII::ValidForTailPredication) == 0 && !IsUse) {
547     LLVM_DEBUG(dbgs() << "ARM Loops: Can't tail predicate: " << *MI);
548     return false;
549   }
550 
551   return true;
552 }
553 
554 bool ARMLowOverheadLoops::runOnMachineFunction(MachineFunction &mf) {
555   const ARMSubtarget &ST = static_cast<const ARMSubtarget&>(mf.getSubtarget());
556   if (!ST.hasLOB())
557     return false;
558 
559   MF = &mf;
560   LLVM_DEBUG(dbgs() << "ARM Loops on " << MF->getName() << " ------------- \n");
561 
562   MLI = &getAnalysis<MachineLoopInfo>();
563   RDA = &getAnalysis<ReachingDefAnalysis>();
564   MF->getProperties().set(MachineFunctionProperties::Property::TracksLiveness);
565   MRI = &MF->getRegInfo();
566   TII = static_cast<const ARMBaseInstrInfo*>(ST.getInstrInfo());
567   TRI = ST.getRegisterInfo();
568   BBUtils = std::unique_ptr<ARMBasicBlockUtils>(new ARMBasicBlockUtils(*MF));
569   BBUtils->computeAllBlockSizes();
570   BBUtils->adjustBBOffsetsAfter(&MF->front());
571 
572   bool Changed = false;
573   for (auto ML : *MLI) {
574     if (!ML->getParentLoop())
575       Changed |= ProcessLoop(ML);
576   }
577   Changed |= RevertNonLoops();
578   return Changed;
579 }
580 
581 bool ARMLowOverheadLoops::ProcessLoop(MachineLoop *ML) {
582 
583   bool Changed = false;
584 
585   // Process inner loops first.
586   for (auto I = ML->begin(), E = ML->end(); I != E; ++I)
587     Changed |= ProcessLoop(*I);
588 
589   LLVM_DEBUG(dbgs() << "ARM Loops: Processing loop containing:\n";
590              if (auto *Preheader = ML->getLoopPreheader())
591                dbgs() << " - " << Preheader->getName() << "\n";
592              else if (auto *Preheader = MLI->findLoopPreheader(ML))
593                dbgs() << " - " << Preheader->getName() << "\n";
594              for (auto *MBB : ML->getBlocks())
595                dbgs() << " - " << MBB->getName() << "\n";
596             );
597 
598   // Search the given block for a loop start instruction. If one isn't found,
599   // and there's only one predecessor block, search that one too.
600   std::function<MachineInstr*(MachineBasicBlock*)> SearchForStart =
601     [&SearchForStart](MachineBasicBlock *MBB) -> MachineInstr* {
602     for (auto &MI : *MBB) {
603       if (isLoopStart(MI))
604         return &MI;
605     }
606     if (MBB->pred_size() == 1)
607       return SearchForStart(*MBB->pred_begin());
608     return nullptr;
609   };
610 
611   LowOverheadLoop LoLoop(ML);
612   // Search the preheader for the start intrinsic.
613   // FIXME: I don't see why we shouldn't be supporting multiple predecessors
614   // with potentially multiple set.loop.iterations, so we need to enable this.
615   if (auto *Preheader = ML->getLoopPreheader())
616     LoLoop.Start = SearchForStart(Preheader);
617   else if (auto *Preheader = MLI->findLoopPreheader(ML, true))
618     LoLoop.Start = SearchForStart(Preheader);
619   else
620     return false;
621 
622   // Find the low-overhead loop components and decide whether or not to fall
623   // back to a normal loop. Also look for a vctp instructions and decide
624   // whether we can convert that predicate using tail predication.
625   for (auto *MBB : reverse(ML->getBlocks())) {
626     for (auto &MI : *MBB) {
627       if (MI.getOpcode() == ARM::t2LoopDec)
628         LoLoop.Dec = &MI;
629       else if (MI.getOpcode() == ARM::t2LoopEnd)
630         LoLoop.End = &MI;
631       else if (isLoopStart(MI))
632         LoLoop.Start = &MI;
633       else if (MI.getDesc().isCall()) {
634         // TODO: Though the call will require LE to execute again, does this
635         // mean we should revert? Always executing LE hopefully should be
636         // faster than performing a sub,cmp,br or even subs,br.
637         LoLoop.Revert = true;
638         LLVM_DEBUG(dbgs() << "ARM Loops: Found call.\n");
639       } else {
640         // Record VPR defs and build up their corresponding vpt blocks.
641         // Check we know how to tail predicate any mve instructions.
642         LoLoop.AnalyseMVEInst(&MI);
643       }
644 
645       // We need to ensure that LR is not used or defined inbetween LoopDec and
646       // LoopEnd.
647       if (!LoLoop.Dec || LoLoop.End || LoLoop.Revert)
648         continue;
649 
650       // If we find that LR has been written or read between LoopDec and
651       // LoopEnd, expect that the decremented value is being used else where.
652       // Because this value isn't actually going to be produced until the
653       // latch, by LE, we would need to generate a real sub. The value is also
654       // likely to be copied/reloaded for use of LoopEnd - in which in case
655       // we'd need to perform an add because it gets subtracted again by LE!
656       // The other option is to then generate the other form of LE which doesn't
657       // perform the sub.
658       for (auto &MO : MI.operands()) {
659         if (MI.getOpcode() != ARM::t2LoopDec && MO.isReg() &&
660             MO.getReg() == ARM::LR) {
661           LLVM_DEBUG(dbgs() << "ARM Loops: Found LR Use/Def: " << MI);
662           LoLoop.Revert = true;
663           break;
664         }
665       }
666     }
667   }
668 
669   LLVM_DEBUG(LoLoop.dump());
670   if (!LoLoop.FoundAllComponents()) {
671     LLVM_DEBUG(dbgs() << "ARM Loops: Didn't find loop start, update, end\n");
672     return false;
673   }
674 
675   LoLoop.CheckLegality(BBUtils.get(), RDA, MLI);
676   Expand(LoLoop);
677   return true;
678 }
679 
680 // WhileLoopStart holds the exit block, so produce a cmp lr, 0 and then a
681 // beq that branches to the exit branch.
682 // TODO: We could also try to generate a cbz if the value in LR is also in
683 // another low register.
684 void ARMLowOverheadLoops::RevertWhile(MachineInstr *MI) const {
685   LLVM_DEBUG(dbgs() << "ARM Loops: Reverting to cmp: " << *MI);
686   MachineBasicBlock *MBB = MI->getParent();
687   MachineInstrBuilder MIB = BuildMI(*MBB, MI, MI->getDebugLoc(),
688                                     TII->get(ARM::t2CMPri));
689   MIB.add(MI->getOperand(0));
690   MIB.addImm(0);
691   MIB.addImm(ARMCC::AL);
692   MIB.addReg(ARM::NoRegister);
693 
694   MachineBasicBlock *DestBB = MI->getOperand(1).getMBB();
695   unsigned BrOpc = BBUtils->isBBInRange(MI, DestBB, 254) ?
696     ARM::tBcc : ARM::t2Bcc;
697 
698   MIB = BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(BrOpc));
699   MIB.add(MI->getOperand(1));   // branch target
700   MIB.addImm(ARMCC::EQ);        // condition code
701   MIB.addReg(ARM::CPSR);
702   MI->eraseFromParent();
703 }
704 
705 bool ARMLowOverheadLoops::RevertLoopDec(MachineInstr *MI,
706                                         bool SetFlags) const {
707   LLVM_DEBUG(dbgs() << "ARM Loops: Reverting to sub: " << *MI);
708   MachineBasicBlock *MBB = MI->getParent();
709 
710   // If nothing defines CPSR between LoopDec and LoopEnd, use a t2SUBS.
711   if (SetFlags &&
712       (RDA->isRegUsedAfter(MI, ARM::CPSR) ||
713        !RDA->hasSameReachingDef(MI, &MBB->back(), ARM::CPSR)))
714       SetFlags = false;
715 
716   MachineInstrBuilder MIB = BuildMI(*MBB, MI, MI->getDebugLoc(),
717                                     TII->get(ARM::t2SUBri));
718   MIB.addDef(ARM::LR);
719   MIB.add(MI->getOperand(1));
720   MIB.add(MI->getOperand(2));
721   MIB.addImm(ARMCC::AL);
722   MIB.addReg(0);
723 
724   if (SetFlags) {
725     MIB.addReg(ARM::CPSR);
726     MIB->getOperand(5).setIsDef(true);
727   } else
728     MIB.addReg(0);
729 
730   MI->eraseFromParent();
731   return SetFlags;
732 }
733 
734 // Generate a subs, or sub and cmp, and a branch instead of an LE.
735 void ARMLowOverheadLoops::RevertLoopEnd(MachineInstr *MI, bool SkipCmp) const {
736   LLVM_DEBUG(dbgs() << "ARM Loops: Reverting to cmp, br: " << *MI);
737 
738   MachineBasicBlock *MBB = MI->getParent();
739   // Create cmp
740   if (!SkipCmp) {
741     MachineInstrBuilder MIB = BuildMI(*MBB, MI, MI->getDebugLoc(),
742                                       TII->get(ARM::t2CMPri));
743     MIB.addReg(ARM::LR);
744     MIB.addImm(0);
745     MIB.addImm(ARMCC::AL);
746     MIB.addReg(ARM::NoRegister);
747   }
748 
749   MachineBasicBlock *DestBB = MI->getOperand(1).getMBB();
750   unsigned BrOpc = BBUtils->isBBInRange(MI, DestBB, 254) ?
751     ARM::tBcc : ARM::t2Bcc;
752 
753   // Create bne
754   MachineInstrBuilder MIB =
755     BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(BrOpc));
756   MIB.add(MI->getOperand(1));   // branch target
757   MIB.addImm(ARMCC::NE);        // condition code
758   MIB.addReg(ARM::CPSR);
759   MI->eraseFromParent();
760 }
761 
762 MachineInstr* ARMLowOverheadLoops::ExpandLoopStart(LowOverheadLoop &LoLoop) {
763   MachineInstr *InsertPt = LoLoop.InsertPt;
764   MachineInstr *Start = LoLoop.Start;
765   MachineBasicBlock *MBB = InsertPt->getParent();
766   bool IsDo = Start->getOpcode() == ARM::t2DoLoopStart;
767   unsigned Opc = LoLoop.getStartOpcode();
768   MachineOperand &Count = LoLoop.getCount();
769 
770   MachineInstrBuilder MIB =
771     BuildMI(*MBB, InsertPt, InsertPt->getDebugLoc(), TII->get(Opc));
772 
773   MIB.addDef(ARM::LR);
774   MIB.add(Count);
775   if (!IsDo)
776     MIB.add(Start->getOperand(1));
777 
778   // When using tail-predication, try to delete the dead code that was used to
779   // calculate the number of loop iterations.
780   if (LoLoop.IsTailPredicationLegal()) {
781     SmallVector<MachineInstr*, 4> Killed;
782     SmallVector<MachineInstr*, 4> Dead;
783     if (auto *Def = RDA->getReachingMIDef(Start,
784                                           Start->getOperand(0).getReg())) {
785       Killed.push_back(Def);
786 
787       while (!Killed.empty()) {
788         MachineInstr *Def = Killed.back();
789         Killed.pop_back();
790         Dead.push_back(Def);
791         for (auto &MO : Def->operands()) {
792           if (!MO.isReg() || !MO.isKill())
793             continue;
794 
795           MachineInstr *Kill = RDA->getReachingMIDef(Def, MO.getReg());
796           if (Kill && RDA->getNumUses(Kill, MO.getReg()) == 1)
797             Killed.push_back(Kill);
798         }
799       }
800       for (auto *MI : Dead)
801         MI->eraseFromParent();
802     }
803   }
804 
805   // If we're inserting at a mov lr, then remove it as it's redundant.
806   if (InsertPt != Start)
807     InsertPt->eraseFromParent();
808   Start->eraseFromParent();
809   LLVM_DEBUG(dbgs() << "ARM Loops: Inserted start: " << *MIB);
810   return &*MIB;
811 }
812 
813 // Goal is to optimise and clean-up these loops:
814 //
815 //   vector.body:
816 //     renamable $vpr = MVE_VCTP32 renamable $r3, 0, $noreg
817 //     renamable $r3, dead $cpsr = tSUBi8 killed renamable $r3(tied-def 0), 4
818 //     ..
819 //     $lr = MVE_DLSTP_32 renamable $r3
820 //
821 // The SUB is the old update of the loop iteration count expression, which
822 // is no longer needed. This sub is removed when the element count, which is in
823 // r3 in this example, is defined by an instruction in the loop, and it has
824 // no uses.
825 //
826 void ARMLowOverheadLoops::RemoveLoopUpdate(LowOverheadLoop &LoLoop) {
827   Register ElemCount = LoLoop.VCTP->getOperand(1).getReg();
828   MachineInstr *LastInstrInBlock = &LoLoop.VCTP->getParent()->back();
829 
830   LLVM_DEBUG(dbgs() << "ARM Loops: Trying to remove loop update stmt\n");
831 
832   if (LoLoop.ML->getNumBlocks() != 1) {
833     LLVM_DEBUG(dbgs() << "ARM Loops: Single block loop expected\n");
834     return;
835   }
836 
837   LLVM_DEBUG(dbgs() << "ARM Loops: Analyzing elemcount in operand: ";
838              LoLoop.VCTP->getOperand(1).dump());
839 
840   // Find the definition we are interested in removing, if there is one.
841   MachineInstr *Def = RDA->getReachingMIDef(LastInstrInBlock, ElemCount);
842   if (!Def) {
843     LLVM_DEBUG(dbgs() << "ARM Loops: Can't find a def, nothing to do.\n");
844     return;
845   }
846 
847   // Bail if we define CPSR and it is not dead
848   if (!Def->registerDefIsDead(ARM::CPSR, TRI)) {
849     LLVM_DEBUG(dbgs() << "ARM Loops: CPSR is not dead\n");
850     return;
851   }
852 
853   // Bail if elemcount is used in exit blocks, i.e. if it is live-in.
854   if (isRegLiveInExitBlocks(LoLoop.ML, ElemCount)) {
855     LLVM_DEBUG(dbgs() << "ARM Loops: Elemcount is live-out, can't remove stmt\n");
856     return;
857   }
858 
859   // Bail if there are uses after this Def in the block.
860   SmallVector<MachineInstr*, 4> Uses;
861   RDA->getReachingLocalUses(Def, ElemCount, Uses);
862   if (Uses.size()) {
863     LLVM_DEBUG(dbgs() << "ARM Loops: Local uses in block, can't remove stmt\n");
864     return;
865   }
866 
867   Uses.clear();
868   RDA->getAllInstWithUseBefore(Def, ElemCount, Uses);
869 
870   // Remove Def if there are no uses, or if the only use is the VCTP
871   // instruction.
872   if (!Uses.size() || (Uses.size() == 1 && Uses[0] == LoLoop.VCTP)) {
873     LLVM_DEBUG(dbgs() << "ARM Loops: Removing loop update instruction: ";
874                Def->dump());
875     Def->eraseFromParent();
876     return;
877   }
878 
879   LLVM_DEBUG(dbgs() << "ARM Loops: Can't remove loop update, it's used by:\n";
880              for (auto U : Uses) U->dump());
881 }
882 
883 void ARMLowOverheadLoops::ConvertVPTBlocks(LowOverheadLoop &LoLoop) {
884   auto RemovePredicate = [](MachineInstr *MI) {
885     LLVM_DEBUG(dbgs() << "ARM Loops: Removing predicate from: " << *MI);
886     if (int PIdx = llvm::findFirstVPTPredOperandIdx(*MI)) {
887       assert(MI->getOperand(PIdx).getImm() == ARMVCC::Then &&
888              "Expected Then predicate!");
889       MI->getOperand(PIdx).setImm(ARMVCC::None);
890       MI->getOperand(PIdx+1).setReg(0);
891     } else
892       llvm_unreachable("trying to unpredicate a non-predicated instruction");
893   };
894 
895   // There are a few scenarios which we have to fix up:
896   // 1) A VPT block with is only predicated by the vctp and has no internal vpr
897   //    defs.
898   // 2) A VPT block which is only predicated by the vctp but has an internal
899   //    vpr def.
900   // 3) A VPT block which is predicated upon the vctp as well as another vpr
901   //    def.
902   // 4) A VPT block which is not predicated upon a vctp, but contains it and
903   //    all instructions within the block are predicated upon in.
904 
905   for (auto &Block : LoLoop.getVPTBlocks()) {
906     SmallVectorImpl<PredicatedMI> &Insts = Block.getInsts();
907     if (Block.HasNonUniformPredicate()) {
908       PredicatedMI *Divergent = Block.getDivergent();
909       if (isVCTP(Divergent->MI)) {
910         // The vctp will be removed, so the size of the vpt block needs to be
911         // modified.
912         uint64_t Size = getARMVPTBlockMask(Block.size() - 1);
913         Block.getVPST()->getOperand(0).setImm(Size);
914         LLVM_DEBUG(dbgs() << "ARM Loops: Modified VPT block mask.\n");
915       } else if (Block.IsOnlyPredicatedOn(LoLoop.VCTP)) {
916         // The VPT block has a non-uniform predicate but it's entry is guarded
917         // only by a vctp, which means we:
918         // - Need to remove the original vpst.
919         // - Then need to unpredicate any following instructions, until
920         //   we come across the divergent vpr def.
921         // - Insert a new vpst to predicate the instruction(s) that following
922         //   the divergent vpr def.
923         // TODO: We could be producing more VPT blocks than necessary and could
924         // fold the newly created one into a proceeding one.
925         for (auto I = ++MachineBasicBlock::iterator(Block.getVPST()),
926              E = ++MachineBasicBlock::iterator(Divergent->MI); I != E; ++I)
927           RemovePredicate(&*I);
928 
929         unsigned Size = 0;
930         auto E = MachineBasicBlock::reverse_iterator(Divergent->MI);
931         auto I = MachineBasicBlock::reverse_iterator(Insts.back().MI);
932         MachineInstr *InsertAt = nullptr;
933         while (I != E) {
934           InsertAt = &*I;
935           ++Size;
936           ++I;
937         }
938         MachineInstrBuilder MIB = BuildMI(*InsertAt->getParent(), InsertAt,
939                                           InsertAt->getDebugLoc(),
940                                           TII->get(ARM::MVE_VPST));
941         MIB.addImm(getARMVPTBlockMask(Size));
942         LLVM_DEBUG(dbgs() << "ARM Loops: Removing VPST: " << *Block.getVPST());
943         LLVM_DEBUG(dbgs() << "ARM Loops: Created VPST: " << *MIB);
944         Block.getVPST()->eraseFromParent();
945       }
946     } else if (Block.IsOnlyPredicatedOn(LoLoop.VCTP)) {
947       // A vpt block which is only predicated upon vctp and has no internal vpr
948       // defs:
949       // - Remove vpst.
950       // - Unpredicate the remaining instructions.
951       LLVM_DEBUG(dbgs() << "ARM Loops: Removing VPST: " << *Block.getVPST());
952       Block.getVPST()->eraseFromParent();
953       for (auto &PredMI : Insts)
954         RemovePredicate(PredMI.MI);
955     }
956   }
957 
958   LLVM_DEBUG(dbgs() << "ARM Loops: Removing VCTP: " << *LoLoop.VCTP);
959   LoLoop.VCTP->eraseFromParent();
960 }
961 
962 void ARMLowOverheadLoops::Expand(LowOverheadLoop &LoLoop) {
963 
964   // Combine the LoopDec and LoopEnd instructions into LE(TP).
965   auto ExpandLoopEnd = [this](LowOverheadLoop &LoLoop) {
966     MachineInstr *End = LoLoop.End;
967     MachineBasicBlock *MBB = End->getParent();
968     unsigned Opc = LoLoop.IsTailPredicationLegal() ?
969       ARM::MVE_LETP : ARM::t2LEUpdate;
970     MachineInstrBuilder MIB = BuildMI(*MBB, End, End->getDebugLoc(),
971                                       TII->get(Opc));
972     MIB.addDef(ARM::LR);
973     MIB.add(End->getOperand(0));
974     MIB.add(End->getOperand(1));
975     LLVM_DEBUG(dbgs() << "ARM Loops: Inserted LE: " << *MIB);
976 
977     LoLoop.End->eraseFromParent();
978     LoLoop.Dec->eraseFromParent();
979     return &*MIB;
980   };
981 
982   // TODO: We should be able to automatically remove these branches before we
983   // get here - probably by teaching analyzeBranch about the pseudo
984   // instructions.
985   // If there is an unconditional branch, after I, that just branches to the
986   // next block, remove it.
987   auto RemoveDeadBranch = [](MachineInstr *I) {
988     MachineBasicBlock *BB = I->getParent();
989     MachineInstr *Terminator = &BB->instr_back();
990     if (Terminator->isUnconditionalBranch() && I != Terminator) {
991       MachineBasicBlock *Succ = Terminator->getOperand(0).getMBB();
992       if (BB->isLayoutSuccessor(Succ)) {
993         LLVM_DEBUG(dbgs() << "ARM Loops: Removing branch: " << *Terminator);
994         Terminator->eraseFromParent();
995       }
996     }
997   };
998 
999   if (LoLoop.Revert) {
1000     if (LoLoop.Start->getOpcode() == ARM::t2WhileLoopStart)
1001       RevertWhile(LoLoop.Start);
1002     else
1003       LoLoop.Start->eraseFromParent();
1004     bool FlagsAlreadySet = RevertLoopDec(LoLoop.Dec, true);
1005     RevertLoopEnd(LoLoop.End, FlagsAlreadySet);
1006   } else {
1007     LoLoop.Start = ExpandLoopStart(LoLoop);
1008     RemoveDeadBranch(LoLoop.Start);
1009     LoLoop.End = ExpandLoopEnd(LoLoop);
1010     RemoveDeadBranch(LoLoop.End);
1011     if (LoLoop.IsTailPredicationLegal()) {
1012       RemoveLoopUpdate(LoLoop);
1013       ConvertVPTBlocks(LoLoop);
1014     }
1015   }
1016 }
1017 
1018 bool ARMLowOverheadLoops::RevertNonLoops() {
1019   LLVM_DEBUG(dbgs() << "ARM Loops: Reverting any remaining pseudos...\n");
1020   bool Changed = false;
1021 
1022   for (auto &MBB : *MF) {
1023     SmallVector<MachineInstr*, 4> Starts;
1024     SmallVector<MachineInstr*, 4> Decs;
1025     SmallVector<MachineInstr*, 4> Ends;
1026 
1027     for (auto &I : MBB) {
1028       if (isLoopStart(I))
1029         Starts.push_back(&I);
1030       else if (I.getOpcode() == ARM::t2LoopDec)
1031         Decs.push_back(&I);
1032       else if (I.getOpcode() == ARM::t2LoopEnd)
1033         Ends.push_back(&I);
1034     }
1035 
1036     if (Starts.empty() && Decs.empty() && Ends.empty())
1037       continue;
1038 
1039     Changed = true;
1040 
1041     for (auto *Start : Starts) {
1042       if (Start->getOpcode() == ARM::t2WhileLoopStart)
1043         RevertWhile(Start);
1044       else
1045         Start->eraseFromParent();
1046     }
1047     for (auto *Dec : Decs)
1048       RevertLoopDec(Dec);
1049 
1050     for (auto *End : Ends)
1051       RevertLoopEnd(End);
1052   }
1053   return Changed;
1054 }
1055 
1056 FunctionPass *llvm::createARMLowOverheadLoopsPass() {
1057   return new ARMLowOverheadLoops();
1058 }
1059