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 (unsigned I = 2; I < MI.getNumOperands(); ++I) 207 MIB.add(MI.getOperand(I)); 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::Trap || 678 Opcode == SystemZ::CallJG || 679 Opcode == SystemZ::CallBR) 680 return true; 681 return false; 682 } 683 684 bool SystemZInstrInfo:: 685 isProfitableToIfCvt(MachineBasicBlock &MBB, 686 unsigned NumCycles, unsigned ExtraPredCycles, 687 BranchProbability Probability) const { 688 // Avoid using conditional returns at the end of a loop (since then 689 // we'd need to emit an unconditional branch to the beginning anyway, 690 // making the loop body longer). This doesn't apply for low-probability 691 // loops (eg. compare-and-swap retry), so just decide based on branch 692 // probability instead of looping structure. 693 // However, since Compare and Trap instructions cost the same as a regular 694 // Compare instruction, we should allow the if conversion to convert this 695 // into a Conditional Compare regardless of the branch probability. 696 if (MBB.getLastNonDebugInstr()->getOpcode() != SystemZ::Trap && 697 MBB.succ_empty() && Probability < BranchProbability(1, 8)) 698 return false; 699 // For now only convert single instructions. 700 return NumCycles == 1; 701 } 702 703 bool SystemZInstrInfo:: 704 isProfitableToIfCvt(MachineBasicBlock &TMBB, 705 unsigned NumCyclesT, unsigned ExtraPredCyclesT, 706 MachineBasicBlock &FMBB, 707 unsigned NumCyclesF, unsigned ExtraPredCyclesF, 708 BranchProbability Probability) const { 709 // For now avoid converting mutually-exclusive cases. 710 return false; 711 } 712 713 bool SystemZInstrInfo:: 714 isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles, 715 BranchProbability Probability) const { 716 // For now only duplicate single instructions. 717 return NumCycles == 1; 718 } 719 720 bool SystemZInstrInfo::PredicateInstruction( 721 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const { 722 assert(Pred.size() == 2 && "Invalid condition"); 723 unsigned CCValid = Pred[0].getImm(); 724 unsigned CCMask = Pred[1].getImm(); 725 assert(CCMask > 0 && CCMask < 15 && "Invalid predicate"); 726 unsigned Opcode = MI.getOpcode(); 727 if (Opcode == SystemZ::Trap) { 728 MI.setDesc(get(SystemZ::CondTrap)); 729 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 730 .addImm(CCValid).addImm(CCMask) 731 .addReg(SystemZ::CC, RegState::Implicit); 732 return true; 733 } 734 if (Opcode == SystemZ::Return) { 735 MI.setDesc(get(SystemZ::CondReturn)); 736 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 737 .addImm(CCValid).addImm(CCMask) 738 .addReg(SystemZ::CC, RegState::Implicit); 739 return true; 740 } 741 if (Opcode == SystemZ::CallJG) { 742 MachineOperand FirstOp = MI.getOperand(0); 743 const uint32_t *RegMask = MI.getOperand(1).getRegMask(); 744 MI.RemoveOperand(1); 745 MI.RemoveOperand(0); 746 MI.setDesc(get(SystemZ::CallBRCL)); 747 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 748 .addImm(CCValid) 749 .addImm(CCMask) 750 .add(FirstOp) 751 .addRegMask(RegMask) 752 .addReg(SystemZ::CC, RegState::Implicit); 753 return true; 754 } 755 if (Opcode == SystemZ::CallBR) { 756 MachineOperand Target = MI.getOperand(0); 757 const uint32_t *RegMask = MI.getOperand(1).getRegMask(); 758 MI.RemoveOperand(1); 759 MI.RemoveOperand(0); 760 MI.setDesc(get(SystemZ::CallBCR)); 761 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 762 .addImm(CCValid).addImm(CCMask) 763 .add(Target) 764 .addRegMask(RegMask) 765 .addReg(SystemZ::CC, RegState::Implicit); 766 return true; 767 } 768 return false; 769 } 770 771 void SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 772 MachineBasicBlock::iterator MBBI, 773 const DebugLoc &DL, MCRegister DestReg, 774 MCRegister SrcReg, bool KillSrc) const { 775 // Split 128-bit GPR moves into two 64-bit moves. Add implicit uses of the 776 // super register in case one of the subregs is undefined. 777 // This handles ADDR128 too. 778 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) { 779 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64), 780 RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc); 781 MachineInstrBuilder(*MBB.getParent(), std::prev(MBBI)) 782 .addReg(SrcReg, RegState::Implicit); 783 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64), 784 RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc); 785 MachineInstrBuilder(*MBB.getParent(), std::prev(MBBI)) 786 .addReg(SrcReg, (getKillRegState(KillSrc) | RegState::Implicit)); 787 return; 788 } 789 790 if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) { 791 emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc, 792 false); 793 return; 794 } 795 796 // Move 128-bit floating-point values between VR128 and FP128. 797 if (SystemZ::VR128BitRegClass.contains(DestReg) && 798 SystemZ::FP128BitRegClass.contains(SrcReg)) { 799 MCRegister SrcRegHi = 800 RI.getMatchingSuperReg(RI.getSubReg(SrcReg, SystemZ::subreg_h64), 801 SystemZ::subreg_h64, &SystemZ::VR128BitRegClass); 802 MCRegister SrcRegLo = 803 RI.getMatchingSuperReg(RI.getSubReg(SrcReg, SystemZ::subreg_l64), 804 SystemZ::subreg_h64, &SystemZ::VR128BitRegClass); 805 806 BuildMI(MBB, MBBI, DL, get(SystemZ::VMRHG), DestReg) 807 .addReg(SrcRegHi, getKillRegState(KillSrc)) 808 .addReg(SrcRegLo, getKillRegState(KillSrc)); 809 return; 810 } 811 if (SystemZ::FP128BitRegClass.contains(DestReg) && 812 SystemZ::VR128BitRegClass.contains(SrcReg)) { 813 MCRegister DestRegHi = 814 RI.getMatchingSuperReg(RI.getSubReg(DestReg, SystemZ::subreg_h64), 815 SystemZ::subreg_h64, &SystemZ::VR128BitRegClass); 816 MCRegister DestRegLo = 817 RI.getMatchingSuperReg(RI.getSubReg(DestReg, SystemZ::subreg_l64), 818 SystemZ::subreg_h64, &SystemZ::VR128BitRegClass); 819 820 if (DestRegHi != SrcReg) 821 copyPhysReg(MBB, MBBI, DL, DestRegHi, SrcReg, false); 822 BuildMI(MBB, MBBI, DL, get(SystemZ::VREPG), DestRegLo) 823 .addReg(SrcReg, getKillRegState(KillSrc)).addImm(1); 824 return; 825 } 826 827 // Move CC value from a GR32. 828 if (DestReg == SystemZ::CC) { 829 unsigned Opcode = 830 SystemZ::GR32BitRegClass.contains(SrcReg) ? SystemZ::TMLH : SystemZ::TMHH; 831 BuildMI(MBB, MBBI, DL, get(Opcode)) 832 .addReg(SrcReg, getKillRegState(KillSrc)) 833 .addImm(3 << (SystemZ::IPM_CC - 16)); 834 return; 835 } 836 837 // Everything else needs only one instruction. 838 unsigned Opcode; 839 if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg)) 840 Opcode = SystemZ::LGR; 841 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg)) 842 // For z13 we prefer LDR over LER to avoid partial register dependencies. 843 Opcode = STI.hasVector() ? SystemZ::LDR32 : SystemZ::LER; 844 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg)) 845 Opcode = SystemZ::LDR; 846 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg)) 847 Opcode = SystemZ::LXR; 848 else if (SystemZ::VR32BitRegClass.contains(DestReg, SrcReg)) 849 Opcode = SystemZ::VLR32; 850 else if (SystemZ::VR64BitRegClass.contains(DestReg, SrcReg)) 851 Opcode = SystemZ::VLR64; 852 else if (SystemZ::VR128BitRegClass.contains(DestReg, SrcReg)) 853 Opcode = SystemZ::VLR; 854 else if (SystemZ::AR32BitRegClass.contains(DestReg, SrcReg)) 855 Opcode = SystemZ::CPYA; 856 else 857 llvm_unreachable("Impossible reg-to-reg copy"); 858 859 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg) 860 .addReg(SrcReg, getKillRegState(KillSrc)); 861 } 862 863 void SystemZInstrInfo::storeRegToStackSlot( 864 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg, 865 bool isKill, int FrameIdx, const TargetRegisterClass *RC, 866 const TargetRegisterInfo *TRI) const { 867 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 868 869 // Callers may expect a single instruction, so keep 128-bit moves 870 // together for now and lower them after register allocation. 871 unsigned LoadOpcode, StoreOpcode; 872 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode); 873 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode)) 874 .addReg(SrcReg, getKillRegState(isKill)), 875 FrameIdx); 876 } 877 878 void SystemZInstrInfo::loadRegFromStackSlot( 879 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register DestReg, 880 int FrameIdx, const TargetRegisterClass *RC, 881 const TargetRegisterInfo *TRI) const { 882 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 883 884 // Callers may expect a single instruction, so keep 128-bit moves 885 // together for now and lower them after register allocation. 886 unsigned LoadOpcode, StoreOpcode; 887 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode); 888 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg), 889 FrameIdx); 890 } 891 892 // Return true if MI is a simple load or store with a 12-bit displacement 893 // and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores. 894 static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) { 895 const MCInstrDesc &MCID = MI->getDesc(); 896 return ((MCID.TSFlags & Flag) && 897 isUInt<12>(MI->getOperand(2).getImm()) && 898 MI->getOperand(3).getReg() == 0); 899 } 900 901 namespace { 902 903 struct LogicOp { 904 LogicOp() = default; 905 LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize) 906 : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {} 907 908 explicit operator bool() const { return RegSize; } 909 910 unsigned RegSize = 0; 911 unsigned ImmLSB = 0; 912 unsigned ImmSize = 0; 913 }; 914 915 } // end anonymous namespace 916 917 static LogicOp interpretAndImmediate(unsigned Opcode) { 918 switch (Opcode) { 919 case SystemZ::NILMux: return LogicOp(32, 0, 16); 920 case SystemZ::NIHMux: return LogicOp(32, 16, 16); 921 case SystemZ::NILL64: return LogicOp(64, 0, 16); 922 case SystemZ::NILH64: return LogicOp(64, 16, 16); 923 case SystemZ::NIHL64: return LogicOp(64, 32, 16); 924 case SystemZ::NIHH64: return LogicOp(64, 48, 16); 925 case SystemZ::NIFMux: return LogicOp(32, 0, 32); 926 case SystemZ::NILF64: return LogicOp(64, 0, 32); 927 case SystemZ::NIHF64: return LogicOp(64, 32, 32); 928 default: return LogicOp(); 929 } 930 } 931 932 static void transferDeadCC(MachineInstr *OldMI, MachineInstr *NewMI) { 933 if (OldMI->registerDefIsDead(SystemZ::CC)) { 934 MachineOperand *CCDef = NewMI->findRegisterDefOperand(SystemZ::CC); 935 if (CCDef != nullptr) 936 CCDef->setIsDead(true); 937 } 938 } 939 940 static void transferMIFlag(MachineInstr *OldMI, MachineInstr *NewMI, 941 MachineInstr::MIFlag Flag) { 942 if (OldMI->getFlag(Flag)) 943 NewMI->setFlag(Flag); 944 } 945 946 MachineInstr *SystemZInstrInfo::convertToThreeAddress(MachineInstr &MI, 947 LiveVariables *LV) const { 948 MachineBasicBlock *MBB = MI.getParent(); 949 950 // Try to convert an AND into an RISBG-type instruction. 951 // TODO: It might be beneficial to select RISBG and shorten to AND instead. 952 if (LogicOp And = interpretAndImmediate(MI.getOpcode())) { 953 uint64_t Imm = MI.getOperand(2).getImm() << And.ImmLSB; 954 // AND IMMEDIATE leaves the other bits of the register unchanged. 955 Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB); 956 unsigned Start, End; 957 if (isRxSBGMask(Imm, And.RegSize, Start, End)) { 958 unsigned NewOpcode; 959 if (And.RegSize == 64) { 960 NewOpcode = SystemZ::RISBG; 961 // Prefer RISBGN if available, since it does not clobber CC. 962 if (STI.hasMiscellaneousExtensions()) 963 NewOpcode = SystemZ::RISBGN; 964 } else { 965 NewOpcode = SystemZ::RISBMux; 966 Start &= 31; 967 End &= 31; 968 } 969 MachineOperand &Dest = MI.getOperand(0); 970 MachineOperand &Src = MI.getOperand(1); 971 MachineInstrBuilder MIB = 972 BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpcode)) 973 .add(Dest) 974 .addReg(0) 975 .addReg(Src.getReg(), getKillRegState(Src.isKill()), 976 Src.getSubReg()) 977 .addImm(Start) 978 .addImm(End + 128) 979 .addImm(0); 980 if (LV) { 981 unsigned NumOps = MI.getNumOperands(); 982 for (unsigned I = 1; I < NumOps; ++I) { 983 MachineOperand &Op = MI.getOperand(I); 984 if (Op.isReg() && Op.isKill()) 985 LV->replaceKillInstruction(Op.getReg(), MI, *MIB); 986 } 987 } 988 transferDeadCC(&MI, MIB); 989 return MIB; 990 } 991 } 992 return nullptr; 993 } 994 995 MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl( 996 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops, 997 MachineBasicBlock::iterator InsertPt, int FrameIndex, 998 LiveIntervals *LIS, VirtRegMap *VRM) const { 999 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 1000 MachineRegisterInfo &MRI = MF.getRegInfo(); 1001 const MachineFrameInfo &MFI = MF.getFrameInfo(); 1002 unsigned Size = MFI.getObjectSize(FrameIndex); 1003 unsigned Opcode = MI.getOpcode(); 1004 1005 // Check CC liveness if new instruction introduces a dead def of CC. 1006 MCRegUnitIterator CCUnit(MCRegister::from(SystemZ::CC), TRI); 1007 SlotIndex MISlot = SlotIndex(); 1008 LiveRange *CCLiveRange = nullptr; 1009 bool CCLiveAtMI = true; 1010 if (LIS) { 1011 MISlot = LIS->getSlotIndexes()->getInstructionIndex(MI).getRegSlot(); 1012 CCLiveRange = &LIS->getRegUnit(*CCUnit); 1013 CCLiveAtMI = CCLiveRange->liveAt(MISlot); 1014 } 1015 ++CCUnit; 1016 assert(!CCUnit.isValid() && "CC only has one reg unit."); 1017 1018 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) { 1019 if (!CCLiveAtMI && (Opcode == SystemZ::LA || Opcode == SystemZ::LAY) && 1020 isInt<8>(MI.getOperand(2).getImm()) && !MI.getOperand(3).getReg()) { 1021 // LA(Y) %reg, CONST(%reg) -> AGSI %mem, CONST 1022 MachineInstr *BuiltMI = BuildMI(*InsertPt->getParent(), InsertPt, 1023 MI.getDebugLoc(), get(SystemZ::AGSI)) 1024 .addFrameIndex(FrameIndex) 1025 .addImm(0) 1026 .addImm(MI.getOperand(2).getImm()); 1027 BuiltMI->findRegisterDefOperand(SystemZ::CC)->setIsDead(true); 1028 CCLiveRange->createDeadDef(MISlot, LIS->getVNInfoAllocator()); 1029 return BuiltMI; 1030 } 1031 return nullptr; 1032 } 1033 1034 // All other cases require a single operand. 1035 if (Ops.size() != 1) 1036 return nullptr; 1037 1038 unsigned OpNum = Ops[0]; 1039 assert(Size * 8 == 1040 TRI->getRegSizeInBits(*MF.getRegInfo() 1041 .getRegClass(MI.getOperand(OpNum).getReg())) && 1042 "Invalid size combination"); 1043 1044 if ((Opcode == SystemZ::AHI || Opcode == SystemZ::AGHI) && OpNum == 0 && 1045 isInt<8>(MI.getOperand(2).getImm())) { 1046 // A(G)HI %reg, CONST -> A(G)SI %mem, CONST 1047 Opcode = (Opcode == SystemZ::AHI ? SystemZ::ASI : SystemZ::AGSI); 1048 MachineInstr *BuiltMI = 1049 BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode)) 1050 .addFrameIndex(FrameIndex) 1051 .addImm(0) 1052 .addImm(MI.getOperand(2).getImm()); 1053 transferDeadCC(&MI, BuiltMI); 1054 transferMIFlag(&MI, BuiltMI, MachineInstr::NoSWrap); 1055 return BuiltMI; 1056 } 1057 1058 if ((Opcode == SystemZ::ALFI && OpNum == 0 && 1059 isInt<8>((int32_t)MI.getOperand(2).getImm())) || 1060 (Opcode == SystemZ::ALGFI && OpNum == 0 && 1061 isInt<8>((int64_t)MI.getOperand(2).getImm()))) { 1062 // AL(G)FI %reg, CONST -> AL(G)SI %mem, CONST 1063 Opcode = (Opcode == SystemZ::ALFI ? SystemZ::ALSI : SystemZ::ALGSI); 1064 MachineInstr *BuiltMI = 1065 BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode)) 1066 .addFrameIndex(FrameIndex) 1067 .addImm(0) 1068 .addImm((int8_t)MI.getOperand(2).getImm()); 1069 transferDeadCC(&MI, BuiltMI); 1070 return BuiltMI; 1071 } 1072 1073 if ((Opcode == SystemZ::SLFI && OpNum == 0 && 1074 isInt<8>((int32_t)-MI.getOperand(2).getImm())) || 1075 (Opcode == SystemZ::SLGFI && OpNum == 0 && 1076 isInt<8>((int64_t)-MI.getOperand(2).getImm()))) { 1077 // SL(G)FI %reg, CONST -> AL(G)SI %mem, -CONST 1078 Opcode = (Opcode == SystemZ::SLFI ? SystemZ::ALSI : SystemZ::ALGSI); 1079 MachineInstr *BuiltMI = 1080 BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode)) 1081 .addFrameIndex(FrameIndex) 1082 .addImm(0) 1083 .addImm((int8_t)-MI.getOperand(2).getImm()); 1084 transferDeadCC(&MI, BuiltMI); 1085 return BuiltMI; 1086 } 1087 1088 unsigned MemImmOpc = 0; 1089 switch (Opcode) { 1090 case SystemZ::LHIMux: 1091 case SystemZ::LHI: MemImmOpc = SystemZ::MVHI; break; 1092 case SystemZ::LGHI: MemImmOpc = SystemZ::MVGHI; break; 1093 case SystemZ::CHIMux: 1094 case SystemZ::CHI: MemImmOpc = SystemZ::CHSI; break; 1095 case SystemZ::CGHI: MemImmOpc = SystemZ::CGHSI; break; 1096 case SystemZ::CLFIMux: 1097 case SystemZ::CLFI: 1098 if (isUInt<16>(MI.getOperand(1).getImm())) 1099 MemImmOpc = SystemZ::CLFHSI; 1100 break; 1101 case SystemZ::CLGFI: 1102 if (isUInt<16>(MI.getOperand(1).getImm())) 1103 MemImmOpc = SystemZ::CLGHSI; 1104 break; 1105 default: break; 1106 } 1107 if (MemImmOpc) 1108 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), 1109 get(MemImmOpc)) 1110 .addFrameIndex(FrameIndex) 1111 .addImm(0) 1112 .addImm(MI.getOperand(1).getImm()); 1113 1114 if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) { 1115 bool Op0IsGPR = (Opcode == SystemZ::LGDR); 1116 bool Op1IsGPR = (Opcode == SystemZ::LDGR); 1117 // If we're spilling the destination of an LDGR or LGDR, store the 1118 // source register instead. 1119 if (OpNum == 0) { 1120 unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD; 1121 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), 1122 get(StoreOpcode)) 1123 .add(MI.getOperand(1)) 1124 .addFrameIndex(FrameIndex) 1125 .addImm(0) 1126 .addReg(0); 1127 } 1128 // If we're spilling the source of an LDGR or LGDR, load the 1129 // destination register instead. 1130 if (OpNum == 1) { 1131 unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD; 1132 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), 1133 get(LoadOpcode)) 1134 .add(MI.getOperand(0)) 1135 .addFrameIndex(FrameIndex) 1136 .addImm(0) 1137 .addReg(0); 1138 } 1139 } 1140 1141 // Look for cases where the source of a simple store or the destination 1142 // of a simple load is being spilled. Try to use MVC instead. 1143 // 1144 // Although MVC is in practice a fast choice in these cases, it is still 1145 // logically a bytewise copy. This means that we cannot use it if the 1146 // load or store is volatile. We also wouldn't be able to use MVC if 1147 // the two memories partially overlap, but that case cannot occur here, 1148 // because we know that one of the memories is a full frame index. 1149 // 1150 // For performance reasons, we also want to avoid using MVC if the addresses 1151 // might be equal. We don't worry about that case here, because spill slot 1152 // coloring happens later, and because we have special code to remove 1153 // MVCs that turn out to be redundant. 1154 if (OpNum == 0 && MI.hasOneMemOperand()) { 1155 MachineMemOperand *MMO = *MI.memoperands_begin(); 1156 if (MMO->getSize() == Size && !MMO->isVolatile() && !MMO->isAtomic()) { 1157 // Handle conversion of loads. 1158 if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXLoad)) { 1159 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), 1160 get(SystemZ::MVC)) 1161 .addFrameIndex(FrameIndex) 1162 .addImm(0) 1163 .addImm(Size) 1164 .add(MI.getOperand(1)) 1165 .addImm(MI.getOperand(2).getImm()) 1166 .addMemOperand(MMO); 1167 } 1168 // Handle conversion of stores. 1169 if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXStore)) { 1170 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), 1171 get(SystemZ::MVC)) 1172 .add(MI.getOperand(1)) 1173 .addImm(MI.getOperand(2).getImm()) 1174 .addImm(Size) 1175 .addFrameIndex(FrameIndex) 1176 .addImm(0) 1177 .addMemOperand(MMO); 1178 } 1179 } 1180 } 1181 1182 // If the spilled operand is the final one or the instruction is 1183 // commutable, try to change <INSN>R into <INSN>. Don't introduce a def of 1184 // CC if it is live and MI does not define it. 1185 unsigned NumOps = MI.getNumExplicitOperands(); 1186 int MemOpcode = SystemZ::getMemOpcode(Opcode); 1187 if (MemOpcode == -1 || 1188 (CCLiveAtMI && !MI.definesRegister(SystemZ::CC) && 1189 get(MemOpcode).hasImplicitDefOfPhysReg(SystemZ::CC))) 1190 return nullptr; 1191 1192 // Check if all other vregs have a usable allocation in the case of vector 1193 // to FP conversion. 1194 const MCInstrDesc &MCID = MI.getDesc(); 1195 for (unsigned I = 0, E = MCID.getNumOperands(); I != E; ++I) { 1196 const MCOperandInfo &MCOI = MCID.OpInfo[I]; 1197 if (MCOI.OperandType != MCOI::OPERAND_REGISTER || I == OpNum) 1198 continue; 1199 const TargetRegisterClass *RC = TRI->getRegClass(MCOI.RegClass); 1200 if (RC == &SystemZ::VR32BitRegClass || RC == &SystemZ::VR64BitRegClass) { 1201 Register Reg = MI.getOperand(I).getReg(); 1202 Register PhysReg = Register::isVirtualRegister(Reg) 1203 ? (VRM ? Register(VRM->getPhys(Reg)) : Register()) 1204 : Reg; 1205 if (!PhysReg || 1206 !(SystemZ::FP32BitRegClass.contains(PhysReg) || 1207 SystemZ::FP64BitRegClass.contains(PhysReg) || 1208 SystemZ::VF128BitRegClass.contains(PhysReg))) 1209 return nullptr; 1210 } 1211 } 1212 // Fused multiply and add/sub need to have the same dst and accumulator reg. 1213 bool FusedFPOp = (Opcode == SystemZ::WFMADB || Opcode == SystemZ::WFMASB || 1214 Opcode == SystemZ::WFMSDB || Opcode == SystemZ::WFMSSB); 1215 if (FusedFPOp) { 1216 Register DstReg = VRM->getPhys(MI.getOperand(0).getReg()); 1217 Register AccReg = VRM->getPhys(MI.getOperand(3).getReg()); 1218 if (OpNum == 0 || OpNum == 3 || DstReg != AccReg) 1219 return nullptr; 1220 } 1221 1222 // Try to swap compare operands if possible. 1223 bool NeedsCommute = false; 1224 if ((MI.getOpcode() == SystemZ::CR || MI.getOpcode() == SystemZ::CGR || 1225 MI.getOpcode() == SystemZ::CLR || MI.getOpcode() == SystemZ::CLGR || 1226 MI.getOpcode() == SystemZ::WFCDB || MI.getOpcode() == SystemZ::WFCSB || 1227 MI.getOpcode() == SystemZ::WFKDB || MI.getOpcode() == SystemZ::WFKSB) && 1228 OpNum == 0 && prepareCompareSwapOperands(MI)) 1229 NeedsCommute = true; 1230 1231 bool CCOperands = false; 1232 if (MI.getOpcode() == SystemZ::LOCRMux || MI.getOpcode() == SystemZ::LOCGR || 1233 MI.getOpcode() == SystemZ::SELRMux || MI.getOpcode() == SystemZ::SELGR) { 1234 assert(MI.getNumOperands() == 6 && NumOps == 5 && 1235 "LOCR/SELR instruction operands corrupt?"); 1236 NumOps -= 2; 1237 CCOperands = true; 1238 } 1239 1240 // See if this is a 3-address instruction that is convertible to 2-address 1241 // and suitable for folding below. Only try this with virtual registers 1242 // and a provided VRM (during regalloc). 1243 if (NumOps == 3 && SystemZ::getTargetMemOpcode(MemOpcode) != -1) { 1244 if (VRM == nullptr) 1245 return nullptr; 1246 else { 1247 Register DstReg = MI.getOperand(0).getReg(); 1248 Register DstPhys = 1249 (Register::isVirtualRegister(DstReg) ? Register(VRM->getPhys(DstReg)) 1250 : DstReg); 1251 Register SrcReg = (OpNum == 2 ? MI.getOperand(1).getReg() 1252 : ((OpNum == 1 && MI.isCommutable()) 1253 ? MI.getOperand(2).getReg() 1254 : Register())); 1255 if (DstPhys && !SystemZ::GRH32BitRegClass.contains(DstPhys) && SrcReg && 1256 Register::isVirtualRegister(SrcReg) && 1257 DstPhys == VRM->getPhys(SrcReg)) 1258 NeedsCommute = (OpNum == 1); 1259 else 1260 return nullptr; 1261 } 1262 } 1263 1264 if ((OpNum == NumOps - 1) || NeedsCommute || FusedFPOp) { 1265 const MCInstrDesc &MemDesc = get(MemOpcode); 1266 uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags); 1267 assert(AccessBytes != 0 && "Size of access should be known"); 1268 assert(AccessBytes <= Size && "Access outside the frame index"); 1269 uint64_t Offset = Size - AccessBytes; 1270 MachineInstrBuilder MIB = BuildMI(*InsertPt->getParent(), InsertPt, 1271 MI.getDebugLoc(), get(MemOpcode)); 1272 if (MI.isCompare()) { 1273 assert(NumOps == 2 && "Expected 2 register operands for a compare."); 1274 MIB.add(MI.getOperand(NeedsCommute ? 1 : 0)); 1275 } 1276 else if (FusedFPOp) { 1277 MIB.add(MI.getOperand(0)); 1278 MIB.add(MI.getOperand(3)); 1279 MIB.add(MI.getOperand(OpNum == 1 ? 2 : 1)); 1280 } 1281 else { 1282 MIB.add(MI.getOperand(0)); 1283 if (NeedsCommute) 1284 MIB.add(MI.getOperand(2)); 1285 else 1286 for (unsigned I = 1; I < OpNum; ++I) 1287 MIB.add(MI.getOperand(I)); 1288 } 1289 MIB.addFrameIndex(FrameIndex).addImm(Offset); 1290 if (MemDesc.TSFlags & SystemZII::HasIndex) 1291 MIB.addReg(0); 1292 if (CCOperands) { 1293 unsigned CCValid = MI.getOperand(NumOps).getImm(); 1294 unsigned CCMask = MI.getOperand(NumOps + 1).getImm(); 1295 MIB.addImm(CCValid); 1296 MIB.addImm(NeedsCommute ? CCMask ^ CCValid : CCMask); 1297 } 1298 if (MIB->definesRegister(SystemZ::CC) && 1299 (!MI.definesRegister(SystemZ::CC) || 1300 MI.registerDefIsDead(SystemZ::CC))) { 1301 MIB->addRegisterDead(SystemZ::CC, TRI); 1302 if (CCLiveRange) 1303 CCLiveRange->createDeadDef(MISlot, LIS->getVNInfoAllocator()); 1304 } 1305 // Constrain the register classes if converted from a vector opcode. The 1306 // allocated regs are in an FP reg-class per previous check above. 1307 for (const MachineOperand &MO : MIB->operands()) 1308 if (MO.isReg() && Register::isVirtualRegister(MO.getReg())) { 1309 unsigned Reg = MO.getReg(); 1310 if (MRI.getRegClass(Reg) == &SystemZ::VR32BitRegClass) 1311 MRI.setRegClass(Reg, &SystemZ::FP32BitRegClass); 1312 else if (MRI.getRegClass(Reg) == &SystemZ::VR64BitRegClass) 1313 MRI.setRegClass(Reg, &SystemZ::FP64BitRegClass); 1314 else if (MRI.getRegClass(Reg) == &SystemZ::VR128BitRegClass) 1315 MRI.setRegClass(Reg, &SystemZ::VF128BitRegClass); 1316 } 1317 1318 transferDeadCC(&MI, MIB); 1319 transferMIFlag(&MI, MIB, MachineInstr::NoSWrap); 1320 transferMIFlag(&MI, MIB, MachineInstr::NoFPExcept); 1321 return MIB; 1322 } 1323 1324 return nullptr; 1325 } 1326 1327 MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl( 1328 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops, 1329 MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI, 1330 LiveIntervals *LIS) const { 1331 return nullptr; 1332 } 1333 1334 bool SystemZInstrInfo::expandPostRAPseudo(MachineInstr &MI) const { 1335 switch (MI.getOpcode()) { 1336 case SystemZ::L128: 1337 splitMove(MI, SystemZ::LG); 1338 return true; 1339 1340 case SystemZ::ST128: 1341 splitMove(MI, SystemZ::STG); 1342 return true; 1343 1344 case SystemZ::LX: 1345 splitMove(MI, SystemZ::LD); 1346 return true; 1347 1348 case SystemZ::STX: 1349 splitMove(MI, SystemZ::STD); 1350 return true; 1351 1352 case SystemZ::LBMux: 1353 expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH); 1354 return true; 1355 1356 case SystemZ::LHMux: 1357 expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH); 1358 return true; 1359 1360 case SystemZ::LLCRMux: 1361 expandZExtPseudo(MI, SystemZ::LLCR, 8); 1362 return true; 1363 1364 case SystemZ::LLHRMux: 1365 expandZExtPseudo(MI, SystemZ::LLHR, 16); 1366 return true; 1367 1368 case SystemZ::LLCMux: 1369 expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH); 1370 return true; 1371 1372 case SystemZ::LLHMux: 1373 expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH); 1374 return true; 1375 1376 case SystemZ::LMux: 1377 expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH); 1378 return true; 1379 1380 case SystemZ::LOCMux: 1381 expandLOCPseudo(MI, SystemZ::LOC, SystemZ::LOCFH); 1382 return true; 1383 1384 case SystemZ::LOCHIMux: 1385 expandLOCPseudo(MI, SystemZ::LOCHI, SystemZ::LOCHHI); 1386 return true; 1387 1388 case SystemZ::STCMux: 1389 expandRXYPseudo(MI, SystemZ::STC, SystemZ::STCH); 1390 return true; 1391 1392 case SystemZ::STHMux: 1393 expandRXYPseudo(MI, SystemZ::STH, SystemZ::STHH); 1394 return true; 1395 1396 case SystemZ::STMux: 1397 expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH); 1398 return true; 1399 1400 case SystemZ::STOCMux: 1401 expandLOCPseudo(MI, SystemZ::STOC, SystemZ::STOCFH); 1402 return true; 1403 1404 case SystemZ::LHIMux: 1405 expandRIPseudo(MI, SystemZ::LHI, SystemZ::IIHF, true); 1406 return true; 1407 1408 case SystemZ::IIFMux: 1409 expandRIPseudo(MI, SystemZ::IILF, SystemZ::IIHF, false); 1410 return true; 1411 1412 case SystemZ::IILMux: 1413 expandRIPseudo(MI, SystemZ::IILL, SystemZ::IIHL, false); 1414 return true; 1415 1416 case SystemZ::IIHMux: 1417 expandRIPseudo(MI, SystemZ::IILH, SystemZ::IIHH, false); 1418 return true; 1419 1420 case SystemZ::NIFMux: 1421 expandRIPseudo(MI, SystemZ::NILF, SystemZ::NIHF, false); 1422 return true; 1423 1424 case SystemZ::NILMux: 1425 expandRIPseudo(MI, SystemZ::NILL, SystemZ::NIHL, false); 1426 return true; 1427 1428 case SystemZ::NIHMux: 1429 expandRIPseudo(MI, SystemZ::NILH, SystemZ::NIHH, false); 1430 return true; 1431 1432 case SystemZ::OIFMux: 1433 expandRIPseudo(MI, SystemZ::OILF, SystemZ::OIHF, false); 1434 return true; 1435 1436 case SystemZ::OILMux: 1437 expandRIPseudo(MI, SystemZ::OILL, SystemZ::OIHL, false); 1438 return true; 1439 1440 case SystemZ::OIHMux: 1441 expandRIPseudo(MI, SystemZ::OILH, SystemZ::OIHH, false); 1442 return true; 1443 1444 case SystemZ::XIFMux: 1445 expandRIPseudo(MI, SystemZ::XILF, SystemZ::XIHF, false); 1446 return true; 1447 1448 case SystemZ::TMLMux: 1449 expandRIPseudo(MI, SystemZ::TMLL, SystemZ::TMHL, false); 1450 return true; 1451 1452 case SystemZ::TMHMux: 1453 expandRIPseudo(MI, SystemZ::TMLH, SystemZ::TMHH, false); 1454 return true; 1455 1456 case SystemZ::AHIMux: 1457 expandRIPseudo(MI, SystemZ::AHI, SystemZ::AIH, false); 1458 return true; 1459 1460 case SystemZ::AHIMuxK: 1461 expandRIEPseudo(MI, SystemZ::AHI, SystemZ::AHIK, SystemZ::AIH); 1462 return true; 1463 1464 case SystemZ::AFIMux: 1465 expandRIPseudo(MI, SystemZ::AFI, SystemZ::AIH, false); 1466 return true; 1467 1468 case SystemZ::CHIMux: 1469 expandRIPseudo(MI, SystemZ::CHI, SystemZ::CIH, false); 1470 return true; 1471 1472 case SystemZ::CFIMux: 1473 expandRIPseudo(MI, SystemZ::CFI, SystemZ::CIH, false); 1474 return true; 1475 1476 case SystemZ::CLFIMux: 1477 expandRIPseudo(MI, SystemZ::CLFI, SystemZ::CLIH, false); 1478 return true; 1479 1480 case SystemZ::CMux: 1481 expandRXYPseudo(MI, SystemZ::C, SystemZ::CHF); 1482 return true; 1483 1484 case SystemZ::CLMux: 1485 expandRXYPseudo(MI, SystemZ::CL, SystemZ::CLHF); 1486 return true; 1487 1488 case SystemZ::RISBMux: { 1489 bool DestIsHigh = SystemZ::isHighReg(MI.getOperand(0).getReg()); 1490 bool SrcIsHigh = SystemZ::isHighReg(MI.getOperand(2).getReg()); 1491 if (SrcIsHigh == DestIsHigh) 1492 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHH : SystemZ::RISBLL)); 1493 else { 1494 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHL : SystemZ::RISBLH)); 1495 MI.getOperand(5).setImm(MI.getOperand(5).getImm() ^ 32); 1496 } 1497 return true; 1498 } 1499 1500 case SystemZ::ADJDYNALLOC: 1501 splitAdjDynAlloc(MI); 1502 return true; 1503 1504 case TargetOpcode::LOAD_STACK_GUARD: 1505 expandLoadStackGuard(&MI); 1506 return true; 1507 1508 default: 1509 return false; 1510 } 1511 } 1512 1513 unsigned SystemZInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { 1514 if (MI.isInlineAsm()) { 1515 const MachineFunction *MF = MI.getParent()->getParent(); 1516 const char *AsmStr = MI.getOperand(0).getSymbolName(); 1517 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); 1518 } 1519 else if (MI.getOpcode() == SystemZ::PATCHPOINT) 1520 return PatchPointOpers(&MI).getNumPatchBytes(); 1521 else if (MI.getOpcode() == SystemZ::STACKMAP) 1522 return MI.getOperand(1).getImm(); 1523 else if (MI.getOpcode() == SystemZ::FENTRY_CALL) 1524 return 6; 1525 1526 return MI.getDesc().getSize(); 1527 } 1528 1529 SystemZII::Branch 1530 SystemZInstrInfo::getBranchInfo(const MachineInstr &MI) const { 1531 switch (MI.getOpcode()) { 1532 case SystemZ::BR: 1533 case SystemZ::BI: 1534 case SystemZ::J: 1535 case SystemZ::JG: 1536 return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY, 1537 SystemZ::CCMASK_ANY, &MI.getOperand(0)); 1538 1539 case SystemZ::BRC: 1540 case SystemZ::BRCL: 1541 return SystemZII::Branch(SystemZII::BranchNormal, MI.getOperand(0).getImm(), 1542 MI.getOperand(1).getImm(), &MI.getOperand(2)); 1543 1544 case SystemZ::BRCT: 1545 case SystemZ::BRCTH: 1546 return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP, 1547 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2)); 1548 1549 case SystemZ::BRCTG: 1550 return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP, 1551 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2)); 1552 1553 case SystemZ::CIJ: 1554 case SystemZ::CRJ: 1555 return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP, 1556 MI.getOperand(2).getImm(), &MI.getOperand(3)); 1557 1558 case SystemZ::CLIJ: 1559 case SystemZ::CLRJ: 1560 return SystemZII::Branch(SystemZII::BranchCL, SystemZ::CCMASK_ICMP, 1561 MI.getOperand(2).getImm(), &MI.getOperand(3)); 1562 1563 case SystemZ::CGIJ: 1564 case SystemZ::CGRJ: 1565 return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP, 1566 MI.getOperand(2).getImm(), &MI.getOperand(3)); 1567 1568 case SystemZ::CLGIJ: 1569 case SystemZ::CLGRJ: 1570 return SystemZII::Branch(SystemZII::BranchCLG, SystemZ::CCMASK_ICMP, 1571 MI.getOperand(2).getImm(), &MI.getOperand(3)); 1572 1573 case SystemZ::INLINEASM_BR: 1574 // Don't try to analyze asm goto, so pass nullptr as branch target argument. 1575 return SystemZII::Branch(SystemZII::AsmGoto, 0, 0, nullptr); 1576 1577 default: 1578 llvm_unreachable("Unrecognized branch opcode"); 1579 } 1580 } 1581 1582 void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC, 1583 unsigned &LoadOpcode, 1584 unsigned &StoreOpcode) const { 1585 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) { 1586 LoadOpcode = SystemZ::L; 1587 StoreOpcode = SystemZ::ST; 1588 } else if (RC == &SystemZ::GRH32BitRegClass) { 1589 LoadOpcode = SystemZ::LFH; 1590 StoreOpcode = SystemZ::STFH; 1591 } else if (RC == &SystemZ::GRX32BitRegClass) { 1592 LoadOpcode = SystemZ::LMux; 1593 StoreOpcode = SystemZ::STMux; 1594 } else if (RC == &SystemZ::GR64BitRegClass || 1595 RC == &SystemZ::ADDR64BitRegClass) { 1596 LoadOpcode = SystemZ::LG; 1597 StoreOpcode = SystemZ::STG; 1598 } else if (RC == &SystemZ::GR128BitRegClass || 1599 RC == &SystemZ::ADDR128BitRegClass) { 1600 LoadOpcode = SystemZ::L128; 1601 StoreOpcode = SystemZ::ST128; 1602 } else if (RC == &SystemZ::FP32BitRegClass) { 1603 LoadOpcode = SystemZ::LE; 1604 StoreOpcode = SystemZ::STE; 1605 } else if (RC == &SystemZ::FP64BitRegClass) { 1606 LoadOpcode = SystemZ::LD; 1607 StoreOpcode = SystemZ::STD; 1608 } else if (RC == &SystemZ::FP128BitRegClass) { 1609 LoadOpcode = SystemZ::LX; 1610 StoreOpcode = SystemZ::STX; 1611 } else if (RC == &SystemZ::VR32BitRegClass) { 1612 LoadOpcode = SystemZ::VL32; 1613 StoreOpcode = SystemZ::VST32; 1614 } else if (RC == &SystemZ::VR64BitRegClass) { 1615 LoadOpcode = SystemZ::VL64; 1616 StoreOpcode = SystemZ::VST64; 1617 } else if (RC == &SystemZ::VF128BitRegClass || 1618 RC == &SystemZ::VR128BitRegClass) { 1619 LoadOpcode = SystemZ::VL; 1620 StoreOpcode = SystemZ::VST; 1621 } else 1622 llvm_unreachable("Unsupported regclass to load or store"); 1623 } 1624 1625 unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode, 1626 int64_t Offset) const { 1627 const MCInstrDesc &MCID = get(Opcode); 1628 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset); 1629 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) { 1630 // Get the instruction to use for unsigned 12-bit displacements. 1631 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode); 1632 if (Disp12Opcode >= 0) 1633 return Disp12Opcode; 1634 1635 // All address-related instructions can use unsigned 12-bit 1636 // displacements. 1637 return Opcode; 1638 } 1639 if (isInt<20>(Offset) && isInt<20>(Offset2)) { 1640 // Get the instruction to use for signed 20-bit displacements. 1641 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode); 1642 if (Disp20Opcode >= 0) 1643 return Disp20Opcode; 1644 1645 // Check whether Opcode allows signed 20-bit displacements. 1646 if (MCID.TSFlags & SystemZII::Has20BitOffset) 1647 return Opcode; 1648 } 1649 return 0; 1650 } 1651 1652 unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const { 1653 switch (Opcode) { 1654 case SystemZ::L: return SystemZ::LT; 1655 case SystemZ::LY: return SystemZ::LT; 1656 case SystemZ::LG: return SystemZ::LTG; 1657 case SystemZ::LGF: return SystemZ::LTGF; 1658 case SystemZ::LR: return SystemZ::LTR; 1659 case SystemZ::LGFR: return SystemZ::LTGFR; 1660 case SystemZ::LGR: return SystemZ::LTGR; 1661 case SystemZ::LER: return SystemZ::LTEBR; 1662 case SystemZ::LDR: return SystemZ::LTDBR; 1663 case SystemZ::LXR: return SystemZ::LTXBR; 1664 case SystemZ::LCDFR: return SystemZ::LCDBR; 1665 case SystemZ::LPDFR: return SystemZ::LPDBR; 1666 case SystemZ::LNDFR: return SystemZ::LNDBR; 1667 case SystemZ::LCDFR_32: return SystemZ::LCEBR; 1668 case SystemZ::LPDFR_32: return SystemZ::LPEBR; 1669 case SystemZ::LNDFR_32: return SystemZ::LNEBR; 1670 // On zEC12 we prefer to use RISBGN. But if there is a chance to 1671 // actually use the condition code, we may turn it back into RISGB. 1672 // Note that RISBG is not really a "load-and-test" instruction, 1673 // but sets the same condition code values, so is OK to use here. 1674 case SystemZ::RISBGN: return SystemZ::RISBG; 1675 default: return 0; 1676 } 1677 } 1678 1679 // Return true if Mask matches the regexp 0*1+0*, given that zero masks 1680 // have already been filtered out. Store the first set bit in LSB and 1681 // the number of set bits in Length if so. 1682 static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) { 1683 unsigned First = findFirstSet(Mask); 1684 uint64_t Top = (Mask >> First) + 1; 1685 if ((Top & -Top) == Top) { 1686 LSB = First; 1687 Length = findFirstSet(Top); 1688 return true; 1689 } 1690 return false; 1691 } 1692 1693 bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize, 1694 unsigned &Start, unsigned &End) const { 1695 // Reject trivial all-zero masks. 1696 Mask &= allOnes(BitSize); 1697 if (Mask == 0) 1698 return false; 1699 1700 // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of 1701 // the msb and End specifies the index of the lsb. 1702 unsigned LSB, Length; 1703 if (isStringOfOnes(Mask, LSB, Length)) { 1704 Start = 63 - (LSB + Length - 1); 1705 End = 63 - LSB; 1706 return true; 1707 } 1708 1709 // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb 1710 // of the low 1s and End specifies the lsb of the high 1s. 1711 if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) { 1712 assert(LSB > 0 && "Bottom bit must be set"); 1713 assert(LSB + Length < BitSize && "Top bit must be set"); 1714 Start = 63 - (LSB - 1); 1715 End = 63 - (LSB + Length); 1716 return true; 1717 } 1718 1719 return false; 1720 } 1721 1722 unsigned SystemZInstrInfo::getFusedCompare(unsigned Opcode, 1723 SystemZII::FusedCompareType Type, 1724 const MachineInstr *MI) const { 1725 switch (Opcode) { 1726 case SystemZ::CHI: 1727 case SystemZ::CGHI: 1728 if (!(MI && isInt<8>(MI->getOperand(1).getImm()))) 1729 return 0; 1730 break; 1731 case SystemZ::CLFI: 1732 case SystemZ::CLGFI: 1733 if (!(MI && isUInt<8>(MI->getOperand(1).getImm()))) 1734 return 0; 1735 break; 1736 case SystemZ::CL: 1737 case SystemZ::CLG: 1738 if (!STI.hasMiscellaneousExtensions()) 1739 return 0; 1740 if (!(MI && MI->getOperand(3).getReg() == 0)) 1741 return 0; 1742 break; 1743 } 1744 switch (Type) { 1745 case SystemZII::CompareAndBranch: 1746 switch (Opcode) { 1747 case SystemZ::CR: 1748 return SystemZ::CRJ; 1749 case SystemZ::CGR: 1750 return SystemZ::CGRJ; 1751 case SystemZ::CHI: 1752 return SystemZ::CIJ; 1753 case SystemZ::CGHI: 1754 return SystemZ::CGIJ; 1755 case SystemZ::CLR: 1756 return SystemZ::CLRJ; 1757 case SystemZ::CLGR: 1758 return SystemZ::CLGRJ; 1759 case SystemZ::CLFI: 1760 return SystemZ::CLIJ; 1761 case SystemZ::CLGFI: 1762 return SystemZ::CLGIJ; 1763 default: 1764 return 0; 1765 } 1766 case SystemZII::CompareAndReturn: 1767 switch (Opcode) { 1768 case SystemZ::CR: 1769 return SystemZ::CRBReturn; 1770 case SystemZ::CGR: 1771 return SystemZ::CGRBReturn; 1772 case SystemZ::CHI: 1773 return SystemZ::CIBReturn; 1774 case SystemZ::CGHI: 1775 return SystemZ::CGIBReturn; 1776 case SystemZ::CLR: 1777 return SystemZ::CLRBReturn; 1778 case SystemZ::CLGR: 1779 return SystemZ::CLGRBReturn; 1780 case SystemZ::CLFI: 1781 return SystemZ::CLIBReturn; 1782 case SystemZ::CLGFI: 1783 return SystemZ::CLGIBReturn; 1784 default: 1785 return 0; 1786 } 1787 case SystemZII::CompareAndSibcall: 1788 switch (Opcode) { 1789 case SystemZ::CR: 1790 return SystemZ::CRBCall; 1791 case SystemZ::CGR: 1792 return SystemZ::CGRBCall; 1793 case SystemZ::CHI: 1794 return SystemZ::CIBCall; 1795 case SystemZ::CGHI: 1796 return SystemZ::CGIBCall; 1797 case SystemZ::CLR: 1798 return SystemZ::CLRBCall; 1799 case SystemZ::CLGR: 1800 return SystemZ::CLGRBCall; 1801 case SystemZ::CLFI: 1802 return SystemZ::CLIBCall; 1803 case SystemZ::CLGFI: 1804 return SystemZ::CLGIBCall; 1805 default: 1806 return 0; 1807 } 1808 case SystemZII::CompareAndTrap: 1809 switch (Opcode) { 1810 case SystemZ::CR: 1811 return SystemZ::CRT; 1812 case SystemZ::CGR: 1813 return SystemZ::CGRT; 1814 case SystemZ::CHI: 1815 return SystemZ::CIT; 1816 case SystemZ::CGHI: 1817 return SystemZ::CGIT; 1818 case SystemZ::CLR: 1819 return SystemZ::CLRT; 1820 case SystemZ::CLGR: 1821 return SystemZ::CLGRT; 1822 case SystemZ::CLFI: 1823 return SystemZ::CLFIT; 1824 case SystemZ::CLGFI: 1825 return SystemZ::CLGIT; 1826 case SystemZ::CL: 1827 return SystemZ::CLT; 1828 case SystemZ::CLG: 1829 return SystemZ::CLGT; 1830 default: 1831 return 0; 1832 } 1833 } 1834 return 0; 1835 } 1836 1837 bool SystemZInstrInfo:: 1838 prepareCompareSwapOperands(MachineBasicBlock::iterator const MBBI) const { 1839 assert(MBBI->isCompare() && MBBI->getOperand(0).isReg() && 1840 MBBI->getOperand(1).isReg() && !MBBI->mayLoad() && 1841 "Not a compare reg/reg."); 1842 1843 MachineBasicBlock *MBB = MBBI->getParent(); 1844 bool CCLive = true; 1845 SmallVector<MachineInstr *, 4> CCUsers; 1846 for (MachineBasicBlock::iterator Itr = std::next(MBBI); 1847 Itr != MBB->end(); ++Itr) { 1848 if (Itr->readsRegister(SystemZ::CC)) { 1849 unsigned Flags = Itr->getDesc().TSFlags; 1850 if ((Flags & SystemZII::CCMaskFirst) || (Flags & SystemZII::CCMaskLast)) 1851 CCUsers.push_back(&*Itr); 1852 else 1853 return false; 1854 } 1855 if (Itr->definesRegister(SystemZ::CC)) { 1856 CCLive = false; 1857 break; 1858 } 1859 } 1860 if (CCLive) { 1861 LivePhysRegs LiveRegs(*MBB->getParent()->getSubtarget().getRegisterInfo()); 1862 LiveRegs.addLiveOuts(*MBB); 1863 if (LiveRegs.contains(SystemZ::CC)) 1864 return false; 1865 } 1866 1867 // Update all CC users. 1868 for (unsigned Idx = 0; Idx < CCUsers.size(); ++Idx) { 1869 unsigned Flags = CCUsers[Idx]->getDesc().TSFlags; 1870 unsigned FirstOpNum = ((Flags & SystemZII::CCMaskFirst) ? 1871 0 : CCUsers[Idx]->getNumExplicitOperands() - 2); 1872 MachineOperand &CCMaskMO = CCUsers[Idx]->getOperand(FirstOpNum + 1); 1873 unsigned NewCCMask = SystemZ::reverseCCMask(CCMaskMO.getImm()); 1874 CCMaskMO.setImm(NewCCMask); 1875 } 1876 1877 return true; 1878 } 1879 1880 unsigned SystemZ::reverseCCMask(unsigned CCMask) { 1881 return ((CCMask & SystemZ::CCMASK_CMP_EQ) | 1882 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) | 1883 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) | 1884 (CCMask & SystemZ::CCMASK_CMP_UO)); 1885 } 1886 1887 MachineBasicBlock *SystemZ::emitBlockAfter(MachineBasicBlock *MBB) { 1888 MachineFunction &MF = *MBB->getParent(); 1889 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock()); 1890 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB); 1891 return NewMBB; 1892 } 1893 1894 MachineBasicBlock *SystemZ::splitBlockAfter(MachineBasicBlock::iterator MI, 1895 MachineBasicBlock *MBB) { 1896 MachineBasicBlock *NewMBB = emitBlockAfter(MBB); 1897 NewMBB->splice(NewMBB->begin(), MBB, 1898 std::next(MachineBasicBlock::iterator(MI)), MBB->end()); 1899 NewMBB->transferSuccessorsAndUpdatePHIs(MBB); 1900 return NewMBB; 1901 } 1902 1903 MachineBasicBlock *SystemZ::splitBlockBefore(MachineBasicBlock::iterator MI, 1904 MachineBasicBlock *MBB) { 1905 MachineBasicBlock *NewMBB = emitBlockAfter(MBB); 1906 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end()); 1907 NewMBB->transferSuccessorsAndUpdatePHIs(MBB); 1908 return NewMBB; 1909 } 1910 1911 unsigned SystemZInstrInfo::getLoadAndTrap(unsigned Opcode) const { 1912 if (!STI.hasLoadAndTrap()) 1913 return 0; 1914 switch (Opcode) { 1915 case SystemZ::L: 1916 case SystemZ::LY: 1917 return SystemZ::LAT; 1918 case SystemZ::LG: 1919 return SystemZ::LGAT; 1920 case SystemZ::LFH: 1921 return SystemZ::LFHAT; 1922 case SystemZ::LLGF: 1923 return SystemZ::LLGFAT; 1924 case SystemZ::LLGT: 1925 return SystemZ::LLGTAT; 1926 } 1927 return 0; 1928 } 1929 1930 void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB, 1931 MachineBasicBlock::iterator MBBI, 1932 unsigned Reg, uint64_t Value) const { 1933 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 1934 unsigned Opcode = 0; 1935 if (isInt<16>(Value)) 1936 Opcode = SystemZ::LGHI; 1937 else if (SystemZ::isImmLL(Value)) 1938 Opcode = SystemZ::LLILL; 1939 else if (SystemZ::isImmLH(Value)) { 1940 Opcode = SystemZ::LLILH; 1941 Value >>= 16; 1942 } 1943 else if (isInt<32>(Value)) 1944 Opcode = SystemZ::LGFI; 1945 if (Opcode) { 1946 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value); 1947 return; 1948 } 1949 1950 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 1951 assert (MRI.isSSA() && "Huge values only handled before reg-alloc ."); 1952 Register Reg0 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass); 1953 Register Reg1 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass); 1954 BuildMI(MBB, MBBI, DL, get(SystemZ::IMPLICIT_DEF), Reg0); 1955 BuildMI(MBB, MBBI, DL, get(SystemZ::IIHF64), Reg1) 1956 .addReg(Reg0).addImm(Value >> 32); 1957 BuildMI(MBB, MBBI, DL, get(SystemZ::IILF64), Reg) 1958 .addReg(Reg1).addImm(Value & ((uint64_t(1) << 32) - 1)); 1959 } 1960 1961 bool SystemZInstrInfo::verifyInstruction(const MachineInstr &MI, 1962 StringRef &ErrInfo) const { 1963 const MCInstrDesc &MCID = MI.getDesc(); 1964 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) { 1965 if (I >= MCID.getNumOperands()) 1966 break; 1967 const MachineOperand &Op = MI.getOperand(I); 1968 const MCOperandInfo &MCOI = MCID.OpInfo[I]; 1969 // Addressing modes have register and immediate operands. Op should be a 1970 // register (or frame index) operand if MCOI.RegClass contains a valid 1971 // register class, or an immediate otherwise. 1972 if (MCOI.OperandType == MCOI::OPERAND_MEMORY && 1973 ((MCOI.RegClass != -1 && !Op.isReg() && !Op.isFI()) || 1974 (MCOI.RegClass == -1 && !Op.isImm()))) { 1975 ErrInfo = "Addressing mode operands corrupt!"; 1976 return false; 1977 } 1978 } 1979 1980 return true; 1981 } 1982 1983 bool SystemZInstrInfo:: 1984 areMemAccessesTriviallyDisjoint(const MachineInstr &MIa, 1985 const MachineInstr &MIb) const { 1986 1987 if (!MIa.hasOneMemOperand() || !MIb.hasOneMemOperand()) 1988 return false; 1989 1990 // If mem-operands show that the same address Value is used by both 1991 // instructions, check for non-overlapping offsets and widths. Not 1992 // sure if a register based analysis would be an improvement... 1993 1994 MachineMemOperand *MMOa = *MIa.memoperands_begin(); 1995 MachineMemOperand *MMOb = *MIb.memoperands_begin(); 1996 const Value *VALa = MMOa->getValue(); 1997 const Value *VALb = MMOb->getValue(); 1998 bool SameVal = (VALa && VALb && (VALa == VALb)); 1999 if (!SameVal) { 2000 const PseudoSourceValue *PSVa = MMOa->getPseudoValue(); 2001 const PseudoSourceValue *PSVb = MMOb->getPseudoValue(); 2002 if (PSVa && PSVb && (PSVa == PSVb)) 2003 SameVal = true; 2004 } 2005 if (SameVal) { 2006 int OffsetA = MMOa->getOffset(), OffsetB = MMOb->getOffset(); 2007 int WidthA = MMOa->getSize(), WidthB = MMOb->getSize(); 2008 int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB; 2009 int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA; 2010 int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB; 2011 if (LowOffset + LowWidth <= HighOffset) 2012 return true; 2013 } 2014 2015 return false; 2016 } 2017