blob: 8ea87f9930c034ba845dfc77475551bce14f95b3 [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"
Evan Cheng1e210d02011-06-28 20:07:07 +000040
David Goodwinaf7451b2009-07-08 16:09:28 +000041using namespace llvm;
42
Chandler Carruthe96dd892014-04-21 22:55:11 +000043#define DEBUG_TYPE "arm-instrinfo"
44
Chandler Carruthd174b722014-04-22 02:03:14 +000045#define GET_INSTRINFO_CTOR_DTOR
46#include "ARMGenInstrInfo.inc"
47
David Goodwinaf7451b2009-07-08 16:09:28 +000048static cl::opt<bool>
49EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
50 cl::desc("Enable ARM 2-addr to 3-addr conv"));
51
Jakob Stoklund Olesencd893392011-08-31 17:00:02 +000052static cl::opt<bool>
Jakob Stoklund Olesen653183f2011-11-15 23:53:18 +000053WidenVMOVS("widen-vmovs", cl::Hidden, cl::init(true),
Jakob Stoklund Olesencd893392011-08-31 17:00:02 +000054 cl::desc("Widen ARM vmovs to vmovd when possible"));
55
Bob Wilsone8a549c2012-09-29 21:43:49 +000056static cl::opt<unsigned>
57SwiftPartialUpdateClearance("swift-partial-update-clearance",
58 cl::Hidden, cl::init(12),
59 cl::desc("Clearance before partial register updates"));
60
Evan Cheng62c7b5b2010-12-05 22:04:16 +000061/// ARM_MLxEntry - Record information about MLA / MLS instructions.
62struct ARM_MLxEntry {
Craig Topper2fbd1302012-05-24 03:59:11 +000063 uint16_t MLxOpc; // MLA / MLS opcode
64 uint16_t MulOpc; // Expanded multiplication opcode
65 uint16_t AddSubOpc; // Expanded add / sub opcode
Evan Cheng62c7b5b2010-12-05 22:04:16 +000066 bool NegAcc; // True if the acc is negated before the add / sub.
67 bool HasLane; // True if instruction has an extra "lane" operand.
68};
69
70static const ARM_MLxEntry ARM_MLxTable[] = {
71 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
72 // fp scalar ops
73 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
74 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
75 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
76 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
Evan Cheng62c7b5b2010-12-05 22:04:16 +000077 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
78 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
79 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
80 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
81
82 // fp SIMD ops
83 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
84 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
85 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
86 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
87 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
88 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
89 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
90 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
91};
92
Anton Korobeynikov14635da2009-11-02 00:10:38 +000093ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
Evan Cheng703a0fb2011-07-01 17:57:27 +000094 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
Anton Korobeynikov14635da2009-11-02 00:10:38 +000095 Subtarget(STI) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +000096 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
97 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
98 assert(false && "Duplicated entries?");
99 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
100 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
101 }
102}
103
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000104// Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
105// currently defaults to no prepass hazard recognizer.
Eric Christopherf047bfd2014-06-13 22:38:52 +0000106ScheduleHazardRecognizer *
107ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
108 const ScheduleDAG *DAG) const {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000109 if (usePreRAHazardRecognizer()) {
Eric Christopherf047bfd2014-06-13 22:38:52 +0000110 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +0000111 static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000112 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
113 }
Eric Christopherf047bfd2014-06-13 22:38:52 +0000114 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000115}
116
117ScheduleHazardRecognizer *ARMBaseInstrInfo::
118CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
119 const ScheduleDAG *DAG) const {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000120 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
Bill Wendlingf95178e2013-06-07 05:54:19 +0000121 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000122 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
David Goodwinaf7451b2009-07-08 16:09:28 +0000123}
124
125MachineInstr *
126ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
127 MachineBasicBlock::iterator &MBBI,
128 LiveVariables *LV) const {
Evan Cheng0e075e22009-07-27 18:44:00 +0000129 // FIXME: Thumb2 support.
130
David Goodwinaf7451b2009-07-08 16:09:28 +0000131 if (!EnableARM3Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +0000132 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000133
134 MachineInstr *MI = MBBI;
135 MachineFunction &MF = *MI->getParent()->getParent();
Bruno Cardoso Lopesc2f87b72010-06-08 22:51:23 +0000136 uint64_t TSFlags = MI->getDesc().TSFlags;
David Goodwinaf7451b2009-07-08 16:09:28 +0000137 bool isPre = false;
138 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000139 default: return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000140 case ARMII::IndexModePre:
141 isPre = true;
142 break;
143 case ARMII::IndexModePost:
144 break;
145 }
146
147 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
148 // operation.
149 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
150 if (MemOpc == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000151 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000152
Craig Topper062a2ba2014-04-25 05:30:21 +0000153 MachineInstr *UpdateMI = nullptr;
154 MachineInstr *MemMI = nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000155 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000156 const MCInstrDesc &MCID = MI->getDesc();
157 unsigned NumOps = MCID.getNumOperands();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000158 bool isLoad = !MI->mayStore();
David Goodwinaf7451b2009-07-08 16:09:28 +0000159 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
160 const MachineOperand &Base = MI->getOperand(2);
161 const MachineOperand &Offset = MI->getOperand(NumOps-3);
162 unsigned WBReg = WB.getReg();
163 unsigned BaseReg = Base.getReg();
164 unsigned OffReg = Offset.getReg();
165 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
166 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
167 switch (AddrMode) {
Craig Toppere55c5562012-02-07 02:50:20 +0000168 default: llvm_unreachable("Unknown indexed op!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000169 case ARMII::AddrMode2: {
170 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
171 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
172 if (OffReg == 0) {
Evan Chenge3a53c42009-07-08 21:03:57 +0000173 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwinaf7451b2009-07-08 16:09:28 +0000174 // Can't encode it in a so_imm operand. This transformation will
175 // add more than 1 instruction. Abandon!
Craig Topper062a2ba2014-04-25 05:30:21 +0000176 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000177 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000178 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Evan Chenge3a53c42009-07-08 21:03:57 +0000179 .addReg(BaseReg).addImm(Amt)
David Goodwinaf7451b2009-07-08 16:09:28 +0000180 .addImm(Pred).addReg(0).addReg(0);
181 } else if (Amt != 0) {
182 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
183 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
184 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Owen Andersonb595ed02011-07-21 18:54:16 +0000185 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000186 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
187 .addImm(Pred).addReg(0).addReg(0);
188 } else
189 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000190 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000191 .addReg(BaseReg).addReg(OffReg)
192 .addImm(Pred).addReg(0).addReg(0);
193 break;
194 }
195 case ARMII::AddrMode3 : {
196 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
197 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
198 if (OffReg == 0)
199 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
200 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000201 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000202 .addReg(BaseReg).addImm(Amt)
203 .addImm(Pred).addReg(0).addReg(0);
204 else
205 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000206 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000207 .addReg(BaseReg).addReg(OffReg)
208 .addImm(Pred).addReg(0).addReg(0);
209 break;
210 }
211 }
212
213 std::vector<MachineInstr*> NewMIs;
214 if (isPre) {
215 if (isLoad)
216 MemMI = BuildMI(MF, MI->getDebugLoc(),
217 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000218 .addReg(WBReg).addImm(0).addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000219 else
220 MemMI = BuildMI(MF, MI->getDebugLoc(),
221 get(MemOpc)).addReg(MI->getOperand(1).getReg())
222 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
223 NewMIs.push_back(MemMI);
224 NewMIs.push_back(UpdateMI);
225 } else {
226 if (isLoad)
227 MemMI = BuildMI(MF, MI->getDebugLoc(),
228 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000229 .addReg(BaseReg).addImm(0).addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000230 else
231 MemMI = BuildMI(MF, MI->getDebugLoc(),
232 get(MemOpc)).addReg(MI->getOperand(1).getReg())
233 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
234 if (WB.isDead())
235 UpdateMI->getOperand(0).setIsDead();
236 NewMIs.push_back(UpdateMI);
237 NewMIs.push_back(MemMI);
238 }
239
240 // Transfer LiveVariables states, kill / dead info.
241 if (LV) {
242 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
243 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000244 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000245 unsigned Reg = MO.getReg();
246
247 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
248 if (MO.isDef()) {
249 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
250 if (MO.isDead())
251 LV->addVirtualRegisterDead(Reg, NewMI);
252 }
253 if (MO.isUse() && MO.isKill()) {
254 for (unsigned j = 0; j < 2; ++j) {
255 // Look at the two new MI's in reverse order.
256 MachineInstr *NewMI = NewMIs[j];
257 if (!NewMI->readsRegister(Reg))
258 continue;
259 LV->addVirtualRegisterKilled(Reg, NewMI);
260 if (VI.removeKill(MI))
261 VI.Kills.push_back(NewMI);
262 break;
263 }
264 }
265 }
266 }
267 }
268
269 MFI->insert(MBBI, NewMIs[1]);
270 MFI->insert(MBBI, NewMIs[0]);
271 return NewMIs[0];
272}
273
274// Branch analysis.
275bool
276ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
277 MachineBasicBlock *&FBB,
278 SmallVectorImpl<MachineOperand> &Cond,
279 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000280 TBB = nullptr;
281 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000282
David Goodwinaf7451b2009-07-08 16:09:28 +0000283 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000284 if (I == MBB.begin())
Lang Hames24864fe2013-07-19 23:52:47 +0000285 return false; // Empty blocks are easy.
Dale Johannesen4244d122010-04-02 01:38:09 +0000286 --I;
Lang Hames24864fe2013-07-19 23:52:47 +0000287
288 // Walk backwards from the end of the basic block until the branch is
289 // analyzed or we give up.
Lang Hames18c98a52013-12-20 20:27:51 +0000290 while (isPredicated(I) || I->isTerminator() || I->isDebugValue()) {
Lang Hames24864fe2013-07-19 23:52:47 +0000291
292 // Flag to be raised on unanalyzeable instructions. This is useful in cases
293 // where we want to clean up on the end of the basic block before we bail
294 // out.
295 bool CantAnalyze = false;
296
297 // Skip over DEBUG values and predicated nonterminators.
298 while (I->isDebugValue() || !I->isTerminator()) {
299 if (I == MBB.begin())
300 return false;
301 --I;
302 }
303
304 if (isIndirectBranchOpcode(I->getOpcode()) ||
305 isJumpTableBranchOpcode(I->getOpcode())) {
306 // Indirect branches and jump tables can't be analyzed, but we still want
307 // to clean up any instructions at the tail of the basic block.
308 CantAnalyze = true;
309 } else if (isUncondBranchOpcode(I->getOpcode())) {
310 TBB = I->getOperand(0).getMBB();
311 } else if (isCondBranchOpcode(I->getOpcode())) {
312 // Bail out if we encounter multiple conditional branches.
313 if (!Cond.empty())
314 return true;
315
316 assert(!FBB && "FBB should have been null.");
317 FBB = TBB;
318 TBB = I->getOperand(0).getMBB();
319 Cond.push_back(I->getOperand(1));
320 Cond.push_back(I->getOperand(2));
321 } else if (I->isReturn()) {
322 // Returns can't be analyzed, but we should run cleanup.
323 CantAnalyze = !isPredicated(I);
324 } else {
325 // We encountered other unrecognized terminator. Bail out immediately.
326 return true;
327 }
328
329 // Cleanup code - to be run for unpredicated unconditional branches and
330 // returns.
331 if (!isPredicated(I) &&
332 (isUncondBranchOpcode(I->getOpcode()) ||
333 isIndirectBranchOpcode(I->getOpcode()) ||
334 isJumpTableBranchOpcode(I->getOpcode()) ||
335 I->isReturn())) {
336 // Forget any previous condition branch information - it no longer applies.
337 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000338 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000339
340 // If we can modify the function, delete everything below this
341 // unconditional branch.
342 if (AllowModify) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000343 MachineBasicBlock::iterator DI = std::next(I);
Lang Hames24864fe2013-07-19 23:52:47 +0000344 while (DI != MBB.end()) {
345 MachineInstr *InstToDelete = DI;
346 ++DI;
347 InstToDelete->eraseFromParent();
348 }
349 }
350 }
351
352 if (CantAnalyze)
353 return true;
354
Dale Johannesen4244d122010-04-02 01:38:09 +0000355 if (I == MBB.begin())
356 return false;
Lang Hames24864fe2013-07-19 23:52:47 +0000357
Dale Johannesen4244d122010-04-02 01:38:09 +0000358 --I;
359 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000360
Lang Hames24864fe2013-07-19 23:52:47 +0000361 // We made it past the terminators without bailing out - we must have
362 // analyzed this branch successfully.
363 return false;
David Goodwinaf7451b2009-07-08 16:09:28 +0000364}
365
366
367unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000368 MachineBasicBlock::iterator I = MBB.end();
369 if (I == MBB.begin()) return 0;
370 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000371 while (I->isDebugValue()) {
372 if (I == MBB.begin())
373 return 0;
374 --I;
375 }
Evan Cheng056c6692009-07-27 18:20:05 +0000376 if (!isUncondBranchOpcode(I->getOpcode()) &&
377 !isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000378 return 0;
379
380 // Remove the branch.
381 I->eraseFromParent();
382
383 I = MBB.end();
384
385 if (I == MBB.begin()) return 1;
386 --I;
Evan Cheng056c6692009-07-27 18:20:05 +0000387 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000388 return 1;
389
390 // Remove the branch.
391 I->eraseFromParent();
392 return 2;
393}
394
395unsigned
396ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000397 MachineBasicBlock *FBB,
398 const SmallVectorImpl<MachineOperand> &Cond,
399 DebugLoc DL) const {
Evan Cheng780748d2009-07-28 05:48:47 +0000400 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
401 int BOpc = !AFI->isThumbFunction()
402 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
403 int BccOpc = !AFI->isThumbFunction()
404 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000405 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
Andrew Trick3f1fdf12011-09-21 02:17:37 +0000406
David Goodwinaf7451b2009-07-08 16:09:28 +0000407 // Shouldn't be a fall through.
408 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
409 assert((Cond.size() == 2 || Cond.size() == 0) &&
410 "ARM branch conditions have two components!");
411
Craig Topper062a2ba2014-04-25 05:30:21 +0000412 if (!FBB) {
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000413 if (Cond.empty()) { // Unconditional branch?
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000414 if (isThumb)
415 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).addImm(ARMCC::AL).addReg(0);
416 else
417 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000418 } else
Stuart Hastings0125b642010-06-17 22:43:56 +0000419 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
David Goodwinaf7451b2009-07-08 16:09:28 +0000420 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
421 return 1;
422 }
423
424 // Two-way conditional branch.
Stuart Hastings0125b642010-06-17 22:43:56 +0000425 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
David Goodwinaf7451b2009-07-08 16:09:28 +0000426 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000427 if (isThumb)
428 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).addImm(ARMCC::AL).addReg(0);
429 else
430 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwinaf7451b2009-07-08 16:09:28 +0000431 return 2;
432}
433
434bool ARMBaseInstrInfo::
435ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
436 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
437 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
438 return false;
439}
440
Evan Cheng7fae11b2011-12-14 02:11:42 +0000441bool ARMBaseInstrInfo::isPredicated(const MachineInstr *MI) const {
442 if (MI->isBundle()) {
443 MachineBasicBlock::const_instr_iterator I = MI;
444 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
445 while (++I != E && I->isInsideBundle()) {
446 int PIdx = I->findFirstPredOperandIdx();
447 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
448 return true;
449 }
450 return false;
451 }
452
453 int PIdx = MI->findFirstPredOperandIdx();
454 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
455}
456
David Goodwinaf7451b2009-07-08 16:09:28 +0000457bool ARMBaseInstrInfo::
458PredicateInstruction(MachineInstr *MI,
459 const SmallVectorImpl<MachineOperand> &Pred) const {
460 unsigned Opc = MI->getOpcode();
Evan Cheng056c6692009-07-27 18:20:05 +0000461 if (isUncondBranchOpcode(Opc)) {
462 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +0000463 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
464 .addImm(Pred[0].getImm())
465 .addReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000466 return true;
467 }
468
469 int PIdx = MI->findFirstPredOperandIdx();
470 if (PIdx != -1) {
471 MachineOperand &PMO = MI->getOperand(PIdx);
472 PMO.setImm(Pred[0].getImm());
473 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
474 return true;
475 }
476 return false;
477}
478
479bool ARMBaseInstrInfo::
480SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
481 const SmallVectorImpl<MachineOperand> &Pred2) const {
482 if (Pred1.size() > 2 || Pred2.size() > 2)
483 return false;
484
485 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
486 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
487 if (CC1 == CC2)
488 return true;
489
490 switch (CC1) {
491 default:
492 return false;
493 case ARMCC::AL:
494 return true;
495 case ARMCC::HS:
496 return CC2 == ARMCC::HI;
497 case ARMCC::LS:
498 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
499 case ARMCC::GE:
500 return CC2 == ARMCC::GT;
501 case ARMCC::LE:
502 return CC2 == ARMCC::LT;
503 }
504}
505
506bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
507 std::vector<MachineOperand> &Pred) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000508 bool Found = false;
509 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
510 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +0000511 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
512 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000513 Pred.push_back(MO);
514 Found = true;
515 }
516 }
517
518 return Found;
519}
520
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000521static bool isCPSRDefined(const MachineInstr *MI) {
522 for (const auto &MO : MI->operands())
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000523 if (MO.isReg() && MO.getReg() == ARM::CPSR && MO.isDef())
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000524 return true;
525 return false;
526}
527
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000528static bool isEligibleForITBlock(const MachineInstr *MI) {
529 switch (MI->getOpcode()) {
530 default: return true;
531 case ARM::tADC: // ADC (register) T1
532 case ARM::tADDi3: // ADD (immediate) T1
533 case ARM::tADDi8: // ADD (immediate) T2
534 case ARM::tADDrr: // ADD (register) T1
535 case ARM::tAND: // AND (register) T1
536 case ARM::tASRri: // ASR (immediate) T1
537 case ARM::tASRrr: // ASR (register) T1
538 case ARM::tBIC: // BIC (register) T1
539 case ARM::tEOR: // EOR (register) T1
540 case ARM::tLSLri: // LSL (immediate) T1
541 case ARM::tLSLrr: // LSL (register) T1
542 case ARM::tLSRri: // LSR (immediate) T1
543 case ARM::tLSRrr: // LSR (register) T1
544 case ARM::tMUL: // MUL T1
545 case ARM::tMVN: // MVN (register) T1
546 case ARM::tORR: // ORR (register) T1
547 case ARM::tROR: // ROR (register) T1
548 case ARM::tRSB: // RSB (immediate) T1
549 case ARM::tSBC: // SBC (register) T1
550 case ARM::tSUBi3: // SUB (immediate) T1
551 case ARM::tSUBi8: // SUB (immediate) T2
552 case ARM::tSUBrr: // SUB (register) T1
553 return !isCPSRDefined(MI);
554 }
555}
556
Evan Chenga33fc862009-11-21 06:21:52 +0000557/// isPredicable - Return true if the specified instruction can be predicated.
558/// By default, this returns true for every instruction with a
559/// PredicateOperand.
560bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000561 if (!MI->isPredicable())
Evan Chenga33fc862009-11-21 06:21:52 +0000562 return false;
563
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000564 if (!isEligibleForITBlock(MI))
565 return false;
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000566
Joey Goulya5153cb2013-09-09 14:21:49 +0000567 ARMFunctionInfo *AFI =
568 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
569
570 if (AFI->isThumb2Function()) {
Weiming Zhao0da5cc02013-11-13 18:29:49 +0000571 if (getSubtarget().restrictIT())
Joey Goulya5153cb2013-09-09 14:21:49 +0000572 return isV8EligibleForIT(MI);
573 } else { // non-Thumb
574 if ((MI->getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
575 return false;
Evan Chenga33fc862009-11-21 06:21:52 +0000576 }
Joey Goulya5153cb2013-09-09 14:21:49 +0000577
Evan Chenga33fc862009-11-21 06:21:52 +0000578 return true;
579}
David Goodwinaf7451b2009-07-08 16:09:28 +0000580
Benjamin Kramer44a53da2014-04-12 18:45:24 +0000581namespace llvm {
582template <> bool IsCPSRDead<MachineInstr>(MachineInstr *MI) {
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000583 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
584 const MachineOperand &MO = MI->getOperand(i);
585 if (!MO.isReg() || MO.isUndef() || MO.isUse())
586 continue;
587 if (MO.getReg() != ARM::CPSR)
588 continue;
589 if (!MO.isDead())
590 return false;
591 }
592 // all definitions of CPSR are dead
593 return true;
594}
Benjamin Kramer44a53da2014-04-12 18:45:24 +0000595}
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000596
Chris Lattnerc831fac2009-12-03 06:58:32 +0000597/// FIXME: Works around a gcc miscompilation with -fstrict-aliasing.
Chandler Carruth82058c02010-10-23 08:40:19 +0000598LLVM_ATTRIBUTE_NOINLINE
David Goodwinaf7451b2009-07-08 16:09:28 +0000599static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
Chris Lattnerc831fac2009-12-03 06:58:32 +0000600 unsigned JTI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000601static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
602 unsigned JTI) {
Chris Lattnerc831fac2009-12-03 06:58:32 +0000603 assert(JTI < JT.size());
David Goodwinaf7451b2009-07-08 16:09:28 +0000604 return JT[JTI].MBBs.size();
605}
606
607/// GetInstSize - Return the size of the specified MachineInstr.
608///
609unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
610 const MachineBasicBlock &MBB = *MI->getParent();
611 const MachineFunction *MF = MBB.getParent();
Chris Lattnere9a75a62009-08-22 21:43:10 +0000612 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwinaf7451b2009-07-08 16:09:28 +0000613
Evan Cheng6cc775f2011-06-28 19:10:37 +0000614 const MCInstrDesc &MCID = MI->getDesc();
Owen Anderson651b2302011-07-13 23:22:26 +0000615 if (MCID.getSize())
616 return MCID.getSize();
David Goodwinaf7451b2009-07-08 16:09:28 +0000617
David Blaikie46a9f012012-01-20 21:51:11 +0000618 // If this machine instr is an inline asm, measure it.
619 if (MI->getOpcode() == ARM::INLINEASM)
620 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
David Blaikie46a9f012012-01-20 21:51:11 +0000621 unsigned Opc = MI->getOpcode();
622 switch (Opc) {
Rafael Espindolaafeb01c2014-03-07 04:45:03 +0000623 default:
624 // pseudo-instruction sizes are zero.
David Blaikie46a9f012012-01-20 21:51:11 +0000625 return 0;
626 case TargetOpcode::BUNDLE:
627 return getInstBundleLength(MI);
628 case ARM::MOVi16_ga_pcrel:
629 case ARM::MOVTi16_ga_pcrel:
630 case ARM::t2MOVi16_ga_pcrel:
631 case ARM::t2MOVTi16_ga_pcrel:
632 return 4;
633 case ARM::MOVi32imm:
634 case ARM::t2MOVi32imm:
635 return 8;
636 case ARM::CONSTPOOL_ENTRY:
637 // If this machine instr is a constant pool entry, its size is recorded as
638 // operand #2.
639 return MI->getOperand(2).getImm();
640 case ARM::Int_eh_sjlj_longjmp:
641 return 16;
642 case ARM::tInt_eh_sjlj_longjmp:
643 return 10;
644 case ARM::Int_eh_sjlj_setjmp:
645 case ARM::Int_eh_sjlj_setjmp_nofp:
646 return 20;
647 case ARM::tInt_eh_sjlj_setjmp:
648 case ARM::t2Int_eh_sjlj_setjmp:
649 case ARM::t2Int_eh_sjlj_setjmp_nofp:
650 return 12;
651 case ARM::BR_JTr:
652 case ARM::BR_JTm:
653 case ARM::BR_JTadd:
654 case ARM::tBR_JTr:
655 case ARM::t2BR_JT:
656 case ARM::t2TBB_JT:
657 case ARM::t2TBH_JT: {
658 // These are jumptable branches, i.e. a branch followed by an inlined
659 // jumptable. The size is 4 + 4 * number of entries. For TBB, each
660 // entry is one byte; TBH two byte each.
661 unsigned EntrySize = (Opc == ARM::t2TBB_JT)
662 ? 1 : ((Opc == ARM::t2TBH_JT) ? 2 : 4);
663 unsigned NumOps = MCID.getNumOperands();
664 MachineOperand JTOP =
665 MI->getOperand(NumOps - (MI->isPredicable() ? 3 : 2));
666 unsigned JTI = JTOP.getIndex();
667 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
Craig Toppere73658d2014-04-28 04:05:08 +0000668 assert(MJTI != nullptr);
David Blaikie46a9f012012-01-20 21:51:11 +0000669 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
670 assert(JTI < JT.size());
671 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
672 // 4 aligned. The assembler / linker may add 2 byte padding just before
673 // the JT entries. The size does not include this padding; the
674 // constant islands pass does separate bookkeeping for it.
675 // FIXME: If we know the size of the function is less than (1 << 16) *2
676 // bytes, we can use 16-bit entries instead. Then there won't be an
677 // alignment issue.
678 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
679 unsigned NumEntries = getNumJTEntries(JT, JTI);
680 if (Opc == ARM::t2TBB_JT && (NumEntries & 1))
681 // Make sure the instruction that follows TBB is 2-byte aligned.
682 // FIXME: Constant island pass should insert an "ALIGN" instruction
683 // instead.
684 ++NumEntries;
685 return NumEntries * EntrySize + InstSize;
686 }
David Blaikie46a9f012012-01-20 21:51:11 +0000687 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000688}
689
Evan Cheng7fae11b2011-12-14 02:11:42 +0000690unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr *MI) const {
691 unsigned Size = 0;
692 MachineBasicBlock::const_instr_iterator I = MI;
693 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
694 while (++I != E && I->isInsideBundle()) {
695 assert(!I->isBundle() && "No nested bundle!");
696 Size += GetInstSizeInBytes(&*I);
697 }
698 return Size;
699}
700
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000701void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
702 MachineBasicBlock::iterator I, DebugLoc DL,
703 unsigned DestReg, unsigned SrcReg,
704 bool KillSrc) const {
705 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000706 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson70aa8d02010-02-16 17:24:15 +0000707
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000708 if (GPRDest && GPRSrc) {
709 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
Jim Grosbach8815bef2013-10-22 02:29:35 +0000710 .addReg(SrcReg, getKillRegState(KillSrc))));
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000711 return;
David Goodwine5b5d8f2009-08-05 21:02:22 +0000712 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000713
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000714 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000715 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000716
Chad Rosierbe762512011-08-20 00:17:25 +0000717 unsigned Opc = 0;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000718 if (SPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000719 Opc = ARM::VMOVS;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000720 else if (GPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000721 Opc = ARM::VMOVRS;
722 else if (SPRDest && GPRSrc)
723 Opc = ARM::VMOVSR;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000724 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP())
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000725 Opc = ARM::VMOVD;
726 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
Owen Anderson454e1c72011-07-15 18:46:47 +0000727 Opc = ARM::VORRq;
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000728
Chad Rosierbe762512011-08-20 00:17:25 +0000729 if (Opc) {
730 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
Owen Anderson454e1c72011-07-15 18:46:47 +0000731 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosierbe762512011-08-20 00:17:25 +0000732 if (Opc == ARM::VORRq)
733 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosier61f92ef2011-08-20 00:52:40 +0000734 AddDefaultPred(MIB);
Chad Rosierbe762512011-08-20 00:17:25 +0000735 return;
736 }
737
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000738 // Handle register classes that require multiple instructions.
739 unsigned BeginIdx = 0;
740 unsigned SubRegs = 0;
Andrew Trickb57e2252012-08-29 04:41:37 +0000741 int Spacing = 1;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000742
743 // Use VORRq when possible.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000744 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
745 Opc = ARM::VORRq;
746 BeginIdx = ARM::qsub_0;
747 SubRegs = 2;
748 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
749 Opc = ARM::VORRq;
750 BeginIdx = ARM::qsub_0;
751 SubRegs = 4;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000752 // Fall back to VMOVD.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000753 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
754 Opc = ARM::VMOVD;
755 BeginIdx = ARM::dsub_0;
756 SubRegs = 2;
757 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
758 Opc = ARM::VMOVD;
759 BeginIdx = ARM::dsub_0;
760 SubRegs = 3;
761 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
762 Opc = ARM::VMOVD;
763 BeginIdx = ARM::dsub_0;
764 SubRegs = 4;
765 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
Jim Grosbachdba14dd2013-10-22 02:29:37 +0000766 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000767 BeginIdx = ARM::gsub_0;
768 SubRegs = 2;
769 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
770 Opc = ARM::VMOVD;
771 BeginIdx = ARM::dsub_0;
772 SubRegs = 2;
773 Spacing = 2;
774 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
775 Opc = ARM::VMOVD;
776 BeginIdx = ARM::dsub_0;
777 SubRegs = 3;
778 Spacing = 2;
779 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
780 Opc = ARM::VMOVD;
781 BeginIdx = ARM::dsub_0;
782 SubRegs = 4;
783 Spacing = 2;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000784 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) {
785 Opc = ARM::VMOVS;
786 BeginIdx = ARM::ssub_0;
787 SubRegs = 2;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000788 }
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000789
Andrew Trickb57e2252012-08-29 04:41:37 +0000790 assert(Opc && "Impossible reg-to-reg copy");
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000791
Andrew Trick4cc69492012-08-29 01:58:52 +0000792 const TargetRegisterInfo *TRI = &getRegisterInfo();
793 MachineInstrBuilder Mov;
Andrew Trickbd0073d2012-08-29 01:58:55 +0000794
795 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
796 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000797 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000798 Spacing = -Spacing;
799 }
800#ifndef NDEBUG
801 SmallSet<unsigned, 4> DstRegs;
802#endif
Andrew Trick4cc69492012-08-29 01:58:52 +0000803 for (unsigned i = 0; i != SubRegs; ++i) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000804 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
805 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
Andrew Trick4cc69492012-08-29 01:58:52 +0000806 assert(Dst && Src && "Bad sub-register");
Andrew Trickbd0073d2012-08-29 01:58:55 +0000807#ifndef NDEBUG
Andrew Trickbd0073d2012-08-29 01:58:55 +0000808 assert(!DstRegs.count(Src) && "destructive vector copy");
Andrew Trickb57e2252012-08-29 04:41:37 +0000809 DstRegs.insert(Dst);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000810#endif
Jim Grosbach8815bef2013-10-22 02:29:35 +0000811 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
Andrew Trick4cc69492012-08-29 01:58:52 +0000812 // VORR takes two source operands.
813 if (Opc == ARM::VORRq)
814 Mov.addReg(Src);
815 Mov = AddDefaultPred(Mov);
JF Bastien583db652013-07-12 23:33:03 +0000816 // MOVr can set CC.
817 if (Opc == ARM::MOVr)
818 Mov = AddDefaultCC(Mov);
Andrew Trick4cc69492012-08-29 01:58:52 +0000819 }
820 // Add implicit super-register defs and kills to the last instruction.
821 Mov->addRegisterDefined(DestReg, TRI);
822 if (KillSrc)
823 Mov->addRegisterKilled(SrcReg, TRI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000824}
825
Tim Northover798697d2013-04-21 11:57:07 +0000826const MachineInstrBuilder &
827ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
828 unsigned SubIdx, unsigned State,
829 const TargetRegisterInfo *TRI) const {
Evan Chengddc93c72010-05-07 00:24:52 +0000830 if (!SubIdx)
831 return MIB.addReg(Reg, State);
832
833 if (TargetRegisterInfo::isPhysicalRegister(Reg))
834 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
835 return MIB.addReg(Reg, State, SubIdx);
836}
837
David Goodwinaf7451b2009-07-08 16:09:28 +0000838void ARMBaseInstrInfo::
839storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
840 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000841 const TargetRegisterClass *RC,
842 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000843 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +0000844 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000845 MachineFunction &MF = *MBB.getParent();
846 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000847 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000848
849 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000850 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000851 MachineMemOperand::MOStore,
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000852 MFI.getObjectSize(FI),
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000853 Align);
David Goodwinaf7451b2009-07-08 16:09:28 +0000854
Owen Anderson732f82c2011-08-10 17:21:20 +0000855 switch (RC->getSize()) {
856 case 4:
857 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
858 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
David Goodwinaf7451b2009-07-08 16:09:28 +0000859 .addReg(SrcReg, getKillRegState(isKill))
Jim Grosbach338de3e2010-10-27 23:12:14 +0000860 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000861 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
862 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
Evan Cheng9d768f42010-05-06 01:34:11 +0000863 .addReg(SrcReg, getKillRegState(isKill))
864 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000865 } else
866 llvm_unreachable("Unknown reg class!");
867 break;
868 case 8:
869 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
870 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwinaf7451b2009-07-08 16:09:28 +0000871 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000872 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +0000873 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +0000874 if (Subtarget.hasV5TEOps()) {
875 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD));
876 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
877 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
878 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
879
880 AddDefaultPred(MIB);
881 } else {
882 // Fallback to STM instruction, which has existed since the dawn of
883 // time.
884 MachineInstrBuilder MIB =
885 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STMIA))
886 .addFrameIndex(FI).addMemOperand(MMO));
887 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
888 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
889 }
Owen Anderson732f82c2011-08-10 17:21:20 +0000890 } else
891 llvm_unreachable("Unknown reg class!");
892 break;
893 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +0000894 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +0000895 // Use aligned spills if the stack can be realigned.
896 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000897 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64))
Bob Wilson4c1ca292010-07-06 21:26:18 +0000898 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000899 .addReg(SrcReg, getKillRegState(isKill))
900 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000901 } else {
902 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000903 .addReg(SrcReg, getKillRegState(isKill))
904 .addFrameIndex(FI)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000905 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000906 }
907 } else
908 llvm_unreachable("Unknown reg class!");
909 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000910 case 24:
911 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
912 // Use aligned spills if the stack can be realigned.
913 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
914 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
915 .addFrameIndex(FI).addImm(16)
916 .addReg(SrcReg, getKillRegState(isKill))
917 .addMemOperand(MMO));
918 } else {
919 MachineInstrBuilder MIB =
920 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
921 .addFrameIndex(FI))
922 .addMemOperand(MMO);
923 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
924 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
925 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
926 }
927 } else
928 llvm_unreachable("Unknown reg class!");
929 break;
Owen Anderson732f82c2011-08-10 17:21:20 +0000930 case 32:
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000931 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +0000932 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
933 // FIXME: It's possible to only store part of the QQ register if the
934 // spilled def has a sub-register index.
935 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +0000936 .addFrameIndex(FI).addImm(16)
937 .addReg(SrcReg, getKillRegState(isKill))
938 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000939 } else {
940 MachineInstrBuilder MIB =
941 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000942 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +0000943 .addMemOperand(MMO);
944 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
945 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
946 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
947 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
948 }
949 } else
950 llvm_unreachable("Unknown reg class!");
951 break;
952 case 64:
953 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
954 MachineInstrBuilder MIB =
955 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
956 .addFrameIndex(FI))
957 .addMemOperand(MMO);
958 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
959 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
960 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
961 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
962 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
963 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
964 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
965 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
966 } else
967 llvm_unreachable("Unknown reg class!");
968 break;
969 default:
970 llvm_unreachable("Unknown reg class!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000971 }
972}
973
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000974unsigned
975ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
976 int &FrameIndex) const {
977 switch (MI->getOpcode()) {
978 default: break;
Jim Grosbach338de3e2010-10-27 23:12:14 +0000979 case ARM::STRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000980 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
981 if (MI->getOperand(1).isFI() &&
982 MI->getOperand(2).isReg() &&
983 MI->getOperand(3).isImm() &&
984 MI->getOperand(2).getReg() == 0 &&
985 MI->getOperand(3).getImm() == 0) {
986 FrameIndex = MI->getOperand(1).getIndex();
987 return MI->getOperand(0).getReg();
988 }
989 break;
Jim Grosbach338de3e2010-10-27 23:12:14 +0000990 case ARM::STRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000991 case ARM::t2STRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +0000992 case ARM::tSTRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000993 case ARM::VSTRD:
994 case ARM::VSTRS:
995 if (MI->getOperand(1).isFI() &&
996 MI->getOperand(2).isImm() &&
997 MI->getOperand(2).getImm() == 0) {
998 FrameIndex = MI->getOperand(1).getIndex();
999 return MI->getOperand(0).getReg();
1000 }
1001 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001002 case ARM::VST1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001003 case ARM::VST1d64TPseudo:
1004 case ARM::VST1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001005 if (MI->getOperand(0).isFI() &&
1006 MI->getOperand(2).getSubReg() == 0) {
1007 FrameIndex = MI->getOperand(0).getIndex();
1008 return MI->getOperand(2).getReg();
1009 }
Jakob Stoklund Olesenb929c712010-09-15 21:40:09 +00001010 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001011 case ARM::VSTMQIA:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001012 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001013 MI->getOperand(0).getSubReg() == 0) {
1014 FrameIndex = MI->getOperand(1).getIndex();
1015 return MI->getOperand(0).getReg();
1016 }
1017 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001018 }
1019
1020 return 0;
1021}
1022
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001023unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
1024 int &FrameIndex) const {
1025 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001026 return MI->mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001027}
1028
David Goodwinaf7451b2009-07-08 16:09:28 +00001029void ARMBaseInstrInfo::
1030loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1031 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +00001032 const TargetRegisterClass *RC,
1033 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +00001034 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +00001035 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001036 MachineFunction &MF = *MBB.getParent();
1037 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001038 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001039 MachineMemOperand *MMO =
Chris Lattnere3d864b2010-09-21 04:39:43 +00001040 MF.getMachineMemOperand(
Jay Foad465101b2011-11-15 07:34:52 +00001041 MachinePointerInfo::getFixedStack(FI),
Chris Lattnere3d864b2010-09-21 04:39:43 +00001042 MachineMemOperand::MOLoad,
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001043 MFI.getObjectSize(FI),
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001044 Align);
David Goodwinaf7451b2009-07-08 16:09:28 +00001045
Owen Anderson732f82c2011-08-10 17:21:20 +00001046 switch (RC->getSize()) {
1047 case 4:
1048 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
1049 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1050 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilson37f106e2010-02-16 22:01:59 +00001051
Owen Anderson732f82c2011-08-10 17:21:20 +00001052 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
1053 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001054 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001055 } else
1056 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001057 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001058 case 8:
1059 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
1060 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Evan Cheng9d768f42010-05-06 01:34:11 +00001061 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001062 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001063 MachineInstrBuilder MIB;
1064
1065 if (Subtarget.hasV5TEOps()) {
1066 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1067 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1068 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1069 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
1070
1071 AddDefaultPred(MIB);
1072 } else {
1073 // Fallback to LDM instruction, which has existed since the dawn of
1074 // time.
1075 MIB = AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDMIA))
1076 .addFrameIndex(FI).addMemOperand(MMO));
1077 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1078 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1079 }
1080
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001081 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1082 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001083 } else
1084 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001085 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001086 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001087 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001088 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001089 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
Bob Wilson4c1ca292010-07-06 21:26:18 +00001090 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +00001091 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001092 } else {
1093 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1094 .addFrameIndex(FI)
1095 .addMemOperand(MMO));
1096 }
1097 } else
1098 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001099 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001100 case 24:
1101 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1102 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1103 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1104 .addFrameIndex(FI).addImm(16)
1105 .addMemOperand(MMO));
1106 } else {
1107 MachineInstrBuilder MIB =
1108 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1109 .addFrameIndex(FI)
1110 .addMemOperand(MMO));
1111 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1112 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1113 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1114 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1115 MIB.addReg(DestReg, RegState::ImplicitDefine);
1116 }
1117 } else
1118 llvm_unreachable("Unknown reg class!");
1119 break;
1120 case 32:
1121 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001122 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1123 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +00001124 .addFrameIndex(FI).addImm(16)
1125 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001126 } else {
1127 MachineInstrBuilder MIB =
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001128 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1129 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +00001130 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001131 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1132 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1133 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1134 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001135 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1136 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001137 }
1138 } else
1139 llvm_unreachable("Unknown reg class!");
1140 break;
1141 case 64:
1142 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1143 MachineInstrBuilder MIB =
1144 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1145 .addFrameIndex(FI))
1146 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001147 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1148 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1149 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1150 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1151 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1152 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1153 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1154 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001155 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1156 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001157 } else
1158 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001159 break;
Bob Wilsona92e41a2010-06-18 21:32:42 +00001160 default:
1161 llvm_unreachable("Unknown regclass!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001162 }
1163}
1164
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001165unsigned
1166ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
1167 int &FrameIndex) const {
1168 switch (MI->getOpcode()) {
1169 default: break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001170 case ARM::LDRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001171 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
1172 if (MI->getOperand(1).isFI() &&
1173 MI->getOperand(2).isReg() &&
1174 MI->getOperand(3).isImm() &&
1175 MI->getOperand(2).getReg() == 0 &&
1176 MI->getOperand(3).getImm() == 0) {
1177 FrameIndex = MI->getOperand(1).getIndex();
1178 return MI->getOperand(0).getReg();
1179 }
1180 break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001181 case ARM::LDRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001182 case ARM::t2LDRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001183 case ARM::tLDRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001184 case ARM::VLDRD:
1185 case ARM::VLDRS:
1186 if (MI->getOperand(1).isFI() &&
1187 MI->getOperand(2).isImm() &&
1188 MI->getOperand(2).getImm() == 0) {
1189 FrameIndex = MI->getOperand(1).getIndex();
1190 return MI->getOperand(0).getReg();
1191 }
1192 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001193 case ARM::VLD1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001194 case ARM::VLD1d64TPseudo:
1195 case ARM::VLD1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001196 if (MI->getOperand(1).isFI() &&
1197 MI->getOperand(0).getSubReg() == 0) {
1198 FrameIndex = MI->getOperand(1).getIndex();
1199 return MI->getOperand(0).getReg();
1200 }
1201 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001202 case ARM::VLDMQIA:
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001203 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001204 MI->getOperand(0).getSubReg() == 0) {
1205 FrameIndex = MI->getOperand(1).getIndex();
1206 return MI->getOperand(0).getReg();
1207 }
1208 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001209 }
1210
1211 return 0;
1212}
1213
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001214unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
1215 int &FrameIndex) const {
1216 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001217 return MI->mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001218}
1219
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001220bool
1221ARMBaseInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
1222 MachineFunction &MF = *MI->getParent()->getParent();
1223 Reloc::Model RM = MF.getTarget().getRelocationModel();
1224
1225 if (MI->getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
1226 assert(getSubtarget().getTargetTriple().getObjectFormat() ==
1227 Triple::MachO &&
1228 "LOAD_STACK_GUARD currently supported only for MachO.");
1229 expandLoadStackGuard(MI, RM);
1230 MI->getParent()->erase(MI);
1231 return true;
1232 }
1233
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001234 // This hook gets to expand COPY instructions before they become
1235 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1236 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1237 // changed into a VORR that can go down the NEON pipeline.
Oliver Stannard51b1d462014-08-21 12:50:31 +00001238 if (!WidenVMOVS || !MI->isCopy() || Subtarget.isCortexA15() ||
1239 Subtarget.isFPOnlySP())
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001240 return false;
1241
1242 // Look for a copy between even S-registers. That is where we keep floats
1243 // when using NEON v2f32 instructions for f32 arithmetic.
1244 unsigned DstRegS = MI->getOperand(0).getReg();
1245 unsigned SrcRegS = MI->getOperand(1).getReg();
1246 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1247 return false;
1248
1249 const TargetRegisterInfo *TRI = &getRegisterInfo();
1250 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1251 &ARM::DPRRegClass);
1252 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1253 &ARM::DPRRegClass);
1254 if (!DstRegD || !SrcRegD)
1255 return false;
1256
1257 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1258 // legal if the COPY already defines the full DstRegD, and it isn't a
1259 // sub-register insertion.
1260 if (!MI->definesRegister(DstRegD, TRI) || MI->readsRegister(DstRegD, TRI))
1261 return false;
1262
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001263 // A dead copy shouldn't show up here, but reject it just in case.
1264 if (MI->getOperand(0).isDead())
1265 return false;
1266
1267 // All clear, widen the COPY.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001268 DEBUG(dbgs() << "widening: " << *MI);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001269 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001270
1271 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg
1272 // or some other super-register.
1273 int ImpDefIdx = MI->findRegisterDefOperandIdx(DstRegD);
1274 if (ImpDefIdx != -1)
1275 MI->RemoveOperand(ImpDefIdx);
1276
1277 // Change the opcode and operands.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001278 MI->setDesc(get(ARM::VMOVD));
1279 MI->getOperand(0).setReg(DstRegD);
1280 MI->getOperand(1).setReg(SrcRegD);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001281 AddDefaultPred(MIB);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001282
1283 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1284 // register scavenger and machine verifier, so we need to indicate that we
1285 // are reading an undefined value from SrcRegD, but a proper value from
1286 // SrcRegS.
1287 MI->getOperand(1).setIsUndef();
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001288 MIB.addReg(SrcRegS, RegState::Implicit);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001289
1290 // SrcRegD may actually contain an unrelated value in the ssub_1
1291 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
1292 if (MI->getOperand(1).isKill()) {
1293 MI->getOperand(1).setIsKill(false);
1294 MI->addRegisterKilled(SrcRegS, TRI, true);
1295 }
1296
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001297 DEBUG(dbgs() << "replaced by: " << *MI);
1298 return true;
1299}
1300
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001301/// Create a copy of a const pool value. Update CPI to the new index and return
1302/// the label UID.
1303static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1304 MachineConstantPool *MCP = MF.getConstantPool();
1305 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1306
1307 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1308 assert(MCPE.isMachineConstantPoolEntry() &&
1309 "Expecting a machine constantpool entry!");
1310 ARMConstantPoolValue *ACPV =
1311 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1312
Evan Chengdfce83c2011-01-17 08:03:18 +00001313 unsigned PCLabelId = AFI->createPICLabelUId();
Craig Topper062a2ba2014-04-25 05:30:21 +00001314 ARMConstantPoolValue *NewCPV = nullptr;
Oliver Stannard8f859942014-01-29 16:01:24 +00001315
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001316 // FIXME: The below assumes PIC relocation model and that the function
1317 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1318 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1319 // instructions, so that's probably OK, but is PIC always correct when
1320 // we get here?
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001321 if (ACPV->isGlobalValue())
Bill Wendling7753d662011-10-01 08:00:54 +00001322 NewCPV = ARMConstantPoolConstant::
1323 Create(cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId,
1324 ARMCP::CPValue, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001325 else if (ACPV->isExtSymbol())
Bill Wendlingc214cb02011-10-01 08:58:29 +00001326 NewCPV = ARMConstantPoolSymbol::
1327 Create(MF.getFunction()->getContext(),
1328 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001329 else if (ACPV->isBlockAddress())
Bill Wendling7753d662011-10-01 08:00:54 +00001330 NewCPV = ARMConstantPoolConstant::
1331 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1332 ARMCP::CPBlockAddress, 4);
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001333 else if (ACPV->isLSDA())
Bill Wendling7753d662011-10-01 08:00:54 +00001334 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId,
1335 ARMCP::CPLSDA, 4);
Bill Wendling69bc3de2011-09-29 23:50:42 +00001336 else if (ACPV->isMachineBasicBlock())
Bill Wendling4a4772f2011-10-01 09:30:42 +00001337 NewCPV = ARMConstantPoolMBB::
1338 Create(MF.getFunction()->getContext(),
1339 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001340 else
1341 llvm_unreachable("Unexpected ARM constantpool value type!!");
1342 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1343 return PCLabelId;
1344}
1345
Evan Chengfe864422009-11-08 00:15:23 +00001346void ARMBaseInstrInfo::
1347reMaterialize(MachineBasicBlock &MBB,
1348 MachineBasicBlock::iterator I,
1349 unsigned DestReg, unsigned SubIdx,
Evan Cheng6ad7da92009-11-14 02:55:43 +00001350 const MachineInstr *Orig,
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001351 const TargetRegisterInfo &TRI) const {
Evan Chengfe864422009-11-08 00:15:23 +00001352 unsigned Opcode = Orig->getOpcode();
1353 switch (Opcode) {
1354 default: {
1355 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001356 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfe864422009-11-08 00:15:23 +00001357 MBB.insert(I, MI);
1358 break;
1359 }
1360 case ARM::tLDRpci_pic:
1361 case ARM::t2LDRpci_pic: {
1362 MachineFunction &MF = *MBB.getParent();
Evan Chengfe864422009-11-08 00:15:23 +00001363 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001364 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfe864422009-11-08 00:15:23 +00001365 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1366 DestReg)
1367 .addConstantPoolIndex(CPI).addImm(PCLabelId);
Chris Lattner1d0c2572011-04-29 05:24:29 +00001368 MIB->setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
Evan Chengfe864422009-11-08 00:15:23 +00001369 break;
1370 }
1371 }
Evan Chengfe864422009-11-08 00:15:23 +00001372}
1373
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001374MachineInstr *
1375ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001376 MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001377 switch(Orig->getOpcode()) {
1378 case ARM::tLDRpci_pic:
1379 case ARM::t2LDRpci_pic: {
1380 unsigned CPI = Orig->getOperand(1).getIndex();
1381 unsigned PCLabelId = duplicateCPV(MF, CPI);
1382 Orig->getOperand(1).setIndex(CPI);
1383 Orig->getOperand(2).setImm(PCLabelId);
1384 break;
1385 }
1386 }
1387 return MI;
1388}
1389
Evan Chenge9c46c22010-03-03 01:44:33 +00001390bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
Evan Chengb8b0ad82011-01-20 08:34:58 +00001391 const MachineInstr *MI1,
1392 const MachineRegisterInfo *MRI) const {
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001393 int Opcode = MI0->getOpcode();
Evan Cheng028ccbfc2011-01-20 23:55:07 +00001394 if (Opcode == ARM::t2LDRpci ||
Evan Chengbbd50b02009-11-20 02:10:27 +00001395 Opcode == ARM::t2LDRpci_pic ||
1396 Opcode == ARM::tLDRpci ||
Evan Chengb8b0ad82011-01-20 08:34:58 +00001397 Opcode == ARM::tLDRpci_pic ||
Tim Northover72360d22013-12-02 10:35:41 +00001398 Opcode == ARM::LDRLIT_ga_pcrel ||
1399 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1400 Opcode == ARM::tLDRLIT_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001401 Opcode == ARM::MOV_ga_pcrel ||
1402 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001403 Opcode == ARM::t2MOV_ga_pcrel) {
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001404 if (MI1->getOpcode() != Opcode)
1405 return false;
1406 if (MI0->getNumOperands() != MI1->getNumOperands())
1407 return false;
1408
1409 const MachineOperand &MO0 = MI0->getOperand(1);
1410 const MachineOperand &MO1 = MI1->getOperand(1);
1411 if (MO0.getOffset() != MO1.getOffset())
1412 return false;
1413
Tim Northover72360d22013-12-02 10:35:41 +00001414 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1415 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1416 Opcode == ARM::tLDRLIT_ga_pcrel ||
1417 Opcode == ARM::MOV_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001418 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001419 Opcode == ARM::t2MOV_ga_pcrel)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001420 // Ignore the PC labels.
1421 return MO0.getGlobal() == MO1.getGlobal();
1422
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001423 const MachineFunction *MF = MI0->getParent()->getParent();
1424 const MachineConstantPool *MCP = MF->getConstantPool();
1425 int CPI0 = MO0.getIndex();
1426 int CPI1 = MO1.getIndex();
1427 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1428 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
Evan Chengf098bf12011-03-24 06:20:03 +00001429 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1430 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1431 if (isARMCP0 && isARMCP1) {
1432 ARMConstantPoolValue *ACPV0 =
1433 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1434 ARMConstantPoolValue *ACPV1 =
1435 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1436 return ACPV0->hasSameValue(ACPV1);
1437 } else if (!isARMCP0 && !isARMCP1) {
1438 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1439 }
1440 return false;
Evan Chengb8b0ad82011-01-20 08:34:58 +00001441 } else if (Opcode == ARM::PICLDR) {
1442 if (MI1->getOpcode() != Opcode)
1443 return false;
1444 if (MI0->getNumOperands() != MI1->getNumOperands())
1445 return false;
1446
1447 unsigned Addr0 = MI0->getOperand(1).getReg();
1448 unsigned Addr1 = MI1->getOperand(1).getReg();
1449 if (Addr0 != Addr1) {
1450 if (!MRI ||
1451 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1452 !TargetRegisterInfo::isVirtualRegister(Addr1))
1453 return false;
1454
1455 // This assumes SSA form.
1456 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1457 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1458 // Check if the loaded value, e.g. a constantpool of a global address, are
1459 // the same.
1460 if (!produceSameValue(Def0, Def1, MRI))
1461 return false;
1462 }
1463
1464 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1465 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1466 const MachineOperand &MO0 = MI0->getOperand(i);
1467 const MachineOperand &MO1 = MI1->getOperand(i);
1468 if (!MO0.isIdenticalTo(MO1))
1469 return false;
1470 }
1471 return true;
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001472 }
1473
Evan Chenge9c46c22010-03-03 01:44:33 +00001474 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001475}
1476
Bill Wendlingf4707472010-06-23 23:00:16 +00001477/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1478/// determine if two loads are loading from the same base address. It should
1479/// only return true if the base pointers are the same and the only differences
1480/// between the two addresses is the offset. It also returns the offsets by
1481/// reference.
Andrew Tricka7714a02012-11-12 19:40:10 +00001482///
1483/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1484/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001485bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1486 int64_t &Offset1,
1487 int64_t &Offset2) const {
1488 // Don't worry about Thumb: just ARM and Thumb2.
1489 if (Subtarget.isThumb1Only()) return false;
1490
1491 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1492 return false;
1493
1494 switch (Load1->getMachineOpcode()) {
1495 default:
1496 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001497 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001498 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001499 case ARM::LDRD:
1500 case ARM::LDRH:
1501 case ARM::LDRSB:
1502 case ARM::LDRSH:
1503 case ARM::VLDRD:
1504 case ARM::VLDRS:
1505 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001506 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001507 case ARM::t2LDRDi8:
1508 case ARM::t2LDRSHi8:
1509 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001510 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001511 case ARM::t2LDRSHi12:
1512 break;
1513 }
1514
1515 switch (Load2->getMachineOpcode()) {
1516 default:
1517 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001518 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001519 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001520 case ARM::LDRD:
1521 case ARM::LDRH:
1522 case ARM::LDRSB:
1523 case ARM::LDRSH:
1524 case ARM::VLDRD:
1525 case ARM::VLDRS:
1526 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001527 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001528 case ARM::t2LDRSHi8:
1529 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001530 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001531 case ARM::t2LDRSHi12:
1532 break;
1533 }
1534
1535 // Check if base addresses and chain operands match.
1536 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1537 Load1->getOperand(4) != Load2->getOperand(4))
1538 return false;
1539
1540 // Index should be Reg0.
1541 if (Load1->getOperand(3) != Load2->getOperand(3))
1542 return false;
1543
1544 // Determine the offsets.
1545 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1546 isa<ConstantSDNode>(Load2->getOperand(1))) {
1547 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1548 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1549 return true;
1550 }
1551
1552 return false;
1553}
1554
1555/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001556/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Bill Wendlingf4707472010-06-23 23:00:16 +00001557/// be scheduled togther. On some targets if two loads are loading from
1558/// addresses in the same cache line, it's better if they are scheduled
1559/// together. This function takes two integers that represent the load offsets
1560/// from the common base address. It returns true if it decides it's desirable
1561/// to schedule the two loads together. "NumLoads" is the number of loads that
1562/// have already been scheduled after Load1.
Andrew Tricka7714a02012-11-12 19:40:10 +00001563///
1564/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1565/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001566bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1567 int64_t Offset1, int64_t Offset2,
1568 unsigned NumLoads) const {
1569 // Don't worry about Thumb: just ARM and Thumb2.
1570 if (Subtarget.isThumb1Only()) return false;
1571
1572 assert(Offset2 > Offset1);
1573
1574 if ((Offset2 - Offset1) / 8 > 64)
1575 return false;
1576
Renato Golinb184cd92013-08-14 16:35:29 +00001577 // Check if the machine opcodes are different. If they are different
1578 // then we consider them to not be of the same base address,
1579 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1580 // In this case, they are considered to be the same because they are different
1581 // encoding forms of the same basic instruction.
1582 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1583 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1584 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1585 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1586 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
Bill Wendlingf4707472010-06-23 23:00:16 +00001587 return false; // FIXME: overly conservative?
1588
1589 // Four loads in a row should be sufficient.
1590 if (NumLoads >= 3)
1591 return false;
1592
1593 return true;
1594}
1595
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001596bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1597 const MachineBasicBlock *MBB,
1598 const MachineFunction &MF) const {
Jim Grosbachba3ece62010-06-25 18:43:14 +00001599 // Debug info is never a scheduling boundary. It's necessary to be explicit
1600 // due to the special treatment of IT instructions below, otherwise a
1601 // dbg_value followed by an IT will result in the IT instruction being
1602 // considered a scheduling hazard, which is wrong. It should be the actual
1603 // instruction preceding the dbg_value instruction(s), just like it is
1604 // when debug info is not present.
1605 if (MI->isDebugValue())
1606 return false;
1607
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001608 // Terminators and labels can't be scheduled around.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001609 if (MI->isTerminator() || MI->isPosition())
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001610 return true;
1611
1612 // Treat the start of the IT block as a scheduling boundary, but schedule
1613 // t2IT along with all instructions following it.
1614 // FIXME: This is a big hammer. But the alternative is to add all potential
1615 // true and anti dependencies to IT block instructions as implicit operands
1616 // to the t2IT instruction. The added compile time and complexity does not
1617 // seem worth it.
1618 MachineBasicBlock::const_iterator I = MI;
Jim Grosbachba3ece62010-06-25 18:43:14 +00001619 // Make sure to skip any dbg_value instructions
1620 while (++I != MBB->end() && I->isDebugValue())
1621 ;
1622 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001623 return true;
1624
1625 // Don't attempt to schedule around any instruction that defines
1626 // a stack-oriented pointer, as it's unlikely to be profitable. This
1627 // saves compile time, because it doesn't require every single
1628 // stack slot reference to depend on the instruction that does the
1629 // modification.
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001630 // Calls don't actually change the stack pointer, even if they have imp-defs.
Jakob Stoklund Olesen5f37f1c2012-02-22 01:07:19 +00001631 // No ARM calling conventions change the stack pointer. (X86 calling
1632 // conventions sometimes do).
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001633 if (!MI->isCall() && MI->definesRegister(ARM::SP))
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001634 return true;
1635
1636 return false;
1637}
1638
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001639bool ARMBaseInstrInfo::
1640isProfitableToIfCvt(MachineBasicBlock &MBB,
1641 unsigned NumCycles, unsigned ExtraPredCycles,
1642 const BranchProbability &Probability) const {
Cameron Zwarich80018502011-04-13 06:39:16 +00001643 if (!NumCycles)
Evan Cheng02b184d2010-06-25 22:42:03 +00001644 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001645
Owen Anderson88af7d02010-09-28 18:32:13 +00001646 // Attempt to estimate the relative costs of predication versus branching.
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001647 unsigned UnpredCost = Probability.getNumerator() * NumCycles;
1648 UnpredCost /= Probability.getDenominator();
1649 UnpredCost += 1; // The branch itself
1650 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001651
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001652 return (NumCycles + ExtraPredCycles) <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001653}
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001654
Evan Cheng02b184d2010-06-25 22:42:03 +00001655bool ARMBaseInstrInfo::
Evan Chengdebf9c52010-11-03 00:45:17 +00001656isProfitableToIfCvt(MachineBasicBlock &TMBB,
1657 unsigned TCycles, unsigned TExtra,
1658 MachineBasicBlock &FMBB,
1659 unsigned FCycles, unsigned FExtra,
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001660 const BranchProbability &Probability) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00001661 if (!TCycles || !FCycles)
Owen Anderson88af7d02010-09-28 18:32:13 +00001662 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001663
Owen Anderson88af7d02010-09-28 18:32:13 +00001664 // Attempt to estimate the relative costs of predication versus branching.
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001665 unsigned TUnpredCost = Probability.getNumerator() * TCycles;
1666 TUnpredCost /= Probability.getDenominator();
Andrew Trick3f1fdf12011-09-21 02:17:37 +00001667
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001668 uint32_t Comp = Probability.getDenominator() - Probability.getNumerator();
1669 unsigned FUnpredCost = Comp * FCycles;
1670 FUnpredCost /= Probability.getDenominator();
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001671
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001672 unsigned UnpredCost = TUnpredCost + FUnpredCost;
1673 UnpredCost += 1; // The branch itself
1674 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
1675
1676 return (TCycles + FCycles + TExtra + FExtra) <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001677}
1678
Bob Wilsone8a549c2012-09-29 21:43:49 +00001679bool
1680ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1681 MachineBasicBlock &FMBB) const {
1682 // Reduce false anti-dependencies to let Swift's out-of-order execution
1683 // engine do its thing.
1684 return Subtarget.isSwift();
1685}
1686
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001687/// getInstrPredicate - If instruction is predicated, returns its predicate
1688/// condition, otherwise returns AL. It also returns the condition code
1689/// register by reference.
Evan Cheng83e0d482009-09-28 09:14:39 +00001690ARMCC::CondCodes
1691llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001692 int PIdx = MI->findFirstPredOperandIdx();
1693 if (PIdx == -1) {
1694 PredReg = 0;
1695 return ARMCC::AL;
1696 }
1697
1698 PredReg = MI->getOperand(PIdx+1).getReg();
1699 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1700}
1701
1702
Evan Cheng780748d2009-07-28 05:48:47 +00001703int llvm::getMatchingCondBranchOpcode(int Opc) {
Evan Cheng056c6692009-07-27 18:20:05 +00001704 if (Opc == ARM::B)
1705 return ARM::Bcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001706 if (Opc == ARM::tB)
Evan Cheng056c6692009-07-27 18:20:05 +00001707 return ARM::tBcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001708 if (Opc == ARM::t2B)
1709 return ARM::t2Bcc;
Evan Cheng056c6692009-07-27 18:20:05 +00001710
1711 llvm_unreachable("Unknown unconditional branch opcode!");
Evan Cheng056c6692009-07-27 18:20:05 +00001712}
1713
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001714/// commuteInstruction - Handle commutable instructions.
1715MachineInstr *
1716ARMBaseInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
1717 switch (MI->getOpcode()) {
1718 case ARM::MOVCCr:
1719 case ARM::t2MOVCCr: {
1720 // MOVCC can be commuted by inverting the condition.
1721 unsigned PredReg = 0;
1722 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
1723 // MOVCC AL can't be inverted. Shouldn't happen.
1724 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
Craig Topper062a2ba2014-04-25 05:30:21 +00001725 return nullptr;
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001726 MI = TargetInstrInfo::commuteInstruction(MI, NewMI);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001727 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001728 return nullptr;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001729 // After swapping the MOVCC operands, also invert the condition.
1730 MI->getOperand(MI->findFirstPredOperandIdx())
1731 .setImm(ARMCC::getOppositeCondition(CC));
1732 return MI;
1733 }
1734 }
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001735 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001736}
Evan Cheng780748d2009-07-28 05:48:47 +00001737
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001738/// Identify instructions that can be folded into a MOVCC instruction, and
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001739/// return the defining instruction.
1740static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
1741 const MachineRegisterInfo &MRI,
1742 const TargetInstrInfo *TII) {
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001743 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001744 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001745 if (!MRI.hasOneNonDBGUse(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001746 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001747 MachineInstr *MI = MRI.getVRegDef(Reg);
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001748 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001749 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001750 // MI is folded into the MOVCC by predicating it.
1751 if (!MI->isPredicable())
Craig Topper062a2ba2014-04-25 05:30:21 +00001752 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001753 // Check if MI has any non-dead defs or physreg uses. This also detects
1754 // predicated instructions which will be reading CPSR.
1755 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
1756 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen7b1a2e82012-08-17 20:55:34 +00001757 // Reject frame index operands, PEI can't handle the predicated pseudos.
1758 if (MO.isFI() || MO.isCPI() || MO.isJTI())
Craig Topper062a2ba2014-04-25 05:30:21 +00001759 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001760 if (!MO.isReg())
1761 continue;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001762 // MI can't have any tied operands, that would conflict with predication.
1763 if (MO.isTied())
Craig Topper062a2ba2014-04-25 05:30:21 +00001764 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001765 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Craig Topper062a2ba2014-04-25 05:30:21 +00001766 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001767 if (MO.isDef() && !MO.isDead())
Craig Topper062a2ba2014-04-25 05:30:21 +00001768 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001769 }
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001770 bool DontMoveAcrossStores = true;
Craig Topper062a2ba2014-04-25 05:30:21 +00001771 if (!MI->isSafeToMove(TII, /* AliasAnalysis = */ nullptr,
1772 DontMoveAcrossStores))
1773 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001774 return MI;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001775}
1776
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001777bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr *MI,
1778 SmallVectorImpl<MachineOperand> &Cond,
1779 unsigned &TrueOp, unsigned &FalseOp,
1780 bool &Optimizable) const {
1781 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1782 "Unknown select instruction");
1783 // MOVCC operands:
1784 // 0: Def.
1785 // 1: True use.
1786 // 2: False use.
1787 // 3: Condition code.
1788 // 4: CPSR use.
1789 TrueOp = 1;
1790 FalseOp = 2;
1791 Cond.push_back(MI->getOperand(3));
1792 Cond.push_back(MI->getOperand(4));
1793 // We can always fold a def.
1794 Optimizable = true;
1795 return false;
1796}
1797
1798MachineInstr *ARMBaseInstrInfo::optimizeSelect(MachineInstr *MI,
1799 bool PreferFalse) const {
1800 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1801 "Unknown select instruction");
Matthias Braun2f169f92013-10-04 16:52:56 +00001802 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001803 MachineInstr *DefMI = canFoldIntoMOVCC(MI->getOperand(2).getReg(), MRI, this);
1804 bool Invert = !DefMI;
1805 if (!DefMI)
1806 DefMI = canFoldIntoMOVCC(MI->getOperand(1).getReg(), MRI, this);
1807 if (!DefMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001808 return nullptr;
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001809
Matthias Braun2f169f92013-10-04 16:52:56 +00001810 // Find new register class to use.
1811 MachineOperand FalseReg = MI->getOperand(Invert ? 2 : 1);
1812 unsigned DestReg = MI->getOperand(0).getReg();
1813 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
1814 if (!MRI.constrainRegClass(DestReg, PreviousClass))
Craig Topper062a2ba2014-04-25 05:30:21 +00001815 return nullptr;
Matthias Braun2f169f92013-10-04 16:52:56 +00001816
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001817 // Create a new predicated version of DefMI.
1818 // Rfalse is the first use.
1819 MachineInstrBuilder NewMI = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
Matthias Braun2f169f92013-10-04 16:52:56 +00001820 DefMI->getDesc(), DestReg);
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001821
1822 // Copy all the DefMI operands, excluding its (null) predicate.
1823 const MCInstrDesc &DefDesc = DefMI->getDesc();
1824 for (unsigned i = 1, e = DefDesc.getNumOperands();
1825 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
1826 NewMI.addOperand(DefMI->getOperand(i));
1827
1828 unsigned CondCode = MI->getOperand(3).getImm();
1829 if (Invert)
1830 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
1831 else
1832 NewMI.addImm(CondCode);
1833 NewMI.addOperand(MI->getOperand(4));
1834
1835 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
1836 if (NewMI->hasOptionalDef())
1837 AddDefaultCC(NewMI);
1838
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001839 // The output register value when the predicate is false is an implicit
1840 // register operand tied to the first def.
1841 // The tie makes the register allocator ensure the FalseReg is allocated the
1842 // same register as operand 0.
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001843 FalseReg.setImplicit();
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +00001844 NewMI.addOperand(FalseReg);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001845 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
1846
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001847 // The caller will erase MI, but not DefMI.
1848 DefMI->eraseFromParent();
1849 return NewMI;
1850}
1851
Andrew Trick924123a2011-09-21 02:20:46 +00001852/// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
1853/// instruction is encoded with an 'S' bit is determined by the optional CPSR
1854/// def operand.
1855///
1856/// This will go away once we can teach tblgen how to set the optional CPSR def
1857/// operand itself.
1858struct AddSubFlagsOpcodePair {
Craig Topper2fbd1302012-05-24 03:59:11 +00001859 uint16_t PseudoOpc;
1860 uint16_t MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001861};
1862
Craig Topper2fbd1302012-05-24 03:59:11 +00001863static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
Andrew Trick924123a2011-09-21 02:20:46 +00001864 {ARM::ADDSri, ARM::ADDri},
1865 {ARM::ADDSrr, ARM::ADDrr},
1866 {ARM::ADDSrsi, ARM::ADDrsi},
1867 {ARM::ADDSrsr, ARM::ADDrsr},
1868
1869 {ARM::SUBSri, ARM::SUBri},
1870 {ARM::SUBSrr, ARM::SUBrr},
1871 {ARM::SUBSrsi, ARM::SUBrsi},
1872 {ARM::SUBSrsr, ARM::SUBrsr},
1873
1874 {ARM::RSBSri, ARM::RSBri},
Andrew Trick924123a2011-09-21 02:20:46 +00001875 {ARM::RSBSrsi, ARM::RSBrsi},
1876 {ARM::RSBSrsr, ARM::RSBrsr},
1877
1878 {ARM::t2ADDSri, ARM::t2ADDri},
1879 {ARM::t2ADDSrr, ARM::t2ADDrr},
1880 {ARM::t2ADDSrs, ARM::t2ADDrs},
1881
1882 {ARM::t2SUBSri, ARM::t2SUBri},
1883 {ARM::t2SUBSrr, ARM::t2SUBrr},
1884 {ARM::t2SUBSrs, ARM::t2SUBrs},
1885
1886 {ARM::t2RSBSri, ARM::t2RSBri},
1887 {ARM::t2RSBSrs, ARM::t2RSBrs},
1888};
1889
1890unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
Craig Topper2fbd1302012-05-24 03:59:11 +00001891 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
1892 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
1893 return AddSubFlagsOpcodeMap[i].MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001894 return 0;
1895}
1896
Evan Cheng780748d2009-07-28 05:48:47 +00001897void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1898 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1899 unsigned DestReg, unsigned BaseReg, int NumBytes,
1900 ARMCC::CondCodes Pred, unsigned PredReg,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001901 const ARMBaseInstrInfo &TII, unsigned MIFlags) {
Tim Northoverc9432eb2013-11-04 23:04:15 +00001902 if (NumBytes == 0 && DestReg != BaseReg) {
1903 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
1904 .addReg(BaseReg, RegState::Kill)
1905 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1906 .setMIFlags(MIFlags);
1907 return;
1908 }
1909
Evan Cheng780748d2009-07-28 05:48:47 +00001910 bool isSub = NumBytes < 0;
1911 if (isSub) NumBytes = -NumBytes;
1912
1913 while (NumBytes) {
1914 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1915 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1916 assert(ThisVal && "Didn't extract field correctly");
1917
1918 // We will handle these bits from offset, clear them.
1919 NumBytes &= ~ThisVal;
1920
1921 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1922
1923 // Build the new ADD / SUB.
1924 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1925 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1926 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001927 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1928 .setMIFlags(MIFlags);
Evan Cheng780748d2009-07-28 05:48:47 +00001929 BaseReg = DestReg;
1930 }
1931}
1932
Weiming Zhao01524852014-03-20 23:28:16 +00001933static bool isAnySubRegLive(unsigned Reg, const TargetRegisterInfo *TRI,
1934 MachineInstr *MI) {
1935 for (MCSubRegIterator Subreg(Reg, TRI, /* IncludeSelf */ true);
1936 Subreg.isValid(); ++Subreg)
1937 if (MI->getParent()->computeRegisterLiveness(TRI, *Subreg, MI) !=
1938 MachineBasicBlock::LQR_Dead)
1939 return true;
1940 return false;
1941}
Tim Northoverdee86042013-12-02 14:46:26 +00001942bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
1943 MachineFunction &MF, MachineInstr *MI,
Tim Northover93bcc662013-11-08 17:18:07 +00001944 unsigned NumBytes) {
1945 // This optimisation potentially adds lots of load and store
1946 // micro-operations, it's only really a great benefit to code-size.
Eric Christopherc1058df2014-07-04 01:55:26 +00001947 if (!MF.getFunction()->getAttributes().hasAttribute(
1948 AttributeSet::FunctionIndex, Attribute::MinSize))
Tim Northover93bcc662013-11-08 17:18:07 +00001949 return false;
1950
1951 // If only one register is pushed/popped, LLVM can use an LDR/STR
1952 // instead. We can't modify those so make sure we're dealing with an
1953 // instruction we understand.
1954 bool IsPop = isPopOpcode(MI->getOpcode());
1955 bool IsPush = isPushOpcode(MI->getOpcode());
1956 if (!IsPush && !IsPop)
1957 return false;
1958
1959 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
1960 MI->getOpcode() == ARM::VLDMDIA_UPD;
1961 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
1962 MI->getOpcode() == ARM::tPOP ||
1963 MI->getOpcode() == ARM::tPOP_RET;
1964
1965 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
1966 MI->getOperand(1).getReg() == ARM::SP)) &&
1967 "trying to fold sp update into non-sp-updating push/pop");
1968
1969 // The VFP push & pop act on D-registers, so we can only fold an adjustment
1970 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
1971 // if this is violated.
1972 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
1973 return false;
1974
1975 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
1976 // pred) so the list starts at 4. Thumb1 starts after the predicate.
1977 int RegListIdx = IsT1PushPop ? 2 : 4;
1978
1979 // Calculate the space we'll need in terms of registers.
1980 unsigned FirstReg = MI->getOperand(RegListIdx).getReg();
1981 unsigned RD0Reg, RegsNeeded;
1982 if (IsVFPPushPop) {
1983 RD0Reg = ARM::D0;
1984 RegsNeeded = NumBytes / 8;
1985 } else {
1986 RD0Reg = ARM::R0;
1987 RegsNeeded = NumBytes / 4;
1988 }
1989
1990 // We're going to have to strip all list operands off before
1991 // re-adding them since the order matters, so save the existing ones
1992 // for later.
1993 SmallVector<MachineOperand, 4> RegList;
1994 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
1995 RegList.push_back(MI->getOperand(i));
1996
Tim Northover93bcc662013-11-08 17:18:07 +00001997 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
Tim Northover45479dc2013-12-01 14:16:24 +00001998 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Tim Northover93bcc662013-11-08 17:18:07 +00001999
2000 // Now try to find enough space in the reglist to allocate NumBytes.
2001 for (unsigned CurReg = FirstReg - 1; CurReg >= RD0Reg && RegsNeeded;
Tim Northover45479dc2013-12-01 14:16:24 +00002002 --CurReg) {
Tim Northover93bcc662013-11-08 17:18:07 +00002003 if (!IsPop) {
2004 // Pushing any register is completely harmless, mark the
2005 // register involved as undef since we don't care about it in
2006 // the slightest.
2007 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2008 false, false, true));
Tim Northover45479dc2013-12-01 14:16:24 +00002009 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002010 continue;
2011 }
2012
Tim Northover45479dc2013-12-01 14:16:24 +00002013 // However, we can only pop an extra register if it's not live. For
2014 // registers live within the function we might clobber a return value
2015 // register; the other way a register can be live here is if it's
2016 // callee-saved.
Weiming Zhao01524852014-03-20 23:28:16 +00002017 // TODO: Currently, computeRegisterLiveness() does not report "live" if a
2018 // sub reg is live. When computeRegisterLiveness() works for sub reg, it
2019 // can replace isAnySubRegLive().
Tim Northover45479dc2013-12-01 14:16:24 +00002020 if (isCalleeSavedRegister(CurReg, CSRegs) ||
Weiming Zhao01524852014-03-20 23:28:16 +00002021 isAnySubRegLive(CurReg, TRI, MI)) {
Tim Northover45479dc2013-12-01 14:16:24 +00002022 // VFP pops don't allow holes in the register list, so any skip is fatal
2023 // for our transformation. GPR pops do, so we should just keep looking.
2024 if (IsVFPPushPop)
2025 return false;
2026 else
2027 continue;
2028 }
Tim Northover93bcc662013-11-08 17:18:07 +00002029
2030 // Mark the unimportant registers as <def,dead> in the POP.
Lang Hames1ca11232013-11-22 00:46:32 +00002031 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2032 true));
Tim Northover45479dc2013-12-01 14:16:24 +00002033 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002034 }
2035
2036 if (RegsNeeded > 0)
2037 return false;
2038
2039 // Finally we know we can profitably perform the optimisation so go
2040 // ahead: strip all existing registers off and add them back again
2041 // in the right order.
2042 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2043 MI->RemoveOperand(i);
2044
2045 // Add the complete list back in.
2046 MachineInstrBuilder MIB(MF, &*MI);
2047 for (int i = RegList.size() - 1; i >= 0; --i)
2048 MIB.addOperand(RegList[i]);
2049
2050 return true;
2051}
2052
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002053bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2054 unsigned FrameReg, int &Offset,
2055 const ARMBaseInstrInfo &TII) {
Evan Cheng780748d2009-07-28 05:48:47 +00002056 unsigned Opcode = MI.getOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002057 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng780748d2009-07-28 05:48:47 +00002058 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2059 bool isSub = false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002060
Evan Cheng780748d2009-07-28 05:48:47 +00002061 // Memory operands in inline assembly always use AddrMode2.
2062 if (Opcode == ARM::INLINEASM)
2063 AddrMode = ARMII::AddrMode2;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002064
Evan Cheng780748d2009-07-28 05:48:47 +00002065 if (Opcode == ARM::ADDri) {
2066 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2067 if (Offset == 0) {
2068 // Turn it into a move.
2069 MI.setDesc(TII.get(ARM::MOVr));
2070 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2071 MI.RemoveOperand(FrameRegIdx+1);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002072 Offset = 0;
2073 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002074 } else if (Offset < 0) {
2075 Offset = -Offset;
2076 isSub = true;
2077 MI.setDesc(TII.get(ARM::SUBri));
2078 }
2079
2080 // Common case: small offset, fits into instruction.
2081 if (ARM_AM::getSOImmVal(Offset) != -1) {
2082 // Replace the FrameIndex with sp / fp
2083 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2084 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002085 Offset = 0;
2086 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002087 }
2088
2089 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2090 // as possible.
2091 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2092 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2093
2094 // We will handle these bits from offset, clear them.
2095 Offset &= ~ThisImmVal;
2096
2097 // Get the properly encoded SOImmVal field.
2098 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2099 "Bit extraction didn't work?");
2100 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2101 } else {
2102 unsigned ImmIdx = 0;
2103 int InstrOffs = 0;
2104 unsigned NumBits = 0;
2105 unsigned Scale = 1;
2106 switch (AddrMode) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002107 case ARMII::AddrMode_i12: {
2108 ImmIdx = FrameRegIdx + 1;
2109 InstrOffs = MI.getOperand(ImmIdx).getImm();
2110 NumBits = 12;
2111 break;
2112 }
Evan Cheng780748d2009-07-28 05:48:47 +00002113 case ARMII::AddrMode2: {
2114 ImmIdx = FrameRegIdx+2;
2115 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2116 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2117 InstrOffs *= -1;
2118 NumBits = 12;
2119 break;
2120 }
2121 case ARMII::AddrMode3: {
2122 ImmIdx = FrameRegIdx+2;
2123 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2124 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2125 InstrOffs *= -1;
2126 NumBits = 8;
2127 break;
2128 }
Anton Korobeynikov887d05c2009-08-08 13:35:48 +00002129 case ARMII::AddrMode4:
Jim Grosbach01c1cae2009-11-15 21:45:34 +00002130 case ARMII::AddrMode6:
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002131 // Can't fold any offset even if it's zero.
2132 return false;
Evan Cheng780748d2009-07-28 05:48:47 +00002133 case ARMII::AddrMode5: {
2134 ImmIdx = FrameRegIdx+1;
2135 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2136 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2137 InstrOffs *= -1;
2138 NumBits = 8;
2139 Scale = 4;
2140 break;
2141 }
2142 default:
2143 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng780748d2009-07-28 05:48:47 +00002144 }
2145
2146 Offset += InstrOffs * Scale;
2147 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2148 if (Offset < 0) {
2149 Offset = -Offset;
2150 isSub = true;
2151 }
2152
2153 // Attempt to fold address comp. if opcode has offset bits
2154 if (NumBits > 0) {
2155 // Common case: small offset, fits into instruction.
2156 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2157 int ImmedOffset = Offset / Scale;
2158 unsigned Mask = (1 << NumBits) - 1;
2159 if ((unsigned)Offset <= Mask * Scale) {
2160 // Replace the FrameIndex with sp
2161 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach9d2d1f02010-10-27 01:19:41 +00002162 // FIXME: When addrmode2 goes away, this will simplify (like the
2163 // T2 version), as the LDR.i12 versions don't need the encoding
2164 // tricks for the offset value.
2165 if (isSub) {
2166 if (AddrMode == ARMII::AddrMode_i12)
2167 ImmedOffset = -ImmedOffset;
2168 else
2169 ImmedOffset |= 1 << NumBits;
2170 }
Evan Cheng780748d2009-07-28 05:48:47 +00002171 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002172 Offset = 0;
2173 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002174 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002175
Evan Cheng780748d2009-07-28 05:48:47 +00002176 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2177 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach8bf14832010-10-27 16:50:31 +00002178 if (isSub) {
2179 if (AddrMode == ARMII::AddrMode_i12)
2180 ImmedOffset = -ImmedOffset;
2181 else
2182 ImmedOffset |= 1 << NumBits;
2183 }
Evan Cheng780748d2009-07-28 05:48:47 +00002184 ImmOp.ChangeToImmediate(ImmedOffset);
2185 Offset &= ~(Mask*Scale);
2186 }
2187 }
2188
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002189 Offset = (isSub) ? -Offset : Offset;
2190 return Offset == 0;
Evan Cheng780748d2009-07-28 05:48:47 +00002191}
Bill Wendling7de9d522010-08-06 01:32:48 +00002192
Manman Ren6fa76dc2012-06-29 21:33:59 +00002193/// analyzeCompare - For a comparison instruction, return the source registers
2194/// in SrcReg and SrcReg2 if having two register operands, and the value it
2195/// compares against in CmpValue. Return true if the comparison instruction
2196/// can be analyzed.
Bill Wendling7de9d522010-08-06 01:32:48 +00002197bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002198analyzeCompare(const MachineInstr *MI, unsigned &SrcReg, unsigned &SrcReg2,
2199 int &CmpMask, int &CmpValue) const {
Bill Wendling7de9d522010-08-06 01:32:48 +00002200 switch (MI->getOpcode()) {
2201 default: break;
Bill Wendling79553ba2010-08-11 00:23:00 +00002202 case ARM::CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002203 case ARM::t2CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002204 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002205 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002206 CmpMask = ~0;
Bill Wendling7de9d522010-08-06 01:32:48 +00002207 CmpValue = MI->getOperand(1).getImm();
2208 return true;
Manman Rendc8ad002012-05-11 01:30:47 +00002209 case ARM::CMPrr:
2210 case ARM::t2CMPrr:
2211 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002212 SrcReg2 = MI->getOperand(1).getReg();
Manman Rendc8ad002012-05-11 01:30:47 +00002213 CmpMask = ~0;
2214 CmpValue = 0;
2215 return true;
Gabor Greifadbbb932010-09-21 12:01:15 +00002216 case ARM::TSTri:
2217 case ARM::t2TSTri:
2218 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002219 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002220 CmpMask = MI->getOperand(1).getImm();
2221 CmpValue = 0;
2222 return true;
2223 }
2224
2225 return false;
2226}
2227
Gabor Greifd36e3e82010-09-29 10:12:08 +00002228/// isSuitableForMask - Identify a suitable 'and' instruction that
2229/// operates on the given source register and applies the same mask
2230/// as a 'tst' instruction. Provide a limited look-through for copies.
2231/// When successful, MI will hold the found instruction.
2232static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif1a25ae82010-09-21 13:30:57 +00002233 int CmpMask, bool CommonUse) {
Gabor Greifd36e3e82010-09-29 10:12:08 +00002234 switch (MI->getOpcode()) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002235 case ARM::ANDri:
2236 case ARM::t2ANDri:
Gabor Greifd36e3e82010-09-29 10:12:08 +00002237 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif1a25ae82010-09-21 13:30:57 +00002238 return false;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002239 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greifadbbb932010-09-21 12:01:15 +00002240 return true;
2241 break;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002242 case ARM::COPY: {
2243 // Walk down one instruction which is potentially an 'and'.
2244 const MachineInstr &Copy = *MI;
Michael J. Spencer70ac5fa2010-10-05 06:00:43 +00002245 MachineBasicBlock::iterator AND(
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002246 std::next(MachineBasicBlock::iterator(MI)));
Gabor Greifd36e3e82010-09-29 10:12:08 +00002247 if (AND == MI->getParent()->end()) return false;
2248 MI = AND;
2249 return isSuitableForMask(MI, Copy.getOperand(0).getReg(),
2250 CmpMask, true);
2251 }
Bill Wendling7de9d522010-08-06 01:32:48 +00002252 }
2253
2254 return false;
2255}
2256
Manman Renb1b3db62012-06-29 22:06:19 +00002257/// getSwappedCondition - assume the flags are set by MI(a,b), return
2258/// the condition code if we modify the instructions such that flags are
2259/// set by MI(b,a).
2260inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2261 switch (CC) {
2262 default: return ARMCC::AL;
2263 case ARMCC::EQ: return ARMCC::EQ;
2264 case ARMCC::NE: return ARMCC::NE;
2265 case ARMCC::HS: return ARMCC::LS;
2266 case ARMCC::LO: return ARMCC::HI;
2267 case ARMCC::HI: return ARMCC::LO;
2268 case ARMCC::LS: return ARMCC::HS;
2269 case ARMCC::GE: return ARMCC::LE;
2270 case ARMCC::LT: return ARMCC::GT;
2271 case ARMCC::GT: return ARMCC::LT;
2272 case ARMCC::LE: return ARMCC::GE;
2273 }
2274}
2275
2276/// isRedundantFlagInstr - check whether the first instruction, whose only
2277/// purpose is to update flags, can be made redundant.
2278/// CMPrr can be made redundant by SUBrr if the operands are the same.
2279/// CMPri can be made redundant by SUBri if the operands are the same.
2280/// This function can be extended later on.
2281inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2282 unsigned SrcReg2, int ImmValue,
2283 MachineInstr *OI) {
2284 if ((CmpI->getOpcode() == ARM::CMPrr ||
2285 CmpI->getOpcode() == ARM::t2CMPrr) &&
2286 (OI->getOpcode() == ARM::SUBrr ||
2287 OI->getOpcode() == ARM::t2SUBrr) &&
2288 ((OI->getOperand(1).getReg() == SrcReg &&
2289 OI->getOperand(2).getReg() == SrcReg2) ||
2290 (OI->getOperand(1).getReg() == SrcReg2 &&
2291 OI->getOperand(2).getReg() == SrcReg)))
2292 return true;
2293
2294 if ((CmpI->getOpcode() == ARM::CMPri ||
2295 CmpI->getOpcode() == ARM::t2CMPri) &&
2296 (OI->getOpcode() == ARM::SUBri ||
2297 OI->getOpcode() == ARM::t2SUBri) &&
2298 OI->getOperand(1).getReg() == SrcReg &&
2299 OI->getOperand(2).getImm() == ImmValue)
2300 return true;
2301 return false;
2302}
2303
Manman Ren6fa76dc2012-06-29 21:33:59 +00002304/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2305/// comparison into one that sets the zero bit in the flags register;
2306/// Remove a redundant Compare instruction if an earlier instruction can set the
2307/// flags in the same way as Compare.
2308/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2309/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2310/// condition code of instructions which use the flags.
Bill Wendling7de9d522010-08-06 01:32:48 +00002311bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002312optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, unsigned SrcReg2,
2313 int CmpMask, int CmpValue,
2314 const MachineRegisterInfo *MRI) const {
Manman Renb1b3db62012-06-29 22:06:19 +00002315 // Get the unique definition of SrcReg.
2316 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2317 if (!MI) return false;
Bill Wendling04123002010-09-10 23:34:19 +00002318
Gabor Greifadbbb932010-09-21 12:01:15 +00002319 // Masked compares sometimes use the same register as the corresponding 'and'.
2320 if (CmpMask != ~0) {
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002321 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(MI)) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002322 MI = nullptr;
Owen Anderson16c6bf42014-03-13 23:12:04 +00002323 for (MachineRegisterInfo::use_instr_iterator
2324 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2325 UI != UE; ++UI) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002326 if (UI->getParent() != CmpInstr->getParent()) continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002327 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002328 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
2329 isPredicated(PotentialAND))
Gabor Greifadbbb932010-09-21 12:01:15 +00002330 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002331 MI = PotentialAND;
Gabor Greifadbbb932010-09-21 12:01:15 +00002332 break;
2333 }
2334 if (!MI) return false;
2335 }
2336 }
2337
Manman Rendc8ad002012-05-11 01:30:47 +00002338 // Get ready to iterate backward from CmpInstr.
2339 MachineBasicBlock::iterator I = CmpInstr, E = MI,
2340 B = CmpInstr->getParent()->begin();
Bill Wendling59ebe442010-10-09 00:03:48 +00002341
2342 // Early exit if CmpInstr is at the beginning of the BB.
2343 if (I == B) return false;
2344
Manman Rendc8ad002012-05-11 01:30:47 +00002345 // There are two possible candidates which can be changed to set CPSR:
2346 // One is MI, the other is a SUB instruction.
2347 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2348 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
Craig Topper062a2ba2014-04-25 05:30:21 +00002349 MachineInstr *Sub = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002350 if (SrcReg2 != 0)
Manman Rendc8ad002012-05-11 01:30:47 +00002351 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00002352 MI = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002353 else if (MI->getParent() != CmpInstr->getParent() || CmpValue != 0) {
Manman Rendc8ad002012-05-11 01:30:47 +00002354 // Conservatively refuse to convert an instruction which isn't in the same
2355 // BB as the comparison.
2356 // For CMPri, we need to check Sub, thus we can't return here.
Manman Ren0d5ec282012-05-11 15:36:46 +00002357 if (CmpInstr->getOpcode() == ARM::CMPri ||
Manman Rendc8ad002012-05-11 01:30:47 +00002358 CmpInstr->getOpcode() == ARM::t2CMPri)
Craig Topper062a2ba2014-04-25 05:30:21 +00002359 MI = nullptr;
Manman Rendc8ad002012-05-11 01:30:47 +00002360 else
2361 return false;
2362 }
2363
2364 // Check that CPSR isn't set between the comparison instruction and the one we
2365 // want to change. At the same time, search for Sub.
Manman Renb1b3db62012-06-29 22:06:19 +00002366 const TargetRegisterInfo *TRI = &getRegisterInfo();
Bill Wendling7de9d522010-08-06 01:32:48 +00002367 --I;
2368 for (; I != E; --I) {
2369 const MachineInstr &Instr = *I;
2370
Manman Renb1b3db62012-06-29 22:06:19 +00002371 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2372 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendlingc6627ee2010-11-01 20:41:43 +00002373 // This instruction modifies or uses CPSR after the one we want to
2374 // change. We can't do this transformation.
Manman Renb1b3db62012-06-29 22:06:19 +00002375 return false;
Evan Chengd757c882010-09-21 23:49:07 +00002376
Manman Renb1b3db62012-06-29 22:06:19 +00002377 // Check whether CmpInstr can be made redundant by the current instruction.
2378 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
Manman Rendc8ad002012-05-11 01:30:47 +00002379 Sub = &*I;
2380 break;
2381 }
2382
Evan Chengd757c882010-09-21 23:49:07 +00002383 if (I == B)
2384 // The 'and' is below the comparison instruction.
2385 return false;
Bill Wendling7de9d522010-08-06 01:32:48 +00002386 }
2387
Manman Rendc8ad002012-05-11 01:30:47 +00002388 // Return false if no candidates exist.
2389 if (!MI && !Sub)
2390 return false;
2391
2392 // The single candidate is called MI.
2393 if (!MI) MI = Sub;
2394
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002395 // We can't use a predicated instruction - it doesn't always write the flags.
2396 if (isPredicated(MI))
2397 return false;
2398
Bill Wendling7de9d522010-08-06 01:32:48 +00002399 switch (MI->getOpcode()) {
2400 default: break;
Cameron Zwarich93eae152011-04-15 20:28:28 +00002401 case ARM::RSBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002402 case ARM::RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002403 case ARM::RSCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002404 case ARM::RSCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002405 case ARM::ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002406 case ARM::ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002407 case ARM::ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002408 case ARM::ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002409 case ARM::SUBrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002410 case ARM::SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002411 case ARM::SBCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002412 case ARM::SBCri:
2413 case ARM::t2RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002414 case ARM::t2ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002415 case ARM::t2ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002416 case ARM::t2ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002417 case ARM::t2ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002418 case ARM::t2SUBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002419 case ARM::t2SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002420 case ARM::t2SBCrr:
Cameron Zwarich0829b302011-04-15 20:45:00 +00002421 case ARM::t2SBCri:
2422 case ARM::ANDrr:
2423 case ARM::ANDri:
2424 case ARM::t2ANDrr:
Cameron Zwarich9c65e4d2011-04-15 21:24:38 +00002425 case ARM::t2ANDri:
2426 case ARM::ORRrr:
2427 case ARM::ORRri:
2428 case ARM::t2ORRrr:
2429 case ARM::t2ORRri:
2430 case ARM::EORrr:
2431 case ARM::EORri:
2432 case ARM::t2EORrr:
2433 case ARM::t2EORri: {
Manman Rendc8ad002012-05-11 01:30:47 +00002434 // Scan forward for the use of CPSR
2435 // When checking against MI: if it's a conditional code requires
Manman Ren34cb93e2012-07-11 22:51:44 +00002436 // checking of V bit, then this is not safe to do.
2437 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2438 // If we are done with the basic block, we need to check whether CPSR is
2439 // live-out.
Manman Renb1b3db62012-06-29 22:06:19 +00002440 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2441 OperandsToUpdate;
Evan Cheng425489d2011-03-23 22:52:04 +00002442 bool isSafe = false;
2443 I = CmpInstr;
Manman Rendc8ad002012-05-11 01:30:47 +00002444 E = CmpInstr->getParent()->end();
Evan Cheng425489d2011-03-23 22:52:04 +00002445 while (!isSafe && ++I != E) {
2446 const MachineInstr &Instr = *I;
2447 for (unsigned IO = 0, EO = Instr.getNumOperands();
2448 !isSafe && IO != EO; ++IO) {
2449 const MachineOperand &MO = Instr.getOperand(IO);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +00002450 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2451 isSafe = true;
2452 break;
2453 }
Evan Cheng425489d2011-03-23 22:52:04 +00002454 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2455 continue;
2456 if (MO.isDef()) {
2457 isSafe = true;
2458 break;
2459 }
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002460 // Condition code is after the operand before CPSR except for VSELs.
2461 ARMCC::CondCodes CC;
2462 bool IsInstrVSel = true;
2463 switch (Instr.getOpcode()) {
2464 default:
2465 IsInstrVSel = false;
2466 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2467 break;
2468 case ARM::VSELEQD:
2469 case ARM::VSELEQS:
2470 CC = ARMCC::EQ;
2471 break;
2472 case ARM::VSELGTD:
2473 case ARM::VSELGTS:
2474 CC = ARMCC::GT;
2475 break;
2476 case ARM::VSELGED:
2477 case ARM::VSELGES:
2478 CC = ARMCC::GE;
2479 break;
2480 case ARM::VSELVSS:
2481 case ARM::VSELVSD:
2482 CC = ARMCC::VS;
2483 break;
2484 }
2485
Manman Renb1b3db62012-06-29 22:06:19 +00002486 if (Sub) {
2487 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2488 if (NewCC == ARMCC::AL)
Manman Rendc8ad002012-05-11 01:30:47 +00002489 return false;
Manman Renb1b3db62012-06-29 22:06:19 +00002490 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2491 // on CMP needs to be updated to be based on SUB.
2492 // Push the condition code operands to OperandsToUpdate.
2493 // If it is safe to remove CmpInstr, the condition code of these
2494 // operands will be modified.
2495 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002496 Sub->getOperand(2).getReg() == SrcReg) {
2497 // VSel doesn't support condition code update.
2498 if (IsInstrVSel)
2499 return false;
2500 OperandsToUpdate.push_back(
2501 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2502 }
2503 } else
Manman Rendc8ad002012-05-11 01:30:47 +00002504 switch (CC) {
2505 default:
Manman Ren88a0d332012-07-11 23:47:00 +00002506 // CPSR can be used multiple times, we should continue.
Manman Rendc8ad002012-05-11 01:30:47 +00002507 break;
2508 case ARMCC::VS:
2509 case ARMCC::VC:
2510 case ARMCC::GE:
2511 case ARMCC::LT:
2512 case ARMCC::GT:
2513 case ARMCC::LE:
2514 return false;
2515 }
Evan Cheng425489d2011-03-23 22:52:04 +00002516 }
2517 }
2518
Manman Ren34cb93e2012-07-11 22:51:44 +00002519 // If CPSR is not killed nor re-defined, we should check whether it is
2520 // live-out. If it is live-out, do not optimize.
2521 if (!isSafe) {
2522 MachineBasicBlock *MBB = CmpInstr->getParent();
2523 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2524 SE = MBB->succ_end(); SI != SE; ++SI)
2525 if ((*SI)->isLiveIn(ARM::CPSR))
2526 return false;
2527 }
Evan Cheng425489d2011-03-23 22:52:04 +00002528
Evan Cheng65536472010-11-17 08:06:50 +00002529 // Toggle the optional operand to CPSR.
2530 MI->getOperand(5).setReg(ARM::CPSR);
2531 MI->getOperand(5).setIsDef(true);
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002532 assert(!isPredicated(MI) && "Can't use flags from predicated instruction");
Bill Wendling7de9d522010-08-06 01:32:48 +00002533 CmpInstr->eraseFromParent();
Manman Rendc8ad002012-05-11 01:30:47 +00002534
2535 // Modify the condition code of operands in OperandsToUpdate.
2536 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2537 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Manman Renb1b3db62012-06-29 22:06:19 +00002538 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2539 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
Bill Wendling7de9d522010-08-06 01:32:48 +00002540 return true;
2541 }
Cameron Zwarich0829b302011-04-15 20:45:00 +00002542 }
Bill Wendling7de9d522010-08-06 01:32:48 +00002543
2544 return false;
2545}
Evan Cheng367a5df2010-09-09 18:18:55 +00002546
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002547bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
2548 MachineInstr *DefMI, unsigned Reg,
2549 MachineRegisterInfo *MRI) const {
2550 // Fold large immediates into add, sub, or, xor.
2551 unsigned DefOpc = DefMI->getOpcode();
2552 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2553 return false;
2554 if (!DefMI->getOperand(1).isImm())
2555 // Could be t2MOVi32imm <ga:xx>
2556 return false;
2557
2558 if (!MRI->hasOneNonDBGUse(Reg))
2559 return false;
2560
Evan Chenga2b48d92012-03-26 23:31:00 +00002561 const MCInstrDesc &DefMCID = DefMI->getDesc();
2562 if (DefMCID.hasOptionalDef()) {
2563 unsigned NumOps = DefMCID.getNumOperands();
2564 const MachineOperand &MO = DefMI->getOperand(NumOps-1);
2565 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2566 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2567 // to delete DefMI.
2568 return false;
2569 }
2570
2571 const MCInstrDesc &UseMCID = UseMI->getDesc();
2572 if (UseMCID.hasOptionalDef()) {
2573 unsigned NumOps = UseMCID.getNumOperands();
2574 if (UseMI->getOperand(NumOps-1).getReg() == ARM::CPSR)
2575 // If the instruction sets the flag, do not attempt this optimization
2576 // since it may change the semantics of the code.
2577 return false;
2578 }
2579
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002580 unsigned UseOpc = UseMI->getOpcode();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002581 unsigned NewUseOpc = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002582 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002583 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002584 bool Commute = false;
2585 switch (UseOpc) {
2586 default: return false;
2587 case ARM::SUBrr:
2588 case ARM::ADDrr:
2589 case ARM::ORRrr:
2590 case ARM::EORrr:
2591 case ARM::t2SUBrr:
2592 case ARM::t2ADDrr:
2593 case ARM::t2ORRrr:
2594 case ARM::t2EORrr: {
2595 Commute = UseMI->getOperand(2).getReg() != Reg;
2596 switch (UseOpc) {
2597 default: break;
2598 case ARM::SUBrr: {
2599 if (Commute)
2600 return false;
2601 ImmVal = -ImmVal;
2602 NewUseOpc = ARM::SUBri;
2603 // Fallthrough
2604 }
2605 case ARM::ADDrr:
2606 case ARM::ORRrr:
2607 case ARM::EORrr: {
2608 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2609 return false;
2610 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2611 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2612 switch (UseOpc) {
2613 default: break;
2614 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
2615 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2616 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2617 }
2618 break;
2619 }
2620 case ARM::t2SUBrr: {
2621 if (Commute)
2622 return false;
2623 ImmVal = -ImmVal;
2624 NewUseOpc = ARM::t2SUBri;
2625 // Fallthrough
2626 }
2627 case ARM::t2ADDrr:
2628 case ARM::t2ORRrr:
2629 case ARM::t2EORrr: {
2630 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2631 return false;
2632 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2633 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2634 switch (UseOpc) {
2635 default: break;
2636 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
2637 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2638 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2639 }
2640 break;
2641 }
2642 }
2643 }
2644 }
2645
2646 unsigned OpIdx = Commute ? 2 : 1;
2647 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
2648 bool isKill = UseMI->getOperand(OpIdx).isKill();
2649 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
2650 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
Evan Cheng7fae11b2011-12-14 02:11:42 +00002651 UseMI, UseMI->getDebugLoc(),
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002652 get(NewUseOpc), NewReg)
2653 .addReg(Reg1, getKillRegState(isKill))
2654 .addImm(SOImmValV1)));
2655 UseMI->setDesc(get(NewUseOpc));
2656 UseMI->getOperand(1).setReg(NewReg);
2657 UseMI->getOperand(1).setIsKill();
2658 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
2659 DefMI->eraseFromParent();
2660 return true;
2661}
2662
Bob Wilsone8a549c2012-09-29 21:43:49 +00002663static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
2664 const MachineInstr *MI) {
2665 switch (MI->getOpcode()) {
2666 default: {
2667 const MCInstrDesc &Desc = MI->getDesc();
2668 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
2669 assert(UOps >= 0 && "bad # UOps");
2670 return UOps;
2671 }
2672
2673 case ARM::LDRrs:
2674 case ARM::LDRBrs:
2675 case ARM::STRrs:
2676 case ARM::STRBrs: {
2677 unsigned ShOpVal = MI->getOperand(3).getImm();
2678 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2679 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2680 if (!isSub &&
2681 (ShImm == 0 ||
2682 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2683 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2684 return 1;
2685 return 2;
2686 }
2687
2688 case ARM::LDRH:
2689 case ARM::STRH: {
2690 if (!MI->getOperand(2).getReg())
2691 return 1;
2692
2693 unsigned ShOpVal = MI->getOperand(3).getImm();
2694 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2695 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2696 if (!isSub &&
2697 (ShImm == 0 ||
2698 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2699 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2700 return 1;
2701 return 2;
2702 }
2703
2704 case ARM::LDRSB:
2705 case ARM::LDRSH:
2706 return (ARM_AM::getAM3Op(MI->getOperand(3).getImm()) == ARM_AM::sub) ? 3:2;
2707
2708 case ARM::LDRSB_POST:
2709 case ARM::LDRSH_POST: {
2710 unsigned Rt = MI->getOperand(0).getReg();
2711 unsigned Rm = MI->getOperand(3).getReg();
2712 return (Rt == Rm) ? 4 : 3;
2713 }
2714
2715 case ARM::LDR_PRE_REG:
2716 case ARM::LDRB_PRE_REG: {
2717 unsigned Rt = MI->getOperand(0).getReg();
2718 unsigned Rm = MI->getOperand(3).getReg();
2719 if (Rt == Rm)
2720 return 3;
2721 unsigned ShOpVal = MI->getOperand(4).getImm();
2722 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2723 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2724 if (!isSub &&
2725 (ShImm == 0 ||
2726 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2727 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2728 return 2;
2729 return 3;
2730 }
2731
2732 case ARM::STR_PRE_REG:
2733 case ARM::STRB_PRE_REG: {
2734 unsigned ShOpVal = MI->getOperand(4).getImm();
2735 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2736 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2737 if (!isSub &&
2738 (ShImm == 0 ||
2739 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2740 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2741 return 2;
2742 return 3;
2743 }
2744
2745 case ARM::LDRH_PRE:
2746 case ARM::STRH_PRE: {
2747 unsigned Rt = MI->getOperand(0).getReg();
2748 unsigned Rm = MI->getOperand(3).getReg();
2749 if (!Rm)
2750 return 2;
2751 if (Rt == Rm)
2752 return 3;
2753 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub)
2754 ? 3 : 2;
2755 }
2756
2757 case ARM::LDR_POST_REG:
2758 case ARM::LDRB_POST_REG:
2759 case ARM::LDRH_POST: {
2760 unsigned Rt = MI->getOperand(0).getReg();
2761 unsigned Rm = MI->getOperand(3).getReg();
2762 return (Rt == Rm) ? 3 : 2;
2763 }
2764
2765 case ARM::LDR_PRE_IMM:
2766 case ARM::LDRB_PRE_IMM:
2767 case ARM::LDR_POST_IMM:
2768 case ARM::LDRB_POST_IMM:
2769 case ARM::STRB_POST_IMM:
2770 case ARM::STRB_POST_REG:
2771 case ARM::STRB_PRE_IMM:
2772 case ARM::STRH_POST:
2773 case ARM::STR_POST_IMM:
2774 case ARM::STR_POST_REG:
2775 case ARM::STR_PRE_IMM:
2776 return 2;
2777
2778 case ARM::LDRSB_PRE:
2779 case ARM::LDRSH_PRE: {
2780 unsigned Rm = MI->getOperand(3).getReg();
2781 if (Rm == 0)
2782 return 3;
2783 unsigned Rt = MI->getOperand(0).getReg();
2784 if (Rt == Rm)
2785 return 4;
2786 unsigned ShOpVal = MI->getOperand(4).getImm();
2787 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2788 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2789 if (!isSub &&
2790 (ShImm == 0 ||
2791 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2792 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2793 return 3;
2794 return 4;
2795 }
2796
2797 case ARM::LDRD: {
2798 unsigned Rt = MI->getOperand(0).getReg();
2799 unsigned Rn = MI->getOperand(2).getReg();
2800 unsigned Rm = MI->getOperand(3).getReg();
2801 if (Rm)
2802 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2803 return (Rt == Rn) ? 3 : 2;
2804 }
2805
2806 case ARM::STRD: {
2807 unsigned Rm = MI->getOperand(3).getReg();
2808 if (Rm)
2809 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2810 return 2;
2811 }
2812
2813 case ARM::LDRD_POST:
2814 case ARM::t2LDRD_POST:
2815 return 3;
2816
2817 case ARM::STRD_POST:
2818 case ARM::t2STRD_POST:
2819 return 4;
2820
2821 case ARM::LDRD_PRE: {
2822 unsigned Rt = MI->getOperand(0).getReg();
2823 unsigned Rn = MI->getOperand(3).getReg();
2824 unsigned Rm = MI->getOperand(4).getReg();
2825 if (Rm)
2826 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2827 return (Rt == Rn) ? 4 : 3;
2828 }
2829
2830 case ARM::t2LDRD_PRE: {
2831 unsigned Rt = MI->getOperand(0).getReg();
2832 unsigned Rn = MI->getOperand(3).getReg();
2833 return (Rt == Rn) ? 4 : 3;
2834 }
2835
2836 case ARM::STRD_PRE: {
2837 unsigned Rm = MI->getOperand(4).getReg();
2838 if (Rm)
2839 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2840 return 3;
2841 }
2842
2843 case ARM::t2STRD_PRE:
2844 return 3;
2845
2846 case ARM::t2LDR_POST:
2847 case ARM::t2LDRB_POST:
2848 case ARM::t2LDRB_PRE:
2849 case ARM::t2LDRSBi12:
2850 case ARM::t2LDRSBi8:
2851 case ARM::t2LDRSBpci:
2852 case ARM::t2LDRSBs:
2853 case ARM::t2LDRH_POST:
2854 case ARM::t2LDRH_PRE:
2855 case ARM::t2LDRSBT:
2856 case ARM::t2LDRSB_POST:
2857 case ARM::t2LDRSB_PRE:
2858 case ARM::t2LDRSH_POST:
2859 case ARM::t2LDRSH_PRE:
2860 case ARM::t2LDRSHi12:
2861 case ARM::t2LDRSHi8:
2862 case ARM::t2LDRSHpci:
2863 case ARM::t2LDRSHs:
2864 return 2;
2865
2866 case ARM::t2LDRDi8: {
2867 unsigned Rt = MI->getOperand(0).getReg();
2868 unsigned Rn = MI->getOperand(2).getReg();
2869 return (Rt == Rn) ? 3 : 2;
2870 }
2871
2872 case ARM::t2STRB_POST:
2873 case ARM::t2STRB_PRE:
2874 case ARM::t2STRBs:
2875 case ARM::t2STRDi8:
2876 case ARM::t2STRH_POST:
2877 case ARM::t2STRH_PRE:
2878 case ARM::t2STRHs:
2879 case ARM::t2STR_POST:
2880 case ARM::t2STR_PRE:
2881 case ARM::t2STRs:
2882 return 2;
2883 }
2884}
2885
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00002886// Return the number of 32-bit words loaded by LDM or stored by STM. If this
2887// can't be easily determined return 0 (missing MachineMemOperand).
2888//
2889// FIXME: The current MachineInstr design does not support relying on machine
2890// mem operands to determine the width of a memory access. Instead, we expect
2891// the target to provide this information based on the instruction opcode and
Robin Morisset039781e2014-08-29 21:53:01 +00002892// operands. However, using MachineMemOperand is the best solution now for
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00002893// two reasons:
2894//
2895// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
2896// operands. This is much more dangerous than using the MachineMemOperand
2897// sizes because CodeGen passes can insert/remove optional machine operands. In
2898// fact, it's totally incorrect for preRA passes and appears to be wrong for
2899// postRA passes as well.
2900//
2901// 2) getNumLDMAddresses is only used by the scheduling machine model and any
2902// machine model that calls this should handle the unknown (zero size) case.
2903//
2904// Long term, we should require a target hook that verifies MachineMemOperand
2905// sizes during MC lowering. That target hook should be local to MC lowering
2906// because we can't ensure that it is aware of other MI forms. Doing this will
2907// ensure that MachineMemOperands are correctly propagated through all passes.
2908unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr *MI) const {
2909 unsigned Size = 0;
2910 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
2911 E = MI->memoperands_end(); I != E; ++I) {
2912 Size += (*I)->getSize();
2913 }
2914 return Size / 4;
2915}
2916
Evan Cheng367a5df2010-09-09 18:18:55 +00002917unsigned
Evan Chengdebf9c52010-11-03 00:45:17 +00002918ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
2919 const MachineInstr *MI) const {
Evan Chengbf407072010-09-10 01:29:16 +00002920 if (!ItinData || ItinData->isEmpty())
Evan Cheng367a5df2010-09-09 18:18:55 +00002921 return 1;
2922
Evan Cheng6cc775f2011-06-28 19:10:37 +00002923 const MCInstrDesc &Desc = MI->getDesc();
Evan Cheng367a5df2010-09-09 18:18:55 +00002924 unsigned Class = Desc.getSchedClass();
Andrew Trickf161e392012-07-02 18:10:42 +00002925 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsone8a549c2012-09-29 21:43:49 +00002926 if (ItinUOps >= 0) {
2927 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
2928 return getNumMicroOpsSwiftLdSt(ItinData, MI);
2929
Andrew Trickf161e392012-07-02 18:10:42 +00002930 return ItinUOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002931 }
Evan Cheng367a5df2010-09-09 18:18:55 +00002932
2933 unsigned Opc = MI->getOpcode();
2934 switch (Opc) {
2935 default:
2936 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002937 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002938 case ARM::VSTMQIA:
Evan Cheng367a5df2010-09-09 18:18:55 +00002939 return 2;
2940
2941 // The number of uOps for load / store multiple are determined by the number
2942 // registers.
Andrew Trickc416ba62010-12-24 04:28:06 +00002943 //
Evan Chengbf407072010-09-10 01:29:16 +00002944 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
2945 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledru35521e22012-07-23 08:51:15 +00002946 // separately by assuming the address is not 64-bit aligned.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002947 //
Evan Chengbf407072010-09-10 01:29:16 +00002948 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002949 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
2950 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
2951 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002952 case ARM::VLDMDIA_UPD:
2953 case ARM::VLDMDDB_UPD:
2954 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002955 case ARM::VLDMSIA_UPD:
2956 case ARM::VLDMSDB_UPD:
2957 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002958 case ARM::VSTMDIA_UPD:
2959 case ARM::VSTMDDB_UPD:
2960 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002961 case ARM::VSTMSIA_UPD:
2962 case ARM::VSTMSDB_UPD: {
Evan Cheng367a5df2010-09-09 18:18:55 +00002963 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
2964 return (NumRegs / 2) + (NumRegs % 2) + 1;
2965 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002966
2967 case ARM::LDMIA_RET:
2968 case ARM::LDMIA:
2969 case ARM::LDMDA:
2970 case ARM::LDMDB:
2971 case ARM::LDMIB:
2972 case ARM::LDMIA_UPD:
2973 case ARM::LDMDA_UPD:
2974 case ARM::LDMDB_UPD:
2975 case ARM::LDMIB_UPD:
2976 case ARM::STMIA:
2977 case ARM::STMDA:
2978 case ARM::STMDB:
2979 case ARM::STMIB:
2980 case ARM::STMIA_UPD:
2981 case ARM::STMDA_UPD:
2982 case ARM::STMDB_UPD:
2983 case ARM::STMIB_UPD:
2984 case ARM::tLDMIA:
2985 case ARM::tLDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002986 case ARM::tSTMIA_UPD:
Evan Cheng367a5df2010-09-09 18:18:55 +00002987 case ARM::tPOP_RET:
2988 case ARM::tPOP:
2989 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002990 case ARM::t2LDMIA_RET:
2991 case ARM::t2LDMIA:
2992 case ARM::t2LDMDB:
2993 case ARM::t2LDMIA_UPD:
2994 case ARM::t2LDMDB_UPD:
2995 case ARM::t2STMIA:
2996 case ARM::t2STMDB:
2997 case ARM::t2STMIA_UPD:
2998 case ARM::t2STMDB_UPD: {
Evan Chengbf407072010-09-10 01:29:16 +00002999 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003000 if (Subtarget.isSwift()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003001 int UOps = 1 + NumRegs; // One for address computation, one for each ld / st.
3002 switch (Opc) {
3003 default: break;
3004 case ARM::VLDMDIA_UPD:
3005 case ARM::VLDMDDB_UPD:
3006 case ARM::VLDMSIA_UPD:
3007 case ARM::VLDMSDB_UPD:
3008 case ARM::VSTMDIA_UPD:
3009 case ARM::VSTMDDB_UPD:
3010 case ARM::VSTMSIA_UPD:
3011 case ARM::VSTMSDB_UPD:
3012 case ARM::LDMIA_UPD:
3013 case ARM::LDMDA_UPD:
3014 case ARM::LDMDB_UPD:
3015 case ARM::LDMIB_UPD:
3016 case ARM::STMIA_UPD:
3017 case ARM::STMDA_UPD:
3018 case ARM::STMDB_UPD:
3019 case ARM::STMIB_UPD:
3020 case ARM::tLDMIA_UPD:
3021 case ARM::tSTMIA_UPD:
3022 case ARM::t2LDMIA_UPD:
3023 case ARM::t2LDMDB_UPD:
3024 case ARM::t2STMIA_UPD:
3025 case ARM::t2STMDB_UPD:
3026 ++UOps; // One for base register writeback.
3027 break;
3028 case ARM::LDMIA_RET:
3029 case ARM::tPOP_RET:
3030 case ARM::t2LDMIA_RET:
3031 UOps += 2; // One for base reg wb, one for write to pc.
3032 break;
3033 }
3034 return UOps;
Tim Northover0feb91e2014-04-01 14:10:07 +00003035 } else if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Chengdebf9c52010-11-03 00:45:17 +00003036 if (NumRegs < 4)
3037 return 2;
3038 // 4 registers would be issued: 2, 2.
3039 // 5 registers would be issued: 2, 2, 1.
Andrew Trickf161e392012-07-02 18:10:42 +00003040 int A8UOps = (NumRegs / 2);
Evan Chengdebf9c52010-11-03 00:45:17 +00003041 if (NumRegs % 2)
Andrew Trickf161e392012-07-02 18:10:42 +00003042 ++A8UOps;
3043 return A8UOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003044 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Andrew Trickf161e392012-07-02 18:10:42 +00003045 int A9UOps = (NumRegs / 2);
Evan Chengbf407072010-09-10 01:29:16 +00003046 // If there are odd number of registers or if it's not 64-bit aligned,
3047 // then it takes an extra AGU (Address Generation Unit) cycle.
3048 if ((NumRegs % 2) ||
3049 !MI->hasOneMemOperand() ||
3050 (*MI->memoperands_begin())->getAlignment() < 8)
Andrew Trickf161e392012-07-02 18:10:42 +00003051 ++A9UOps;
3052 return A9UOps;
Evan Chengbf407072010-09-10 01:29:16 +00003053 } else {
3054 // Assume the worst.
3055 return NumRegs;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00003056 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003057 }
3058 }
3059}
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003060
3061int
Evan Cheng412e37b2010-10-07 23:12:15 +00003062ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003063 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003064 unsigned DefClass,
3065 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003066 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003067 if (RegNo <= 0)
3068 // Def is the address writeback.
3069 return ItinData->getOperandCycle(DefClass, DefIdx);
3070
3071 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003072 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003073 // (regno / 2) + (regno % 2) + 1
3074 DefCycle = RegNo / 2 + 1;
3075 if (RegNo % 2)
3076 ++DefCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003077 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003078 DefCycle = RegNo;
3079 bool isSLoad = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003080
Evan Cheng6cc775f2011-06-28 19:10:37 +00003081 switch (DefMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003082 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003083 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003084 case ARM::VLDMSIA_UPD:
3085 case ARM::VLDMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003086 isSLoad = true;
3087 break;
3088 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003089
Evan Cheng412e37b2010-10-07 23:12:15 +00003090 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3091 // then it takes an extra cycle.
3092 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3093 ++DefCycle;
3094 } else {
3095 // Assume the worst.
3096 DefCycle = RegNo + 2;
3097 }
3098
3099 return DefCycle;
3100}
3101
3102int
3103ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003104 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003105 unsigned DefClass,
3106 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003107 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003108 if (RegNo <= 0)
3109 // Def is the address writeback.
3110 return ItinData->getOperandCycle(DefClass, DefIdx);
3111
3112 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003113 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003114 // 4 registers would be issued: 1, 2, 1.
3115 // 5 registers would be issued: 1, 2, 2.
3116 DefCycle = RegNo / 2;
3117 if (DefCycle < 1)
3118 DefCycle = 1;
3119 // Result latency is issue cycle + 2: E2.
3120 DefCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003121 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003122 DefCycle = (RegNo / 2);
3123 // If there are odd number of registers or if it's not 64-bit aligned,
3124 // then it takes an extra AGU (Address Generation Unit) cycle.
3125 if ((RegNo % 2) || DefAlign < 8)
3126 ++DefCycle;
3127 // Result latency is AGU cycles + 2.
3128 DefCycle += 2;
3129 } else {
3130 // Assume the worst.
3131 DefCycle = RegNo + 2;
3132 }
3133
3134 return DefCycle;
3135}
3136
3137int
3138ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003139 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003140 unsigned UseClass,
3141 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003142 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003143 if (RegNo <= 0)
3144 return ItinData->getOperandCycle(UseClass, UseIdx);
3145
3146 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003147 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003148 // (regno / 2) + (regno % 2) + 1
3149 UseCycle = RegNo / 2 + 1;
3150 if (RegNo % 2)
3151 ++UseCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003152 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003153 UseCycle = RegNo;
3154 bool isSStore = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003155
Evan Cheng6cc775f2011-06-28 19:10:37 +00003156 switch (UseMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003157 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003158 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003159 case ARM::VSTMSIA_UPD:
3160 case ARM::VSTMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003161 isSStore = true;
3162 break;
3163 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003164
Evan Cheng412e37b2010-10-07 23:12:15 +00003165 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3166 // then it takes an extra cycle.
3167 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3168 ++UseCycle;
3169 } else {
3170 // Assume the worst.
3171 UseCycle = RegNo + 2;
3172 }
3173
3174 return UseCycle;
3175}
3176
3177int
3178ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003179 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003180 unsigned UseClass,
3181 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003182 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003183 if (RegNo <= 0)
3184 return ItinData->getOperandCycle(UseClass, UseIdx);
3185
3186 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003187 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003188 UseCycle = RegNo / 2;
3189 if (UseCycle < 2)
3190 UseCycle = 2;
3191 // Read in E3.
3192 UseCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003193 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003194 UseCycle = (RegNo / 2);
3195 // If there are odd number of registers or if it's not 64-bit aligned,
3196 // then it takes an extra AGU (Address Generation Unit) cycle.
3197 if ((RegNo % 2) || UseAlign < 8)
3198 ++UseCycle;
3199 } else {
3200 // Assume the worst.
3201 UseCycle = 1;
3202 }
3203 return UseCycle;
3204}
3205
3206int
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003207ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003208 const MCInstrDesc &DefMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003209 unsigned DefIdx, unsigned DefAlign,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003210 const MCInstrDesc &UseMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003211 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003212 unsigned DefClass = DefMCID.getSchedClass();
3213 unsigned UseClass = UseMCID.getSchedClass();
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003214
Evan Cheng6cc775f2011-06-28 19:10:37 +00003215 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003216 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3217
3218 // This may be a def / use of a variable_ops instruction, the operand
3219 // latency might be determinable dynamically. Let the target try to
3220 // figure it out.
Evan Chenge2c211c2010-10-28 02:00:25 +00003221 int DefCycle = -1;
Evan Chengff310732010-10-28 06:47:08 +00003222 bool LdmBypass = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003223 switch (DefMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003224 default:
3225 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3226 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003227
3228 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003229 case ARM::VLDMDIA_UPD:
3230 case ARM::VLDMDDB_UPD:
3231 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003232 case ARM::VLDMSIA_UPD:
3233 case ARM::VLDMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003234 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003235 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003236
3237 case ARM::LDMIA_RET:
3238 case ARM::LDMIA:
3239 case ARM::LDMDA:
3240 case ARM::LDMDB:
3241 case ARM::LDMIB:
3242 case ARM::LDMIA_UPD:
3243 case ARM::LDMDA_UPD:
3244 case ARM::LDMDB_UPD:
3245 case ARM::LDMIB_UPD:
3246 case ARM::tLDMIA:
3247 case ARM::tLDMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003248 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003249 case ARM::t2LDMIA_RET:
3250 case ARM::t2LDMIA:
3251 case ARM::t2LDMDB:
3252 case ARM::t2LDMIA_UPD:
3253 case ARM::t2LDMDB_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003254 LdmBypass = 1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003255 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng412e37b2010-10-07 23:12:15 +00003256 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003257 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003258
3259 if (DefCycle == -1)
3260 // We can't seem to determine the result latency of the def, assume it's 2.
3261 DefCycle = 2;
3262
3263 int UseCycle = -1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003264 switch (UseMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003265 default:
3266 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3267 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003268
3269 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003270 case ARM::VSTMDIA_UPD:
3271 case ARM::VSTMDDB_UPD:
3272 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003273 case ARM::VSTMSIA_UPD:
3274 case ARM::VSTMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003275 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003276 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003277
3278 case ARM::STMIA:
3279 case ARM::STMDA:
3280 case ARM::STMDB:
3281 case ARM::STMIB:
3282 case ARM::STMIA_UPD:
3283 case ARM::STMDA_UPD:
3284 case ARM::STMDB_UPD:
3285 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003286 case ARM::tSTMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003287 case ARM::tPOP_RET:
3288 case ARM::tPOP:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003289 case ARM::t2STMIA:
3290 case ARM::t2STMDB:
3291 case ARM::t2STMIA_UPD:
3292 case ARM::t2STMDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003293 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003294 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003295 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003296
3297 if (UseCycle == -1)
3298 // Assume it's read in the first stage.
3299 UseCycle = 1;
3300
3301 UseCycle = DefCycle - UseCycle + 1;
3302 if (UseCycle > 0) {
3303 if (LdmBypass) {
3304 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3305 // first def operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003306 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003307 UseClass, UseIdx))
3308 --UseCycle;
3309 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003310 UseClass, UseIdx)) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003311 --UseCycle;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003312 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003313 }
3314
3315 return UseCycle;
3316}
3317
Evan Cheng7fae11b2011-12-14 02:11:42 +00003318static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003319 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003320 unsigned &DefIdx, unsigned &Dist) {
3321 Dist = 0;
3322
3323 MachineBasicBlock::const_iterator I = MI; ++I;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003324 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
Evan Cheng7fae11b2011-12-14 02:11:42 +00003325 assert(II->isInsideBundle() && "Empty bundle?");
3326
3327 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003328 while (II->isInsideBundle()) {
3329 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3330 if (Idx != -1)
3331 break;
3332 --II;
3333 ++Dist;
3334 }
3335
3336 assert(Idx != -1 && "Cannot find bundled definition!");
3337 DefIdx = Idx;
3338 return II;
3339}
3340
3341static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003342 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003343 unsigned &UseIdx, unsigned &Dist) {
3344 Dist = 0;
3345
3346 MachineBasicBlock::const_instr_iterator II = MI; ++II;
3347 assert(II->isInsideBundle() && "Empty bundle?");
3348 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3349
3350 // FIXME: This doesn't properly handle multiple uses.
3351 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003352 while (II != E && II->isInsideBundle()) {
3353 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3354 if (Idx != -1)
3355 break;
3356 if (II->getOpcode() != ARM::t2IT)
3357 ++Dist;
3358 ++II;
3359 }
3360
Evan Chengda103bf2011-12-14 20:00:08 +00003361 if (Idx == -1) {
3362 Dist = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003363 return nullptr;
Evan Chengda103bf2011-12-14 20:00:08 +00003364 }
3365
Evan Cheng7fae11b2011-12-14 02:11:42 +00003366 UseIdx = Idx;
3367 return II;
3368}
3369
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003370/// Return the number of cycles to add to (or subtract from) the static
3371/// itinerary based on the def opcode and alignment. The caller will ensure that
3372/// adjusted latency is at least one cycle.
3373static int adjustDefLatency(const ARMSubtarget &Subtarget,
3374 const MachineInstr *DefMI,
3375 const MCInstrDesc *DefMCID, unsigned DefAlign) {
3376 int Adjust = 0;
Tim Northover0feb91e2014-04-01 14:10:07 +00003377 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
Evan Chengff310732010-10-28 06:47:08 +00003378 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3379 // variants are one cycle cheaper.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003380 switch (DefMCID->getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003381 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003382 case ARM::LDRrs:
3383 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003384 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3385 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3386 if (ShImm == 0 ||
3387 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003388 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003389 break;
3390 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003391 case ARM::t2LDRs:
3392 case ARM::t2LDRBs:
3393 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003394 case ARM::t2LDRSHs: {
3395 // Thumb2 mode: lsl only.
3396 unsigned ShAmt = DefMI->getOperand(3).getImm();
3397 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003398 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003399 break;
3400 }
3401 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003402 } else if (Subtarget.isSwift()) {
3403 // FIXME: Properly handle all of the latency adjustments for address
3404 // writeback.
3405 switch (DefMCID->getOpcode()) {
3406 default: break;
3407 case ARM::LDRrs:
3408 case ARM::LDRBrs: {
3409 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3410 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3411 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3412 if (!isSub &&
3413 (ShImm == 0 ||
3414 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3415 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3416 Adjust -= 2;
3417 else if (!isSub &&
3418 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3419 --Adjust;
3420 break;
3421 }
3422 case ARM::t2LDRs:
3423 case ARM::t2LDRBs:
3424 case ARM::t2LDRHs:
3425 case ARM::t2LDRSHs: {
3426 // Thumb2 mode: lsl only.
3427 unsigned ShAmt = DefMI->getOperand(3).getImm();
3428 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3429 Adjust -= 2;
3430 break;
3431 }
3432 }
Evan Chengff310732010-10-28 06:47:08 +00003433 }
3434
Silviu Barangab47bb942012-09-13 15:05:10 +00003435 if (DefAlign < 8 && Subtarget.isLikeA9()) {
Evan Cheng7fae11b2011-12-14 02:11:42 +00003436 switch (DefMCID->getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003437 default: break;
3438 case ARM::VLD1q8:
3439 case ARM::VLD1q16:
3440 case ARM::VLD1q32:
3441 case ARM::VLD1q64:
Jim Grosbach2098cb12011-10-24 21:45:13 +00003442 case ARM::VLD1q8wb_fixed:
3443 case ARM::VLD1q16wb_fixed:
3444 case ARM::VLD1q32wb_fixed:
3445 case ARM::VLD1q64wb_fixed:
3446 case ARM::VLD1q8wb_register:
3447 case ARM::VLD1q16wb_register:
3448 case ARM::VLD1q32wb_register:
3449 case ARM::VLD1q64wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003450 case ARM::VLD2d8:
3451 case ARM::VLD2d16:
3452 case ARM::VLD2d32:
3453 case ARM::VLD2q8:
3454 case ARM::VLD2q16:
3455 case ARM::VLD2q32:
Jim Grosbachd146a022011-12-09 21:28:25 +00003456 case ARM::VLD2d8wb_fixed:
3457 case ARM::VLD2d16wb_fixed:
3458 case ARM::VLD2d32wb_fixed:
3459 case ARM::VLD2q8wb_fixed:
3460 case ARM::VLD2q16wb_fixed:
3461 case ARM::VLD2q32wb_fixed:
3462 case ARM::VLD2d8wb_register:
3463 case ARM::VLD2d16wb_register:
3464 case ARM::VLD2d32wb_register:
3465 case ARM::VLD2q8wb_register:
3466 case ARM::VLD2q16wb_register:
3467 case ARM::VLD2q32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003468 case ARM::VLD3d8:
3469 case ARM::VLD3d16:
3470 case ARM::VLD3d32:
3471 case ARM::VLD1d64T:
3472 case ARM::VLD3d8_UPD:
3473 case ARM::VLD3d16_UPD:
3474 case ARM::VLD3d32_UPD:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00003475 case ARM::VLD1d64Twb_fixed:
3476 case ARM::VLD1d64Twb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003477 case ARM::VLD3q8_UPD:
3478 case ARM::VLD3q16_UPD:
3479 case ARM::VLD3q32_UPD:
3480 case ARM::VLD4d8:
3481 case ARM::VLD4d16:
3482 case ARM::VLD4d32:
3483 case ARM::VLD1d64Q:
3484 case ARM::VLD4d8_UPD:
3485 case ARM::VLD4d16_UPD:
3486 case ARM::VLD4d32_UPD:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00003487 case ARM::VLD1d64Qwb_fixed:
3488 case ARM::VLD1d64Qwb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003489 case ARM::VLD4q8_UPD:
3490 case ARM::VLD4q16_UPD:
3491 case ARM::VLD4q32_UPD:
3492 case ARM::VLD1DUPq8:
3493 case ARM::VLD1DUPq16:
3494 case ARM::VLD1DUPq32:
Jim Grosbacha68c9a82011-11-30 19:35:44 +00003495 case ARM::VLD1DUPq8wb_fixed:
3496 case ARM::VLD1DUPq16wb_fixed:
3497 case ARM::VLD1DUPq32wb_fixed:
3498 case ARM::VLD1DUPq8wb_register:
3499 case ARM::VLD1DUPq16wb_register:
3500 case ARM::VLD1DUPq32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003501 case ARM::VLD2DUPd8:
3502 case ARM::VLD2DUPd16:
3503 case ARM::VLD2DUPd32:
Jim Grosbachc80a2642011-12-21 19:40:55 +00003504 case ARM::VLD2DUPd8wb_fixed:
3505 case ARM::VLD2DUPd16wb_fixed:
3506 case ARM::VLD2DUPd32wb_fixed:
3507 case ARM::VLD2DUPd8wb_register:
3508 case ARM::VLD2DUPd16wb_register:
3509 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003510 case ARM::VLD4DUPd8:
3511 case ARM::VLD4DUPd16:
3512 case ARM::VLD4DUPd32:
3513 case ARM::VLD4DUPd8_UPD:
3514 case ARM::VLD4DUPd16_UPD:
3515 case ARM::VLD4DUPd32_UPD:
3516 case ARM::VLD1LNd8:
3517 case ARM::VLD1LNd16:
3518 case ARM::VLD1LNd32:
3519 case ARM::VLD1LNd8_UPD:
3520 case ARM::VLD1LNd16_UPD:
3521 case ARM::VLD1LNd32_UPD:
3522 case ARM::VLD2LNd8:
3523 case ARM::VLD2LNd16:
3524 case ARM::VLD2LNd32:
3525 case ARM::VLD2LNq16:
3526 case ARM::VLD2LNq32:
3527 case ARM::VLD2LNd8_UPD:
3528 case ARM::VLD2LNd16_UPD:
3529 case ARM::VLD2LNd32_UPD:
3530 case ARM::VLD2LNq16_UPD:
3531 case ARM::VLD2LNq32_UPD:
3532 case ARM::VLD4LNd8:
3533 case ARM::VLD4LNd16:
3534 case ARM::VLD4LNd32:
3535 case ARM::VLD4LNq16:
3536 case ARM::VLD4LNq32:
3537 case ARM::VLD4LNd8_UPD:
3538 case ARM::VLD4LNd16_UPD:
3539 case ARM::VLD4LNd32_UPD:
3540 case ARM::VLD4LNq16_UPD:
3541 case ARM::VLD4LNq32_UPD:
3542 // If the address is not 64-bit aligned, the latencies of these
3543 // instructions increases by one.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003544 ++Adjust;
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003545 break;
3546 }
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003547 }
3548 return Adjust;
3549}
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003550
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003551
3552
3553int
3554ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3555 const MachineInstr *DefMI, unsigned DefIdx,
3556 const MachineInstr *UseMI,
3557 unsigned UseIdx) const {
3558 // No operand latency. The caller may fall back to getInstrLatency.
3559 if (!ItinData || ItinData->isEmpty())
3560 return -1;
3561
3562 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
3563 unsigned Reg = DefMO.getReg();
3564 const MCInstrDesc *DefMCID = &DefMI->getDesc();
3565 const MCInstrDesc *UseMCID = &UseMI->getDesc();
3566
3567 unsigned DefAdj = 0;
3568 if (DefMI->isBundle()) {
3569 DefMI = getBundledDefMI(&getRegisterInfo(), DefMI, Reg, DefIdx, DefAdj);
3570 DefMCID = &DefMI->getDesc();
3571 }
3572 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
3573 DefMI->isRegSequence() || DefMI->isImplicitDef()) {
3574 return 1;
3575 }
3576
3577 unsigned UseAdj = 0;
3578 if (UseMI->isBundle()) {
3579 unsigned NewUseIdx;
3580 const MachineInstr *NewUseMI = getBundledUseMI(&getRegisterInfo(), UseMI,
3581 Reg, NewUseIdx, UseAdj);
Andrew Trick77d0b882012-06-22 02:50:33 +00003582 if (!NewUseMI)
3583 return -1;
3584
3585 UseMI = NewUseMI;
3586 UseIdx = NewUseIdx;
3587 UseMCID = &UseMI->getDesc();
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003588 }
3589
3590 if (Reg == ARM::CPSR) {
3591 if (DefMI->getOpcode() == ARM::FMSTAT) {
3592 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Barangab47bb942012-09-13 15:05:10 +00003593 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003594 }
3595
3596 // CPSR set and branch can be paired in the same cycle.
3597 if (UseMI->isBranch())
3598 return 0;
3599
3600 // Otherwise it takes the instruction latency (generally one).
3601 unsigned Latency = getInstrLatency(ItinData, DefMI);
3602
3603 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3604 // its uses. Instructions which are otherwise scheduled between them may
3605 // incur a code size penalty (not able to use the CPSR setting 16-bit
3606 // instructions).
3607 if (Latency > 0 && Subtarget.isThumb2()) {
3608 const MachineFunction *MF = DefMI->getParent()->getParent();
Bill Wendling698e84f2012-12-30 10:32:01 +00003609 if (MF->getFunction()->getAttributes().
3610 hasAttribute(AttributeSet::FunctionIndex,
3611 Attribute::OptimizeForSize))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003612 --Latency;
3613 }
3614 return Latency;
3615 }
3616
Andrew Trick77d0b882012-06-22 02:50:33 +00003617 if (DefMO.isImplicit() || UseMI->getOperand(UseIdx).isImplicit())
3618 return -1;
3619
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003620 unsigned DefAlign = DefMI->hasOneMemOperand()
3621 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
3622 unsigned UseAlign = UseMI->hasOneMemOperand()
3623 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
3624
3625 // Get the itinerary's latency if possible, and handle variable_ops.
3626 int Latency = getOperandLatency(ItinData, *DefMCID, DefIdx, DefAlign,
3627 *UseMCID, UseIdx, UseAlign);
3628 // Unable to find operand latency. The caller may resort to getInstrLatency.
3629 if (Latency < 0)
3630 return Latency;
3631
3632 // Adjust for IT block position.
3633 int Adj = DefAdj + UseAdj;
3634
3635 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3636 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
3637 if (Adj >= 0 || (int)Latency > -Adj) {
3638 return Latency + Adj;
3639 }
3640 // Return the itinerary latency, which may be zero but not less than zero.
Evan Chengff310732010-10-28 06:47:08 +00003641 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003642}
3643
3644int
3645ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3646 SDNode *DefNode, unsigned DefIdx,
3647 SDNode *UseNode, unsigned UseIdx) const {
3648 if (!DefNode->isMachineOpcode())
3649 return 1;
3650
Evan Cheng6cc775f2011-06-28 19:10:37 +00003651 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trick47ff14b2011-01-21 05:51:33 +00003652
Evan Cheng6cc775f2011-06-28 19:10:37 +00003653 if (isZeroCost(DefMCID.Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +00003654 return 0;
3655
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003656 if (!ItinData || ItinData->isEmpty())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003657 return DefMCID.mayLoad() ? 3 : 1;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003658
Evan Cheng6c1414f2010-10-29 18:09:28 +00003659 if (!UseNode->isMachineOpcode()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003660 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003661 if (Subtarget.isLikeA9() || Subtarget.isSwift())
Evan Cheng6c1414f2010-10-29 18:09:28 +00003662 return Latency <= 2 ? 1 : Latency - 1;
3663 else
3664 return Latency <= 3 ? 1 : Latency - 2;
3665 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003666
Evan Cheng6cc775f2011-06-28 19:10:37 +00003667 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003668 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
3669 unsigned DefAlign = !DefMN->memoperands_empty()
3670 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
3671 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
3672 unsigned UseAlign = !UseMN->memoperands_empty()
3673 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003674 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
3675 UseMCID, UseIdx, UseAlign);
Evan Chengff310732010-10-28 06:47:08 +00003676
3677 if (Latency > 1 &&
Tim Northover0feb91e2014-04-01 14:10:07 +00003678 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
3679 Subtarget.isCortexA7())) {
Evan Chengff310732010-10-28 06:47:08 +00003680 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3681 // variants are one cycle cheaper.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003682 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003683 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003684 case ARM::LDRrs:
3685 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003686 unsigned ShOpVal =
3687 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3688 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3689 if (ShImm == 0 ||
3690 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3691 --Latency;
3692 break;
3693 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003694 case ARM::t2LDRs:
3695 case ARM::t2LDRBs:
3696 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003697 case ARM::t2LDRSHs: {
3698 // Thumb2 mode: lsl only.
3699 unsigned ShAmt =
3700 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3701 if (ShAmt == 0 || ShAmt == 2)
3702 --Latency;
3703 break;
3704 }
3705 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003706 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
3707 // FIXME: Properly handle all of the latency adjustments for address
3708 // writeback.
3709 switch (DefMCID.getOpcode()) {
3710 default: break;
3711 case ARM::LDRrs:
3712 case ARM::LDRBrs: {
3713 unsigned ShOpVal =
3714 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3715 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3716 if (ShImm == 0 ||
3717 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3718 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3719 Latency -= 2;
3720 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3721 --Latency;
3722 break;
3723 }
3724 case ARM::t2LDRs:
3725 case ARM::t2LDRBs:
3726 case ARM::t2LDRHs:
3727 case ARM::t2LDRSHs: {
3728 // Thumb2 mode: lsl 0-3 only.
3729 Latency -= 2;
3730 break;
3731 }
3732 }
Evan Chengff310732010-10-28 06:47:08 +00003733 }
3734
Silviu Barangab47bb942012-09-13 15:05:10 +00003735 if (DefAlign < 8 && Subtarget.isLikeA9())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003736 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003737 default: break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003738 case ARM::VLD1q8:
3739 case ARM::VLD1q16:
3740 case ARM::VLD1q32:
3741 case ARM::VLD1q64:
3742 case ARM::VLD1q8wb_register:
3743 case ARM::VLD1q16wb_register:
3744 case ARM::VLD1q32wb_register:
3745 case ARM::VLD1q64wb_register:
3746 case ARM::VLD1q8wb_fixed:
3747 case ARM::VLD1q16wb_fixed:
3748 case ARM::VLD1q32wb_fixed:
3749 case ARM::VLD1q64wb_fixed:
3750 case ARM::VLD2d8:
3751 case ARM::VLD2d16:
3752 case ARM::VLD2d32:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003753 case ARM::VLD2q8Pseudo:
3754 case ARM::VLD2q16Pseudo:
3755 case ARM::VLD2q32Pseudo:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003756 case ARM::VLD2d8wb_fixed:
3757 case ARM::VLD2d16wb_fixed:
3758 case ARM::VLD2d32wb_fixed:
Jim Grosbachd146a022011-12-09 21:28:25 +00003759 case ARM::VLD2q8PseudoWB_fixed:
3760 case ARM::VLD2q16PseudoWB_fixed:
3761 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003762 case ARM::VLD2d8wb_register:
3763 case ARM::VLD2d16wb_register:
3764 case ARM::VLD2d32wb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00003765 case ARM::VLD2q8PseudoWB_register:
3766 case ARM::VLD2q16PseudoWB_register:
3767 case ARM::VLD2q32PseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003768 case ARM::VLD3d8Pseudo:
3769 case ARM::VLD3d16Pseudo:
3770 case ARM::VLD3d32Pseudo:
3771 case ARM::VLD1d64TPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003772 case ARM::VLD1d64TPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003773 case ARM::VLD3d8Pseudo_UPD:
3774 case ARM::VLD3d16Pseudo_UPD:
3775 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003776 case ARM::VLD3q8Pseudo_UPD:
3777 case ARM::VLD3q16Pseudo_UPD:
3778 case ARM::VLD3q32Pseudo_UPD:
3779 case ARM::VLD3q8oddPseudo:
3780 case ARM::VLD3q16oddPseudo:
3781 case ARM::VLD3q32oddPseudo:
3782 case ARM::VLD3q8oddPseudo_UPD:
3783 case ARM::VLD3q16oddPseudo_UPD:
3784 case ARM::VLD3q32oddPseudo_UPD:
3785 case ARM::VLD4d8Pseudo:
3786 case ARM::VLD4d16Pseudo:
3787 case ARM::VLD4d32Pseudo:
3788 case ARM::VLD1d64QPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003789 case ARM::VLD1d64QPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003790 case ARM::VLD4d8Pseudo_UPD:
3791 case ARM::VLD4d16Pseudo_UPD:
3792 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003793 case ARM::VLD4q8Pseudo_UPD:
3794 case ARM::VLD4q16Pseudo_UPD:
3795 case ARM::VLD4q32Pseudo_UPD:
3796 case ARM::VLD4q8oddPseudo:
3797 case ARM::VLD4q16oddPseudo:
3798 case ARM::VLD4q32oddPseudo:
3799 case ARM::VLD4q8oddPseudo_UPD:
3800 case ARM::VLD4q16oddPseudo_UPD:
3801 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbach13a292c2012-03-06 22:01:44 +00003802 case ARM::VLD1DUPq8:
3803 case ARM::VLD1DUPq16:
3804 case ARM::VLD1DUPq32:
3805 case ARM::VLD1DUPq8wb_fixed:
3806 case ARM::VLD1DUPq16wb_fixed:
3807 case ARM::VLD1DUPq32wb_fixed:
3808 case ARM::VLD1DUPq8wb_register:
3809 case ARM::VLD1DUPq16wb_register:
3810 case ARM::VLD1DUPq32wb_register:
3811 case ARM::VLD2DUPd8:
3812 case ARM::VLD2DUPd16:
3813 case ARM::VLD2DUPd32:
3814 case ARM::VLD2DUPd8wb_fixed:
3815 case ARM::VLD2DUPd16wb_fixed:
3816 case ARM::VLD2DUPd32wb_fixed:
3817 case ARM::VLD2DUPd8wb_register:
3818 case ARM::VLD2DUPd16wb_register:
3819 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003820 case ARM::VLD4DUPd8Pseudo:
3821 case ARM::VLD4DUPd16Pseudo:
3822 case ARM::VLD4DUPd32Pseudo:
3823 case ARM::VLD4DUPd8Pseudo_UPD:
3824 case ARM::VLD4DUPd16Pseudo_UPD:
3825 case ARM::VLD4DUPd32Pseudo_UPD:
3826 case ARM::VLD1LNq8Pseudo:
3827 case ARM::VLD1LNq16Pseudo:
3828 case ARM::VLD1LNq32Pseudo:
3829 case ARM::VLD1LNq8Pseudo_UPD:
3830 case ARM::VLD1LNq16Pseudo_UPD:
3831 case ARM::VLD1LNq32Pseudo_UPD:
3832 case ARM::VLD2LNd8Pseudo:
3833 case ARM::VLD2LNd16Pseudo:
3834 case ARM::VLD2LNd32Pseudo:
3835 case ARM::VLD2LNq16Pseudo:
3836 case ARM::VLD2LNq32Pseudo:
3837 case ARM::VLD2LNd8Pseudo_UPD:
3838 case ARM::VLD2LNd16Pseudo_UPD:
3839 case ARM::VLD2LNd32Pseudo_UPD:
3840 case ARM::VLD2LNq16Pseudo_UPD:
3841 case ARM::VLD2LNq32Pseudo_UPD:
3842 case ARM::VLD4LNd8Pseudo:
3843 case ARM::VLD4LNd16Pseudo:
3844 case ARM::VLD4LNd32Pseudo:
3845 case ARM::VLD4LNq16Pseudo:
3846 case ARM::VLD4LNq32Pseudo:
3847 case ARM::VLD4LNd8Pseudo_UPD:
3848 case ARM::VLD4LNd16Pseudo_UPD:
3849 case ARM::VLD4LNd32Pseudo_UPD:
3850 case ARM::VLD4LNq16Pseudo_UPD:
3851 case ARM::VLD4LNq32Pseudo_UPD:
3852 // If the address is not 64-bit aligned, the latencies of these
3853 // instructions increases by one.
3854 ++Latency;
3855 break;
3856 }
3857
Evan Chengff310732010-10-28 06:47:08 +00003858 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003859}
Evan Cheng63c76082010-10-19 18:58:51 +00003860
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00003861unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr *MI) const {
3862 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3863 MI->isRegSequence() || MI->isImplicitDef())
3864 return 0;
3865
3866 if (MI->isBundle())
3867 return 0;
3868
3869 const MCInstrDesc &MCID = MI->getDesc();
3870
3871 if (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR)) {
3872 // When predicated, CPSR is an additional source operand for CPSR updating
3873 // instructions, this apparently increases their latencies.
3874 return 1;
3875 }
3876 return 0;
3877}
3878
Andrew Trick45446062012-06-05 21:11:27 +00003879unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3880 const MachineInstr *MI,
3881 unsigned *PredCost) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00003882 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3883 MI->isRegSequence() || MI->isImplicitDef())
3884 return 1;
3885
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003886 // An instruction scheduler typically runs on unbundled instructions, however
3887 // other passes may query the latency of a bundled instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003888 if (MI->isBundle()) {
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003889 unsigned Latency = 0;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003890 MachineBasicBlock::const_instr_iterator I = MI;
3891 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3892 while (++I != E && I->isInsideBundle()) {
3893 if (I->getOpcode() != ARM::t2IT)
3894 Latency += getInstrLatency(ItinData, I, PredCost);
3895 }
3896 return Latency;
3897 }
3898
Evan Cheng6cc775f2011-06-28 19:10:37 +00003899 const MCInstrDesc &MCID = MI->getDesc();
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003900 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) {
Evan Chengdebf9c52010-11-03 00:45:17 +00003901 // When predicated, CPSR is an additional source operand for CPSR updating
3902 // instructions, this apparently increases their latencies.
3903 *PredCost = 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003904 }
3905 // Be sure to call getStageLatency for an empty itinerary in case it has a
3906 // valid MinLatency property.
3907 if (!ItinData)
3908 return MI->mayLoad() ? 3 : 1;
3909
3910 unsigned Class = MCID.getSchedClass();
3911
3912 // For instructions with variable uops, use uops as latency.
Andrew Trick21cca972012-07-02 19:12:29 +00003913 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003914 return getNumMicroOps(ItinData, MI);
Andrew Trick21cca972012-07-02 19:12:29 +00003915
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003916 // For the common case, fall back on the itinerary's latency.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003917 unsigned Latency = ItinData->getStageLatency(Class);
3918
3919 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3920 unsigned DefAlign = MI->hasOneMemOperand()
3921 ? (*MI->memoperands_begin())->getAlignment() : 0;
3922 int Adj = adjustDefLatency(Subtarget, MI, &MCID, DefAlign);
3923 if (Adj >= 0 || (int)Latency > -Adj) {
3924 return Latency + Adj;
3925 }
3926 return Latency;
Evan Chengdebf9c52010-11-03 00:45:17 +00003927}
3928
3929int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3930 SDNode *Node) const {
3931 if (!Node->isMachineOpcode())
3932 return 1;
3933
3934 if (!ItinData || ItinData->isEmpty())
3935 return 1;
3936
3937 unsigned Opcode = Node->getMachineOpcode();
3938 switch (Opcode) {
3939 default:
3940 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003941 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003942 case ARM::VSTMQIA:
Evan Chengdebf9c52010-11-03 00:45:17 +00003943 return 2;
Eric Christopherb006fc92010-11-18 19:40:05 +00003944 }
Evan Chengdebf9c52010-11-03 00:45:17 +00003945}
3946
Evan Cheng63c76082010-10-19 18:58:51 +00003947bool ARMBaseInstrInfo::
3948hasHighOperandLatency(const InstrItineraryData *ItinData,
3949 const MachineRegisterInfo *MRI,
3950 const MachineInstr *DefMI, unsigned DefIdx,
3951 const MachineInstr *UseMI, unsigned UseIdx) const {
3952 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
3953 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
3954 if (Subtarget.isCortexA8() &&
3955 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
3956 // CortexA8 VFP instructions are not pipelined.
3957 return true;
3958
3959 // Hoist VFP / NEON instructions with 4 or higher latency.
Andrew Trickde2109e2013-06-15 04:49:57 +00003960 int Latency = computeOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
Andrew Trick3564bdf2012-06-07 19:41:58 +00003961 if (Latency < 0)
3962 Latency = getInstrLatency(ItinData, DefMI);
Evan Cheng63c76082010-10-19 18:58:51 +00003963 if (Latency <= 3)
3964 return false;
3965 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
3966 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
3967}
Evan Chenge96b8d72010-10-26 02:08:50 +00003968
3969bool ARMBaseInstrInfo::
3970hasLowDefLatency(const InstrItineraryData *ItinData,
3971 const MachineInstr *DefMI, unsigned DefIdx) const {
3972 if (!ItinData || ItinData->isEmpty())
3973 return false;
3974
3975 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
3976 if (DDomain == ARMII::DomainGeneral) {
3977 unsigned DefClass = DefMI->getDesc().getSchedClass();
3978 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3979 return (DefCycle != -1 && DefCycle <= 2);
3980 }
3981 return false;
3982}
Evan Cheng62c7b5b2010-12-05 22:04:16 +00003983
Andrew Trick924123a2011-09-21 02:20:46 +00003984bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr *MI,
3985 StringRef &ErrInfo) const {
3986 if (convertAddSubFlagsOpcode(MI->getOpcode())) {
3987 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
3988 return false;
3989 }
3990 return true;
3991}
3992
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00003993// LoadStackGuard has so far only been implemented for MachO. Different code
3994// sequence is needed for other targets.
3995void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
3996 unsigned LoadImmOpc,
3997 unsigned LoadOpc,
3998 Reloc::Model RM) const {
3999 MachineBasicBlock &MBB = *MI->getParent();
4000 DebugLoc DL = MI->getDebugLoc();
4001 unsigned Reg = MI->getOperand(0).getReg();
4002 const GlobalValue *GV =
4003 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4004 MachineInstrBuilder MIB;
4005
4006 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4007 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4008
4009 if (Subtarget.GVIsIndirectSymbol(GV, RM)) {
4010 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4011 MIB.addReg(Reg, RegState::Kill).addImm(0);
4012 unsigned Flag = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant;
4013 MachineMemOperand *MMO = MBB.getParent()->
4014 getMachineMemOperand(MachinePointerInfo::getGOT(), Flag, 4, 4);
4015 MIB.addMemOperand(MMO);
4016 AddDefaultPred(MIB);
4017 }
4018
4019 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4020 MIB.addReg(Reg, RegState::Kill).addImm(0);
4021 MIB.setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
4022 AddDefaultPred(MIB);
4023}
4024
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004025bool
4026ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4027 unsigned &AddSubOpc,
4028 bool &NegAcc, bool &HasLane) const {
4029 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4030 if (I == MLxEntryMap.end())
4031 return false;
4032
4033 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4034 MulOpc = Entry.MulOpc;
4035 AddSubOpc = Entry.AddSubOpc;
4036 NegAcc = Entry.NegAcc;
4037 HasLane = Entry.HasLane;
4038 return true;
4039}
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004040
4041//===----------------------------------------------------------------------===//
4042// Execution domains.
4043//===----------------------------------------------------------------------===//
4044//
4045// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4046// and some can go down both. The vmov instructions go down the VFP pipeline,
4047// but they can be changed to vorr equivalents that are executed by the NEON
4048// pipeline.
4049//
4050// We use the following execution domain numbering:
4051//
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004052enum ARMExeDomain {
4053 ExeGeneric = 0,
4054 ExeVFP = 1,
4055 ExeNEON = 2
4056};
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004057//
4058// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4059//
4060std::pair<uint16_t, uint16_t>
4061ARMBaseInstrInfo::getExecutionDomain(const MachineInstr *MI) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004062 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4063 // if they are not predicated.
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004064 if (MI->getOpcode() == ARM::VMOVD && !isPredicated(MI))
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004065 return std::make_pair(ExeVFP, (1<<ExeVFP) | (1<<ExeNEON));
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004066
Silviu Barangadc453362013-03-27 12:38:44 +00004067 // CortexA9 is particularly picky about mixing the two and wants these
Tim Northoverf6618152012-08-17 11:32:52 +00004068 // converted.
Silviu Barangadc453362013-03-27 12:38:44 +00004069 if (Subtarget.isCortexA9() && !isPredicated(MI) &&
Tim Northoverf6618152012-08-17 11:32:52 +00004070 (MI->getOpcode() == ARM::VMOVRS ||
Tim Northoverca9f3842012-08-30 10:17:45 +00004071 MI->getOpcode() == ARM::VMOVSR ||
4072 MI->getOpcode() == ARM::VMOVS))
Tim Northoverf6618152012-08-17 11:32:52 +00004073 return std::make_pair(ExeVFP, (1<<ExeVFP) | (1<<ExeNEON));
4074
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004075 // No other instructions can be swizzled, so just determine their domain.
4076 unsigned Domain = MI->getDesc().TSFlags & ARMII::DomainMask;
4077
4078 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004079 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004080
4081 // Certain instructions can go either way on Cortex-A8.
4082 // Treat them as NEON instructions.
4083 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004084 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004085
4086 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004087 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004088
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004089 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004090}
4091
Tim Northover771f1602012-08-29 16:36:07 +00004092static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4093 unsigned SReg, unsigned &Lane) {
4094 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4095 Lane = 0;
4096
4097 if (DReg != ARM::NoRegister)
4098 return DReg;
4099
4100 Lane = 1;
4101 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4102
4103 assert(DReg && "S-register with no D super-register?");
4104 return DReg;
4105}
4106
Andrew Trickd9296ec2012-10-10 05:43:01 +00004107/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloyea052562012-09-18 08:31:15 +00004108/// set ImplicitSReg to a register number that must be marked as implicit-use or
4109/// zero if no register needs to be defined as implicit-use.
4110///
4111/// If the function cannot determine if an SPR should be marked implicit use or
4112/// not, it returns false.
4113///
4114/// This function handles cases where an instruction is being modified from taking
Andrew Trickd9296ec2012-10-10 05:43:01 +00004115/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloyea052562012-09-18 08:31:15 +00004116/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4117/// lane of the DPR).
4118///
4119/// If the other SPR is defined, an implicit-use of it should be added. Else,
4120/// (including the case where the DPR itself is defined), it should not.
Andrew Trickd9296ec2012-10-10 05:43:01 +00004121///
James Molloyea052562012-09-18 08:31:15 +00004122static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
4123 MachineInstr *MI,
4124 unsigned DReg, unsigned Lane,
4125 unsigned &ImplicitSReg) {
4126 // If the DPR is defined or used already, the other SPR lane will be chained
4127 // correctly, so there is nothing to be done.
4128 if (MI->definesRegister(DReg, TRI) || MI->readsRegister(DReg, TRI)) {
4129 ImplicitSReg = 0;
4130 return true;
4131 }
4132
4133 // Otherwise we need to go searching to see if the SPR is set explicitly.
4134 ImplicitSReg = TRI->getSubReg(DReg,
4135 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4136 MachineBasicBlock::LivenessQueryResult LQR =
4137 MI->getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
4138
4139 if (LQR == MachineBasicBlock::LQR_Live)
4140 return true;
4141 else if (LQR == MachineBasicBlock::LQR_Unknown)
4142 return false;
4143
4144 // If the register is known not to be live, there is no need to add an
4145 // implicit-use.
4146 ImplicitSReg = 0;
4147 return true;
4148}
Tim Northover771f1602012-08-29 16:36:07 +00004149
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004150void
4151ARMBaseInstrInfo::setExecutionDomain(MachineInstr *MI, unsigned Domain) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004152 unsigned DstReg, SrcReg, DReg;
4153 unsigned Lane;
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00004154 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Tim Northoverf6618152012-08-17 11:32:52 +00004155 const TargetRegisterInfo *TRI = &getRegisterInfo();
Tim Northoverf6618152012-08-17 11:32:52 +00004156 switch (MI->getOpcode()) {
4157 default:
4158 llvm_unreachable("cannot handle opcode!");
4159 break;
4160 case ARM::VMOVD:
4161 if (Domain != ExeNEON)
4162 break;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004163
Tim Northoverf6618152012-08-17 11:32:52 +00004164 // Zap the predicate operands.
4165 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004166
Tim Northover771f1602012-08-29 16:36:07 +00004167 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4168 DstReg = MI->getOperand(0).getReg();
4169 SrcReg = MI->getOperand(1).getReg();
4170
4171 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4172 MI->RemoveOperand(i-1);
4173
4174 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004175 MI->setDesc(get(ARM::VORRd));
Tim Northover771f1602012-08-29 16:36:07 +00004176 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4177 .addReg(SrcReg)
4178 .addReg(SrcReg));
Tim Northoverf6618152012-08-17 11:32:52 +00004179 break;
4180 case ARM::VMOVRS:
4181 if (Domain != ExeNEON)
4182 break;
4183 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
4184
Tim Northover771f1602012-08-29 16:36:07 +00004185 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004186 DstReg = MI->getOperand(0).getReg();
4187 SrcReg = MI->getOperand(1).getReg();
4188
Tim Northover771f1602012-08-29 16:36:07 +00004189 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4190 MI->RemoveOperand(i-1);
Tim Northoverf6618152012-08-17 11:32:52 +00004191
Tim Northover771f1602012-08-29 16:36:07 +00004192 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northoverf6618152012-08-17 11:32:52 +00004193
Tim Northover771f1602012-08-29 16:36:07 +00004194 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4195 // Note that DSrc has been widened and the other lane may be undef, which
4196 // contaminates the entire register.
Tim Northoverf6618152012-08-17 11:32:52 +00004197 MI->setDesc(get(ARM::VGETLNi32));
Tim Northover771f1602012-08-29 16:36:07 +00004198 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4199 .addReg(DReg, RegState::Undef)
4200 .addImm(Lane));
Tim Northoverf6618152012-08-17 11:32:52 +00004201
Tim Northover771f1602012-08-29 16:36:07 +00004202 // The old source should be an implicit use, otherwise we might think it
4203 // was dead before here.
Tim Northoverf6618152012-08-17 11:32:52 +00004204 MIB.addReg(SrcReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004205 break;
James Molloyea052562012-09-18 08:31:15 +00004206 case ARM::VMOVSR: {
Tim Northoverf6618152012-08-17 11:32:52 +00004207 if (Domain != ExeNEON)
4208 break;
4209 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
4210
Tim Northover771f1602012-08-29 16:36:07 +00004211 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004212 DstReg = MI->getOperand(0).getReg();
4213 SrcReg = MI->getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004214
Tim Northover771f1602012-08-29 16:36:07 +00004215 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
4216
James Molloyea052562012-09-18 08:31:15 +00004217 unsigned ImplicitSReg;
4218 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
4219 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004220
Tim Northoverc8d867d2012-09-05 18:37:53 +00004221 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4222 MI->RemoveOperand(i-1);
4223
Tim Northover771f1602012-08-29 16:36:07 +00004224 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4225 // Again DDst may be undefined at the beginning of this instruction.
Tim Northoverf6618152012-08-17 11:32:52 +00004226 MI->setDesc(get(ARM::VSETLNi32));
Tim Northover726d32c2012-09-01 18:07:29 +00004227 MIB.addReg(DReg, RegState::Define)
4228 .addReg(DReg, getUndefRegState(!MI->readsRegister(DReg, TRI)))
4229 .addReg(SrcReg)
4230 .addImm(Lane);
4231 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004232
Tim Northover726d32c2012-09-01 18:07:29 +00004233 // The narrower destination must be marked as set to keep previous chains
4234 // in place.
Tim Northover771f1602012-08-29 16:36:07 +00004235 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004236 if (ImplicitSReg != 0)
4237 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004238 break;
James Molloyea052562012-09-18 08:31:15 +00004239 }
Tim Northoverca9f3842012-08-30 10:17:45 +00004240 case ARM::VMOVS: {
4241 if (Domain != ExeNEON)
4242 break;
4243
4244 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
4245 DstReg = MI->getOperand(0).getReg();
4246 SrcReg = MI->getOperand(1).getReg();
4247
Tim Northoverca9f3842012-08-30 10:17:45 +00004248 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4249 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4250 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4251
James Molloyea052562012-09-18 08:31:15 +00004252 unsigned ImplicitSReg;
4253 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4254 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004255
Tim Northoverc8d867d2012-09-05 18:37:53 +00004256 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4257 MI->RemoveOperand(i-1);
4258
Tim Northoverca9f3842012-08-30 10:17:45 +00004259 if (DSrc == DDst) {
4260 // Destination can be:
4261 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
4262 MI->setDesc(get(ARM::VDUPLN32d));
Tim Northover726d32c2012-09-01 18:07:29 +00004263 MIB.addReg(DDst, RegState::Define)
4264 .addReg(DDst, getUndefRegState(!MI->readsRegister(DDst, TRI)))
4265 .addImm(SrcLane);
4266 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004267
4268 // Neither the source or the destination are naturally represented any
4269 // more, so add them in manually.
4270 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4271 MIB.addReg(SrcReg, RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004272 if (ImplicitSReg != 0)
4273 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004274 break;
4275 }
4276
4277 // In general there's no single instruction that can perform an S <-> S
4278 // move in NEON space, but a pair of VEXT instructions *can* do the
4279 // job. It turns out that the VEXTs needed will only use DSrc once, with
4280 // the position based purely on the combination of lane-0 and lane-1
4281 // involved. For example
4282 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4283 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4284 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4285 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4286 //
4287 // Pattern of the MachineInstrs is:
4288 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4289 MachineInstrBuilder NewMIB;
4290 NewMIB = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4291 get(ARM::VEXTd32), DDst);
Tim Northover726d32c2012-09-01 18:07:29 +00004292
4293 // On the first instruction, both DSrc and DDst may be <undef> if present.
4294 // Specifically when the original instruction didn't have them as an
4295 // <imp-use>.
4296 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
4297 bool CurUndef = !MI->readsRegister(CurReg, TRI);
4298 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4299
4300 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
4301 CurUndef = !MI->readsRegister(CurReg, TRI);
4302 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4303
Tim Northoverca9f3842012-08-30 10:17:45 +00004304 NewMIB.addImm(1);
4305 AddDefaultPred(NewMIB);
4306
4307 if (SrcLane == DstLane)
4308 NewMIB.addReg(SrcReg, RegState::Implicit);
4309
4310 MI->setDesc(get(ARM::VEXTd32));
4311 MIB.addReg(DDst, RegState::Define);
Tim Northover726d32c2012-09-01 18:07:29 +00004312
4313 // On the second instruction, DDst has definitely been defined above, so
4314 // it is not <undef>. DSrc, if present, can be <undef> as above.
4315 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
4316 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4317 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4318
4319 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
4320 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4321 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4322
Tim Northoverca9f3842012-08-30 10:17:45 +00004323 MIB.addImm(1);
4324 AddDefaultPred(MIB);
4325
4326 if (SrcLane != DstLane)
4327 MIB.addReg(SrcReg, RegState::Implicit);
4328
4329 // As before, the original destination is no longer represented, add it
4330 // implicitly.
4331 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004332 if (ImplicitSReg != 0)
4333 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004334 break;
4335 }
Tim Northoverf6618152012-08-17 11:32:52 +00004336 }
4337
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004338}
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004339
Bob Wilsone8a549c2012-09-29 21:43:49 +00004340//===----------------------------------------------------------------------===//
4341// Partial register updates
4342//===----------------------------------------------------------------------===//
4343//
4344// Swift renames NEON registers with 64-bit granularity. That means any
4345// instruction writing an S-reg implicitly reads the containing D-reg. The
4346// problem is mostly avoided by translating f32 operations to v2f32 operations
4347// on D-registers, but f32 loads are still a problem.
4348//
4349// These instructions can load an f32 into a NEON register:
4350//
4351// VLDRS - Only writes S, partial D update.
4352// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4353// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4354//
4355// FCONSTD can be used as a dependency-breaking instruction.
Bob Wilsone8a549c2012-09-29 21:43:49 +00004356unsigned ARMBaseInstrInfo::
4357getPartialRegUpdateClearance(const MachineInstr *MI,
4358 unsigned OpNum,
4359 const TargetRegisterInfo *TRI) const {
Silviu Barangadc453362013-03-27 12:38:44 +00004360 if (!SwiftPartialUpdateClearance ||
4361 !(Subtarget.isSwift() || Subtarget.isCortexA15()))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004362 return 0;
4363
4364 assert(TRI && "Need TRI instance");
4365
4366 const MachineOperand &MO = MI->getOperand(OpNum);
4367 if (MO.readsReg())
4368 return 0;
4369 unsigned Reg = MO.getReg();
4370 int UseOp = -1;
4371
4372 switch(MI->getOpcode()) {
4373 // Normal instructions writing only an S-register.
4374 case ARM::VLDRS:
4375 case ARM::FCONSTS:
4376 case ARM::VMOVSR:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004377 case ARM::VMOVv8i8:
4378 case ARM::VMOVv4i16:
4379 case ARM::VMOVv2i32:
4380 case ARM::VMOVv2f32:
4381 case ARM::VMOVv1i64:
4382 UseOp = MI->findRegisterUseOperandIdx(Reg, false, TRI);
4383 break;
4384
4385 // Explicitly reads the dependency.
4386 case ARM::VLD1LNd32:
Silviu Barangadc453362013-03-27 12:38:44 +00004387 UseOp = 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004388 break;
4389 default:
4390 return 0;
4391 }
4392
4393 // If this instruction actually reads a value from Reg, there is no unwanted
4394 // dependency.
4395 if (UseOp != -1 && MI->getOperand(UseOp).readsReg())
4396 return 0;
4397
4398 // We must be able to clobber the whole D-reg.
4399 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4400 // Virtual register must be a foo:ssub_0<def,undef> operand.
4401 if (!MO.getSubReg() || MI->readsVirtualRegister(Reg))
4402 return 0;
4403 } else if (ARM::SPRRegClass.contains(Reg)) {
4404 // Physical register: MI must define the full D-reg.
4405 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4406 &ARM::DPRRegClass);
4407 if (!DReg || !MI->definesRegister(DReg, TRI))
4408 return 0;
4409 }
4410
4411 // MI has an unwanted D-register dependency.
4412 // Avoid defs in the previous N instructrions.
4413 return SwiftPartialUpdateClearance;
4414}
4415
4416// Break a partial register dependency after getPartialRegUpdateClearance
4417// returned non-zero.
4418void ARMBaseInstrInfo::
4419breakPartialRegDependency(MachineBasicBlock::iterator MI,
4420 unsigned OpNum,
4421 const TargetRegisterInfo *TRI) const {
4422 assert(MI && OpNum < MI->getDesc().getNumDefs() && "OpNum is not a def");
4423 assert(TRI && "Need TRI instance");
4424
4425 const MachineOperand &MO = MI->getOperand(OpNum);
4426 unsigned Reg = MO.getReg();
4427 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4428 "Can't break virtual register dependencies.");
4429 unsigned DReg = Reg;
4430
4431 // If MI defines an S-reg, find the corresponding D super-register.
4432 if (ARM::SPRRegClass.contains(Reg)) {
4433 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4434 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4435 }
4436
4437 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
4438 assert(MI->definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
4439
4440 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4441 // the full D-register by loading the same value to both lanes. The
4442 // instruction is micro-coded with 2 uops, so don't do this until we can
Robert Wilhelm516be562013-09-14 09:34:24 +00004443 // properly schedule micro-coded instructions. The dispatcher stalls cause
Bob Wilsone8a549c2012-09-29 21:43:49 +00004444 // too big regressions.
4445
4446 // Insert the dependency-breaking FCONSTD before MI.
4447 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
4448 AddDefaultPred(BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4449 get(ARM::FCONSTD), DReg).addImm(96));
4450 MI->addRegisterKilled(DReg, TRI, true);
4451}
4452
Tom Roeder44cb65f2014-06-05 19:29:43 +00004453void ARMBaseInstrInfo::getUnconditionalBranch(
4454 MCInst &Branch, const MCSymbolRefExpr *BranchTarget) const {
4455 if (Subtarget.isThumb())
4456 Branch.setOpcode(ARM::tB);
4457 else if (Subtarget.isThumb2())
4458 Branch.setOpcode(ARM::t2B);
4459 else
4460 Branch.setOpcode(ARM::Bcc);
4461
4462 Branch.addOperand(MCOperand::CreateExpr(BranchTarget));
4463 Branch.addOperand(MCOperand::CreateImm(ARMCC::AL));
4464 Branch.addOperand(MCOperand::CreateReg(0));
4465}
4466
4467void ARMBaseInstrInfo::getTrap(MCInst &MI) const {
4468 if (Subtarget.isThumb())
4469 MI.setOpcode(ARM::tTRAP);
4470 else if (Subtarget.useNaClTrap())
4471 MI.setOpcode(ARM::TRAPNaCl);
4472 else
4473 MI.setOpcode(ARM::TRAP);
4474}
4475
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004476bool ARMBaseInstrInfo::hasNOP() const {
4477 return (Subtarget.getFeatureBits() & ARM::HasV6T2Ops) != 0;
4478}
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004479
4480bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
Arnold Schwaighofere9375922013-06-05 14:59:36 +00004481 if (MI->getNumOperands() < 4)
4482 return true;
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004483 unsigned ShOpVal = MI->getOperand(3).getImm();
4484 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4485 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4486 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4487 ((ShImm == 1 || ShImm == 2) &&
4488 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
4489 return true;
4490
4491 return false;
4492}
Quentin Colombetd358e842014-08-22 18:05:22 +00004493
4494bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
4495 const MachineInstr &MI, unsigned DefIdx,
4496 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
4497 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4498 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
4499
4500 switch (MI.getOpcode()) {
4501 case ARM::VMOVDRR:
4502 // dX = VMOVDRR rY, rZ
4503 // is the same as:
4504 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
4505 // Populate the InputRegs accordingly.
4506 // rY
4507 const MachineOperand *MOReg = &MI.getOperand(1);
4508 InputRegs.push_back(
4509 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0));
4510 // rZ
4511 MOReg = &MI.getOperand(2);
4512 InputRegs.push_back(
4513 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1));
4514 return true;
4515 }
4516 llvm_unreachable("Target dependent opcode missing");
4517}
4518
4519bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
4520 const MachineInstr &MI, unsigned DefIdx,
4521 RegSubRegPairAndIdx &InputReg) const {
4522 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4523 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
4524
4525 switch (MI.getOpcode()) {
4526 case ARM::VMOVRRD:
4527 // rX, rY = VMOVRRD dZ
4528 // is the same as:
4529 // rX = EXTRACT_SUBREG dZ, ssub_0
4530 // rY = EXTRACT_SUBREG dZ, ssub_1
4531 const MachineOperand &MOReg = MI.getOperand(2);
4532 InputReg.Reg = MOReg.getReg();
4533 InputReg.SubReg = MOReg.getSubReg();
4534 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
4535 return true;
4536 }
4537 llvm_unreachable("Target dependent opcode missing");
4538}
4539
4540bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
4541 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
4542 RegSubRegPairAndIdx &InsertedReg) const {
4543 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4544 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
4545
4546 switch (MI.getOpcode()) {
4547 case ARM::VSETLNi32:
4548 // dX = VSETLNi32 dY, rZ, imm
4549 const MachineOperand &MOBaseReg = MI.getOperand(1);
4550 const MachineOperand &MOInsertedReg = MI.getOperand(2);
4551 const MachineOperand &MOIndex = MI.getOperand(3);
4552 BaseReg.Reg = MOBaseReg.getReg();
4553 BaseReg.SubReg = MOBaseReg.getSubReg();
4554
4555 InsertedReg.Reg = MOInsertedReg.getReg();
4556 InsertedReg.SubReg = MOInsertedReg.getSubReg();
4557 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
4558 return true;
4559 }
4560 llvm_unreachable("Target dependent opcode missing");
4561}