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