1 //===---- PPCReduceCRLogicals.cpp - Reduce CR Bit Logical operations ------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===---------------------------------------------------------------------===// 9 // 10 // This pass aims to reduce the number of logical operations on bits in the CR 11 // register. These instructions have a fairly high latency and only a single 12 // pipeline at their disposal in modern PPC cores. Furthermore, they have a 13 // tendency to occur in fairly small blocks where there's little opportunity 14 // to hide the latency between the CR logical operation and its user. 15 // 16 //===---------------------------------------------------------------------===// 17 18 #include "PPC.h" 19 #include "PPCInstrInfo.h" 20 #include "PPCTargetMachine.h" 21 #include "llvm/ADT/Statistic.h" 22 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" 23 #include "llvm/CodeGen/MachineDominators.h" 24 #include "llvm/CodeGen/MachineFunctionPass.h" 25 #include "llvm/CodeGen/MachineInstrBuilder.h" 26 #include "llvm/CodeGen/MachineRegisterInfo.h" 27 #include "llvm/Support/Debug.h" 28 29 using namespace llvm; 30 31 #define DEBUG_TYPE "ppc-reduce-cr-ops" 32 33 STATISTIC(NumContainedSingleUseBinOps, 34 "Number of single-use binary CR logical ops contained in a block"); 35 STATISTIC(NumToSplitBlocks, 36 "Number of binary CR logical ops that can be used to split blocks"); 37 STATISTIC(TotalCRLogicals, "Number of CR logical ops."); 38 STATISTIC(TotalNullaryCRLogicals, 39 "Number of nullary CR logical ops (CRSET/CRUNSET)."); 40 STATISTIC(TotalUnaryCRLogicals, "Number of unary CR logical ops."); 41 STATISTIC(TotalBinaryCRLogicals, "Number of CR logical ops."); 42 STATISTIC(NumBlocksSplitOnBinaryCROp, 43 "Number of blocks split on CR binary logical ops."); 44 STATISTIC(NumNotSplitIdenticalOperands, 45 "Number of blocks not split due to operands being identical."); 46 STATISTIC(NumNotSplitChainCopies, 47 "Number of blocks not split due to operands being chained copies."); 48 STATISTIC(NumNotSplitWrongOpcode, 49 "Number of blocks not split due to the wrong opcode."); 50 51 namespace llvm { 52 void initializePPCReduceCRLogicalsPass(PassRegistry&); 53 } 54 55 /// Given a basic block \p Successor that potentially contains PHIs, this 56 /// function will look for any incoming values in the PHIs that are supposed to 57 /// be coming from \p OrigMBB but whose definition is actually in \p NewMBB. 58 /// Any such PHIs will be updated to reflect reality. 59 static void updatePHIs(MachineBasicBlock *Successor, MachineBasicBlock *OrigMBB, 60 MachineBasicBlock *NewMBB, MachineRegisterInfo *MRI) { 61 for (auto &MI : Successor->instrs()) { 62 if (!MI.isPHI()) 63 continue; 64 // This is a really ugly-looking loop, but it was pillaged directly from 65 // MachineBasicBlock::transferSuccessorsAndUpdatePHIs(). 66 for (unsigned i = 2, e = MI.getNumOperands() + 1; i != e; i += 2) { 67 MachineOperand &MO = MI.getOperand(i); 68 if (MO.getMBB() == OrigMBB) { 69 // Check if the instruction is actualy defined in NewMBB. 70 if (MI.getOperand(i - 1).isReg()) { 71 MachineInstr *DefMI = MRI->getVRegDef(MI.getOperand(i - 1).getReg()); 72 if (DefMI->getParent() == NewMBB || 73 !OrigMBB->isSuccessor(Successor)) { 74 MO.setMBB(NewMBB); 75 break; 76 } 77 } 78 } 79 } 80 } 81 } 82 83 /// Given a basic block \p Successor that potentially contains PHIs, this 84 /// function will look for PHIs that have an incoming value from \p OrigMBB 85 /// and will add the same incoming value from \p NewMBB. 86 /// NOTE: This should only be used if \p NewMBB is an immediate dominator of 87 /// \p OrigMBB. 88 static void addIncomingValuesToPHIs(MachineBasicBlock *Successor, 89 MachineBasicBlock *OrigMBB, 90 MachineBasicBlock *NewMBB, 91 MachineRegisterInfo *MRI) { 92 assert(OrigMBB->isSuccessor(NewMBB) && 93 "NewMBB must be a sucessor of OrigMBB"); 94 for (auto &MI : Successor->instrs()) { 95 if (!MI.isPHI()) 96 continue; 97 // This is a really ugly-looking loop, but it was pillaged directly from 98 // MachineBasicBlock::transferSuccessorsAndUpdatePHIs(). 99 for (unsigned i = 2, e = MI.getNumOperands() + 1; i != e; i += 2) { 100 MachineOperand &MO = MI.getOperand(i); 101 if (MO.getMBB() == OrigMBB) { 102 MachineInstrBuilder MIB(*MI.getParent()->getParent(), &MI); 103 MIB.addReg(MI.getOperand(i - 1).getReg()).addMBB(NewMBB); 104 break; 105 } 106 } 107 } 108 } 109 110 struct BlockSplitInfo { 111 MachineInstr *OrigBranch; 112 MachineInstr *SplitBefore; 113 MachineInstr *SplitCond; 114 bool InvertNewBranch; 115 bool InvertOrigBranch; 116 bool BranchToFallThrough; 117 const MachineBranchProbabilityInfo *MBPI; 118 MachineInstr *MIToDelete; 119 MachineInstr *NewCond; 120 bool allInstrsInSameMBB() { 121 if (!OrigBranch || !SplitBefore || !SplitCond) 122 return false; 123 MachineBasicBlock *MBB = OrigBranch->getParent(); 124 if (SplitBefore->getParent() != MBB || SplitCond->getParent() != MBB) 125 return false; 126 if (MIToDelete && MIToDelete->getParent() != MBB) 127 return false; 128 if (NewCond && NewCond->getParent() != MBB) 129 return false; 130 return true; 131 } 132 }; 133 134 /// Splits a MachineBasicBlock to branch before \p SplitBefore. The original 135 /// branch is \p OrigBranch. The target of the new branch can either be the same 136 /// as the target of the original branch or the fallthrough successor of the 137 /// original block as determined by \p BranchToFallThrough. The branch 138 /// conditions will be inverted according to \p InvertNewBranch and 139 /// \p InvertOrigBranch. If an instruction that previously fed the branch is to 140 /// be deleted, it is provided in \p MIToDelete and \p NewCond will be used as 141 /// the branch condition. The branch probabilities will be set if the 142 /// MachineBranchProbabilityInfo isn't null. 143 static bool splitMBB(BlockSplitInfo &BSI) { 144 assert(BSI.allInstrsInSameMBB() && 145 "All instructions must be in the same block."); 146 147 MachineBasicBlock *ThisMBB = BSI.OrigBranch->getParent(); 148 MachineFunction *MF = ThisMBB->getParent(); 149 MachineRegisterInfo *MRI = &MF->getRegInfo(); 150 assert(MRI->isSSA() && "Can only do this while the function is in SSA form."); 151 if (ThisMBB->succ_size() != 2) { 152 DEBUG(dbgs() << "Don't know how to handle blocks that don't have exactly" 153 << " two succesors.\n"); 154 return false; 155 } 156 157 const PPCInstrInfo *TII = MF->getSubtarget<PPCSubtarget>().getInstrInfo(); 158 unsigned OrigBROpcode = BSI.OrigBranch->getOpcode(); 159 unsigned InvertedOpcode = 160 OrigBROpcode == PPC::BC 161 ? PPC::BCn 162 : OrigBROpcode == PPC::BCn 163 ? PPC::BC 164 : OrigBROpcode == PPC::BCLR ? PPC::BCLRn : PPC::BCLR; 165 unsigned NewBROpcode = BSI.InvertNewBranch ? InvertedOpcode : OrigBROpcode; 166 MachineBasicBlock *OrigTarget = BSI.OrigBranch->getOperand(1).getMBB(); 167 MachineBasicBlock *OrigFallThrough = OrigTarget == *ThisMBB->succ_begin() 168 ? *ThisMBB->succ_rbegin() 169 : *ThisMBB->succ_begin(); 170 MachineBasicBlock *NewBRTarget = 171 BSI.BranchToFallThrough ? OrigFallThrough : OrigTarget; 172 BranchProbability ProbToNewTarget = 173 !BSI.MBPI ? BranchProbability::getUnknown() 174 : BSI.MBPI->getEdgeProbability(ThisMBB, NewBRTarget); 175 176 // Create a new basic block. 177 MachineBasicBlock::iterator InsertPoint = BSI.SplitBefore; 178 const BasicBlock *LLVM_BB = ThisMBB->getBasicBlock(); 179 MachineFunction::iterator It = ThisMBB->getIterator(); 180 MachineBasicBlock *NewMBB = MF->CreateMachineBasicBlock(LLVM_BB); 181 MF->insert(++It, NewMBB); 182 183 // Move everything after SplitBefore into the new block. 184 NewMBB->splice(NewMBB->end(), ThisMBB, InsertPoint, ThisMBB->end()); 185 NewMBB->transferSuccessors(ThisMBB); 186 187 // Add the two successors to ThisMBB. The probabilities come from the 188 // existing blocks if available. 189 ThisMBB->addSuccessor(NewBRTarget, ProbToNewTarget); 190 ThisMBB->addSuccessor(NewMBB, ProbToNewTarget.getCompl()); 191 192 // Add the branches to ThisMBB. 193 BuildMI(*ThisMBB, ThisMBB->end(), BSI.SplitBefore->getDebugLoc(), 194 TII->get(NewBROpcode)) 195 .addReg(BSI.SplitCond->getOperand(0).getReg()) 196 .addMBB(NewBRTarget); 197 BuildMI(*ThisMBB, ThisMBB->end(), BSI.SplitBefore->getDebugLoc(), 198 TII->get(PPC::B)) 199 .addMBB(NewMBB); 200 if (BSI.MIToDelete) 201 BSI.MIToDelete->eraseFromParent(); 202 203 // Change the condition on the original branch and invert it if requested. 204 auto FirstTerminator = NewMBB->getFirstTerminator(); 205 if (BSI.NewCond) { 206 assert(FirstTerminator->getOperand(0).isReg() && 207 "Can't update condition of unconditional branch."); 208 FirstTerminator->getOperand(0).setReg(BSI.NewCond->getOperand(0).getReg()); 209 } 210 if (BSI.InvertOrigBranch) 211 FirstTerminator->setDesc(TII->get(InvertedOpcode)); 212 213 // If any of the PHIs in the successors of NewMBB reference values that 214 // now come from NewMBB, they need to be updated. 215 for (auto *Succ : NewMBB->successors()) { 216 updatePHIs(Succ, ThisMBB, NewMBB, MRI); 217 } 218 addIncomingValuesToPHIs(NewBRTarget, ThisMBB, NewMBB, MRI); 219 220 DEBUG(dbgs() << "After splitting, ThisMBB:\n"; ThisMBB->dump()); 221 DEBUG(dbgs() << "NewMBB:\n"; NewMBB->dump()); 222 DEBUG(dbgs() << "New branch-to block:\n"; NewBRTarget->dump()); 223 return true; 224 } 225 226 static bool isBinary(MachineInstr &MI) { 227 return MI.getNumOperands() == 3; 228 } 229 230 static bool isNullary(MachineInstr &MI) { 231 return MI.getNumOperands() == 1; 232 } 233 234 /// Given a CR logical operation \p CROp, branch opcode \p BROp as well as 235 /// a flag to indicate if the first operand of \p CROp is used as the 236 /// SplitBefore operand, determines whether either of the branches are to be 237 /// inverted as well as whether the new target should be the original 238 /// fall-through block. 239 static void 240 computeBranchTargetAndInversion(unsigned CROp, unsigned BROp, bool UsingDef1, 241 bool &InvertNewBranch, bool &InvertOrigBranch, 242 bool &TargetIsFallThrough) { 243 // The conditions under which each of the output operands should be [un]set 244 // can certainly be written much more concisely with just 3 if statements or 245 // ternary expressions. However, this provides a much clearer overview to the 246 // reader as to what is set for each <CROp, BROp, OpUsed> combination. 247 if (BROp == PPC::BC || BROp == PPC::BCLR) { 248 // Regular branches. 249 switch (CROp) { 250 default: 251 llvm_unreachable("Don't know how to handle this CR logical."); 252 case PPC::CROR: 253 InvertNewBranch = false; 254 InvertOrigBranch = false; 255 TargetIsFallThrough = false; 256 return; 257 case PPC::CRAND: 258 InvertNewBranch = true; 259 InvertOrigBranch = false; 260 TargetIsFallThrough = true; 261 return; 262 case PPC::CRNAND: 263 InvertNewBranch = true; 264 InvertOrigBranch = true; 265 TargetIsFallThrough = false; 266 return; 267 case PPC::CRNOR: 268 InvertNewBranch = false; 269 InvertOrigBranch = true; 270 TargetIsFallThrough = true; 271 return; 272 case PPC::CRORC: 273 InvertNewBranch = UsingDef1; 274 InvertOrigBranch = !UsingDef1; 275 TargetIsFallThrough = false; 276 return; 277 case PPC::CRANDC: 278 InvertNewBranch = !UsingDef1; 279 InvertOrigBranch = !UsingDef1; 280 TargetIsFallThrough = true; 281 return; 282 } 283 } else if (BROp == PPC::BCn || BROp == PPC::BCLRn) { 284 // Negated branches. 285 switch (CROp) { 286 default: 287 llvm_unreachable("Don't know how to handle this CR logical."); 288 case PPC::CROR: 289 InvertNewBranch = true; 290 InvertOrigBranch = false; 291 TargetIsFallThrough = true; 292 return; 293 case PPC::CRAND: 294 InvertNewBranch = false; 295 InvertOrigBranch = false; 296 TargetIsFallThrough = false; 297 return; 298 case PPC::CRNAND: 299 InvertNewBranch = false; 300 InvertOrigBranch = true; 301 TargetIsFallThrough = true; 302 return; 303 case PPC::CRNOR: 304 InvertNewBranch = true; 305 InvertOrigBranch = true; 306 TargetIsFallThrough = false; 307 return; 308 case PPC::CRORC: 309 InvertNewBranch = !UsingDef1; 310 InvertOrigBranch = !UsingDef1; 311 TargetIsFallThrough = true; 312 return; 313 case PPC::CRANDC: 314 InvertNewBranch = UsingDef1; 315 InvertOrigBranch = !UsingDef1; 316 TargetIsFallThrough = false; 317 return; 318 } 319 } else 320 llvm_unreachable("Don't know how to handle this branch."); 321 } 322 323 namespace { 324 325 class PPCReduceCRLogicals : public MachineFunctionPass { 326 327 public: 328 static char ID; 329 struct CRLogicalOpInfo { 330 MachineInstr *MI; 331 // FIXME: If chains of copies are to be handled, this should be a vector. 332 std::pair<MachineInstr*, MachineInstr*> CopyDefs; 333 std::pair<MachineInstr*, MachineInstr*> TrueDefs; 334 unsigned IsBinary : 1; 335 unsigned IsNullary : 1; 336 unsigned ContainedInBlock : 1; 337 unsigned FeedsISEL : 1; 338 unsigned FeedsBR : 1; 339 unsigned FeedsLogical : 1; 340 unsigned SingleUse : 1; 341 unsigned DefsSingleUse : 1; 342 unsigned SubregDef1; 343 unsigned SubregDef2; 344 CRLogicalOpInfo() : MI(nullptr), IsBinary(0), IsNullary(0), 345 ContainedInBlock(0), FeedsISEL(0), FeedsBR(0), 346 FeedsLogical(0), SingleUse(0), DefsSingleUse(1), 347 SubregDef1(0), SubregDef2(0) { } 348 void dump(); 349 }; 350 351 private: 352 const PPCInstrInfo *TII; 353 MachineFunction *MF; 354 MachineRegisterInfo *MRI; 355 const MachineBranchProbabilityInfo *MBPI; 356 357 // A vector to contain all the CR logical operations 358 std::vector<CRLogicalOpInfo> AllCRLogicalOps; 359 void initialize(MachineFunction &MFParm); 360 void collectCRLogicals(); 361 bool handleCROp(CRLogicalOpInfo &CRI); 362 bool splitBlockOnBinaryCROp(CRLogicalOpInfo &CRI); 363 static bool isCRLogical(MachineInstr &MI) { 364 unsigned Opc = MI.getOpcode(); 365 return Opc == PPC::CRAND || Opc == PPC::CRNAND || Opc == PPC::CROR || 366 Opc == PPC::CRXOR || Opc == PPC::CRNOR || Opc == PPC::CREQV || 367 Opc == PPC::CRANDC || Opc == PPC::CRORC || Opc == PPC::CRSET || 368 Opc == PPC::CRUNSET || Opc == PPC::CR6SET || Opc == PPC::CR6UNSET; 369 } 370 bool simplifyCode() { 371 bool Changed = false; 372 // Not using a range-based for loop here as the vector may grow while being 373 // operated on. 374 for (unsigned i = 0; i < AllCRLogicalOps.size(); i++) 375 Changed |= handleCROp(AllCRLogicalOps[i]); 376 return Changed; 377 } 378 379 public: 380 PPCReduceCRLogicals() : MachineFunctionPass(ID) { 381 initializePPCReduceCRLogicalsPass(*PassRegistry::getPassRegistry()); 382 } 383 384 MachineInstr *lookThroughCRCopy(unsigned Reg, unsigned &Subreg, 385 MachineInstr *&CpDef); 386 bool runOnMachineFunction(MachineFunction &MF) override { 387 if (skipFunction(MF.getFunction())) 388 return false; 389 390 // If the subtarget doesn't use CR bits, there's nothing to do. 391 const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>(); 392 if (!STI.useCRBits()) 393 return false; 394 395 initialize(MF); 396 collectCRLogicals(); 397 return simplifyCode(); 398 } 399 CRLogicalOpInfo createCRLogicalOpInfo(MachineInstr &MI); 400 void getAnalysisUsage(AnalysisUsage &AU) const override { 401 AU.addRequired<MachineBranchProbabilityInfo>(); 402 AU.addRequired<MachineDominatorTree>(); 403 MachineFunctionPass::getAnalysisUsage(AU); 404 } 405 }; 406 407 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 408 LLVM_DUMP_METHOD void PPCReduceCRLogicals::CRLogicalOpInfo::dump() { 409 dbgs() << "CRLogicalOpMI: "; 410 MI->dump(); 411 dbgs() << "IsBinary: " << IsBinary << ", FeedsISEL: " << FeedsISEL; 412 dbgs() << ", FeedsBR: " << FeedsBR << ", FeedsLogical: "; 413 dbgs() << FeedsLogical << ", SingleUse: " << SingleUse; 414 dbgs() << ", DefsSingleUse: " << DefsSingleUse; 415 dbgs() << ", SubregDef1: " << SubregDef1 << ", SubregDef2: "; 416 dbgs() << SubregDef2 << ", ContainedInBlock: " << ContainedInBlock; 417 if (!IsNullary) { 418 dbgs() << "\nDefs:\n"; 419 TrueDefs.first->dump(); 420 } 421 if (IsBinary) 422 TrueDefs.second->dump(); 423 dbgs() << "\n"; 424 if (CopyDefs.first) { 425 dbgs() << "CopyDef1: "; 426 CopyDefs.first->dump(); 427 } 428 if (CopyDefs.second) { 429 dbgs() << "CopyDef2: "; 430 CopyDefs.second->dump(); 431 } 432 } 433 #endif 434 435 PPCReduceCRLogicals::CRLogicalOpInfo 436 PPCReduceCRLogicals::createCRLogicalOpInfo(MachineInstr &MIParam) { 437 CRLogicalOpInfo Ret; 438 Ret.MI = &MIParam; 439 // Get the defs 440 if (isNullary(MIParam)) { 441 Ret.IsNullary = 1; 442 Ret.TrueDefs = std::make_pair(nullptr, nullptr); 443 Ret.CopyDefs = std::make_pair(nullptr, nullptr); 444 } else { 445 MachineInstr *Def1 = lookThroughCRCopy(MIParam.getOperand(1).getReg(), 446 Ret.SubregDef1, Ret.CopyDefs.first); 447 Ret.DefsSingleUse &= 448 MRI->hasOneNonDBGUse(Def1->getOperand(0).getReg()); 449 Ret.DefsSingleUse &= 450 MRI->hasOneNonDBGUse(Ret.CopyDefs.first->getOperand(0).getReg()); 451 assert(Def1 && "Must be able to find a definition of operand 1."); 452 if (isBinary(MIParam)) { 453 Ret.IsBinary = 1; 454 MachineInstr *Def2 = lookThroughCRCopy(MIParam.getOperand(2).getReg(), 455 Ret.SubregDef2, 456 Ret.CopyDefs.second); 457 Ret.DefsSingleUse &= 458 MRI->hasOneNonDBGUse(Def2->getOperand(0).getReg()); 459 Ret.DefsSingleUse &= 460 MRI->hasOneNonDBGUse(Ret.CopyDefs.second->getOperand(0).getReg()); 461 assert(Def2 && "Must be able to find a definition of operand 2."); 462 Ret.TrueDefs = std::make_pair(Def1, Def2); 463 } else { 464 Ret.TrueDefs = std::make_pair(Def1, nullptr); 465 Ret.CopyDefs.second = nullptr; 466 } 467 } 468 469 Ret.ContainedInBlock = 1; 470 // Get the uses 471 for (MachineInstr &UseMI : 472 MRI->use_nodbg_instructions(MIParam.getOperand(0).getReg())) { 473 unsigned Opc = UseMI.getOpcode(); 474 if (Opc == PPC::ISEL || Opc == PPC::ISEL8) 475 Ret.FeedsISEL = 1; 476 if (Opc == PPC::BC || Opc == PPC::BCn || Opc == PPC::BCLR || 477 Opc == PPC::BCLRn) 478 Ret.FeedsBR = 1; 479 Ret.FeedsLogical = isCRLogical(UseMI); 480 if (UseMI.getParent() != MIParam.getParent()) 481 Ret.ContainedInBlock = 0; 482 } 483 Ret.SingleUse = MRI->hasOneNonDBGUse(MIParam.getOperand(0).getReg()) ? 1 : 0; 484 485 // We now know whether all the uses of the CR logical are in the same block. 486 if (!Ret.IsNullary) { 487 Ret.ContainedInBlock &= 488 (MIParam.getParent() == Ret.TrueDefs.first->getParent()); 489 if (Ret.IsBinary) 490 Ret.ContainedInBlock &= 491 (MIParam.getParent() == Ret.TrueDefs.second->getParent()); 492 } 493 DEBUG(Ret.dump()); 494 if (Ret.IsBinary && Ret.ContainedInBlock && Ret.SingleUse) { 495 NumContainedSingleUseBinOps++; 496 if (Ret.FeedsBR && Ret.DefsSingleUse) 497 NumToSplitBlocks++; 498 } 499 return Ret; 500 } 501 502 /// Looks trhough a COPY instruction to the actual definition of the CR-bit 503 /// register and returns the instruction that defines it. 504 /// FIXME: This currently handles what is by-far the most common case: 505 /// an instruction that defines a CR field followed by a single copy of a bit 506 /// from that field into a virtual register. If chains of copies need to be 507 /// handled, this should have a loop until a non-copy instruction is found. 508 MachineInstr *PPCReduceCRLogicals::lookThroughCRCopy(unsigned Reg, 509 unsigned &Subreg, 510 MachineInstr *&CpDef) { 511 Subreg = -1; 512 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 513 return nullptr; 514 MachineInstr *Copy = MRI->getVRegDef(Reg); 515 CpDef = Copy; 516 if (!Copy->isCopy()) 517 return Copy; 518 unsigned CopySrc = Copy->getOperand(1).getReg(); 519 Subreg = Copy->getOperand(1).getSubReg(); 520 if (!TargetRegisterInfo::isVirtualRegister(CopySrc)) { 521 const TargetRegisterInfo *TRI = &TII->getRegisterInfo(); 522 // Set the Subreg 523 if (CopySrc == PPC::CR0EQ || CopySrc == PPC::CR6EQ) 524 Subreg = PPC::sub_eq; 525 if (CopySrc == PPC::CR0LT || CopySrc == PPC::CR6LT) 526 Subreg = PPC::sub_lt; 527 if (CopySrc == PPC::CR0GT || CopySrc == PPC::CR6GT) 528 Subreg = PPC::sub_gt; 529 if (CopySrc == PPC::CR0UN || CopySrc == PPC::CR6UN) 530 Subreg = PPC::sub_un; 531 // Loop backwards and return the first MI that modifies the physical CR Reg. 532 MachineBasicBlock::iterator Me = Copy, B = Copy->getParent()->begin(); 533 while (Me != B) 534 if ((--Me)->modifiesRegister(CopySrc, TRI)) 535 return &*Me; 536 return nullptr; 537 } 538 return MRI->getVRegDef(CopySrc); 539 } 540 541 void PPCReduceCRLogicals::initialize(MachineFunction &MFParam) { 542 MF = &MFParam; 543 MRI = &MF->getRegInfo(); 544 TII = MF->getSubtarget<PPCSubtarget>().getInstrInfo(); 545 MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); 546 547 AllCRLogicalOps.clear(); 548 } 549 550 /// Contains all the implemented transformations on CR logical operations. 551 /// For example, a binary CR logical can be used to split a block on its inputs, 552 /// a unary CR logical might be used to change the condition code on a 553 /// comparison feeding it. A nullary CR logical might simply be removable 554 /// if the user of the bit it [un]sets can be transformed. 555 bool PPCReduceCRLogicals::handleCROp(CRLogicalOpInfo &CRI) { 556 // We can definitely split a block on the inputs to a binary CR operation 557 // whose defs and (single) use are within the same block. 558 bool Changed = false; 559 if (CRI.IsBinary && CRI.ContainedInBlock && CRI.SingleUse && CRI.FeedsBR && 560 CRI.DefsSingleUse) { 561 Changed = splitBlockOnBinaryCROp(CRI); 562 if (Changed) 563 NumBlocksSplitOnBinaryCROp++; 564 } 565 return Changed; 566 } 567 568 /// Splits a block that contains a CR-logical operation that feeds a branch 569 /// and whose operands are produced within the block. 570 /// Example: 571 /// %vr5<def> = CMPDI %vr2, 0; CRRC:%vr5 G8RC:%vr2 572 /// %vr6<def> = COPY %vr5:sub_eq; CRBITRC:%vr6 CRRC:%vr5 573 /// %vr7<def> = CMPDI %vr3, 0; CRRC:%vr7 G8RC:%vr3 574 /// %vr8<def> = COPY %vr7:sub_eq; CRBITRC:%vr8 CRRC:%vr7 575 /// %vr9<def> = CROR %vr6<kill>, %vr8<kill>; CRBITRC:%vr9,%vr6,%vr8 576 /// BC %vr9<kill>, <BB#2>; CRBITRC:%vr9 577 /// Becomes: 578 /// %vr5<def> = CMPDI %vr2, 0; CRRC:%vr5 G8RC:%vr2 579 /// %vr6<def> = COPY %vr5:sub_eq; CRBITRC:%vr6 CRRC:%vr5 580 /// BC %vr6<kill>, <BB#2>; CRBITRC:%vr6 581 /// 582 /// %vr7<def> = CMPDI %vr3, 0; CRRC:%vr7 G8RC:%vr3 583 /// %vr8<def> = COPY %vr7:sub_eq; CRBITRC:%vr8 CRRC:%vr7 584 /// BC %vr9<kill>, <BB#2>; CRBITRC:%vr9 585 bool PPCReduceCRLogicals::splitBlockOnBinaryCROp(CRLogicalOpInfo &CRI) { 586 if (CRI.CopyDefs.first == CRI.CopyDefs.second) { 587 DEBUG(dbgs() << "Unable to split as the two operands are the same\n"); 588 NumNotSplitIdenticalOperands++; 589 return false; 590 } 591 if (CRI.TrueDefs.first->isCopy() || CRI.TrueDefs.second->isCopy() || 592 CRI.TrueDefs.first->isPHI() || CRI.TrueDefs.second->isPHI()) { 593 DEBUG(dbgs() << "Unable to split because one of the operands is a PHI or " 594 "chain of copies.\n"); 595 NumNotSplitChainCopies++; 596 return false; 597 } 598 // Note: keep in sync with computeBranchTargetAndInversion(). 599 if (CRI.MI->getOpcode() != PPC::CROR && 600 CRI.MI->getOpcode() != PPC::CRAND && 601 CRI.MI->getOpcode() != PPC::CRNOR && 602 CRI.MI->getOpcode() != PPC::CRNAND && 603 CRI.MI->getOpcode() != PPC::CRORC && 604 CRI.MI->getOpcode() != PPC::CRANDC) { 605 DEBUG(dbgs() << "Unable to split blocks on this opcode.\n"); 606 NumNotSplitWrongOpcode++; 607 return false; 608 } 609 DEBUG(dbgs() << "Splitting the following CR op:\n"; CRI.dump()); 610 MachineBasicBlock::iterator Def1It = CRI.TrueDefs.first; 611 MachineBasicBlock::iterator Def2It = CRI.TrueDefs.second; 612 613 bool UsingDef1 = false; 614 MachineInstr *SplitBefore = &*Def2It; 615 for (auto E = CRI.MI->getParent()->end(); Def2It != E; ++Def2It) { 616 if (Def1It == Def2It) { // Def2 comes before Def1. 617 SplitBefore = &*Def1It; 618 UsingDef1 = true; 619 break; 620 } 621 } 622 623 DEBUG(dbgs() << "We will split the following block:\n";); 624 DEBUG(CRI.MI->getParent()->dump()); 625 DEBUG(dbgs() << "Before instruction:\n"; SplitBefore->dump()); 626 627 // Get the branch instruction. 628 MachineInstr *Branch = 629 MRI->use_nodbg_begin(CRI.MI->getOperand(0).getReg())->getParent(); 630 631 // We want the new block to have no code in it other than the definition 632 // of the input to the CR logical and the CR logical itself. So we move 633 // those to the bottom of the block (just before the branch). Then we 634 // will split before the CR logical. 635 MachineBasicBlock *MBB = SplitBefore->getParent(); 636 auto FirstTerminator = MBB->getFirstTerminator(); 637 MachineBasicBlock::iterator FirstInstrToMove = 638 UsingDef1 ? CRI.TrueDefs.first : CRI.TrueDefs.second; 639 MachineBasicBlock::iterator SecondInstrToMove = 640 UsingDef1 ? CRI.CopyDefs.first : CRI.CopyDefs.second; 641 642 // The instructions that need to be moved are not guaranteed to be 643 // contiguous. Move them individually. 644 // FIXME: If one of the operands is a chain of (single use) copies, they 645 // can all be moved and we can still split. 646 MBB->splice(FirstTerminator, MBB, FirstInstrToMove); 647 if (FirstInstrToMove != SecondInstrToMove) 648 MBB->splice(FirstTerminator, MBB, SecondInstrToMove); 649 MBB->splice(FirstTerminator, MBB, CRI.MI); 650 651 unsigned Opc = CRI.MI->getOpcode(); 652 bool InvertOrigBranch, InvertNewBranch, TargetIsFallThrough; 653 computeBranchTargetAndInversion(Opc, Branch->getOpcode(), UsingDef1, 654 InvertNewBranch, InvertOrigBranch, 655 TargetIsFallThrough); 656 MachineInstr *SplitCond = 657 UsingDef1 ? CRI.CopyDefs.second : CRI.CopyDefs.first; 658 DEBUG(dbgs() << "We will " << (InvertNewBranch ? "invert" : "copy")); 659 DEBUG(dbgs() << " the original branch and the target is the " << 660 (TargetIsFallThrough ? "fallthrough block\n" : "orig. target block\n")); 661 DEBUG(dbgs() << "Original branch instruction: "; Branch->dump()); 662 BlockSplitInfo BSI { Branch, SplitBefore, SplitCond, InvertNewBranch, 663 InvertOrigBranch, TargetIsFallThrough, MBPI, CRI.MI, 664 UsingDef1 ? CRI.CopyDefs.first : CRI.CopyDefs.second }; 665 bool Changed = splitMBB(BSI); 666 // If we've split on a CR logical that is fed by a CR logical, 667 // recompute the source CR logical as it may be usable for splitting. 668 if (Changed) { 669 bool Input1CRlogical = 670 CRI.TrueDefs.first && isCRLogical(*CRI.TrueDefs.first); 671 bool Input2CRlogical = 672 CRI.TrueDefs.second && isCRLogical(*CRI.TrueDefs.second); 673 if (Input1CRlogical) 674 AllCRLogicalOps.push_back(createCRLogicalOpInfo(*CRI.TrueDefs.first)); 675 if (Input2CRlogical) 676 AllCRLogicalOps.push_back(createCRLogicalOpInfo(*CRI.TrueDefs.second)); 677 } 678 return Changed; 679 } 680 681 void PPCReduceCRLogicals::collectCRLogicals() { 682 for (MachineBasicBlock &MBB : *MF) { 683 for (MachineInstr &MI : MBB) { 684 if (isCRLogical(MI)) { 685 AllCRLogicalOps.push_back(createCRLogicalOpInfo(MI)); 686 TotalCRLogicals++; 687 if (AllCRLogicalOps.back().IsNullary) 688 TotalNullaryCRLogicals++; 689 else if (AllCRLogicalOps.back().IsBinary) 690 TotalBinaryCRLogicals++; 691 else 692 TotalUnaryCRLogicals++; 693 } 694 } 695 } 696 } 697 698 } // end annonymous namespace 699 700 INITIALIZE_PASS_BEGIN(PPCReduceCRLogicals, DEBUG_TYPE, 701 "PowerPC Reduce CR logical Operation", false, false) 702 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 703 INITIALIZE_PASS_END(PPCReduceCRLogicals, DEBUG_TYPE, 704 "PowerPC Reduce CR logical Operation", false, false) 705 706 char PPCReduceCRLogicals::ID = 0; 707 FunctionPass* 708 llvm::createPPCReduceCRLogicalsPass() { return new PPCReduceCRLogicals(); } 709