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