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