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; 179 MachineLoopInfo &MLI; 180 ReachingDefAnalysis &RDA; 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 // TODO: On failing to move an instruction, check if the count is provided by 394 // a mov and whether we can use the mov operand directly. 395 MachineBasicBlock *InsertBB = StartInsertPt->getParent(); 396 if (!RDA.isReachingDefLiveOut(StartInsertPt, NumElements)) { 397 if (auto *ElemDef = RDA.getLocalLiveOutMIDef(InsertBB, NumElements)) { 398 if (RDA.isSafeToMoveForwards(ElemDef, StartInsertPt)) { 399 ElemDef->removeFromParent(); 400 InsertBB->insert(MachineBasicBlock::iterator(StartInsertPt), ElemDef); 401 LLVM_DEBUG(dbgs() << "ARM Loops: Moved element count def: " 402 << *ElemDef); 403 } else if (RDA.isSafeToMoveBackwards(StartInsertPt, ElemDef)) { 404 StartInsertPt->removeFromParent(); 405 InsertBB->insertAfter(MachineBasicBlock::iterator(ElemDef), 406 StartInsertPt); 407 LLVM_DEBUG(dbgs() << "ARM Loops: Moved start past: " << *ElemDef); 408 } else { 409 LLVM_DEBUG(dbgs() << "ARM Loops: Unable to move element count to loop " 410 << "start instruction.\n"); 411 return false; 412 } 413 } 414 } 415 416 // Especially in the case of while loops, InsertBB may not be the 417 // preheader, so we need to check that the register isn't redefined 418 // before entering the loop. 419 auto CannotProvideElements = [this](MachineBasicBlock *MBB, 420 Register NumElements) { 421 // NumElements is redefined in this block. 422 if (RDA.hasLocalDefBefore(&MBB->back(), NumElements)) 423 return true; 424 425 // Don't continue searching up through multiple predecessors. 426 if (MBB->pred_size() > 1) 427 return true; 428 429 return false; 430 }; 431 432 // First, find the block that looks like the preheader. 433 MachineBasicBlock *MBB = MLI.findLoopPreheader(&ML, true); 434 if (!MBB) { 435 LLVM_DEBUG(dbgs() << "ARM Loops: Didn't find preheader.\n"); 436 return false; 437 } 438 439 // Then search backwards for a def, until we get to InsertBB. 440 while (MBB != InsertBB) { 441 if (CannotProvideElements(MBB, NumElements)) { 442 LLVM_DEBUG(dbgs() << "ARM Loops: Unable to provide element count.\n"); 443 return false; 444 } 445 MBB = *MBB->pred_begin(); 446 } 447 448 // Check that the value change of the element count is what we expect and 449 // that the predication will be equivalent. For this we need: 450 // NumElements = NumElements - VectorWidth. The sub will be a sub immediate 451 // and we can also allow register copies within the chain too. 452 auto IsValidSub = [](MachineInstr *MI, unsigned ExpectedVecWidth) { 453 unsigned ImmOpIdx = 0; 454 switch (MI->getOpcode()) { 455 default: 456 llvm_unreachable("unhandled sub opcode"); 457 case ARM::tSUBi3: 458 case ARM::tSUBi8: 459 ImmOpIdx = 3; 460 break; 461 case ARM::t2SUBri: 462 case ARM::t2SUBri12: 463 ImmOpIdx = 2; 464 break; 465 } 466 return MI->getOperand(ImmOpIdx).getImm() == ExpectedVecWidth; 467 }; 468 469 MBB = VCTP->getParent(); 470 if (MachineInstr *Def = RDA.getReachingMIDef(&MBB->back(), NumElements)) { 471 SmallPtrSet<MachineInstr*, 2> ElementChain; 472 SmallPtrSet<MachineInstr*, 2> Ignore = { VCTP }; 473 unsigned ExpectedVectorWidth = getTailPredVectorWidth(VCTP->getOpcode()); 474 475 if (RDA.isSafeToRemove(Def, ElementChain, Ignore)) { 476 bool FoundSub = false; 477 478 for (auto *MI : ElementChain) { 479 if (isMovRegOpcode(MI->getOpcode())) 480 continue; 481 482 if (isSubImmOpcode(MI->getOpcode())) { 483 if (FoundSub || !IsValidSub(MI, ExpectedVectorWidth)) 484 return false; 485 FoundSub = true; 486 } else 487 return false; 488 } 489 490 LLVM_DEBUG(dbgs() << "ARM Loops: Will remove element count chain:\n"; 491 for (auto *MI : ElementChain) 492 dbgs() << " - " << *MI); 493 ToRemove.insert(ElementChain.begin(), ElementChain.end()); 494 } 495 } 496 return true; 497 } 498 499 void LowOverheadLoop::CheckLegality(ARMBasicBlockUtils *BBUtils) { 500 if (Revert) 501 return; 502 503 if (!End->getOperand(1).isMBB()) 504 report_fatal_error("Expected LoopEnd to target basic block"); 505 506 // TODO Maybe there's cases where the target doesn't have to be the header, 507 // but for now be safe and revert. 508 if (End->getOperand(1).getMBB() != ML.getHeader()) { 509 LLVM_DEBUG(dbgs() << "ARM Loops: LoopEnd is not targetting header.\n"); 510 Revert = true; 511 return; 512 } 513 514 // The WLS and LE instructions have 12-bits for the label offset. WLS 515 // requires a positive offset, while LE uses negative. 516 if (BBUtils->getOffsetOf(End) < BBUtils->getOffsetOf(ML.getHeader()) || 517 !BBUtils->isBBInRange(End, ML.getHeader(), 4094)) { 518 LLVM_DEBUG(dbgs() << "ARM Loops: LE offset is out-of-range\n"); 519 Revert = true; 520 return; 521 } 522 523 if (Start->getOpcode() == ARM::t2WhileLoopStart && 524 (BBUtils->getOffsetOf(Start) > 525 BBUtils->getOffsetOf(Start->getOperand(1).getMBB()) || 526 !BBUtils->isBBInRange(Start, Start->getOperand(1).getMBB(), 4094))) { 527 LLVM_DEBUG(dbgs() << "ARM Loops: WLS offset is out-of-range!\n"); 528 Revert = true; 529 return; 530 } 531 532 InsertPt = Revert ? nullptr : isSafeToDefineLR(); 533 if (!InsertPt) { 534 LLVM_DEBUG(dbgs() << "ARM Loops: Unable to find safe insertion point.\n"); 535 Revert = true; 536 return; 537 } else 538 LLVM_DEBUG(dbgs() << "ARM Loops: Start insertion point: " << *InsertPt); 539 540 if (!IsTailPredicationLegal()) { 541 LLVM_DEBUG(if (!VCTP) 542 dbgs() << "ARM Loops: Didn't find a VCTP instruction.\n"; 543 dbgs() << "ARM Loops: Tail-predication is not valid.\n"); 544 return; 545 } 546 547 assert(ML.getBlocks().size() == 1 && 548 "Shouldn't be processing a loop with more than one block"); 549 CannotTailPredicate = !ValidateTailPredicate(InsertPt); 550 LLVM_DEBUG(if (CannotTailPredicate) 551 dbgs() << "ARM Loops: Couldn't validate tail predicate.\n"); 552 } 553 554 bool LowOverheadLoop::ValidateMVEInst(MachineInstr* MI) { 555 if (CannotTailPredicate) 556 return false; 557 558 // Only support a single vctp. 559 if (isVCTP(MI) && VCTP) 560 return false; 561 562 // Start a new vpt block when we discover a vpt. 563 if (MI->getOpcode() == ARM::MVE_VPST) { 564 VPTBlocks.emplace_back(MI, CurrentPredicate); 565 CurrentBlock = &VPTBlocks.back(); 566 return true; 567 } else if (isVCTP(MI)) 568 VCTP = MI; 569 else if (MI->getOpcode() == ARM::MVE_VPSEL || 570 MI->getOpcode() == ARM::MVE_VPNOT) 571 return false; 572 573 // TODO: Allow VPSEL and VPNOT, we currently cannot because: 574 // 1) It will use the VPR as a predicate operand, but doesn't have to be 575 // instead a VPT block, which means we can assert while building up 576 // the VPT block because we don't find another VPST to being a new 577 // one. 578 // 2) VPSEL still requires a VPR operand even after tail predicating, 579 // which means we can't remove it unless there is another 580 // instruction, such as vcmp, that can provide the VPR def. 581 582 bool IsUse = false; 583 bool IsDef = false; 584 const MCInstrDesc &MCID = MI->getDesc(); 585 for (int i = MI->getNumOperands() - 1; i >= 0; --i) { 586 const MachineOperand &MO = MI->getOperand(i); 587 if (!MO.isReg() || MO.getReg() != ARM::VPR) 588 continue; 589 590 if (MO.isDef()) { 591 CurrentPredicate.insert(MI); 592 IsDef = true; 593 } else if (ARM::isVpred(MCID.OpInfo[i].OperandType)) { 594 CurrentBlock->addInst(MI, CurrentPredicate); 595 IsUse = true; 596 } else { 597 LLVM_DEBUG(dbgs() << "ARM Loops: Found instruction using vpr: " << *MI); 598 return false; 599 } 600 } 601 602 // If we find a vpr def that is not already predicated on the vctp, we've 603 // got disjoint predicates that may not be equivalent when we do the 604 // conversion. 605 if (IsDef && !IsUse && VCTP && !isVCTP(MI)) { 606 LLVM_DEBUG(dbgs() << "ARM Loops: Found disjoint vpr def: " << *MI); 607 return false; 608 } 609 610 uint64_t Flags = MCID.TSFlags; 611 if ((Flags & ARMII::DomainMask) != ARMII::DomainMVE) 612 return true; 613 614 // If we find an instruction that has been marked as not valid for tail 615 // predication, only allow the instruction if it's contained within a valid 616 // VPT block. 617 if ((Flags & ARMII::ValidForTailPredication) == 0 && !IsUse) { 618 LLVM_DEBUG(dbgs() << "ARM Loops: Can't tail predicate: " << *MI); 619 return false; 620 } 621 622 // Ensure that all memory operations are predicated. 623 return !IsUse && MI->mayLoadOrStore() ? false : true; 624 } 625 626 bool ARMLowOverheadLoops::runOnMachineFunction(MachineFunction &mf) { 627 const ARMSubtarget &ST = static_cast<const ARMSubtarget&>(mf.getSubtarget()); 628 if (!ST.hasLOB()) 629 return false; 630 631 MF = &mf; 632 LLVM_DEBUG(dbgs() << "ARM Loops on " << MF->getName() << " ------------- \n"); 633 634 MLI = &getAnalysis<MachineLoopInfo>(); 635 RDA = &getAnalysis<ReachingDefAnalysis>(); 636 MF->getProperties().set(MachineFunctionProperties::Property::TracksLiveness); 637 MRI = &MF->getRegInfo(); 638 TII = static_cast<const ARMBaseInstrInfo*>(ST.getInstrInfo()); 639 TRI = ST.getRegisterInfo(); 640 BBUtils = std::unique_ptr<ARMBasicBlockUtils>(new ARMBasicBlockUtils(*MF)); 641 BBUtils->computeAllBlockSizes(); 642 BBUtils->adjustBBOffsetsAfter(&MF->front()); 643 644 bool Changed = false; 645 for (auto ML : *MLI) { 646 if (!ML->getParentLoop()) 647 Changed |= ProcessLoop(ML); 648 } 649 Changed |= RevertNonLoops(); 650 return Changed; 651 } 652 653 bool ARMLowOverheadLoops::ProcessLoop(MachineLoop *ML) { 654 655 bool Changed = false; 656 657 // Process inner loops first. 658 for (auto I = ML->begin(), E = ML->end(); I != E; ++I) 659 Changed |= ProcessLoop(*I); 660 661 LLVM_DEBUG(dbgs() << "ARM Loops: Processing loop containing:\n"; 662 if (auto *Preheader = ML->getLoopPreheader()) 663 dbgs() << " - " << Preheader->getName() << "\n"; 664 else if (auto *Preheader = MLI->findLoopPreheader(ML)) 665 dbgs() << " - " << Preheader->getName() << "\n"; 666 else if (auto *Preheader = MLI->findLoopPreheader(ML, true)) 667 dbgs() << " - " << Preheader->getName() << "\n"; 668 for (auto *MBB : ML->getBlocks()) 669 dbgs() << " - " << MBB->getName() << "\n"; 670 ); 671 672 // Search the given block for a loop start instruction. If one isn't found, 673 // and there's only one predecessor block, search that one too. 674 std::function<MachineInstr*(MachineBasicBlock*)> SearchForStart = 675 [&SearchForStart](MachineBasicBlock *MBB) -> MachineInstr* { 676 for (auto &MI : *MBB) { 677 if (isLoopStart(MI)) 678 return &MI; 679 } 680 if (MBB->pred_size() == 1) 681 return SearchForStart(*MBB->pred_begin()); 682 return nullptr; 683 }; 684 685 LowOverheadLoop LoLoop(*ML, *MLI, *RDA); 686 // Search the preheader for the start intrinsic. 687 // FIXME: I don't see why we shouldn't be supporting multiple predecessors 688 // with potentially multiple set.loop.iterations, so we need to enable this. 689 if (auto *Preheader = ML->getLoopPreheader()) 690 LoLoop.Start = SearchForStart(Preheader); 691 else if (auto *Preheader = MLI->findLoopPreheader(ML, true)) 692 LoLoop.Start = SearchForStart(Preheader); 693 else 694 return false; 695 696 // Find the low-overhead loop components and decide whether or not to fall 697 // back to a normal loop. Also look for a vctp instructions and decide 698 // whether we can convert that predicate using tail predication. 699 for (auto *MBB : reverse(ML->getBlocks())) { 700 for (auto &MI : *MBB) { 701 if (MI.isDebugValue()) 702 continue; 703 else if (MI.getOpcode() == ARM::t2LoopDec) 704 LoLoop.Dec = &MI; 705 else if (MI.getOpcode() == ARM::t2LoopEnd) 706 LoLoop.End = &MI; 707 else if (isLoopStart(MI)) 708 LoLoop.Start = &MI; 709 else if (MI.getDesc().isCall()) { 710 // TODO: Though the call will require LE to execute again, does this 711 // mean we should revert? Always executing LE hopefully should be 712 // faster than performing a sub,cmp,br or even subs,br. 713 LoLoop.Revert = true; 714 LLVM_DEBUG(dbgs() << "ARM Loops: Found call.\n"); 715 } else { 716 // Record VPR defs and build up their corresponding vpt blocks. 717 // Check we know how to tail predicate any mve instructions. 718 LoLoop.AnalyseMVEInst(&MI); 719 } 720 } 721 } 722 723 LLVM_DEBUG(LoLoop.dump()); 724 if (!LoLoop.FoundAllComponents()) { 725 LLVM_DEBUG(dbgs() << "ARM Loops: Didn't find loop start, update, end\n"); 726 return false; 727 } 728 729 // Check that the only instruction using LoopDec is LoopEnd. 730 // TODO: Check for copy chains that really have no effect. 731 SmallPtrSet<MachineInstr*, 2> Uses; 732 RDA->getReachingLocalUses(LoLoop.Dec, ARM::LR, Uses); 733 if (Uses.size() > 1 || !Uses.count(LoLoop.End)) { 734 LLVM_DEBUG(dbgs() << "ARM Loops: Unable to remove LoopDec.\n"); 735 LoLoop.Revert = true; 736 } 737 LoLoop.CheckLegality(BBUtils.get()); 738 Expand(LoLoop); 739 return true; 740 } 741 742 // WhileLoopStart holds the exit block, so produce a cmp lr, 0 and then a 743 // beq that branches to the exit branch. 744 // TODO: We could also try to generate a cbz if the value in LR is also in 745 // another low register. 746 void ARMLowOverheadLoops::RevertWhile(MachineInstr *MI) const { 747 LLVM_DEBUG(dbgs() << "ARM Loops: Reverting to cmp: " << *MI); 748 MachineBasicBlock *MBB = MI->getParent(); 749 MachineInstrBuilder MIB = BuildMI(*MBB, MI, MI->getDebugLoc(), 750 TII->get(ARM::t2CMPri)); 751 MIB.add(MI->getOperand(0)); 752 MIB.addImm(0); 753 MIB.addImm(ARMCC::AL); 754 MIB.addReg(ARM::NoRegister); 755 756 MachineBasicBlock *DestBB = MI->getOperand(1).getMBB(); 757 unsigned BrOpc = BBUtils->isBBInRange(MI, DestBB, 254) ? 758 ARM::tBcc : ARM::t2Bcc; 759 760 MIB = BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(BrOpc)); 761 MIB.add(MI->getOperand(1)); // branch target 762 MIB.addImm(ARMCC::EQ); // condition code 763 MIB.addReg(ARM::CPSR); 764 MI->eraseFromParent(); 765 } 766 767 bool ARMLowOverheadLoops::RevertLoopDec(MachineInstr *MI) const { 768 LLVM_DEBUG(dbgs() << "ARM Loops: Reverting to sub: " << *MI); 769 MachineBasicBlock *MBB = MI->getParent(); 770 MachineInstr *Last = &MBB->back(); 771 SmallPtrSet<MachineInstr*, 1> Ignore; 772 if (Last->getOpcode() == ARM::t2LoopEnd) 773 Ignore.insert(Last); 774 775 // If nothing defines CPSR between LoopDec and LoopEnd, use a t2SUBS. 776 bool SetFlags = RDA->isSafeToDefRegAt(MI, ARM::CPSR, Ignore); 777 778 MachineInstrBuilder MIB = BuildMI(*MBB, MI, MI->getDebugLoc(), 779 TII->get(ARM::t2SUBri)); 780 MIB.addDef(ARM::LR); 781 MIB.add(MI->getOperand(1)); 782 MIB.add(MI->getOperand(2)); 783 MIB.addImm(ARMCC::AL); 784 MIB.addReg(0); 785 786 if (SetFlags) { 787 MIB.addReg(ARM::CPSR); 788 MIB->getOperand(5).setIsDef(true); 789 } else 790 MIB.addReg(0); 791 792 MI->eraseFromParent(); 793 return SetFlags; 794 } 795 796 // Generate a subs, or sub and cmp, and a branch instead of an LE. 797 void ARMLowOverheadLoops::RevertLoopEnd(MachineInstr *MI, bool SkipCmp) const { 798 LLVM_DEBUG(dbgs() << "ARM Loops: Reverting to cmp, br: " << *MI); 799 800 MachineBasicBlock *MBB = MI->getParent(); 801 // Create cmp 802 if (!SkipCmp) { 803 MachineInstrBuilder MIB = BuildMI(*MBB, MI, MI->getDebugLoc(), 804 TII->get(ARM::t2CMPri)); 805 MIB.addReg(ARM::LR); 806 MIB.addImm(0); 807 MIB.addImm(ARMCC::AL); 808 MIB.addReg(ARM::NoRegister); 809 } 810 811 MachineBasicBlock *DestBB = MI->getOperand(1).getMBB(); 812 unsigned BrOpc = BBUtils->isBBInRange(MI, DestBB, 254) ? 813 ARM::tBcc : ARM::t2Bcc; 814 815 // Create bne 816 MachineInstrBuilder MIB = 817 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(BrOpc)); 818 MIB.add(MI->getOperand(1)); // branch target 819 MIB.addImm(ARMCC::NE); // condition code 820 MIB.addReg(ARM::CPSR); 821 MI->eraseFromParent(); 822 } 823 824 // Perform dead code elimation on the loop iteration count setup expression. 825 // If we are tail-predicating, the number of elements to be processed is the 826 // operand of the VCTP instruction in the vector body, see getCount(), which is 827 // register $r3 in this example: 828 // 829 // $lr = big-itercount-expression 830 // .. 831 // t2DoLoopStart renamable $lr 832 // vector.body: 833 // .. 834 // $vpr = MVE_VCTP32 renamable $r3 835 // renamable $lr = t2LoopDec killed renamable $lr, 1 836 // t2LoopEnd renamable $lr, %vector.body 837 // tB %end 838 // 839 // What we would like achieve here is to replace the do-loop start pseudo 840 // instruction t2DoLoopStart with: 841 // 842 // $lr = MVE_DLSTP_32 killed renamable $r3 843 // 844 // Thus, $r3 which defines the number of elements, is written to $lr, 845 // and then we want to delete the whole chain that used to define $lr, 846 // see the comment below how this chain could look like. 847 // 848 void ARMLowOverheadLoops::IterationCountDCE(LowOverheadLoop &LoLoop) { 849 if (!LoLoop.IsTailPredicationLegal()) 850 return; 851 852 if (auto *Def = RDA->getReachingMIDef(LoLoop.Start, 853 LoLoop.Start->getOperand(0).getReg())) { 854 SmallPtrSet<MachineInstr*, 4> Remove; 855 SmallPtrSet<MachineInstr*, 4> Ignore = { LoLoop.Start, LoLoop.Dec, 856 LoLoop.End, LoLoop.InsertPt }; 857 SmallVector<MachineInstr*, 4> Chain = { Def }; 858 while (!Chain.empty()) { 859 MachineInstr *MI = Chain.back(); 860 Chain.pop_back(); 861 862 // If an instruction is conditionally executed, we assume here that this 863 // an IT-block with just this single instruction in it, otherwise we 864 // continue and can't perform dead-code elimination on it. This will 865 // capture most cases, because the loop iteration count expression 866 // that performs a round-up to next multiple of the vector length will 867 // look like this: 868 // 869 // %mull = .. 870 // %0 = add i32 %mul, 3 871 // %1 = icmp slt i32 %mul, 4 872 // %smin = select i1 %1, i32 %mul, i32 4 873 // %2 = sub i32 %0, %smin 874 // %3 = lshr i32 %2, 2 875 // %4 = add nuw nsw i32 %3, 1 876 // 877 // There can be a select instruction, checking if we need to execute only 878 // 1 vector iteration (in this examples that means 4 elements). Thus, 879 // we conditionally execute one instructions to materialise the iteration 880 // count. 881 MachineInstr *IT = nullptr; 882 if (TII->getPredicate(*MI) != ARMCC::AL) { 883 auto PrevMI = std::prev(MI->getIterator()); 884 auto NextMI = std::next(MI->getIterator()); 885 886 if (PrevMI->getOpcode() == ARM::t2IT && 887 TII->getPredicate(*NextMI) == ARMCC::AL) 888 IT = &*PrevMI; 889 else 890 // We can't analyse IT-blocks with multiple statements. Be 891 // conservative here: clear the list, and don't remove any statements 892 // at all. 893 return; 894 } 895 896 if (RDA->isSafeToRemove(MI, Remove, Ignore)) { 897 for (auto &MO : MI->operands()) { 898 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0) 899 continue; 900 if (auto *Op = RDA->getReachingMIDef(MI, MO.getReg())) 901 Chain.push_back(Op); 902 } 903 Ignore.insert(MI); 904 905 if (IT) 906 Remove.insert(IT); 907 } 908 } 909 LoLoop.ToRemove.insert(Remove.begin(), Remove.end()); 910 } 911 } 912 913 MachineInstr* ARMLowOverheadLoops::ExpandLoopStart(LowOverheadLoop &LoLoop) { 914 LLVM_DEBUG(dbgs() << "ARM Loops: Expanding LoopStart.\n"); 915 // When using tail-predication, try to delete the dead code that was used to 916 // calculate the number of loop iterations. 917 IterationCountDCE(LoLoop); 918 919 MachineInstr *InsertPt = LoLoop.InsertPt; 920 MachineInstr *Start = LoLoop.Start; 921 MachineBasicBlock *MBB = InsertPt->getParent(); 922 bool IsDo = Start->getOpcode() == ARM::t2DoLoopStart; 923 unsigned Opc = LoLoop.getStartOpcode(); 924 MachineOperand &Count = LoLoop.getCount(); 925 926 MachineInstrBuilder MIB = 927 BuildMI(*MBB, InsertPt, InsertPt->getDebugLoc(), TII->get(Opc)); 928 929 MIB.addDef(ARM::LR); 930 MIB.add(Count); 931 if (!IsDo) 932 MIB.add(Start->getOperand(1)); 933 934 // If we're inserting at a mov lr, then remove it as it's redundant. 935 if (InsertPt != Start) 936 LoLoop.ToRemove.insert(InsertPt); 937 LoLoop.ToRemove.insert(Start); 938 LLVM_DEBUG(dbgs() << "ARM Loops: Inserted start: " << *MIB); 939 return &*MIB; 940 } 941 942 void ARMLowOverheadLoops::ConvertVPTBlocks(LowOverheadLoop &LoLoop) { 943 auto RemovePredicate = [](MachineInstr *MI) { 944 LLVM_DEBUG(dbgs() << "ARM Loops: Removing predicate from: " << *MI); 945 if (int PIdx = llvm::findFirstVPTPredOperandIdx(*MI)) { 946 assert(MI->getOperand(PIdx).getImm() == ARMVCC::Then && 947 "Expected Then predicate!"); 948 MI->getOperand(PIdx).setImm(ARMVCC::None); 949 MI->getOperand(PIdx+1).setReg(0); 950 } else 951 llvm_unreachable("trying to unpredicate a non-predicated instruction"); 952 }; 953 954 // There are a few scenarios which we have to fix up: 955 // 1) A VPT block with is only predicated by the vctp and has no internal vpr 956 // defs. 957 // 2) A VPT block which is only predicated by the vctp but has an internal 958 // vpr def. 959 // 3) A VPT block which is predicated upon the vctp as well as another vpr 960 // def. 961 // 4) A VPT block which is not predicated upon a vctp, but contains it and 962 // all instructions within the block are predicated upon in. 963 964 for (auto &Block : LoLoop.getVPTBlocks()) { 965 SmallVectorImpl<PredicatedMI> &Insts = Block.getInsts(); 966 if (Block.HasNonUniformPredicate()) { 967 PredicatedMI *Divergent = Block.getDivergent(); 968 if (isVCTP(Divergent->MI)) { 969 // The vctp will be removed, so the size of the vpt block needs to be 970 // modified. 971 uint64_t Size = getARMVPTBlockMask(Block.size() - 1); 972 Block.getVPST()->getOperand(0).setImm(Size); 973 LLVM_DEBUG(dbgs() << "ARM Loops: Modified VPT block mask.\n"); 974 } else if (Block.IsOnlyPredicatedOn(LoLoop.VCTP)) { 975 // The VPT block has a non-uniform predicate but it's entry is guarded 976 // only by a vctp, which means we: 977 // - Need to remove the original vpst. 978 // - Then need to unpredicate any following instructions, until 979 // we come across the divergent vpr def. 980 // - Insert a new vpst to predicate the instruction(s) that following 981 // the divergent vpr def. 982 // TODO: We could be producing more VPT blocks than necessary and could 983 // fold the newly created one into a proceeding one. 984 for (auto I = ++MachineBasicBlock::iterator(Block.getVPST()), 985 E = ++MachineBasicBlock::iterator(Divergent->MI); I != E; ++I) 986 RemovePredicate(&*I); 987 988 unsigned Size = 0; 989 auto E = MachineBasicBlock::reverse_iterator(Divergent->MI); 990 auto I = MachineBasicBlock::reverse_iterator(Insts.back().MI); 991 MachineInstr *InsertAt = nullptr; 992 while (I != E) { 993 InsertAt = &*I; 994 ++Size; 995 ++I; 996 } 997 MachineInstrBuilder MIB = BuildMI(*InsertAt->getParent(), InsertAt, 998 InsertAt->getDebugLoc(), 999 TII->get(ARM::MVE_VPST)); 1000 MIB.addImm(getARMVPTBlockMask(Size)); 1001 LLVM_DEBUG(dbgs() << "ARM Loops: Removing VPST: " << *Block.getVPST()); 1002 LLVM_DEBUG(dbgs() << "ARM Loops: Created VPST: " << *MIB); 1003 LoLoop.ToRemove.insert(Block.getVPST()); 1004 } 1005 } else if (Block.IsOnlyPredicatedOn(LoLoop.VCTP)) { 1006 // A vpt block which is only predicated upon vctp and has no internal vpr 1007 // defs: 1008 // - Remove vpst. 1009 // - Unpredicate the remaining instructions. 1010 LLVM_DEBUG(dbgs() << "ARM Loops: Removing VPST: " << *Block.getVPST()); 1011 LoLoop.ToRemove.insert(Block.getVPST()); 1012 for (auto &PredMI : Insts) 1013 RemovePredicate(PredMI.MI); 1014 } 1015 } 1016 LLVM_DEBUG(dbgs() << "ARM Loops: Removing VCTP: " << *LoLoop.VCTP); 1017 LoLoop.ToRemove.insert(LoLoop.VCTP); 1018 } 1019 1020 void ARMLowOverheadLoops::Expand(LowOverheadLoop &LoLoop) { 1021 1022 // Combine the LoopDec and LoopEnd instructions into LE(TP). 1023 auto ExpandLoopEnd = [this](LowOverheadLoop &LoLoop) { 1024 MachineInstr *End = LoLoop.End; 1025 MachineBasicBlock *MBB = End->getParent(); 1026 unsigned Opc = LoLoop.IsTailPredicationLegal() ? 1027 ARM::MVE_LETP : ARM::t2LEUpdate; 1028 MachineInstrBuilder MIB = BuildMI(*MBB, End, End->getDebugLoc(), 1029 TII->get(Opc)); 1030 MIB.addDef(ARM::LR); 1031 MIB.add(End->getOperand(0)); 1032 MIB.add(End->getOperand(1)); 1033 LLVM_DEBUG(dbgs() << "ARM Loops: Inserted LE: " << *MIB); 1034 LoLoop.Dec->eraseFromParent(); 1035 End->eraseFromParent(); 1036 return &*MIB; 1037 }; 1038 1039 // TODO: We should be able to automatically remove these branches before we 1040 // get here - probably by teaching analyzeBranch about the pseudo 1041 // instructions. 1042 // If there is an unconditional branch, after I, that just branches to the 1043 // next block, remove it. 1044 auto RemoveDeadBranch = [](MachineInstr *I) { 1045 MachineBasicBlock *BB = I->getParent(); 1046 MachineInstr *Terminator = &BB->instr_back(); 1047 if (Terminator->isUnconditionalBranch() && I != Terminator) { 1048 MachineBasicBlock *Succ = Terminator->getOperand(0).getMBB(); 1049 if (BB->isLayoutSuccessor(Succ)) { 1050 LLVM_DEBUG(dbgs() << "ARM Loops: Removing branch: " << *Terminator); 1051 Terminator->eraseFromParent(); 1052 } 1053 } 1054 }; 1055 1056 if (LoLoop.Revert) { 1057 if (LoLoop.Start->getOpcode() == ARM::t2WhileLoopStart) 1058 RevertWhile(LoLoop.Start); 1059 else 1060 LoLoop.Start->eraseFromParent(); 1061 bool FlagsAlreadySet = RevertLoopDec(LoLoop.Dec); 1062 RevertLoopEnd(LoLoop.End, FlagsAlreadySet); 1063 } else { 1064 LoLoop.Start = ExpandLoopStart(LoLoop); 1065 RemoveDeadBranch(LoLoop.Start); 1066 LoLoop.End = ExpandLoopEnd(LoLoop); 1067 RemoveDeadBranch(LoLoop.End); 1068 if (LoLoop.IsTailPredicationLegal()) 1069 ConvertVPTBlocks(LoLoop); 1070 for (auto *I : LoLoop.ToRemove) { 1071 LLVM_DEBUG(dbgs() << "ARM Loops: Erasing " << *I); 1072 I->eraseFromParent(); 1073 } 1074 } 1075 1076 PostOrderLoopTraversal DFS(LoLoop.ML, *MLI); 1077 DFS.ProcessLoop(); 1078 const SmallVectorImpl<MachineBasicBlock*> &PostOrder = DFS.getOrder(); 1079 for (auto *MBB : PostOrder) { 1080 recomputeLiveIns(*MBB); 1081 // FIXME: For some reason, the live-in print order is non-deterministic for 1082 // our tests and I can't out why... So just sort them. 1083 MBB->sortUniqueLiveIns(); 1084 } 1085 1086 for (auto *MBB : reverse(PostOrder)) 1087 recomputeLivenessFlags(*MBB); 1088 } 1089 1090 bool ARMLowOverheadLoops::RevertNonLoops() { 1091 LLVM_DEBUG(dbgs() << "ARM Loops: Reverting any remaining pseudos...\n"); 1092 bool Changed = false; 1093 1094 for (auto &MBB : *MF) { 1095 SmallVector<MachineInstr*, 4> Starts; 1096 SmallVector<MachineInstr*, 4> Decs; 1097 SmallVector<MachineInstr*, 4> Ends; 1098 1099 for (auto &I : MBB) { 1100 if (isLoopStart(I)) 1101 Starts.push_back(&I); 1102 else if (I.getOpcode() == ARM::t2LoopDec) 1103 Decs.push_back(&I); 1104 else if (I.getOpcode() == ARM::t2LoopEnd) 1105 Ends.push_back(&I); 1106 } 1107 1108 if (Starts.empty() && Decs.empty() && Ends.empty()) 1109 continue; 1110 1111 Changed = true; 1112 1113 for (auto *Start : Starts) { 1114 if (Start->getOpcode() == ARM::t2WhileLoopStart) 1115 RevertWhile(Start); 1116 else 1117 Start->eraseFromParent(); 1118 } 1119 for (auto *Dec : Decs) 1120 RevertLoopDec(Dec); 1121 1122 for (auto *End : Ends) 1123 RevertLoopEnd(End); 1124 } 1125 return Changed; 1126 } 1127 1128 FunctionPass *llvm::createARMLowOverheadLoopsPass() { 1129 return new ARMLowOverheadLoops(); 1130 } 1131