1 //===-- SystemZElimCompare.cpp - Eliminate comparison instructions --------===// 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 pass: 10 // (1) tries to remove compares if CC already contains the required information 11 // (2) fuses compares and branches into COMPARE AND BRANCH instructions 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "SystemZ.h" 16 #include "SystemZInstrInfo.h" 17 #include "SystemZTargetMachine.h" 18 #include "llvm/ADT/SmallVector.h" 19 #include "llvm/ADT/Statistic.h" 20 #include "llvm/ADT/StringRef.h" 21 #include "llvm/CodeGen/MachineBasicBlock.h" 22 #include "llvm/CodeGen/MachineFunction.h" 23 #include "llvm/CodeGen/MachineFunctionPass.h" 24 #include "llvm/CodeGen/MachineInstr.h" 25 #include "llvm/CodeGen/MachineInstrBuilder.h" 26 #include "llvm/CodeGen/MachineOperand.h" 27 #include "llvm/CodeGen/TargetRegisterInfo.h" 28 #include "llvm/CodeGen/TargetSubtargetInfo.h" 29 #include "llvm/MC/MCInstrDesc.h" 30 #include <cassert> 31 #include <cstdint> 32 33 using namespace llvm; 34 35 #define DEBUG_TYPE "systemz-elim-compare" 36 37 STATISTIC(BranchOnCounts, "Number of branch-on-count instructions"); 38 STATISTIC(LoadAndTraps, "Number of load-and-trap instructions"); 39 STATISTIC(EliminatedComparisons, "Number of eliminated comparisons"); 40 STATISTIC(FusedComparisons, "Number of fused compare-and-branch instructions"); 41 42 namespace { 43 44 // Represents the references to a particular register in one or more 45 // instructions. 46 struct Reference { 47 Reference() = default; 48 49 Reference &operator|=(const Reference &Other) { 50 Def |= Other.Def; 51 Use |= Other.Use; 52 return *this; 53 } 54 55 explicit operator bool() const { return Def || Use; } 56 57 // True if the register is defined or used in some form, either directly or 58 // via a sub- or super-register. 59 bool Def = false; 60 bool Use = false; 61 }; 62 63 class SystemZElimCompare : public MachineFunctionPass { 64 public: 65 static char ID; 66 67 SystemZElimCompare(const SystemZTargetMachine &tm) 68 : MachineFunctionPass(ID) {} 69 70 StringRef getPassName() const override { 71 return "SystemZ Comparison Elimination"; 72 } 73 74 bool processBlock(MachineBasicBlock &MBB); 75 bool runOnMachineFunction(MachineFunction &F) override; 76 77 MachineFunctionProperties getRequiredProperties() const override { 78 return MachineFunctionProperties().set( 79 MachineFunctionProperties::Property::NoVRegs); 80 } 81 82 private: 83 Reference getRegReferences(MachineInstr &MI, unsigned Reg); 84 bool convertToBRCT(MachineInstr &MI, MachineInstr &Compare, 85 SmallVectorImpl<MachineInstr *> &CCUsers); 86 bool convertToLoadAndTrap(MachineInstr &MI, MachineInstr &Compare, 87 SmallVectorImpl<MachineInstr *> &CCUsers); 88 bool convertToLoadAndTest(MachineInstr &MI, MachineInstr &Compare, 89 SmallVectorImpl<MachineInstr *> &CCUsers); 90 bool adjustCCMasksForInstr(MachineInstr &MI, MachineInstr &Compare, 91 SmallVectorImpl<MachineInstr *> &CCUsers, 92 unsigned ConvOpc = 0); 93 bool optimizeCompareZero(MachineInstr &Compare, 94 SmallVectorImpl<MachineInstr *> &CCUsers); 95 bool fuseCompareOperations(MachineInstr &Compare, 96 SmallVectorImpl<MachineInstr *> &CCUsers); 97 98 const SystemZInstrInfo *TII = nullptr; 99 const TargetRegisterInfo *TRI = nullptr; 100 }; 101 102 char SystemZElimCompare::ID = 0; 103 104 } // end anonymous namespace 105 106 // Return true if CC is live out of MBB. 107 static bool isCCLiveOut(MachineBasicBlock &MBB) { 108 for (auto SI = MBB.succ_begin(), SE = MBB.succ_end(); SI != SE; ++SI) 109 if ((*SI)->isLiveIn(SystemZ::CC)) 110 return true; 111 return false; 112 } 113 114 // Returns true if MI is an instruction whose output equals the value in Reg. 115 static bool preservesValueOf(MachineInstr &MI, unsigned Reg) { 116 switch (MI.getOpcode()) { 117 case SystemZ::LR: 118 case SystemZ::LGR: 119 case SystemZ::LGFR: 120 case SystemZ::LTR: 121 case SystemZ::LTGR: 122 case SystemZ::LTGFR: 123 case SystemZ::LER: 124 case SystemZ::LDR: 125 case SystemZ::LXR: 126 case SystemZ::LTEBR: 127 case SystemZ::LTDBR: 128 case SystemZ::LTXBR: 129 if (MI.getOperand(1).getReg() == Reg) 130 return true; 131 } 132 133 return false; 134 } 135 136 // Return true if any CC result of MI would (perhaps after conversion) 137 // reflect the value of Reg. 138 static bool resultTests(MachineInstr &MI, unsigned Reg) { 139 if (MI.getNumOperands() > 0 && MI.getOperand(0).isReg() && 140 MI.getOperand(0).isDef() && MI.getOperand(0).getReg() == Reg) 141 return true; 142 143 return (preservesValueOf(MI, Reg)); 144 } 145 146 // Describe the references to Reg or any of its aliases in MI. 147 Reference SystemZElimCompare::getRegReferences(MachineInstr &MI, unsigned Reg) { 148 Reference Ref; 149 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) { 150 const MachineOperand &MO = MI.getOperand(I); 151 if (MO.isReg()) { 152 if (unsigned MOReg = MO.getReg()) { 153 if (TRI->regsOverlap(MOReg, Reg)) { 154 if (MO.isUse()) 155 Ref.Use = true; 156 else if (MO.isDef()) 157 Ref.Def = true; 158 } 159 } 160 } 161 } 162 return Ref; 163 } 164 165 // Return true if this is a load and test which can be optimized the 166 // same way as compare instruction. 167 static bool isLoadAndTestAsCmp(MachineInstr &MI) { 168 // If we during isel used a load-and-test as a compare with 0, the 169 // def operand is dead. 170 return (MI.getOpcode() == SystemZ::LTEBR || 171 MI.getOpcode() == SystemZ::LTDBR || 172 MI.getOpcode() == SystemZ::LTXBR) && 173 MI.getOperand(0).isDead(); 174 } 175 176 // Return the source register of Compare, which is the unknown value 177 // being tested. 178 static unsigned getCompareSourceReg(MachineInstr &Compare) { 179 unsigned reg = 0; 180 if (Compare.isCompare()) 181 reg = Compare.getOperand(0).getReg(); 182 else if (isLoadAndTestAsCmp(Compare)) 183 reg = Compare.getOperand(1).getReg(); 184 assert(reg); 185 186 return reg; 187 } 188 189 // Compare compares the result of MI against zero. If MI is an addition 190 // of -1 and if CCUsers is a single branch on nonzero, eliminate the addition 191 // and convert the branch to a BRCT(G) or BRCTH. Return true on success. 192 bool SystemZElimCompare::convertToBRCT( 193 MachineInstr &MI, MachineInstr &Compare, 194 SmallVectorImpl<MachineInstr *> &CCUsers) { 195 // Check whether we have an addition of -1. 196 unsigned Opcode = MI.getOpcode(); 197 unsigned BRCT; 198 if (Opcode == SystemZ::AHI) 199 BRCT = SystemZ::BRCT; 200 else if (Opcode == SystemZ::AGHI) 201 BRCT = SystemZ::BRCTG; 202 else if (Opcode == SystemZ::AIH) 203 BRCT = SystemZ::BRCTH; 204 else 205 return false; 206 if (MI.getOperand(2).getImm() != -1) 207 return false; 208 209 // Check whether we have a single JLH. 210 if (CCUsers.size() != 1) 211 return false; 212 MachineInstr *Branch = CCUsers[0]; 213 if (Branch->getOpcode() != SystemZ::BRC || 214 Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP || 215 Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_NE) 216 return false; 217 218 // We already know that there are no references to the register between 219 // MI and Compare. Make sure that there are also no references between 220 // Compare and Branch. 221 unsigned SrcReg = getCompareSourceReg(Compare); 222 MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch; 223 for (++MBBI; MBBI != MBBE; ++MBBI) 224 if (getRegReferences(*MBBI, SrcReg)) 225 return false; 226 227 // The transformation is OK. Rebuild Branch as a BRCT(G) or BRCTH. 228 MachineOperand Target(Branch->getOperand(2)); 229 while (Branch->getNumOperands()) 230 Branch->RemoveOperand(0); 231 Branch->setDesc(TII->get(BRCT)); 232 MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch); 233 MIB.add(MI.getOperand(0)).add(MI.getOperand(1)).add(Target); 234 // Add a CC def to BRCT(G), since we may have to split them again if the 235 // branch displacement overflows. BRCTH has a 32-bit displacement, so 236 // this is not necessary there. 237 if (BRCT != SystemZ::BRCTH) 238 MIB.addReg(SystemZ::CC, RegState::ImplicitDefine | RegState::Dead); 239 MI.eraseFromParent(); 240 return true; 241 } 242 243 // Compare compares the result of MI against zero. If MI is a suitable load 244 // instruction and if CCUsers is a single conditional trap on zero, eliminate 245 // the load and convert the branch to a load-and-trap. Return true on success. 246 bool SystemZElimCompare::convertToLoadAndTrap( 247 MachineInstr &MI, MachineInstr &Compare, 248 SmallVectorImpl<MachineInstr *> &CCUsers) { 249 unsigned LATOpcode = TII->getLoadAndTrap(MI.getOpcode()); 250 if (!LATOpcode) 251 return false; 252 253 // Check whether we have a single CondTrap that traps on zero. 254 if (CCUsers.size() != 1) 255 return false; 256 MachineInstr *Branch = CCUsers[0]; 257 if (Branch->getOpcode() != SystemZ::CondTrap || 258 Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP || 259 Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_EQ) 260 return false; 261 262 // We already know that there are no references to the register between 263 // MI and Compare. Make sure that there are also no references between 264 // Compare and Branch. 265 unsigned SrcReg = getCompareSourceReg(Compare); 266 MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch; 267 for (++MBBI; MBBI != MBBE; ++MBBI) 268 if (getRegReferences(*MBBI, SrcReg)) 269 return false; 270 271 // The transformation is OK. Rebuild Branch as a load-and-trap. 272 while (Branch->getNumOperands()) 273 Branch->RemoveOperand(0); 274 Branch->setDesc(TII->get(LATOpcode)); 275 MachineInstrBuilder(*Branch->getParent()->getParent(), Branch) 276 .add(MI.getOperand(0)) 277 .add(MI.getOperand(1)) 278 .add(MI.getOperand(2)) 279 .add(MI.getOperand(3)); 280 MI.eraseFromParent(); 281 return true; 282 } 283 284 // If MI is a load instruction, try to convert it into a LOAD AND TEST. 285 // Return true on success. 286 bool SystemZElimCompare::convertToLoadAndTest( 287 MachineInstr &MI, MachineInstr &Compare, 288 SmallVectorImpl<MachineInstr *> &CCUsers) { 289 290 // Try to adjust CC masks for the LOAD AND TEST opcode that could replace MI. 291 unsigned Opcode = TII->getLoadAndTest(MI.getOpcode()); 292 if (!Opcode || !adjustCCMasksForInstr(MI, Compare, CCUsers, Opcode)) 293 return false; 294 295 // Rebuild to get the CC operand in the right place. 296 auto MIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(Opcode)); 297 for (const auto &MO : MI.operands()) 298 MIB.add(MO); 299 MIB.setMemRefs(MI.memoperands()); 300 MI.eraseFromParent(); 301 302 return true; 303 } 304 305 // The CC users in CCUsers are testing the result of a comparison of some 306 // value X against zero and we know that any CC value produced by MI would 307 // also reflect the value of X. ConvOpc may be used to pass the transfomed 308 // opcode MI will have if this succeeds. Try to adjust CCUsers so that they 309 // test the result of MI directly, returning true on success. Leave 310 // everything unchanged on failure. 311 bool SystemZElimCompare::adjustCCMasksForInstr( 312 MachineInstr &MI, MachineInstr &Compare, 313 SmallVectorImpl<MachineInstr *> &CCUsers, 314 unsigned ConvOpc) { 315 int Opcode = (ConvOpc ? ConvOpc : MI.getOpcode()); 316 const MCInstrDesc &Desc = TII->get(Opcode); 317 unsigned MIFlags = Desc.TSFlags; 318 319 // See which compare-style condition codes are available. 320 unsigned ReusableCCMask = SystemZII::getCompareZeroCCMask(MIFlags); 321 322 // For unsigned comparisons with zero, only equality makes sense. 323 unsigned CompareFlags = Compare.getDesc().TSFlags; 324 if (CompareFlags & SystemZII::IsLogical) 325 ReusableCCMask &= SystemZ::CCMASK_CMP_EQ; 326 327 if (ReusableCCMask == 0) 328 return false; 329 330 unsigned CCValues = SystemZII::getCCValues(MIFlags); 331 assert((ReusableCCMask & ~CCValues) == 0 && "Invalid CCValues"); 332 333 bool MIEquivalentToCmp = 334 (ReusableCCMask == CCValues && 335 CCValues == SystemZII::getCCValues(CompareFlags)); 336 337 if (!MIEquivalentToCmp) { 338 // Now check whether these flags are enough for all users. 339 SmallVector<MachineOperand *, 4> AlterMasks; 340 for (unsigned int I = 0, E = CCUsers.size(); I != E; ++I) { 341 MachineInstr *MI = CCUsers[I]; 342 343 // Fail if this isn't a use of CC that we understand. 344 unsigned Flags = MI->getDesc().TSFlags; 345 unsigned FirstOpNum; 346 if (Flags & SystemZII::CCMaskFirst) 347 FirstOpNum = 0; 348 else if (Flags & SystemZII::CCMaskLast) 349 FirstOpNum = MI->getNumExplicitOperands() - 2; 350 else 351 return false; 352 353 // Check whether the instruction predicate treats all CC values 354 // outside of ReusableCCMask in the same way. In that case it 355 // doesn't matter what those CC values mean. 356 unsigned CCValid = MI->getOperand(FirstOpNum).getImm(); 357 unsigned CCMask = MI->getOperand(FirstOpNum + 1).getImm(); 358 unsigned OutValid = ~ReusableCCMask & CCValid; 359 unsigned OutMask = ~ReusableCCMask & CCMask; 360 if (OutMask != 0 && OutMask != OutValid) 361 return false; 362 363 AlterMasks.push_back(&MI->getOperand(FirstOpNum)); 364 AlterMasks.push_back(&MI->getOperand(FirstOpNum + 1)); 365 } 366 367 // All users are OK. Adjust the masks for MI. 368 for (unsigned I = 0, E = AlterMasks.size(); I != E; I += 2) { 369 AlterMasks[I]->setImm(CCValues); 370 unsigned CCMask = AlterMasks[I + 1]->getImm(); 371 if (CCMask & ~ReusableCCMask) 372 AlterMasks[I + 1]->setImm((CCMask & ReusableCCMask) | 373 (CCValues & ~ReusableCCMask)); 374 } 375 } 376 377 // CC is now live after MI. 378 if (!ConvOpc) { 379 int CCDef = MI.findRegisterDefOperandIdx(SystemZ::CC, false, true, TRI); 380 assert(CCDef >= 0 && "Couldn't find CC set"); 381 MI.getOperand(CCDef).setIsDead(false); 382 } 383 384 // Check if MI lies before Compare. 385 bool BeforeCmp = false; 386 MachineBasicBlock::iterator MBBI = MI, MBBE = MI.getParent()->end(); 387 for (++MBBI; MBBI != MBBE; ++MBBI) 388 if (MBBI == Compare) { 389 BeforeCmp = true; 390 break; 391 } 392 393 // Clear any intervening kills of CC. 394 if (BeforeCmp) { 395 MachineBasicBlock::iterator MBBI = MI, MBBE = Compare; 396 for (++MBBI; MBBI != MBBE; ++MBBI) 397 MBBI->clearRegisterKills(SystemZ::CC, TRI); 398 } 399 400 return true; 401 } 402 403 // Return true if Compare is a comparison against zero. 404 static bool isCompareZero(MachineInstr &Compare) { 405 switch (Compare.getOpcode()) { 406 case SystemZ::LTEBRCompare: 407 case SystemZ::LTDBRCompare: 408 case SystemZ::LTXBRCompare: 409 return true; 410 411 default: 412 if (isLoadAndTestAsCmp(Compare)) 413 return true; 414 return Compare.getNumExplicitOperands() == 2 && 415 Compare.getOperand(1).isImm() && Compare.getOperand(1).getImm() == 0; 416 } 417 } 418 419 // Try to optimize cases where comparison instruction Compare is testing 420 // a value against zero. Return true on success and if Compare should be 421 // deleted as dead. CCUsers is the list of instructions that use the CC 422 // value produced by Compare. 423 bool SystemZElimCompare::optimizeCompareZero( 424 MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) { 425 if (!isCompareZero(Compare)) 426 return false; 427 428 // Search back for CC results that are based on the first operand. 429 unsigned SrcReg = getCompareSourceReg(Compare); 430 MachineBasicBlock &MBB = *Compare.getParent(); 431 Reference CCRefs; 432 Reference SrcRefs; 433 for (MachineBasicBlock::reverse_iterator MBBI = 434 std::next(MachineBasicBlock::reverse_iterator(&Compare)), 435 MBBE = MBB.rend(); MBBI != MBBE;) { 436 MachineInstr &MI = *MBBI++; 437 if (resultTests(MI, SrcReg)) { 438 // Try to remove both MI and Compare by converting a branch to BRCT(G). 439 // or a load-and-trap instruction. We don't care in this case whether 440 // CC is modified between MI and Compare. 441 if (!CCRefs.Use && !SrcRefs) { 442 if (convertToBRCT(MI, Compare, CCUsers)) { 443 BranchOnCounts += 1; 444 return true; 445 } 446 if (convertToLoadAndTrap(MI, Compare, CCUsers)) { 447 LoadAndTraps += 1; 448 return true; 449 } 450 } 451 // Try to eliminate Compare by reusing a CC result from MI. 452 if ((!CCRefs && convertToLoadAndTest(MI, Compare, CCUsers)) || 453 (!CCRefs.Def && adjustCCMasksForInstr(MI, Compare, CCUsers))) { 454 EliminatedComparisons += 1; 455 return true; 456 } 457 } 458 SrcRefs |= getRegReferences(MI, SrcReg); 459 if (SrcRefs.Def) 460 break; 461 CCRefs |= getRegReferences(MI, SystemZ::CC); 462 if (CCRefs.Use && CCRefs.Def) 463 break; 464 } 465 466 // Also do a forward search to handle cases where an instruction after the 467 // compare can be converted, like 468 // LTEBRCompare %f0s, %f0s; %f2s = LER %f0s => LTEBRCompare %f2s, %f0s 469 for (MachineBasicBlock::iterator MBBI = 470 std::next(MachineBasicBlock::iterator(&Compare)), MBBE = MBB.end(); 471 MBBI != MBBE;) { 472 MachineInstr &MI = *MBBI++; 473 if (preservesValueOf(MI, SrcReg)) { 474 // Try to eliminate Compare by reusing a CC result from MI. 475 if (convertToLoadAndTest(MI, Compare, CCUsers)) { 476 EliminatedComparisons += 1; 477 return true; 478 } 479 } 480 if (getRegReferences(MI, SrcReg).Def) 481 return false; 482 if (getRegReferences(MI, SystemZ::CC)) 483 return false; 484 } 485 486 return false; 487 } 488 489 // Try to fuse comparison instruction Compare into a later branch. 490 // Return true on success and if Compare is therefore redundant. 491 bool SystemZElimCompare::fuseCompareOperations( 492 MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) { 493 // See whether we have a single branch with which to fuse. 494 if (CCUsers.size() != 1) 495 return false; 496 MachineInstr *Branch = CCUsers[0]; 497 SystemZII::FusedCompareType Type; 498 switch (Branch->getOpcode()) { 499 case SystemZ::BRC: 500 Type = SystemZII::CompareAndBranch; 501 break; 502 case SystemZ::CondReturn: 503 Type = SystemZII::CompareAndReturn; 504 break; 505 case SystemZ::CallBCR: 506 Type = SystemZII::CompareAndSibcall; 507 break; 508 case SystemZ::CondTrap: 509 Type = SystemZII::CompareAndTrap; 510 break; 511 default: 512 return false; 513 } 514 515 // See whether we have a comparison that can be fused. 516 unsigned FusedOpcode = 517 TII->getFusedCompare(Compare.getOpcode(), Type, &Compare); 518 if (!FusedOpcode) 519 return false; 520 521 // Make sure that the operands are available at the branch. 522 // SrcReg2 is the register if the source operand is a register, 523 // 0 if the source operand is immediate, and the base register 524 // if the source operand is memory (index is not supported). 525 unsigned SrcReg = Compare.getOperand(0).getReg(); 526 unsigned SrcReg2 = 527 Compare.getOperand(1).isReg() ? Compare.getOperand(1).getReg() : 0; 528 MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch; 529 for (++MBBI; MBBI != MBBE; ++MBBI) 530 if (MBBI->modifiesRegister(SrcReg, TRI) || 531 (SrcReg2 && MBBI->modifiesRegister(SrcReg2, TRI))) 532 return false; 533 534 // Read the branch mask, target (if applicable), regmask (if applicable). 535 MachineOperand CCMask(MBBI->getOperand(1)); 536 assert((CCMask.getImm() & ~SystemZ::CCMASK_ICMP) == 0 && 537 "Invalid condition-code mask for integer comparison"); 538 // This is only valid for CompareAndBranch. 539 MachineOperand Target(MBBI->getOperand( 540 Type == SystemZII::CompareAndBranch ? 2 : 0)); 541 const uint32_t *RegMask; 542 if (Type == SystemZII::CompareAndSibcall) 543 RegMask = MBBI->getOperand(2).getRegMask(); 544 545 // Clear out all current operands. 546 int CCUse = MBBI->findRegisterUseOperandIdx(SystemZ::CC, false, TRI); 547 assert(CCUse >= 0 && "BRC/BCR must use CC"); 548 Branch->RemoveOperand(CCUse); 549 // Remove target (branch) or regmask (sibcall). 550 if (Type == SystemZII::CompareAndBranch || 551 Type == SystemZII::CompareAndSibcall) 552 Branch->RemoveOperand(2); 553 Branch->RemoveOperand(1); 554 Branch->RemoveOperand(0); 555 556 // Rebuild Branch as a fused compare and branch. 557 // SrcNOps is the number of MI operands of the compare instruction 558 // that we need to copy over. 559 unsigned SrcNOps = 2; 560 if (FusedOpcode == SystemZ::CLT || FusedOpcode == SystemZ::CLGT) 561 SrcNOps = 3; 562 Branch->setDesc(TII->get(FusedOpcode)); 563 MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch); 564 for (unsigned I = 0; I < SrcNOps; I++) 565 MIB.add(Compare.getOperand(I)); 566 MIB.add(CCMask); 567 568 if (Type == SystemZII::CompareAndBranch) { 569 // Only conditional branches define CC, as they may be converted back 570 // to a non-fused branch because of a long displacement. Conditional 571 // returns don't have that problem. 572 MIB.add(Target).addReg(SystemZ::CC, 573 RegState::ImplicitDefine | RegState::Dead); 574 } 575 576 if (Type == SystemZII::CompareAndSibcall) 577 MIB.addRegMask(RegMask); 578 579 // Clear any intervening kills of SrcReg and SrcReg2. 580 MBBI = Compare; 581 for (++MBBI; MBBI != MBBE; ++MBBI) { 582 MBBI->clearRegisterKills(SrcReg, TRI); 583 if (SrcReg2) 584 MBBI->clearRegisterKills(SrcReg2, TRI); 585 } 586 FusedComparisons += 1; 587 return true; 588 } 589 590 // Process all comparison instructions in MBB. Return true if something 591 // changed. 592 bool SystemZElimCompare::processBlock(MachineBasicBlock &MBB) { 593 bool Changed = false; 594 595 // Walk backwards through the block looking for comparisons, recording 596 // all CC users as we go. The subroutines can delete Compare and 597 // instructions before it. 598 bool CompleteCCUsers = !isCCLiveOut(MBB); 599 SmallVector<MachineInstr *, 4> CCUsers; 600 MachineBasicBlock::iterator MBBI = MBB.end(); 601 while (MBBI != MBB.begin()) { 602 MachineInstr &MI = *--MBBI; 603 if (CompleteCCUsers && (MI.isCompare() || isLoadAndTestAsCmp(MI)) && 604 (optimizeCompareZero(MI, CCUsers) || 605 fuseCompareOperations(MI, CCUsers))) { 606 ++MBBI; 607 MI.eraseFromParent(); 608 Changed = true; 609 CCUsers.clear(); 610 continue; 611 } 612 613 if (MI.definesRegister(SystemZ::CC)) { 614 CCUsers.clear(); 615 CompleteCCUsers = true; 616 } 617 if (MI.readsRegister(SystemZ::CC) && CompleteCCUsers) 618 CCUsers.push_back(&MI); 619 } 620 return Changed; 621 } 622 623 bool SystemZElimCompare::runOnMachineFunction(MachineFunction &F) { 624 if (skipFunction(F.getFunction())) 625 return false; 626 627 TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo()); 628 TRI = &TII->getRegisterInfo(); 629 630 bool Changed = false; 631 for (auto &MBB : F) 632 Changed |= processBlock(MBB); 633 634 return Changed; 635 } 636 637 FunctionPass *llvm::createSystemZElimComparePass(SystemZTargetMachine &TM) { 638 return new SystemZElimCompare(TM); 639 } 640