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 ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, 394 MachineBasicBlock *TBB, 395 MachineBasicBlock *FBB, 396 ArrayRef<MachineOperand> Cond, 397 const 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, 705 const DebugLoc &DL, unsigned DestReg, 706 unsigned SrcReg, 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, 1999 const DebugLoc &dl, unsigned DestReg, 2000 unsigned BaseReg, int NumBytes, 2001 ARMCC::CondCodes Pred, unsigned PredReg, 2002 const ARMBaseInstrInfo &TII, 2003 unsigned MIFlags) { 2004 if (NumBytes == 0 && DestReg != BaseReg) { 2005 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg) 2006 .addReg(BaseReg, RegState::Kill) 2007 .addImm((unsigned)Pred).addReg(PredReg).addReg(0) 2008 .setMIFlags(MIFlags); 2009 return; 2010 } 2011 2012 bool isSub = NumBytes < 0; 2013 if (isSub) NumBytes = -NumBytes; 2014 2015 while (NumBytes) { 2016 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes); 2017 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt); 2018 assert(ThisVal && "Didn't extract field correctly"); 2019 2020 // We will handle these bits from offset, clear them. 2021 NumBytes &= ~ThisVal; 2022 2023 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?"); 2024 2025 // Build the new ADD / SUB. 2026 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri; 2027 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg) 2028 .addReg(BaseReg, RegState::Kill).addImm(ThisVal) 2029 .addImm((unsigned)Pred).addReg(PredReg).addReg(0) 2030 .setMIFlags(MIFlags); 2031 BaseReg = DestReg; 2032 } 2033 } 2034 2035 bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget, 2036 MachineFunction &MF, MachineInstr *MI, 2037 unsigned NumBytes) { 2038 // This optimisation potentially adds lots of load and store 2039 // micro-operations, it's only really a great benefit to code-size. 2040 if (!MF.getFunction()->optForMinSize()) 2041 return false; 2042 2043 // If only one register is pushed/popped, LLVM can use an LDR/STR 2044 // instead. We can't modify those so make sure we're dealing with an 2045 // instruction we understand. 2046 bool IsPop = isPopOpcode(MI->getOpcode()); 2047 bool IsPush = isPushOpcode(MI->getOpcode()); 2048 if (!IsPush && !IsPop) 2049 return false; 2050 2051 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD || 2052 MI->getOpcode() == ARM::VLDMDIA_UPD; 2053 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH || 2054 MI->getOpcode() == ARM::tPOP || 2055 MI->getOpcode() == ARM::tPOP_RET; 2056 2057 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP && 2058 MI->getOperand(1).getReg() == ARM::SP)) && 2059 "trying to fold sp update into non-sp-updating push/pop"); 2060 2061 // The VFP push & pop act on D-registers, so we can only fold an adjustment 2062 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try 2063 // if this is violated. 2064 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0) 2065 return false; 2066 2067 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+ 2068 // pred) so the list starts at 4. Thumb1 starts after the predicate. 2069 int RegListIdx = IsT1PushPop ? 2 : 4; 2070 2071 // Calculate the space we'll need in terms of registers. 2072 unsigned FirstReg = MI->getOperand(RegListIdx).getReg(); 2073 unsigned RD0Reg, RegsNeeded; 2074 if (IsVFPPushPop) { 2075 RD0Reg = ARM::D0; 2076 RegsNeeded = NumBytes / 8; 2077 } else { 2078 RD0Reg = ARM::R0; 2079 RegsNeeded = NumBytes / 4; 2080 } 2081 2082 // We're going to have to strip all list operands off before 2083 // re-adding them since the order matters, so save the existing ones 2084 // for later. 2085 SmallVector<MachineOperand, 4> RegList; 2086 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i) 2087 RegList.push_back(MI->getOperand(i)); 2088 2089 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo(); 2090 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF); 2091 2092 // Now try to find enough space in the reglist to allocate NumBytes. 2093 for (unsigned CurReg = FirstReg - 1; CurReg >= RD0Reg && RegsNeeded; 2094 --CurReg) { 2095 if (!IsPop) { 2096 // Pushing any register is completely harmless, mark the 2097 // register involved as undef since we don't care about it in 2098 // the slightest. 2099 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false, 2100 false, false, true)); 2101 --RegsNeeded; 2102 continue; 2103 } 2104 2105 // However, we can only pop an extra register if it's not live. For 2106 // registers live within the function we might clobber a return value 2107 // register; the other way a register can be live here is if it's 2108 // callee-saved. 2109 if (isCalleeSavedRegister(CurReg, CSRegs) || 2110 MI->getParent()->computeRegisterLiveness(TRI, CurReg, MI) != 2111 MachineBasicBlock::LQR_Dead) { 2112 // VFP pops don't allow holes in the register list, so any skip is fatal 2113 // for our transformation. GPR pops do, so we should just keep looking. 2114 if (IsVFPPushPop) 2115 return false; 2116 else 2117 continue; 2118 } 2119 2120 // Mark the unimportant registers as <def,dead> in the POP. 2121 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false, 2122 true)); 2123 --RegsNeeded; 2124 } 2125 2126 if (RegsNeeded > 0) 2127 return false; 2128 2129 // Finally we know we can profitably perform the optimisation so go 2130 // ahead: strip all existing registers off and add them back again 2131 // in the right order. 2132 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i) 2133 MI->RemoveOperand(i); 2134 2135 // Add the complete list back in. 2136 MachineInstrBuilder MIB(MF, &*MI); 2137 for (int i = RegList.size() - 1; i >= 0; --i) 2138 MIB.addOperand(RegList[i]); 2139 2140 return true; 2141 } 2142 2143 bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx, 2144 unsigned FrameReg, int &Offset, 2145 const ARMBaseInstrInfo &TII) { 2146 unsigned Opcode = MI.getOpcode(); 2147 const MCInstrDesc &Desc = MI.getDesc(); 2148 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask); 2149 bool isSub = false; 2150 2151 // Memory operands in inline assembly always use AddrMode2. 2152 if (Opcode == ARM::INLINEASM) 2153 AddrMode = ARMII::AddrMode2; 2154 2155 if (Opcode == ARM::ADDri) { 2156 Offset += MI.getOperand(FrameRegIdx+1).getImm(); 2157 if (Offset == 0) { 2158 // Turn it into a move. 2159 MI.setDesc(TII.get(ARM::MOVr)); 2160 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); 2161 MI.RemoveOperand(FrameRegIdx+1); 2162 Offset = 0; 2163 return true; 2164 } else if (Offset < 0) { 2165 Offset = -Offset; 2166 isSub = true; 2167 MI.setDesc(TII.get(ARM::SUBri)); 2168 } 2169 2170 // Common case: small offset, fits into instruction. 2171 if (ARM_AM::getSOImmVal(Offset) != -1) { 2172 // Replace the FrameIndex with sp / fp 2173 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); 2174 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset); 2175 Offset = 0; 2176 return true; 2177 } 2178 2179 // Otherwise, pull as much of the immedidate into this ADDri/SUBri 2180 // as possible. 2181 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset); 2182 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt); 2183 2184 // We will handle these bits from offset, clear them. 2185 Offset &= ~ThisImmVal; 2186 2187 // Get the properly encoded SOImmVal field. 2188 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 && 2189 "Bit extraction didn't work?"); 2190 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal); 2191 } else { 2192 unsigned ImmIdx = 0; 2193 int InstrOffs = 0; 2194 unsigned NumBits = 0; 2195 unsigned Scale = 1; 2196 switch (AddrMode) { 2197 case ARMII::AddrMode_i12: { 2198 ImmIdx = FrameRegIdx + 1; 2199 InstrOffs = MI.getOperand(ImmIdx).getImm(); 2200 NumBits = 12; 2201 break; 2202 } 2203 case ARMII::AddrMode2: { 2204 ImmIdx = FrameRegIdx+2; 2205 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm()); 2206 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub) 2207 InstrOffs *= -1; 2208 NumBits = 12; 2209 break; 2210 } 2211 case ARMII::AddrMode3: { 2212 ImmIdx = FrameRegIdx+2; 2213 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm()); 2214 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub) 2215 InstrOffs *= -1; 2216 NumBits = 8; 2217 break; 2218 } 2219 case ARMII::AddrMode4: 2220 case ARMII::AddrMode6: 2221 // Can't fold any offset even if it's zero. 2222 return false; 2223 case ARMII::AddrMode5: { 2224 ImmIdx = FrameRegIdx+1; 2225 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm()); 2226 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub) 2227 InstrOffs *= -1; 2228 NumBits = 8; 2229 Scale = 4; 2230 break; 2231 } 2232 default: 2233 llvm_unreachable("Unsupported addressing mode!"); 2234 } 2235 2236 Offset += InstrOffs * Scale; 2237 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!"); 2238 if (Offset < 0) { 2239 Offset = -Offset; 2240 isSub = true; 2241 } 2242 2243 // Attempt to fold address comp. if opcode has offset bits 2244 if (NumBits > 0) { 2245 // Common case: small offset, fits into instruction. 2246 MachineOperand &ImmOp = MI.getOperand(ImmIdx); 2247 int ImmedOffset = Offset / Scale; 2248 unsigned Mask = (1 << NumBits) - 1; 2249 if ((unsigned)Offset <= Mask * Scale) { 2250 // Replace the FrameIndex with sp 2251 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); 2252 // FIXME: When addrmode2 goes away, this will simplify (like the 2253 // T2 version), as the LDR.i12 versions don't need the encoding 2254 // tricks for the offset value. 2255 if (isSub) { 2256 if (AddrMode == ARMII::AddrMode_i12) 2257 ImmedOffset = -ImmedOffset; 2258 else 2259 ImmedOffset |= 1 << NumBits; 2260 } 2261 ImmOp.ChangeToImmediate(ImmedOffset); 2262 Offset = 0; 2263 return true; 2264 } 2265 2266 // Otherwise, it didn't fit. Pull in what we can to simplify the immed. 2267 ImmedOffset = ImmedOffset & Mask; 2268 if (isSub) { 2269 if (AddrMode == ARMII::AddrMode_i12) 2270 ImmedOffset = -ImmedOffset; 2271 else 2272 ImmedOffset |= 1 << NumBits; 2273 } 2274 ImmOp.ChangeToImmediate(ImmedOffset); 2275 Offset &= ~(Mask*Scale); 2276 } 2277 } 2278 2279 Offset = (isSub) ? -Offset : Offset; 2280 return Offset == 0; 2281 } 2282 2283 /// analyzeCompare - For a comparison instruction, return the source registers 2284 /// in SrcReg and SrcReg2 if having two register operands, and the value it 2285 /// compares against in CmpValue. Return true if the comparison instruction 2286 /// can be analyzed. 2287 bool ARMBaseInstrInfo:: 2288 analyzeCompare(const MachineInstr *MI, unsigned &SrcReg, unsigned &SrcReg2, 2289 int &CmpMask, int &CmpValue) const { 2290 switch (MI->getOpcode()) { 2291 default: break; 2292 case ARM::CMPri: 2293 case ARM::t2CMPri: 2294 SrcReg = MI->getOperand(0).getReg(); 2295 SrcReg2 = 0; 2296 CmpMask = ~0; 2297 CmpValue = MI->getOperand(1).getImm(); 2298 return true; 2299 case ARM::CMPrr: 2300 case ARM::t2CMPrr: 2301 SrcReg = MI->getOperand(0).getReg(); 2302 SrcReg2 = MI->getOperand(1).getReg(); 2303 CmpMask = ~0; 2304 CmpValue = 0; 2305 return true; 2306 case ARM::TSTri: 2307 case ARM::t2TSTri: 2308 SrcReg = MI->getOperand(0).getReg(); 2309 SrcReg2 = 0; 2310 CmpMask = MI->getOperand(1).getImm(); 2311 CmpValue = 0; 2312 return true; 2313 } 2314 2315 return false; 2316 } 2317 2318 /// isSuitableForMask - Identify a suitable 'and' instruction that 2319 /// operates on the given source register and applies the same mask 2320 /// as a 'tst' instruction. Provide a limited look-through for copies. 2321 /// When successful, MI will hold the found instruction. 2322 static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg, 2323 int CmpMask, bool CommonUse) { 2324 switch (MI->getOpcode()) { 2325 case ARM::ANDri: 2326 case ARM::t2ANDri: 2327 if (CmpMask != MI->getOperand(2).getImm()) 2328 return false; 2329 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg()) 2330 return true; 2331 break; 2332 } 2333 2334 return false; 2335 } 2336 2337 /// getSwappedCondition - assume the flags are set by MI(a,b), return 2338 /// the condition code if we modify the instructions such that flags are 2339 /// set by MI(b,a). 2340 inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) { 2341 switch (CC) { 2342 default: return ARMCC::AL; 2343 case ARMCC::EQ: return ARMCC::EQ; 2344 case ARMCC::NE: return ARMCC::NE; 2345 case ARMCC::HS: return ARMCC::LS; 2346 case ARMCC::LO: return ARMCC::HI; 2347 case ARMCC::HI: return ARMCC::LO; 2348 case ARMCC::LS: return ARMCC::HS; 2349 case ARMCC::GE: return ARMCC::LE; 2350 case ARMCC::LT: return ARMCC::GT; 2351 case ARMCC::GT: return ARMCC::LT; 2352 case ARMCC::LE: return ARMCC::GE; 2353 } 2354 } 2355 2356 /// isRedundantFlagInstr - check whether the first instruction, whose only 2357 /// purpose is to update flags, can be made redundant. 2358 /// CMPrr can be made redundant by SUBrr if the operands are the same. 2359 /// CMPri can be made redundant by SUBri if the operands are the same. 2360 /// This function can be extended later on. 2361 inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg, 2362 unsigned SrcReg2, int ImmValue, 2363 MachineInstr *OI) { 2364 if ((CmpI->getOpcode() == ARM::CMPrr || 2365 CmpI->getOpcode() == ARM::t2CMPrr) && 2366 (OI->getOpcode() == ARM::SUBrr || 2367 OI->getOpcode() == ARM::t2SUBrr) && 2368 ((OI->getOperand(1).getReg() == SrcReg && 2369 OI->getOperand(2).getReg() == SrcReg2) || 2370 (OI->getOperand(1).getReg() == SrcReg2 && 2371 OI->getOperand(2).getReg() == SrcReg))) 2372 return true; 2373 2374 if ((CmpI->getOpcode() == ARM::CMPri || 2375 CmpI->getOpcode() == ARM::t2CMPri) && 2376 (OI->getOpcode() == ARM::SUBri || 2377 OI->getOpcode() == ARM::t2SUBri) && 2378 OI->getOperand(1).getReg() == SrcReg && 2379 OI->getOperand(2).getImm() == ImmValue) 2380 return true; 2381 return false; 2382 } 2383 2384 /// optimizeCompareInstr - Convert the instruction supplying the argument to the 2385 /// comparison into one that sets the zero bit in the flags register; 2386 /// Remove a redundant Compare instruction if an earlier instruction can set the 2387 /// flags in the same way as Compare. 2388 /// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two 2389 /// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the 2390 /// condition code of instructions which use the flags. 2391 bool ARMBaseInstrInfo:: 2392 optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, unsigned SrcReg2, 2393 int CmpMask, int CmpValue, 2394 const MachineRegisterInfo *MRI) const { 2395 // Get the unique definition of SrcReg. 2396 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg); 2397 if (!MI) return false; 2398 2399 // Masked compares sometimes use the same register as the corresponding 'and'. 2400 if (CmpMask != ~0) { 2401 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(*MI)) { 2402 MI = nullptr; 2403 for (MachineRegisterInfo::use_instr_iterator 2404 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end(); 2405 UI != UE; ++UI) { 2406 if (UI->getParent() != CmpInstr->getParent()) continue; 2407 MachineInstr *PotentialAND = &*UI; 2408 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) || 2409 isPredicated(*PotentialAND)) 2410 continue; 2411 MI = PotentialAND; 2412 break; 2413 } 2414 if (!MI) return false; 2415 } 2416 } 2417 2418 // Get ready to iterate backward from CmpInstr. 2419 MachineBasicBlock::iterator I = CmpInstr, E = MI, 2420 B = CmpInstr->getParent()->begin(); 2421 2422 // Early exit if CmpInstr is at the beginning of the BB. 2423 if (I == B) return false; 2424 2425 // There are two possible candidates which can be changed to set CPSR: 2426 // One is MI, the other is a SUB instruction. 2427 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1). 2428 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue). 2429 MachineInstr *Sub = nullptr; 2430 if (SrcReg2 != 0) 2431 // MI is not a candidate for CMPrr. 2432 MI = nullptr; 2433 else if (MI->getParent() != CmpInstr->getParent() || CmpValue != 0) { 2434 // Conservatively refuse to convert an instruction which isn't in the same 2435 // BB as the comparison. 2436 // For CMPri w/ CmpValue != 0, a Sub may still be a candidate. 2437 // Thus we cannot return here. 2438 if (CmpInstr->getOpcode() == ARM::CMPri || 2439 CmpInstr->getOpcode() == ARM::t2CMPri) 2440 MI = nullptr; 2441 else 2442 return false; 2443 } 2444 2445 // Check that CPSR isn't set between the comparison instruction and the one we 2446 // want to change. At the same time, search for Sub. 2447 const TargetRegisterInfo *TRI = &getRegisterInfo(); 2448 --I; 2449 for (; I != E; --I) { 2450 const MachineInstr &Instr = *I; 2451 2452 if (Instr.modifiesRegister(ARM::CPSR, TRI) || 2453 Instr.readsRegister(ARM::CPSR, TRI)) 2454 // This instruction modifies or uses CPSR after the one we want to 2455 // change. We can't do this transformation. 2456 return false; 2457 2458 // Check whether CmpInstr can be made redundant by the current instruction. 2459 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) { 2460 Sub = &*I; 2461 break; 2462 } 2463 2464 if (I == B) 2465 // The 'and' is below the comparison instruction. 2466 return false; 2467 } 2468 2469 // Return false if no candidates exist. 2470 if (!MI && !Sub) 2471 return false; 2472 2473 // The single candidate is called MI. 2474 if (!MI) MI = Sub; 2475 2476 // We can't use a predicated instruction - it doesn't always write the flags. 2477 if (isPredicated(*MI)) 2478 return false; 2479 2480 switch (MI->getOpcode()) { 2481 default: break; 2482 case ARM::RSBrr: 2483 case ARM::RSBri: 2484 case ARM::RSCrr: 2485 case ARM::RSCri: 2486 case ARM::ADDrr: 2487 case ARM::ADDri: 2488 case ARM::ADCrr: 2489 case ARM::ADCri: 2490 case ARM::SUBrr: 2491 case ARM::SUBri: 2492 case ARM::SBCrr: 2493 case ARM::SBCri: 2494 case ARM::t2RSBri: 2495 case ARM::t2ADDrr: 2496 case ARM::t2ADDri: 2497 case ARM::t2ADCrr: 2498 case ARM::t2ADCri: 2499 case ARM::t2SUBrr: 2500 case ARM::t2SUBri: 2501 case ARM::t2SBCrr: 2502 case ARM::t2SBCri: 2503 case ARM::ANDrr: 2504 case ARM::ANDri: 2505 case ARM::t2ANDrr: 2506 case ARM::t2ANDri: 2507 case ARM::ORRrr: 2508 case ARM::ORRri: 2509 case ARM::t2ORRrr: 2510 case ARM::t2ORRri: 2511 case ARM::EORrr: 2512 case ARM::EORri: 2513 case ARM::t2EORrr: 2514 case ARM::t2EORri: { 2515 // Scan forward for the use of CPSR 2516 // When checking against MI: if it's a conditional code that requires 2517 // checking of the V bit or C bit, then this is not safe to do. 2518 // It is safe to remove CmpInstr if CPSR is redefined or killed. 2519 // If we are done with the basic block, we need to check whether CPSR is 2520 // live-out. 2521 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4> 2522 OperandsToUpdate; 2523 bool isSafe = false; 2524 I = CmpInstr; 2525 E = CmpInstr->getParent()->end(); 2526 while (!isSafe && ++I != E) { 2527 const MachineInstr &Instr = *I; 2528 for (unsigned IO = 0, EO = Instr.getNumOperands(); 2529 !isSafe && IO != EO; ++IO) { 2530 const MachineOperand &MO = Instr.getOperand(IO); 2531 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) { 2532 isSafe = true; 2533 break; 2534 } 2535 if (!MO.isReg() || MO.getReg() != ARM::CPSR) 2536 continue; 2537 if (MO.isDef()) { 2538 isSafe = true; 2539 break; 2540 } 2541 // Condition code is after the operand before CPSR except for VSELs. 2542 ARMCC::CondCodes CC; 2543 bool IsInstrVSel = true; 2544 switch (Instr.getOpcode()) { 2545 default: 2546 IsInstrVSel = false; 2547 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm(); 2548 break; 2549 case ARM::VSELEQD: 2550 case ARM::VSELEQS: 2551 CC = ARMCC::EQ; 2552 break; 2553 case ARM::VSELGTD: 2554 case ARM::VSELGTS: 2555 CC = ARMCC::GT; 2556 break; 2557 case ARM::VSELGED: 2558 case ARM::VSELGES: 2559 CC = ARMCC::GE; 2560 break; 2561 case ARM::VSELVSS: 2562 case ARM::VSELVSD: 2563 CC = ARMCC::VS; 2564 break; 2565 } 2566 2567 if (Sub) { 2568 ARMCC::CondCodes NewCC = getSwappedCondition(CC); 2569 if (NewCC == ARMCC::AL) 2570 return false; 2571 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based 2572 // on CMP needs to be updated to be based on SUB. 2573 // Push the condition code operands to OperandsToUpdate. 2574 // If it is safe to remove CmpInstr, the condition code of these 2575 // operands will be modified. 2576 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 && 2577 Sub->getOperand(2).getReg() == SrcReg) { 2578 // VSel doesn't support condition code update. 2579 if (IsInstrVSel) 2580 return false; 2581 OperandsToUpdate.push_back( 2582 std::make_pair(&((*I).getOperand(IO - 1)), NewCC)); 2583 } 2584 } else { 2585 // No Sub, so this is x = <op> y, z; cmp x, 0. 2586 switch (CC) { 2587 case ARMCC::EQ: // Z 2588 case ARMCC::NE: // Z 2589 case ARMCC::MI: // N 2590 case ARMCC::PL: // N 2591 case ARMCC::AL: // none 2592 // CPSR can be used multiple times, we should continue. 2593 break; 2594 case ARMCC::HS: // C 2595 case ARMCC::LO: // C 2596 case ARMCC::VS: // V 2597 case ARMCC::VC: // V 2598 case ARMCC::HI: // C Z 2599 case ARMCC::LS: // C Z 2600 case ARMCC::GE: // N V 2601 case ARMCC::LT: // N V 2602 case ARMCC::GT: // Z N V 2603 case ARMCC::LE: // Z N V 2604 // The instruction uses the V bit or C bit which is not safe. 2605 return false; 2606 } 2607 } 2608 } 2609 } 2610 2611 // If CPSR is not killed nor re-defined, we should check whether it is 2612 // live-out. If it is live-out, do not optimize. 2613 if (!isSafe) { 2614 MachineBasicBlock *MBB = CmpInstr->getParent(); 2615 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), 2616 SE = MBB->succ_end(); SI != SE; ++SI) 2617 if ((*SI)->isLiveIn(ARM::CPSR)) 2618 return false; 2619 } 2620 2621 // Toggle the optional operand to CPSR. 2622 MI->getOperand(5).setReg(ARM::CPSR); 2623 MI->getOperand(5).setIsDef(true); 2624 assert(!isPredicated(*MI) && "Can't use flags from predicated instruction"); 2625 CmpInstr->eraseFromParent(); 2626 2627 // Modify the condition code of operands in OperandsToUpdate. 2628 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to 2629 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc. 2630 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++) 2631 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second); 2632 return true; 2633 } 2634 } 2635 2636 return false; 2637 } 2638 2639 bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI, 2640 MachineInstr *DefMI, unsigned Reg, 2641 MachineRegisterInfo *MRI) const { 2642 // Fold large immediates into add, sub, or, xor. 2643 unsigned DefOpc = DefMI->getOpcode(); 2644 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm) 2645 return false; 2646 if (!DefMI->getOperand(1).isImm()) 2647 // Could be t2MOVi32imm <ga:xx> 2648 return false; 2649 2650 if (!MRI->hasOneNonDBGUse(Reg)) 2651 return false; 2652 2653 const MCInstrDesc &DefMCID = DefMI->getDesc(); 2654 if (DefMCID.hasOptionalDef()) { 2655 unsigned NumOps = DefMCID.getNumOperands(); 2656 const MachineOperand &MO = DefMI->getOperand(NumOps-1); 2657 if (MO.getReg() == ARM::CPSR && !MO.isDead()) 2658 // If DefMI defines CPSR and it is not dead, it's obviously not safe 2659 // to delete DefMI. 2660 return false; 2661 } 2662 2663 const MCInstrDesc &UseMCID = UseMI->getDesc(); 2664 if (UseMCID.hasOptionalDef()) { 2665 unsigned NumOps = UseMCID.getNumOperands(); 2666 if (UseMI->getOperand(NumOps-1).getReg() == ARM::CPSR) 2667 // If the instruction sets the flag, do not attempt this optimization 2668 // since it may change the semantics of the code. 2669 return false; 2670 } 2671 2672 unsigned UseOpc = UseMI->getOpcode(); 2673 unsigned NewUseOpc = 0; 2674 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm(); 2675 uint32_t SOImmValV1 = 0, SOImmValV2 = 0; 2676 bool Commute = false; 2677 switch (UseOpc) { 2678 default: return false; 2679 case ARM::SUBrr: 2680 case ARM::ADDrr: 2681 case ARM::ORRrr: 2682 case ARM::EORrr: 2683 case ARM::t2SUBrr: 2684 case ARM::t2ADDrr: 2685 case ARM::t2ORRrr: 2686 case ARM::t2EORrr: { 2687 Commute = UseMI->getOperand(2).getReg() != Reg; 2688 switch (UseOpc) { 2689 default: break; 2690 case ARM::ADDrr: 2691 case ARM::SUBrr: { 2692 if (UseOpc == ARM::SUBrr && Commute) 2693 return false; 2694 2695 // ADD/SUB are special because they're essentially the same operation, so 2696 // we can handle a larger range of immediates. 2697 if (ARM_AM::isSOImmTwoPartVal(ImmVal)) 2698 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::ADDri : ARM::SUBri; 2699 else if (ARM_AM::isSOImmTwoPartVal(-ImmVal)) { 2700 ImmVal = -ImmVal; 2701 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::SUBri : ARM::ADDri; 2702 } else 2703 return false; 2704 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal); 2705 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal); 2706 break; 2707 } 2708 case ARM::ORRrr: 2709 case ARM::EORrr: { 2710 if (!ARM_AM::isSOImmTwoPartVal(ImmVal)) 2711 return false; 2712 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal); 2713 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal); 2714 switch (UseOpc) { 2715 default: break; 2716 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break; 2717 case ARM::EORrr: NewUseOpc = ARM::EORri; break; 2718 } 2719 break; 2720 } 2721 case ARM::t2ADDrr: 2722 case ARM::t2SUBrr: { 2723 if (UseOpc == ARM::t2SUBrr && Commute) 2724 return false; 2725 2726 // ADD/SUB are special because they're essentially the same operation, so 2727 // we can handle a larger range of immediates. 2728 if (ARM_AM::isT2SOImmTwoPartVal(ImmVal)) 2729 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2ADDri : ARM::t2SUBri; 2730 else if (ARM_AM::isT2SOImmTwoPartVal(-ImmVal)) { 2731 ImmVal = -ImmVal; 2732 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2SUBri : ARM::t2ADDri; 2733 } else 2734 return false; 2735 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal); 2736 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal); 2737 break; 2738 } 2739 case ARM::t2ORRrr: 2740 case ARM::t2EORrr: { 2741 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal)) 2742 return false; 2743 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal); 2744 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal); 2745 switch (UseOpc) { 2746 default: break; 2747 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break; 2748 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break; 2749 } 2750 break; 2751 } 2752 } 2753 } 2754 } 2755 2756 unsigned OpIdx = Commute ? 2 : 1; 2757 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg(); 2758 bool isKill = UseMI->getOperand(OpIdx).isKill(); 2759 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg)); 2760 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(), 2761 UseMI, UseMI->getDebugLoc(), 2762 get(NewUseOpc), NewReg) 2763 .addReg(Reg1, getKillRegState(isKill)) 2764 .addImm(SOImmValV1))); 2765 UseMI->setDesc(get(NewUseOpc)); 2766 UseMI->getOperand(1).setReg(NewReg); 2767 UseMI->getOperand(1).setIsKill(); 2768 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2); 2769 DefMI->eraseFromParent(); 2770 return true; 2771 } 2772 2773 static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData, 2774 const MachineInstr *MI) { 2775 switch (MI->getOpcode()) { 2776 default: { 2777 const MCInstrDesc &Desc = MI->getDesc(); 2778 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass()); 2779 assert(UOps >= 0 && "bad # UOps"); 2780 return UOps; 2781 } 2782 2783 case ARM::LDRrs: 2784 case ARM::LDRBrs: 2785 case ARM::STRrs: 2786 case ARM::STRBrs: { 2787 unsigned ShOpVal = MI->getOperand(3).getImm(); 2788 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub; 2789 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 2790 if (!isSub && 2791 (ShImm == 0 || 2792 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 2793 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))) 2794 return 1; 2795 return 2; 2796 } 2797 2798 case ARM::LDRH: 2799 case ARM::STRH: { 2800 if (!MI->getOperand(2).getReg()) 2801 return 1; 2802 2803 unsigned ShOpVal = MI->getOperand(3).getImm(); 2804 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub; 2805 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 2806 if (!isSub && 2807 (ShImm == 0 || 2808 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 2809 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))) 2810 return 1; 2811 return 2; 2812 } 2813 2814 case ARM::LDRSB: 2815 case ARM::LDRSH: 2816 return (ARM_AM::getAM3Op(MI->getOperand(3).getImm()) == ARM_AM::sub) ? 3:2; 2817 2818 case ARM::LDRSB_POST: 2819 case ARM::LDRSH_POST: { 2820 unsigned Rt = MI->getOperand(0).getReg(); 2821 unsigned Rm = MI->getOperand(3).getReg(); 2822 return (Rt == Rm) ? 4 : 3; 2823 } 2824 2825 case ARM::LDR_PRE_REG: 2826 case ARM::LDRB_PRE_REG: { 2827 unsigned Rt = MI->getOperand(0).getReg(); 2828 unsigned Rm = MI->getOperand(3).getReg(); 2829 if (Rt == Rm) 2830 return 3; 2831 unsigned ShOpVal = MI->getOperand(4).getImm(); 2832 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub; 2833 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 2834 if (!isSub && 2835 (ShImm == 0 || 2836 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 2837 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))) 2838 return 2; 2839 return 3; 2840 } 2841 2842 case ARM::STR_PRE_REG: 2843 case ARM::STRB_PRE_REG: { 2844 unsigned ShOpVal = MI->getOperand(4).getImm(); 2845 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub; 2846 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 2847 if (!isSub && 2848 (ShImm == 0 || 2849 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 2850 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))) 2851 return 2; 2852 return 3; 2853 } 2854 2855 case ARM::LDRH_PRE: 2856 case ARM::STRH_PRE: { 2857 unsigned Rt = MI->getOperand(0).getReg(); 2858 unsigned Rm = MI->getOperand(3).getReg(); 2859 if (!Rm) 2860 return 2; 2861 if (Rt == Rm) 2862 return 3; 2863 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) 2864 ? 3 : 2; 2865 } 2866 2867 case ARM::LDR_POST_REG: 2868 case ARM::LDRB_POST_REG: 2869 case ARM::LDRH_POST: { 2870 unsigned Rt = MI->getOperand(0).getReg(); 2871 unsigned Rm = MI->getOperand(3).getReg(); 2872 return (Rt == Rm) ? 3 : 2; 2873 } 2874 2875 case ARM::LDR_PRE_IMM: 2876 case ARM::LDRB_PRE_IMM: 2877 case ARM::LDR_POST_IMM: 2878 case ARM::LDRB_POST_IMM: 2879 case ARM::STRB_POST_IMM: 2880 case ARM::STRB_POST_REG: 2881 case ARM::STRB_PRE_IMM: 2882 case ARM::STRH_POST: 2883 case ARM::STR_POST_IMM: 2884 case ARM::STR_POST_REG: 2885 case ARM::STR_PRE_IMM: 2886 return 2; 2887 2888 case ARM::LDRSB_PRE: 2889 case ARM::LDRSH_PRE: { 2890 unsigned Rm = MI->getOperand(3).getReg(); 2891 if (Rm == 0) 2892 return 3; 2893 unsigned Rt = MI->getOperand(0).getReg(); 2894 if (Rt == Rm) 2895 return 4; 2896 unsigned ShOpVal = MI->getOperand(4).getImm(); 2897 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub; 2898 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 2899 if (!isSub && 2900 (ShImm == 0 || 2901 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 2902 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))) 2903 return 3; 2904 return 4; 2905 } 2906 2907 case ARM::LDRD: { 2908 unsigned Rt = MI->getOperand(0).getReg(); 2909 unsigned Rn = MI->getOperand(2).getReg(); 2910 unsigned Rm = MI->getOperand(3).getReg(); 2911 if (Rm) 2912 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3; 2913 return (Rt == Rn) ? 3 : 2; 2914 } 2915 2916 case ARM::STRD: { 2917 unsigned Rm = MI->getOperand(3).getReg(); 2918 if (Rm) 2919 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3; 2920 return 2; 2921 } 2922 2923 case ARM::LDRD_POST: 2924 case ARM::t2LDRD_POST: 2925 return 3; 2926 2927 case ARM::STRD_POST: 2928 case ARM::t2STRD_POST: 2929 return 4; 2930 2931 case ARM::LDRD_PRE: { 2932 unsigned Rt = MI->getOperand(0).getReg(); 2933 unsigned Rn = MI->getOperand(3).getReg(); 2934 unsigned Rm = MI->getOperand(4).getReg(); 2935 if (Rm) 2936 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4; 2937 return (Rt == Rn) ? 4 : 3; 2938 } 2939 2940 case ARM::t2LDRD_PRE: { 2941 unsigned Rt = MI->getOperand(0).getReg(); 2942 unsigned Rn = MI->getOperand(3).getReg(); 2943 return (Rt == Rn) ? 4 : 3; 2944 } 2945 2946 case ARM::STRD_PRE: { 2947 unsigned Rm = MI->getOperand(4).getReg(); 2948 if (Rm) 2949 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4; 2950 return 3; 2951 } 2952 2953 case ARM::t2STRD_PRE: 2954 return 3; 2955 2956 case ARM::t2LDR_POST: 2957 case ARM::t2LDRB_POST: 2958 case ARM::t2LDRB_PRE: 2959 case ARM::t2LDRSBi12: 2960 case ARM::t2LDRSBi8: 2961 case ARM::t2LDRSBpci: 2962 case ARM::t2LDRSBs: 2963 case ARM::t2LDRH_POST: 2964 case ARM::t2LDRH_PRE: 2965 case ARM::t2LDRSBT: 2966 case ARM::t2LDRSB_POST: 2967 case ARM::t2LDRSB_PRE: 2968 case ARM::t2LDRSH_POST: 2969 case ARM::t2LDRSH_PRE: 2970 case ARM::t2LDRSHi12: 2971 case ARM::t2LDRSHi8: 2972 case ARM::t2LDRSHpci: 2973 case ARM::t2LDRSHs: 2974 return 2; 2975 2976 case ARM::t2LDRDi8: { 2977 unsigned Rt = MI->getOperand(0).getReg(); 2978 unsigned Rn = MI->getOperand(2).getReg(); 2979 return (Rt == Rn) ? 3 : 2; 2980 } 2981 2982 case ARM::t2STRB_POST: 2983 case ARM::t2STRB_PRE: 2984 case ARM::t2STRBs: 2985 case ARM::t2STRDi8: 2986 case ARM::t2STRH_POST: 2987 case ARM::t2STRH_PRE: 2988 case ARM::t2STRHs: 2989 case ARM::t2STR_POST: 2990 case ARM::t2STR_PRE: 2991 case ARM::t2STRs: 2992 return 2; 2993 } 2994 } 2995 2996 // Return the number of 32-bit words loaded by LDM or stored by STM. If this 2997 // can't be easily determined return 0 (missing MachineMemOperand). 2998 // 2999 // FIXME: The current MachineInstr design does not support relying on machine 3000 // mem operands to determine the width of a memory access. Instead, we expect 3001 // the target to provide this information based on the instruction opcode and 3002 // operands. However, using MachineMemOperand is the best solution now for 3003 // two reasons: 3004 // 3005 // 1) getNumMicroOps tries to infer LDM memory width from the total number of MI 3006 // operands. This is much more dangerous than using the MachineMemOperand 3007 // sizes because CodeGen passes can insert/remove optional machine operands. In 3008 // fact, it's totally incorrect for preRA passes and appears to be wrong for 3009 // postRA passes as well. 3010 // 3011 // 2) getNumLDMAddresses is only used by the scheduling machine model and any 3012 // machine model that calls this should handle the unknown (zero size) case. 3013 // 3014 // Long term, we should require a target hook that verifies MachineMemOperand 3015 // sizes during MC lowering. That target hook should be local to MC lowering 3016 // because we can't ensure that it is aware of other MI forms. Doing this will 3017 // ensure that MachineMemOperands are correctly propagated through all passes. 3018 unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr *MI) const { 3019 unsigned Size = 0; 3020 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(), 3021 E = MI->memoperands_end(); I != E; ++I) { 3022 Size += (*I)->getSize(); 3023 } 3024 return Size / 4; 3025 } 3026 3027 unsigned 3028 ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData, 3029 const MachineInstr *MI) const { 3030 if (!ItinData || ItinData->isEmpty()) 3031 return 1; 3032 3033 const MCInstrDesc &Desc = MI->getDesc(); 3034 unsigned Class = Desc.getSchedClass(); 3035 int ItinUOps = ItinData->getNumMicroOps(Class); 3036 if (ItinUOps >= 0) { 3037 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore())) 3038 return getNumMicroOpsSwiftLdSt(ItinData, MI); 3039 3040 return ItinUOps; 3041 } 3042 3043 unsigned Opc = MI->getOpcode(); 3044 switch (Opc) { 3045 default: 3046 llvm_unreachable("Unexpected multi-uops instruction!"); 3047 case ARM::VLDMQIA: 3048 case ARM::VSTMQIA: 3049 return 2; 3050 3051 // The number of uOps for load / store multiple are determined by the number 3052 // registers. 3053 // 3054 // On Cortex-A8, each pair of register loads / stores can be scheduled on the 3055 // same cycle. The scheduling for the first load / store must be done 3056 // separately by assuming the address is not 64-bit aligned. 3057 // 3058 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address 3059 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON 3060 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1. 3061 case ARM::VLDMDIA: 3062 case ARM::VLDMDIA_UPD: 3063 case ARM::VLDMDDB_UPD: 3064 case ARM::VLDMSIA: 3065 case ARM::VLDMSIA_UPD: 3066 case ARM::VLDMSDB_UPD: 3067 case ARM::VSTMDIA: 3068 case ARM::VSTMDIA_UPD: 3069 case ARM::VSTMDDB_UPD: 3070 case ARM::VSTMSIA: 3071 case ARM::VSTMSIA_UPD: 3072 case ARM::VSTMSDB_UPD: { 3073 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands(); 3074 return (NumRegs / 2) + (NumRegs % 2) + 1; 3075 } 3076 3077 case ARM::LDMIA_RET: 3078 case ARM::LDMIA: 3079 case ARM::LDMDA: 3080 case ARM::LDMDB: 3081 case ARM::LDMIB: 3082 case ARM::LDMIA_UPD: 3083 case ARM::LDMDA_UPD: 3084 case ARM::LDMDB_UPD: 3085 case ARM::LDMIB_UPD: 3086 case ARM::STMIA: 3087 case ARM::STMDA: 3088 case ARM::STMDB: 3089 case ARM::STMIB: 3090 case ARM::STMIA_UPD: 3091 case ARM::STMDA_UPD: 3092 case ARM::STMDB_UPD: 3093 case ARM::STMIB_UPD: 3094 case ARM::tLDMIA: 3095 case ARM::tLDMIA_UPD: 3096 case ARM::tSTMIA_UPD: 3097 case ARM::tPOP_RET: 3098 case ARM::tPOP: 3099 case ARM::tPUSH: 3100 case ARM::t2LDMIA_RET: 3101 case ARM::t2LDMIA: 3102 case ARM::t2LDMDB: 3103 case ARM::t2LDMIA_UPD: 3104 case ARM::t2LDMDB_UPD: 3105 case ARM::t2STMIA: 3106 case ARM::t2STMDB: 3107 case ARM::t2STMIA_UPD: 3108 case ARM::t2STMDB_UPD: { 3109 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1; 3110 if (Subtarget.isSwift()) { 3111 int UOps = 1 + NumRegs; // One for address computation, one for each ld / st. 3112 switch (Opc) { 3113 default: break; 3114 case ARM::VLDMDIA_UPD: 3115 case ARM::VLDMDDB_UPD: 3116 case ARM::VLDMSIA_UPD: 3117 case ARM::VLDMSDB_UPD: 3118 case ARM::VSTMDIA_UPD: 3119 case ARM::VSTMDDB_UPD: 3120 case ARM::VSTMSIA_UPD: 3121 case ARM::VSTMSDB_UPD: 3122 case ARM::LDMIA_UPD: 3123 case ARM::LDMDA_UPD: 3124 case ARM::LDMDB_UPD: 3125 case ARM::LDMIB_UPD: 3126 case ARM::STMIA_UPD: 3127 case ARM::STMDA_UPD: 3128 case ARM::STMDB_UPD: 3129 case ARM::STMIB_UPD: 3130 case ARM::tLDMIA_UPD: 3131 case ARM::tSTMIA_UPD: 3132 case ARM::t2LDMIA_UPD: 3133 case ARM::t2LDMDB_UPD: 3134 case ARM::t2STMIA_UPD: 3135 case ARM::t2STMDB_UPD: 3136 ++UOps; // One for base register writeback. 3137 break; 3138 case ARM::LDMIA_RET: 3139 case ARM::tPOP_RET: 3140 case ARM::t2LDMIA_RET: 3141 UOps += 2; // One for base reg wb, one for write to pc. 3142 break; 3143 } 3144 return UOps; 3145 } else if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) { 3146 if (NumRegs < 4) 3147 return 2; 3148 // 4 registers would be issued: 2, 2. 3149 // 5 registers would be issued: 2, 2, 1. 3150 int A8UOps = (NumRegs / 2); 3151 if (NumRegs % 2) 3152 ++A8UOps; 3153 return A8UOps; 3154 } else if (Subtarget.isLikeA9()) { 3155 int A9UOps = (NumRegs / 2); 3156 // If there are odd number of registers or if it's not 64-bit aligned, 3157 // then it takes an extra AGU (Address Generation Unit) cycle. 3158 if ((NumRegs % 2) || 3159 !MI->hasOneMemOperand() || 3160 (*MI->memoperands_begin())->getAlignment() < 8) 3161 ++A9UOps; 3162 return A9UOps; 3163 } else { 3164 // Assume the worst. 3165 return NumRegs; 3166 } 3167 } 3168 } 3169 } 3170 3171 int 3172 ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData, 3173 const MCInstrDesc &DefMCID, 3174 unsigned DefClass, 3175 unsigned DefIdx, unsigned DefAlign) const { 3176 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1; 3177 if (RegNo <= 0) 3178 // Def is the address writeback. 3179 return ItinData->getOperandCycle(DefClass, DefIdx); 3180 3181 int DefCycle; 3182 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) { 3183 // (regno / 2) + (regno % 2) + 1 3184 DefCycle = RegNo / 2 + 1; 3185 if (RegNo % 2) 3186 ++DefCycle; 3187 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) { 3188 DefCycle = RegNo; 3189 bool isSLoad = false; 3190 3191 switch (DefMCID.getOpcode()) { 3192 default: break; 3193 case ARM::VLDMSIA: 3194 case ARM::VLDMSIA_UPD: 3195 case ARM::VLDMSDB_UPD: 3196 isSLoad = true; 3197 break; 3198 } 3199 3200 // If there are odd number of 'S' registers or if it's not 64-bit aligned, 3201 // then it takes an extra cycle. 3202 if ((isSLoad && (RegNo % 2)) || DefAlign < 8) 3203 ++DefCycle; 3204 } else { 3205 // Assume the worst. 3206 DefCycle = RegNo + 2; 3207 } 3208 3209 return DefCycle; 3210 } 3211 3212 int 3213 ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData, 3214 const MCInstrDesc &DefMCID, 3215 unsigned DefClass, 3216 unsigned DefIdx, unsigned DefAlign) const { 3217 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1; 3218 if (RegNo <= 0) 3219 // Def is the address writeback. 3220 return ItinData->getOperandCycle(DefClass, DefIdx); 3221 3222 int DefCycle; 3223 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) { 3224 // 4 registers would be issued: 1, 2, 1. 3225 // 5 registers would be issued: 1, 2, 2. 3226 DefCycle = RegNo / 2; 3227 if (DefCycle < 1) 3228 DefCycle = 1; 3229 // Result latency is issue cycle + 2: E2. 3230 DefCycle += 2; 3231 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) { 3232 DefCycle = (RegNo / 2); 3233 // If there are odd number of registers or if it's not 64-bit aligned, 3234 // then it takes an extra AGU (Address Generation Unit) cycle. 3235 if ((RegNo % 2) || DefAlign < 8) 3236 ++DefCycle; 3237 // Result latency is AGU cycles + 2. 3238 DefCycle += 2; 3239 } else { 3240 // Assume the worst. 3241 DefCycle = RegNo + 2; 3242 } 3243 3244 return DefCycle; 3245 } 3246 3247 int 3248 ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData, 3249 const MCInstrDesc &UseMCID, 3250 unsigned UseClass, 3251 unsigned UseIdx, unsigned UseAlign) const { 3252 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1; 3253 if (RegNo <= 0) 3254 return ItinData->getOperandCycle(UseClass, UseIdx); 3255 3256 int UseCycle; 3257 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) { 3258 // (regno / 2) + (regno % 2) + 1 3259 UseCycle = RegNo / 2 + 1; 3260 if (RegNo % 2) 3261 ++UseCycle; 3262 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) { 3263 UseCycle = RegNo; 3264 bool isSStore = false; 3265 3266 switch (UseMCID.getOpcode()) { 3267 default: break; 3268 case ARM::VSTMSIA: 3269 case ARM::VSTMSIA_UPD: 3270 case ARM::VSTMSDB_UPD: 3271 isSStore = true; 3272 break; 3273 } 3274 3275 // If there are odd number of 'S' registers or if it's not 64-bit aligned, 3276 // then it takes an extra cycle. 3277 if ((isSStore && (RegNo % 2)) || UseAlign < 8) 3278 ++UseCycle; 3279 } else { 3280 // Assume the worst. 3281 UseCycle = RegNo + 2; 3282 } 3283 3284 return UseCycle; 3285 } 3286 3287 int 3288 ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData, 3289 const MCInstrDesc &UseMCID, 3290 unsigned UseClass, 3291 unsigned UseIdx, unsigned UseAlign) const { 3292 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1; 3293 if (RegNo <= 0) 3294 return ItinData->getOperandCycle(UseClass, UseIdx); 3295 3296 int UseCycle; 3297 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) { 3298 UseCycle = RegNo / 2; 3299 if (UseCycle < 2) 3300 UseCycle = 2; 3301 // Read in E3. 3302 UseCycle += 2; 3303 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) { 3304 UseCycle = (RegNo / 2); 3305 // If there are odd number of registers or if it's not 64-bit aligned, 3306 // then it takes an extra AGU (Address Generation Unit) cycle. 3307 if ((RegNo % 2) || UseAlign < 8) 3308 ++UseCycle; 3309 } else { 3310 // Assume the worst. 3311 UseCycle = 1; 3312 } 3313 return UseCycle; 3314 } 3315 3316 int 3317 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, 3318 const MCInstrDesc &DefMCID, 3319 unsigned DefIdx, unsigned DefAlign, 3320 const MCInstrDesc &UseMCID, 3321 unsigned UseIdx, unsigned UseAlign) const { 3322 unsigned DefClass = DefMCID.getSchedClass(); 3323 unsigned UseClass = UseMCID.getSchedClass(); 3324 3325 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands()) 3326 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx); 3327 3328 // This may be a def / use of a variable_ops instruction, the operand 3329 // latency might be determinable dynamically. Let the target try to 3330 // figure it out. 3331 int DefCycle = -1; 3332 bool LdmBypass = false; 3333 switch (DefMCID.getOpcode()) { 3334 default: 3335 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx); 3336 break; 3337 3338 case ARM::VLDMDIA: 3339 case ARM::VLDMDIA_UPD: 3340 case ARM::VLDMDDB_UPD: 3341 case ARM::VLDMSIA: 3342 case ARM::VLDMSIA_UPD: 3343 case ARM::VLDMSDB_UPD: 3344 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign); 3345 break; 3346 3347 case ARM::LDMIA_RET: 3348 case ARM::LDMIA: 3349 case ARM::LDMDA: 3350 case ARM::LDMDB: 3351 case ARM::LDMIB: 3352 case ARM::LDMIA_UPD: 3353 case ARM::LDMDA_UPD: 3354 case ARM::LDMDB_UPD: 3355 case ARM::LDMIB_UPD: 3356 case ARM::tLDMIA: 3357 case ARM::tLDMIA_UPD: 3358 case ARM::tPUSH: 3359 case ARM::t2LDMIA_RET: 3360 case ARM::t2LDMIA: 3361 case ARM::t2LDMDB: 3362 case ARM::t2LDMIA_UPD: 3363 case ARM::t2LDMDB_UPD: 3364 LdmBypass = 1; 3365 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign); 3366 break; 3367 } 3368 3369 if (DefCycle == -1) 3370 // We can't seem to determine the result latency of the def, assume it's 2. 3371 DefCycle = 2; 3372 3373 int UseCycle = -1; 3374 switch (UseMCID.getOpcode()) { 3375 default: 3376 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx); 3377 break; 3378 3379 case ARM::VSTMDIA: 3380 case ARM::VSTMDIA_UPD: 3381 case ARM::VSTMDDB_UPD: 3382 case ARM::VSTMSIA: 3383 case ARM::VSTMSIA_UPD: 3384 case ARM::VSTMSDB_UPD: 3385 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign); 3386 break; 3387 3388 case ARM::STMIA: 3389 case ARM::STMDA: 3390 case ARM::STMDB: 3391 case ARM::STMIB: 3392 case ARM::STMIA_UPD: 3393 case ARM::STMDA_UPD: 3394 case ARM::STMDB_UPD: 3395 case ARM::STMIB_UPD: 3396 case ARM::tSTMIA_UPD: 3397 case ARM::tPOP_RET: 3398 case ARM::tPOP: 3399 case ARM::t2STMIA: 3400 case ARM::t2STMDB: 3401 case ARM::t2STMIA_UPD: 3402 case ARM::t2STMDB_UPD: 3403 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign); 3404 break; 3405 } 3406 3407 if (UseCycle == -1) 3408 // Assume it's read in the first stage. 3409 UseCycle = 1; 3410 3411 UseCycle = DefCycle - UseCycle + 1; 3412 if (UseCycle > 0) { 3413 if (LdmBypass) { 3414 // It's a variable_ops instruction so we can't use DefIdx here. Just use 3415 // first def operand. 3416 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1, 3417 UseClass, UseIdx)) 3418 --UseCycle; 3419 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx, 3420 UseClass, UseIdx)) { 3421 --UseCycle; 3422 } 3423 } 3424 3425 return UseCycle; 3426 } 3427 3428 static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI, 3429 const MachineInstr *MI, unsigned Reg, 3430 unsigned &DefIdx, unsigned &Dist) { 3431 Dist = 0; 3432 3433 MachineBasicBlock::const_iterator I = MI; ++I; 3434 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator()); 3435 assert(II->isInsideBundle() && "Empty bundle?"); 3436 3437 int Idx = -1; 3438 while (II->isInsideBundle()) { 3439 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI); 3440 if (Idx != -1) 3441 break; 3442 --II; 3443 ++Dist; 3444 } 3445 3446 assert(Idx != -1 && "Cannot find bundled definition!"); 3447 DefIdx = Idx; 3448 return &*II; 3449 } 3450 3451 static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI, 3452 const MachineInstr *MI, unsigned Reg, 3453 unsigned &UseIdx, unsigned &Dist) { 3454 Dist = 0; 3455 3456 MachineBasicBlock::const_instr_iterator II = ++MI->getIterator(); 3457 assert(II->isInsideBundle() && "Empty bundle?"); 3458 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end(); 3459 3460 // FIXME: This doesn't properly handle multiple uses. 3461 int Idx = -1; 3462 while (II != E && II->isInsideBundle()) { 3463 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI); 3464 if (Idx != -1) 3465 break; 3466 if (II->getOpcode() != ARM::t2IT) 3467 ++Dist; 3468 ++II; 3469 } 3470 3471 if (Idx == -1) { 3472 Dist = 0; 3473 return nullptr; 3474 } 3475 3476 UseIdx = Idx; 3477 return &*II; 3478 } 3479 3480 /// Return the number of cycles to add to (or subtract from) the static 3481 /// itinerary based on the def opcode and alignment. The caller will ensure that 3482 /// adjusted latency is at least one cycle. 3483 static int adjustDefLatency(const ARMSubtarget &Subtarget, 3484 const MachineInstr *DefMI, 3485 const MCInstrDesc *DefMCID, unsigned DefAlign) { 3486 int Adjust = 0; 3487 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) { 3488 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2] 3489 // variants are one cycle cheaper. 3490 switch (DefMCID->getOpcode()) { 3491 default: break; 3492 case ARM::LDRrs: 3493 case ARM::LDRBrs: { 3494 unsigned ShOpVal = DefMI->getOperand(3).getImm(); 3495 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 3496 if (ShImm == 0 || 3497 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)) 3498 --Adjust; 3499 break; 3500 } 3501 case ARM::t2LDRs: 3502 case ARM::t2LDRBs: 3503 case ARM::t2LDRHs: 3504 case ARM::t2LDRSHs: { 3505 // Thumb2 mode: lsl only. 3506 unsigned ShAmt = DefMI->getOperand(3).getImm(); 3507 if (ShAmt == 0 || ShAmt == 2) 3508 --Adjust; 3509 break; 3510 } 3511 } 3512 } else if (Subtarget.isSwift()) { 3513 // FIXME: Properly handle all of the latency adjustments for address 3514 // writeback. 3515 switch (DefMCID->getOpcode()) { 3516 default: break; 3517 case ARM::LDRrs: 3518 case ARM::LDRBrs: { 3519 unsigned ShOpVal = DefMI->getOperand(3).getImm(); 3520 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub; 3521 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 3522 if (!isSub && 3523 (ShImm == 0 || 3524 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 3525 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))) 3526 Adjust -= 2; 3527 else if (!isSub && 3528 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr) 3529 --Adjust; 3530 break; 3531 } 3532 case ARM::t2LDRs: 3533 case ARM::t2LDRBs: 3534 case ARM::t2LDRHs: 3535 case ARM::t2LDRSHs: { 3536 // Thumb2 mode: lsl only. 3537 unsigned ShAmt = DefMI->getOperand(3).getImm(); 3538 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3) 3539 Adjust -= 2; 3540 break; 3541 } 3542 } 3543 } 3544 3545 if (DefAlign < 8 && Subtarget.isLikeA9()) { 3546 switch (DefMCID->getOpcode()) { 3547 default: break; 3548 case ARM::VLD1q8: 3549 case ARM::VLD1q16: 3550 case ARM::VLD1q32: 3551 case ARM::VLD1q64: 3552 case ARM::VLD1q8wb_fixed: 3553 case ARM::VLD1q16wb_fixed: 3554 case ARM::VLD1q32wb_fixed: 3555 case ARM::VLD1q64wb_fixed: 3556 case ARM::VLD1q8wb_register: 3557 case ARM::VLD1q16wb_register: 3558 case ARM::VLD1q32wb_register: 3559 case ARM::VLD1q64wb_register: 3560 case ARM::VLD2d8: 3561 case ARM::VLD2d16: 3562 case ARM::VLD2d32: 3563 case ARM::VLD2q8: 3564 case ARM::VLD2q16: 3565 case ARM::VLD2q32: 3566 case ARM::VLD2d8wb_fixed: 3567 case ARM::VLD2d16wb_fixed: 3568 case ARM::VLD2d32wb_fixed: 3569 case ARM::VLD2q8wb_fixed: 3570 case ARM::VLD2q16wb_fixed: 3571 case ARM::VLD2q32wb_fixed: 3572 case ARM::VLD2d8wb_register: 3573 case ARM::VLD2d16wb_register: 3574 case ARM::VLD2d32wb_register: 3575 case ARM::VLD2q8wb_register: 3576 case ARM::VLD2q16wb_register: 3577 case ARM::VLD2q32wb_register: 3578 case ARM::VLD3d8: 3579 case ARM::VLD3d16: 3580 case ARM::VLD3d32: 3581 case ARM::VLD1d64T: 3582 case ARM::VLD3d8_UPD: 3583 case ARM::VLD3d16_UPD: 3584 case ARM::VLD3d32_UPD: 3585 case ARM::VLD1d64Twb_fixed: 3586 case ARM::VLD1d64Twb_register: 3587 case ARM::VLD3q8_UPD: 3588 case ARM::VLD3q16_UPD: 3589 case ARM::VLD3q32_UPD: 3590 case ARM::VLD4d8: 3591 case ARM::VLD4d16: 3592 case ARM::VLD4d32: 3593 case ARM::VLD1d64Q: 3594 case ARM::VLD4d8_UPD: 3595 case ARM::VLD4d16_UPD: 3596 case ARM::VLD4d32_UPD: 3597 case ARM::VLD1d64Qwb_fixed: 3598 case ARM::VLD1d64Qwb_register: 3599 case ARM::VLD4q8_UPD: 3600 case ARM::VLD4q16_UPD: 3601 case ARM::VLD4q32_UPD: 3602 case ARM::VLD1DUPq8: 3603 case ARM::VLD1DUPq16: 3604 case ARM::VLD1DUPq32: 3605 case ARM::VLD1DUPq8wb_fixed: 3606 case ARM::VLD1DUPq16wb_fixed: 3607 case ARM::VLD1DUPq32wb_fixed: 3608 case ARM::VLD1DUPq8wb_register: 3609 case ARM::VLD1DUPq16wb_register: 3610 case ARM::VLD1DUPq32wb_register: 3611 case ARM::VLD2DUPd8: 3612 case ARM::VLD2DUPd16: 3613 case ARM::VLD2DUPd32: 3614 case ARM::VLD2DUPd8wb_fixed: 3615 case ARM::VLD2DUPd16wb_fixed: 3616 case ARM::VLD2DUPd32wb_fixed: 3617 case ARM::VLD2DUPd8wb_register: 3618 case ARM::VLD2DUPd16wb_register: 3619 case ARM::VLD2DUPd32wb_register: 3620 case ARM::VLD4DUPd8: 3621 case ARM::VLD4DUPd16: 3622 case ARM::VLD4DUPd32: 3623 case ARM::VLD4DUPd8_UPD: 3624 case ARM::VLD4DUPd16_UPD: 3625 case ARM::VLD4DUPd32_UPD: 3626 case ARM::VLD1LNd8: 3627 case ARM::VLD1LNd16: 3628 case ARM::VLD1LNd32: 3629 case ARM::VLD1LNd8_UPD: 3630 case ARM::VLD1LNd16_UPD: 3631 case ARM::VLD1LNd32_UPD: 3632 case ARM::VLD2LNd8: 3633 case ARM::VLD2LNd16: 3634 case ARM::VLD2LNd32: 3635 case ARM::VLD2LNq16: 3636 case ARM::VLD2LNq32: 3637 case ARM::VLD2LNd8_UPD: 3638 case ARM::VLD2LNd16_UPD: 3639 case ARM::VLD2LNd32_UPD: 3640 case ARM::VLD2LNq16_UPD: 3641 case ARM::VLD2LNq32_UPD: 3642 case ARM::VLD4LNd8: 3643 case ARM::VLD4LNd16: 3644 case ARM::VLD4LNd32: 3645 case ARM::VLD4LNq16: 3646 case ARM::VLD4LNq32: 3647 case ARM::VLD4LNd8_UPD: 3648 case ARM::VLD4LNd16_UPD: 3649 case ARM::VLD4LNd32_UPD: 3650 case ARM::VLD4LNq16_UPD: 3651 case ARM::VLD4LNq32_UPD: 3652 // If the address is not 64-bit aligned, the latencies of these 3653 // instructions increases by one. 3654 ++Adjust; 3655 break; 3656 } 3657 } 3658 return Adjust; 3659 } 3660 3661 3662 3663 int 3664 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, 3665 const MachineInstr *DefMI, unsigned DefIdx, 3666 const MachineInstr *UseMI, 3667 unsigned UseIdx) const { 3668 // No operand latency. The caller may fall back to getInstrLatency. 3669 if (!ItinData || ItinData->isEmpty()) 3670 return -1; 3671 3672 const MachineOperand &DefMO = DefMI->getOperand(DefIdx); 3673 unsigned Reg = DefMO.getReg(); 3674 const MCInstrDesc *DefMCID = &DefMI->getDesc(); 3675 const MCInstrDesc *UseMCID = &UseMI->getDesc(); 3676 3677 unsigned DefAdj = 0; 3678 if (DefMI->isBundle()) { 3679 DefMI = getBundledDefMI(&getRegisterInfo(), DefMI, Reg, DefIdx, DefAdj); 3680 DefMCID = &DefMI->getDesc(); 3681 } 3682 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() || 3683 DefMI->isRegSequence() || DefMI->isImplicitDef()) { 3684 return 1; 3685 } 3686 3687 unsigned UseAdj = 0; 3688 if (UseMI->isBundle()) { 3689 unsigned NewUseIdx; 3690 const MachineInstr *NewUseMI = getBundledUseMI(&getRegisterInfo(), UseMI, 3691 Reg, NewUseIdx, UseAdj); 3692 if (!NewUseMI) 3693 return -1; 3694 3695 UseMI = NewUseMI; 3696 UseIdx = NewUseIdx; 3697 UseMCID = &UseMI->getDesc(); 3698 } 3699 3700 if (Reg == ARM::CPSR) { 3701 if (DefMI->getOpcode() == ARM::FMSTAT) { 3702 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?) 3703 return Subtarget.isLikeA9() ? 1 : 20; 3704 } 3705 3706 // CPSR set and branch can be paired in the same cycle. 3707 if (UseMI->isBranch()) 3708 return 0; 3709 3710 // Otherwise it takes the instruction latency (generally one). 3711 unsigned Latency = getInstrLatency(ItinData, DefMI); 3712 3713 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to 3714 // its uses. Instructions which are otherwise scheduled between them may 3715 // incur a code size penalty (not able to use the CPSR setting 16-bit 3716 // instructions). 3717 if (Latency > 0 && Subtarget.isThumb2()) { 3718 const MachineFunction *MF = DefMI->getParent()->getParent(); 3719 // FIXME: Use Function::optForSize(). 3720 if (MF->getFunction()->hasFnAttribute(Attribute::OptimizeForSize)) 3721 --Latency; 3722 } 3723 return Latency; 3724 } 3725 3726 if (DefMO.isImplicit() || UseMI->getOperand(UseIdx).isImplicit()) 3727 return -1; 3728 3729 unsigned DefAlign = DefMI->hasOneMemOperand() 3730 ? (*DefMI->memoperands_begin())->getAlignment() : 0; 3731 unsigned UseAlign = UseMI->hasOneMemOperand() 3732 ? (*UseMI->memoperands_begin())->getAlignment() : 0; 3733 3734 // Get the itinerary's latency if possible, and handle variable_ops. 3735 int Latency = getOperandLatency(ItinData, *DefMCID, DefIdx, DefAlign, 3736 *UseMCID, UseIdx, UseAlign); 3737 // Unable to find operand latency. The caller may resort to getInstrLatency. 3738 if (Latency < 0) 3739 return Latency; 3740 3741 // Adjust for IT block position. 3742 int Adj = DefAdj + UseAdj; 3743 3744 // Adjust for dynamic def-side opcode variants not captured by the itinerary. 3745 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign); 3746 if (Adj >= 0 || (int)Latency > -Adj) { 3747 return Latency + Adj; 3748 } 3749 // Return the itinerary latency, which may be zero but not less than zero. 3750 return Latency; 3751 } 3752 3753 int 3754 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, 3755 SDNode *DefNode, unsigned DefIdx, 3756 SDNode *UseNode, unsigned UseIdx) const { 3757 if (!DefNode->isMachineOpcode()) 3758 return 1; 3759 3760 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode()); 3761 3762 if (isZeroCost(DefMCID.Opcode)) 3763 return 0; 3764 3765 if (!ItinData || ItinData->isEmpty()) 3766 return DefMCID.mayLoad() ? 3 : 1; 3767 3768 if (!UseNode->isMachineOpcode()) { 3769 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx); 3770 if (Subtarget.isLikeA9() || Subtarget.isSwift()) 3771 return Latency <= 2 ? 1 : Latency - 1; 3772 else 3773 return Latency <= 3 ? 1 : Latency - 2; 3774 } 3775 3776 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode()); 3777 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode); 3778 unsigned DefAlign = !DefMN->memoperands_empty() 3779 ? (*DefMN->memoperands_begin())->getAlignment() : 0; 3780 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode); 3781 unsigned UseAlign = !UseMN->memoperands_empty() 3782 ? (*UseMN->memoperands_begin())->getAlignment() : 0; 3783 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign, 3784 UseMCID, UseIdx, UseAlign); 3785 3786 if (Latency > 1 && 3787 (Subtarget.isCortexA8() || Subtarget.isLikeA9() || 3788 Subtarget.isCortexA7())) { 3789 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2] 3790 // variants are one cycle cheaper. 3791 switch (DefMCID.getOpcode()) { 3792 default: break; 3793 case ARM::LDRrs: 3794 case ARM::LDRBrs: { 3795 unsigned ShOpVal = 3796 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue(); 3797 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 3798 if (ShImm == 0 || 3799 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)) 3800 --Latency; 3801 break; 3802 } 3803 case ARM::t2LDRs: 3804 case ARM::t2LDRBs: 3805 case ARM::t2LDRHs: 3806 case ARM::t2LDRSHs: { 3807 // Thumb2 mode: lsl only. 3808 unsigned ShAmt = 3809 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue(); 3810 if (ShAmt == 0 || ShAmt == 2) 3811 --Latency; 3812 break; 3813 } 3814 } 3815 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) { 3816 // FIXME: Properly handle all of the latency adjustments for address 3817 // writeback. 3818 switch (DefMCID.getOpcode()) { 3819 default: break; 3820 case ARM::LDRrs: 3821 case ARM::LDRBrs: { 3822 unsigned ShOpVal = 3823 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue(); 3824 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 3825 if (ShImm == 0 || 3826 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 3827 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)) 3828 Latency -= 2; 3829 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr) 3830 --Latency; 3831 break; 3832 } 3833 case ARM::t2LDRs: 3834 case ARM::t2LDRBs: 3835 case ARM::t2LDRHs: 3836 case ARM::t2LDRSHs: { 3837 // Thumb2 mode: lsl 0-3 only. 3838 Latency -= 2; 3839 break; 3840 } 3841 } 3842 } 3843 3844 if (DefAlign < 8 && Subtarget.isLikeA9()) 3845 switch (DefMCID.getOpcode()) { 3846 default: break; 3847 case ARM::VLD1q8: 3848 case ARM::VLD1q16: 3849 case ARM::VLD1q32: 3850 case ARM::VLD1q64: 3851 case ARM::VLD1q8wb_register: 3852 case ARM::VLD1q16wb_register: 3853 case ARM::VLD1q32wb_register: 3854 case ARM::VLD1q64wb_register: 3855 case ARM::VLD1q8wb_fixed: 3856 case ARM::VLD1q16wb_fixed: 3857 case ARM::VLD1q32wb_fixed: 3858 case ARM::VLD1q64wb_fixed: 3859 case ARM::VLD2d8: 3860 case ARM::VLD2d16: 3861 case ARM::VLD2d32: 3862 case ARM::VLD2q8Pseudo: 3863 case ARM::VLD2q16Pseudo: 3864 case ARM::VLD2q32Pseudo: 3865 case ARM::VLD2d8wb_fixed: 3866 case ARM::VLD2d16wb_fixed: 3867 case ARM::VLD2d32wb_fixed: 3868 case ARM::VLD2q8PseudoWB_fixed: 3869 case ARM::VLD2q16PseudoWB_fixed: 3870 case ARM::VLD2q32PseudoWB_fixed: 3871 case ARM::VLD2d8wb_register: 3872 case ARM::VLD2d16wb_register: 3873 case ARM::VLD2d32wb_register: 3874 case ARM::VLD2q8PseudoWB_register: 3875 case ARM::VLD2q16PseudoWB_register: 3876 case ARM::VLD2q32PseudoWB_register: 3877 case ARM::VLD3d8Pseudo: 3878 case ARM::VLD3d16Pseudo: 3879 case ARM::VLD3d32Pseudo: 3880 case ARM::VLD1d64TPseudo: 3881 case ARM::VLD1d64TPseudoWB_fixed: 3882 case ARM::VLD3d8Pseudo_UPD: 3883 case ARM::VLD3d16Pseudo_UPD: 3884 case ARM::VLD3d32Pseudo_UPD: 3885 case ARM::VLD3q8Pseudo_UPD: 3886 case ARM::VLD3q16Pseudo_UPD: 3887 case ARM::VLD3q32Pseudo_UPD: 3888 case ARM::VLD3q8oddPseudo: 3889 case ARM::VLD3q16oddPseudo: 3890 case ARM::VLD3q32oddPseudo: 3891 case ARM::VLD3q8oddPseudo_UPD: 3892 case ARM::VLD3q16oddPseudo_UPD: 3893 case ARM::VLD3q32oddPseudo_UPD: 3894 case ARM::VLD4d8Pseudo: 3895 case ARM::VLD4d16Pseudo: 3896 case ARM::VLD4d32Pseudo: 3897 case ARM::VLD1d64QPseudo: 3898 case ARM::VLD1d64QPseudoWB_fixed: 3899 case ARM::VLD4d8Pseudo_UPD: 3900 case ARM::VLD4d16Pseudo_UPD: 3901 case ARM::VLD4d32Pseudo_UPD: 3902 case ARM::VLD4q8Pseudo_UPD: 3903 case ARM::VLD4q16Pseudo_UPD: 3904 case ARM::VLD4q32Pseudo_UPD: 3905 case ARM::VLD4q8oddPseudo: 3906 case ARM::VLD4q16oddPseudo: 3907 case ARM::VLD4q32oddPseudo: 3908 case ARM::VLD4q8oddPseudo_UPD: 3909 case ARM::VLD4q16oddPseudo_UPD: 3910 case ARM::VLD4q32oddPseudo_UPD: 3911 case ARM::VLD1DUPq8: 3912 case ARM::VLD1DUPq16: 3913 case ARM::VLD1DUPq32: 3914 case ARM::VLD1DUPq8wb_fixed: 3915 case ARM::VLD1DUPq16wb_fixed: 3916 case ARM::VLD1DUPq32wb_fixed: 3917 case ARM::VLD1DUPq8wb_register: 3918 case ARM::VLD1DUPq16wb_register: 3919 case ARM::VLD1DUPq32wb_register: 3920 case ARM::VLD2DUPd8: 3921 case ARM::VLD2DUPd16: 3922 case ARM::VLD2DUPd32: 3923 case ARM::VLD2DUPd8wb_fixed: 3924 case ARM::VLD2DUPd16wb_fixed: 3925 case ARM::VLD2DUPd32wb_fixed: 3926 case ARM::VLD2DUPd8wb_register: 3927 case ARM::VLD2DUPd16wb_register: 3928 case ARM::VLD2DUPd32wb_register: 3929 case ARM::VLD4DUPd8Pseudo: 3930 case ARM::VLD4DUPd16Pseudo: 3931 case ARM::VLD4DUPd32Pseudo: 3932 case ARM::VLD4DUPd8Pseudo_UPD: 3933 case ARM::VLD4DUPd16Pseudo_UPD: 3934 case ARM::VLD4DUPd32Pseudo_UPD: 3935 case ARM::VLD1LNq8Pseudo: 3936 case ARM::VLD1LNq16Pseudo: 3937 case ARM::VLD1LNq32Pseudo: 3938 case ARM::VLD1LNq8Pseudo_UPD: 3939 case ARM::VLD1LNq16Pseudo_UPD: 3940 case ARM::VLD1LNq32Pseudo_UPD: 3941 case ARM::VLD2LNd8Pseudo: 3942 case ARM::VLD2LNd16Pseudo: 3943 case ARM::VLD2LNd32Pseudo: 3944 case ARM::VLD2LNq16Pseudo: 3945 case ARM::VLD2LNq32Pseudo: 3946 case ARM::VLD2LNd8Pseudo_UPD: 3947 case ARM::VLD2LNd16Pseudo_UPD: 3948 case ARM::VLD2LNd32Pseudo_UPD: 3949 case ARM::VLD2LNq16Pseudo_UPD: 3950 case ARM::VLD2LNq32Pseudo_UPD: 3951 case ARM::VLD4LNd8Pseudo: 3952 case ARM::VLD4LNd16Pseudo: 3953 case ARM::VLD4LNd32Pseudo: 3954 case ARM::VLD4LNq16Pseudo: 3955 case ARM::VLD4LNq32Pseudo: 3956 case ARM::VLD4LNd8Pseudo_UPD: 3957 case ARM::VLD4LNd16Pseudo_UPD: 3958 case ARM::VLD4LNd32Pseudo_UPD: 3959 case ARM::VLD4LNq16Pseudo_UPD: 3960 case ARM::VLD4LNq32Pseudo_UPD: 3961 // If the address is not 64-bit aligned, the latencies of these 3962 // instructions increases by one. 3963 ++Latency; 3964 break; 3965 } 3966 3967 return Latency; 3968 } 3969 3970 unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr &MI) const { 3971 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() || 3972 MI.isImplicitDef()) 3973 return 0; 3974 3975 if (MI.isBundle()) 3976 return 0; 3977 3978 const MCInstrDesc &MCID = MI.getDesc(); 3979 3980 if (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR)) { 3981 // When predicated, CPSR is an additional source operand for CPSR updating 3982 // instructions, this apparently increases their latencies. 3983 return 1; 3984 } 3985 return 0; 3986 } 3987 3988 unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, 3989 const MachineInstr *MI, 3990 unsigned *PredCost) const { 3991 if (MI->isCopyLike() || MI->isInsertSubreg() || 3992 MI->isRegSequence() || MI->isImplicitDef()) 3993 return 1; 3994 3995 // An instruction scheduler typically runs on unbundled instructions, however 3996 // other passes may query the latency of a bundled instruction. 3997 if (MI->isBundle()) { 3998 unsigned Latency = 0; 3999 MachineBasicBlock::const_instr_iterator I = MI->getIterator(); 4000 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end(); 4001 while (++I != E && I->isInsideBundle()) { 4002 if (I->getOpcode() != ARM::t2IT) 4003 Latency += getInstrLatency(ItinData, &*I, PredCost); 4004 } 4005 return Latency; 4006 } 4007 4008 const MCInstrDesc &MCID = MI->getDesc(); 4009 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) { 4010 // When predicated, CPSR is an additional source operand for CPSR updating 4011 // instructions, this apparently increases their latencies. 4012 *PredCost = 1; 4013 } 4014 // Be sure to call getStageLatency for an empty itinerary in case it has a 4015 // valid MinLatency property. 4016 if (!ItinData) 4017 return MI->mayLoad() ? 3 : 1; 4018 4019 unsigned Class = MCID.getSchedClass(); 4020 4021 // For instructions with variable uops, use uops as latency. 4022 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0) 4023 return getNumMicroOps(ItinData, MI); 4024 4025 // For the common case, fall back on the itinerary's latency. 4026 unsigned Latency = ItinData->getStageLatency(Class); 4027 4028 // Adjust for dynamic def-side opcode variants not captured by the itinerary. 4029 unsigned DefAlign = MI->hasOneMemOperand() 4030 ? (*MI->memoperands_begin())->getAlignment() : 0; 4031 int Adj = adjustDefLatency(Subtarget, MI, &MCID, DefAlign); 4032 if (Adj >= 0 || (int)Latency > -Adj) { 4033 return Latency + Adj; 4034 } 4035 return Latency; 4036 } 4037 4038 int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, 4039 SDNode *Node) const { 4040 if (!Node->isMachineOpcode()) 4041 return 1; 4042 4043 if (!ItinData || ItinData->isEmpty()) 4044 return 1; 4045 4046 unsigned Opcode = Node->getMachineOpcode(); 4047 switch (Opcode) { 4048 default: 4049 return ItinData->getStageLatency(get(Opcode).getSchedClass()); 4050 case ARM::VLDMQIA: 4051 case ARM::VSTMQIA: 4052 return 2; 4053 } 4054 } 4055 4056 bool ARMBaseInstrInfo:: 4057 hasHighOperandLatency(const TargetSchedModel &SchedModel, 4058 const MachineRegisterInfo *MRI, 4059 const MachineInstr *DefMI, unsigned DefIdx, 4060 const MachineInstr *UseMI, unsigned UseIdx) const { 4061 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask; 4062 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask; 4063 if (Subtarget.isCortexA8() && 4064 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP)) 4065 // CortexA8 VFP instructions are not pipelined. 4066 return true; 4067 4068 // Hoist VFP / NEON instructions with 4 or higher latency. 4069 unsigned Latency 4070 = SchedModel.computeOperandLatency(DefMI, DefIdx, UseMI, UseIdx); 4071 if (Latency <= 3) 4072 return false; 4073 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON || 4074 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON; 4075 } 4076 4077 bool ARMBaseInstrInfo:: 4078 hasLowDefLatency(const TargetSchedModel &SchedModel, 4079 const MachineInstr *DefMI, unsigned DefIdx) const { 4080 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries(); 4081 if (!ItinData || ItinData->isEmpty()) 4082 return false; 4083 4084 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask; 4085 if (DDomain == ARMII::DomainGeneral) { 4086 unsigned DefClass = DefMI->getDesc().getSchedClass(); 4087 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx); 4088 return (DefCycle != -1 && DefCycle <= 2); 4089 } 4090 return false; 4091 } 4092 4093 bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr *MI, 4094 StringRef &ErrInfo) const { 4095 if (convertAddSubFlagsOpcode(MI->getOpcode())) { 4096 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG"; 4097 return false; 4098 } 4099 return true; 4100 } 4101 4102 // LoadStackGuard has so far only been implemented for MachO. Different code 4103 // sequence is needed for other targets. 4104 void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI, 4105 unsigned LoadImmOpc, 4106 unsigned LoadOpc, 4107 Reloc::Model RM) const { 4108 MachineBasicBlock &MBB = *MI->getParent(); 4109 DebugLoc DL = MI->getDebugLoc(); 4110 unsigned Reg = MI->getOperand(0).getReg(); 4111 const GlobalValue *GV = 4112 cast<GlobalValue>((*MI->memoperands_begin())->getValue()); 4113 MachineInstrBuilder MIB; 4114 4115 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg) 4116 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY); 4117 4118 if (Subtarget.GVIsIndirectSymbol(GV, RM)) { 4119 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg); 4120 MIB.addReg(Reg, RegState::Kill).addImm(0); 4121 unsigned Flag = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant; 4122 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand( 4123 MachinePointerInfo::getGOT(*MBB.getParent()), Flag, 4, 4); 4124 MIB.addMemOperand(MMO); 4125 AddDefaultPred(MIB); 4126 } 4127 4128 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg); 4129 MIB.addReg(Reg, RegState::Kill).addImm(0); 4130 MIB.setMemRefs(MI->memoperands_begin(), MI->memoperands_end()); 4131 AddDefaultPred(MIB); 4132 } 4133 4134 bool 4135 ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc, 4136 unsigned &AddSubOpc, 4137 bool &NegAcc, bool &HasLane) const { 4138 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode); 4139 if (I == MLxEntryMap.end()) 4140 return false; 4141 4142 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second]; 4143 MulOpc = Entry.MulOpc; 4144 AddSubOpc = Entry.AddSubOpc; 4145 NegAcc = Entry.NegAcc; 4146 HasLane = Entry.HasLane; 4147 return true; 4148 } 4149 4150 //===----------------------------------------------------------------------===// 4151 // Execution domains. 4152 //===----------------------------------------------------------------------===// 4153 // 4154 // Some instructions go down the NEON pipeline, some go down the VFP pipeline, 4155 // and some can go down both. The vmov instructions go down the VFP pipeline, 4156 // but they can be changed to vorr equivalents that are executed by the NEON 4157 // pipeline. 4158 // 4159 // We use the following execution domain numbering: 4160 // 4161 enum ARMExeDomain { 4162 ExeGeneric = 0, 4163 ExeVFP = 1, 4164 ExeNEON = 2 4165 }; 4166 // 4167 // Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h 4168 // 4169 std::pair<uint16_t, uint16_t> 4170 ARMBaseInstrInfo::getExecutionDomain(const MachineInstr *MI) const { 4171 // If we don't have access to NEON instructions then we won't be able 4172 // to swizzle anything to the NEON domain. Check to make sure. 4173 if (Subtarget.hasNEON()) { 4174 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON 4175 // if they are not predicated. 4176 if (MI->getOpcode() == ARM::VMOVD && !isPredicated(*MI)) 4177 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON)); 4178 4179 // CortexA9 is particularly picky about mixing the two and wants these 4180 // converted. 4181 if (Subtarget.isCortexA9() && !isPredicated(*MI) && 4182 (MI->getOpcode() == ARM::VMOVRS || MI->getOpcode() == ARM::VMOVSR || 4183 MI->getOpcode() == ARM::VMOVS)) 4184 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON)); 4185 } 4186 // No other instructions can be swizzled, so just determine their domain. 4187 unsigned Domain = MI->getDesc().TSFlags & ARMII::DomainMask; 4188 4189 if (Domain & ARMII::DomainNEON) 4190 return std::make_pair(ExeNEON, 0); 4191 4192 // Certain instructions can go either way on Cortex-A8. 4193 // Treat them as NEON instructions. 4194 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8()) 4195 return std::make_pair(ExeNEON, 0); 4196 4197 if (Domain & ARMII::DomainVFP) 4198 return std::make_pair(ExeVFP, 0); 4199 4200 return std::make_pair(ExeGeneric, 0); 4201 } 4202 4203 static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI, 4204 unsigned SReg, unsigned &Lane) { 4205 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass); 4206 Lane = 0; 4207 4208 if (DReg != ARM::NoRegister) 4209 return DReg; 4210 4211 Lane = 1; 4212 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass); 4213 4214 assert(DReg && "S-register with no D super-register?"); 4215 return DReg; 4216 } 4217 4218 /// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane, 4219 /// set ImplicitSReg to a register number that must be marked as implicit-use or 4220 /// zero if no register needs to be defined as implicit-use. 4221 /// 4222 /// If the function cannot determine if an SPR should be marked implicit use or 4223 /// not, it returns false. 4224 /// 4225 /// This function handles cases where an instruction is being modified from taking 4226 /// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict 4227 /// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other 4228 /// lane of the DPR). 4229 /// 4230 /// If the other SPR is defined, an implicit-use of it should be added. Else, 4231 /// (including the case where the DPR itself is defined), it should not. 4232 /// 4233 static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI, 4234 MachineInstr *MI, 4235 unsigned DReg, unsigned Lane, 4236 unsigned &ImplicitSReg) { 4237 // If the DPR is defined or used already, the other SPR lane will be chained 4238 // correctly, so there is nothing to be done. 4239 if (MI->definesRegister(DReg, TRI) || MI->readsRegister(DReg, TRI)) { 4240 ImplicitSReg = 0; 4241 return true; 4242 } 4243 4244 // Otherwise we need to go searching to see if the SPR is set explicitly. 4245 ImplicitSReg = TRI->getSubReg(DReg, 4246 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1); 4247 MachineBasicBlock::LivenessQueryResult LQR = 4248 MI->getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI); 4249 4250 if (LQR == MachineBasicBlock::LQR_Live) 4251 return true; 4252 else if (LQR == MachineBasicBlock::LQR_Unknown) 4253 return false; 4254 4255 // If the register is known not to be live, there is no need to add an 4256 // implicit-use. 4257 ImplicitSReg = 0; 4258 return true; 4259 } 4260 4261 void 4262 ARMBaseInstrInfo::setExecutionDomain(MachineInstr *MI, unsigned Domain) const { 4263 unsigned DstReg, SrcReg, DReg; 4264 unsigned Lane; 4265 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI); 4266 const TargetRegisterInfo *TRI = &getRegisterInfo(); 4267 switch (MI->getOpcode()) { 4268 default: 4269 llvm_unreachable("cannot handle opcode!"); 4270 break; 4271 case ARM::VMOVD: 4272 if (Domain != ExeNEON) 4273 break; 4274 4275 // Zap the predicate operands. 4276 assert(!isPredicated(*MI) && "Cannot predicate a VORRd"); 4277 4278 // Make sure we've got NEON instructions. 4279 assert(Subtarget.hasNEON() && "VORRd requires NEON"); 4280 4281 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits) 4282 DstReg = MI->getOperand(0).getReg(); 4283 SrcReg = MI->getOperand(1).getReg(); 4284 4285 for (unsigned i = MI->getDesc().getNumOperands(); i; --i) 4286 MI->RemoveOperand(i-1); 4287 4288 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits) 4289 MI->setDesc(get(ARM::VORRd)); 4290 AddDefaultPred(MIB.addReg(DstReg, RegState::Define) 4291 .addReg(SrcReg) 4292 .addReg(SrcReg)); 4293 break; 4294 case ARM::VMOVRS: 4295 if (Domain != ExeNEON) 4296 break; 4297 assert(!isPredicated(*MI) && "Cannot predicate a VGETLN"); 4298 4299 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits) 4300 DstReg = MI->getOperand(0).getReg(); 4301 SrcReg = MI->getOperand(1).getReg(); 4302 4303 for (unsigned i = MI->getDesc().getNumOperands(); i; --i) 4304 MI->RemoveOperand(i-1); 4305 4306 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane); 4307 4308 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps) 4309 // Note that DSrc has been widened and the other lane may be undef, which 4310 // contaminates the entire register. 4311 MI->setDesc(get(ARM::VGETLNi32)); 4312 AddDefaultPred(MIB.addReg(DstReg, RegState::Define) 4313 .addReg(DReg, RegState::Undef) 4314 .addImm(Lane)); 4315 4316 // The old source should be an implicit use, otherwise we might think it 4317 // was dead before here. 4318 MIB.addReg(SrcReg, RegState::Implicit); 4319 break; 4320 case ARM::VMOVSR: { 4321 if (Domain != ExeNEON) 4322 break; 4323 assert(!isPredicated(*MI) && "Cannot predicate a VSETLN"); 4324 4325 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits) 4326 DstReg = MI->getOperand(0).getReg(); 4327 SrcReg = MI->getOperand(1).getReg(); 4328 4329 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane); 4330 4331 unsigned ImplicitSReg; 4332 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg)) 4333 break; 4334 4335 for (unsigned i = MI->getDesc().getNumOperands(); i; --i) 4336 MI->RemoveOperand(i-1); 4337 4338 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps) 4339 // Again DDst may be undefined at the beginning of this instruction. 4340 MI->setDesc(get(ARM::VSETLNi32)); 4341 MIB.addReg(DReg, RegState::Define) 4342 .addReg(DReg, getUndefRegState(!MI->readsRegister(DReg, TRI))) 4343 .addReg(SrcReg) 4344 .addImm(Lane); 4345 AddDefaultPred(MIB); 4346 4347 // The narrower destination must be marked as set to keep previous chains 4348 // in place. 4349 MIB.addReg(DstReg, RegState::Define | RegState::Implicit); 4350 if (ImplicitSReg != 0) 4351 MIB.addReg(ImplicitSReg, RegState::Implicit); 4352 break; 4353 } 4354 case ARM::VMOVS: { 4355 if (Domain != ExeNEON) 4356 break; 4357 4358 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits) 4359 DstReg = MI->getOperand(0).getReg(); 4360 SrcReg = MI->getOperand(1).getReg(); 4361 4362 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc; 4363 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane); 4364 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane); 4365 4366 unsigned ImplicitSReg; 4367 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg)) 4368 break; 4369 4370 for (unsigned i = MI->getDesc().getNumOperands(); i; --i) 4371 MI->RemoveOperand(i-1); 4372 4373 if (DSrc == DDst) { 4374 // Destination can be: 4375 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits) 4376 MI->setDesc(get(ARM::VDUPLN32d)); 4377 MIB.addReg(DDst, RegState::Define) 4378 .addReg(DDst, getUndefRegState(!MI->readsRegister(DDst, TRI))) 4379 .addImm(SrcLane); 4380 AddDefaultPred(MIB); 4381 4382 // Neither the source or the destination are naturally represented any 4383 // more, so add them in manually. 4384 MIB.addReg(DstReg, RegState::Implicit | RegState::Define); 4385 MIB.addReg(SrcReg, RegState::Implicit); 4386 if (ImplicitSReg != 0) 4387 MIB.addReg(ImplicitSReg, RegState::Implicit); 4388 break; 4389 } 4390 4391 // In general there's no single instruction that can perform an S <-> S 4392 // move in NEON space, but a pair of VEXT instructions *can* do the 4393 // job. It turns out that the VEXTs needed will only use DSrc once, with 4394 // the position based purely on the combination of lane-0 and lane-1 4395 // involved. For example 4396 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1 4397 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1 4398 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1 4399 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1 4400 // 4401 // Pattern of the MachineInstrs is: 4402 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits) 4403 MachineInstrBuilder NewMIB; 4404 NewMIB = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 4405 get(ARM::VEXTd32), DDst); 4406 4407 // On the first instruction, both DSrc and DDst may be <undef> if present. 4408 // Specifically when the original instruction didn't have them as an 4409 // <imp-use>. 4410 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst; 4411 bool CurUndef = !MI->readsRegister(CurReg, TRI); 4412 NewMIB.addReg(CurReg, getUndefRegState(CurUndef)); 4413 4414 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst; 4415 CurUndef = !MI->readsRegister(CurReg, TRI); 4416 NewMIB.addReg(CurReg, getUndefRegState(CurUndef)); 4417 4418 NewMIB.addImm(1); 4419 AddDefaultPred(NewMIB); 4420 4421 if (SrcLane == DstLane) 4422 NewMIB.addReg(SrcReg, RegState::Implicit); 4423 4424 MI->setDesc(get(ARM::VEXTd32)); 4425 MIB.addReg(DDst, RegState::Define); 4426 4427 // On the second instruction, DDst has definitely been defined above, so 4428 // it is not <undef>. DSrc, if present, can be <undef> as above. 4429 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst; 4430 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI); 4431 MIB.addReg(CurReg, getUndefRegState(CurUndef)); 4432 4433 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst; 4434 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI); 4435 MIB.addReg(CurReg, getUndefRegState(CurUndef)); 4436 4437 MIB.addImm(1); 4438 AddDefaultPred(MIB); 4439 4440 if (SrcLane != DstLane) 4441 MIB.addReg(SrcReg, RegState::Implicit); 4442 4443 // As before, the original destination is no longer represented, add it 4444 // implicitly. 4445 MIB.addReg(DstReg, RegState::Define | RegState::Implicit); 4446 if (ImplicitSReg != 0) 4447 MIB.addReg(ImplicitSReg, RegState::Implicit); 4448 break; 4449 } 4450 } 4451 4452 } 4453 4454 //===----------------------------------------------------------------------===// 4455 // Partial register updates 4456 //===----------------------------------------------------------------------===// 4457 // 4458 // Swift renames NEON registers with 64-bit granularity. That means any 4459 // instruction writing an S-reg implicitly reads the containing D-reg. The 4460 // problem is mostly avoided by translating f32 operations to v2f32 operations 4461 // on D-registers, but f32 loads are still a problem. 4462 // 4463 // These instructions can load an f32 into a NEON register: 4464 // 4465 // VLDRS - Only writes S, partial D update. 4466 // VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops. 4467 // VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops. 4468 // 4469 // FCONSTD can be used as a dependency-breaking instruction. 4470 unsigned ARMBaseInstrInfo:: 4471 getPartialRegUpdateClearance(const MachineInstr *MI, 4472 unsigned OpNum, 4473 const TargetRegisterInfo *TRI) const { 4474 if (!SwiftPartialUpdateClearance || 4475 !(Subtarget.isSwift() || Subtarget.isCortexA15())) 4476 return 0; 4477 4478 assert(TRI && "Need TRI instance"); 4479 4480 const MachineOperand &MO = MI->getOperand(OpNum); 4481 if (MO.readsReg()) 4482 return 0; 4483 unsigned Reg = MO.getReg(); 4484 int UseOp = -1; 4485 4486 switch(MI->getOpcode()) { 4487 // Normal instructions writing only an S-register. 4488 case ARM::VLDRS: 4489 case ARM::FCONSTS: 4490 case ARM::VMOVSR: 4491 case ARM::VMOVv8i8: 4492 case ARM::VMOVv4i16: 4493 case ARM::VMOVv2i32: 4494 case ARM::VMOVv2f32: 4495 case ARM::VMOVv1i64: 4496 UseOp = MI->findRegisterUseOperandIdx(Reg, false, TRI); 4497 break; 4498 4499 // Explicitly reads the dependency. 4500 case ARM::VLD1LNd32: 4501 UseOp = 3; 4502 break; 4503 default: 4504 return 0; 4505 } 4506 4507 // If this instruction actually reads a value from Reg, there is no unwanted 4508 // dependency. 4509 if (UseOp != -1 && MI->getOperand(UseOp).readsReg()) 4510 return 0; 4511 4512 // We must be able to clobber the whole D-reg. 4513 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 4514 // Virtual register must be a foo:ssub_0<def,undef> operand. 4515 if (!MO.getSubReg() || MI->readsVirtualRegister(Reg)) 4516 return 0; 4517 } else if (ARM::SPRRegClass.contains(Reg)) { 4518 // Physical register: MI must define the full D-reg. 4519 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0, 4520 &ARM::DPRRegClass); 4521 if (!DReg || !MI->definesRegister(DReg, TRI)) 4522 return 0; 4523 } 4524 4525 // MI has an unwanted D-register dependency. 4526 // Avoid defs in the previous N instructrions. 4527 return SwiftPartialUpdateClearance; 4528 } 4529 4530 // Break a partial register dependency after getPartialRegUpdateClearance 4531 // returned non-zero. 4532 void ARMBaseInstrInfo:: 4533 breakPartialRegDependency(MachineBasicBlock::iterator MI, 4534 unsigned OpNum, 4535 const TargetRegisterInfo *TRI) const { 4536 assert(MI && OpNum < MI->getDesc().getNumDefs() && "OpNum is not a def"); 4537 assert(TRI && "Need TRI instance"); 4538 4539 const MachineOperand &MO = MI->getOperand(OpNum); 4540 unsigned Reg = MO.getReg(); 4541 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && 4542 "Can't break virtual register dependencies."); 4543 unsigned DReg = Reg; 4544 4545 // If MI defines an S-reg, find the corresponding D super-register. 4546 if (ARM::SPRRegClass.contains(Reg)) { 4547 DReg = ARM::D0 + (Reg - ARM::S0) / 2; 4548 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken"); 4549 } 4550 4551 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps"); 4552 assert(MI->definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg"); 4553 4554 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines 4555 // the full D-register by loading the same value to both lanes. The 4556 // instruction is micro-coded with 2 uops, so don't do this until we can 4557 // properly schedule micro-coded instructions. The dispatcher stalls cause 4558 // too big regressions. 4559 4560 // Insert the dependency-breaking FCONSTD before MI. 4561 // 96 is the encoding of 0.5, but the actual value doesn't matter here. 4562 AddDefaultPred(BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 4563 get(ARM::FCONSTD), DReg).addImm(96)); 4564 MI->addRegisterKilled(DReg, TRI, true); 4565 } 4566 4567 bool ARMBaseInstrInfo::hasNOP() const { 4568 return Subtarget.getFeatureBits()[ARM::HasV6KOps]; 4569 } 4570 4571 bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const { 4572 if (MI->getNumOperands() < 4) 4573 return true; 4574 unsigned ShOpVal = MI->getOperand(3).getImm(); 4575 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal); 4576 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1. 4577 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) || 4578 ((ShImm == 1 || ShImm == 2) && 4579 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl)) 4580 return true; 4581 4582 return false; 4583 } 4584 4585 bool ARMBaseInstrInfo::getRegSequenceLikeInputs( 4586 const MachineInstr &MI, unsigned DefIdx, 4587 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const { 4588 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index"); 4589 assert(MI.isRegSequenceLike() && "Invalid kind of instruction"); 4590 4591 switch (MI.getOpcode()) { 4592 case ARM::VMOVDRR: 4593 // dX = VMOVDRR rY, rZ 4594 // is the same as: 4595 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1 4596 // Populate the InputRegs accordingly. 4597 // rY 4598 const MachineOperand *MOReg = &MI.getOperand(1); 4599 InputRegs.push_back( 4600 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0)); 4601 // rZ 4602 MOReg = &MI.getOperand(2); 4603 InputRegs.push_back( 4604 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1)); 4605 return true; 4606 } 4607 llvm_unreachable("Target dependent opcode missing"); 4608 } 4609 4610 bool ARMBaseInstrInfo::getExtractSubregLikeInputs( 4611 const MachineInstr &MI, unsigned DefIdx, 4612 RegSubRegPairAndIdx &InputReg) const { 4613 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index"); 4614 assert(MI.isExtractSubregLike() && "Invalid kind of instruction"); 4615 4616 switch (MI.getOpcode()) { 4617 case ARM::VMOVRRD: 4618 // rX, rY = VMOVRRD dZ 4619 // is the same as: 4620 // rX = EXTRACT_SUBREG dZ, ssub_0 4621 // rY = EXTRACT_SUBREG dZ, ssub_1 4622 const MachineOperand &MOReg = MI.getOperand(2); 4623 InputReg.Reg = MOReg.getReg(); 4624 InputReg.SubReg = MOReg.getSubReg(); 4625 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1; 4626 return true; 4627 } 4628 llvm_unreachable("Target dependent opcode missing"); 4629 } 4630 4631 bool ARMBaseInstrInfo::getInsertSubregLikeInputs( 4632 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg, 4633 RegSubRegPairAndIdx &InsertedReg) const { 4634 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index"); 4635 assert(MI.isInsertSubregLike() && "Invalid kind of instruction"); 4636 4637 switch (MI.getOpcode()) { 4638 case ARM::VSETLNi32: 4639 // dX = VSETLNi32 dY, rZ, imm 4640 const MachineOperand &MOBaseReg = MI.getOperand(1); 4641 const MachineOperand &MOInsertedReg = MI.getOperand(2); 4642 const MachineOperand &MOIndex = MI.getOperand(3); 4643 BaseReg.Reg = MOBaseReg.getReg(); 4644 BaseReg.SubReg = MOBaseReg.getSubReg(); 4645 4646 InsertedReg.Reg = MOInsertedReg.getReg(); 4647 InsertedReg.SubReg = MOInsertedReg.getSubReg(); 4648 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1; 4649 return true; 4650 } 4651 llvm_unreachable("Target dependent opcode missing"); 4652 } 4653