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