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