1 //===- ARMBaseInstrInfo.cpp - ARM Instruction Information -------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains the Base ARM implementation of the TargetInstrInfo class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "ARMBaseInstrInfo.h" 15 #include "ARM.h" 16 #include "ARMAddressingModes.h" 17 #include "ARMConstantPoolValue.h" 18 #include "ARMGenInstrInfo.inc" 19 #include "ARMMachineFunctionInfo.h" 20 #include "ARMRegisterInfo.h" 21 #include "llvm/Constants.h" 22 #include "llvm/Function.h" 23 #include "llvm/GlobalValue.h" 24 #include "llvm/ADT/STLExtras.h" 25 #include "llvm/CodeGen/LiveVariables.h" 26 #include "llvm/CodeGen/MachineConstantPool.h" 27 #include "llvm/CodeGen/MachineFrameInfo.h" 28 #include "llvm/CodeGen/MachineInstrBuilder.h" 29 #include "llvm/CodeGen/MachineJumpTableInfo.h" 30 #include "llvm/CodeGen/MachineMemOperand.h" 31 #include "llvm/CodeGen/MachineRegisterInfo.h" 32 #include "llvm/CodeGen/PseudoSourceValue.h" 33 #include "llvm/MC/MCAsmInfo.h" 34 #include "llvm/Support/CommandLine.h" 35 #include "llvm/Support/Debug.h" 36 #include "llvm/Support/ErrorHandling.h" 37 using namespace llvm; 38 39 static cl::opt<bool> 40 EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden, 41 cl::desc("Enable ARM 2-addr to 3-addr conv")); 42 43 ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI) 44 : TargetInstrInfoImpl(ARMInsts, array_lengthof(ARMInsts)), 45 Subtarget(STI) { 46 } 47 48 MachineInstr * 49 ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI, 50 MachineBasicBlock::iterator &MBBI, 51 LiveVariables *LV) const { 52 // FIXME: Thumb2 support. 53 54 if (!EnableARM3Addr) 55 return NULL; 56 57 MachineInstr *MI = MBBI; 58 MachineFunction &MF = *MI->getParent()->getParent(); 59 uint64_t TSFlags = MI->getDesc().TSFlags; 60 bool isPre = false; 61 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) { 62 default: return NULL; 63 case ARMII::IndexModePre: 64 isPre = true; 65 break; 66 case ARMII::IndexModePost: 67 break; 68 } 69 70 // Try splitting an indexed load/store to an un-indexed one plus an add/sub 71 // operation. 72 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode()); 73 if (MemOpc == 0) 74 return NULL; 75 76 MachineInstr *UpdateMI = NULL; 77 MachineInstr *MemMI = NULL; 78 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask); 79 const TargetInstrDesc &TID = MI->getDesc(); 80 unsigned NumOps = TID.getNumOperands(); 81 bool isLoad = !TID.mayStore(); 82 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0); 83 const MachineOperand &Base = MI->getOperand(2); 84 const MachineOperand &Offset = MI->getOperand(NumOps-3); 85 unsigned WBReg = WB.getReg(); 86 unsigned BaseReg = Base.getReg(); 87 unsigned OffReg = Offset.getReg(); 88 unsigned OffImm = MI->getOperand(NumOps-2).getImm(); 89 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm(); 90 switch (AddrMode) { 91 default: 92 assert(false && "Unknown indexed op!"); 93 return NULL; 94 case ARMII::AddrMode2: { 95 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub; 96 unsigned Amt = ARM_AM::getAM2Offset(OffImm); 97 if (OffReg == 0) { 98 if (ARM_AM::getSOImmVal(Amt) == -1) 99 // Can't encode it in a so_imm operand. This transformation will 100 // add more than 1 instruction. Abandon! 101 return NULL; 102 UpdateMI = BuildMI(MF, MI->getDebugLoc(), 103 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg) 104 .addReg(BaseReg).addImm(Amt) 105 .addImm(Pred).addReg(0).addReg(0); 106 } else if (Amt != 0) { 107 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm); 108 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt); 109 UpdateMI = BuildMI(MF, MI->getDebugLoc(), 110 get(isSub ? ARM::SUBrs : ARM::ADDrs), WBReg) 111 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc) 112 .addImm(Pred).addReg(0).addReg(0); 113 } else 114 UpdateMI = BuildMI(MF, MI->getDebugLoc(), 115 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg) 116 .addReg(BaseReg).addReg(OffReg) 117 .addImm(Pred).addReg(0).addReg(0); 118 break; 119 } 120 case ARMII::AddrMode3 : { 121 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub; 122 unsigned Amt = ARM_AM::getAM3Offset(OffImm); 123 if (OffReg == 0) 124 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand. 125 UpdateMI = BuildMI(MF, MI->getDebugLoc(), 126 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg) 127 .addReg(BaseReg).addImm(Amt) 128 .addImm(Pred).addReg(0).addReg(0); 129 else 130 UpdateMI = BuildMI(MF, MI->getDebugLoc(), 131 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg) 132 .addReg(BaseReg).addReg(OffReg) 133 .addImm(Pred).addReg(0).addReg(0); 134 break; 135 } 136 } 137 138 std::vector<MachineInstr*> NewMIs; 139 if (isPre) { 140 if (isLoad) 141 MemMI = BuildMI(MF, MI->getDebugLoc(), 142 get(MemOpc), MI->getOperand(0).getReg()) 143 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred); 144 else 145 MemMI = BuildMI(MF, MI->getDebugLoc(), 146 get(MemOpc)).addReg(MI->getOperand(1).getReg()) 147 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred); 148 NewMIs.push_back(MemMI); 149 NewMIs.push_back(UpdateMI); 150 } else { 151 if (isLoad) 152 MemMI = BuildMI(MF, MI->getDebugLoc(), 153 get(MemOpc), MI->getOperand(0).getReg()) 154 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred); 155 else 156 MemMI = BuildMI(MF, MI->getDebugLoc(), 157 get(MemOpc)).addReg(MI->getOperand(1).getReg()) 158 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred); 159 if (WB.isDead()) 160 UpdateMI->getOperand(0).setIsDead(); 161 NewMIs.push_back(UpdateMI); 162 NewMIs.push_back(MemMI); 163 } 164 165 // Transfer LiveVariables states, kill / dead info. 166 if (LV) { 167 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 168 MachineOperand &MO = MI->getOperand(i); 169 if (MO.isReg() && MO.getReg() && 170 TargetRegisterInfo::isVirtualRegister(MO.getReg())) { 171 unsigned Reg = MO.getReg(); 172 173 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg); 174 if (MO.isDef()) { 175 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI; 176 if (MO.isDead()) 177 LV->addVirtualRegisterDead(Reg, NewMI); 178 } 179 if (MO.isUse() && MO.isKill()) { 180 for (unsigned j = 0; j < 2; ++j) { 181 // Look at the two new MI's in reverse order. 182 MachineInstr *NewMI = NewMIs[j]; 183 if (!NewMI->readsRegister(Reg)) 184 continue; 185 LV->addVirtualRegisterKilled(Reg, NewMI); 186 if (VI.removeKill(MI)) 187 VI.Kills.push_back(NewMI); 188 break; 189 } 190 } 191 } 192 } 193 } 194 195 MFI->insert(MBBI, NewMIs[1]); 196 MFI->insert(MBBI, NewMIs[0]); 197 return NewMIs[0]; 198 } 199 200 bool 201 ARMBaseInstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB, 202 MachineBasicBlock::iterator MI, 203 const std::vector<CalleeSavedInfo> &CSI, 204 const TargetRegisterInfo *TRI) const { 205 if (CSI.empty()) 206 return false; 207 208 DebugLoc DL; 209 if (MI != MBB.end()) DL = MI->getDebugLoc(); 210 211 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 212 unsigned Reg = CSI[i].getReg(); 213 bool isKill = true; 214 215 // Add the callee-saved register as live-in unless it's LR and 216 // @llvm.returnaddress is called. If LR is returned for @llvm.returnaddress 217 // then it's already added to the function and entry block live-in sets. 218 if (Reg == ARM::LR) { 219 MachineFunction &MF = *MBB.getParent(); 220 if (MF.getFrameInfo()->isReturnAddressTaken() && 221 MF.getRegInfo().isLiveIn(Reg)) 222 isKill = false; 223 } 224 225 if (isKill) 226 MBB.addLiveIn(Reg); 227 228 // Insert the spill to the stack frame. The register is killed at the spill 229 // 230 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); 231 storeRegToStackSlot(MBB, MI, Reg, isKill, 232 CSI[i].getFrameIdx(), RC, TRI); 233 } 234 return true; 235 } 236 237 // Branch analysis. 238 bool 239 ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB, 240 MachineBasicBlock *&FBB, 241 SmallVectorImpl<MachineOperand> &Cond, 242 bool AllowModify) const { 243 // If the block has no terminators, it just falls into the block after it. 244 MachineBasicBlock::iterator I = MBB.end(); 245 if (I == MBB.begin()) 246 return false; 247 --I; 248 while (I->isDebugValue()) { 249 if (I == MBB.begin()) 250 return false; 251 --I; 252 } 253 if (!isUnpredicatedTerminator(I)) 254 return false; 255 256 // Get the last instruction in the block. 257 MachineInstr *LastInst = I; 258 259 // If there is only one terminator instruction, process it. 260 unsigned LastOpc = LastInst->getOpcode(); 261 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) { 262 if (isUncondBranchOpcode(LastOpc)) { 263 TBB = LastInst->getOperand(0).getMBB(); 264 return false; 265 } 266 if (isCondBranchOpcode(LastOpc)) { 267 // Block ends with fall-through condbranch. 268 TBB = LastInst->getOperand(0).getMBB(); 269 Cond.push_back(LastInst->getOperand(1)); 270 Cond.push_back(LastInst->getOperand(2)); 271 return false; 272 } 273 return true; // Can't handle indirect branch. 274 } 275 276 // Get the instruction before it if it is a terminator. 277 MachineInstr *SecondLastInst = I; 278 279 // If there are three terminators, we don't know what sort of block this is. 280 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I)) 281 return true; 282 283 // If the block ends with a B and a Bcc, handle it. 284 unsigned SecondLastOpc = SecondLastInst->getOpcode(); 285 if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) { 286 TBB = SecondLastInst->getOperand(0).getMBB(); 287 Cond.push_back(SecondLastInst->getOperand(1)); 288 Cond.push_back(SecondLastInst->getOperand(2)); 289 FBB = LastInst->getOperand(0).getMBB(); 290 return false; 291 } 292 293 // If the block ends with two unconditional branches, handle it. The second 294 // one is not executed, so remove it. 295 if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) { 296 TBB = SecondLastInst->getOperand(0).getMBB(); 297 I = LastInst; 298 if (AllowModify) 299 I->eraseFromParent(); 300 return false; 301 } 302 303 // ...likewise if it ends with a branch table followed by an unconditional 304 // branch. The branch folder can create these, and we must get rid of them for 305 // correctness of Thumb constant islands. 306 if ((isJumpTableBranchOpcode(SecondLastOpc) || 307 isIndirectBranchOpcode(SecondLastOpc)) && 308 isUncondBranchOpcode(LastOpc)) { 309 I = LastInst; 310 if (AllowModify) 311 I->eraseFromParent(); 312 return true; 313 } 314 315 // Otherwise, can't handle this. 316 return true; 317 } 318 319 320 unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const { 321 MachineBasicBlock::iterator I = MBB.end(); 322 if (I == MBB.begin()) return 0; 323 --I; 324 while (I->isDebugValue()) { 325 if (I == MBB.begin()) 326 return 0; 327 --I; 328 } 329 if (!isUncondBranchOpcode(I->getOpcode()) && 330 !isCondBranchOpcode(I->getOpcode())) 331 return 0; 332 333 // Remove the branch. 334 I->eraseFromParent(); 335 336 I = MBB.end(); 337 338 if (I == MBB.begin()) return 1; 339 --I; 340 if (!isCondBranchOpcode(I->getOpcode())) 341 return 1; 342 343 // Remove the branch. 344 I->eraseFromParent(); 345 return 2; 346 } 347 348 unsigned 349 ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, 350 MachineBasicBlock *FBB, 351 const SmallVectorImpl<MachineOperand> &Cond, 352 DebugLoc DL) const { 353 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>(); 354 int BOpc = !AFI->isThumbFunction() 355 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB); 356 int BccOpc = !AFI->isThumbFunction() 357 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc); 358 359 // Shouldn't be a fall through. 360 assert(TBB && "InsertBranch must not be told to insert a fallthrough"); 361 assert((Cond.size() == 2 || Cond.size() == 0) && 362 "ARM branch conditions have two components!"); 363 364 if (FBB == 0) { 365 if (Cond.empty()) // Unconditional branch? 366 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB); 367 else 368 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB) 369 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()); 370 return 1; 371 } 372 373 // Two-way conditional branch. 374 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB) 375 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()); 376 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB); 377 return 2; 378 } 379 380 bool ARMBaseInstrInfo:: 381 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { 382 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm(); 383 Cond[0].setImm(ARMCC::getOppositeCondition(CC)); 384 return false; 385 } 386 387 bool ARMBaseInstrInfo:: 388 PredicateInstruction(MachineInstr *MI, 389 const SmallVectorImpl<MachineOperand> &Pred) const { 390 unsigned Opc = MI->getOpcode(); 391 if (isUncondBranchOpcode(Opc)) { 392 MI->setDesc(get(getMatchingCondBranchOpcode(Opc))); 393 MI->addOperand(MachineOperand::CreateImm(Pred[0].getImm())); 394 MI->addOperand(MachineOperand::CreateReg(Pred[1].getReg(), false)); 395 return true; 396 } 397 398 int PIdx = MI->findFirstPredOperandIdx(); 399 if (PIdx != -1) { 400 MachineOperand &PMO = MI->getOperand(PIdx); 401 PMO.setImm(Pred[0].getImm()); 402 MI->getOperand(PIdx+1).setReg(Pred[1].getReg()); 403 return true; 404 } 405 return false; 406 } 407 408 bool ARMBaseInstrInfo:: 409 SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1, 410 const SmallVectorImpl<MachineOperand> &Pred2) const { 411 if (Pred1.size() > 2 || Pred2.size() > 2) 412 return false; 413 414 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm(); 415 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm(); 416 if (CC1 == CC2) 417 return true; 418 419 switch (CC1) { 420 default: 421 return false; 422 case ARMCC::AL: 423 return true; 424 case ARMCC::HS: 425 return CC2 == ARMCC::HI; 426 case ARMCC::LS: 427 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ; 428 case ARMCC::GE: 429 return CC2 == ARMCC::GT; 430 case ARMCC::LE: 431 return CC2 == ARMCC::LT; 432 } 433 } 434 435 bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI, 436 std::vector<MachineOperand> &Pred) const { 437 // FIXME: This confuses implicit_def with optional CPSR def. 438 const TargetInstrDesc &TID = MI->getDesc(); 439 if (!TID.getImplicitDefs() && !TID.hasOptionalDef()) 440 return false; 441 442 bool Found = false; 443 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 444 const MachineOperand &MO = MI->getOperand(i); 445 if (MO.isReg() && MO.getReg() == ARM::CPSR) { 446 Pred.push_back(MO); 447 Found = true; 448 } 449 } 450 451 return Found; 452 } 453 454 /// isPredicable - Return true if the specified instruction can be predicated. 455 /// By default, this returns true for every instruction with a 456 /// PredicateOperand. 457 bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const { 458 const TargetInstrDesc &TID = MI->getDesc(); 459 if (!TID.isPredicable()) 460 return false; 461 462 if ((TID.TSFlags & ARMII::DomainMask) == ARMII::DomainNEON) { 463 ARMFunctionInfo *AFI = 464 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>(); 465 return AFI->isThumb2Function(); 466 } 467 return true; 468 } 469 470 /// FIXME: Works around a gcc miscompilation with -fstrict-aliasing. 471 DISABLE_INLINE 472 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT, 473 unsigned JTI); 474 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT, 475 unsigned JTI) { 476 assert(JTI < JT.size()); 477 return JT[JTI].MBBs.size(); 478 } 479 480 /// GetInstSize - Return the size of the specified MachineInstr. 481 /// 482 unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const { 483 const MachineBasicBlock &MBB = *MI->getParent(); 484 const MachineFunction *MF = MBB.getParent(); 485 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo(); 486 487 // Basic size info comes from the TSFlags field. 488 const TargetInstrDesc &TID = MI->getDesc(); 489 uint64_t TSFlags = TID.TSFlags; 490 491 unsigned Opc = MI->getOpcode(); 492 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) { 493 default: { 494 // If this machine instr is an inline asm, measure it. 495 if (MI->getOpcode() == ARM::INLINEASM) 496 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI); 497 if (MI->isLabel()) 498 return 0; 499 switch (Opc) { 500 default: 501 llvm_unreachable("Unknown or unset size field for instr!"); 502 case TargetOpcode::IMPLICIT_DEF: 503 case TargetOpcode::KILL: 504 case TargetOpcode::DBG_LABEL: 505 case TargetOpcode::EH_LABEL: 506 case TargetOpcode::DBG_VALUE: 507 return 0; 508 } 509 break; 510 } 511 case ARMII::Size8Bytes: return 8; // ARM instruction x 2. 512 case ARMII::Size4Bytes: return 4; // ARM / Thumb2 instruction. 513 case ARMII::Size2Bytes: return 2; // Thumb1 instruction. 514 case ARMII::SizeSpecial: { 515 switch (Opc) { 516 case ARM::CONSTPOOL_ENTRY: 517 // If this machine instr is a constant pool entry, its size is recorded as 518 // operand #2. 519 return MI->getOperand(2).getImm(); 520 case ARM::Int_eh_sjlj_longjmp: 521 return 16; 522 case ARM::tInt_eh_sjlj_longjmp: 523 return 10; 524 case ARM::Int_eh_sjlj_setjmp: 525 case ARM::Int_eh_sjlj_setjmp_nofp: 526 return 20; 527 case ARM::tInt_eh_sjlj_setjmp: 528 case ARM::t2Int_eh_sjlj_setjmp: 529 case ARM::t2Int_eh_sjlj_setjmp_nofp: 530 return 12; 531 case ARM::BR_JTr: 532 case ARM::BR_JTm: 533 case ARM::BR_JTadd: 534 case ARM::tBR_JTr: 535 case ARM::t2BR_JT: 536 case ARM::t2TBB: 537 case ARM::t2TBH: { 538 // These are jumptable branches, i.e. a branch followed by an inlined 539 // jumptable. The size is 4 + 4 * number of entries. For TBB, each 540 // entry is one byte; TBH two byte each. 541 unsigned EntrySize = (Opc == ARM::t2TBB) 542 ? 1 : ((Opc == ARM::t2TBH) ? 2 : 4); 543 unsigned NumOps = TID.getNumOperands(); 544 MachineOperand JTOP = 545 MI->getOperand(NumOps - (TID.isPredicable() ? 3 : 2)); 546 unsigned JTI = JTOP.getIndex(); 547 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo(); 548 assert(MJTI != 0); 549 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); 550 assert(JTI < JT.size()); 551 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte 552 // 4 aligned. The assembler / linker may add 2 byte padding just before 553 // the JT entries. The size does not include this padding; the 554 // constant islands pass does separate bookkeeping for it. 555 // FIXME: If we know the size of the function is less than (1 << 16) *2 556 // bytes, we can use 16-bit entries instead. Then there won't be an 557 // alignment issue. 558 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4; 559 unsigned NumEntries = getNumJTEntries(JT, JTI); 560 if (Opc == ARM::t2TBB && (NumEntries & 1)) 561 // Make sure the instruction that follows TBB is 2-byte aligned. 562 // FIXME: Constant island pass should insert an "ALIGN" instruction 563 // instead. 564 ++NumEntries; 565 return NumEntries * EntrySize + InstSize; 566 } 567 default: 568 // Otherwise, pseudo-instruction sizes are zero. 569 return 0; 570 } 571 } 572 } 573 return 0; // Not reached 574 } 575 576 /// Return true if the instruction is a register to register move and 577 /// leave the source and dest operands in the passed parameters. 578 /// 579 bool 580 ARMBaseInstrInfo::isMoveInstr(const MachineInstr &MI, 581 unsigned &SrcReg, unsigned &DstReg, 582 unsigned& SrcSubIdx, unsigned& DstSubIdx) const { 583 switch (MI.getOpcode()) { 584 default: break; 585 case ARM::VMOVS: 586 case ARM::VMOVD: 587 case ARM::VMOVDneon: 588 case ARM::VMOVQ: 589 case ARM::VMOVQQ : { 590 SrcReg = MI.getOperand(1).getReg(); 591 DstReg = MI.getOperand(0).getReg(); 592 SrcSubIdx = MI.getOperand(1).getSubReg(); 593 DstSubIdx = MI.getOperand(0).getSubReg(); 594 return true; 595 } 596 case ARM::MOVr: 597 case ARM::MOVr_TC: 598 case ARM::tMOVr: 599 case ARM::tMOVgpr2tgpr: 600 case ARM::tMOVtgpr2gpr: 601 case ARM::tMOVgpr2gpr: 602 case ARM::t2MOVr: { 603 assert(MI.getDesc().getNumOperands() >= 2 && 604 MI.getOperand(0).isReg() && 605 MI.getOperand(1).isReg() && 606 "Invalid ARM MOV instruction"); 607 SrcReg = MI.getOperand(1).getReg(); 608 DstReg = MI.getOperand(0).getReg(); 609 SrcSubIdx = MI.getOperand(1).getSubReg(); 610 DstSubIdx = MI.getOperand(0).getSubReg(); 611 return true; 612 } 613 } 614 615 return false; 616 } 617 618 unsigned 619 ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI, 620 int &FrameIndex) const { 621 switch (MI->getOpcode()) { 622 default: break; 623 case ARM::LDR: 624 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame. 625 if (MI->getOperand(1).isFI() && 626 MI->getOperand(2).isReg() && 627 MI->getOperand(3).isImm() && 628 MI->getOperand(2).getReg() == 0 && 629 MI->getOperand(3).getImm() == 0) { 630 FrameIndex = MI->getOperand(1).getIndex(); 631 return MI->getOperand(0).getReg(); 632 } 633 break; 634 case ARM::t2LDRi12: 635 case ARM::tRestore: 636 if (MI->getOperand(1).isFI() && 637 MI->getOperand(2).isImm() && 638 MI->getOperand(2).getImm() == 0) { 639 FrameIndex = MI->getOperand(1).getIndex(); 640 return MI->getOperand(0).getReg(); 641 } 642 break; 643 case ARM::VLDRD: 644 case ARM::VLDRS: 645 if (MI->getOperand(1).isFI() && 646 MI->getOperand(2).isImm() && 647 MI->getOperand(2).getImm() == 0) { 648 FrameIndex = MI->getOperand(1).getIndex(); 649 return MI->getOperand(0).getReg(); 650 } 651 break; 652 } 653 654 return 0; 655 } 656 657 unsigned 658 ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI, 659 int &FrameIndex) const { 660 switch (MI->getOpcode()) { 661 default: break; 662 case ARM::STR: 663 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame. 664 if (MI->getOperand(1).isFI() && 665 MI->getOperand(2).isReg() && 666 MI->getOperand(3).isImm() && 667 MI->getOperand(2).getReg() == 0 && 668 MI->getOperand(3).getImm() == 0) { 669 FrameIndex = MI->getOperand(1).getIndex(); 670 return MI->getOperand(0).getReg(); 671 } 672 break; 673 case ARM::t2STRi12: 674 case ARM::tSpill: 675 if (MI->getOperand(1).isFI() && 676 MI->getOperand(2).isImm() && 677 MI->getOperand(2).getImm() == 0) { 678 FrameIndex = MI->getOperand(1).getIndex(); 679 return MI->getOperand(0).getReg(); 680 } 681 break; 682 case ARM::VSTRD: 683 case ARM::VSTRS: 684 if (MI->getOperand(1).isFI() && 685 MI->getOperand(2).isImm() && 686 MI->getOperand(2).getImm() == 0) { 687 FrameIndex = MI->getOperand(1).getIndex(); 688 return MI->getOperand(0).getReg(); 689 } 690 break; 691 } 692 693 return 0; 694 } 695 696 void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 697 MachineBasicBlock::iterator I, DebugLoc DL, 698 unsigned DestReg, unsigned SrcReg, 699 bool KillSrc) const { 700 bool GPRDest = ARM::GPRRegClass.contains(DestReg); 701 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg); 702 703 if (GPRDest && GPRSrc) { 704 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg) 705 .addReg(SrcReg, getKillRegState(KillSrc)))); 706 return; 707 } 708 709 bool SPRDest = ARM::SPRRegClass.contains(DestReg); 710 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg); 711 712 unsigned Opc; 713 if (SPRDest && SPRSrc) 714 Opc = ARM::VMOVS; 715 else if (GPRDest && SPRSrc) 716 Opc = ARM::VMOVRS; 717 else if (SPRDest && GPRSrc) 718 Opc = ARM::VMOVSR; 719 else if (ARM::DPRRegClass.contains(DestReg, SrcReg)) 720 Opc = ARM::VMOVD; 721 else if (ARM::QPRRegClass.contains(DestReg, SrcReg)) 722 Opc = ARM::VMOVQ; 723 else if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) 724 Opc = ARM::VMOVQQ; 725 else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) 726 Opc = ARM::VMOVQQQQ; 727 else 728 llvm_unreachable("Impossible reg-to-reg copy"); 729 730 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg); 731 MIB.addReg(SrcReg, getKillRegState(KillSrc)); 732 if (Opc != ARM::VMOVQQ && Opc != ARM::VMOVQQQQ) 733 AddDefaultPred(MIB); 734 } 735 736 static const 737 MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB, 738 unsigned Reg, unsigned SubIdx, unsigned State, 739 const TargetRegisterInfo *TRI) { 740 if (!SubIdx) 741 return MIB.addReg(Reg, State); 742 743 if (TargetRegisterInfo::isPhysicalRegister(Reg)) 744 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State); 745 return MIB.addReg(Reg, State, SubIdx); 746 } 747 748 void ARMBaseInstrInfo:: 749 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, 750 unsigned SrcReg, bool isKill, int FI, 751 const TargetRegisterClass *RC, 752 const TargetRegisterInfo *TRI) const { 753 DebugLoc DL; 754 if (I != MBB.end()) DL = I->getDebugLoc(); 755 MachineFunction &MF = *MBB.getParent(); 756 MachineFrameInfo &MFI = *MF.getFrameInfo(); 757 unsigned Align = MFI.getObjectAlignment(FI); 758 759 MachineMemOperand *MMO = 760 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FI), 761 MachineMemOperand::MOStore, 0, 762 MFI.getObjectSize(FI), 763 Align); 764 765 // tGPR is used sometimes in ARM instructions that need to avoid using 766 // certain registers. Just treat it as GPR here. 767 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass) 768 RC = ARM::GPRRegisterClass; 769 770 switch (RC->getID()) { 771 case ARM::GPRRegClassID: 772 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STR)) 773 .addReg(SrcReg, getKillRegState(isKill)) 774 .addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)); 775 break; 776 case ARM::SPRRegClassID: 777 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS)) 778 .addReg(SrcReg, getKillRegState(isKill)) 779 .addFrameIndex(FI).addImm(0).addMemOperand(MMO)); 780 break; 781 case ARM::DPRRegClassID: 782 case ARM::DPR_VFP2RegClassID: 783 case ARM::DPR_8RegClassID: 784 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD)) 785 .addReg(SrcReg, getKillRegState(isKill)) 786 .addFrameIndex(FI).addImm(0).addMemOperand(MMO)); 787 break; 788 case ARM::QPRRegClassID: 789 case ARM::QPR_VFP2RegClassID: 790 case ARM::QPR_8RegClassID: 791 // FIXME: Neon instructions should support predicates 792 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) { 793 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q)) 794 .addFrameIndex(FI).addImm(16) 795 .addReg(SrcReg, getKillRegState(isKill)) 796 .addMemOperand(MMO)); 797 } else { 798 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQ)) 799 .addReg(SrcReg, getKillRegState(isKill)) 800 .addFrameIndex(FI) 801 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4)) 802 .addMemOperand(MMO)); 803 } 804 break; 805 case ARM::QQPRRegClassID: 806 case ARM::QQPR_VFP2RegClassID: 807 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) { 808 // FIXME: It's possible to only store part of the QQ register if the 809 // spilled def has a sub-register index. 810 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VST1d64Q)) 811 .addFrameIndex(FI).addImm(16); 812 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI); 813 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI); 814 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI); 815 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI); 816 AddDefaultPred(MIB.addMemOperand(MMO)); 817 } else { 818 MachineInstrBuilder MIB = 819 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMD)) 820 .addFrameIndex(FI) 821 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4))) 822 .addMemOperand(MMO); 823 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI); 824 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI); 825 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI); 826 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI); 827 } 828 break; 829 case ARM::QQQQPRRegClassID: { 830 MachineInstrBuilder MIB = 831 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMD)) 832 .addFrameIndex(FI) 833 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4))) 834 .addMemOperand(MMO); 835 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI); 836 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI); 837 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI); 838 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI); 839 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI); 840 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI); 841 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI); 842 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI); 843 break; 844 } 845 default: 846 llvm_unreachable("Unknown regclass!"); 847 } 848 } 849 850 void ARMBaseInstrInfo:: 851 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, 852 unsigned DestReg, int FI, 853 const TargetRegisterClass *RC, 854 const TargetRegisterInfo *TRI) const { 855 DebugLoc DL; 856 if (I != MBB.end()) DL = I->getDebugLoc(); 857 MachineFunction &MF = *MBB.getParent(); 858 MachineFrameInfo &MFI = *MF.getFrameInfo(); 859 unsigned Align = MFI.getObjectAlignment(FI); 860 MachineMemOperand *MMO = 861 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FI), 862 MachineMemOperand::MOLoad, 0, 863 MFI.getObjectSize(FI), 864 Align); 865 866 // tGPR is used sometimes in ARM instructions that need to avoid using 867 // certain registers. Just treat it as GPR here. 868 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass) 869 RC = ARM::GPRRegisterClass; 870 871 switch (RC->getID()) { 872 case ARM::GPRRegClassID: 873 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDR), DestReg) 874 .addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)); 875 break; 876 case ARM::SPRRegClassID: 877 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg) 878 .addFrameIndex(FI).addImm(0).addMemOperand(MMO)); 879 break; 880 case ARM::DPRRegClassID: 881 case ARM::DPR_VFP2RegClassID: 882 case ARM::DPR_8RegClassID: 883 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg) 884 .addFrameIndex(FI).addImm(0).addMemOperand(MMO)); 885 break; 886 case ARM::QPRRegClassID: 887 case ARM::QPR_VFP2RegClassID: 888 case ARM::QPR_8RegClassID: 889 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) { 890 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q), DestReg) 891 .addFrameIndex(FI).addImm(16) 892 .addMemOperand(MMO)); 893 } else { 894 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQ), DestReg) 895 .addFrameIndex(FI) 896 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4)) 897 .addMemOperand(MMO)); 898 } 899 break; 900 case ARM::QQPRRegClassID: 901 case ARM::QQPR_VFP2RegClassID: 902 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) { 903 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLD1d64Q)); 904 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI); 905 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI); 906 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI); 907 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI); 908 AddDefaultPred(MIB.addFrameIndex(FI).addImm(16).addMemOperand(MMO)); 909 } else { 910 MachineInstrBuilder MIB = 911 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMD)) 912 .addFrameIndex(FI) 913 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4))) 914 .addMemOperand(MMO); 915 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI); 916 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI); 917 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI); 918 AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI); 919 } 920 break; 921 case ARM::QQQQPRRegClassID: { 922 MachineInstrBuilder MIB = 923 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMD)) 924 .addFrameIndex(FI) 925 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4))) 926 .addMemOperand(MMO); 927 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI); 928 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI); 929 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI); 930 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI); 931 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::Define, TRI); 932 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::Define, TRI); 933 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::Define, TRI); 934 AddDReg(MIB, DestReg, ARM::dsub_7, RegState::Define, TRI); 935 break; 936 } 937 default: 938 llvm_unreachable("Unknown regclass!"); 939 } 940 } 941 942 MachineInstr* 943 ARMBaseInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, 944 int FrameIx, uint64_t Offset, 945 const MDNode *MDPtr, 946 DebugLoc DL) const { 947 MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM::DBG_VALUE)) 948 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr); 949 return &*MIB; 950 } 951 952 /// Create a copy of a const pool value. Update CPI to the new index and return 953 /// the label UID. 954 static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) { 955 MachineConstantPool *MCP = MF.getConstantPool(); 956 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 957 958 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI]; 959 assert(MCPE.isMachineConstantPoolEntry() && 960 "Expecting a machine constantpool entry!"); 961 ARMConstantPoolValue *ACPV = 962 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal); 963 964 unsigned PCLabelId = AFI->createConstPoolEntryUId(); 965 ARMConstantPoolValue *NewCPV = 0; 966 if (ACPV->isGlobalValue()) 967 NewCPV = new ARMConstantPoolValue(ACPV->getGV(), PCLabelId, 968 ARMCP::CPValue, 4); 969 else if (ACPV->isExtSymbol()) 970 NewCPV = new ARMConstantPoolValue(MF.getFunction()->getContext(), 971 ACPV->getSymbol(), PCLabelId, 4); 972 else if (ACPV->isBlockAddress()) 973 NewCPV = new ARMConstantPoolValue(ACPV->getBlockAddress(), PCLabelId, 974 ARMCP::CPBlockAddress, 4); 975 else 976 llvm_unreachable("Unexpected ARM constantpool value type!!"); 977 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment()); 978 return PCLabelId; 979 } 980 981 void ARMBaseInstrInfo:: 982 reMaterialize(MachineBasicBlock &MBB, 983 MachineBasicBlock::iterator I, 984 unsigned DestReg, unsigned SubIdx, 985 const MachineInstr *Orig, 986 const TargetRegisterInfo &TRI) const { 987 unsigned Opcode = Orig->getOpcode(); 988 switch (Opcode) { 989 default: { 990 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig); 991 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI); 992 MBB.insert(I, MI); 993 break; 994 } 995 case ARM::tLDRpci_pic: 996 case ARM::t2LDRpci_pic: { 997 MachineFunction &MF = *MBB.getParent(); 998 unsigned CPI = Orig->getOperand(1).getIndex(); 999 unsigned PCLabelId = duplicateCPV(MF, CPI); 1000 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode), 1001 DestReg) 1002 .addConstantPoolIndex(CPI).addImm(PCLabelId); 1003 (*MIB).setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end()); 1004 break; 1005 } 1006 } 1007 } 1008 1009 MachineInstr * 1010 ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const { 1011 MachineInstr *MI = TargetInstrInfoImpl::duplicate(Orig, MF); 1012 switch(Orig->getOpcode()) { 1013 case ARM::tLDRpci_pic: 1014 case ARM::t2LDRpci_pic: { 1015 unsigned CPI = Orig->getOperand(1).getIndex(); 1016 unsigned PCLabelId = duplicateCPV(MF, CPI); 1017 Orig->getOperand(1).setIndex(CPI); 1018 Orig->getOperand(2).setImm(PCLabelId); 1019 break; 1020 } 1021 } 1022 return MI; 1023 } 1024 1025 bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0, 1026 const MachineInstr *MI1) const { 1027 int Opcode = MI0->getOpcode(); 1028 if (Opcode == ARM::t2LDRpci || 1029 Opcode == ARM::t2LDRpci_pic || 1030 Opcode == ARM::tLDRpci || 1031 Opcode == ARM::tLDRpci_pic) { 1032 if (MI1->getOpcode() != Opcode) 1033 return false; 1034 if (MI0->getNumOperands() != MI1->getNumOperands()) 1035 return false; 1036 1037 const MachineOperand &MO0 = MI0->getOperand(1); 1038 const MachineOperand &MO1 = MI1->getOperand(1); 1039 if (MO0.getOffset() != MO1.getOffset()) 1040 return false; 1041 1042 const MachineFunction *MF = MI0->getParent()->getParent(); 1043 const MachineConstantPool *MCP = MF->getConstantPool(); 1044 int CPI0 = MO0.getIndex(); 1045 int CPI1 = MO1.getIndex(); 1046 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0]; 1047 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1]; 1048 ARMConstantPoolValue *ACPV0 = 1049 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal); 1050 ARMConstantPoolValue *ACPV1 = 1051 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal); 1052 return ACPV0->hasSameValue(ACPV1); 1053 } 1054 1055 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs); 1056 } 1057 1058 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to 1059 /// determine if two loads are loading from the same base address. It should 1060 /// only return true if the base pointers are the same and the only differences 1061 /// between the two addresses is the offset. It also returns the offsets by 1062 /// reference. 1063 bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2, 1064 int64_t &Offset1, 1065 int64_t &Offset2) const { 1066 // Don't worry about Thumb: just ARM and Thumb2. 1067 if (Subtarget.isThumb1Only()) return false; 1068 1069 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode()) 1070 return false; 1071 1072 switch (Load1->getMachineOpcode()) { 1073 default: 1074 return false; 1075 case ARM::LDR: 1076 case ARM::LDRB: 1077 case ARM::LDRD: 1078 case ARM::LDRH: 1079 case ARM::LDRSB: 1080 case ARM::LDRSH: 1081 case ARM::VLDRD: 1082 case ARM::VLDRS: 1083 case ARM::t2LDRi8: 1084 case ARM::t2LDRDi8: 1085 case ARM::t2LDRSHi8: 1086 case ARM::t2LDRi12: 1087 case ARM::t2LDRSHi12: 1088 break; 1089 } 1090 1091 switch (Load2->getMachineOpcode()) { 1092 default: 1093 return false; 1094 case ARM::LDR: 1095 case ARM::LDRB: 1096 case ARM::LDRD: 1097 case ARM::LDRH: 1098 case ARM::LDRSB: 1099 case ARM::LDRSH: 1100 case ARM::VLDRD: 1101 case ARM::VLDRS: 1102 case ARM::t2LDRi8: 1103 case ARM::t2LDRDi8: 1104 case ARM::t2LDRSHi8: 1105 case ARM::t2LDRi12: 1106 case ARM::t2LDRSHi12: 1107 break; 1108 } 1109 1110 // Check if base addresses and chain operands match. 1111 if (Load1->getOperand(0) != Load2->getOperand(0) || 1112 Load1->getOperand(4) != Load2->getOperand(4)) 1113 return false; 1114 1115 // Index should be Reg0. 1116 if (Load1->getOperand(3) != Load2->getOperand(3)) 1117 return false; 1118 1119 // Determine the offsets. 1120 if (isa<ConstantSDNode>(Load1->getOperand(1)) && 1121 isa<ConstantSDNode>(Load2->getOperand(1))) { 1122 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue(); 1123 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue(); 1124 return true; 1125 } 1126 1127 return false; 1128 } 1129 1130 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to 1131 /// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should 1132 /// be scheduled togther. On some targets if two loads are loading from 1133 /// addresses in the same cache line, it's better if they are scheduled 1134 /// together. This function takes two integers that represent the load offsets 1135 /// from the common base address. It returns true if it decides it's desirable 1136 /// to schedule the two loads together. "NumLoads" is the number of loads that 1137 /// have already been scheduled after Load1. 1138 bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2, 1139 int64_t Offset1, int64_t Offset2, 1140 unsigned NumLoads) const { 1141 // Don't worry about Thumb: just ARM and Thumb2. 1142 if (Subtarget.isThumb1Only()) return false; 1143 1144 assert(Offset2 > Offset1); 1145 1146 if ((Offset2 - Offset1) / 8 > 64) 1147 return false; 1148 1149 if (Load1->getMachineOpcode() != Load2->getMachineOpcode()) 1150 return false; // FIXME: overly conservative? 1151 1152 // Four loads in a row should be sufficient. 1153 if (NumLoads >= 3) 1154 return false; 1155 1156 return true; 1157 } 1158 1159 bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI, 1160 const MachineBasicBlock *MBB, 1161 const MachineFunction &MF) const { 1162 // Debug info is never a scheduling boundary. It's necessary to be explicit 1163 // due to the special treatment of IT instructions below, otherwise a 1164 // dbg_value followed by an IT will result in the IT instruction being 1165 // considered a scheduling hazard, which is wrong. It should be the actual 1166 // instruction preceding the dbg_value instruction(s), just like it is 1167 // when debug info is not present. 1168 if (MI->isDebugValue()) 1169 return false; 1170 1171 // Terminators and labels can't be scheduled around. 1172 if (MI->getDesc().isTerminator() || MI->isLabel()) 1173 return true; 1174 1175 // Treat the start of the IT block as a scheduling boundary, but schedule 1176 // t2IT along with all instructions following it. 1177 // FIXME: This is a big hammer. But the alternative is to add all potential 1178 // true and anti dependencies to IT block instructions as implicit operands 1179 // to the t2IT instruction. The added compile time and complexity does not 1180 // seem worth it. 1181 MachineBasicBlock::const_iterator I = MI; 1182 // Make sure to skip any dbg_value instructions 1183 while (++I != MBB->end() && I->isDebugValue()) 1184 ; 1185 if (I != MBB->end() && I->getOpcode() == ARM::t2IT) 1186 return true; 1187 1188 // Don't attempt to schedule around any instruction that defines 1189 // a stack-oriented pointer, as it's unlikely to be profitable. This 1190 // saves compile time, because it doesn't require every single 1191 // stack slot reference to depend on the instruction that does the 1192 // modification. 1193 if (MI->definesRegister(ARM::SP)) 1194 return true; 1195 1196 return false; 1197 } 1198 1199 bool ARMBaseInstrInfo:: 1200 isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumInstrs) const { 1201 if (!NumInstrs) 1202 return false; 1203 if (Subtarget.getCPUString() == "generic") 1204 // Generic (and overly aggressive) if-conversion limits for testing. 1205 return NumInstrs <= 10; 1206 else if (Subtarget.hasV7Ops()) 1207 return NumInstrs <= 3; 1208 return NumInstrs <= 2; 1209 } 1210 1211 bool ARMBaseInstrInfo:: 1212 isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumT, 1213 MachineBasicBlock &FMBB, unsigned NumF) const { 1214 return NumT && NumF && NumT <= 2 && NumF <= 2; 1215 } 1216 1217 /// getInstrPredicate - If instruction is predicated, returns its predicate 1218 /// condition, otherwise returns AL. It also returns the condition code 1219 /// register by reference. 1220 ARMCC::CondCodes 1221 llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) { 1222 int PIdx = MI->findFirstPredOperandIdx(); 1223 if (PIdx == -1) { 1224 PredReg = 0; 1225 return ARMCC::AL; 1226 } 1227 1228 PredReg = MI->getOperand(PIdx+1).getReg(); 1229 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm(); 1230 } 1231 1232 1233 int llvm::getMatchingCondBranchOpcode(int Opc) { 1234 if (Opc == ARM::B) 1235 return ARM::Bcc; 1236 else if (Opc == ARM::tB) 1237 return ARM::tBcc; 1238 else if (Opc == ARM::t2B) 1239 return ARM::t2Bcc; 1240 1241 llvm_unreachable("Unknown unconditional branch opcode!"); 1242 return 0; 1243 } 1244 1245 1246 void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB, 1247 MachineBasicBlock::iterator &MBBI, DebugLoc dl, 1248 unsigned DestReg, unsigned BaseReg, int NumBytes, 1249 ARMCC::CondCodes Pred, unsigned PredReg, 1250 const ARMBaseInstrInfo &TII) { 1251 bool isSub = NumBytes < 0; 1252 if (isSub) NumBytes = -NumBytes; 1253 1254 while (NumBytes) { 1255 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes); 1256 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt); 1257 assert(ThisVal && "Didn't extract field correctly"); 1258 1259 // We will handle these bits from offset, clear them. 1260 NumBytes &= ~ThisVal; 1261 1262 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?"); 1263 1264 // Build the new ADD / SUB. 1265 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri; 1266 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg) 1267 .addReg(BaseReg, RegState::Kill).addImm(ThisVal) 1268 .addImm((unsigned)Pred).addReg(PredReg).addReg(0); 1269 BaseReg = DestReg; 1270 } 1271 } 1272 1273 bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx, 1274 unsigned FrameReg, int &Offset, 1275 const ARMBaseInstrInfo &TII) { 1276 unsigned Opcode = MI.getOpcode(); 1277 const TargetInstrDesc &Desc = MI.getDesc(); 1278 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask); 1279 bool isSub = false; 1280 1281 // Memory operands in inline assembly always use AddrMode2. 1282 if (Opcode == ARM::INLINEASM) 1283 AddrMode = ARMII::AddrMode2; 1284 1285 if (Opcode == ARM::ADDri) { 1286 Offset += MI.getOperand(FrameRegIdx+1).getImm(); 1287 if (Offset == 0) { 1288 // Turn it into a move. 1289 MI.setDesc(TII.get(ARM::MOVr)); 1290 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); 1291 MI.RemoveOperand(FrameRegIdx+1); 1292 Offset = 0; 1293 return true; 1294 } else if (Offset < 0) { 1295 Offset = -Offset; 1296 isSub = true; 1297 MI.setDesc(TII.get(ARM::SUBri)); 1298 } 1299 1300 // Common case: small offset, fits into instruction. 1301 if (ARM_AM::getSOImmVal(Offset) != -1) { 1302 // Replace the FrameIndex with sp / fp 1303 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); 1304 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset); 1305 Offset = 0; 1306 return true; 1307 } 1308 1309 // Otherwise, pull as much of the immedidate into this ADDri/SUBri 1310 // as possible. 1311 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset); 1312 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt); 1313 1314 // We will handle these bits from offset, clear them. 1315 Offset &= ~ThisImmVal; 1316 1317 // Get the properly encoded SOImmVal field. 1318 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 && 1319 "Bit extraction didn't work?"); 1320 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal); 1321 } else { 1322 unsigned ImmIdx = 0; 1323 int InstrOffs = 0; 1324 unsigned NumBits = 0; 1325 unsigned Scale = 1; 1326 switch (AddrMode) { 1327 case ARMII::AddrMode2: { 1328 ImmIdx = FrameRegIdx+2; 1329 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm()); 1330 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub) 1331 InstrOffs *= -1; 1332 NumBits = 12; 1333 break; 1334 } 1335 case ARMII::AddrMode3: { 1336 ImmIdx = FrameRegIdx+2; 1337 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm()); 1338 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub) 1339 InstrOffs *= -1; 1340 NumBits = 8; 1341 break; 1342 } 1343 case ARMII::AddrMode4: 1344 case ARMII::AddrMode6: 1345 // Can't fold any offset even if it's zero. 1346 return false; 1347 case ARMII::AddrMode5: { 1348 ImmIdx = FrameRegIdx+1; 1349 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm()); 1350 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub) 1351 InstrOffs *= -1; 1352 NumBits = 8; 1353 Scale = 4; 1354 break; 1355 } 1356 default: 1357 llvm_unreachable("Unsupported addressing mode!"); 1358 break; 1359 } 1360 1361 Offset += InstrOffs * Scale; 1362 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!"); 1363 if (Offset < 0) { 1364 Offset = -Offset; 1365 isSub = true; 1366 } 1367 1368 // Attempt to fold address comp. if opcode has offset bits 1369 if (NumBits > 0) { 1370 // Common case: small offset, fits into instruction. 1371 MachineOperand &ImmOp = MI.getOperand(ImmIdx); 1372 int ImmedOffset = Offset / Scale; 1373 unsigned Mask = (1 << NumBits) - 1; 1374 if ((unsigned)Offset <= Mask * Scale) { 1375 // Replace the FrameIndex with sp 1376 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); 1377 if (isSub) 1378 ImmedOffset |= 1 << NumBits; 1379 ImmOp.ChangeToImmediate(ImmedOffset); 1380 Offset = 0; 1381 return true; 1382 } 1383 1384 // Otherwise, it didn't fit. Pull in what we can to simplify the immed. 1385 ImmedOffset = ImmedOffset & Mask; 1386 if (isSub) 1387 ImmedOffset |= 1 << NumBits; 1388 ImmOp.ChangeToImmediate(ImmedOffset); 1389 Offset &= ~(Mask*Scale); 1390 } 1391 } 1392 1393 Offset = (isSub) ? -Offset : Offset; 1394 return Offset == 0; 1395 } 1396