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