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 return Subtarget.isThumb2() ? divideCeil(NumInsts, 4) * 2 : 0; 2165 } 2166 2167 unsigned 2168 ARMBaseInstrInfo::predictBranchSizeForIfCvt(MachineInstr &MI) const { 2169 // If this branch is likely to be folded into the comparison to form a 2170 // CB(N)Z, then removing it won't reduce code size at all, because that will 2171 // just replace the CB(N)Z with a CMP. 2172 if (MI.getOpcode() == ARM::t2Bcc && 2173 findCMPToFoldIntoCBZ(&MI, &getRegisterInfo())) 2174 return 0; 2175 2176 unsigned Size = getInstSizeInBytes(MI); 2177 2178 // For Thumb2, all branches are 32-bit instructions during the if conversion 2179 // pass, but may be replaced with 16-bit instructions during size reduction. 2180 // Since the branches considered by if conversion tend to be forward branches 2181 // over small basic blocks, they are very likely to be in range for the 2182 // narrow instructions, so we assume the final code size will be half what it 2183 // currently is. 2184 if (Subtarget.isThumb2()) 2185 Size /= 2; 2186 2187 return Size; 2188 } 2189 2190 bool 2191 ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB, 2192 MachineBasicBlock &FMBB) const { 2193 // Reduce false anti-dependencies to let the target's out-of-order execution 2194 // engine do its thing. 2195 return Subtarget.isProfitableToUnpredicate(); 2196 } 2197 2198 /// getInstrPredicate - If instruction is predicated, returns its predicate 2199 /// condition, otherwise returns AL. It also returns the condition code 2200 /// register by reference. 2201 ARMCC::CondCodes llvm::getInstrPredicate(const MachineInstr &MI, 2202 Register &PredReg) { 2203 int PIdx = MI.findFirstPredOperandIdx(); 2204 if (PIdx == -1) { 2205 PredReg = 0; 2206 return ARMCC::AL; 2207 } 2208 2209 PredReg = MI.getOperand(PIdx+1).getReg(); 2210 return (ARMCC::CondCodes)MI.getOperand(PIdx).getImm(); 2211 } 2212 2213 unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) { 2214 if (Opc == ARM::B) 2215 return ARM::Bcc; 2216 if (Opc == ARM::tB) 2217 return ARM::tBcc; 2218 if (Opc == ARM::t2B) 2219 return ARM::t2Bcc; 2220 2221 llvm_unreachable("Unknown unconditional branch opcode!"); 2222 } 2223 2224 MachineInstr *ARMBaseInstrInfo::commuteInstructionImpl(MachineInstr &MI, 2225 bool NewMI, 2226 unsigned OpIdx1, 2227 unsigned OpIdx2) const { 2228 switch (MI.getOpcode()) { 2229 case ARM::MOVCCr: 2230 case ARM::t2MOVCCr: { 2231 // MOVCC can be commuted by inverting the condition. 2232 Register PredReg; 2233 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg); 2234 // MOVCC AL can't be inverted. Shouldn't happen. 2235 if (CC == ARMCC::AL || PredReg != ARM::CPSR) 2236 return nullptr; 2237 MachineInstr *CommutedMI = 2238 TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); 2239 if (!CommutedMI) 2240 return nullptr; 2241 // After swapping the MOVCC operands, also invert the condition. 2242 CommutedMI->getOperand(CommutedMI->findFirstPredOperandIdx()) 2243 .setImm(ARMCC::getOppositeCondition(CC)); 2244 return CommutedMI; 2245 } 2246 } 2247 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); 2248 } 2249 2250 /// Identify instructions that can be folded into a MOVCC instruction, and 2251 /// return the defining instruction. 2252 MachineInstr * 2253 ARMBaseInstrInfo::canFoldIntoMOVCC(Register Reg, const MachineRegisterInfo &MRI, 2254 const TargetInstrInfo *TII) const { 2255 if (!Reg.isVirtual()) 2256 return nullptr; 2257 if (!MRI.hasOneNonDBGUse(Reg)) 2258 return nullptr; 2259 MachineInstr *MI = MRI.getVRegDef(Reg); 2260 if (!MI) 2261 return nullptr; 2262 // Check if MI can be predicated and folded into the MOVCC. 2263 if (!isPredicable(*MI)) 2264 return nullptr; 2265 // Check if MI has any non-dead defs or physreg uses. This also detects 2266 // predicated instructions which will be reading CPSR. 2267 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) { 2268 const MachineOperand &MO = MI->getOperand(i); 2269 // Reject frame index operands, PEI can't handle the predicated pseudos. 2270 if (MO.isFI() || MO.isCPI() || MO.isJTI()) 2271 return nullptr; 2272 if (!MO.isReg()) 2273 continue; 2274 // MI can't have any tied operands, that would conflict with predication. 2275 if (MO.isTied()) 2276 return nullptr; 2277 if (Register::isPhysicalRegister(MO.getReg())) 2278 return nullptr; 2279 if (MO.isDef() && !MO.isDead()) 2280 return nullptr; 2281 } 2282 bool DontMoveAcrossStores = true; 2283 if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores)) 2284 return nullptr; 2285 return MI; 2286 } 2287 2288 bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr &MI, 2289 SmallVectorImpl<MachineOperand> &Cond, 2290 unsigned &TrueOp, unsigned &FalseOp, 2291 bool &Optimizable) const { 2292 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) && 2293 "Unknown select instruction"); 2294 // MOVCC operands: 2295 // 0: Def. 2296 // 1: True use. 2297 // 2: False use. 2298 // 3: Condition code. 2299 // 4: CPSR use. 2300 TrueOp = 1; 2301 FalseOp = 2; 2302 Cond.push_back(MI.getOperand(3)); 2303 Cond.push_back(MI.getOperand(4)); 2304 // We can always fold a def. 2305 Optimizable = true; 2306 return false; 2307 } 2308 2309 MachineInstr * 2310 ARMBaseInstrInfo::optimizeSelect(MachineInstr &MI, 2311 SmallPtrSetImpl<MachineInstr *> &SeenMIs, 2312 bool PreferFalse) const { 2313 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) && 2314 "Unknown select instruction"); 2315 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 2316 MachineInstr *DefMI = canFoldIntoMOVCC(MI.getOperand(2).getReg(), MRI, this); 2317 bool Invert = !DefMI; 2318 if (!DefMI) 2319 DefMI = canFoldIntoMOVCC(MI.getOperand(1).getReg(), MRI, this); 2320 if (!DefMI) 2321 return nullptr; 2322 2323 // Find new register class to use. 2324 MachineOperand FalseReg = MI.getOperand(Invert ? 2 : 1); 2325 Register DestReg = MI.getOperand(0).getReg(); 2326 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg()); 2327 if (!MRI.constrainRegClass(DestReg, PreviousClass)) 2328 return nullptr; 2329 2330 // Create a new predicated version of DefMI. 2331 // Rfalse is the first use. 2332 MachineInstrBuilder NewMI = 2333 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), DefMI->getDesc(), DestReg); 2334 2335 // Copy all the DefMI operands, excluding its (null) predicate. 2336 const MCInstrDesc &DefDesc = DefMI->getDesc(); 2337 for (unsigned i = 1, e = DefDesc.getNumOperands(); 2338 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i) 2339 NewMI.add(DefMI->getOperand(i)); 2340 2341 unsigned CondCode = MI.getOperand(3).getImm(); 2342 if (Invert) 2343 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode))); 2344 else 2345 NewMI.addImm(CondCode); 2346 NewMI.add(MI.getOperand(4)); 2347 2348 // DefMI is not the -S version that sets CPSR, so add an optional %noreg. 2349 if (NewMI->hasOptionalDef()) 2350 NewMI.add(condCodeOp()); 2351 2352 // The output register value when the predicate is false is an implicit 2353 // register operand tied to the first def. 2354 // The tie makes the register allocator ensure the FalseReg is allocated the 2355 // same register as operand 0. 2356 FalseReg.setImplicit(); 2357 NewMI.add(FalseReg); 2358 NewMI->tieOperands(0, NewMI->getNumOperands() - 1); 2359 2360 // Update SeenMIs set: register newly created MI and erase removed DefMI. 2361 SeenMIs.insert(NewMI); 2362 SeenMIs.erase(DefMI); 2363 2364 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on 2365 // DefMI would be invalid when tranferred inside the loop. Checking for a 2366 // loop is expensive, but at least remove kill flags if they are in different 2367 // BBs. 2368 if (DefMI->getParent() != MI.getParent()) 2369 NewMI->clearKillInfo(); 2370 2371 // The caller will erase MI, but not DefMI. 2372 DefMI->eraseFromParent(); 2373 return NewMI; 2374 } 2375 2376 /// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the 2377 /// instruction is encoded with an 'S' bit is determined by the optional CPSR 2378 /// def operand. 2379 /// 2380 /// This will go away once we can teach tblgen how to set the optional CPSR def 2381 /// operand itself. 2382 struct AddSubFlagsOpcodePair { 2383 uint16_t PseudoOpc; 2384 uint16_t MachineOpc; 2385 }; 2386 2387 static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = { 2388 {ARM::ADDSri, ARM::ADDri}, 2389 {ARM::ADDSrr, ARM::ADDrr}, 2390 {ARM::ADDSrsi, ARM::ADDrsi}, 2391 {ARM::ADDSrsr, ARM::ADDrsr}, 2392 2393 {ARM::SUBSri, ARM::SUBri}, 2394 {ARM::SUBSrr, ARM::SUBrr}, 2395 {ARM::SUBSrsi, ARM::SUBrsi}, 2396 {ARM::SUBSrsr, ARM::SUBrsr}, 2397 2398 {ARM::RSBSri, ARM::RSBri}, 2399 {ARM::RSBSrsi, ARM::RSBrsi}, 2400 {ARM::RSBSrsr, ARM::RSBrsr}, 2401 2402 {ARM::tADDSi3, ARM::tADDi3}, 2403 {ARM::tADDSi8, ARM::tADDi8}, 2404 {ARM::tADDSrr, ARM::tADDrr}, 2405 {ARM::tADCS, ARM::tADC}, 2406 2407 {ARM::tSUBSi3, ARM::tSUBi3}, 2408 {ARM::tSUBSi8, ARM::tSUBi8}, 2409 {ARM::tSUBSrr, ARM::tSUBrr}, 2410 {ARM::tSBCS, ARM::tSBC}, 2411 {ARM::tRSBS, ARM::tRSB}, 2412 {ARM::tLSLSri, ARM::tLSLri}, 2413 2414 {ARM::t2ADDSri, ARM::t2ADDri}, 2415 {ARM::t2ADDSrr, ARM::t2ADDrr}, 2416 {ARM::t2ADDSrs, ARM::t2ADDrs}, 2417 2418 {ARM::t2SUBSri, ARM::t2SUBri}, 2419 {ARM::t2SUBSrr, ARM::t2SUBrr}, 2420 {ARM::t2SUBSrs, ARM::t2SUBrs}, 2421 2422 {ARM::t2RSBSri, ARM::t2RSBri}, 2423 {ARM::t2RSBSrs, ARM::t2RSBrs}, 2424 }; 2425 2426 unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) { 2427 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i) 2428 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc) 2429 return AddSubFlagsOpcodeMap[i].MachineOpc; 2430 return 0; 2431 } 2432 2433 void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB, 2434 MachineBasicBlock::iterator &MBBI, 2435 const DebugLoc &dl, Register DestReg, 2436 Register BaseReg, int NumBytes, 2437 ARMCC::CondCodes Pred, Register PredReg, 2438 const ARMBaseInstrInfo &TII, 2439 unsigned MIFlags) { 2440 if (NumBytes == 0 && DestReg != BaseReg) { 2441 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg) 2442 .addReg(BaseReg, RegState::Kill) 2443 .add(predOps(Pred, PredReg)) 2444 .add(condCodeOp()) 2445 .setMIFlags(MIFlags); 2446 return; 2447 } 2448 2449 bool isSub = NumBytes < 0; 2450 if (isSub) NumBytes = -NumBytes; 2451 2452 while (NumBytes) { 2453 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes); 2454 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt); 2455 assert(ThisVal && "Didn't extract field correctly"); 2456 2457 // We will handle these bits from offset, clear them. 2458 NumBytes &= ~ThisVal; 2459 2460 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?"); 2461 2462 // Build the new ADD / SUB. 2463 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri; 2464 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg) 2465 .addReg(BaseReg, RegState::Kill) 2466 .addImm(ThisVal) 2467 .add(predOps(Pred, PredReg)) 2468 .add(condCodeOp()) 2469 .setMIFlags(MIFlags); 2470 BaseReg = DestReg; 2471 } 2472 } 2473 2474 bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget, 2475 MachineFunction &MF, MachineInstr *MI, 2476 unsigned NumBytes) { 2477 // This optimisation potentially adds lots of load and store 2478 // micro-operations, it's only really a great benefit to code-size. 2479 if (!Subtarget.hasMinSize()) 2480 return false; 2481 2482 // If only one register is pushed/popped, LLVM can use an LDR/STR 2483 // instead. We can't modify those so make sure we're dealing with an 2484 // instruction we understand. 2485 bool IsPop = isPopOpcode(MI->getOpcode()); 2486 bool IsPush = isPushOpcode(MI->getOpcode()); 2487 if (!IsPush && !IsPop) 2488 return false; 2489 2490 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD || 2491 MI->getOpcode() == ARM::VLDMDIA_UPD; 2492 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH || 2493 MI->getOpcode() == ARM::tPOP || 2494 MI->getOpcode() == ARM::tPOP_RET; 2495 2496 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP && 2497 MI->getOperand(1).getReg() == ARM::SP)) && 2498 "trying to fold sp update into non-sp-updating push/pop"); 2499 2500 // The VFP push & pop act on D-registers, so we can only fold an adjustment 2501 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try 2502 // if this is violated. 2503 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0) 2504 return false; 2505 2506 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+ 2507 // pred) so the list starts at 4. Thumb1 starts after the predicate. 2508 int RegListIdx = IsT1PushPop ? 2 : 4; 2509 2510 // Calculate the space we'll need in terms of registers. 2511 unsigned RegsNeeded; 2512 const TargetRegisterClass *RegClass; 2513 if (IsVFPPushPop) { 2514 RegsNeeded = NumBytes / 8; 2515 RegClass = &ARM::DPRRegClass; 2516 } else { 2517 RegsNeeded = NumBytes / 4; 2518 RegClass = &ARM::GPRRegClass; 2519 } 2520 2521 // We're going to have to strip all list operands off before 2522 // re-adding them since the order matters, so save the existing ones 2523 // for later. 2524 SmallVector<MachineOperand, 4> RegList; 2525 2526 // We're also going to need the first register transferred by this 2527 // instruction, which won't necessarily be the first register in the list. 2528 unsigned FirstRegEnc = -1; 2529 2530 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo(); 2531 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i) { 2532 MachineOperand &MO = MI->getOperand(i); 2533 RegList.push_back(MO); 2534 2535 if (MO.isReg() && !MO.isImplicit() && 2536 TRI->getEncodingValue(MO.getReg()) < FirstRegEnc) 2537 FirstRegEnc = TRI->getEncodingValue(MO.getReg()); 2538 } 2539 2540 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF); 2541 2542 // Now try to find enough space in the reglist to allocate NumBytes. 2543 for (int CurRegEnc = FirstRegEnc - 1; CurRegEnc >= 0 && RegsNeeded; 2544 --CurRegEnc) { 2545 unsigned CurReg = RegClass->getRegister(CurRegEnc); 2546 if (IsT1PushPop && CurRegEnc > TRI->getEncodingValue(ARM::R7)) 2547 continue; 2548 if (!IsPop) { 2549 // Pushing any register is completely harmless, mark the register involved 2550 // as undef since we don't care about its value and must not restore it 2551 // during stack unwinding. 2552 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false, 2553 false, false, true)); 2554 --RegsNeeded; 2555 continue; 2556 } 2557 2558 // However, we can only pop an extra register if it's not live. For 2559 // registers live within the function we might clobber a return value 2560 // register; the other way a register can be live here is if it's 2561 // callee-saved. 2562 if (isCalleeSavedRegister(CurReg, CSRegs) || 2563 MI->getParent()->computeRegisterLiveness(TRI, CurReg, MI) != 2564 MachineBasicBlock::LQR_Dead) { 2565 // VFP pops don't allow holes in the register list, so any skip is fatal 2566 // for our transformation. GPR pops do, so we should just keep looking. 2567 if (IsVFPPushPop) 2568 return false; 2569 else 2570 continue; 2571 } 2572 2573 // Mark the unimportant registers as <def,dead> in the POP. 2574 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false, 2575 true)); 2576 --RegsNeeded; 2577 } 2578 2579 if (RegsNeeded > 0) 2580 return false; 2581 2582 // Finally we know we can profitably perform the optimisation so go 2583 // ahead: strip all existing registers off and add them back again 2584 // in the right order. 2585 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i) 2586 MI->RemoveOperand(i); 2587 2588 // Add the complete list back in. 2589 MachineInstrBuilder MIB(MF, &*MI); 2590 for (int i = RegList.size() - 1; i >= 0; --i) 2591 MIB.add(RegList[i]); 2592 2593 return true; 2594 } 2595 2596 bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx, 2597 Register FrameReg, int &Offset, 2598 const ARMBaseInstrInfo &TII) { 2599 unsigned Opcode = MI.getOpcode(); 2600 const MCInstrDesc &Desc = MI.getDesc(); 2601 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask); 2602 bool isSub = false; 2603 2604 // Memory operands in inline assembly always use AddrMode2. 2605 if (Opcode == ARM::INLINEASM || Opcode == ARM::INLINEASM_BR) 2606 AddrMode = ARMII::AddrMode2; 2607 2608 if (Opcode == ARM::ADDri) { 2609 Offset += MI.getOperand(FrameRegIdx+1).getImm(); 2610 if (Offset == 0) { 2611 // Turn it into a move. 2612 MI.setDesc(TII.get(ARM::MOVr)); 2613 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); 2614 MI.RemoveOperand(FrameRegIdx+1); 2615 Offset = 0; 2616 return true; 2617 } else if (Offset < 0) { 2618 Offset = -Offset; 2619 isSub = true; 2620 MI.setDesc(TII.get(ARM::SUBri)); 2621 } 2622 2623 // Common case: small offset, fits into instruction. 2624 if (ARM_AM::getSOImmVal(Offset) != -1) { 2625 // Replace the FrameIndex with sp / fp 2626 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); 2627 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset); 2628 Offset = 0; 2629 return true; 2630 } 2631 2632 // Otherwise, pull as much of the immedidate into this ADDri/SUBri 2633 // as possible. 2634 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset); 2635 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt); 2636 2637 // We will handle these bits from offset, clear them. 2638 Offset &= ~ThisImmVal; 2639 2640 // Get the properly encoded SOImmVal field. 2641 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 && 2642 "Bit extraction didn't work?"); 2643 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal); 2644 } else { 2645 unsigned ImmIdx = 0; 2646 int InstrOffs = 0; 2647 unsigned NumBits = 0; 2648 unsigned Scale = 1; 2649 switch (AddrMode) { 2650 case ARMII::AddrMode_i12: 2651 ImmIdx = FrameRegIdx + 1; 2652 InstrOffs = MI.getOperand(ImmIdx).getImm(); 2653 NumBits = 12; 2654 break; 2655 case ARMII::AddrMode2: 2656 ImmIdx = FrameRegIdx+2; 2657 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm()); 2658 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub) 2659 InstrOffs *= -1; 2660 NumBits = 12; 2661 break; 2662 case ARMII::AddrMode3: 2663 ImmIdx = FrameRegIdx+2; 2664 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm()); 2665 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub) 2666 InstrOffs *= -1; 2667 NumBits = 8; 2668 break; 2669 case ARMII::AddrMode4: 2670 case ARMII::AddrMode6: 2671 // Can't fold any offset even if it's zero. 2672 return false; 2673 case ARMII::AddrMode5: 2674 ImmIdx = FrameRegIdx+1; 2675 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm()); 2676 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub) 2677 InstrOffs *= -1; 2678 NumBits = 8; 2679 Scale = 4; 2680 break; 2681 case ARMII::AddrMode5FP16: 2682 ImmIdx = FrameRegIdx+1; 2683 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm()); 2684 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub) 2685 InstrOffs *= -1; 2686 NumBits = 8; 2687 Scale = 2; 2688 break; 2689 case ARMII::AddrModeT2_i7: 2690 case ARMII::AddrModeT2_i7s2: 2691 case ARMII::AddrModeT2_i7s4: 2692 ImmIdx = FrameRegIdx+1; 2693 InstrOffs = MI.getOperand(ImmIdx).getImm(); 2694 NumBits = 7; 2695 Scale = (AddrMode == ARMII::AddrModeT2_i7s2 ? 2 : 2696 AddrMode == ARMII::AddrModeT2_i7s4 ? 4 : 1); 2697 break; 2698 default: 2699 llvm_unreachable("Unsupported addressing mode!"); 2700 } 2701 2702 Offset += InstrOffs * Scale; 2703 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!"); 2704 if (Offset < 0) { 2705 Offset = -Offset; 2706 isSub = true; 2707 } 2708 2709 // Attempt to fold address comp. if opcode has offset bits 2710 if (NumBits > 0) { 2711 // Common case: small offset, fits into instruction. 2712 MachineOperand &ImmOp = MI.getOperand(ImmIdx); 2713 int ImmedOffset = Offset / Scale; 2714 unsigned Mask = (1 << NumBits) - 1; 2715 if ((unsigned)Offset <= Mask * Scale) { 2716 // Replace the FrameIndex with sp 2717 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); 2718 // FIXME: When addrmode2 goes away, this will simplify (like the 2719 // T2 version), as the LDR.i12 versions don't need the encoding 2720 // tricks for the offset value. 2721 if (isSub) { 2722 if (AddrMode == ARMII::AddrMode_i12) 2723 ImmedOffset = -ImmedOffset; 2724 else 2725 ImmedOffset |= 1 << NumBits; 2726 } 2727 ImmOp.ChangeToImmediate(ImmedOffset); 2728 Offset = 0; 2729 return true; 2730 } 2731 2732 // Otherwise, it didn't fit. Pull in what we can to simplify the immed. 2733 ImmedOffset = ImmedOffset & Mask; 2734 if (isSub) { 2735 if (AddrMode == ARMII::AddrMode_i12) 2736 ImmedOffset = -ImmedOffset; 2737 else 2738 ImmedOffset |= 1 << NumBits; 2739 } 2740 ImmOp.ChangeToImmediate(ImmedOffset); 2741 Offset &= ~(Mask*Scale); 2742 } 2743 } 2744 2745 Offset = (isSub) ? -Offset : Offset; 2746 return Offset == 0; 2747 } 2748 2749 /// analyzeCompare - For a comparison instruction, return the source registers 2750 /// in SrcReg and SrcReg2 if having two register operands, and the value it 2751 /// compares against in CmpValue. Return true if the comparison instruction 2752 /// can be analyzed. 2753 bool ARMBaseInstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg, 2754 Register &SrcReg2, int &CmpMask, 2755 int &CmpValue) const { 2756 switch (MI.getOpcode()) { 2757 default: break; 2758 case ARM::CMPri: 2759 case ARM::t2CMPri: 2760 case ARM::tCMPi8: 2761 SrcReg = MI.getOperand(0).getReg(); 2762 SrcReg2 = 0; 2763 CmpMask = ~0; 2764 CmpValue = MI.getOperand(1).getImm(); 2765 return true; 2766 case ARM::CMPrr: 2767 case ARM::t2CMPrr: 2768 case ARM::tCMPr: 2769 SrcReg = MI.getOperand(0).getReg(); 2770 SrcReg2 = MI.getOperand(1).getReg(); 2771 CmpMask = ~0; 2772 CmpValue = 0; 2773 return true; 2774 case ARM::TSTri: 2775 case ARM::t2TSTri: 2776 SrcReg = MI.getOperand(0).getReg(); 2777 SrcReg2 = 0; 2778 CmpMask = MI.getOperand(1).getImm(); 2779 CmpValue = 0; 2780 return true; 2781 } 2782 2783 return false; 2784 } 2785 2786 /// isSuitableForMask - Identify a suitable 'and' instruction that 2787 /// operates on the given source register and applies the same mask 2788 /// as a 'tst' instruction. Provide a limited look-through for copies. 2789 /// When successful, MI will hold the found instruction. 2790 static bool isSuitableForMask(MachineInstr *&MI, Register SrcReg, 2791 int CmpMask, bool CommonUse) { 2792 switch (MI->getOpcode()) { 2793 case ARM::ANDri: 2794 case ARM::t2ANDri: 2795 if (CmpMask != MI->getOperand(2).getImm()) 2796 return false; 2797 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg()) 2798 return true; 2799 break; 2800 } 2801 2802 return false; 2803 } 2804 2805 /// getCmpToAddCondition - assume the flags are set by CMP(a,b), return 2806 /// the condition code if we modify the instructions such that flags are 2807 /// set by ADD(a,b,X). 2808 inline static ARMCC::CondCodes getCmpToAddCondition(ARMCC::CondCodes CC) { 2809 switch (CC) { 2810 default: return ARMCC::AL; 2811 case ARMCC::HS: return ARMCC::LO; 2812 case ARMCC::LO: return ARMCC::HS; 2813 case ARMCC::VS: return ARMCC::VS; 2814 case ARMCC::VC: return ARMCC::VC; 2815 } 2816 } 2817 2818 /// isRedundantFlagInstr - check whether the first instruction, whose only 2819 /// purpose is to update flags, can be made redundant. 2820 /// CMPrr can be made redundant by SUBrr if the operands are the same. 2821 /// CMPri can be made redundant by SUBri if the operands are the same. 2822 /// CMPrr(r0, r1) can be made redundant by ADDr[ri](r0, r1, X). 2823 /// This function can be extended later on. 2824 inline static bool isRedundantFlagInstr(const MachineInstr *CmpI, 2825 Register SrcReg, Register SrcReg2, 2826 int ImmValue, const MachineInstr *OI, 2827 bool &IsThumb1) { 2828 if ((CmpI->getOpcode() == ARM::CMPrr || CmpI->getOpcode() == ARM::t2CMPrr) && 2829 (OI->getOpcode() == ARM::SUBrr || OI->getOpcode() == ARM::t2SUBrr) && 2830 ((OI->getOperand(1).getReg() == SrcReg && 2831 OI->getOperand(2).getReg() == SrcReg2) || 2832 (OI->getOperand(1).getReg() == SrcReg2 && 2833 OI->getOperand(2).getReg() == SrcReg))) { 2834 IsThumb1 = false; 2835 return true; 2836 } 2837 2838 if (CmpI->getOpcode() == ARM::tCMPr && OI->getOpcode() == ARM::tSUBrr && 2839 ((OI->getOperand(2).getReg() == SrcReg && 2840 OI->getOperand(3).getReg() == SrcReg2) || 2841 (OI->getOperand(2).getReg() == SrcReg2 && 2842 OI->getOperand(3).getReg() == SrcReg))) { 2843 IsThumb1 = true; 2844 return true; 2845 } 2846 2847 if ((CmpI->getOpcode() == ARM::CMPri || CmpI->getOpcode() == ARM::t2CMPri) && 2848 (OI->getOpcode() == ARM::SUBri || OI->getOpcode() == ARM::t2SUBri) && 2849 OI->getOperand(1).getReg() == SrcReg && 2850 OI->getOperand(2).getImm() == ImmValue) { 2851 IsThumb1 = false; 2852 return true; 2853 } 2854 2855 if (CmpI->getOpcode() == ARM::tCMPi8 && 2856 (OI->getOpcode() == ARM::tSUBi8 || OI->getOpcode() == ARM::tSUBi3) && 2857 OI->getOperand(2).getReg() == SrcReg && 2858 OI->getOperand(3).getImm() == ImmValue) { 2859 IsThumb1 = true; 2860 return true; 2861 } 2862 2863 if ((CmpI->getOpcode() == ARM::CMPrr || CmpI->getOpcode() == ARM::t2CMPrr) && 2864 (OI->getOpcode() == ARM::ADDrr || OI->getOpcode() == ARM::t2ADDrr || 2865 OI->getOpcode() == ARM::ADDri || OI->getOpcode() == ARM::t2ADDri) && 2866 OI->getOperand(0).isReg() && OI->getOperand(1).isReg() && 2867 OI->getOperand(0).getReg() == SrcReg && 2868 OI->getOperand(1).getReg() == SrcReg2) { 2869 IsThumb1 = false; 2870 return true; 2871 } 2872 2873 if (CmpI->getOpcode() == ARM::tCMPr && 2874 (OI->getOpcode() == ARM::tADDi3 || OI->getOpcode() == ARM::tADDi8 || 2875 OI->getOpcode() == ARM::tADDrr) && 2876 OI->getOperand(0).getReg() == SrcReg && 2877 OI->getOperand(2).getReg() == SrcReg2) { 2878 IsThumb1 = true; 2879 return true; 2880 } 2881 2882 return false; 2883 } 2884 2885 static bool isOptimizeCompareCandidate(MachineInstr *MI, bool &IsThumb1) { 2886 switch (MI->getOpcode()) { 2887 default: return false; 2888 case ARM::tLSLri: 2889 case ARM::tLSRri: 2890 case ARM::tLSLrr: 2891 case ARM::tLSRrr: 2892 case ARM::tSUBrr: 2893 case ARM::tADDrr: 2894 case ARM::tADDi3: 2895 case ARM::tADDi8: 2896 case ARM::tSUBi3: 2897 case ARM::tSUBi8: 2898 case ARM::tMUL: 2899 case ARM::tADC: 2900 case ARM::tSBC: 2901 case ARM::tRSB: 2902 case ARM::tAND: 2903 case ARM::tORR: 2904 case ARM::tEOR: 2905 case ARM::tBIC: 2906 case ARM::tMVN: 2907 case ARM::tASRri: 2908 case ARM::tASRrr: 2909 case ARM::tROR: 2910 IsThumb1 = true; 2911 LLVM_FALLTHROUGH; 2912 case ARM::RSBrr: 2913 case ARM::RSBri: 2914 case ARM::RSCrr: 2915 case ARM::RSCri: 2916 case ARM::ADDrr: 2917 case ARM::ADDri: 2918 case ARM::ADCrr: 2919 case ARM::ADCri: 2920 case ARM::SUBrr: 2921 case ARM::SUBri: 2922 case ARM::SBCrr: 2923 case ARM::SBCri: 2924 case ARM::t2RSBri: 2925 case ARM::t2ADDrr: 2926 case ARM::t2ADDri: 2927 case ARM::t2ADCrr: 2928 case ARM::t2ADCri: 2929 case ARM::t2SUBrr: 2930 case ARM::t2SUBri: 2931 case ARM::t2SBCrr: 2932 case ARM::t2SBCri: 2933 case ARM::ANDrr: 2934 case ARM::ANDri: 2935 case ARM::t2ANDrr: 2936 case ARM::t2ANDri: 2937 case ARM::ORRrr: 2938 case ARM::ORRri: 2939 case ARM::t2ORRrr: 2940 case ARM::t2ORRri: 2941 case ARM::EORrr: 2942 case ARM::EORri: 2943 case ARM::t2EORrr: 2944 case ARM::t2EORri: 2945 case ARM::t2LSRri: 2946 case ARM::t2LSRrr: 2947 case ARM::t2LSLri: 2948 case ARM::t2LSLrr: 2949 return true; 2950 } 2951 } 2952 2953 /// optimizeCompareInstr - Convert the instruction supplying the argument to the 2954 /// comparison into one that sets the zero bit in the flags register; 2955 /// Remove a redundant Compare instruction if an earlier instruction can set the 2956 /// flags in the same way as Compare. 2957 /// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two 2958 /// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the 2959 /// condition code of instructions which use the flags. 2960 bool ARMBaseInstrInfo::optimizeCompareInstr( 2961 MachineInstr &CmpInstr, Register SrcReg, Register SrcReg2, int CmpMask, 2962 int CmpValue, const MachineRegisterInfo *MRI) const { 2963 // Get the unique definition of SrcReg. 2964 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg); 2965 if (!MI) return false; 2966 2967 // Masked compares sometimes use the same register as the corresponding 'and'. 2968 if (CmpMask != ~0) { 2969 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(*MI)) { 2970 MI = nullptr; 2971 for (MachineRegisterInfo::use_instr_iterator 2972 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end(); 2973 UI != UE; ++UI) { 2974 if (UI->getParent() != CmpInstr.getParent()) 2975 continue; 2976 MachineInstr *PotentialAND = &*UI; 2977 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) || 2978 isPredicated(*PotentialAND)) 2979 continue; 2980 MI = PotentialAND; 2981 break; 2982 } 2983 if (!MI) return false; 2984 } 2985 } 2986 2987 // Get ready to iterate backward from CmpInstr. 2988 MachineBasicBlock::iterator I = CmpInstr, E = MI, 2989 B = CmpInstr.getParent()->begin(); 2990 2991 // Early exit if CmpInstr is at the beginning of the BB. 2992 if (I == B) return false; 2993 2994 // There are two possible candidates which can be changed to set CPSR: 2995 // One is MI, the other is a SUB or ADD instruction. 2996 // For CMPrr(r1,r2), we are looking for SUB(r1,r2), SUB(r2,r1), or 2997 // ADDr[ri](r1, r2, X). 2998 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue). 2999 MachineInstr *SubAdd = nullptr; 3000 if (SrcReg2 != 0) 3001 // MI is not a candidate for CMPrr. 3002 MI = nullptr; 3003 else if (MI->getParent() != CmpInstr.getParent() || CmpValue != 0) { 3004 // Conservatively refuse to convert an instruction which isn't in the same 3005 // BB as the comparison. 3006 // For CMPri w/ CmpValue != 0, a SubAdd may still be a candidate. 3007 // Thus we cannot return here. 3008 if (CmpInstr.getOpcode() == ARM::CMPri || 3009 CmpInstr.getOpcode() == ARM::t2CMPri || 3010 CmpInstr.getOpcode() == ARM::tCMPi8) 3011 MI = nullptr; 3012 else 3013 return false; 3014 } 3015 3016 bool IsThumb1 = false; 3017 if (MI && !isOptimizeCompareCandidate(MI, IsThumb1)) 3018 return false; 3019 3020 // We also want to do this peephole for cases like this: if (a*b == 0), 3021 // and optimise away the CMP instruction from the generated code sequence: 3022 // MULS, MOVS, MOVS, CMP. Here the MOVS instructions load the boolean values 3023 // resulting from the select instruction, but these MOVS instructions for 3024 // Thumb1 (V6M) are flag setting and are thus preventing this optimisation. 3025 // However, if we only have MOVS instructions in between the CMP and the 3026 // other instruction (the MULS in this example), then the CPSR is dead so we 3027 // can safely reorder the sequence into: MOVS, MOVS, MULS, CMP. We do this 3028 // reordering and then continue the analysis hoping we can eliminate the 3029 // CMP. This peephole works on the vregs, so is still in SSA form. As a 3030 // consequence, the movs won't redefine/kill the MUL operands which would 3031 // make this reordering illegal. 3032 const TargetRegisterInfo *TRI = &getRegisterInfo(); 3033 if (MI && IsThumb1) { 3034 --I; 3035 if (I != E && !MI->readsRegister(ARM::CPSR, TRI)) { 3036 bool CanReorder = true; 3037 for (; I != E; --I) { 3038 if (I->getOpcode() != ARM::tMOVi8) { 3039 CanReorder = false; 3040 break; 3041 } 3042 } 3043 if (CanReorder) { 3044 MI = MI->removeFromParent(); 3045 E = CmpInstr; 3046 CmpInstr.getParent()->insert(E, MI); 3047 } 3048 } 3049 I = CmpInstr; 3050 E = MI; 3051 } 3052 3053 // Check that CPSR isn't set between the comparison instruction and the one we 3054 // want to change. At the same time, search for SubAdd. 3055 bool SubAddIsThumb1 = false; 3056 do { 3057 const MachineInstr &Instr = *--I; 3058 3059 // Check whether CmpInstr can be made redundant by the current instruction. 3060 if (isRedundantFlagInstr(&CmpInstr, SrcReg, SrcReg2, CmpValue, &Instr, 3061 SubAddIsThumb1)) { 3062 SubAdd = &*I; 3063 break; 3064 } 3065 3066 // Allow E (which was initially MI) to be SubAdd but do not search before E. 3067 if (I == E) 3068 break; 3069 3070 if (Instr.modifiesRegister(ARM::CPSR, TRI) || 3071 Instr.readsRegister(ARM::CPSR, TRI)) 3072 // This instruction modifies or uses CPSR after the one we want to 3073 // change. We can't do this transformation. 3074 return false; 3075 3076 if (I == B) { 3077 // In some cases, we scan the use-list of an instruction for an AND; 3078 // that AND is in the same BB, but may not be scheduled before the 3079 // corresponding TST. In that case, bail out. 3080 // 3081 // FIXME: We could try to reschedule the AND. 3082 return false; 3083 } 3084 } while (true); 3085 3086 // Return false if no candidates exist. 3087 if (!MI && !SubAdd) 3088 return false; 3089 3090 // If we found a SubAdd, use it as it will be closer to the CMP 3091 if (SubAdd) { 3092 MI = SubAdd; 3093 IsThumb1 = SubAddIsThumb1; 3094 } 3095 3096 // We can't use a predicated instruction - it doesn't always write the flags. 3097 if (isPredicated(*MI)) 3098 return false; 3099 3100 // Scan forward for the use of CPSR 3101 // When checking against MI: if it's a conditional code that requires 3102 // checking of the V bit or C bit, then this is not safe to do. 3103 // It is safe to remove CmpInstr if CPSR is redefined or killed. 3104 // If we are done with the basic block, we need to check whether CPSR is 3105 // live-out. 3106 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4> 3107 OperandsToUpdate; 3108 bool isSafe = false; 3109 I = CmpInstr; 3110 E = CmpInstr.getParent()->end(); 3111 while (!isSafe && ++I != E) { 3112 const MachineInstr &Instr = *I; 3113 for (unsigned IO = 0, EO = Instr.getNumOperands(); 3114 !isSafe && IO != EO; ++IO) { 3115 const MachineOperand &MO = Instr.getOperand(IO); 3116 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) { 3117 isSafe = true; 3118 break; 3119 } 3120 if (!MO.isReg() || MO.getReg() != ARM::CPSR) 3121 continue; 3122 if (MO.isDef()) { 3123 isSafe = true; 3124 break; 3125 } 3126 // Condition code is after the operand before CPSR except for VSELs. 3127 ARMCC::CondCodes CC; 3128 bool IsInstrVSel = true; 3129 switch (Instr.getOpcode()) { 3130 default: 3131 IsInstrVSel = false; 3132 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm(); 3133 break; 3134 case ARM::VSELEQD: 3135 case ARM::VSELEQS: 3136 case ARM::VSELEQH: 3137 CC = ARMCC::EQ; 3138 break; 3139 case ARM::VSELGTD: 3140 case ARM::VSELGTS: 3141 case ARM::VSELGTH: 3142 CC = ARMCC::GT; 3143 break; 3144 case ARM::VSELGED: 3145 case ARM::VSELGES: 3146 case ARM::VSELGEH: 3147 CC = ARMCC::GE; 3148 break; 3149 case ARM::VSELVSD: 3150 case ARM::VSELVSS: 3151 case ARM::VSELVSH: 3152 CC = ARMCC::VS; 3153 break; 3154 } 3155 3156 if (SubAdd) { 3157 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based 3158 // on CMP needs to be updated to be based on SUB. 3159 // If we have ADD(r1, r2, X) and CMP(r1, r2), the condition code also 3160 // needs to be modified. 3161 // Push the condition code operands to OperandsToUpdate. 3162 // If it is safe to remove CmpInstr, the condition code of these 3163 // operands will be modified. 3164 unsigned Opc = SubAdd->getOpcode(); 3165 bool IsSub = Opc == ARM::SUBrr || Opc == ARM::t2SUBrr || 3166 Opc == ARM::SUBri || Opc == ARM::t2SUBri || 3167 Opc == ARM::tSUBrr || Opc == ARM::tSUBi3 || 3168 Opc == ARM::tSUBi8; 3169 unsigned OpI = Opc != ARM::tSUBrr ? 1 : 2; 3170 if (!IsSub || 3171 (SrcReg2 != 0 && SubAdd->getOperand(OpI).getReg() == SrcReg2 && 3172 SubAdd->getOperand(OpI + 1).getReg() == SrcReg)) { 3173 // VSel doesn't support condition code update. 3174 if (IsInstrVSel) 3175 return false; 3176 // Ensure we can swap the condition. 3177 ARMCC::CondCodes NewCC = (IsSub ? getSwappedCondition(CC) : getCmpToAddCondition(CC)); 3178 if (NewCC == ARMCC::AL) 3179 return false; 3180 OperandsToUpdate.push_back( 3181 std::make_pair(&((*I).getOperand(IO - 1)), NewCC)); 3182 } 3183 } else { 3184 // No SubAdd, so this is x = <op> y, z; cmp x, 0. 3185 switch (CC) { 3186 case ARMCC::EQ: // Z 3187 case ARMCC::NE: // Z 3188 case ARMCC::MI: // N 3189 case ARMCC::PL: // N 3190 case ARMCC::AL: // none 3191 // CPSR can be used multiple times, we should continue. 3192 break; 3193 case ARMCC::HS: // C 3194 case ARMCC::LO: // C 3195 case ARMCC::VS: // V 3196 case ARMCC::VC: // V 3197 case ARMCC::HI: // C Z 3198 case ARMCC::LS: // C Z 3199 case ARMCC::GE: // N V 3200 case ARMCC::LT: // N V 3201 case ARMCC::GT: // Z N V 3202 case ARMCC::LE: // Z N V 3203 // The instruction uses the V bit or C bit which is not safe. 3204 return false; 3205 } 3206 } 3207 } 3208 } 3209 3210 // If CPSR is not killed nor re-defined, we should check whether it is 3211 // live-out. If it is live-out, do not optimize. 3212 if (!isSafe) { 3213 MachineBasicBlock *MBB = CmpInstr.getParent(); 3214 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), 3215 SE = MBB->succ_end(); SI != SE; ++SI) 3216 if ((*SI)->isLiveIn(ARM::CPSR)) 3217 return false; 3218 } 3219 3220 // Toggle the optional operand to CPSR (if it exists - in Thumb1 we always 3221 // set CPSR so this is represented as an explicit output) 3222 if (!IsThumb1) { 3223 MI->getOperand(5).setReg(ARM::CPSR); 3224 MI->getOperand(5).setIsDef(true); 3225 } 3226 assert(!isPredicated(*MI) && "Can't use flags from predicated instruction"); 3227 CmpInstr.eraseFromParent(); 3228 3229 // Modify the condition code of operands in OperandsToUpdate. 3230 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to 3231 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc. 3232 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++) 3233 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second); 3234 3235 MI->clearRegisterDeads(ARM::CPSR); 3236 3237 return true; 3238 } 3239 3240 bool ARMBaseInstrInfo::shouldSink(const MachineInstr &MI) const { 3241 // Do not sink MI if it might be used to optimize a redundant compare. 3242 // We heuristically only look at the instruction immediately following MI to 3243 // avoid potentially searching the entire basic block. 3244 if (isPredicated(MI)) 3245 return true; 3246 MachineBasicBlock::const_iterator Next = &MI; 3247 ++Next; 3248 Register SrcReg, SrcReg2; 3249 int CmpMask, CmpValue; 3250 bool IsThumb1; 3251 if (Next != MI.getParent()->end() && 3252 analyzeCompare(*Next, SrcReg, SrcReg2, CmpMask, CmpValue) && 3253 isRedundantFlagInstr(&*Next, SrcReg, SrcReg2, CmpValue, &MI, IsThumb1)) 3254 return false; 3255 return true; 3256 } 3257 3258 bool ARMBaseInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, 3259 Register Reg, 3260 MachineRegisterInfo *MRI) const { 3261 // Fold large immediates into add, sub, or, xor. 3262 unsigned DefOpc = DefMI.getOpcode(); 3263 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm) 3264 return false; 3265 if (!DefMI.getOperand(1).isImm()) 3266 // Could be t2MOVi32imm @xx 3267 return false; 3268 3269 if (!MRI->hasOneNonDBGUse(Reg)) 3270 return false; 3271 3272 const MCInstrDesc &DefMCID = DefMI.getDesc(); 3273 if (DefMCID.hasOptionalDef()) { 3274 unsigned NumOps = DefMCID.getNumOperands(); 3275 const MachineOperand &MO = DefMI.getOperand(NumOps - 1); 3276 if (MO.getReg() == ARM::CPSR && !MO.isDead()) 3277 // If DefMI defines CPSR and it is not dead, it's obviously not safe 3278 // to delete DefMI. 3279 return false; 3280 } 3281 3282 const MCInstrDesc &UseMCID = UseMI.getDesc(); 3283 if (UseMCID.hasOptionalDef()) { 3284 unsigned NumOps = UseMCID.getNumOperands(); 3285 if (UseMI.getOperand(NumOps - 1).getReg() == ARM::CPSR) 3286 // If the instruction sets the flag, do not attempt this optimization 3287 // since it may change the semantics of the code. 3288 return false; 3289 } 3290 3291 unsigned UseOpc = UseMI.getOpcode(); 3292 unsigned NewUseOpc = 0; 3293 uint32_t ImmVal = (uint32_t)DefMI.getOperand(1).getImm(); 3294 uint32_t SOImmValV1 = 0, SOImmValV2 = 0; 3295 bool Commute = false; 3296 switch (UseOpc) { 3297 default: return false; 3298 case ARM::SUBrr: 3299 case ARM::ADDrr: 3300 case ARM::ORRrr: 3301 case ARM::EORrr: 3302 case ARM::t2SUBrr: 3303 case ARM::t2ADDrr: 3304 case ARM::t2ORRrr: 3305 case ARM::t2EORrr: { 3306 Commute = UseMI.getOperand(2).getReg() != Reg; 3307 switch (UseOpc) { 3308 default: break; 3309 case ARM::ADDrr: 3310 case ARM::SUBrr: 3311 if (UseOpc == ARM::SUBrr && Commute) 3312 return false; 3313 3314 // ADD/SUB are special because they're essentially the same operation, so 3315 // we can handle a larger range of immediates. 3316 if (ARM_AM::isSOImmTwoPartVal(ImmVal)) 3317 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::ADDri : ARM::SUBri; 3318 else if (ARM_AM::isSOImmTwoPartVal(-ImmVal)) { 3319 ImmVal = -ImmVal; 3320 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::SUBri : ARM::ADDri; 3321 } else 3322 return false; 3323 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal); 3324 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal); 3325 break; 3326 case ARM::ORRrr: 3327 case ARM::EORrr: 3328 if (!ARM_AM::isSOImmTwoPartVal(ImmVal)) 3329 return false; 3330 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal); 3331 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal); 3332 switch (UseOpc) { 3333 default: break; 3334 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break; 3335 case ARM::EORrr: NewUseOpc = ARM::EORri; break; 3336 } 3337 break; 3338 case ARM::t2ADDrr: 3339 case ARM::t2SUBrr: { 3340 if (UseOpc == ARM::t2SUBrr && Commute) 3341 return false; 3342 3343 // ADD/SUB are special because they're essentially the same operation, so 3344 // we can handle a larger range of immediates. 3345 const bool ToSP = DefMI.getOperand(0).getReg() == ARM::SP; 3346 const unsigned t2ADD = ToSP ? ARM::t2ADDspImm : ARM::t2ADDri; 3347 const unsigned t2SUB = ToSP ? ARM::t2SUBspImm : ARM::t2SUBri; 3348 if (ARM_AM::isT2SOImmTwoPartVal(ImmVal)) 3349 NewUseOpc = UseOpc == ARM::t2ADDrr ? t2ADD : t2SUB; 3350 else if (ARM_AM::isT2SOImmTwoPartVal(-ImmVal)) { 3351 ImmVal = -ImmVal; 3352 NewUseOpc = UseOpc == ARM::t2ADDrr ? t2SUB : t2ADD; 3353 } else 3354 return false; 3355 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal); 3356 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal); 3357 break; 3358 } 3359 case ARM::t2ORRrr: 3360 case ARM::t2EORrr: 3361 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal)) 3362 return false; 3363 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal); 3364 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal); 3365 switch (UseOpc) { 3366 default: break; 3367 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break; 3368 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break; 3369 } 3370 break; 3371 } 3372 } 3373 } 3374 3375 unsigned OpIdx = Commute ? 2 : 1; 3376 Register Reg1 = UseMI.getOperand(OpIdx).getReg(); 3377 bool isKill = UseMI.getOperand(OpIdx).isKill(); 3378 const TargetRegisterClass *TRC = MRI->getRegClass(Reg); 3379 Register NewReg = MRI->createVirtualRegister(TRC); 3380 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), get(NewUseOpc), 3381 NewReg) 3382 .addReg(Reg1, getKillRegState(isKill)) 3383 .addImm(SOImmValV1) 3384 .add(predOps(ARMCC::AL)) 3385 .add(condCodeOp()); 3386 UseMI.setDesc(get(NewUseOpc)); 3387 UseMI.getOperand(1).setReg(NewReg); 3388 UseMI.getOperand(1).setIsKill(); 3389 UseMI.getOperand(2).ChangeToImmediate(SOImmValV2); 3390 DefMI.eraseFromParent(); 3391 // FIXME: t2ADDrr should be split, as different rulles apply when writing to SP. 3392 // Just as t2ADDri, that was split to [t2ADDri, t2ADDspImm]. 3393 // Then the below code will not be needed, as the input/output register 3394 // classes will be rgpr or gprSP. 3395 // For now, we fix the UseMI operand explicitly here: 3396 switch(NewUseOpc){ 3397 case ARM::t2ADDspImm: 3398 case ARM::t2SUBspImm: 3399 case ARM::t2ADDri: 3400 case ARM::t2SUBri: 3401 MRI->setRegClass(UseMI.getOperand(0).getReg(), TRC); 3402 } 3403 return true; 3404 } 3405 3406 static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData, 3407 const MachineInstr &MI) { 3408 switch (MI.getOpcode()) { 3409 default: { 3410 const MCInstrDesc &Desc = MI.getDesc(); 3411 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass()); 3412 assert(UOps >= 0 && "bad # UOps"); 3413 return UOps; 3414 } 3415 3416 case ARM::LDRrs: 3417 case ARM::LDRBrs: 3418 case ARM::STRrs: 3419 case ARM::STRBrs: { 3420 unsigned ShOpVal = MI.getOperand(3).getImm(); 3421 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub; 3422 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 3423 if (!isSub && 3424 (ShImm == 0 || 3425 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 3426 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))) 3427 return 1; 3428 return 2; 3429 } 3430 3431 case ARM::LDRH: 3432 case ARM::STRH: { 3433 if (!MI.getOperand(2).getReg()) 3434 return 1; 3435 3436 unsigned ShOpVal = MI.getOperand(3).getImm(); 3437 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub; 3438 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 3439 if (!isSub && 3440 (ShImm == 0 || 3441 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 3442 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))) 3443 return 1; 3444 return 2; 3445 } 3446 3447 case ARM::LDRSB: 3448 case ARM::LDRSH: 3449 return (ARM_AM::getAM3Op(MI.getOperand(3).getImm()) == ARM_AM::sub) ? 3 : 2; 3450 3451 case ARM::LDRSB_POST: 3452 case ARM::LDRSH_POST: { 3453 Register Rt = MI.getOperand(0).getReg(); 3454 Register Rm = MI.getOperand(3).getReg(); 3455 return (Rt == Rm) ? 4 : 3; 3456 } 3457 3458 case ARM::LDR_PRE_REG: 3459 case ARM::LDRB_PRE_REG: { 3460 Register Rt = MI.getOperand(0).getReg(); 3461 Register Rm = MI.getOperand(3).getReg(); 3462 if (Rt == Rm) 3463 return 3; 3464 unsigned ShOpVal = MI.getOperand(4).getImm(); 3465 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub; 3466 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 3467 if (!isSub && 3468 (ShImm == 0 || 3469 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 3470 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))) 3471 return 2; 3472 return 3; 3473 } 3474 3475 case ARM::STR_PRE_REG: 3476 case ARM::STRB_PRE_REG: { 3477 unsigned ShOpVal = MI.getOperand(4).getImm(); 3478 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub; 3479 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 3480 if (!isSub && 3481 (ShImm == 0 || 3482 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 3483 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))) 3484 return 2; 3485 return 3; 3486 } 3487 3488 case ARM::LDRH_PRE: 3489 case ARM::STRH_PRE: { 3490 Register Rt = MI.getOperand(0).getReg(); 3491 Register Rm = MI.getOperand(3).getReg(); 3492 if (!Rm) 3493 return 2; 3494 if (Rt == Rm) 3495 return 3; 3496 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 3 : 2; 3497 } 3498 3499 case ARM::LDR_POST_REG: 3500 case ARM::LDRB_POST_REG: 3501 case ARM::LDRH_POST: { 3502 Register Rt = MI.getOperand(0).getReg(); 3503 Register Rm = MI.getOperand(3).getReg(); 3504 return (Rt == Rm) ? 3 : 2; 3505 } 3506 3507 case ARM::LDR_PRE_IMM: 3508 case ARM::LDRB_PRE_IMM: 3509 case ARM::LDR_POST_IMM: 3510 case ARM::LDRB_POST_IMM: 3511 case ARM::STRB_POST_IMM: 3512 case ARM::STRB_POST_REG: 3513 case ARM::STRB_PRE_IMM: 3514 case ARM::STRH_POST: 3515 case ARM::STR_POST_IMM: 3516 case ARM::STR_POST_REG: 3517 case ARM::STR_PRE_IMM: 3518 return 2; 3519 3520 case ARM::LDRSB_PRE: 3521 case ARM::LDRSH_PRE: { 3522 Register Rm = MI.getOperand(3).getReg(); 3523 if (Rm == 0) 3524 return 3; 3525 Register Rt = MI.getOperand(0).getReg(); 3526 if (Rt == Rm) 3527 return 4; 3528 unsigned ShOpVal = MI.getOperand(4).getImm(); 3529 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub; 3530 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 3531 if (!isSub && 3532 (ShImm == 0 || 3533 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 3534 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))) 3535 return 3; 3536 return 4; 3537 } 3538 3539 case ARM::LDRD: { 3540 Register Rt = MI.getOperand(0).getReg(); 3541 Register Rn = MI.getOperand(2).getReg(); 3542 Register Rm = MI.getOperand(3).getReg(); 3543 if (Rm) 3544 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4 3545 : 3; 3546 return (Rt == Rn) ? 3 : 2; 3547 } 3548 3549 case ARM::STRD: { 3550 Register Rm = MI.getOperand(3).getReg(); 3551 if (Rm) 3552 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4 3553 : 3; 3554 return 2; 3555 } 3556 3557 case ARM::LDRD_POST: 3558 case ARM::t2LDRD_POST: 3559 return 3; 3560 3561 case ARM::STRD_POST: 3562 case ARM::t2STRD_POST: 3563 return 4; 3564 3565 case ARM::LDRD_PRE: { 3566 Register Rt = MI.getOperand(0).getReg(); 3567 Register Rn = MI.getOperand(3).getReg(); 3568 Register Rm = MI.getOperand(4).getReg(); 3569 if (Rm) 3570 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5 3571 : 4; 3572 return (Rt == Rn) ? 4 : 3; 3573 } 3574 3575 case ARM::t2LDRD_PRE: { 3576 Register Rt = MI.getOperand(0).getReg(); 3577 Register Rn = MI.getOperand(3).getReg(); 3578 return (Rt == Rn) ? 4 : 3; 3579 } 3580 3581 case ARM::STRD_PRE: { 3582 Register Rm = MI.getOperand(4).getReg(); 3583 if (Rm) 3584 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5 3585 : 4; 3586 return 3; 3587 } 3588 3589 case ARM::t2STRD_PRE: 3590 return 3; 3591 3592 case ARM::t2LDR_POST: 3593 case ARM::t2LDRB_POST: 3594 case ARM::t2LDRB_PRE: 3595 case ARM::t2LDRSBi12: 3596 case ARM::t2LDRSBi8: 3597 case ARM::t2LDRSBpci: 3598 case ARM::t2LDRSBs: 3599 case ARM::t2LDRH_POST: 3600 case ARM::t2LDRH_PRE: 3601 case ARM::t2LDRSBT: 3602 case ARM::t2LDRSB_POST: 3603 case ARM::t2LDRSB_PRE: 3604 case ARM::t2LDRSH_POST: 3605 case ARM::t2LDRSH_PRE: 3606 case ARM::t2LDRSHi12: 3607 case ARM::t2LDRSHi8: 3608 case ARM::t2LDRSHpci: 3609 case ARM::t2LDRSHs: 3610 return 2; 3611 3612 case ARM::t2LDRDi8: { 3613 Register Rt = MI.getOperand(0).getReg(); 3614 Register Rn = MI.getOperand(2).getReg(); 3615 return (Rt == Rn) ? 3 : 2; 3616 } 3617 3618 case ARM::t2STRB_POST: 3619 case ARM::t2STRB_PRE: 3620 case ARM::t2STRBs: 3621 case ARM::t2STRDi8: 3622 case ARM::t2STRH_POST: 3623 case ARM::t2STRH_PRE: 3624 case ARM::t2STRHs: 3625 case ARM::t2STR_POST: 3626 case ARM::t2STR_PRE: 3627 case ARM::t2STRs: 3628 return 2; 3629 } 3630 } 3631 3632 // Return the number of 32-bit words loaded by LDM or stored by STM. If this 3633 // can't be easily determined return 0 (missing MachineMemOperand). 3634 // 3635 // FIXME: The current MachineInstr design does not support relying on machine 3636 // mem operands to determine the width of a memory access. Instead, we expect 3637 // the target to provide this information based on the instruction opcode and 3638 // operands. However, using MachineMemOperand is the best solution now for 3639 // two reasons: 3640 // 3641 // 1) getNumMicroOps tries to infer LDM memory width from the total number of MI 3642 // operands. This is much more dangerous than using the MachineMemOperand 3643 // sizes because CodeGen passes can insert/remove optional machine operands. In 3644 // fact, it's totally incorrect for preRA passes and appears to be wrong for 3645 // postRA passes as well. 3646 // 3647 // 2) getNumLDMAddresses is only used by the scheduling machine model and any 3648 // machine model that calls this should handle the unknown (zero size) case. 3649 // 3650 // Long term, we should require a target hook that verifies MachineMemOperand 3651 // sizes during MC lowering. That target hook should be local to MC lowering 3652 // because we can't ensure that it is aware of other MI forms. Doing this will 3653 // ensure that MachineMemOperands are correctly propagated through all passes. 3654 unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr &MI) const { 3655 unsigned Size = 0; 3656 for (MachineInstr::mmo_iterator I = MI.memoperands_begin(), 3657 E = MI.memoperands_end(); 3658 I != E; ++I) { 3659 Size += (*I)->getSize(); 3660 } 3661 // FIXME: The scheduler currently can't handle values larger than 16. But 3662 // the values can actually go up to 32 for floating-point load/store 3663 // multiple (VLDMIA etc.). Also, the way this code is reasoning about memory 3664 // operations isn't right; we could end up with "extra" memory operands for 3665 // various reasons, like tail merge merging two memory operations. 3666 return std::min(Size / 4, 16U); 3667 } 3668 3669 static unsigned getNumMicroOpsSingleIssuePlusExtras(unsigned Opc, 3670 unsigned NumRegs) { 3671 unsigned UOps = 1 + NumRegs; // 1 for address computation. 3672 switch (Opc) { 3673 default: 3674 break; 3675 case ARM::VLDMDIA_UPD: 3676 case ARM::VLDMDDB_UPD: 3677 case ARM::VLDMSIA_UPD: 3678 case ARM::VLDMSDB_UPD: 3679 case ARM::VSTMDIA_UPD: 3680 case ARM::VSTMDDB_UPD: 3681 case ARM::VSTMSIA_UPD: 3682 case ARM::VSTMSDB_UPD: 3683 case ARM::LDMIA_UPD: 3684 case ARM::LDMDA_UPD: 3685 case ARM::LDMDB_UPD: 3686 case ARM::LDMIB_UPD: 3687 case ARM::STMIA_UPD: 3688 case ARM::STMDA_UPD: 3689 case ARM::STMDB_UPD: 3690 case ARM::STMIB_UPD: 3691 case ARM::tLDMIA_UPD: 3692 case ARM::tSTMIA_UPD: 3693 case ARM::t2LDMIA_UPD: 3694 case ARM::t2LDMDB_UPD: 3695 case ARM::t2STMIA_UPD: 3696 case ARM::t2STMDB_UPD: 3697 ++UOps; // One for base register writeback. 3698 break; 3699 case ARM::LDMIA_RET: 3700 case ARM::tPOP_RET: 3701 case ARM::t2LDMIA_RET: 3702 UOps += 2; // One for base reg wb, one for write to pc. 3703 break; 3704 } 3705 return UOps; 3706 } 3707 3708 unsigned ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData, 3709 const MachineInstr &MI) const { 3710 if (!ItinData || ItinData->isEmpty()) 3711 return 1; 3712 3713 const MCInstrDesc &Desc = MI.getDesc(); 3714 unsigned Class = Desc.getSchedClass(); 3715 int ItinUOps = ItinData->getNumMicroOps(Class); 3716 if (ItinUOps >= 0) { 3717 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore())) 3718 return getNumMicroOpsSwiftLdSt(ItinData, MI); 3719 3720 return ItinUOps; 3721 } 3722 3723 unsigned Opc = MI.getOpcode(); 3724 switch (Opc) { 3725 default: 3726 llvm_unreachable("Unexpected multi-uops instruction!"); 3727 case ARM::VLDMQIA: 3728 case ARM::VSTMQIA: 3729 return 2; 3730 3731 // The number of uOps for load / store multiple are determined by the number 3732 // registers. 3733 // 3734 // On Cortex-A8, each pair of register loads / stores can be scheduled on the 3735 // same cycle. The scheduling for the first load / store must be done 3736 // separately by assuming the address is not 64-bit aligned. 3737 // 3738 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address 3739 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON 3740 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1. 3741 case ARM::VLDMDIA: 3742 case ARM::VLDMDIA_UPD: 3743 case ARM::VLDMDDB_UPD: 3744 case ARM::VLDMSIA: 3745 case ARM::VLDMSIA_UPD: 3746 case ARM::VLDMSDB_UPD: 3747 case ARM::VSTMDIA: 3748 case ARM::VSTMDIA_UPD: 3749 case ARM::VSTMDDB_UPD: 3750 case ARM::VSTMSIA: 3751 case ARM::VSTMSIA_UPD: 3752 case ARM::VSTMSDB_UPD: { 3753 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands(); 3754 return (NumRegs / 2) + (NumRegs % 2) + 1; 3755 } 3756 3757 case ARM::LDMIA_RET: 3758 case ARM::LDMIA: 3759 case ARM::LDMDA: 3760 case ARM::LDMDB: 3761 case ARM::LDMIB: 3762 case ARM::LDMIA_UPD: 3763 case ARM::LDMDA_UPD: 3764 case ARM::LDMDB_UPD: 3765 case ARM::LDMIB_UPD: 3766 case ARM::STMIA: 3767 case ARM::STMDA: 3768 case ARM::STMDB: 3769 case ARM::STMIB: 3770 case ARM::STMIA_UPD: 3771 case ARM::STMDA_UPD: 3772 case ARM::STMDB_UPD: 3773 case ARM::STMIB_UPD: 3774 case ARM::tLDMIA: 3775 case ARM::tLDMIA_UPD: 3776 case ARM::tSTMIA_UPD: 3777 case ARM::tPOP_RET: 3778 case ARM::tPOP: 3779 case ARM::tPUSH: 3780 case ARM::t2LDMIA_RET: 3781 case ARM::t2LDMIA: 3782 case ARM::t2LDMDB: 3783 case ARM::t2LDMIA_UPD: 3784 case ARM::t2LDMDB_UPD: 3785 case ARM::t2STMIA: 3786 case ARM::t2STMDB: 3787 case ARM::t2STMIA_UPD: 3788 case ARM::t2STMDB_UPD: { 3789 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands() + 1; 3790 switch (Subtarget.getLdStMultipleTiming()) { 3791 case ARMSubtarget::SingleIssuePlusExtras: 3792 return getNumMicroOpsSingleIssuePlusExtras(Opc, NumRegs); 3793 case ARMSubtarget::SingleIssue: 3794 // Assume the worst. 3795 return NumRegs; 3796 case ARMSubtarget::DoubleIssue: { 3797 if (NumRegs < 4) 3798 return 2; 3799 // 4 registers would be issued: 2, 2. 3800 // 5 registers would be issued: 2, 2, 1. 3801 unsigned UOps = (NumRegs / 2); 3802 if (NumRegs % 2) 3803 ++UOps; 3804 return UOps; 3805 } 3806 case ARMSubtarget::DoubleIssueCheckUnalignedAccess: { 3807 unsigned UOps = (NumRegs / 2); 3808 // If there are odd number of registers or if it's not 64-bit aligned, 3809 // then it takes an extra AGU (Address Generation Unit) cycle. 3810 if ((NumRegs % 2) || !MI.hasOneMemOperand() || 3811 (*MI.memoperands_begin())->getAlign() < Align(8)) 3812 ++UOps; 3813 return UOps; 3814 } 3815 } 3816 } 3817 } 3818 llvm_unreachable("Didn't find the number of microops"); 3819 } 3820 3821 int 3822 ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData, 3823 const MCInstrDesc &DefMCID, 3824 unsigned DefClass, 3825 unsigned DefIdx, unsigned DefAlign) const { 3826 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1; 3827 if (RegNo <= 0) 3828 // Def is the address writeback. 3829 return ItinData->getOperandCycle(DefClass, DefIdx); 3830 3831 int DefCycle; 3832 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) { 3833 // (regno / 2) + (regno % 2) + 1 3834 DefCycle = RegNo / 2 + 1; 3835 if (RegNo % 2) 3836 ++DefCycle; 3837 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) { 3838 DefCycle = RegNo; 3839 bool isSLoad = false; 3840 3841 switch (DefMCID.getOpcode()) { 3842 default: break; 3843 case ARM::VLDMSIA: 3844 case ARM::VLDMSIA_UPD: 3845 case ARM::VLDMSDB_UPD: 3846 isSLoad = true; 3847 break; 3848 } 3849 3850 // If there are odd number of 'S' registers or if it's not 64-bit aligned, 3851 // then it takes an extra cycle. 3852 if ((isSLoad && (RegNo % 2)) || DefAlign < 8) 3853 ++DefCycle; 3854 } else { 3855 // Assume the worst. 3856 DefCycle = RegNo + 2; 3857 } 3858 3859 return DefCycle; 3860 } 3861 3862 bool ARMBaseInstrInfo::isLDMBaseRegInList(const MachineInstr &MI) const { 3863 Register BaseReg = MI.getOperand(0).getReg(); 3864 for (unsigned i = 1, sz = MI.getNumOperands(); i < sz; ++i) { 3865 const auto &Op = MI.getOperand(i); 3866 if (Op.isReg() && Op.getReg() == BaseReg) 3867 return true; 3868 } 3869 return false; 3870 } 3871 unsigned 3872 ARMBaseInstrInfo::getLDMVariableDefsSize(const MachineInstr &MI) const { 3873 // ins GPR:$Rn, $p (2xOp), reglist:$regs, variable_ops 3874 // (outs GPR:$wb), (ins GPR:$Rn, $p (2xOp), reglist:$regs, variable_ops) 3875 return MI.getNumOperands() + 1 - MI.getDesc().getNumOperands(); 3876 } 3877 3878 int 3879 ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData, 3880 const MCInstrDesc &DefMCID, 3881 unsigned DefClass, 3882 unsigned DefIdx, unsigned DefAlign) const { 3883 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1; 3884 if (RegNo <= 0) 3885 // Def is the address writeback. 3886 return ItinData->getOperandCycle(DefClass, DefIdx); 3887 3888 int DefCycle; 3889 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) { 3890 // 4 registers would be issued: 1, 2, 1. 3891 // 5 registers would be issued: 1, 2, 2. 3892 DefCycle = RegNo / 2; 3893 if (DefCycle < 1) 3894 DefCycle = 1; 3895 // Result latency is issue cycle + 2: E2. 3896 DefCycle += 2; 3897 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) { 3898 DefCycle = (RegNo / 2); 3899 // If there are odd number of registers or if it's not 64-bit aligned, 3900 // then it takes an extra AGU (Address Generation Unit) cycle. 3901 if ((RegNo % 2) || DefAlign < 8) 3902 ++DefCycle; 3903 // Result latency is AGU cycles + 2. 3904 DefCycle += 2; 3905 } else { 3906 // Assume the worst. 3907 DefCycle = RegNo + 2; 3908 } 3909 3910 return DefCycle; 3911 } 3912 3913 int 3914 ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData, 3915 const MCInstrDesc &UseMCID, 3916 unsigned UseClass, 3917 unsigned UseIdx, unsigned UseAlign) const { 3918 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1; 3919 if (RegNo <= 0) 3920 return ItinData->getOperandCycle(UseClass, UseIdx); 3921 3922 int UseCycle; 3923 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) { 3924 // (regno / 2) + (regno % 2) + 1 3925 UseCycle = RegNo / 2 + 1; 3926 if (RegNo % 2) 3927 ++UseCycle; 3928 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) { 3929 UseCycle = RegNo; 3930 bool isSStore = false; 3931 3932 switch (UseMCID.getOpcode()) { 3933 default: break; 3934 case ARM::VSTMSIA: 3935 case ARM::VSTMSIA_UPD: 3936 case ARM::VSTMSDB_UPD: 3937 isSStore = true; 3938 break; 3939 } 3940 3941 // If there are odd number of 'S' registers or if it's not 64-bit aligned, 3942 // then it takes an extra cycle. 3943 if ((isSStore && (RegNo % 2)) || UseAlign < 8) 3944 ++UseCycle; 3945 } else { 3946 // Assume the worst. 3947 UseCycle = RegNo + 2; 3948 } 3949 3950 return UseCycle; 3951 } 3952 3953 int 3954 ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData, 3955 const MCInstrDesc &UseMCID, 3956 unsigned UseClass, 3957 unsigned UseIdx, unsigned UseAlign) const { 3958 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1; 3959 if (RegNo <= 0) 3960 return ItinData->getOperandCycle(UseClass, UseIdx); 3961 3962 int UseCycle; 3963 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) { 3964 UseCycle = RegNo / 2; 3965 if (UseCycle < 2) 3966 UseCycle = 2; 3967 // Read in E3. 3968 UseCycle += 2; 3969 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) { 3970 UseCycle = (RegNo / 2); 3971 // If there are odd number of registers or if it's not 64-bit aligned, 3972 // then it takes an extra AGU (Address Generation Unit) cycle. 3973 if ((RegNo % 2) || UseAlign < 8) 3974 ++UseCycle; 3975 } else { 3976 // Assume the worst. 3977 UseCycle = 1; 3978 } 3979 return UseCycle; 3980 } 3981 3982 int 3983 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, 3984 const MCInstrDesc &DefMCID, 3985 unsigned DefIdx, unsigned DefAlign, 3986 const MCInstrDesc &UseMCID, 3987 unsigned UseIdx, unsigned UseAlign) const { 3988 unsigned DefClass = DefMCID.getSchedClass(); 3989 unsigned UseClass = UseMCID.getSchedClass(); 3990 3991 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands()) 3992 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx); 3993 3994 // This may be a def / use of a variable_ops instruction, the operand 3995 // latency might be determinable dynamically. Let the target try to 3996 // figure it out. 3997 int DefCycle = -1; 3998 bool LdmBypass = false; 3999 switch (DefMCID.getOpcode()) { 4000 default: 4001 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx); 4002 break; 4003 4004 case ARM::VLDMDIA: 4005 case ARM::VLDMDIA_UPD: 4006 case ARM::VLDMDDB_UPD: 4007 case ARM::VLDMSIA: 4008 case ARM::VLDMSIA_UPD: 4009 case ARM::VLDMSDB_UPD: 4010 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign); 4011 break; 4012 4013 case ARM::LDMIA_RET: 4014 case ARM::LDMIA: 4015 case ARM::LDMDA: 4016 case ARM::LDMDB: 4017 case ARM::LDMIB: 4018 case ARM::LDMIA_UPD: 4019 case ARM::LDMDA_UPD: 4020 case ARM::LDMDB_UPD: 4021 case ARM::LDMIB_UPD: 4022 case ARM::tLDMIA: 4023 case ARM::tLDMIA_UPD: 4024 case ARM::tPUSH: 4025 case ARM::t2LDMIA_RET: 4026 case ARM::t2LDMIA: 4027 case ARM::t2LDMDB: 4028 case ARM::t2LDMIA_UPD: 4029 case ARM::t2LDMDB_UPD: 4030 LdmBypass = true; 4031 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign); 4032 break; 4033 } 4034 4035 if (DefCycle == -1) 4036 // We can't seem to determine the result latency of the def, assume it's 2. 4037 DefCycle = 2; 4038 4039 int UseCycle = -1; 4040 switch (UseMCID.getOpcode()) { 4041 default: 4042 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx); 4043 break; 4044 4045 case ARM::VSTMDIA: 4046 case ARM::VSTMDIA_UPD: 4047 case ARM::VSTMDDB_UPD: 4048 case ARM::VSTMSIA: 4049 case ARM::VSTMSIA_UPD: 4050 case ARM::VSTMSDB_UPD: 4051 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign); 4052 break; 4053 4054 case ARM::STMIA: 4055 case ARM::STMDA: 4056 case ARM::STMDB: 4057 case ARM::STMIB: 4058 case ARM::STMIA_UPD: 4059 case ARM::STMDA_UPD: 4060 case ARM::STMDB_UPD: 4061 case ARM::STMIB_UPD: 4062 case ARM::tSTMIA_UPD: 4063 case ARM::tPOP_RET: 4064 case ARM::tPOP: 4065 case ARM::t2STMIA: 4066 case ARM::t2STMDB: 4067 case ARM::t2STMIA_UPD: 4068 case ARM::t2STMDB_UPD: 4069 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign); 4070 break; 4071 } 4072 4073 if (UseCycle == -1) 4074 // Assume it's read in the first stage. 4075 UseCycle = 1; 4076 4077 UseCycle = DefCycle - UseCycle + 1; 4078 if (UseCycle > 0) { 4079 if (LdmBypass) { 4080 // It's a variable_ops instruction so we can't use DefIdx here. Just use 4081 // first def operand. 4082 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1, 4083 UseClass, UseIdx)) 4084 --UseCycle; 4085 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx, 4086 UseClass, UseIdx)) { 4087 --UseCycle; 4088 } 4089 } 4090 4091 return UseCycle; 4092 } 4093 4094 static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI, 4095 const MachineInstr *MI, unsigned Reg, 4096 unsigned &DefIdx, unsigned &Dist) { 4097 Dist = 0; 4098 4099 MachineBasicBlock::const_iterator I = MI; ++I; 4100 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator()); 4101 assert(II->isInsideBundle() && "Empty bundle?"); 4102 4103 int Idx = -1; 4104 while (II->isInsideBundle()) { 4105 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI); 4106 if (Idx != -1) 4107 break; 4108 --II; 4109 ++Dist; 4110 } 4111 4112 assert(Idx != -1 && "Cannot find bundled definition!"); 4113 DefIdx = Idx; 4114 return &*II; 4115 } 4116 4117 static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI, 4118 const MachineInstr &MI, unsigned Reg, 4119 unsigned &UseIdx, unsigned &Dist) { 4120 Dist = 0; 4121 4122 MachineBasicBlock::const_instr_iterator II = ++MI.getIterator(); 4123 assert(II->isInsideBundle() && "Empty bundle?"); 4124 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end(); 4125 4126 // FIXME: This doesn't properly handle multiple uses. 4127 int Idx = -1; 4128 while (II != E && II->isInsideBundle()) { 4129 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI); 4130 if (Idx != -1) 4131 break; 4132 if (II->getOpcode() != ARM::t2IT) 4133 ++Dist; 4134 ++II; 4135 } 4136 4137 if (Idx == -1) { 4138 Dist = 0; 4139 return nullptr; 4140 } 4141 4142 UseIdx = Idx; 4143 return &*II; 4144 } 4145 4146 /// Return the number of cycles to add to (or subtract from) the static 4147 /// itinerary based on the def opcode and alignment. The caller will ensure that 4148 /// adjusted latency is at least one cycle. 4149 static int adjustDefLatency(const ARMSubtarget &Subtarget, 4150 const MachineInstr &DefMI, 4151 const MCInstrDesc &DefMCID, unsigned DefAlign) { 4152 int Adjust = 0; 4153 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) { 4154 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2] 4155 // variants are one cycle cheaper. 4156 switch (DefMCID.getOpcode()) { 4157 default: break; 4158 case ARM::LDRrs: 4159 case ARM::LDRBrs: { 4160 unsigned ShOpVal = DefMI.getOperand(3).getImm(); 4161 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 4162 if (ShImm == 0 || 4163 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)) 4164 --Adjust; 4165 break; 4166 } 4167 case ARM::t2LDRs: 4168 case ARM::t2LDRBs: 4169 case ARM::t2LDRHs: 4170 case ARM::t2LDRSHs: { 4171 // Thumb2 mode: lsl only. 4172 unsigned ShAmt = DefMI.getOperand(3).getImm(); 4173 if (ShAmt == 0 || ShAmt == 2) 4174 --Adjust; 4175 break; 4176 } 4177 } 4178 } else if (Subtarget.isSwift()) { 4179 // FIXME: Properly handle all of the latency adjustments for address 4180 // writeback. 4181 switch (DefMCID.getOpcode()) { 4182 default: break; 4183 case ARM::LDRrs: 4184 case ARM::LDRBrs: { 4185 unsigned ShOpVal = DefMI.getOperand(3).getImm(); 4186 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub; 4187 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 4188 if (!isSub && 4189 (ShImm == 0 || 4190 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 4191 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))) 4192 Adjust -= 2; 4193 else if (!isSub && 4194 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr) 4195 --Adjust; 4196 break; 4197 } 4198 case ARM::t2LDRs: 4199 case ARM::t2LDRBs: 4200 case ARM::t2LDRHs: 4201 case ARM::t2LDRSHs: { 4202 // Thumb2 mode: lsl only. 4203 unsigned ShAmt = DefMI.getOperand(3).getImm(); 4204 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3) 4205 Adjust -= 2; 4206 break; 4207 } 4208 } 4209 } 4210 4211 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment()) { 4212 switch (DefMCID.getOpcode()) { 4213 default: break; 4214 case ARM::VLD1q8: 4215 case ARM::VLD1q16: 4216 case ARM::VLD1q32: 4217 case ARM::VLD1q64: 4218 case ARM::VLD1q8wb_fixed: 4219 case ARM::VLD1q16wb_fixed: 4220 case ARM::VLD1q32wb_fixed: 4221 case ARM::VLD1q64wb_fixed: 4222 case ARM::VLD1q8wb_register: 4223 case ARM::VLD1q16wb_register: 4224 case ARM::VLD1q32wb_register: 4225 case ARM::VLD1q64wb_register: 4226 case ARM::VLD2d8: 4227 case ARM::VLD2d16: 4228 case ARM::VLD2d32: 4229 case ARM::VLD2q8: 4230 case ARM::VLD2q16: 4231 case ARM::VLD2q32: 4232 case ARM::VLD2d8wb_fixed: 4233 case ARM::VLD2d16wb_fixed: 4234 case ARM::VLD2d32wb_fixed: 4235 case ARM::VLD2q8wb_fixed: 4236 case ARM::VLD2q16wb_fixed: 4237 case ARM::VLD2q32wb_fixed: 4238 case ARM::VLD2d8wb_register: 4239 case ARM::VLD2d16wb_register: 4240 case ARM::VLD2d32wb_register: 4241 case ARM::VLD2q8wb_register: 4242 case ARM::VLD2q16wb_register: 4243 case ARM::VLD2q32wb_register: 4244 case ARM::VLD3d8: 4245 case ARM::VLD3d16: 4246 case ARM::VLD3d32: 4247 case ARM::VLD1d64T: 4248 case ARM::VLD3d8_UPD: 4249 case ARM::VLD3d16_UPD: 4250 case ARM::VLD3d32_UPD: 4251 case ARM::VLD1d64Twb_fixed: 4252 case ARM::VLD1d64Twb_register: 4253 case ARM::VLD3q8_UPD: 4254 case ARM::VLD3q16_UPD: 4255 case ARM::VLD3q32_UPD: 4256 case ARM::VLD4d8: 4257 case ARM::VLD4d16: 4258 case ARM::VLD4d32: 4259 case ARM::VLD1d64Q: 4260 case ARM::VLD4d8_UPD: 4261 case ARM::VLD4d16_UPD: 4262 case ARM::VLD4d32_UPD: 4263 case ARM::VLD1d64Qwb_fixed: 4264 case ARM::VLD1d64Qwb_register: 4265 case ARM::VLD4q8_UPD: 4266 case ARM::VLD4q16_UPD: 4267 case ARM::VLD4q32_UPD: 4268 case ARM::VLD1DUPq8: 4269 case ARM::VLD1DUPq16: 4270 case ARM::VLD1DUPq32: 4271 case ARM::VLD1DUPq8wb_fixed: 4272 case ARM::VLD1DUPq16wb_fixed: 4273 case ARM::VLD1DUPq32wb_fixed: 4274 case ARM::VLD1DUPq8wb_register: 4275 case ARM::VLD1DUPq16wb_register: 4276 case ARM::VLD1DUPq32wb_register: 4277 case ARM::VLD2DUPd8: 4278 case ARM::VLD2DUPd16: 4279 case ARM::VLD2DUPd32: 4280 case ARM::VLD2DUPd8wb_fixed: 4281 case ARM::VLD2DUPd16wb_fixed: 4282 case ARM::VLD2DUPd32wb_fixed: 4283 case ARM::VLD2DUPd8wb_register: 4284 case ARM::VLD2DUPd16wb_register: 4285 case ARM::VLD2DUPd32wb_register: 4286 case ARM::VLD4DUPd8: 4287 case ARM::VLD4DUPd16: 4288 case ARM::VLD4DUPd32: 4289 case ARM::VLD4DUPd8_UPD: 4290 case ARM::VLD4DUPd16_UPD: 4291 case ARM::VLD4DUPd32_UPD: 4292 case ARM::VLD1LNd8: 4293 case ARM::VLD1LNd16: 4294 case ARM::VLD1LNd32: 4295 case ARM::VLD1LNd8_UPD: 4296 case ARM::VLD1LNd16_UPD: 4297 case ARM::VLD1LNd32_UPD: 4298 case ARM::VLD2LNd8: 4299 case ARM::VLD2LNd16: 4300 case ARM::VLD2LNd32: 4301 case ARM::VLD2LNq16: 4302 case ARM::VLD2LNq32: 4303 case ARM::VLD2LNd8_UPD: 4304 case ARM::VLD2LNd16_UPD: 4305 case ARM::VLD2LNd32_UPD: 4306 case ARM::VLD2LNq16_UPD: 4307 case ARM::VLD2LNq32_UPD: 4308 case ARM::VLD4LNd8: 4309 case ARM::VLD4LNd16: 4310 case ARM::VLD4LNd32: 4311 case ARM::VLD4LNq16: 4312 case ARM::VLD4LNq32: 4313 case ARM::VLD4LNd8_UPD: 4314 case ARM::VLD4LNd16_UPD: 4315 case ARM::VLD4LNd32_UPD: 4316 case ARM::VLD4LNq16_UPD: 4317 case ARM::VLD4LNq32_UPD: 4318 // If the address is not 64-bit aligned, the latencies of these 4319 // instructions increases by one. 4320 ++Adjust; 4321 break; 4322 } 4323 } 4324 return Adjust; 4325 } 4326 4327 int ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, 4328 const MachineInstr &DefMI, 4329 unsigned DefIdx, 4330 const MachineInstr &UseMI, 4331 unsigned UseIdx) const { 4332 // No operand latency. The caller may fall back to getInstrLatency. 4333 if (!ItinData || ItinData->isEmpty()) 4334 return -1; 4335 4336 const MachineOperand &DefMO = DefMI.getOperand(DefIdx); 4337 Register Reg = DefMO.getReg(); 4338 4339 const MachineInstr *ResolvedDefMI = &DefMI; 4340 unsigned DefAdj = 0; 4341 if (DefMI.isBundle()) 4342 ResolvedDefMI = 4343 getBundledDefMI(&getRegisterInfo(), &DefMI, Reg, DefIdx, DefAdj); 4344 if (ResolvedDefMI->isCopyLike() || ResolvedDefMI->isInsertSubreg() || 4345 ResolvedDefMI->isRegSequence() || ResolvedDefMI->isImplicitDef()) { 4346 return 1; 4347 } 4348 4349 const MachineInstr *ResolvedUseMI = &UseMI; 4350 unsigned UseAdj = 0; 4351 if (UseMI.isBundle()) { 4352 ResolvedUseMI = 4353 getBundledUseMI(&getRegisterInfo(), UseMI, Reg, UseIdx, UseAdj); 4354 if (!ResolvedUseMI) 4355 return -1; 4356 } 4357 4358 return getOperandLatencyImpl( 4359 ItinData, *ResolvedDefMI, DefIdx, ResolvedDefMI->getDesc(), DefAdj, DefMO, 4360 Reg, *ResolvedUseMI, UseIdx, ResolvedUseMI->getDesc(), UseAdj); 4361 } 4362 4363 int ARMBaseInstrInfo::getOperandLatencyImpl( 4364 const InstrItineraryData *ItinData, const MachineInstr &DefMI, 4365 unsigned DefIdx, const MCInstrDesc &DefMCID, unsigned DefAdj, 4366 const MachineOperand &DefMO, unsigned Reg, const MachineInstr &UseMI, 4367 unsigned UseIdx, const MCInstrDesc &UseMCID, unsigned UseAdj) const { 4368 if (Reg == ARM::CPSR) { 4369 if (DefMI.getOpcode() == ARM::FMSTAT) { 4370 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?) 4371 return Subtarget.isLikeA9() ? 1 : 20; 4372 } 4373 4374 // CPSR set and branch can be paired in the same cycle. 4375 if (UseMI.isBranch()) 4376 return 0; 4377 4378 // Otherwise it takes the instruction latency (generally one). 4379 unsigned Latency = getInstrLatency(ItinData, DefMI); 4380 4381 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to 4382 // its uses. Instructions which are otherwise scheduled between them may 4383 // incur a code size penalty (not able to use the CPSR setting 16-bit 4384 // instructions). 4385 if (Latency > 0 && Subtarget.isThumb2()) { 4386 const MachineFunction *MF = DefMI.getParent()->getParent(); 4387 // FIXME: Use Function::hasOptSize(). 4388 if (MF->getFunction().hasFnAttribute(Attribute::OptimizeForSize)) 4389 --Latency; 4390 } 4391 return Latency; 4392 } 4393 4394 if (DefMO.isImplicit() || UseMI.getOperand(UseIdx).isImplicit()) 4395 return -1; 4396 4397 unsigned DefAlign = DefMI.hasOneMemOperand() 4398 ? (*DefMI.memoperands_begin())->getAlign().value() 4399 : 0; 4400 unsigned UseAlign = UseMI.hasOneMemOperand() 4401 ? (*UseMI.memoperands_begin())->getAlign().value() 4402 : 0; 4403 4404 // Get the itinerary's latency if possible, and handle variable_ops. 4405 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign, UseMCID, 4406 UseIdx, UseAlign); 4407 // Unable to find operand latency. The caller may resort to getInstrLatency. 4408 if (Latency < 0) 4409 return Latency; 4410 4411 // Adjust for IT block position. 4412 int Adj = DefAdj + UseAdj; 4413 4414 // Adjust for dynamic def-side opcode variants not captured by the itinerary. 4415 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign); 4416 if (Adj >= 0 || (int)Latency > -Adj) { 4417 return Latency + Adj; 4418 } 4419 // Return the itinerary latency, which may be zero but not less than zero. 4420 return Latency; 4421 } 4422 4423 int 4424 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, 4425 SDNode *DefNode, unsigned DefIdx, 4426 SDNode *UseNode, unsigned UseIdx) const { 4427 if (!DefNode->isMachineOpcode()) 4428 return 1; 4429 4430 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode()); 4431 4432 if (isZeroCost(DefMCID.Opcode)) 4433 return 0; 4434 4435 if (!ItinData || ItinData->isEmpty()) 4436 return DefMCID.mayLoad() ? 3 : 1; 4437 4438 if (!UseNode->isMachineOpcode()) { 4439 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx); 4440 int Adj = Subtarget.getPreISelOperandLatencyAdjustment(); 4441 int Threshold = 1 + Adj; 4442 return Latency <= Threshold ? 1 : Latency - Adj; 4443 } 4444 4445 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode()); 4446 auto *DefMN = cast<MachineSDNode>(DefNode); 4447 unsigned DefAlign = !DefMN->memoperands_empty() 4448 ? (*DefMN->memoperands_begin())->getAlign().value() 4449 : 0; 4450 auto *UseMN = cast<MachineSDNode>(UseNode); 4451 unsigned UseAlign = !UseMN->memoperands_empty() 4452 ? (*UseMN->memoperands_begin())->getAlign().value() 4453 : 0; 4454 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign, 4455 UseMCID, UseIdx, UseAlign); 4456 4457 if (Latency > 1 && 4458 (Subtarget.isCortexA8() || Subtarget.isLikeA9() || 4459 Subtarget.isCortexA7())) { 4460 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2] 4461 // variants are one cycle cheaper. 4462 switch (DefMCID.getOpcode()) { 4463 default: break; 4464 case ARM::LDRrs: 4465 case ARM::LDRBrs: { 4466 unsigned ShOpVal = 4467 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue(); 4468 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 4469 if (ShImm == 0 || 4470 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)) 4471 --Latency; 4472 break; 4473 } 4474 case ARM::t2LDRs: 4475 case ARM::t2LDRBs: 4476 case ARM::t2LDRHs: 4477 case ARM::t2LDRSHs: { 4478 // Thumb2 mode: lsl only. 4479 unsigned ShAmt = 4480 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue(); 4481 if (ShAmt == 0 || ShAmt == 2) 4482 --Latency; 4483 break; 4484 } 4485 } 4486 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) { 4487 // FIXME: Properly handle all of the latency adjustments for address 4488 // writeback. 4489 switch (DefMCID.getOpcode()) { 4490 default: break; 4491 case ARM::LDRrs: 4492 case ARM::LDRBrs: { 4493 unsigned ShOpVal = 4494 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue(); 4495 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal); 4496 if (ShImm == 0 || 4497 ((ShImm == 1 || ShImm == 2 || ShImm == 3) && 4498 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)) 4499 Latency -= 2; 4500 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr) 4501 --Latency; 4502 break; 4503 } 4504 case ARM::t2LDRs: 4505 case ARM::t2LDRBs: 4506 case ARM::t2LDRHs: 4507 case ARM::t2LDRSHs: 4508 // Thumb2 mode: lsl 0-3 only. 4509 Latency -= 2; 4510 break; 4511 } 4512 } 4513 4514 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment()) 4515 switch (DefMCID.getOpcode()) { 4516 default: break; 4517 case ARM::VLD1q8: 4518 case ARM::VLD1q16: 4519 case ARM::VLD1q32: 4520 case ARM::VLD1q64: 4521 case ARM::VLD1q8wb_register: 4522 case ARM::VLD1q16wb_register: 4523 case ARM::VLD1q32wb_register: 4524 case ARM::VLD1q64wb_register: 4525 case ARM::VLD1q8wb_fixed: 4526 case ARM::VLD1q16wb_fixed: 4527 case ARM::VLD1q32wb_fixed: 4528 case ARM::VLD1q64wb_fixed: 4529 case ARM::VLD2d8: 4530 case ARM::VLD2d16: 4531 case ARM::VLD2d32: 4532 case ARM::VLD2q8Pseudo: 4533 case ARM::VLD2q16Pseudo: 4534 case ARM::VLD2q32Pseudo: 4535 case ARM::VLD2d8wb_fixed: 4536 case ARM::VLD2d16wb_fixed: 4537 case ARM::VLD2d32wb_fixed: 4538 case ARM::VLD2q8PseudoWB_fixed: 4539 case ARM::VLD2q16PseudoWB_fixed: 4540 case ARM::VLD2q32PseudoWB_fixed: 4541 case ARM::VLD2d8wb_register: 4542 case ARM::VLD2d16wb_register: 4543 case ARM::VLD2d32wb_register: 4544 case ARM::VLD2q8PseudoWB_register: 4545 case ARM::VLD2q16PseudoWB_register: 4546 case ARM::VLD2q32PseudoWB_register: 4547 case ARM::VLD3d8Pseudo: 4548 case ARM::VLD3d16Pseudo: 4549 case ARM::VLD3d32Pseudo: 4550 case ARM::VLD1d8TPseudo: 4551 case ARM::VLD1d16TPseudo: 4552 case ARM::VLD1d32TPseudo: 4553 case ARM::VLD1d64TPseudo: 4554 case ARM::VLD1d64TPseudoWB_fixed: 4555 case ARM::VLD1d64TPseudoWB_register: 4556 case ARM::VLD3d8Pseudo_UPD: 4557 case ARM::VLD3d16Pseudo_UPD: 4558 case ARM::VLD3d32Pseudo_UPD: 4559 case ARM::VLD3q8Pseudo_UPD: 4560 case ARM::VLD3q16Pseudo_UPD: 4561 case ARM::VLD3q32Pseudo_UPD: 4562 case ARM::VLD3q8oddPseudo: 4563 case ARM::VLD3q16oddPseudo: 4564 case ARM::VLD3q32oddPseudo: 4565 case ARM::VLD3q8oddPseudo_UPD: 4566 case ARM::VLD3q16oddPseudo_UPD: 4567 case ARM::VLD3q32oddPseudo_UPD: 4568 case ARM::VLD4d8Pseudo: 4569 case ARM::VLD4d16Pseudo: 4570 case ARM::VLD4d32Pseudo: 4571 case ARM::VLD1d8QPseudo: 4572 case ARM::VLD1d16QPseudo: 4573 case ARM::VLD1d32QPseudo: 4574 case ARM::VLD1d64QPseudo: 4575 case ARM::VLD1d64QPseudoWB_fixed: 4576 case ARM::VLD1d64QPseudoWB_register: 4577 case ARM::VLD1q8HighQPseudo: 4578 case ARM::VLD1q8LowQPseudo_UPD: 4579 case ARM::VLD1q8HighTPseudo: 4580 case ARM::VLD1q8LowTPseudo_UPD: 4581 case ARM::VLD1q16HighQPseudo: 4582 case ARM::VLD1q16LowQPseudo_UPD: 4583 case ARM::VLD1q16HighTPseudo: 4584 case ARM::VLD1q16LowTPseudo_UPD: 4585 case ARM::VLD1q32HighQPseudo: 4586 case ARM::VLD1q32LowQPseudo_UPD: 4587 case ARM::VLD1q32HighTPseudo: 4588 case ARM::VLD1q32LowTPseudo_UPD: 4589 case ARM::VLD1q64HighQPseudo: 4590 case ARM::VLD1q64LowQPseudo_UPD: 4591 case ARM::VLD1q64HighTPseudo: 4592 case ARM::VLD1q64LowTPseudo_UPD: 4593 case ARM::VLD4d8Pseudo_UPD: 4594 case ARM::VLD4d16Pseudo_UPD: 4595 case ARM::VLD4d32Pseudo_UPD: 4596 case ARM::VLD4q8Pseudo_UPD: 4597 case ARM::VLD4q16Pseudo_UPD: 4598 case ARM::VLD4q32Pseudo_UPD: 4599 case ARM::VLD4q8oddPseudo: 4600 case ARM::VLD4q16oddPseudo: 4601 case ARM::VLD4q32oddPseudo: 4602 case ARM::VLD4q8oddPseudo_UPD: 4603 case ARM::VLD4q16oddPseudo_UPD: 4604 case ARM::VLD4q32oddPseudo_UPD: 4605 case ARM::VLD1DUPq8: 4606 case ARM::VLD1DUPq16: 4607 case ARM::VLD1DUPq32: 4608 case ARM::VLD1DUPq8wb_fixed: 4609 case ARM::VLD1DUPq16wb_fixed: 4610 case ARM::VLD1DUPq32wb_fixed: 4611 case ARM::VLD1DUPq8wb_register: 4612 case ARM::VLD1DUPq16wb_register: 4613 case ARM::VLD1DUPq32wb_register: 4614 case ARM::VLD2DUPd8: 4615 case ARM::VLD2DUPd16: 4616 case ARM::VLD2DUPd32: 4617 case ARM::VLD2DUPd8wb_fixed: 4618 case ARM::VLD2DUPd16wb_fixed: 4619 case ARM::VLD2DUPd32wb_fixed: 4620 case ARM::VLD2DUPd8wb_register: 4621 case ARM::VLD2DUPd16wb_register: 4622 case ARM::VLD2DUPd32wb_register: 4623 case ARM::VLD2DUPq8EvenPseudo: 4624 case ARM::VLD2DUPq8OddPseudo: 4625 case ARM::VLD2DUPq16EvenPseudo: 4626 case ARM::VLD2DUPq16OddPseudo: 4627 case ARM::VLD2DUPq32EvenPseudo: 4628 case ARM::VLD2DUPq32OddPseudo: 4629 case ARM::VLD3DUPq8EvenPseudo: 4630 case ARM::VLD3DUPq8OddPseudo: 4631 case ARM::VLD3DUPq16EvenPseudo: 4632 case ARM::VLD3DUPq16OddPseudo: 4633 case ARM::VLD3DUPq32EvenPseudo: 4634 case ARM::VLD3DUPq32OddPseudo: 4635 case ARM::VLD4DUPd8Pseudo: 4636 case ARM::VLD4DUPd16Pseudo: 4637 case ARM::VLD4DUPd32Pseudo: 4638 case ARM::VLD4DUPd8Pseudo_UPD: 4639 case ARM::VLD4DUPd16Pseudo_UPD: 4640 case ARM::VLD4DUPd32Pseudo_UPD: 4641 case ARM::VLD4DUPq8EvenPseudo: 4642 case ARM::VLD4DUPq8OddPseudo: 4643 case ARM::VLD4DUPq16EvenPseudo: 4644 case ARM::VLD4DUPq16OddPseudo: 4645 case ARM::VLD4DUPq32EvenPseudo: 4646 case ARM::VLD4DUPq32OddPseudo: 4647 case ARM::VLD1LNq8Pseudo: 4648 case ARM::VLD1LNq16Pseudo: 4649 case ARM::VLD1LNq32Pseudo: 4650 case ARM::VLD1LNq8Pseudo_UPD: 4651 case ARM::VLD1LNq16Pseudo_UPD: 4652 case ARM::VLD1LNq32Pseudo_UPD: 4653 case ARM::VLD2LNd8Pseudo: 4654 case ARM::VLD2LNd16Pseudo: 4655 case ARM::VLD2LNd32Pseudo: 4656 case ARM::VLD2LNq16Pseudo: 4657 case ARM::VLD2LNq32Pseudo: 4658 case ARM::VLD2LNd8Pseudo_UPD: 4659 case ARM::VLD2LNd16Pseudo_UPD: 4660 case ARM::VLD2LNd32Pseudo_UPD: 4661 case ARM::VLD2LNq16Pseudo_UPD: 4662 case ARM::VLD2LNq32Pseudo_UPD: 4663 case ARM::VLD4LNd8Pseudo: 4664 case ARM::VLD4LNd16Pseudo: 4665 case ARM::VLD4LNd32Pseudo: 4666 case ARM::VLD4LNq16Pseudo: 4667 case ARM::VLD4LNq32Pseudo: 4668 case ARM::VLD4LNd8Pseudo_UPD: 4669 case ARM::VLD4LNd16Pseudo_UPD: 4670 case ARM::VLD4LNd32Pseudo_UPD: 4671 case ARM::VLD4LNq16Pseudo_UPD: 4672 case ARM::VLD4LNq32Pseudo_UPD: 4673 // If the address is not 64-bit aligned, the latencies of these 4674 // instructions increases by one. 4675 ++Latency; 4676 break; 4677 } 4678 4679 return Latency; 4680 } 4681 4682 unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr &MI) const { 4683 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() || 4684 MI.isImplicitDef()) 4685 return 0; 4686 4687 if (MI.isBundle()) 4688 return 0; 4689 4690 const MCInstrDesc &MCID = MI.getDesc(); 4691 4692 if (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) && 4693 !Subtarget.cheapPredicableCPSRDef())) { 4694 // When predicated, CPSR is an additional source operand for CPSR updating 4695 // instructions, this apparently increases their latencies. 4696 return 1; 4697 } 4698 return 0; 4699 } 4700 4701 unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, 4702 const MachineInstr &MI, 4703 unsigned *PredCost) const { 4704 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() || 4705 MI.isImplicitDef()) 4706 return 1; 4707 4708 // An instruction scheduler typically runs on unbundled instructions, however 4709 // other passes may query the latency of a bundled instruction. 4710 if (MI.isBundle()) { 4711 unsigned Latency = 0; 4712 MachineBasicBlock::const_instr_iterator I = MI.getIterator(); 4713 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end(); 4714 while (++I != E && I->isInsideBundle()) { 4715 if (I->getOpcode() != ARM::t2IT) 4716 Latency += getInstrLatency(ItinData, *I, PredCost); 4717 } 4718 return Latency; 4719 } 4720 4721 const MCInstrDesc &MCID = MI.getDesc(); 4722 if (PredCost && (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) && 4723 !Subtarget.cheapPredicableCPSRDef()))) { 4724 // When predicated, CPSR is an additional source operand for CPSR updating 4725 // instructions, this apparently increases their latencies. 4726 *PredCost = 1; 4727 } 4728 // Be sure to call getStageLatency for an empty itinerary in case it has a 4729 // valid MinLatency property. 4730 if (!ItinData) 4731 return MI.mayLoad() ? 3 : 1; 4732 4733 unsigned Class = MCID.getSchedClass(); 4734 4735 // For instructions with variable uops, use uops as latency. 4736 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0) 4737 return getNumMicroOps(ItinData, MI); 4738 4739 // For the common case, fall back on the itinerary's latency. 4740 unsigned Latency = ItinData->getStageLatency(Class); 4741 4742 // Adjust for dynamic def-side opcode variants not captured by the itinerary. 4743 unsigned DefAlign = 4744 MI.hasOneMemOperand() ? (*MI.memoperands_begin())->getAlign().value() : 0; 4745 int Adj = adjustDefLatency(Subtarget, MI, MCID, DefAlign); 4746 if (Adj >= 0 || (int)Latency > -Adj) { 4747 return Latency + Adj; 4748 } 4749 return Latency; 4750 } 4751 4752 int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, 4753 SDNode *Node) const { 4754 if (!Node->isMachineOpcode()) 4755 return 1; 4756 4757 if (!ItinData || ItinData->isEmpty()) 4758 return 1; 4759 4760 unsigned Opcode = Node->getMachineOpcode(); 4761 switch (Opcode) { 4762 default: 4763 return ItinData->getStageLatency(get(Opcode).getSchedClass()); 4764 case ARM::VLDMQIA: 4765 case ARM::VSTMQIA: 4766 return 2; 4767 } 4768 } 4769 4770 bool ARMBaseInstrInfo::hasHighOperandLatency(const TargetSchedModel &SchedModel, 4771 const MachineRegisterInfo *MRI, 4772 const MachineInstr &DefMI, 4773 unsigned DefIdx, 4774 const MachineInstr &UseMI, 4775 unsigned UseIdx) const { 4776 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask; 4777 unsigned UDomain = UseMI.getDesc().TSFlags & ARMII::DomainMask; 4778 if (Subtarget.nonpipelinedVFP() && 4779 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP)) 4780 return true; 4781 4782 // Hoist VFP / NEON instructions with 4 or higher latency. 4783 unsigned Latency = 4784 SchedModel.computeOperandLatency(&DefMI, DefIdx, &UseMI, UseIdx); 4785 if (Latency <= 3) 4786 return false; 4787 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON || 4788 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON; 4789 } 4790 4791 bool ARMBaseInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel, 4792 const MachineInstr &DefMI, 4793 unsigned DefIdx) const { 4794 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries(); 4795 if (!ItinData || ItinData->isEmpty()) 4796 return false; 4797 4798 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask; 4799 if (DDomain == ARMII::DomainGeneral) { 4800 unsigned DefClass = DefMI.getDesc().getSchedClass(); 4801 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx); 4802 return (DefCycle != -1 && DefCycle <= 2); 4803 } 4804 return false; 4805 } 4806 4807 bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr &MI, 4808 StringRef &ErrInfo) const { 4809 if (convertAddSubFlagsOpcode(MI.getOpcode())) { 4810 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG"; 4811 return false; 4812 } 4813 if (MI.getOpcode() == ARM::tMOVr && !Subtarget.hasV6Ops()) { 4814 // Make sure we don't generate a lo-lo mov that isn't supported. 4815 if (!ARM::hGPRRegClass.contains(MI.getOperand(0).getReg()) && 4816 !ARM::hGPRRegClass.contains(MI.getOperand(1).getReg())) { 4817 ErrInfo = "Non-flag-setting Thumb1 mov is v6-only"; 4818 return false; 4819 } 4820 } 4821 if (MI.getOpcode() == ARM::tPUSH || 4822 MI.getOpcode() == ARM::tPOP || 4823 MI.getOpcode() == ARM::tPOP_RET) { 4824 for (int i = 2, e = MI.getNumOperands(); i < e; ++i) { 4825 if (MI.getOperand(i).isImplicit() || 4826 !MI.getOperand(i).isReg()) 4827 continue; 4828 Register Reg = MI.getOperand(i).getReg(); 4829 if (Reg < ARM::R0 || Reg > ARM::R7) { 4830 if (!(MI.getOpcode() == ARM::tPUSH && Reg == ARM::LR) && 4831 !(MI.getOpcode() == ARM::tPOP_RET && Reg == ARM::PC)) { 4832 ErrInfo = "Unsupported register in Thumb1 push/pop"; 4833 return false; 4834 } 4835 } 4836 } 4837 } 4838 return true; 4839 } 4840 4841 // LoadStackGuard has so far only been implemented for MachO. Different code 4842 // sequence is needed for other targets. 4843 void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI, 4844 unsigned LoadImmOpc, 4845 unsigned LoadOpc) const { 4846 assert(!Subtarget.isROPI() && !Subtarget.isRWPI() && 4847 "ROPI/RWPI not currently supported with stack guard"); 4848 4849 MachineBasicBlock &MBB = *MI->getParent(); 4850 DebugLoc DL = MI->getDebugLoc(); 4851 Register Reg = MI->getOperand(0).getReg(); 4852 const GlobalValue *GV = 4853 cast<GlobalValue>((*MI->memoperands_begin())->getValue()); 4854 MachineInstrBuilder MIB; 4855 4856 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg) 4857 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY); 4858 4859 if (Subtarget.isGVIndirectSymbol(GV)) { 4860 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg); 4861 MIB.addReg(Reg, RegState::Kill).addImm(0); 4862 auto Flags = MachineMemOperand::MOLoad | 4863 MachineMemOperand::MODereferenceable | 4864 MachineMemOperand::MOInvariant; 4865 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand( 4866 MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 4, Align(4)); 4867 MIB.addMemOperand(MMO).add(predOps(ARMCC::AL)); 4868 } 4869 4870 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg); 4871 MIB.addReg(Reg, RegState::Kill) 4872 .addImm(0) 4873 .cloneMemRefs(*MI) 4874 .add(predOps(ARMCC::AL)); 4875 } 4876 4877 bool 4878 ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc, 4879 unsigned &AddSubOpc, 4880 bool &NegAcc, bool &HasLane) const { 4881 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode); 4882 if (I == MLxEntryMap.end()) 4883 return false; 4884 4885 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second]; 4886 MulOpc = Entry.MulOpc; 4887 AddSubOpc = Entry.AddSubOpc; 4888 NegAcc = Entry.NegAcc; 4889 HasLane = Entry.HasLane; 4890 return true; 4891 } 4892 4893 //===----------------------------------------------------------------------===// 4894 // Execution domains. 4895 //===----------------------------------------------------------------------===// 4896 // 4897 // Some instructions go down the NEON pipeline, some go down the VFP pipeline, 4898 // and some can go down both. The vmov instructions go down the VFP pipeline, 4899 // but they can be changed to vorr equivalents that are executed by the NEON 4900 // pipeline. 4901 // 4902 // We use the following execution domain numbering: 4903 // 4904 enum ARMExeDomain { 4905 ExeGeneric = 0, 4906 ExeVFP = 1, 4907 ExeNEON = 2 4908 }; 4909 4910 // 4911 // Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h 4912 // 4913 std::pair<uint16_t, uint16_t> 4914 ARMBaseInstrInfo::getExecutionDomain(const MachineInstr &MI) const { 4915 // If we don't have access to NEON instructions then we won't be able 4916 // to swizzle anything to the NEON domain. Check to make sure. 4917 if (Subtarget.hasNEON()) { 4918 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON 4919 // if they are not predicated. 4920 if (MI.getOpcode() == ARM::VMOVD && !isPredicated(MI)) 4921 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON)); 4922 4923 // CortexA9 is particularly picky about mixing the two and wants these 4924 // converted. 4925 if (Subtarget.useNEONForFPMovs() && !isPredicated(MI) && 4926 (MI.getOpcode() == ARM::VMOVRS || MI.getOpcode() == ARM::VMOVSR || 4927 MI.getOpcode() == ARM::VMOVS)) 4928 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON)); 4929 } 4930 // No other instructions can be swizzled, so just determine their domain. 4931 unsigned Domain = MI.getDesc().TSFlags & ARMII::DomainMask; 4932 4933 if (Domain & ARMII::DomainNEON) 4934 return std::make_pair(ExeNEON, 0); 4935 4936 // Certain instructions can go either way on Cortex-A8. 4937 // Treat them as NEON instructions. 4938 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8()) 4939 return std::make_pair(ExeNEON, 0); 4940 4941 if (Domain & ARMII::DomainVFP) 4942 return std::make_pair(ExeVFP, 0); 4943 4944 return std::make_pair(ExeGeneric, 0); 4945 } 4946 4947 static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI, 4948 unsigned SReg, unsigned &Lane) { 4949 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass); 4950 Lane = 0; 4951 4952 if (DReg != ARM::NoRegister) 4953 return DReg; 4954 4955 Lane = 1; 4956 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass); 4957 4958 assert(DReg && "S-register with no D super-register?"); 4959 return DReg; 4960 } 4961 4962 /// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane, 4963 /// set ImplicitSReg to a register number that must be marked as implicit-use or 4964 /// zero if no register needs to be defined as implicit-use. 4965 /// 4966 /// If the function cannot determine if an SPR should be marked implicit use or 4967 /// not, it returns false. 4968 /// 4969 /// This function handles cases where an instruction is being modified from taking 4970 /// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict 4971 /// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other 4972 /// lane of the DPR). 4973 /// 4974 /// If the other SPR is defined, an implicit-use of it should be added. Else, 4975 /// (including the case where the DPR itself is defined), it should not. 4976 /// 4977 static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI, 4978 MachineInstr &MI, unsigned DReg, 4979 unsigned Lane, unsigned &ImplicitSReg) { 4980 // If the DPR is defined or used already, the other SPR lane will be chained 4981 // correctly, so there is nothing to be done. 4982 if (MI.definesRegister(DReg, TRI) || MI.readsRegister(DReg, TRI)) { 4983 ImplicitSReg = 0; 4984 return true; 4985 } 4986 4987 // Otherwise we need to go searching to see if the SPR is set explicitly. 4988 ImplicitSReg = TRI->getSubReg(DReg, 4989 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1); 4990 MachineBasicBlock::LivenessQueryResult LQR = 4991 MI.getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI); 4992 4993 if (LQR == MachineBasicBlock::LQR_Live) 4994 return true; 4995 else if (LQR == MachineBasicBlock::LQR_Unknown) 4996 return false; 4997 4998 // If the register is known not to be live, there is no need to add an 4999 // implicit-use. 5000 ImplicitSReg = 0; 5001 return true; 5002 } 5003 5004 void ARMBaseInstrInfo::setExecutionDomain(MachineInstr &MI, 5005 unsigned Domain) const { 5006 unsigned DstReg, SrcReg, DReg; 5007 unsigned Lane; 5008 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI); 5009 const TargetRegisterInfo *TRI = &getRegisterInfo(); 5010 switch (MI.getOpcode()) { 5011 default: 5012 llvm_unreachable("cannot handle opcode!"); 5013 break; 5014 case ARM::VMOVD: 5015 if (Domain != ExeNEON) 5016 break; 5017 5018 // Zap the predicate operands. 5019 assert(!isPredicated(MI) && "Cannot predicate a VORRd"); 5020 5021 // Make sure we've got NEON instructions. 5022 assert(Subtarget.hasNEON() && "VORRd requires NEON"); 5023 5024 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits) 5025 DstReg = MI.getOperand(0).getReg(); 5026 SrcReg = MI.getOperand(1).getReg(); 5027 5028 for (unsigned i = MI.getDesc().getNumOperands(); i; --i) 5029 MI.RemoveOperand(i - 1); 5030 5031 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits) 5032 MI.setDesc(get(ARM::VORRd)); 5033 MIB.addReg(DstReg, RegState::Define) 5034 .addReg(SrcReg) 5035 .addReg(SrcReg) 5036 .add(predOps(ARMCC::AL)); 5037 break; 5038 case ARM::VMOVRS: 5039 if (Domain != ExeNEON) 5040 break; 5041 assert(!isPredicated(MI) && "Cannot predicate a VGETLN"); 5042 5043 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits) 5044 DstReg = MI.getOperand(0).getReg(); 5045 SrcReg = MI.getOperand(1).getReg(); 5046 5047 for (unsigned i = MI.getDesc().getNumOperands(); i; --i) 5048 MI.RemoveOperand(i - 1); 5049 5050 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane); 5051 5052 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps) 5053 // Note that DSrc has been widened and the other lane may be undef, which 5054 // contaminates the entire register. 5055 MI.setDesc(get(ARM::VGETLNi32)); 5056 MIB.addReg(DstReg, RegState::Define) 5057 .addReg(DReg, RegState::Undef) 5058 .addImm(Lane) 5059 .add(predOps(ARMCC::AL)); 5060 5061 // The old source should be an implicit use, otherwise we might think it 5062 // was dead before here. 5063 MIB.addReg(SrcReg, RegState::Implicit); 5064 break; 5065 case ARM::VMOVSR: { 5066 if (Domain != ExeNEON) 5067 break; 5068 assert(!isPredicated(MI) && "Cannot predicate a VSETLN"); 5069 5070 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits) 5071 DstReg = MI.getOperand(0).getReg(); 5072 SrcReg = MI.getOperand(1).getReg(); 5073 5074 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane); 5075 5076 unsigned ImplicitSReg; 5077 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg)) 5078 break; 5079 5080 for (unsigned i = MI.getDesc().getNumOperands(); i; --i) 5081 MI.RemoveOperand(i - 1); 5082 5083 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps) 5084 // Again DDst may be undefined at the beginning of this instruction. 5085 MI.setDesc(get(ARM::VSETLNi32)); 5086 MIB.addReg(DReg, RegState::Define) 5087 .addReg(DReg, getUndefRegState(!MI.readsRegister(DReg, TRI))) 5088 .addReg(SrcReg) 5089 .addImm(Lane) 5090 .add(predOps(ARMCC::AL)); 5091 5092 // The narrower destination must be marked as set to keep previous chains 5093 // in place. 5094 MIB.addReg(DstReg, RegState::Define | RegState::Implicit); 5095 if (ImplicitSReg != 0) 5096 MIB.addReg(ImplicitSReg, RegState::Implicit); 5097 break; 5098 } 5099 case ARM::VMOVS: { 5100 if (Domain != ExeNEON) 5101 break; 5102 5103 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits) 5104 DstReg = MI.getOperand(0).getReg(); 5105 SrcReg = MI.getOperand(1).getReg(); 5106 5107 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc; 5108 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane); 5109 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane); 5110 5111 unsigned ImplicitSReg; 5112 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg)) 5113 break; 5114 5115 for (unsigned i = MI.getDesc().getNumOperands(); i; --i) 5116 MI.RemoveOperand(i - 1); 5117 5118 if (DSrc == DDst) { 5119 // Destination can be: 5120 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits) 5121 MI.setDesc(get(ARM::VDUPLN32d)); 5122 MIB.addReg(DDst, RegState::Define) 5123 .addReg(DDst, getUndefRegState(!MI.readsRegister(DDst, TRI))) 5124 .addImm(SrcLane) 5125 .add(predOps(ARMCC::AL)); 5126 5127 // Neither the source or the destination are naturally represented any 5128 // more, so add them in manually. 5129 MIB.addReg(DstReg, RegState::Implicit | RegState::Define); 5130 MIB.addReg(SrcReg, RegState::Implicit); 5131 if (ImplicitSReg != 0) 5132 MIB.addReg(ImplicitSReg, RegState::Implicit); 5133 break; 5134 } 5135 5136 // In general there's no single instruction that can perform an S <-> S 5137 // move in NEON space, but a pair of VEXT instructions *can* do the 5138 // job. It turns out that the VEXTs needed will only use DSrc once, with 5139 // the position based purely on the combination of lane-0 and lane-1 5140 // involved. For example 5141 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1 5142 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1 5143 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1 5144 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1 5145 // 5146 // Pattern of the MachineInstrs is: 5147 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits) 5148 MachineInstrBuilder NewMIB; 5149 NewMIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::VEXTd32), 5150 DDst); 5151 5152 // On the first instruction, both DSrc and DDst may be undef if present. 5153 // Specifically when the original instruction didn't have them as an 5154 // <imp-use>. 5155 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst; 5156 bool CurUndef = !MI.readsRegister(CurReg, TRI); 5157 NewMIB.addReg(CurReg, getUndefRegState(CurUndef)); 5158 5159 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst; 5160 CurUndef = !MI.readsRegister(CurReg, TRI); 5161 NewMIB.addReg(CurReg, getUndefRegState(CurUndef)) 5162 .addImm(1) 5163 .add(predOps(ARMCC::AL)); 5164 5165 if (SrcLane == DstLane) 5166 NewMIB.addReg(SrcReg, RegState::Implicit); 5167 5168 MI.setDesc(get(ARM::VEXTd32)); 5169 MIB.addReg(DDst, RegState::Define); 5170 5171 // On the second instruction, DDst has definitely been defined above, so 5172 // it is not undef. DSrc, if present, can be undef as above. 5173 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst; 5174 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI); 5175 MIB.addReg(CurReg, getUndefRegState(CurUndef)); 5176 5177 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst; 5178 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI); 5179 MIB.addReg(CurReg, getUndefRegState(CurUndef)) 5180 .addImm(1) 5181 .add(predOps(ARMCC::AL)); 5182 5183 if (SrcLane != DstLane) 5184 MIB.addReg(SrcReg, RegState::Implicit); 5185 5186 // As before, the original destination is no longer represented, add it 5187 // implicitly. 5188 MIB.addReg(DstReg, RegState::Define | RegState::Implicit); 5189 if (ImplicitSReg != 0) 5190 MIB.addReg(ImplicitSReg, RegState::Implicit); 5191 break; 5192 } 5193 } 5194 } 5195 5196 //===----------------------------------------------------------------------===// 5197 // Partial register updates 5198 //===----------------------------------------------------------------------===// 5199 // 5200 // Swift renames NEON registers with 64-bit granularity. That means any 5201 // instruction writing an S-reg implicitly reads the containing D-reg. The 5202 // problem is mostly avoided by translating f32 operations to v2f32 operations 5203 // on D-registers, but f32 loads are still a problem. 5204 // 5205 // These instructions can load an f32 into a NEON register: 5206 // 5207 // VLDRS - Only writes S, partial D update. 5208 // VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops. 5209 // VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops. 5210 // 5211 // FCONSTD can be used as a dependency-breaking instruction. 5212 unsigned ARMBaseInstrInfo::getPartialRegUpdateClearance( 5213 const MachineInstr &MI, unsigned OpNum, 5214 const TargetRegisterInfo *TRI) const { 5215 auto PartialUpdateClearance = Subtarget.getPartialUpdateClearance(); 5216 if (!PartialUpdateClearance) 5217 return 0; 5218 5219 assert(TRI && "Need TRI instance"); 5220 5221 const MachineOperand &MO = MI.getOperand(OpNum); 5222 if (MO.readsReg()) 5223 return 0; 5224 Register Reg = MO.getReg(); 5225 int UseOp = -1; 5226 5227 switch (MI.getOpcode()) { 5228 // Normal instructions writing only an S-register. 5229 case ARM::VLDRS: 5230 case ARM::FCONSTS: 5231 case ARM::VMOVSR: 5232 case ARM::VMOVv8i8: 5233 case ARM::VMOVv4i16: 5234 case ARM::VMOVv2i32: 5235 case ARM::VMOVv2f32: 5236 case ARM::VMOVv1i64: 5237 UseOp = MI.findRegisterUseOperandIdx(Reg, false, TRI); 5238 break; 5239 5240 // Explicitly reads the dependency. 5241 case ARM::VLD1LNd32: 5242 UseOp = 3; 5243 break; 5244 default: 5245 return 0; 5246 } 5247 5248 // If this instruction actually reads a value from Reg, there is no unwanted 5249 // dependency. 5250 if (UseOp != -1 && MI.getOperand(UseOp).readsReg()) 5251 return 0; 5252 5253 // We must be able to clobber the whole D-reg. 5254 if (Register::isVirtualRegister(Reg)) { 5255 // Virtual register must be a def undef foo:ssub_0 operand. 5256 if (!MO.getSubReg() || MI.readsVirtualRegister(Reg)) 5257 return 0; 5258 } else if (ARM::SPRRegClass.contains(Reg)) { 5259 // Physical register: MI must define the full D-reg. 5260 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0, 5261 &ARM::DPRRegClass); 5262 if (!DReg || !MI.definesRegister(DReg, TRI)) 5263 return 0; 5264 } 5265 5266 // MI has an unwanted D-register dependency. 5267 // Avoid defs in the previous N instructrions. 5268 return PartialUpdateClearance; 5269 } 5270 5271 // Break a partial register dependency after getPartialRegUpdateClearance 5272 // returned non-zero. 5273 void ARMBaseInstrInfo::breakPartialRegDependency( 5274 MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const { 5275 assert(OpNum < MI.getDesc().getNumDefs() && "OpNum is not a def"); 5276 assert(TRI && "Need TRI instance"); 5277 5278 const MachineOperand &MO = MI.getOperand(OpNum); 5279 Register Reg = MO.getReg(); 5280 assert(Register::isPhysicalRegister(Reg) && 5281 "Can't break virtual register dependencies."); 5282 unsigned DReg = Reg; 5283 5284 // If MI defines an S-reg, find the corresponding D super-register. 5285 if (ARM::SPRRegClass.contains(Reg)) { 5286 DReg = ARM::D0 + (Reg - ARM::S0) / 2; 5287 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken"); 5288 } 5289 5290 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps"); 5291 assert(MI.definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg"); 5292 5293 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines 5294 // the full D-register by loading the same value to both lanes. The 5295 // instruction is micro-coded with 2 uops, so don't do this until we can 5296 // properly schedule micro-coded instructions. The dispatcher stalls cause 5297 // too big regressions. 5298 5299 // Insert the dependency-breaking FCONSTD before MI. 5300 // 96 is the encoding of 0.5, but the actual value doesn't matter here. 5301 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::FCONSTD), DReg) 5302 .addImm(96) 5303 .add(predOps(ARMCC::AL)); 5304 MI.addRegisterKilled(DReg, TRI, true); 5305 } 5306 5307 bool ARMBaseInstrInfo::hasNOP() const { 5308 return Subtarget.getFeatureBits()[ARM::HasV6KOps]; 5309 } 5310 5311 bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const { 5312 if (MI->getNumOperands() < 4) 5313 return true; 5314 unsigned ShOpVal = MI->getOperand(3).getImm(); 5315 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal); 5316 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1. 5317 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) || 5318 ((ShImm == 1 || ShImm == 2) && 5319 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl)) 5320 return true; 5321 5322 return false; 5323 } 5324 5325 bool ARMBaseInstrInfo::getRegSequenceLikeInputs( 5326 const MachineInstr &MI, unsigned DefIdx, 5327 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const { 5328 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index"); 5329 assert(MI.isRegSequenceLike() && "Invalid kind of instruction"); 5330 5331 switch (MI.getOpcode()) { 5332 case ARM::VMOVDRR: 5333 // dX = VMOVDRR rY, rZ 5334 // is the same as: 5335 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1 5336 // Populate the InputRegs accordingly. 5337 // rY 5338 const MachineOperand *MOReg = &MI.getOperand(1); 5339 if (!MOReg->isUndef()) 5340 InputRegs.push_back(RegSubRegPairAndIdx(MOReg->getReg(), 5341 MOReg->getSubReg(), ARM::ssub_0)); 5342 // rZ 5343 MOReg = &MI.getOperand(2); 5344 if (!MOReg->isUndef()) 5345 InputRegs.push_back(RegSubRegPairAndIdx(MOReg->getReg(), 5346 MOReg->getSubReg(), ARM::ssub_1)); 5347 return true; 5348 } 5349 llvm_unreachable("Target dependent opcode missing"); 5350 } 5351 5352 bool ARMBaseInstrInfo::getExtractSubregLikeInputs( 5353 const MachineInstr &MI, unsigned DefIdx, 5354 RegSubRegPairAndIdx &InputReg) const { 5355 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index"); 5356 assert(MI.isExtractSubregLike() && "Invalid kind of instruction"); 5357 5358 switch (MI.getOpcode()) { 5359 case ARM::VMOVRRD: 5360 // rX, rY = VMOVRRD dZ 5361 // is the same as: 5362 // rX = EXTRACT_SUBREG dZ, ssub_0 5363 // rY = EXTRACT_SUBREG dZ, ssub_1 5364 const MachineOperand &MOReg = MI.getOperand(2); 5365 if (MOReg.isUndef()) 5366 return false; 5367 InputReg.Reg = MOReg.getReg(); 5368 InputReg.SubReg = MOReg.getSubReg(); 5369 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1; 5370 return true; 5371 } 5372 llvm_unreachable("Target dependent opcode missing"); 5373 } 5374 5375 bool ARMBaseInstrInfo::getInsertSubregLikeInputs( 5376 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg, 5377 RegSubRegPairAndIdx &InsertedReg) const { 5378 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index"); 5379 assert(MI.isInsertSubregLike() && "Invalid kind of instruction"); 5380 5381 switch (MI.getOpcode()) { 5382 case ARM::VSETLNi32: 5383 // dX = VSETLNi32 dY, rZ, imm 5384 const MachineOperand &MOBaseReg = MI.getOperand(1); 5385 const MachineOperand &MOInsertedReg = MI.getOperand(2); 5386 if (MOInsertedReg.isUndef()) 5387 return false; 5388 const MachineOperand &MOIndex = MI.getOperand(3); 5389 BaseReg.Reg = MOBaseReg.getReg(); 5390 BaseReg.SubReg = MOBaseReg.getSubReg(); 5391 5392 InsertedReg.Reg = MOInsertedReg.getReg(); 5393 InsertedReg.SubReg = MOInsertedReg.getSubReg(); 5394 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1; 5395 return true; 5396 } 5397 llvm_unreachable("Target dependent opcode missing"); 5398 } 5399 5400 std::pair<unsigned, unsigned> 5401 ARMBaseInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const { 5402 const unsigned Mask = ARMII::MO_OPTION_MASK; 5403 return std::make_pair(TF & Mask, TF & ~Mask); 5404 } 5405 5406 ArrayRef<std::pair<unsigned, const char *>> 5407 ARMBaseInstrInfo::getSerializableDirectMachineOperandTargetFlags() const { 5408 using namespace ARMII; 5409 5410 static const std::pair<unsigned, const char *> TargetFlags[] = { 5411 {MO_LO16, "arm-lo16"}, {MO_HI16, "arm-hi16"}}; 5412 return makeArrayRef(TargetFlags); 5413 } 5414 5415 ArrayRef<std::pair<unsigned, const char *>> 5416 ARMBaseInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const { 5417 using namespace ARMII; 5418 5419 static const std::pair<unsigned, const char *> TargetFlags[] = { 5420 {MO_COFFSTUB, "arm-coffstub"}, 5421 {MO_GOT, "arm-got"}, 5422 {MO_SBREL, "arm-sbrel"}, 5423 {MO_DLLIMPORT, "arm-dllimport"}, 5424 {MO_SECREL, "arm-secrel"}, 5425 {MO_NONLAZY, "arm-nonlazy"}}; 5426 return makeArrayRef(TargetFlags); 5427 } 5428 5429 Optional<RegImmPair> ARMBaseInstrInfo::isAddImmediate(const MachineInstr &MI, 5430 Register Reg) const { 5431 int Sign = 1; 5432 unsigned Opcode = MI.getOpcode(); 5433 int64_t Offset = 0; 5434 5435 // TODO: Handle cases where Reg is a super- or sub-register of the 5436 // destination register. 5437 const MachineOperand &Op0 = MI.getOperand(0); 5438 if (!Op0.isReg() || Reg != Op0.getReg()) 5439 return None; 5440 5441 // We describe SUBri or ADDri instructions. 5442 if (Opcode == ARM::SUBri) 5443 Sign = -1; 5444 else if (Opcode != ARM::ADDri) 5445 return None; 5446 5447 // TODO: Third operand can be global address (usually some string). Since 5448 // strings can be relocated we cannot calculate their offsets for 5449 // now. 5450 if (!MI.getOperand(1).isReg() || !MI.getOperand(2).isImm()) 5451 return None; 5452 5453 Offset = MI.getOperand(2).getImm() * Sign; 5454 return RegImmPair{MI.getOperand(1).getReg(), Offset}; 5455 } 5456 5457 bool llvm::registerDefinedBetween(unsigned Reg, 5458 MachineBasicBlock::iterator From, 5459 MachineBasicBlock::iterator To, 5460 const TargetRegisterInfo *TRI) { 5461 for (auto I = From; I != To; ++I) 5462 if (I->modifiesRegister(Reg, TRI)) 5463 return true; 5464 return false; 5465 } 5466 5467 MachineInstr *llvm::findCMPToFoldIntoCBZ(MachineInstr *Br, 5468 const TargetRegisterInfo *TRI) { 5469 // Search backwards to the instruction that defines CSPR. This may or not 5470 // be a CMP, we check that after this loop. If we find another instruction 5471 // that reads cpsr, we return nullptr. 5472 MachineBasicBlock::iterator CmpMI = Br; 5473 while (CmpMI != Br->getParent()->begin()) { 5474 --CmpMI; 5475 if (CmpMI->modifiesRegister(ARM::CPSR, TRI)) 5476 break; 5477 if (CmpMI->readsRegister(ARM::CPSR, TRI)) 5478 break; 5479 } 5480 5481 // Check that this inst is a CMP r[0-7], #0 and that the register 5482 // is not redefined between the cmp and the br. 5483 if (CmpMI->getOpcode() != ARM::tCMPi8 && CmpMI->getOpcode() != ARM::t2CMPri) 5484 return nullptr; 5485 Register Reg = CmpMI->getOperand(0).getReg(); 5486 Register PredReg; 5487 ARMCC::CondCodes Pred = getInstrPredicate(*CmpMI, PredReg); 5488 if (Pred != ARMCC::AL || CmpMI->getOperand(1).getImm() != 0) 5489 return nullptr; 5490 if (!isARMLowRegister(Reg)) 5491 return nullptr; 5492 if (registerDefinedBetween(Reg, CmpMI->getNextNode(), Br, TRI)) 5493 return nullptr; 5494 5495 return &*CmpMI; 5496 } 5497 5498 unsigned llvm::ConstantMaterializationCost(unsigned Val, 5499 const ARMSubtarget *Subtarget, 5500 bool ForCodesize) { 5501 if (Subtarget->isThumb()) { 5502 if (Val <= 255) // MOV 5503 return ForCodesize ? 2 : 1; 5504 if (Subtarget->hasV6T2Ops() && (Val <= 0xffff || // MOV 5505 ARM_AM::getT2SOImmVal(Val) != -1 || // MOVW 5506 ARM_AM::getT2SOImmVal(~Val) != -1)) // MVN 5507 return ForCodesize ? 4 : 1; 5508 if (Val <= 510) // MOV + ADDi8 5509 return ForCodesize ? 4 : 2; 5510 if (~Val <= 255) // MOV + MVN 5511 return ForCodesize ? 4 : 2; 5512 if (ARM_AM::isThumbImmShiftedVal(Val)) // MOV + LSL 5513 return ForCodesize ? 4 : 2; 5514 } else { 5515 if (ARM_AM::getSOImmVal(Val) != -1) // MOV 5516 return ForCodesize ? 4 : 1; 5517 if (ARM_AM::getSOImmVal(~Val) != -1) // MVN 5518 return ForCodesize ? 4 : 1; 5519 if (Subtarget->hasV6T2Ops() && Val <= 0xffff) // MOVW 5520 return ForCodesize ? 4 : 1; 5521 if (ARM_AM::isSOImmTwoPartVal(Val)) // two instrs 5522 return ForCodesize ? 8 : 2; 5523 if (ARM_AM::isSOImmTwoPartValNeg(Val)) // two instrs 5524 return ForCodesize ? 8 : 2; 5525 } 5526 if (Subtarget->useMovt()) // MOVW + MOVT 5527 return ForCodesize ? 8 : 2; 5528 return ForCodesize ? 8 : 3; // Literal pool load 5529 } 5530 5531 bool llvm::HasLowerConstantMaterializationCost(unsigned Val1, unsigned Val2, 5532 const ARMSubtarget *Subtarget, 5533 bool ForCodesize) { 5534 // Check with ForCodesize 5535 unsigned Cost1 = ConstantMaterializationCost(Val1, Subtarget, ForCodesize); 5536 unsigned Cost2 = ConstantMaterializationCost(Val2, Subtarget, ForCodesize); 5537 if (Cost1 < Cost2) 5538 return true; 5539 if (Cost1 > Cost2) 5540 return false; 5541 5542 // If they are equal, try with !ForCodesize 5543 return ConstantMaterializationCost(Val1, Subtarget, !ForCodesize) < 5544 ConstantMaterializationCost(Val2, Subtarget, !ForCodesize); 5545 } 5546 5547 /// Constants defining how certain sequences should be outlined. 5548 /// This encompasses how an outlined function should be called, and what kind of 5549 /// frame should be emitted for that outlined function. 5550 /// 5551 /// \p MachineOutlinerTailCall implies that the function is being created from 5552 /// a sequence of instructions ending in a return. 5553 /// 5554 /// That is, 5555 /// 5556 /// I1 OUTLINED_FUNCTION: 5557 /// I2 --> B OUTLINED_FUNCTION I1 5558 /// BX LR I2 5559 /// BX LR 5560 /// 5561 /// +-------------------------+--------+-----+ 5562 /// | | Thumb2 | ARM | 5563 /// +-------------------------+--------+-----+ 5564 /// | Call overhead in Bytes | 4 | 4 | 5565 /// | Frame overhead in Bytes | 0 | 0 | 5566 /// | Stack fixup required | No | No | 5567 /// +-------------------------+--------+-----+ 5568 /// 5569 /// \p MachineOutlinerThunk implies that the function is being created from 5570 /// a sequence of instructions ending in a call. The outlined function is 5571 /// called with a BL instruction, and the outlined function tail-calls the 5572 /// original call destination. 5573 /// 5574 /// That is, 5575 /// 5576 /// I1 OUTLINED_FUNCTION: 5577 /// I2 --> BL OUTLINED_FUNCTION I1 5578 /// BL f I2 5579 /// B f 5580 /// 5581 /// +-------------------------+--------+-----+ 5582 /// | | Thumb2 | ARM | 5583 /// +-------------------------+--------+-----+ 5584 /// | Call overhead in Bytes | 4 | 4 | 5585 /// | Frame overhead in Bytes | 0 | 0 | 5586 /// | Stack fixup required | No | No | 5587 /// +-------------------------+--------+-----+ 5588 /// 5589 /// \p MachineOutlinerNoLRSave implies that the function should be called using 5590 /// a BL instruction, but doesn't require LR to be saved and restored. This 5591 /// happens when LR is known to be dead. 5592 /// 5593 /// That is, 5594 /// 5595 /// I1 OUTLINED_FUNCTION: 5596 /// I2 --> BL OUTLINED_FUNCTION I1 5597 /// I3 I2 5598 /// I3 5599 /// BX LR 5600 /// 5601 /// +-------------------------+--------+-----+ 5602 /// | | Thumb2 | ARM | 5603 /// +-------------------------+--------+-----+ 5604 /// | Call overhead in Bytes | 4 | 4 | 5605 /// | Frame overhead in Bytes | 4 | 4 | 5606 /// | Stack fixup required | No | No | 5607 /// +-------------------------+--------+-----+ 5608 /// 5609 /// \p MachineOutlinerRegSave implies that the function should be called with a 5610 /// save and restore of LR to an available register. This allows us to avoid 5611 /// stack fixups. Note that this outlining variant is compatible with the 5612 /// NoLRSave case. 5613 /// 5614 /// That is, 5615 /// 5616 /// I1 Save LR OUTLINED_FUNCTION: 5617 /// I2 --> BL OUTLINED_FUNCTION I1 5618 /// I3 Restore LR I2 5619 /// I3 5620 /// BX LR 5621 /// 5622 /// +-------------------------+--------+-----+ 5623 /// | | Thumb2 | ARM | 5624 /// +-------------------------+--------+-----+ 5625 /// | Call overhead in Bytes | 8 | 12 | 5626 /// | Frame overhead in Bytes | 2 | 4 | 5627 /// | Stack fixup required | No | No | 5628 /// +-------------------------+--------+-----+ 5629 5630 enum MachineOutlinerClass { 5631 MachineOutlinerTailCall, 5632 MachineOutlinerThunk, 5633 MachineOutlinerNoLRSave, 5634 MachineOutlinerRegSave 5635 }; 5636 5637 enum MachineOutlinerMBBFlags { 5638 LRUnavailableSomewhere = 0x2, 5639 HasCalls = 0x4, 5640 UnsafeRegsDead = 0x8 5641 }; 5642 5643 struct OutlinerCosts { 5644 const int CallTailCall; 5645 const int FrameTailCall; 5646 const int CallThunk; 5647 const int FrameThunk; 5648 const int CallNoLRSave; 5649 const int FrameNoLRSave; 5650 const int CallRegSave; 5651 const int FrameRegSave; 5652 5653 OutlinerCosts(const ARMSubtarget &target) 5654 : CallTailCall(target.isThumb() ? 4 : 4), 5655 FrameTailCall(target.isThumb() ? 0 : 0), 5656 CallThunk(target.isThumb() ? 4 : 4), 5657 FrameThunk(target.isThumb() ? 0 : 0), 5658 CallNoLRSave(target.isThumb() ? 4 : 4), 5659 FrameNoLRSave(target.isThumb() ? 4 : 4), 5660 CallRegSave(target.isThumb() ? 8 : 12), 5661 FrameRegSave(target.isThumb() ? 2 : 4) {} 5662 }; 5663 5664 unsigned 5665 ARMBaseInstrInfo::findRegisterToSaveLRTo(const outliner::Candidate &C) const { 5666 assert(C.LRUWasSet && "LRU wasn't set?"); 5667 MachineFunction *MF = C.getMF(); 5668 const ARMBaseRegisterInfo *ARI = static_cast<const ARMBaseRegisterInfo *>( 5669 MF->getSubtarget().getRegisterInfo()); 5670 5671 BitVector regsReserved = ARI->getReservedRegs(*MF); 5672 // Check if there is an available register across the sequence that we can 5673 // use. 5674 for (unsigned Reg : ARM::rGPRRegClass) { 5675 if (!(Reg < regsReserved.size() && regsReserved.test(Reg)) && 5676 Reg != ARM::LR && // LR is not reserved, but don't use it. 5677 Reg != ARM::R12 && // R12 is not guaranteed to be preserved. 5678 C.LRU.available(Reg) && C.UsedInSequence.available(Reg)) 5679 return Reg; 5680 } 5681 5682 // No suitable register. Return 0. 5683 return 0u; 5684 } 5685 5686 outliner::OutlinedFunction ARMBaseInstrInfo::getOutliningCandidateInfo( 5687 std::vector<outliner::Candidate> &RepeatedSequenceLocs) const { 5688 outliner::Candidate &FirstCand = RepeatedSequenceLocs[0]; 5689 unsigned SequenceSize = 5690 std::accumulate(FirstCand.front(), std::next(FirstCand.back()), 0, 5691 [this](unsigned Sum, const MachineInstr &MI) { 5692 return Sum + getInstSizeInBytes(MI); 5693 }); 5694 5695 // Properties about candidate MBBs that hold for all of them. 5696 unsigned FlagsSetInAll = 0xF; 5697 5698 // Compute liveness information for each candidate, and set FlagsSetInAll. 5699 const TargetRegisterInfo &TRI = getRegisterInfo(); 5700 std::for_each( 5701 RepeatedSequenceLocs.begin(), RepeatedSequenceLocs.end(), 5702 [&FlagsSetInAll](outliner::Candidate &C) { FlagsSetInAll &= C.Flags; }); 5703 5704 // According to the ARM Procedure Call Standard, the following are 5705 // undefined on entry/exit from a function call: 5706 // 5707 // * Register R12(IP), 5708 // * Condition codes (and thus the CPSR register) 5709 // 5710 // Since we control the instructions which are part of the outlined regions 5711 // we don't need to be fully compliant with the AAPCS, but we have to 5712 // guarantee that if a veneer is inserted at link time the code is still 5713 // correct. Because of this, we can't outline any sequence of instructions 5714 // where one of these registers is live into/across it. Thus, we need to 5715 // delete those candidates. 5716 auto CantGuaranteeValueAcrossCall = [&TRI](outliner::Candidate &C) { 5717 // If the unsafe registers in this block are all dead, then we don't need 5718 // to compute liveness here. 5719 if (C.Flags & UnsafeRegsDead) 5720 return false; 5721 C.initLRU(TRI); 5722 LiveRegUnits LRU = C.LRU; 5723 return (!LRU.available(ARM::R12) || !LRU.available(ARM::CPSR)); 5724 }; 5725 5726 // Are there any candidates where those registers are live? 5727 if (!(FlagsSetInAll & UnsafeRegsDead)) { 5728 // Erase every candidate that violates the restrictions above. (It could be 5729 // true that we have viable candidates, so it's not worth bailing out in 5730 // the case that, say, 1 out of 20 candidates violate the restructions.) 5731 RepeatedSequenceLocs.erase(std::remove_if(RepeatedSequenceLocs.begin(), 5732 RepeatedSequenceLocs.end(), 5733 CantGuaranteeValueAcrossCall), 5734 RepeatedSequenceLocs.end()); 5735 5736 // If the sequence doesn't have enough candidates left, then we're done. 5737 if (RepeatedSequenceLocs.size() < 2) 5738 return outliner::OutlinedFunction(); 5739 } 5740 5741 // At this point, we have only "safe" candidates to outline. Figure out 5742 // frame + call instruction information. 5743 5744 unsigned LastInstrOpcode = RepeatedSequenceLocs[0].back()->getOpcode(); 5745 5746 // Helper lambda which sets call information for every candidate. 5747 auto SetCandidateCallInfo = 5748 [&RepeatedSequenceLocs](unsigned CallID, unsigned NumBytesForCall) { 5749 for (outliner::Candidate &C : RepeatedSequenceLocs) 5750 C.setCallInfo(CallID, NumBytesForCall); 5751 }; 5752 5753 OutlinerCosts Costs(Subtarget); 5754 unsigned FrameID = 0; 5755 unsigned NumBytesToCreateFrame = 0; 5756 5757 // If the last instruction in any candidate is a terminator, then we should 5758 // tail call all of the candidates. 5759 if (RepeatedSequenceLocs[0].back()->isTerminator()) { 5760 FrameID = MachineOutlinerTailCall; 5761 NumBytesToCreateFrame = Costs.FrameTailCall; 5762 SetCandidateCallInfo(MachineOutlinerTailCall, Costs.CallTailCall); 5763 } else if (LastInstrOpcode == ARM::BL || LastInstrOpcode == ARM::BLX || 5764 LastInstrOpcode == ARM::tBL || LastInstrOpcode == ARM::tBLXr || 5765 LastInstrOpcode == ARM::tBLXi) { 5766 FrameID = MachineOutlinerThunk; 5767 NumBytesToCreateFrame = Costs.FrameThunk; 5768 SetCandidateCallInfo(MachineOutlinerThunk, Costs.CallThunk); 5769 } else { 5770 // We need to decide how to emit calls + frames. We can always emit the same 5771 // frame if we don't need to save to the stack. 5772 unsigned NumBytesNoStackCalls = 0; 5773 std::vector<outliner::Candidate> CandidatesWithoutStackFixups; 5774 5775 for (outliner::Candidate &C : RepeatedSequenceLocs) { 5776 C.initLRU(TRI); 5777 5778 // Is LR available? If so, we don't need a save. 5779 if (C.LRU.available(ARM::LR)) { 5780 FrameID = MachineOutlinerNoLRSave; 5781 NumBytesNoStackCalls += Costs.CallNoLRSave; 5782 C.setCallInfo(MachineOutlinerNoLRSave, Costs.CallNoLRSave); 5783 CandidatesWithoutStackFixups.push_back(C); 5784 } 5785 5786 // Is an unused register available? If so, we won't modify the stack, so 5787 // we can outline with the same frame type as those that don't save LR. 5788 else if (findRegisterToSaveLRTo(C)) { 5789 FrameID = MachineOutlinerRegSave; 5790 NumBytesNoStackCalls += Costs.CallRegSave; 5791 C.setCallInfo(MachineOutlinerRegSave, Costs.CallRegSave); 5792 CandidatesWithoutStackFixups.push_back(C); 5793 } 5794 } 5795 5796 if (!CandidatesWithoutStackFixups.empty()) { 5797 RepeatedSequenceLocs = CandidatesWithoutStackFixups; 5798 } else 5799 return outliner::OutlinedFunction(); 5800 } 5801 5802 return outliner::OutlinedFunction(RepeatedSequenceLocs, SequenceSize, 5803 NumBytesToCreateFrame, FrameID); 5804 } 5805 5806 bool ARMBaseInstrInfo::isFunctionSafeToOutlineFrom( 5807 MachineFunction &MF, bool OutlineFromLinkOnceODRs) const { 5808 const Function &F = MF.getFunction(); 5809 5810 // Can F be deduplicated by the linker? If it can, don't outline from it. 5811 if (!OutlineFromLinkOnceODRs && F.hasLinkOnceODRLinkage()) 5812 return false; 5813 5814 // Don't outline from functions with section markings; the program could 5815 // expect that all the code is in the named section. 5816 // FIXME: Allow outlining from multiple functions with the same section 5817 // marking. 5818 if (F.hasSection()) 5819 return false; 5820 5821 // FIXME: Thumb1 outlining is not handled 5822 if (MF.getInfo<ARMFunctionInfo>()->isThumb1OnlyFunction()) 5823 return false; 5824 5825 // It's safe to outline from MF. 5826 return true; 5827 } 5828 5829 bool ARMBaseInstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock &MBB, 5830 unsigned &Flags) const { 5831 // Check if LR is available through all of the MBB. If it's not, then set 5832 // a flag. 5833 assert(MBB.getParent()->getRegInfo().tracksLiveness() && 5834 "Suitable Machine Function for outlining must track liveness"); 5835 5836 LiveRegUnits LRU(getRegisterInfo()); 5837 5838 std::for_each(MBB.rbegin(), MBB.rend(), 5839 [&LRU](MachineInstr &MI) { LRU.accumulate(MI); }); 5840 5841 // Check if each of the unsafe registers are available... 5842 bool R12AvailableInBlock = LRU.available(ARM::R12); 5843 bool CPSRAvailableInBlock = LRU.available(ARM::CPSR); 5844 5845 // If all of these are dead (and not live out), we know we don't have to check 5846 // them later. 5847 if (R12AvailableInBlock && CPSRAvailableInBlock) 5848 Flags |= MachineOutlinerMBBFlags::UnsafeRegsDead; 5849 5850 // Now, add the live outs to the set. 5851 LRU.addLiveOuts(MBB); 5852 5853 // If any of these registers is available in the MBB, but also a live out of 5854 // the block, then we know outlining is unsafe. 5855 if (R12AvailableInBlock && !LRU.available(ARM::R12)) 5856 return false; 5857 if (CPSRAvailableInBlock && !LRU.available(ARM::CPSR)) 5858 return false; 5859 5860 // Check if there's a call inside this MachineBasicBlock. If there is, then 5861 // set a flag. 5862 if (any_of(MBB, [](MachineInstr &MI) { return MI.isCall(); })) 5863 Flags |= MachineOutlinerMBBFlags::HasCalls; 5864 5865 if (!LRU.available(ARM::LR)) 5866 Flags |= MachineOutlinerMBBFlags::LRUnavailableSomewhere; 5867 5868 return true; 5869 } 5870 5871 outliner::InstrType 5872 ARMBaseInstrInfo::getOutliningType(MachineBasicBlock::iterator &MIT, 5873 unsigned Flags) const { 5874 MachineInstr &MI = *MIT; 5875 const TargetRegisterInfo *TRI = &getRegisterInfo(); 5876 5877 // Be conservative with inline ASM 5878 if (MI.isInlineAsm()) 5879 return outliner::InstrType::Illegal; 5880 5881 // Don't allow debug values to impact outlining type. 5882 if (MI.isDebugInstr() || MI.isIndirectDebugValue()) 5883 return outliner::InstrType::Invisible; 5884 5885 // At this point, KILL or IMPLICIT_DEF instructions don't really tell us much 5886 // so we can go ahead and skip over them. 5887 if (MI.isKill() || MI.isImplicitDef()) 5888 return outliner::InstrType::Invisible; 5889 5890 // PIC instructions contain labels, outlining them would break offset 5891 // computing. unsigned Opc = MI.getOpcode(); 5892 unsigned Opc = MI.getOpcode(); 5893 if (Opc == ARM::tPICADD || Opc == ARM::PICADD || Opc == ARM::PICSTR || 5894 Opc == ARM::PICSTRB || Opc == ARM::PICSTRH || Opc == ARM::PICLDR || 5895 Opc == ARM::PICLDRB || Opc == ARM::PICLDRH || Opc == ARM::PICLDRSB || 5896 Opc == ARM::PICLDRSH || Opc == ARM::t2LDRpci_pic || 5897 Opc == ARM::t2MOVi16_ga_pcrel || Opc == ARM::t2MOVTi16_ga_pcrel || 5898 Opc == ARM::t2MOV_ga_pcrel) 5899 return outliner::InstrType::Illegal; 5900 5901 // Be conservative with ARMv8.1 MVE instructions. 5902 if (Opc == ARM::t2BF_LabelPseudo || Opc == ARM::t2DoLoopStart || 5903 Opc == ARM::t2WhileLoopStart || Opc == ARM::t2LoopDec || 5904 Opc == ARM::t2LoopEnd) 5905 return outliner::InstrType::Illegal; 5906 5907 const MCInstrDesc &MCID = MI.getDesc(); 5908 uint64_t MIFlags = MCID.TSFlags; 5909 if ((MIFlags & ARMII::DomainMask) == ARMII::DomainMVE) 5910 return outliner::InstrType::Illegal; 5911 5912 // Is this a terminator for a basic block? 5913 if (MI.isTerminator()) { 5914 // Don't outline if the branch is not unconditional. 5915 if (isPredicated(MI)) 5916 return outliner::InstrType::Illegal; 5917 5918 // Is this the end of a function? 5919 if (MI.getParent()->succ_empty()) 5920 return outliner::InstrType::Legal; 5921 5922 // It's not, so don't outline it. 5923 return outliner::InstrType::Illegal; 5924 } 5925 5926 // Make sure none of the operands are un-outlinable. 5927 for (const MachineOperand &MOP : MI.operands()) { 5928 if (MOP.isCPI() || MOP.isJTI() || MOP.isCFIIndex() || MOP.isFI() || 5929 MOP.isTargetIndex()) 5930 return outliner::InstrType::Illegal; 5931 } 5932 5933 // Don't outline if link register or program counter value are used. 5934 if (MI.readsRegister(ARM::LR, TRI) || MI.readsRegister(ARM::PC, TRI)) 5935 return outliner::InstrType::Illegal; 5936 5937 if (MI.isCall()) { 5938 // If we don't know anything about the callee, assume it depends on the 5939 // stack layout of the caller. In that case, it's only legal to outline 5940 // as a tail-call. Explicitly list the call instructions we know about so 5941 // we don't get unexpected results with call pseudo-instructions. 5942 auto UnknownCallOutlineType = outliner::InstrType::Illegal; 5943 if (Opc == ARM::BL || Opc == ARM::tBL || Opc == ARM::BLX || 5944 Opc == ARM::tBLXr || Opc == ARM::tBLXi) 5945 UnknownCallOutlineType = outliner::InstrType::LegalTerminator; 5946 5947 return UnknownCallOutlineType; 5948 } 5949 5950 // Since calls are handled, don't touch LR or PC 5951 if (MI.modifiesRegister(ARM::LR, TRI) || MI.modifiesRegister(ARM::PC, TRI)) 5952 return outliner::InstrType::Illegal; 5953 5954 // Does this use the stack? 5955 if (MI.modifiesRegister(ARM::SP, TRI) || MI.readsRegister(ARM::SP, TRI)) { 5956 // True if there is no chance that any outlined candidate from this range 5957 // could require stack fixups. That is, both 5958 // * LR is available in the range (No save/restore around call) 5959 // * The range doesn't include calls (No save/restore in outlined frame) 5960 // are true. 5961 // FIXME: This is very restrictive; the flags check the whole block, 5962 // not just the bit we will try to outline. 5963 bool MightNeedStackFixUp = 5964 (Flags & (MachineOutlinerMBBFlags::LRUnavailableSomewhere | 5965 MachineOutlinerMBBFlags::HasCalls)); 5966 5967 if (!MightNeedStackFixUp) 5968 return outliner::InstrType::Legal; 5969 5970 return outliner::InstrType::Illegal; 5971 } 5972 5973 // Be conservative with IT blocks. 5974 if (MI.readsRegister(ARM::ITSTATE, TRI) || 5975 MI.modifiesRegister(ARM::ITSTATE, TRI)) 5976 return outliner::InstrType::Illegal; 5977 5978 // Don't outline positions. 5979 if (MI.isPosition()) 5980 return outliner::InstrType::Illegal; 5981 5982 return outliner::InstrType::Legal; 5983 } 5984 5985 void ARMBaseInstrInfo::buildOutlinedFrame( 5986 MachineBasicBlock &MBB, MachineFunction &MF, 5987 const outliner::OutlinedFunction &OF) const { 5988 // Nothing is needed for tail-calls. 5989 if (OF.FrameConstructionID == MachineOutlinerTailCall) 5990 return; 5991 5992 // For thunk outlining, rewrite the last instruction from a call to a 5993 // tail-call. 5994 if (OF.FrameConstructionID == MachineOutlinerThunk) { 5995 MachineInstr *Call = &*--MBB.instr_end(); 5996 bool isThumb = Subtarget.isThumb(); 5997 unsigned FuncOp = isThumb ? 2 : 0; 5998 unsigned Opc = Call->getOperand(FuncOp).isReg() 5999 ? isThumb ? ARM::tTAILJMPr : ARM::TAILJMPr 6000 : isThumb ? Subtarget.isTargetMachO() ? ARM::tTAILJMPd 6001 : ARM::tTAILJMPdND 6002 : ARM::TAILJMPd; 6003 MachineInstrBuilder MIB = BuildMI(MBB, MBB.end(), DebugLoc(), get(Opc)) 6004 .add(Call->getOperand(FuncOp)); 6005 if (isThumb && !Call->getOperand(FuncOp).isReg()) 6006 MIB.add(predOps(ARMCC::AL)); 6007 Call->eraseFromParent(); 6008 return; 6009 } 6010 6011 // Here we have to insert the return ourselves. Get the correct opcode from 6012 // current feature set. 6013 BuildMI(MBB, MBB.end(), DebugLoc(), get(Subtarget.getReturnOpcode())) 6014 .add(predOps(ARMCC::AL)); 6015 } 6016 6017 MachineBasicBlock::iterator ARMBaseInstrInfo::insertOutlinedCall( 6018 Module &M, MachineBasicBlock &MBB, MachineBasicBlock::iterator &It, 6019 MachineFunction &MF, const outliner::Candidate &C) const { 6020 MachineInstrBuilder MIB; 6021 MachineBasicBlock::iterator CallPt; 6022 unsigned Opc; 6023 bool isThumb = Subtarget.isThumb(); 6024 6025 // Are we tail calling? 6026 if (C.CallConstructionID == MachineOutlinerTailCall) { 6027 // If yes, then we can just branch to the label. 6028 Opc = isThumb 6029 ? Subtarget.isTargetMachO() ? ARM::tTAILJMPd : ARM::tTAILJMPdND 6030 : ARM::TAILJMPd; 6031 MIB = BuildMI(MF, DebugLoc(), get(Opc)) 6032 .addGlobalAddress(M.getNamedValue(MF.getName())); 6033 if (isThumb) 6034 MIB.add(predOps(ARMCC::AL)); 6035 It = MBB.insert(It, MIB); 6036 return It; 6037 } 6038 6039 // Create the call instruction. 6040 Opc = isThumb ? ARM::tBL : ARM::BL; 6041 MachineInstrBuilder CallMIB = BuildMI(MF, DebugLoc(), get(Opc)); 6042 if (isThumb) 6043 CallMIB.add(predOps(ARMCC::AL)); 6044 CallMIB.addGlobalAddress(M.getNamedValue(MF.getName())); 6045 6046 // Can we save to a register? 6047 if (C.CallConstructionID == MachineOutlinerRegSave) { 6048 unsigned Reg = findRegisterToSaveLRTo(C); 6049 assert(Reg != 0 && "No callee-saved register available?"); 6050 6051 // Save and restore LR from that register. 6052 if (!MBB.isLiveIn(ARM::LR)) 6053 MBB.addLiveIn(ARM::LR); 6054 copyPhysReg(MBB, It, DebugLoc(), Reg, ARM::LR, true); 6055 CallPt = MBB.insert(It, CallMIB); 6056 copyPhysReg(MBB, It, DebugLoc(), ARM::LR, Reg, true); 6057 It--; 6058 return CallPt; 6059 } 6060 // Insert the call. 6061 It = MBB.insert(It, CallMIB); 6062 return It; 6063 } 6064