1 //===-- ARMBaseInstrInfo.cpp - ARM 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 Base ARM implementation of the TargetInstrInfo class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "ARM.h" 15 #include "ARMBaseInstrInfo.h" 16 #include "ARMBaseRegisterInfo.h" 17 #include "ARMConstantPoolValue.h" 18 #include "ARMFeatures.h" 19 #include "ARMHazardRecognizer.h" 20 #include "ARMMachineFunctionInfo.h" 21 #include "MCTargetDesc/ARMAddressingModes.h" 22 #include "llvm/ADT/STLExtras.h" 23 #include "llvm/CodeGen/LiveVariables.h" 24 #include "llvm/CodeGen/MachineConstantPool.h" 25 #include "llvm/CodeGen/MachineFrameInfo.h" 26 #include "llvm/CodeGen/MachineInstrBuilder.h" 27 #include "llvm/CodeGen/MachineJumpTableInfo.h" 28 #include "llvm/CodeGen/MachineMemOperand.h" 29 #include "llvm/CodeGen/MachineRegisterInfo.h" 30 #include "llvm/CodeGen/SelectionDAGNodes.h" 31 #include "llvm/CodeGen/TargetSchedule.h" 32 #include "llvm/IR/Constants.h" 33 #include "llvm/IR/Function.h" 34 #include "llvm/IR/GlobalValue.h" 35 #include "llvm/MC/MCAsmInfo.h" 36 #include "llvm/MC/MCExpr.h" 37 #include "llvm/Support/BranchProbability.h" 38 #include "llvm/Support/CommandLine.h" 39 #include "llvm/Support/Debug.h" 40 #include "llvm/Support/ErrorHandling.h" 41 #include "llvm/Support/raw_ostream.h" 42 43 using namespace llvm; 44 45 #define DEBUG_TYPE "arm-instrinfo" 46 47 #define GET_INSTRINFO_CTOR_DTOR 48 #include "ARMGenInstrInfo.inc" 49 50 static cl::opt<bool> 51 EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden, 52 cl::desc("Enable ARM 2-addr to 3-addr conv")); 53 54 /// ARM_MLxEntry - Record information about MLA / MLS instructions. 55 struct ARM_MLxEntry { 56 uint16_t MLxOpc; // MLA / MLS opcode 57 uint16_t MulOpc; // Expanded multiplication opcode 58 uint16_t AddSubOpc; // Expanded add / sub opcode 59 bool NegAcc; // True if the acc is negated before the add / sub. 60 bool HasLane; // True if instruction has an extra "lane" operand. 61 }; 62 63 static const ARM_MLxEntry ARM_MLxTable[] = { 64 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane 65 // fp scalar ops 66 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false }, 67 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false }, 68 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false }, 69 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false }, 70 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false }, 71 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false }, 72 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false }, 73 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false }, 74 75 // fp SIMD ops 76 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false }, 77 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false }, 78 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false }, 79 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false }, 80 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true }, 81 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true }, 82 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true }, 83 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true }, 84 }; 85 86 ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI) 87 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP), 88 Subtarget(STI) { 89 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) { 90 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second) 91 llvm_unreachable("Duplicated entries?"); 92 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc); 93 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc); 94 } 95 } 96 97 // Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl 98 // currently defaults to no prepass hazard recognizer. 99 ScheduleHazardRecognizer * 100 ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI, 101 const ScheduleDAG *DAG) const { 102 if (usePreRAHazardRecognizer()) { 103 const InstrItineraryData *II = 104 static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData(); 105 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched"); 106 } 107 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG); 108 } 109 110 ScheduleHazardRecognizer *ARMBaseInstrInfo:: 111 CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, 112 const ScheduleDAG *DAG) const { 113 if (Subtarget.isThumb2() || Subtarget.hasVFP2()) 114 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG); 115 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG); 116 } 117 118 MachineInstr *ARMBaseInstrInfo::convertToThreeAddress( 119 MachineFunction::iterator &MFI, MachineInstr &MI, LiveVariables *LV) const { 120 // FIXME: Thumb2 support. 121 122 if (!EnableARM3Addr) 123 return nullptr; 124 125 MachineFunction &MF = *MI.getParent()->getParent(); 126 uint64_t TSFlags = MI.getDesc().TSFlags; 127 bool isPre = false; 128 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) { 129 default: return nullptr; 130 case ARMII::IndexModePre: 131 isPre = true; 132 break; 133 case ARMII::IndexModePost: 134 break; 135 } 136 137 // Try splitting an indexed load/store to an un-indexed one plus an add/sub 138 // operation. 139 unsigned MemOpc = getUnindexedOpcode(MI.getOpcode()); 140 if (MemOpc == 0) 141 return nullptr; 142 143 MachineInstr *UpdateMI = nullptr; 144 MachineInstr *MemMI = nullptr; 145 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask); 146 const MCInstrDesc &MCID = MI.getDesc(); 147 unsigned NumOps = MCID.getNumOperands(); 148 bool isLoad = !MI.mayStore(); 149 const MachineOperand &WB = isLoad ? MI.getOperand(1) : MI.getOperand(0); 150 const MachineOperand &Base = MI.getOperand(2); 151 const MachineOperand &Offset = MI.getOperand(NumOps - 3); 152 unsigned WBReg = WB.getReg(); 153 unsigned BaseReg = Base.getReg(); 154 unsigned OffReg = Offset.getReg(); 155 unsigned OffImm = MI.getOperand(NumOps - 2).getImm(); 156 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI.getOperand(NumOps - 1).getImm(); 157 switch (AddrMode) { 158 default: llvm_unreachable("Unknown indexed op!"); 159 case ARMII::AddrMode2: { 160 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub; 161 unsigned Amt = ARM_AM::getAM2Offset(OffImm); 162 if (OffReg == 0) { 163 if (ARM_AM::getSOImmVal(Amt) == -1) 164 // Can't encode it in a so_imm operand. This transformation will 165 // add more than 1 instruction. Abandon! 166 return nullptr; 167 UpdateMI = BuildMI(MF, MI.getDebugLoc(), 168 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg) 169 .addReg(BaseReg) 170 .addImm(Amt) 171 .addImm(Pred) 172 .addReg(0) 173 .addReg(0); 174 } else if (Amt != 0) { 175 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm); 176 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt); 177 UpdateMI = BuildMI(MF, MI.getDebugLoc(), 178 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg) 179 .addReg(BaseReg) 180 .addReg(OffReg) 181 .addReg(0) 182 .addImm(SOOpc) 183 .addImm(Pred) 184 .addReg(0) 185 .addReg(0); 186 } else 187 UpdateMI = BuildMI(MF, MI.getDebugLoc(), 188 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg) 189 .addReg(BaseReg) 190 .addReg(OffReg) 191 .addImm(Pred) 192 .addReg(0) 193 .addReg(0); 194 break; 195 } 196 case ARMII::AddrMode3 : { 197 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub; 198 unsigned Amt = ARM_AM::getAM3Offset(OffImm); 199 if (OffReg == 0) 200 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand. 201 UpdateMI = BuildMI(MF, MI.getDebugLoc(), 202 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg) 203 .addReg(BaseReg) 204 .addImm(Amt) 205 .addImm(Pred) 206 .addReg(0) 207 .addReg(0); 208 else 209 UpdateMI = BuildMI(MF, MI.getDebugLoc(), 210 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg) 211 .addReg(BaseReg) 212 .addReg(OffReg) 213 .addImm(Pred) 214 .addReg(0) 215 .addReg(0); 216 break; 217 } 218 } 219 220 std::vector<MachineInstr*> NewMIs; 221 if (isPre) { 222 if (isLoad) 223 MemMI = 224 BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg()) 225 .addReg(WBReg) 226 .addImm(0) 227 .addImm(Pred); 228 else 229 MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc)) 230 .addReg(MI.getOperand(1).getReg()) 231 .addReg(WBReg) 232 .addReg(0) 233 .addImm(0) 234 .addImm(Pred); 235 NewMIs.push_back(MemMI); 236 NewMIs.push_back(UpdateMI); 237 } else { 238 if (isLoad) 239 MemMI = 240 BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg()) 241 .addReg(BaseReg) 242 .addImm(0) 243 .addImm(Pred); 244 else 245 MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc)) 246 .addReg(MI.getOperand(1).getReg()) 247 .addReg(BaseReg) 248 .addReg(0) 249 .addImm(0) 250 .addImm(Pred); 251 if (WB.isDead()) 252 UpdateMI->getOperand(0).setIsDead(); 253 NewMIs.push_back(UpdateMI); 254 NewMIs.push_back(MemMI); 255 } 256 257 // Transfer LiveVariables states, kill / dead info. 258 if (LV) { 259 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 260 MachineOperand &MO = MI.getOperand(i); 261 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) { 262 unsigned Reg = MO.getReg(); 263 264 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg); 265 if (MO.isDef()) { 266 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI; 267 if (MO.isDead()) 268 LV->addVirtualRegisterDead(Reg, *NewMI); 269 } 270 if (MO.isUse() && MO.isKill()) { 271 for (unsigned j = 0; j < 2; ++j) { 272 // Look at the two new MI's in reverse order. 273 MachineInstr *NewMI = NewMIs[j]; 274 if (!NewMI->readsRegister(Reg)) 275 continue; 276 LV->addVirtualRegisterKilled(Reg, *NewMI); 277 if (VI.removeKill(MI)) 278 VI.Kills.push_back(NewMI); 279 break; 280 } 281 } 282 } 283 } 284 } 285 286 MachineBasicBlock::iterator MBBI = MI.getIterator(); 287 MFI->insert(MBBI, NewMIs[1]); 288 MFI->insert(MBBI, NewMIs[0]); 289 return NewMIs[0]; 290 } 291 292 // Branch analysis. 293 bool ARMBaseInstrInfo::analyzeBranch(MachineBasicBlock &MBB, 294 MachineBasicBlock *&TBB, 295 MachineBasicBlock *&FBB, 296 SmallVectorImpl<MachineOperand> &Cond, 297 bool AllowModify) const { 298 TBB = nullptr; 299 FBB = nullptr; 300 301 MachineBasicBlock::iterator I = MBB.end(); 302 if (I == MBB.begin()) 303 return false; // Empty blocks are easy. 304 --I; 305 306 // Walk backwards from the end of the basic block until the branch is 307 // analyzed or we give up. 308 while (isPredicated(*I) || I->isTerminator() || I->isDebugValue()) { 309 310 // Flag to be raised on unanalyzeable instructions. This is useful in cases 311 // where we want to clean up on the end of the basic block before we bail 312 // out. 313 bool CantAnalyze = false; 314 315 // Skip over DEBUG values and predicated nonterminators. 316 while (I->isDebugValue() || !I->isTerminator()) { 317 if (I == MBB.begin()) 318 return false; 319 --I; 320 } 321 322 if (isIndirectBranchOpcode(I->getOpcode()) || 323 isJumpTableBranchOpcode(I->getOpcode())) { 324 // Indirect branches and jump tables can't be analyzed, but we still want 325 // to clean up any instructions at the tail of the basic block. 326 CantAnalyze = true; 327 } else if (isUncondBranchOpcode(I->getOpcode())) { 328 TBB = I->getOperand(0).getMBB(); 329 } else if (isCondBranchOpcode(I->getOpcode())) { 330 // Bail out if we encounter multiple conditional branches. 331 if (!Cond.empty()) 332 return true; 333 334 assert(!FBB && "FBB should have been null."); 335 FBB = TBB; 336 TBB = I->getOperand(0).getMBB(); 337 Cond.push_back(I->getOperand(1)); 338 Cond.push_back(I->getOperand(2)); 339 } else if (I->isReturn()) { 340 // Returns can't be analyzed, but we should run cleanup. 341 CantAnalyze = !isPredicated(*I); 342 } else { 343 // We encountered other unrecognized terminator. Bail out immediately. 344 return true; 345 } 346 347 // Cleanup code - to be run for unpredicated unconditional branches and 348 // returns. 349 if (!isPredicated(*I) && 350 (isUncondBranchOpcode(I->getOpcode()) || 351 isIndirectBranchOpcode(I->getOpcode()) || 352 isJumpTableBranchOpcode(I->getOpcode()) || 353 I->isReturn())) { 354 // Forget any previous condition branch information - it no longer applies. 355 Cond.clear(); 356 FBB = nullptr; 357 358 // If we can modify the function, delete everything below this 359 // unconditional branch. 360 if (AllowModify) { 361 MachineBasicBlock::iterator DI = std::next(I); 362 while (DI != MBB.end()) { 363 MachineInstr &InstToDelete = *DI; 364 ++DI; 365 InstToDelete.eraseFromParent(); 366 } 367 } 368 } 369 370 if (CantAnalyze) 371 return true; 372 373 if (I == MBB.begin()) 374 return false; 375 376 --I; 377 } 378 379 // We made it past the terminators without bailing out - we must have 380 // analyzed this branch successfully. 381 return false; 382 } 383 384 385 unsigned ARMBaseInstrInfo::removeBranch(MachineBasicBlock &MBB, 386 int *BytesRemoved) const { 387 assert(!BytesRemoved && "code size not handled"); 388 389 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); 390 if (I == MBB.end()) 391 return 0; 392 393 if (!isUncondBranchOpcode(I->getOpcode()) && 394 !isCondBranchOpcode(I->getOpcode())) 395 return 0; 396 397 // Remove the branch. 398 I->eraseFromParent(); 399 400 I = MBB.end(); 401 402 if (I == MBB.begin()) return 1; 403 --I; 404 if (!isCondBranchOpcode(I->getOpcode())) 405 return 1; 406 407 // Remove the branch. 408 I->eraseFromParent(); 409 return 2; 410 } 411 412 unsigned ARMBaseInstrInfo::insertBranch(MachineBasicBlock &MBB, 413 MachineBasicBlock *TBB, 414 MachineBasicBlock *FBB, 415 ArrayRef<MachineOperand> Cond, 416 const DebugLoc &DL, 417 int *BytesAdded) const { 418 assert(!BytesAdded && "code size not handled"); 419 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>(); 420 int BOpc = !AFI->isThumbFunction() 421 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB); 422 int BccOpc = !AFI->isThumbFunction() 423 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc); 424 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function(); 425 426 // Shouldn't be a fall through. 427 assert(TBB && "insertBranch must not be told to insert a fallthrough"); 428 assert((Cond.size() == 2 || Cond.size() == 0) && 429 "ARM branch conditions have two components!"); 430 431 // For conditional branches, we use addOperand to preserve CPSR flags. 432 433 if (!FBB) { 434 if (Cond.empty()) { // Unconditional branch? 435 if (isThumb) 436 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).addImm(ARMCC::AL).addReg(0); 437 else 438 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB); 439 } else 440 BuildMI(&MBB, DL, get(BccOpc)) 441 .addMBB(TBB) 442 .addImm(Cond[0].getImm()) 443 .add(Cond[1]); 444 return 1; 445 } 446 447 // Two-way conditional branch. 448 BuildMI(&MBB, DL, get(BccOpc)) 449 .addMBB(TBB) 450 .addImm(Cond[0].getImm()) 451 .add(Cond[1]); 452 if (isThumb) 453 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).addImm(ARMCC::AL).addReg(0); 454 else 455 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB); 456 return 2; 457 } 458 459 bool ARMBaseInstrInfo:: 460 reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { 461 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm(); 462 Cond[0].setImm(ARMCC::getOppositeCondition(CC)); 463 return false; 464 } 465 466 bool ARMBaseInstrInfo::isPredicated(const MachineInstr &MI) const { 467 if (MI.isBundle()) { 468 MachineBasicBlock::const_instr_iterator I = MI.getIterator(); 469 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end(); 470 while (++I != E && I->isInsideBundle()) { 471 int PIdx = I->findFirstPredOperandIdx(); 472 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL) 473 return true; 474 } 475 return false; 476 } 477 478 int PIdx = MI.findFirstPredOperandIdx(); 479 return PIdx != -1 && MI.getOperand(PIdx).getImm() != ARMCC::AL; 480 } 481 482 bool ARMBaseInstrInfo::PredicateInstruction( 483 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const { 484 unsigned Opc = MI.getOpcode(); 485 if (isUncondBranchOpcode(Opc)) { 486 MI.setDesc(get(getMatchingCondBranchOpcode(Opc))); 487 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 488 .addImm(Pred[0].getImm()) 489 .addReg(Pred[1].getReg()); 490 return true; 491 } 492 493 int PIdx = MI.findFirstPredOperandIdx(); 494 if (PIdx != -1) { 495 MachineOperand &PMO = MI.getOperand(PIdx); 496 PMO.setImm(Pred[0].getImm()); 497 MI.getOperand(PIdx+1).setReg(Pred[1].getReg()); 498 return true; 499 } 500 return false; 501 } 502 503 bool ARMBaseInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1, 504 ArrayRef<MachineOperand> Pred2) const { 505 if (Pred1.size() > 2 || Pred2.size() > 2) 506 return false; 507 508 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm(); 509 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm(); 510 if (CC1 == CC2) 511 return true; 512 513 switch (CC1) { 514 default: 515 return false; 516 case ARMCC::AL: 517 return true; 518 case ARMCC::HS: 519 return CC2 == ARMCC::HI; 520 case ARMCC::LS: 521 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ; 522 case ARMCC::GE: 523 return CC2 == ARMCC::GT; 524 case ARMCC::LE: 525 return CC2 == ARMCC::LT; 526 } 527 } 528 529 bool ARMBaseInstrInfo::DefinesPredicate( 530 MachineInstr &MI, std::vector<MachineOperand> &Pred) const { 531 bool Found = false; 532 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 533 const MachineOperand &MO = MI.getOperand(i); 534 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) || 535 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) { 536 Pred.push_back(MO); 537 Found = true; 538 } 539 } 540 541 return Found; 542 } 543 544 static bool isCPSRDefined(const MachineInstr *MI) { 545 for (const auto &MO : MI->operands()) 546 if (MO.isReg() && MO.getReg() == ARM::CPSR && MO.isDef() && !MO.isDead()) 547 return true; 548 return false; 549 } 550 551 static bool isEligibleForITBlock(const MachineInstr *MI) { 552 switch (MI->getOpcode()) { 553 default: return true; 554 case ARM::tADC: // ADC (register) T1 555 case ARM::tADDi3: // ADD (immediate) T1 556 case ARM::tADDi8: // ADD (immediate) T2 557 case ARM::tADDrr: // ADD (register) T1 558 case ARM::tAND: // AND (register) T1 559 case ARM::tASRri: // ASR (immediate) T1 560 case ARM::tASRrr: // ASR (register) T1 561 case ARM::tBIC: // BIC (register) T1 562 case ARM::tEOR: // EOR (register) T1 563 case ARM::tLSLri: // LSL (immediate) T1 564 case ARM::tLSLrr: // LSL (register) T1 565 case ARM::tLSRri: // LSR (immediate) T1 566 case ARM::tLSRrr: // LSR (register) T1 567 case ARM::tMUL: // MUL T1 568 case ARM::tMVN: // MVN (register) T1 569 case ARM::tORR: // ORR (register) T1 570 case ARM::tROR: // ROR (register) T1 571 case ARM::tRSB: // RSB (immediate) T1 572 case ARM::tSBC: // SBC (register) T1 573 case ARM::tSUBi3: // SUB (immediate) T1 574 case ARM::tSUBi8: // SUB (immediate) T2 575 case ARM::tSUBrr: // SUB (register) T1 576 return !isCPSRDefined(MI); 577 } 578 } 579 580 /// isPredicable - Return true if the specified instruction can be predicated. 581 /// By default, this returns true for every instruction with a 582 /// PredicateOperand. 583 bool ARMBaseInstrInfo::isPredicable(MachineInstr &MI) const { 584 if (!MI.isPredicable()) 585 return false; 586 587 if (MI.isBundle()) 588 return false; 589 590 if (!isEligibleForITBlock(&MI)) 591 return false; 592 593 ARMFunctionInfo *AFI = 594 MI.getParent()->getParent()->getInfo<ARMFunctionInfo>(); 595 596 if (AFI->isThumb2Function()) { 597 if (getSubtarget().restrictIT()) 598 return isV8EligibleForIT(&MI); 599 } else { // non-Thumb 600 if ((MI.getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON) 601 return false; 602 } 603 604 return true; 605 } 606 607 namespace llvm { 608 template <> bool IsCPSRDead<MachineInstr>(MachineInstr *MI) { 609 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 610 const MachineOperand &MO = MI->getOperand(i); 611 if (!MO.isReg() || MO.isUndef() || MO.isUse()) 612 continue; 613 if (MO.getReg() != ARM::CPSR) 614 continue; 615 if (!MO.isDead()) 616 return false; 617 } 618 // all definitions of CPSR are dead 619 return true; 620 } 621 } 622 623 /// GetInstSize - Return the size of the specified MachineInstr. 624 /// 625 unsigned ARMBaseInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { 626 const MachineBasicBlock &MBB = *MI.getParent(); 627 const MachineFunction *MF = MBB.getParent(); 628 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo(); 629 630 const MCInstrDesc &MCID = MI.getDesc(); 631 if (MCID.getSize()) 632 return MCID.getSize(); 633 634 // If this machine instr is an inline asm, measure it. 635 if (MI.getOpcode() == ARM::INLINEASM) 636 return getInlineAsmLength(MI.getOperand(0).getSymbolName(), *MAI); 637 unsigned Opc = MI.getOpcode(); 638 switch (Opc) { 639 default: 640 // pseudo-instruction sizes are zero. 641 return 0; 642 case TargetOpcode::BUNDLE: 643 return getInstBundleLength(MI); 644 case ARM::MOVi16_ga_pcrel: 645 case ARM::MOVTi16_ga_pcrel: 646 case ARM::t2MOVi16_ga_pcrel: 647 case ARM::t2MOVTi16_ga_pcrel: 648 return 4; 649 case ARM::MOVi32imm: 650 case ARM::t2MOVi32imm: 651 return 8; 652 case ARM::CONSTPOOL_ENTRY: 653 case ARM::JUMPTABLE_INSTS: 654 case ARM::JUMPTABLE_ADDRS: 655 case ARM::JUMPTABLE_TBB: 656 case ARM::JUMPTABLE_TBH: 657 // If this machine instr is a constant pool entry, its size is recorded as 658 // operand #2. 659 return MI.getOperand(2).getImm(); 660 case ARM::Int_eh_sjlj_longjmp: 661 return 16; 662 case ARM::tInt_eh_sjlj_longjmp: 663 return 10; 664 case ARM::tInt_WIN_eh_sjlj_longjmp: 665 return 12; 666 case ARM::Int_eh_sjlj_setjmp: 667 case ARM::Int_eh_sjlj_setjmp_nofp: 668 return 20; 669 case ARM::tInt_eh_sjlj_setjmp: 670 case ARM::t2Int_eh_sjlj_setjmp: 671 case ARM::t2Int_eh_sjlj_setjmp_nofp: 672 return 12; 673 case ARM::SPACE: 674 return MI.getOperand(1).getImm(); 675 } 676 } 677 678 unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr &MI) const { 679 unsigned Size = 0; 680 MachineBasicBlock::const_instr_iterator I = MI.getIterator(); 681 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end(); 682 while (++I != E && I->isInsideBundle()) { 683 assert(!I->isBundle() && "No nested bundle!"); 684 Size += getInstSizeInBytes(*I); 685 } 686 return Size; 687 } 688 689 void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB, 690 MachineBasicBlock::iterator I, 691 unsigned DestReg, bool KillSrc, 692 const ARMSubtarget &Subtarget) const { 693 unsigned Opc = Subtarget.isThumb() 694 ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR) 695 : ARM::MRS; 696 697 MachineInstrBuilder MIB = 698 BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg); 699 700 // There is only 1 A/R class MRS instruction, and it always refers to 701 // APSR. However, there are lots of other possibilities on M-class cores. 702 if (Subtarget.isMClass()) 703 MIB.addImm(0x800); 704 705 MIB.add(predOps(ARMCC::AL)) 706 .addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc)); 707 } 708 709 void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB, 710 MachineBasicBlock::iterator I, 711 unsigned SrcReg, bool KillSrc, 712 const ARMSubtarget &Subtarget) const { 713 unsigned Opc = Subtarget.isThumb() 714 ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR) 715 : ARM::MSR; 716 717 MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc)); 718 719 if (Subtarget.isMClass()) 720 MIB.addImm(0x800); 721 else 722 MIB.addImm(8); 723 724 MIB.addReg(SrcReg, getKillRegState(KillSrc)) 725 .add(predOps(ARMCC::AL)) 726 .addReg(ARM::CPSR, RegState::Implicit | RegState::Define); 727 } 728 729 void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 730 MachineBasicBlock::iterator I, 731 const DebugLoc &DL, unsigned DestReg, 732 unsigned SrcReg, bool KillSrc) const { 733 bool GPRDest = ARM::GPRRegClass.contains(DestReg); 734 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg); 735 736 if (GPRDest && GPRSrc) { 737 BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg) 738 .addReg(SrcReg, getKillRegState(KillSrc)) 739 .add(predOps(ARMCC::AL)) 740 .add(condCodeOp()); 741 return; 742 } 743 744 bool SPRDest = ARM::SPRRegClass.contains(DestReg); 745 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg); 746 747 unsigned Opc = 0; 748 if (SPRDest && SPRSrc) 749 Opc = ARM::VMOVS; 750 else if (GPRDest && SPRSrc) 751 Opc = ARM::VMOVRS; 752 else if (SPRDest && GPRSrc) 753 Opc = ARM::VMOVSR; 754 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP()) 755 Opc = ARM::VMOVD; 756 else if (ARM::QPRRegClass.contains(DestReg, SrcReg)) 757 Opc = ARM::VORRq; 758 759 if (Opc) { 760 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg); 761 MIB.addReg(SrcReg, getKillRegState(KillSrc)); 762 if (Opc == ARM::VORRq) 763 MIB.addReg(SrcReg, getKillRegState(KillSrc)); 764 MIB.add(predOps(ARMCC::AL)); 765 return; 766 } 767 768 // Handle register classes that require multiple instructions. 769 unsigned BeginIdx = 0; 770 unsigned SubRegs = 0; 771 int Spacing = 1; 772 773 // Use VORRq when possible. 774 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) { 775 Opc = ARM::VORRq; 776 BeginIdx = ARM::qsub_0; 777 SubRegs = 2; 778 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) { 779 Opc = ARM::VORRq; 780 BeginIdx = ARM::qsub_0; 781 SubRegs = 4; 782 // Fall back to VMOVD. 783 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) { 784 Opc = ARM::VMOVD; 785 BeginIdx = ARM::dsub_0; 786 SubRegs = 2; 787 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) { 788 Opc = ARM::VMOVD; 789 BeginIdx = ARM::dsub_0; 790 SubRegs = 3; 791 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) { 792 Opc = ARM::VMOVD; 793 BeginIdx = ARM::dsub_0; 794 SubRegs = 4; 795 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) { 796 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr; 797 BeginIdx = ARM::gsub_0; 798 SubRegs = 2; 799 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) { 800 Opc = ARM::VMOVD; 801 BeginIdx = ARM::dsub_0; 802 SubRegs = 2; 803 Spacing = 2; 804 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) { 805 Opc = ARM::VMOVD; 806 BeginIdx = ARM::dsub_0; 807 SubRegs = 3; 808 Spacing = 2; 809 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) { 810 Opc = ARM::VMOVD; 811 BeginIdx = ARM::dsub_0; 812 SubRegs = 4; 813 Spacing = 2; 814 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) { 815 Opc = ARM::VMOVS; 816 BeginIdx = ARM::ssub_0; 817 SubRegs = 2; 818 } else if (SrcReg == ARM::CPSR) { 819 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget); 820 return; 821 } else if (DestReg == ARM::CPSR) { 822 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget); 823 return; 824 } 825 826 assert(Opc && "Impossible reg-to-reg copy"); 827 828 const TargetRegisterInfo *TRI = &getRegisterInfo(); 829 MachineInstrBuilder Mov; 830 831 // Copy register tuples backward when the first Dest reg overlaps with SrcReg. 832 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) { 833 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing); 834 Spacing = -Spacing; 835 } 836 #ifndef NDEBUG 837 SmallSet<unsigned, 4> DstRegs; 838 #endif 839 for (unsigned i = 0; i != SubRegs; ++i) { 840 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing); 841 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing); 842 assert(Dst && Src && "Bad sub-register"); 843 #ifndef NDEBUG 844 assert(!DstRegs.count(Src) && "destructive vector copy"); 845 DstRegs.insert(Dst); 846 #endif 847 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src); 848 // VORR takes two source operands. 849 if (Opc == ARM::VORRq) 850 Mov.addReg(Src); 851 Mov = Mov.add(predOps(ARMCC::AL)); 852 // MOVr can set CC. 853 if (Opc == ARM::MOVr) 854 Mov = Mov.add(condCodeOp()); 855 } 856 // Add implicit super-register defs and kills to the last instruction. 857 Mov->addRegisterDefined(DestReg, TRI); 858 if (KillSrc) 859 Mov->addRegisterKilled(SrcReg, TRI); 860 } 861 862 const MachineInstrBuilder & 863 ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg, 864 unsigned SubIdx, unsigned State, 865 const TargetRegisterInfo *TRI) const { 866 if (!SubIdx) 867 return MIB.addReg(Reg, State); 868 869 if (TargetRegisterInfo::isPhysicalRegister(Reg)) 870 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State); 871 return MIB.addReg(Reg, State, SubIdx); 872 } 873 874 void ARMBaseInstrInfo:: 875 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, 876 unsigned SrcReg, bool isKill, int FI, 877 const TargetRegisterClass *RC, 878 const TargetRegisterInfo *TRI) const { 879 DebugLoc DL; 880 if (I != MBB.end()) DL = I->getDebugLoc(); 881 MachineFunction &MF = *MBB.getParent(); 882 MachineFrameInfo &MFI = MF.getFrameInfo(); 883 unsigned Align = MFI.getObjectAlignment(FI); 884 885 MachineMemOperand *MMO = MF.getMachineMemOperand( 886 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore, 887 MFI.getObjectSize(FI), Align); 888 889 switch (RC->getSize()) { 890 case 4: 891 if (ARM::GPRRegClass.hasSubClassEq(RC)) { 892 BuildMI(MBB, I, DL, get(ARM::STRi12)) 893 .addReg(SrcReg, getKillRegState(isKill)) 894 .addFrameIndex(FI) 895 .addImm(0) 896 .addMemOperand(MMO) 897 .add(predOps(ARMCC::AL)); 898 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) { 899 BuildMI(MBB, I, DL, get(ARM::VSTRS)) 900 .addReg(SrcReg, getKillRegState(isKill)) 901 .addFrameIndex(FI) 902 .addImm(0) 903 .addMemOperand(MMO) 904 .add(predOps(ARMCC::AL)); 905 } else 906 llvm_unreachable("Unknown reg class!"); 907 break; 908 case 8: 909 if (ARM::DPRRegClass.hasSubClassEq(RC)) { 910 BuildMI(MBB, I, DL, get(ARM::VSTRD)) 911 .addReg(SrcReg, getKillRegState(isKill)) 912 .addFrameIndex(FI) 913 .addImm(0) 914 .addMemOperand(MMO) 915 .add(predOps(ARMCC::AL)); 916 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) { 917 if (Subtarget.hasV5TEOps()) { 918 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD)); 919 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI); 920 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI); 921 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO) 922 .add(predOps(ARMCC::AL)); 923 } else { 924 // Fallback to STM instruction, which has existed since the dawn of 925 // time. 926 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STMIA)) 927 .addFrameIndex(FI) 928 .addMemOperand(MMO) 929 .add(predOps(ARMCC::AL)); 930 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI); 931 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI); 932 } 933 } else 934 llvm_unreachable("Unknown reg class!"); 935 break; 936 case 16: 937 if (ARM::DPairRegClass.hasSubClassEq(RC)) { 938 // Use aligned spills if the stack can be realigned. 939 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) { 940 BuildMI(MBB, I, DL, get(ARM::VST1q64)) 941 .addFrameIndex(FI) 942 .addImm(16) 943 .addReg(SrcReg, getKillRegState(isKill)) 944 .addMemOperand(MMO) 945 .add(predOps(ARMCC::AL)); 946 } else { 947 BuildMI(MBB, I, DL, get(ARM::VSTMQIA)) 948 .addReg(SrcReg, getKillRegState(isKill)) 949 .addFrameIndex(FI) 950 .addMemOperand(MMO) 951 .add(predOps(ARMCC::AL)); 952 } 953 } else 954 llvm_unreachable("Unknown reg class!"); 955 break; 956 case 24: 957 if (ARM::DTripleRegClass.hasSubClassEq(RC)) { 958 // Use aligned spills if the stack can be realigned. 959 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) { 960 BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo)) 961 .addFrameIndex(FI) 962 .addImm(16) 963 .addReg(SrcReg, getKillRegState(isKill)) 964 .addMemOperand(MMO) 965 .add(predOps(ARMCC::AL)); 966 } else { 967 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VSTMDIA)) 968 .addFrameIndex(FI) 969 .add(predOps(ARMCC::AL)) 970 .addMemOperand(MMO); 971 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI); 972 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI); 973 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI); 974 } 975 } else 976 llvm_unreachable("Unknown reg class!"); 977 break; 978 case 32: 979 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) { 980 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) { 981 // FIXME: It's possible to only store part of the QQ register if the 982 // spilled def has a sub-register index. 983 BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo)) 984 .addFrameIndex(FI) 985 .addImm(16) 986 .addReg(SrcReg, getKillRegState(isKill)) 987 .addMemOperand(MMO) 988 .add(predOps(ARMCC::AL)); 989 } else { 990 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VSTMDIA)) 991 .addFrameIndex(FI) 992 .add(predOps(ARMCC::AL)) 993 .addMemOperand(MMO); 994 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI); 995 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI); 996 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI); 997 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI); 998 } 999 } else 1000 llvm_unreachable("Unknown reg class!"); 1001 break; 1002 case 64: 1003 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) { 1004 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VSTMDIA)) 1005 .addFrameIndex(FI) 1006 .add(predOps(ARMCC::AL)) 1007 .addMemOperand(MMO); 1008 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI); 1009 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI); 1010 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI); 1011 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI); 1012 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI); 1013 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI); 1014 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI); 1015 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI); 1016 } else 1017 llvm_unreachable("Unknown reg class!"); 1018 break; 1019 default: 1020 llvm_unreachable("Unknown reg class!"); 1021 } 1022 } 1023 1024 unsigned ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr &MI, 1025 int &FrameIndex) const { 1026 switch (MI.getOpcode()) { 1027 default: break; 1028 case ARM::STRrs: 1029 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame. 1030 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() && 1031 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 && 1032 MI.getOperand(3).getImm() == 0) { 1033 FrameIndex = MI.getOperand(1).getIndex(); 1034 return MI.getOperand(0).getReg(); 1035 } 1036 break; 1037 case ARM::STRi12: 1038 case ARM::t2STRi12: 1039 case ARM::tSTRspi: 1040 case ARM::VSTRD: 1041 case ARM::VSTRS: 1042 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() && 1043 MI.getOperand(2).getImm() == 0) { 1044 FrameIndex = MI.getOperand(1).getIndex(); 1045 return MI.getOperand(0).getReg(); 1046 } 1047 break; 1048 case ARM::VST1q64: 1049 case ARM::VST1d64TPseudo: 1050 case ARM::VST1d64QPseudo: 1051 if (MI.getOperand(0).isFI() && MI.getOperand(2).getSubReg() == 0) { 1052 FrameIndex = MI.getOperand(0).getIndex(); 1053 return MI.getOperand(2).getReg(); 1054 } 1055 break; 1056 case ARM::VSTMQIA: 1057 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) { 1058 FrameIndex = MI.getOperand(1).getIndex(); 1059 return MI.getOperand(0).getReg(); 1060 } 1061 break; 1062 } 1063 1064 return 0; 1065 } 1066 1067 unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr &MI, 1068 int &FrameIndex) const { 1069 const MachineMemOperand *Dummy; 1070 return MI.mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex); 1071 } 1072 1073 void ARMBaseInstrInfo:: 1074 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, 1075 unsigned DestReg, int FI, 1076 const TargetRegisterClass *RC, 1077 const TargetRegisterInfo *TRI) const { 1078 DebugLoc DL; 1079 if (I != MBB.end()) DL = I->getDebugLoc(); 1080 MachineFunction &MF = *MBB.getParent(); 1081 MachineFrameInfo &MFI = MF.getFrameInfo(); 1082 unsigned Align = MFI.getObjectAlignment(FI); 1083 MachineMemOperand *MMO = MF.getMachineMemOperand( 1084 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad, 1085 MFI.getObjectSize(FI), Align); 1086 1087 switch (RC->getSize()) { 1088 case 4: 1089 if (ARM::GPRRegClass.hasSubClassEq(RC)) { 1090 BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg) 1091 .addFrameIndex(FI) 1092 .addImm(0) 1093 .addMemOperand(MMO) 1094 .add(predOps(ARMCC::AL)); 1095 1096 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) { 1097 BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg) 1098 .addFrameIndex(FI) 1099 .addImm(0) 1100 .addMemOperand(MMO) 1101 .add(predOps(ARMCC::AL)); 1102 } else 1103 llvm_unreachable("Unknown reg class!"); 1104 break; 1105 case 8: 1106 if (ARM::DPRRegClass.hasSubClassEq(RC)) { 1107 BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg) 1108 .addFrameIndex(FI) 1109 .addImm(0) 1110 .addMemOperand(MMO) 1111 .add(predOps(ARMCC::AL)); 1112 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) { 1113 MachineInstrBuilder MIB; 1114 1115 if (Subtarget.hasV5TEOps()) { 1116 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD)); 1117 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI); 1118 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI); 1119 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO) 1120 .add(predOps(ARMCC::AL)); 1121 } else { 1122 // Fallback to LDM instruction, which has existed since the dawn of 1123 // time. 1124 MIB = BuildMI(MBB, I, DL, get(ARM::LDMIA)) 1125 .addFrameIndex(FI) 1126 .addMemOperand(MMO) 1127 .add(predOps(ARMCC::AL)); 1128 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI); 1129 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI); 1130 } 1131 1132 if (TargetRegisterInfo::isPhysicalRegister(DestReg)) 1133 MIB.addReg(DestReg, RegState::ImplicitDefine); 1134 } else 1135 llvm_unreachable("Unknown reg class!"); 1136 break; 1137 case 16: 1138 if (ARM::DPairRegClass.hasSubClassEq(RC)) { 1139 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) { 1140 BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg) 1141 .addFrameIndex(FI) 1142 .addImm(16) 1143 .addMemOperand(MMO) 1144 .add(predOps(ARMCC::AL)); 1145 } else { 1146 BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg) 1147 .addFrameIndex(FI) 1148 .addMemOperand(MMO) 1149 .add(predOps(ARMCC::AL)); 1150 } 1151 } else 1152 llvm_unreachable("Unknown reg class!"); 1153 break; 1154 case 24: 1155 if (ARM::DTripleRegClass.hasSubClassEq(RC)) { 1156 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) { 1157 BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg) 1158 .addFrameIndex(FI) 1159 .addImm(16) 1160 .addMemOperand(MMO) 1161 .add(predOps(ARMCC::AL)); 1162 } else { 1163 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA)) 1164 .addFrameIndex(FI) 1165 .addMemOperand(MMO) 1166 .add(predOps(ARMCC::AL)); 1167 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI); 1168 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI); 1169 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI); 1170 if (TargetRegisterInfo::isPhysicalRegister(DestReg)) 1171 MIB.addReg(DestReg, RegState::ImplicitDefine); 1172 } 1173 } else 1174 llvm_unreachable("Unknown reg class!"); 1175 break; 1176 case 32: 1177 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) { 1178 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) { 1179 BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg) 1180 .addFrameIndex(FI) 1181 .addImm(16) 1182 .addMemOperand(MMO) 1183 .add(predOps(ARMCC::AL)); 1184 } else { 1185 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA)) 1186 .addFrameIndex(FI) 1187 .add(predOps(ARMCC::AL)) 1188 .addMemOperand(MMO); 1189 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI); 1190 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI); 1191 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI); 1192 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI); 1193 if (TargetRegisterInfo::isPhysicalRegister(DestReg)) 1194 MIB.addReg(DestReg, RegState::ImplicitDefine); 1195 } 1196 } else 1197 llvm_unreachable("Unknown reg class!"); 1198 break; 1199 case 64: 1200 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) { 1201 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA)) 1202 .addFrameIndex(FI) 1203 .add(predOps(ARMCC::AL)) 1204 .addMemOperand(MMO); 1205 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI); 1206 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI); 1207 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI); 1208 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI); 1209 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI); 1210 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI); 1211 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI); 1212 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI); 1213 if (TargetRegisterInfo::isPhysicalRegister(DestReg)) 1214 MIB.addReg(DestReg, RegState::ImplicitDefine); 1215 } else 1216 llvm_unreachable("Unknown reg class!"); 1217 break; 1218 default: 1219 llvm_unreachable("Unknown regclass!"); 1220 } 1221 } 1222 1223 unsigned ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, 1224 int &FrameIndex) const { 1225 switch (MI.getOpcode()) { 1226 default: break; 1227 case ARM::LDRrs: 1228 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame. 1229 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() && 1230 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 && 1231 MI.getOperand(3).getImm() == 0) { 1232 FrameIndex = MI.getOperand(1).getIndex(); 1233 return MI.getOperand(0).getReg(); 1234 } 1235 break; 1236 case ARM::LDRi12: 1237 case ARM::t2LDRi12: 1238 case ARM::tLDRspi: 1239 case ARM::VLDRD: 1240 case ARM::VLDRS: 1241 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() && 1242 MI.getOperand(2).getImm() == 0) { 1243 FrameIndex = MI.getOperand(1).getIndex(); 1244 return MI.getOperand(0).getReg(); 1245 } 1246 break; 1247 case ARM::VLD1q64: 1248 case ARM::VLD1d64TPseudo: 1249 case ARM::VLD1d64QPseudo: 1250 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) { 1251 FrameIndex = MI.getOperand(1).getIndex(); 1252 return MI.getOperand(0).getReg(); 1253 } 1254 break; 1255 case ARM::VLDMQIA: 1256 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) { 1257 FrameIndex = MI.getOperand(1).getIndex(); 1258 return MI.getOperand(0).getReg(); 1259 } 1260 break; 1261 } 1262 1263 return 0; 1264 } 1265 1266 unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr &MI, 1267 int &FrameIndex) const { 1268 const MachineMemOperand *Dummy; 1269 return MI.mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex); 1270 } 1271 1272 /// \brief Expands MEMCPY to either LDMIA/STMIA or LDMIA_UPD/STMID_UPD 1273 /// depending on whether the result is used. 1274 void ARMBaseInstrInfo::expandMEMCPY(MachineBasicBlock::iterator MI) const { 1275 bool isThumb1 = Subtarget.isThumb1Only(); 1276 bool isThumb2 = Subtarget.isThumb2(); 1277 const ARMBaseInstrInfo *TII = Subtarget.getInstrInfo(); 1278 1279 DebugLoc dl = MI->getDebugLoc(); 1280 MachineBasicBlock *BB = MI->getParent(); 1281 1282 MachineInstrBuilder LDM, STM; 1283 if (isThumb1 || !MI->getOperand(1).isDead()) { 1284 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA_UPD 1285 : isThumb1 ? ARM::tLDMIA_UPD 1286 : ARM::LDMIA_UPD)) 1287 .add(MI->getOperand(1)); 1288 } else { 1289 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA : ARM::LDMIA)); 1290 } 1291 1292 if (isThumb1 || !MI->getOperand(0).isDead()) { 1293 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA_UPD 1294 : isThumb1 ? ARM::tSTMIA_UPD 1295 : ARM::STMIA_UPD)) 1296 .add(MI->getOperand(0)); 1297 } else { 1298 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA : ARM::STMIA)); 1299 } 1300 1301 LDM.add(MI->getOperand(3)).add(predOps(ARMCC::AL)); 1302 STM.add(MI->getOperand(2)).add(predOps(ARMCC::AL)); 1303 1304 // Sort the scratch registers into ascending order. 1305 const TargetRegisterInfo &TRI = getRegisterInfo(); 1306 llvm::SmallVector<unsigned, 6> ScratchRegs; 1307 for(unsigned I = 5; I < MI->getNumOperands(); ++I) 1308 ScratchRegs.push_back(MI->getOperand(I).getReg()); 1309 std::sort(ScratchRegs.begin(), ScratchRegs.end(), 1310 [&TRI](const unsigned &Reg1, 1311 const unsigned &Reg2) -> bool { 1312 return TRI.getEncodingValue(Reg1) < 1313 TRI.getEncodingValue(Reg2); 1314 }); 1315 1316 for (const auto &Reg : ScratchRegs) { 1317 LDM.addReg(Reg, RegState::Define); 1318 STM.addReg(Reg, RegState::Kill); 1319 } 1320 1321 BB->erase(MI); 1322 } 1323 1324 1325 bool ARMBaseInstrInfo::expandPostRAPseudo(MachineInstr &MI) const { 1326 if (MI.getOpcode() == TargetOpcode::LOAD_STACK_GUARD) { 1327 assert(getSubtarget().getTargetTriple().isOSBinFormatMachO() && 1328 "LOAD_STACK_GUARD currently supported only for MachO."); 1329 expandLoadStackGuard(MI); 1330 MI.getParent()->erase(MI); 1331 return true; 1332 } 1333 1334 if (MI.getOpcode() == ARM::MEMCPY) { 1335 expandMEMCPY(MI); 1336 return true; 1337 } 1338 1339 // This hook gets to expand COPY instructions before they become 1340 // copyPhysReg() calls. Look for VMOVS instructions that can legally be 1341 // widened to VMOVD. We prefer the VMOVD when possible because it may be 1342 // changed into a VORR that can go down the NEON pipeline. 1343 if (!MI.isCopy() || Subtarget.dontWidenVMOVS() || Subtarget.isFPOnlySP()) 1344 return false; 1345 1346 // Look for a copy between even S-registers. That is where we keep floats 1347 // when using NEON v2f32 instructions for f32 arithmetic. 1348 unsigned DstRegS = MI.getOperand(0).getReg(); 1349 unsigned SrcRegS = MI.getOperand(1).getReg(); 1350 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS)) 1351 return false; 1352 1353 const TargetRegisterInfo *TRI = &getRegisterInfo(); 1354 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0, 1355 &ARM::DPRRegClass); 1356 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0, 1357 &ARM::DPRRegClass); 1358 if (!DstRegD || !SrcRegD) 1359 return false; 1360 1361 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only 1362 // legal if the COPY already defines the full DstRegD, and it isn't a 1363 // sub-register insertion. 1364 if (!MI.definesRegister(DstRegD, TRI) || MI.readsRegister(DstRegD, TRI)) 1365 return false; 1366 1367 // A dead copy shouldn't show up here, but reject it just in case. 1368 if (MI.getOperand(0).isDead()) 1369 return false; 1370 1371 // All clear, widen the COPY. 1372 DEBUG(dbgs() << "widening: " << MI); 1373 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI); 1374 1375 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg 1376 // or some other super-register. 1377 int ImpDefIdx = MI.findRegisterDefOperandIdx(DstRegD); 1378 if (ImpDefIdx != -1) 1379 MI.RemoveOperand(ImpDefIdx); 1380 1381 // Change the opcode and operands. 1382 MI.setDesc(get(ARM::VMOVD)); 1383 MI.getOperand(0).setReg(DstRegD); 1384 MI.getOperand(1).setReg(SrcRegD); 1385 MIB.add(predOps(ARMCC::AL)); 1386 1387 // We are now reading SrcRegD instead of SrcRegS. This may upset the 1388 // register scavenger and machine verifier, so we need to indicate that we 1389 // are reading an undefined value from SrcRegD, but a proper value from 1390 // SrcRegS. 1391 MI.getOperand(1).setIsUndef(); 1392 MIB.addReg(SrcRegS, RegState::Implicit); 1393 1394 // SrcRegD may actually contain an unrelated value in the ssub_1 1395 // sub-register. Don't kill it. Only kill the ssub_0 sub-register. 1396 if (MI.getOperand(1).isKill()) { 1397 MI.getOperand(1).setIsKill(false); 1398 MI.addRegisterKilled(SrcRegS, TRI, true); 1399 } 1400 1401 DEBUG(dbgs() << "replaced by: " << MI); 1402 return true; 1403 } 1404 1405 /// Create a copy of a const pool value. Update CPI to the new index and return 1406 /// the label UID. 1407 static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) { 1408 MachineConstantPool *MCP = MF.getConstantPool(); 1409 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1410 1411 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI]; 1412 assert(MCPE.isMachineConstantPoolEntry() && 1413 "Expecting a machine constantpool entry!"); 1414 ARMConstantPoolValue *ACPV = 1415 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal); 1416 1417 unsigned PCLabelId = AFI->createPICLabelUId(); 1418 ARMConstantPoolValue *NewCPV = nullptr; 1419 1420 // FIXME: The below assumes PIC relocation model and that the function 1421 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and 1422 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR 1423 // instructions, so that's probably OK, but is PIC always correct when 1424 // we get here? 1425 if (ACPV->isGlobalValue()) 1426 NewCPV = ARMConstantPoolConstant::Create( 1427 cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId, ARMCP::CPValue, 1428 4, ACPV->getModifier(), ACPV->mustAddCurrentAddress()); 1429 else if (ACPV->isExtSymbol()) 1430 NewCPV = ARMConstantPoolSymbol:: 1431 Create(MF.getFunction()->getContext(), 1432 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4); 1433 else if (ACPV->isBlockAddress()) 1434 NewCPV = ARMConstantPoolConstant:: 1435 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId, 1436 ARMCP::CPBlockAddress, 4); 1437 else if (ACPV->isLSDA()) 1438 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId, 1439 ARMCP::CPLSDA, 4); 1440 else if (ACPV->isMachineBasicBlock()) 1441 NewCPV = ARMConstantPoolMBB:: 1442 Create(MF.getFunction()->getContext(), 1443 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4); 1444 else 1445 llvm_unreachable("Unexpected ARM constantpool value type!!"); 1446 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment()); 1447 return PCLabelId; 1448 } 1449 1450 void ARMBaseInstrInfo::reMaterialize(MachineBasicBlock &MBB, 1451 MachineBasicBlock::iterator I, 1452 unsigned DestReg, unsigned SubIdx, 1453 const MachineInstr &Orig, 1454 const TargetRegisterInfo &TRI) const { 1455 unsigned Opcode = Orig.getOpcode(); 1456 switch (Opcode) { 1457 default: { 1458 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig); 1459 MI->substituteRegister(Orig.getOperand(0).getReg(), DestReg, SubIdx, TRI); 1460 MBB.insert(I, MI); 1461 break; 1462 } 1463 case ARM::tLDRpci_pic: 1464 case ARM::t2LDRpci_pic: { 1465 MachineFunction &MF = *MBB.getParent(); 1466 unsigned CPI = Orig.getOperand(1).getIndex(); 1467 unsigned PCLabelId = duplicateCPV(MF, CPI); 1468 MachineInstrBuilder MIB = 1469 BuildMI(MBB, I, Orig.getDebugLoc(), get(Opcode), DestReg) 1470 .addConstantPoolIndex(CPI) 1471 .addImm(PCLabelId); 1472 MIB->setMemRefs(Orig.memoperands_begin(), Orig.memoperands_end()); 1473 break; 1474 } 1475 } 1476 } 1477 1478 MachineInstr *ARMBaseInstrInfo::duplicate(MachineInstr &Orig, 1479 MachineFunction &MF) const { 1480 MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF); 1481 switch (Orig.getOpcode()) { 1482 case ARM::tLDRpci_pic: 1483 case ARM::t2LDRpci_pic: { 1484 unsigned CPI = Orig.getOperand(1).getIndex(); 1485 unsigned PCLabelId = duplicateCPV(MF, CPI); 1486 Orig.getOperand(1).setIndex(CPI); 1487 Orig.getOperand(2).setImm(PCLabelId); 1488 break; 1489 } 1490 } 1491 return MI; 1492 } 1493 1494 bool ARMBaseInstrInfo::produceSameValue(const MachineInstr &MI0, 1495 const MachineInstr &MI1, 1496 const MachineRegisterInfo *MRI) const { 1497 unsigned Opcode = MI0.getOpcode(); 1498 if (Opcode == ARM::t2LDRpci || 1499 Opcode == ARM::t2LDRpci_pic || 1500 Opcode == ARM::tLDRpci || 1501 Opcode == ARM::tLDRpci_pic || 1502 Opcode == ARM::LDRLIT_ga_pcrel || 1503 Opcode == ARM::LDRLIT_ga_pcrel_ldr || 1504 Opcode == ARM::tLDRLIT_ga_pcrel || 1505 Opcode == ARM::MOV_ga_pcrel || 1506 Opcode == ARM::MOV_ga_pcrel_ldr || 1507 Opcode == ARM::t2MOV_ga_pcrel) { 1508 if (MI1.getOpcode() != Opcode) 1509 return false; 1510 if (MI0.getNumOperands() != MI1.getNumOperands()) 1511 return false; 1512 1513 const MachineOperand &MO0 = MI0.getOperand(1); 1514 const MachineOperand &MO1 = MI1.getOperand(1); 1515 if (MO0.getOffset() != MO1.getOffset()) 1516 return false; 1517 1518 if (Opcode == ARM::LDRLIT_ga_pcrel || 1519 Opcode == ARM::LDRLIT_ga_pcrel_ldr || 1520 Opcode == ARM::tLDRLIT_ga_pcrel || 1521 Opcode == ARM::MOV_ga_pcrel || 1522 Opcode == ARM::MOV_ga_pcrel_ldr || 1523 Opcode == ARM::t2MOV_ga_pcrel) 1524 // Ignore the PC labels. 1525 return MO0.getGlobal() == MO1.getGlobal(); 1526 1527 const MachineFunction *MF = MI0.getParent()->getParent(); 1528 const MachineConstantPool *MCP = MF->getConstantPool(); 1529 int CPI0 = MO0.getIndex(); 1530 int CPI1 = MO1.getIndex(); 1531 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0]; 1532 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1]; 1533 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry(); 1534 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry(); 1535 if (isARMCP0 && isARMCP1) { 1536 ARMConstantPoolValue *ACPV0 = 1537 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal); 1538 ARMConstantPoolValue *ACPV1 = 1539 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal); 1540 return ACPV0->hasSameValue(ACPV1); 1541 } else if (!isARMCP0 && !isARMCP1) { 1542 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal; 1543 } 1544 return false; 1545 } else if (Opcode == ARM::PICLDR) { 1546 if (MI1.getOpcode() != Opcode) 1547 return false; 1548 if (MI0.getNumOperands() != MI1.getNumOperands()) 1549 return false; 1550 1551 unsigned Addr0 = MI0.getOperand(1).getReg(); 1552 unsigned Addr1 = MI1.getOperand(1).getReg(); 1553 if (Addr0 != Addr1) { 1554 if (!MRI || 1555 !TargetRegisterInfo::isVirtualRegister(Addr0) || 1556 !TargetRegisterInfo::isVirtualRegister(Addr1)) 1557 return false; 1558 1559 // This assumes SSA form. 1560 MachineInstr *Def0 = MRI->getVRegDef(Addr0); 1561 MachineInstr *Def1 = MRI->getVRegDef(Addr1); 1562 // Check if the loaded value, e.g. a constantpool of a global address, are 1563 // the same. 1564 if (!produceSameValue(*Def0, *Def1, MRI)) 1565 return false; 1566 } 1567 1568 for (unsigned i = 3, e = MI0.getNumOperands(); i != e; ++i) { 1569 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg 1570 const MachineOperand &MO0 = MI0.getOperand(i); 1571 const MachineOperand &MO1 = MI1.getOperand(i); 1572 if (!MO0.isIdenticalTo(MO1)) 1573 return false; 1574 } 1575 return true; 1576 } 1577 1578 return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs); 1579 } 1580 1581 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to 1582 /// determine if two loads are loading from the same base address. It should 1583 /// only return true if the base pointers are the same and the only differences 1584 /// between the two addresses is the offset. It also returns the offsets by 1585 /// reference. 1586 /// 1587 /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched 1588 /// is permanently disabled. 1589 bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2, 1590 int64_t &Offset1, 1591 int64_t &Offset2) const { 1592 // Don't worry about Thumb: just ARM and Thumb2. 1593 if (Subtarget.isThumb1Only()) return false; 1594 1595 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode()) 1596 return false; 1597 1598 switch (Load1->getMachineOpcode()) { 1599 default: 1600 return false; 1601 case ARM::LDRi12: 1602 case ARM::LDRBi12: 1603 case ARM::LDRD: 1604 case ARM::LDRH: 1605 case ARM::LDRSB: 1606 case ARM::LDRSH: 1607 case ARM::VLDRD: 1608 case ARM::VLDRS: 1609 case ARM::t2LDRi8: 1610 case ARM::t2LDRBi8: 1611 case ARM::t2LDRDi8: 1612 case ARM::t2LDRSHi8: 1613 case ARM::t2LDRi12: 1614 case ARM::t2LDRBi12: 1615 case ARM::t2LDRSHi12: 1616 break; 1617 } 1618 1619 switch (Load2->getMachineOpcode()) { 1620 default: 1621 return false; 1622 case ARM::LDRi12: 1623 case ARM::LDRBi12: 1624 case ARM::LDRD: 1625 case ARM::LDRH: 1626 case ARM::LDRSB: 1627 case ARM::LDRSH: 1628 case ARM::VLDRD: 1629 case ARM::VLDRS: 1630 case ARM::t2LDRi8: 1631 case ARM::t2LDRBi8: 1632 case ARM::t2LDRSHi8: 1633 case ARM::t2LDRi12: 1634 case ARM::t2LDRBi12: 1635 case ARM::t2LDRSHi12: 1636 break; 1637 } 1638 1639 // Check if base addresses and chain operands match. 1640 if (Load1->getOperand(0) != Load2->getOperand(0) || 1641 Load1->getOperand(4) != Load2->getOperand(4)) 1642 return false; 1643 1644 // Index should be Reg0. 1645 if (Load1->getOperand(3) != Load2->getOperand(3)) 1646 return false; 1647 1648 // Determine the offsets. 1649 if (isa<ConstantSDNode>(Load1->getOperand(1)) && 1650 isa<ConstantSDNode>(Load2->getOperand(1))) { 1651 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue(); 1652 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue(); 1653 return true; 1654 } 1655 1656 return false; 1657 } 1658 1659 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to 1660 /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should 1661 /// be scheduled togther. On some targets if two loads are loading from 1662 /// addresses in the same cache line, it's better if they are scheduled 1663 /// together. This function takes two integers that represent the load offsets 1664 /// from the common base address. It returns true if it decides it's desirable 1665 /// to schedule the two loads together. "NumLoads" is the number of loads that 1666 /// have already been scheduled after Load1. 1667 /// 1668 /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched 1669 /// is permanently disabled. 1670 bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2, 1671 int64_t Offset1, int64_t Offset2, 1672 unsigned NumLoads) const { 1673 // Don't worry about Thumb: just ARM and Thumb2. 1674 if (Subtarget.isThumb1Only()) return false; 1675 1676 assert(Offset2 > Offset1); 1677 1678 if ((Offset2 - Offset1) / 8 > 64) 1679 return false; 1680 1681 // Check if the machine opcodes are different. If they are different 1682 // then we consider them to not be of the same base address, 1683 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12. 1684 // In this case, they are considered to be the same because they are different 1685 // encoding forms of the same basic instruction. 1686 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) && 1687 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 && 1688 Load2->getMachineOpcode() == ARM::t2LDRBi12) || 1689 (Load1->getMachineOpcode() == ARM::t2LDRBi12 && 1690 Load2->getMachineOpcode() == ARM::t2LDRBi8))) 1691 return false; // FIXME: overly conservative? 1692 1693 // Four loads in a row should be sufficient. 1694 if (NumLoads >= 3) 1695 return false; 1696 1697 return true; 1698 } 1699 1700 bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr &MI, 1701 const MachineBasicBlock *MBB, 1702 const MachineFunction &MF) const { 1703 // Debug info is never a scheduling boundary. It's necessary to be explicit 1704 // due to the special treatment of IT instructions below, otherwise a 1705 // dbg_value followed by an IT will result in the IT instruction being 1706 // considered a scheduling hazard, which is wrong. It should be the actual 1707 // instruction preceding the dbg_value instruction(s), just like it is 1708 // when debug info is not present. 1709 if (MI.isDebugValue()) 1710 return false; 1711 1712 // Terminators and labels can't be scheduled around. 1713 if (MI.isTerminator() || MI.isPosition()) 1714 return true; 1715 1716 // Treat the start of the IT block as a scheduling boundary, but schedule 1717 // t2IT along with all instructions following it. 1718 // FIXME: This is a big hammer. But the alternative is to add all potential 1719 // true and anti dependencies to IT block instructions as implicit operands 1720 // to the t2IT instruction. The added compile time and complexity does not 1721 // seem worth it. 1722 MachineBasicBlock::const_iterator I = MI; 1723 // Make sure to skip any dbg_value instructions 1724 while (++I != MBB->end() && I->isDebugValue()) 1725 ; 1726 if (I != MBB->end() && I->getOpcode() == ARM::t2IT) 1727 return true; 1728 1729 // Don't attempt to schedule around any instruction that defines 1730 // a stack-oriented pointer, as it's unlikely to be profitable. This 1731 // saves compile time, because it doesn't require every single 1732 // stack slot reference to depend on the instruction that does the 1733 // modification. 1734 // Calls don't actually change the stack pointer, even if they have imp-defs. 1735 // No ARM calling conventions change the stack pointer. (X86 calling 1736 // conventions sometimes do). 1737 if (!MI.isCall() && MI.definesRegister(ARM::SP)) 1738 return true; 1739 1740 return false; 1741 } 1742 1743 bool ARMBaseInstrInfo:: 1744 isProfitableToIfCvt(MachineBasicBlock &MBB, 1745 unsigned NumCycles, unsigned ExtraPredCycles, 1746 BranchProbability Probability) const { 1747 if (!NumCycles) 1748 return false; 1749 1750 // If we are optimizing for size, see if the branch in the predecessor can be 1751 // lowered to cbn?z by the constant island lowering pass, and return false if 1752 // so. This results in a shorter instruction sequence. 1753 if (MBB.getParent()->getFunction()->optForSize()) { 1754 MachineBasicBlock *Pred = *MBB.pred_begin(); 1755 if (!Pred->empty()) { 1756 MachineInstr *LastMI = &*Pred->rbegin(); 1757 if (LastMI->getOpcode() == ARM::t2Bcc) { 1758 MachineBasicBlock::iterator CmpMI = LastMI; 1759 if (CmpMI != Pred->begin()) { 1760 --CmpMI; 1761 if (CmpMI->getOpcode() == ARM::tCMPi8 || 1762 CmpMI->getOpcode() == ARM::t2CMPri) { 1763 unsigned Reg = CmpMI->getOperand(0).getReg(); 1764 unsigned PredReg = 0; 1765 ARMCC::CondCodes P = getInstrPredicate(*CmpMI, PredReg); 1766 if (P == ARMCC::AL && CmpMI->getOperand(1).getImm() == 0 && 1767 isARMLowRegister(Reg)) 1768 return false; 1769 } 1770 } 1771 } 1772 } 1773 } 1774 1775 // Attempt to estimate the relative costs of predication versus branching. 1776 // Here we scale up each component of UnpredCost to avoid precision issue when 1777 // scaling NumCycles by Probability. 1778 const unsigned ScalingUpFactor = 1024; 1779 unsigned UnpredCost = Probability.scale(NumCycles * ScalingUpFactor); 1780 UnpredCost += ScalingUpFactor; // The branch itself 1781 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10; 1782 1783 return (NumCycles + ExtraPredCycles) * ScalingUpFactor <= UnpredCost; 1784 } 1785 1786 bool ARMBaseInstrInfo:: 1787 isProfitableToIfCvt(MachineBasicBlock &TMBB, 1788 unsigned TCycles, unsigned TExtra, 1789 MachineBasicBlock &FMBB, 1790 unsigned FCycles, unsigned FExtra, 1791 BranchProbability Probability) const { 1792 if (!TCycles || !FCycles) 1793 return false; 1794 1795 // Attempt to estimate the relative costs of predication versus branching. 1796 // Here we scale up each component of UnpredCost to avoid precision issue when 1797 // scaling TCycles/FCycles by Probability. 1798 const unsigned ScalingUpFactor = 1024; 1799 unsigned TUnpredCost = Probability.scale(TCycles * ScalingUpFactor); 1800 unsigned FUnpredCost = 1801 Probability.getCompl().scale(FCycles * ScalingUpFactor); 1802 unsigned UnpredCost = TUnpredCost + FUnpredCost; 1803 UnpredCost += 1 * ScalingUpFactor; // The branch itself 1804 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10; 1805 1806 return (TCycles + FCycles + TExtra + FExtra) * ScalingUpFactor <= UnpredCost; 1807 } 1808 1809 bool 1810 ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB, 1811 MachineBasicBlock &FMBB) const { 1812 // Reduce false anti-dependencies to let the target's out-of-order execution 1813 // engine do its thing. 1814 return Subtarget.isProfitableToUnpredicate(); 1815 } 1816 1817 /// getInstrPredicate - If instruction is predicated, returns its predicate 1818 /// condition, otherwise returns AL. It also returns the condition code 1819 /// register by reference. 1820 ARMCC::CondCodes llvm::getInstrPredicate(const MachineInstr &MI, 1821 unsigned &PredReg) { 1822 int PIdx = MI.findFirstPredOperandIdx(); 1823 if (PIdx == -1) { 1824 PredReg = 0; 1825 return ARMCC::AL; 1826 } 1827 1828 PredReg = MI.getOperand(PIdx+1).getReg(); 1829 return (ARMCC::CondCodes)MI.getOperand(PIdx).getImm(); 1830 } 1831 1832 1833 unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) { 1834 if (Opc == ARM::B) 1835 return ARM::Bcc; 1836 if (Opc == ARM::tB) 1837 return ARM::tBcc; 1838 if (Opc == ARM::t2B) 1839 return ARM::t2Bcc; 1840 1841 llvm_unreachable("Unknown unconditional branch opcode!"); 1842 } 1843 1844 MachineInstr *ARMBaseInstrInfo::commuteInstructionImpl(MachineInstr &MI, 1845 bool NewMI, 1846 unsigned OpIdx1, 1847 unsigned OpIdx2) const { 1848 switch (MI.getOpcode()) { 1849 case ARM::MOVCCr: 1850 case ARM::t2MOVCCr: { 1851 // MOVCC can be commuted by inverting the condition. 1852 unsigned PredReg = 0; 1853 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg); 1854 // MOVCC AL can't be inverted. Shouldn't happen. 1855 if (CC == ARMCC::AL || PredReg != ARM::CPSR) 1856 return nullptr; 1857 MachineInstr *CommutedMI = 1858 TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); 1859 if (!CommutedMI) 1860 return nullptr; 1861 // After swapping the MOVCC operands, also invert the condition. 1862 CommutedMI->getOperand(CommutedMI->findFirstPredOperandIdx()) 1863 .setImm(ARMCC::getOppositeCondition(CC)); 1864 return CommutedMI; 1865 } 1866 } 1867 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); 1868 } 1869 1870 /// Identify instructions that can be folded into a MOVCC instruction, and 1871 /// return the defining instruction. 1872 static MachineInstr *canFoldIntoMOVCC(unsigned Reg, 1873 const MachineRegisterInfo &MRI, 1874 const TargetInstrInfo *TII) { 1875 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 1876 return nullptr; 1877 if (!MRI.hasOneNonDBGUse(Reg)) 1878 return nullptr; 1879 MachineInstr *MI = MRI.getVRegDef(Reg); 1880 if (!MI) 1881 return nullptr; 1882 // MI is folded into the MOVCC by predicating it. 1883 if (!MI->isPredicable()) 1884 return nullptr; 1885 // Check if MI has any non-dead defs or physreg uses. This also detects 1886 // predicated instructions which will be reading CPSR. 1887 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) { 1888 const MachineOperand &MO = MI->getOperand(i); 1889 // Reject frame index operands, PEI can't handle the predicated pseudos. 1890 if (MO.isFI() || MO.isCPI() || MO.isJTI()) 1891 return nullptr; 1892 if (!MO.isReg()) 1893 continue; 1894 // MI can't have any tied operands, that would conflict with predication. 1895 if (MO.isTied()) 1896 return nullptr; 1897 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) 1898 return nullptr; 1899 if (MO.isDef() && !MO.isDead()) 1900 return nullptr; 1901 } 1902 bool DontMoveAcrossStores = true; 1903 if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores)) 1904 return nullptr; 1905 return MI; 1906 } 1907 1908 bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr &MI, 1909 SmallVectorImpl<MachineOperand> &Cond, 1910 unsigned &TrueOp, unsigned &FalseOp, 1911 bool &Optimizable) const { 1912 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) && 1913 "Unknown select instruction"); 1914 // MOVCC operands: 1915 // 0: Def. 1916 // 1: True use. 1917 // 2: False use. 1918 // 3: Condition code. 1919 // 4: CPSR use. 1920 TrueOp = 1; 1921 FalseOp = 2; 1922 Cond.push_back(MI.getOperand(3)); 1923 Cond.push_back(MI.getOperand(4)); 1924 // We can always fold a def. 1925 Optimizable = true; 1926 return false; 1927 } 1928 1929 MachineInstr * 1930 ARMBaseInstrInfo::optimizeSelect(MachineInstr &MI, 1931 SmallPtrSetImpl<MachineInstr *> &SeenMIs, 1932 bool PreferFalse) const { 1933 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) && 1934 "Unknown select instruction"); 1935 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 1936 MachineInstr *DefMI = canFoldIntoMOVCC(MI.getOperand(2).getReg(), MRI, this); 1937 bool Invert = !DefMI; 1938 if (!DefMI) 1939 DefMI = canFoldIntoMOVCC(MI.getOperand(1).getReg(), MRI, this); 1940 if (!DefMI) 1941 return nullptr; 1942 1943 // Find new register class to use. 1944 MachineOperand FalseReg = MI.getOperand(Invert ? 2 : 1); 1945 unsigned DestReg = MI.getOperand(0).getReg(); 1946 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg()); 1947 if (!MRI.constrainRegClass(DestReg, PreviousClass)) 1948 return nullptr; 1949 1950 // Create a new predicated version of DefMI. 1951 // Rfalse is the first use. 1952 MachineInstrBuilder NewMI = 1953 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), DefMI->getDesc(), DestReg); 1954 1955 // Copy all the DefMI operands, excluding its (null) predicate. 1956 const MCInstrDesc &DefDesc = DefMI->getDesc(); 1957 for (unsigned i = 1, e = DefDesc.getNumOperands(); 1958 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i) 1959 NewMI.add(DefMI->getOperand(i)); 1960 1961 unsigned CondCode = MI.getOperand(3).getImm(); 1962 if (Invert) 1963 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode))); 1964 else 1965 NewMI.addImm(CondCode); 1966 NewMI.add(MI.getOperand(4)); 1967 1968 // DefMI is not the -S version that sets CPSR, so add an optional %noreg. 1969 if (NewMI->hasOptionalDef()) 1970 NewMI.add(condCodeOp()); 1971 1972 // The output register value when the predicate is false is an implicit 1973 // register operand tied to the first def. 1974 // The tie makes the register allocator ensure the FalseReg is allocated the 1975 // same register as operand 0. 1976 FalseReg.setImplicit(); 1977 NewMI.add(FalseReg); 1978 NewMI->tieOperands(0, NewMI->getNumOperands() - 1); 1979 1980 // Update SeenMIs set: register newly created MI and erase removed DefMI. 1981 SeenMIs.insert(NewMI); 1982 SeenMIs.erase(DefMI); 1983 1984 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on 1985 // DefMI would be invalid when tranferred inside the loop. Checking for a 1986 // loop is expensive, but at least remove kill flags if they are in different 1987 // BBs. 1988 if (DefMI->getParent() != MI.getParent()) 1989 NewMI->clearKillInfo(); 1990 1991 // The caller will erase MI, but not DefMI. 1992 DefMI->eraseFromParent(); 1993 return NewMI; 1994 } 1995 1996 /// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the 1997 /// instruction is encoded with an 'S' bit is determined by the optional CPSR 1998 /// def operand. 1999 /// 2000 /// This will go away once we can teach tblgen how to set the optional CPSR def 2001 /// operand itself. 2002 struct AddSubFlagsOpcodePair { 2003 uint16_t PseudoOpc; 2004 uint16_t MachineOpc; 2005 }; 2006 2007 static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = { 2008 {ARM::ADDSri, ARM::ADDri}, 2009 {ARM::ADDSrr, ARM::ADDrr}, 2010 {ARM::ADDSrsi, ARM::ADDrsi}, 2011 {ARM::ADDSrsr, ARM::ADDrsr}, 2012 2013 {ARM::SUBSri, ARM::SUBri}, 2014 {ARM::SUBSrr, ARM::SUBrr}, 2015 {ARM::SUBSrsi, ARM::SUBrsi}, 2016 {ARM::SUBSrsr, ARM::SUBrsr}, 2017 2018 {ARM::RSBSri, ARM::RSBri}, 2019 {ARM::RSBSrsi, ARM::RSBrsi}, 2020 {ARM::RSBSrsr, ARM::RSBrsr}, 2021 2022 {ARM::t2ADDSri, ARM::t2ADDri}, 2023 {ARM::t2ADDSrr, ARM::t2ADDrr}, 2024 {ARM::t2ADDSrs, ARM::t2ADDrs}, 2025 2026 {ARM::t2SUBSri, ARM::t2SUBri}, 2027 {ARM::t2SUBSrr, ARM::t2SUBrr}, 2028 {ARM::t2SUBSrs, ARM::t2SUBrs}, 2029 2030 {ARM::t2RSBSri, ARM::t2RSBri}, 2031 {ARM::t2RSBSrs, ARM::t2RSBrs}, 2032 }; 2033 2034 unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) { 2035 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i) 2036 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc) 2037 return AddSubFlagsOpcodeMap[i].MachineOpc; 2038 return 0; 2039 } 2040 2041 void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB, 2042 MachineBasicBlock::iterator &MBBI, 2043 const DebugLoc &dl, unsigned DestReg, 2044 unsigned BaseReg, int NumBytes, 2045 ARMCC::CondCodes Pred, unsigned PredReg, 2046 const ARMBaseInstrInfo &TII, 2047 unsigned MIFlags) { 2048 if (NumBytes == 0 && DestReg != BaseReg) { 2049 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg) 2050 .addReg(BaseReg, RegState::Kill) 2051 .addImm((unsigned)Pred).addReg(PredReg).addReg(0) 2052 .setMIFlags(MIFlags); 2053 return; 2054 } 2055 2056 bool isSub = NumBytes < 0; 2057 if (isSub) NumBytes = -NumBytes; 2058 2059 while (NumBytes) { 2060 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes); 2061 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt); 2062 assert(ThisVal && "Didn't extract field correctly"); 2063 2064 // We will handle these bits from offset, clear them. 2065 NumBytes &= ~ThisVal; 2066 2067 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?"); 2068 2069 // Build the new ADD / SUB. 2070 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri; 2071 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg) 2072 .addReg(BaseReg, RegState::Kill).addImm(ThisVal) 2073 .addImm((unsigned)Pred).addReg(PredReg).addReg(0) 2074 .setMIFlags(MIFlags); 2075 BaseReg = DestReg; 2076 } 2077 } 2078 2079 bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget, 2080 MachineFunction &MF, MachineInstr *MI, 2081 unsigned NumBytes) { 2082 // This optimisation potentially adds lots of load and store 2083 // micro-operations, it's only really a great benefit to code-size. 2084 if (!MF.getFunction()->optForMinSize()) 2085 return false; 2086 2087 // If only one register is pushed/popped, LLVM can use an LDR/STR 2088 // instead. We can't modify those so make sure we're dealing with an 2089 // instruction we understand. 2090 bool IsPop = isPopOpcode(MI->getOpcode()); 2091 bool IsPush = isPushOpcode(MI->getOpcode()); 2092 if (!IsPush && !IsPop) 2093 return false; 2094 2095 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD || 2096 MI->getOpcode() == ARM::VLDMDIA_UPD; 2097 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH || 2098 MI->getOpcode() == ARM::tPOP || 2099 MI->getOpcode() == ARM::tPOP_RET; 2100 2101 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP && 2102 MI->getOperand(1).getReg() == ARM::SP)) && 2103 "trying to fold sp update into non-sp-updating push/pop"); 2104 2105 // The VFP push & pop act on D-registers, so we can only fold an adjustment 2106 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try 2107 // if this is violated. 2108 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0) 2109 return false; 2110 2111 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+ 2112 // pred) so the list starts at 4. Thumb1 starts after the predicate. 2113 int RegListIdx = IsT1PushPop ? 2 : 4; 2114 2115 // Calculate the space we'll need in terms of registers. 2116 unsigned RegsNeeded; 2117 const TargetRegisterClass *RegClass; 2118 if (IsVFPPushPop) { 2119 RegsNeeded = NumBytes / 8; 2120 RegClass = &ARM::DPRRegClass; 2121 } else { 2122 RegsNeeded = NumBytes / 4; 2123 RegClass = &ARM::GPRRegClass; 2124 } 2125 2126 // We're going to have to strip all list operands off before 2127 // re-adding them since the order matters, so save the existing ones 2128 // for later. 2129 SmallVector<MachineOperand, 4> RegList; 2130 2131 // We're also going to need the first register transferred by this 2132 // instruction, which won't necessarily be the first register in the list. 2133 unsigned FirstRegEnc = -1; 2134 2135 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo(); 2136 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i) { 2137 MachineOperand &MO = MI->getOperand(i); 2138 RegList.push_back(MO); 2139 2140 if (MO.isReg() && TRI->getEncodingValue(MO.getReg()) < FirstRegEnc) 2141 FirstRegEnc = TRI->getEncodingValue(MO.getReg()); 2142 } 2143 2144 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF); 2145 2146 // Now try to find enough space in the reglist to allocate NumBytes. 2147 for (int CurRegEnc = FirstRegEnc - 1; CurRegEnc >= 0 && RegsNeeded; 2148 --CurRegEnc) { 2149 unsigned CurReg = RegClass->getRegister(CurRegEnc); 2150 if (!IsPop) { 2151 // Pushing any register is completely harmless, mark the 2152 // register involved as undef since we don't care about it in 2153 // the slightest. 2154 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false, 2155 false, false, true)); 2156 --RegsNeeded; 2157 continue; 2158 } 2159 2160 // However, we can only pop an extra register if it's not live. For 2161 // registers live within the function we might clobber a return value 2162 // register; the other way a register can be live here is if it's 2163 // callee-saved. 2164 if (isCalleeSavedRegister(CurReg, CSRegs) || 2165 MI->getParent()->computeRegisterLiveness(TRI, CurReg, MI) != 2166 MachineBasicBlock::LQR_Dead) { 2167 // VFP pops don't allow holes in the register list, so any skip is fatal 2168 // for our transformation. GPR pops do, so we should just keep looking. 2169 if (IsVFPPushPop) 2170 return false; 2171 else 2172 continue; 2173 } 2174 2175 // Mark the unimportant registers as <def,dead> in the POP. 2176 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false, 2177 true)); 2178 --RegsNeeded; 2179 } 2180 2181 if (RegsNeeded > 0) 2182 return false; 2183 2184 // Finally we know we can profitably perform the optimisation so go 2185 // ahead: strip all existing registers off and add them back again 2186 // in the right order. 2187 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i) 2188 MI->RemoveOperand(i); 2189 2190 // Add the complete list back in. 2191 MachineInstrBuilder MIB(MF, &*MI); 2192 for (int i = RegList.size() - 1; i >= 0; --i) 2193 MIB.add(RegList[i]); 2194 2195 return true; 2196 } 2197 2198 bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx, 2199 unsigned FrameReg, int &Offset, 2200 const ARMBaseInstrInfo &TII) { 2201 unsigned Opcode = MI.getOpcode(); 2202 const MCInstrDesc &Desc = MI.getDesc(); 2203 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask); 2204 bool isSub = false; 2205 2206 // Memory operands in inline assembly always use AddrMode2. 2207 if (Opcode == ARM::INLINEASM) 2208 AddrMode = ARMII::AddrMode2; 2209 2210 if (Opcode == ARM::ADDri) { 2211 Offset += MI.getOperand(FrameRegIdx+1).getImm(); 2212 if (Offset == 0) { 2213 // Turn it into a move. 2214 MI.setDesc(TII.get(ARM::MOVr)); 2215 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); 2216 MI.RemoveOperand(FrameRegIdx+1); 2217 Offset = 0; 2218 return true; 2219 } else if (Offset < 0) { 2220 Offset = -Offset; 2221 isSub = true; 2222 MI.setDesc(TII.get(ARM::SUBri)); 2223 } 2224 2225 // Common case: small offset, fits into instruction. 2226 if (ARM_AM::getSOImmVal(Offset) != -1) { 2227 // Replace the FrameIndex with sp / fp 2228 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); 2229 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset); 2230 Offset = 0; 2231 return true; 2232 } 2233 2234 // Otherwise, pull as much of the immedidate into this ADDri/SUBri 2235 // as possible. 2236 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset); 2237 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt); 2238 2239 // We will handle these bits from offset, clear them. 2240 Offset &= ~ThisImmVal; 2241 2242 // Get the properly encoded SOImmVal field. 2243 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 && 2244 "Bit extraction didn't work?"); 2245 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal); 2246 } else { 2247 unsigned ImmIdx = 0; 2248 int InstrOffs = 0; 2249 unsigned NumBits = 0; 2250 unsigned Scale = 1; 2251 switch (AddrMode) { 2252 case ARMII::AddrMode_i12: { 2253 ImmIdx = FrameRegIdx + 1; 2254 InstrOffs = MI.getOperand(ImmIdx).getImm(); 2255 NumBits = 12; 2256 break; 2257 } 2258 case ARMII::AddrMode2: { 2259 ImmIdx = FrameRegIdx+2; 2260 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm()); 2261 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub) 2262 InstrOffs *= -1; 2263 NumBits = 12; 2264 break; 2265 } 2266 case ARMII::AddrMode3: { 2267 ImmIdx = FrameRegIdx+2; 2268 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm()); 2269 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub) 2270 InstrOffs *= -1; 2271 NumBits = 8; 2272 break; 2273 } 2274 case ARMII::AddrMode4: 2275 case ARMII::AddrMode6: 2276 // Can't fold any offset even if it's zero. 2277 return false; 2278 case ARMII::AddrMode5: { 2279 ImmIdx = FrameRegIdx+1; 2280 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm()); 2281 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub) 2282 InstrOffs *= -1; 2283 NumBits = 8; 2284 Scale = 4; 2285 break; 2286 } 2287 default: 2288 llvm_unreachable("Unsupported addressing mode!"); 2289 } 2290 2291 Offset += InstrOffs * Scale; 2292 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!"); 2293 if (Offset < 0) { 2294 Offset = -Offset; 2295 isSub = true; 2296 } 2297 2298 // Attempt to fold address comp. if opcode has offset bits 2299 if (NumBits > 0) { 2300 // Common case: small offset, fits into instruction. 2301 MachineOperand &ImmOp = MI.getOperand(ImmIdx); 2302 int ImmedOffset = Offset / Scale; 2303 unsigned Mask = (1 << NumBits) - 1; 2304 if ((unsigned)Offset <= Mask * Scale) { 2305 // Replace the FrameIndex with sp 2306 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); 2307 // FIXME: When addrmode2 goes away, this will simplify (like the 2308 // T2 version), as the LDR.i12 versions don't need the encoding 2309 // tricks for the offset value. 2310 if (isSub) { 2311 if (AddrMode == ARMII::AddrMode_i12) 2312 ImmedOffset = -ImmedOffset; 2313 else 2314 ImmedOffset |= 1 << NumBits; 2315 } 2316 ImmOp.ChangeToImmediate(ImmedOffset); 2317 Offset = 0; 2318 return true; 2319 } 2320 2321 // Otherwise, it didn't fit. Pull in what we can to simplify the immed. 2322 ImmedOffset = ImmedOffset & Mask; 2323 if (isSub) { 2324 if (AddrMode == ARMII::AddrMode_i12) 2325 ImmedOffset = -ImmedOffset; 2326 else 2327 ImmedOffset |= 1 << NumBits; 2328 } 2329 ImmOp.ChangeToImmediate(ImmedOffset); 2330 Offset &= ~(Mask*Scale); 2331 } 2332 } 2333 2334 Offset = (isSub) ? -Offset : Offset; 2335 return Offset == 0; 2336 } 2337 2338 /// analyzeCompare - For a comparison instruction, return the source registers 2339 /// in SrcReg and SrcReg2 if having two register operands, and the value it 2340 /// compares against in CmpValue. Return true if the comparison instruction 2341 /// can be analyzed. 2342 bool ARMBaseInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg, 2343 unsigned &SrcReg2, int &CmpMask, 2344 int &CmpValue) const { 2345 switch (MI.getOpcode()) { 2346 default: break; 2347 case ARM::CMPri: 2348 case ARM::t2CMPri: 2349 case ARM::tCMPi8: 2350 SrcReg = MI.getOperand(0).getReg(); 2351 SrcReg2 = 0; 2352 CmpMask = ~0; 2353 CmpValue = MI.getOperand(1).getImm(); 2354 return true; 2355 case ARM::CMPrr: 2356 case ARM::t2CMPrr: 2357 SrcReg = MI.getOperand(0).getReg(); 2358 SrcReg2 = MI.getOperand(1).getReg(); 2359 CmpMask = ~0; 2360 CmpValue = 0; 2361 return true; 2362 case ARM::TSTri: 2363 case ARM::t2TSTri: 2364 SrcReg = MI.getOperand(0).getReg(); 2365 SrcReg2 = 0; 2366 CmpMask = MI.getOperand(1).getImm(); 2367 CmpValue = 0; 2368 return true; 2369 } 2370 2371 return false; 2372 } 2373 2374 /// isSuitableForMask - Identify a suitable 'and' instruction that 2375 /// operates on the given source register and applies the same mask 2376 /// as a 'tst' instruction. Provide a limited look-through for copies. 2377 /// When successful, MI will hold the found instruction. 2378 static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg, 2379 int CmpMask, bool CommonUse) { 2380 switch (MI->getOpcode()) { 2381 case ARM::ANDri: 2382 case ARM::t2ANDri: 2383 if (CmpMask != MI->getOperand(2).getImm()) 2384 return false; 2385 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg()) 2386 return true; 2387 break; 2388 } 2389 2390 return false; 2391 } 2392 2393 /// getSwappedCondition - assume the flags are set by MI(a,b), return 2394 /// the condition code if we modify the instructions such that flags are 2395 /// set by MI(b,a). 2396 inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) { 2397 switch (CC) { 2398 default: return ARMCC::AL; 2399 case ARMCC::EQ: return ARMCC::EQ; 2400 case ARMCC::NE: return ARMCC::NE; 2401 case ARMCC::HS: return ARMCC::LS; 2402 case ARMCC::LO: return ARMCC::HI; 2403 case ARMCC::HI: return ARMCC::LO; 2404 case ARMCC::LS: return ARMCC::HS; 2405 case ARMCC::GE: return ARMCC::LE; 2406 case ARMCC::LT: return ARMCC::GT; 2407 case ARMCC::GT: return ARMCC::LT; 2408 case ARMCC::LE: return ARMCC::GE; 2409 } 2410 } 2411 2412 /// isRedundantFlagInstr - check whether the first instruction, whose only 2413 /// purpose is to update flags, can be made redundant. 2414 /// CMPrr can be made redundant by SUBrr if the operands are the same. 2415 /// CMPri can be made redundant by SUBri if the operands are the same. 2416 /// This function can be extended later on. 2417 inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg, 2418 unsigned SrcReg2, int ImmValue, 2419 MachineInstr *OI) { 2420 if ((CmpI->getOpcode() == ARM::CMPrr || 2421 CmpI->getOpcode() == ARM::t2CMPrr) && 2422 (OI->getOpcode() == ARM::SUBrr || 2423 OI->getOpcode() == ARM::t2SUBrr) && 2424 ((OI->getOperand(1).getReg() == SrcReg && 2425 OI->getOperand(2).getReg() == SrcReg2) || 2426 (OI->getOperand(1).getReg() == SrcReg2 && 2427 OI->getOperand(2).getReg() == SrcReg))) 2428 return true; 2429 2430 if ((CmpI->getOpcode() == ARM::CMPri || 2431 CmpI->getOpcode() == ARM::t2CMPri) && 2432 (OI->getOpcode() == ARM::SUBri || 2433 OI->getOpcode() == ARM::t2SUBri) && 2434 OI->getOperand(1).getReg() == SrcReg && 2435 OI->getOperand(2).getImm() == ImmValue) 2436 return true; 2437 return false; 2438 } 2439 2440 /// optimizeCompareInstr - Convert the instruction supplying the argument to the 2441 /// comparison into one that sets the zero bit in the flags register; 2442 /// Remove a redundant Compare instruction if an earlier instruction can set the 2443 /// flags in the same way as Compare. 2444 /// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two 2445 /// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the 2446 /// condition code of instructions which use the flags. 2447 bool ARMBaseInstrInfo::optimizeCompareInstr( 2448 MachineInstr &CmpInstr, unsigned SrcReg, unsigned SrcReg2, int CmpMask, 2449 int CmpValue, const MachineRegisterInfo *MRI) const { 2450 // Get the unique definition of SrcReg. 2451 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg); 2452 if (!MI) return false; 2453 2454 // Masked compares sometimes use the same register as the corresponding 'and'. 2455 if (CmpMask != ~0) { 2456 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(*MI)) { 2457 MI = nullptr; 2458 for (MachineRegisterInfo::use_instr_iterator 2459 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end(); 2460 UI != UE; ++UI) { 2461 if (UI->getParent() != CmpInstr.getParent()) 2462 continue; 2463 MachineInstr *PotentialAND = &*UI; 2464 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) || 2465 isPredicated(*PotentialAND)) 2466 continue; 2467 MI = PotentialAND; 2468 break; 2469 } 2470 if (!MI) return false; 2471 } 2472 } 2473 2474 // Get ready to iterate backward from CmpInstr. 2475 MachineBasicBlock::iterator I = CmpInstr, E = MI, 2476 B = CmpInstr.getParent()->begin(); 2477 2478 // Early exit if CmpInstr is at the beginning of the BB. 2479 if (I == B) return false; 2480 2481 // There are two possible candidates which can be changed to set CPSR: 2482 // One is MI, the other is a SUB instruction. 2483 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1). 2484 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue). 2485 MachineInstr *Sub = nullptr; 2486 if (SrcReg2 != 0) 2487 // MI is not a candidate for CMPrr. 2488 MI = nullptr; 2489 else if (MI->getParent() != CmpInstr.getParent() || CmpValue != 0) { 2490 // Conservatively refuse to convert an instruction which isn't in the same 2491 // BB as the comparison. 2492 // For CMPri w/ CmpValue != 0, a Sub may still be a candidate. 2493 // Thus we cannot return here. 2494 if (CmpInstr.getOpcode() == ARM::CMPri || 2495 CmpInstr.getOpcode() == ARM::t2CMPri) 2496 MI = nullptr; 2497 else 2498 return false; 2499 } 2500 2501 // Check that CPSR isn't set between the comparison instruction and the one we 2502 // want to change. At the same time, search for Sub. 2503 const TargetRegisterInfo *TRI = &getRegisterInfo(); 2504 --I; 2505 for (; I != E; --I) { 2506 const MachineInstr &Instr = *I; 2507 2508 if (Instr.modifiesRegister(ARM::CPSR, TRI) || 2509 Instr.readsRegister(ARM::CPSR, TRI)) 2510 // This instruction modifies or uses CPSR after the one we want to 2511 // change. We can't do this transformation. 2512 return false; 2513 2514 // Check whether CmpInstr can be made redundant by the current instruction. 2515 if (isRedundantFlagInstr(&CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) { 2516 Sub = &*I; 2517 break; 2518 } 2519 2520 if (I == B) 2521 // The 'and' is below the comparison instruction. 2522 return false; 2523 } 2524 2525 // Return false if no candidates exist. 2526 if (!MI && !Sub) 2527 return false; 2528 2529 // The single candidate is called MI. 2530 if (!MI) MI = Sub; 2531 2532 // We can't use a predicated instruction - it doesn't always write the flags. 2533 if (isPredicated(*MI)) 2534 return false; 2535 2536 bool IsThumb1 = false; 2537 switch (MI->getOpcode()) { 2538 default: break; 2539 case ARM::tLSLri: 2540 case ARM::tLSRri: 2541 case ARM::tLSLrr: 2542 case ARM::tLSRrr: 2543 case ARM::tSUBrr: 2544 case ARM::tADDrr: 2545 case ARM::tADDi3: 2546 case ARM::tADDi8: 2547 case ARM::tSUBi3: 2548 case ARM::tSUBi8: 2549 IsThumb1 = true; 2550 LLVM_FALLTHROUGH; 2551 case ARM::RSBrr: 2552 case ARM::RSBri: 2553 case ARM::RSCrr: 2554 case ARM::RSCri: 2555 case ARM::ADDrr: 2556 case ARM::ADDri: 2557 case ARM::ADCrr: 2558 case ARM::ADCri: 2559 case ARM::SUBrr: 2560 case ARM::SUBri: 2561 case ARM::SBCrr: 2562 case ARM::SBCri: 2563 case ARM::t2RSBri: 2564 case ARM::t2ADDrr: 2565 case ARM::t2ADDri: 2566 case ARM::t2ADCrr: 2567 case ARM::t2ADCri: 2568 case ARM::t2SUBrr: 2569 case ARM::t2SUBri: 2570 case ARM::t2SBCrr: 2571 case ARM::t2SBCri: 2572 case ARM::ANDrr: 2573 case ARM::ANDri: 2574 case ARM::t2ANDrr: 2575 case ARM::t2ANDri: 2576 case ARM::ORRrr: 2577 case ARM::ORRri: 2578 case ARM::t2ORRrr: 2579 case ARM::t2ORRri: 2580 case ARM::EORrr: 2581 case ARM::EORri: 2582 case ARM::t2EORrr: 2583 case ARM::t2EORri: 2584 case ARM::t2LSRri: 2585 case ARM::t2LSRrr: 2586 case ARM::t2LSLri: 2587 case ARM::t2LSLrr: { 2588 // Scan forward for the use of CPSR 2589 // When checking against MI: if it's a conditional code that requires 2590 // checking of the V bit or C bit, then this is not safe to do. 2591 // It is safe to remove CmpInstr if CPSR is redefined or killed. 2592 // If we are done with the basic block, we need to check whether CPSR is 2593 // live-out. 2594 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4> 2595 OperandsToUpdate; 2596 bool isSafe = false; 2597 I = CmpInstr; 2598 E = CmpInstr.getParent()->end(); 2599 while (!isSafe && ++I != E) { 2600 const MachineInstr &Instr = *I; 2601 for (unsigned IO = 0, EO = Instr.getNumOperands(); 2602 !isSafe && IO != EO; ++IO) { 2603 const MachineOperand &MO = Instr.getOperand(IO); 2604 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) { 2605 isSafe = true; 2606 break; 2607 } 2608 if (!MO.isReg() || MO.getReg() != ARM::CPSR) 2609 continue; 2610 if (MO.isDef()) { 2611 isSafe = true; 2612 break; 2613 } 2614 // Condition code is after the operand before CPSR except for VSELs. 2615 ARMCC::CondCodes CC; 2616 bool IsInstrVSel = true; 2617 switch (Instr.getOpcode()) { 2618 default: 2619 IsInstrVSel = false; 2620 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm(); 2621 break; 2622 case ARM::VSELEQD: 2623 case ARM::VSELEQS: 2624 CC = ARMCC::EQ; 2625 break; 2626 case ARM::VSELGTD: 2627 case ARM::VSELGTS: 2628 CC = ARMCC::GT; 2629 break; 2630 case ARM::VSELGED: 2631 case ARM::VSELGES: 2632 CC = ARMCC::GE; 2633 break; 2634 case ARM::VSELVSS: 2635 case ARM::VSELVSD: 2636 CC = ARMCC::VS; 2637 break; 2638 } 2639 2640 if (Sub) { 2641 ARMCC::CondCodes NewCC = getSwappedCondition(CC); 2642 if (NewCC == ARMCC::AL) 2643 return false; 2644 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based 2645 // on CMP needs to be updated to be based on SUB. 2646 // Push the condition code operands to OperandsToUpdate. 2647 // If it is safe to remove CmpInstr, the condition code of these 2648 // operands will be modified. 2649 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 && 2650 Sub->getOperand(2).getReg() == SrcReg) { 2651 // VSel doesn't support condition code update. 2652 if (IsInstrVSel) 2653 return false; 2654 OperandsToUpdate.push_back( 2655 std::make_pair(&((*I).getOperand(IO - 1)), NewCC)); 2656 } 2657 } else { 2658 // No Sub, so this is x = <op> y, z; cmp x, 0. 2659 switch (CC) { 2660 case ARMCC::EQ: // Z 2661 case ARMCC::NE: // Z 2662 case ARMCC::MI: // N 2663 case ARMCC::PL: // N 2664 case ARMCC::AL: // none 2665 // CPSR can be used multiple times, we should continue. 2666 break; 2667 case ARMCC::HS: // C 2668 case ARMCC::LO: // C 2669 case ARMCC::VS: // V 2670 case ARMCC::VC: // V 2671 case ARMCC::HI: // C Z 2672 case ARMCC::LS: // C Z 2673 case ARMCC::GE: // N V 2674 case ARMCC::LT: // N V 2675 case ARMCC::GT: // Z N V 2676 case ARMCC::LE: // Z N V 2677 // The instruction uses the V bit or C bit which is not safe. 2678 return false; 2679 } 2680 } 2681 } 2682 } 2683 2684 // If CPSR is not killed nor re-defined, we should check whether it is 2685 // live-out. If it is live-out, do not optimize. 2686 if (!isSafe) { 2687 MachineBasicBlock *MBB = CmpInstr.getParent(); 2688 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), 2689 SE = MBB->succ_end(); SI != SE; ++SI) 2690 if ((*SI)->isLiveIn(ARM::CPSR)) 2691 return false; 2692 } 2693 2694 // Toggle the optional operand to CPSR (if it exists - in Thumb1 we always 2695 // set CPSR so this is represented as an explicit output) 2696 if (!IsThumb1) { 2697 MI->getOperand(5).setReg(ARM::CPSR); 2698 MI->getOperand(5).setIsDef(true); 2699 } 2700 assert(!isPredicated(*MI) && "Can't use flags from predicated instruction"); 2701 CmpInstr.eraseFromParent(); 2702 2703 // Modify the condition code of operands in OperandsToUpdate. 2704 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to 2705 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc. 2706 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++) 2707 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second); 2708 return true; 2709 } 2710 } 2711 2712 return false; 2713 } 2714 2715 bool ARMBaseInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, 2716 unsigned Reg, 2717 MachineRegisterInfo *MRI) const { 2718 // Fold large immediates into add, sub, or, xor. 2719 unsigned DefOpc = DefMI.getOpcode(); 2720 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm) 2721 return false; 2722 if (!DefMI.getOperand(1).isImm()) 2723 // Could be t2MOVi32imm <ga:xx> 2724 return false; 2725 2726 if (!MRI->hasOneNonDBGUse(Reg)) 2727 return false; 2728 2729 const MCInstrDesc &DefMCID = DefMI.getDesc(); 2730 if (DefMCID.hasOptionalDef()) { 2731 unsigned NumOps = DefMCID.getNumOperands(); 2732 const MachineOperand &MO = DefMI.getOperand(NumOps - 1); 2733 if (MO.getReg() == ARM::CPSR && !MO.isDead()) 2734 // If DefMI defines CPSR and it is not dead, it's obviously not safe 2735 // to delete DefMI. 2736 return false; 2737 } 2738 2739 const MCInstrDesc &UseMCID = UseMI.getDesc(); 2740 if (UseMCID.hasOptionalDef()) { 2741 unsigned NumOps = UseMCID.getNumOperands(); 2742 if (UseMI.getOperand(NumOps - 1).getReg() == ARM::CPSR) 2743 // If the instruction sets the flag, do not attempt this optimization 2744 // since it may change the semantics of the code. 2745 return false; 2746 } 2747 2748 unsigned UseOpc = UseMI.getOpcode(); 2749 unsigned NewUseOpc = 0; 2750 uint32_t ImmVal = (uint32_t)DefMI.getOperand(1).getImm(); 2751 uint32_t SOImmValV1 = 0, SOImmValV2 = 0; 2752 bool Commute = false; 2753 switch (UseOpc) { 2754 default: return false; 2755 case ARM::SUBrr: 2756 case ARM::ADDrr: 2757 case ARM::ORRrr: 2758 case ARM::EORrr: 2759 case ARM::t2SUBrr: 2760 case ARM::t2ADDrr: 2761 case ARM::t2ORRrr: 2762 case ARM::t2EORrr: { 2763 Commute = UseMI.getOperand(2).getReg() != Reg; 2764 switch (UseOpc) { 2765 default: break; 2766 case ARM::ADDrr: 2767 case ARM::SUBrr: { 2768 if (UseOpc == ARM::SUBrr && Commute) 2769 return false; 2770 2771 // ADD/SUB are special because they're essentially the same operation, so 2772 // we can handle a larger range of immediates. 2773 if (ARM_AM::isSOImmTwoPartVal(ImmVal)) 2774 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::ADDri : ARM::SUBri; 2775 else if (ARM_AM::isSOImmTwoPartVal(-ImmVal)) { 2776 ImmVal = -ImmVal; 2777 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::SUBri : ARM::ADDri; 2778 } else 2779 return false; 2780 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal); 2781 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal); 2782 break; 2783 } 2784 case ARM::ORRrr: 2785 case ARM::EORrr: { 2786 if (!ARM_AM::isSOImmTwoPartVal(ImmVal)) 2787 return false; 2788 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal); 2789 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal); 2790 switch (UseOpc) { 2791 default: break; 2792 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break; 2793 case ARM::EORrr: NewUseOpc = ARM::EORri; break; 2794 } 2795 break; 2796 } 2797 case ARM::t2ADDrr: 2798 case ARM::t2SUBrr: { 2799 if (UseOpc == ARM::t2SUBrr && Commute) 2800 return false; 2801 2802 // ADD/SUB are special because they're essentially the same operation, so 2803 // we can handle a larger range of immediates. 2804 if (ARM_AM::isT2SOImmTwoPartVal(ImmVal)) 2805 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2ADDri : ARM::t2SUBri; 2806 else if (ARM_AM::isT2SOImmTwoPartVal(-ImmVal)) { 2807 ImmVal = -ImmVal; 2808 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2SUBri : ARM::t2ADDri; 2809 } else 2810 return false; 2811 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal); 2812 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal); 2813 break; 2814 } 2815 case ARM::t2ORRrr: 2816 case ARM::t2EORrr: { 2817 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal)) 2818 return false; 2819 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal); 2820 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal); 2821 switch (UseOpc) { 2822 default: break; 2823 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break; 2824 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break; 2825 } 2826 break; 2827 } 2828 } 2829 } 2830 } 2831 2832 unsigned OpIdx = Commute ? 2 : 1; 2833 unsigned Reg1 = UseMI.getOperand(OpIdx).getReg(); 2834 bool isKill = UseMI.getOperand(OpIdx).isKill(); 2835 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg)); 2836 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), get(NewUseOpc), 2837 NewReg) 2838 .addReg(Reg1, getKillRegState(isKill)) 2839 .addImm(SOImmValV1) 2840 .add(predOps(ARMCC::AL)) 2841 .add(condCodeOp()); 2842 UseMI.setDesc(get(NewUseOpc)); 2843 UseMI.getOperand(1).setReg(NewReg); 2844 UseMI.getOperand(1).setIsKill(); 2845 UseMI.getOperand(2).ChangeToImmediate(SOImmValV2); 2846 DefMI.eraseFromParent(); 2847 return true; 2848 } 2849 2850 static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData, 2851 const MachineInstr &MI) { 2852 switch (MI.getOpcode()) { 2853 default: { 2854 const MCInstrDesc &Desc = MI.getDesc(); 2855 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass()); 2856 assert(UOps >= 0 && "bad # UOps"); 2857 return UOps; 2858 } 2859 2860 case ARM::LDRrs: 2861 case ARM::LDRBrs: 2862 case ARM::STRrs: 2863 case ARM::STRBrs: { 2864 unsigned ShOpVal = MI.getOperand(3).getImm(); 2865 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub; 2866 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 2867 if (!isSub && 2868 (ShImm == 0 || 2869 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 2870 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))) 2871 return 1; 2872 return 2; 2873 } 2874 2875 case ARM::LDRH: 2876 case ARM::STRH: { 2877 if (!MI.getOperand(2).getReg()) 2878 return 1; 2879 2880 unsigned ShOpVal = MI.getOperand(3).getImm(); 2881 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub; 2882 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 2883 if (!isSub && 2884 (ShImm == 0 || 2885 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 2886 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))) 2887 return 1; 2888 return 2; 2889 } 2890 2891 case ARM::LDRSB: 2892 case ARM::LDRSH: 2893 return (ARM_AM::getAM3Op(MI.getOperand(3).getImm()) == ARM_AM::sub) ? 3 : 2; 2894 2895 case ARM::LDRSB_POST: 2896 case ARM::LDRSH_POST: { 2897 unsigned Rt = MI.getOperand(0).getReg(); 2898 unsigned Rm = MI.getOperand(3).getReg(); 2899 return (Rt == Rm) ? 4 : 3; 2900 } 2901 2902 case ARM::LDR_PRE_REG: 2903 case ARM::LDRB_PRE_REG: { 2904 unsigned Rt = MI.getOperand(0).getReg(); 2905 unsigned Rm = MI.getOperand(3).getReg(); 2906 if (Rt == Rm) 2907 return 3; 2908 unsigned ShOpVal = MI.getOperand(4).getImm(); 2909 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub; 2910 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 2911 if (!isSub && 2912 (ShImm == 0 || 2913 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 2914 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))) 2915 return 2; 2916 return 3; 2917 } 2918 2919 case ARM::STR_PRE_REG: 2920 case ARM::STRB_PRE_REG: { 2921 unsigned ShOpVal = MI.getOperand(4).getImm(); 2922 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub; 2923 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 2924 if (!isSub && 2925 (ShImm == 0 || 2926 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 2927 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))) 2928 return 2; 2929 return 3; 2930 } 2931 2932 case ARM::LDRH_PRE: 2933 case ARM::STRH_PRE: { 2934 unsigned Rt = MI.getOperand(0).getReg(); 2935 unsigned Rm = MI.getOperand(3).getReg(); 2936 if (!Rm) 2937 return 2; 2938 if (Rt == Rm) 2939 return 3; 2940 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 3 : 2; 2941 } 2942 2943 case ARM::LDR_POST_REG: 2944 case ARM::LDRB_POST_REG: 2945 case ARM::LDRH_POST: { 2946 unsigned Rt = MI.getOperand(0).getReg(); 2947 unsigned Rm = MI.getOperand(3).getReg(); 2948 return (Rt == Rm) ? 3 : 2; 2949 } 2950 2951 case ARM::LDR_PRE_IMM: 2952 case ARM::LDRB_PRE_IMM: 2953 case ARM::LDR_POST_IMM: 2954 case ARM::LDRB_POST_IMM: 2955 case ARM::STRB_POST_IMM: 2956 case ARM::STRB_POST_REG: 2957 case ARM::STRB_PRE_IMM: 2958 case ARM::STRH_POST: 2959 case ARM::STR_POST_IMM: 2960 case ARM::STR_POST_REG: 2961 case ARM::STR_PRE_IMM: 2962 return 2; 2963 2964 case ARM::LDRSB_PRE: 2965 case ARM::LDRSH_PRE: { 2966 unsigned Rm = MI.getOperand(3).getReg(); 2967 if (Rm == 0) 2968 return 3; 2969 unsigned Rt = MI.getOperand(0).getReg(); 2970 if (Rt == Rm) 2971 return 4; 2972 unsigned ShOpVal = MI.getOperand(4).getImm(); 2973 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub; 2974 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 2975 if (!isSub && 2976 (ShImm == 0 || 2977 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 2978 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))) 2979 return 3; 2980 return 4; 2981 } 2982 2983 case ARM::LDRD: { 2984 unsigned Rt = MI.getOperand(0).getReg(); 2985 unsigned Rn = MI.getOperand(2).getReg(); 2986 unsigned Rm = MI.getOperand(3).getReg(); 2987 if (Rm) 2988 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4 2989 : 3; 2990 return (Rt == Rn) ? 3 : 2; 2991 } 2992 2993 case ARM::STRD: { 2994 unsigned Rm = MI.getOperand(3).getReg(); 2995 if (Rm) 2996 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4 2997 : 3; 2998 return 2; 2999 } 3000 3001 case ARM::LDRD_POST: 3002 case ARM::t2LDRD_POST: 3003 return 3; 3004 3005 case ARM::STRD_POST: 3006 case ARM::t2STRD_POST: 3007 return 4; 3008 3009 case ARM::LDRD_PRE: { 3010 unsigned Rt = MI.getOperand(0).getReg(); 3011 unsigned Rn = MI.getOperand(3).getReg(); 3012 unsigned Rm = MI.getOperand(4).getReg(); 3013 if (Rm) 3014 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5 3015 : 4; 3016 return (Rt == Rn) ? 4 : 3; 3017 } 3018 3019 case ARM::t2LDRD_PRE: { 3020 unsigned Rt = MI.getOperand(0).getReg(); 3021 unsigned Rn = MI.getOperand(3).getReg(); 3022 return (Rt == Rn) ? 4 : 3; 3023 } 3024 3025 case ARM::STRD_PRE: { 3026 unsigned Rm = MI.getOperand(4).getReg(); 3027 if (Rm) 3028 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5 3029 : 4; 3030 return 3; 3031 } 3032 3033 case ARM::t2STRD_PRE: 3034 return 3; 3035 3036 case ARM::t2LDR_POST: 3037 case ARM::t2LDRB_POST: 3038 case ARM::t2LDRB_PRE: 3039 case ARM::t2LDRSBi12: 3040 case ARM::t2LDRSBi8: 3041 case ARM::t2LDRSBpci: 3042 case ARM::t2LDRSBs: 3043 case ARM::t2LDRH_POST: 3044 case ARM::t2LDRH_PRE: 3045 case ARM::t2LDRSBT: 3046 case ARM::t2LDRSB_POST: 3047 case ARM::t2LDRSB_PRE: 3048 case ARM::t2LDRSH_POST: 3049 case ARM::t2LDRSH_PRE: 3050 case ARM::t2LDRSHi12: 3051 case ARM::t2LDRSHi8: 3052 case ARM::t2LDRSHpci: 3053 case ARM::t2LDRSHs: 3054 return 2; 3055 3056 case ARM::t2LDRDi8: { 3057 unsigned Rt = MI.getOperand(0).getReg(); 3058 unsigned Rn = MI.getOperand(2).getReg(); 3059 return (Rt == Rn) ? 3 : 2; 3060 } 3061 3062 case ARM::t2STRB_POST: 3063 case ARM::t2STRB_PRE: 3064 case ARM::t2STRBs: 3065 case ARM::t2STRDi8: 3066 case ARM::t2STRH_POST: 3067 case ARM::t2STRH_PRE: 3068 case ARM::t2STRHs: 3069 case ARM::t2STR_POST: 3070 case ARM::t2STR_PRE: 3071 case ARM::t2STRs: 3072 return 2; 3073 } 3074 } 3075 3076 // Return the number of 32-bit words loaded by LDM or stored by STM. If this 3077 // can't be easily determined return 0 (missing MachineMemOperand). 3078 // 3079 // FIXME: The current MachineInstr design does not support relying on machine 3080 // mem operands to determine the width of a memory access. Instead, we expect 3081 // the target to provide this information based on the instruction opcode and 3082 // operands. However, using MachineMemOperand is the best solution now for 3083 // two reasons: 3084 // 3085 // 1) getNumMicroOps tries to infer LDM memory width from the total number of MI 3086 // operands. This is much more dangerous than using the MachineMemOperand 3087 // sizes because CodeGen passes can insert/remove optional machine operands. In 3088 // fact, it's totally incorrect for preRA passes and appears to be wrong for 3089 // postRA passes as well. 3090 // 3091 // 2) getNumLDMAddresses is only used by the scheduling machine model and any 3092 // machine model that calls this should handle the unknown (zero size) case. 3093 // 3094 // Long term, we should require a target hook that verifies MachineMemOperand 3095 // sizes during MC lowering. That target hook should be local to MC lowering 3096 // because we can't ensure that it is aware of other MI forms. Doing this will 3097 // ensure that MachineMemOperands are correctly propagated through all passes. 3098 unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr &MI) const { 3099 unsigned Size = 0; 3100 for (MachineInstr::mmo_iterator I = MI.memoperands_begin(), 3101 E = MI.memoperands_end(); 3102 I != E; ++I) { 3103 Size += (*I)->getSize(); 3104 } 3105 return Size / 4; 3106 } 3107 3108 static unsigned getNumMicroOpsSingleIssuePlusExtras(unsigned Opc, 3109 unsigned NumRegs) { 3110 unsigned UOps = 1 + NumRegs; // 1 for address computation. 3111 switch (Opc) { 3112 default: 3113 break; 3114 case ARM::VLDMDIA_UPD: 3115 case ARM::VLDMDDB_UPD: 3116 case ARM::VLDMSIA_UPD: 3117 case ARM::VLDMSDB_UPD: 3118 case ARM::VSTMDIA_UPD: 3119 case ARM::VSTMDDB_UPD: 3120 case ARM::VSTMSIA_UPD: 3121 case ARM::VSTMSDB_UPD: 3122 case ARM::LDMIA_UPD: 3123 case ARM::LDMDA_UPD: 3124 case ARM::LDMDB_UPD: 3125 case ARM::LDMIB_UPD: 3126 case ARM::STMIA_UPD: 3127 case ARM::STMDA_UPD: 3128 case ARM::STMDB_UPD: 3129 case ARM::STMIB_UPD: 3130 case ARM::tLDMIA_UPD: 3131 case ARM::tSTMIA_UPD: 3132 case ARM::t2LDMIA_UPD: 3133 case ARM::t2LDMDB_UPD: 3134 case ARM::t2STMIA_UPD: 3135 case ARM::t2STMDB_UPD: 3136 ++UOps; // One for base register writeback. 3137 break; 3138 case ARM::LDMIA_RET: 3139 case ARM::tPOP_RET: 3140 case ARM::t2LDMIA_RET: 3141 UOps += 2; // One for base reg wb, one for write to pc. 3142 break; 3143 } 3144 return UOps; 3145 } 3146 3147 unsigned ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData, 3148 const MachineInstr &MI) const { 3149 if (!ItinData || ItinData->isEmpty()) 3150 return 1; 3151 3152 const MCInstrDesc &Desc = MI.getDesc(); 3153 unsigned Class = Desc.getSchedClass(); 3154 int ItinUOps = ItinData->getNumMicroOps(Class); 3155 if (ItinUOps >= 0) { 3156 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore())) 3157 return getNumMicroOpsSwiftLdSt(ItinData, MI); 3158 3159 return ItinUOps; 3160 } 3161 3162 unsigned Opc = MI.getOpcode(); 3163 switch (Opc) { 3164 default: 3165 llvm_unreachable("Unexpected multi-uops instruction!"); 3166 case ARM::VLDMQIA: 3167 case ARM::VSTMQIA: 3168 return 2; 3169 3170 // The number of uOps for load / store multiple are determined by the number 3171 // registers. 3172 // 3173 // On Cortex-A8, each pair of register loads / stores can be scheduled on the 3174 // same cycle. The scheduling for the first load / store must be done 3175 // separately by assuming the address is not 64-bit aligned. 3176 // 3177 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address 3178 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON 3179 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1. 3180 case ARM::VLDMDIA: 3181 case ARM::VLDMDIA_UPD: 3182 case ARM::VLDMDDB_UPD: 3183 case ARM::VLDMSIA: 3184 case ARM::VLDMSIA_UPD: 3185 case ARM::VLDMSDB_UPD: 3186 case ARM::VSTMDIA: 3187 case ARM::VSTMDIA_UPD: 3188 case ARM::VSTMDDB_UPD: 3189 case ARM::VSTMSIA: 3190 case ARM::VSTMSIA_UPD: 3191 case ARM::VSTMSDB_UPD: { 3192 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands(); 3193 return (NumRegs / 2) + (NumRegs % 2) + 1; 3194 } 3195 3196 case ARM::LDMIA_RET: 3197 case ARM::LDMIA: 3198 case ARM::LDMDA: 3199 case ARM::LDMDB: 3200 case ARM::LDMIB: 3201 case ARM::LDMIA_UPD: 3202 case ARM::LDMDA_UPD: 3203 case ARM::LDMDB_UPD: 3204 case ARM::LDMIB_UPD: 3205 case ARM::STMIA: 3206 case ARM::STMDA: 3207 case ARM::STMDB: 3208 case ARM::STMIB: 3209 case ARM::STMIA_UPD: 3210 case ARM::STMDA_UPD: 3211 case ARM::STMDB_UPD: 3212 case ARM::STMIB_UPD: 3213 case ARM::tLDMIA: 3214 case ARM::tLDMIA_UPD: 3215 case ARM::tSTMIA_UPD: 3216 case ARM::tPOP_RET: 3217 case ARM::tPOP: 3218 case ARM::tPUSH: 3219 case ARM::t2LDMIA_RET: 3220 case ARM::t2LDMIA: 3221 case ARM::t2LDMDB: 3222 case ARM::t2LDMIA_UPD: 3223 case ARM::t2LDMDB_UPD: 3224 case ARM::t2STMIA: 3225 case ARM::t2STMDB: 3226 case ARM::t2STMIA_UPD: 3227 case ARM::t2STMDB_UPD: { 3228 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands() + 1; 3229 switch (Subtarget.getLdStMultipleTiming()) { 3230 case ARMSubtarget::SingleIssuePlusExtras: 3231 return getNumMicroOpsSingleIssuePlusExtras(Opc, NumRegs); 3232 case ARMSubtarget::SingleIssue: 3233 // Assume the worst. 3234 return NumRegs; 3235 case ARMSubtarget::DoubleIssue: { 3236 if (NumRegs < 4) 3237 return 2; 3238 // 4 registers would be issued: 2, 2. 3239 // 5 registers would be issued: 2, 2, 1. 3240 unsigned UOps = (NumRegs / 2); 3241 if (NumRegs % 2) 3242 ++UOps; 3243 return UOps; 3244 } 3245 case ARMSubtarget::DoubleIssueCheckUnalignedAccess: { 3246 unsigned UOps = (NumRegs / 2); 3247 // If there are odd number of registers or if it's not 64-bit aligned, 3248 // then it takes an extra AGU (Address Generation Unit) cycle. 3249 if ((NumRegs % 2) || !MI.hasOneMemOperand() || 3250 (*MI.memoperands_begin())->getAlignment() < 8) 3251 ++UOps; 3252 return UOps; 3253 } 3254 } 3255 } 3256 } 3257 llvm_unreachable("Didn't find the number of microops"); 3258 } 3259 3260 int 3261 ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData, 3262 const MCInstrDesc &DefMCID, 3263 unsigned DefClass, 3264 unsigned DefIdx, unsigned DefAlign) const { 3265 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1; 3266 if (RegNo <= 0) 3267 // Def is the address writeback. 3268 return ItinData->getOperandCycle(DefClass, DefIdx); 3269 3270 int DefCycle; 3271 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) { 3272 // (regno / 2) + (regno % 2) + 1 3273 DefCycle = RegNo / 2 + 1; 3274 if (RegNo % 2) 3275 ++DefCycle; 3276 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) { 3277 DefCycle = RegNo; 3278 bool isSLoad = false; 3279 3280 switch (DefMCID.getOpcode()) { 3281 default: break; 3282 case ARM::VLDMSIA: 3283 case ARM::VLDMSIA_UPD: 3284 case ARM::VLDMSDB_UPD: 3285 isSLoad = true; 3286 break; 3287 } 3288 3289 // If there are odd number of 'S' registers or if it's not 64-bit aligned, 3290 // then it takes an extra cycle. 3291 if ((isSLoad && (RegNo % 2)) || DefAlign < 8) 3292 ++DefCycle; 3293 } else { 3294 // Assume the worst. 3295 DefCycle = RegNo + 2; 3296 } 3297 3298 return DefCycle; 3299 } 3300 3301 int 3302 ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData, 3303 const MCInstrDesc &DefMCID, 3304 unsigned DefClass, 3305 unsigned DefIdx, unsigned DefAlign) const { 3306 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1; 3307 if (RegNo <= 0) 3308 // Def is the address writeback. 3309 return ItinData->getOperandCycle(DefClass, DefIdx); 3310 3311 int DefCycle; 3312 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) { 3313 // 4 registers would be issued: 1, 2, 1. 3314 // 5 registers would be issued: 1, 2, 2. 3315 DefCycle = RegNo / 2; 3316 if (DefCycle < 1) 3317 DefCycle = 1; 3318 // Result latency is issue cycle + 2: E2. 3319 DefCycle += 2; 3320 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) { 3321 DefCycle = (RegNo / 2); 3322 // If there are odd number of registers or if it's not 64-bit aligned, 3323 // then it takes an extra AGU (Address Generation Unit) cycle. 3324 if ((RegNo % 2) || DefAlign < 8) 3325 ++DefCycle; 3326 // Result latency is AGU cycles + 2. 3327 DefCycle += 2; 3328 } else { 3329 // Assume the worst. 3330 DefCycle = RegNo + 2; 3331 } 3332 3333 return DefCycle; 3334 } 3335 3336 int 3337 ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData, 3338 const MCInstrDesc &UseMCID, 3339 unsigned UseClass, 3340 unsigned UseIdx, unsigned UseAlign) const { 3341 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1; 3342 if (RegNo <= 0) 3343 return ItinData->getOperandCycle(UseClass, UseIdx); 3344 3345 int UseCycle; 3346 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) { 3347 // (regno / 2) + (regno % 2) + 1 3348 UseCycle = RegNo / 2 + 1; 3349 if (RegNo % 2) 3350 ++UseCycle; 3351 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) { 3352 UseCycle = RegNo; 3353 bool isSStore = false; 3354 3355 switch (UseMCID.getOpcode()) { 3356 default: break; 3357 case ARM::VSTMSIA: 3358 case ARM::VSTMSIA_UPD: 3359 case ARM::VSTMSDB_UPD: 3360 isSStore = true; 3361 break; 3362 } 3363 3364 // If there are odd number of 'S' registers or if it's not 64-bit aligned, 3365 // then it takes an extra cycle. 3366 if ((isSStore && (RegNo % 2)) || UseAlign < 8) 3367 ++UseCycle; 3368 } else { 3369 // Assume the worst. 3370 UseCycle = RegNo + 2; 3371 } 3372 3373 return UseCycle; 3374 } 3375 3376 int 3377 ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData, 3378 const MCInstrDesc &UseMCID, 3379 unsigned UseClass, 3380 unsigned UseIdx, unsigned UseAlign) const { 3381 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1; 3382 if (RegNo <= 0) 3383 return ItinData->getOperandCycle(UseClass, UseIdx); 3384 3385 int UseCycle; 3386 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) { 3387 UseCycle = RegNo / 2; 3388 if (UseCycle < 2) 3389 UseCycle = 2; 3390 // Read in E3. 3391 UseCycle += 2; 3392 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) { 3393 UseCycle = (RegNo / 2); 3394 // If there are odd number of registers or if it's not 64-bit aligned, 3395 // then it takes an extra AGU (Address Generation Unit) cycle. 3396 if ((RegNo % 2) || UseAlign < 8) 3397 ++UseCycle; 3398 } else { 3399 // Assume the worst. 3400 UseCycle = 1; 3401 } 3402 return UseCycle; 3403 } 3404 3405 int 3406 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, 3407 const MCInstrDesc &DefMCID, 3408 unsigned DefIdx, unsigned DefAlign, 3409 const MCInstrDesc &UseMCID, 3410 unsigned UseIdx, unsigned UseAlign) const { 3411 unsigned DefClass = DefMCID.getSchedClass(); 3412 unsigned UseClass = UseMCID.getSchedClass(); 3413 3414 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands()) 3415 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx); 3416 3417 // This may be a def / use of a variable_ops instruction, the operand 3418 // latency might be determinable dynamically. Let the target try to 3419 // figure it out. 3420 int DefCycle = -1; 3421 bool LdmBypass = false; 3422 switch (DefMCID.getOpcode()) { 3423 default: 3424 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx); 3425 break; 3426 3427 case ARM::VLDMDIA: 3428 case ARM::VLDMDIA_UPD: 3429 case ARM::VLDMDDB_UPD: 3430 case ARM::VLDMSIA: 3431 case ARM::VLDMSIA_UPD: 3432 case ARM::VLDMSDB_UPD: 3433 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign); 3434 break; 3435 3436 case ARM::LDMIA_RET: 3437 case ARM::LDMIA: 3438 case ARM::LDMDA: 3439 case ARM::LDMDB: 3440 case ARM::LDMIB: 3441 case ARM::LDMIA_UPD: 3442 case ARM::LDMDA_UPD: 3443 case ARM::LDMDB_UPD: 3444 case ARM::LDMIB_UPD: 3445 case ARM::tLDMIA: 3446 case ARM::tLDMIA_UPD: 3447 case ARM::tPUSH: 3448 case ARM::t2LDMIA_RET: 3449 case ARM::t2LDMIA: 3450 case ARM::t2LDMDB: 3451 case ARM::t2LDMIA_UPD: 3452 case ARM::t2LDMDB_UPD: 3453 LdmBypass = 1; 3454 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign); 3455 break; 3456 } 3457 3458 if (DefCycle == -1) 3459 // We can't seem to determine the result latency of the def, assume it's 2. 3460 DefCycle = 2; 3461 3462 int UseCycle = -1; 3463 switch (UseMCID.getOpcode()) { 3464 default: 3465 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx); 3466 break; 3467 3468 case ARM::VSTMDIA: 3469 case ARM::VSTMDIA_UPD: 3470 case ARM::VSTMDDB_UPD: 3471 case ARM::VSTMSIA: 3472 case ARM::VSTMSIA_UPD: 3473 case ARM::VSTMSDB_UPD: 3474 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign); 3475 break; 3476 3477 case ARM::STMIA: 3478 case ARM::STMDA: 3479 case ARM::STMDB: 3480 case ARM::STMIB: 3481 case ARM::STMIA_UPD: 3482 case ARM::STMDA_UPD: 3483 case ARM::STMDB_UPD: 3484 case ARM::STMIB_UPD: 3485 case ARM::tSTMIA_UPD: 3486 case ARM::tPOP_RET: 3487 case ARM::tPOP: 3488 case ARM::t2STMIA: 3489 case ARM::t2STMDB: 3490 case ARM::t2STMIA_UPD: 3491 case ARM::t2STMDB_UPD: 3492 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign); 3493 break; 3494 } 3495 3496 if (UseCycle == -1) 3497 // Assume it's read in the first stage. 3498 UseCycle = 1; 3499 3500 UseCycle = DefCycle - UseCycle + 1; 3501 if (UseCycle > 0) { 3502 if (LdmBypass) { 3503 // It's a variable_ops instruction so we can't use DefIdx here. Just use 3504 // first def operand. 3505 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1, 3506 UseClass, UseIdx)) 3507 --UseCycle; 3508 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx, 3509 UseClass, UseIdx)) { 3510 --UseCycle; 3511 } 3512 } 3513 3514 return UseCycle; 3515 } 3516 3517 static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI, 3518 const MachineInstr *MI, unsigned Reg, 3519 unsigned &DefIdx, unsigned &Dist) { 3520 Dist = 0; 3521 3522 MachineBasicBlock::const_iterator I = MI; ++I; 3523 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator()); 3524 assert(II->isInsideBundle() && "Empty bundle?"); 3525 3526 int Idx = -1; 3527 while (II->isInsideBundle()) { 3528 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI); 3529 if (Idx != -1) 3530 break; 3531 --II; 3532 ++Dist; 3533 } 3534 3535 assert(Idx != -1 && "Cannot find bundled definition!"); 3536 DefIdx = Idx; 3537 return &*II; 3538 } 3539 3540 static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI, 3541 const MachineInstr &MI, unsigned Reg, 3542 unsigned &UseIdx, unsigned &Dist) { 3543 Dist = 0; 3544 3545 MachineBasicBlock::const_instr_iterator II = ++MI.getIterator(); 3546 assert(II->isInsideBundle() && "Empty bundle?"); 3547 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end(); 3548 3549 // FIXME: This doesn't properly handle multiple uses. 3550 int Idx = -1; 3551 while (II != E && II->isInsideBundle()) { 3552 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI); 3553 if (Idx != -1) 3554 break; 3555 if (II->getOpcode() != ARM::t2IT) 3556 ++Dist; 3557 ++II; 3558 } 3559 3560 if (Idx == -1) { 3561 Dist = 0; 3562 return nullptr; 3563 } 3564 3565 UseIdx = Idx; 3566 return &*II; 3567 } 3568 3569 /// Return the number of cycles to add to (or subtract from) the static 3570 /// itinerary based on the def opcode and alignment. The caller will ensure that 3571 /// adjusted latency is at least one cycle. 3572 static int adjustDefLatency(const ARMSubtarget &Subtarget, 3573 const MachineInstr &DefMI, 3574 const MCInstrDesc &DefMCID, unsigned DefAlign) { 3575 int Adjust = 0; 3576 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) { 3577 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2] 3578 // variants are one cycle cheaper. 3579 switch (DefMCID.getOpcode()) { 3580 default: break; 3581 case ARM::LDRrs: 3582 case ARM::LDRBrs: { 3583 unsigned ShOpVal = DefMI.getOperand(3).getImm(); 3584 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 3585 if (ShImm == 0 || 3586 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)) 3587 --Adjust; 3588 break; 3589 } 3590 case ARM::t2LDRs: 3591 case ARM::t2LDRBs: 3592 case ARM::t2LDRHs: 3593 case ARM::t2LDRSHs: { 3594 // Thumb2 mode: lsl only. 3595 unsigned ShAmt = DefMI.getOperand(3).getImm(); 3596 if (ShAmt == 0 || ShAmt == 2) 3597 --Adjust; 3598 break; 3599 } 3600 } 3601 } else if (Subtarget.isSwift()) { 3602 // FIXME: Properly handle all of the latency adjustments for address 3603 // writeback. 3604 switch (DefMCID.getOpcode()) { 3605 default: break; 3606 case ARM::LDRrs: 3607 case ARM::LDRBrs: { 3608 unsigned ShOpVal = DefMI.getOperand(3).getImm(); 3609 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub; 3610 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 3611 if (!isSub && 3612 (ShImm == 0 || 3613 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 3614 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))) 3615 Adjust -= 2; 3616 else if (!isSub && 3617 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr) 3618 --Adjust; 3619 break; 3620 } 3621 case ARM::t2LDRs: 3622 case ARM::t2LDRBs: 3623 case ARM::t2LDRHs: 3624 case ARM::t2LDRSHs: { 3625 // Thumb2 mode: lsl only. 3626 unsigned ShAmt = DefMI.getOperand(3).getImm(); 3627 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3) 3628 Adjust -= 2; 3629 break; 3630 } 3631 } 3632 } 3633 3634 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment()) { 3635 switch (DefMCID.getOpcode()) { 3636 default: break; 3637 case ARM::VLD1q8: 3638 case ARM::VLD1q16: 3639 case ARM::VLD1q32: 3640 case ARM::VLD1q64: 3641 case ARM::VLD1q8wb_fixed: 3642 case ARM::VLD1q16wb_fixed: 3643 case ARM::VLD1q32wb_fixed: 3644 case ARM::VLD1q64wb_fixed: 3645 case ARM::VLD1q8wb_register: 3646 case ARM::VLD1q16wb_register: 3647 case ARM::VLD1q32wb_register: 3648 case ARM::VLD1q64wb_register: 3649 case ARM::VLD2d8: 3650 case ARM::VLD2d16: 3651 case ARM::VLD2d32: 3652 case ARM::VLD2q8: 3653 case ARM::VLD2q16: 3654 case ARM::VLD2q32: 3655 case ARM::VLD2d8wb_fixed: 3656 case ARM::VLD2d16wb_fixed: 3657 case ARM::VLD2d32wb_fixed: 3658 case ARM::VLD2q8wb_fixed: 3659 case ARM::VLD2q16wb_fixed: 3660 case ARM::VLD2q32wb_fixed: 3661 case ARM::VLD2d8wb_register: 3662 case ARM::VLD2d16wb_register: 3663 case ARM::VLD2d32wb_register: 3664 case ARM::VLD2q8wb_register: 3665 case ARM::VLD2q16wb_register: 3666 case ARM::VLD2q32wb_register: 3667 case ARM::VLD3d8: 3668 case ARM::VLD3d16: 3669 case ARM::VLD3d32: 3670 case ARM::VLD1d64T: 3671 case ARM::VLD3d8_UPD: 3672 case ARM::VLD3d16_UPD: 3673 case ARM::VLD3d32_UPD: 3674 case ARM::VLD1d64Twb_fixed: 3675 case ARM::VLD1d64Twb_register: 3676 case ARM::VLD3q8_UPD: 3677 case ARM::VLD3q16_UPD: 3678 case ARM::VLD3q32_UPD: 3679 case ARM::VLD4d8: 3680 case ARM::VLD4d16: 3681 case ARM::VLD4d32: 3682 case ARM::VLD1d64Q: 3683 case ARM::VLD4d8_UPD: 3684 case ARM::VLD4d16_UPD: 3685 case ARM::VLD4d32_UPD: 3686 case ARM::VLD1d64Qwb_fixed: 3687 case ARM::VLD1d64Qwb_register: 3688 case ARM::VLD4q8_UPD: 3689 case ARM::VLD4q16_UPD: 3690 case ARM::VLD4q32_UPD: 3691 case ARM::VLD1DUPq8: 3692 case ARM::VLD1DUPq16: 3693 case ARM::VLD1DUPq32: 3694 case ARM::VLD1DUPq8wb_fixed: 3695 case ARM::VLD1DUPq16wb_fixed: 3696 case ARM::VLD1DUPq32wb_fixed: 3697 case ARM::VLD1DUPq8wb_register: 3698 case ARM::VLD1DUPq16wb_register: 3699 case ARM::VLD1DUPq32wb_register: 3700 case ARM::VLD2DUPd8: 3701 case ARM::VLD2DUPd16: 3702 case ARM::VLD2DUPd32: 3703 case ARM::VLD2DUPd8wb_fixed: 3704 case ARM::VLD2DUPd16wb_fixed: 3705 case ARM::VLD2DUPd32wb_fixed: 3706 case ARM::VLD2DUPd8wb_register: 3707 case ARM::VLD2DUPd16wb_register: 3708 case ARM::VLD2DUPd32wb_register: 3709 case ARM::VLD4DUPd8: 3710 case ARM::VLD4DUPd16: 3711 case ARM::VLD4DUPd32: 3712 case ARM::VLD4DUPd8_UPD: 3713 case ARM::VLD4DUPd16_UPD: 3714 case ARM::VLD4DUPd32_UPD: 3715 case ARM::VLD1LNd8: 3716 case ARM::VLD1LNd16: 3717 case ARM::VLD1LNd32: 3718 case ARM::VLD1LNd8_UPD: 3719 case ARM::VLD1LNd16_UPD: 3720 case ARM::VLD1LNd32_UPD: 3721 case ARM::VLD2LNd8: 3722 case ARM::VLD2LNd16: 3723 case ARM::VLD2LNd32: 3724 case ARM::VLD2LNq16: 3725 case ARM::VLD2LNq32: 3726 case ARM::VLD2LNd8_UPD: 3727 case ARM::VLD2LNd16_UPD: 3728 case ARM::VLD2LNd32_UPD: 3729 case ARM::VLD2LNq16_UPD: 3730 case ARM::VLD2LNq32_UPD: 3731 case ARM::VLD4LNd8: 3732 case ARM::VLD4LNd16: 3733 case ARM::VLD4LNd32: 3734 case ARM::VLD4LNq16: 3735 case ARM::VLD4LNq32: 3736 case ARM::VLD4LNd8_UPD: 3737 case ARM::VLD4LNd16_UPD: 3738 case ARM::VLD4LNd32_UPD: 3739 case ARM::VLD4LNq16_UPD: 3740 case ARM::VLD4LNq32_UPD: 3741 // If the address is not 64-bit aligned, the latencies of these 3742 // instructions increases by one. 3743 ++Adjust; 3744 break; 3745 } 3746 } 3747 return Adjust; 3748 } 3749 3750 int ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, 3751 const MachineInstr &DefMI, 3752 unsigned DefIdx, 3753 const MachineInstr &UseMI, 3754 unsigned UseIdx) const { 3755 // No operand latency. The caller may fall back to getInstrLatency. 3756 if (!ItinData || ItinData->isEmpty()) 3757 return -1; 3758 3759 const MachineOperand &DefMO = DefMI.getOperand(DefIdx); 3760 unsigned Reg = DefMO.getReg(); 3761 3762 const MachineInstr *ResolvedDefMI = &DefMI; 3763 unsigned DefAdj = 0; 3764 if (DefMI.isBundle()) 3765 ResolvedDefMI = 3766 getBundledDefMI(&getRegisterInfo(), &DefMI, Reg, DefIdx, DefAdj); 3767 if (ResolvedDefMI->isCopyLike() || ResolvedDefMI->isInsertSubreg() || 3768 ResolvedDefMI->isRegSequence() || ResolvedDefMI->isImplicitDef()) { 3769 return 1; 3770 } 3771 3772 const MachineInstr *ResolvedUseMI = &UseMI; 3773 unsigned UseAdj = 0; 3774 if (UseMI.isBundle()) { 3775 ResolvedUseMI = 3776 getBundledUseMI(&getRegisterInfo(), UseMI, Reg, UseIdx, UseAdj); 3777 if (!ResolvedUseMI) 3778 return -1; 3779 } 3780 3781 return getOperandLatencyImpl( 3782 ItinData, *ResolvedDefMI, DefIdx, ResolvedDefMI->getDesc(), DefAdj, DefMO, 3783 Reg, *ResolvedUseMI, UseIdx, ResolvedUseMI->getDesc(), UseAdj); 3784 } 3785 3786 int ARMBaseInstrInfo::getOperandLatencyImpl( 3787 const InstrItineraryData *ItinData, const MachineInstr &DefMI, 3788 unsigned DefIdx, const MCInstrDesc &DefMCID, unsigned DefAdj, 3789 const MachineOperand &DefMO, unsigned Reg, const MachineInstr &UseMI, 3790 unsigned UseIdx, const MCInstrDesc &UseMCID, unsigned UseAdj) const { 3791 if (Reg == ARM::CPSR) { 3792 if (DefMI.getOpcode() == ARM::FMSTAT) { 3793 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?) 3794 return Subtarget.isLikeA9() ? 1 : 20; 3795 } 3796 3797 // CPSR set and branch can be paired in the same cycle. 3798 if (UseMI.isBranch()) 3799 return 0; 3800 3801 // Otherwise it takes the instruction latency (generally one). 3802 unsigned Latency = getInstrLatency(ItinData, DefMI); 3803 3804 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to 3805 // its uses. Instructions which are otherwise scheduled between them may 3806 // incur a code size penalty (not able to use the CPSR setting 16-bit 3807 // instructions). 3808 if (Latency > 0 && Subtarget.isThumb2()) { 3809 const MachineFunction *MF = DefMI.getParent()->getParent(); 3810 // FIXME: Use Function::optForSize(). 3811 if (MF->getFunction()->hasFnAttribute(Attribute::OptimizeForSize)) 3812 --Latency; 3813 } 3814 return Latency; 3815 } 3816 3817 if (DefMO.isImplicit() || UseMI.getOperand(UseIdx).isImplicit()) 3818 return -1; 3819 3820 unsigned DefAlign = DefMI.hasOneMemOperand() 3821 ? (*DefMI.memoperands_begin())->getAlignment() 3822 : 0; 3823 unsigned UseAlign = UseMI.hasOneMemOperand() 3824 ? (*UseMI.memoperands_begin())->getAlignment() 3825 : 0; 3826 3827 // Get the itinerary's latency if possible, and handle variable_ops. 3828 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign, UseMCID, 3829 UseIdx, UseAlign); 3830 // Unable to find operand latency. The caller may resort to getInstrLatency. 3831 if (Latency < 0) 3832 return Latency; 3833 3834 // Adjust for IT block position. 3835 int Adj = DefAdj + UseAdj; 3836 3837 // Adjust for dynamic def-side opcode variants not captured by the itinerary. 3838 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign); 3839 if (Adj >= 0 || (int)Latency > -Adj) { 3840 return Latency + Adj; 3841 } 3842 // Return the itinerary latency, which may be zero but not less than zero. 3843 return Latency; 3844 } 3845 3846 int 3847 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, 3848 SDNode *DefNode, unsigned DefIdx, 3849 SDNode *UseNode, unsigned UseIdx) const { 3850 if (!DefNode->isMachineOpcode()) 3851 return 1; 3852 3853 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode()); 3854 3855 if (isZeroCost(DefMCID.Opcode)) 3856 return 0; 3857 3858 if (!ItinData || ItinData->isEmpty()) 3859 return DefMCID.mayLoad() ? 3 : 1; 3860 3861 if (!UseNode->isMachineOpcode()) { 3862 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx); 3863 int Adj = Subtarget.getPreISelOperandLatencyAdjustment(); 3864 int Threshold = 1 + Adj; 3865 return Latency <= Threshold ? 1 : Latency - Adj; 3866 } 3867 3868 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode()); 3869 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode); 3870 unsigned DefAlign = !DefMN->memoperands_empty() 3871 ? (*DefMN->memoperands_begin())->getAlignment() : 0; 3872 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode); 3873 unsigned UseAlign = !UseMN->memoperands_empty() 3874 ? (*UseMN->memoperands_begin())->getAlignment() : 0; 3875 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign, 3876 UseMCID, UseIdx, UseAlign); 3877 3878 if (Latency > 1 && 3879 (Subtarget.isCortexA8() || Subtarget.isLikeA9() || 3880 Subtarget.isCortexA7())) { 3881 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2] 3882 // variants are one cycle cheaper. 3883 switch (DefMCID.getOpcode()) { 3884 default: break; 3885 case ARM::LDRrs: 3886 case ARM::LDRBrs: { 3887 unsigned ShOpVal = 3888 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue(); 3889 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 3890 if (ShImm == 0 || 3891 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)) 3892 --Latency; 3893 break; 3894 } 3895 case ARM::t2LDRs: 3896 case ARM::t2LDRBs: 3897 case ARM::t2LDRHs: 3898 case ARM::t2LDRSHs: { 3899 // Thumb2 mode: lsl only. 3900 unsigned ShAmt = 3901 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue(); 3902 if (ShAmt == 0 || ShAmt == 2) 3903 --Latency; 3904 break; 3905 } 3906 } 3907 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) { 3908 // FIXME: Properly handle all of the latency adjustments for address 3909 // writeback. 3910 switch (DefMCID.getOpcode()) { 3911 default: break; 3912 case ARM::LDRrs: 3913 case ARM::LDRBrs: { 3914 unsigned ShOpVal = 3915 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue(); 3916 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 3917 if (ShImm == 0 || 3918 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 3919 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)) 3920 Latency -= 2; 3921 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr) 3922 --Latency; 3923 break; 3924 } 3925 case ARM::t2LDRs: 3926 case ARM::t2LDRBs: 3927 case ARM::t2LDRHs: 3928 case ARM::t2LDRSHs: { 3929 // Thumb2 mode: lsl 0-3 only. 3930 Latency -= 2; 3931 break; 3932 } 3933 } 3934 } 3935 3936 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment()) 3937 switch (DefMCID.getOpcode()) { 3938 default: break; 3939 case ARM::VLD1q8: 3940 case ARM::VLD1q16: 3941 case ARM::VLD1q32: 3942 case ARM::VLD1q64: 3943 case ARM::VLD1q8wb_register: 3944 case ARM::VLD1q16wb_register: 3945 case ARM::VLD1q32wb_register: 3946 case ARM::VLD1q64wb_register: 3947 case ARM::VLD1q8wb_fixed: 3948 case ARM::VLD1q16wb_fixed: 3949 case ARM::VLD1q32wb_fixed: 3950 case ARM::VLD1q64wb_fixed: 3951 case ARM::VLD2d8: 3952 case ARM::VLD2d16: 3953 case ARM::VLD2d32: 3954 case ARM::VLD2q8Pseudo: 3955 case ARM::VLD2q16Pseudo: 3956 case ARM::VLD2q32Pseudo: 3957 case ARM::VLD2d8wb_fixed: 3958 case ARM::VLD2d16wb_fixed: 3959 case ARM::VLD2d32wb_fixed: 3960 case ARM::VLD2q8PseudoWB_fixed: 3961 case ARM::VLD2q16PseudoWB_fixed: 3962 case ARM::VLD2q32PseudoWB_fixed: 3963 case ARM::VLD2d8wb_register: 3964 case ARM::VLD2d16wb_register: 3965 case ARM::VLD2d32wb_register: 3966 case ARM::VLD2q8PseudoWB_register: 3967 case ARM::VLD2q16PseudoWB_register: 3968 case ARM::VLD2q32PseudoWB_register: 3969 case ARM::VLD3d8Pseudo: 3970 case ARM::VLD3d16Pseudo: 3971 case ARM::VLD3d32Pseudo: 3972 case ARM::VLD1d64TPseudo: 3973 case ARM::VLD1d64TPseudoWB_fixed: 3974 case ARM::VLD3d8Pseudo_UPD: 3975 case ARM::VLD3d16Pseudo_UPD: 3976 case ARM::VLD3d32Pseudo_UPD: 3977 case ARM::VLD3q8Pseudo_UPD: 3978 case ARM::VLD3q16Pseudo_UPD: 3979 case ARM::VLD3q32Pseudo_UPD: 3980 case ARM::VLD3q8oddPseudo: 3981 case ARM::VLD3q16oddPseudo: 3982 case ARM::VLD3q32oddPseudo: 3983 case ARM::VLD3q8oddPseudo_UPD: 3984 case ARM::VLD3q16oddPseudo_UPD: 3985 case ARM::VLD3q32oddPseudo_UPD: 3986 case ARM::VLD4d8Pseudo: 3987 case ARM::VLD4d16Pseudo: 3988 case ARM::VLD4d32Pseudo: 3989 case ARM::VLD1d64QPseudo: 3990 case ARM::VLD1d64QPseudoWB_fixed: 3991 case ARM::VLD4d8Pseudo_UPD: 3992 case ARM::VLD4d16Pseudo_UPD: 3993 case ARM::VLD4d32Pseudo_UPD: 3994 case ARM::VLD4q8Pseudo_UPD: 3995 case ARM::VLD4q16Pseudo_UPD: 3996 case ARM::VLD4q32Pseudo_UPD: 3997 case ARM::VLD4q8oddPseudo: 3998 case ARM::VLD4q16oddPseudo: 3999 case ARM::VLD4q32oddPseudo: 4000 case ARM::VLD4q8oddPseudo_UPD: 4001 case ARM::VLD4q16oddPseudo_UPD: 4002 case ARM::VLD4q32oddPseudo_UPD: 4003 case ARM::VLD1DUPq8: 4004 case ARM::VLD1DUPq16: 4005 case ARM::VLD1DUPq32: 4006 case ARM::VLD1DUPq8wb_fixed: 4007 case ARM::VLD1DUPq16wb_fixed: 4008 case ARM::VLD1DUPq32wb_fixed: 4009 case ARM::VLD1DUPq8wb_register: 4010 case ARM::VLD1DUPq16wb_register: 4011 case ARM::VLD1DUPq32wb_register: 4012 case ARM::VLD2DUPd8: 4013 case ARM::VLD2DUPd16: 4014 case ARM::VLD2DUPd32: 4015 case ARM::VLD2DUPd8wb_fixed: 4016 case ARM::VLD2DUPd16wb_fixed: 4017 case ARM::VLD2DUPd32wb_fixed: 4018 case ARM::VLD2DUPd8wb_register: 4019 case ARM::VLD2DUPd16wb_register: 4020 case ARM::VLD2DUPd32wb_register: 4021 case ARM::VLD4DUPd8Pseudo: 4022 case ARM::VLD4DUPd16Pseudo: 4023 case ARM::VLD4DUPd32Pseudo: 4024 case ARM::VLD4DUPd8Pseudo_UPD: 4025 case ARM::VLD4DUPd16Pseudo_UPD: 4026 case ARM::VLD4DUPd32Pseudo_UPD: 4027 case ARM::VLD1LNq8Pseudo: 4028 case ARM::VLD1LNq16Pseudo: 4029 case ARM::VLD1LNq32Pseudo: 4030 case ARM::VLD1LNq8Pseudo_UPD: 4031 case ARM::VLD1LNq16Pseudo_UPD: 4032 case ARM::VLD1LNq32Pseudo_UPD: 4033 case ARM::VLD2LNd8Pseudo: 4034 case ARM::VLD2LNd16Pseudo: 4035 case ARM::VLD2LNd32Pseudo: 4036 case ARM::VLD2LNq16Pseudo: 4037 case ARM::VLD2LNq32Pseudo: 4038 case ARM::VLD2LNd8Pseudo_UPD: 4039 case ARM::VLD2LNd16Pseudo_UPD: 4040 case ARM::VLD2LNd32Pseudo_UPD: 4041 case ARM::VLD2LNq16Pseudo_UPD: 4042 case ARM::VLD2LNq32Pseudo_UPD: 4043 case ARM::VLD4LNd8Pseudo: 4044 case ARM::VLD4LNd16Pseudo: 4045 case ARM::VLD4LNd32Pseudo: 4046 case ARM::VLD4LNq16Pseudo: 4047 case ARM::VLD4LNq32Pseudo: 4048 case ARM::VLD4LNd8Pseudo_UPD: 4049 case ARM::VLD4LNd16Pseudo_UPD: 4050 case ARM::VLD4LNd32Pseudo_UPD: 4051 case ARM::VLD4LNq16Pseudo_UPD: 4052 case ARM::VLD4LNq32Pseudo_UPD: 4053 // If the address is not 64-bit aligned, the latencies of these 4054 // instructions increases by one. 4055 ++Latency; 4056 break; 4057 } 4058 4059 return Latency; 4060 } 4061 4062 unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr &MI) const { 4063 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() || 4064 MI.isImplicitDef()) 4065 return 0; 4066 4067 if (MI.isBundle()) 4068 return 0; 4069 4070 const MCInstrDesc &MCID = MI.getDesc(); 4071 4072 if (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR)) { 4073 // When predicated, CPSR is an additional source operand for CPSR updating 4074 // instructions, this apparently increases their latencies. 4075 return 1; 4076 } 4077 return 0; 4078 } 4079 4080 unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, 4081 const MachineInstr &MI, 4082 unsigned *PredCost) const { 4083 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() || 4084 MI.isImplicitDef()) 4085 return 1; 4086 4087 // An instruction scheduler typically runs on unbundled instructions, however 4088 // other passes may query the latency of a bundled instruction. 4089 if (MI.isBundle()) { 4090 unsigned Latency = 0; 4091 MachineBasicBlock::const_instr_iterator I = MI.getIterator(); 4092 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end(); 4093 while (++I != E && I->isInsideBundle()) { 4094 if (I->getOpcode() != ARM::t2IT) 4095 Latency += getInstrLatency(ItinData, *I, PredCost); 4096 } 4097 return Latency; 4098 } 4099 4100 const MCInstrDesc &MCID = MI.getDesc(); 4101 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) { 4102 // When predicated, CPSR is an additional source operand for CPSR updating 4103 // instructions, this apparently increases their latencies. 4104 *PredCost = 1; 4105 } 4106 // Be sure to call getStageLatency for an empty itinerary in case it has a 4107 // valid MinLatency property. 4108 if (!ItinData) 4109 return MI.mayLoad() ? 3 : 1; 4110 4111 unsigned Class = MCID.getSchedClass(); 4112 4113 // For instructions with variable uops, use uops as latency. 4114 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0) 4115 return getNumMicroOps(ItinData, MI); 4116 4117 // For the common case, fall back on the itinerary's latency. 4118 unsigned Latency = ItinData->getStageLatency(Class); 4119 4120 // Adjust for dynamic def-side opcode variants not captured by the itinerary. 4121 unsigned DefAlign = 4122 MI.hasOneMemOperand() ? (*MI.memoperands_begin())->getAlignment() : 0; 4123 int Adj = adjustDefLatency(Subtarget, MI, MCID, DefAlign); 4124 if (Adj >= 0 || (int)Latency > -Adj) { 4125 return Latency + Adj; 4126 } 4127 return Latency; 4128 } 4129 4130 int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, 4131 SDNode *Node) const { 4132 if (!Node->isMachineOpcode()) 4133 return 1; 4134 4135 if (!ItinData || ItinData->isEmpty()) 4136 return 1; 4137 4138 unsigned Opcode = Node->getMachineOpcode(); 4139 switch (Opcode) { 4140 default: 4141 return ItinData->getStageLatency(get(Opcode).getSchedClass()); 4142 case ARM::VLDMQIA: 4143 case ARM::VSTMQIA: 4144 return 2; 4145 } 4146 } 4147 4148 bool ARMBaseInstrInfo::hasHighOperandLatency(const TargetSchedModel &SchedModel, 4149 const MachineRegisterInfo *MRI, 4150 const MachineInstr &DefMI, 4151 unsigned DefIdx, 4152 const MachineInstr &UseMI, 4153 unsigned UseIdx) const { 4154 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask; 4155 unsigned UDomain = UseMI.getDesc().TSFlags & ARMII::DomainMask; 4156 if (Subtarget.nonpipelinedVFP() && 4157 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP)) 4158 return true; 4159 4160 // Hoist VFP / NEON instructions with 4 or higher latency. 4161 unsigned Latency = 4162 SchedModel.computeOperandLatency(&DefMI, DefIdx, &UseMI, UseIdx); 4163 if (Latency <= 3) 4164 return false; 4165 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON || 4166 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON; 4167 } 4168 4169 bool ARMBaseInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel, 4170 const MachineInstr &DefMI, 4171 unsigned DefIdx) const { 4172 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries(); 4173 if (!ItinData || ItinData->isEmpty()) 4174 return false; 4175 4176 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask; 4177 if (DDomain == ARMII::DomainGeneral) { 4178 unsigned DefClass = DefMI.getDesc().getSchedClass(); 4179 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx); 4180 return (DefCycle != -1 && DefCycle <= 2); 4181 } 4182 return false; 4183 } 4184 4185 bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr &MI, 4186 StringRef &ErrInfo) const { 4187 if (convertAddSubFlagsOpcode(MI.getOpcode())) { 4188 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG"; 4189 return false; 4190 } 4191 return true; 4192 } 4193 4194 // LoadStackGuard has so far only been implemented for MachO. Different code 4195 // sequence is needed for other targets. 4196 void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI, 4197 unsigned LoadImmOpc, 4198 unsigned LoadOpc) const { 4199 assert(!Subtarget.isROPI() && !Subtarget.isRWPI() && 4200 "ROPI/RWPI not currently supported with stack guard"); 4201 4202 MachineBasicBlock &MBB = *MI->getParent(); 4203 DebugLoc DL = MI->getDebugLoc(); 4204 unsigned Reg = MI->getOperand(0).getReg(); 4205 const GlobalValue *GV = 4206 cast<GlobalValue>((*MI->memoperands_begin())->getValue()); 4207 MachineInstrBuilder MIB; 4208 4209 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg) 4210 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY); 4211 4212 if (Subtarget.isGVIndirectSymbol(GV)) { 4213 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg); 4214 MIB.addReg(Reg, RegState::Kill).addImm(0); 4215 auto Flags = MachineMemOperand::MOLoad | 4216 MachineMemOperand::MODereferenceable | 4217 MachineMemOperand::MOInvariant; 4218 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand( 4219 MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 4, 4); 4220 MIB.addMemOperand(MMO).add(predOps(ARMCC::AL)); 4221 } 4222 4223 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg); 4224 MIB.addReg(Reg, RegState::Kill) 4225 .addImm(0) 4226 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end()) 4227 .add(predOps(ARMCC::AL)); 4228 } 4229 4230 bool 4231 ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc, 4232 unsigned &AddSubOpc, 4233 bool &NegAcc, bool &HasLane) const { 4234 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode); 4235 if (I == MLxEntryMap.end()) 4236 return false; 4237 4238 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second]; 4239 MulOpc = Entry.MulOpc; 4240 AddSubOpc = Entry.AddSubOpc; 4241 NegAcc = Entry.NegAcc; 4242 HasLane = Entry.HasLane; 4243 return true; 4244 } 4245 4246 //===----------------------------------------------------------------------===// 4247 // Execution domains. 4248 //===----------------------------------------------------------------------===// 4249 // 4250 // Some instructions go down the NEON pipeline, some go down the VFP pipeline, 4251 // and some can go down both. The vmov instructions go down the VFP pipeline, 4252 // but they can be changed to vorr equivalents that are executed by the NEON 4253 // pipeline. 4254 // 4255 // We use the following execution domain numbering: 4256 // 4257 enum ARMExeDomain { 4258 ExeGeneric = 0, 4259 ExeVFP = 1, 4260 ExeNEON = 2 4261 }; 4262 // 4263 // Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h 4264 // 4265 std::pair<uint16_t, uint16_t> 4266 ARMBaseInstrInfo::getExecutionDomain(const MachineInstr &MI) const { 4267 // If we don't have access to NEON instructions then we won't be able 4268 // to swizzle anything to the NEON domain. Check to make sure. 4269 if (Subtarget.hasNEON()) { 4270 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON 4271 // if they are not predicated. 4272 if (MI.getOpcode() == ARM::VMOVD && !isPredicated(MI)) 4273 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON)); 4274 4275 // CortexA9 is particularly picky about mixing the two and wants these 4276 // converted. 4277 if (Subtarget.useNEONForFPMovs() && !isPredicated(MI) && 4278 (MI.getOpcode() == ARM::VMOVRS || MI.getOpcode() == ARM::VMOVSR || 4279 MI.getOpcode() == ARM::VMOVS)) 4280 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON)); 4281 } 4282 // No other instructions can be swizzled, so just determine their domain. 4283 unsigned Domain = MI.getDesc().TSFlags & ARMII::DomainMask; 4284 4285 if (Domain & ARMII::DomainNEON) 4286 return std::make_pair(ExeNEON, 0); 4287 4288 // Certain instructions can go either way on Cortex-A8. 4289 // Treat them as NEON instructions. 4290 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8()) 4291 return std::make_pair(ExeNEON, 0); 4292 4293 if (Domain & ARMII::DomainVFP) 4294 return std::make_pair(ExeVFP, 0); 4295 4296 return std::make_pair(ExeGeneric, 0); 4297 } 4298 4299 static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI, 4300 unsigned SReg, unsigned &Lane) { 4301 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass); 4302 Lane = 0; 4303 4304 if (DReg != ARM::NoRegister) 4305 return DReg; 4306 4307 Lane = 1; 4308 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass); 4309 4310 assert(DReg && "S-register with no D super-register?"); 4311 return DReg; 4312 } 4313 4314 /// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane, 4315 /// set ImplicitSReg to a register number that must be marked as implicit-use or 4316 /// zero if no register needs to be defined as implicit-use. 4317 /// 4318 /// If the function cannot determine if an SPR should be marked implicit use or 4319 /// not, it returns false. 4320 /// 4321 /// This function handles cases where an instruction is being modified from taking 4322 /// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict 4323 /// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other 4324 /// lane of the DPR). 4325 /// 4326 /// If the other SPR is defined, an implicit-use of it should be added. Else, 4327 /// (including the case where the DPR itself is defined), it should not. 4328 /// 4329 static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI, 4330 MachineInstr &MI, unsigned DReg, 4331 unsigned Lane, unsigned &ImplicitSReg) { 4332 // If the DPR is defined or used already, the other SPR lane will be chained 4333 // correctly, so there is nothing to be done. 4334 if (MI.definesRegister(DReg, TRI) || MI.readsRegister(DReg, TRI)) { 4335 ImplicitSReg = 0; 4336 return true; 4337 } 4338 4339 // Otherwise we need to go searching to see if the SPR is set explicitly. 4340 ImplicitSReg = TRI->getSubReg(DReg, 4341 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1); 4342 MachineBasicBlock::LivenessQueryResult LQR = 4343 MI.getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI); 4344 4345 if (LQR == MachineBasicBlock::LQR_Live) 4346 return true; 4347 else if (LQR == MachineBasicBlock::LQR_Unknown) 4348 return false; 4349 4350 // If the register is known not to be live, there is no need to add an 4351 // implicit-use. 4352 ImplicitSReg = 0; 4353 return true; 4354 } 4355 4356 void ARMBaseInstrInfo::setExecutionDomain(MachineInstr &MI, 4357 unsigned Domain) const { 4358 unsigned DstReg, SrcReg, DReg; 4359 unsigned Lane; 4360 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI); 4361 const TargetRegisterInfo *TRI = &getRegisterInfo(); 4362 switch (MI.getOpcode()) { 4363 default: 4364 llvm_unreachable("cannot handle opcode!"); 4365 break; 4366 case ARM::VMOVD: 4367 if (Domain != ExeNEON) 4368 break; 4369 4370 // Zap the predicate operands. 4371 assert(!isPredicated(MI) && "Cannot predicate a VORRd"); 4372 4373 // Make sure we've got NEON instructions. 4374 assert(Subtarget.hasNEON() && "VORRd requires NEON"); 4375 4376 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits) 4377 DstReg = MI.getOperand(0).getReg(); 4378 SrcReg = MI.getOperand(1).getReg(); 4379 4380 for (unsigned i = MI.getDesc().getNumOperands(); i; --i) 4381 MI.RemoveOperand(i - 1); 4382 4383 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits) 4384 MI.setDesc(get(ARM::VORRd)); 4385 MIB.addReg(DstReg, RegState::Define) 4386 .addReg(SrcReg) 4387 .addReg(SrcReg) 4388 .add(predOps(ARMCC::AL)); 4389 break; 4390 case ARM::VMOVRS: 4391 if (Domain != ExeNEON) 4392 break; 4393 assert(!isPredicated(MI) && "Cannot predicate a VGETLN"); 4394 4395 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits) 4396 DstReg = MI.getOperand(0).getReg(); 4397 SrcReg = MI.getOperand(1).getReg(); 4398 4399 for (unsigned i = MI.getDesc().getNumOperands(); i; --i) 4400 MI.RemoveOperand(i - 1); 4401 4402 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane); 4403 4404 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps) 4405 // Note that DSrc has been widened and the other lane may be undef, which 4406 // contaminates the entire register. 4407 MI.setDesc(get(ARM::VGETLNi32)); 4408 MIB.addReg(DstReg, RegState::Define) 4409 .addReg(DReg, RegState::Undef) 4410 .addImm(Lane) 4411 .add(predOps(ARMCC::AL)); 4412 4413 // The old source should be an implicit use, otherwise we might think it 4414 // was dead before here. 4415 MIB.addReg(SrcReg, RegState::Implicit); 4416 break; 4417 case ARM::VMOVSR: { 4418 if (Domain != ExeNEON) 4419 break; 4420 assert(!isPredicated(MI) && "Cannot predicate a VSETLN"); 4421 4422 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits) 4423 DstReg = MI.getOperand(0).getReg(); 4424 SrcReg = MI.getOperand(1).getReg(); 4425 4426 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane); 4427 4428 unsigned ImplicitSReg; 4429 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg)) 4430 break; 4431 4432 for (unsigned i = MI.getDesc().getNumOperands(); i; --i) 4433 MI.RemoveOperand(i - 1); 4434 4435 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps) 4436 // Again DDst may be undefined at the beginning of this instruction. 4437 MI.setDesc(get(ARM::VSETLNi32)); 4438 MIB.addReg(DReg, RegState::Define) 4439 .addReg(DReg, getUndefRegState(!MI.readsRegister(DReg, TRI))) 4440 .addReg(SrcReg) 4441 .addImm(Lane) 4442 .add(predOps(ARMCC::AL)); 4443 4444 // The narrower destination must be marked as set to keep previous chains 4445 // in place. 4446 MIB.addReg(DstReg, RegState::Define | RegState::Implicit); 4447 if (ImplicitSReg != 0) 4448 MIB.addReg(ImplicitSReg, RegState::Implicit); 4449 break; 4450 } 4451 case ARM::VMOVS: { 4452 if (Domain != ExeNEON) 4453 break; 4454 4455 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits) 4456 DstReg = MI.getOperand(0).getReg(); 4457 SrcReg = MI.getOperand(1).getReg(); 4458 4459 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc; 4460 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane); 4461 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane); 4462 4463 unsigned ImplicitSReg; 4464 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg)) 4465 break; 4466 4467 for (unsigned i = MI.getDesc().getNumOperands(); i; --i) 4468 MI.RemoveOperand(i - 1); 4469 4470 if (DSrc == DDst) { 4471 // Destination can be: 4472 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits) 4473 MI.setDesc(get(ARM::VDUPLN32d)); 4474 MIB.addReg(DDst, RegState::Define) 4475 .addReg(DDst, getUndefRegState(!MI.readsRegister(DDst, TRI))) 4476 .addImm(SrcLane) 4477 .add(predOps(ARMCC::AL)); 4478 4479 // Neither the source or the destination are naturally represented any 4480 // more, so add them in manually. 4481 MIB.addReg(DstReg, RegState::Implicit | RegState::Define); 4482 MIB.addReg(SrcReg, RegState::Implicit); 4483 if (ImplicitSReg != 0) 4484 MIB.addReg(ImplicitSReg, RegState::Implicit); 4485 break; 4486 } 4487 4488 // In general there's no single instruction that can perform an S <-> S 4489 // move in NEON space, but a pair of VEXT instructions *can* do the 4490 // job. It turns out that the VEXTs needed will only use DSrc once, with 4491 // the position based purely on the combination of lane-0 and lane-1 4492 // involved. For example 4493 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1 4494 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1 4495 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1 4496 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1 4497 // 4498 // Pattern of the MachineInstrs is: 4499 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits) 4500 MachineInstrBuilder NewMIB; 4501 NewMIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::VEXTd32), 4502 DDst); 4503 4504 // On the first instruction, both DSrc and DDst may be <undef> if present. 4505 // Specifically when the original instruction didn't have them as an 4506 // <imp-use>. 4507 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst; 4508 bool CurUndef = !MI.readsRegister(CurReg, TRI); 4509 NewMIB.addReg(CurReg, getUndefRegState(CurUndef)); 4510 4511 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst; 4512 CurUndef = !MI.readsRegister(CurReg, TRI); 4513 NewMIB.addReg(CurReg, getUndefRegState(CurUndef)) 4514 .addImm(1) 4515 .add(predOps(ARMCC::AL)); 4516 4517 if (SrcLane == DstLane) 4518 NewMIB.addReg(SrcReg, RegState::Implicit); 4519 4520 MI.setDesc(get(ARM::VEXTd32)); 4521 MIB.addReg(DDst, RegState::Define); 4522 4523 // On the second instruction, DDst has definitely been defined above, so 4524 // it is not <undef>. DSrc, if present, can be <undef> as above. 4525 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst; 4526 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI); 4527 MIB.addReg(CurReg, getUndefRegState(CurUndef)); 4528 4529 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst; 4530 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI); 4531 MIB.addReg(CurReg, getUndefRegState(CurUndef)) 4532 .addImm(1) 4533 .add(predOps(ARMCC::AL)); 4534 4535 if (SrcLane != DstLane) 4536 MIB.addReg(SrcReg, RegState::Implicit); 4537 4538 // As before, the original destination is no longer represented, add it 4539 // implicitly. 4540 MIB.addReg(DstReg, RegState::Define | RegState::Implicit); 4541 if (ImplicitSReg != 0) 4542 MIB.addReg(ImplicitSReg, RegState::Implicit); 4543 break; 4544 } 4545 } 4546 4547 } 4548 4549 //===----------------------------------------------------------------------===// 4550 // Partial register updates 4551 //===----------------------------------------------------------------------===// 4552 // 4553 // Swift renames NEON registers with 64-bit granularity. That means any 4554 // instruction writing an S-reg implicitly reads the containing D-reg. The 4555 // problem is mostly avoided by translating f32 operations to v2f32 operations 4556 // on D-registers, but f32 loads are still a problem. 4557 // 4558 // These instructions can load an f32 into a NEON register: 4559 // 4560 // VLDRS - Only writes S, partial D update. 4561 // VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops. 4562 // VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops. 4563 // 4564 // FCONSTD can be used as a dependency-breaking instruction. 4565 unsigned ARMBaseInstrInfo::getPartialRegUpdateClearance( 4566 const MachineInstr &MI, unsigned OpNum, 4567 const TargetRegisterInfo *TRI) const { 4568 auto PartialUpdateClearance = Subtarget.getPartialUpdateClearance(); 4569 if (!PartialUpdateClearance) 4570 return 0; 4571 4572 assert(TRI && "Need TRI instance"); 4573 4574 const MachineOperand &MO = MI.getOperand(OpNum); 4575 if (MO.readsReg()) 4576 return 0; 4577 unsigned Reg = MO.getReg(); 4578 int UseOp = -1; 4579 4580 switch (MI.getOpcode()) { 4581 // Normal instructions writing only an S-register. 4582 case ARM::VLDRS: 4583 case ARM::FCONSTS: 4584 case ARM::VMOVSR: 4585 case ARM::VMOVv8i8: 4586 case ARM::VMOVv4i16: 4587 case ARM::VMOVv2i32: 4588 case ARM::VMOVv2f32: 4589 case ARM::VMOVv1i64: 4590 UseOp = MI.findRegisterUseOperandIdx(Reg, false, TRI); 4591 break; 4592 4593 // Explicitly reads the dependency. 4594 case ARM::VLD1LNd32: 4595 UseOp = 3; 4596 break; 4597 default: 4598 return 0; 4599 } 4600 4601 // If this instruction actually reads a value from Reg, there is no unwanted 4602 // dependency. 4603 if (UseOp != -1 && MI.getOperand(UseOp).readsReg()) 4604 return 0; 4605 4606 // We must be able to clobber the whole D-reg. 4607 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 4608 // Virtual register must be a foo:ssub_0<def,undef> operand. 4609 if (!MO.getSubReg() || MI.readsVirtualRegister(Reg)) 4610 return 0; 4611 } else if (ARM::SPRRegClass.contains(Reg)) { 4612 // Physical register: MI must define the full D-reg. 4613 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0, 4614 &ARM::DPRRegClass); 4615 if (!DReg || !MI.definesRegister(DReg, TRI)) 4616 return 0; 4617 } 4618 4619 // MI has an unwanted D-register dependency. 4620 // Avoid defs in the previous N instructrions. 4621 return PartialUpdateClearance; 4622 } 4623 4624 // Break a partial register dependency after getPartialRegUpdateClearance 4625 // returned non-zero. 4626 void ARMBaseInstrInfo::breakPartialRegDependency( 4627 MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const { 4628 assert(OpNum < MI.getDesc().getNumDefs() && "OpNum is not a def"); 4629 assert(TRI && "Need TRI instance"); 4630 4631 const MachineOperand &MO = MI.getOperand(OpNum); 4632 unsigned Reg = MO.getReg(); 4633 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && 4634 "Can't break virtual register dependencies."); 4635 unsigned DReg = Reg; 4636 4637 // If MI defines an S-reg, find the corresponding D super-register. 4638 if (ARM::SPRRegClass.contains(Reg)) { 4639 DReg = ARM::D0 + (Reg - ARM::S0) / 2; 4640 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken"); 4641 } 4642 4643 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps"); 4644 assert(MI.definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg"); 4645 4646 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines 4647 // the full D-register by loading the same value to both lanes. The 4648 // instruction is micro-coded with 2 uops, so don't do this until we can 4649 // properly schedule micro-coded instructions. The dispatcher stalls cause 4650 // too big regressions. 4651 4652 // Insert the dependency-breaking FCONSTD before MI. 4653 // 96 is the encoding of 0.5, but the actual value doesn't matter here. 4654 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::FCONSTD), DReg) 4655 .addImm(96) 4656 .add(predOps(ARMCC::AL)); 4657 MI.addRegisterKilled(DReg, TRI, true); 4658 } 4659 4660 bool ARMBaseInstrInfo::hasNOP() const { 4661 return Subtarget.getFeatureBits()[ARM::HasV6KOps]; 4662 } 4663 4664 bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const { 4665 if (MI->getNumOperands() < 4) 4666 return true; 4667 unsigned ShOpVal = MI->getOperand(3).getImm(); 4668 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal); 4669 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1. 4670 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) || 4671 ((ShImm == 1 || ShImm == 2) && 4672 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl)) 4673 return true; 4674 4675 return false; 4676 } 4677 4678 bool ARMBaseInstrInfo::getRegSequenceLikeInputs( 4679 const MachineInstr &MI, unsigned DefIdx, 4680 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const { 4681 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index"); 4682 assert(MI.isRegSequenceLike() && "Invalid kind of instruction"); 4683 4684 switch (MI.getOpcode()) { 4685 case ARM::VMOVDRR: 4686 // dX = VMOVDRR rY, rZ 4687 // is the same as: 4688 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1 4689 // Populate the InputRegs accordingly. 4690 // rY 4691 const MachineOperand *MOReg = &MI.getOperand(1); 4692 InputRegs.push_back( 4693 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0)); 4694 // rZ 4695 MOReg = &MI.getOperand(2); 4696 InputRegs.push_back( 4697 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1)); 4698 return true; 4699 } 4700 llvm_unreachable("Target dependent opcode missing"); 4701 } 4702 4703 bool ARMBaseInstrInfo::getExtractSubregLikeInputs( 4704 const MachineInstr &MI, unsigned DefIdx, 4705 RegSubRegPairAndIdx &InputReg) const { 4706 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index"); 4707 assert(MI.isExtractSubregLike() && "Invalid kind of instruction"); 4708 4709 switch (MI.getOpcode()) { 4710 case ARM::VMOVRRD: 4711 // rX, rY = VMOVRRD dZ 4712 // is the same as: 4713 // rX = EXTRACT_SUBREG dZ, ssub_0 4714 // rY = EXTRACT_SUBREG dZ, ssub_1 4715 const MachineOperand &MOReg = MI.getOperand(2); 4716 InputReg.Reg = MOReg.getReg(); 4717 InputReg.SubReg = MOReg.getSubReg(); 4718 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1; 4719 return true; 4720 } 4721 llvm_unreachable("Target dependent opcode missing"); 4722 } 4723 4724 bool ARMBaseInstrInfo::getInsertSubregLikeInputs( 4725 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg, 4726 RegSubRegPairAndIdx &InsertedReg) const { 4727 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index"); 4728 assert(MI.isInsertSubregLike() && "Invalid kind of instruction"); 4729 4730 switch (MI.getOpcode()) { 4731 case ARM::VSETLNi32: 4732 // dX = VSETLNi32 dY, rZ, imm 4733 const MachineOperand &MOBaseReg = MI.getOperand(1); 4734 const MachineOperand &MOInsertedReg = MI.getOperand(2); 4735 const MachineOperand &MOIndex = MI.getOperand(3); 4736 BaseReg.Reg = MOBaseReg.getReg(); 4737 BaseReg.SubReg = MOBaseReg.getSubReg(); 4738 4739 InsertedReg.Reg = MOInsertedReg.getReg(); 4740 InsertedReg.SubReg = MOInsertedReg.getSubReg(); 4741 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1; 4742 return true; 4743 } 4744 llvm_unreachable("Target dependent opcode missing"); 4745 } 4746