blob: 9ce42470452d1ddc53b3f8ee3a9723470f4183e7 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- ARMBaseInstrInfo.cpp - ARM Instruction Information ----------------===//
David Goodwinaf7451b2009-07-08 16:09:28 +00002//
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
David Goodwinaf7451b2009-07-08 16:09:28 +000014#include "ARM.h"
Amara Emerson52cfb6a2013-10-03 09:31:51 +000015#include "ARMBaseInstrInfo.h"
Craig Topper5fa0caa2012-03-26 00:45:15 +000016#include "ARMBaseRegisterInfo.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000017#include "ARMConstantPoolValue.h"
Amara Emerson52cfb6a2013-10-03 09:31:51 +000018#include "ARMFeatures.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000019#include "ARMHazardRecognizer.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000020#include "ARMMachineFunctionInfo.h"
Evan Chenga20cde32011-07-20 23:34:39 +000021#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/ADT/STLExtras.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000023#include "llvm/CodeGen/LiveVariables.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000024#include "llvm/CodeGen/MachineConstantPool.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/MachineJumpTableInfo.h"
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +000028#include "llvm/CodeGen/MachineMemOperand.h"
Evan Cheng168ced92010-05-22 01:47:14 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chenga20cde32011-07-20 23:34:39 +000030#include "llvm/CodeGen/SelectionDAGNodes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/Constants.h"
32#include "llvm/IR/Function.h"
33#include "llvm/IR/GlobalValue.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000034#include "llvm/MC/MCAsmInfo.h"
Tom Roeder44cb65f2014-06-05 19:29:43 +000035#include "llvm/MC/MCExpr.h"
Jakub Staszak9b07c0a2011-07-10 02:58:07 +000036#include "llvm/Support/BranchProbability.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000037#include "llvm/Support/CommandLine.h"
Anton Korobeynikov14635da2009-11-02 00:10:38 +000038#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000039#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000040#include "llvm/Support/raw_ostream.h"
Evan Cheng1e210d02011-06-28 20:07:07 +000041
David Goodwinaf7451b2009-07-08 16:09:28 +000042using namespace llvm;
43
Chandler Carruthe96dd892014-04-21 22:55:11 +000044#define DEBUG_TYPE "arm-instrinfo"
45
Chandler Carruthd174b722014-04-22 02:03:14 +000046#define GET_INSTRINFO_CTOR_DTOR
47#include "ARMGenInstrInfo.inc"
48
David Goodwinaf7451b2009-07-08 16:09:28 +000049static cl::opt<bool>
50EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
51 cl::desc("Enable ARM 2-addr to 3-addr conv"));
52
Jakob Stoklund Olesencd893392011-08-31 17:00:02 +000053static cl::opt<bool>
Jakob Stoklund Olesen653183f2011-11-15 23:53:18 +000054WidenVMOVS("widen-vmovs", cl::Hidden, cl::init(true),
Jakob Stoklund Olesencd893392011-08-31 17:00:02 +000055 cl::desc("Widen ARM vmovs to vmovd when possible"));
56
Bob Wilsone8a549c2012-09-29 21:43:49 +000057static cl::opt<unsigned>
58SwiftPartialUpdateClearance("swift-partial-update-clearance",
59 cl::Hidden, cl::init(12),
60 cl::desc("Clearance before partial register updates"));
61
Evan Cheng62c7b5b2010-12-05 22:04:16 +000062/// ARM_MLxEntry - Record information about MLA / MLS instructions.
63struct ARM_MLxEntry {
Craig Topper2fbd1302012-05-24 03:59:11 +000064 uint16_t MLxOpc; // MLA / MLS opcode
65 uint16_t MulOpc; // Expanded multiplication opcode
66 uint16_t AddSubOpc; // Expanded add / sub opcode
Evan Cheng62c7b5b2010-12-05 22:04:16 +000067 bool NegAcc; // True if the acc is negated before the add / sub.
68 bool HasLane; // True if instruction has an extra "lane" operand.
69};
70
71static const ARM_MLxEntry ARM_MLxTable[] = {
72 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
73 // fp scalar ops
74 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
75 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
76 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
77 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
Evan Cheng62c7b5b2010-12-05 22:04:16 +000078 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
79 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
80 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
81 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
82
83 // fp SIMD ops
84 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
85 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
86 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
87 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
88 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
89 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
90 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
91 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
92};
93
Anton Korobeynikov14635da2009-11-02 00:10:38 +000094ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
Evan Cheng703a0fb2011-07-01 17:57:27 +000095 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
Anton Korobeynikov14635da2009-11-02 00:10:38 +000096 Subtarget(STI) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +000097 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
98 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
99 assert(false && "Duplicated entries?");
100 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
101 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
102 }
103}
104
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000105// Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
106// currently defaults to no prepass hazard recognizer.
Eric Christopherf047bfd2014-06-13 22:38:52 +0000107ScheduleHazardRecognizer *
108ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
109 const ScheduleDAG *DAG) const {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000110 if (usePreRAHazardRecognizer()) {
Eric Christopherf047bfd2014-06-13 22:38:52 +0000111 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +0000112 static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000113 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
114 }
Eric Christopherf047bfd2014-06-13 22:38:52 +0000115 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000116}
117
118ScheduleHazardRecognizer *ARMBaseInstrInfo::
119CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
120 const ScheduleDAG *DAG) const {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000121 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
Bill Wendlingf95178e2013-06-07 05:54:19 +0000122 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000123 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
David Goodwinaf7451b2009-07-08 16:09:28 +0000124}
125
126MachineInstr *
127ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
128 MachineBasicBlock::iterator &MBBI,
129 LiveVariables *LV) const {
Evan Cheng0e075e22009-07-27 18:44:00 +0000130 // FIXME: Thumb2 support.
131
David Goodwinaf7451b2009-07-08 16:09:28 +0000132 if (!EnableARM3Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +0000133 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000134
135 MachineInstr *MI = MBBI;
136 MachineFunction &MF = *MI->getParent()->getParent();
Bruno Cardoso Lopesc2f87b72010-06-08 22:51:23 +0000137 uint64_t TSFlags = MI->getDesc().TSFlags;
David Goodwinaf7451b2009-07-08 16:09:28 +0000138 bool isPre = false;
139 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000140 default: return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000141 case ARMII::IndexModePre:
142 isPre = true;
143 break;
144 case ARMII::IndexModePost:
145 break;
146 }
147
148 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
149 // operation.
150 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
151 if (MemOpc == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000152 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000153
Craig Topper062a2ba2014-04-25 05:30:21 +0000154 MachineInstr *UpdateMI = nullptr;
155 MachineInstr *MemMI = nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000156 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000157 const MCInstrDesc &MCID = MI->getDesc();
158 unsigned NumOps = MCID.getNumOperands();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000159 bool isLoad = !MI->mayStore();
David Goodwinaf7451b2009-07-08 16:09:28 +0000160 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
161 const MachineOperand &Base = MI->getOperand(2);
162 const MachineOperand &Offset = MI->getOperand(NumOps-3);
163 unsigned WBReg = WB.getReg();
164 unsigned BaseReg = Base.getReg();
165 unsigned OffReg = Offset.getReg();
166 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
167 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
168 switch (AddrMode) {
Craig Toppere55c5562012-02-07 02:50:20 +0000169 default: llvm_unreachable("Unknown indexed op!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000170 case ARMII::AddrMode2: {
171 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
172 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
173 if (OffReg == 0) {
Evan Chenge3a53c42009-07-08 21:03:57 +0000174 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwinaf7451b2009-07-08 16:09:28 +0000175 // Can't encode it in a so_imm operand. This transformation will
176 // add more than 1 instruction. Abandon!
Craig Topper062a2ba2014-04-25 05:30:21 +0000177 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000178 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000179 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Evan Chenge3a53c42009-07-08 21:03:57 +0000180 .addReg(BaseReg).addImm(Amt)
David Goodwinaf7451b2009-07-08 16:09:28 +0000181 .addImm(Pred).addReg(0).addReg(0);
182 } else if (Amt != 0) {
183 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
184 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
185 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Owen Andersonb595ed02011-07-21 18:54:16 +0000186 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000187 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
188 .addImm(Pred).addReg(0).addReg(0);
189 } else
190 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000191 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000192 .addReg(BaseReg).addReg(OffReg)
193 .addImm(Pred).addReg(0).addReg(0);
194 break;
195 }
196 case ARMII::AddrMode3 : {
197 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
198 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
199 if (OffReg == 0)
200 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
201 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000202 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000203 .addReg(BaseReg).addImm(Amt)
204 .addImm(Pred).addReg(0).addReg(0);
205 else
206 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000207 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000208 .addReg(BaseReg).addReg(OffReg)
209 .addImm(Pred).addReg(0).addReg(0);
210 break;
211 }
212 }
213
214 std::vector<MachineInstr*> NewMIs;
215 if (isPre) {
216 if (isLoad)
217 MemMI = BuildMI(MF, MI->getDebugLoc(),
218 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000219 .addReg(WBReg).addImm(0).addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000220 else
221 MemMI = BuildMI(MF, MI->getDebugLoc(),
222 get(MemOpc)).addReg(MI->getOperand(1).getReg())
223 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
224 NewMIs.push_back(MemMI);
225 NewMIs.push_back(UpdateMI);
226 } else {
227 if (isLoad)
228 MemMI = BuildMI(MF, MI->getDebugLoc(),
229 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000230 .addReg(BaseReg).addImm(0).addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000231 else
232 MemMI = BuildMI(MF, MI->getDebugLoc(),
233 get(MemOpc)).addReg(MI->getOperand(1).getReg())
234 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
235 if (WB.isDead())
236 UpdateMI->getOperand(0).setIsDead();
237 NewMIs.push_back(UpdateMI);
238 NewMIs.push_back(MemMI);
239 }
240
241 // Transfer LiveVariables states, kill / dead info.
242 if (LV) {
243 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
244 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000245 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000246 unsigned Reg = MO.getReg();
247
248 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
249 if (MO.isDef()) {
250 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
251 if (MO.isDead())
252 LV->addVirtualRegisterDead(Reg, NewMI);
253 }
254 if (MO.isUse() && MO.isKill()) {
255 for (unsigned j = 0; j < 2; ++j) {
256 // Look at the two new MI's in reverse order.
257 MachineInstr *NewMI = NewMIs[j];
258 if (!NewMI->readsRegister(Reg))
259 continue;
260 LV->addVirtualRegisterKilled(Reg, NewMI);
261 if (VI.removeKill(MI))
262 VI.Kills.push_back(NewMI);
263 break;
264 }
265 }
266 }
267 }
268 }
269
270 MFI->insert(MBBI, NewMIs[1]);
271 MFI->insert(MBBI, NewMIs[0]);
272 return NewMIs[0];
273}
274
275// Branch analysis.
276bool
277ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
278 MachineBasicBlock *&FBB,
279 SmallVectorImpl<MachineOperand> &Cond,
280 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000281 TBB = nullptr;
282 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000283
David Goodwinaf7451b2009-07-08 16:09:28 +0000284 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000285 if (I == MBB.begin())
Lang Hames24864fe2013-07-19 23:52:47 +0000286 return false; // Empty blocks are easy.
Dale Johannesen4244d122010-04-02 01:38:09 +0000287 --I;
Lang Hames24864fe2013-07-19 23:52:47 +0000288
289 // Walk backwards from the end of the basic block until the branch is
290 // analyzed or we give up.
Lang Hames18c98a52013-12-20 20:27:51 +0000291 while (isPredicated(I) || I->isTerminator() || I->isDebugValue()) {
Lang Hames24864fe2013-07-19 23:52:47 +0000292
293 // Flag to be raised on unanalyzeable instructions. This is useful in cases
294 // where we want to clean up on the end of the basic block before we bail
295 // out.
296 bool CantAnalyze = false;
297
298 // Skip over DEBUG values and predicated nonterminators.
299 while (I->isDebugValue() || !I->isTerminator()) {
300 if (I == MBB.begin())
301 return false;
302 --I;
303 }
304
305 if (isIndirectBranchOpcode(I->getOpcode()) ||
306 isJumpTableBranchOpcode(I->getOpcode())) {
307 // Indirect branches and jump tables can't be analyzed, but we still want
308 // to clean up any instructions at the tail of the basic block.
309 CantAnalyze = true;
310 } else if (isUncondBranchOpcode(I->getOpcode())) {
311 TBB = I->getOperand(0).getMBB();
312 } else if (isCondBranchOpcode(I->getOpcode())) {
313 // Bail out if we encounter multiple conditional branches.
314 if (!Cond.empty())
315 return true;
316
317 assert(!FBB && "FBB should have been null.");
318 FBB = TBB;
319 TBB = I->getOperand(0).getMBB();
320 Cond.push_back(I->getOperand(1));
321 Cond.push_back(I->getOperand(2));
322 } else if (I->isReturn()) {
323 // Returns can't be analyzed, but we should run cleanup.
324 CantAnalyze = !isPredicated(I);
325 } else {
326 // We encountered other unrecognized terminator. Bail out immediately.
327 return true;
328 }
329
330 // Cleanup code - to be run for unpredicated unconditional branches and
331 // returns.
332 if (!isPredicated(I) &&
333 (isUncondBranchOpcode(I->getOpcode()) ||
334 isIndirectBranchOpcode(I->getOpcode()) ||
335 isJumpTableBranchOpcode(I->getOpcode()) ||
336 I->isReturn())) {
337 // Forget any previous condition branch information - it no longer applies.
338 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000339 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000340
341 // If we can modify the function, delete everything below this
342 // unconditional branch.
343 if (AllowModify) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000344 MachineBasicBlock::iterator DI = std::next(I);
Lang Hames24864fe2013-07-19 23:52:47 +0000345 while (DI != MBB.end()) {
346 MachineInstr *InstToDelete = DI;
347 ++DI;
348 InstToDelete->eraseFromParent();
349 }
350 }
351 }
352
353 if (CantAnalyze)
354 return true;
355
Dale Johannesen4244d122010-04-02 01:38:09 +0000356 if (I == MBB.begin())
357 return false;
Lang Hames24864fe2013-07-19 23:52:47 +0000358
Dale Johannesen4244d122010-04-02 01:38:09 +0000359 --I;
360 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000361
Lang Hames24864fe2013-07-19 23:52:47 +0000362 // We made it past the terminators without bailing out - we must have
363 // analyzed this branch successfully.
364 return false;
David Goodwinaf7451b2009-07-08 16:09:28 +0000365}
366
367
368unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000369 MachineBasicBlock::iterator I = MBB.end();
370 if (I == MBB.begin()) return 0;
371 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000372 while (I->isDebugValue()) {
373 if (I == MBB.begin())
374 return 0;
375 --I;
376 }
Evan Cheng056c6692009-07-27 18:20:05 +0000377 if (!isUncondBranchOpcode(I->getOpcode()) &&
378 !isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000379 return 0;
380
381 // Remove the branch.
382 I->eraseFromParent();
383
384 I = MBB.end();
385
386 if (I == MBB.begin()) return 1;
387 --I;
Evan Cheng056c6692009-07-27 18:20:05 +0000388 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000389 return 1;
390
391 // Remove the branch.
392 I->eraseFromParent();
393 return 2;
394}
395
396unsigned
397ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000398 MachineBasicBlock *FBB,
399 const SmallVectorImpl<MachineOperand> &Cond,
400 DebugLoc DL) const {
Evan Cheng780748d2009-07-28 05:48:47 +0000401 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
402 int BOpc = !AFI->isThumbFunction()
403 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
404 int BccOpc = !AFI->isThumbFunction()
405 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000406 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
Andrew Trick3f1fdf12011-09-21 02:17:37 +0000407
David Goodwinaf7451b2009-07-08 16:09:28 +0000408 // Shouldn't be a fall through.
409 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
410 assert((Cond.size() == 2 || Cond.size() == 0) &&
411 "ARM branch conditions have two components!");
412
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000413 // For conditional branches, we use addOperand to preserve CPSR flags.
414
Craig Topper062a2ba2014-04-25 05:30:21 +0000415 if (!FBB) {
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000416 if (Cond.empty()) { // Unconditional branch?
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000417 if (isThumb)
418 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).addImm(ARMCC::AL).addReg(0);
419 else
420 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000421 } else
Stuart Hastings0125b642010-06-17 22:43:56 +0000422 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000423 .addImm(Cond[0].getImm()).addOperand(Cond[1]);
David Goodwinaf7451b2009-07-08 16:09:28 +0000424 return 1;
425 }
426
427 // Two-way conditional branch.
Stuart Hastings0125b642010-06-17 22:43:56 +0000428 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000429 .addImm(Cond[0].getImm()).addOperand(Cond[1]);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000430 if (isThumb)
431 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).addImm(ARMCC::AL).addReg(0);
432 else
433 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwinaf7451b2009-07-08 16:09:28 +0000434 return 2;
435}
436
437bool ARMBaseInstrInfo::
438ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
439 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
440 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
441 return false;
442}
443
Evan Cheng7fae11b2011-12-14 02:11:42 +0000444bool ARMBaseInstrInfo::isPredicated(const MachineInstr *MI) const {
445 if (MI->isBundle()) {
446 MachineBasicBlock::const_instr_iterator I = MI;
447 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
448 while (++I != E && I->isInsideBundle()) {
449 int PIdx = I->findFirstPredOperandIdx();
450 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
451 return true;
452 }
453 return false;
454 }
455
456 int PIdx = MI->findFirstPredOperandIdx();
457 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
458}
459
David Goodwinaf7451b2009-07-08 16:09:28 +0000460bool ARMBaseInstrInfo::
461PredicateInstruction(MachineInstr *MI,
462 const SmallVectorImpl<MachineOperand> &Pred) const {
463 unsigned Opc = MI->getOpcode();
Evan Cheng056c6692009-07-27 18:20:05 +0000464 if (isUncondBranchOpcode(Opc)) {
465 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +0000466 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
467 .addImm(Pred[0].getImm())
468 .addReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000469 return true;
470 }
471
472 int PIdx = MI->findFirstPredOperandIdx();
473 if (PIdx != -1) {
474 MachineOperand &PMO = MI->getOperand(PIdx);
475 PMO.setImm(Pred[0].getImm());
476 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
477 return true;
478 }
479 return false;
480}
481
482bool ARMBaseInstrInfo::
483SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
484 const SmallVectorImpl<MachineOperand> &Pred2) const {
485 if (Pred1.size() > 2 || Pred2.size() > 2)
486 return false;
487
488 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
489 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
490 if (CC1 == CC2)
491 return true;
492
493 switch (CC1) {
494 default:
495 return false;
496 case ARMCC::AL:
497 return true;
498 case ARMCC::HS:
499 return CC2 == ARMCC::HI;
500 case ARMCC::LS:
501 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
502 case ARMCC::GE:
503 return CC2 == ARMCC::GT;
504 case ARMCC::LE:
505 return CC2 == ARMCC::LT;
506 }
507}
508
509bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
510 std::vector<MachineOperand> &Pred) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000511 bool Found = false;
512 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
513 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +0000514 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
515 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000516 Pred.push_back(MO);
517 Found = true;
518 }
519 }
520
521 return Found;
522}
523
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000524static bool isCPSRDefined(const MachineInstr *MI) {
525 for (const auto &MO : MI->operands())
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000526 if (MO.isReg() && MO.getReg() == ARM::CPSR && MO.isDef())
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000527 return true;
528 return false;
529}
530
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000531static bool isEligibleForITBlock(const MachineInstr *MI) {
532 switch (MI->getOpcode()) {
533 default: return true;
534 case ARM::tADC: // ADC (register) T1
535 case ARM::tADDi3: // ADD (immediate) T1
536 case ARM::tADDi8: // ADD (immediate) T2
537 case ARM::tADDrr: // ADD (register) T1
538 case ARM::tAND: // AND (register) T1
539 case ARM::tASRri: // ASR (immediate) T1
540 case ARM::tASRrr: // ASR (register) T1
541 case ARM::tBIC: // BIC (register) T1
542 case ARM::tEOR: // EOR (register) T1
543 case ARM::tLSLri: // LSL (immediate) T1
544 case ARM::tLSLrr: // LSL (register) T1
545 case ARM::tLSRri: // LSR (immediate) T1
546 case ARM::tLSRrr: // LSR (register) T1
547 case ARM::tMUL: // MUL T1
548 case ARM::tMVN: // MVN (register) T1
549 case ARM::tORR: // ORR (register) T1
550 case ARM::tROR: // ROR (register) T1
551 case ARM::tRSB: // RSB (immediate) T1
552 case ARM::tSBC: // SBC (register) T1
553 case ARM::tSUBi3: // SUB (immediate) T1
554 case ARM::tSUBi8: // SUB (immediate) T2
555 case ARM::tSUBrr: // SUB (register) T1
556 return !isCPSRDefined(MI);
557 }
558}
559
Evan Chenga33fc862009-11-21 06:21:52 +0000560/// isPredicable - Return true if the specified instruction can be predicated.
561/// By default, this returns true for every instruction with a
562/// PredicateOperand.
563bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000564 if (!MI->isPredicable())
Evan Chenga33fc862009-11-21 06:21:52 +0000565 return false;
566
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000567 if (!isEligibleForITBlock(MI))
568 return false;
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000569
Joey Goulya5153cb2013-09-09 14:21:49 +0000570 ARMFunctionInfo *AFI =
571 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
572
573 if (AFI->isThumb2Function()) {
Weiming Zhao0da5cc02013-11-13 18:29:49 +0000574 if (getSubtarget().restrictIT())
Joey Goulya5153cb2013-09-09 14:21:49 +0000575 return isV8EligibleForIT(MI);
576 } else { // non-Thumb
577 if ((MI->getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
578 return false;
Evan Chenga33fc862009-11-21 06:21:52 +0000579 }
Joey Goulya5153cb2013-09-09 14:21:49 +0000580
Evan Chenga33fc862009-11-21 06:21:52 +0000581 return true;
582}
David Goodwinaf7451b2009-07-08 16:09:28 +0000583
Benjamin Kramer44a53da2014-04-12 18:45:24 +0000584namespace llvm {
585template <> bool IsCPSRDead<MachineInstr>(MachineInstr *MI) {
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000586 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
587 const MachineOperand &MO = MI->getOperand(i);
588 if (!MO.isReg() || MO.isUndef() || MO.isUse())
589 continue;
590 if (MO.getReg() != ARM::CPSR)
591 continue;
592 if (!MO.isDead())
593 return false;
594 }
595 // all definitions of CPSR are dead
596 return true;
597}
Benjamin Kramer44a53da2014-04-12 18:45:24 +0000598}
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000599
David Goodwinaf7451b2009-07-08 16:09:28 +0000600/// GetInstSize - Return the size of the specified MachineInstr.
601///
602unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
603 const MachineBasicBlock &MBB = *MI->getParent();
604 const MachineFunction *MF = MBB.getParent();
Chris Lattnere9a75a62009-08-22 21:43:10 +0000605 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwinaf7451b2009-07-08 16:09:28 +0000606
Evan Cheng6cc775f2011-06-28 19:10:37 +0000607 const MCInstrDesc &MCID = MI->getDesc();
Owen Anderson651b2302011-07-13 23:22:26 +0000608 if (MCID.getSize())
609 return MCID.getSize();
David Goodwinaf7451b2009-07-08 16:09:28 +0000610
David Blaikie46a9f012012-01-20 21:51:11 +0000611 // If this machine instr is an inline asm, measure it.
612 if (MI->getOpcode() == ARM::INLINEASM)
613 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
David Blaikie46a9f012012-01-20 21:51:11 +0000614 unsigned Opc = MI->getOpcode();
615 switch (Opc) {
Rafael Espindolaafeb01c2014-03-07 04:45:03 +0000616 default:
617 // pseudo-instruction sizes are zero.
David Blaikie46a9f012012-01-20 21:51:11 +0000618 return 0;
619 case TargetOpcode::BUNDLE:
620 return getInstBundleLength(MI);
621 case ARM::MOVi16_ga_pcrel:
622 case ARM::MOVTi16_ga_pcrel:
623 case ARM::t2MOVi16_ga_pcrel:
624 case ARM::t2MOVTi16_ga_pcrel:
625 return 4;
626 case ARM::MOVi32imm:
627 case ARM::t2MOVi32imm:
628 return 8;
629 case ARM::CONSTPOOL_ENTRY:
630 // If this machine instr is a constant pool entry, its size is recorded as
631 // operand #2.
632 return MI->getOperand(2).getImm();
633 case ARM::Int_eh_sjlj_longjmp:
634 return 16;
635 case ARM::tInt_eh_sjlj_longjmp:
636 return 10;
637 case ARM::Int_eh_sjlj_setjmp:
638 case ARM::Int_eh_sjlj_setjmp_nofp:
639 return 20;
640 case ARM::tInt_eh_sjlj_setjmp:
641 case ARM::t2Int_eh_sjlj_setjmp:
642 case ARM::t2Int_eh_sjlj_setjmp_nofp:
643 return 12;
644 case ARM::BR_JTr:
645 case ARM::BR_JTm:
646 case ARM::BR_JTadd:
647 case ARM::tBR_JTr:
648 case ARM::t2BR_JT:
649 case ARM::t2TBB_JT:
650 case ARM::t2TBH_JT: {
651 // These are jumptable branches, i.e. a branch followed by an inlined
652 // jumptable. The size is 4 + 4 * number of entries. For TBB, each
653 // entry is one byte; TBH two byte each.
654 unsigned EntrySize = (Opc == ARM::t2TBB_JT)
655 ? 1 : ((Opc == ARM::t2TBH_JT) ? 2 : 4);
656 unsigned NumOps = MCID.getNumOperands();
657 MachineOperand JTOP =
658 MI->getOperand(NumOps - (MI->isPredicable() ? 3 : 2));
659 unsigned JTI = JTOP.getIndex();
660 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
Craig Toppere73658d2014-04-28 04:05:08 +0000661 assert(MJTI != nullptr);
David Blaikie46a9f012012-01-20 21:51:11 +0000662 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
663 assert(JTI < JT.size());
664 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
665 // 4 aligned. The assembler / linker may add 2 byte padding just before
666 // the JT entries. The size does not include this padding; the
667 // constant islands pass does separate bookkeeping for it.
668 // FIXME: If we know the size of the function is less than (1 << 16) *2
669 // bytes, we can use 16-bit entries instead. Then there won't be an
670 // alignment issue.
671 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
Benjamin Kramer2c3778d2014-10-09 19:50:39 +0000672 unsigned NumEntries = JT[JTI].MBBs.size();
David Blaikie46a9f012012-01-20 21:51:11 +0000673 if (Opc == ARM::t2TBB_JT && (NumEntries & 1))
674 // Make sure the instruction that follows TBB is 2-byte aligned.
675 // FIXME: Constant island pass should insert an "ALIGN" instruction
676 // instead.
677 ++NumEntries;
678 return NumEntries * EntrySize + InstSize;
679 }
Tim Northover650b0ee52014-11-13 17:58:48 +0000680 case ARM::SPACE:
681 return MI->getOperand(1).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000682 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000683}
684
Evan Cheng7fae11b2011-12-14 02:11:42 +0000685unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr *MI) const {
686 unsigned Size = 0;
687 MachineBasicBlock::const_instr_iterator I = MI;
688 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
689 while (++I != E && I->isInsideBundle()) {
690 assert(!I->isBundle() && "No nested bundle!");
691 Size += GetInstSizeInBytes(&*I);
692 }
693 return Size;
694}
695
Tim Northover5d72c5d2014-10-01 19:21:03 +0000696void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB,
697 MachineBasicBlock::iterator I,
698 unsigned DestReg, bool KillSrc,
699 const ARMSubtarget &Subtarget) const {
700 unsigned Opc = Subtarget.isThumb()
701 ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
702 : ARM::MRS;
703
704 MachineInstrBuilder MIB =
705 BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
706
707 // There is only 1 A/R class MRS instruction, and it always refers to
708 // APSR. However, there are lots of other possibilities on M-class cores.
709 if (Subtarget.isMClass())
710 MIB.addImm(0x800);
711
712 AddDefaultPred(MIB);
713
714 MIB.addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
715}
716
717void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
718 MachineBasicBlock::iterator I,
719 unsigned SrcReg, bool KillSrc,
720 const ARMSubtarget &Subtarget) const {
721 unsigned Opc = Subtarget.isThumb()
722 ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
723 : ARM::MSR;
724
725 MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
726
727 if (Subtarget.isMClass())
728 MIB.addImm(0x800);
729 else
730 MIB.addImm(8);
731
732 MIB.addReg(SrcReg, getKillRegState(KillSrc));
733
734 AddDefaultPred(MIB);
735
736 MIB.addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
737}
738
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000739void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
740 MachineBasicBlock::iterator I, DebugLoc DL,
741 unsigned DestReg, unsigned SrcReg,
742 bool KillSrc) const {
743 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000744 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson70aa8d02010-02-16 17:24:15 +0000745
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000746 if (GPRDest && GPRSrc) {
747 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
Jim Grosbach8815bef2013-10-22 02:29:35 +0000748 .addReg(SrcReg, getKillRegState(KillSrc))));
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000749 return;
David Goodwine5b5d8f2009-08-05 21:02:22 +0000750 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000751
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000752 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000753 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000754
Chad Rosierbe762512011-08-20 00:17:25 +0000755 unsigned Opc = 0;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000756 if (SPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000757 Opc = ARM::VMOVS;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000758 else if (GPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000759 Opc = ARM::VMOVRS;
760 else if (SPRDest && GPRSrc)
761 Opc = ARM::VMOVSR;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000762 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP())
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000763 Opc = ARM::VMOVD;
764 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
Owen Anderson454e1c72011-07-15 18:46:47 +0000765 Opc = ARM::VORRq;
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000766
Chad Rosierbe762512011-08-20 00:17:25 +0000767 if (Opc) {
768 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
Owen Anderson454e1c72011-07-15 18:46:47 +0000769 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosierbe762512011-08-20 00:17:25 +0000770 if (Opc == ARM::VORRq)
771 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosier61f92ef2011-08-20 00:52:40 +0000772 AddDefaultPred(MIB);
Chad Rosierbe762512011-08-20 00:17:25 +0000773 return;
774 }
775
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000776 // Handle register classes that require multiple instructions.
777 unsigned BeginIdx = 0;
778 unsigned SubRegs = 0;
Andrew Trickb57e2252012-08-29 04:41:37 +0000779 int Spacing = 1;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000780
781 // Use VORRq when possible.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000782 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
783 Opc = ARM::VORRq;
784 BeginIdx = ARM::qsub_0;
785 SubRegs = 2;
786 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
787 Opc = ARM::VORRq;
788 BeginIdx = ARM::qsub_0;
789 SubRegs = 4;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000790 // Fall back to VMOVD.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000791 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
792 Opc = ARM::VMOVD;
793 BeginIdx = ARM::dsub_0;
794 SubRegs = 2;
795 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
796 Opc = ARM::VMOVD;
797 BeginIdx = ARM::dsub_0;
798 SubRegs = 3;
799 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
800 Opc = ARM::VMOVD;
801 BeginIdx = ARM::dsub_0;
802 SubRegs = 4;
803 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
Jim Grosbachdba14dd2013-10-22 02:29:37 +0000804 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000805 BeginIdx = ARM::gsub_0;
806 SubRegs = 2;
807 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
808 Opc = ARM::VMOVD;
809 BeginIdx = ARM::dsub_0;
810 SubRegs = 2;
811 Spacing = 2;
812 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
813 Opc = ARM::VMOVD;
814 BeginIdx = ARM::dsub_0;
815 SubRegs = 3;
816 Spacing = 2;
817 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
818 Opc = ARM::VMOVD;
819 BeginIdx = ARM::dsub_0;
820 SubRegs = 4;
821 Spacing = 2;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000822 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) {
823 Opc = ARM::VMOVS;
824 BeginIdx = ARM::ssub_0;
825 SubRegs = 2;
Tim Northover5d72c5d2014-10-01 19:21:03 +0000826 } else if (SrcReg == ARM::CPSR) {
827 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
828 return;
829 } else if (DestReg == ARM::CPSR) {
830 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
831 return;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000832 }
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000833
Andrew Trickb57e2252012-08-29 04:41:37 +0000834 assert(Opc && "Impossible reg-to-reg copy");
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000835
Andrew Trick4cc69492012-08-29 01:58:52 +0000836 const TargetRegisterInfo *TRI = &getRegisterInfo();
837 MachineInstrBuilder Mov;
Andrew Trickbd0073d2012-08-29 01:58:55 +0000838
839 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
840 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000841 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000842 Spacing = -Spacing;
843 }
844#ifndef NDEBUG
845 SmallSet<unsigned, 4> DstRegs;
846#endif
Andrew Trick4cc69492012-08-29 01:58:52 +0000847 for (unsigned i = 0; i != SubRegs; ++i) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000848 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
849 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
Andrew Trick4cc69492012-08-29 01:58:52 +0000850 assert(Dst && Src && "Bad sub-register");
Andrew Trickbd0073d2012-08-29 01:58:55 +0000851#ifndef NDEBUG
Andrew Trickbd0073d2012-08-29 01:58:55 +0000852 assert(!DstRegs.count(Src) && "destructive vector copy");
Andrew Trickb57e2252012-08-29 04:41:37 +0000853 DstRegs.insert(Dst);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000854#endif
Jim Grosbach8815bef2013-10-22 02:29:35 +0000855 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
Andrew Trick4cc69492012-08-29 01:58:52 +0000856 // VORR takes two source operands.
857 if (Opc == ARM::VORRq)
858 Mov.addReg(Src);
859 Mov = AddDefaultPred(Mov);
JF Bastien583db652013-07-12 23:33:03 +0000860 // MOVr can set CC.
861 if (Opc == ARM::MOVr)
862 Mov = AddDefaultCC(Mov);
Andrew Trick4cc69492012-08-29 01:58:52 +0000863 }
864 // Add implicit super-register defs and kills to the last instruction.
865 Mov->addRegisterDefined(DestReg, TRI);
866 if (KillSrc)
867 Mov->addRegisterKilled(SrcReg, TRI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000868}
869
Tim Northover798697d2013-04-21 11:57:07 +0000870const MachineInstrBuilder &
871ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
872 unsigned SubIdx, unsigned State,
873 const TargetRegisterInfo *TRI) const {
Evan Chengddc93c72010-05-07 00:24:52 +0000874 if (!SubIdx)
875 return MIB.addReg(Reg, State);
876
877 if (TargetRegisterInfo::isPhysicalRegister(Reg))
878 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
879 return MIB.addReg(Reg, State, SubIdx);
880}
881
David Goodwinaf7451b2009-07-08 16:09:28 +0000882void ARMBaseInstrInfo::
883storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
884 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000885 const TargetRegisterClass *RC,
886 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000887 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +0000888 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000889 MachineFunction &MF = *MBB.getParent();
890 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000891 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000892
893 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000894 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000895 MachineMemOperand::MOStore,
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000896 MFI.getObjectSize(FI),
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000897 Align);
David Goodwinaf7451b2009-07-08 16:09:28 +0000898
Owen Anderson732f82c2011-08-10 17:21:20 +0000899 switch (RC->getSize()) {
900 case 4:
901 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
902 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
David Goodwinaf7451b2009-07-08 16:09:28 +0000903 .addReg(SrcReg, getKillRegState(isKill))
Jim Grosbach338de3e2010-10-27 23:12:14 +0000904 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000905 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
906 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
Evan Cheng9d768f42010-05-06 01:34:11 +0000907 .addReg(SrcReg, getKillRegState(isKill))
908 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000909 } else
910 llvm_unreachable("Unknown reg class!");
911 break;
912 case 8:
913 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
914 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwinaf7451b2009-07-08 16:09:28 +0000915 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000916 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +0000917 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +0000918 if (Subtarget.hasV5TEOps()) {
919 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD));
920 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
921 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
922 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
923
924 AddDefaultPred(MIB);
925 } else {
926 // Fallback to STM instruction, which has existed since the dawn of
927 // time.
928 MachineInstrBuilder MIB =
929 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STMIA))
930 .addFrameIndex(FI).addMemOperand(MMO));
931 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
932 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
933 }
Owen Anderson732f82c2011-08-10 17:21:20 +0000934 } else
935 llvm_unreachable("Unknown reg class!");
936 break;
937 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +0000938 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +0000939 // Use aligned spills if the stack can be realigned.
940 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000941 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64))
Bob Wilson4c1ca292010-07-06 21:26:18 +0000942 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000943 .addReg(SrcReg, getKillRegState(isKill))
944 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000945 } else {
946 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000947 .addReg(SrcReg, getKillRegState(isKill))
948 .addFrameIndex(FI)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000949 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000950 }
951 } else
952 llvm_unreachable("Unknown reg class!");
953 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000954 case 24:
955 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
956 // Use aligned spills if the stack can be realigned.
957 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
958 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
959 .addFrameIndex(FI).addImm(16)
960 .addReg(SrcReg, getKillRegState(isKill))
961 .addMemOperand(MMO));
962 } else {
963 MachineInstrBuilder MIB =
964 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
965 .addFrameIndex(FI))
966 .addMemOperand(MMO);
967 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
968 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
969 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
970 }
971 } else
972 llvm_unreachable("Unknown reg class!");
973 break;
Owen Anderson732f82c2011-08-10 17:21:20 +0000974 case 32:
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000975 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +0000976 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
977 // FIXME: It's possible to only store part of the QQ register if the
978 // spilled def has a sub-register index.
979 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +0000980 .addFrameIndex(FI).addImm(16)
981 .addReg(SrcReg, getKillRegState(isKill))
982 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000983 } else {
984 MachineInstrBuilder MIB =
985 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000986 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +0000987 .addMemOperand(MMO);
988 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
989 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
990 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
991 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
992 }
993 } else
994 llvm_unreachable("Unknown reg class!");
995 break;
996 case 64:
997 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
998 MachineInstrBuilder MIB =
999 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
1000 .addFrameIndex(FI))
1001 .addMemOperand(MMO);
1002 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1003 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1004 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1005 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
1006 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
1007 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
1008 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
1009 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
1010 } else
1011 llvm_unreachable("Unknown reg class!");
1012 break;
1013 default:
1014 llvm_unreachable("Unknown reg class!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001015 }
1016}
1017
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001018unsigned
1019ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
1020 int &FrameIndex) const {
1021 switch (MI->getOpcode()) {
1022 default: break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001023 case ARM::STRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001024 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
1025 if (MI->getOperand(1).isFI() &&
1026 MI->getOperand(2).isReg() &&
1027 MI->getOperand(3).isImm() &&
1028 MI->getOperand(2).getReg() == 0 &&
1029 MI->getOperand(3).getImm() == 0) {
1030 FrameIndex = MI->getOperand(1).getIndex();
1031 return MI->getOperand(0).getReg();
1032 }
1033 break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001034 case ARM::STRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001035 case ARM::t2STRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001036 case ARM::tSTRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001037 case ARM::VSTRD:
1038 case ARM::VSTRS:
1039 if (MI->getOperand(1).isFI() &&
1040 MI->getOperand(2).isImm() &&
1041 MI->getOperand(2).getImm() == 0) {
1042 FrameIndex = MI->getOperand(1).getIndex();
1043 return MI->getOperand(0).getReg();
1044 }
1045 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001046 case ARM::VST1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001047 case ARM::VST1d64TPseudo:
1048 case ARM::VST1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001049 if (MI->getOperand(0).isFI() &&
1050 MI->getOperand(2).getSubReg() == 0) {
1051 FrameIndex = MI->getOperand(0).getIndex();
1052 return MI->getOperand(2).getReg();
1053 }
Jakob Stoklund Olesenb929c712010-09-15 21:40:09 +00001054 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001055 case ARM::VSTMQIA:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001056 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001057 MI->getOperand(0).getSubReg() == 0) {
1058 FrameIndex = MI->getOperand(1).getIndex();
1059 return MI->getOperand(0).getReg();
1060 }
1061 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001062 }
1063
1064 return 0;
1065}
1066
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001067unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
1068 int &FrameIndex) const {
1069 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001070 return MI->mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001071}
1072
David Goodwinaf7451b2009-07-08 16:09:28 +00001073void ARMBaseInstrInfo::
1074loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1075 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +00001076 const TargetRegisterClass *RC,
1077 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +00001078 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +00001079 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001080 MachineFunction &MF = *MBB.getParent();
1081 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001082 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001083 MachineMemOperand *MMO =
Chris Lattnere3d864b2010-09-21 04:39:43 +00001084 MF.getMachineMemOperand(
Jay Foad465101b2011-11-15 07:34:52 +00001085 MachinePointerInfo::getFixedStack(FI),
Chris Lattnere3d864b2010-09-21 04:39:43 +00001086 MachineMemOperand::MOLoad,
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001087 MFI.getObjectSize(FI),
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001088 Align);
David Goodwinaf7451b2009-07-08 16:09:28 +00001089
Owen Anderson732f82c2011-08-10 17:21:20 +00001090 switch (RC->getSize()) {
1091 case 4:
1092 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
1093 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1094 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilson37f106e2010-02-16 22:01:59 +00001095
Owen Anderson732f82c2011-08-10 17:21:20 +00001096 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
1097 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001098 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001099 } else
1100 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001101 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001102 case 8:
1103 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
1104 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Evan Cheng9d768f42010-05-06 01:34:11 +00001105 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001106 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001107 MachineInstrBuilder MIB;
1108
1109 if (Subtarget.hasV5TEOps()) {
1110 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1111 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1112 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1113 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
1114
1115 AddDefaultPred(MIB);
1116 } else {
1117 // Fallback to LDM instruction, which has existed since the dawn of
1118 // time.
1119 MIB = AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDMIA))
1120 .addFrameIndex(FI).addMemOperand(MMO));
1121 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1122 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1123 }
1124
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001125 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1126 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001127 } else
1128 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001129 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001130 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001131 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001132 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001133 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
Bob Wilson4c1ca292010-07-06 21:26:18 +00001134 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +00001135 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001136 } else {
1137 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1138 .addFrameIndex(FI)
1139 .addMemOperand(MMO));
1140 }
1141 } else
1142 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001143 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001144 case 24:
1145 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1146 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1147 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1148 .addFrameIndex(FI).addImm(16)
1149 .addMemOperand(MMO));
1150 } else {
1151 MachineInstrBuilder MIB =
1152 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1153 .addFrameIndex(FI)
1154 .addMemOperand(MMO));
1155 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1156 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1157 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1158 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1159 MIB.addReg(DestReg, RegState::ImplicitDefine);
1160 }
1161 } else
1162 llvm_unreachable("Unknown reg class!");
1163 break;
1164 case 32:
1165 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001166 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1167 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +00001168 .addFrameIndex(FI).addImm(16)
1169 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001170 } else {
1171 MachineInstrBuilder MIB =
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001172 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1173 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +00001174 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001175 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1176 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1177 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1178 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001179 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1180 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001181 }
1182 } else
1183 llvm_unreachable("Unknown reg class!");
1184 break;
1185 case 64:
1186 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1187 MachineInstrBuilder MIB =
1188 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1189 .addFrameIndex(FI))
1190 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001191 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1192 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1193 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1194 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1195 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1196 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1197 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1198 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001199 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1200 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001201 } else
1202 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001203 break;
Bob Wilsona92e41a2010-06-18 21:32:42 +00001204 default:
1205 llvm_unreachable("Unknown regclass!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001206 }
1207}
1208
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001209unsigned
1210ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
1211 int &FrameIndex) const {
1212 switch (MI->getOpcode()) {
1213 default: break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001214 case ARM::LDRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001215 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
1216 if (MI->getOperand(1).isFI() &&
1217 MI->getOperand(2).isReg() &&
1218 MI->getOperand(3).isImm() &&
1219 MI->getOperand(2).getReg() == 0 &&
1220 MI->getOperand(3).getImm() == 0) {
1221 FrameIndex = MI->getOperand(1).getIndex();
1222 return MI->getOperand(0).getReg();
1223 }
1224 break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001225 case ARM::LDRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001226 case ARM::t2LDRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001227 case ARM::tLDRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001228 case ARM::VLDRD:
1229 case ARM::VLDRS:
1230 if (MI->getOperand(1).isFI() &&
1231 MI->getOperand(2).isImm() &&
1232 MI->getOperand(2).getImm() == 0) {
1233 FrameIndex = MI->getOperand(1).getIndex();
1234 return MI->getOperand(0).getReg();
1235 }
1236 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001237 case ARM::VLD1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001238 case ARM::VLD1d64TPseudo:
1239 case ARM::VLD1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001240 if (MI->getOperand(1).isFI() &&
1241 MI->getOperand(0).getSubReg() == 0) {
1242 FrameIndex = MI->getOperand(1).getIndex();
1243 return MI->getOperand(0).getReg();
1244 }
1245 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001246 case ARM::VLDMQIA:
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001247 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001248 MI->getOperand(0).getSubReg() == 0) {
1249 FrameIndex = MI->getOperand(1).getIndex();
1250 return MI->getOperand(0).getReg();
1251 }
1252 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001253 }
1254
1255 return 0;
1256}
1257
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001258unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
1259 int &FrameIndex) const {
1260 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001261 return MI->mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001262}
1263
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001264bool
1265ARMBaseInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
1266 MachineFunction &MF = *MI->getParent()->getParent();
1267 Reloc::Model RM = MF.getTarget().getRelocationModel();
1268
1269 if (MI->getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
1270 assert(getSubtarget().getTargetTriple().getObjectFormat() ==
1271 Triple::MachO &&
1272 "LOAD_STACK_GUARD currently supported only for MachO.");
1273 expandLoadStackGuard(MI, RM);
1274 MI->getParent()->erase(MI);
1275 return true;
1276 }
1277
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001278 // This hook gets to expand COPY instructions before they become
1279 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1280 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1281 // changed into a VORR that can go down the NEON pipeline.
Oliver Stannard51b1d462014-08-21 12:50:31 +00001282 if (!WidenVMOVS || !MI->isCopy() || Subtarget.isCortexA15() ||
1283 Subtarget.isFPOnlySP())
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001284 return false;
1285
1286 // Look for a copy between even S-registers. That is where we keep floats
1287 // when using NEON v2f32 instructions for f32 arithmetic.
1288 unsigned DstRegS = MI->getOperand(0).getReg();
1289 unsigned SrcRegS = MI->getOperand(1).getReg();
1290 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1291 return false;
1292
1293 const TargetRegisterInfo *TRI = &getRegisterInfo();
1294 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1295 &ARM::DPRRegClass);
1296 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1297 &ARM::DPRRegClass);
1298 if (!DstRegD || !SrcRegD)
1299 return false;
1300
1301 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1302 // legal if the COPY already defines the full DstRegD, and it isn't a
1303 // sub-register insertion.
1304 if (!MI->definesRegister(DstRegD, TRI) || MI->readsRegister(DstRegD, TRI))
1305 return false;
1306
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001307 // A dead copy shouldn't show up here, but reject it just in case.
1308 if (MI->getOperand(0).isDead())
1309 return false;
1310
1311 // All clear, widen the COPY.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001312 DEBUG(dbgs() << "widening: " << *MI);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001313 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001314
1315 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg
1316 // or some other super-register.
1317 int ImpDefIdx = MI->findRegisterDefOperandIdx(DstRegD);
1318 if (ImpDefIdx != -1)
1319 MI->RemoveOperand(ImpDefIdx);
1320
1321 // Change the opcode and operands.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001322 MI->setDesc(get(ARM::VMOVD));
1323 MI->getOperand(0).setReg(DstRegD);
1324 MI->getOperand(1).setReg(SrcRegD);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001325 AddDefaultPred(MIB);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001326
1327 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1328 // register scavenger and machine verifier, so we need to indicate that we
1329 // are reading an undefined value from SrcRegD, but a proper value from
1330 // SrcRegS.
1331 MI->getOperand(1).setIsUndef();
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001332 MIB.addReg(SrcRegS, RegState::Implicit);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001333
1334 // SrcRegD may actually contain an unrelated value in the ssub_1
1335 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
1336 if (MI->getOperand(1).isKill()) {
1337 MI->getOperand(1).setIsKill(false);
1338 MI->addRegisterKilled(SrcRegS, TRI, true);
1339 }
1340
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001341 DEBUG(dbgs() << "replaced by: " << *MI);
1342 return true;
1343}
1344
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001345/// Create a copy of a const pool value. Update CPI to the new index and return
1346/// the label UID.
1347static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1348 MachineConstantPool *MCP = MF.getConstantPool();
1349 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1350
1351 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1352 assert(MCPE.isMachineConstantPoolEntry() &&
1353 "Expecting a machine constantpool entry!");
1354 ARMConstantPoolValue *ACPV =
1355 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1356
Evan Chengdfce83c2011-01-17 08:03:18 +00001357 unsigned PCLabelId = AFI->createPICLabelUId();
Craig Topper062a2ba2014-04-25 05:30:21 +00001358 ARMConstantPoolValue *NewCPV = nullptr;
Oliver Stannard8f859942014-01-29 16:01:24 +00001359
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001360 // FIXME: The below assumes PIC relocation model and that the function
1361 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1362 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1363 // instructions, so that's probably OK, but is PIC always correct when
1364 // we get here?
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001365 if (ACPV->isGlobalValue())
Bill Wendling7753d662011-10-01 08:00:54 +00001366 NewCPV = ARMConstantPoolConstant::
1367 Create(cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId,
1368 ARMCP::CPValue, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001369 else if (ACPV->isExtSymbol())
Bill Wendlingc214cb02011-10-01 08:58:29 +00001370 NewCPV = ARMConstantPoolSymbol::
1371 Create(MF.getFunction()->getContext(),
1372 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001373 else if (ACPV->isBlockAddress())
Bill Wendling7753d662011-10-01 08:00:54 +00001374 NewCPV = ARMConstantPoolConstant::
1375 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1376 ARMCP::CPBlockAddress, 4);
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001377 else if (ACPV->isLSDA())
Bill Wendling7753d662011-10-01 08:00:54 +00001378 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId,
1379 ARMCP::CPLSDA, 4);
Bill Wendling69bc3de2011-09-29 23:50:42 +00001380 else if (ACPV->isMachineBasicBlock())
Bill Wendling4a4772f2011-10-01 09:30:42 +00001381 NewCPV = ARMConstantPoolMBB::
1382 Create(MF.getFunction()->getContext(),
1383 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001384 else
1385 llvm_unreachable("Unexpected ARM constantpool value type!!");
1386 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1387 return PCLabelId;
1388}
1389
Evan Chengfe864422009-11-08 00:15:23 +00001390void ARMBaseInstrInfo::
1391reMaterialize(MachineBasicBlock &MBB,
1392 MachineBasicBlock::iterator I,
1393 unsigned DestReg, unsigned SubIdx,
Evan Cheng6ad7da92009-11-14 02:55:43 +00001394 const MachineInstr *Orig,
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001395 const TargetRegisterInfo &TRI) const {
Evan Chengfe864422009-11-08 00:15:23 +00001396 unsigned Opcode = Orig->getOpcode();
1397 switch (Opcode) {
1398 default: {
1399 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001400 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfe864422009-11-08 00:15:23 +00001401 MBB.insert(I, MI);
1402 break;
1403 }
1404 case ARM::tLDRpci_pic:
1405 case ARM::t2LDRpci_pic: {
1406 MachineFunction &MF = *MBB.getParent();
Evan Chengfe864422009-11-08 00:15:23 +00001407 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001408 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfe864422009-11-08 00:15:23 +00001409 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1410 DestReg)
1411 .addConstantPoolIndex(CPI).addImm(PCLabelId);
Chris Lattner1d0c2572011-04-29 05:24:29 +00001412 MIB->setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
Evan Chengfe864422009-11-08 00:15:23 +00001413 break;
1414 }
1415 }
Evan Chengfe864422009-11-08 00:15:23 +00001416}
1417
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001418MachineInstr *
1419ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001420 MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001421 switch(Orig->getOpcode()) {
1422 case ARM::tLDRpci_pic:
1423 case ARM::t2LDRpci_pic: {
1424 unsigned CPI = Orig->getOperand(1).getIndex();
1425 unsigned PCLabelId = duplicateCPV(MF, CPI);
1426 Orig->getOperand(1).setIndex(CPI);
1427 Orig->getOperand(2).setImm(PCLabelId);
1428 break;
1429 }
1430 }
1431 return MI;
1432}
1433
Evan Chenge9c46c22010-03-03 01:44:33 +00001434bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
Evan Chengb8b0ad82011-01-20 08:34:58 +00001435 const MachineInstr *MI1,
1436 const MachineRegisterInfo *MRI) const {
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001437 int Opcode = MI0->getOpcode();
Evan Cheng028ccbfc2011-01-20 23:55:07 +00001438 if (Opcode == ARM::t2LDRpci ||
Evan Chengbbd50b02009-11-20 02:10:27 +00001439 Opcode == ARM::t2LDRpci_pic ||
1440 Opcode == ARM::tLDRpci ||
Evan Chengb8b0ad82011-01-20 08:34:58 +00001441 Opcode == ARM::tLDRpci_pic ||
Tim Northover72360d22013-12-02 10:35:41 +00001442 Opcode == ARM::LDRLIT_ga_pcrel ||
1443 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1444 Opcode == ARM::tLDRLIT_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001445 Opcode == ARM::MOV_ga_pcrel ||
1446 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001447 Opcode == ARM::t2MOV_ga_pcrel) {
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001448 if (MI1->getOpcode() != Opcode)
1449 return false;
1450 if (MI0->getNumOperands() != MI1->getNumOperands())
1451 return false;
1452
1453 const MachineOperand &MO0 = MI0->getOperand(1);
1454 const MachineOperand &MO1 = MI1->getOperand(1);
1455 if (MO0.getOffset() != MO1.getOffset())
1456 return false;
1457
Tim Northover72360d22013-12-02 10:35:41 +00001458 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1459 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1460 Opcode == ARM::tLDRLIT_ga_pcrel ||
1461 Opcode == ARM::MOV_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001462 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001463 Opcode == ARM::t2MOV_ga_pcrel)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001464 // Ignore the PC labels.
1465 return MO0.getGlobal() == MO1.getGlobal();
1466
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001467 const MachineFunction *MF = MI0->getParent()->getParent();
1468 const MachineConstantPool *MCP = MF->getConstantPool();
1469 int CPI0 = MO0.getIndex();
1470 int CPI1 = MO1.getIndex();
1471 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1472 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
Evan Chengf098bf12011-03-24 06:20:03 +00001473 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1474 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1475 if (isARMCP0 && isARMCP1) {
1476 ARMConstantPoolValue *ACPV0 =
1477 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1478 ARMConstantPoolValue *ACPV1 =
1479 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1480 return ACPV0->hasSameValue(ACPV1);
1481 } else if (!isARMCP0 && !isARMCP1) {
1482 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1483 }
1484 return false;
Evan Chengb8b0ad82011-01-20 08:34:58 +00001485 } else if (Opcode == ARM::PICLDR) {
1486 if (MI1->getOpcode() != Opcode)
1487 return false;
1488 if (MI0->getNumOperands() != MI1->getNumOperands())
1489 return false;
1490
1491 unsigned Addr0 = MI0->getOperand(1).getReg();
1492 unsigned Addr1 = MI1->getOperand(1).getReg();
1493 if (Addr0 != Addr1) {
1494 if (!MRI ||
1495 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1496 !TargetRegisterInfo::isVirtualRegister(Addr1))
1497 return false;
1498
1499 // This assumes SSA form.
1500 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1501 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1502 // Check if the loaded value, e.g. a constantpool of a global address, are
1503 // the same.
1504 if (!produceSameValue(Def0, Def1, MRI))
1505 return false;
1506 }
1507
1508 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1509 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1510 const MachineOperand &MO0 = MI0->getOperand(i);
1511 const MachineOperand &MO1 = MI1->getOperand(i);
1512 if (!MO0.isIdenticalTo(MO1))
1513 return false;
1514 }
1515 return true;
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001516 }
1517
Evan Chenge9c46c22010-03-03 01:44:33 +00001518 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001519}
1520
Bill Wendlingf4707472010-06-23 23:00:16 +00001521/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1522/// determine if two loads are loading from the same base address. It should
1523/// only return true if the base pointers are the same and the only differences
1524/// between the two addresses is the offset. It also returns the offsets by
1525/// reference.
Andrew Tricka7714a02012-11-12 19:40:10 +00001526///
1527/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1528/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001529bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1530 int64_t &Offset1,
1531 int64_t &Offset2) const {
1532 // Don't worry about Thumb: just ARM and Thumb2.
1533 if (Subtarget.isThumb1Only()) return false;
1534
1535 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1536 return false;
1537
1538 switch (Load1->getMachineOpcode()) {
1539 default:
1540 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001541 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001542 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001543 case ARM::LDRD:
1544 case ARM::LDRH:
1545 case ARM::LDRSB:
1546 case ARM::LDRSH:
1547 case ARM::VLDRD:
1548 case ARM::VLDRS:
1549 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001550 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001551 case ARM::t2LDRDi8:
1552 case ARM::t2LDRSHi8:
1553 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001554 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001555 case ARM::t2LDRSHi12:
1556 break;
1557 }
1558
1559 switch (Load2->getMachineOpcode()) {
1560 default:
1561 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001562 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001563 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001564 case ARM::LDRD:
1565 case ARM::LDRH:
1566 case ARM::LDRSB:
1567 case ARM::LDRSH:
1568 case ARM::VLDRD:
1569 case ARM::VLDRS:
1570 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001571 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001572 case ARM::t2LDRSHi8:
1573 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001574 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001575 case ARM::t2LDRSHi12:
1576 break;
1577 }
1578
1579 // Check if base addresses and chain operands match.
1580 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1581 Load1->getOperand(4) != Load2->getOperand(4))
1582 return false;
1583
1584 // Index should be Reg0.
1585 if (Load1->getOperand(3) != Load2->getOperand(3))
1586 return false;
1587
1588 // Determine the offsets.
1589 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1590 isa<ConstantSDNode>(Load2->getOperand(1))) {
1591 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1592 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1593 return true;
1594 }
1595
1596 return false;
1597}
1598
1599/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001600/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Bill Wendlingf4707472010-06-23 23:00:16 +00001601/// be scheduled togther. On some targets if two loads are loading from
1602/// addresses in the same cache line, it's better if they are scheduled
1603/// together. This function takes two integers that represent the load offsets
1604/// from the common base address. It returns true if it decides it's desirable
1605/// to schedule the two loads together. "NumLoads" is the number of loads that
1606/// have already been scheduled after Load1.
Andrew Tricka7714a02012-11-12 19:40:10 +00001607///
1608/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1609/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001610bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1611 int64_t Offset1, int64_t Offset2,
1612 unsigned NumLoads) const {
1613 // Don't worry about Thumb: just ARM and Thumb2.
1614 if (Subtarget.isThumb1Only()) return false;
1615
1616 assert(Offset2 > Offset1);
1617
1618 if ((Offset2 - Offset1) / 8 > 64)
1619 return false;
1620
Renato Golinb184cd92013-08-14 16:35:29 +00001621 // Check if the machine opcodes are different. If they are different
1622 // then we consider them to not be of the same base address,
1623 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1624 // In this case, they are considered to be the same because they are different
1625 // encoding forms of the same basic instruction.
1626 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1627 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1628 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1629 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1630 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
Bill Wendlingf4707472010-06-23 23:00:16 +00001631 return false; // FIXME: overly conservative?
1632
1633 // Four loads in a row should be sufficient.
1634 if (NumLoads >= 3)
1635 return false;
1636
1637 return true;
1638}
1639
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001640bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1641 const MachineBasicBlock *MBB,
1642 const MachineFunction &MF) const {
Jim Grosbachba3ece62010-06-25 18:43:14 +00001643 // Debug info is never a scheduling boundary. It's necessary to be explicit
1644 // due to the special treatment of IT instructions below, otherwise a
1645 // dbg_value followed by an IT will result in the IT instruction being
1646 // considered a scheduling hazard, which is wrong. It should be the actual
1647 // instruction preceding the dbg_value instruction(s), just like it is
1648 // when debug info is not present.
1649 if (MI->isDebugValue())
1650 return false;
1651
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001652 // Terminators and labels can't be scheduled around.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001653 if (MI->isTerminator() || MI->isPosition())
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001654 return true;
1655
1656 // Treat the start of the IT block as a scheduling boundary, but schedule
1657 // t2IT along with all instructions following it.
1658 // FIXME: This is a big hammer. But the alternative is to add all potential
1659 // true and anti dependencies to IT block instructions as implicit operands
1660 // to the t2IT instruction. The added compile time and complexity does not
1661 // seem worth it.
1662 MachineBasicBlock::const_iterator I = MI;
Jim Grosbachba3ece62010-06-25 18:43:14 +00001663 // Make sure to skip any dbg_value instructions
1664 while (++I != MBB->end() && I->isDebugValue())
1665 ;
1666 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001667 return true;
1668
1669 // Don't attempt to schedule around any instruction that defines
1670 // a stack-oriented pointer, as it's unlikely to be profitable. This
1671 // saves compile time, because it doesn't require every single
1672 // stack slot reference to depend on the instruction that does the
1673 // modification.
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001674 // Calls don't actually change the stack pointer, even if they have imp-defs.
Jakob Stoklund Olesen5f37f1c2012-02-22 01:07:19 +00001675 // No ARM calling conventions change the stack pointer. (X86 calling
1676 // conventions sometimes do).
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001677 if (!MI->isCall() && MI->definesRegister(ARM::SP))
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001678 return true;
1679
1680 return false;
1681}
1682
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001683bool ARMBaseInstrInfo::
1684isProfitableToIfCvt(MachineBasicBlock &MBB,
1685 unsigned NumCycles, unsigned ExtraPredCycles,
1686 const BranchProbability &Probability) const {
Cameron Zwarich80018502011-04-13 06:39:16 +00001687 if (!NumCycles)
Evan Cheng02b184d2010-06-25 22:42:03 +00001688 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001689
Peter Collingbourne65295232015-04-23 20:31:30 +00001690 // If we are optimizing for size, see if the branch in the predecessor can be
1691 // lowered to cbn?z by the constant island lowering pass, and return false if
1692 // so. This results in a shorter instruction sequence.
1693 const Function *F = MBB.getParent()->getFunction();
1694 if (F->hasFnAttribute(Attribute::OptimizeForSize) ||
1695 F->hasFnAttribute(Attribute::MinSize)) {
1696 MachineBasicBlock *Pred = *MBB.pred_begin();
1697 if (!Pred->empty()) {
1698 MachineInstr *LastMI = &*Pred->rbegin();
1699 if (LastMI->getOpcode() == ARM::t2Bcc) {
1700 MachineBasicBlock::iterator CmpMI = LastMI;
1701 if (CmpMI != Pred->begin()) {
1702 --CmpMI;
1703 if (CmpMI->getOpcode() == ARM::tCMPi8 ||
1704 CmpMI->getOpcode() == ARM::t2CMPri) {
1705 unsigned Reg = CmpMI->getOperand(0).getReg();
1706 unsigned PredReg = 0;
1707 ARMCC::CondCodes P = getInstrPredicate(CmpMI, PredReg);
1708 if (P == ARMCC::AL && CmpMI->getOperand(1).getImm() == 0 &&
1709 isARMLowRegister(Reg))
1710 return false;
1711 }
1712 }
1713 }
1714 }
1715 }
1716
Owen Anderson88af7d02010-09-28 18:32:13 +00001717 // Attempt to estimate the relative costs of predication versus branching.
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001718 unsigned UnpredCost = Probability.getNumerator() * NumCycles;
1719 UnpredCost /= Probability.getDenominator();
1720 UnpredCost += 1; // The branch itself
1721 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001722
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001723 return (NumCycles + ExtraPredCycles) <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001724}
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001725
Evan Cheng02b184d2010-06-25 22:42:03 +00001726bool ARMBaseInstrInfo::
Evan Chengdebf9c52010-11-03 00:45:17 +00001727isProfitableToIfCvt(MachineBasicBlock &TMBB,
1728 unsigned TCycles, unsigned TExtra,
1729 MachineBasicBlock &FMBB,
1730 unsigned FCycles, unsigned FExtra,
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001731 const BranchProbability &Probability) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00001732 if (!TCycles || !FCycles)
Owen Anderson88af7d02010-09-28 18:32:13 +00001733 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001734
Owen Anderson88af7d02010-09-28 18:32:13 +00001735 // Attempt to estimate the relative costs of predication versus branching.
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001736 unsigned TUnpredCost = Probability.getNumerator() * TCycles;
1737 TUnpredCost /= Probability.getDenominator();
Andrew Trick3f1fdf12011-09-21 02:17:37 +00001738
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001739 uint32_t Comp = Probability.getDenominator() - Probability.getNumerator();
1740 unsigned FUnpredCost = Comp * FCycles;
1741 FUnpredCost /= Probability.getDenominator();
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001742
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001743 unsigned UnpredCost = TUnpredCost + FUnpredCost;
1744 UnpredCost += 1; // The branch itself
1745 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
1746
1747 return (TCycles + FCycles + TExtra + FExtra) <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001748}
1749
Bob Wilsone8a549c2012-09-29 21:43:49 +00001750bool
1751ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1752 MachineBasicBlock &FMBB) const {
1753 // Reduce false anti-dependencies to let Swift's out-of-order execution
1754 // engine do its thing.
1755 return Subtarget.isSwift();
1756}
1757
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001758/// getInstrPredicate - If instruction is predicated, returns its predicate
1759/// condition, otherwise returns AL. It also returns the condition code
1760/// register by reference.
Evan Cheng83e0d482009-09-28 09:14:39 +00001761ARMCC::CondCodes
1762llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001763 int PIdx = MI->findFirstPredOperandIdx();
1764 if (PIdx == -1) {
1765 PredReg = 0;
1766 return ARMCC::AL;
1767 }
1768
1769 PredReg = MI->getOperand(PIdx+1).getReg();
1770 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1771}
1772
1773
Evan Cheng780748d2009-07-28 05:48:47 +00001774int llvm::getMatchingCondBranchOpcode(int Opc) {
Evan Cheng056c6692009-07-27 18:20:05 +00001775 if (Opc == ARM::B)
1776 return ARM::Bcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001777 if (Opc == ARM::tB)
Evan Cheng056c6692009-07-27 18:20:05 +00001778 return ARM::tBcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001779 if (Opc == ARM::t2B)
1780 return ARM::t2Bcc;
Evan Cheng056c6692009-07-27 18:20:05 +00001781
1782 llvm_unreachable("Unknown unconditional branch opcode!");
Evan Cheng056c6692009-07-27 18:20:05 +00001783}
1784
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001785/// commuteInstruction - Handle commutable instructions.
1786MachineInstr *
1787ARMBaseInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
1788 switch (MI->getOpcode()) {
1789 case ARM::MOVCCr:
1790 case ARM::t2MOVCCr: {
1791 // MOVCC can be commuted by inverting the condition.
1792 unsigned PredReg = 0;
1793 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
1794 // MOVCC AL can't be inverted. Shouldn't happen.
1795 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
Craig Topper062a2ba2014-04-25 05:30:21 +00001796 return nullptr;
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001797 MI = TargetInstrInfo::commuteInstruction(MI, NewMI);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001798 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001799 return nullptr;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001800 // After swapping the MOVCC operands, also invert the condition.
1801 MI->getOperand(MI->findFirstPredOperandIdx())
1802 .setImm(ARMCC::getOppositeCondition(CC));
1803 return MI;
1804 }
1805 }
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001806 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001807}
Evan Cheng780748d2009-07-28 05:48:47 +00001808
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001809/// Identify instructions that can be folded into a MOVCC instruction, and
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001810/// return the defining instruction.
1811static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
1812 const MachineRegisterInfo &MRI,
1813 const TargetInstrInfo *TII) {
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001814 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001815 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001816 if (!MRI.hasOneNonDBGUse(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001817 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001818 MachineInstr *MI = MRI.getVRegDef(Reg);
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001819 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001820 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001821 // MI is folded into the MOVCC by predicating it.
1822 if (!MI->isPredicable())
Craig Topper062a2ba2014-04-25 05:30:21 +00001823 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001824 // Check if MI has any non-dead defs or physreg uses. This also detects
1825 // predicated instructions which will be reading CPSR.
1826 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
1827 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen7b1a2e82012-08-17 20:55:34 +00001828 // Reject frame index operands, PEI can't handle the predicated pseudos.
1829 if (MO.isFI() || MO.isCPI() || MO.isJTI())
Craig Topper062a2ba2014-04-25 05:30:21 +00001830 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001831 if (!MO.isReg())
1832 continue;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001833 // MI can't have any tied operands, that would conflict with predication.
1834 if (MO.isTied())
Craig Topper062a2ba2014-04-25 05:30:21 +00001835 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001836 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Craig Topper062a2ba2014-04-25 05:30:21 +00001837 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001838 if (MO.isDef() && !MO.isDead())
Craig Topper062a2ba2014-04-25 05:30:21 +00001839 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001840 }
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001841 bool DontMoveAcrossStores = true;
Craig Topper062a2ba2014-04-25 05:30:21 +00001842 if (!MI->isSafeToMove(TII, /* AliasAnalysis = */ nullptr,
1843 DontMoveAcrossStores))
1844 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001845 return MI;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001846}
1847
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001848bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr *MI,
1849 SmallVectorImpl<MachineOperand> &Cond,
1850 unsigned &TrueOp, unsigned &FalseOp,
1851 bool &Optimizable) const {
1852 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1853 "Unknown select instruction");
1854 // MOVCC operands:
1855 // 0: Def.
1856 // 1: True use.
1857 // 2: False use.
1858 // 3: Condition code.
1859 // 4: CPSR use.
1860 TrueOp = 1;
1861 FalseOp = 2;
1862 Cond.push_back(MI->getOperand(3));
1863 Cond.push_back(MI->getOperand(4));
1864 // We can always fold a def.
1865 Optimizable = true;
1866 return false;
1867}
1868
Mehdi Amini22e59742015-01-13 07:07:13 +00001869MachineInstr *
1870ARMBaseInstrInfo::optimizeSelect(MachineInstr *MI,
1871 SmallPtrSetImpl<MachineInstr *> &SeenMIs,
1872 bool PreferFalse) const {
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001873 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1874 "Unknown select instruction");
Matthias Braun2f169f92013-10-04 16:52:56 +00001875 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001876 MachineInstr *DefMI = canFoldIntoMOVCC(MI->getOperand(2).getReg(), MRI, this);
1877 bool Invert = !DefMI;
1878 if (!DefMI)
1879 DefMI = canFoldIntoMOVCC(MI->getOperand(1).getReg(), MRI, this);
1880 if (!DefMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001881 return nullptr;
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001882
Matthias Braun2f169f92013-10-04 16:52:56 +00001883 // Find new register class to use.
1884 MachineOperand FalseReg = MI->getOperand(Invert ? 2 : 1);
1885 unsigned DestReg = MI->getOperand(0).getReg();
1886 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
1887 if (!MRI.constrainRegClass(DestReg, PreviousClass))
Craig Topper062a2ba2014-04-25 05:30:21 +00001888 return nullptr;
Matthias Braun2f169f92013-10-04 16:52:56 +00001889
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001890 // Create a new predicated version of DefMI.
1891 // Rfalse is the first use.
1892 MachineInstrBuilder NewMI = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
Matthias Braun2f169f92013-10-04 16:52:56 +00001893 DefMI->getDesc(), DestReg);
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001894
1895 // Copy all the DefMI operands, excluding its (null) predicate.
1896 const MCInstrDesc &DefDesc = DefMI->getDesc();
1897 for (unsigned i = 1, e = DefDesc.getNumOperands();
1898 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
1899 NewMI.addOperand(DefMI->getOperand(i));
1900
1901 unsigned CondCode = MI->getOperand(3).getImm();
1902 if (Invert)
1903 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
1904 else
1905 NewMI.addImm(CondCode);
1906 NewMI.addOperand(MI->getOperand(4));
1907
1908 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
1909 if (NewMI->hasOptionalDef())
1910 AddDefaultCC(NewMI);
1911
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001912 // The output register value when the predicate is false is an implicit
1913 // register operand tied to the first def.
1914 // The tie makes the register allocator ensure the FalseReg is allocated the
1915 // same register as operand 0.
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001916 FalseReg.setImplicit();
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +00001917 NewMI.addOperand(FalseReg);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001918 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
1919
Mehdi Amini22e59742015-01-13 07:07:13 +00001920 // Update SeenMIs set: register newly created MI and erase removed DefMI.
1921 SeenMIs.insert(NewMI);
1922 SeenMIs.erase(DefMI);
1923
Pete Cooper2127b002015-04-30 23:57:47 +00001924 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
1925 // DefMI would be invalid when tranferred inside the loop. Checking for a
1926 // loop is expensive, but at least remove kill flags if they are in different
1927 // BBs.
1928 if (DefMI->getParent() != MI->getParent())
1929 NewMI->clearKillInfo();
1930
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001931 // The caller will erase MI, but not DefMI.
1932 DefMI->eraseFromParent();
1933 return NewMI;
1934}
1935
Andrew Trick924123a2011-09-21 02:20:46 +00001936/// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
1937/// instruction is encoded with an 'S' bit is determined by the optional CPSR
1938/// def operand.
1939///
1940/// This will go away once we can teach tblgen how to set the optional CPSR def
1941/// operand itself.
1942struct AddSubFlagsOpcodePair {
Craig Topper2fbd1302012-05-24 03:59:11 +00001943 uint16_t PseudoOpc;
1944 uint16_t MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001945};
1946
Craig Topper2fbd1302012-05-24 03:59:11 +00001947static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
Andrew Trick924123a2011-09-21 02:20:46 +00001948 {ARM::ADDSri, ARM::ADDri},
1949 {ARM::ADDSrr, ARM::ADDrr},
1950 {ARM::ADDSrsi, ARM::ADDrsi},
1951 {ARM::ADDSrsr, ARM::ADDrsr},
1952
1953 {ARM::SUBSri, ARM::SUBri},
1954 {ARM::SUBSrr, ARM::SUBrr},
1955 {ARM::SUBSrsi, ARM::SUBrsi},
1956 {ARM::SUBSrsr, ARM::SUBrsr},
1957
1958 {ARM::RSBSri, ARM::RSBri},
Andrew Trick924123a2011-09-21 02:20:46 +00001959 {ARM::RSBSrsi, ARM::RSBrsi},
1960 {ARM::RSBSrsr, ARM::RSBrsr},
1961
1962 {ARM::t2ADDSri, ARM::t2ADDri},
1963 {ARM::t2ADDSrr, ARM::t2ADDrr},
1964 {ARM::t2ADDSrs, ARM::t2ADDrs},
1965
1966 {ARM::t2SUBSri, ARM::t2SUBri},
1967 {ARM::t2SUBSrr, ARM::t2SUBrr},
1968 {ARM::t2SUBSrs, ARM::t2SUBrs},
1969
1970 {ARM::t2RSBSri, ARM::t2RSBri},
1971 {ARM::t2RSBSrs, ARM::t2RSBrs},
1972};
1973
1974unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
Craig Topper2fbd1302012-05-24 03:59:11 +00001975 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
1976 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
1977 return AddSubFlagsOpcodeMap[i].MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001978 return 0;
1979}
1980
Evan Cheng780748d2009-07-28 05:48:47 +00001981void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1982 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1983 unsigned DestReg, unsigned BaseReg, int NumBytes,
1984 ARMCC::CondCodes Pred, unsigned PredReg,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001985 const ARMBaseInstrInfo &TII, unsigned MIFlags) {
Tim Northoverc9432eb2013-11-04 23:04:15 +00001986 if (NumBytes == 0 && DestReg != BaseReg) {
1987 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
1988 .addReg(BaseReg, RegState::Kill)
1989 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1990 .setMIFlags(MIFlags);
1991 return;
1992 }
1993
Evan Cheng780748d2009-07-28 05:48:47 +00001994 bool isSub = NumBytes < 0;
1995 if (isSub) NumBytes = -NumBytes;
1996
1997 while (NumBytes) {
1998 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1999 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
2000 assert(ThisVal && "Didn't extract field correctly");
2001
2002 // We will handle these bits from offset, clear them.
2003 NumBytes &= ~ThisVal;
2004
2005 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
2006
2007 // Build the new ADD / SUB.
2008 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
2009 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
2010 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00002011 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
2012 .setMIFlags(MIFlags);
Evan Cheng780748d2009-07-28 05:48:47 +00002013 BaseReg = DestReg;
2014 }
2015}
2016
Weiming Zhao01524852014-03-20 23:28:16 +00002017static bool isAnySubRegLive(unsigned Reg, const TargetRegisterInfo *TRI,
2018 MachineInstr *MI) {
2019 for (MCSubRegIterator Subreg(Reg, TRI, /* IncludeSelf */ true);
2020 Subreg.isValid(); ++Subreg)
2021 if (MI->getParent()->computeRegisterLiveness(TRI, *Subreg, MI) !=
2022 MachineBasicBlock::LQR_Dead)
2023 return true;
2024 return false;
2025}
Tim Northoverdee86042013-12-02 14:46:26 +00002026bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
2027 MachineFunction &MF, MachineInstr *MI,
Tim Northover93bcc662013-11-08 17:18:07 +00002028 unsigned NumBytes) {
2029 // This optimisation potentially adds lots of load and store
2030 // micro-operations, it's only really a great benefit to code-size.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00002031 if (!MF.getFunction()->hasFnAttribute(Attribute::MinSize))
Tim Northover93bcc662013-11-08 17:18:07 +00002032 return false;
2033
2034 // If only one register is pushed/popped, LLVM can use an LDR/STR
2035 // instead. We can't modify those so make sure we're dealing with an
2036 // instruction we understand.
2037 bool IsPop = isPopOpcode(MI->getOpcode());
2038 bool IsPush = isPushOpcode(MI->getOpcode());
2039 if (!IsPush && !IsPop)
2040 return false;
2041
2042 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
2043 MI->getOpcode() == ARM::VLDMDIA_UPD;
2044 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
2045 MI->getOpcode() == ARM::tPOP ||
2046 MI->getOpcode() == ARM::tPOP_RET;
2047
2048 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
2049 MI->getOperand(1).getReg() == ARM::SP)) &&
2050 "trying to fold sp update into non-sp-updating push/pop");
2051
2052 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2053 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2054 // if this is violated.
2055 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
2056 return false;
2057
2058 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2059 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2060 int RegListIdx = IsT1PushPop ? 2 : 4;
2061
2062 // Calculate the space we'll need in terms of registers.
2063 unsigned FirstReg = MI->getOperand(RegListIdx).getReg();
2064 unsigned RD0Reg, RegsNeeded;
2065 if (IsVFPPushPop) {
2066 RD0Reg = ARM::D0;
2067 RegsNeeded = NumBytes / 8;
2068 } else {
2069 RD0Reg = ARM::R0;
2070 RegsNeeded = NumBytes / 4;
2071 }
2072
2073 // We're going to have to strip all list operands off before
2074 // re-adding them since the order matters, so save the existing ones
2075 // for later.
2076 SmallVector<MachineOperand, 4> RegList;
2077 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2078 RegList.push_back(MI->getOperand(i));
2079
Tim Northover93bcc662013-11-08 17:18:07 +00002080 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
Tim Northover45479dc2013-12-01 14:16:24 +00002081 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Tim Northover93bcc662013-11-08 17:18:07 +00002082
2083 // Now try to find enough space in the reglist to allocate NumBytes.
2084 for (unsigned CurReg = FirstReg - 1; CurReg >= RD0Reg && RegsNeeded;
Tim Northover45479dc2013-12-01 14:16:24 +00002085 --CurReg) {
Tim Northover93bcc662013-11-08 17:18:07 +00002086 if (!IsPop) {
2087 // Pushing any register is completely harmless, mark the
2088 // register involved as undef since we don't care about it in
2089 // the slightest.
2090 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2091 false, false, true));
Tim Northover45479dc2013-12-01 14:16:24 +00002092 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002093 continue;
2094 }
2095
Tim Northover45479dc2013-12-01 14:16:24 +00002096 // However, we can only pop an extra register if it's not live. For
2097 // registers live within the function we might clobber a return value
2098 // register; the other way a register can be live here is if it's
2099 // callee-saved.
Weiming Zhao01524852014-03-20 23:28:16 +00002100 // TODO: Currently, computeRegisterLiveness() does not report "live" if a
2101 // sub reg is live. When computeRegisterLiveness() works for sub reg, it
2102 // can replace isAnySubRegLive().
Tim Northover45479dc2013-12-01 14:16:24 +00002103 if (isCalleeSavedRegister(CurReg, CSRegs) ||
Weiming Zhao01524852014-03-20 23:28:16 +00002104 isAnySubRegLive(CurReg, TRI, MI)) {
Tim Northover45479dc2013-12-01 14:16:24 +00002105 // VFP pops don't allow holes in the register list, so any skip is fatal
2106 // for our transformation. GPR pops do, so we should just keep looking.
2107 if (IsVFPPushPop)
2108 return false;
2109 else
2110 continue;
2111 }
Tim Northover93bcc662013-11-08 17:18:07 +00002112
2113 // Mark the unimportant registers as <def,dead> in the POP.
Lang Hames1ca11232013-11-22 00:46:32 +00002114 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2115 true));
Tim Northover45479dc2013-12-01 14:16:24 +00002116 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002117 }
2118
2119 if (RegsNeeded > 0)
2120 return false;
2121
2122 // Finally we know we can profitably perform the optimisation so go
2123 // ahead: strip all existing registers off and add them back again
2124 // in the right order.
2125 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2126 MI->RemoveOperand(i);
2127
2128 // Add the complete list back in.
2129 MachineInstrBuilder MIB(MF, &*MI);
2130 for (int i = RegList.size() - 1; i >= 0; --i)
2131 MIB.addOperand(RegList[i]);
2132
2133 return true;
2134}
2135
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002136bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2137 unsigned FrameReg, int &Offset,
2138 const ARMBaseInstrInfo &TII) {
Evan Cheng780748d2009-07-28 05:48:47 +00002139 unsigned Opcode = MI.getOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002140 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng780748d2009-07-28 05:48:47 +00002141 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2142 bool isSub = false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002143
Evan Cheng780748d2009-07-28 05:48:47 +00002144 // Memory operands in inline assembly always use AddrMode2.
2145 if (Opcode == ARM::INLINEASM)
2146 AddrMode = ARMII::AddrMode2;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002147
Evan Cheng780748d2009-07-28 05:48:47 +00002148 if (Opcode == ARM::ADDri) {
2149 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2150 if (Offset == 0) {
2151 // Turn it into a move.
2152 MI.setDesc(TII.get(ARM::MOVr));
2153 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2154 MI.RemoveOperand(FrameRegIdx+1);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002155 Offset = 0;
2156 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002157 } else if (Offset < 0) {
2158 Offset = -Offset;
2159 isSub = true;
2160 MI.setDesc(TII.get(ARM::SUBri));
2161 }
2162
2163 // Common case: small offset, fits into instruction.
2164 if (ARM_AM::getSOImmVal(Offset) != -1) {
2165 // Replace the FrameIndex with sp / fp
2166 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2167 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002168 Offset = 0;
2169 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002170 }
2171
2172 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2173 // as possible.
2174 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2175 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2176
2177 // We will handle these bits from offset, clear them.
2178 Offset &= ~ThisImmVal;
2179
2180 // Get the properly encoded SOImmVal field.
2181 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2182 "Bit extraction didn't work?");
2183 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2184 } else {
2185 unsigned ImmIdx = 0;
2186 int InstrOffs = 0;
2187 unsigned NumBits = 0;
2188 unsigned Scale = 1;
2189 switch (AddrMode) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002190 case ARMII::AddrMode_i12: {
2191 ImmIdx = FrameRegIdx + 1;
2192 InstrOffs = MI.getOperand(ImmIdx).getImm();
2193 NumBits = 12;
2194 break;
2195 }
Evan Cheng780748d2009-07-28 05:48:47 +00002196 case ARMII::AddrMode2: {
2197 ImmIdx = FrameRegIdx+2;
2198 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2199 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2200 InstrOffs *= -1;
2201 NumBits = 12;
2202 break;
2203 }
2204 case ARMII::AddrMode3: {
2205 ImmIdx = FrameRegIdx+2;
2206 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2207 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2208 InstrOffs *= -1;
2209 NumBits = 8;
2210 break;
2211 }
Anton Korobeynikov887d05c2009-08-08 13:35:48 +00002212 case ARMII::AddrMode4:
Jim Grosbach01c1cae2009-11-15 21:45:34 +00002213 case ARMII::AddrMode6:
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002214 // Can't fold any offset even if it's zero.
2215 return false;
Evan Cheng780748d2009-07-28 05:48:47 +00002216 case ARMII::AddrMode5: {
2217 ImmIdx = FrameRegIdx+1;
2218 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2219 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2220 InstrOffs *= -1;
2221 NumBits = 8;
2222 Scale = 4;
2223 break;
2224 }
2225 default:
2226 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng780748d2009-07-28 05:48:47 +00002227 }
2228
2229 Offset += InstrOffs * Scale;
2230 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2231 if (Offset < 0) {
2232 Offset = -Offset;
2233 isSub = true;
2234 }
2235
2236 // Attempt to fold address comp. if opcode has offset bits
2237 if (NumBits > 0) {
2238 // Common case: small offset, fits into instruction.
2239 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2240 int ImmedOffset = Offset / Scale;
2241 unsigned Mask = (1 << NumBits) - 1;
2242 if ((unsigned)Offset <= Mask * Scale) {
2243 // Replace the FrameIndex with sp
2244 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach9d2d1f02010-10-27 01:19:41 +00002245 // FIXME: When addrmode2 goes away, this will simplify (like the
2246 // T2 version), as the LDR.i12 versions don't need the encoding
2247 // tricks for the offset value.
2248 if (isSub) {
2249 if (AddrMode == ARMII::AddrMode_i12)
2250 ImmedOffset = -ImmedOffset;
2251 else
2252 ImmedOffset |= 1 << NumBits;
2253 }
Evan Cheng780748d2009-07-28 05:48:47 +00002254 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002255 Offset = 0;
2256 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002257 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002258
Evan Cheng780748d2009-07-28 05:48:47 +00002259 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2260 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach8bf14832010-10-27 16:50:31 +00002261 if (isSub) {
2262 if (AddrMode == ARMII::AddrMode_i12)
2263 ImmedOffset = -ImmedOffset;
2264 else
2265 ImmedOffset |= 1 << NumBits;
2266 }
Evan Cheng780748d2009-07-28 05:48:47 +00002267 ImmOp.ChangeToImmediate(ImmedOffset);
2268 Offset &= ~(Mask*Scale);
2269 }
2270 }
2271
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002272 Offset = (isSub) ? -Offset : Offset;
2273 return Offset == 0;
Evan Cheng780748d2009-07-28 05:48:47 +00002274}
Bill Wendling7de9d522010-08-06 01:32:48 +00002275
Manman Ren6fa76dc2012-06-29 21:33:59 +00002276/// analyzeCompare - For a comparison instruction, return the source registers
2277/// in SrcReg and SrcReg2 if having two register operands, and the value it
2278/// compares against in CmpValue. Return true if the comparison instruction
2279/// can be analyzed.
Bill Wendling7de9d522010-08-06 01:32:48 +00002280bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002281analyzeCompare(const MachineInstr *MI, unsigned &SrcReg, unsigned &SrcReg2,
2282 int &CmpMask, int &CmpValue) const {
Bill Wendling7de9d522010-08-06 01:32:48 +00002283 switch (MI->getOpcode()) {
2284 default: break;
Bill Wendling79553ba2010-08-11 00:23:00 +00002285 case ARM::CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002286 case ARM::t2CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002287 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002288 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002289 CmpMask = ~0;
Bill Wendling7de9d522010-08-06 01:32:48 +00002290 CmpValue = MI->getOperand(1).getImm();
2291 return true;
Manman Rendc8ad002012-05-11 01:30:47 +00002292 case ARM::CMPrr:
2293 case ARM::t2CMPrr:
2294 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002295 SrcReg2 = MI->getOperand(1).getReg();
Manman Rendc8ad002012-05-11 01:30:47 +00002296 CmpMask = ~0;
2297 CmpValue = 0;
2298 return true;
Gabor Greifadbbb932010-09-21 12:01:15 +00002299 case ARM::TSTri:
2300 case ARM::t2TSTri:
2301 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002302 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002303 CmpMask = MI->getOperand(1).getImm();
2304 CmpValue = 0;
2305 return true;
2306 }
2307
2308 return false;
2309}
2310
Gabor Greifd36e3e82010-09-29 10:12:08 +00002311/// isSuitableForMask - Identify a suitable 'and' instruction that
2312/// operates on the given source register and applies the same mask
2313/// as a 'tst' instruction. Provide a limited look-through for copies.
2314/// When successful, MI will hold the found instruction.
2315static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif1a25ae82010-09-21 13:30:57 +00002316 int CmpMask, bool CommonUse) {
Gabor Greifd36e3e82010-09-29 10:12:08 +00002317 switch (MI->getOpcode()) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002318 case ARM::ANDri:
2319 case ARM::t2ANDri:
Gabor Greifd36e3e82010-09-29 10:12:08 +00002320 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif1a25ae82010-09-21 13:30:57 +00002321 return false;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002322 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greifadbbb932010-09-21 12:01:15 +00002323 return true;
2324 break;
Bill Wendling7de9d522010-08-06 01:32:48 +00002325 }
2326
2327 return false;
2328}
2329
Manman Renb1b3db62012-06-29 22:06:19 +00002330/// getSwappedCondition - assume the flags are set by MI(a,b), return
2331/// the condition code if we modify the instructions such that flags are
2332/// set by MI(b,a).
2333inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2334 switch (CC) {
2335 default: return ARMCC::AL;
2336 case ARMCC::EQ: return ARMCC::EQ;
2337 case ARMCC::NE: return ARMCC::NE;
2338 case ARMCC::HS: return ARMCC::LS;
2339 case ARMCC::LO: return ARMCC::HI;
2340 case ARMCC::HI: return ARMCC::LO;
2341 case ARMCC::LS: return ARMCC::HS;
2342 case ARMCC::GE: return ARMCC::LE;
2343 case ARMCC::LT: return ARMCC::GT;
2344 case ARMCC::GT: return ARMCC::LT;
2345 case ARMCC::LE: return ARMCC::GE;
2346 }
2347}
2348
2349/// isRedundantFlagInstr - check whether the first instruction, whose only
2350/// purpose is to update flags, can be made redundant.
2351/// CMPrr can be made redundant by SUBrr if the operands are the same.
2352/// CMPri can be made redundant by SUBri if the operands are the same.
2353/// This function can be extended later on.
2354inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2355 unsigned SrcReg2, int ImmValue,
2356 MachineInstr *OI) {
2357 if ((CmpI->getOpcode() == ARM::CMPrr ||
2358 CmpI->getOpcode() == ARM::t2CMPrr) &&
2359 (OI->getOpcode() == ARM::SUBrr ||
2360 OI->getOpcode() == ARM::t2SUBrr) &&
2361 ((OI->getOperand(1).getReg() == SrcReg &&
2362 OI->getOperand(2).getReg() == SrcReg2) ||
2363 (OI->getOperand(1).getReg() == SrcReg2 &&
2364 OI->getOperand(2).getReg() == SrcReg)))
2365 return true;
2366
2367 if ((CmpI->getOpcode() == ARM::CMPri ||
2368 CmpI->getOpcode() == ARM::t2CMPri) &&
2369 (OI->getOpcode() == ARM::SUBri ||
2370 OI->getOpcode() == ARM::t2SUBri) &&
2371 OI->getOperand(1).getReg() == SrcReg &&
2372 OI->getOperand(2).getImm() == ImmValue)
2373 return true;
2374 return false;
2375}
2376
Manman Ren6fa76dc2012-06-29 21:33:59 +00002377/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2378/// comparison into one that sets the zero bit in the flags register;
2379/// Remove a redundant Compare instruction if an earlier instruction can set the
2380/// flags in the same way as Compare.
2381/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2382/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2383/// condition code of instructions which use the flags.
Bill Wendling7de9d522010-08-06 01:32:48 +00002384bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002385optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, unsigned SrcReg2,
2386 int CmpMask, int CmpValue,
2387 const MachineRegisterInfo *MRI) const {
Manman Renb1b3db62012-06-29 22:06:19 +00002388 // Get the unique definition of SrcReg.
2389 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2390 if (!MI) return false;
Bill Wendling04123002010-09-10 23:34:19 +00002391
Gabor Greifadbbb932010-09-21 12:01:15 +00002392 // Masked compares sometimes use the same register as the corresponding 'and'.
2393 if (CmpMask != ~0) {
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002394 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(MI)) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002395 MI = nullptr;
Owen Anderson16c6bf42014-03-13 23:12:04 +00002396 for (MachineRegisterInfo::use_instr_iterator
2397 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2398 UI != UE; ++UI) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002399 if (UI->getParent() != CmpInstr->getParent()) continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002400 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002401 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
2402 isPredicated(PotentialAND))
Gabor Greifadbbb932010-09-21 12:01:15 +00002403 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002404 MI = PotentialAND;
Gabor Greifadbbb932010-09-21 12:01:15 +00002405 break;
2406 }
2407 if (!MI) return false;
2408 }
2409 }
2410
Manman Rendc8ad002012-05-11 01:30:47 +00002411 // Get ready to iterate backward from CmpInstr.
2412 MachineBasicBlock::iterator I = CmpInstr, E = MI,
2413 B = CmpInstr->getParent()->begin();
Bill Wendling59ebe442010-10-09 00:03:48 +00002414
2415 // Early exit if CmpInstr is at the beginning of the BB.
2416 if (I == B) return false;
2417
Manman Rendc8ad002012-05-11 01:30:47 +00002418 // There are two possible candidates which can be changed to set CPSR:
2419 // One is MI, the other is a SUB instruction.
2420 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2421 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
Craig Topper062a2ba2014-04-25 05:30:21 +00002422 MachineInstr *Sub = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002423 if (SrcReg2 != 0)
Manman Rendc8ad002012-05-11 01:30:47 +00002424 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00002425 MI = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002426 else if (MI->getParent() != CmpInstr->getParent() || CmpValue != 0) {
Manman Rendc8ad002012-05-11 01:30:47 +00002427 // Conservatively refuse to convert an instruction which isn't in the same
2428 // BB as the comparison.
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002429 // For CMPri w/ CmpValue != 0, a Sub may still be a candidate.
2430 // Thus we cannot return here.
Manman Ren0d5ec282012-05-11 15:36:46 +00002431 if (CmpInstr->getOpcode() == ARM::CMPri ||
Manman Rendc8ad002012-05-11 01:30:47 +00002432 CmpInstr->getOpcode() == ARM::t2CMPri)
Craig Topper062a2ba2014-04-25 05:30:21 +00002433 MI = nullptr;
Manman Rendc8ad002012-05-11 01:30:47 +00002434 else
2435 return false;
2436 }
2437
2438 // Check that CPSR isn't set between the comparison instruction and the one we
2439 // want to change. At the same time, search for Sub.
Manman Renb1b3db62012-06-29 22:06:19 +00002440 const TargetRegisterInfo *TRI = &getRegisterInfo();
Bill Wendling7de9d522010-08-06 01:32:48 +00002441 --I;
2442 for (; I != E; --I) {
2443 const MachineInstr &Instr = *I;
2444
Manman Renb1b3db62012-06-29 22:06:19 +00002445 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2446 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendlingc6627ee2010-11-01 20:41:43 +00002447 // This instruction modifies or uses CPSR after the one we want to
2448 // change. We can't do this transformation.
Manman Renb1b3db62012-06-29 22:06:19 +00002449 return false;
Evan Chengd757c882010-09-21 23:49:07 +00002450
Manman Renb1b3db62012-06-29 22:06:19 +00002451 // Check whether CmpInstr can be made redundant by the current instruction.
2452 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
Manman Rendc8ad002012-05-11 01:30:47 +00002453 Sub = &*I;
2454 break;
2455 }
2456
Evan Chengd757c882010-09-21 23:49:07 +00002457 if (I == B)
2458 // The 'and' is below the comparison instruction.
2459 return false;
Bill Wendling7de9d522010-08-06 01:32:48 +00002460 }
2461
Manman Rendc8ad002012-05-11 01:30:47 +00002462 // Return false if no candidates exist.
2463 if (!MI && !Sub)
2464 return false;
2465
2466 // The single candidate is called MI.
2467 if (!MI) MI = Sub;
2468
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002469 // We can't use a predicated instruction - it doesn't always write the flags.
2470 if (isPredicated(MI))
2471 return false;
2472
Bill Wendling7de9d522010-08-06 01:32:48 +00002473 switch (MI->getOpcode()) {
2474 default: break;
Cameron Zwarich93eae152011-04-15 20:28:28 +00002475 case ARM::RSBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002476 case ARM::RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002477 case ARM::RSCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002478 case ARM::RSCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002479 case ARM::ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002480 case ARM::ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002481 case ARM::ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002482 case ARM::ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002483 case ARM::SUBrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002484 case ARM::SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002485 case ARM::SBCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002486 case ARM::SBCri:
2487 case ARM::t2RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002488 case ARM::t2ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002489 case ARM::t2ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002490 case ARM::t2ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002491 case ARM::t2ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002492 case ARM::t2SUBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002493 case ARM::t2SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002494 case ARM::t2SBCrr:
Cameron Zwarich0829b302011-04-15 20:45:00 +00002495 case ARM::t2SBCri:
2496 case ARM::ANDrr:
2497 case ARM::ANDri:
2498 case ARM::t2ANDrr:
Cameron Zwarich9c65e4d2011-04-15 21:24:38 +00002499 case ARM::t2ANDri:
2500 case ARM::ORRrr:
2501 case ARM::ORRri:
2502 case ARM::t2ORRrr:
2503 case ARM::t2ORRri:
2504 case ARM::EORrr:
2505 case ARM::EORri:
2506 case ARM::t2EORrr:
2507 case ARM::t2EORri: {
Manman Rendc8ad002012-05-11 01:30:47 +00002508 // Scan forward for the use of CPSR
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002509 // When checking against MI: if it's a conditional code that requires
2510 // checking of the V bit or C bit, then this is not safe to do.
Manman Ren34cb93e2012-07-11 22:51:44 +00002511 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2512 // If we are done with the basic block, we need to check whether CPSR is
2513 // live-out.
Manman Renb1b3db62012-06-29 22:06:19 +00002514 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2515 OperandsToUpdate;
Evan Cheng425489d2011-03-23 22:52:04 +00002516 bool isSafe = false;
2517 I = CmpInstr;
Manman Rendc8ad002012-05-11 01:30:47 +00002518 E = CmpInstr->getParent()->end();
Evan Cheng425489d2011-03-23 22:52:04 +00002519 while (!isSafe && ++I != E) {
2520 const MachineInstr &Instr = *I;
2521 for (unsigned IO = 0, EO = Instr.getNumOperands();
2522 !isSafe && IO != EO; ++IO) {
2523 const MachineOperand &MO = Instr.getOperand(IO);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +00002524 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2525 isSafe = true;
2526 break;
2527 }
Evan Cheng425489d2011-03-23 22:52:04 +00002528 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2529 continue;
2530 if (MO.isDef()) {
2531 isSafe = true;
2532 break;
2533 }
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002534 // Condition code is after the operand before CPSR except for VSELs.
2535 ARMCC::CondCodes CC;
2536 bool IsInstrVSel = true;
2537 switch (Instr.getOpcode()) {
2538 default:
2539 IsInstrVSel = false;
2540 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2541 break;
2542 case ARM::VSELEQD:
2543 case ARM::VSELEQS:
2544 CC = ARMCC::EQ;
2545 break;
2546 case ARM::VSELGTD:
2547 case ARM::VSELGTS:
2548 CC = ARMCC::GT;
2549 break;
2550 case ARM::VSELGED:
2551 case ARM::VSELGES:
2552 CC = ARMCC::GE;
2553 break;
2554 case ARM::VSELVSS:
2555 case ARM::VSELVSD:
2556 CC = ARMCC::VS;
2557 break;
2558 }
2559
Manman Renb1b3db62012-06-29 22:06:19 +00002560 if (Sub) {
2561 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2562 if (NewCC == ARMCC::AL)
Manman Rendc8ad002012-05-11 01:30:47 +00002563 return false;
Manman Renb1b3db62012-06-29 22:06:19 +00002564 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2565 // on CMP needs to be updated to be based on SUB.
2566 // Push the condition code operands to OperandsToUpdate.
2567 // If it is safe to remove CmpInstr, the condition code of these
2568 // operands will be modified.
2569 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002570 Sub->getOperand(2).getReg() == SrcReg) {
2571 // VSel doesn't support condition code update.
2572 if (IsInstrVSel)
2573 return false;
2574 OperandsToUpdate.push_back(
2575 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2576 }
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002577 } else {
2578 // No Sub, so this is x = <op> y, z; cmp x, 0.
Manman Rendc8ad002012-05-11 01:30:47 +00002579 switch (CC) {
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002580 case ARMCC::EQ: // Z
2581 case ARMCC::NE: // Z
2582 case ARMCC::MI: // N
2583 case ARMCC::PL: // N
2584 case ARMCC::AL: // none
Manman Ren88a0d332012-07-11 23:47:00 +00002585 // CPSR can be used multiple times, we should continue.
Manman Rendc8ad002012-05-11 01:30:47 +00002586 break;
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002587 case ARMCC::HS: // C
2588 case ARMCC::LO: // C
2589 case ARMCC::VS: // V
2590 case ARMCC::VC: // V
2591 case ARMCC::HI: // C Z
2592 case ARMCC::LS: // C Z
2593 case ARMCC::GE: // N V
2594 case ARMCC::LT: // N V
2595 case ARMCC::GT: // Z N V
2596 case ARMCC::LE: // Z N V
2597 // The instruction uses the V bit or C bit which is not safe.
Manman Rendc8ad002012-05-11 01:30:47 +00002598 return false;
2599 }
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002600 }
Evan Cheng425489d2011-03-23 22:52:04 +00002601 }
2602 }
2603
Manman Ren34cb93e2012-07-11 22:51:44 +00002604 // If CPSR is not killed nor re-defined, we should check whether it is
2605 // live-out. If it is live-out, do not optimize.
2606 if (!isSafe) {
2607 MachineBasicBlock *MBB = CmpInstr->getParent();
2608 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2609 SE = MBB->succ_end(); SI != SE; ++SI)
2610 if ((*SI)->isLiveIn(ARM::CPSR))
2611 return false;
2612 }
Evan Cheng425489d2011-03-23 22:52:04 +00002613
Evan Cheng65536472010-11-17 08:06:50 +00002614 // Toggle the optional operand to CPSR.
2615 MI->getOperand(5).setReg(ARM::CPSR);
2616 MI->getOperand(5).setIsDef(true);
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002617 assert(!isPredicated(MI) && "Can't use flags from predicated instruction");
Bill Wendling7de9d522010-08-06 01:32:48 +00002618 CmpInstr->eraseFromParent();
Manman Rendc8ad002012-05-11 01:30:47 +00002619
2620 // Modify the condition code of operands in OperandsToUpdate.
2621 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2622 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Manman Renb1b3db62012-06-29 22:06:19 +00002623 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2624 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
Bill Wendling7de9d522010-08-06 01:32:48 +00002625 return true;
2626 }
Cameron Zwarich0829b302011-04-15 20:45:00 +00002627 }
Bill Wendling7de9d522010-08-06 01:32:48 +00002628
2629 return false;
2630}
Evan Cheng367a5df2010-09-09 18:18:55 +00002631
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002632bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
2633 MachineInstr *DefMI, unsigned Reg,
2634 MachineRegisterInfo *MRI) const {
2635 // Fold large immediates into add, sub, or, xor.
2636 unsigned DefOpc = DefMI->getOpcode();
2637 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2638 return false;
2639 if (!DefMI->getOperand(1).isImm())
2640 // Could be t2MOVi32imm <ga:xx>
2641 return false;
2642
2643 if (!MRI->hasOneNonDBGUse(Reg))
2644 return false;
2645
Evan Chenga2b48d92012-03-26 23:31:00 +00002646 const MCInstrDesc &DefMCID = DefMI->getDesc();
2647 if (DefMCID.hasOptionalDef()) {
2648 unsigned NumOps = DefMCID.getNumOperands();
2649 const MachineOperand &MO = DefMI->getOperand(NumOps-1);
2650 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2651 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2652 // to delete DefMI.
2653 return false;
2654 }
2655
2656 const MCInstrDesc &UseMCID = UseMI->getDesc();
2657 if (UseMCID.hasOptionalDef()) {
2658 unsigned NumOps = UseMCID.getNumOperands();
2659 if (UseMI->getOperand(NumOps-1).getReg() == ARM::CPSR)
2660 // If the instruction sets the flag, do not attempt this optimization
2661 // since it may change the semantics of the code.
2662 return false;
2663 }
2664
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002665 unsigned UseOpc = UseMI->getOpcode();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002666 unsigned NewUseOpc = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002667 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002668 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002669 bool Commute = false;
2670 switch (UseOpc) {
2671 default: return false;
2672 case ARM::SUBrr:
2673 case ARM::ADDrr:
2674 case ARM::ORRrr:
2675 case ARM::EORrr:
2676 case ARM::t2SUBrr:
2677 case ARM::t2ADDrr:
2678 case ARM::t2ORRrr:
2679 case ARM::t2EORrr: {
2680 Commute = UseMI->getOperand(2).getReg() != Reg;
2681 switch (UseOpc) {
2682 default: break;
2683 case ARM::SUBrr: {
2684 if (Commute)
2685 return false;
2686 ImmVal = -ImmVal;
2687 NewUseOpc = ARM::SUBri;
2688 // Fallthrough
2689 }
2690 case ARM::ADDrr:
2691 case ARM::ORRrr:
2692 case ARM::EORrr: {
2693 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2694 return false;
2695 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2696 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2697 switch (UseOpc) {
2698 default: break;
2699 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
2700 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2701 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2702 }
2703 break;
2704 }
2705 case ARM::t2SUBrr: {
2706 if (Commute)
2707 return false;
2708 ImmVal = -ImmVal;
2709 NewUseOpc = ARM::t2SUBri;
2710 // Fallthrough
2711 }
2712 case ARM::t2ADDrr:
2713 case ARM::t2ORRrr:
2714 case ARM::t2EORrr: {
2715 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2716 return false;
2717 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2718 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2719 switch (UseOpc) {
2720 default: break;
2721 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
2722 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2723 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2724 }
2725 break;
2726 }
2727 }
2728 }
2729 }
2730
2731 unsigned OpIdx = Commute ? 2 : 1;
2732 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
2733 bool isKill = UseMI->getOperand(OpIdx).isKill();
2734 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
2735 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
Evan Cheng7fae11b2011-12-14 02:11:42 +00002736 UseMI, UseMI->getDebugLoc(),
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002737 get(NewUseOpc), NewReg)
2738 .addReg(Reg1, getKillRegState(isKill))
2739 .addImm(SOImmValV1)));
2740 UseMI->setDesc(get(NewUseOpc));
2741 UseMI->getOperand(1).setReg(NewReg);
2742 UseMI->getOperand(1).setIsKill();
2743 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
2744 DefMI->eraseFromParent();
2745 return true;
2746}
2747
Bob Wilsone8a549c2012-09-29 21:43:49 +00002748static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
2749 const MachineInstr *MI) {
2750 switch (MI->getOpcode()) {
2751 default: {
2752 const MCInstrDesc &Desc = MI->getDesc();
2753 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
2754 assert(UOps >= 0 && "bad # UOps");
2755 return UOps;
2756 }
2757
2758 case ARM::LDRrs:
2759 case ARM::LDRBrs:
2760 case ARM::STRrs:
2761 case ARM::STRBrs: {
2762 unsigned ShOpVal = MI->getOperand(3).getImm();
2763 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2764 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2765 if (!isSub &&
2766 (ShImm == 0 ||
2767 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2768 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2769 return 1;
2770 return 2;
2771 }
2772
2773 case ARM::LDRH:
2774 case ARM::STRH: {
2775 if (!MI->getOperand(2).getReg())
2776 return 1;
2777
2778 unsigned ShOpVal = MI->getOperand(3).getImm();
2779 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2780 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2781 if (!isSub &&
2782 (ShImm == 0 ||
2783 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2784 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2785 return 1;
2786 return 2;
2787 }
2788
2789 case ARM::LDRSB:
2790 case ARM::LDRSH:
2791 return (ARM_AM::getAM3Op(MI->getOperand(3).getImm()) == ARM_AM::sub) ? 3:2;
2792
2793 case ARM::LDRSB_POST:
2794 case ARM::LDRSH_POST: {
2795 unsigned Rt = MI->getOperand(0).getReg();
2796 unsigned Rm = MI->getOperand(3).getReg();
2797 return (Rt == Rm) ? 4 : 3;
2798 }
2799
2800 case ARM::LDR_PRE_REG:
2801 case ARM::LDRB_PRE_REG: {
2802 unsigned Rt = MI->getOperand(0).getReg();
2803 unsigned Rm = MI->getOperand(3).getReg();
2804 if (Rt == Rm)
2805 return 3;
2806 unsigned ShOpVal = MI->getOperand(4).getImm();
2807 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2808 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2809 if (!isSub &&
2810 (ShImm == 0 ||
2811 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2812 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2813 return 2;
2814 return 3;
2815 }
2816
2817 case ARM::STR_PRE_REG:
2818 case ARM::STRB_PRE_REG: {
2819 unsigned ShOpVal = MI->getOperand(4).getImm();
2820 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2821 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2822 if (!isSub &&
2823 (ShImm == 0 ||
2824 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2825 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2826 return 2;
2827 return 3;
2828 }
2829
2830 case ARM::LDRH_PRE:
2831 case ARM::STRH_PRE: {
2832 unsigned Rt = MI->getOperand(0).getReg();
2833 unsigned Rm = MI->getOperand(3).getReg();
2834 if (!Rm)
2835 return 2;
2836 if (Rt == Rm)
2837 return 3;
2838 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub)
2839 ? 3 : 2;
2840 }
2841
2842 case ARM::LDR_POST_REG:
2843 case ARM::LDRB_POST_REG:
2844 case ARM::LDRH_POST: {
2845 unsigned Rt = MI->getOperand(0).getReg();
2846 unsigned Rm = MI->getOperand(3).getReg();
2847 return (Rt == Rm) ? 3 : 2;
2848 }
2849
2850 case ARM::LDR_PRE_IMM:
2851 case ARM::LDRB_PRE_IMM:
2852 case ARM::LDR_POST_IMM:
2853 case ARM::LDRB_POST_IMM:
2854 case ARM::STRB_POST_IMM:
2855 case ARM::STRB_POST_REG:
2856 case ARM::STRB_PRE_IMM:
2857 case ARM::STRH_POST:
2858 case ARM::STR_POST_IMM:
2859 case ARM::STR_POST_REG:
2860 case ARM::STR_PRE_IMM:
2861 return 2;
2862
2863 case ARM::LDRSB_PRE:
2864 case ARM::LDRSH_PRE: {
2865 unsigned Rm = MI->getOperand(3).getReg();
2866 if (Rm == 0)
2867 return 3;
2868 unsigned Rt = MI->getOperand(0).getReg();
2869 if (Rt == Rm)
2870 return 4;
2871 unsigned ShOpVal = MI->getOperand(4).getImm();
2872 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2873 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2874 if (!isSub &&
2875 (ShImm == 0 ||
2876 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2877 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2878 return 3;
2879 return 4;
2880 }
2881
2882 case ARM::LDRD: {
2883 unsigned Rt = MI->getOperand(0).getReg();
2884 unsigned Rn = MI->getOperand(2).getReg();
2885 unsigned Rm = MI->getOperand(3).getReg();
2886 if (Rm)
2887 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2888 return (Rt == Rn) ? 3 : 2;
2889 }
2890
2891 case ARM::STRD: {
2892 unsigned Rm = MI->getOperand(3).getReg();
2893 if (Rm)
2894 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2895 return 2;
2896 }
2897
2898 case ARM::LDRD_POST:
2899 case ARM::t2LDRD_POST:
2900 return 3;
2901
2902 case ARM::STRD_POST:
2903 case ARM::t2STRD_POST:
2904 return 4;
2905
2906 case ARM::LDRD_PRE: {
2907 unsigned Rt = MI->getOperand(0).getReg();
2908 unsigned Rn = MI->getOperand(3).getReg();
2909 unsigned Rm = MI->getOperand(4).getReg();
2910 if (Rm)
2911 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2912 return (Rt == Rn) ? 4 : 3;
2913 }
2914
2915 case ARM::t2LDRD_PRE: {
2916 unsigned Rt = MI->getOperand(0).getReg();
2917 unsigned Rn = MI->getOperand(3).getReg();
2918 return (Rt == Rn) ? 4 : 3;
2919 }
2920
2921 case ARM::STRD_PRE: {
2922 unsigned Rm = MI->getOperand(4).getReg();
2923 if (Rm)
2924 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2925 return 3;
2926 }
2927
2928 case ARM::t2STRD_PRE:
2929 return 3;
2930
2931 case ARM::t2LDR_POST:
2932 case ARM::t2LDRB_POST:
2933 case ARM::t2LDRB_PRE:
2934 case ARM::t2LDRSBi12:
2935 case ARM::t2LDRSBi8:
2936 case ARM::t2LDRSBpci:
2937 case ARM::t2LDRSBs:
2938 case ARM::t2LDRH_POST:
2939 case ARM::t2LDRH_PRE:
2940 case ARM::t2LDRSBT:
2941 case ARM::t2LDRSB_POST:
2942 case ARM::t2LDRSB_PRE:
2943 case ARM::t2LDRSH_POST:
2944 case ARM::t2LDRSH_PRE:
2945 case ARM::t2LDRSHi12:
2946 case ARM::t2LDRSHi8:
2947 case ARM::t2LDRSHpci:
2948 case ARM::t2LDRSHs:
2949 return 2;
2950
2951 case ARM::t2LDRDi8: {
2952 unsigned Rt = MI->getOperand(0).getReg();
2953 unsigned Rn = MI->getOperand(2).getReg();
2954 return (Rt == Rn) ? 3 : 2;
2955 }
2956
2957 case ARM::t2STRB_POST:
2958 case ARM::t2STRB_PRE:
2959 case ARM::t2STRBs:
2960 case ARM::t2STRDi8:
2961 case ARM::t2STRH_POST:
2962 case ARM::t2STRH_PRE:
2963 case ARM::t2STRHs:
2964 case ARM::t2STR_POST:
2965 case ARM::t2STR_PRE:
2966 case ARM::t2STRs:
2967 return 2;
2968 }
2969}
2970
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00002971// Return the number of 32-bit words loaded by LDM or stored by STM. If this
2972// can't be easily determined return 0 (missing MachineMemOperand).
2973//
2974// FIXME: The current MachineInstr design does not support relying on machine
2975// mem operands to determine the width of a memory access. Instead, we expect
2976// the target to provide this information based on the instruction opcode and
Robin Morisset039781e2014-08-29 21:53:01 +00002977// operands. However, using MachineMemOperand is the best solution now for
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00002978// two reasons:
2979//
2980// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
2981// operands. This is much more dangerous than using the MachineMemOperand
2982// sizes because CodeGen passes can insert/remove optional machine operands. In
2983// fact, it's totally incorrect for preRA passes and appears to be wrong for
2984// postRA passes as well.
2985//
2986// 2) getNumLDMAddresses is only used by the scheduling machine model and any
2987// machine model that calls this should handle the unknown (zero size) case.
2988//
2989// Long term, we should require a target hook that verifies MachineMemOperand
2990// sizes during MC lowering. That target hook should be local to MC lowering
2991// because we can't ensure that it is aware of other MI forms. Doing this will
2992// ensure that MachineMemOperands are correctly propagated through all passes.
2993unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr *MI) const {
2994 unsigned Size = 0;
2995 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
2996 E = MI->memoperands_end(); I != E; ++I) {
2997 Size += (*I)->getSize();
2998 }
2999 return Size / 4;
3000}
3001
Evan Cheng367a5df2010-09-09 18:18:55 +00003002unsigned
Evan Chengdebf9c52010-11-03 00:45:17 +00003003ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
3004 const MachineInstr *MI) const {
Evan Chengbf407072010-09-10 01:29:16 +00003005 if (!ItinData || ItinData->isEmpty())
Evan Cheng367a5df2010-09-09 18:18:55 +00003006 return 1;
3007
Evan Cheng6cc775f2011-06-28 19:10:37 +00003008 const MCInstrDesc &Desc = MI->getDesc();
Evan Cheng367a5df2010-09-09 18:18:55 +00003009 unsigned Class = Desc.getSchedClass();
Andrew Trickf161e392012-07-02 18:10:42 +00003010 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003011 if (ItinUOps >= 0) {
3012 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
3013 return getNumMicroOpsSwiftLdSt(ItinData, MI);
3014
Andrew Trickf161e392012-07-02 18:10:42 +00003015 return ItinUOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003016 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003017
3018 unsigned Opc = MI->getOpcode();
3019 switch (Opc) {
3020 default:
3021 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003022 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003023 case ARM::VSTMQIA:
Evan Cheng367a5df2010-09-09 18:18:55 +00003024 return 2;
3025
3026 // The number of uOps for load / store multiple are determined by the number
3027 // registers.
Andrew Trickc416ba62010-12-24 04:28:06 +00003028 //
Evan Chengbf407072010-09-10 01:29:16 +00003029 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
3030 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledru35521e22012-07-23 08:51:15 +00003031 // separately by assuming the address is not 64-bit aligned.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003032 //
Evan Chengbf407072010-09-10 01:29:16 +00003033 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003034 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
3035 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3036 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003037 case ARM::VLDMDIA_UPD:
3038 case ARM::VLDMDDB_UPD:
3039 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003040 case ARM::VLDMSIA_UPD:
3041 case ARM::VLDMSDB_UPD:
3042 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003043 case ARM::VSTMDIA_UPD:
3044 case ARM::VSTMDDB_UPD:
3045 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003046 case ARM::VSTMSIA_UPD:
3047 case ARM::VSTMSDB_UPD: {
Evan Cheng367a5df2010-09-09 18:18:55 +00003048 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
3049 return (NumRegs / 2) + (NumRegs % 2) + 1;
3050 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003051
3052 case ARM::LDMIA_RET:
3053 case ARM::LDMIA:
3054 case ARM::LDMDA:
3055 case ARM::LDMDB:
3056 case ARM::LDMIB:
3057 case ARM::LDMIA_UPD:
3058 case ARM::LDMDA_UPD:
3059 case ARM::LDMDB_UPD:
3060 case ARM::LDMIB_UPD:
3061 case ARM::STMIA:
3062 case ARM::STMDA:
3063 case ARM::STMDB:
3064 case ARM::STMIB:
3065 case ARM::STMIA_UPD:
3066 case ARM::STMDA_UPD:
3067 case ARM::STMDB_UPD:
3068 case ARM::STMIB_UPD:
3069 case ARM::tLDMIA:
3070 case ARM::tLDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003071 case ARM::tSTMIA_UPD:
Evan Cheng367a5df2010-09-09 18:18:55 +00003072 case ARM::tPOP_RET:
3073 case ARM::tPOP:
3074 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003075 case ARM::t2LDMIA_RET:
3076 case ARM::t2LDMIA:
3077 case ARM::t2LDMDB:
3078 case ARM::t2LDMIA_UPD:
3079 case ARM::t2LDMDB_UPD:
3080 case ARM::t2STMIA:
3081 case ARM::t2STMDB:
3082 case ARM::t2STMIA_UPD:
3083 case ARM::t2STMDB_UPD: {
Evan Chengbf407072010-09-10 01:29:16 +00003084 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003085 if (Subtarget.isSwift()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003086 int UOps = 1 + NumRegs; // One for address computation, one for each ld / st.
3087 switch (Opc) {
3088 default: break;
3089 case ARM::VLDMDIA_UPD:
3090 case ARM::VLDMDDB_UPD:
3091 case ARM::VLDMSIA_UPD:
3092 case ARM::VLDMSDB_UPD:
3093 case ARM::VSTMDIA_UPD:
3094 case ARM::VSTMDDB_UPD:
3095 case ARM::VSTMSIA_UPD:
3096 case ARM::VSTMSDB_UPD:
3097 case ARM::LDMIA_UPD:
3098 case ARM::LDMDA_UPD:
3099 case ARM::LDMDB_UPD:
3100 case ARM::LDMIB_UPD:
3101 case ARM::STMIA_UPD:
3102 case ARM::STMDA_UPD:
3103 case ARM::STMDB_UPD:
3104 case ARM::STMIB_UPD:
3105 case ARM::tLDMIA_UPD:
3106 case ARM::tSTMIA_UPD:
3107 case ARM::t2LDMIA_UPD:
3108 case ARM::t2LDMDB_UPD:
3109 case ARM::t2STMIA_UPD:
3110 case ARM::t2STMDB_UPD:
3111 ++UOps; // One for base register writeback.
3112 break;
3113 case ARM::LDMIA_RET:
3114 case ARM::tPOP_RET:
3115 case ARM::t2LDMIA_RET:
3116 UOps += 2; // One for base reg wb, one for write to pc.
3117 break;
3118 }
3119 return UOps;
Tim Northover0feb91e2014-04-01 14:10:07 +00003120 } else if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Chengdebf9c52010-11-03 00:45:17 +00003121 if (NumRegs < 4)
3122 return 2;
3123 // 4 registers would be issued: 2, 2.
3124 // 5 registers would be issued: 2, 2, 1.
Andrew Trickf161e392012-07-02 18:10:42 +00003125 int A8UOps = (NumRegs / 2);
Evan Chengdebf9c52010-11-03 00:45:17 +00003126 if (NumRegs % 2)
Andrew Trickf161e392012-07-02 18:10:42 +00003127 ++A8UOps;
3128 return A8UOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003129 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Andrew Trickf161e392012-07-02 18:10:42 +00003130 int A9UOps = (NumRegs / 2);
Evan Chengbf407072010-09-10 01:29:16 +00003131 // If there are odd number of registers or if it's not 64-bit aligned,
3132 // then it takes an extra AGU (Address Generation Unit) cycle.
3133 if ((NumRegs % 2) ||
3134 !MI->hasOneMemOperand() ||
3135 (*MI->memoperands_begin())->getAlignment() < 8)
Andrew Trickf161e392012-07-02 18:10:42 +00003136 ++A9UOps;
3137 return A9UOps;
Evan Chengbf407072010-09-10 01:29:16 +00003138 } else {
3139 // Assume the worst.
3140 return NumRegs;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00003141 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003142 }
3143 }
3144}
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003145
3146int
Evan Cheng412e37b2010-10-07 23:12:15 +00003147ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003148 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003149 unsigned DefClass,
3150 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003151 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003152 if (RegNo <= 0)
3153 // Def is the address writeback.
3154 return ItinData->getOperandCycle(DefClass, DefIdx);
3155
3156 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003157 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003158 // (regno / 2) + (regno % 2) + 1
3159 DefCycle = RegNo / 2 + 1;
3160 if (RegNo % 2)
3161 ++DefCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003162 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003163 DefCycle = RegNo;
3164 bool isSLoad = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003165
Evan Cheng6cc775f2011-06-28 19:10:37 +00003166 switch (DefMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003167 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003168 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003169 case ARM::VLDMSIA_UPD:
3170 case ARM::VLDMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003171 isSLoad = true;
3172 break;
3173 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003174
Evan Cheng412e37b2010-10-07 23:12:15 +00003175 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3176 // then it takes an extra cycle.
3177 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3178 ++DefCycle;
3179 } else {
3180 // Assume the worst.
3181 DefCycle = RegNo + 2;
3182 }
3183
3184 return DefCycle;
3185}
3186
3187int
3188ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003189 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003190 unsigned DefClass,
3191 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003192 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003193 if (RegNo <= 0)
3194 // Def is the address writeback.
3195 return ItinData->getOperandCycle(DefClass, DefIdx);
3196
3197 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003198 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003199 // 4 registers would be issued: 1, 2, 1.
3200 // 5 registers would be issued: 1, 2, 2.
3201 DefCycle = RegNo / 2;
3202 if (DefCycle < 1)
3203 DefCycle = 1;
3204 // Result latency is issue cycle + 2: E2.
3205 DefCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003206 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003207 DefCycle = (RegNo / 2);
3208 // If there are odd number of registers or if it's not 64-bit aligned,
3209 // then it takes an extra AGU (Address Generation Unit) cycle.
3210 if ((RegNo % 2) || DefAlign < 8)
3211 ++DefCycle;
3212 // Result latency is AGU cycles + 2.
3213 DefCycle += 2;
3214 } else {
3215 // Assume the worst.
3216 DefCycle = RegNo + 2;
3217 }
3218
3219 return DefCycle;
3220}
3221
3222int
3223ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003224 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003225 unsigned UseClass,
3226 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003227 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003228 if (RegNo <= 0)
3229 return ItinData->getOperandCycle(UseClass, UseIdx);
3230
3231 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003232 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003233 // (regno / 2) + (regno % 2) + 1
3234 UseCycle = RegNo / 2 + 1;
3235 if (RegNo % 2)
3236 ++UseCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003237 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003238 UseCycle = RegNo;
3239 bool isSStore = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003240
Evan Cheng6cc775f2011-06-28 19:10:37 +00003241 switch (UseMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003242 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003243 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003244 case ARM::VSTMSIA_UPD:
3245 case ARM::VSTMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003246 isSStore = true;
3247 break;
3248 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003249
Evan Cheng412e37b2010-10-07 23:12:15 +00003250 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3251 // then it takes an extra cycle.
3252 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3253 ++UseCycle;
3254 } else {
3255 // Assume the worst.
3256 UseCycle = RegNo + 2;
3257 }
3258
3259 return UseCycle;
3260}
3261
3262int
3263ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003264 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003265 unsigned UseClass,
3266 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003267 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003268 if (RegNo <= 0)
3269 return ItinData->getOperandCycle(UseClass, UseIdx);
3270
3271 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003272 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003273 UseCycle = RegNo / 2;
3274 if (UseCycle < 2)
3275 UseCycle = 2;
3276 // Read in E3.
3277 UseCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003278 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003279 UseCycle = (RegNo / 2);
3280 // If there are odd number of registers or if it's not 64-bit aligned,
3281 // then it takes an extra AGU (Address Generation Unit) cycle.
3282 if ((RegNo % 2) || UseAlign < 8)
3283 ++UseCycle;
3284 } else {
3285 // Assume the worst.
3286 UseCycle = 1;
3287 }
3288 return UseCycle;
3289}
3290
3291int
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003292ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003293 const MCInstrDesc &DefMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003294 unsigned DefIdx, unsigned DefAlign,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003295 const MCInstrDesc &UseMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003296 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003297 unsigned DefClass = DefMCID.getSchedClass();
3298 unsigned UseClass = UseMCID.getSchedClass();
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003299
Evan Cheng6cc775f2011-06-28 19:10:37 +00003300 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003301 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3302
3303 // This may be a def / use of a variable_ops instruction, the operand
3304 // latency might be determinable dynamically. Let the target try to
3305 // figure it out.
Evan Chenge2c211c2010-10-28 02:00:25 +00003306 int DefCycle = -1;
Evan Chengff310732010-10-28 06:47:08 +00003307 bool LdmBypass = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003308 switch (DefMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003309 default:
3310 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3311 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003312
3313 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003314 case ARM::VLDMDIA_UPD:
3315 case ARM::VLDMDDB_UPD:
3316 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003317 case ARM::VLDMSIA_UPD:
3318 case ARM::VLDMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003319 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003320 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003321
3322 case ARM::LDMIA_RET:
3323 case ARM::LDMIA:
3324 case ARM::LDMDA:
3325 case ARM::LDMDB:
3326 case ARM::LDMIB:
3327 case ARM::LDMIA_UPD:
3328 case ARM::LDMDA_UPD:
3329 case ARM::LDMDB_UPD:
3330 case ARM::LDMIB_UPD:
3331 case ARM::tLDMIA:
3332 case ARM::tLDMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003333 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003334 case ARM::t2LDMIA_RET:
3335 case ARM::t2LDMIA:
3336 case ARM::t2LDMDB:
3337 case ARM::t2LDMIA_UPD:
3338 case ARM::t2LDMDB_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003339 LdmBypass = 1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003340 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng412e37b2010-10-07 23:12:15 +00003341 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003342 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003343
3344 if (DefCycle == -1)
3345 // We can't seem to determine the result latency of the def, assume it's 2.
3346 DefCycle = 2;
3347
3348 int UseCycle = -1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003349 switch (UseMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003350 default:
3351 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3352 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003353
3354 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003355 case ARM::VSTMDIA_UPD:
3356 case ARM::VSTMDDB_UPD:
3357 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003358 case ARM::VSTMSIA_UPD:
3359 case ARM::VSTMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003360 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003361 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003362
3363 case ARM::STMIA:
3364 case ARM::STMDA:
3365 case ARM::STMDB:
3366 case ARM::STMIB:
3367 case ARM::STMIA_UPD:
3368 case ARM::STMDA_UPD:
3369 case ARM::STMDB_UPD:
3370 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003371 case ARM::tSTMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003372 case ARM::tPOP_RET:
3373 case ARM::tPOP:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003374 case ARM::t2STMIA:
3375 case ARM::t2STMDB:
3376 case ARM::t2STMIA_UPD:
3377 case ARM::t2STMDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003378 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003379 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003380 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003381
3382 if (UseCycle == -1)
3383 // Assume it's read in the first stage.
3384 UseCycle = 1;
3385
3386 UseCycle = DefCycle - UseCycle + 1;
3387 if (UseCycle > 0) {
3388 if (LdmBypass) {
3389 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3390 // first def operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003391 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003392 UseClass, UseIdx))
3393 --UseCycle;
3394 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003395 UseClass, UseIdx)) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003396 --UseCycle;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003397 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003398 }
3399
3400 return UseCycle;
3401}
3402
Evan Cheng7fae11b2011-12-14 02:11:42 +00003403static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003404 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003405 unsigned &DefIdx, unsigned &Dist) {
3406 Dist = 0;
3407
3408 MachineBasicBlock::const_iterator I = MI; ++I;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003409 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
Evan Cheng7fae11b2011-12-14 02:11:42 +00003410 assert(II->isInsideBundle() && "Empty bundle?");
3411
3412 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003413 while (II->isInsideBundle()) {
3414 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3415 if (Idx != -1)
3416 break;
3417 --II;
3418 ++Dist;
3419 }
3420
3421 assert(Idx != -1 && "Cannot find bundled definition!");
3422 DefIdx = Idx;
3423 return II;
3424}
3425
3426static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003427 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003428 unsigned &UseIdx, unsigned &Dist) {
3429 Dist = 0;
3430
3431 MachineBasicBlock::const_instr_iterator II = MI; ++II;
3432 assert(II->isInsideBundle() && "Empty bundle?");
3433 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3434
3435 // FIXME: This doesn't properly handle multiple uses.
3436 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003437 while (II != E && II->isInsideBundle()) {
3438 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3439 if (Idx != -1)
3440 break;
3441 if (II->getOpcode() != ARM::t2IT)
3442 ++Dist;
3443 ++II;
3444 }
3445
Evan Chengda103bf2011-12-14 20:00:08 +00003446 if (Idx == -1) {
3447 Dist = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003448 return nullptr;
Evan Chengda103bf2011-12-14 20:00:08 +00003449 }
3450
Evan Cheng7fae11b2011-12-14 02:11:42 +00003451 UseIdx = Idx;
3452 return II;
3453}
3454
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003455/// Return the number of cycles to add to (or subtract from) the static
3456/// itinerary based on the def opcode and alignment. The caller will ensure that
3457/// adjusted latency is at least one cycle.
3458static int adjustDefLatency(const ARMSubtarget &Subtarget,
3459 const MachineInstr *DefMI,
3460 const MCInstrDesc *DefMCID, unsigned DefAlign) {
3461 int Adjust = 0;
Tim Northover0feb91e2014-04-01 14:10:07 +00003462 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
Evan Chengff310732010-10-28 06:47:08 +00003463 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3464 // variants are one cycle cheaper.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003465 switch (DefMCID->getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003466 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003467 case ARM::LDRrs:
3468 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003469 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3470 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3471 if (ShImm == 0 ||
3472 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003473 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003474 break;
3475 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003476 case ARM::t2LDRs:
3477 case ARM::t2LDRBs:
3478 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003479 case ARM::t2LDRSHs: {
3480 // Thumb2 mode: lsl only.
3481 unsigned ShAmt = DefMI->getOperand(3).getImm();
3482 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003483 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003484 break;
3485 }
3486 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003487 } else if (Subtarget.isSwift()) {
3488 // FIXME: Properly handle all of the latency adjustments for address
3489 // writeback.
3490 switch (DefMCID->getOpcode()) {
3491 default: break;
3492 case ARM::LDRrs:
3493 case ARM::LDRBrs: {
3494 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3495 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3496 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3497 if (!isSub &&
3498 (ShImm == 0 ||
3499 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3500 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3501 Adjust -= 2;
3502 else if (!isSub &&
3503 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3504 --Adjust;
3505 break;
3506 }
3507 case ARM::t2LDRs:
3508 case ARM::t2LDRBs:
3509 case ARM::t2LDRHs:
3510 case ARM::t2LDRSHs: {
3511 // Thumb2 mode: lsl only.
3512 unsigned ShAmt = DefMI->getOperand(3).getImm();
3513 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3514 Adjust -= 2;
3515 break;
3516 }
3517 }
Evan Chengff310732010-10-28 06:47:08 +00003518 }
3519
Silviu Barangab47bb942012-09-13 15:05:10 +00003520 if (DefAlign < 8 && Subtarget.isLikeA9()) {
Evan Cheng7fae11b2011-12-14 02:11:42 +00003521 switch (DefMCID->getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003522 default: break;
3523 case ARM::VLD1q8:
3524 case ARM::VLD1q16:
3525 case ARM::VLD1q32:
3526 case ARM::VLD1q64:
Jim Grosbach2098cb12011-10-24 21:45:13 +00003527 case ARM::VLD1q8wb_fixed:
3528 case ARM::VLD1q16wb_fixed:
3529 case ARM::VLD1q32wb_fixed:
3530 case ARM::VLD1q64wb_fixed:
3531 case ARM::VLD1q8wb_register:
3532 case ARM::VLD1q16wb_register:
3533 case ARM::VLD1q32wb_register:
3534 case ARM::VLD1q64wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003535 case ARM::VLD2d8:
3536 case ARM::VLD2d16:
3537 case ARM::VLD2d32:
3538 case ARM::VLD2q8:
3539 case ARM::VLD2q16:
3540 case ARM::VLD2q32:
Jim Grosbachd146a022011-12-09 21:28:25 +00003541 case ARM::VLD2d8wb_fixed:
3542 case ARM::VLD2d16wb_fixed:
3543 case ARM::VLD2d32wb_fixed:
3544 case ARM::VLD2q8wb_fixed:
3545 case ARM::VLD2q16wb_fixed:
3546 case ARM::VLD2q32wb_fixed:
3547 case ARM::VLD2d8wb_register:
3548 case ARM::VLD2d16wb_register:
3549 case ARM::VLD2d32wb_register:
3550 case ARM::VLD2q8wb_register:
3551 case ARM::VLD2q16wb_register:
3552 case ARM::VLD2q32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003553 case ARM::VLD3d8:
3554 case ARM::VLD3d16:
3555 case ARM::VLD3d32:
3556 case ARM::VLD1d64T:
3557 case ARM::VLD3d8_UPD:
3558 case ARM::VLD3d16_UPD:
3559 case ARM::VLD3d32_UPD:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00003560 case ARM::VLD1d64Twb_fixed:
3561 case ARM::VLD1d64Twb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003562 case ARM::VLD3q8_UPD:
3563 case ARM::VLD3q16_UPD:
3564 case ARM::VLD3q32_UPD:
3565 case ARM::VLD4d8:
3566 case ARM::VLD4d16:
3567 case ARM::VLD4d32:
3568 case ARM::VLD1d64Q:
3569 case ARM::VLD4d8_UPD:
3570 case ARM::VLD4d16_UPD:
3571 case ARM::VLD4d32_UPD:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00003572 case ARM::VLD1d64Qwb_fixed:
3573 case ARM::VLD1d64Qwb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003574 case ARM::VLD4q8_UPD:
3575 case ARM::VLD4q16_UPD:
3576 case ARM::VLD4q32_UPD:
3577 case ARM::VLD1DUPq8:
3578 case ARM::VLD1DUPq16:
3579 case ARM::VLD1DUPq32:
Jim Grosbacha68c9a82011-11-30 19:35:44 +00003580 case ARM::VLD1DUPq8wb_fixed:
3581 case ARM::VLD1DUPq16wb_fixed:
3582 case ARM::VLD1DUPq32wb_fixed:
3583 case ARM::VLD1DUPq8wb_register:
3584 case ARM::VLD1DUPq16wb_register:
3585 case ARM::VLD1DUPq32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003586 case ARM::VLD2DUPd8:
3587 case ARM::VLD2DUPd16:
3588 case ARM::VLD2DUPd32:
Jim Grosbachc80a2642011-12-21 19:40:55 +00003589 case ARM::VLD2DUPd8wb_fixed:
3590 case ARM::VLD2DUPd16wb_fixed:
3591 case ARM::VLD2DUPd32wb_fixed:
3592 case ARM::VLD2DUPd8wb_register:
3593 case ARM::VLD2DUPd16wb_register:
3594 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003595 case ARM::VLD4DUPd8:
3596 case ARM::VLD4DUPd16:
3597 case ARM::VLD4DUPd32:
3598 case ARM::VLD4DUPd8_UPD:
3599 case ARM::VLD4DUPd16_UPD:
3600 case ARM::VLD4DUPd32_UPD:
3601 case ARM::VLD1LNd8:
3602 case ARM::VLD1LNd16:
3603 case ARM::VLD1LNd32:
3604 case ARM::VLD1LNd8_UPD:
3605 case ARM::VLD1LNd16_UPD:
3606 case ARM::VLD1LNd32_UPD:
3607 case ARM::VLD2LNd8:
3608 case ARM::VLD2LNd16:
3609 case ARM::VLD2LNd32:
3610 case ARM::VLD2LNq16:
3611 case ARM::VLD2LNq32:
3612 case ARM::VLD2LNd8_UPD:
3613 case ARM::VLD2LNd16_UPD:
3614 case ARM::VLD2LNd32_UPD:
3615 case ARM::VLD2LNq16_UPD:
3616 case ARM::VLD2LNq32_UPD:
3617 case ARM::VLD4LNd8:
3618 case ARM::VLD4LNd16:
3619 case ARM::VLD4LNd32:
3620 case ARM::VLD4LNq16:
3621 case ARM::VLD4LNq32:
3622 case ARM::VLD4LNd8_UPD:
3623 case ARM::VLD4LNd16_UPD:
3624 case ARM::VLD4LNd32_UPD:
3625 case ARM::VLD4LNq16_UPD:
3626 case ARM::VLD4LNq32_UPD:
3627 // If the address is not 64-bit aligned, the latencies of these
3628 // instructions increases by one.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003629 ++Adjust;
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003630 break;
3631 }
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003632 }
3633 return Adjust;
3634}
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003635
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003636
3637
3638int
3639ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3640 const MachineInstr *DefMI, unsigned DefIdx,
3641 const MachineInstr *UseMI,
3642 unsigned UseIdx) const {
3643 // No operand latency. The caller may fall back to getInstrLatency.
3644 if (!ItinData || ItinData->isEmpty())
3645 return -1;
3646
3647 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
3648 unsigned Reg = DefMO.getReg();
3649 const MCInstrDesc *DefMCID = &DefMI->getDesc();
3650 const MCInstrDesc *UseMCID = &UseMI->getDesc();
3651
3652 unsigned DefAdj = 0;
3653 if (DefMI->isBundle()) {
3654 DefMI = getBundledDefMI(&getRegisterInfo(), DefMI, Reg, DefIdx, DefAdj);
3655 DefMCID = &DefMI->getDesc();
3656 }
3657 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
3658 DefMI->isRegSequence() || DefMI->isImplicitDef()) {
3659 return 1;
3660 }
3661
3662 unsigned UseAdj = 0;
3663 if (UseMI->isBundle()) {
3664 unsigned NewUseIdx;
3665 const MachineInstr *NewUseMI = getBundledUseMI(&getRegisterInfo(), UseMI,
3666 Reg, NewUseIdx, UseAdj);
Andrew Trick77d0b882012-06-22 02:50:33 +00003667 if (!NewUseMI)
3668 return -1;
3669
3670 UseMI = NewUseMI;
3671 UseIdx = NewUseIdx;
3672 UseMCID = &UseMI->getDesc();
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003673 }
3674
3675 if (Reg == ARM::CPSR) {
3676 if (DefMI->getOpcode() == ARM::FMSTAT) {
3677 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Barangab47bb942012-09-13 15:05:10 +00003678 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003679 }
3680
3681 // CPSR set and branch can be paired in the same cycle.
3682 if (UseMI->isBranch())
3683 return 0;
3684
3685 // Otherwise it takes the instruction latency (generally one).
3686 unsigned Latency = getInstrLatency(ItinData, DefMI);
3687
3688 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3689 // its uses. Instructions which are otherwise scheduled between them may
3690 // incur a code size penalty (not able to use the CPSR setting 16-bit
3691 // instructions).
3692 if (Latency > 0 && Subtarget.isThumb2()) {
3693 const MachineFunction *MF = DefMI->getParent()->getParent();
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00003694 if (MF->getFunction()->hasFnAttribute(Attribute::OptimizeForSize))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003695 --Latency;
3696 }
3697 return Latency;
3698 }
3699
Andrew Trick77d0b882012-06-22 02:50:33 +00003700 if (DefMO.isImplicit() || UseMI->getOperand(UseIdx).isImplicit())
3701 return -1;
3702
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003703 unsigned DefAlign = DefMI->hasOneMemOperand()
3704 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
3705 unsigned UseAlign = UseMI->hasOneMemOperand()
3706 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
3707
3708 // Get the itinerary's latency if possible, and handle variable_ops.
3709 int Latency = getOperandLatency(ItinData, *DefMCID, DefIdx, DefAlign,
3710 *UseMCID, UseIdx, UseAlign);
3711 // Unable to find operand latency. The caller may resort to getInstrLatency.
3712 if (Latency < 0)
3713 return Latency;
3714
3715 // Adjust for IT block position.
3716 int Adj = DefAdj + UseAdj;
3717
3718 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3719 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
3720 if (Adj >= 0 || (int)Latency > -Adj) {
3721 return Latency + Adj;
3722 }
3723 // Return the itinerary latency, which may be zero but not less than zero.
Evan Chengff310732010-10-28 06:47:08 +00003724 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003725}
3726
3727int
3728ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3729 SDNode *DefNode, unsigned DefIdx,
3730 SDNode *UseNode, unsigned UseIdx) const {
3731 if (!DefNode->isMachineOpcode())
3732 return 1;
3733
Evan Cheng6cc775f2011-06-28 19:10:37 +00003734 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trick47ff14b2011-01-21 05:51:33 +00003735
Evan Cheng6cc775f2011-06-28 19:10:37 +00003736 if (isZeroCost(DefMCID.Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +00003737 return 0;
3738
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003739 if (!ItinData || ItinData->isEmpty())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003740 return DefMCID.mayLoad() ? 3 : 1;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003741
Evan Cheng6c1414f2010-10-29 18:09:28 +00003742 if (!UseNode->isMachineOpcode()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003743 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003744 if (Subtarget.isLikeA9() || Subtarget.isSwift())
Evan Cheng6c1414f2010-10-29 18:09:28 +00003745 return Latency <= 2 ? 1 : Latency - 1;
3746 else
3747 return Latency <= 3 ? 1 : Latency - 2;
3748 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003749
Evan Cheng6cc775f2011-06-28 19:10:37 +00003750 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003751 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
3752 unsigned DefAlign = !DefMN->memoperands_empty()
3753 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
3754 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
3755 unsigned UseAlign = !UseMN->memoperands_empty()
3756 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003757 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
3758 UseMCID, UseIdx, UseAlign);
Evan Chengff310732010-10-28 06:47:08 +00003759
3760 if (Latency > 1 &&
Tim Northover0feb91e2014-04-01 14:10:07 +00003761 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
3762 Subtarget.isCortexA7())) {
Evan Chengff310732010-10-28 06:47:08 +00003763 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3764 // variants are one cycle cheaper.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003765 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003766 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003767 case ARM::LDRrs:
3768 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003769 unsigned ShOpVal =
3770 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3771 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3772 if (ShImm == 0 ||
3773 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3774 --Latency;
3775 break;
3776 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003777 case ARM::t2LDRs:
3778 case ARM::t2LDRBs:
3779 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003780 case ARM::t2LDRSHs: {
3781 // Thumb2 mode: lsl only.
3782 unsigned ShAmt =
3783 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3784 if (ShAmt == 0 || ShAmt == 2)
3785 --Latency;
3786 break;
3787 }
3788 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003789 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
3790 // FIXME: Properly handle all of the latency adjustments for address
3791 // writeback.
3792 switch (DefMCID.getOpcode()) {
3793 default: break;
3794 case ARM::LDRrs:
3795 case ARM::LDRBrs: {
3796 unsigned ShOpVal =
3797 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3798 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3799 if (ShImm == 0 ||
3800 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3801 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3802 Latency -= 2;
3803 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3804 --Latency;
3805 break;
3806 }
3807 case ARM::t2LDRs:
3808 case ARM::t2LDRBs:
3809 case ARM::t2LDRHs:
3810 case ARM::t2LDRSHs: {
3811 // Thumb2 mode: lsl 0-3 only.
3812 Latency -= 2;
3813 break;
3814 }
3815 }
Evan Chengff310732010-10-28 06:47:08 +00003816 }
3817
Silviu Barangab47bb942012-09-13 15:05:10 +00003818 if (DefAlign < 8 && Subtarget.isLikeA9())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003819 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003820 default: break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003821 case ARM::VLD1q8:
3822 case ARM::VLD1q16:
3823 case ARM::VLD1q32:
3824 case ARM::VLD1q64:
3825 case ARM::VLD1q8wb_register:
3826 case ARM::VLD1q16wb_register:
3827 case ARM::VLD1q32wb_register:
3828 case ARM::VLD1q64wb_register:
3829 case ARM::VLD1q8wb_fixed:
3830 case ARM::VLD1q16wb_fixed:
3831 case ARM::VLD1q32wb_fixed:
3832 case ARM::VLD1q64wb_fixed:
3833 case ARM::VLD2d8:
3834 case ARM::VLD2d16:
3835 case ARM::VLD2d32:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003836 case ARM::VLD2q8Pseudo:
3837 case ARM::VLD2q16Pseudo:
3838 case ARM::VLD2q32Pseudo:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003839 case ARM::VLD2d8wb_fixed:
3840 case ARM::VLD2d16wb_fixed:
3841 case ARM::VLD2d32wb_fixed:
Jim Grosbachd146a022011-12-09 21:28:25 +00003842 case ARM::VLD2q8PseudoWB_fixed:
3843 case ARM::VLD2q16PseudoWB_fixed:
3844 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003845 case ARM::VLD2d8wb_register:
3846 case ARM::VLD2d16wb_register:
3847 case ARM::VLD2d32wb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00003848 case ARM::VLD2q8PseudoWB_register:
3849 case ARM::VLD2q16PseudoWB_register:
3850 case ARM::VLD2q32PseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003851 case ARM::VLD3d8Pseudo:
3852 case ARM::VLD3d16Pseudo:
3853 case ARM::VLD3d32Pseudo:
3854 case ARM::VLD1d64TPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003855 case ARM::VLD1d64TPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003856 case ARM::VLD3d8Pseudo_UPD:
3857 case ARM::VLD3d16Pseudo_UPD:
3858 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003859 case ARM::VLD3q8Pseudo_UPD:
3860 case ARM::VLD3q16Pseudo_UPD:
3861 case ARM::VLD3q32Pseudo_UPD:
3862 case ARM::VLD3q8oddPseudo:
3863 case ARM::VLD3q16oddPseudo:
3864 case ARM::VLD3q32oddPseudo:
3865 case ARM::VLD3q8oddPseudo_UPD:
3866 case ARM::VLD3q16oddPseudo_UPD:
3867 case ARM::VLD3q32oddPseudo_UPD:
3868 case ARM::VLD4d8Pseudo:
3869 case ARM::VLD4d16Pseudo:
3870 case ARM::VLD4d32Pseudo:
3871 case ARM::VLD1d64QPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003872 case ARM::VLD1d64QPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003873 case ARM::VLD4d8Pseudo_UPD:
3874 case ARM::VLD4d16Pseudo_UPD:
3875 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003876 case ARM::VLD4q8Pseudo_UPD:
3877 case ARM::VLD4q16Pseudo_UPD:
3878 case ARM::VLD4q32Pseudo_UPD:
3879 case ARM::VLD4q8oddPseudo:
3880 case ARM::VLD4q16oddPseudo:
3881 case ARM::VLD4q32oddPseudo:
3882 case ARM::VLD4q8oddPseudo_UPD:
3883 case ARM::VLD4q16oddPseudo_UPD:
3884 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbach13a292c2012-03-06 22:01:44 +00003885 case ARM::VLD1DUPq8:
3886 case ARM::VLD1DUPq16:
3887 case ARM::VLD1DUPq32:
3888 case ARM::VLD1DUPq8wb_fixed:
3889 case ARM::VLD1DUPq16wb_fixed:
3890 case ARM::VLD1DUPq32wb_fixed:
3891 case ARM::VLD1DUPq8wb_register:
3892 case ARM::VLD1DUPq16wb_register:
3893 case ARM::VLD1DUPq32wb_register:
3894 case ARM::VLD2DUPd8:
3895 case ARM::VLD2DUPd16:
3896 case ARM::VLD2DUPd32:
3897 case ARM::VLD2DUPd8wb_fixed:
3898 case ARM::VLD2DUPd16wb_fixed:
3899 case ARM::VLD2DUPd32wb_fixed:
3900 case ARM::VLD2DUPd8wb_register:
3901 case ARM::VLD2DUPd16wb_register:
3902 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003903 case ARM::VLD4DUPd8Pseudo:
3904 case ARM::VLD4DUPd16Pseudo:
3905 case ARM::VLD4DUPd32Pseudo:
3906 case ARM::VLD4DUPd8Pseudo_UPD:
3907 case ARM::VLD4DUPd16Pseudo_UPD:
3908 case ARM::VLD4DUPd32Pseudo_UPD:
3909 case ARM::VLD1LNq8Pseudo:
3910 case ARM::VLD1LNq16Pseudo:
3911 case ARM::VLD1LNq32Pseudo:
3912 case ARM::VLD1LNq8Pseudo_UPD:
3913 case ARM::VLD1LNq16Pseudo_UPD:
3914 case ARM::VLD1LNq32Pseudo_UPD:
3915 case ARM::VLD2LNd8Pseudo:
3916 case ARM::VLD2LNd16Pseudo:
3917 case ARM::VLD2LNd32Pseudo:
3918 case ARM::VLD2LNq16Pseudo:
3919 case ARM::VLD2LNq32Pseudo:
3920 case ARM::VLD2LNd8Pseudo_UPD:
3921 case ARM::VLD2LNd16Pseudo_UPD:
3922 case ARM::VLD2LNd32Pseudo_UPD:
3923 case ARM::VLD2LNq16Pseudo_UPD:
3924 case ARM::VLD2LNq32Pseudo_UPD:
3925 case ARM::VLD4LNd8Pseudo:
3926 case ARM::VLD4LNd16Pseudo:
3927 case ARM::VLD4LNd32Pseudo:
3928 case ARM::VLD4LNq16Pseudo:
3929 case ARM::VLD4LNq32Pseudo:
3930 case ARM::VLD4LNd8Pseudo_UPD:
3931 case ARM::VLD4LNd16Pseudo_UPD:
3932 case ARM::VLD4LNd32Pseudo_UPD:
3933 case ARM::VLD4LNq16Pseudo_UPD:
3934 case ARM::VLD4LNq32Pseudo_UPD:
3935 // If the address is not 64-bit aligned, the latencies of these
3936 // instructions increases by one.
3937 ++Latency;
3938 break;
3939 }
3940
Evan Chengff310732010-10-28 06:47:08 +00003941 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003942}
Evan Cheng63c76082010-10-19 18:58:51 +00003943
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00003944unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr *MI) const {
3945 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3946 MI->isRegSequence() || MI->isImplicitDef())
3947 return 0;
3948
3949 if (MI->isBundle())
3950 return 0;
3951
3952 const MCInstrDesc &MCID = MI->getDesc();
3953
3954 if (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR)) {
3955 // When predicated, CPSR is an additional source operand for CPSR updating
3956 // instructions, this apparently increases their latencies.
3957 return 1;
3958 }
3959 return 0;
3960}
3961
Andrew Trick45446062012-06-05 21:11:27 +00003962unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3963 const MachineInstr *MI,
3964 unsigned *PredCost) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00003965 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3966 MI->isRegSequence() || MI->isImplicitDef())
3967 return 1;
3968
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003969 // An instruction scheduler typically runs on unbundled instructions, however
3970 // other passes may query the latency of a bundled instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003971 if (MI->isBundle()) {
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003972 unsigned Latency = 0;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003973 MachineBasicBlock::const_instr_iterator I = MI;
3974 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3975 while (++I != E && I->isInsideBundle()) {
3976 if (I->getOpcode() != ARM::t2IT)
3977 Latency += getInstrLatency(ItinData, I, PredCost);
3978 }
3979 return Latency;
3980 }
3981
Evan Cheng6cc775f2011-06-28 19:10:37 +00003982 const MCInstrDesc &MCID = MI->getDesc();
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003983 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) {
Evan Chengdebf9c52010-11-03 00:45:17 +00003984 // When predicated, CPSR is an additional source operand for CPSR updating
3985 // instructions, this apparently increases their latencies.
3986 *PredCost = 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003987 }
3988 // Be sure to call getStageLatency for an empty itinerary in case it has a
3989 // valid MinLatency property.
3990 if (!ItinData)
3991 return MI->mayLoad() ? 3 : 1;
3992
3993 unsigned Class = MCID.getSchedClass();
3994
3995 // For instructions with variable uops, use uops as latency.
Andrew Trick21cca972012-07-02 19:12:29 +00003996 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003997 return getNumMicroOps(ItinData, MI);
Andrew Trick21cca972012-07-02 19:12:29 +00003998
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003999 // For the common case, fall back on the itinerary's latency.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004000 unsigned Latency = ItinData->getStageLatency(Class);
4001
4002 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
4003 unsigned DefAlign = MI->hasOneMemOperand()
4004 ? (*MI->memoperands_begin())->getAlignment() : 0;
4005 int Adj = adjustDefLatency(Subtarget, MI, &MCID, DefAlign);
4006 if (Adj >= 0 || (int)Latency > -Adj) {
4007 return Latency + Adj;
4008 }
4009 return Latency;
Evan Chengdebf9c52010-11-03 00:45:17 +00004010}
4011
4012int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
4013 SDNode *Node) const {
4014 if (!Node->isMachineOpcode())
4015 return 1;
4016
4017 if (!ItinData || ItinData->isEmpty())
4018 return 1;
4019
4020 unsigned Opcode = Node->getMachineOpcode();
4021 switch (Opcode) {
4022 default:
4023 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004024 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004025 case ARM::VSTMQIA:
Evan Chengdebf9c52010-11-03 00:45:17 +00004026 return 2;
Eric Christopherb006fc92010-11-18 19:40:05 +00004027 }
Evan Chengdebf9c52010-11-03 00:45:17 +00004028}
4029
Evan Cheng63c76082010-10-19 18:58:51 +00004030bool ARMBaseInstrInfo::
4031hasHighOperandLatency(const InstrItineraryData *ItinData,
4032 const MachineRegisterInfo *MRI,
4033 const MachineInstr *DefMI, unsigned DefIdx,
4034 const MachineInstr *UseMI, unsigned UseIdx) const {
4035 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
4036 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
4037 if (Subtarget.isCortexA8() &&
4038 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
4039 // CortexA8 VFP instructions are not pipelined.
4040 return true;
4041
4042 // Hoist VFP / NEON instructions with 4 or higher latency.
Andrew Trickde2109e2013-06-15 04:49:57 +00004043 int Latency = computeOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
Andrew Trick3564bdf2012-06-07 19:41:58 +00004044 if (Latency < 0)
4045 Latency = getInstrLatency(ItinData, DefMI);
Evan Cheng63c76082010-10-19 18:58:51 +00004046 if (Latency <= 3)
4047 return false;
4048 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4049 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4050}
Evan Chenge96b8d72010-10-26 02:08:50 +00004051
4052bool ARMBaseInstrInfo::
4053hasLowDefLatency(const InstrItineraryData *ItinData,
4054 const MachineInstr *DefMI, unsigned DefIdx) const {
4055 if (!ItinData || ItinData->isEmpty())
4056 return false;
4057
4058 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
4059 if (DDomain == ARMII::DomainGeneral) {
4060 unsigned DefClass = DefMI->getDesc().getSchedClass();
4061 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4062 return (DefCycle != -1 && DefCycle <= 2);
4063 }
4064 return false;
4065}
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004066
Andrew Trick924123a2011-09-21 02:20:46 +00004067bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr *MI,
4068 StringRef &ErrInfo) const {
4069 if (convertAddSubFlagsOpcode(MI->getOpcode())) {
4070 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4071 return false;
4072 }
4073 return true;
4074}
4075
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004076// LoadStackGuard has so far only been implemented for MachO. Different code
4077// sequence is needed for other targets.
4078void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4079 unsigned LoadImmOpc,
4080 unsigned LoadOpc,
4081 Reloc::Model RM) const {
4082 MachineBasicBlock &MBB = *MI->getParent();
4083 DebugLoc DL = MI->getDebugLoc();
4084 unsigned Reg = MI->getOperand(0).getReg();
4085 const GlobalValue *GV =
4086 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4087 MachineInstrBuilder MIB;
4088
4089 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4090 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4091
4092 if (Subtarget.GVIsIndirectSymbol(GV, RM)) {
4093 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4094 MIB.addReg(Reg, RegState::Kill).addImm(0);
4095 unsigned Flag = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant;
4096 MachineMemOperand *MMO = MBB.getParent()->
4097 getMachineMemOperand(MachinePointerInfo::getGOT(), Flag, 4, 4);
4098 MIB.addMemOperand(MMO);
4099 AddDefaultPred(MIB);
4100 }
4101
4102 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4103 MIB.addReg(Reg, RegState::Kill).addImm(0);
4104 MIB.setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
4105 AddDefaultPred(MIB);
4106}
4107
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004108bool
4109ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4110 unsigned &AddSubOpc,
4111 bool &NegAcc, bool &HasLane) const {
4112 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4113 if (I == MLxEntryMap.end())
4114 return false;
4115
4116 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4117 MulOpc = Entry.MulOpc;
4118 AddSubOpc = Entry.AddSubOpc;
4119 NegAcc = Entry.NegAcc;
4120 HasLane = Entry.HasLane;
4121 return true;
4122}
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004123
4124//===----------------------------------------------------------------------===//
4125// Execution domains.
4126//===----------------------------------------------------------------------===//
4127//
4128// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4129// and some can go down both. The vmov instructions go down the VFP pipeline,
4130// but they can be changed to vorr equivalents that are executed by the NEON
4131// pipeline.
4132//
4133// We use the following execution domain numbering:
4134//
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004135enum ARMExeDomain {
4136 ExeGeneric = 0,
4137 ExeVFP = 1,
4138 ExeNEON = 2
4139};
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004140//
4141// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4142//
4143std::pair<uint16_t, uint16_t>
4144ARMBaseInstrInfo::getExecutionDomain(const MachineInstr *MI) const {
Eric Christopher7e70aba2015-03-07 00:12:22 +00004145 // If we don't have access to NEON instructions then we won't be able
4146 // to swizzle anything to the NEON domain. Check to make sure.
4147 if (Subtarget.hasNEON()) {
4148 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4149 // if they are not predicated.
4150 if (MI->getOpcode() == ARM::VMOVD && !isPredicated(MI))
4151 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004152
Eric Christopher7e70aba2015-03-07 00:12:22 +00004153 // CortexA9 is particularly picky about mixing the two and wants these
4154 // converted.
4155 if (Subtarget.isCortexA9() && !isPredicated(MI) &&
4156 (MI->getOpcode() == ARM::VMOVRS || MI->getOpcode() == ARM::VMOVSR ||
4157 MI->getOpcode() == ARM::VMOVS))
4158 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4159 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004160 // No other instructions can be swizzled, so just determine their domain.
4161 unsigned Domain = MI->getDesc().TSFlags & ARMII::DomainMask;
4162
4163 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004164 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004165
4166 // Certain instructions can go either way on Cortex-A8.
4167 // Treat them as NEON instructions.
4168 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004169 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004170
4171 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004172 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004173
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004174 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004175}
4176
Tim Northover771f1602012-08-29 16:36:07 +00004177static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4178 unsigned SReg, unsigned &Lane) {
4179 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4180 Lane = 0;
4181
4182 if (DReg != ARM::NoRegister)
4183 return DReg;
4184
4185 Lane = 1;
4186 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4187
4188 assert(DReg && "S-register with no D super-register?");
4189 return DReg;
4190}
4191
Andrew Trickd9296ec2012-10-10 05:43:01 +00004192/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloyea052562012-09-18 08:31:15 +00004193/// set ImplicitSReg to a register number that must be marked as implicit-use or
4194/// zero if no register needs to be defined as implicit-use.
4195///
4196/// If the function cannot determine if an SPR should be marked implicit use or
4197/// not, it returns false.
4198///
4199/// This function handles cases where an instruction is being modified from taking
Andrew Trickd9296ec2012-10-10 05:43:01 +00004200/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloyea052562012-09-18 08:31:15 +00004201/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4202/// lane of the DPR).
4203///
4204/// If the other SPR is defined, an implicit-use of it should be added. Else,
4205/// (including the case where the DPR itself is defined), it should not.
Andrew Trickd9296ec2012-10-10 05:43:01 +00004206///
James Molloyea052562012-09-18 08:31:15 +00004207static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
4208 MachineInstr *MI,
4209 unsigned DReg, unsigned Lane,
4210 unsigned &ImplicitSReg) {
4211 // If the DPR is defined or used already, the other SPR lane will be chained
4212 // correctly, so there is nothing to be done.
4213 if (MI->definesRegister(DReg, TRI) || MI->readsRegister(DReg, TRI)) {
4214 ImplicitSReg = 0;
4215 return true;
4216 }
4217
4218 // Otherwise we need to go searching to see if the SPR is set explicitly.
4219 ImplicitSReg = TRI->getSubReg(DReg,
4220 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4221 MachineBasicBlock::LivenessQueryResult LQR =
4222 MI->getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
4223
4224 if (LQR == MachineBasicBlock::LQR_Live)
4225 return true;
4226 else if (LQR == MachineBasicBlock::LQR_Unknown)
4227 return false;
4228
4229 // If the register is known not to be live, there is no need to add an
4230 // implicit-use.
4231 ImplicitSReg = 0;
4232 return true;
4233}
Tim Northover771f1602012-08-29 16:36:07 +00004234
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004235void
4236ARMBaseInstrInfo::setExecutionDomain(MachineInstr *MI, unsigned Domain) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004237 unsigned DstReg, SrcReg, DReg;
4238 unsigned Lane;
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00004239 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Tim Northoverf6618152012-08-17 11:32:52 +00004240 const TargetRegisterInfo *TRI = &getRegisterInfo();
Tim Northoverf6618152012-08-17 11:32:52 +00004241 switch (MI->getOpcode()) {
4242 default:
4243 llvm_unreachable("cannot handle opcode!");
4244 break;
4245 case ARM::VMOVD:
4246 if (Domain != ExeNEON)
4247 break;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004248
Tim Northoverf6618152012-08-17 11:32:52 +00004249 // Zap the predicate operands.
4250 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004251
Eric Christopher7e70aba2015-03-07 00:12:22 +00004252 // Make sure we've got NEON instructions.
4253 assert(Subtarget.hasNEON() && "VORRd requires NEON");
4254
Tim Northover771f1602012-08-29 16:36:07 +00004255 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4256 DstReg = MI->getOperand(0).getReg();
4257 SrcReg = MI->getOperand(1).getReg();
4258
4259 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4260 MI->RemoveOperand(i-1);
4261
4262 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004263 MI->setDesc(get(ARM::VORRd));
Tim Northover771f1602012-08-29 16:36:07 +00004264 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4265 .addReg(SrcReg)
4266 .addReg(SrcReg));
Tim Northoverf6618152012-08-17 11:32:52 +00004267 break;
4268 case ARM::VMOVRS:
4269 if (Domain != ExeNEON)
4270 break;
4271 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
4272
Tim Northover771f1602012-08-29 16:36:07 +00004273 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004274 DstReg = MI->getOperand(0).getReg();
4275 SrcReg = MI->getOperand(1).getReg();
4276
Tim Northover771f1602012-08-29 16:36:07 +00004277 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4278 MI->RemoveOperand(i-1);
Tim Northoverf6618152012-08-17 11:32:52 +00004279
Tim Northover771f1602012-08-29 16:36:07 +00004280 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northoverf6618152012-08-17 11:32:52 +00004281
Tim Northover771f1602012-08-29 16:36:07 +00004282 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4283 // Note that DSrc has been widened and the other lane may be undef, which
4284 // contaminates the entire register.
Tim Northoverf6618152012-08-17 11:32:52 +00004285 MI->setDesc(get(ARM::VGETLNi32));
Tim Northover771f1602012-08-29 16:36:07 +00004286 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4287 .addReg(DReg, RegState::Undef)
4288 .addImm(Lane));
Tim Northoverf6618152012-08-17 11:32:52 +00004289
Tim Northover771f1602012-08-29 16:36:07 +00004290 // The old source should be an implicit use, otherwise we might think it
4291 // was dead before here.
Tim Northoverf6618152012-08-17 11:32:52 +00004292 MIB.addReg(SrcReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004293 break;
James Molloyea052562012-09-18 08:31:15 +00004294 case ARM::VMOVSR: {
Tim Northoverf6618152012-08-17 11:32:52 +00004295 if (Domain != ExeNEON)
4296 break;
4297 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
4298
Tim Northover771f1602012-08-29 16:36:07 +00004299 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004300 DstReg = MI->getOperand(0).getReg();
4301 SrcReg = MI->getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004302
Tim Northover771f1602012-08-29 16:36:07 +00004303 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
4304
James Molloyea052562012-09-18 08:31:15 +00004305 unsigned ImplicitSReg;
4306 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
4307 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004308
Tim Northoverc8d867d2012-09-05 18:37:53 +00004309 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4310 MI->RemoveOperand(i-1);
4311
Tim Northover771f1602012-08-29 16:36:07 +00004312 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4313 // Again DDst may be undefined at the beginning of this instruction.
Tim Northoverf6618152012-08-17 11:32:52 +00004314 MI->setDesc(get(ARM::VSETLNi32));
Tim Northover726d32c2012-09-01 18:07:29 +00004315 MIB.addReg(DReg, RegState::Define)
4316 .addReg(DReg, getUndefRegState(!MI->readsRegister(DReg, TRI)))
4317 .addReg(SrcReg)
4318 .addImm(Lane);
4319 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004320
Tim Northover726d32c2012-09-01 18:07:29 +00004321 // The narrower destination must be marked as set to keep previous chains
4322 // in place.
Tim Northover771f1602012-08-29 16:36:07 +00004323 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004324 if (ImplicitSReg != 0)
4325 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004326 break;
James Molloyea052562012-09-18 08:31:15 +00004327 }
Tim Northoverca9f3842012-08-30 10:17:45 +00004328 case ARM::VMOVS: {
4329 if (Domain != ExeNEON)
4330 break;
4331
4332 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
4333 DstReg = MI->getOperand(0).getReg();
4334 SrcReg = MI->getOperand(1).getReg();
4335
Tim Northoverca9f3842012-08-30 10:17:45 +00004336 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4337 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4338 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4339
James Molloyea052562012-09-18 08:31:15 +00004340 unsigned ImplicitSReg;
4341 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4342 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004343
Tim Northoverc8d867d2012-09-05 18:37:53 +00004344 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4345 MI->RemoveOperand(i-1);
4346
Tim Northoverca9f3842012-08-30 10:17:45 +00004347 if (DSrc == DDst) {
4348 // Destination can be:
4349 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
4350 MI->setDesc(get(ARM::VDUPLN32d));
Tim Northover726d32c2012-09-01 18:07:29 +00004351 MIB.addReg(DDst, RegState::Define)
4352 .addReg(DDst, getUndefRegState(!MI->readsRegister(DDst, TRI)))
4353 .addImm(SrcLane);
4354 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004355
4356 // Neither the source or the destination are naturally represented any
4357 // more, so add them in manually.
4358 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4359 MIB.addReg(SrcReg, RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004360 if (ImplicitSReg != 0)
4361 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004362 break;
4363 }
4364
4365 // In general there's no single instruction that can perform an S <-> S
4366 // move in NEON space, but a pair of VEXT instructions *can* do the
4367 // job. It turns out that the VEXTs needed will only use DSrc once, with
4368 // the position based purely on the combination of lane-0 and lane-1
4369 // involved. For example
4370 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4371 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4372 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4373 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4374 //
4375 // Pattern of the MachineInstrs is:
4376 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4377 MachineInstrBuilder NewMIB;
4378 NewMIB = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4379 get(ARM::VEXTd32), DDst);
Tim Northover726d32c2012-09-01 18:07:29 +00004380
4381 // On the first instruction, both DSrc and DDst may be <undef> if present.
4382 // Specifically when the original instruction didn't have them as an
4383 // <imp-use>.
4384 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
4385 bool CurUndef = !MI->readsRegister(CurReg, TRI);
4386 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4387
4388 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
4389 CurUndef = !MI->readsRegister(CurReg, TRI);
4390 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4391
Tim Northoverca9f3842012-08-30 10:17:45 +00004392 NewMIB.addImm(1);
4393 AddDefaultPred(NewMIB);
4394
4395 if (SrcLane == DstLane)
4396 NewMIB.addReg(SrcReg, RegState::Implicit);
4397
4398 MI->setDesc(get(ARM::VEXTd32));
4399 MIB.addReg(DDst, RegState::Define);
Tim Northover726d32c2012-09-01 18:07:29 +00004400
4401 // On the second instruction, DDst has definitely been defined above, so
4402 // it is not <undef>. DSrc, if present, can be <undef> as above.
4403 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
4404 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4405 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4406
4407 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
4408 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4409 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4410
Tim Northoverca9f3842012-08-30 10:17:45 +00004411 MIB.addImm(1);
4412 AddDefaultPred(MIB);
4413
4414 if (SrcLane != DstLane)
4415 MIB.addReg(SrcReg, RegState::Implicit);
4416
4417 // As before, the original destination is no longer represented, add it
4418 // implicitly.
4419 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004420 if (ImplicitSReg != 0)
4421 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004422 break;
4423 }
Tim Northoverf6618152012-08-17 11:32:52 +00004424 }
4425
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004426}
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004427
Bob Wilsone8a549c2012-09-29 21:43:49 +00004428//===----------------------------------------------------------------------===//
4429// Partial register updates
4430//===----------------------------------------------------------------------===//
4431//
4432// Swift renames NEON registers with 64-bit granularity. That means any
4433// instruction writing an S-reg implicitly reads the containing D-reg. The
4434// problem is mostly avoided by translating f32 operations to v2f32 operations
4435// on D-registers, but f32 loads are still a problem.
4436//
4437// These instructions can load an f32 into a NEON register:
4438//
4439// VLDRS - Only writes S, partial D update.
4440// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4441// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4442//
4443// FCONSTD can be used as a dependency-breaking instruction.
Bob Wilsone8a549c2012-09-29 21:43:49 +00004444unsigned ARMBaseInstrInfo::
4445getPartialRegUpdateClearance(const MachineInstr *MI,
4446 unsigned OpNum,
4447 const TargetRegisterInfo *TRI) const {
Silviu Barangadc453362013-03-27 12:38:44 +00004448 if (!SwiftPartialUpdateClearance ||
4449 !(Subtarget.isSwift() || Subtarget.isCortexA15()))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004450 return 0;
4451
4452 assert(TRI && "Need TRI instance");
4453
4454 const MachineOperand &MO = MI->getOperand(OpNum);
4455 if (MO.readsReg())
4456 return 0;
4457 unsigned Reg = MO.getReg();
4458 int UseOp = -1;
4459
4460 switch(MI->getOpcode()) {
4461 // Normal instructions writing only an S-register.
4462 case ARM::VLDRS:
4463 case ARM::FCONSTS:
4464 case ARM::VMOVSR:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004465 case ARM::VMOVv8i8:
4466 case ARM::VMOVv4i16:
4467 case ARM::VMOVv2i32:
4468 case ARM::VMOVv2f32:
4469 case ARM::VMOVv1i64:
4470 UseOp = MI->findRegisterUseOperandIdx(Reg, false, TRI);
4471 break;
4472
4473 // Explicitly reads the dependency.
4474 case ARM::VLD1LNd32:
Silviu Barangadc453362013-03-27 12:38:44 +00004475 UseOp = 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004476 break;
4477 default:
4478 return 0;
4479 }
4480
4481 // If this instruction actually reads a value from Reg, there is no unwanted
4482 // dependency.
4483 if (UseOp != -1 && MI->getOperand(UseOp).readsReg())
4484 return 0;
4485
4486 // We must be able to clobber the whole D-reg.
4487 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4488 // Virtual register must be a foo:ssub_0<def,undef> operand.
4489 if (!MO.getSubReg() || MI->readsVirtualRegister(Reg))
4490 return 0;
4491 } else if (ARM::SPRRegClass.contains(Reg)) {
4492 // Physical register: MI must define the full D-reg.
4493 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4494 &ARM::DPRRegClass);
4495 if (!DReg || !MI->definesRegister(DReg, TRI))
4496 return 0;
4497 }
4498
4499 // MI has an unwanted D-register dependency.
4500 // Avoid defs in the previous N instructrions.
4501 return SwiftPartialUpdateClearance;
4502}
4503
4504// Break a partial register dependency after getPartialRegUpdateClearance
4505// returned non-zero.
4506void ARMBaseInstrInfo::
4507breakPartialRegDependency(MachineBasicBlock::iterator MI,
4508 unsigned OpNum,
4509 const TargetRegisterInfo *TRI) const {
4510 assert(MI && OpNum < MI->getDesc().getNumDefs() && "OpNum is not a def");
4511 assert(TRI && "Need TRI instance");
4512
4513 const MachineOperand &MO = MI->getOperand(OpNum);
4514 unsigned Reg = MO.getReg();
4515 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4516 "Can't break virtual register dependencies.");
4517 unsigned DReg = Reg;
4518
4519 // If MI defines an S-reg, find the corresponding D super-register.
4520 if (ARM::SPRRegClass.contains(Reg)) {
4521 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4522 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4523 }
4524
4525 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
4526 assert(MI->definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
4527
4528 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4529 // the full D-register by loading the same value to both lanes. The
4530 // instruction is micro-coded with 2 uops, so don't do this until we can
Robert Wilhelm516be562013-09-14 09:34:24 +00004531 // properly schedule micro-coded instructions. The dispatcher stalls cause
Bob Wilsone8a549c2012-09-29 21:43:49 +00004532 // too big regressions.
4533
4534 // Insert the dependency-breaking FCONSTD before MI.
4535 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
4536 AddDefaultPred(BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4537 get(ARM::FCONSTD), DReg).addImm(96));
4538 MI->addRegisterKilled(DReg, TRI, true);
4539}
4540
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004541bool ARMBaseInstrInfo::hasNOP() const {
Michael Kupersteinc3434b32015-05-13 10:28:46 +00004542 return (Subtarget.getFeatureBits() & ARM::HasV6KOps) != 0;
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004543}
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004544
4545bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
Arnold Schwaighofere9375922013-06-05 14:59:36 +00004546 if (MI->getNumOperands() < 4)
4547 return true;
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004548 unsigned ShOpVal = MI->getOperand(3).getImm();
4549 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4550 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4551 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4552 ((ShImm == 1 || ShImm == 2) &&
4553 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
4554 return true;
4555
4556 return false;
4557}
Quentin Colombetd358e842014-08-22 18:05:22 +00004558
4559bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
4560 const MachineInstr &MI, unsigned DefIdx,
4561 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
4562 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4563 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
4564
4565 switch (MI.getOpcode()) {
4566 case ARM::VMOVDRR:
4567 // dX = VMOVDRR rY, rZ
4568 // is the same as:
4569 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
4570 // Populate the InputRegs accordingly.
4571 // rY
4572 const MachineOperand *MOReg = &MI.getOperand(1);
4573 InputRegs.push_back(
4574 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0));
4575 // rZ
4576 MOReg = &MI.getOperand(2);
4577 InputRegs.push_back(
4578 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1));
4579 return true;
4580 }
4581 llvm_unreachable("Target dependent opcode missing");
4582}
4583
4584bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
4585 const MachineInstr &MI, unsigned DefIdx,
4586 RegSubRegPairAndIdx &InputReg) const {
4587 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4588 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
4589
4590 switch (MI.getOpcode()) {
4591 case ARM::VMOVRRD:
4592 // rX, rY = VMOVRRD dZ
4593 // is the same as:
4594 // rX = EXTRACT_SUBREG dZ, ssub_0
4595 // rY = EXTRACT_SUBREG dZ, ssub_1
4596 const MachineOperand &MOReg = MI.getOperand(2);
4597 InputReg.Reg = MOReg.getReg();
4598 InputReg.SubReg = MOReg.getSubReg();
4599 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
4600 return true;
4601 }
4602 llvm_unreachable("Target dependent opcode missing");
4603}
4604
4605bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
4606 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
4607 RegSubRegPairAndIdx &InsertedReg) const {
4608 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4609 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
4610
4611 switch (MI.getOpcode()) {
4612 case ARM::VSETLNi32:
4613 // dX = VSETLNi32 dY, rZ, imm
4614 const MachineOperand &MOBaseReg = MI.getOperand(1);
4615 const MachineOperand &MOInsertedReg = MI.getOperand(2);
4616 const MachineOperand &MOIndex = MI.getOperand(3);
4617 BaseReg.Reg = MOBaseReg.getReg();
4618 BaseReg.SubReg = MOBaseReg.getSubReg();
4619
4620 InsertedReg.Reg = MOInsertedReg.getReg();
4621 InsertedReg.SubReg = MOInsertedReg.getSubReg();
4622 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
4623 return true;
4624 }
4625 llvm_unreachable("Target dependent opcode missing");
4626}