1 //===-- TargetInstrInfo.cpp - Target 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 implements the TargetInstrInfo class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/TargetInstrInfo.h" 15 #include "llvm/CodeGen/MachineFrameInfo.h" 16 #include "llvm/CodeGen/MachineInstrBuilder.h" 17 #include "llvm/CodeGen/MachineMemOperand.h" 18 #include "llvm/CodeGen/MachineRegisterInfo.h" 19 #include "llvm/CodeGen/PseudoSourceValue.h" 20 #include "llvm/CodeGen/ScoreboardHazardRecognizer.h" 21 #include "llvm/CodeGen/StackMaps.h" 22 #include "llvm/CodeGen/TargetFrameLowering.h" 23 #include "llvm/CodeGen/TargetLowering.h" 24 #include "llvm/CodeGen/TargetRegisterInfo.h" 25 #include "llvm/CodeGen/TargetSchedule.h" 26 #include "llvm/IR/DataLayout.h" 27 #include "llvm/MC/MCAsmInfo.h" 28 #include "llvm/MC/MCInstrItineraries.h" 29 #include "llvm/Support/CommandLine.h" 30 #include "llvm/Support/ErrorHandling.h" 31 #include "llvm/Support/raw_ostream.h" 32 #include "llvm/Target/TargetMachine.h" 33 #include <cctype> 34 35 using namespace llvm; 36 37 static cl::opt<bool> DisableHazardRecognizer( 38 "disable-sched-hazard", cl::Hidden, cl::init(false), 39 cl::desc("Disable hazard detection during preRA scheduling")); 40 41 TargetInstrInfo::~TargetInstrInfo() { 42 } 43 44 const TargetRegisterClass* 45 TargetInstrInfo::getRegClass(const MCInstrDesc &MCID, unsigned OpNum, 46 const TargetRegisterInfo *TRI, 47 const MachineFunction &MF) const { 48 if (OpNum >= MCID.getNumOperands()) 49 return nullptr; 50 51 short RegClass = MCID.OpInfo[OpNum].RegClass; 52 if (MCID.OpInfo[OpNum].isLookupPtrRegClass()) 53 return TRI->getPointerRegClass(MF, RegClass); 54 55 // Instructions like INSERT_SUBREG do not have fixed register classes. 56 if (RegClass < 0) 57 return nullptr; 58 59 // Otherwise just look it up normally. 60 return TRI->getRegClass(RegClass); 61 } 62 63 /// insertNoop - Insert a noop into the instruction stream at the specified 64 /// point. 65 void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB, 66 MachineBasicBlock::iterator MI) const { 67 llvm_unreachable("Target didn't implement insertNoop!"); 68 } 69 70 static bool isAsmComment(const char *Str, const MCAsmInfo &MAI) { 71 return strncmp(Str, MAI.getCommentString().data(), 72 MAI.getCommentString().size()) == 0; 73 } 74 75 /// Measure the specified inline asm to determine an approximation of its 76 /// length. 77 /// Comments (which run till the next SeparatorString or newline) do not 78 /// count as an instruction. 79 /// Any other non-whitespace text is considered an instruction, with 80 /// multiple instructions separated by SeparatorString or newlines. 81 /// Variable-length instructions are not handled here; this function 82 /// may be overloaded in the target code to do that. 83 /// We implement a special case of the .space directive which takes only a 84 /// single integer argument in base 10 that is the size in bytes. This is a 85 /// restricted form of the GAS directive in that we only interpret 86 /// simple--i.e. not a logical or arithmetic expression--size values without 87 /// the optional fill value. This is primarily used for creating arbitrary 88 /// sized inline asm blocks for testing purposes. 89 unsigned TargetInstrInfo::getInlineAsmLength(const char *Str, 90 const MCAsmInfo &MAI) const { 91 // Count the number of instructions in the asm. 92 bool AtInsnStart = true; 93 unsigned Length = 0; 94 for (; *Str; ++Str) { 95 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(), 96 strlen(MAI.getSeparatorString())) == 0) { 97 AtInsnStart = true; 98 } else if (isAsmComment(Str, MAI)) { 99 // Stop counting as an instruction after a comment until the next 100 // separator. 101 AtInsnStart = false; 102 } 103 104 if (AtInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) { 105 unsigned AddLength = MAI.getMaxInstLength(); 106 if (strncmp(Str, ".space", 6) == 0) { 107 char *EStr; 108 int SpaceSize; 109 SpaceSize = strtol(Str + 6, &EStr, 10); 110 SpaceSize = SpaceSize < 0 ? 0 : SpaceSize; 111 while (*EStr != '\n' && std::isspace(static_cast<unsigned char>(*EStr))) 112 ++EStr; 113 if (*EStr == '\0' || *EStr == '\n' || 114 isAsmComment(EStr, MAI)) // Successfully parsed .space argument 115 AddLength = SpaceSize; 116 } 117 Length += AddLength; 118 AtInsnStart = false; 119 } 120 } 121 122 return Length; 123 } 124 125 /// ReplaceTailWithBranchTo - Delete the instruction OldInst and everything 126 /// after it, replacing it with an unconditional branch to NewDest. 127 void 128 TargetInstrInfo::ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail, 129 MachineBasicBlock *NewDest) const { 130 MachineBasicBlock *MBB = Tail->getParent(); 131 132 // Remove all the old successors of MBB from the CFG. 133 while (!MBB->succ_empty()) 134 MBB->removeSuccessor(MBB->succ_begin()); 135 136 // Save off the debug loc before erasing the instruction. 137 DebugLoc DL = Tail->getDebugLoc(); 138 139 // Remove all the dead instructions from the end of MBB. 140 MBB->erase(Tail, MBB->end()); 141 142 // If MBB isn't immediately before MBB, insert a branch to it. 143 if (++MachineFunction::iterator(MBB) != MachineFunction::iterator(NewDest)) 144 insertBranch(*MBB, NewDest, nullptr, SmallVector<MachineOperand, 0>(), DL); 145 MBB->addSuccessor(NewDest); 146 } 147 148 MachineInstr *TargetInstrInfo::commuteInstructionImpl(MachineInstr &MI, 149 bool NewMI, unsigned Idx1, 150 unsigned Idx2) const { 151 const MCInstrDesc &MCID = MI.getDesc(); 152 bool HasDef = MCID.getNumDefs(); 153 if (HasDef && !MI.getOperand(0).isReg()) 154 // No idea how to commute this instruction. Target should implement its own. 155 return nullptr; 156 157 unsigned CommutableOpIdx1 = Idx1; (void)CommutableOpIdx1; 158 unsigned CommutableOpIdx2 = Idx2; (void)CommutableOpIdx2; 159 assert(findCommutedOpIndices(MI, CommutableOpIdx1, CommutableOpIdx2) && 160 CommutableOpIdx1 == Idx1 && CommutableOpIdx2 == Idx2 && 161 "TargetInstrInfo::CommuteInstructionImpl(): not commutable operands."); 162 assert(MI.getOperand(Idx1).isReg() && MI.getOperand(Idx2).isReg() && 163 "This only knows how to commute register operands so far"); 164 165 unsigned Reg0 = HasDef ? MI.getOperand(0).getReg() : 0; 166 unsigned Reg1 = MI.getOperand(Idx1).getReg(); 167 unsigned Reg2 = MI.getOperand(Idx2).getReg(); 168 unsigned SubReg0 = HasDef ? MI.getOperand(0).getSubReg() : 0; 169 unsigned SubReg1 = MI.getOperand(Idx1).getSubReg(); 170 unsigned SubReg2 = MI.getOperand(Idx2).getSubReg(); 171 bool Reg1IsKill = MI.getOperand(Idx1).isKill(); 172 bool Reg2IsKill = MI.getOperand(Idx2).isKill(); 173 bool Reg1IsUndef = MI.getOperand(Idx1).isUndef(); 174 bool Reg2IsUndef = MI.getOperand(Idx2).isUndef(); 175 bool Reg1IsInternal = MI.getOperand(Idx1).isInternalRead(); 176 bool Reg2IsInternal = MI.getOperand(Idx2).isInternalRead(); 177 // Avoid calling isRenamable for virtual registers since we assert that 178 // renamable property is only queried/set for physical registers. 179 bool Reg1IsRenamable = TargetRegisterInfo::isPhysicalRegister(Reg1) 180 ? MI.getOperand(Idx1).isRenamable() 181 : false; 182 bool Reg2IsRenamable = TargetRegisterInfo::isPhysicalRegister(Reg2) 183 ? MI.getOperand(Idx2).isRenamable() 184 : false; 185 // If destination is tied to either of the commuted source register, then 186 // it must be updated. 187 if (HasDef && Reg0 == Reg1 && 188 MI.getDesc().getOperandConstraint(Idx1, MCOI::TIED_TO) == 0) { 189 Reg2IsKill = false; 190 Reg0 = Reg2; 191 SubReg0 = SubReg2; 192 } else if (HasDef && Reg0 == Reg2 && 193 MI.getDesc().getOperandConstraint(Idx2, MCOI::TIED_TO) == 0) { 194 Reg1IsKill = false; 195 Reg0 = Reg1; 196 SubReg0 = SubReg1; 197 } 198 199 MachineInstr *CommutedMI = nullptr; 200 if (NewMI) { 201 // Create a new instruction. 202 MachineFunction &MF = *MI.getMF(); 203 CommutedMI = MF.CloneMachineInstr(&MI); 204 } else { 205 CommutedMI = &MI; 206 } 207 208 if (HasDef) { 209 CommutedMI->getOperand(0).setReg(Reg0); 210 CommutedMI->getOperand(0).setSubReg(SubReg0); 211 } 212 CommutedMI->getOperand(Idx2).setReg(Reg1); 213 CommutedMI->getOperand(Idx1).setReg(Reg2); 214 CommutedMI->getOperand(Idx2).setSubReg(SubReg1); 215 CommutedMI->getOperand(Idx1).setSubReg(SubReg2); 216 CommutedMI->getOperand(Idx2).setIsKill(Reg1IsKill); 217 CommutedMI->getOperand(Idx1).setIsKill(Reg2IsKill); 218 CommutedMI->getOperand(Idx2).setIsUndef(Reg1IsUndef); 219 CommutedMI->getOperand(Idx1).setIsUndef(Reg2IsUndef); 220 CommutedMI->getOperand(Idx2).setIsInternalRead(Reg1IsInternal); 221 CommutedMI->getOperand(Idx1).setIsInternalRead(Reg2IsInternal); 222 // Avoid calling setIsRenamable for virtual registers since we assert that 223 // renamable property is only queried/set for physical registers. 224 if (TargetRegisterInfo::isPhysicalRegister(Reg1)) 225 CommutedMI->getOperand(Idx2).setIsRenamable(Reg1IsRenamable); 226 if (TargetRegisterInfo::isPhysicalRegister(Reg2)) 227 CommutedMI->getOperand(Idx1).setIsRenamable(Reg2IsRenamable); 228 return CommutedMI; 229 } 230 231 MachineInstr *TargetInstrInfo::commuteInstruction(MachineInstr &MI, bool NewMI, 232 unsigned OpIdx1, 233 unsigned OpIdx2) const { 234 // If OpIdx1 or OpIdx2 is not specified, then this method is free to choose 235 // any commutable operand, which is done in findCommutedOpIndices() method 236 // called below. 237 if ((OpIdx1 == CommuteAnyOperandIndex || OpIdx2 == CommuteAnyOperandIndex) && 238 !findCommutedOpIndices(MI, OpIdx1, OpIdx2)) { 239 assert(MI.isCommutable() && 240 "Precondition violation: MI must be commutable."); 241 return nullptr; 242 } 243 return commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); 244 } 245 246 bool TargetInstrInfo::fixCommutedOpIndices(unsigned &ResultIdx1, 247 unsigned &ResultIdx2, 248 unsigned CommutableOpIdx1, 249 unsigned CommutableOpIdx2) { 250 if (ResultIdx1 == CommuteAnyOperandIndex && 251 ResultIdx2 == CommuteAnyOperandIndex) { 252 ResultIdx1 = CommutableOpIdx1; 253 ResultIdx2 = CommutableOpIdx2; 254 } else if (ResultIdx1 == CommuteAnyOperandIndex) { 255 if (ResultIdx2 == CommutableOpIdx1) 256 ResultIdx1 = CommutableOpIdx2; 257 else if (ResultIdx2 == CommutableOpIdx2) 258 ResultIdx1 = CommutableOpIdx1; 259 else 260 return false; 261 } else if (ResultIdx2 == CommuteAnyOperandIndex) { 262 if (ResultIdx1 == CommutableOpIdx1) 263 ResultIdx2 = CommutableOpIdx2; 264 else if (ResultIdx1 == CommutableOpIdx2) 265 ResultIdx2 = CommutableOpIdx1; 266 else 267 return false; 268 } else 269 // Check that the result operand indices match the given commutable 270 // operand indices. 271 return (ResultIdx1 == CommutableOpIdx1 && ResultIdx2 == CommutableOpIdx2) || 272 (ResultIdx1 == CommutableOpIdx2 && ResultIdx2 == CommutableOpIdx1); 273 274 return true; 275 } 276 277 bool TargetInstrInfo::findCommutedOpIndices(MachineInstr &MI, 278 unsigned &SrcOpIdx1, 279 unsigned &SrcOpIdx2) const { 280 assert(!MI.isBundle() && 281 "TargetInstrInfo::findCommutedOpIndices() can't handle bundles"); 282 283 const MCInstrDesc &MCID = MI.getDesc(); 284 if (!MCID.isCommutable()) 285 return false; 286 287 // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this 288 // is not true, then the target must implement this. 289 unsigned CommutableOpIdx1 = MCID.getNumDefs(); 290 unsigned CommutableOpIdx2 = CommutableOpIdx1 + 1; 291 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 292 CommutableOpIdx1, CommutableOpIdx2)) 293 return false; 294 295 if (!MI.getOperand(SrcOpIdx1).isReg() || !MI.getOperand(SrcOpIdx2).isReg()) 296 // No idea. 297 return false; 298 return true; 299 } 300 301 bool TargetInstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const { 302 if (!MI.isTerminator()) return false; 303 304 // Conditional branch is a special case. 305 if (MI.isBranch() && !MI.isBarrier()) 306 return true; 307 if (!MI.isPredicable()) 308 return true; 309 return !isPredicated(MI); 310 } 311 312 bool TargetInstrInfo::PredicateInstruction( 313 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const { 314 bool MadeChange = false; 315 316 assert(!MI.isBundle() && 317 "TargetInstrInfo::PredicateInstruction() can't handle bundles"); 318 319 const MCInstrDesc &MCID = MI.getDesc(); 320 if (!MI.isPredicable()) 321 return false; 322 323 for (unsigned j = 0, i = 0, e = MI.getNumOperands(); i != e; ++i) { 324 if (MCID.OpInfo[i].isPredicate()) { 325 MachineOperand &MO = MI.getOperand(i); 326 if (MO.isReg()) { 327 MO.setReg(Pred[j].getReg()); 328 MadeChange = true; 329 } else if (MO.isImm()) { 330 MO.setImm(Pred[j].getImm()); 331 MadeChange = true; 332 } else if (MO.isMBB()) { 333 MO.setMBB(Pred[j].getMBB()); 334 MadeChange = true; 335 } 336 ++j; 337 } 338 } 339 return MadeChange; 340 } 341 342 bool TargetInstrInfo::hasLoadFromStackSlot(const MachineInstr &MI, 343 const MachineMemOperand *&MMO, 344 int &FrameIndex) const { 345 for (MachineInstr::mmo_iterator o = MI.memoperands_begin(), 346 oe = MI.memoperands_end(); 347 o != oe; ++o) { 348 if ((*o)->isLoad()) { 349 if (const FixedStackPseudoSourceValue *Value = 350 dyn_cast_or_null<FixedStackPseudoSourceValue>( 351 (*o)->getPseudoValue())) { 352 FrameIndex = Value->getFrameIndex(); 353 MMO = *o; 354 return true; 355 } 356 } 357 } 358 return false; 359 } 360 361 bool TargetInstrInfo::hasStoreToStackSlot(const MachineInstr &MI, 362 const MachineMemOperand *&MMO, 363 int &FrameIndex) const { 364 for (MachineInstr::mmo_iterator o = MI.memoperands_begin(), 365 oe = MI.memoperands_end(); 366 o != oe; ++o) { 367 if ((*o)->isStore()) { 368 if (const FixedStackPseudoSourceValue *Value = 369 dyn_cast_or_null<FixedStackPseudoSourceValue>( 370 (*o)->getPseudoValue())) { 371 FrameIndex = Value->getFrameIndex(); 372 MMO = *o; 373 return true; 374 } 375 } 376 } 377 return false; 378 } 379 380 bool TargetInstrInfo::getStackSlotRange(const TargetRegisterClass *RC, 381 unsigned SubIdx, unsigned &Size, 382 unsigned &Offset, 383 const MachineFunction &MF) const { 384 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 385 if (!SubIdx) { 386 Size = TRI->getSpillSize(*RC); 387 Offset = 0; 388 return true; 389 } 390 unsigned BitSize = TRI->getSubRegIdxSize(SubIdx); 391 // Convert bit size to byte size. 392 if (BitSize % 8) 393 return false; 394 395 int BitOffset = TRI->getSubRegIdxOffset(SubIdx); 396 if (BitOffset < 0 || BitOffset % 8) 397 return false; 398 399 Size = BitSize /= 8; 400 Offset = (unsigned)BitOffset / 8; 401 402 assert(TRI->getSpillSize(*RC) >= (Offset + Size) && "bad subregister range"); 403 404 if (!MF.getDataLayout().isLittleEndian()) { 405 Offset = TRI->getSpillSize(*RC) - (Offset + Size); 406 } 407 return true; 408 } 409 410 void TargetInstrInfo::reMaterialize(MachineBasicBlock &MBB, 411 MachineBasicBlock::iterator I, 412 unsigned DestReg, unsigned SubIdx, 413 const MachineInstr &Orig, 414 const TargetRegisterInfo &TRI) const { 415 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig); 416 MI->substituteRegister(MI->getOperand(0).getReg(), DestReg, SubIdx, TRI); 417 MBB.insert(I, MI); 418 } 419 420 bool TargetInstrInfo::produceSameValue(const MachineInstr &MI0, 421 const MachineInstr &MI1, 422 const MachineRegisterInfo *MRI) const { 423 return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs); 424 } 425 426 MachineInstr &TargetInstrInfo::duplicate(MachineBasicBlock &MBB, 427 MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) const { 428 assert(!Orig.isNotDuplicable() && "Instruction cannot be duplicated"); 429 MachineFunction &MF = *MBB.getParent(); 430 return MF.CloneMachineInstrBundle(MBB, InsertBefore, Orig); 431 } 432 433 // If the COPY instruction in MI can be folded to a stack operation, return 434 // the register class to use. 435 static const TargetRegisterClass *canFoldCopy(const MachineInstr &MI, 436 unsigned FoldIdx) { 437 assert(MI.isCopy() && "MI must be a COPY instruction"); 438 if (MI.getNumOperands() != 2) 439 return nullptr; 440 assert(FoldIdx<2 && "FoldIdx refers no nonexistent operand"); 441 442 const MachineOperand &FoldOp = MI.getOperand(FoldIdx); 443 const MachineOperand &LiveOp = MI.getOperand(1 - FoldIdx); 444 445 if (FoldOp.getSubReg() || LiveOp.getSubReg()) 446 return nullptr; 447 448 unsigned FoldReg = FoldOp.getReg(); 449 unsigned LiveReg = LiveOp.getReg(); 450 451 assert(TargetRegisterInfo::isVirtualRegister(FoldReg) && 452 "Cannot fold physregs"); 453 454 const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo(); 455 const TargetRegisterClass *RC = MRI.getRegClass(FoldReg); 456 457 if (TargetRegisterInfo::isPhysicalRegister(LiveOp.getReg())) 458 return RC->contains(LiveOp.getReg()) ? RC : nullptr; 459 460 if (RC->hasSubClassEq(MRI.getRegClass(LiveReg))) 461 return RC; 462 463 // FIXME: Allow folding when register classes are memory compatible. 464 return nullptr; 465 } 466 467 void TargetInstrInfo::getNoop(MCInst &NopInst) const { 468 llvm_unreachable("Not implemented"); 469 } 470 471 static MachineInstr *foldPatchpoint(MachineFunction &MF, MachineInstr &MI, 472 ArrayRef<unsigned> Ops, int FrameIndex, 473 const TargetInstrInfo &TII) { 474 unsigned StartIdx = 0; 475 switch (MI.getOpcode()) { 476 case TargetOpcode::STACKMAP: { 477 // StackMapLiveValues are foldable 478 StartIdx = StackMapOpers(&MI).getVarIdx(); 479 break; 480 } 481 case TargetOpcode::PATCHPOINT: { 482 // For PatchPoint, the call args are not foldable (even if reported in the 483 // stackmap e.g. via anyregcc). 484 StartIdx = PatchPointOpers(&MI).getVarIdx(); 485 break; 486 } 487 case TargetOpcode::STATEPOINT: { 488 // For statepoints, fold deopt and gc arguments, but not call arguments. 489 StartIdx = StatepointOpers(&MI).getVarIdx(); 490 break; 491 } 492 default: 493 llvm_unreachable("unexpected stackmap opcode"); 494 } 495 496 // Return false if any operands requested for folding are not foldable (not 497 // part of the stackmap's live values). 498 for (unsigned Op : Ops) { 499 if (Op < StartIdx) 500 return nullptr; 501 } 502 503 MachineInstr *NewMI = 504 MF.CreateMachineInstr(TII.get(MI.getOpcode()), MI.getDebugLoc(), true); 505 MachineInstrBuilder MIB(MF, NewMI); 506 507 // No need to fold return, the meta data, and function arguments 508 for (unsigned i = 0; i < StartIdx; ++i) 509 MIB.add(MI.getOperand(i)); 510 511 for (unsigned i = StartIdx; i < MI.getNumOperands(); ++i) { 512 MachineOperand &MO = MI.getOperand(i); 513 if (is_contained(Ops, i)) { 514 unsigned SpillSize; 515 unsigned SpillOffset; 516 // Compute the spill slot size and offset. 517 const TargetRegisterClass *RC = 518 MF.getRegInfo().getRegClass(MO.getReg()); 519 bool Valid = 520 TII.getStackSlotRange(RC, MO.getSubReg(), SpillSize, SpillOffset, MF); 521 if (!Valid) 522 report_fatal_error("cannot spill patchpoint subregister operand"); 523 MIB.addImm(StackMaps::IndirectMemRefOp); 524 MIB.addImm(SpillSize); 525 MIB.addFrameIndex(FrameIndex); 526 MIB.addImm(SpillOffset); 527 } 528 else 529 MIB.add(MO); 530 } 531 return NewMI; 532 } 533 534 MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI, 535 ArrayRef<unsigned> Ops, int FI, 536 LiveIntervals *LIS) const { 537 auto Flags = MachineMemOperand::MONone; 538 for (unsigned OpIdx : Ops) 539 Flags |= MI.getOperand(OpIdx).isDef() ? MachineMemOperand::MOStore 540 : MachineMemOperand::MOLoad; 541 542 MachineBasicBlock *MBB = MI.getParent(); 543 assert(MBB && "foldMemoryOperand needs an inserted instruction"); 544 MachineFunction &MF = *MBB->getParent(); 545 546 // If we're not folding a load into a subreg, the size of the load is the 547 // size of the spill slot. But if we are, we need to figure out what the 548 // actual load size is. 549 int64_t MemSize = 0; 550 const MachineFrameInfo &MFI = MF.getFrameInfo(); 551 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 552 553 if (Flags & MachineMemOperand::MOStore) { 554 MemSize = MFI.getObjectSize(FI); 555 } else { 556 for (unsigned OpIdx : Ops) { 557 int64_t OpSize = MFI.getObjectSize(FI); 558 559 if (auto SubReg = MI.getOperand(OpIdx).getSubReg()) { 560 unsigned SubRegSize = TRI->getSubRegIdxSize(SubReg); 561 if (SubRegSize > 0 && !(SubRegSize % 8)) 562 OpSize = SubRegSize / 8; 563 } 564 565 MemSize = std::max(MemSize, OpSize); 566 } 567 } 568 569 assert(MemSize && "Did not expect a zero-sized stack slot"); 570 571 MachineInstr *NewMI = nullptr; 572 573 if (MI.getOpcode() == TargetOpcode::STACKMAP || 574 MI.getOpcode() == TargetOpcode::PATCHPOINT || 575 MI.getOpcode() == TargetOpcode::STATEPOINT) { 576 // Fold stackmap/patchpoint. 577 NewMI = foldPatchpoint(MF, MI, Ops, FI, *this); 578 if (NewMI) 579 MBB->insert(MI, NewMI); 580 } else { 581 // Ask the target to do the actual folding. 582 NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, FI, LIS); 583 } 584 585 if (NewMI) { 586 NewMI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 587 // Add a memory operand, foldMemoryOperandImpl doesn't do that. 588 assert((!(Flags & MachineMemOperand::MOStore) || 589 NewMI->mayStore()) && 590 "Folded a def to a non-store!"); 591 assert((!(Flags & MachineMemOperand::MOLoad) || 592 NewMI->mayLoad()) && 593 "Folded a use to a non-load!"); 594 assert(MFI.getObjectOffset(FI) != -1); 595 MachineMemOperand *MMO = MF.getMachineMemOperand( 596 MachinePointerInfo::getFixedStack(MF, FI), Flags, MemSize, 597 MFI.getObjectAlignment(FI)); 598 NewMI->addMemOperand(MF, MMO); 599 600 return NewMI; 601 } 602 603 // Straight COPY may fold as load/store. 604 if (!MI.isCopy() || Ops.size() != 1) 605 return nullptr; 606 607 const TargetRegisterClass *RC = canFoldCopy(MI, Ops[0]); 608 if (!RC) 609 return nullptr; 610 611 const MachineOperand &MO = MI.getOperand(1 - Ops[0]); 612 MachineBasicBlock::iterator Pos = MI; 613 614 if (Flags == MachineMemOperand::MOStore) 615 storeRegToStackSlot(*MBB, Pos, MO.getReg(), MO.isKill(), FI, RC, TRI); 616 else 617 loadRegFromStackSlot(*MBB, Pos, MO.getReg(), FI, RC, TRI); 618 return &*--Pos; 619 } 620 621 MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI, 622 ArrayRef<unsigned> Ops, 623 MachineInstr &LoadMI, 624 LiveIntervals *LIS) const { 625 assert(LoadMI.canFoldAsLoad() && "LoadMI isn't foldable!"); 626 #ifndef NDEBUG 627 for (unsigned OpIdx : Ops) 628 assert(MI.getOperand(OpIdx).isUse() && "Folding load into def!"); 629 #endif 630 631 MachineBasicBlock &MBB = *MI.getParent(); 632 MachineFunction &MF = *MBB.getParent(); 633 634 // Ask the target to do the actual folding. 635 MachineInstr *NewMI = nullptr; 636 int FrameIndex = 0; 637 638 if ((MI.getOpcode() == TargetOpcode::STACKMAP || 639 MI.getOpcode() == TargetOpcode::PATCHPOINT || 640 MI.getOpcode() == TargetOpcode::STATEPOINT) && 641 isLoadFromStackSlot(LoadMI, FrameIndex)) { 642 // Fold stackmap/patchpoint. 643 NewMI = foldPatchpoint(MF, MI, Ops, FrameIndex, *this); 644 if (NewMI) 645 NewMI = &*MBB.insert(MI, NewMI); 646 } else { 647 // Ask the target to do the actual folding. 648 NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, LoadMI, LIS); 649 } 650 651 if (!NewMI) 652 return nullptr; 653 654 // Copy the memoperands from the load to the folded instruction. 655 if (MI.memoperands_empty()) { 656 NewMI->setMemRefs(LoadMI.memoperands_begin(), LoadMI.memoperands_end()); 657 } else { 658 // Handle the rare case of folding multiple loads. 659 NewMI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 660 for (MachineInstr::mmo_iterator I = LoadMI.memoperands_begin(), 661 E = LoadMI.memoperands_end(); 662 I != E; ++I) { 663 NewMI->addMemOperand(MF, *I); 664 } 665 } 666 return NewMI; 667 } 668 669 bool TargetInstrInfo::hasReassociableOperands( 670 const MachineInstr &Inst, const MachineBasicBlock *MBB) const { 671 const MachineOperand &Op1 = Inst.getOperand(1); 672 const MachineOperand &Op2 = Inst.getOperand(2); 673 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 674 675 // We need virtual register definitions for the operands that we will 676 // reassociate. 677 MachineInstr *MI1 = nullptr; 678 MachineInstr *MI2 = nullptr; 679 if (Op1.isReg() && TargetRegisterInfo::isVirtualRegister(Op1.getReg())) 680 MI1 = MRI.getUniqueVRegDef(Op1.getReg()); 681 if (Op2.isReg() && TargetRegisterInfo::isVirtualRegister(Op2.getReg())) 682 MI2 = MRI.getUniqueVRegDef(Op2.getReg()); 683 684 // And they need to be in the trace (otherwise, they won't have a depth). 685 return MI1 && MI2 && MI1->getParent() == MBB && MI2->getParent() == MBB; 686 } 687 688 bool TargetInstrInfo::hasReassociableSibling(const MachineInstr &Inst, 689 bool &Commuted) const { 690 const MachineBasicBlock *MBB = Inst.getParent(); 691 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 692 MachineInstr *MI1 = MRI.getUniqueVRegDef(Inst.getOperand(1).getReg()); 693 MachineInstr *MI2 = MRI.getUniqueVRegDef(Inst.getOperand(2).getReg()); 694 unsigned AssocOpcode = Inst.getOpcode(); 695 696 // If only one operand has the same opcode and it's the second source operand, 697 // the operands must be commuted. 698 Commuted = MI1->getOpcode() != AssocOpcode && MI2->getOpcode() == AssocOpcode; 699 if (Commuted) 700 std::swap(MI1, MI2); 701 702 // 1. The previous instruction must be the same type as Inst. 703 // 2. The previous instruction must have virtual register definitions for its 704 // operands in the same basic block as Inst. 705 // 3. The previous instruction's result must only be used by Inst. 706 return MI1->getOpcode() == AssocOpcode && 707 hasReassociableOperands(*MI1, MBB) && 708 MRI.hasOneNonDBGUse(MI1->getOperand(0).getReg()); 709 } 710 711 // 1. The operation must be associative and commutative. 712 // 2. The instruction must have virtual register definitions for its 713 // operands in the same basic block. 714 // 3. The instruction must have a reassociable sibling. 715 bool TargetInstrInfo::isReassociationCandidate(const MachineInstr &Inst, 716 bool &Commuted) const { 717 return isAssociativeAndCommutative(Inst) && 718 hasReassociableOperands(Inst, Inst.getParent()) && 719 hasReassociableSibling(Inst, Commuted); 720 } 721 722 // The concept of the reassociation pass is that these operations can benefit 723 // from this kind of transformation: 724 // 725 // A = ? op ? 726 // B = A op X (Prev) 727 // C = B op Y (Root) 728 // --> 729 // A = ? op ? 730 // B = X op Y 731 // C = A op B 732 // 733 // breaking the dependency between A and B, allowing them to be executed in 734 // parallel (or back-to-back in a pipeline) instead of depending on each other. 735 736 // FIXME: This has the potential to be expensive (compile time) while not 737 // improving the code at all. Some ways to limit the overhead: 738 // 1. Track successful transforms; bail out if hit rate gets too low. 739 // 2. Only enable at -O3 or some other non-default optimization level. 740 // 3. Pre-screen pattern candidates here: if an operand of the previous 741 // instruction is known to not increase the critical path, then don't match 742 // that pattern. 743 bool TargetInstrInfo::getMachineCombinerPatterns( 744 MachineInstr &Root, 745 SmallVectorImpl<MachineCombinerPattern> &Patterns) const { 746 bool Commute; 747 if (isReassociationCandidate(Root, Commute)) { 748 // We found a sequence of instructions that may be suitable for a 749 // reassociation of operands to increase ILP. Specify each commutation 750 // possibility for the Prev instruction in the sequence and let the 751 // machine combiner decide if changing the operands is worthwhile. 752 if (Commute) { 753 Patterns.push_back(MachineCombinerPattern::REASSOC_AX_YB); 754 Patterns.push_back(MachineCombinerPattern::REASSOC_XA_YB); 755 } else { 756 Patterns.push_back(MachineCombinerPattern::REASSOC_AX_BY); 757 Patterns.push_back(MachineCombinerPattern::REASSOC_XA_BY); 758 } 759 return true; 760 } 761 762 return false; 763 } 764 765 /// Return true when a code sequence can improve loop throughput. 766 bool 767 TargetInstrInfo::isThroughputPattern(MachineCombinerPattern Pattern) const { 768 return false; 769 } 770 771 /// Attempt the reassociation transformation to reduce critical path length. 772 /// See the above comments before getMachineCombinerPatterns(). 773 void TargetInstrInfo::reassociateOps( 774 MachineInstr &Root, MachineInstr &Prev, 775 MachineCombinerPattern Pattern, 776 SmallVectorImpl<MachineInstr *> &InsInstrs, 777 SmallVectorImpl<MachineInstr *> &DelInstrs, 778 DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const { 779 MachineFunction *MF = Root.getMF(); 780 MachineRegisterInfo &MRI = MF->getRegInfo(); 781 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); 782 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 783 const TargetRegisterClass *RC = Root.getRegClassConstraint(0, TII, TRI); 784 785 // This array encodes the operand index for each parameter because the 786 // operands may be commuted. Each row corresponds to a pattern value, 787 // and each column specifies the index of A, B, X, Y. 788 unsigned OpIdx[4][4] = { 789 { 1, 1, 2, 2 }, 790 { 1, 2, 2, 1 }, 791 { 2, 1, 1, 2 }, 792 { 2, 2, 1, 1 } 793 }; 794 795 int Row; 796 switch (Pattern) { 797 case MachineCombinerPattern::REASSOC_AX_BY: Row = 0; break; 798 case MachineCombinerPattern::REASSOC_AX_YB: Row = 1; break; 799 case MachineCombinerPattern::REASSOC_XA_BY: Row = 2; break; 800 case MachineCombinerPattern::REASSOC_XA_YB: Row = 3; break; 801 default: llvm_unreachable("unexpected MachineCombinerPattern"); 802 } 803 804 MachineOperand &OpA = Prev.getOperand(OpIdx[Row][0]); 805 MachineOperand &OpB = Root.getOperand(OpIdx[Row][1]); 806 MachineOperand &OpX = Prev.getOperand(OpIdx[Row][2]); 807 MachineOperand &OpY = Root.getOperand(OpIdx[Row][3]); 808 MachineOperand &OpC = Root.getOperand(0); 809 810 unsigned RegA = OpA.getReg(); 811 unsigned RegB = OpB.getReg(); 812 unsigned RegX = OpX.getReg(); 813 unsigned RegY = OpY.getReg(); 814 unsigned RegC = OpC.getReg(); 815 816 if (TargetRegisterInfo::isVirtualRegister(RegA)) 817 MRI.constrainRegClass(RegA, RC); 818 if (TargetRegisterInfo::isVirtualRegister(RegB)) 819 MRI.constrainRegClass(RegB, RC); 820 if (TargetRegisterInfo::isVirtualRegister(RegX)) 821 MRI.constrainRegClass(RegX, RC); 822 if (TargetRegisterInfo::isVirtualRegister(RegY)) 823 MRI.constrainRegClass(RegY, RC); 824 if (TargetRegisterInfo::isVirtualRegister(RegC)) 825 MRI.constrainRegClass(RegC, RC); 826 827 // Create a new virtual register for the result of (X op Y) instead of 828 // recycling RegB because the MachineCombiner's computation of the critical 829 // path requires a new register definition rather than an existing one. 830 unsigned NewVR = MRI.createVirtualRegister(RC); 831 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0)); 832 833 unsigned Opcode = Root.getOpcode(); 834 bool KillA = OpA.isKill(); 835 bool KillX = OpX.isKill(); 836 bool KillY = OpY.isKill(); 837 838 // Create new instructions for insertion. 839 MachineInstrBuilder MIB1 = 840 BuildMI(*MF, Prev.getDebugLoc(), TII->get(Opcode), NewVR) 841 .addReg(RegX, getKillRegState(KillX)) 842 .addReg(RegY, getKillRegState(KillY)); 843 MachineInstrBuilder MIB2 = 844 BuildMI(*MF, Root.getDebugLoc(), TII->get(Opcode), RegC) 845 .addReg(RegA, getKillRegState(KillA)) 846 .addReg(NewVR, getKillRegState(true)); 847 848 setSpecialOperandAttr(Root, Prev, *MIB1, *MIB2); 849 850 // Record new instructions for insertion and old instructions for deletion. 851 InsInstrs.push_back(MIB1); 852 InsInstrs.push_back(MIB2); 853 DelInstrs.push_back(&Prev); 854 DelInstrs.push_back(&Root); 855 } 856 857 void TargetInstrInfo::genAlternativeCodeSequence( 858 MachineInstr &Root, MachineCombinerPattern Pattern, 859 SmallVectorImpl<MachineInstr *> &InsInstrs, 860 SmallVectorImpl<MachineInstr *> &DelInstrs, 861 DenseMap<unsigned, unsigned> &InstIdxForVirtReg) const { 862 MachineRegisterInfo &MRI = Root.getMF()->getRegInfo(); 863 864 // Select the previous instruction in the sequence based on the input pattern. 865 MachineInstr *Prev = nullptr; 866 switch (Pattern) { 867 case MachineCombinerPattern::REASSOC_AX_BY: 868 case MachineCombinerPattern::REASSOC_XA_BY: 869 Prev = MRI.getUniqueVRegDef(Root.getOperand(1).getReg()); 870 break; 871 case MachineCombinerPattern::REASSOC_AX_YB: 872 case MachineCombinerPattern::REASSOC_XA_YB: 873 Prev = MRI.getUniqueVRegDef(Root.getOperand(2).getReg()); 874 break; 875 default: 876 break; 877 } 878 879 assert(Prev && "Unknown pattern for machine combiner"); 880 881 reassociateOps(Root, *Prev, Pattern, InsInstrs, DelInstrs, InstIdxForVirtReg); 882 } 883 884 bool TargetInstrInfo::isReallyTriviallyReMaterializableGeneric( 885 const MachineInstr &MI, AliasAnalysis *AA) const { 886 const MachineFunction &MF = *MI.getMF(); 887 const MachineRegisterInfo &MRI = MF.getRegInfo(); 888 889 // Remat clients assume operand 0 is the defined register. 890 if (!MI.getNumOperands() || !MI.getOperand(0).isReg()) 891 return false; 892 unsigned DefReg = MI.getOperand(0).getReg(); 893 894 // A sub-register definition can only be rematerialized if the instruction 895 // doesn't read the other parts of the register. Otherwise it is really a 896 // read-modify-write operation on the full virtual register which cannot be 897 // moved safely. 898 if (TargetRegisterInfo::isVirtualRegister(DefReg) && 899 MI.getOperand(0).getSubReg() && MI.readsVirtualRegister(DefReg)) 900 return false; 901 902 // A load from a fixed stack slot can be rematerialized. This may be 903 // redundant with subsequent checks, but it's target-independent, 904 // simple, and a common case. 905 int FrameIdx = 0; 906 if (isLoadFromStackSlot(MI, FrameIdx) && 907 MF.getFrameInfo().isImmutableObjectIndex(FrameIdx)) 908 return true; 909 910 // Avoid instructions obviously unsafe for remat. 911 if (MI.isNotDuplicable() || MI.mayStore() || MI.hasUnmodeledSideEffects()) 912 return false; 913 914 // Don't remat inline asm. We have no idea how expensive it is 915 // even if it's side effect free. 916 if (MI.isInlineAsm()) 917 return false; 918 919 // Avoid instructions which load from potentially varying memory. 920 if (MI.mayLoad() && !MI.isDereferenceableInvariantLoad(AA)) 921 return false; 922 923 // If any of the registers accessed are non-constant, conservatively assume 924 // the instruction is not rematerializable. 925 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 926 const MachineOperand &MO = MI.getOperand(i); 927 if (!MO.isReg()) continue; 928 unsigned Reg = MO.getReg(); 929 if (Reg == 0) 930 continue; 931 932 // Check for a well-behaved physical register. 933 if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 934 if (MO.isUse()) { 935 // If the physreg has no defs anywhere, it's just an ambient register 936 // and we can freely move its uses. Alternatively, if it's allocatable, 937 // it could get allocated to something with a def during allocation. 938 if (!MRI.isConstantPhysReg(Reg)) 939 return false; 940 } else { 941 // A physreg def. We can't remat it. 942 return false; 943 } 944 continue; 945 } 946 947 // Only allow one virtual-register def. There may be multiple defs of the 948 // same virtual register, though. 949 if (MO.isDef() && Reg != DefReg) 950 return false; 951 952 // Don't allow any virtual-register uses. Rematting an instruction with 953 // virtual register uses would length the live ranges of the uses, which 954 // is not necessarily a good idea, certainly not "trivial". 955 if (MO.isUse()) 956 return false; 957 } 958 959 // Everything checked out. 960 return true; 961 } 962 963 int TargetInstrInfo::getSPAdjust(const MachineInstr &MI) const { 964 const MachineFunction *MF = MI.getMF(); 965 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering(); 966 bool StackGrowsDown = 967 TFI->getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown; 968 969 unsigned FrameSetupOpcode = getCallFrameSetupOpcode(); 970 unsigned FrameDestroyOpcode = getCallFrameDestroyOpcode(); 971 972 if (!isFrameInstr(MI)) 973 return 0; 974 975 int SPAdj = TFI->alignSPAdjust(getFrameSize(MI)); 976 977 if ((!StackGrowsDown && MI.getOpcode() == FrameSetupOpcode) || 978 (StackGrowsDown && MI.getOpcode() == FrameDestroyOpcode)) 979 SPAdj = -SPAdj; 980 981 return SPAdj; 982 } 983 984 /// isSchedulingBoundary - Test if the given instruction should be 985 /// considered a scheduling boundary. This primarily includes labels 986 /// and terminators. 987 bool TargetInstrInfo::isSchedulingBoundary(const MachineInstr &MI, 988 const MachineBasicBlock *MBB, 989 const MachineFunction &MF) const { 990 // Terminators and labels can't be scheduled around. 991 if (MI.isTerminator() || MI.isPosition()) 992 return true; 993 994 // Don't attempt to schedule around any instruction that defines 995 // a stack-oriented pointer, as it's unlikely to be profitable. This 996 // saves compile time, because it doesn't require every single 997 // stack slot reference to depend on the instruction that does the 998 // modification. 999 const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering(); 1000 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 1001 return MI.modifiesRegister(TLI.getStackPointerRegisterToSaveRestore(), TRI); 1002 } 1003 1004 // Provide a global flag for disabling the PreRA hazard recognizer that targets 1005 // may choose to honor. 1006 bool TargetInstrInfo::usePreRAHazardRecognizer() const { 1007 return !DisableHazardRecognizer; 1008 } 1009 1010 // Default implementation of CreateTargetRAHazardRecognizer. 1011 ScheduleHazardRecognizer *TargetInstrInfo:: 1012 CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI, 1013 const ScheduleDAG *DAG) const { 1014 // Dummy hazard recognizer allows all instructions to issue. 1015 return new ScheduleHazardRecognizer(); 1016 } 1017 1018 // Default implementation of CreateTargetMIHazardRecognizer. 1019 ScheduleHazardRecognizer *TargetInstrInfo:: 1020 CreateTargetMIHazardRecognizer(const InstrItineraryData *II, 1021 const ScheduleDAG *DAG) const { 1022 return (ScheduleHazardRecognizer *) 1023 new ScoreboardHazardRecognizer(II, DAG, "misched"); 1024 } 1025 1026 // Default implementation of CreateTargetPostRAHazardRecognizer. 1027 ScheduleHazardRecognizer *TargetInstrInfo:: 1028 CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, 1029 const ScheduleDAG *DAG) const { 1030 return (ScheduleHazardRecognizer *) 1031 new ScoreboardHazardRecognizer(II, DAG, "post-RA-sched"); 1032 } 1033 1034 //===----------------------------------------------------------------------===// 1035 // SelectionDAG latency interface. 1036 //===----------------------------------------------------------------------===// 1037 1038 int 1039 TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, 1040 SDNode *DefNode, unsigned DefIdx, 1041 SDNode *UseNode, unsigned UseIdx) const { 1042 if (!ItinData || ItinData->isEmpty()) 1043 return -1; 1044 1045 if (!DefNode->isMachineOpcode()) 1046 return -1; 1047 1048 unsigned DefClass = get(DefNode->getMachineOpcode()).getSchedClass(); 1049 if (!UseNode->isMachineOpcode()) 1050 return ItinData->getOperandCycle(DefClass, DefIdx); 1051 unsigned UseClass = get(UseNode->getMachineOpcode()).getSchedClass(); 1052 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx); 1053 } 1054 1055 int TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, 1056 SDNode *N) const { 1057 if (!ItinData || ItinData->isEmpty()) 1058 return 1; 1059 1060 if (!N->isMachineOpcode()) 1061 return 1; 1062 1063 return ItinData->getStageLatency(get(N->getMachineOpcode()).getSchedClass()); 1064 } 1065 1066 //===----------------------------------------------------------------------===// 1067 // MachineInstr latency interface. 1068 //===----------------------------------------------------------------------===// 1069 1070 unsigned TargetInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData, 1071 const MachineInstr &MI) const { 1072 if (!ItinData || ItinData->isEmpty()) 1073 return 1; 1074 1075 unsigned Class = MI.getDesc().getSchedClass(); 1076 int UOps = ItinData->Itineraries[Class].NumMicroOps; 1077 if (UOps >= 0) 1078 return UOps; 1079 1080 // The # of u-ops is dynamically determined. The specific target should 1081 // override this function to return the right number. 1082 return 1; 1083 } 1084 1085 /// Return the default expected latency for a def based on it's opcode. 1086 unsigned TargetInstrInfo::defaultDefLatency(const MCSchedModel &SchedModel, 1087 const MachineInstr &DefMI) const { 1088 if (DefMI.isTransient()) 1089 return 0; 1090 if (DefMI.mayLoad()) 1091 return SchedModel.LoadLatency; 1092 if (isHighLatencyDef(DefMI.getOpcode())) 1093 return SchedModel.HighLatency; 1094 return 1; 1095 } 1096 1097 unsigned TargetInstrInfo::getPredicationCost(const MachineInstr &) const { 1098 return 0; 1099 } 1100 1101 unsigned TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, 1102 const MachineInstr &MI, 1103 unsigned *PredCost) const { 1104 // Default to one cycle for no itinerary. However, an "empty" itinerary may 1105 // still have a MinLatency property, which getStageLatency checks. 1106 if (!ItinData) 1107 return MI.mayLoad() ? 2 : 1; 1108 1109 return ItinData->getStageLatency(MI.getDesc().getSchedClass()); 1110 } 1111 1112 bool TargetInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel, 1113 const MachineInstr &DefMI, 1114 unsigned DefIdx) const { 1115 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries(); 1116 if (!ItinData || ItinData->isEmpty()) 1117 return false; 1118 1119 unsigned DefClass = DefMI.getDesc().getSchedClass(); 1120 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx); 1121 return (DefCycle != -1 && DefCycle <= 1); 1122 } 1123 1124 /// Both DefMI and UseMI must be valid. By default, call directly to the 1125 /// itinerary. This may be overriden by the target. 1126 int TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, 1127 const MachineInstr &DefMI, 1128 unsigned DefIdx, 1129 const MachineInstr &UseMI, 1130 unsigned UseIdx) const { 1131 unsigned DefClass = DefMI.getDesc().getSchedClass(); 1132 unsigned UseClass = UseMI.getDesc().getSchedClass(); 1133 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx); 1134 } 1135 1136 /// If we can determine the operand latency from the def only, without itinerary 1137 /// lookup, do so. Otherwise return -1. 1138 int TargetInstrInfo::computeDefOperandLatency( 1139 const InstrItineraryData *ItinData, const MachineInstr &DefMI) const { 1140 1141 // Let the target hook getInstrLatency handle missing itineraries. 1142 if (!ItinData) 1143 return getInstrLatency(ItinData, DefMI); 1144 1145 if(ItinData->isEmpty()) 1146 return defaultDefLatency(ItinData->SchedModel, DefMI); 1147 1148 // ...operand lookup required 1149 return -1; 1150 } 1151 1152 bool TargetInstrInfo::getRegSequenceInputs( 1153 const MachineInstr &MI, unsigned DefIdx, 1154 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const { 1155 assert((MI.isRegSequence() || 1156 MI.isRegSequenceLike()) && "Instruction do not have the proper type"); 1157 1158 if (!MI.isRegSequence()) 1159 return getRegSequenceLikeInputs(MI, DefIdx, InputRegs); 1160 1161 // We are looking at: 1162 // Def = REG_SEQUENCE v0, sub0, v1, sub1, ... 1163 assert(DefIdx == 0 && "REG_SEQUENCE only has one def"); 1164 for (unsigned OpIdx = 1, EndOpIdx = MI.getNumOperands(); OpIdx != EndOpIdx; 1165 OpIdx += 2) { 1166 const MachineOperand &MOReg = MI.getOperand(OpIdx); 1167 if (MOReg.isUndef()) 1168 continue; 1169 const MachineOperand &MOSubIdx = MI.getOperand(OpIdx + 1); 1170 assert(MOSubIdx.isImm() && 1171 "One of the subindex of the reg_sequence is not an immediate"); 1172 // Record Reg:SubReg, SubIdx. 1173 InputRegs.push_back(RegSubRegPairAndIdx(MOReg.getReg(), MOReg.getSubReg(), 1174 (unsigned)MOSubIdx.getImm())); 1175 } 1176 return true; 1177 } 1178 1179 bool TargetInstrInfo::getExtractSubregInputs( 1180 const MachineInstr &MI, unsigned DefIdx, 1181 RegSubRegPairAndIdx &InputReg) const { 1182 assert((MI.isExtractSubreg() || 1183 MI.isExtractSubregLike()) && "Instruction do not have the proper type"); 1184 1185 if (!MI.isExtractSubreg()) 1186 return getExtractSubregLikeInputs(MI, DefIdx, InputReg); 1187 1188 // We are looking at: 1189 // Def = EXTRACT_SUBREG v0.sub1, sub0. 1190 assert(DefIdx == 0 && "EXTRACT_SUBREG only has one def"); 1191 const MachineOperand &MOReg = MI.getOperand(1); 1192 if (MOReg.isUndef()) 1193 return false; 1194 const MachineOperand &MOSubIdx = MI.getOperand(2); 1195 assert(MOSubIdx.isImm() && 1196 "The subindex of the extract_subreg is not an immediate"); 1197 1198 InputReg.Reg = MOReg.getReg(); 1199 InputReg.SubReg = MOReg.getSubReg(); 1200 InputReg.SubIdx = (unsigned)MOSubIdx.getImm(); 1201 return true; 1202 } 1203 1204 bool TargetInstrInfo::getInsertSubregInputs( 1205 const MachineInstr &MI, unsigned DefIdx, 1206 RegSubRegPair &BaseReg, RegSubRegPairAndIdx &InsertedReg) const { 1207 assert((MI.isInsertSubreg() || 1208 MI.isInsertSubregLike()) && "Instruction do not have the proper type"); 1209 1210 if (!MI.isInsertSubreg()) 1211 return getInsertSubregLikeInputs(MI, DefIdx, BaseReg, InsertedReg); 1212 1213 // We are looking at: 1214 // Def = INSERT_SEQUENCE v0, v1, sub0. 1215 assert(DefIdx == 0 && "INSERT_SUBREG only has one def"); 1216 const MachineOperand &MOBaseReg = MI.getOperand(1); 1217 const MachineOperand &MOInsertedReg = MI.getOperand(2); 1218 if (MOInsertedReg.isUndef()) 1219 return false; 1220 const MachineOperand &MOSubIdx = MI.getOperand(3); 1221 assert(MOSubIdx.isImm() && 1222 "One of the subindex of the reg_sequence is not an immediate"); 1223 BaseReg.Reg = MOBaseReg.getReg(); 1224 BaseReg.SubReg = MOBaseReg.getSubReg(); 1225 1226 InsertedReg.Reg = MOInsertedReg.getReg(); 1227 InsertedReg.SubReg = MOInsertedReg.getSubReg(); 1228 InsertedReg.SubIdx = (unsigned)MOSubIdx.getImm(); 1229 return true; 1230 } 1231