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