1 //===- TailDuplicator.cpp - Duplicate blocks into predecessors' tails -----===// 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 // 9 // This utility class duplicates basic blocks ending in unconditional branches 10 // into the tails of their predecessors. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/TailDuplicator.h" 15 #include "llvm/ADT/DenseMap.h" 16 #include "llvm/ADT/DenseSet.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/SetVector.h" 19 #include "llvm/ADT/SmallPtrSet.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/Statistic.h" 22 #include "llvm/CodeGen/MachineBasicBlock.h" 23 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" 24 #include "llvm/CodeGen/MachineFunction.h" 25 #include "llvm/CodeGen/MachineInstr.h" 26 #include "llvm/CodeGen/MachineInstrBuilder.h" 27 #include "llvm/CodeGen/MachineOperand.h" 28 #include "llvm/CodeGen/MachineRegisterInfo.h" 29 #include "llvm/CodeGen/MachineSSAUpdater.h" 30 #include "llvm/CodeGen/MachineSizeOpts.h" 31 #include "llvm/CodeGen/TargetInstrInfo.h" 32 #include "llvm/CodeGen/TargetRegisterInfo.h" 33 #include "llvm/CodeGen/TargetSubtargetInfo.h" 34 #include "llvm/IR/DebugLoc.h" 35 #include "llvm/IR/Function.h" 36 #include "llvm/Support/CommandLine.h" 37 #include "llvm/Support/Debug.h" 38 #include "llvm/Support/ErrorHandling.h" 39 #include "llvm/Support/raw_ostream.h" 40 #include "llvm/Target/TargetMachine.h" 41 #include <algorithm> 42 #include <cassert> 43 #include <iterator> 44 #include <utility> 45 46 using namespace llvm; 47 48 #define DEBUG_TYPE "tailduplication" 49 50 STATISTIC(NumTails, "Number of tails duplicated"); 51 STATISTIC(NumTailDups, "Number of tail duplicated blocks"); 52 STATISTIC(NumTailDupAdded, 53 "Number of instructions added due to tail duplication"); 54 STATISTIC(NumTailDupRemoved, 55 "Number of instructions removed due to tail duplication"); 56 STATISTIC(NumDeadBlocks, "Number of dead blocks removed"); 57 STATISTIC(NumAddedPHIs, "Number of phis added"); 58 59 // Heuristic for tail duplication. 60 static cl::opt<unsigned> TailDuplicateSize( 61 "tail-dup-size", 62 cl::desc("Maximum instructions to consider tail duplicating"), cl::init(2), 63 cl::Hidden); 64 65 static cl::opt<unsigned> TailDupIndirectBranchSize( 66 "tail-dup-indirect-size", 67 cl::desc("Maximum instructions to consider tail duplicating blocks that " 68 "end with indirect branches."), cl::init(20), 69 cl::Hidden); 70 71 static cl::opt<unsigned> 72 TailDupPredSize("tail-dup-pred-size", 73 cl::desc("Maximum predecessors (maximum successors at the " 74 "same time) to consider tail duplicating blocks."), 75 cl::init(16), cl::Hidden); 76 77 static cl::opt<unsigned> 78 TailDupSuccSize("tail-dup-succ-size", 79 cl::desc("Maximum successors (maximum predecessors at the " 80 "same time) to consider tail duplicating blocks."), 81 cl::init(16), cl::Hidden); 82 83 static cl::opt<bool> 84 TailDupVerify("tail-dup-verify", 85 cl::desc("Verify sanity of PHI instructions during taildup"), 86 cl::init(false), cl::Hidden); 87 88 static cl::opt<unsigned> TailDupLimit("tail-dup-limit", cl::init(~0U), 89 cl::Hidden); 90 91 void TailDuplicator::initMF(MachineFunction &MFin, bool PreRegAlloc, 92 const MachineBranchProbabilityInfo *MBPIin, 93 MBFIWrapper *MBFIin, 94 ProfileSummaryInfo *PSIin, 95 bool LayoutModeIn, unsigned TailDupSizeIn) { 96 MF = &MFin; 97 TII = MF->getSubtarget().getInstrInfo(); 98 TRI = MF->getSubtarget().getRegisterInfo(); 99 MRI = &MF->getRegInfo(); 100 MMI = &MF->getMMI(); 101 MBPI = MBPIin; 102 MBFI = MBFIin; 103 PSI = PSIin; 104 TailDupSize = TailDupSizeIn; 105 106 assert(MBPI != nullptr && "Machine Branch Probability Info required"); 107 108 LayoutMode = LayoutModeIn; 109 this->PreRegAlloc = PreRegAlloc; 110 } 111 112 static void VerifyPHIs(MachineFunction &MF, bool CheckExtra) { 113 for (MachineBasicBlock &MBB : llvm::drop_begin(MF)) { 114 SmallSetVector<MachineBasicBlock *, 8> Preds(MBB.pred_begin(), 115 MBB.pred_end()); 116 MachineBasicBlock::iterator MI = MBB.begin(); 117 while (MI != MBB.end()) { 118 if (!MI->isPHI()) 119 break; 120 for (MachineBasicBlock *PredBB : Preds) { 121 bool Found = false; 122 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) { 123 MachineBasicBlock *PHIBB = MI->getOperand(i + 1).getMBB(); 124 if (PHIBB == PredBB) { 125 Found = true; 126 break; 127 } 128 } 129 if (!Found) { 130 dbgs() << "Malformed PHI in " << printMBBReference(MBB) << ": " 131 << *MI; 132 dbgs() << " missing input from predecessor " 133 << printMBBReference(*PredBB) << '\n'; 134 llvm_unreachable(nullptr); 135 } 136 } 137 138 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) { 139 MachineBasicBlock *PHIBB = MI->getOperand(i + 1).getMBB(); 140 if (CheckExtra && !Preds.count(PHIBB)) { 141 dbgs() << "Warning: malformed PHI in " << printMBBReference(MBB) 142 << ": " << *MI; 143 dbgs() << " extra input from predecessor " 144 << printMBBReference(*PHIBB) << '\n'; 145 llvm_unreachable(nullptr); 146 } 147 if (PHIBB->getNumber() < 0) { 148 dbgs() << "Malformed PHI in " << printMBBReference(MBB) << ": " 149 << *MI; 150 dbgs() << " non-existing " << printMBBReference(*PHIBB) << '\n'; 151 llvm_unreachable(nullptr); 152 } 153 } 154 ++MI; 155 } 156 } 157 } 158 159 /// Tail duplicate the block and cleanup. 160 /// \p IsSimple - return value of isSimpleBB 161 /// \p MBB - block to be duplicated 162 /// \p ForcedLayoutPred - If non-null, treat this block as the layout 163 /// predecessor, instead of using the ordering in MF 164 /// \p DuplicatedPreds - if non-null, \p DuplicatedPreds will contain a list of 165 /// all Preds that received a copy of \p MBB. 166 /// \p RemovalCallback - if non-null, called just before MBB is deleted. 167 bool TailDuplicator::tailDuplicateAndUpdate( 168 bool IsSimple, MachineBasicBlock *MBB, 169 MachineBasicBlock *ForcedLayoutPred, 170 SmallVectorImpl<MachineBasicBlock*> *DuplicatedPreds, 171 function_ref<void(MachineBasicBlock *)> *RemovalCallback, 172 SmallVectorImpl<MachineBasicBlock *> *CandidatePtr) { 173 // Save the successors list. 174 SmallSetVector<MachineBasicBlock *, 8> Succs(MBB->succ_begin(), 175 MBB->succ_end()); 176 177 SmallVector<MachineBasicBlock *, 8> TDBBs; 178 SmallVector<MachineInstr *, 16> Copies; 179 if (!tailDuplicate(IsSimple, MBB, ForcedLayoutPred, 180 TDBBs, Copies, CandidatePtr)) 181 return false; 182 183 ++NumTails; 184 185 SmallVector<MachineInstr *, 8> NewPHIs; 186 MachineSSAUpdater SSAUpdate(*MF, &NewPHIs); 187 188 // TailBB's immediate successors are now successors of those predecessors 189 // which duplicated TailBB. Add the predecessors as sources to the PHI 190 // instructions. 191 bool isDead = MBB->pred_empty() && !MBB->hasAddressTaken(); 192 if (PreRegAlloc) 193 updateSuccessorsPHIs(MBB, isDead, TDBBs, Succs); 194 195 // If it is dead, remove it. 196 if (isDead) { 197 NumTailDupRemoved += MBB->size(); 198 removeDeadBlock(MBB, RemovalCallback); 199 ++NumDeadBlocks; 200 } 201 202 // Update SSA form. 203 if (!SSAUpdateVRs.empty()) { 204 for (unsigned VReg : SSAUpdateVRs) { 205 SSAUpdate.Initialize(VReg); 206 207 // If the original definition is still around, add it as an available 208 // value. 209 MachineInstr *DefMI = MRI->getVRegDef(VReg); 210 MachineBasicBlock *DefBB = nullptr; 211 if (DefMI) { 212 DefBB = DefMI->getParent(); 213 SSAUpdate.AddAvailableValue(DefBB, VReg); 214 } 215 216 // Add the new vregs as available values. 217 DenseMap<Register, AvailableValsTy>::iterator LI = 218 SSAUpdateVals.find(VReg); 219 for (std::pair<MachineBasicBlock *, Register> &J : LI->second) { 220 MachineBasicBlock *SrcBB = J.first; 221 Register SrcReg = J.second; 222 SSAUpdate.AddAvailableValue(SrcBB, SrcReg); 223 } 224 225 SmallVector<MachineOperand *> DebugUses; 226 // Rewrite uses that are outside of the original def's block. 227 for (MachineOperand &UseMO : 228 llvm::make_early_inc_range(MRI->use_operands(VReg))) { 229 MachineInstr *UseMI = UseMO.getParent(); 230 // Rewrite debug uses last so that they can take advantage of any 231 // register mappings introduced by other users in its BB, since we 232 // cannot create new register definitions specifically for the debug 233 // instruction (as debug instructions should not affect CodeGen). 234 if (UseMI->isDebugValue()) { 235 DebugUses.push_back(&UseMO); 236 continue; 237 } 238 if (UseMI->getParent() == DefBB && !UseMI->isPHI()) 239 continue; 240 SSAUpdate.RewriteUse(UseMO); 241 } 242 for (auto *UseMO : DebugUses) { 243 MachineInstr *UseMI = UseMO->getParent(); 244 UseMO->setReg( 245 SSAUpdate.GetValueInMiddleOfBlock(UseMI->getParent(), true)); 246 } 247 } 248 249 SSAUpdateVRs.clear(); 250 SSAUpdateVals.clear(); 251 } 252 253 // Eliminate some of the copies inserted by tail duplication to maintain 254 // SSA form. 255 for (MachineInstr *Copy : Copies) { 256 if (!Copy->isCopy()) 257 continue; 258 Register Dst = Copy->getOperand(0).getReg(); 259 Register Src = Copy->getOperand(1).getReg(); 260 if (MRI->hasOneNonDBGUse(Src) && 261 MRI->constrainRegClass(Src, MRI->getRegClass(Dst))) { 262 // Copy is the only use. Do trivial copy propagation here. 263 MRI->replaceRegWith(Dst, Src); 264 Copy->eraseFromParent(); 265 } 266 } 267 268 if (NewPHIs.size()) 269 NumAddedPHIs += NewPHIs.size(); 270 271 if (DuplicatedPreds) 272 *DuplicatedPreds = std::move(TDBBs); 273 274 return true; 275 } 276 277 /// Look for small blocks that are unconditionally branched to and do not fall 278 /// through. Tail-duplicate their instructions into their predecessors to 279 /// eliminate (dynamic) branches. 280 bool TailDuplicator::tailDuplicateBlocks() { 281 bool MadeChange = false; 282 283 if (PreRegAlloc && TailDupVerify) { 284 LLVM_DEBUG(dbgs() << "\n*** Before tail-duplicating\n"); 285 VerifyPHIs(*MF, true); 286 } 287 288 for (MachineBasicBlock &MBB : 289 llvm::make_early_inc_range(llvm::drop_begin(*MF))) { 290 if (NumTails == TailDupLimit) 291 break; 292 293 bool IsSimple = isSimpleBB(&MBB); 294 295 if (!shouldTailDuplicate(IsSimple, MBB)) 296 continue; 297 298 MadeChange |= tailDuplicateAndUpdate(IsSimple, &MBB, nullptr); 299 } 300 301 if (PreRegAlloc && TailDupVerify) 302 VerifyPHIs(*MF, false); 303 304 return MadeChange; 305 } 306 307 static bool isDefLiveOut(Register Reg, MachineBasicBlock *BB, 308 const MachineRegisterInfo *MRI) { 309 for (MachineInstr &UseMI : MRI->use_instructions(Reg)) { 310 if (UseMI.isDebugValue()) 311 continue; 312 if (UseMI.getParent() != BB) 313 return true; 314 } 315 return false; 316 } 317 318 static unsigned getPHISrcRegOpIdx(MachineInstr *MI, MachineBasicBlock *SrcBB) { 319 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) 320 if (MI->getOperand(i + 1).getMBB() == SrcBB) 321 return i; 322 return 0; 323 } 324 325 // Remember which registers are used by phis in this block. This is 326 // used to determine which registers are liveout while modifying the 327 // block (which is why we need to copy the information). 328 static void getRegsUsedByPHIs(const MachineBasicBlock &BB, 329 DenseSet<Register> *UsedByPhi) { 330 for (const auto &MI : BB) { 331 if (!MI.isPHI()) 332 break; 333 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) { 334 Register SrcReg = MI.getOperand(i).getReg(); 335 UsedByPhi->insert(SrcReg); 336 } 337 } 338 } 339 340 /// Add a definition and source virtual registers pair for SSA update. 341 void TailDuplicator::addSSAUpdateEntry(Register OrigReg, Register NewReg, 342 MachineBasicBlock *BB) { 343 DenseMap<Register, AvailableValsTy>::iterator LI = 344 SSAUpdateVals.find(OrigReg); 345 if (LI != SSAUpdateVals.end()) 346 LI->second.push_back(std::make_pair(BB, NewReg)); 347 else { 348 AvailableValsTy Vals; 349 Vals.push_back(std::make_pair(BB, NewReg)); 350 SSAUpdateVals.insert(std::make_pair(OrigReg, Vals)); 351 SSAUpdateVRs.push_back(OrigReg); 352 } 353 } 354 355 /// Process PHI node in TailBB by turning it into a copy in PredBB. Remember the 356 /// source register that's contributed by PredBB and update SSA update map. 357 void TailDuplicator::processPHI( 358 MachineInstr *MI, MachineBasicBlock *TailBB, MachineBasicBlock *PredBB, 359 DenseMap<Register, RegSubRegPair> &LocalVRMap, 360 SmallVectorImpl<std::pair<Register, RegSubRegPair>> &Copies, 361 const DenseSet<Register> &RegsUsedByPhi, bool Remove) { 362 Register DefReg = MI->getOperand(0).getReg(); 363 unsigned SrcOpIdx = getPHISrcRegOpIdx(MI, PredBB); 364 assert(SrcOpIdx && "Unable to find matching PHI source?"); 365 Register SrcReg = MI->getOperand(SrcOpIdx).getReg(); 366 unsigned SrcSubReg = MI->getOperand(SrcOpIdx).getSubReg(); 367 const TargetRegisterClass *RC = MRI->getRegClass(DefReg); 368 LocalVRMap.insert(std::make_pair(DefReg, RegSubRegPair(SrcReg, SrcSubReg))); 369 370 // Insert a copy from source to the end of the block. The def register is the 371 // available value liveout of the block. 372 Register NewDef = MRI->createVirtualRegister(RC); 373 Copies.push_back(std::make_pair(NewDef, RegSubRegPair(SrcReg, SrcSubReg))); 374 if (isDefLiveOut(DefReg, TailBB, MRI) || RegsUsedByPhi.count(DefReg)) 375 addSSAUpdateEntry(DefReg, NewDef, PredBB); 376 377 if (!Remove) 378 return; 379 380 // Remove PredBB from the PHI node. 381 MI->removeOperand(SrcOpIdx + 1); 382 MI->removeOperand(SrcOpIdx); 383 if (MI->getNumOperands() == 1 && !TailBB->hasAddressTaken()) 384 MI->eraseFromParent(); 385 else if (MI->getNumOperands() == 1) 386 MI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF)); 387 } 388 389 /// Duplicate a TailBB instruction to PredBB and update 390 /// the source operands due to earlier PHI translation. 391 void TailDuplicator::duplicateInstruction( 392 MachineInstr *MI, MachineBasicBlock *TailBB, MachineBasicBlock *PredBB, 393 DenseMap<Register, RegSubRegPair> &LocalVRMap, 394 const DenseSet<Register> &UsedByPhi) { 395 // Allow duplication of CFI instructions. 396 if (MI->isCFIInstruction()) { 397 BuildMI(*PredBB, PredBB->end(), PredBB->findDebugLoc(PredBB->begin()), 398 TII->get(TargetOpcode::CFI_INSTRUCTION)) 399 .addCFIIndex(MI->getOperand(0).getCFIIndex()) 400 .setMIFlags(MI->getFlags()); 401 return; 402 } 403 MachineInstr &NewMI = TII->duplicate(*PredBB, PredBB->end(), *MI); 404 if (PreRegAlloc) { 405 for (unsigned i = 0, e = NewMI.getNumOperands(); i != e; ++i) { 406 MachineOperand &MO = NewMI.getOperand(i); 407 if (!MO.isReg()) 408 continue; 409 Register Reg = MO.getReg(); 410 if (!Reg.isVirtual()) 411 continue; 412 if (MO.isDef()) { 413 const TargetRegisterClass *RC = MRI->getRegClass(Reg); 414 Register NewReg = MRI->createVirtualRegister(RC); 415 MO.setReg(NewReg); 416 LocalVRMap.insert(std::make_pair(Reg, RegSubRegPair(NewReg, 0))); 417 if (isDefLiveOut(Reg, TailBB, MRI) || UsedByPhi.count(Reg)) 418 addSSAUpdateEntry(Reg, NewReg, PredBB); 419 } else { 420 auto VI = LocalVRMap.find(Reg); 421 if (VI != LocalVRMap.end()) { 422 // Need to make sure that the register class of the mapped register 423 // will satisfy the constraints of the class of the register being 424 // replaced. 425 auto *OrigRC = MRI->getRegClass(Reg); 426 auto *MappedRC = MRI->getRegClass(VI->second.Reg); 427 const TargetRegisterClass *ConstrRC; 428 if (VI->second.SubReg != 0) { 429 ConstrRC = TRI->getMatchingSuperRegClass(MappedRC, OrigRC, 430 VI->second.SubReg); 431 if (ConstrRC) { 432 // The actual constraining (as in "find appropriate new class") 433 // is done by getMatchingSuperRegClass, so now we only need to 434 // change the class of the mapped register. 435 MRI->setRegClass(VI->second.Reg, ConstrRC); 436 } 437 } else { 438 // For mapped registers that do not have sub-registers, simply 439 // restrict their class to match the original one. 440 441 // We don't want debug instructions affecting the resulting code so 442 // if we're cloning a debug instruction then just use MappedRC 443 // rather than constraining the register class further. 444 ConstrRC = NewMI.isDebugInstr() 445 ? MappedRC 446 : MRI->constrainRegClass(VI->second.Reg, OrigRC); 447 } 448 449 if (ConstrRC) { 450 // If the class constraining succeeded, we can simply replace 451 // the old register with the mapped one. 452 MO.setReg(VI->second.Reg); 453 // We have Reg -> VI.Reg:VI.SubReg, so if Reg is used with a 454 // sub-register, we need to compose the sub-register indices. 455 MO.setSubReg( 456 TRI->composeSubRegIndices(VI->second.SubReg, MO.getSubReg())); 457 } else { 458 // The direct replacement is not possible, due to failing register 459 // class constraints. An explicit COPY is necessary. Create one 460 // that can be reused. 461 Register NewReg = MRI->createVirtualRegister(OrigRC); 462 BuildMI(*PredBB, NewMI, NewMI.getDebugLoc(), 463 TII->get(TargetOpcode::COPY), NewReg) 464 .addReg(VI->second.Reg, 0, VI->second.SubReg); 465 LocalVRMap.erase(VI); 466 LocalVRMap.insert(std::make_pair(Reg, RegSubRegPair(NewReg, 0))); 467 MO.setReg(NewReg); 468 // The composed VI.Reg:VI.SubReg is replaced with NewReg, which 469 // is equivalent to the whole register Reg. Hence, Reg:subreg 470 // is same as NewReg:subreg, so keep the sub-register index 471 // unchanged. 472 } 473 // Clear any kill flags from this operand. The new register could 474 // have uses after this one, so kills are not valid here. 475 MO.setIsKill(false); 476 } 477 } 478 } 479 } 480 } 481 482 /// After FromBB is tail duplicated into its predecessor blocks, the successors 483 /// have gained new predecessors. Update the PHI instructions in them 484 /// accordingly. 485 void TailDuplicator::updateSuccessorsPHIs( 486 MachineBasicBlock *FromBB, bool isDead, 487 SmallVectorImpl<MachineBasicBlock *> &TDBBs, 488 SmallSetVector<MachineBasicBlock *, 8> &Succs) { 489 for (MachineBasicBlock *SuccBB : Succs) { 490 for (MachineInstr &MI : *SuccBB) { 491 if (!MI.isPHI()) 492 break; 493 MachineInstrBuilder MIB(*FromBB->getParent(), MI); 494 unsigned Idx = 0; 495 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) { 496 MachineOperand &MO = MI.getOperand(i + 1); 497 if (MO.getMBB() == FromBB) { 498 Idx = i; 499 break; 500 } 501 } 502 503 assert(Idx != 0); 504 MachineOperand &MO0 = MI.getOperand(Idx); 505 Register Reg = MO0.getReg(); 506 if (isDead) { 507 // Folded into the previous BB. 508 // There could be duplicate phi source entries. FIXME: Should sdisel 509 // or earlier pass fixed this? 510 for (unsigned i = MI.getNumOperands() - 2; i != Idx; i -= 2) { 511 MachineOperand &MO = MI.getOperand(i + 1); 512 if (MO.getMBB() == FromBB) { 513 MI.removeOperand(i + 1); 514 MI.removeOperand(i); 515 } 516 } 517 } else 518 Idx = 0; 519 520 // If Idx is set, the operands at Idx and Idx+1 must be removed. 521 // We reuse the location to avoid expensive removeOperand calls. 522 523 DenseMap<Register, AvailableValsTy>::iterator LI = 524 SSAUpdateVals.find(Reg); 525 if (LI != SSAUpdateVals.end()) { 526 // This register is defined in the tail block. 527 for (const std::pair<MachineBasicBlock *, Register> &J : LI->second) { 528 MachineBasicBlock *SrcBB = J.first; 529 // If we didn't duplicate a bb into a particular predecessor, we 530 // might still have added an entry to SSAUpdateVals to correcly 531 // recompute SSA. If that case, avoid adding a dummy extra argument 532 // this PHI. 533 if (!SrcBB->isSuccessor(SuccBB)) 534 continue; 535 536 Register SrcReg = J.second; 537 if (Idx != 0) { 538 MI.getOperand(Idx).setReg(SrcReg); 539 MI.getOperand(Idx + 1).setMBB(SrcBB); 540 Idx = 0; 541 } else { 542 MIB.addReg(SrcReg).addMBB(SrcBB); 543 } 544 } 545 } else { 546 // Live in tail block, must also be live in predecessors. 547 for (MachineBasicBlock *SrcBB : TDBBs) { 548 if (Idx != 0) { 549 MI.getOperand(Idx).setReg(Reg); 550 MI.getOperand(Idx + 1).setMBB(SrcBB); 551 Idx = 0; 552 } else { 553 MIB.addReg(Reg).addMBB(SrcBB); 554 } 555 } 556 } 557 if (Idx != 0) { 558 MI.removeOperand(Idx + 1); 559 MI.removeOperand(Idx); 560 } 561 } 562 } 563 } 564 565 /// Determine if it is profitable to duplicate this block. 566 bool TailDuplicator::shouldTailDuplicate(bool IsSimple, 567 MachineBasicBlock &TailBB) { 568 // When doing tail-duplication during layout, the block ordering is in flux, 569 // so canFallThrough returns a result based on incorrect information and 570 // should just be ignored. 571 if (!LayoutMode && TailBB.canFallThrough()) 572 return false; 573 574 // Don't try to tail-duplicate single-block loops. 575 if (TailBB.isSuccessor(&TailBB)) 576 return false; 577 578 // Duplicating a BB which has both multiple predecessors and successors will 579 // result in a complex CFG and also may cause huge amount of PHI nodes. If we 580 // want to remove this limitation, we have to address 581 // https://github.com/llvm/llvm-project/issues/78578. 582 if (TailBB.pred_size() > TailDupPredSize && 583 TailBB.succ_size() > TailDupSuccSize) 584 return false; 585 586 // Set the limit on the cost to duplicate. When optimizing for size, 587 // duplicate only one, because one branch instruction can be eliminated to 588 // compensate for the duplication. 589 unsigned MaxDuplicateCount; 590 bool OptForSize = MF->getFunction().hasOptSize() || 591 llvm::shouldOptimizeForSize(&TailBB, PSI, MBFI); 592 if (TailDupSize == 0) 593 MaxDuplicateCount = TailDuplicateSize; 594 else 595 MaxDuplicateCount = TailDupSize; 596 if (OptForSize) 597 MaxDuplicateCount = 1; 598 599 // If the block to be duplicated ends in an unanalyzable fallthrough, don't 600 // duplicate it. 601 // A similar check is necessary in MachineBlockPlacement to make sure pairs of 602 // blocks with unanalyzable fallthrough get layed out contiguously. 603 MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr; 604 SmallVector<MachineOperand, 4> PredCond; 605 if (TII->analyzeBranch(TailBB, PredTBB, PredFBB, PredCond) && 606 TailBB.canFallThrough()) 607 return false; 608 609 // If the target has hardware branch prediction that can handle indirect 610 // branches, duplicating them can often make them predictable when there 611 // are common paths through the code. The limit needs to be high enough 612 // to allow undoing the effects of tail merging and other optimizations 613 // that rearrange the predecessors of the indirect branch. 614 615 bool HasIndirectbr = false; 616 if (!TailBB.empty()) 617 HasIndirectbr = TailBB.back().isIndirectBranch(); 618 619 if (HasIndirectbr && PreRegAlloc) 620 MaxDuplicateCount = TailDupIndirectBranchSize; 621 622 // Check the instructions in the block to determine whether tail-duplication 623 // is invalid or unlikely to be profitable. 624 unsigned InstrCount = 0; 625 for (MachineInstr &MI : TailBB) { 626 // Non-duplicable things shouldn't be tail-duplicated. 627 // CFI instructions are marked as non-duplicable, because Darwin compact 628 // unwind info emission can't handle multiple prologue setups. In case of 629 // DWARF, allow them be duplicated, so that their existence doesn't prevent 630 // tail duplication of some basic blocks, that would be duplicated otherwise. 631 if (MI.isNotDuplicable() && 632 (TailBB.getParent()->getTarget().getTargetTriple().isOSDarwin() || 633 !MI.isCFIInstruction())) 634 return false; 635 636 // Convergent instructions can be duplicated only if doing so doesn't add 637 // new control dependencies, which is what we're going to do here. 638 if (MI.isConvergent()) 639 return false; 640 641 // Do not duplicate 'return' instructions if this is a pre-regalloc run. 642 // A return may expand into a lot more instructions (e.g. reload of callee 643 // saved registers) after PEI. 644 if (PreRegAlloc && MI.isReturn()) 645 return false; 646 647 // Avoid duplicating calls before register allocation. Calls presents a 648 // barrier to register allocation so duplicating them may end up increasing 649 // spills. 650 if (PreRegAlloc && MI.isCall()) 651 return false; 652 653 // TailDuplicator::appendCopies will erroneously place COPYs after 654 // INLINEASM_BR instructions after 4b0aa5724fea, which demonstrates the same 655 // bug that was fixed in f7a53d82c090. 656 // FIXME: Use findPHICopyInsertPoint() to find the correct insertion point 657 // for the COPY when replacing PHIs. 658 if (MI.getOpcode() == TargetOpcode::INLINEASM_BR) 659 return false; 660 661 if (MI.isBundle()) 662 InstrCount += MI.getBundleSize(); 663 else if (!MI.isPHI() && !MI.isMetaInstruction()) 664 InstrCount += 1; 665 666 if (InstrCount > MaxDuplicateCount) 667 return false; 668 } 669 670 // Check if any of the successors of TailBB has a PHI node in which the 671 // value corresponding to TailBB uses a subregister. 672 // If a phi node uses a register paired with a subregister, the actual 673 // "value type" of the phi may differ from the type of the register without 674 // any subregisters. Due to a bug, tail duplication may add a new operand 675 // without a necessary subregister, producing an invalid code. This is 676 // demonstrated by test/CodeGen/Hexagon/tail-dup-subreg-abort.ll. 677 // Disable tail duplication for this case for now, until the problem is 678 // fixed. 679 for (auto *SB : TailBB.successors()) { 680 for (auto &I : *SB) { 681 if (!I.isPHI()) 682 break; 683 unsigned Idx = getPHISrcRegOpIdx(&I, &TailBB); 684 assert(Idx != 0); 685 MachineOperand &PU = I.getOperand(Idx); 686 if (PU.getSubReg() != 0) 687 return false; 688 } 689 } 690 691 if (HasIndirectbr && PreRegAlloc) 692 return true; 693 694 if (IsSimple) 695 return true; 696 697 if (!PreRegAlloc) 698 return true; 699 700 return canCompletelyDuplicateBB(TailBB); 701 } 702 703 /// True if this BB has only one unconditional jump. 704 bool TailDuplicator::isSimpleBB(MachineBasicBlock *TailBB) { 705 if (TailBB->succ_size() != 1) 706 return false; 707 if (TailBB->pred_empty()) 708 return false; 709 MachineBasicBlock::iterator I = TailBB->getFirstNonDebugInstr(true); 710 if (I == TailBB->end()) 711 return true; 712 return I->isUnconditionalBranch(); 713 } 714 715 static bool bothUsedInPHI(const MachineBasicBlock &A, 716 const SmallPtrSet<MachineBasicBlock *, 8> &SuccsB) { 717 for (MachineBasicBlock *BB : A.successors()) 718 if (SuccsB.count(BB) && !BB->empty() && BB->begin()->isPHI()) 719 return true; 720 721 return false; 722 } 723 724 bool TailDuplicator::canCompletelyDuplicateBB(MachineBasicBlock &BB) { 725 for (MachineBasicBlock *PredBB : BB.predecessors()) { 726 if (PredBB->succ_size() > 1) 727 return false; 728 729 MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr; 730 SmallVector<MachineOperand, 4> PredCond; 731 if (TII->analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond)) 732 return false; 733 734 if (!PredCond.empty()) 735 return false; 736 } 737 return true; 738 } 739 740 bool TailDuplicator::duplicateSimpleBB( 741 MachineBasicBlock *TailBB, SmallVectorImpl<MachineBasicBlock *> &TDBBs, 742 const DenseSet<Register> &UsedByPhi) { 743 SmallPtrSet<MachineBasicBlock *, 8> Succs(TailBB->succ_begin(), 744 TailBB->succ_end()); 745 SmallVector<MachineBasicBlock *, 8> Preds(TailBB->predecessors()); 746 bool Changed = false; 747 for (MachineBasicBlock *PredBB : Preds) { 748 if (PredBB->hasEHPadSuccessor() || PredBB->mayHaveInlineAsmBr()) 749 continue; 750 751 if (bothUsedInPHI(*PredBB, Succs)) 752 continue; 753 754 MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr; 755 SmallVector<MachineOperand, 4> PredCond; 756 if (TII->analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond)) 757 continue; 758 759 Changed = true; 760 LLVM_DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB 761 << "From simple Succ: " << *TailBB); 762 763 MachineBasicBlock *NewTarget = *TailBB->succ_begin(); 764 MachineBasicBlock *NextBB = PredBB->getNextNode(); 765 766 // Make PredFBB explicit. 767 if (PredCond.empty()) 768 PredFBB = PredTBB; 769 770 // Make fall through explicit. 771 if (!PredTBB) 772 PredTBB = NextBB; 773 if (!PredFBB) 774 PredFBB = NextBB; 775 776 // Redirect 777 if (PredFBB == TailBB) 778 PredFBB = NewTarget; 779 if (PredTBB == TailBB) 780 PredTBB = NewTarget; 781 782 // Make the branch unconditional if possible 783 if (PredTBB == PredFBB) { 784 PredCond.clear(); 785 PredFBB = nullptr; 786 } 787 788 // Avoid adding fall through branches. 789 if (PredFBB == NextBB) 790 PredFBB = nullptr; 791 if (PredTBB == NextBB && PredFBB == nullptr) 792 PredTBB = nullptr; 793 794 auto DL = PredBB->findBranchDebugLoc(); 795 TII->removeBranch(*PredBB); 796 797 if (!PredBB->isSuccessor(NewTarget)) 798 PredBB->replaceSuccessor(TailBB, NewTarget); 799 else { 800 PredBB->removeSuccessor(TailBB, true); 801 assert(PredBB->succ_size() <= 1); 802 } 803 804 if (PredTBB) 805 TII->insertBranch(*PredBB, PredTBB, PredFBB, PredCond, DL); 806 807 TDBBs.push_back(PredBB); 808 } 809 return Changed; 810 } 811 812 bool TailDuplicator::canTailDuplicate(MachineBasicBlock *TailBB, 813 MachineBasicBlock *PredBB) { 814 // EH edges are ignored by analyzeBranch. 815 if (PredBB->succ_size() > 1) 816 return false; 817 818 MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr; 819 SmallVector<MachineOperand, 4> PredCond; 820 if (TII->analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond)) 821 return false; 822 if (!PredCond.empty()) 823 return false; 824 // FIXME: This is overly conservative; it may be ok to relax this in the 825 // future under more specific conditions. If TailBB is an INLINEASM_BR 826 // indirect target, we need to see if the edge from PredBB to TailBB is from 827 // an INLINEASM_BR in PredBB, and then also if that edge was from the 828 // indirect target list, fallthrough/default target, or potentially both. If 829 // it's both, TailDuplicator::tailDuplicate will remove the edge, corrupting 830 // the successor list in PredBB and predecessor list in TailBB. 831 if (TailBB->isInlineAsmBrIndirectTarget()) 832 return false; 833 return true; 834 } 835 836 /// If it is profitable, duplicate TailBB's contents in each 837 /// of its predecessors. 838 /// \p IsSimple result of isSimpleBB 839 /// \p TailBB Block to be duplicated. 840 /// \p ForcedLayoutPred When non-null, use this block as the layout predecessor 841 /// instead of the previous block in MF's order. 842 /// \p TDBBs A vector to keep track of all blocks tail-duplicated 843 /// into. 844 /// \p Copies A vector of copy instructions inserted. Used later to 845 /// walk all the inserted copies and remove redundant ones. 846 bool TailDuplicator::tailDuplicate(bool IsSimple, MachineBasicBlock *TailBB, 847 MachineBasicBlock *ForcedLayoutPred, 848 SmallVectorImpl<MachineBasicBlock *> &TDBBs, 849 SmallVectorImpl<MachineInstr *> &Copies, 850 SmallVectorImpl<MachineBasicBlock *> *CandidatePtr) { 851 LLVM_DEBUG(dbgs() << "\n*** Tail-duplicating " << printMBBReference(*TailBB) 852 << '\n'); 853 854 bool ShouldUpdateTerminators = TailBB->canFallThrough(); 855 856 DenseSet<Register> UsedByPhi; 857 getRegsUsedByPHIs(*TailBB, &UsedByPhi); 858 859 if (IsSimple) 860 return duplicateSimpleBB(TailBB, TDBBs, UsedByPhi); 861 862 // Iterate through all the unique predecessors and tail-duplicate this 863 // block into them, if possible. Copying the list ahead of time also 864 // avoids trouble with the predecessor list reallocating. 865 bool Changed = false; 866 SmallSetVector<MachineBasicBlock *, 8> Preds; 867 if (CandidatePtr) 868 Preds.insert(CandidatePtr->begin(), CandidatePtr->end()); 869 else 870 Preds.insert(TailBB->pred_begin(), TailBB->pred_end()); 871 872 for (MachineBasicBlock *PredBB : Preds) { 873 assert(TailBB != PredBB && 874 "Single-block loop should have been rejected earlier!"); 875 876 if (!canTailDuplicate(TailBB, PredBB)) 877 continue; 878 879 // Don't duplicate into a fall-through predecessor (at least for now). 880 // If profile is available, findDuplicateCandidates can choose better 881 // fall-through predecessor. 882 if (!(MF->getFunction().hasProfileData() && LayoutMode)) { 883 bool IsLayoutSuccessor = false; 884 if (ForcedLayoutPred) 885 IsLayoutSuccessor = (ForcedLayoutPred == PredBB); 886 else if (PredBB->isLayoutSuccessor(TailBB) && PredBB->canFallThrough()) 887 IsLayoutSuccessor = true; 888 if (IsLayoutSuccessor) 889 continue; 890 } 891 892 LLVM_DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB 893 << "From Succ: " << *TailBB); 894 895 TDBBs.push_back(PredBB); 896 897 // Remove PredBB's unconditional branch. 898 TII->removeBranch(*PredBB); 899 900 // Clone the contents of TailBB into PredBB. 901 DenseMap<Register, RegSubRegPair> LocalVRMap; 902 SmallVector<std::pair<Register, RegSubRegPair>, 4> CopyInfos; 903 for (MachineInstr &MI : llvm::make_early_inc_range(*TailBB)) { 904 if (MI.isPHI()) { 905 // Replace the uses of the def of the PHI with the register coming 906 // from PredBB. 907 processPHI(&MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi, true); 908 } else { 909 // Replace def of virtual registers with new registers, and update 910 // uses with PHI source register or the new registers. 911 duplicateInstruction(&MI, TailBB, PredBB, LocalVRMap, UsedByPhi); 912 } 913 } 914 appendCopies(PredBB, CopyInfos, Copies); 915 916 NumTailDupAdded += TailBB->size() - 1; // subtract one for removed branch 917 918 // Update the CFG. 919 PredBB->removeSuccessor(PredBB->succ_begin()); 920 assert(PredBB->succ_empty() && 921 "TailDuplicate called on block with multiple successors!"); 922 for (MachineBasicBlock *Succ : TailBB->successors()) 923 PredBB->addSuccessor(Succ, MBPI->getEdgeProbability(TailBB, Succ)); 924 925 // Update branches in pred to jump to tail's layout successor if needed. 926 if (ShouldUpdateTerminators) 927 PredBB->updateTerminator(TailBB->getNextNode()); 928 929 Changed = true; 930 ++NumTailDups; 931 } 932 933 // If TailBB was duplicated into all its predecessors except for the prior 934 // block, which falls through unconditionally, move the contents of this 935 // block into the prior block. 936 MachineBasicBlock *PrevBB = ForcedLayoutPred; 937 if (!PrevBB) 938 PrevBB = &*std::prev(TailBB->getIterator()); 939 MachineBasicBlock *PriorTBB = nullptr, *PriorFBB = nullptr; 940 SmallVector<MachineOperand, 4> PriorCond; 941 // This has to check PrevBB->succ_size() because EH edges are ignored by 942 // analyzeBranch. 943 if (PrevBB->succ_size() == 1 && 944 // Layout preds are not always CFG preds. Check. 945 *PrevBB->succ_begin() == TailBB && 946 !TII->analyzeBranch(*PrevBB, PriorTBB, PriorFBB, PriorCond) && 947 PriorCond.empty() && 948 (!PriorTBB || PriorTBB == TailBB) && 949 TailBB->pred_size() == 1 && 950 !TailBB->hasAddressTaken()) { 951 LLVM_DEBUG(dbgs() << "\nMerging into block: " << *PrevBB 952 << "From MBB: " << *TailBB); 953 // There may be a branch to the layout successor. This is unlikely but it 954 // happens. The correct thing to do is to remove the branch before 955 // duplicating the instructions in all cases. 956 bool RemovedBranches = TII->removeBranch(*PrevBB) != 0; 957 958 // If there are still tail instructions, abort the merge 959 if (PrevBB->getFirstTerminator() == PrevBB->end()) { 960 if (PreRegAlloc) { 961 DenseMap<Register, RegSubRegPair> LocalVRMap; 962 SmallVector<std::pair<Register, RegSubRegPair>, 4> CopyInfos; 963 MachineBasicBlock::iterator I = TailBB->begin(); 964 // Process PHI instructions first. 965 while (I != TailBB->end() && I->isPHI()) { 966 // Replace the uses of the def of the PHI with the register coming 967 // from PredBB. 968 MachineInstr *MI = &*I++; 969 processPHI(MI, TailBB, PrevBB, LocalVRMap, CopyInfos, UsedByPhi, 970 true); 971 } 972 973 // Now copy the non-PHI instructions. 974 while (I != TailBB->end()) { 975 // Replace def of virtual registers with new registers, and update 976 // uses with PHI source register or the new registers. 977 MachineInstr *MI = &*I++; 978 assert(!MI->isBundle() && "Not expecting bundles before regalloc!"); 979 duplicateInstruction(MI, TailBB, PrevBB, LocalVRMap, UsedByPhi); 980 MI->eraseFromParent(); 981 } 982 appendCopies(PrevBB, CopyInfos, Copies); 983 } else { 984 TII->removeBranch(*PrevBB); 985 // No PHIs to worry about, just splice the instructions over. 986 PrevBB->splice(PrevBB->end(), TailBB, TailBB->begin(), TailBB->end()); 987 } 988 PrevBB->removeSuccessor(PrevBB->succ_begin()); 989 assert(PrevBB->succ_empty()); 990 PrevBB->transferSuccessors(TailBB); 991 992 // Update branches in PrevBB based on Tail's layout successor. 993 if (ShouldUpdateTerminators) 994 PrevBB->updateTerminator(TailBB->getNextNode()); 995 996 TDBBs.push_back(PrevBB); 997 Changed = true; 998 } else { 999 LLVM_DEBUG(dbgs() << "Abort merging blocks, the predecessor still " 1000 "contains terminator instructions"); 1001 // Return early if no changes were made 1002 if (!Changed) 1003 return RemovedBranches; 1004 } 1005 Changed |= RemovedBranches; 1006 } 1007 1008 // If this is after register allocation, there are no phis to fix. 1009 if (!PreRegAlloc) 1010 return Changed; 1011 1012 // If we made no changes so far, we are safe. 1013 if (!Changed) 1014 return Changed; 1015 1016 // Handle the nasty case in that we duplicated a block that is part of a loop 1017 // into some but not all of its predecessors. For example: 1018 // 1 -> 2 <-> 3 | 1019 // \ | 1020 // \---> rest | 1021 // if we duplicate 2 into 1 but not into 3, we end up with 1022 // 12 -> 3 <-> 2 -> rest | 1023 // \ / | 1024 // \----->-----/ | 1025 // If there was a "var = phi(1, 3)" in 2, it has to be ultimately replaced 1026 // with a phi in 3 (which now dominates 2). 1027 // What we do here is introduce a copy in 3 of the register defined by the 1028 // phi, just like when we are duplicating 2 into 3, but we don't copy any 1029 // real instructions or remove the 3 -> 2 edge from the phi in 2. 1030 for (MachineBasicBlock *PredBB : Preds) { 1031 if (is_contained(TDBBs, PredBB)) 1032 continue; 1033 1034 // EH edges 1035 if (PredBB->succ_size() != 1) 1036 continue; 1037 1038 DenseMap<Register, RegSubRegPair> LocalVRMap; 1039 SmallVector<std::pair<Register, RegSubRegPair>, 4> CopyInfos; 1040 // Process PHI instructions first. 1041 for (MachineInstr &MI : make_early_inc_range(TailBB->phis())) { 1042 // Replace the uses of the def of the PHI with the register coming 1043 // from PredBB. 1044 processPHI(&MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi, false); 1045 } 1046 appendCopies(PredBB, CopyInfos, Copies); 1047 } 1048 1049 return Changed; 1050 } 1051 1052 /// At the end of the block \p MBB generate COPY instructions between registers 1053 /// described by \p CopyInfos. Append resulting instructions to \p Copies. 1054 void TailDuplicator::appendCopies(MachineBasicBlock *MBB, 1055 SmallVectorImpl<std::pair<Register, RegSubRegPair>> &CopyInfos, 1056 SmallVectorImpl<MachineInstr*> &Copies) { 1057 MachineBasicBlock::iterator Loc = MBB->getFirstTerminator(); 1058 const MCInstrDesc &CopyD = TII->get(TargetOpcode::COPY); 1059 for (auto &CI : CopyInfos) { 1060 auto C = BuildMI(*MBB, Loc, DebugLoc(), CopyD, CI.first) 1061 .addReg(CI.second.Reg, 0, CI.second.SubReg); 1062 Copies.push_back(C); 1063 } 1064 } 1065 1066 /// Remove the specified dead machine basic block from the function, updating 1067 /// the CFG. 1068 void TailDuplicator::removeDeadBlock( 1069 MachineBasicBlock *MBB, 1070 function_ref<void(MachineBasicBlock *)> *RemovalCallback) { 1071 assert(MBB->pred_empty() && "MBB must be dead!"); 1072 LLVM_DEBUG(dbgs() << "\nRemoving MBB: " << *MBB); 1073 1074 MachineFunction *MF = MBB->getParent(); 1075 // Update the call site info. 1076 for (const MachineInstr &MI : *MBB) 1077 if (MI.shouldUpdateCallSiteInfo()) 1078 MF->eraseCallSiteInfo(&MI); 1079 1080 if (RemovalCallback) 1081 (*RemovalCallback)(MBB); 1082 1083 // Remove all successors. 1084 while (!MBB->succ_empty()) 1085 MBB->removeSuccessor(MBB->succ_end() - 1); 1086 1087 // Remove the block. 1088 MBB->eraseFromParent(); 1089 } 1090