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