1 //===-- SystemZInstrInfo.cpp - SystemZ instruction information ------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains the SystemZ implementation of the TargetInstrInfo class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "SystemZInstrInfo.h" 14 #include "MCTargetDesc/SystemZMCTargetDesc.h" 15 #include "SystemZ.h" 16 #include "SystemZInstrBuilder.h" 17 #include "SystemZSubtarget.h" 18 #include "llvm/ADT/Statistic.h" 19 #include "llvm/CodeGen/LiveInterval.h" 20 #include "llvm/CodeGen/LiveIntervals.h" 21 #include "llvm/CodeGen/LiveVariables.h" 22 #include "llvm/CodeGen/MachineBasicBlock.h" 23 #include "llvm/CodeGen/MachineFrameInfo.h" 24 #include "llvm/CodeGen/MachineFunction.h" 25 #include "llvm/CodeGen/MachineInstr.h" 26 #include "llvm/CodeGen/MachineMemOperand.h" 27 #include "llvm/CodeGen/MachineOperand.h" 28 #include "llvm/CodeGen/MachineRegisterInfo.h" 29 #include "llvm/CodeGen/SlotIndexes.h" 30 #include "llvm/CodeGen/StackMaps.h" 31 #include "llvm/CodeGen/TargetInstrInfo.h" 32 #include "llvm/CodeGen/TargetSubtargetInfo.h" 33 #include "llvm/MC/MCInstrDesc.h" 34 #include "llvm/MC/MCRegisterInfo.h" 35 #include "llvm/Support/BranchProbability.h" 36 #include "llvm/Support/ErrorHandling.h" 37 #include "llvm/Support/MathExtras.h" 38 #include "llvm/Target/TargetMachine.h" 39 #include <cassert> 40 #include <cstdint> 41 #include <iterator> 42 43 using namespace llvm; 44 45 #define GET_INSTRINFO_CTOR_DTOR 46 #define GET_INSTRMAP_INFO 47 #include "SystemZGenInstrInfo.inc" 48 49 #define DEBUG_TYPE "systemz-II" 50 51 // Return a mask with Count low bits set. 52 static uint64_t allOnes(unsigned int Count) { 53 return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1; 54 } 55 56 // Pin the vtable to this file. 57 void SystemZInstrInfo::anchor() {} 58 59 SystemZInstrInfo::SystemZInstrInfo(SystemZSubtarget &sti) 60 : SystemZGenInstrInfo(SystemZ::ADJCALLSTACKDOWN, SystemZ::ADJCALLSTACKUP), 61 RI(sti.getSpecialRegisters()->getReturnFunctionAddressRegister()), 62 STI(sti) {} 63 64 // MI is a 128-bit load or store. Split it into two 64-bit loads or stores, 65 // each having the opcode given by NewOpcode. 66 void SystemZInstrInfo::splitMove(MachineBasicBlock::iterator MI, 67 unsigned NewOpcode) const { 68 MachineBasicBlock *MBB = MI->getParent(); 69 MachineFunction &MF = *MBB->getParent(); 70 71 // Get two load or store instructions. Use the original instruction for one 72 // of them (arbitrarily the second here) and create a clone for the other. 73 MachineInstr *EarlierMI = MF.CloneMachineInstr(&*MI); 74 MBB->insert(MI, EarlierMI); 75 76 // Set up the two 64-bit registers and remember super reg and its flags. 77 MachineOperand &HighRegOp = EarlierMI->getOperand(0); 78 MachineOperand &LowRegOp = MI->getOperand(0); 79 Register Reg128 = LowRegOp.getReg(); 80 unsigned Reg128Killed = getKillRegState(LowRegOp.isKill()); 81 unsigned Reg128Undef = getUndefRegState(LowRegOp.isUndef()); 82 HighRegOp.setReg(RI.getSubReg(HighRegOp.getReg(), SystemZ::subreg_h64)); 83 LowRegOp.setReg(RI.getSubReg(LowRegOp.getReg(), SystemZ::subreg_l64)); 84 85 if (MI->mayStore()) { 86 // Add implicit uses of the super register in case one of the subregs is 87 // undefined. We could track liveness and skip storing an undefined 88 // subreg, but this is hopefully rare (discovered with llvm-stress). 89 // If Reg128 was killed, set kill flag on MI. 90 unsigned Reg128UndefImpl = (Reg128Undef | RegState::Implicit); 91 MachineInstrBuilder(MF, EarlierMI).addReg(Reg128, Reg128UndefImpl); 92 MachineInstrBuilder(MF, MI).addReg(Reg128, (Reg128UndefImpl | Reg128Killed)); 93 } 94 95 // The address in the first (high) instruction is already correct. 96 // Adjust the offset in the second (low) instruction. 97 MachineOperand &HighOffsetOp = EarlierMI->getOperand(2); 98 MachineOperand &LowOffsetOp = MI->getOperand(2); 99 LowOffsetOp.setImm(LowOffsetOp.getImm() + 8); 100 101 // Clear the kill flags on the registers in the first instruction. 102 if (EarlierMI->getOperand(0).isReg() && EarlierMI->getOperand(0).isUse()) 103 EarlierMI->getOperand(0).setIsKill(false); 104 EarlierMI->getOperand(1).setIsKill(false); 105 EarlierMI->getOperand(3).setIsKill(false); 106 107 // Set the opcodes. 108 unsigned HighOpcode = getOpcodeForOffset(NewOpcode, HighOffsetOp.getImm()); 109 unsigned LowOpcode = getOpcodeForOffset(NewOpcode, LowOffsetOp.getImm()); 110 assert(HighOpcode && LowOpcode && "Both offsets should be in range"); 111 112 EarlierMI->setDesc(get(HighOpcode)); 113 MI->setDesc(get(LowOpcode)); 114 } 115 116 // Split ADJDYNALLOC instruction MI. 117 void SystemZInstrInfo::splitAdjDynAlloc(MachineBasicBlock::iterator MI) const { 118 MachineBasicBlock *MBB = MI->getParent(); 119 MachineFunction &MF = *MBB->getParent(); 120 MachineFrameInfo &MFFrame = MF.getFrameInfo(); 121 MachineOperand &OffsetMO = MI->getOperand(2); 122 123 uint64_t Offset = (MFFrame.getMaxCallFrameSize() + 124 SystemZMC::ELFCallFrameSize + 125 OffsetMO.getImm()); 126 unsigned NewOpcode = getOpcodeForOffset(SystemZ::LA, Offset); 127 assert(NewOpcode && "No support for huge argument lists yet"); 128 MI->setDesc(get(NewOpcode)); 129 OffsetMO.setImm(Offset); 130 } 131 132 // MI is an RI-style pseudo instruction. Replace it with LowOpcode 133 // if the first operand is a low GR32 and HighOpcode if the first operand 134 // is a high GR32. ConvertHigh is true if LowOpcode takes a signed operand 135 // and HighOpcode takes an unsigned 32-bit operand. In those cases, 136 // MI has the same kind of operand as LowOpcode, so needs to be converted 137 // if HighOpcode is used. 138 void SystemZInstrInfo::expandRIPseudo(MachineInstr &MI, unsigned LowOpcode, 139 unsigned HighOpcode, 140 bool ConvertHigh) const { 141 Register Reg = MI.getOperand(0).getReg(); 142 bool IsHigh = SystemZ::isHighReg(Reg); 143 MI.setDesc(get(IsHigh ? HighOpcode : LowOpcode)); 144 if (IsHigh && ConvertHigh) 145 MI.getOperand(1).setImm(uint32_t(MI.getOperand(1).getImm())); 146 } 147 148 // MI is a three-operand RIE-style pseudo instruction. Replace it with 149 // LowOpcodeK if the registers are both low GR32s, otherwise use a move 150 // followed by HighOpcode or LowOpcode, depending on whether the target 151 // is a high or low GR32. 152 void SystemZInstrInfo::expandRIEPseudo(MachineInstr &MI, unsigned LowOpcode, 153 unsigned LowOpcodeK, 154 unsigned HighOpcode) const { 155 Register DestReg = MI.getOperand(0).getReg(); 156 Register SrcReg = MI.getOperand(1).getReg(); 157 bool DestIsHigh = SystemZ::isHighReg(DestReg); 158 bool SrcIsHigh = SystemZ::isHighReg(SrcReg); 159 if (!DestIsHigh && !SrcIsHigh) 160 MI.setDesc(get(LowOpcodeK)); 161 else { 162 if (DestReg != SrcReg) { 163 emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(), DestReg, SrcReg, 164 SystemZ::LR, 32, MI.getOperand(1).isKill(), 165 MI.getOperand(1).isUndef()); 166 MI.getOperand(1).setReg(DestReg); 167 } 168 MI.setDesc(get(DestIsHigh ? HighOpcode : LowOpcode)); 169 MI.tieOperands(0, 1); 170 } 171 } 172 173 // MI is an RXY-style pseudo instruction. Replace it with LowOpcode 174 // if the first operand is a low GR32 and HighOpcode if the first operand 175 // is a high GR32. 176 void SystemZInstrInfo::expandRXYPseudo(MachineInstr &MI, unsigned LowOpcode, 177 unsigned HighOpcode) const { 178 Register Reg = MI.getOperand(0).getReg(); 179 unsigned Opcode = getOpcodeForOffset( 180 SystemZ::isHighReg(Reg) ? HighOpcode : LowOpcode, 181 MI.getOperand(2).getImm()); 182 MI.setDesc(get(Opcode)); 183 } 184 185 // MI is a load-on-condition pseudo instruction with a single register 186 // (source or destination) operand. Replace it with LowOpcode if the 187 // register is a low GR32 and HighOpcode if the register is a high GR32. 188 void SystemZInstrInfo::expandLOCPseudo(MachineInstr &MI, unsigned LowOpcode, 189 unsigned HighOpcode) const { 190 Register Reg = MI.getOperand(0).getReg(); 191 unsigned Opcode = SystemZ::isHighReg(Reg) ? HighOpcode : LowOpcode; 192 MI.setDesc(get(Opcode)); 193 } 194 195 // MI is an RR-style pseudo instruction that zero-extends the low Size bits 196 // of one GRX32 into another. Replace it with LowOpcode if both operands 197 // are low registers, otherwise use RISB[LH]G. 198 void SystemZInstrInfo::expandZExtPseudo(MachineInstr &MI, unsigned LowOpcode, 199 unsigned Size) const { 200 MachineInstrBuilder MIB = 201 emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(), 202 MI.getOperand(0).getReg(), MI.getOperand(1).getReg(), LowOpcode, 203 Size, MI.getOperand(1).isKill(), MI.getOperand(1).isUndef()); 204 205 // Keep the remaining operands as-is. 206 for (const MachineOperand &MO : llvm::drop_begin(MI.operands(), 2)) 207 MIB.add(MO); 208 209 MI.eraseFromParent(); 210 } 211 212 void SystemZInstrInfo::expandLoadStackGuard(MachineInstr *MI) const { 213 MachineBasicBlock *MBB = MI->getParent(); 214 MachineFunction &MF = *MBB->getParent(); 215 const Register Reg64 = MI->getOperand(0).getReg(); 216 const Register Reg32 = RI.getSubReg(Reg64, SystemZ::subreg_l32); 217 218 // EAR can only load the low subregister so us a shift for %a0 to produce 219 // the GR containing %a0 and %a1. 220 221 // ear <reg>, %a0 222 BuildMI(*MBB, MI, MI->getDebugLoc(), get(SystemZ::EAR), Reg32) 223 .addReg(SystemZ::A0) 224 .addReg(Reg64, RegState::ImplicitDefine); 225 226 // sllg <reg>, <reg>, 32 227 BuildMI(*MBB, MI, MI->getDebugLoc(), get(SystemZ::SLLG), Reg64) 228 .addReg(Reg64) 229 .addReg(0) 230 .addImm(32); 231 232 // ear <reg>, %a1 233 BuildMI(*MBB, MI, MI->getDebugLoc(), get(SystemZ::EAR), Reg32) 234 .addReg(SystemZ::A1); 235 236 // lg <reg>, 40(<reg>) 237 MI->setDesc(get(SystemZ::LG)); 238 MachineInstrBuilder(MF, MI).addReg(Reg64).addImm(40).addReg(0); 239 } 240 241 // Emit a zero-extending move from 32-bit GPR SrcReg to 32-bit GPR 242 // DestReg before MBBI in MBB. Use LowLowOpcode when both DestReg and SrcReg 243 // are low registers, otherwise use RISB[LH]G. Size is the number of bits 244 // taken from the low end of SrcReg (8 for LLCR, 16 for LLHR and 32 for LR). 245 // KillSrc is true if this move is the last use of SrcReg. 246 MachineInstrBuilder 247 SystemZInstrInfo::emitGRX32Move(MachineBasicBlock &MBB, 248 MachineBasicBlock::iterator MBBI, 249 const DebugLoc &DL, unsigned DestReg, 250 unsigned SrcReg, unsigned LowLowOpcode, 251 unsigned Size, bool KillSrc, 252 bool UndefSrc) const { 253 unsigned Opcode; 254 bool DestIsHigh = SystemZ::isHighReg(DestReg); 255 bool SrcIsHigh = SystemZ::isHighReg(SrcReg); 256 if (DestIsHigh && SrcIsHigh) 257 Opcode = SystemZ::RISBHH; 258 else if (DestIsHigh && !SrcIsHigh) 259 Opcode = SystemZ::RISBHL; 260 else if (!DestIsHigh && SrcIsHigh) 261 Opcode = SystemZ::RISBLH; 262 else { 263 return BuildMI(MBB, MBBI, DL, get(LowLowOpcode), DestReg) 264 .addReg(SrcReg, getKillRegState(KillSrc) | getUndefRegState(UndefSrc)); 265 } 266 unsigned Rotate = (DestIsHigh != SrcIsHigh ? 32 : 0); 267 return BuildMI(MBB, MBBI, DL, get(Opcode), DestReg) 268 .addReg(DestReg, RegState::Undef) 269 .addReg(SrcReg, getKillRegState(KillSrc) | getUndefRegState(UndefSrc)) 270 .addImm(32 - Size).addImm(128 + 31).addImm(Rotate); 271 } 272 273 MachineInstr *SystemZInstrInfo::commuteInstructionImpl(MachineInstr &MI, 274 bool NewMI, 275 unsigned OpIdx1, 276 unsigned OpIdx2) const { 277 auto cloneIfNew = [NewMI](MachineInstr &MI) -> MachineInstr & { 278 if (NewMI) 279 return *MI.getParent()->getParent()->CloneMachineInstr(&MI); 280 return MI; 281 }; 282 283 switch (MI.getOpcode()) { 284 case SystemZ::SELRMux: 285 case SystemZ::SELFHR: 286 case SystemZ::SELR: 287 case SystemZ::SELGR: 288 case SystemZ::LOCRMux: 289 case SystemZ::LOCFHR: 290 case SystemZ::LOCR: 291 case SystemZ::LOCGR: { 292 auto &WorkingMI = cloneIfNew(MI); 293 // Invert condition. 294 unsigned CCValid = WorkingMI.getOperand(3).getImm(); 295 unsigned CCMask = WorkingMI.getOperand(4).getImm(); 296 WorkingMI.getOperand(4).setImm(CCMask ^ CCValid); 297 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 298 OpIdx1, OpIdx2); 299 } 300 default: 301 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); 302 } 303 } 304 305 // If MI is a simple load or store for a frame object, return the register 306 // it loads or stores and set FrameIndex to the index of the frame object. 307 // Return 0 otherwise. 308 // 309 // Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores. 310 static int isSimpleMove(const MachineInstr &MI, int &FrameIndex, 311 unsigned Flag) { 312 const MCInstrDesc &MCID = MI.getDesc(); 313 if ((MCID.TSFlags & Flag) && MI.getOperand(1).isFI() && 314 MI.getOperand(2).getImm() == 0 && MI.getOperand(3).getReg() == 0) { 315 FrameIndex = MI.getOperand(1).getIndex(); 316 return MI.getOperand(0).getReg(); 317 } 318 return 0; 319 } 320 321 unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, 322 int &FrameIndex) const { 323 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad); 324 } 325 326 unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr &MI, 327 int &FrameIndex) const { 328 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore); 329 } 330 331 bool SystemZInstrInfo::isStackSlotCopy(const MachineInstr &MI, 332 int &DestFrameIndex, 333 int &SrcFrameIndex) const { 334 // Check for MVC 0(Length,FI1),0(FI2) 335 const MachineFrameInfo &MFI = MI.getParent()->getParent()->getFrameInfo(); 336 if (MI.getOpcode() != SystemZ::MVC || !MI.getOperand(0).isFI() || 337 MI.getOperand(1).getImm() != 0 || !MI.getOperand(3).isFI() || 338 MI.getOperand(4).getImm() != 0) 339 return false; 340 341 // Check that Length covers the full slots. 342 int64_t Length = MI.getOperand(2).getImm(); 343 unsigned FI1 = MI.getOperand(0).getIndex(); 344 unsigned FI2 = MI.getOperand(3).getIndex(); 345 if (MFI.getObjectSize(FI1) != Length || 346 MFI.getObjectSize(FI2) != Length) 347 return false; 348 349 DestFrameIndex = FI1; 350 SrcFrameIndex = FI2; 351 return true; 352 } 353 354 bool SystemZInstrInfo::analyzeBranch(MachineBasicBlock &MBB, 355 MachineBasicBlock *&TBB, 356 MachineBasicBlock *&FBB, 357 SmallVectorImpl<MachineOperand> &Cond, 358 bool AllowModify) const { 359 // Most of the code and comments here are boilerplate. 360 361 // Start from the bottom of the block and work up, examining the 362 // terminator instructions. 363 MachineBasicBlock::iterator I = MBB.end(); 364 while (I != MBB.begin()) { 365 --I; 366 if (I->isDebugInstr()) 367 continue; 368 369 // Working from the bottom, when we see a non-terminator instruction, we're 370 // done. 371 if (!isUnpredicatedTerminator(*I)) 372 break; 373 374 // A terminator that isn't a branch can't easily be handled by this 375 // analysis. 376 if (!I->isBranch()) 377 return true; 378 379 // Can't handle indirect branches. 380 SystemZII::Branch Branch(getBranchInfo(*I)); 381 if (!Branch.hasMBBTarget()) 382 return true; 383 384 // Punt on compound branches. 385 if (Branch.Type != SystemZII::BranchNormal) 386 return true; 387 388 if (Branch.CCMask == SystemZ::CCMASK_ANY) { 389 // Handle unconditional branches. 390 if (!AllowModify) { 391 TBB = Branch.getMBBTarget(); 392 continue; 393 } 394 395 // If the block has any instructions after a JMP, delete them. 396 while (std::next(I) != MBB.end()) 397 std::next(I)->eraseFromParent(); 398 399 Cond.clear(); 400 FBB = nullptr; 401 402 // Delete the JMP if it's equivalent to a fall-through. 403 if (MBB.isLayoutSuccessor(Branch.getMBBTarget())) { 404 TBB = nullptr; 405 I->eraseFromParent(); 406 I = MBB.end(); 407 continue; 408 } 409 410 // TBB is used to indicate the unconditinal destination. 411 TBB = Branch.getMBBTarget(); 412 continue; 413 } 414 415 // Working from the bottom, handle the first conditional branch. 416 if (Cond.empty()) { 417 // FIXME: add X86-style branch swap 418 FBB = TBB; 419 TBB = Branch.getMBBTarget(); 420 Cond.push_back(MachineOperand::CreateImm(Branch.CCValid)); 421 Cond.push_back(MachineOperand::CreateImm(Branch.CCMask)); 422 continue; 423 } 424 425 // Handle subsequent conditional branches. 426 assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch"); 427 428 // Only handle the case where all conditional branches branch to the same 429 // destination. 430 if (TBB != Branch.getMBBTarget()) 431 return true; 432 433 // If the conditions are the same, we can leave them alone. 434 unsigned OldCCValid = Cond[0].getImm(); 435 unsigned OldCCMask = Cond[1].getImm(); 436 if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask) 437 continue; 438 439 // FIXME: Try combining conditions like X86 does. Should be easy on Z! 440 return false; 441 } 442 443 return false; 444 } 445 446 unsigned SystemZInstrInfo::removeBranch(MachineBasicBlock &MBB, 447 int *BytesRemoved) const { 448 assert(!BytesRemoved && "code size not handled"); 449 450 // Most of the code and comments here are boilerplate. 451 MachineBasicBlock::iterator I = MBB.end(); 452 unsigned Count = 0; 453 454 while (I != MBB.begin()) { 455 --I; 456 if (I->isDebugInstr()) 457 continue; 458 if (!I->isBranch()) 459 break; 460 if (!getBranchInfo(*I).hasMBBTarget()) 461 break; 462 // Remove the branch. 463 I->eraseFromParent(); 464 I = MBB.end(); 465 ++Count; 466 } 467 468 return Count; 469 } 470 471 bool SystemZInstrInfo:: 472 reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { 473 assert(Cond.size() == 2 && "Invalid condition"); 474 Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm()); 475 return false; 476 } 477 478 unsigned SystemZInstrInfo::insertBranch(MachineBasicBlock &MBB, 479 MachineBasicBlock *TBB, 480 MachineBasicBlock *FBB, 481 ArrayRef<MachineOperand> Cond, 482 const DebugLoc &DL, 483 int *BytesAdded) const { 484 // In this function we output 32-bit branches, which should always 485 // have enough range. They can be shortened and relaxed by later code 486 // in the pipeline, if desired. 487 488 // Shouldn't be a fall through. 489 assert(TBB && "insertBranch must not be told to insert a fallthrough"); 490 assert((Cond.size() == 2 || Cond.size() == 0) && 491 "SystemZ branch conditions have one component!"); 492 assert(!BytesAdded && "code size not handled"); 493 494 if (Cond.empty()) { 495 // Unconditional branch? 496 assert(!FBB && "Unconditional branch with multiple successors!"); 497 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB); 498 return 1; 499 } 500 501 // Conditional branch. 502 unsigned Count = 0; 503 unsigned CCValid = Cond[0].getImm(); 504 unsigned CCMask = Cond[1].getImm(); 505 BuildMI(&MBB, DL, get(SystemZ::BRC)) 506 .addImm(CCValid).addImm(CCMask).addMBB(TBB); 507 ++Count; 508 509 if (FBB) { 510 // Two-way Conditional branch. Insert the second branch. 511 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB); 512 ++Count; 513 } 514 return Count; 515 } 516 517 bool SystemZInstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg, 518 Register &SrcReg2, int64_t &Mask, 519 int64_t &Value) const { 520 assert(MI.isCompare() && "Caller should have checked for a comparison"); 521 522 if (MI.getNumExplicitOperands() == 2 && MI.getOperand(0).isReg() && 523 MI.getOperand(1).isImm()) { 524 SrcReg = MI.getOperand(0).getReg(); 525 SrcReg2 = 0; 526 Value = MI.getOperand(1).getImm(); 527 Mask = ~0; 528 return true; 529 } 530 531 return false; 532 } 533 534 bool SystemZInstrInfo::canInsertSelect(const MachineBasicBlock &MBB, 535 ArrayRef<MachineOperand> Pred, 536 Register DstReg, Register TrueReg, 537 Register FalseReg, int &CondCycles, 538 int &TrueCycles, 539 int &FalseCycles) const { 540 // Not all subtargets have LOCR instructions. 541 if (!STI.hasLoadStoreOnCond()) 542 return false; 543 if (Pred.size() != 2) 544 return false; 545 546 // Check register classes. 547 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 548 const TargetRegisterClass *RC = 549 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); 550 if (!RC) 551 return false; 552 553 // We have LOCR instructions for 32 and 64 bit general purpose registers. 554 if ((STI.hasLoadStoreOnCond2() && 555 SystemZ::GRX32BitRegClass.hasSubClassEq(RC)) || 556 SystemZ::GR32BitRegClass.hasSubClassEq(RC) || 557 SystemZ::GR64BitRegClass.hasSubClassEq(RC)) { 558 CondCycles = 2; 559 TrueCycles = 2; 560 FalseCycles = 2; 561 return true; 562 } 563 564 // Can't do anything else. 565 return false; 566 } 567 568 void SystemZInstrInfo::insertSelect(MachineBasicBlock &MBB, 569 MachineBasicBlock::iterator I, 570 const DebugLoc &DL, Register DstReg, 571 ArrayRef<MachineOperand> Pred, 572 Register TrueReg, 573 Register FalseReg) const { 574 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 575 const TargetRegisterClass *RC = MRI.getRegClass(DstReg); 576 577 assert(Pred.size() == 2 && "Invalid condition"); 578 unsigned CCValid = Pred[0].getImm(); 579 unsigned CCMask = Pred[1].getImm(); 580 581 unsigned Opc; 582 if (SystemZ::GRX32BitRegClass.hasSubClassEq(RC)) { 583 if (STI.hasMiscellaneousExtensions3()) 584 Opc = SystemZ::SELRMux; 585 else if (STI.hasLoadStoreOnCond2()) 586 Opc = SystemZ::LOCRMux; 587 else { 588 Opc = SystemZ::LOCR; 589 MRI.constrainRegClass(DstReg, &SystemZ::GR32BitRegClass); 590 Register TReg = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass); 591 Register FReg = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass); 592 BuildMI(MBB, I, DL, get(TargetOpcode::COPY), TReg).addReg(TrueReg); 593 BuildMI(MBB, I, DL, get(TargetOpcode::COPY), FReg).addReg(FalseReg); 594 TrueReg = TReg; 595 FalseReg = FReg; 596 } 597 } else if (SystemZ::GR64BitRegClass.hasSubClassEq(RC)) { 598 if (STI.hasMiscellaneousExtensions3()) 599 Opc = SystemZ::SELGR; 600 else 601 Opc = SystemZ::LOCGR; 602 } else 603 llvm_unreachable("Invalid register class"); 604 605 BuildMI(MBB, I, DL, get(Opc), DstReg) 606 .addReg(FalseReg).addReg(TrueReg) 607 .addImm(CCValid).addImm(CCMask); 608 } 609 610 bool SystemZInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, 611 Register Reg, 612 MachineRegisterInfo *MRI) const { 613 unsigned DefOpc = DefMI.getOpcode(); 614 if (DefOpc != SystemZ::LHIMux && DefOpc != SystemZ::LHI && 615 DefOpc != SystemZ::LGHI) 616 return false; 617 if (DefMI.getOperand(0).getReg() != Reg) 618 return false; 619 int32_t ImmVal = (int32_t)DefMI.getOperand(1).getImm(); 620 621 unsigned UseOpc = UseMI.getOpcode(); 622 unsigned NewUseOpc; 623 unsigned UseIdx; 624 int CommuteIdx = -1; 625 bool TieOps = false; 626 switch (UseOpc) { 627 case SystemZ::SELRMux: 628 TieOps = true; 629 LLVM_FALLTHROUGH; 630 case SystemZ::LOCRMux: 631 if (!STI.hasLoadStoreOnCond2()) 632 return false; 633 NewUseOpc = SystemZ::LOCHIMux; 634 if (UseMI.getOperand(2).getReg() == Reg) 635 UseIdx = 2; 636 else if (UseMI.getOperand(1).getReg() == Reg) 637 UseIdx = 2, CommuteIdx = 1; 638 else 639 return false; 640 break; 641 case SystemZ::SELGR: 642 TieOps = true; 643 LLVM_FALLTHROUGH; 644 case SystemZ::LOCGR: 645 if (!STI.hasLoadStoreOnCond2()) 646 return false; 647 NewUseOpc = SystemZ::LOCGHI; 648 if (UseMI.getOperand(2).getReg() == Reg) 649 UseIdx = 2; 650 else if (UseMI.getOperand(1).getReg() == Reg) 651 UseIdx = 2, CommuteIdx = 1; 652 else 653 return false; 654 break; 655 default: 656 return false; 657 } 658 659 if (CommuteIdx != -1) 660 if (!commuteInstruction(UseMI, false, CommuteIdx, UseIdx)) 661 return false; 662 663 bool DeleteDef = MRI->hasOneNonDBGUse(Reg); 664 UseMI.setDesc(get(NewUseOpc)); 665 if (TieOps) 666 UseMI.tieOperands(0, 1); 667 UseMI.getOperand(UseIdx).ChangeToImmediate(ImmVal); 668 if (DeleteDef) 669 DefMI.eraseFromParent(); 670 671 return true; 672 } 673 674 bool SystemZInstrInfo::isPredicable(const MachineInstr &MI) const { 675 unsigned Opcode = MI.getOpcode(); 676 if (Opcode == SystemZ::Return || 677 Opcode == SystemZ::Return_XPLINK || 678 Opcode == SystemZ::Trap || 679 Opcode == SystemZ::CallJG || 680 Opcode == SystemZ::CallBR) 681 return true; 682 return false; 683 } 684 685 bool SystemZInstrInfo:: 686 isProfitableToIfCvt(MachineBasicBlock &MBB, 687 unsigned NumCycles, unsigned ExtraPredCycles, 688 BranchProbability Probability) const { 689 // Avoid using conditional returns at the end of a loop (since then 690 // we'd need to emit an unconditional branch to the beginning anyway, 691 // making the loop body longer). This doesn't apply for low-probability 692 // loops (eg. compare-and-swap retry), so just decide based on branch 693 // probability instead of looping structure. 694 // However, since Compare and Trap instructions cost the same as a regular 695 // Compare instruction, we should allow the if conversion to convert this 696 // into a Conditional Compare regardless of the branch probability. 697 if (MBB.getLastNonDebugInstr()->getOpcode() != SystemZ::Trap && 698 MBB.succ_empty() && Probability < BranchProbability(1, 8)) 699 return false; 700 // For now only convert single instructions. 701 return NumCycles == 1; 702 } 703 704 bool SystemZInstrInfo:: 705 isProfitableToIfCvt(MachineBasicBlock &TMBB, 706 unsigned NumCyclesT, unsigned ExtraPredCyclesT, 707 MachineBasicBlock &FMBB, 708 unsigned NumCyclesF, unsigned ExtraPredCyclesF, 709 BranchProbability Probability) const { 710 // For now avoid converting mutually-exclusive cases. 711 return false; 712 } 713 714 bool SystemZInstrInfo:: 715 isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles, 716 BranchProbability Probability) const { 717 // For now only duplicate single instructions. 718 return NumCycles == 1; 719 } 720 721 bool SystemZInstrInfo::PredicateInstruction( 722 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const { 723 assert(Pred.size() == 2 && "Invalid condition"); 724 unsigned CCValid = Pred[0].getImm(); 725 unsigned CCMask = Pred[1].getImm(); 726 assert(CCMask > 0 && CCMask < 15 && "Invalid predicate"); 727 unsigned Opcode = MI.getOpcode(); 728 if (Opcode == SystemZ::Trap) { 729 MI.setDesc(get(SystemZ::CondTrap)); 730 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 731 .addImm(CCValid).addImm(CCMask) 732 .addReg(SystemZ::CC, RegState::Implicit); 733 return true; 734 } 735 if (Opcode == SystemZ::Return || Opcode == SystemZ::Return_XPLINK) { 736 MI.setDesc(get(Opcode == SystemZ::Return ? SystemZ::CondReturn 737 : SystemZ::CondReturn_XPLINK)); 738 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 739 .addImm(CCValid) 740 .addImm(CCMask) 741 .addReg(SystemZ::CC, RegState::Implicit); 742 return true; 743 } 744 if (Opcode == SystemZ::CallJG) { 745 MachineOperand FirstOp = MI.getOperand(0); 746 const uint32_t *RegMask = MI.getOperand(1).getRegMask(); 747 MI.RemoveOperand(1); 748 MI.RemoveOperand(0); 749 MI.setDesc(get(SystemZ::CallBRCL)); 750 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 751 .addImm(CCValid) 752 .addImm(CCMask) 753 .add(FirstOp) 754 .addRegMask(RegMask) 755 .addReg(SystemZ::CC, RegState::Implicit); 756 return true; 757 } 758 if (Opcode == SystemZ::CallBR) { 759 MachineOperand Target = MI.getOperand(0); 760 const uint32_t *RegMask = MI.getOperand(1).getRegMask(); 761 MI.RemoveOperand(1); 762 MI.RemoveOperand(0); 763 MI.setDesc(get(SystemZ::CallBCR)); 764 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 765 .addImm(CCValid).addImm(CCMask) 766 .add(Target) 767 .addRegMask(RegMask) 768 .addReg(SystemZ::CC, RegState::Implicit); 769 return true; 770 } 771 return false; 772 } 773 774 void SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 775 MachineBasicBlock::iterator MBBI, 776 const DebugLoc &DL, MCRegister DestReg, 777 MCRegister SrcReg, bool KillSrc) const { 778 // Split 128-bit GPR moves into two 64-bit moves. Add implicit uses of the 779 // super register in case one of the subregs is undefined. 780 // This handles ADDR128 too. 781 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) { 782 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64), 783 RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc); 784 MachineInstrBuilder(*MBB.getParent(), std::prev(MBBI)) 785 .addReg(SrcReg, RegState::Implicit); 786 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64), 787 RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc); 788 MachineInstrBuilder(*MBB.getParent(), std::prev(MBBI)) 789 .addReg(SrcReg, (getKillRegState(KillSrc) | RegState::Implicit)); 790 return; 791 } 792 793 if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) { 794 emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc, 795 false); 796 return; 797 } 798 799 // Move 128-bit floating-point values between VR128 and FP128. 800 if (SystemZ::VR128BitRegClass.contains(DestReg) && 801 SystemZ::FP128BitRegClass.contains(SrcReg)) { 802 MCRegister SrcRegHi = 803 RI.getMatchingSuperReg(RI.getSubReg(SrcReg, SystemZ::subreg_h64), 804 SystemZ::subreg_h64, &SystemZ::VR128BitRegClass); 805 MCRegister SrcRegLo = 806 RI.getMatchingSuperReg(RI.getSubReg(SrcReg, SystemZ::subreg_l64), 807 SystemZ::subreg_h64, &SystemZ::VR128BitRegClass); 808 809 BuildMI(MBB, MBBI, DL, get(SystemZ::VMRHG), DestReg) 810 .addReg(SrcRegHi, getKillRegState(KillSrc)) 811 .addReg(SrcRegLo, getKillRegState(KillSrc)); 812 return; 813 } 814 if (SystemZ::FP128BitRegClass.contains(DestReg) && 815 SystemZ::VR128BitRegClass.contains(SrcReg)) { 816 MCRegister DestRegHi = 817 RI.getMatchingSuperReg(RI.getSubReg(DestReg, SystemZ::subreg_h64), 818 SystemZ::subreg_h64, &SystemZ::VR128BitRegClass); 819 MCRegister DestRegLo = 820 RI.getMatchingSuperReg(RI.getSubReg(DestReg, SystemZ::subreg_l64), 821 SystemZ::subreg_h64, &SystemZ::VR128BitRegClass); 822 823 if (DestRegHi != SrcReg) 824 copyPhysReg(MBB, MBBI, DL, DestRegHi, SrcReg, false); 825 BuildMI(MBB, MBBI, DL, get(SystemZ::VREPG), DestRegLo) 826 .addReg(SrcReg, getKillRegState(KillSrc)).addImm(1); 827 return; 828 } 829 830 // Move CC value from a GR32. 831 if (DestReg == SystemZ::CC) { 832 unsigned Opcode = 833 SystemZ::GR32BitRegClass.contains(SrcReg) ? SystemZ::TMLH : SystemZ::TMHH; 834 BuildMI(MBB, MBBI, DL, get(Opcode)) 835 .addReg(SrcReg, getKillRegState(KillSrc)) 836 .addImm(3 << (SystemZ::IPM_CC - 16)); 837 return; 838 } 839 840 // Everything else needs only one instruction. 841 unsigned Opcode; 842 if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg)) 843 Opcode = SystemZ::LGR; 844 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg)) 845 // For z13 we prefer LDR over LER to avoid partial register dependencies. 846 Opcode = STI.hasVector() ? SystemZ::LDR32 : SystemZ::LER; 847 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg)) 848 Opcode = SystemZ::LDR; 849 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg)) 850 Opcode = SystemZ::LXR; 851 else if (SystemZ::VR32BitRegClass.contains(DestReg, SrcReg)) 852 Opcode = SystemZ::VLR32; 853 else if (SystemZ::VR64BitRegClass.contains(DestReg, SrcReg)) 854 Opcode = SystemZ::VLR64; 855 else if (SystemZ::VR128BitRegClass.contains(DestReg, SrcReg)) 856 Opcode = SystemZ::VLR; 857 else if (SystemZ::AR32BitRegClass.contains(DestReg, SrcReg)) 858 Opcode = SystemZ::CPYA; 859 else 860 llvm_unreachable("Impossible reg-to-reg copy"); 861 862 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg) 863 .addReg(SrcReg, getKillRegState(KillSrc)); 864 } 865 866 void SystemZInstrInfo::storeRegToStackSlot( 867 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg, 868 bool isKill, int FrameIdx, const TargetRegisterClass *RC, 869 const TargetRegisterInfo *TRI) const { 870 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 871 872 // Callers may expect a single instruction, so keep 128-bit moves 873 // together for now and lower them after register allocation. 874 unsigned LoadOpcode, StoreOpcode; 875 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode); 876 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode)) 877 .addReg(SrcReg, getKillRegState(isKill)), 878 FrameIdx); 879 } 880 881 void SystemZInstrInfo::loadRegFromStackSlot( 882 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register DestReg, 883 int FrameIdx, const TargetRegisterClass *RC, 884 const TargetRegisterInfo *TRI) const { 885 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 886 887 // Callers may expect a single instruction, so keep 128-bit moves 888 // together for now and lower them after register allocation. 889 unsigned LoadOpcode, StoreOpcode; 890 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode); 891 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg), 892 FrameIdx); 893 } 894 895 // Return true if MI is a simple load or store with a 12-bit displacement 896 // and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores. 897 static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) { 898 const MCInstrDesc &MCID = MI->getDesc(); 899 return ((MCID.TSFlags & Flag) && 900 isUInt<12>(MI->getOperand(2).getImm()) && 901 MI->getOperand(3).getReg() == 0); 902 } 903 904 namespace { 905 906 struct LogicOp { 907 LogicOp() = default; 908 LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize) 909 : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {} 910 911 explicit operator bool() const { return RegSize; } 912 913 unsigned RegSize = 0; 914 unsigned ImmLSB = 0; 915 unsigned ImmSize = 0; 916 }; 917 918 } // end anonymous namespace 919 920 static LogicOp interpretAndImmediate(unsigned Opcode) { 921 switch (Opcode) { 922 case SystemZ::NILMux: return LogicOp(32, 0, 16); 923 case SystemZ::NIHMux: return LogicOp(32, 16, 16); 924 case SystemZ::NILL64: return LogicOp(64, 0, 16); 925 case SystemZ::NILH64: return LogicOp(64, 16, 16); 926 case SystemZ::NIHL64: return LogicOp(64, 32, 16); 927 case SystemZ::NIHH64: return LogicOp(64, 48, 16); 928 case SystemZ::NIFMux: return LogicOp(32, 0, 32); 929 case SystemZ::NILF64: return LogicOp(64, 0, 32); 930 case SystemZ::NIHF64: return LogicOp(64, 32, 32); 931 default: return LogicOp(); 932 } 933 } 934 935 static void transferDeadCC(MachineInstr *OldMI, MachineInstr *NewMI) { 936 if (OldMI->registerDefIsDead(SystemZ::CC)) { 937 MachineOperand *CCDef = NewMI->findRegisterDefOperand(SystemZ::CC); 938 if (CCDef != nullptr) 939 CCDef->setIsDead(true); 940 } 941 } 942 943 static void transferMIFlag(MachineInstr *OldMI, MachineInstr *NewMI, 944 MachineInstr::MIFlag Flag) { 945 if (OldMI->getFlag(Flag)) 946 NewMI->setFlag(Flag); 947 } 948 949 MachineInstr * 950 SystemZInstrInfo::convertToThreeAddress(MachineInstr &MI, LiveVariables *LV, 951 LiveIntervals *LIS) const { 952 MachineBasicBlock *MBB = MI.getParent(); 953 954 // Try to convert an AND into an RISBG-type instruction. 955 // TODO: It might be beneficial to select RISBG and shorten to AND instead. 956 if (LogicOp And = interpretAndImmediate(MI.getOpcode())) { 957 uint64_t Imm = MI.getOperand(2).getImm() << And.ImmLSB; 958 // AND IMMEDIATE leaves the other bits of the register unchanged. 959 Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB); 960 unsigned Start, End; 961 if (isRxSBGMask(Imm, And.RegSize, Start, End)) { 962 unsigned NewOpcode; 963 if (And.RegSize == 64) { 964 NewOpcode = SystemZ::RISBG; 965 // Prefer RISBGN if available, since it does not clobber CC. 966 if (STI.hasMiscellaneousExtensions()) 967 NewOpcode = SystemZ::RISBGN; 968 } else { 969 NewOpcode = SystemZ::RISBMux; 970 Start &= 31; 971 End &= 31; 972 } 973 MachineOperand &Dest = MI.getOperand(0); 974 MachineOperand &Src = MI.getOperand(1); 975 MachineInstrBuilder MIB = 976 BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpcode)) 977 .add(Dest) 978 .addReg(0) 979 .addReg(Src.getReg(), getKillRegState(Src.isKill()), 980 Src.getSubReg()) 981 .addImm(Start) 982 .addImm(End + 128) 983 .addImm(0); 984 if (LV) { 985 unsigned NumOps = MI.getNumOperands(); 986 for (unsigned I = 1; I < NumOps; ++I) { 987 MachineOperand &Op = MI.getOperand(I); 988 if (Op.isReg() && Op.isKill()) 989 LV->replaceKillInstruction(Op.getReg(), MI, *MIB); 990 } 991 } 992 if (LIS) 993 LIS->ReplaceMachineInstrInMaps(MI, *MIB); 994 transferDeadCC(&MI, MIB); 995 return MIB; 996 } 997 } 998 return nullptr; 999 } 1000 1001 MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl( 1002 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops, 1003 MachineBasicBlock::iterator InsertPt, int FrameIndex, 1004 LiveIntervals *LIS, VirtRegMap *VRM) const { 1005 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 1006 MachineRegisterInfo &MRI = MF.getRegInfo(); 1007 const MachineFrameInfo &MFI = MF.getFrameInfo(); 1008 unsigned Size = MFI.getObjectSize(FrameIndex); 1009 unsigned Opcode = MI.getOpcode(); 1010 1011 // Check CC liveness if new instruction introduces a dead def of CC. 1012 MCRegUnitIterator CCUnit(MCRegister::from(SystemZ::CC), TRI); 1013 SlotIndex MISlot = SlotIndex(); 1014 LiveRange *CCLiveRange = nullptr; 1015 bool CCLiveAtMI = true; 1016 if (LIS) { 1017 MISlot = LIS->getSlotIndexes()->getInstructionIndex(MI).getRegSlot(); 1018 CCLiveRange = &LIS->getRegUnit(*CCUnit); 1019 CCLiveAtMI = CCLiveRange->liveAt(MISlot); 1020 } 1021 ++CCUnit; 1022 assert(!CCUnit.isValid() && "CC only has one reg unit."); 1023 1024 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) { 1025 if (!CCLiveAtMI && (Opcode == SystemZ::LA || Opcode == SystemZ::LAY) && 1026 isInt<8>(MI.getOperand(2).getImm()) && !MI.getOperand(3).getReg()) { 1027 // LA(Y) %reg, CONST(%reg) -> AGSI %mem, CONST 1028 MachineInstr *BuiltMI = BuildMI(*InsertPt->getParent(), InsertPt, 1029 MI.getDebugLoc(), get(SystemZ::AGSI)) 1030 .addFrameIndex(FrameIndex) 1031 .addImm(0) 1032 .addImm(MI.getOperand(2).getImm()); 1033 BuiltMI->findRegisterDefOperand(SystemZ::CC)->setIsDead(true); 1034 CCLiveRange->createDeadDef(MISlot, LIS->getVNInfoAllocator()); 1035 return BuiltMI; 1036 } 1037 return nullptr; 1038 } 1039 1040 // All other cases require a single operand. 1041 if (Ops.size() != 1) 1042 return nullptr; 1043 1044 unsigned OpNum = Ops[0]; 1045 assert(Size * 8 == 1046 TRI->getRegSizeInBits(*MF.getRegInfo() 1047 .getRegClass(MI.getOperand(OpNum).getReg())) && 1048 "Invalid size combination"); 1049 1050 if ((Opcode == SystemZ::AHI || Opcode == SystemZ::AGHI) && OpNum == 0 && 1051 isInt<8>(MI.getOperand(2).getImm())) { 1052 // A(G)HI %reg, CONST -> A(G)SI %mem, CONST 1053 Opcode = (Opcode == SystemZ::AHI ? SystemZ::ASI : SystemZ::AGSI); 1054 MachineInstr *BuiltMI = 1055 BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode)) 1056 .addFrameIndex(FrameIndex) 1057 .addImm(0) 1058 .addImm(MI.getOperand(2).getImm()); 1059 transferDeadCC(&MI, BuiltMI); 1060 transferMIFlag(&MI, BuiltMI, MachineInstr::NoSWrap); 1061 return BuiltMI; 1062 } 1063 1064 if ((Opcode == SystemZ::ALFI && OpNum == 0 && 1065 isInt<8>((int32_t)MI.getOperand(2).getImm())) || 1066 (Opcode == SystemZ::ALGFI && OpNum == 0 && 1067 isInt<8>((int64_t)MI.getOperand(2).getImm()))) { 1068 // AL(G)FI %reg, CONST -> AL(G)SI %mem, CONST 1069 Opcode = (Opcode == SystemZ::ALFI ? SystemZ::ALSI : SystemZ::ALGSI); 1070 MachineInstr *BuiltMI = 1071 BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode)) 1072 .addFrameIndex(FrameIndex) 1073 .addImm(0) 1074 .addImm((int8_t)MI.getOperand(2).getImm()); 1075 transferDeadCC(&MI, BuiltMI); 1076 return BuiltMI; 1077 } 1078 1079 if ((Opcode == SystemZ::SLFI && OpNum == 0 && 1080 isInt<8>((int32_t)-MI.getOperand(2).getImm())) || 1081 (Opcode == SystemZ::SLGFI && OpNum == 0 && 1082 isInt<8>((int64_t)-MI.getOperand(2).getImm()))) { 1083 // SL(G)FI %reg, CONST -> AL(G)SI %mem, -CONST 1084 Opcode = (Opcode == SystemZ::SLFI ? SystemZ::ALSI : SystemZ::ALGSI); 1085 MachineInstr *BuiltMI = 1086 BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode)) 1087 .addFrameIndex(FrameIndex) 1088 .addImm(0) 1089 .addImm((int8_t)-MI.getOperand(2).getImm()); 1090 transferDeadCC(&MI, BuiltMI); 1091 return BuiltMI; 1092 } 1093 1094 unsigned MemImmOpc = 0; 1095 switch (Opcode) { 1096 case SystemZ::LHIMux: 1097 case SystemZ::LHI: MemImmOpc = SystemZ::MVHI; break; 1098 case SystemZ::LGHI: MemImmOpc = SystemZ::MVGHI; break; 1099 case SystemZ::CHIMux: 1100 case SystemZ::CHI: MemImmOpc = SystemZ::CHSI; break; 1101 case SystemZ::CGHI: MemImmOpc = SystemZ::CGHSI; break; 1102 case SystemZ::CLFIMux: 1103 case SystemZ::CLFI: 1104 if (isUInt<16>(MI.getOperand(1).getImm())) 1105 MemImmOpc = SystemZ::CLFHSI; 1106 break; 1107 case SystemZ::CLGFI: 1108 if (isUInt<16>(MI.getOperand(1).getImm())) 1109 MemImmOpc = SystemZ::CLGHSI; 1110 break; 1111 default: break; 1112 } 1113 if (MemImmOpc) 1114 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), 1115 get(MemImmOpc)) 1116 .addFrameIndex(FrameIndex) 1117 .addImm(0) 1118 .addImm(MI.getOperand(1).getImm()); 1119 1120 if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) { 1121 bool Op0IsGPR = (Opcode == SystemZ::LGDR); 1122 bool Op1IsGPR = (Opcode == SystemZ::LDGR); 1123 // If we're spilling the destination of an LDGR or LGDR, store the 1124 // source register instead. 1125 if (OpNum == 0) { 1126 unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD; 1127 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), 1128 get(StoreOpcode)) 1129 .add(MI.getOperand(1)) 1130 .addFrameIndex(FrameIndex) 1131 .addImm(0) 1132 .addReg(0); 1133 } 1134 // If we're spilling the source of an LDGR or LGDR, load the 1135 // destination register instead. 1136 if (OpNum == 1) { 1137 unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD; 1138 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), 1139 get(LoadOpcode)) 1140 .add(MI.getOperand(0)) 1141 .addFrameIndex(FrameIndex) 1142 .addImm(0) 1143 .addReg(0); 1144 } 1145 } 1146 1147 // Look for cases where the source of a simple store or the destination 1148 // of a simple load is being spilled. Try to use MVC instead. 1149 // 1150 // Although MVC is in practice a fast choice in these cases, it is still 1151 // logically a bytewise copy. This means that we cannot use it if the 1152 // load or store is volatile. We also wouldn't be able to use MVC if 1153 // the two memories partially overlap, but that case cannot occur here, 1154 // because we know that one of the memories is a full frame index. 1155 // 1156 // For performance reasons, we also want to avoid using MVC if the addresses 1157 // might be equal. We don't worry about that case here, because spill slot 1158 // coloring happens later, and because we have special code to remove 1159 // MVCs that turn out to be redundant. 1160 if (OpNum == 0 && MI.hasOneMemOperand()) { 1161 MachineMemOperand *MMO = *MI.memoperands_begin(); 1162 if (MMO->getSize() == Size && !MMO->isVolatile() && !MMO->isAtomic()) { 1163 // Handle conversion of loads. 1164 if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXLoad)) { 1165 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), 1166 get(SystemZ::MVC)) 1167 .addFrameIndex(FrameIndex) 1168 .addImm(0) 1169 .addImm(Size) 1170 .add(MI.getOperand(1)) 1171 .addImm(MI.getOperand(2).getImm()) 1172 .addMemOperand(MMO); 1173 } 1174 // Handle conversion of stores. 1175 if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXStore)) { 1176 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), 1177 get(SystemZ::MVC)) 1178 .add(MI.getOperand(1)) 1179 .addImm(MI.getOperand(2).getImm()) 1180 .addImm(Size) 1181 .addFrameIndex(FrameIndex) 1182 .addImm(0) 1183 .addMemOperand(MMO); 1184 } 1185 } 1186 } 1187 1188 // If the spilled operand is the final one or the instruction is 1189 // commutable, try to change <INSN>R into <INSN>. Don't introduce a def of 1190 // CC if it is live and MI does not define it. 1191 unsigned NumOps = MI.getNumExplicitOperands(); 1192 int MemOpcode = SystemZ::getMemOpcode(Opcode); 1193 if (MemOpcode == -1 || 1194 (CCLiveAtMI && !MI.definesRegister(SystemZ::CC) && 1195 get(MemOpcode).hasImplicitDefOfPhysReg(SystemZ::CC))) 1196 return nullptr; 1197 1198 // Check if all other vregs have a usable allocation in the case of vector 1199 // to FP conversion. 1200 const MCInstrDesc &MCID = MI.getDesc(); 1201 for (unsigned I = 0, E = MCID.getNumOperands(); I != E; ++I) { 1202 const MCOperandInfo &MCOI = MCID.OpInfo[I]; 1203 if (MCOI.OperandType != MCOI::OPERAND_REGISTER || I == OpNum) 1204 continue; 1205 const TargetRegisterClass *RC = TRI->getRegClass(MCOI.RegClass); 1206 if (RC == &SystemZ::VR32BitRegClass || RC == &SystemZ::VR64BitRegClass) { 1207 Register Reg = MI.getOperand(I).getReg(); 1208 Register PhysReg = Register::isVirtualRegister(Reg) 1209 ? (VRM ? Register(VRM->getPhys(Reg)) : Register()) 1210 : Reg; 1211 if (!PhysReg || 1212 !(SystemZ::FP32BitRegClass.contains(PhysReg) || 1213 SystemZ::FP64BitRegClass.contains(PhysReg) || 1214 SystemZ::VF128BitRegClass.contains(PhysReg))) 1215 return nullptr; 1216 } 1217 } 1218 // Fused multiply and add/sub need to have the same dst and accumulator reg. 1219 bool FusedFPOp = (Opcode == SystemZ::WFMADB || Opcode == SystemZ::WFMASB || 1220 Opcode == SystemZ::WFMSDB || Opcode == SystemZ::WFMSSB); 1221 if (FusedFPOp) { 1222 Register DstReg = VRM->getPhys(MI.getOperand(0).getReg()); 1223 Register AccReg = VRM->getPhys(MI.getOperand(3).getReg()); 1224 if (OpNum == 0 || OpNum == 3 || DstReg != AccReg) 1225 return nullptr; 1226 } 1227 1228 // Try to swap compare operands if possible. 1229 bool NeedsCommute = false; 1230 if ((MI.getOpcode() == SystemZ::CR || MI.getOpcode() == SystemZ::CGR || 1231 MI.getOpcode() == SystemZ::CLR || MI.getOpcode() == SystemZ::CLGR || 1232 MI.getOpcode() == SystemZ::WFCDB || MI.getOpcode() == SystemZ::WFCSB || 1233 MI.getOpcode() == SystemZ::WFKDB || MI.getOpcode() == SystemZ::WFKSB) && 1234 OpNum == 0 && prepareCompareSwapOperands(MI)) 1235 NeedsCommute = true; 1236 1237 bool CCOperands = false; 1238 if (MI.getOpcode() == SystemZ::LOCRMux || MI.getOpcode() == SystemZ::LOCGR || 1239 MI.getOpcode() == SystemZ::SELRMux || MI.getOpcode() == SystemZ::SELGR) { 1240 assert(MI.getNumOperands() == 6 && NumOps == 5 && 1241 "LOCR/SELR instruction operands corrupt?"); 1242 NumOps -= 2; 1243 CCOperands = true; 1244 } 1245 1246 // See if this is a 3-address instruction that is convertible to 2-address 1247 // and suitable for folding below. Only try this with virtual registers 1248 // and a provided VRM (during regalloc). 1249 if (NumOps == 3 && SystemZ::getTargetMemOpcode(MemOpcode) != -1) { 1250 if (VRM == nullptr) 1251 return nullptr; 1252 else { 1253 Register DstReg = MI.getOperand(0).getReg(); 1254 Register DstPhys = 1255 (Register::isVirtualRegister(DstReg) ? Register(VRM->getPhys(DstReg)) 1256 : DstReg); 1257 Register SrcReg = (OpNum == 2 ? MI.getOperand(1).getReg() 1258 : ((OpNum == 1 && MI.isCommutable()) 1259 ? MI.getOperand(2).getReg() 1260 : Register())); 1261 if (DstPhys && !SystemZ::GRH32BitRegClass.contains(DstPhys) && SrcReg && 1262 Register::isVirtualRegister(SrcReg) && 1263 DstPhys == VRM->getPhys(SrcReg)) 1264 NeedsCommute = (OpNum == 1); 1265 else 1266 return nullptr; 1267 } 1268 } 1269 1270 if ((OpNum == NumOps - 1) || NeedsCommute || FusedFPOp) { 1271 const MCInstrDesc &MemDesc = get(MemOpcode); 1272 uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags); 1273 assert(AccessBytes != 0 && "Size of access should be known"); 1274 assert(AccessBytes <= Size && "Access outside the frame index"); 1275 uint64_t Offset = Size - AccessBytes; 1276 MachineInstrBuilder MIB = BuildMI(*InsertPt->getParent(), InsertPt, 1277 MI.getDebugLoc(), get(MemOpcode)); 1278 if (MI.isCompare()) { 1279 assert(NumOps == 2 && "Expected 2 register operands for a compare."); 1280 MIB.add(MI.getOperand(NeedsCommute ? 1 : 0)); 1281 } 1282 else if (FusedFPOp) { 1283 MIB.add(MI.getOperand(0)); 1284 MIB.add(MI.getOperand(3)); 1285 MIB.add(MI.getOperand(OpNum == 1 ? 2 : 1)); 1286 } 1287 else { 1288 MIB.add(MI.getOperand(0)); 1289 if (NeedsCommute) 1290 MIB.add(MI.getOperand(2)); 1291 else 1292 for (unsigned I = 1; I < OpNum; ++I) 1293 MIB.add(MI.getOperand(I)); 1294 } 1295 MIB.addFrameIndex(FrameIndex).addImm(Offset); 1296 if (MemDesc.TSFlags & SystemZII::HasIndex) 1297 MIB.addReg(0); 1298 if (CCOperands) { 1299 unsigned CCValid = MI.getOperand(NumOps).getImm(); 1300 unsigned CCMask = MI.getOperand(NumOps + 1).getImm(); 1301 MIB.addImm(CCValid); 1302 MIB.addImm(NeedsCommute ? CCMask ^ CCValid : CCMask); 1303 } 1304 if (MIB->definesRegister(SystemZ::CC) && 1305 (!MI.definesRegister(SystemZ::CC) || 1306 MI.registerDefIsDead(SystemZ::CC))) { 1307 MIB->addRegisterDead(SystemZ::CC, TRI); 1308 if (CCLiveRange) 1309 CCLiveRange->createDeadDef(MISlot, LIS->getVNInfoAllocator()); 1310 } 1311 // Constrain the register classes if converted from a vector opcode. The 1312 // allocated regs are in an FP reg-class per previous check above. 1313 for (const MachineOperand &MO : MIB->operands()) 1314 if (MO.isReg() && Register::isVirtualRegister(MO.getReg())) { 1315 Register Reg = MO.getReg(); 1316 if (MRI.getRegClass(Reg) == &SystemZ::VR32BitRegClass) 1317 MRI.setRegClass(Reg, &SystemZ::FP32BitRegClass); 1318 else if (MRI.getRegClass(Reg) == &SystemZ::VR64BitRegClass) 1319 MRI.setRegClass(Reg, &SystemZ::FP64BitRegClass); 1320 else if (MRI.getRegClass(Reg) == &SystemZ::VR128BitRegClass) 1321 MRI.setRegClass(Reg, &SystemZ::VF128BitRegClass); 1322 } 1323 1324 transferDeadCC(&MI, MIB); 1325 transferMIFlag(&MI, MIB, MachineInstr::NoSWrap); 1326 transferMIFlag(&MI, MIB, MachineInstr::NoFPExcept); 1327 return MIB; 1328 } 1329 1330 return nullptr; 1331 } 1332 1333 MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl( 1334 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops, 1335 MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI, 1336 LiveIntervals *LIS) const { 1337 return nullptr; 1338 } 1339 1340 bool SystemZInstrInfo::expandPostRAPseudo(MachineInstr &MI) const { 1341 switch (MI.getOpcode()) { 1342 case SystemZ::L128: 1343 splitMove(MI, SystemZ::LG); 1344 return true; 1345 1346 case SystemZ::ST128: 1347 splitMove(MI, SystemZ::STG); 1348 return true; 1349 1350 case SystemZ::LX: 1351 splitMove(MI, SystemZ::LD); 1352 return true; 1353 1354 case SystemZ::STX: 1355 splitMove(MI, SystemZ::STD); 1356 return true; 1357 1358 case SystemZ::LBMux: 1359 expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH); 1360 return true; 1361 1362 case SystemZ::LHMux: 1363 expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH); 1364 return true; 1365 1366 case SystemZ::LLCRMux: 1367 expandZExtPseudo(MI, SystemZ::LLCR, 8); 1368 return true; 1369 1370 case SystemZ::LLHRMux: 1371 expandZExtPseudo(MI, SystemZ::LLHR, 16); 1372 return true; 1373 1374 case SystemZ::LLCMux: 1375 expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH); 1376 return true; 1377 1378 case SystemZ::LLHMux: 1379 expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH); 1380 return true; 1381 1382 case SystemZ::LMux: 1383 expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH); 1384 return true; 1385 1386 case SystemZ::LOCMux: 1387 expandLOCPseudo(MI, SystemZ::LOC, SystemZ::LOCFH); 1388 return true; 1389 1390 case SystemZ::LOCHIMux: 1391 expandLOCPseudo(MI, SystemZ::LOCHI, SystemZ::LOCHHI); 1392 return true; 1393 1394 case SystemZ::STCMux: 1395 expandRXYPseudo(MI, SystemZ::STC, SystemZ::STCH); 1396 return true; 1397 1398 case SystemZ::STHMux: 1399 expandRXYPseudo(MI, SystemZ::STH, SystemZ::STHH); 1400 return true; 1401 1402 case SystemZ::STMux: 1403 expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH); 1404 return true; 1405 1406 case SystemZ::STOCMux: 1407 expandLOCPseudo(MI, SystemZ::STOC, SystemZ::STOCFH); 1408 return true; 1409 1410 case SystemZ::LHIMux: 1411 expandRIPseudo(MI, SystemZ::LHI, SystemZ::IIHF, true); 1412 return true; 1413 1414 case SystemZ::IIFMux: 1415 expandRIPseudo(MI, SystemZ::IILF, SystemZ::IIHF, false); 1416 return true; 1417 1418 case SystemZ::IILMux: 1419 expandRIPseudo(MI, SystemZ::IILL, SystemZ::IIHL, false); 1420 return true; 1421 1422 case SystemZ::IIHMux: 1423 expandRIPseudo(MI, SystemZ::IILH, SystemZ::IIHH, false); 1424 return true; 1425 1426 case SystemZ::NIFMux: 1427 expandRIPseudo(MI, SystemZ::NILF, SystemZ::NIHF, false); 1428 return true; 1429 1430 case SystemZ::NILMux: 1431 expandRIPseudo(MI, SystemZ::NILL, SystemZ::NIHL, false); 1432 return true; 1433 1434 case SystemZ::NIHMux: 1435 expandRIPseudo(MI, SystemZ::NILH, SystemZ::NIHH, false); 1436 return true; 1437 1438 case SystemZ::OIFMux: 1439 expandRIPseudo(MI, SystemZ::OILF, SystemZ::OIHF, false); 1440 return true; 1441 1442 case SystemZ::OILMux: 1443 expandRIPseudo(MI, SystemZ::OILL, SystemZ::OIHL, false); 1444 return true; 1445 1446 case SystemZ::OIHMux: 1447 expandRIPseudo(MI, SystemZ::OILH, SystemZ::OIHH, false); 1448 return true; 1449 1450 case SystemZ::XIFMux: 1451 expandRIPseudo(MI, SystemZ::XILF, SystemZ::XIHF, false); 1452 return true; 1453 1454 case SystemZ::TMLMux: 1455 expandRIPseudo(MI, SystemZ::TMLL, SystemZ::TMHL, false); 1456 return true; 1457 1458 case SystemZ::TMHMux: 1459 expandRIPseudo(MI, SystemZ::TMLH, SystemZ::TMHH, false); 1460 return true; 1461 1462 case SystemZ::AHIMux: 1463 expandRIPseudo(MI, SystemZ::AHI, SystemZ::AIH, false); 1464 return true; 1465 1466 case SystemZ::AHIMuxK: 1467 expandRIEPseudo(MI, SystemZ::AHI, SystemZ::AHIK, SystemZ::AIH); 1468 return true; 1469 1470 case SystemZ::AFIMux: 1471 expandRIPseudo(MI, SystemZ::AFI, SystemZ::AIH, false); 1472 return true; 1473 1474 case SystemZ::CHIMux: 1475 expandRIPseudo(MI, SystemZ::CHI, SystemZ::CIH, false); 1476 return true; 1477 1478 case SystemZ::CFIMux: 1479 expandRIPseudo(MI, SystemZ::CFI, SystemZ::CIH, false); 1480 return true; 1481 1482 case SystemZ::CLFIMux: 1483 expandRIPseudo(MI, SystemZ::CLFI, SystemZ::CLIH, false); 1484 return true; 1485 1486 case SystemZ::CMux: 1487 expandRXYPseudo(MI, SystemZ::C, SystemZ::CHF); 1488 return true; 1489 1490 case SystemZ::CLMux: 1491 expandRXYPseudo(MI, SystemZ::CL, SystemZ::CLHF); 1492 return true; 1493 1494 case SystemZ::RISBMux: { 1495 bool DestIsHigh = SystemZ::isHighReg(MI.getOperand(0).getReg()); 1496 bool SrcIsHigh = SystemZ::isHighReg(MI.getOperand(2).getReg()); 1497 if (SrcIsHigh == DestIsHigh) 1498 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHH : SystemZ::RISBLL)); 1499 else { 1500 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHL : SystemZ::RISBLH)); 1501 MI.getOperand(5).setImm(MI.getOperand(5).getImm() ^ 32); 1502 } 1503 return true; 1504 } 1505 1506 case SystemZ::ADJDYNALLOC: 1507 splitAdjDynAlloc(MI); 1508 return true; 1509 1510 case TargetOpcode::LOAD_STACK_GUARD: 1511 expandLoadStackGuard(&MI); 1512 return true; 1513 1514 default: 1515 return false; 1516 } 1517 } 1518 1519 unsigned SystemZInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { 1520 if (MI.isInlineAsm()) { 1521 const MachineFunction *MF = MI.getParent()->getParent(); 1522 const char *AsmStr = MI.getOperand(0).getSymbolName(); 1523 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); 1524 } 1525 else if (MI.getOpcode() == SystemZ::PATCHPOINT) 1526 return PatchPointOpers(&MI).getNumPatchBytes(); 1527 else if (MI.getOpcode() == SystemZ::STACKMAP) 1528 return MI.getOperand(1).getImm(); 1529 else if (MI.getOpcode() == SystemZ::FENTRY_CALL) 1530 return 6; 1531 1532 return MI.getDesc().getSize(); 1533 } 1534 1535 SystemZII::Branch 1536 SystemZInstrInfo::getBranchInfo(const MachineInstr &MI) const { 1537 switch (MI.getOpcode()) { 1538 case SystemZ::BR: 1539 case SystemZ::BI: 1540 case SystemZ::J: 1541 case SystemZ::JG: 1542 return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY, 1543 SystemZ::CCMASK_ANY, &MI.getOperand(0)); 1544 1545 case SystemZ::BRC: 1546 case SystemZ::BRCL: 1547 return SystemZII::Branch(SystemZII::BranchNormal, MI.getOperand(0).getImm(), 1548 MI.getOperand(1).getImm(), &MI.getOperand(2)); 1549 1550 case SystemZ::BRCT: 1551 case SystemZ::BRCTH: 1552 return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP, 1553 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2)); 1554 1555 case SystemZ::BRCTG: 1556 return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP, 1557 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2)); 1558 1559 case SystemZ::CIJ: 1560 case SystemZ::CRJ: 1561 return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP, 1562 MI.getOperand(2).getImm(), &MI.getOperand(3)); 1563 1564 case SystemZ::CLIJ: 1565 case SystemZ::CLRJ: 1566 return SystemZII::Branch(SystemZII::BranchCL, SystemZ::CCMASK_ICMP, 1567 MI.getOperand(2).getImm(), &MI.getOperand(3)); 1568 1569 case SystemZ::CGIJ: 1570 case SystemZ::CGRJ: 1571 return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP, 1572 MI.getOperand(2).getImm(), &MI.getOperand(3)); 1573 1574 case SystemZ::CLGIJ: 1575 case SystemZ::CLGRJ: 1576 return SystemZII::Branch(SystemZII::BranchCLG, SystemZ::CCMASK_ICMP, 1577 MI.getOperand(2).getImm(), &MI.getOperand(3)); 1578 1579 case SystemZ::INLINEASM_BR: 1580 // Don't try to analyze asm goto, so pass nullptr as branch target argument. 1581 return SystemZII::Branch(SystemZII::AsmGoto, 0, 0, nullptr); 1582 1583 default: 1584 llvm_unreachable("Unrecognized branch opcode"); 1585 } 1586 } 1587 1588 void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC, 1589 unsigned &LoadOpcode, 1590 unsigned &StoreOpcode) const { 1591 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) { 1592 LoadOpcode = SystemZ::L; 1593 StoreOpcode = SystemZ::ST; 1594 } else if (RC == &SystemZ::GRH32BitRegClass) { 1595 LoadOpcode = SystemZ::LFH; 1596 StoreOpcode = SystemZ::STFH; 1597 } else if (RC == &SystemZ::GRX32BitRegClass) { 1598 LoadOpcode = SystemZ::LMux; 1599 StoreOpcode = SystemZ::STMux; 1600 } else if (RC == &SystemZ::GR64BitRegClass || 1601 RC == &SystemZ::ADDR64BitRegClass) { 1602 LoadOpcode = SystemZ::LG; 1603 StoreOpcode = SystemZ::STG; 1604 } else if (RC == &SystemZ::GR128BitRegClass || 1605 RC == &SystemZ::ADDR128BitRegClass) { 1606 LoadOpcode = SystemZ::L128; 1607 StoreOpcode = SystemZ::ST128; 1608 } else if (RC == &SystemZ::FP32BitRegClass) { 1609 LoadOpcode = SystemZ::LE; 1610 StoreOpcode = SystemZ::STE; 1611 } else if (RC == &SystemZ::FP64BitRegClass) { 1612 LoadOpcode = SystemZ::LD; 1613 StoreOpcode = SystemZ::STD; 1614 } else if (RC == &SystemZ::FP128BitRegClass) { 1615 LoadOpcode = SystemZ::LX; 1616 StoreOpcode = SystemZ::STX; 1617 } else if (RC == &SystemZ::VR32BitRegClass) { 1618 LoadOpcode = SystemZ::VL32; 1619 StoreOpcode = SystemZ::VST32; 1620 } else if (RC == &SystemZ::VR64BitRegClass) { 1621 LoadOpcode = SystemZ::VL64; 1622 StoreOpcode = SystemZ::VST64; 1623 } else if (RC == &SystemZ::VF128BitRegClass || 1624 RC == &SystemZ::VR128BitRegClass) { 1625 LoadOpcode = SystemZ::VL; 1626 StoreOpcode = SystemZ::VST; 1627 } else 1628 llvm_unreachable("Unsupported regclass to load or store"); 1629 } 1630 1631 unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode, 1632 int64_t Offset) const { 1633 const MCInstrDesc &MCID = get(Opcode); 1634 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset); 1635 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) { 1636 // Get the instruction to use for unsigned 12-bit displacements. 1637 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode); 1638 if (Disp12Opcode >= 0) 1639 return Disp12Opcode; 1640 1641 // All address-related instructions can use unsigned 12-bit 1642 // displacements. 1643 return Opcode; 1644 } 1645 if (isInt<20>(Offset) && isInt<20>(Offset2)) { 1646 // Get the instruction to use for signed 20-bit displacements. 1647 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode); 1648 if (Disp20Opcode >= 0) 1649 return Disp20Opcode; 1650 1651 // Check whether Opcode allows signed 20-bit displacements. 1652 if (MCID.TSFlags & SystemZII::Has20BitOffset) 1653 return Opcode; 1654 } 1655 return 0; 1656 } 1657 1658 bool SystemZInstrInfo::hasDisplacementPairInsn(unsigned Opcode) const { 1659 const MCInstrDesc &MCID = get(Opcode); 1660 if (MCID.TSFlags & SystemZII::Has20BitOffset) 1661 return SystemZ::getDisp12Opcode(Opcode) >= 0; 1662 return SystemZ::getDisp20Opcode(Opcode) >= 0; 1663 } 1664 1665 unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const { 1666 switch (Opcode) { 1667 case SystemZ::L: return SystemZ::LT; 1668 case SystemZ::LY: return SystemZ::LT; 1669 case SystemZ::LG: return SystemZ::LTG; 1670 case SystemZ::LGF: return SystemZ::LTGF; 1671 case SystemZ::LR: return SystemZ::LTR; 1672 case SystemZ::LGFR: return SystemZ::LTGFR; 1673 case SystemZ::LGR: return SystemZ::LTGR; 1674 case SystemZ::LER: return SystemZ::LTEBR; 1675 case SystemZ::LDR: return SystemZ::LTDBR; 1676 case SystemZ::LXR: return SystemZ::LTXBR; 1677 case SystemZ::LCDFR: return SystemZ::LCDBR; 1678 case SystemZ::LPDFR: return SystemZ::LPDBR; 1679 case SystemZ::LNDFR: return SystemZ::LNDBR; 1680 case SystemZ::LCDFR_32: return SystemZ::LCEBR; 1681 case SystemZ::LPDFR_32: return SystemZ::LPEBR; 1682 case SystemZ::LNDFR_32: return SystemZ::LNEBR; 1683 // On zEC12 we prefer to use RISBGN. But if there is a chance to 1684 // actually use the condition code, we may turn it back into RISGB. 1685 // Note that RISBG is not really a "load-and-test" instruction, 1686 // but sets the same condition code values, so is OK to use here. 1687 case SystemZ::RISBGN: return SystemZ::RISBG; 1688 default: return 0; 1689 } 1690 } 1691 1692 // Return true if Mask matches the regexp 0*1+0*, given that zero masks 1693 // have already been filtered out. Store the first set bit in LSB and 1694 // the number of set bits in Length if so. 1695 static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) { 1696 unsigned First = findFirstSet(Mask); 1697 uint64_t Top = (Mask >> First) + 1; 1698 if ((Top & -Top) == Top) { 1699 LSB = First; 1700 Length = findFirstSet(Top); 1701 return true; 1702 } 1703 return false; 1704 } 1705 1706 bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize, 1707 unsigned &Start, unsigned &End) const { 1708 // Reject trivial all-zero masks. 1709 Mask &= allOnes(BitSize); 1710 if (Mask == 0) 1711 return false; 1712 1713 // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of 1714 // the msb and End specifies the index of the lsb. 1715 unsigned LSB, Length; 1716 if (isStringOfOnes(Mask, LSB, Length)) { 1717 Start = 63 - (LSB + Length - 1); 1718 End = 63 - LSB; 1719 return true; 1720 } 1721 1722 // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb 1723 // of the low 1s and End specifies the lsb of the high 1s. 1724 if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) { 1725 assert(LSB > 0 && "Bottom bit must be set"); 1726 assert(LSB + Length < BitSize && "Top bit must be set"); 1727 Start = 63 - (LSB - 1); 1728 End = 63 - (LSB + Length); 1729 return true; 1730 } 1731 1732 return false; 1733 } 1734 1735 unsigned SystemZInstrInfo::getFusedCompare(unsigned Opcode, 1736 SystemZII::FusedCompareType Type, 1737 const MachineInstr *MI) const { 1738 switch (Opcode) { 1739 case SystemZ::CHI: 1740 case SystemZ::CGHI: 1741 if (!(MI && isInt<8>(MI->getOperand(1).getImm()))) 1742 return 0; 1743 break; 1744 case SystemZ::CLFI: 1745 case SystemZ::CLGFI: 1746 if (!(MI && isUInt<8>(MI->getOperand(1).getImm()))) 1747 return 0; 1748 break; 1749 case SystemZ::CL: 1750 case SystemZ::CLG: 1751 if (!STI.hasMiscellaneousExtensions()) 1752 return 0; 1753 if (!(MI && MI->getOperand(3).getReg() == 0)) 1754 return 0; 1755 break; 1756 } 1757 switch (Type) { 1758 case SystemZII::CompareAndBranch: 1759 switch (Opcode) { 1760 case SystemZ::CR: 1761 return SystemZ::CRJ; 1762 case SystemZ::CGR: 1763 return SystemZ::CGRJ; 1764 case SystemZ::CHI: 1765 return SystemZ::CIJ; 1766 case SystemZ::CGHI: 1767 return SystemZ::CGIJ; 1768 case SystemZ::CLR: 1769 return SystemZ::CLRJ; 1770 case SystemZ::CLGR: 1771 return SystemZ::CLGRJ; 1772 case SystemZ::CLFI: 1773 return SystemZ::CLIJ; 1774 case SystemZ::CLGFI: 1775 return SystemZ::CLGIJ; 1776 default: 1777 return 0; 1778 } 1779 case SystemZII::CompareAndReturn: 1780 switch (Opcode) { 1781 case SystemZ::CR: 1782 return SystemZ::CRBReturn; 1783 case SystemZ::CGR: 1784 return SystemZ::CGRBReturn; 1785 case SystemZ::CHI: 1786 return SystemZ::CIBReturn; 1787 case SystemZ::CGHI: 1788 return SystemZ::CGIBReturn; 1789 case SystemZ::CLR: 1790 return SystemZ::CLRBReturn; 1791 case SystemZ::CLGR: 1792 return SystemZ::CLGRBReturn; 1793 case SystemZ::CLFI: 1794 return SystemZ::CLIBReturn; 1795 case SystemZ::CLGFI: 1796 return SystemZ::CLGIBReturn; 1797 default: 1798 return 0; 1799 } 1800 case SystemZII::CompareAndSibcall: 1801 switch (Opcode) { 1802 case SystemZ::CR: 1803 return SystemZ::CRBCall; 1804 case SystemZ::CGR: 1805 return SystemZ::CGRBCall; 1806 case SystemZ::CHI: 1807 return SystemZ::CIBCall; 1808 case SystemZ::CGHI: 1809 return SystemZ::CGIBCall; 1810 case SystemZ::CLR: 1811 return SystemZ::CLRBCall; 1812 case SystemZ::CLGR: 1813 return SystemZ::CLGRBCall; 1814 case SystemZ::CLFI: 1815 return SystemZ::CLIBCall; 1816 case SystemZ::CLGFI: 1817 return SystemZ::CLGIBCall; 1818 default: 1819 return 0; 1820 } 1821 case SystemZII::CompareAndTrap: 1822 switch (Opcode) { 1823 case SystemZ::CR: 1824 return SystemZ::CRT; 1825 case SystemZ::CGR: 1826 return SystemZ::CGRT; 1827 case SystemZ::CHI: 1828 return SystemZ::CIT; 1829 case SystemZ::CGHI: 1830 return SystemZ::CGIT; 1831 case SystemZ::CLR: 1832 return SystemZ::CLRT; 1833 case SystemZ::CLGR: 1834 return SystemZ::CLGRT; 1835 case SystemZ::CLFI: 1836 return SystemZ::CLFIT; 1837 case SystemZ::CLGFI: 1838 return SystemZ::CLGIT; 1839 case SystemZ::CL: 1840 return SystemZ::CLT; 1841 case SystemZ::CLG: 1842 return SystemZ::CLGT; 1843 default: 1844 return 0; 1845 } 1846 } 1847 return 0; 1848 } 1849 1850 bool SystemZInstrInfo:: 1851 prepareCompareSwapOperands(MachineBasicBlock::iterator const MBBI) const { 1852 assert(MBBI->isCompare() && MBBI->getOperand(0).isReg() && 1853 MBBI->getOperand(1).isReg() && !MBBI->mayLoad() && 1854 "Not a compare reg/reg."); 1855 1856 MachineBasicBlock *MBB = MBBI->getParent(); 1857 bool CCLive = true; 1858 SmallVector<MachineInstr *, 4> CCUsers; 1859 for (MachineBasicBlock::iterator Itr = std::next(MBBI); 1860 Itr != MBB->end(); ++Itr) { 1861 if (Itr->readsRegister(SystemZ::CC)) { 1862 unsigned Flags = Itr->getDesc().TSFlags; 1863 if ((Flags & SystemZII::CCMaskFirst) || (Flags & SystemZII::CCMaskLast)) 1864 CCUsers.push_back(&*Itr); 1865 else 1866 return false; 1867 } 1868 if (Itr->definesRegister(SystemZ::CC)) { 1869 CCLive = false; 1870 break; 1871 } 1872 } 1873 if (CCLive) { 1874 LivePhysRegs LiveRegs(*MBB->getParent()->getSubtarget().getRegisterInfo()); 1875 LiveRegs.addLiveOuts(*MBB); 1876 if (LiveRegs.contains(SystemZ::CC)) 1877 return false; 1878 } 1879 1880 // Update all CC users. 1881 for (unsigned Idx = 0; Idx < CCUsers.size(); ++Idx) { 1882 unsigned Flags = CCUsers[Idx]->getDesc().TSFlags; 1883 unsigned FirstOpNum = ((Flags & SystemZII::CCMaskFirst) ? 1884 0 : CCUsers[Idx]->getNumExplicitOperands() - 2); 1885 MachineOperand &CCMaskMO = CCUsers[Idx]->getOperand(FirstOpNum + 1); 1886 unsigned NewCCMask = SystemZ::reverseCCMask(CCMaskMO.getImm()); 1887 CCMaskMO.setImm(NewCCMask); 1888 } 1889 1890 return true; 1891 } 1892 1893 unsigned SystemZ::reverseCCMask(unsigned CCMask) { 1894 return ((CCMask & SystemZ::CCMASK_CMP_EQ) | 1895 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) | 1896 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) | 1897 (CCMask & SystemZ::CCMASK_CMP_UO)); 1898 } 1899 1900 MachineBasicBlock *SystemZ::emitBlockAfter(MachineBasicBlock *MBB) { 1901 MachineFunction &MF = *MBB->getParent(); 1902 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock()); 1903 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB); 1904 return NewMBB; 1905 } 1906 1907 MachineBasicBlock *SystemZ::splitBlockAfter(MachineBasicBlock::iterator MI, 1908 MachineBasicBlock *MBB) { 1909 MachineBasicBlock *NewMBB = emitBlockAfter(MBB); 1910 NewMBB->splice(NewMBB->begin(), MBB, 1911 std::next(MachineBasicBlock::iterator(MI)), MBB->end()); 1912 NewMBB->transferSuccessorsAndUpdatePHIs(MBB); 1913 return NewMBB; 1914 } 1915 1916 MachineBasicBlock *SystemZ::splitBlockBefore(MachineBasicBlock::iterator MI, 1917 MachineBasicBlock *MBB) { 1918 MachineBasicBlock *NewMBB = emitBlockAfter(MBB); 1919 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end()); 1920 NewMBB->transferSuccessorsAndUpdatePHIs(MBB); 1921 return NewMBB; 1922 } 1923 1924 unsigned SystemZInstrInfo::getLoadAndTrap(unsigned Opcode) const { 1925 if (!STI.hasLoadAndTrap()) 1926 return 0; 1927 switch (Opcode) { 1928 case SystemZ::L: 1929 case SystemZ::LY: 1930 return SystemZ::LAT; 1931 case SystemZ::LG: 1932 return SystemZ::LGAT; 1933 case SystemZ::LFH: 1934 return SystemZ::LFHAT; 1935 case SystemZ::LLGF: 1936 return SystemZ::LLGFAT; 1937 case SystemZ::LLGT: 1938 return SystemZ::LLGTAT; 1939 } 1940 return 0; 1941 } 1942 1943 void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB, 1944 MachineBasicBlock::iterator MBBI, 1945 unsigned Reg, uint64_t Value) const { 1946 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 1947 unsigned Opcode = 0; 1948 if (isInt<16>(Value)) 1949 Opcode = SystemZ::LGHI; 1950 else if (SystemZ::isImmLL(Value)) 1951 Opcode = SystemZ::LLILL; 1952 else if (SystemZ::isImmLH(Value)) { 1953 Opcode = SystemZ::LLILH; 1954 Value >>= 16; 1955 } 1956 else if (isInt<32>(Value)) 1957 Opcode = SystemZ::LGFI; 1958 if (Opcode) { 1959 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value); 1960 return; 1961 } 1962 1963 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 1964 assert (MRI.isSSA() && "Huge values only handled before reg-alloc ."); 1965 Register Reg0 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass); 1966 Register Reg1 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass); 1967 BuildMI(MBB, MBBI, DL, get(SystemZ::IMPLICIT_DEF), Reg0); 1968 BuildMI(MBB, MBBI, DL, get(SystemZ::IIHF64), Reg1) 1969 .addReg(Reg0).addImm(Value >> 32); 1970 BuildMI(MBB, MBBI, DL, get(SystemZ::IILF64), Reg) 1971 .addReg(Reg1).addImm(Value & ((uint64_t(1) << 32) - 1)); 1972 } 1973 1974 bool SystemZInstrInfo::verifyInstruction(const MachineInstr &MI, 1975 StringRef &ErrInfo) const { 1976 const MCInstrDesc &MCID = MI.getDesc(); 1977 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) { 1978 if (I >= MCID.getNumOperands()) 1979 break; 1980 const MachineOperand &Op = MI.getOperand(I); 1981 const MCOperandInfo &MCOI = MCID.OpInfo[I]; 1982 // Addressing modes have register and immediate operands. Op should be a 1983 // register (or frame index) operand if MCOI.RegClass contains a valid 1984 // register class, or an immediate otherwise. 1985 if (MCOI.OperandType == MCOI::OPERAND_MEMORY && 1986 ((MCOI.RegClass != -1 && !Op.isReg() && !Op.isFI()) || 1987 (MCOI.RegClass == -1 && !Op.isImm()))) { 1988 ErrInfo = "Addressing mode operands corrupt!"; 1989 return false; 1990 } 1991 } 1992 1993 return true; 1994 } 1995 1996 bool SystemZInstrInfo:: 1997 areMemAccessesTriviallyDisjoint(const MachineInstr &MIa, 1998 const MachineInstr &MIb) const { 1999 2000 if (!MIa.hasOneMemOperand() || !MIb.hasOneMemOperand()) 2001 return false; 2002 2003 // If mem-operands show that the same address Value is used by both 2004 // instructions, check for non-overlapping offsets and widths. Not 2005 // sure if a register based analysis would be an improvement... 2006 2007 MachineMemOperand *MMOa = *MIa.memoperands_begin(); 2008 MachineMemOperand *MMOb = *MIb.memoperands_begin(); 2009 const Value *VALa = MMOa->getValue(); 2010 const Value *VALb = MMOb->getValue(); 2011 bool SameVal = (VALa && VALb && (VALa == VALb)); 2012 if (!SameVal) { 2013 const PseudoSourceValue *PSVa = MMOa->getPseudoValue(); 2014 const PseudoSourceValue *PSVb = MMOb->getPseudoValue(); 2015 if (PSVa && PSVb && (PSVa == PSVb)) 2016 SameVal = true; 2017 } 2018 if (SameVal) { 2019 int OffsetA = MMOa->getOffset(), OffsetB = MMOb->getOffset(); 2020 int WidthA = MMOa->getSize(), WidthB = MMOb->getSize(); 2021 int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB; 2022 int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA; 2023 int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB; 2024 if (LowOffset + LowWidth <= HighOffset) 2025 return true; 2026 } 2027 2028 return false; 2029 } 2030