Jim Grosbach | 31c24bf | 2009-11-07 22:00:39 +0000 | [diff] [blame] | 1 | //===- ARMBaseInstrInfo.cpp - ARM Instruction Information -------*- C++ -*-===// |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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" |
Evan Cheng | d457e6e | 2009-11-07 04:04:34 +0000 | [diff] [blame] | 17 | #include "ARMConstantPoolValue.h" |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 18 | #include "ARMMachineFunctionInfo.h" |
Anton Korobeynikov | f95215f | 2009-11-02 00:10:38 +0000 | [diff] [blame] | 19 | #include "ARMRegisterInfo.h" |
Chris Lattner | 4dbbe34 | 2010-07-20 21:17:29 +0000 | [diff] [blame] | 20 | #include "ARMGenInstrInfo.inc" |
Evan Cheng | fdc8340 | 2009-11-08 00:15:23 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
| 22 | #include "llvm/Function.h" |
| 23 | #include "llvm/GlobalValue.h" |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
| 25 | #include "llvm/CodeGen/LiveVariables.h" |
Evan Cheng | d457e6e | 2009-11-07 04:04:34 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineConstantPool.h" |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 28 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 29 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Anton Korobeynikov | 249fb33 | 2009-10-07 00:06:35 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineMemOperand.h" |
Evan Cheng | 2457f2c | 2010-05-22 01:47:14 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Anton Korobeynikov | 249fb33 | 2009-10-07 00:06:35 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCAsmInfo.h" |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Anton Korobeynikov | f95215f | 2009-11-02 00:10:38 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ErrorHandling.h" |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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 | |
Anton Korobeynikov | f95215f | 2009-11-02 00:10:38 +0000 | [diff] [blame] | 43 | ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI) |
| 44 | : TargetInstrInfoImpl(ARMInsts, array_lengthof(ARMInsts)), |
| 45 | Subtarget(STI) { |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | MachineInstr * |
| 49 | ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI, |
| 50 | MachineBasicBlock::iterator &MBBI, |
| 51 | LiveVariables *LV) const { |
Evan Cheng | 78703dd | 2009-07-27 18:44:00 +0000 | [diff] [blame] | 52 | // FIXME: Thumb2 support. |
| 53 | |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 54 | if (!EnableARM3Addr) |
| 55 | return NULL; |
| 56 | |
| 57 | MachineInstr *MI = MBBI; |
| 58 | MachineFunction &MF = *MI->getParent()->getParent(); |
Bruno Cardoso Lopes | 99405df | 2010-06-08 22:51:23 +0000 | [diff] [blame] | 59 | uint64_t TSFlags = MI->getDesc().TSFlags; |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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) { |
Evan Cheng | e7cbe41 | 2009-07-08 21:03:57 +0000 | [diff] [blame] | 98 | if (ARM_AM::getSOImmVal(Amt) == -1) |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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(), |
Evan Cheng | 78703dd | 2009-07-27 18:44:00 +0000 | [diff] [blame] | 103 | get(isSub ? ARM::SUBri : ARM::ADDri), WBReg) |
Evan Cheng | e7cbe41 | 2009-07-08 21:03:57 +0000 | [diff] [blame] | 104 | .addReg(BaseReg).addImm(Amt) |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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(), |
Evan Cheng | 78703dd | 2009-07-27 18:44:00 +0000 | [diff] [blame] | 110 | get(isSub ? ARM::SUBrs : ARM::ADDrs), WBReg) |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 111 | .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc) |
| 112 | .addImm(Pred).addReg(0).addReg(0); |
| 113 | } else |
| 114 | UpdateMI = BuildMI(MF, MI->getDebugLoc(), |
Evan Cheng | 78703dd | 2009-07-27 18:44:00 +0000 | [diff] [blame] | 115 | get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg) |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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(), |
Evan Cheng | 78703dd | 2009-07-27 18:44:00 +0000 | [diff] [blame] | 126 | get(isSub ? ARM::SUBri : ARM::ADDri), WBReg) |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 127 | .addReg(BaseReg).addImm(Amt) |
| 128 | .addImm(Pred).addReg(0).addReg(0); |
| 129 | else |
| 130 | UpdateMI = BuildMI(MF, MI->getDebugLoc(), |
Evan Cheng | 78703dd | 2009-07-27 18:44:00 +0000 | [diff] [blame] | 131 | get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg) |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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 | |
Evan Cheng | 2457f2c | 2010-05-22 01:47:14 +0000 | [diff] [blame] | 200 | bool |
| 201 | ARMBaseInstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB, |
Jim Grosbach | 18f30e6 | 2010-06-02 21:53:11 +0000 | [diff] [blame] | 202 | MachineBasicBlock::iterator MI, |
| 203 | const std::vector<CalleeSavedInfo> &CSI, |
| 204 | const TargetRegisterInfo *TRI) const { |
Evan Cheng | 2457f2c | 2010-05-22 01:47:14 +0000 | [diff] [blame] | 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 | // |
Rafael Espindola | 42d075c | 2010-06-02 20:02:30 +0000 | [diff] [blame] | 230 | const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); |
Evan Cheng | 2457f2c | 2010-05-22 01:47:14 +0000 | [diff] [blame] | 231 | storeRegToStackSlot(MBB, MI, Reg, isKill, |
Rafael Espindola | 42d075c | 2010-06-02 20:02:30 +0000 | [diff] [blame] | 232 | CSI[i].getFrameIdx(), RC, TRI); |
Evan Cheng | 2457f2c | 2010-05-22 01:47:14 +0000 | [diff] [blame] | 233 | } |
| 234 | return true; |
| 235 | } |
| 236 | |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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(); |
Dale Johannesen | 93d6a7e | 2010-04-02 01:38:09 +0000 | [diff] [blame] | 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)) |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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)) { |
Evan Cheng | 5ca53a7 | 2009-07-27 18:20:05 +0000 | [diff] [blame] | 262 | if (isUncondBranchOpcode(LastOpc)) { |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 263 | TBB = LastInst->getOperand(0).getMBB(); |
| 264 | return false; |
| 265 | } |
Evan Cheng | 5ca53a7 | 2009-07-27 18:20:05 +0000 | [diff] [blame] | 266 | if (isCondBranchOpcode(LastOpc)) { |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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 | |
Evan Cheng | 5ca53a7 | 2009-07-27 18:20:05 +0000 | [diff] [blame] | 283 | // If the block ends with a B and a Bcc, handle it. |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 284 | unsigned SecondLastOpc = SecondLastInst->getOpcode(); |
Evan Cheng | 5ca53a7 | 2009-07-27 18:20:05 +0000 | [diff] [blame] | 285 | if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) { |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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. |
Evan Cheng | 5ca53a7 | 2009-07-27 18:20:05 +0000 | [diff] [blame] | 295 | if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) { |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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. |
Bob Wilson | 8d4de5a | 2009-10-28 18:26:41 +0000 | [diff] [blame] | 306 | if ((isJumpTableBranchOpcode(SecondLastOpc) || |
| 307 | isIndirectBranchOpcode(SecondLastOpc)) && |
Evan Cheng | 5ca53a7 | 2009-07-27 18:20:05 +0000 | [diff] [blame] | 308 | isUncondBranchOpcode(LastOpc)) { |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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 { |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 321 | MachineBasicBlock::iterator I = MBB.end(); |
| 322 | if (I == MBB.begin()) return 0; |
| 323 | --I; |
Dale Johannesen | 93d6a7e | 2010-04-02 01:38:09 +0000 | [diff] [blame] | 324 | while (I->isDebugValue()) { |
| 325 | if (I == MBB.begin()) |
| 326 | return 0; |
| 327 | --I; |
| 328 | } |
Evan Cheng | 5ca53a7 | 2009-07-27 18:20:05 +0000 | [diff] [blame] | 329 | if (!isUncondBranchOpcode(I->getOpcode()) && |
| 330 | !isCondBranchOpcode(I->getOpcode())) |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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; |
Evan Cheng | 5ca53a7 | 2009-07-27 18:20:05 +0000 | [diff] [blame] | 340 | if (!isCondBranchOpcode(I->getOpcode())) |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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, |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 350 | MachineBasicBlock *FBB, |
| 351 | const SmallVectorImpl<MachineOperand> &Cond, |
| 352 | DebugLoc DL) const { |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 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); |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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? |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 366 | BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB); |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 367 | else |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 368 | BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB) |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 369 | .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()); |
| 370 | return 1; |
| 371 | } |
| 372 | |
| 373 | // Two-way conditional branch. |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 374 | BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB) |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 375 | .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()); |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 376 | BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB); |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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 | |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 387 | bool ARMBaseInstrInfo:: |
| 388 | PredicateInstruction(MachineInstr *MI, |
| 389 | const SmallVectorImpl<MachineOperand> &Pred) const { |
| 390 | unsigned Opc = MI->getOpcode(); |
Evan Cheng | 5ca53a7 | 2009-07-27 18:20:05 +0000 | [diff] [blame] | 391 | if (isUncondBranchOpcode(Opc)) { |
| 392 | MI->setDesc(get(getMatchingCondBranchOpcode(Opc))); |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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 { |
Evan Cheng | 8fb9036 | 2009-08-08 03:20:32 +0000 | [diff] [blame] | 437 | // FIXME: This confuses implicit_def with optional CPSR def. |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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 | |
Evan Cheng | ac0869d | 2009-11-21 06:21:52 +0000 | [diff] [blame] | 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>(); |
Evan Cheng | d7f0810 | 2009-11-24 08:06:15 +0000 | [diff] [blame] | 465 | return AFI->isThumb2Function(); |
Evan Cheng | ac0869d | 2009-11-21 06:21:52 +0000 | [diff] [blame] | 466 | } |
| 467 | return true; |
| 468 | } |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 469 | |
Chris Lattner | 56856b1 | 2009-12-03 06:58:32 +0000 | [diff] [blame] | 470 | /// FIXME: Works around a gcc miscompilation with -fstrict-aliasing. |
| 471 | DISABLE_INLINE |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 472 | static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT, |
Chris Lattner | 56856b1 | 2009-12-03 06:58:32 +0000 | [diff] [blame] | 473 | unsigned JTI); |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 474 | static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT, |
| 475 | unsigned JTI) { |
Chris Lattner | 56856b1 | 2009-12-03 06:58:32 +0000 | [diff] [blame] | 476 | assert(JTI < JT.size()); |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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(); |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 485 | const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo(); |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 486 | |
| 487 | // Basic size info comes from the TSFlags field. |
| 488 | const TargetInstrDesc &TID = MI->getDesc(); |
Bruno Cardoso Lopes | 99405df | 2010-06-08 22:51:23 +0000 | [diff] [blame] | 489 | uint64_t TSFlags = TID.TSFlags; |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 490 | |
Evan Cheng | a0ee862 | 2009-07-31 22:22:22 +0000 | [diff] [blame] | 491 | unsigned Opc = MI->getOpcode(); |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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) |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 496 | return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI); |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 497 | if (MI->isLabel()) |
| 498 | return 0; |
Evan Cheng | a0ee862 | 2009-07-31 22:22:22 +0000 | [diff] [blame] | 499 | switch (Opc) { |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 500 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 501 | llvm_unreachable("Unknown or unset size field for instr!"); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 502 | case TargetOpcode::IMPLICIT_DEF: |
| 503 | case TargetOpcode::KILL: |
Bill Wendling | 7431bea | 2010-07-16 22:20:36 +0000 | [diff] [blame] | 504 | case TargetOpcode::PROLOG_LABEL: |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 505 | case TargetOpcode::EH_LABEL: |
Dale Johannesen | 375be77 | 2010-04-07 19:51:44 +0000 | [diff] [blame] | 506 | case TargetOpcode::DBG_VALUE: |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 507 | return 0; |
| 508 | } |
| 509 | break; |
| 510 | } |
Evan Cheng | 7894762 | 2009-07-24 18:20:44 +0000 | [diff] [blame] | 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. |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 514 | case ARMII::SizeSpecial: { |
Evan Cheng | a0ee862 | 2009-07-31 22:22:22 +0000 | [diff] [blame] | 515 | switch (Opc) { |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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(); |
Jim Grosbach | 5eb1951 | 2010-05-22 01:06:18 +0000 | [diff] [blame] | 520 | case ARM::Int_eh_sjlj_longjmp: |
| 521 | return 16; |
| 522 | case ARM::tInt_eh_sjlj_longjmp: |
| 523 | return 10; |
Evan Cheng | 7894762 | 2009-07-24 18:20:44 +0000 | [diff] [blame] | 524 | case ARM::Int_eh_sjlj_setjmp: |
Jim Grosbach | d100755 | 2010-04-28 20:33:09 +0000 | [diff] [blame] | 525 | case ARM::Int_eh_sjlj_setjmp_nofp: |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 526 | return 20; |
Jim Grosbach | d122874 | 2009-12-01 18:10:36 +0000 | [diff] [blame] | 527 | case ARM::tInt_eh_sjlj_setjmp: |
Jim Grosbach | 5aa1684 | 2009-08-11 19:42:21 +0000 | [diff] [blame] | 528 | case ARM::t2Int_eh_sjlj_setjmp: |
Jim Grosbach | d100755 | 2010-04-28 20:33:09 +0000 | [diff] [blame] | 529 | case ARM::t2Int_eh_sjlj_setjmp_nofp: |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 530 | return 12; |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 531 | case ARM::BR_JTr: |
| 532 | case ARM::BR_JTm: |
| 533 | case ARM::BR_JTadd: |
Evan Cheng | a0ee862 | 2009-07-31 22:22:22 +0000 | [diff] [blame] | 534 | case ARM::tBR_JTr: |
Evan Cheng | d26b14c | 2009-07-31 18:28:05 +0000 | [diff] [blame] | 535 | case ARM::t2BR_JT: |
| 536 | case ARM::t2TBB: |
| 537 | case ARM::t2TBH: { |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 538 | // These are jumptable branches, i.e. a branch followed by an inlined |
Evan Cheng | d26b14c | 2009-07-31 18:28:05 +0000 | [diff] [blame] | 539 | // jumptable. The size is 4 + 4 * number of entries. For TBB, each |
| 540 | // entry is one byte; TBH two byte each. |
Evan Cheng | a0ee862 | 2009-07-31 22:22:22 +0000 | [diff] [blame] | 541 | unsigned EntrySize = (Opc == ARM::t2TBB) |
| 542 | ? 1 : ((Opc == ARM::t2TBH) ? 2 : 4); |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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(); |
Chris Lattner | b1e8039 | 2010-01-25 23:22:00 +0000 | [diff] [blame] | 548 | assert(MJTI != 0); |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 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. |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 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; |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 566 | } |
| 567 | default: |
| 568 | // Otherwise, pseudo-instruction sizes are zero. |
| 569 | return 0; |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | return 0; // Not reached |
| 574 | } |
| 575 | |
Jakob Stoklund Olesen | ac27366 | 2010-07-11 06:33:54 +0000 | [diff] [blame] | 576 | void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB, |
| 577 | MachineBasicBlock::iterator I, DebugLoc DL, |
| 578 | unsigned DestReg, unsigned SrcReg, |
| 579 | bool KillSrc) const { |
| 580 | bool GPRDest = ARM::GPRRegClass.contains(DestReg); |
| 581 | bool GPRSrc = ARM::GPRRegClass.contains(SrcReg); |
Bob Wilson | 1665b0a | 2010-02-16 17:24:15 +0000 | [diff] [blame] | 582 | |
Jakob Stoklund Olesen | ac27366 | 2010-07-11 06:33:54 +0000 | [diff] [blame] | 583 | if (GPRDest && GPRSrc) { |
| 584 | AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg) |
| 585 | .addReg(SrcReg, getKillRegState(KillSrc)))); |
| 586 | return; |
David Goodwin | 7bfdca0 | 2009-08-05 21:02:22 +0000 | [diff] [blame] | 587 | } |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 588 | |
Jakob Stoklund Olesen | ac27366 | 2010-07-11 06:33:54 +0000 | [diff] [blame] | 589 | bool SPRDest = ARM::SPRRegClass.contains(DestReg); |
| 590 | bool SPRSrc = ARM::SPRRegClass.contains(SrcReg); |
| 591 | |
| 592 | unsigned Opc; |
| 593 | if (SPRDest && SPRSrc) |
| 594 | Opc = ARM::VMOVS; |
| 595 | else if (GPRDest && SPRSrc) |
| 596 | Opc = ARM::VMOVRS; |
| 597 | else if (SPRDest && GPRSrc) |
| 598 | Opc = ARM::VMOVSR; |
| 599 | else if (ARM::DPRRegClass.contains(DestReg, SrcReg)) |
| 600 | Opc = ARM::VMOVD; |
| 601 | else if (ARM::QPRRegClass.contains(DestReg, SrcReg)) |
| 602 | Opc = ARM::VMOVQ; |
| 603 | else if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) |
| 604 | Opc = ARM::VMOVQQ; |
| 605 | else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) |
| 606 | Opc = ARM::VMOVQQQQ; |
| 607 | else |
| 608 | llvm_unreachable("Impossible reg-to-reg copy"); |
| 609 | |
| 610 | MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg); |
| 611 | MIB.addReg(SrcReg, getKillRegState(KillSrc)); |
| 612 | if (Opc != ARM::VMOVQQ && Opc != ARM::VMOVQQQQ) |
| 613 | AddDefaultPred(MIB); |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Evan Cheng | c10b5af | 2010-05-07 00:24:52 +0000 | [diff] [blame] | 616 | static const |
| 617 | MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB, |
| 618 | unsigned Reg, unsigned SubIdx, unsigned State, |
| 619 | const TargetRegisterInfo *TRI) { |
| 620 | if (!SubIdx) |
| 621 | return MIB.addReg(Reg, State); |
| 622 | |
| 623 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 624 | return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State); |
| 625 | return MIB.addReg(Reg, State, SubIdx); |
| 626 | } |
| 627 | |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 628 | void ARMBaseInstrInfo:: |
| 629 | storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
| 630 | unsigned SrcReg, bool isKill, int FI, |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 631 | const TargetRegisterClass *RC, |
| 632 | const TargetRegisterInfo *TRI) const { |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 633 | DebugLoc DL; |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 634 | if (I != MBB.end()) DL = I->getDebugLoc(); |
Anton Korobeynikov | 249fb33 | 2009-10-07 00:06:35 +0000 | [diff] [blame] | 635 | MachineFunction &MF = *MBB.getParent(); |
| 636 | MachineFrameInfo &MFI = *MF.getFrameInfo(); |
Jim Grosbach | 31bc849 | 2009-11-08 00:27:19 +0000 | [diff] [blame] | 637 | unsigned Align = MFI.getObjectAlignment(FI); |
Anton Korobeynikov | 249fb33 | 2009-10-07 00:06:35 +0000 | [diff] [blame] | 638 | |
| 639 | MachineMemOperand *MMO = |
Chris Lattner | 59db549 | 2010-09-21 04:39:43 +0000 | [diff] [blame^] | 640 | MF.getMachineMemOperand(MachinePointerInfo( |
| 641 | PseudoSourceValue::getFixedStack(FI)), |
| 642 | MachineMemOperand::MOStore, |
Anton Korobeynikov | 249fb33 | 2009-10-07 00:06:35 +0000 | [diff] [blame] | 643 | MFI.getObjectSize(FI), |
Jim Grosbach | 31bc849 | 2009-11-08 00:27:19 +0000 | [diff] [blame] | 644 | Align); |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 645 | |
Bob Wilson | 0eb0c74 | 2010-02-16 22:01:59 +0000 | [diff] [blame] | 646 | // tGPR is used sometimes in ARM instructions that need to avoid using |
Jim Grosbach | 6ccfc50 | 2010-07-30 02:41:01 +0000 | [diff] [blame] | 647 | // certain registers. Just treat it as GPR here. Likewise, rGPR. |
| 648 | if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass |
| 649 | || RC == ARM::rGPRRegisterClass) |
Bob Wilson | 0eb0c74 | 2010-02-16 22:01:59 +0000 | [diff] [blame] | 650 | RC = ARM::GPRRegisterClass; |
| 651 | |
Bob Wilson | ebe99b2 | 2010-06-18 21:32:42 +0000 | [diff] [blame] | 652 | switch (RC->getID()) { |
| 653 | case ARM::GPRRegClassID: |
Evan Cheng | 5732ca0 | 2009-07-27 03:14:20 +0000 | [diff] [blame] | 654 | AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STR)) |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 655 | .addReg(SrcReg, getKillRegState(isKill)) |
Anton Korobeynikov | 249fb33 | 2009-10-07 00:06:35 +0000 | [diff] [blame] | 656 | .addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)); |
Bob Wilson | ebe99b2 | 2010-06-18 21:32:42 +0000 | [diff] [blame] | 657 | break; |
| 658 | case ARM::SPRRegClassID: |
Evan Cheng | d31c549 | 2010-05-06 01:34:11 +0000 | [diff] [blame] | 659 | AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS)) |
| 660 | .addReg(SrcReg, getKillRegState(isKill)) |
| 661 | .addFrameIndex(FI).addImm(0).addMemOperand(MMO)); |
Bob Wilson | ebe99b2 | 2010-06-18 21:32:42 +0000 | [diff] [blame] | 662 | break; |
| 663 | case ARM::DPRRegClassID: |
| 664 | case ARM::DPR_VFP2RegClassID: |
| 665 | case ARM::DPR_8RegClassID: |
Jim Grosbach | e516549 | 2009-11-09 00:11:35 +0000 | [diff] [blame] | 666 | AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD)) |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 667 | .addReg(SrcReg, getKillRegState(isKill)) |
Anton Korobeynikov | 249fb33 | 2009-10-07 00:06:35 +0000 | [diff] [blame] | 668 | .addFrameIndex(FI).addImm(0).addMemOperand(MMO)); |
Bob Wilson | ebe99b2 | 2010-06-18 21:32:42 +0000 | [diff] [blame] | 669 | break; |
| 670 | case ARM::QPRRegClassID: |
| 671 | case ARM::QPR_VFP2RegClassID: |
| 672 | case ARM::QPR_8RegClassID: |
Jim Grosbach | 0cfcf93 | 2010-09-08 00:26:59 +0000 | [diff] [blame] | 673 | if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) { |
Bob Wilson | 168f382 | 2010-09-15 01:48:05 +0000 | [diff] [blame] | 674 | AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64Pseudo)) |
Bob Wilson | f967ca0 | 2010-07-06 21:26:18 +0000 | [diff] [blame] | 675 | .addFrameIndex(FI).addImm(16) |
Evan Cheng | 69b9f98 | 2010-05-13 01:12:06 +0000 | [diff] [blame] | 676 | .addReg(SrcReg, getKillRegState(isKill)) |
| 677 | .addMemOperand(MMO)); |
Jim Grosbach | 31bc849 | 2009-11-08 00:27:19 +0000 | [diff] [blame] | 678 | } else { |
Evan Cheng | 69b9f98 | 2010-05-13 01:12:06 +0000 | [diff] [blame] | 679 | AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQ)) |
| 680 | .addReg(SrcReg, getKillRegState(isKill)) |
| 681 | .addFrameIndex(FI) |
Bob Wilson | d4bfd54 | 2010-08-27 23:18:17 +0000 | [diff] [blame] | 682 | .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)) |
Evan Cheng | 69b9f98 | 2010-05-13 01:12:06 +0000 | [diff] [blame] | 683 | .addMemOperand(MMO)); |
Jim Grosbach | 31bc849 | 2009-11-08 00:27:19 +0000 | [diff] [blame] | 684 | } |
Bob Wilson | ebe99b2 | 2010-06-18 21:32:42 +0000 | [diff] [blame] | 685 | break; |
| 686 | case ARM::QQPRRegClassID: |
| 687 | case ARM::QQPR_VFP2RegClassID: |
Evan Cheng | 435d499 | 2010-05-07 02:04:02 +0000 | [diff] [blame] | 688 | if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) { |
Evan Cheng | 22c687b | 2010-05-14 02:13:41 +0000 | [diff] [blame] | 689 | // FIXME: It's possible to only store part of the QQ register if the |
| 690 | // spilled def has a sub-register index. |
Bob Wilson | 168f382 | 2010-09-15 01:48:05 +0000 | [diff] [blame] | 691 | AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo)) |
| 692 | .addFrameIndex(FI).addImm(16) |
| 693 | .addReg(SrcReg, getKillRegState(isKill)) |
| 694 | .addMemOperand(MMO)); |
Evan Cheng | 435d499 | 2010-05-07 02:04:02 +0000 | [diff] [blame] | 695 | } else { |
| 696 | MachineInstrBuilder MIB = |
| 697 | AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMD)) |
| 698 | .addFrameIndex(FI) |
Bob Wilson | d4bfd54 | 2010-08-27 23:18:17 +0000 | [diff] [blame] | 699 | .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia))) |
Evan Cheng | 435d499 | 2010-05-07 02:04:02 +0000 | [diff] [blame] | 700 | .addMemOperand(MMO); |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 701 | MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI); |
| 702 | MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI); |
| 703 | MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI); |
| 704 | AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI); |
Evan Cheng | 435d499 | 2010-05-07 02:04:02 +0000 | [diff] [blame] | 705 | } |
Bob Wilson | ebe99b2 | 2010-06-18 21:32:42 +0000 | [diff] [blame] | 706 | break; |
| 707 | case ARM::QQQQPRRegClassID: { |
Evan Cheng | 22c687b | 2010-05-14 02:13:41 +0000 | [diff] [blame] | 708 | MachineInstrBuilder MIB = |
| 709 | AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMD)) |
| 710 | .addFrameIndex(FI) |
Bob Wilson | d4bfd54 | 2010-08-27 23:18:17 +0000 | [diff] [blame] | 711 | .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia))) |
Evan Cheng | 22c687b | 2010-05-14 02:13:41 +0000 | [diff] [blame] | 712 | .addMemOperand(MMO); |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 713 | MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI); |
| 714 | MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI); |
| 715 | MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI); |
| 716 | MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI); |
| 717 | MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI); |
| 718 | MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI); |
| 719 | MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI); |
| 720 | AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI); |
Bob Wilson | ebe99b2 | 2010-06-18 21:32:42 +0000 | [diff] [blame] | 721 | break; |
| 722 | } |
| 723 | default: |
| 724 | llvm_unreachable("Unknown regclass!"); |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
Jakob Stoklund Olesen | 3432785 | 2010-09-15 16:36:26 +0000 | [diff] [blame] | 728 | unsigned |
| 729 | ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI, |
| 730 | int &FrameIndex) const { |
| 731 | switch (MI->getOpcode()) { |
| 732 | default: break; |
| 733 | case ARM::STR: |
| 734 | case ARM::t2STRs: // FIXME: don't use t2STRs to access frame. |
| 735 | if (MI->getOperand(1).isFI() && |
| 736 | MI->getOperand(2).isReg() && |
| 737 | MI->getOperand(3).isImm() && |
| 738 | MI->getOperand(2).getReg() == 0 && |
| 739 | MI->getOperand(3).getImm() == 0) { |
| 740 | FrameIndex = MI->getOperand(1).getIndex(); |
| 741 | return MI->getOperand(0).getReg(); |
| 742 | } |
| 743 | break; |
| 744 | case ARM::t2STRi12: |
| 745 | case ARM::tSpill: |
| 746 | case ARM::VSTRD: |
| 747 | case ARM::VSTRS: |
| 748 | if (MI->getOperand(1).isFI() && |
| 749 | MI->getOperand(2).isImm() && |
| 750 | MI->getOperand(2).getImm() == 0) { |
| 751 | FrameIndex = MI->getOperand(1).getIndex(); |
| 752 | return MI->getOperand(0).getReg(); |
| 753 | } |
| 754 | break; |
Jakob Stoklund Olesen | d64816a | 2010-09-15 17:27:09 +0000 | [diff] [blame] | 755 | case ARM::VST1q64Pseudo: |
| 756 | if (MI->getOperand(0).isFI() && |
| 757 | MI->getOperand(2).getSubReg() == 0) { |
| 758 | FrameIndex = MI->getOperand(0).getIndex(); |
| 759 | return MI->getOperand(2).getReg(); |
| 760 | } |
Jakob Stoklund Olesen | 31bbc51 | 2010-09-15 21:40:09 +0000 | [diff] [blame] | 761 | break; |
Jakob Stoklund Olesen | d64816a | 2010-09-15 17:27:09 +0000 | [diff] [blame] | 762 | case ARM::VSTMQ: |
| 763 | if (MI->getOperand(1).isFI() && |
| 764 | MI->getOperand(2).isImm() && |
| 765 | MI->getOperand(2).getImm() == ARM_AM::getAM4ModeImm(ARM_AM::ia) && |
| 766 | MI->getOperand(0).getSubReg() == 0) { |
| 767 | FrameIndex = MI->getOperand(1).getIndex(); |
| 768 | return MI->getOperand(0).getReg(); |
| 769 | } |
| 770 | break; |
Jakob Stoklund Olesen | 3432785 | 2010-09-15 16:36:26 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 776 | void ARMBaseInstrInfo:: |
| 777 | loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
| 778 | unsigned DestReg, int FI, |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 779 | const TargetRegisterClass *RC, |
| 780 | const TargetRegisterInfo *TRI) const { |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 781 | DebugLoc DL; |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 782 | if (I != MBB.end()) DL = I->getDebugLoc(); |
Anton Korobeynikov | 249fb33 | 2009-10-07 00:06:35 +0000 | [diff] [blame] | 783 | MachineFunction &MF = *MBB.getParent(); |
| 784 | MachineFrameInfo &MFI = *MF.getFrameInfo(); |
Jim Grosbach | 31bc849 | 2009-11-08 00:27:19 +0000 | [diff] [blame] | 785 | unsigned Align = MFI.getObjectAlignment(FI); |
Anton Korobeynikov | 249fb33 | 2009-10-07 00:06:35 +0000 | [diff] [blame] | 786 | MachineMemOperand *MMO = |
Chris Lattner | 59db549 | 2010-09-21 04:39:43 +0000 | [diff] [blame^] | 787 | MF.getMachineMemOperand( |
| 788 | MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)), |
| 789 | MachineMemOperand::MOLoad, |
Anton Korobeynikov | 249fb33 | 2009-10-07 00:06:35 +0000 | [diff] [blame] | 790 | MFI.getObjectSize(FI), |
Jim Grosbach | 31bc849 | 2009-11-08 00:27:19 +0000 | [diff] [blame] | 791 | Align); |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 792 | |
Bob Wilson | 0eb0c74 | 2010-02-16 22:01:59 +0000 | [diff] [blame] | 793 | // tGPR is used sometimes in ARM instructions that need to avoid using |
| 794 | // certain registers. Just treat it as GPR here. |
Jim Grosbach | 6ccfc50 | 2010-07-30 02:41:01 +0000 | [diff] [blame] | 795 | if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass |
| 796 | || RC == ARM::rGPRRegisterClass) |
Bob Wilson | 0eb0c74 | 2010-02-16 22:01:59 +0000 | [diff] [blame] | 797 | RC = ARM::GPRRegisterClass; |
| 798 | |
Bob Wilson | ebe99b2 | 2010-06-18 21:32:42 +0000 | [diff] [blame] | 799 | switch (RC->getID()) { |
| 800 | case ARM::GPRRegClassID: |
Evan Cheng | 5732ca0 | 2009-07-27 03:14:20 +0000 | [diff] [blame] | 801 | AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDR), DestReg) |
Anton Korobeynikov | 249fb33 | 2009-10-07 00:06:35 +0000 | [diff] [blame] | 802 | .addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)); |
Bob Wilson | ebe99b2 | 2010-06-18 21:32:42 +0000 | [diff] [blame] | 803 | break; |
| 804 | case ARM::SPRRegClassID: |
Evan Cheng | d31c549 | 2010-05-06 01:34:11 +0000 | [diff] [blame] | 805 | AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg) |
| 806 | .addFrameIndex(FI).addImm(0).addMemOperand(MMO)); |
Bob Wilson | ebe99b2 | 2010-06-18 21:32:42 +0000 | [diff] [blame] | 807 | break; |
| 808 | case ARM::DPRRegClassID: |
| 809 | case ARM::DPR_VFP2RegClassID: |
| 810 | case ARM::DPR_8RegClassID: |
Jim Grosbach | e516549 | 2009-11-09 00:11:35 +0000 | [diff] [blame] | 811 | AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg) |
Anton Korobeynikov | 249fb33 | 2009-10-07 00:06:35 +0000 | [diff] [blame] | 812 | .addFrameIndex(FI).addImm(0).addMemOperand(MMO)); |
Bob Wilson | ebe99b2 | 2010-06-18 21:32:42 +0000 | [diff] [blame] | 813 | break; |
| 814 | case ARM::QPRRegClassID: |
| 815 | case ARM::QPR_VFP2RegClassID: |
| 816 | case ARM::QPR_8RegClassID: |
Jim Grosbach | 0cfcf93 | 2010-09-08 00:26:59 +0000 | [diff] [blame] | 817 | if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) { |
Bob Wilson | 168f382 | 2010-09-15 01:48:05 +0000 | [diff] [blame] | 818 | AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64Pseudo), DestReg) |
Bob Wilson | f967ca0 | 2010-07-06 21:26:18 +0000 | [diff] [blame] | 819 | .addFrameIndex(FI).addImm(16) |
Evan Cheng | 69b9f98 | 2010-05-13 01:12:06 +0000 | [diff] [blame] | 820 | .addMemOperand(MMO)); |
Jim Grosbach | 31bc849 | 2009-11-08 00:27:19 +0000 | [diff] [blame] | 821 | } else { |
Evan Cheng | 69b9f98 | 2010-05-13 01:12:06 +0000 | [diff] [blame] | 822 | AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQ), DestReg) |
| 823 | .addFrameIndex(FI) |
Bob Wilson | d4bfd54 | 2010-08-27 23:18:17 +0000 | [diff] [blame] | 824 | .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)) |
Evan Cheng | 69b9f98 | 2010-05-13 01:12:06 +0000 | [diff] [blame] | 825 | .addMemOperand(MMO)); |
Jim Grosbach | 31bc849 | 2009-11-08 00:27:19 +0000 | [diff] [blame] | 826 | } |
Bob Wilson | ebe99b2 | 2010-06-18 21:32:42 +0000 | [diff] [blame] | 827 | break; |
| 828 | case ARM::QQPRRegClassID: |
| 829 | case ARM::QQPR_VFP2RegClassID: |
Evan Cheng | 435d499 | 2010-05-07 02:04:02 +0000 | [diff] [blame] | 830 | if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) { |
Bob Wilson | 168f382 | 2010-09-15 01:48:05 +0000 | [diff] [blame] | 831 | AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg) |
| 832 | .addFrameIndex(FI).addImm(16) |
| 833 | .addMemOperand(MMO)); |
Evan Cheng | 435d499 | 2010-05-07 02:04:02 +0000 | [diff] [blame] | 834 | } else { |
| 835 | MachineInstrBuilder MIB = |
| 836 | AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMD)) |
| 837 | .addFrameIndex(FI) |
Bob Wilson | d4bfd54 | 2010-08-27 23:18:17 +0000 | [diff] [blame] | 838 | .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia))) |
Evan Cheng | 435d499 | 2010-05-07 02:04:02 +0000 | [diff] [blame] | 839 | .addMemOperand(MMO); |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 840 | MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI); |
| 841 | MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI); |
| 842 | MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI); |
| 843 | AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI); |
Evan Cheng | 435d499 | 2010-05-07 02:04:02 +0000 | [diff] [blame] | 844 | } |
Bob Wilson | ebe99b2 | 2010-06-18 21:32:42 +0000 | [diff] [blame] | 845 | break; |
| 846 | case ARM::QQQQPRRegClassID: { |
| 847 | MachineInstrBuilder MIB = |
| 848 | AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMD)) |
| 849 | .addFrameIndex(FI) |
Bob Wilson | d4bfd54 | 2010-08-27 23:18:17 +0000 | [diff] [blame] | 850 | .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia))) |
Bob Wilson | ebe99b2 | 2010-06-18 21:32:42 +0000 | [diff] [blame] | 851 | .addMemOperand(MMO); |
| 852 | MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI); |
| 853 | MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI); |
| 854 | MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI); |
| 855 | MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI); |
| 856 | MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::Define, TRI); |
| 857 | MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::Define, TRI); |
| 858 | MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::Define, TRI); |
| 859 | AddDReg(MIB, DestReg, ARM::dsub_7, RegState::Define, TRI); |
| 860 | break; |
| 861 | } |
| 862 | default: |
| 863 | llvm_unreachable("Unknown regclass!"); |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | |
Jakob Stoklund Olesen | 3432785 | 2010-09-15 16:36:26 +0000 | [diff] [blame] | 867 | unsigned |
| 868 | ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI, |
| 869 | int &FrameIndex) const { |
| 870 | switch (MI->getOpcode()) { |
| 871 | default: break; |
| 872 | case ARM::LDR: |
| 873 | case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame. |
| 874 | if (MI->getOperand(1).isFI() && |
| 875 | MI->getOperand(2).isReg() && |
| 876 | MI->getOperand(3).isImm() && |
| 877 | MI->getOperand(2).getReg() == 0 && |
| 878 | MI->getOperand(3).getImm() == 0) { |
| 879 | FrameIndex = MI->getOperand(1).getIndex(); |
| 880 | return MI->getOperand(0).getReg(); |
| 881 | } |
| 882 | break; |
| 883 | case ARM::t2LDRi12: |
| 884 | case ARM::tRestore: |
| 885 | case ARM::VLDRD: |
| 886 | case ARM::VLDRS: |
| 887 | if (MI->getOperand(1).isFI() && |
| 888 | MI->getOperand(2).isImm() && |
| 889 | MI->getOperand(2).getImm() == 0) { |
| 890 | FrameIndex = MI->getOperand(1).getIndex(); |
| 891 | return MI->getOperand(0).getReg(); |
| 892 | } |
| 893 | break; |
Jakob Stoklund Olesen | d64816a | 2010-09-15 17:27:09 +0000 | [diff] [blame] | 894 | case ARM::VLD1q64Pseudo: |
| 895 | if (MI->getOperand(1).isFI() && |
| 896 | MI->getOperand(0).getSubReg() == 0) { |
| 897 | FrameIndex = MI->getOperand(1).getIndex(); |
| 898 | return MI->getOperand(0).getReg(); |
| 899 | } |
| 900 | break; |
Jakob Stoklund Olesen | 06f264e | 2010-09-15 21:40:11 +0000 | [diff] [blame] | 901 | case ARM::VLDMQ: |
| 902 | if (MI->getOperand(1).isFI() && |
| 903 | MI->getOperand(2).isImm() && |
| 904 | MI->getOperand(2).getImm() == ARM_AM::getAM4ModeImm(ARM_AM::ia) && |
| 905 | MI->getOperand(0).getSubReg() == 0) { |
| 906 | FrameIndex = MI->getOperand(1).getIndex(); |
| 907 | return MI->getOperand(0).getReg(); |
| 908 | } |
| 909 | break; |
Jakob Stoklund Olesen | 3432785 | 2010-09-15 16:36:26 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | return 0; |
| 913 | } |
| 914 | |
Evan Cheng | 62b5065 | 2010-04-26 07:39:25 +0000 | [diff] [blame] | 915 | MachineInstr* |
| 916 | ARMBaseInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, |
Evan Cheng | 8601a3d | 2010-04-29 01:13:30 +0000 | [diff] [blame] | 917 | int FrameIx, uint64_t Offset, |
Evan Cheng | 62b5065 | 2010-04-26 07:39:25 +0000 | [diff] [blame] | 918 | const MDNode *MDPtr, |
| 919 | DebugLoc DL) const { |
| 920 | MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM::DBG_VALUE)) |
| 921 | .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr); |
| 922 | return &*MIB; |
| 923 | } |
| 924 | |
Jakob Stoklund Olesen | 30ac046 | 2010-01-06 23:47:07 +0000 | [diff] [blame] | 925 | /// Create a copy of a const pool value. Update CPI to the new index and return |
| 926 | /// the label UID. |
| 927 | static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) { |
| 928 | MachineConstantPool *MCP = MF.getConstantPool(); |
| 929 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 930 | |
| 931 | const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI]; |
| 932 | assert(MCPE.isMachineConstantPoolEntry() && |
| 933 | "Expecting a machine constantpool entry!"); |
| 934 | ARMConstantPoolValue *ACPV = |
| 935 | static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal); |
| 936 | |
| 937 | unsigned PCLabelId = AFI->createConstPoolEntryUId(); |
| 938 | ARMConstantPoolValue *NewCPV = 0; |
Jim Grosbach | 51f5b67 | 2010-09-10 21:38:22 +0000 | [diff] [blame] | 939 | // FIXME: The below assumes PIC relocation model and that the function |
| 940 | // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and |
| 941 | // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR |
| 942 | // instructions, so that's probably OK, but is PIC always correct when |
| 943 | // we get here? |
Jakob Stoklund Olesen | 30ac046 | 2010-01-06 23:47:07 +0000 | [diff] [blame] | 944 | if (ACPV->isGlobalValue()) |
| 945 | NewCPV = new ARMConstantPoolValue(ACPV->getGV(), PCLabelId, |
| 946 | ARMCP::CPValue, 4); |
| 947 | else if (ACPV->isExtSymbol()) |
| 948 | NewCPV = new ARMConstantPoolValue(MF.getFunction()->getContext(), |
| 949 | ACPV->getSymbol(), PCLabelId, 4); |
| 950 | else if (ACPV->isBlockAddress()) |
| 951 | NewCPV = new ARMConstantPoolValue(ACPV->getBlockAddress(), PCLabelId, |
| 952 | ARMCP::CPBlockAddress, 4); |
Jim Grosbach | 51f5b67 | 2010-09-10 21:38:22 +0000 | [diff] [blame] | 953 | else if (ACPV->isLSDA()) |
| 954 | NewCPV = new ARMConstantPoolValue(MF.getFunction(), PCLabelId, |
| 955 | ARMCP::CPLSDA, 4); |
Jakob Stoklund Olesen | 30ac046 | 2010-01-06 23:47:07 +0000 | [diff] [blame] | 956 | else |
| 957 | llvm_unreachable("Unexpected ARM constantpool value type!!"); |
| 958 | CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment()); |
| 959 | return PCLabelId; |
| 960 | } |
| 961 | |
Evan Cheng | fdc8340 | 2009-11-08 00:15:23 +0000 | [diff] [blame] | 962 | void ARMBaseInstrInfo:: |
| 963 | reMaterialize(MachineBasicBlock &MBB, |
| 964 | MachineBasicBlock::iterator I, |
| 965 | unsigned DestReg, unsigned SubIdx, |
Evan Cheng | d57cdd5 | 2009-11-14 02:55:43 +0000 | [diff] [blame] | 966 | const MachineInstr *Orig, |
Jakob Stoklund Olesen | 9edf7de | 2010-06-02 22:47:25 +0000 | [diff] [blame] | 967 | const TargetRegisterInfo &TRI) const { |
Evan Cheng | fdc8340 | 2009-11-08 00:15:23 +0000 | [diff] [blame] | 968 | unsigned Opcode = Orig->getOpcode(); |
| 969 | switch (Opcode) { |
| 970 | default: { |
| 971 | MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig); |
Jakob Stoklund Olesen | 9edf7de | 2010-06-02 22:47:25 +0000 | [diff] [blame] | 972 | MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI); |
Evan Cheng | fdc8340 | 2009-11-08 00:15:23 +0000 | [diff] [blame] | 973 | MBB.insert(I, MI); |
| 974 | break; |
| 975 | } |
| 976 | case ARM::tLDRpci_pic: |
| 977 | case ARM::t2LDRpci_pic: { |
| 978 | MachineFunction &MF = *MBB.getParent(); |
Evan Cheng | fdc8340 | 2009-11-08 00:15:23 +0000 | [diff] [blame] | 979 | unsigned CPI = Orig->getOperand(1).getIndex(); |
Jakob Stoklund Olesen | 30ac046 | 2010-01-06 23:47:07 +0000 | [diff] [blame] | 980 | unsigned PCLabelId = duplicateCPV(MF, CPI); |
Evan Cheng | fdc8340 | 2009-11-08 00:15:23 +0000 | [diff] [blame] | 981 | MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode), |
| 982 | DestReg) |
| 983 | .addConstantPoolIndex(CPI).addImm(PCLabelId); |
| 984 | (*MIB).setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end()); |
| 985 | break; |
| 986 | } |
| 987 | } |
Evan Cheng | fdc8340 | 2009-11-08 00:15:23 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Jakob Stoklund Olesen | 30ac046 | 2010-01-06 23:47:07 +0000 | [diff] [blame] | 990 | MachineInstr * |
| 991 | ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const { |
| 992 | MachineInstr *MI = TargetInstrInfoImpl::duplicate(Orig, MF); |
| 993 | switch(Orig->getOpcode()) { |
| 994 | case ARM::tLDRpci_pic: |
| 995 | case ARM::t2LDRpci_pic: { |
| 996 | unsigned CPI = Orig->getOperand(1).getIndex(); |
| 997 | unsigned PCLabelId = duplicateCPV(MF, CPI); |
| 998 | Orig->getOperand(1).setIndex(CPI); |
| 999 | Orig->getOperand(2).setImm(PCLabelId); |
| 1000 | break; |
| 1001 | } |
| 1002 | } |
| 1003 | return MI; |
| 1004 | } |
| 1005 | |
Evan Cheng | 506049f | 2010-03-03 01:44:33 +0000 | [diff] [blame] | 1006 | bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0, |
| 1007 | const MachineInstr *MI1) const { |
Evan Cheng | d457e6e | 2009-11-07 04:04:34 +0000 | [diff] [blame] | 1008 | int Opcode = MI0->getOpcode(); |
Evan Cheng | 9b82425 | 2009-11-20 02:10:27 +0000 | [diff] [blame] | 1009 | if (Opcode == ARM::t2LDRpci || |
| 1010 | Opcode == ARM::t2LDRpci_pic || |
| 1011 | Opcode == ARM::tLDRpci || |
| 1012 | Opcode == ARM::tLDRpci_pic) { |
Evan Cheng | d457e6e | 2009-11-07 04:04:34 +0000 | [diff] [blame] | 1013 | if (MI1->getOpcode() != Opcode) |
| 1014 | return false; |
| 1015 | if (MI0->getNumOperands() != MI1->getNumOperands()) |
| 1016 | return false; |
| 1017 | |
| 1018 | const MachineOperand &MO0 = MI0->getOperand(1); |
| 1019 | const MachineOperand &MO1 = MI1->getOperand(1); |
| 1020 | if (MO0.getOffset() != MO1.getOffset()) |
| 1021 | return false; |
| 1022 | |
| 1023 | const MachineFunction *MF = MI0->getParent()->getParent(); |
| 1024 | const MachineConstantPool *MCP = MF->getConstantPool(); |
| 1025 | int CPI0 = MO0.getIndex(); |
| 1026 | int CPI1 = MO1.getIndex(); |
| 1027 | const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0]; |
| 1028 | const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1]; |
| 1029 | ARMConstantPoolValue *ACPV0 = |
| 1030 | static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal); |
| 1031 | ARMConstantPoolValue *ACPV1 = |
| 1032 | static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal); |
| 1033 | return ACPV0->hasSameValue(ACPV1); |
| 1034 | } |
| 1035 | |
Evan Cheng | 506049f | 2010-03-03 01:44:33 +0000 | [diff] [blame] | 1036 | return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs); |
Evan Cheng | d457e6e | 2009-11-07 04:04:34 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Bill Wendling | 4b72210 | 2010-06-23 23:00:16 +0000 | [diff] [blame] | 1039 | /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to |
| 1040 | /// determine if two loads are loading from the same base address. It should |
| 1041 | /// only return true if the base pointers are the same and the only differences |
| 1042 | /// between the two addresses is the offset. It also returns the offsets by |
| 1043 | /// reference. |
| 1044 | bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2, |
| 1045 | int64_t &Offset1, |
| 1046 | int64_t &Offset2) const { |
| 1047 | // Don't worry about Thumb: just ARM and Thumb2. |
| 1048 | if (Subtarget.isThumb1Only()) return false; |
| 1049 | |
| 1050 | if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode()) |
| 1051 | return false; |
| 1052 | |
| 1053 | switch (Load1->getMachineOpcode()) { |
| 1054 | default: |
| 1055 | return false; |
| 1056 | case ARM::LDR: |
| 1057 | case ARM::LDRB: |
| 1058 | case ARM::LDRD: |
| 1059 | case ARM::LDRH: |
| 1060 | case ARM::LDRSB: |
| 1061 | case ARM::LDRSH: |
| 1062 | case ARM::VLDRD: |
| 1063 | case ARM::VLDRS: |
| 1064 | case ARM::t2LDRi8: |
| 1065 | case ARM::t2LDRDi8: |
| 1066 | case ARM::t2LDRSHi8: |
| 1067 | case ARM::t2LDRi12: |
| 1068 | case ARM::t2LDRSHi12: |
| 1069 | break; |
| 1070 | } |
| 1071 | |
| 1072 | switch (Load2->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 | // Check if base addresses and chain operands match. |
| 1092 | if (Load1->getOperand(0) != Load2->getOperand(0) || |
| 1093 | Load1->getOperand(4) != Load2->getOperand(4)) |
| 1094 | return false; |
| 1095 | |
| 1096 | // Index should be Reg0. |
| 1097 | if (Load1->getOperand(3) != Load2->getOperand(3)) |
| 1098 | return false; |
| 1099 | |
| 1100 | // Determine the offsets. |
| 1101 | if (isa<ConstantSDNode>(Load1->getOperand(1)) && |
| 1102 | isa<ConstantSDNode>(Load2->getOperand(1))) { |
| 1103 | Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue(); |
| 1104 | Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue(); |
| 1105 | return true; |
| 1106 | } |
| 1107 | |
| 1108 | return false; |
| 1109 | } |
| 1110 | |
| 1111 | /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to |
| 1112 | /// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should |
| 1113 | /// be scheduled togther. On some targets if two loads are loading from |
| 1114 | /// addresses in the same cache line, it's better if they are scheduled |
| 1115 | /// together. This function takes two integers that represent the load offsets |
| 1116 | /// from the common base address. It returns true if it decides it's desirable |
| 1117 | /// to schedule the two loads together. "NumLoads" is the number of loads that |
| 1118 | /// have already been scheduled after Load1. |
| 1119 | bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2, |
| 1120 | int64_t Offset1, int64_t Offset2, |
| 1121 | unsigned NumLoads) const { |
| 1122 | // Don't worry about Thumb: just ARM and Thumb2. |
| 1123 | if (Subtarget.isThumb1Only()) return false; |
| 1124 | |
| 1125 | assert(Offset2 > Offset1); |
| 1126 | |
| 1127 | if ((Offset2 - Offset1) / 8 > 64) |
| 1128 | return false; |
| 1129 | |
| 1130 | if (Load1->getMachineOpcode() != Load2->getMachineOpcode()) |
| 1131 | return false; // FIXME: overly conservative? |
| 1132 | |
| 1133 | // Four loads in a row should be sufficient. |
| 1134 | if (NumLoads >= 3) |
| 1135 | return false; |
| 1136 | |
| 1137 | return true; |
| 1138 | } |
| 1139 | |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 1140 | bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI, |
| 1141 | const MachineBasicBlock *MBB, |
| 1142 | const MachineFunction &MF) const { |
Jim Grosbach | 57bb394 | 2010-06-25 18:43:14 +0000 | [diff] [blame] | 1143 | // Debug info is never a scheduling boundary. It's necessary to be explicit |
| 1144 | // due to the special treatment of IT instructions below, otherwise a |
| 1145 | // dbg_value followed by an IT will result in the IT instruction being |
| 1146 | // considered a scheduling hazard, which is wrong. It should be the actual |
| 1147 | // instruction preceding the dbg_value instruction(s), just like it is |
| 1148 | // when debug info is not present. |
| 1149 | if (MI->isDebugValue()) |
| 1150 | return false; |
| 1151 | |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 1152 | // Terminators and labels can't be scheduled around. |
| 1153 | if (MI->getDesc().isTerminator() || MI->isLabel()) |
| 1154 | return true; |
| 1155 | |
| 1156 | // Treat the start of the IT block as a scheduling boundary, but schedule |
| 1157 | // t2IT along with all instructions following it. |
| 1158 | // FIXME: This is a big hammer. But the alternative is to add all potential |
| 1159 | // true and anti dependencies to IT block instructions as implicit operands |
| 1160 | // to the t2IT instruction. The added compile time and complexity does not |
| 1161 | // seem worth it. |
| 1162 | MachineBasicBlock::const_iterator I = MI; |
Jim Grosbach | 57bb394 | 2010-06-25 18:43:14 +0000 | [diff] [blame] | 1163 | // Make sure to skip any dbg_value instructions |
| 1164 | while (++I != MBB->end() && I->isDebugValue()) |
| 1165 | ; |
| 1166 | if (I != MBB->end() && I->getOpcode() == ARM::t2IT) |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 1167 | return true; |
| 1168 | |
| 1169 | // Don't attempt to schedule around any instruction that defines |
| 1170 | // a stack-oriented pointer, as it's unlikely to be profitable. This |
| 1171 | // saves compile time, because it doesn't require every single |
| 1172 | // stack slot reference to depend on the instruction that does the |
| 1173 | // modification. |
| 1174 | if (MI->definesRegister(ARM::SP)) |
| 1175 | return true; |
| 1176 | |
| 1177 | return false; |
| 1178 | } |
| 1179 | |
Evan Cheng | 1315143 | 2010-06-25 22:42:03 +0000 | [diff] [blame] | 1180 | bool ARMBaseInstrInfo:: |
| 1181 | isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumInstrs) const { |
| 1182 | if (!NumInstrs) |
| 1183 | return false; |
| 1184 | if (Subtarget.getCPUString() == "generic") |
| 1185 | // Generic (and overly aggressive) if-conversion limits for testing. |
| 1186 | return NumInstrs <= 10; |
| 1187 | else if (Subtarget.hasV7Ops()) |
| 1188 | return NumInstrs <= 3; |
| 1189 | return NumInstrs <= 2; |
| 1190 | } |
| 1191 | |
| 1192 | bool ARMBaseInstrInfo:: |
| 1193 | isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumT, |
| 1194 | MachineBasicBlock &FMBB, unsigned NumF) const { |
| 1195 | return NumT && NumF && NumT <= 2 && NumF <= 2; |
| 1196 | } |
| 1197 | |
Evan Cheng | 8fb9036 | 2009-08-08 03:20:32 +0000 | [diff] [blame] | 1198 | /// getInstrPredicate - If instruction is predicated, returns its predicate |
| 1199 | /// condition, otherwise returns AL. It also returns the condition code |
| 1200 | /// register by reference. |
Evan Cheng | 5adb66a | 2009-09-28 09:14:39 +0000 | [diff] [blame] | 1201 | ARMCC::CondCodes |
| 1202 | llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) { |
Evan Cheng | 8fb9036 | 2009-08-08 03:20:32 +0000 | [diff] [blame] | 1203 | int PIdx = MI->findFirstPredOperandIdx(); |
| 1204 | if (PIdx == -1) { |
| 1205 | PredReg = 0; |
| 1206 | return ARMCC::AL; |
| 1207 | } |
| 1208 | |
| 1209 | PredReg = MI->getOperand(PIdx+1).getReg(); |
| 1210 | return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm(); |
| 1211 | } |
| 1212 | |
| 1213 | |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1214 | int llvm::getMatchingCondBranchOpcode(int Opc) { |
Evan Cheng | 5ca53a7 | 2009-07-27 18:20:05 +0000 | [diff] [blame] | 1215 | if (Opc == ARM::B) |
| 1216 | return ARM::Bcc; |
| 1217 | else if (Opc == ARM::tB) |
| 1218 | return ARM::tBcc; |
| 1219 | else if (Opc == ARM::t2B) |
| 1220 | return ARM::t2Bcc; |
| 1221 | |
| 1222 | llvm_unreachable("Unknown unconditional branch opcode!"); |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1226 | |
| 1227 | void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB, |
| 1228 | MachineBasicBlock::iterator &MBBI, DebugLoc dl, |
| 1229 | unsigned DestReg, unsigned BaseReg, int NumBytes, |
| 1230 | ARMCC::CondCodes Pred, unsigned PredReg, |
| 1231 | const ARMBaseInstrInfo &TII) { |
| 1232 | bool isSub = NumBytes < 0; |
| 1233 | if (isSub) NumBytes = -NumBytes; |
| 1234 | |
| 1235 | while (NumBytes) { |
| 1236 | unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes); |
| 1237 | unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt); |
| 1238 | assert(ThisVal && "Didn't extract field correctly"); |
| 1239 | |
| 1240 | // We will handle these bits from offset, clear them. |
| 1241 | NumBytes &= ~ThisVal; |
| 1242 | |
| 1243 | assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?"); |
| 1244 | |
| 1245 | // Build the new ADD / SUB. |
| 1246 | unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri; |
| 1247 | BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg) |
| 1248 | .addReg(BaseReg, RegState::Kill).addImm(ThisVal) |
| 1249 | .addImm((unsigned)Pred).addReg(PredReg).addReg(0); |
| 1250 | BaseReg = DestReg; |
| 1251 | } |
| 1252 | } |
| 1253 | |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 1254 | bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx, |
| 1255 | unsigned FrameReg, int &Offset, |
| 1256 | const ARMBaseInstrInfo &TII) { |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1257 | unsigned Opcode = MI.getOpcode(); |
| 1258 | const TargetInstrDesc &Desc = MI.getDesc(); |
| 1259 | unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask); |
| 1260 | bool isSub = false; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 1261 | |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1262 | // Memory operands in inline assembly always use AddrMode2. |
| 1263 | if (Opcode == ARM::INLINEASM) |
| 1264 | AddrMode = ARMII::AddrMode2; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 1265 | |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1266 | if (Opcode == ARM::ADDri) { |
| 1267 | Offset += MI.getOperand(FrameRegIdx+1).getImm(); |
| 1268 | if (Offset == 0) { |
| 1269 | // Turn it into a move. |
| 1270 | MI.setDesc(TII.get(ARM::MOVr)); |
| 1271 | MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); |
| 1272 | MI.RemoveOperand(FrameRegIdx+1); |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 1273 | Offset = 0; |
| 1274 | return true; |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1275 | } else if (Offset < 0) { |
| 1276 | Offset = -Offset; |
| 1277 | isSub = true; |
| 1278 | MI.setDesc(TII.get(ARM::SUBri)); |
| 1279 | } |
| 1280 | |
| 1281 | // Common case: small offset, fits into instruction. |
| 1282 | if (ARM_AM::getSOImmVal(Offset) != -1) { |
| 1283 | // Replace the FrameIndex with sp / fp |
| 1284 | MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); |
| 1285 | MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset); |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 1286 | Offset = 0; |
| 1287 | return true; |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | // Otherwise, pull as much of the immedidate into this ADDri/SUBri |
| 1291 | // as possible. |
| 1292 | unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset); |
| 1293 | unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt); |
| 1294 | |
| 1295 | // We will handle these bits from offset, clear them. |
| 1296 | Offset &= ~ThisImmVal; |
| 1297 | |
| 1298 | // Get the properly encoded SOImmVal field. |
| 1299 | assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 && |
| 1300 | "Bit extraction didn't work?"); |
| 1301 | MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal); |
| 1302 | } else { |
| 1303 | unsigned ImmIdx = 0; |
| 1304 | int InstrOffs = 0; |
| 1305 | unsigned NumBits = 0; |
| 1306 | unsigned Scale = 1; |
| 1307 | switch (AddrMode) { |
| 1308 | case ARMII::AddrMode2: { |
| 1309 | ImmIdx = FrameRegIdx+2; |
| 1310 | InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm()); |
| 1311 | if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub) |
| 1312 | InstrOffs *= -1; |
| 1313 | NumBits = 12; |
| 1314 | break; |
| 1315 | } |
| 1316 | case ARMII::AddrMode3: { |
| 1317 | ImmIdx = FrameRegIdx+2; |
| 1318 | InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm()); |
| 1319 | if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub) |
| 1320 | InstrOffs *= -1; |
| 1321 | NumBits = 8; |
| 1322 | break; |
| 1323 | } |
Anton Korobeynikov | baf3108 | 2009-08-08 13:35:48 +0000 | [diff] [blame] | 1324 | case ARMII::AddrMode4: |
Jim Grosbach | a443217 | 2009-11-15 21:45:34 +0000 | [diff] [blame] | 1325 | case ARMII::AddrMode6: |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 1326 | // Can't fold any offset even if it's zero. |
| 1327 | return false; |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1328 | case ARMII::AddrMode5: { |
| 1329 | ImmIdx = FrameRegIdx+1; |
| 1330 | InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm()); |
| 1331 | if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub) |
| 1332 | InstrOffs *= -1; |
| 1333 | NumBits = 8; |
| 1334 | Scale = 4; |
| 1335 | break; |
| 1336 | } |
| 1337 | default: |
| 1338 | llvm_unreachable("Unsupported addressing mode!"); |
| 1339 | break; |
| 1340 | } |
| 1341 | |
| 1342 | Offset += InstrOffs * Scale; |
| 1343 | assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!"); |
| 1344 | if (Offset < 0) { |
| 1345 | Offset = -Offset; |
| 1346 | isSub = true; |
| 1347 | } |
| 1348 | |
| 1349 | // Attempt to fold address comp. if opcode has offset bits |
| 1350 | if (NumBits > 0) { |
| 1351 | // Common case: small offset, fits into instruction. |
| 1352 | MachineOperand &ImmOp = MI.getOperand(ImmIdx); |
| 1353 | int ImmedOffset = Offset / Scale; |
| 1354 | unsigned Mask = (1 << NumBits) - 1; |
| 1355 | if ((unsigned)Offset <= Mask * Scale) { |
| 1356 | // Replace the FrameIndex with sp |
| 1357 | MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false); |
| 1358 | if (isSub) |
| 1359 | ImmedOffset |= 1 << NumBits; |
| 1360 | ImmOp.ChangeToImmediate(ImmedOffset); |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 1361 | Offset = 0; |
| 1362 | return true; |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1363 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 1364 | |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1365 | // Otherwise, it didn't fit. Pull in what we can to simplify the immed. |
| 1366 | ImmedOffset = ImmedOffset & Mask; |
| 1367 | if (isSub) |
| 1368 | ImmedOffset |= 1 << NumBits; |
| 1369 | ImmOp.ChangeToImmediate(ImmedOffset); |
| 1370 | Offset &= ~(Mask*Scale); |
| 1371 | } |
| 1372 | } |
| 1373 | |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 1374 | Offset = (isSub) ? -Offset : Offset; |
| 1375 | return Offset == 0; |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1376 | } |
Bill Wendling | e4ddbdf | 2010-08-06 01:32:48 +0000 | [diff] [blame] | 1377 | |
| 1378 | bool ARMBaseInstrInfo:: |
Bill Wendling | c98af33 | 2010-08-08 05:04:59 +0000 | [diff] [blame] | 1379 | AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg, int &CmpValue) const { |
Bill Wendling | e4ddbdf | 2010-08-06 01:32:48 +0000 | [diff] [blame] | 1380 | switch (MI->getOpcode()) { |
| 1381 | default: break; |
Bill Wendling | 38ae997 | 2010-08-11 00:23:00 +0000 | [diff] [blame] | 1382 | case ARM::CMPri: |
| 1383 | case ARM::CMPzri: |
Bill Wendling | e4ddbdf | 2010-08-06 01:32:48 +0000 | [diff] [blame] | 1384 | case ARM::t2CMPri: |
| 1385 | case ARM::t2CMPzri: |
| 1386 | SrcReg = MI->getOperand(0).getReg(); |
| 1387 | CmpValue = MI->getOperand(1).getImm(); |
| 1388 | return true; |
Bob Wilson | 3a95182 | 2010-09-15 17:12:08 +0000 | [diff] [blame] | 1389 | case ARM::TSTri: { |
| 1390 | MachineBasicBlock::const_iterator MII(MI); |
| 1391 | if (MI->getParent()->begin() == MII) |
| 1392 | return false; |
| 1393 | const MachineInstr *AND = llvm::prior(MII); |
| 1394 | if (AND->getOpcode() != ARM::ANDri) |
| 1395 | return false; |
| 1396 | if (MI->getOperand(0).getReg() == AND->getOperand(1).getReg() && |
| 1397 | MI->getOperand(1).getImm() == AND->getOperand(2).getImm()) { |
| 1398 | SrcReg = AND->getOperand(0).getReg(); |
| 1399 | CmpValue = 0; |
| 1400 | return true; |
| 1401 | } |
| 1402 | } |
| 1403 | break; |
Bill Wendling | e4ddbdf | 2010-08-06 01:32:48 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | return false; |
| 1407 | } |
| 1408 | |
Bill Wendling | a655686 | 2010-09-11 00:13:50 +0000 | [diff] [blame] | 1409 | /// OptimizeCompareInstr - Convert the instruction supplying the argument to the |
Bill Wendling | 92ad57f | 2010-09-10 23:34:19 +0000 | [diff] [blame] | 1410 | /// comparison into one that sets the zero bit in the flags register. Update the |
| 1411 | /// iterator *only* if a transformation took place. |
Bill Wendling | e4ddbdf | 2010-08-06 01:32:48 +0000 | [diff] [blame] | 1412 | bool ARMBaseInstrInfo:: |
Bill Wendling | a655686 | 2010-09-11 00:13:50 +0000 | [diff] [blame] | 1413 | OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, int CmpValue, |
Bill Wendling | 220e240 | 2010-09-10 21:55:43 +0000 | [diff] [blame] | 1414 | MachineBasicBlock::iterator &MII) const { |
Bill Wendling | 3665661 | 2010-09-10 23:46:12 +0000 | [diff] [blame] | 1415 | if (CmpValue != 0) |
Bill Wendling | 92ad57f | 2010-09-10 23:34:19 +0000 | [diff] [blame] | 1416 | return false; |
| 1417 | |
| 1418 | MachineRegisterInfo &MRI = CmpInstr->getParent()->getParent()->getRegInfo(); |
| 1419 | MachineRegisterInfo::def_iterator DI = MRI.def_begin(SrcReg); |
| 1420 | if (llvm::next(DI) != MRI.def_end()) |
| 1421 | // Only support one definition. |
| 1422 | return false; |
| 1423 | |
| 1424 | MachineInstr *MI = &*DI; |
| 1425 | |
Bill Wendling | e4ddbdf | 2010-08-06 01:32:48 +0000 | [diff] [blame] | 1426 | // Conservatively refuse to convert an instruction which isn't in the same BB |
| 1427 | // as the comparison. |
| 1428 | if (MI->getParent() != CmpInstr->getParent()) |
| 1429 | return false; |
| 1430 | |
| 1431 | // Check that CPSR isn't set between the comparison instruction and the one we |
| 1432 | // want to change. |
| 1433 | MachineBasicBlock::const_iterator I = CmpInstr, E = MI; |
| 1434 | --I; |
| 1435 | for (; I != E; --I) { |
| 1436 | const MachineInstr &Instr = *I; |
| 1437 | |
| 1438 | for (unsigned IO = 0, EO = Instr.getNumOperands(); IO != EO; ++IO) { |
| 1439 | const MachineOperand &MO = Instr.getOperand(IO); |
Bill Wendling | 75486db | 2010-08-10 21:38:11 +0000 | [diff] [blame] | 1440 | if (!MO.isReg() || !MO.isDef()) continue; |
Bill Wendling | e4ddbdf | 2010-08-06 01:32:48 +0000 | [diff] [blame] | 1441 | |
| 1442 | // This instruction modifies CPSR before the one we want to change. We |
| 1443 | // can't do this transformation. |
| 1444 | if (MO.getReg() == ARM::CPSR) |
| 1445 | return false; |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | // Set the "zero" bit in CPSR. |
| 1450 | switch (MI->getOpcode()) { |
| 1451 | default: break; |
Bill Wendling | 38ae997 | 2010-08-11 00:23:00 +0000 | [diff] [blame] | 1452 | case ARM::ADDri: |
Bob Wilson | 3a95182 | 2010-09-15 17:12:08 +0000 | [diff] [blame] | 1453 | case ARM::ANDri: |
| 1454 | case ARM::t2ANDri: |
Bill Wendling | 38ae997 | 2010-08-11 00:23:00 +0000 | [diff] [blame] | 1455 | case ARM::SUBri: |
| 1456 | case ARM::t2ADDri: |
Bill Wendling | ad42271 | 2010-08-18 21:32:07 +0000 | [diff] [blame] | 1457 | case ARM::t2SUBri: |
Bill Wendling | e4ddbdf | 2010-08-06 01:32:48 +0000 | [diff] [blame] | 1458 | MI->RemoveOperand(5); |
Bill Wendling | ad42271 | 2010-08-18 21:32:07 +0000 | [diff] [blame] | 1459 | MachineInstrBuilder(MI) |
| 1460 | .addReg(ARM::CPSR, RegState::Define | RegState::Implicit); |
Bill Wendling | 220e240 | 2010-09-10 21:55:43 +0000 | [diff] [blame] | 1461 | MII = llvm::next(MachineBasicBlock::iterator(CmpInstr)); |
Bill Wendling | e4ddbdf | 2010-08-06 01:32:48 +0000 | [diff] [blame] | 1462 | CmpInstr->eraseFromParent(); |
| 1463 | return true; |
| 1464 | } |
Bill Wendling | e4ddbdf | 2010-08-06 01:32:48 +0000 | [diff] [blame] | 1465 | |
| 1466 | return false; |
| 1467 | } |
Evan Cheng | 5f54ce3 | 2010-09-09 18:18:55 +0000 | [diff] [blame] | 1468 | |
| 1469 | unsigned |
| 1470 | ARMBaseInstrInfo::getNumMicroOps(const MachineInstr *MI, |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 1471 | const InstrItineraryData *ItinData) const { |
| 1472 | if (!ItinData || ItinData->isEmpty()) |
Evan Cheng | 5f54ce3 | 2010-09-09 18:18:55 +0000 | [diff] [blame] | 1473 | return 1; |
| 1474 | |
| 1475 | const TargetInstrDesc &Desc = MI->getDesc(); |
| 1476 | unsigned Class = Desc.getSchedClass(); |
Bob Wilson | 064312d | 2010-09-15 16:28:21 +0000 | [diff] [blame] | 1477 | unsigned UOps = ItinData->Itineraries[Class].NumMicroOps; |
Evan Cheng | 5f54ce3 | 2010-09-09 18:18:55 +0000 | [diff] [blame] | 1478 | if (UOps) |
| 1479 | return UOps; |
| 1480 | |
| 1481 | unsigned Opc = MI->getOpcode(); |
| 1482 | switch (Opc) { |
| 1483 | default: |
| 1484 | llvm_unreachable("Unexpected multi-uops instruction!"); |
| 1485 | break; |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 1486 | case ARM::VLDMQ: |
Evan Cheng | 5f54ce3 | 2010-09-09 18:18:55 +0000 | [diff] [blame] | 1487 | case ARM::VSTMQ: |
| 1488 | return 2; |
| 1489 | |
| 1490 | // The number of uOps for load / store multiple are determined by the number |
| 1491 | // registers. |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 1492 | // On Cortex-A8, each pair of register loads / stores can be scheduled on the |
| 1493 | // same cycle. The scheduling for the first load / store must be done |
| 1494 | // separately by assuming the the address is not 64-bit aligned. |
| 1495 | // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address |
| 1496 | // is not 64-bit aligned, then AGU would take an extra cycle. |
| 1497 | // For VFP / NEON load / store multiple, the formula is |
Evan Cheng | 5f54ce3 | 2010-09-09 18:18:55 +0000 | [diff] [blame] | 1498 | // (#reg / 2) + (#reg % 2) + 1. |
Evan Cheng | 5f54ce3 | 2010-09-09 18:18:55 +0000 | [diff] [blame] | 1499 | case ARM::VLDMD: |
| 1500 | case ARM::VLDMS: |
| 1501 | case ARM::VLDMD_UPD: |
| 1502 | case ARM::VLDMS_UPD: |
| 1503 | case ARM::VSTMD: |
| 1504 | case ARM::VSTMS: |
| 1505 | case ARM::VSTMD_UPD: |
| 1506 | case ARM::VSTMS_UPD: { |
| 1507 | unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands(); |
| 1508 | return (NumRegs / 2) + (NumRegs % 2) + 1; |
| 1509 | } |
| 1510 | case ARM::LDM_RET: |
| 1511 | case ARM::LDM: |
| 1512 | case ARM::LDM_UPD: |
| 1513 | case ARM::STM: |
| 1514 | case ARM::STM_UPD: |
| 1515 | case ARM::tLDM: |
| 1516 | case ARM::tLDM_UPD: |
| 1517 | case ARM::tSTM_UPD: |
| 1518 | case ARM::tPOP_RET: |
| 1519 | case ARM::tPOP: |
| 1520 | case ARM::tPUSH: |
| 1521 | case ARM::t2LDM_RET: |
| 1522 | case ARM::t2LDM: |
| 1523 | case ARM::t2LDM_UPD: |
| 1524 | case ARM::t2STM: |
| 1525 | case ARM::t2STM_UPD: { |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 1526 | unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1; |
| 1527 | if (Subtarget.isCortexA8()) { |
| 1528 | // 4 registers would be issued: 1, 2, 1. |
| 1529 | // 5 registers would be issued: 1, 2, 2. |
| 1530 | return 1 + (NumRegs / 2); |
| 1531 | } else if (Subtarget.isCortexA9()) { |
| 1532 | UOps = (NumRegs / 2); |
| 1533 | // If there are odd number of registers or if it's not 64-bit aligned, |
| 1534 | // then it takes an extra AGU (Address Generation Unit) cycle. |
| 1535 | if ((NumRegs % 2) || |
| 1536 | !MI->hasOneMemOperand() || |
| 1537 | (*MI->memoperands_begin())->getAlignment() < 8) |
| 1538 | ++UOps; |
| 1539 | return UOps; |
| 1540 | } else { |
| 1541 | // Assume the worst. |
| 1542 | return NumRegs; |
| 1543 | } |
Evan Cheng | 5f54ce3 | 2010-09-09 18:18:55 +0000 | [diff] [blame] | 1544 | } |
| 1545 | } |
| 1546 | } |