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