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