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