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