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