1 //===-- SystemZInstrInfo.cpp - SystemZ instruction information ------------===// 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 file contains the SystemZ implementation of the TargetInstrInfo class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "SystemZInstrInfo.h" 15 #include "SystemZInstrBuilder.h" 16 #include "SystemZTargetMachine.h" 17 #include "llvm/CodeGen/LiveVariables.h" 18 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 19 #include "llvm/CodeGen/MachineRegisterInfo.h" 20 21 using namespace llvm; 22 23 #define GET_INSTRINFO_CTOR_DTOR 24 #define GET_INSTRMAP_INFO 25 #include "SystemZGenInstrInfo.inc" 26 27 // Return a mask with Count low bits set. 28 static uint64_t allOnes(unsigned int Count) { 29 return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1; 30 } 31 32 // Reg should be a 32-bit GPR. Return true if it is a high register rather 33 // than a low register. 34 static bool isHighReg(unsigned int Reg) { 35 if (SystemZ::GRH32BitRegClass.contains(Reg)) 36 return true; 37 assert(SystemZ::GR32BitRegClass.contains(Reg) && "Invalid GRX32"); 38 return false; 39 } 40 41 // Pin the vtable to this file. 42 void SystemZInstrInfo::anchor() {} 43 44 SystemZInstrInfo::SystemZInstrInfo(SystemZSubtarget &sti) 45 : SystemZGenInstrInfo(SystemZ::ADJCALLSTACKDOWN, SystemZ::ADJCALLSTACKUP), 46 RI(), STI(sti) { 47 } 48 49 // MI is a 128-bit load or store. Split it into two 64-bit loads or stores, 50 // each having the opcode given by NewOpcode. 51 void SystemZInstrInfo::splitMove(MachineBasicBlock::iterator MI, 52 unsigned NewOpcode) const { 53 MachineBasicBlock *MBB = MI->getParent(); 54 MachineFunction &MF = *MBB->getParent(); 55 56 // Get two load or store instructions. Use the original instruction for one 57 // of them (arbitrarily the second here) and create a clone for the other. 58 MachineInstr *EarlierMI = MF.CloneMachineInstr(&*MI); 59 MBB->insert(MI, EarlierMI); 60 61 // Set up the two 64-bit registers. 62 MachineOperand &HighRegOp = EarlierMI->getOperand(0); 63 MachineOperand &LowRegOp = MI->getOperand(0); 64 HighRegOp.setReg(RI.getSubReg(HighRegOp.getReg(), SystemZ::subreg_h64)); 65 LowRegOp.setReg(RI.getSubReg(LowRegOp.getReg(), SystemZ::subreg_l64)); 66 67 // The address in the first (high) instruction is already correct. 68 // Adjust the offset in the second (low) instruction. 69 MachineOperand &HighOffsetOp = EarlierMI->getOperand(2); 70 MachineOperand &LowOffsetOp = MI->getOperand(2); 71 LowOffsetOp.setImm(LowOffsetOp.getImm() + 8); 72 73 // Clear the kill flags for the base and index registers in the first 74 // instruction. 75 EarlierMI->getOperand(1).setIsKill(false); 76 EarlierMI->getOperand(3).setIsKill(false); 77 78 // Set the opcodes. 79 unsigned HighOpcode = getOpcodeForOffset(NewOpcode, HighOffsetOp.getImm()); 80 unsigned LowOpcode = getOpcodeForOffset(NewOpcode, LowOffsetOp.getImm()); 81 assert(HighOpcode && LowOpcode && "Both offsets should be in range"); 82 83 EarlierMI->setDesc(get(HighOpcode)); 84 MI->setDesc(get(LowOpcode)); 85 } 86 87 // Split ADJDYNALLOC instruction MI. 88 void SystemZInstrInfo::splitAdjDynAlloc(MachineBasicBlock::iterator MI) const { 89 MachineBasicBlock *MBB = MI->getParent(); 90 MachineFunction &MF = *MBB->getParent(); 91 MachineFrameInfo &MFFrame = MF.getFrameInfo(); 92 MachineOperand &OffsetMO = MI->getOperand(2); 93 94 uint64_t Offset = (MFFrame.getMaxCallFrameSize() + 95 SystemZMC::CallFrameSize + 96 OffsetMO.getImm()); 97 unsigned NewOpcode = getOpcodeForOffset(SystemZ::LA, Offset); 98 assert(NewOpcode && "No support for huge argument lists yet"); 99 MI->setDesc(get(NewOpcode)); 100 OffsetMO.setImm(Offset); 101 } 102 103 // MI is an RI-style pseudo instruction. Replace it with LowOpcode 104 // if the first operand is a low GR32 and HighOpcode if the first operand 105 // is a high GR32. ConvertHigh is true if LowOpcode takes a signed operand 106 // and HighOpcode takes an unsigned 32-bit operand. In those cases, 107 // MI has the same kind of operand as LowOpcode, so needs to be converted 108 // if HighOpcode is used. 109 void SystemZInstrInfo::expandRIPseudo(MachineInstr &MI, unsigned LowOpcode, 110 unsigned HighOpcode, 111 bool ConvertHigh) const { 112 unsigned Reg = MI.getOperand(0).getReg(); 113 bool IsHigh = isHighReg(Reg); 114 MI.setDesc(get(IsHigh ? HighOpcode : LowOpcode)); 115 if (IsHigh && ConvertHigh) 116 MI.getOperand(1).setImm(uint32_t(MI.getOperand(1).getImm())); 117 } 118 119 // MI is a three-operand RIE-style pseudo instruction. Replace it with 120 // LowOpcodeK if the registers are both low GR32s, otherwise use a move 121 // followed by HighOpcode or LowOpcode, depending on whether the target 122 // is a high or low GR32. 123 void SystemZInstrInfo::expandRIEPseudo(MachineInstr &MI, unsigned LowOpcode, 124 unsigned LowOpcodeK, 125 unsigned HighOpcode) const { 126 unsigned DestReg = MI.getOperand(0).getReg(); 127 unsigned SrcReg = MI.getOperand(1).getReg(); 128 bool DestIsHigh = isHighReg(DestReg); 129 bool SrcIsHigh = isHighReg(SrcReg); 130 if (!DestIsHigh && !SrcIsHigh) 131 MI.setDesc(get(LowOpcodeK)); 132 else { 133 emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(), DestReg, SrcReg, 134 SystemZ::LR, 32, MI.getOperand(1).isKill()); 135 MI.setDesc(get(DestIsHigh ? HighOpcode : LowOpcode)); 136 MI.getOperand(1).setReg(DestReg); 137 MI.tieOperands(0, 1); 138 } 139 } 140 141 // MI is an RXY-style pseudo instruction. Replace it with LowOpcode 142 // if the first operand is a low GR32 and HighOpcode if the first operand 143 // is a high GR32. 144 void SystemZInstrInfo::expandRXYPseudo(MachineInstr &MI, unsigned LowOpcode, 145 unsigned HighOpcode) const { 146 unsigned Reg = MI.getOperand(0).getReg(); 147 unsigned Opcode = getOpcodeForOffset(isHighReg(Reg) ? HighOpcode : LowOpcode, 148 MI.getOperand(2).getImm()); 149 MI.setDesc(get(Opcode)); 150 } 151 152 // MI is an RR-style pseudo instruction that zero-extends the low Size bits 153 // of one GRX32 into another. Replace it with LowOpcode if both operands 154 // are low registers, otherwise use RISB[LH]G. 155 void SystemZInstrInfo::expandZExtPseudo(MachineInstr &MI, unsigned LowOpcode, 156 unsigned Size) const { 157 emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(), 158 MI.getOperand(0).getReg(), MI.getOperand(1).getReg(), LowOpcode, 159 Size, MI.getOperand(1).isKill()); 160 MI.eraseFromParent(); 161 } 162 163 void SystemZInstrInfo::expandLoadStackGuard(MachineInstr *MI) const { 164 MachineBasicBlock *MBB = MI->getParent(); 165 MachineFunction &MF = *MBB->getParent(); 166 const unsigned Reg = MI->getOperand(0).getReg(); 167 168 // Conveniently, all 4 instructions are cloned from LOAD_STACK_GUARD, 169 // so they already have operand 0 set to reg. 170 171 // ear <reg>, %a0 172 MachineInstr *Ear1MI = MF.CloneMachineInstr(MI); 173 MBB->insert(MI, Ear1MI); 174 Ear1MI->setDesc(get(SystemZ::EAR)); 175 MachineInstrBuilder(MF, Ear1MI).addImm(0); 176 177 // sllg <reg>, <reg>, 32 178 MachineInstr *SllgMI = MF.CloneMachineInstr(MI); 179 MBB->insert(MI, SllgMI); 180 SllgMI->setDesc(get(SystemZ::SLLG)); 181 MachineInstrBuilder(MF, SllgMI).addReg(Reg).addReg(0).addImm(32); 182 183 // ear <reg>, %a1 184 MachineInstr *Ear2MI = MF.CloneMachineInstr(MI); 185 MBB->insert(MI, Ear2MI); 186 Ear2MI->setDesc(get(SystemZ::EAR)); 187 MachineInstrBuilder(MF, Ear2MI).addImm(1); 188 189 // lg <reg>, 40(<reg>) 190 MI->setDesc(get(SystemZ::LG)); 191 MachineInstrBuilder(MF, MI).addReg(Reg).addImm(40).addReg(0); 192 } 193 194 // Emit a zero-extending move from 32-bit GPR SrcReg to 32-bit GPR 195 // DestReg before MBBI in MBB. Use LowLowOpcode when both DestReg and SrcReg 196 // are low registers, otherwise use RISB[LH]G. Size is the number of bits 197 // taken from the low end of SrcReg (8 for LLCR, 16 for LLHR and 32 for LR). 198 // KillSrc is true if this move is the last use of SrcReg. 199 void SystemZInstrInfo::emitGRX32Move(MachineBasicBlock &MBB, 200 MachineBasicBlock::iterator MBBI, 201 const DebugLoc &DL, unsigned DestReg, 202 unsigned SrcReg, unsigned LowLowOpcode, 203 unsigned Size, bool KillSrc) const { 204 unsigned Opcode; 205 bool DestIsHigh = isHighReg(DestReg); 206 bool SrcIsHigh = isHighReg(SrcReg); 207 if (DestIsHigh && SrcIsHigh) 208 Opcode = SystemZ::RISBHH; 209 else if (DestIsHigh && !SrcIsHigh) 210 Opcode = SystemZ::RISBHL; 211 else if (!DestIsHigh && SrcIsHigh) 212 Opcode = SystemZ::RISBLH; 213 else { 214 BuildMI(MBB, MBBI, DL, get(LowLowOpcode), DestReg) 215 .addReg(SrcReg, getKillRegState(KillSrc)); 216 return; 217 } 218 unsigned Rotate = (DestIsHigh != SrcIsHigh ? 32 : 0); 219 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg) 220 .addReg(DestReg, RegState::Undef) 221 .addReg(SrcReg, getKillRegState(KillSrc)) 222 .addImm(32 - Size).addImm(128 + 31).addImm(Rotate); 223 } 224 225 // If MI is a simple load or store for a frame object, return the register 226 // it loads or stores and set FrameIndex to the index of the frame object. 227 // Return 0 otherwise. 228 // 229 // Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores. 230 static int isSimpleMove(const MachineInstr &MI, int &FrameIndex, 231 unsigned Flag) { 232 const MCInstrDesc &MCID = MI.getDesc(); 233 if ((MCID.TSFlags & Flag) && MI.getOperand(1).isFI() && 234 MI.getOperand(2).getImm() == 0 && MI.getOperand(3).getReg() == 0) { 235 FrameIndex = MI.getOperand(1).getIndex(); 236 return MI.getOperand(0).getReg(); 237 } 238 return 0; 239 } 240 241 unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, 242 int &FrameIndex) const { 243 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad); 244 } 245 246 unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr &MI, 247 int &FrameIndex) const { 248 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore); 249 } 250 251 bool SystemZInstrInfo::isStackSlotCopy(const MachineInstr &MI, 252 int &DestFrameIndex, 253 int &SrcFrameIndex) const { 254 // Check for MVC 0(Length,FI1),0(FI2) 255 const MachineFrameInfo &MFI = MI.getParent()->getParent()->getFrameInfo(); 256 if (MI.getOpcode() != SystemZ::MVC || !MI.getOperand(0).isFI() || 257 MI.getOperand(1).getImm() != 0 || !MI.getOperand(3).isFI() || 258 MI.getOperand(4).getImm() != 0) 259 return false; 260 261 // Check that Length covers the full slots. 262 int64_t Length = MI.getOperand(2).getImm(); 263 unsigned FI1 = MI.getOperand(0).getIndex(); 264 unsigned FI2 = MI.getOperand(3).getIndex(); 265 if (MFI.getObjectSize(FI1) != Length || 266 MFI.getObjectSize(FI2) != Length) 267 return false; 268 269 DestFrameIndex = FI1; 270 SrcFrameIndex = FI2; 271 return true; 272 } 273 274 bool SystemZInstrInfo::analyzeBranch(MachineBasicBlock &MBB, 275 MachineBasicBlock *&TBB, 276 MachineBasicBlock *&FBB, 277 SmallVectorImpl<MachineOperand> &Cond, 278 bool AllowModify) const { 279 // Most of the code and comments here are boilerplate. 280 281 // Start from the bottom of the block and work up, examining the 282 // terminator instructions. 283 MachineBasicBlock::iterator I = MBB.end(); 284 while (I != MBB.begin()) { 285 --I; 286 if (I->isDebugValue()) 287 continue; 288 289 // Working from the bottom, when we see a non-terminator instruction, we're 290 // done. 291 if (!isUnpredicatedTerminator(*I)) 292 break; 293 294 // A terminator that isn't a branch can't easily be handled by this 295 // analysis. 296 if (!I->isBranch()) 297 return true; 298 299 // Can't handle indirect branches. 300 SystemZII::Branch Branch(getBranchInfo(*I)); 301 if (!Branch.Target->isMBB()) 302 return true; 303 304 // Punt on compound branches. 305 if (Branch.Type != SystemZII::BranchNormal) 306 return true; 307 308 if (Branch.CCMask == SystemZ::CCMASK_ANY) { 309 // Handle unconditional branches. 310 if (!AllowModify) { 311 TBB = Branch.Target->getMBB(); 312 continue; 313 } 314 315 // If the block has any instructions after a JMP, delete them. 316 while (std::next(I) != MBB.end()) 317 std::next(I)->eraseFromParent(); 318 319 Cond.clear(); 320 FBB = nullptr; 321 322 // Delete the JMP if it's equivalent to a fall-through. 323 if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) { 324 TBB = nullptr; 325 I->eraseFromParent(); 326 I = MBB.end(); 327 continue; 328 } 329 330 // TBB is used to indicate the unconditinal destination. 331 TBB = Branch.Target->getMBB(); 332 continue; 333 } 334 335 // Working from the bottom, handle the first conditional branch. 336 if (Cond.empty()) { 337 // FIXME: add X86-style branch swap 338 FBB = TBB; 339 TBB = Branch.Target->getMBB(); 340 Cond.push_back(MachineOperand::CreateImm(Branch.CCValid)); 341 Cond.push_back(MachineOperand::CreateImm(Branch.CCMask)); 342 continue; 343 } 344 345 // Handle subsequent conditional branches. 346 assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch"); 347 348 // Only handle the case where all conditional branches branch to the same 349 // destination. 350 if (TBB != Branch.Target->getMBB()) 351 return true; 352 353 // If the conditions are the same, we can leave them alone. 354 unsigned OldCCValid = Cond[0].getImm(); 355 unsigned OldCCMask = Cond[1].getImm(); 356 if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask) 357 continue; 358 359 // FIXME: Try combining conditions like X86 does. Should be easy on Z! 360 return false; 361 } 362 363 return false; 364 } 365 366 unsigned SystemZInstrInfo::removeBranch(MachineBasicBlock &MBB, 367 int *BytesRemoved) const { 368 assert(!BytesRemoved && "code size not handled"); 369 370 // Most of the code and comments here are boilerplate. 371 MachineBasicBlock::iterator I = MBB.end(); 372 unsigned Count = 0; 373 374 while (I != MBB.begin()) { 375 --I; 376 if (I->isDebugValue()) 377 continue; 378 if (!I->isBranch()) 379 break; 380 if (!getBranchInfo(*I).Target->isMBB()) 381 break; 382 // Remove the branch. 383 I->eraseFromParent(); 384 I = MBB.end(); 385 ++Count; 386 } 387 388 return Count; 389 } 390 391 bool SystemZInstrInfo:: 392 reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { 393 assert(Cond.size() == 2 && "Invalid condition"); 394 Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm()); 395 return false; 396 } 397 398 unsigned SystemZInstrInfo::insertBranch(MachineBasicBlock &MBB, 399 MachineBasicBlock *TBB, 400 MachineBasicBlock *FBB, 401 ArrayRef<MachineOperand> Cond, 402 const DebugLoc &DL, 403 int *BytesAdded) const { 404 // In this function we output 32-bit branches, which should always 405 // have enough range. They can be shortened and relaxed by later code 406 // in the pipeline, if desired. 407 408 // Shouldn't be a fall through. 409 assert(TBB && "insertBranch must not be told to insert a fallthrough"); 410 assert((Cond.size() == 2 || Cond.size() == 0) && 411 "SystemZ branch conditions have one component!"); 412 assert(!BytesAdded && "code size not handled"); 413 414 if (Cond.empty()) { 415 // Unconditional branch? 416 assert(!FBB && "Unconditional branch with multiple successors!"); 417 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB); 418 return 1; 419 } 420 421 // Conditional branch. 422 unsigned Count = 0; 423 unsigned CCValid = Cond[0].getImm(); 424 unsigned CCMask = Cond[1].getImm(); 425 BuildMI(&MBB, DL, get(SystemZ::BRC)) 426 .addImm(CCValid).addImm(CCMask).addMBB(TBB); 427 ++Count; 428 429 if (FBB) { 430 // Two-way Conditional branch. Insert the second branch. 431 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB); 432 ++Count; 433 } 434 return Count; 435 } 436 437 bool SystemZInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg, 438 unsigned &SrcReg2, int &Mask, 439 int &Value) const { 440 assert(MI.isCompare() && "Caller should have checked for a comparison"); 441 442 if (MI.getNumExplicitOperands() == 2 && MI.getOperand(0).isReg() && 443 MI.getOperand(1).isImm()) { 444 SrcReg = MI.getOperand(0).getReg(); 445 SrcReg2 = 0; 446 Value = MI.getOperand(1).getImm(); 447 Mask = ~0; 448 return true; 449 } 450 451 return false; 452 } 453 454 // If Reg is a virtual register, return its definition, otherwise return null. 455 static MachineInstr *getDef(unsigned Reg, 456 const MachineRegisterInfo *MRI) { 457 if (TargetRegisterInfo::isPhysicalRegister(Reg)) 458 return nullptr; 459 return MRI->getUniqueVRegDef(Reg); 460 } 461 462 // Return true if MI is a shift of type Opcode by Imm bits. 463 static bool isShift(MachineInstr *MI, unsigned Opcode, int64_t Imm) { 464 return (MI->getOpcode() == Opcode && 465 !MI->getOperand(2).getReg() && 466 MI->getOperand(3).getImm() == Imm); 467 } 468 469 // If the destination of MI has no uses, delete it as dead. 470 static void eraseIfDead(MachineInstr *MI, const MachineRegisterInfo *MRI) { 471 if (MRI->use_nodbg_empty(MI->getOperand(0).getReg())) 472 MI->eraseFromParent(); 473 } 474 475 // Compare compares SrcReg against zero. Check whether SrcReg contains 476 // the result of an IPM sequence whose input CC survives until Compare, 477 // and whether Compare is therefore redundant. Delete it and return 478 // true if so. 479 static bool removeIPMBasedCompare(MachineInstr &Compare, unsigned SrcReg, 480 const MachineRegisterInfo *MRI, 481 const TargetRegisterInfo *TRI) { 482 MachineInstr *LGFR = nullptr; 483 MachineInstr *RLL = getDef(SrcReg, MRI); 484 if (RLL && RLL->getOpcode() == SystemZ::LGFR) { 485 LGFR = RLL; 486 RLL = getDef(LGFR->getOperand(1).getReg(), MRI); 487 } 488 if (!RLL || !isShift(RLL, SystemZ::RLL, 31)) 489 return false; 490 491 MachineInstr *SRL = getDef(RLL->getOperand(1).getReg(), MRI); 492 if (!SRL || !isShift(SRL, SystemZ::SRL, SystemZ::IPM_CC)) 493 return false; 494 495 MachineInstr *IPM = getDef(SRL->getOperand(1).getReg(), MRI); 496 if (!IPM || IPM->getOpcode() != SystemZ::IPM) 497 return false; 498 499 // Check that there are no assignments to CC between the IPM and Compare, 500 if (IPM->getParent() != Compare.getParent()) 501 return false; 502 MachineBasicBlock::iterator MBBI = IPM, MBBE = Compare.getIterator(); 503 for (++MBBI; MBBI != MBBE; ++MBBI) { 504 MachineInstr &MI = *MBBI; 505 if (MI.modifiesRegister(SystemZ::CC, TRI)) 506 return false; 507 } 508 509 Compare.eraseFromParent(); 510 if (LGFR) 511 eraseIfDead(LGFR, MRI); 512 eraseIfDead(RLL, MRI); 513 eraseIfDead(SRL, MRI); 514 eraseIfDead(IPM, MRI); 515 516 return true; 517 } 518 519 bool SystemZInstrInfo::optimizeCompareInstr( 520 MachineInstr &Compare, unsigned SrcReg, unsigned SrcReg2, int Mask, 521 int Value, const MachineRegisterInfo *MRI) const { 522 assert(!SrcReg2 && "Only optimizing constant comparisons so far"); 523 bool IsLogical = (Compare.getDesc().TSFlags & SystemZII::IsLogical) != 0; 524 return Value == 0 && !IsLogical && 525 removeIPMBasedCompare(Compare, SrcReg, MRI, &RI); 526 } 527 528 // If Opcode is a move that has a conditional variant, return that variant, 529 // otherwise return 0. 530 static unsigned getConditionalMove(unsigned Opcode) { 531 switch (Opcode) { 532 case SystemZ::LR: return SystemZ::LOCR; 533 case SystemZ::LGR: return SystemZ::LOCGR; 534 default: return 0; 535 } 536 } 537 538 static unsigned getConditionalLoadImmediate(unsigned Opcode) { 539 switch (Opcode) { 540 case SystemZ::LHI: return SystemZ::LOCHI; 541 case SystemZ::LGHI: return SystemZ::LOCGHI; 542 default: return 0; 543 } 544 } 545 546 bool SystemZInstrInfo::isPredicable(MachineInstr &MI) const { 547 unsigned Opcode = MI.getOpcode(); 548 if (STI.hasLoadStoreOnCond() && getConditionalMove(Opcode)) 549 return true; 550 if (STI.hasLoadStoreOnCond2() && getConditionalLoadImmediate(Opcode)) 551 return true; 552 if (Opcode == SystemZ::Return || 553 Opcode == SystemZ::Trap || 554 Opcode == SystemZ::CallJG || 555 Opcode == SystemZ::CallBR) 556 return true; 557 return false; 558 } 559 560 bool SystemZInstrInfo:: 561 isProfitableToIfCvt(MachineBasicBlock &MBB, 562 unsigned NumCycles, unsigned ExtraPredCycles, 563 BranchProbability Probability) const { 564 // Avoid using conditional returns at the end of a loop (since then 565 // we'd need to emit an unconditional branch to the beginning anyway, 566 // making the loop body longer). This doesn't apply for low-probability 567 // loops (eg. compare-and-swap retry), so just decide based on branch 568 // probability instead of looping structure. 569 // However, since Compare and Trap instructions cost the same as a regular 570 // Compare instruction, we should allow the if conversion to convert this 571 // into a Conditional Compare regardless of the branch probability. 572 if (MBB.getLastNonDebugInstr()->getOpcode() != SystemZ::Trap && 573 MBB.succ_empty() && Probability < BranchProbability(1, 8)) 574 return false; 575 // For now only convert single instructions. 576 return NumCycles == 1; 577 } 578 579 bool SystemZInstrInfo:: 580 isProfitableToIfCvt(MachineBasicBlock &TMBB, 581 unsigned NumCyclesT, unsigned ExtraPredCyclesT, 582 MachineBasicBlock &FMBB, 583 unsigned NumCyclesF, unsigned ExtraPredCyclesF, 584 BranchProbability Probability) const { 585 // For now avoid converting mutually-exclusive cases. 586 return false; 587 } 588 589 bool SystemZInstrInfo:: 590 isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles, 591 BranchProbability Probability) const { 592 // For now only duplicate single instructions. 593 return NumCycles == 1; 594 } 595 596 bool SystemZInstrInfo::PredicateInstruction( 597 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const { 598 assert(Pred.size() == 2 && "Invalid condition"); 599 unsigned CCValid = Pred[0].getImm(); 600 unsigned CCMask = Pred[1].getImm(); 601 assert(CCMask > 0 && CCMask < 15 && "Invalid predicate"); 602 unsigned Opcode = MI.getOpcode(); 603 if (STI.hasLoadStoreOnCond()) { 604 if (unsigned CondOpcode = getConditionalMove(Opcode)) { 605 MI.setDesc(get(CondOpcode)); 606 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 607 .addImm(CCValid) 608 .addImm(CCMask) 609 .addReg(SystemZ::CC, RegState::Implicit); 610 return true; 611 } 612 } 613 if (STI.hasLoadStoreOnCond2()) { 614 if (unsigned CondOpcode = getConditionalLoadImmediate(Opcode)) { 615 MI.setDesc(get(CondOpcode)); 616 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 617 .addImm(CCValid) 618 .addImm(CCMask) 619 .addReg(SystemZ::CC, RegState::Implicit); 620 return true; 621 } 622 } 623 if (Opcode == SystemZ::Trap) { 624 MI.setDesc(get(SystemZ::CondTrap)); 625 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 626 .addImm(CCValid).addImm(CCMask) 627 .addReg(SystemZ::CC, RegState::Implicit); 628 return true; 629 } 630 if (Opcode == SystemZ::Return) { 631 MI.setDesc(get(SystemZ::CondReturn)); 632 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 633 .addImm(CCValid).addImm(CCMask) 634 .addReg(SystemZ::CC, RegState::Implicit); 635 return true; 636 } 637 if (Opcode == SystemZ::CallJG) { 638 MachineOperand FirstOp = MI.getOperand(0); 639 const uint32_t *RegMask = MI.getOperand(1).getRegMask(); 640 MI.RemoveOperand(1); 641 MI.RemoveOperand(0); 642 MI.setDesc(get(SystemZ::CallBRCL)); 643 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 644 .addImm(CCValid).addImm(CCMask) 645 .addOperand(FirstOp) 646 .addRegMask(RegMask) 647 .addReg(SystemZ::CC, RegState::Implicit); 648 return true; 649 } 650 if (Opcode == SystemZ::CallBR) { 651 const uint32_t *RegMask = MI.getOperand(0).getRegMask(); 652 MI.RemoveOperand(0); 653 MI.setDesc(get(SystemZ::CallBCR)); 654 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 655 .addImm(CCValid).addImm(CCMask) 656 .addRegMask(RegMask) 657 .addReg(SystemZ::CC, RegState::Implicit); 658 return true; 659 } 660 return false; 661 } 662 663 void SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 664 MachineBasicBlock::iterator MBBI, 665 const DebugLoc &DL, unsigned DestReg, 666 unsigned SrcReg, bool KillSrc) const { 667 // Split 128-bit GPR moves into two 64-bit moves. This handles ADDR128 too. 668 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) { 669 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64), 670 RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc); 671 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64), 672 RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc); 673 return; 674 } 675 676 if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) { 677 emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc); 678 return; 679 } 680 681 // Everything else needs only one instruction. 682 unsigned Opcode; 683 if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg)) 684 Opcode = SystemZ::LGR; 685 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg)) 686 // For z13 we prefer LDR over LER to avoid partial register dependencies. 687 Opcode = STI.hasVector() ? SystemZ::LDR32 : SystemZ::LER; 688 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg)) 689 Opcode = SystemZ::LDR; 690 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg)) 691 Opcode = SystemZ::LXR; 692 else if (SystemZ::VR32BitRegClass.contains(DestReg, SrcReg)) 693 Opcode = SystemZ::VLR32; 694 else if (SystemZ::VR64BitRegClass.contains(DestReg, SrcReg)) 695 Opcode = SystemZ::VLR64; 696 else if (SystemZ::VR128BitRegClass.contains(DestReg, SrcReg)) 697 Opcode = SystemZ::VLR; 698 else 699 llvm_unreachable("Impossible reg-to-reg copy"); 700 701 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg) 702 .addReg(SrcReg, getKillRegState(KillSrc)); 703 } 704 705 void SystemZInstrInfo::storeRegToStackSlot( 706 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned SrcReg, 707 bool isKill, int FrameIdx, const TargetRegisterClass *RC, 708 const TargetRegisterInfo *TRI) const { 709 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 710 711 // Callers may expect a single instruction, so keep 128-bit moves 712 // together for now and lower them after register allocation. 713 unsigned LoadOpcode, StoreOpcode; 714 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode); 715 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode)) 716 .addReg(SrcReg, getKillRegState(isKill)), 717 FrameIdx); 718 } 719 720 void SystemZInstrInfo::loadRegFromStackSlot( 721 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned DestReg, 722 int FrameIdx, const TargetRegisterClass *RC, 723 const TargetRegisterInfo *TRI) const { 724 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 725 726 // Callers may expect a single instruction, so keep 128-bit moves 727 // together for now and lower them after register allocation. 728 unsigned LoadOpcode, StoreOpcode; 729 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode); 730 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg), 731 FrameIdx); 732 } 733 734 // Return true if MI is a simple load or store with a 12-bit displacement 735 // and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores. 736 static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) { 737 const MCInstrDesc &MCID = MI->getDesc(); 738 return ((MCID.TSFlags & Flag) && 739 isUInt<12>(MI->getOperand(2).getImm()) && 740 MI->getOperand(3).getReg() == 0); 741 } 742 743 namespace { 744 struct LogicOp { 745 LogicOp() : RegSize(0), ImmLSB(0), ImmSize(0) {} 746 LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize) 747 : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {} 748 749 explicit operator bool() const { return RegSize; } 750 751 unsigned RegSize, ImmLSB, ImmSize; 752 }; 753 } // end anonymous namespace 754 755 static LogicOp interpretAndImmediate(unsigned Opcode) { 756 switch (Opcode) { 757 case SystemZ::NILMux: return LogicOp(32, 0, 16); 758 case SystemZ::NIHMux: return LogicOp(32, 16, 16); 759 case SystemZ::NILL64: return LogicOp(64, 0, 16); 760 case SystemZ::NILH64: return LogicOp(64, 16, 16); 761 case SystemZ::NIHL64: return LogicOp(64, 32, 16); 762 case SystemZ::NIHH64: return LogicOp(64, 48, 16); 763 case SystemZ::NIFMux: return LogicOp(32, 0, 32); 764 case SystemZ::NILF64: return LogicOp(64, 0, 32); 765 case SystemZ::NIHF64: return LogicOp(64, 32, 32); 766 default: return LogicOp(); 767 } 768 } 769 770 static void transferDeadCC(MachineInstr *OldMI, MachineInstr *NewMI) { 771 if (OldMI->registerDefIsDead(SystemZ::CC)) { 772 MachineOperand *CCDef = NewMI->findRegisterDefOperand(SystemZ::CC); 773 if (CCDef != nullptr) 774 CCDef->setIsDead(true); 775 } 776 } 777 778 // Used to return from convertToThreeAddress after replacing two-address 779 // instruction OldMI with three-address instruction NewMI. 780 static MachineInstr *finishConvertToThreeAddress(MachineInstr *OldMI, 781 MachineInstr *NewMI, 782 LiveVariables *LV) { 783 if (LV) { 784 unsigned NumOps = OldMI->getNumOperands(); 785 for (unsigned I = 1; I < NumOps; ++I) { 786 MachineOperand &Op = OldMI->getOperand(I); 787 if (Op.isReg() && Op.isKill()) 788 LV->replaceKillInstruction(Op.getReg(), *OldMI, *NewMI); 789 } 790 } 791 transferDeadCC(OldMI, NewMI); 792 return NewMI; 793 } 794 795 MachineInstr *SystemZInstrInfo::convertToThreeAddress( 796 MachineFunction::iterator &MFI, MachineInstr &MI, LiveVariables *LV) const { 797 MachineBasicBlock *MBB = MI.getParent(); 798 MachineFunction *MF = MBB->getParent(); 799 MachineRegisterInfo &MRI = MF->getRegInfo(); 800 801 unsigned Opcode = MI.getOpcode(); 802 unsigned NumOps = MI.getNumOperands(); 803 804 // Try to convert something like SLL into SLLK, if supported. 805 // We prefer to keep the two-operand form where possible both 806 // because it tends to be shorter and because some instructions 807 // have memory forms that can be used during spilling. 808 if (STI.hasDistinctOps()) { 809 MachineOperand &Dest = MI.getOperand(0); 810 MachineOperand &Src = MI.getOperand(1); 811 unsigned DestReg = Dest.getReg(); 812 unsigned SrcReg = Src.getReg(); 813 // AHIMux is only really a three-operand instruction when both operands 814 // are low registers. Try to constrain both operands to be low if 815 // possible. 816 if (Opcode == SystemZ::AHIMux && 817 TargetRegisterInfo::isVirtualRegister(DestReg) && 818 TargetRegisterInfo::isVirtualRegister(SrcReg) && 819 MRI.getRegClass(DestReg)->contains(SystemZ::R1L) && 820 MRI.getRegClass(SrcReg)->contains(SystemZ::R1L)) { 821 MRI.constrainRegClass(DestReg, &SystemZ::GR32BitRegClass); 822 MRI.constrainRegClass(SrcReg, &SystemZ::GR32BitRegClass); 823 } 824 int ThreeOperandOpcode = SystemZ::getThreeOperandOpcode(Opcode); 825 if (ThreeOperandOpcode >= 0) { 826 // Create three address instruction without adding the implicit 827 // operands. Those will instead be copied over from the original 828 // instruction by the loop below. 829 MachineInstrBuilder MIB( 830 *MF, MF->CreateMachineInstr(get(ThreeOperandOpcode), MI.getDebugLoc(), 831 /*NoImplicit=*/true)); 832 MIB.addOperand(Dest); 833 // Keep the kill state, but drop the tied flag. 834 MIB.addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg()); 835 // Keep the remaining operands as-is. 836 for (unsigned I = 2; I < NumOps; ++I) 837 MIB.addOperand(MI.getOperand(I)); 838 MBB->insert(MI, MIB); 839 return finishConvertToThreeAddress(&MI, MIB, LV); 840 } 841 } 842 843 // Try to convert an AND into an RISBG-type instruction. 844 if (LogicOp And = interpretAndImmediate(Opcode)) { 845 uint64_t Imm = MI.getOperand(2).getImm() << And.ImmLSB; 846 // AND IMMEDIATE leaves the other bits of the register unchanged. 847 Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB); 848 unsigned Start, End; 849 if (isRxSBGMask(Imm, And.RegSize, Start, End)) { 850 unsigned NewOpcode; 851 if (And.RegSize == 64) { 852 NewOpcode = SystemZ::RISBG; 853 // Prefer RISBGN if available, since it does not clobber CC. 854 if (STI.hasMiscellaneousExtensions()) 855 NewOpcode = SystemZ::RISBGN; 856 } else { 857 NewOpcode = SystemZ::RISBMux; 858 Start &= 31; 859 End &= 31; 860 } 861 MachineOperand &Dest = MI.getOperand(0); 862 MachineOperand &Src = MI.getOperand(1); 863 MachineInstrBuilder MIB = 864 BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpcode)) 865 .addOperand(Dest) 866 .addReg(0) 867 .addReg(Src.getReg(), getKillRegState(Src.isKill()), 868 Src.getSubReg()) 869 .addImm(Start) 870 .addImm(End + 128) 871 .addImm(0); 872 return finishConvertToThreeAddress(&MI, MIB, LV); 873 } 874 } 875 return nullptr; 876 } 877 878 MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl( 879 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops, 880 MachineBasicBlock::iterator InsertPt, int FrameIndex, 881 LiveIntervals *LIS) const { 882 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 883 const MachineFrameInfo &MFI = MF.getFrameInfo(); 884 unsigned Size = MFI.getObjectSize(FrameIndex); 885 unsigned Opcode = MI.getOpcode(); 886 887 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) { 888 if (LIS != nullptr && (Opcode == SystemZ::LA || Opcode == SystemZ::LAY) && 889 isInt<8>(MI.getOperand(2).getImm()) && !MI.getOperand(3).getReg()) { 890 891 // Check CC liveness, since new instruction introduces a dead 892 // def of CC. 893 MCRegUnitIterator CCUnit(SystemZ::CC, TRI); 894 LiveRange &CCLiveRange = LIS->getRegUnit(*CCUnit); 895 ++CCUnit; 896 assert (!CCUnit.isValid() && "CC only has one reg unit."); 897 SlotIndex MISlot = 898 LIS->getSlotIndexes()->getInstructionIndex(MI).getRegSlot(); 899 if (!CCLiveRange.liveAt(MISlot)) { 900 // LA(Y) %reg, CONST(%reg) -> AGSI %mem, CONST 901 MachineInstr *BuiltMI = BuildMI(*InsertPt->getParent(), InsertPt, 902 MI.getDebugLoc(), get(SystemZ::AGSI)) 903 .addFrameIndex(FrameIndex) 904 .addImm(0) 905 .addImm(MI.getOperand(2).getImm()); 906 BuiltMI->findRegisterDefOperand(SystemZ::CC)->setIsDead(true); 907 CCLiveRange.createDeadDef(MISlot, LIS->getVNInfoAllocator()); 908 return BuiltMI; 909 } 910 } 911 return nullptr; 912 } 913 914 // All other cases require a single operand. 915 if (Ops.size() != 1) 916 return nullptr; 917 918 unsigned OpNum = Ops[0]; 919 assert(Size == 920 MF.getRegInfo() 921 .getRegClass(MI.getOperand(OpNum).getReg()) 922 ->getSize() && 923 "Invalid size combination"); 924 925 if ((Opcode == SystemZ::AHI || Opcode == SystemZ::AGHI) && OpNum == 0 && 926 isInt<8>(MI.getOperand(2).getImm())) { 927 // A(G)HI %reg, CONST -> A(G)SI %mem, CONST 928 Opcode = (Opcode == SystemZ::AHI ? SystemZ::ASI : SystemZ::AGSI); 929 MachineInstr *BuiltMI = 930 BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode)) 931 .addFrameIndex(FrameIndex) 932 .addImm(0) 933 .addImm(MI.getOperand(2).getImm()); 934 transferDeadCC(&MI, BuiltMI); 935 return BuiltMI; 936 } 937 938 if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) { 939 bool Op0IsGPR = (Opcode == SystemZ::LGDR); 940 bool Op1IsGPR = (Opcode == SystemZ::LDGR); 941 // If we're spilling the destination of an LDGR or LGDR, store the 942 // source register instead. 943 if (OpNum == 0) { 944 unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD; 945 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), 946 get(StoreOpcode)) 947 .addOperand(MI.getOperand(1)) 948 .addFrameIndex(FrameIndex) 949 .addImm(0) 950 .addReg(0); 951 } 952 // If we're spilling the source of an LDGR or LGDR, load the 953 // destination register instead. 954 if (OpNum == 1) { 955 unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD; 956 unsigned Dest = MI.getOperand(0).getReg(); 957 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), 958 get(LoadOpcode), Dest) 959 .addFrameIndex(FrameIndex) 960 .addImm(0) 961 .addReg(0); 962 } 963 } 964 965 // Look for cases where the source of a simple store or the destination 966 // of a simple load is being spilled. Try to use MVC instead. 967 // 968 // Although MVC is in practice a fast choice in these cases, it is still 969 // logically a bytewise copy. This means that we cannot use it if the 970 // load or store is volatile. We also wouldn't be able to use MVC if 971 // the two memories partially overlap, but that case cannot occur here, 972 // because we know that one of the memories is a full frame index. 973 // 974 // For performance reasons, we also want to avoid using MVC if the addresses 975 // might be equal. We don't worry about that case here, because spill slot 976 // coloring happens later, and because we have special code to remove 977 // MVCs that turn out to be redundant. 978 if (OpNum == 0 && MI.hasOneMemOperand()) { 979 MachineMemOperand *MMO = *MI.memoperands_begin(); 980 if (MMO->getSize() == Size && !MMO->isVolatile()) { 981 // Handle conversion of loads. 982 if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXLoad)) { 983 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), 984 get(SystemZ::MVC)) 985 .addFrameIndex(FrameIndex) 986 .addImm(0) 987 .addImm(Size) 988 .addOperand(MI.getOperand(1)) 989 .addImm(MI.getOperand(2).getImm()) 990 .addMemOperand(MMO); 991 } 992 // Handle conversion of stores. 993 if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXStore)) { 994 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), 995 get(SystemZ::MVC)) 996 .addOperand(MI.getOperand(1)) 997 .addImm(MI.getOperand(2).getImm()) 998 .addImm(Size) 999 .addFrameIndex(FrameIndex) 1000 .addImm(0) 1001 .addMemOperand(MMO); 1002 } 1003 } 1004 } 1005 1006 // If the spilled operand is the final one, try to change <INSN>R 1007 // into <INSN>. 1008 int MemOpcode = SystemZ::getMemOpcode(Opcode); 1009 if (MemOpcode >= 0) { 1010 unsigned NumOps = MI.getNumExplicitOperands(); 1011 if (OpNum == NumOps - 1) { 1012 const MCInstrDesc &MemDesc = get(MemOpcode); 1013 uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags); 1014 assert(AccessBytes != 0 && "Size of access should be known"); 1015 assert(AccessBytes <= Size && "Access outside the frame index"); 1016 uint64_t Offset = Size - AccessBytes; 1017 MachineInstrBuilder MIB = BuildMI(*InsertPt->getParent(), InsertPt, 1018 MI.getDebugLoc(), get(MemOpcode)); 1019 for (unsigned I = 0; I < OpNum; ++I) 1020 MIB.addOperand(MI.getOperand(I)); 1021 MIB.addFrameIndex(FrameIndex).addImm(Offset); 1022 if (MemDesc.TSFlags & SystemZII::HasIndex) 1023 MIB.addReg(0); 1024 transferDeadCC(&MI, MIB); 1025 return MIB; 1026 } 1027 } 1028 1029 return nullptr; 1030 } 1031 1032 MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl( 1033 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops, 1034 MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI, 1035 LiveIntervals *LIS) const { 1036 return nullptr; 1037 } 1038 1039 bool SystemZInstrInfo::expandPostRAPseudo(MachineInstr &MI) const { 1040 switch (MI.getOpcode()) { 1041 case SystemZ::L128: 1042 splitMove(MI, SystemZ::LG); 1043 return true; 1044 1045 case SystemZ::ST128: 1046 splitMove(MI, SystemZ::STG); 1047 return true; 1048 1049 case SystemZ::LX: 1050 splitMove(MI, SystemZ::LD); 1051 return true; 1052 1053 case SystemZ::STX: 1054 splitMove(MI, SystemZ::STD); 1055 return true; 1056 1057 case SystemZ::LBMux: 1058 expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH); 1059 return true; 1060 1061 case SystemZ::LHMux: 1062 expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH); 1063 return true; 1064 1065 case SystemZ::LLCRMux: 1066 expandZExtPseudo(MI, SystemZ::LLCR, 8); 1067 return true; 1068 1069 case SystemZ::LLHRMux: 1070 expandZExtPseudo(MI, SystemZ::LLHR, 16); 1071 return true; 1072 1073 case SystemZ::LLCMux: 1074 expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH); 1075 return true; 1076 1077 case SystemZ::LLHMux: 1078 expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH); 1079 return true; 1080 1081 case SystemZ::LMux: 1082 expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH); 1083 return true; 1084 1085 case SystemZ::STCMux: 1086 expandRXYPseudo(MI, SystemZ::STC, SystemZ::STCH); 1087 return true; 1088 1089 case SystemZ::STHMux: 1090 expandRXYPseudo(MI, SystemZ::STH, SystemZ::STHH); 1091 return true; 1092 1093 case SystemZ::STMux: 1094 expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH); 1095 return true; 1096 1097 case SystemZ::LHIMux: 1098 expandRIPseudo(MI, SystemZ::LHI, SystemZ::IIHF, true); 1099 return true; 1100 1101 case SystemZ::IIFMux: 1102 expandRIPseudo(MI, SystemZ::IILF, SystemZ::IIHF, false); 1103 return true; 1104 1105 case SystemZ::IILMux: 1106 expandRIPseudo(MI, SystemZ::IILL, SystemZ::IIHL, false); 1107 return true; 1108 1109 case SystemZ::IIHMux: 1110 expandRIPseudo(MI, SystemZ::IILH, SystemZ::IIHH, false); 1111 return true; 1112 1113 case SystemZ::NIFMux: 1114 expandRIPseudo(MI, SystemZ::NILF, SystemZ::NIHF, false); 1115 return true; 1116 1117 case SystemZ::NILMux: 1118 expandRIPseudo(MI, SystemZ::NILL, SystemZ::NIHL, false); 1119 return true; 1120 1121 case SystemZ::NIHMux: 1122 expandRIPseudo(MI, SystemZ::NILH, SystemZ::NIHH, false); 1123 return true; 1124 1125 case SystemZ::OIFMux: 1126 expandRIPseudo(MI, SystemZ::OILF, SystemZ::OIHF, false); 1127 return true; 1128 1129 case SystemZ::OILMux: 1130 expandRIPseudo(MI, SystemZ::OILL, SystemZ::OIHL, false); 1131 return true; 1132 1133 case SystemZ::OIHMux: 1134 expandRIPseudo(MI, SystemZ::OILH, SystemZ::OIHH, false); 1135 return true; 1136 1137 case SystemZ::XIFMux: 1138 expandRIPseudo(MI, SystemZ::XILF, SystemZ::XIHF, false); 1139 return true; 1140 1141 case SystemZ::TMLMux: 1142 expandRIPseudo(MI, SystemZ::TMLL, SystemZ::TMHL, false); 1143 return true; 1144 1145 case SystemZ::TMHMux: 1146 expandRIPseudo(MI, SystemZ::TMLH, SystemZ::TMHH, false); 1147 return true; 1148 1149 case SystemZ::AHIMux: 1150 expandRIPseudo(MI, SystemZ::AHI, SystemZ::AIH, false); 1151 return true; 1152 1153 case SystemZ::AHIMuxK: 1154 expandRIEPseudo(MI, SystemZ::AHI, SystemZ::AHIK, SystemZ::AIH); 1155 return true; 1156 1157 case SystemZ::AFIMux: 1158 expandRIPseudo(MI, SystemZ::AFI, SystemZ::AIH, false); 1159 return true; 1160 1161 case SystemZ::CFIMux: 1162 expandRIPseudo(MI, SystemZ::CFI, SystemZ::CIH, false); 1163 return true; 1164 1165 case SystemZ::CLFIMux: 1166 expandRIPseudo(MI, SystemZ::CLFI, SystemZ::CLIH, false); 1167 return true; 1168 1169 case SystemZ::CMux: 1170 expandRXYPseudo(MI, SystemZ::C, SystemZ::CHF); 1171 return true; 1172 1173 case SystemZ::CLMux: 1174 expandRXYPseudo(MI, SystemZ::CL, SystemZ::CLHF); 1175 return true; 1176 1177 case SystemZ::RISBMux: { 1178 bool DestIsHigh = isHighReg(MI.getOperand(0).getReg()); 1179 bool SrcIsHigh = isHighReg(MI.getOperand(2).getReg()); 1180 if (SrcIsHigh == DestIsHigh) 1181 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHH : SystemZ::RISBLL)); 1182 else { 1183 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHL : SystemZ::RISBLH)); 1184 MI.getOperand(5).setImm(MI.getOperand(5).getImm() ^ 32); 1185 } 1186 return true; 1187 } 1188 1189 case SystemZ::ADJDYNALLOC: 1190 splitAdjDynAlloc(MI); 1191 return true; 1192 1193 case TargetOpcode::LOAD_STACK_GUARD: 1194 expandLoadStackGuard(&MI); 1195 return true; 1196 1197 default: 1198 return false; 1199 } 1200 } 1201 1202 unsigned SystemZInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { 1203 if (MI.getOpcode() == TargetOpcode::INLINEASM) { 1204 const MachineFunction *MF = MI.getParent()->getParent(); 1205 const char *AsmStr = MI.getOperand(0).getSymbolName(); 1206 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); 1207 } 1208 return MI.getDesc().getSize(); 1209 } 1210 1211 SystemZII::Branch 1212 SystemZInstrInfo::getBranchInfo(const MachineInstr &MI) const { 1213 switch (MI.getOpcode()) { 1214 case SystemZ::BR: 1215 case SystemZ::J: 1216 case SystemZ::JG: 1217 return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY, 1218 SystemZ::CCMASK_ANY, &MI.getOperand(0)); 1219 1220 case SystemZ::BRC: 1221 case SystemZ::BRCL: 1222 return SystemZII::Branch(SystemZII::BranchNormal, MI.getOperand(0).getImm(), 1223 MI.getOperand(1).getImm(), &MI.getOperand(2)); 1224 1225 case SystemZ::BRCT: 1226 return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP, 1227 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2)); 1228 1229 case SystemZ::BRCTG: 1230 return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP, 1231 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2)); 1232 1233 case SystemZ::CIJ: 1234 case SystemZ::CRJ: 1235 return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP, 1236 MI.getOperand(2).getImm(), &MI.getOperand(3)); 1237 1238 case SystemZ::CLIJ: 1239 case SystemZ::CLRJ: 1240 return SystemZII::Branch(SystemZII::BranchCL, SystemZ::CCMASK_ICMP, 1241 MI.getOperand(2).getImm(), &MI.getOperand(3)); 1242 1243 case SystemZ::CGIJ: 1244 case SystemZ::CGRJ: 1245 return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP, 1246 MI.getOperand(2).getImm(), &MI.getOperand(3)); 1247 1248 case SystemZ::CLGIJ: 1249 case SystemZ::CLGRJ: 1250 return SystemZII::Branch(SystemZII::BranchCLG, SystemZ::CCMASK_ICMP, 1251 MI.getOperand(2).getImm(), &MI.getOperand(3)); 1252 1253 default: 1254 llvm_unreachable("Unrecognized branch opcode"); 1255 } 1256 } 1257 1258 void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC, 1259 unsigned &LoadOpcode, 1260 unsigned &StoreOpcode) const { 1261 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) { 1262 LoadOpcode = SystemZ::L; 1263 StoreOpcode = SystemZ::ST; 1264 } else if (RC == &SystemZ::GRH32BitRegClass) { 1265 LoadOpcode = SystemZ::LFH; 1266 StoreOpcode = SystemZ::STFH; 1267 } else if (RC == &SystemZ::GRX32BitRegClass) { 1268 LoadOpcode = SystemZ::LMux; 1269 StoreOpcode = SystemZ::STMux; 1270 } else if (RC == &SystemZ::GR64BitRegClass || 1271 RC == &SystemZ::ADDR64BitRegClass) { 1272 LoadOpcode = SystemZ::LG; 1273 StoreOpcode = SystemZ::STG; 1274 } else if (RC == &SystemZ::GR128BitRegClass || 1275 RC == &SystemZ::ADDR128BitRegClass) { 1276 LoadOpcode = SystemZ::L128; 1277 StoreOpcode = SystemZ::ST128; 1278 } else if (RC == &SystemZ::FP32BitRegClass) { 1279 LoadOpcode = SystemZ::LE; 1280 StoreOpcode = SystemZ::STE; 1281 } else if (RC == &SystemZ::FP64BitRegClass) { 1282 LoadOpcode = SystemZ::LD; 1283 StoreOpcode = SystemZ::STD; 1284 } else if (RC == &SystemZ::FP128BitRegClass) { 1285 LoadOpcode = SystemZ::LX; 1286 StoreOpcode = SystemZ::STX; 1287 } else if (RC == &SystemZ::VR32BitRegClass) { 1288 LoadOpcode = SystemZ::VL32; 1289 StoreOpcode = SystemZ::VST32; 1290 } else if (RC == &SystemZ::VR64BitRegClass) { 1291 LoadOpcode = SystemZ::VL64; 1292 StoreOpcode = SystemZ::VST64; 1293 } else if (RC == &SystemZ::VF128BitRegClass || 1294 RC == &SystemZ::VR128BitRegClass) { 1295 LoadOpcode = SystemZ::VL; 1296 StoreOpcode = SystemZ::VST; 1297 } else 1298 llvm_unreachable("Unsupported regclass to load or store"); 1299 } 1300 1301 unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode, 1302 int64_t Offset) const { 1303 const MCInstrDesc &MCID = get(Opcode); 1304 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset); 1305 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) { 1306 // Get the instruction to use for unsigned 12-bit displacements. 1307 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode); 1308 if (Disp12Opcode >= 0) 1309 return Disp12Opcode; 1310 1311 // All address-related instructions can use unsigned 12-bit 1312 // displacements. 1313 return Opcode; 1314 } 1315 if (isInt<20>(Offset) && isInt<20>(Offset2)) { 1316 // Get the instruction to use for signed 20-bit displacements. 1317 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode); 1318 if (Disp20Opcode >= 0) 1319 return Disp20Opcode; 1320 1321 // Check whether Opcode allows signed 20-bit displacements. 1322 if (MCID.TSFlags & SystemZII::Has20BitOffset) 1323 return Opcode; 1324 } 1325 return 0; 1326 } 1327 1328 unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const { 1329 switch (Opcode) { 1330 case SystemZ::L: return SystemZ::LT; 1331 case SystemZ::LY: return SystemZ::LT; 1332 case SystemZ::LG: return SystemZ::LTG; 1333 case SystemZ::LGF: return SystemZ::LTGF; 1334 case SystemZ::LR: return SystemZ::LTR; 1335 case SystemZ::LGFR: return SystemZ::LTGFR; 1336 case SystemZ::LGR: return SystemZ::LTGR; 1337 case SystemZ::LER: return SystemZ::LTEBR; 1338 case SystemZ::LDR: return SystemZ::LTDBR; 1339 case SystemZ::LXR: return SystemZ::LTXBR; 1340 case SystemZ::LCDFR: return SystemZ::LCDBR; 1341 case SystemZ::LPDFR: return SystemZ::LPDBR; 1342 case SystemZ::LNDFR: return SystemZ::LNDBR; 1343 case SystemZ::LCDFR_32: return SystemZ::LCEBR; 1344 case SystemZ::LPDFR_32: return SystemZ::LPEBR; 1345 case SystemZ::LNDFR_32: return SystemZ::LNEBR; 1346 // On zEC12 we prefer to use RISBGN. But if there is a chance to 1347 // actually use the condition code, we may turn it back into RISGB. 1348 // Note that RISBG is not really a "load-and-test" instruction, 1349 // but sets the same condition code values, so is OK to use here. 1350 case SystemZ::RISBGN: return SystemZ::RISBG; 1351 default: return 0; 1352 } 1353 } 1354 1355 // Return true if Mask matches the regexp 0*1+0*, given that zero masks 1356 // have already been filtered out. Store the first set bit in LSB and 1357 // the number of set bits in Length if so. 1358 static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) { 1359 unsigned First = findFirstSet(Mask); 1360 uint64_t Top = (Mask >> First) + 1; 1361 if ((Top & -Top) == Top) { 1362 LSB = First; 1363 Length = findFirstSet(Top); 1364 return true; 1365 } 1366 return false; 1367 } 1368 1369 bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize, 1370 unsigned &Start, unsigned &End) const { 1371 // Reject trivial all-zero masks. 1372 Mask &= allOnes(BitSize); 1373 if (Mask == 0) 1374 return false; 1375 1376 // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of 1377 // the msb and End specifies the index of the lsb. 1378 unsigned LSB, Length; 1379 if (isStringOfOnes(Mask, LSB, Length)) { 1380 Start = 63 - (LSB + Length - 1); 1381 End = 63 - LSB; 1382 return true; 1383 } 1384 1385 // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb 1386 // of the low 1s and End specifies the lsb of the high 1s. 1387 if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) { 1388 assert(LSB > 0 && "Bottom bit must be set"); 1389 assert(LSB + Length < BitSize && "Top bit must be set"); 1390 Start = 63 - (LSB - 1); 1391 End = 63 - (LSB + Length); 1392 return true; 1393 } 1394 1395 return false; 1396 } 1397 1398 unsigned SystemZInstrInfo::getFusedCompare(unsigned Opcode, 1399 SystemZII::FusedCompareType Type, 1400 const MachineInstr *MI) const { 1401 switch (Opcode) { 1402 case SystemZ::CHI: 1403 case SystemZ::CGHI: 1404 if (!(MI && isInt<8>(MI->getOperand(1).getImm()))) 1405 return 0; 1406 break; 1407 case SystemZ::CLFI: 1408 case SystemZ::CLGFI: 1409 if (!(MI && isUInt<8>(MI->getOperand(1).getImm()))) 1410 return 0; 1411 } 1412 switch (Type) { 1413 case SystemZII::CompareAndBranch: 1414 switch (Opcode) { 1415 case SystemZ::CR: 1416 return SystemZ::CRJ; 1417 case SystemZ::CGR: 1418 return SystemZ::CGRJ; 1419 case SystemZ::CHI: 1420 return SystemZ::CIJ; 1421 case SystemZ::CGHI: 1422 return SystemZ::CGIJ; 1423 case SystemZ::CLR: 1424 return SystemZ::CLRJ; 1425 case SystemZ::CLGR: 1426 return SystemZ::CLGRJ; 1427 case SystemZ::CLFI: 1428 return SystemZ::CLIJ; 1429 case SystemZ::CLGFI: 1430 return SystemZ::CLGIJ; 1431 default: 1432 return 0; 1433 } 1434 case SystemZII::CompareAndReturn: 1435 switch (Opcode) { 1436 case SystemZ::CR: 1437 return SystemZ::CRBReturn; 1438 case SystemZ::CGR: 1439 return SystemZ::CGRBReturn; 1440 case SystemZ::CHI: 1441 return SystemZ::CIBReturn; 1442 case SystemZ::CGHI: 1443 return SystemZ::CGIBReturn; 1444 case SystemZ::CLR: 1445 return SystemZ::CLRBReturn; 1446 case SystemZ::CLGR: 1447 return SystemZ::CLGRBReturn; 1448 case SystemZ::CLFI: 1449 return SystemZ::CLIBReturn; 1450 case SystemZ::CLGFI: 1451 return SystemZ::CLGIBReturn; 1452 default: 1453 return 0; 1454 } 1455 case SystemZII::CompareAndSibcall: 1456 switch (Opcode) { 1457 case SystemZ::CR: 1458 return SystemZ::CRBCall; 1459 case SystemZ::CGR: 1460 return SystemZ::CGRBCall; 1461 case SystemZ::CHI: 1462 return SystemZ::CIBCall; 1463 case SystemZ::CGHI: 1464 return SystemZ::CGIBCall; 1465 case SystemZ::CLR: 1466 return SystemZ::CLRBCall; 1467 case SystemZ::CLGR: 1468 return SystemZ::CLGRBCall; 1469 case SystemZ::CLFI: 1470 return SystemZ::CLIBCall; 1471 case SystemZ::CLGFI: 1472 return SystemZ::CLGIBCall; 1473 default: 1474 return 0; 1475 } 1476 case SystemZII::CompareAndTrap: 1477 switch (Opcode) { 1478 case SystemZ::CR: 1479 return SystemZ::CRT; 1480 case SystemZ::CGR: 1481 return SystemZ::CGRT; 1482 case SystemZ::CHI: 1483 return SystemZ::CIT; 1484 case SystemZ::CGHI: 1485 return SystemZ::CGIT; 1486 case SystemZ::CLR: 1487 return SystemZ::CLRT; 1488 case SystemZ::CLGR: 1489 return SystemZ::CLGRT; 1490 case SystemZ::CLFI: 1491 return SystemZ::CLFIT; 1492 case SystemZ::CLGFI: 1493 return SystemZ::CLGIT; 1494 default: 1495 return 0; 1496 } 1497 } 1498 return 0; 1499 } 1500 1501 void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB, 1502 MachineBasicBlock::iterator MBBI, 1503 unsigned Reg, uint64_t Value) const { 1504 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 1505 unsigned Opcode; 1506 if (isInt<16>(Value)) 1507 Opcode = SystemZ::LGHI; 1508 else if (SystemZ::isImmLL(Value)) 1509 Opcode = SystemZ::LLILL; 1510 else if (SystemZ::isImmLH(Value)) { 1511 Opcode = SystemZ::LLILH; 1512 Value >>= 16; 1513 } else { 1514 assert(isInt<32>(Value) && "Huge values not handled yet"); 1515 Opcode = SystemZ::LGFI; 1516 } 1517 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value); 1518 } 1519