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