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