blob: ee0143e2e5d35c5fc8b904a5fa147887d8e994e7 [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
Tim Northover5d72c5d2014-10-01 19:21:03 +0000701void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB,
702 MachineBasicBlock::iterator I,
703 unsigned DestReg, bool KillSrc,
704 const ARMSubtarget &Subtarget) const {
705 unsigned Opc = Subtarget.isThumb()
706 ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
707 : ARM::MRS;
708
709 MachineInstrBuilder MIB =
710 BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
711
712 // There is only 1 A/R class MRS instruction, and it always refers to
713 // APSR. However, there are lots of other possibilities on M-class cores.
714 if (Subtarget.isMClass())
715 MIB.addImm(0x800);
716
717 AddDefaultPred(MIB);
718
719 MIB.addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
720}
721
722void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
723 MachineBasicBlock::iterator I,
724 unsigned SrcReg, bool KillSrc,
725 const ARMSubtarget &Subtarget) const {
726 unsigned Opc = Subtarget.isThumb()
727 ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
728 : ARM::MSR;
729
730 MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
731
732 if (Subtarget.isMClass())
733 MIB.addImm(0x800);
734 else
735 MIB.addImm(8);
736
737 MIB.addReg(SrcReg, getKillRegState(KillSrc));
738
739 AddDefaultPred(MIB);
740
741 MIB.addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
742}
743
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000744void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
745 MachineBasicBlock::iterator I, DebugLoc DL,
746 unsigned DestReg, unsigned SrcReg,
747 bool KillSrc) const {
748 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000749 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson70aa8d02010-02-16 17:24:15 +0000750
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000751 if (GPRDest && GPRSrc) {
752 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
Jim Grosbach8815bef2013-10-22 02:29:35 +0000753 .addReg(SrcReg, getKillRegState(KillSrc))));
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000754 return;
David Goodwine5b5d8f2009-08-05 21:02:22 +0000755 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000756
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000757 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000758 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000759
Chad Rosierbe762512011-08-20 00:17:25 +0000760 unsigned Opc = 0;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000761 if (SPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000762 Opc = ARM::VMOVS;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000763 else if (GPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000764 Opc = ARM::VMOVRS;
765 else if (SPRDest && GPRSrc)
766 Opc = ARM::VMOVSR;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000767 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP())
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000768 Opc = ARM::VMOVD;
769 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
Owen Anderson454e1c72011-07-15 18:46:47 +0000770 Opc = ARM::VORRq;
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000771
Chad Rosierbe762512011-08-20 00:17:25 +0000772 if (Opc) {
773 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
Owen Anderson454e1c72011-07-15 18:46:47 +0000774 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosierbe762512011-08-20 00:17:25 +0000775 if (Opc == ARM::VORRq)
776 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosier61f92ef2011-08-20 00:52:40 +0000777 AddDefaultPred(MIB);
Chad Rosierbe762512011-08-20 00:17:25 +0000778 return;
779 }
780
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000781 // Handle register classes that require multiple instructions.
782 unsigned BeginIdx = 0;
783 unsigned SubRegs = 0;
Andrew Trickb57e2252012-08-29 04:41:37 +0000784 int Spacing = 1;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000785
786 // Use VORRq when possible.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000787 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
788 Opc = ARM::VORRq;
789 BeginIdx = ARM::qsub_0;
790 SubRegs = 2;
791 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
792 Opc = ARM::VORRq;
793 BeginIdx = ARM::qsub_0;
794 SubRegs = 4;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000795 // Fall back to VMOVD.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000796 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
797 Opc = ARM::VMOVD;
798 BeginIdx = ARM::dsub_0;
799 SubRegs = 2;
800 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
801 Opc = ARM::VMOVD;
802 BeginIdx = ARM::dsub_0;
803 SubRegs = 3;
804 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
805 Opc = ARM::VMOVD;
806 BeginIdx = ARM::dsub_0;
807 SubRegs = 4;
808 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
Jim Grosbachdba14dd2013-10-22 02:29:37 +0000809 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000810 BeginIdx = ARM::gsub_0;
811 SubRegs = 2;
812 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
813 Opc = ARM::VMOVD;
814 BeginIdx = ARM::dsub_0;
815 SubRegs = 2;
816 Spacing = 2;
817 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
818 Opc = ARM::VMOVD;
819 BeginIdx = ARM::dsub_0;
820 SubRegs = 3;
821 Spacing = 2;
822 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
823 Opc = ARM::VMOVD;
824 BeginIdx = ARM::dsub_0;
825 SubRegs = 4;
826 Spacing = 2;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000827 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) {
828 Opc = ARM::VMOVS;
829 BeginIdx = ARM::ssub_0;
830 SubRegs = 2;
Tim Northover5d72c5d2014-10-01 19:21:03 +0000831 } else if (SrcReg == ARM::CPSR) {
832 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
833 return;
834 } else if (DestReg == ARM::CPSR) {
835 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
836 return;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000837 }
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000838
Andrew Trickb57e2252012-08-29 04:41:37 +0000839 assert(Opc && "Impossible reg-to-reg copy");
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000840
Andrew Trick4cc69492012-08-29 01:58:52 +0000841 const TargetRegisterInfo *TRI = &getRegisterInfo();
842 MachineInstrBuilder Mov;
Andrew Trickbd0073d2012-08-29 01:58:55 +0000843
844 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
845 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000846 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000847 Spacing = -Spacing;
848 }
849#ifndef NDEBUG
850 SmallSet<unsigned, 4> DstRegs;
851#endif
Andrew Trick4cc69492012-08-29 01:58:52 +0000852 for (unsigned i = 0; i != SubRegs; ++i) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000853 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
854 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
Andrew Trick4cc69492012-08-29 01:58:52 +0000855 assert(Dst && Src && "Bad sub-register");
Andrew Trickbd0073d2012-08-29 01:58:55 +0000856#ifndef NDEBUG
Andrew Trickbd0073d2012-08-29 01:58:55 +0000857 assert(!DstRegs.count(Src) && "destructive vector copy");
Andrew Trickb57e2252012-08-29 04:41:37 +0000858 DstRegs.insert(Dst);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000859#endif
Jim Grosbach8815bef2013-10-22 02:29:35 +0000860 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
Andrew Trick4cc69492012-08-29 01:58:52 +0000861 // VORR takes two source operands.
862 if (Opc == ARM::VORRq)
863 Mov.addReg(Src);
864 Mov = AddDefaultPred(Mov);
JF Bastien583db652013-07-12 23:33:03 +0000865 // MOVr can set CC.
866 if (Opc == ARM::MOVr)
867 Mov = AddDefaultCC(Mov);
Andrew Trick4cc69492012-08-29 01:58:52 +0000868 }
869 // Add implicit super-register defs and kills to the last instruction.
870 Mov->addRegisterDefined(DestReg, TRI);
871 if (KillSrc)
872 Mov->addRegisterKilled(SrcReg, TRI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000873}
874
Tim Northover798697d2013-04-21 11:57:07 +0000875const MachineInstrBuilder &
876ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
877 unsigned SubIdx, unsigned State,
878 const TargetRegisterInfo *TRI) const {
Evan Chengddc93c72010-05-07 00:24:52 +0000879 if (!SubIdx)
880 return MIB.addReg(Reg, State);
881
882 if (TargetRegisterInfo::isPhysicalRegister(Reg))
883 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
884 return MIB.addReg(Reg, State, SubIdx);
885}
886
David Goodwinaf7451b2009-07-08 16:09:28 +0000887void ARMBaseInstrInfo::
888storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
889 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000890 const TargetRegisterClass *RC,
891 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000892 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +0000893 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000894 MachineFunction &MF = *MBB.getParent();
895 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000896 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000897
898 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000899 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000900 MachineMemOperand::MOStore,
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000901 MFI.getObjectSize(FI),
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000902 Align);
David Goodwinaf7451b2009-07-08 16:09:28 +0000903
Owen Anderson732f82c2011-08-10 17:21:20 +0000904 switch (RC->getSize()) {
905 case 4:
906 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
907 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
David Goodwinaf7451b2009-07-08 16:09:28 +0000908 .addReg(SrcReg, getKillRegState(isKill))
Jim Grosbach338de3e2010-10-27 23:12:14 +0000909 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000910 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
911 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
Evan Cheng9d768f42010-05-06 01:34:11 +0000912 .addReg(SrcReg, getKillRegState(isKill))
913 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000914 } else
915 llvm_unreachable("Unknown reg class!");
916 break;
917 case 8:
918 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
919 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwinaf7451b2009-07-08 16:09:28 +0000920 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000921 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +0000922 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +0000923 if (Subtarget.hasV5TEOps()) {
924 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD));
925 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
926 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
927 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
928
929 AddDefaultPred(MIB);
930 } else {
931 // Fallback to STM instruction, which has existed since the dawn of
932 // time.
933 MachineInstrBuilder MIB =
934 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STMIA))
935 .addFrameIndex(FI).addMemOperand(MMO));
936 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
937 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
938 }
Owen Anderson732f82c2011-08-10 17:21:20 +0000939 } else
940 llvm_unreachable("Unknown reg class!");
941 break;
942 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +0000943 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +0000944 // Use aligned spills if the stack can be realigned.
945 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000946 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64))
Bob Wilson4c1ca292010-07-06 21:26:18 +0000947 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000948 .addReg(SrcReg, getKillRegState(isKill))
949 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000950 } else {
951 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000952 .addReg(SrcReg, getKillRegState(isKill))
953 .addFrameIndex(FI)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000954 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000955 }
956 } else
957 llvm_unreachable("Unknown reg class!");
958 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000959 case 24:
960 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
961 // Use aligned spills if the stack can be realigned.
962 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
963 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
964 .addFrameIndex(FI).addImm(16)
965 .addReg(SrcReg, getKillRegState(isKill))
966 .addMemOperand(MMO));
967 } else {
968 MachineInstrBuilder MIB =
969 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
970 .addFrameIndex(FI))
971 .addMemOperand(MMO);
972 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
973 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
974 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
975 }
976 } else
977 llvm_unreachable("Unknown reg class!");
978 break;
Owen Anderson732f82c2011-08-10 17:21:20 +0000979 case 32:
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000980 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +0000981 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
982 // FIXME: It's possible to only store part of the QQ register if the
983 // spilled def has a sub-register index.
984 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +0000985 .addFrameIndex(FI).addImm(16)
986 .addReg(SrcReg, getKillRegState(isKill))
987 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000988 } else {
989 MachineInstrBuilder MIB =
990 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000991 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +0000992 .addMemOperand(MMO);
993 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
994 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
995 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
996 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
997 }
998 } else
999 llvm_unreachable("Unknown reg class!");
1000 break;
1001 case 64:
1002 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1003 MachineInstrBuilder MIB =
1004 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
1005 .addFrameIndex(FI))
1006 .addMemOperand(MMO);
1007 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1008 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1009 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1010 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
1011 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
1012 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
1013 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
1014 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
1015 } else
1016 llvm_unreachable("Unknown reg class!");
1017 break;
1018 default:
1019 llvm_unreachable("Unknown reg class!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001020 }
1021}
1022
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001023unsigned
1024ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
1025 int &FrameIndex) const {
1026 switch (MI->getOpcode()) {
1027 default: break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001028 case ARM::STRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001029 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
1030 if (MI->getOperand(1).isFI() &&
1031 MI->getOperand(2).isReg() &&
1032 MI->getOperand(3).isImm() &&
1033 MI->getOperand(2).getReg() == 0 &&
1034 MI->getOperand(3).getImm() == 0) {
1035 FrameIndex = MI->getOperand(1).getIndex();
1036 return MI->getOperand(0).getReg();
1037 }
1038 break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001039 case ARM::STRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001040 case ARM::t2STRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001041 case ARM::tSTRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001042 case ARM::VSTRD:
1043 case ARM::VSTRS:
1044 if (MI->getOperand(1).isFI() &&
1045 MI->getOperand(2).isImm() &&
1046 MI->getOperand(2).getImm() == 0) {
1047 FrameIndex = MI->getOperand(1).getIndex();
1048 return MI->getOperand(0).getReg();
1049 }
1050 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001051 case ARM::VST1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001052 case ARM::VST1d64TPseudo:
1053 case ARM::VST1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001054 if (MI->getOperand(0).isFI() &&
1055 MI->getOperand(2).getSubReg() == 0) {
1056 FrameIndex = MI->getOperand(0).getIndex();
1057 return MI->getOperand(2).getReg();
1058 }
Jakob Stoklund Olesenb929c712010-09-15 21:40:09 +00001059 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001060 case ARM::VSTMQIA:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001061 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001062 MI->getOperand(0).getSubReg() == 0) {
1063 FrameIndex = MI->getOperand(1).getIndex();
1064 return MI->getOperand(0).getReg();
1065 }
1066 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001067 }
1068
1069 return 0;
1070}
1071
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001072unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
1073 int &FrameIndex) const {
1074 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001075 return MI->mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001076}
1077
David Goodwinaf7451b2009-07-08 16:09:28 +00001078void ARMBaseInstrInfo::
1079loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1080 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +00001081 const TargetRegisterClass *RC,
1082 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +00001083 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +00001084 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001085 MachineFunction &MF = *MBB.getParent();
1086 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001087 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001088 MachineMemOperand *MMO =
Chris Lattnere3d864b2010-09-21 04:39:43 +00001089 MF.getMachineMemOperand(
Jay Foad465101b2011-11-15 07:34:52 +00001090 MachinePointerInfo::getFixedStack(FI),
Chris Lattnere3d864b2010-09-21 04:39:43 +00001091 MachineMemOperand::MOLoad,
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001092 MFI.getObjectSize(FI),
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001093 Align);
David Goodwinaf7451b2009-07-08 16:09:28 +00001094
Owen Anderson732f82c2011-08-10 17:21:20 +00001095 switch (RC->getSize()) {
1096 case 4:
1097 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
1098 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1099 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilson37f106e2010-02-16 22:01:59 +00001100
Owen Anderson732f82c2011-08-10 17:21:20 +00001101 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
1102 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001103 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001104 } else
1105 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001106 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001107 case 8:
1108 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
1109 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Evan Cheng9d768f42010-05-06 01:34:11 +00001110 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001111 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001112 MachineInstrBuilder MIB;
1113
1114 if (Subtarget.hasV5TEOps()) {
1115 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1116 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1117 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1118 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
1119
1120 AddDefaultPred(MIB);
1121 } else {
1122 // Fallback to LDM instruction, which has existed since the dawn of
1123 // time.
1124 MIB = AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDMIA))
1125 .addFrameIndex(FI).addMemOperand(MMO));
1126 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1127 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1128 }
1129
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001130 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1131 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001132 } else
1133 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001134 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001135 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001136 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001137 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001138 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
Bob Wilson4c1ca292010-07-06 21:26:18 +00001139 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +00001140 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001141 } else {
1142 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1143 .addFrameIndex(FI)
1144 .addMemOperand(MMO));
1145 }
1146 } else
1147 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001148 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001149 case 24:
1150 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1151 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1152 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1153 .addFrameIndex(FI).addImm(16)
1154 .addMemOperand(MMO));
1155 } else {
1156 MachineInstrBuilder MIB =
1157 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1158 .addFrameIndex(FI)
1159 .addMemOperand(MMO));
1160 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1161 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1162 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1163 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1164 MIB.addReg(DestReg, RegState::ImplicitDefine);
1165 }
1166 } else
1167 llvm_unreachable("Unknown reg class!");
1168 break;
1169 case 32:
1170 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001171 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1172 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +00001173 .addFrameIndex(FI).addImm(16)
1174 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001175 } else {
1176 MachineInstrBuilder MIB =
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001177 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1178 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +00001179 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001180 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1181 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1182 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1183 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001184 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1185 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001186 }
1187 } else
1188 llvm_unreachable("Unknown reg class!");
1189 break;
1190 case 64:
1191 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1192 MachineInstrBuilder MIB =
1193 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1194 .addFrameIndex(FI))
1195 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001196 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1197 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1198 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1199 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1200 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1201 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1202 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1203 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001204 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1205 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001206 } else
1207 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001208 break;
Bob Wilsona92e41a2010-06-18 21:32:42 +00001209 default:
1210 llvm_unreachable("Unknown regclass!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001211 }
1212}
1213
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001214unsigned
1215ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
1216 int &FrameIndex) const {
1217 switch (MI->getOpcode()) {
1218 default: break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001219 case ARM::LDRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001220 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
1221 if (MI->getOperand(1).isFI() &&
1222 MI->getOperand(2).isReg() &&
1223 MI->getOperand(3).isImm() &&
1224 MI->getOperand(2).getReg() == 0 &&
1225 MI->getOperand(3).getImm() == 0) {
1226 FrameIndex = MI->getOperand(1).getIndex();
1227 return MI->getOperand(0).getReg();
1228 }
1229 break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001230 case ARM::LDRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001231 case ARM::t2LDRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001232 case ARM::tLDRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001233 case ARM::VLDRD:
1234 case ARM::VLDRS:
1235 if (MI->getOperand(1).isFI() &&
1236 MI->getOperand(2).isImm() &&
1237 MI->getOperand(2).getImm() == 0) {
1238 FrameIndex = MI->getOperand(1).getIndex();
1239 return MI->getOperand(0).getReg();
1240 }
1241 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001242 case ARM::VLD1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001243 case ARM::VLD1d64TPseudo:
1244 case ARM::VLD1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001245 if (MI->getOperand(1).isFI() &&
1246 MI->getOperand(0).getSubReg() == 0) {
1247 FrameIndex = MI->getOperand(1).getIndex();
1248 return MI->getOperand(0).getReg();
1249 }
1250 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001251 case ARM::VLDMQIA:
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001252 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001253 MI->getOperand(0).getSubReg() == 0) {
1254 FrameIndex = MI->getOperand(1).getIndex();
1255 return MI->getOperand(0).getReg();
1256 }
1257 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001258 }
1259
1260 return 0;
1261}
1262
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001263unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
1264 int &FrameIndex) const {
1265 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001266 return MI->mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001267}
1268
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001269bool
1270ARMBaseInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
1271 MachineFunction &MF = *MI->getParent()->getParent();
1272 Reloc::Model RM = MF.getTarget().getRelocationModel();
1273
1274 if (MI->getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
1275 assert(getSubtarget().getTargetTriple().getObjectFormat() ==
1276 Triple::MachO &&
1277 "LOAD_STACK_GUARD currently supported only for MachO.");
1278 expandLoadStackGuard(MI, RM);
1279 MI->getParent()->erase(MI);
1280 return true;
1281 }
1282
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001283 // This hook gets to expand COPY instructions before they become
1284 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1285 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1286 // changed into a VORR that can go down the NEON pipeline.
Oliver Stannard51b1d462014-08-21 12:50:31 +00001287 if (!WidenVMOVS || !MI->isCopy() || Subtarget.isCortexA15() ||
1288 Subtarget.isFPOnlySP())
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001289 return false;
1290
1291 // Look for a copy between even S-registers. That is where we keep floats
1292 // when using NEON v2f32 instructions for f32 arithmetic.
1293 unsigned DstRegS = MI->getOperand(0).getReg();
1294 unsigned SrcRegS = MI->getOperand(1).getReg();
1295 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1296 return false;
1297
1298 const TargetRegisterInfo *TRI = &getRegisterInfo();
1299 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1300 &ARM::DPRRegClass);
1301 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1302 &ARM::DPRRegClass);
1303 if (!DstRegD || !SrcRegD)
1304 return false;
1305
1306 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1307 // legal if the COPY already defines the full DstRegD, and it isn't a
1308 // sub-register insertion.
1309 if (!MI->definesRegister(DstRegD, TRI) || MI->readsRegister(DstRegD, TRI))
1310 return false;
1311
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001312 // A dead copy shouldn't show up here, but reject it just in case.
1313 if (MI->getOperand(0).isDead())
1314 return false;
1315
1316 // All clear, widen the COPY.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001317 DEBUG(dbgs() << "widening: " << *MI);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001318 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001319
1320 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg
1321 // or some other super-register.
1322 int ImpDefIdx = MI->findRegisterDefOperandIdx(DstRegD);
1323 if (ImpDefIdx != -1)
1324 MI->RemoveOperand(ImpDefIdx);
1325
1326 // Change the opcode and operands.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001327 MI->setDesc(get(ARM::VMOVD));
1328 MI->getOperand(0).setReg(DstRegD);
1329 MI->getOperand(1).setReg(SrcRegD);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001330 AddDefaultPred(MIB);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001331
1332 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1333 // register scavenger and machine verifier, so we need to indicate that we
1334 // are reading an undefined value from SrcRegD, but a proper value from
1335 // SrcRegS.
1336 MI->getOperand(1).setIsUndef();
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001337 MIB.addReg(SrcRegS, RegState::Implicit);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001338
1339 // SrcRegD may actually contain an unrelated value in the ssub_1
1340 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
1341 if (MI->getOperand(1).isKill()) {
1342 MI->getOperand(1).setIsKill(false);
1343 MI->addRegisterKilled(SrcRegS, TRI, true);
1344 }
1345
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001346 DEBUG(dbgs() << "replaced by: " << *MI);
1347 return true;
1348}
1349
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001350/// Create a copy of a const pool value. Update CPI to the new index and return
1351/// the label UID.
1352static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1353 MachineConstantPool *MCP = MF.getConstantPool();
1354 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1355
1356 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1357 assert(MCPE.isMachineConstantPoolEntry() &&
1358 "Expecting a machine constantpool entry!");
1359 ARMConstantPoolValue *ACPV =
1360 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1361
Evan Chengdfce83c2011-01-17 08:03:18 +00001362 unsigned PCLabelId = AFI->createPICLabelUId();
Craig Topper062a2ba2014-04-25 05:30:21 +00001363 ARMConstantPoolValue *NewCPV = nullptr;
Oliver Stannard8f859942014-01-29 16:01:24 +00001364
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001365 // FIXME: The below assumes PIC relocation model and that the function
1366 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1367 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1368 // instructions, so that's probably OK, but is PIC always correct when
1369 // we get here?
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001370 if (ACPV->isGlobalValue())
Bill Wendling7753d662011-10-01 08:00:54 +00001371 NewCPV = ARMConstantPoolConstant::
1372 Create(cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId,
1373 ARMCP::CPValue, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001374 else if (ACPV->isExtSymbol())
Bill Wendlingc214cb02011-10-01 08:58:29 +00001375 NewCPV = ARMConstantPoolSymbol::
1376 Create(MF.getFunction()->getContext(),
1377 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001378 else if (ACPV->isBlockAddress())
Bill Wendling7753d662011-10-01 08:00:54 +00001379 NewCPV = ARMConstantPoolConstant::
1380 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1381 ARMCP::CPBlockAddress, 4);
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001382 else if (ACPV->isLSDA())
Bill Wendling7753d662011-10-01 08:00:54 +00001383 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId,
1384 ARMCP::CPLSDA, 4);
Bill Wendling69bc3de2011-09-29 23:50:42 +00001385 else if (ACPV->isMachineBasicBlock())
Bill Wendling4a4772f2011-10-01 09:30:42 +00001386 NewCPV = ARMConstantPoolMBB::
1387 Create(MF.getFunction()->getContext(),
1388 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001389 else
1390 llvm_unreachable("Unexpected ARM constantpool value type!!");
1391 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1392 return PCLabelId;
1393}
1394
Evan Chengfe864422009-11-08 00:15:23 +00001395void ARMBaseInstrInfo::
1396reMaterialize(MachineBasicBlock &MBB,
1397 MachineBasicBlock::iterator I,
1398 unsigned DestReg, unsigned SubIdx,
Evan Cheng6ad7da92009-11-14 02:55:43 +00001399 const MachineInstr *Orig,
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001400 const TargetRegisterInfo &TRI) const {
Evan Chengfe864422009-11-08 00:15:23 +00001401 unsigned Opcode = Orig->getOpcode();
1402 switch (Opcode) {
1403 default: {
1404 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001405 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfe864422009-11-08 00:15:23 +00001406 MBB.insert(I, MI);
1407 break;
1408 }
1409 case ARM::tLDRpci_pic:
1410 case ARM::t2LDRpci_pic: {
1411 MachineFunction &MF = *MBB.getParent();
Evan Chengfe864422009-11-08 00:15:23 +00001412 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001413 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfe864422009-11-08 00:15:23 +00001414 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1415 DestReg)
1416 .addConstantPoolIndex(CPI).addImm(PCLabelId);
Chris Lattner1d0c2572011-04-29 05:24:29 +00001417 MIB->setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
Evan Chengfe864422009-11-08 00:15:23 +00001418 break;
1419 }
1420 }
Evan Chengfe864422009-11-08 00:15:23 +00001421}
1422
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001423MachineInstr *
1424ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001425 MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001426 switch(Orig->getOpcode()) {
1427 case ARM::tLDRpci_pic:
1428 case ARM::t2LDRpci_pic: {
1429 unsigned CPI = Orig->getOperand(1).getIndex();
1430 unsigned PCLabelId = duplicateCPV(MF, CPI);
1431 Orig->getOperand(1).setIndex(CPI);
1432 Orig->getOperand(2).setImm(PCLabelId);
1433 break;
1434 }
1435 }
1436 return MI;
1437}
1438
Evan Chenge9c46c22010-03-03 01:44:33 +00001439bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
Evan Chengb8b0ad82011-01-20 08:34:58 +00001440 const MachineInstr *MI1,
1441 const MachineRegisterInfo *MRI) const {
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001442 int Opcode = MI0->getOpcode();
Evan Cheng028ccbfc2011-01-20 23:55:07 +00001443 if (Opcode == ARM::t2LDRpci ||
Evan Chengbbd50b02009-11-20 02:10:27 +00001444 Opcode == ARM::t2LDRpci_pic ||
1445 Opcode == ARM::tLDRpci ||
Evan Chengb8b0ad82011-01-20 08:34:58 +00001446 Opcode == ARM::tLDRpci_pic ||
Tim Northover72360d22013-12-02 10:35:41 +00001447 Opcode == ARM::LDRLIT_ga_pcrel ||
1448 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1449 Opcode == ARM::tLDRLIT_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001450 Opcode == ARM::MOV_ga_pcrel ||
1451 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001452 Opcode == ARM::t2MOV_ga_pcrel) {
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001453 if (MI1->getOpcode() != Opcode)
1454 return false;
1455 if (MI0->getNumOperands() != MI1->getNumOperands())
1456 return false;
1457
1458 const MachineOperand &MO0 = MI0->getOperand(1);
1459 const MachineOperand &MO1 = MI1->getOperand(1);
1460 if (MO0.getOffset() != MO1.getOffset())
1461 return false;
1462
Tim Northover72360d22013-12-02 10:35:41 +00001463 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1464 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1465 Opcode == ARM::tLDRLIT_ga_pcrel ||
1466 Opcode == ARM::MOV_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001467 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001468 Opcode == ARM::t2MOV_ga_pcrel)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001469 // Ignore the PC labels.
1470 return MO0.getGlobal() == MO1.getGlobal();
1471
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001472 const MachineFunction *MF = MI0->getParent()->getParent();
1473 const MachineConstantPool *MCP = MF->getConstantPool();
1474 int CPI0 = MO0.getIndex();
1475 int CPI1 = MO1.getIndex();
1476 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1477 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
Evan Chengf098bf12011-03-24 06:20:03 +00001478 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1479 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1480 if (isARMCP0 && isARMCP1) {
1481 ARMConstantPoolValue *ACPV0 =
1482 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1483 ARMConstantPoolValue *ACPV1 =
1484 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1485 return ACPV0->hasSameValue(ACPV1);
1486 } else if (!isARMCP0 && !isARMCP1) {
1487 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1488 }
1489 return false;
Evan Chengb8b0ad82011-01-20 08:34:58 +00001490 } else if (Opcode == ARM::PICLDR) {
1491 if (MI1->getOpcode() != Opcode)
1492 return false;
1493 if (MI0->getNumOperands() != MI1->getNumOperands())
1494 return false;
1495
1496 unsigned Addr0 = MI0->getOperand(1).getReg();
1497 unsigned Addr1 = MI1->getOperand(1).getReg();
1498 if (Addr0 != Addr1) {
1499 if (!MRI ||
1500 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1501 !TargetRegisterInfo::isVirtualRegister(Addr1))
1502 return false;
1503
1504 // This assumes SSA form.
1505 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1506 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1507 // Check if the loaded value, e.g. a constantpool of a global address, are
1508 // the same.
1509 if (!produceSameValue(Def0, Def1, MRI))
1510 return false;
1511 }
1512
1513 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1514 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1515 const MachineOperand &MO0 = MI0->getOperand(i);
1516 const MachineOperand &MO1 = MI1->getOperand(i);
1517 if (!MO0.isIdenticalTo(MO1))
1518 return false;
1519 }
1520 return true;
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001521 }
1522
Evan Chenge9c46c22010-03-03 01:44:33 +00001523 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001524}
1525
Bill Wendlingf4707472010-06-23 23:00:16 +00001526/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1527/// determine if two loads are loading from the same base address. It should
1528/// only return true if the base pointers are the same and the only differences
1529/// between the two addresses is the offset. It also returns the offsets by
1530/// reference.
Andrew Tricka7714a02012-11-12 19:40:10 +00001531///
1532/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1533/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001534bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1535 int64_t &Offset1,
1536 int64_t &Offset2) const {
1537 // Don't worry about Thumb: just ARM and Thumb2.
1538 if (Subtarget.isThumb1Only()) return false;
1539
1540 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1541 return false;
1542
1543 switch (Load1->getMachineOpcode()) {
1544 default:
1545 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001546 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001547 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001548 case ARM::LDRD:
1549 case ARM::LDRH:
1550 case ARM::LDRSB:
1551 case ARM::LDRSH:
1552 case ARM::VLDRD:
1553 case ARM::VLDRS:
1554 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001555 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001556 case ARM::t2LDRDi8:
1557 case ARM::t2LDRSHi8:
1558 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001559 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001560 case ARM::t2LDRSHi12:
1561 break;
1562 }
1563
1564 switch (Load2->getMachineOpcode()) {
1565 default:
1566 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001567 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001568 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001569 case ARM::LDRD:
1570 case ARM::LDRH:
1571 case ARM::LDRSB:
1572 case ARM::LDRSH:
1573 case ARM::VLDRD:
1574 case ARM::VLDRS:
1575 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001576 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001577 case ARM::t2LDRSHi8:
1578 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001579 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001580 case ARM::t2LDRSHi12:
1581 break;
1582 }
1583
1584 // Check if base addresses and chain operands match.
1585 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1586 Load1->getOperand(4) != Load2->getOperand(4))
1587 return false;
1588
1589 // Index should be Reg0.
1590 if (Load1->getOperand(3) != Load2->getOperand(3))
1591 return false;
1592
1593 // Determine the offsets.
1594 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1595 isa<ConstantSDNode>(Load2->getOperand(1))) {
1596 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1597 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1598 return true;
1599 }
1600
1601 return false;
1602}
1603
1604/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001605/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Bill Wendlingf4707472010-06-23 23:00:16 +00001606/// be scheduled togther. On some targets if two loads are loading from
1607/// addresses in the same cache line, it's better if they are scheduled
1608/// together. This function takes two integers that represent the load offsets
1609/// from the common base address. It returns true if it decides it's desirable
1610/// to schedule the two loads together. "NumLoads" is the number of loads that
1611/// have already been scheduled after Load1.
Andrew Tricka7714a02012-11-12 19:40:10 +00001612///
1613/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1614/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001615bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1616 int64_t Offset1, int64_t Offset2,
1617 unsigned NumLoads) const {
1618 // Don't worry about Thumb: just ARM and Thumb2.
1619 if (Subtarget.isThumb1Only()) return false;
1620
1621 assert(Offset2 > Offset1);
1622
1623 if ((Offset2 - Offset1) / 8 > 64)
1624 return false;
1625
Renato Golinb184cd92013-08-14 16:35:29 +00001626 // Check if the machine opcodes are different. If they are different
1627 // then we consider them to not be of the same base address,
1628 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1629 // In this case, they are considered to be the same because they are different
1630 // encoding forms of the same basic instruction.
1631 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1632 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1633 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1634 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1635 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
Bill Wendlingf4707472010-06-23 23:00:16 +00001636 return false; // FIXME: overly conservative?
1637
1638 // Four loads in a row should be sufficient.
1639 if (NumLoads >= 3)
1640 return false;
1641
1642 return true;
1643}
1644
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001645bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1646 const MachineBasicBlock *MBB,
1647 const MachineFunction &MF) const {
Jim Grosbachba3ece62010-06-25 18:43:14 +00001648 // Debug info is never a scheduling boundary. It's necessary to be explicit
1649 // due to the special treatment of IT instructions below, otherwise a
1650 // dbg_value followed by an IT will result in the IT instruction being
1651 // considered a scheduling hazard, which is wrong. It should be the actual
1652 // instruction preceding the dbg_value instruction(s), just like it is
1653 // when debug info is not present.
1654 if (MI->isDebugValue())
1655 return false;
1656
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001657 // Terminators and labels can't be scheduled around.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001658 if (MI->isTerminator() || MI->isPosition())
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001659 return true;
1660
1661 // Treat the start of the IT block as a scheduling boundary, but schedule
1662 // t2IT along with all instructions following it.
1663 // FIXME: This is a big hammer. But the alternative is to add all potential
1664 // true and anti dependencies to IT block instructions as implicit operands
1665 // to the t2IT instruction. The added compile time and complexity does not
1666 // seem worth it.
1667 MachineBasicBlock::const_iterator I = MI;
Jim Grosbachba3ece62010-06-25 18:43:14 +00001668 // Make sure to skip any dbg_value instructions
1669 while (++I != MBB->end() && I->isDebugValue())
1670 ;
1671 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001672 return true;
1673
1674 // Don't attempt to schedule around any instruction that defines
1675 // a stack-oriented pointer, as it's unlikely to be profitable. This
1676 // saves compile time, because it doesn't require every single
1677 // stack slot reference to depend on the instruction that does the
1678 // modification.
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001679 // Calls don't actually change the stack pointer, even if they have imp-defs.
Jakob Stoklund Olesen5f37f1c2012-02-22 01:07:19 +00001680 // No ARM calling conventions change the stack pointer. (X86 calling
1681 // conventions sometimes do).
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001682 if (!MI->isCall() && MI->definesRegister(ARM::SP))
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001683 return true;
1684
1685 return false;
1686}
1687
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001688bool ARMBaseInstrInfo::
1689isProfitableToIfCvt(MachineBasicBlock &MBB,
1690 unsigned NumCycles, unsigned ExtraPredCycles,
1691 const BranchProbability &Probability) const {
Cameron Zwarich80018502011-04-13 06:39:16 +00001692 if (!NumCycles)
Evan Cheng02b184d2010-06-25 22:42:03 +00001693 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001694
Owen Anderson88af7d02010-09-28 18:32:13 +00001695 // Attempt to estimate the relative costs of predication versus branching.
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001696 unsigned UnpredCost = Probability.getNumerator() * NumCycles;
1697 UnpredCost /= Probability.getDenominator();
1698 UnpredCost += 1; // The branch itself
1699 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001700
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001701 return (NumCycles + ExtraPredCycles) <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001702}
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001703
Evan Cheng02b184d2010-06-25 22:42:03 +00001704bool ARMBaseInstrInfo::
Evan Chengdebf9c52010-11-03 00:45:17 +00001705isProfitableToIfCvt(MachineBasicBlock &TMBB,
1706 unsigned TCycles, unsigned TExtra,
1707 MachineBasicBlock &FMBB,
1708 unsigned FCycles, unsigned FExtra,
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001709 const BranchProbability &Probability) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00001710 if (!TCycles || !FCycles)
Owen Anderson88af7d02010-09-28 18:32:13 +00001711 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001712
Owen Anderson88af7d02010-09-28 18:32:13 +00001713 // Attempt to estimate the relative costs of predication versus branching.
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001714 unsigned TUnpredCost = Probability.getNumerator() * TCycles;
1715 TUnpredCost /= Probability.getDenominator();
Andrew Trick3f1fdf12011-09-21 02:17:37 +00001716
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001717 uint32_t Comp = Probability.getDenominator() - Probability.getNumerator();
1718 unsigned FUnpredCost = Comp * FCycles;
1719 FUnpredCost /= Probability.getDenominator();
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001720
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001721 unsigned UnpredCost = TUnpredCost + FUnpredCost;
1722 UnpredCost += 1; // The branch itself
1723 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
1724
1725 return (TCycles + FCycles + TExtra + FExtra) <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001726}
1727
Bob Wilsone8a549c2012-09-29 21:43:49 +00001728bool
1729ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1730 MachineBasicBlock &FMBB) const {
1731 // Reduce false anti-dependencies to let Swift's out-of-order execution
1732 // engine do its thing.
1733 return Subtarget.isSwift();
1734}
1735
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001736/// getInstrPredicate - If instruction is predicated, returns its predicate
1737/// condition, otherwise returns AL. It also returns the condition code
1738/// register by reference.
Evan Cheng83e0d482009-09-28 09:14:39 +00001739ARMCC::CondCodes
1740llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001741 int PIdx = MI->findFirstPredOperandIdx();
1742 if (PIdx == -1) {
1743 PredReg = 0;
1744 return ARMCC::AL;
1745 }
1746
1747 PredReg = MI->getOperand(PIdx+1).getReg();
1748 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1749}
1750
1751
Evan Cheng780748d2009-07-28 05:48:47 +00001752int llvm::getMatchingCondBranchOpcode(int Opc) {
Evan Cheng056c6692009-07-27 18:20:05 +00001753 if (Opc == ARM::B)
1754 return ARM::Bcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001755 if (Opc == ARM::tB)
Evan Cheng056c6692009-07-27 18:20:05 +00001756 return ARM::tBcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001757 if (Opc == ARM::t2B)
1758 return ARM::t2Bcc;
Evan Cheng056c6692009-07-27 18:20:05 +00001759
1760 llvm_unreachable("Unknown unconditional branch opcode!");
Evan Cheng056c6692009-07-27 18:20:05 +00001761}
1762
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001763/// commuteInstruction - Handle commutable instructions.
1764MachineInstr *
1765ARMBaseInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
1766 switch (MI->getOpcode()) {
1767 case ARM::MOVCCr:
1768 case ARM::t2MOVCCr: {
1769 // MOVCC can be commuted by inverting the condition.
1770 unsigned PredReg = 0;
1771 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
1772 // MOVCC AL can't be inverted. Shouldn't happen.
1773 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
Craig Topper062a2ba2014-04-25 05:30:21 +00001774 return nullptr;
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001775 MI = TargetInstrInfo::commuteInstruction(MI, NewMI);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001776 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001777 return nullptr;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001778 // After swapping the MOVCC operands, also invert the condition.
1779 MI->getOperand(MI->findFirstPredOperandIdx())
1780 .setImm(ARMCC::getOppositeCondition(CC));
1781 return MI;
1782 }
1783 }
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001784 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001785}
Evan Cheng780748d2009-07-28 05:48:47 +00001786
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001787/// Identify instructions that can be folded into a MOVCC instruction, and
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001788/// return the defining instruction.
1789static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
1790 const MachineRegisterInfo &MRI,
1791 const TargetInstrInfo *TII) {
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001792 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001793 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001794 if (!MRI.hasOneNonDBGUse(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001795 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001796 MachineInstr *MI = MRI.getVRegDef(Reg);
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001797 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001798 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001799 // MI is folded into the MOVCC by predicating it.
1800 if (!MI->isPredicable())
Craig Topper062a2ba2014-04-25 05:30:21 +00001801 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001802 // Check if MI has any non-dead defs or physreg uses. This also detects
1803 // predicated instructions which will be reading CPSR.
1804 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
1805 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen7b1a2e82012-08-17 20:55:34 +00001806 // Reject frame index operands, PEI can't handle the predicated pseudos.
1807 if (MO.isFI() || MO.isCPI() || MO.isJTI())
Craig Topper062a2ba2014-04-25 05:30:21 +00001808 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001809 if (!MO.isReg())
1810 continue;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001811 // MI can't have any tied operands, that would conflict with predication.
1812 if (MO.isTied())
Craig Topper062a2ba2014-04-25 05:30:21 +00001813 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001814 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Craig Topper062a2ba2014-04-25 05:30:21 +00001815 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001816 if (MO.isDef() && !MO.isDead())
Craig Topper062a2ba2014-04-25 05:30:21 +00001817 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001818 }
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001819 bool DontMoveAcrossStores = true;
Craig Topper062a2ba2014-04-25 05:30:21 +00001820 if (!MI->isSafeToMove(TII, /* AliasAnalysis = */ nullptr,
1821 DontMoveAcrossStores))
1822 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001823 return MI;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001824}
1825
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001826bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr *MI,
1827 SmallVectorImpl<MachineOperand> &Cond,
1828 unsigned &TrueOp, unsigned &FalseOp,
1829 bool &Optimizable) const {
1830 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1831 "Unknown select instruction");
1832 // MOVCC operands:
1833 // 0: Def.
1834 // 1: True use.
1835 // 2: False use.
1836 // 3: Condition code.
1837 // 4: CPSR use.
1838 TrueOp = 1;
1839 FalseOp = 2;
1840 Cond.push_back(MI->getOperand(3));
1841 Cond.push_back(MI->getOperand(4));
1842 // We can always fold a def.
1843 Optimizable = true;
1844 return false;
1845}
1846
1847MachineInstr *ARMBaseInstrInfo::optimizeSelect(MachineInstr *MI,
1848 bool PreferFalse) const {
1849 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1850 "Unknown select instruction");
Matthias Braun2f169f92013-10-04 16:52:56 +00001851 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001852 MachineInstr *DefMI = canFoldIntoMOVCC(MI->getOperand(2).getReg(), MRI, this);
1853 bool Invert = !DefMI;
1854 if (!DefMI)
1855 DefMI = canFoldIntoMOVCC(MI->getOperand(1).getReg(), MRI, this);
1856 if (!DefMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001857 return nullptr;
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001858
Matthias Braun2f169f92013-10-04 16:52:56 +00001859 // Find new register class to use.
1860 MachineOperand FalseReg = MI->getOperand(Invert ? 2 : 1);
1861 unsigned DestReg = MI->getOperand(0).getReg();
1862 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
1863 if (!MRI.constrainRegClass(DestReg, PreviousClass))
Craig Topper062a2ba2014-04-25 05:30:21 +00001864 return nullptr;
Matthias Braun2f169f92013-10-04 16:52:56 +00001865
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001866 // Create a new predicated version of DefMI.
1867 // Rfalse is the first use.
1868 MachineInstrBuilder NewMI = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
Matthias Braun2f169f92013-10-04 16:52:56 +00001869 DefMI->getDesc(), DestReg);
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001870
1871 // Copy all the DefMI operands, excluding its (null) predicate.
1872 const MCInstrDesc &DefDesc = DefMI->getDesc();
1873 for (unsigned i = 1, e = DefDesc.getNumOperands();
1874 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
1875 NewMI.addOperand(DefMI->getOperand(i));
1876
1877 unsigned CondCode = MI->getOperand(3).getImm();
1878 if (Invert)
1879 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
1880 else
1881 NewMI.addImm(CondCode);
1882 NewMI.addOperand(MI->getOperand(4));
1883
1884 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
1885 if (NewMI->hasOptionalDef())
1886 AddDefaultCC(NewMI);
1887
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001888 // The output register value when the predicate is false is an implicit
1889 // register operand tied to the first def.
1890 // The tie makes the register allocator ensure the FalseReg is allocated the
1891 // same register as operand 0.
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001892 FalseReg.setImplicit();
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +00001893 NewMI.addOperand(FalseReg);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001894 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
1895
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001896 // The caller will erase MI, but not DefMI.
1897 DefMI->eraseFromParent();
1898 return NewMI;
1899}
1900
Andrew Trick924123a2011-09-21 02:20:46 +00001901/// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
1902/// instruction is encoded with an 'S' bit is determined by the optional CPSR
1903/// def operand.
1904///
1905/// This will go away once we can teach tblgen how to set the optional CPSR def
1906/// operand itself.
1907struct AddSubFlagsOpcodePair {
Craig Topper2fbd1302012-05-24 03:59:11 +00001908 uint16_t PseudoOpc;
1909 uint16_t MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001910};
1911
Craig Topper2fbd1302012-05-24 03:59:11 +00001912static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
Andrew Trick924123a2011-09-21 02:20:46 +00001913 {ARM::ADDSri, ARM::ADDri},
1914 {ARM::ADDSrr, ARM::ADDrr},
1915 {ARM::ADDSrsi, ARM::ADDrsi},
1916 {ARM::ADDSrsr, ARM::ADDrsr},
1917
1918 {ARM::SUBSri, ARM::SUBri},
1919 {ARM::SUBSrr, ARM::SUBrr},
1920 {ARM::SUBSrsi, ARM::SUBrsi},
1921 {ARM::SUBSrsr, ARM::SUBrsr},
1922
1923 {ARM::RSBSri, ARM::RSBri},
Andrew Trick924123a2011-09-21 02:20:46 +00001924 {ARM::RSBSrsi, ARM::RSBrsi},
1925 {ARM::RSBSrsr, ARM::RSBrsr},
1926
1927 {ARM::t2ADDSri, ARM::t2ADDri},
1928 {ARM::t2ADDSrr, ARM::t2ADDrr},
1929 {ARM::t2ADDSrs, ARM::t2ADDrs},
1930
1931 {ARM::t2SUBSri, ARM::t2SUBri},
1932 {ARM::t2SUBSrr, ARM::t2SUBrr},
1933 {ARM::t2SUBSrs, ARM::t2SUBrs},
1934
1935 {ARM::t2RSBSri, ARM::t2RSBri},
1936 {ARM::t2RSBSrs, ARM::t2RSBrs},
1937};
1938
1939unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
Craig Topper2fbd1302012-05-24 03:59:11 +00001940 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
1941 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
1942 return AddSubFlagsOpcodeMap[i].MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001943 return 0;
1944}
1945
Evan Cheng780748d2009-07-28 05:48:47 +00001946void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1947 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1948 unsigned DestReg, unsigned BaseReg, int NumBytes,
1949 ARMCC::CondCodes Pred, unsigned PredReg,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001950 const ARMBaseInstrInfo &TII, unsigned MIFlags) {
Tim Northoverc9432eb2013-11-04 23:04:15 +00001951 if (NumBytes == 0 && DestReg != BaseReg) {
1952 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
1953 .addReg(BaseReg, RegState::Kill)
1954 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1955 .setMIFlags(MIFlags);
1956 return;
1957 }
1958
Evan Cheng780748d2009-07-28 05:48:47 +00001959 bool isSub = NumBytes < 0;
1960 if (isSub) NumBytes = -NumBytes;
1961
1962 while (NumBytes) {
1963 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1964 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1965 assert(ThisVal && "Didn't extract field correctly");
1966
1967 // We will handle these bits from offset, clear them.
1968 NumBytes &= ~ThisVal;
1969
1970 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1971
1972 // Build the new ADD / SUB.
1973 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1974 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1975 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001976 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1977 .setMIFlags(MIFlags);
Evan Cheng780748d2009-07-28 05:48:47 +00001978 BaseReg = DestReg;
1979 }
1980}
1981
Weiming Zhao01524852014-03-20 23:28:16 +00001982static bool isAnySubRegLive(unsigned Reg, const TargetRegisterInfo *TRI,
1983 MachineInstr *MI) {
1984 for (MCSubRegIterator Subreg(Reg, TRI, /* IncludeSelf */ true);
1985 Subreg.isValid(); ++Subreg)
1986 if (MI->getParent()->computeRegisterLiveness(TRI, *Subreg, MI) !=
1987 MachineBasicBlock::LQR_Dead)
1988 return true;
1989 return false;
1990}
Tim Northoverdee86042013-12-02 14:46:26 +00001991bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
1992 MachineFunction &MF, MachineInstr *MI,
Tim Northover93bcc662013-11-08 17:18:07 +00001993 unsigned NumBytes) {
1994 // This optimisation potentially adds lots of load and store
1995 // micro-operations, it's only really a great benefit to code-size.
Eric Christopherc1058df2014-07-04 01:55:26 +00001996 if (!MF.getFunction()->getAttributes().hasAttribute(
1997 AttributeSet::FunctionIndex, Attribute::MinSize))
Tim Northover93bcc662013-11-08 17:18:07 +00001998 return false;
1999
2000 // If only one register is pushed/popped, LLVM can use an LDR/STR
2001 // instead. We can't modify those so make sure we're dealing with an
2002 // instruction we understand.
2003 bool IsPop = isPopOpcode(MI->getOpcode());
2004 bool IsPush = isPushOpcode(MI->getOpcode());
2005 if (!IsPush && !IsPop)
2006 return false;
2007
2008 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
2009 MI->getOpcode() == ARM::VLDMDIA_UPD;
2010 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
2011 MI->getOpcode() == ARM::tPOP ||
2012 MI->getOpcode() == ARM::tPOP_RET;
2013
2014 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
2015 MI->getOperand(1).getReg() == ARM::SP)) &&
2016 "trying to fold sp update into non-sp-updating push/pop");
2017
2018 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2019 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2020 // if this is violated.
2021 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
2022 return false;
2023
2024 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2025 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2026 int RegListIdx = IsT1PushPop ? 2 : 4;
2027
2028 // Calculate the space we'll need in terms of registers.
2029 unsigned FirstReg = MI->getOperand(RegListIdx).getReg();
2030 unsigned RD0Reg, RegsNeeded;
2031 if (IsVFPPushPop) {
2032 RD0Reg = ARM::D0;
2033 RegsNeeded = NumBytes / 8;
2034 } else {
2035 RD0Reg = ARM::R0;
2036 RegsNeeded = NumBytes / 4;
2037 }
2038
2039 // We're going to have to strip all list operands off before
2040 // re-adding them since the order matters, so save the existing ones
2041 // for later.
2042 SmallVector<MachineOperand, 4> RegList;
2043 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2044 RegList.push_back(MI->getOperand(i));
2045
Tim Northover93bcc662013-11-08 17:18:07 +00002046 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
Tim Northover45479dc2013-12-01 14:16:24 +00002047 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Tim Northover93bcc662013-11-08 17:18:07 +00002048
2049 // Now try to find enough space in the reglist to allocate NumBytes.
2050 for (unsigned CurReg = FirstReg - 1; CurReg >= RD0Reg && RegsNeeded;
Tim Northover45479dc2013-12-01 14:16:24 +00002051 --CurReg) {
Tim Northover93bcc662013-11-08 17:18:07 +00002052 if (!IsPop) {
2053 // Pushing any register is completely harmless, mark the
2054 // register involved as undef since we don't care about it in
2055 // the slightest.
2056 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2057 false, false, true));
Tim Northover45479dc2013-12-01 14:16:24 +00002058 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002059 continue;
2060 }
2061
Tim Northover45479dc2013-12-01 14:16:24 +00002062 // However, we can only pop an extra register if it's not live. For
2063 // registers live within the function we might clobber a return value
2064 // register; the other way a register can be live here is if it's
2065 // callee-saved.
Weiming Zhao01524852014-03-20 23:28:16 +00002066 // TODO: Currently, computeRegisterLiveness() does not report "live" if a
2067 // sub reg is live. When computeRegisterLiveness() works for sub reg, it
2068 // can replace isAnySubRegLive().
Tim Northover45479dc2013-12-01 14:16:24 +00002069 if (isCalleeSavedRegister(CurReg, CSRegs) ||
Weiming Zhao01524852014-03-20 23:28:16 +00002070 isAnySubRegLive(CurReg, TRI, MI)) {
Tim Northover45479dc2013-12-01 14:16:24 +00002071 // VFP pops don't allow holes in the register list, so any skip is fatal
2072 // for our transformation. GPR pops do, so we should just keep looking.
2073 if (IsVFPPushPop)
2074 return false;
2075 else
2076 continue;
2077 }
Tim Northover93bcc662013-11-08 17:18:07 +00002078
2079 // Mark the unimportant registers as <def,dead> in the POP.
Lang Hames1ca11232013-11-22 00:46:32 +00002080 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2081 true));
Tim Northover45479dc2013-12-01 14:16:24 +00002082 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002083 }
2084
2085 if (RegsNeeded > 0)
2086 return false;
2087
2088 // Finally we know we can profitably perform the optimisation so go
2089 // ahead: strip all existing registers off and add them back again
2090 // in the right order.
2091 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2092 MI->RemoveOperand(i);
2093
2094 // Add the complete list back in.
2095 MachineInstrBuilder MIB(MF, &*MI);
2096 for (int i = RegList.size() - 1; i >= 0; --i)
2097 MIB.addOperand(RegList[i]);
2098
2099 return true;
2100}
2101
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002102bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2103 unsigned FrameReg, int &Offset,
2104 const ARMBaseInstrInfo &TII) {
Evan Cheng780748d2009-07-28 05:48:47 +00002105 unsigned Opcode = MI.getOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002106 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng780748d2009-07-28 05:48:47 +00002107 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2108 bool isSub = false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002109
Evan Cheng780748d2009-07-28 05:48:47 +00002110 // Memory operands in inline assembly always use AddrMode2.
2111 if (Opcode == ARM::INLINEASM)
2112 AddrMode = ARMII::AddrMode2;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002113
Evan Cheng780748d2009-07-28 05:48:47 +00002114 if (Opcode == ARM::ADDri) {
2115 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2116 if (Offset == 0) {
2117 // Turn it into a move.
2118 MI.setDesc(TII.get(ARM::MOVr));
2119 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2120 MI.RemoveOperand(FrameRegIdx+1);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002121 Offset = 0;
2122 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002123 } else if (Offset < 0) {
2124 Offset = -Offset;
2125 isSub = true;
2126 MI.setDesc(TII.get(ARM::SUBri));
2127 }
2128
2129 // Common case: small offset, fits into instruction.
2130 if (ARM_AM::getSOImmVal(Offset) != -1) {
2131 // Replace the FrameIndex with sp / fp
2132 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2133 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002134 Offset = 0;
2135 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002136 }
2137
2138 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2139 // as possible.
2140 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2141 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2142
2143 // We will handle these bits from offset, clear them.
2144 Offset &= ~ThisImmVal;
2145
2146 // Get the properly encoded SOImmVal field.
2147 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2148 "Bit extraction didn't work?");
2149 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2150 } else {
2151 unsigned ImmIdx = 0;
2152 int InstrOffs = 0;
2153 unsigned NumBits = 0;
2154 unsigned Scale = 1;
2155 switch (AddrMode) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002156 case ARMII::AddrMode_i12: {
2157 ImmIdx = FrameRegIdx + 1;
2158 InstrOffs = MI.getOperand(ImmIdx).getImm();
2159 NumBits = 12;
2160 break;
2161 }
Evan Cheng780748d2009-07-28 05:48:47 +00002162 case ARMII::AddrMode2: {
2163 ImmIdx = FrameRegIdx+2;
2164 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2165 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2166 InstrOffs *= -1;
2167 NumBits = 12;
2168 break;
2169 }
2170 case ARMII::AddrMode3: {
2171 ImmIdx = FrameRegIdx+2;
2172 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2173 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2174 InstrOffs *= -1;
2175 NumBits = 8;
2176 break;
2177 }
Anton Korobeynikov887d05c2009-08-08 13:35:48 +00002178 case ARMII::AddrMode4:
Jim Grosbach01c1cae2009-11-15 21:45:34 +00002179 case ARMII::AddrMode6:
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002180 // Can't fold any offset even if it's zero.
2181 return false;
Evan Cheng780748d2009-07-28 05:48:47 +00002182 case ARMII::AddrMode5: {
2183 ImmIdx = FrameRegIdx+1;
2184 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2185 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2186 InstrOffs *= -1;
2187 NumBits = 8;
2188 Scale = 4;
2189 break;
2190 }
2191 default:
2192 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng780748d2009-07-28 05:48:47 +00002193 }
2194
2195 Offset += InstrOffs * Scale;
2196 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2197 if (Offset < 0) {
2198 Offset = -Offset;
2199 isSub = true;
2200 }
2201
2202 // Attempt to fold address comp. if opcode has offset bits
2203 if (NumBits > 0) {
2204 // Common case: small offset, fits into instruction.
2205 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2206 int ImmedOffset = Offset / Scale;
2207 unsigned Mask = (1 << NumBits) - 1;
2208 if ((unsigned)Offset <= Mask * Scale) {
2209 // Replace the FrameIndex with sp
2210 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach9d2d1f02010-10-27 01:19:41 +00002211 // FIXME: When addrmode2 goes away, this will simplify (like the
2212 // T2 version), as the LDR.i12 versions don't need the encoding
2213 // tricks for the offset value.
2214 if (isSub) {
2215 if (AddrMode == ARMII::AddrMode_i12)
2216 ImmedOffset = -ImmedOffset;
2217 else
2218 ImmedOffset |= 1 << NumBits;
2219 }
Evan Cheng780748d2009-07-28 05:48:47 +00002220 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002221 Offset = 0;
2222 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002223 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002224
Evan Cheng780748d2009-07-28 05:48:47 +00002225 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2226 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach8bf14832010-10-27 16:50:31 +00002227 if (isSub) {
2228 if (AddrMode == ARMII::AddrMode_i12)
2229 ImmedOffset = -ImmedOffset;
2230 else
2231 ImmedOffset |= 1 << NumBits;
2232 }
Evan Cheng780748d2009-07-28 05:48:47 +00002233 ImmOp.ChangeToImmediate(ImmedOffset);
2234 Offset &= ~(Mask*Scale);
2235 }
2236 }
2237
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002238 Offset = (isSub) ? -Offset : Offset;
2239 return Offset == 0;
Evan Cheng780748d2009-07-28 05:48:47 +00002240}
Bill Wendling7de9d522010-08-06 01:32:48 +00002241
Manman Ren6fa76dc2012-06-29 21:33:59 +00002242/// analyzeCompare - For a comparison instruction, return the source registers
2243/// in SrcReg and SrcReg2 if having two register operands, and the value it
2244/// compares against in CmpValue. Return true if the comparison instruction
2245/// can be analyzed.
Bill Wendling7de9d522010-08-06 01:32:48 +00002246bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002247analyzeCompare(const MachineInstr *MI, unsigned &SrcReg, unsigned &SrcReg2,
2248 int &CmpMask, int &CmpValue) const {
Bill Wendling7de9d522010-08-06 01:32:48 +00002249 switch (MI->getOpcode()) {
2250 default: break;
Bill Wendling79553ba2010-08-11 00:23:00 +00002251 case ARM::CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002252 case ARM::t2CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002253 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002254 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002255 CmpMask = ~0;
Bill Wendling7de9d522010-08-06 01:32:48 +00002256 CmpValue = MI->getOperand(1).getImm();
2257 return true;
Manman Rendc8ad002012-05-11 01:30:47 +00002258 case ARM::CMPrr:
2259 case ARM::t2CMPrr:
2260 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002261 SrcReg2 = MI->getOperand(1).getReg();
Manman Rendc8ad002012-05-11 01:30:47 +00002262 CmpMask = ~0;
2263 CmpValue = 0;
2264 return true;
Gabor Greifadbbb932010-09-21 12:01:15 +00002265 case ARM::TSTri:
2266 case ARM::t2TSTri:
2267 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002268 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002269 CmpMask = MI->getOperand(1).getImm();
2270 CmpValue = 0;
2271 return true;
2272 }
2273
2274 return false;
2275}
2276
Gabor Greifd36e3e82010-09-29 10:12:08 +00002277/// isSuitableForMask - Identify a suitable 'and' instruction that
2278/// operates on the given source register and applies the same mask
2279/// as a 'tst' instruction. Provide a limited look-through for copies.
2280/// When successful, MI will hold the found instruction.
2281static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif1a25ae82010-09-21 13:30:57 +00002282 int CmpMask, bool CommonUse) {
Gabor Greifd36e3e82010-09-29 10:12:08 +00002283 switch (MI->getOpcode()) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002284 case ARM::ANDri:
2285 case ARM::t2ANDri:
Gabor Greifd36e3e82010-09-29 10:12:08 +00002286 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif1a25ae82010-09-21 13:30:57 +00002287 return false;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002288 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greifadbbb932010-09-21 12:01:15 +00002289 return true;
2290 break;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002291 case ARM::COPY: {
2292 // Walk down one instruction which is potentially an 'and'.
2293 const MachineInstr &Copy = *MI;
Michael J. Spencer70ac5fa2010-10-05 06:00:43 +00002294 MachineBasicBlock::iterator AND(
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002295 std::next(MachineBasicBlock::iterator(MI)));
Gabor Greifd36e3e82010-09-29 10:12:08 +00002296 if (AND == MI->getParent()->end()) return false;
2297 MI = AND;
2298 return isSuitableForMask(MI, Copy.getOperand(0).getReg(),
2299 CmpMask, true);
2300 }
Bill Wendling7de9d522010-08-06 01:32:48 +00002301 }
2302
2303 return false;
2304}
2305
Manman Renb1b3db62012-06-29 22:06:19 +00002306/// getSwappedCondition - assume the flags are set by MI(a,b), return
2307/// the condition code if we modify the instructions such that flags are
2308/// set by MI(b,a).
2309inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2310 switch (CC) {
2311 default: return ARMCC::AL;
2312 case ARMCC::EQ: return ARMCC::EQ;
2313 case ARMCC::NE: return ARMCC::NE;
2314 case ARMCC::HS: return ARMCC::LS;
2315 case ARMCC::LO: return ARMCC::HI;
2316 case ARMCC::HI: return ARMCC::LO;
2317 case ARMCC::LS: return ARMCC::HS;
2318 case ARMCC::GE: return ARMCC::LE;
2319 case ARMCC::LT: return ARMCC::GT;
2320 case ARMCC::GT: return ARMCC::LT;
2321 case ARMCC::LE: return ARMCC::GE;
2322 }
2323}
2324
2325/// isRedundantFlagInstr - check whether the first instruction, whose only
2326/// purpose is to update flags, can be made redundant.
2327/// CMPrr can be made redundant by SUBrr if the operands are the same.
2328/// CMPri can be made redundant by SUBri if the operands are the same.
2329/// This function can be extended later on.
2330inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2331 unsigned SrcReg2, int ImmValue,
2332 MachineInstr *OI) {
2333 if ((CmpI->getOpcode() == ARM::CMPrr ||
2334 CmpI->getOpcode() == ARM::t2CMPrr) &&
2335 (OI->getOpcode() == ARM::SUBrr ||
2336 OI->getOpcode() == ARM::t2SUBrr) &&
2337 ((OI->getOperand(1).getReg() == SrcReg &&
2338 OI->getOperand(2).getReg() == SrcReg2) ||
2339 (OI->getOperand(1).getReg() == SrcReg2 &&
2340 OI->getOperand(2).getReg() == SrcReg)))
2341 return true;
2342
2343 if ((CmpI->getOpcode() == ARM::CMPri ||
2344 CmpI->getOpcode() == ARM::t2CMPri) &&
2345 (OI->getOpcode() == ARM::SUBri ||
2346 OI->getOpcode() == ARM::t2SUBri) &&
2347 OI->getOperand(1).getReg() == SrcReg &&
2348 OI->getOperand(2).getImm() == ImmValue)
2349 return true;
2350 return false;
2351}
2352
Manman Ren6fa76dc2012-06-29 21:33:59 +00002353/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2354/// comparison into one that sets the zero bit in the flags register;
2355/// Remove a redundant Compare instruction if an earlier instruction can set the
2356/// flags in the same way as Compare.
2357/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2358/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2359/// condition code of instructions which use the flags.
Bill Wendling7de9d522010-08-06 01:32:48 +00002360bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002361optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, unsigned SrcReg2,
2362 int CmpMask, int CmpValue,
2363 const MachineRegisterInfo *MRI) const {
Manman Renb1b3db62012-06-29 22:06:19 +00002364 // Get the unique definition of SrcReg.
2365 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2366 if (!MI) return false;
Bill Wendling04123002010-09-10 23:34:19 +00002367
Gabor Greifadbbb932010-09-21 12:01:15 +00002368 // Masked compares sometimes use the same register as the corresponding 'and'.
2369 if (CmpMask != ~0) {
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002370 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(MI)) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002371 MI = nullptr;
Owen Anderson16c6bf42014-03-13 23:12:04 +00002372 for (MachineRegisterInfo::use_instr_iterator
2373 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2374 UI != UE; ++UI) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002375 if (UI->getParent() != CmpInstr->getParent()) continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002376 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002377 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
2378 isPredicated(PotentialAND))
Gabor Greifadbbb932010-09-21 12:01:15 +00002379 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002380 MI = PotentialAND;
Gabor Greifadbbb932010-09-21 12:01:15 +00002381 break;
2382 }
2383 if (!MI) return false;
2384 }
2385 }
2386
Manman Rendc8ad002012-05-11 01:30:47 +00002387 // Get ready to iterate backward from CmpInstr.
2388 MachineBasicBlock::iterator I = CmpInstr, E = MI,
2389 B = CmpInstr->getParent()->begin();
Bill Wendling59ebe442010-10-09 00:03:48 +00002390
2391 // Early exit if CmpInstr is at the beginning of the BB.
2392 if (I == B) return false;
2393
Manman Rendc8ad002012-05-11 01:30:47 +00002394 // There are two possible candidates which can be changed to set CPSR:
2395 // One is MI, the other is a SUB instruction.
2396 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2397 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
Craig Topper062a2ba2014-04-25 05:30:21 +00002398 MachineInstr *Sub = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002399 if (SrcReg2 != 0)
Manman Rendc8ad002012-05-11 01:30:47 +00002400 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00002401 MI = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002402 else if (MI->getParent() != CmpInstr->getParent() || CmpValue != 0) {
Manman Rendc8ad002012-05-11 01:30:47 +00002403 // Conservatively refuse to convert an instruction which isn't in the same
2404 // BB as the comparison.
2405 // For CMPri, we need to check Sub, thus we can't return here.
Manman Ren0d5ec282012-05-11 15:36:46 +00002406 if (CmpInstr->getOpcode() == ARM::CMPri ||
Manman Rendc8ad002012-05-11 01:30:47 +00002407 CmpInstr->getOpcode() == ARM::t2CMPri)
Craig Topper062a2ba2014-04-25 05:30:21 +00002408 MI = nullptr;
Manman Rendc8ad002012-05-11 01:30:47 +00002409 else
2410 return false;
2411 }
2412
2413 // Check that CPSR isn't set between the comparison instruction and the one we
2414 // want to change. At the same time, search for Sub.
Manman Renb1b3db62012-06-29 22:06:19 +00002415 const TargetRegisterInfo *TRI = &getRegisterInfo();
Bill Wendling7de9d522010-08-06 01:32:48 +00002416 --I;
2417 for (; I != E; --I) {
2418 const MachineInstr &Instr = *I;
2419
Manman Renb1b3db62012-06-29 22:06:19 +00002420 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2421 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendlingc6627ee2010-11-01 20:41:43 +00002422 // This instruction modifies or uses CPSR after the one we want to
2423 // change. We can't do this transformation.
Manman Renb1b3db62012-06-29 22:06:19 +00002424 return false;
Evan Chengd757c882010-09-21 23:49:07 +00002425
Manman Renb1b3db62012-06-29 22:06:19 +00002426 // Check whether CmpInstr can be made redundant by the current instruction.
2427 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
Manman Rendc8ad002012-05-11 01:30:47 +00002428 Sub = &*I;
2429 break;
2430 }
2431
Evan Chengd757c882010-09-21 23:49:07 +00002432 if (I == B)
2433 // The 'and' is below the comparison instruction.
2434 return false;
Bill Wendling7de9d522010-08-06 01:32:48 +00002435 }
2436
Manman Rendc8ad002012-05-11 01:30:47 +00002437 // Return false if no candidates exist.
2438 if (!MI && !Sub)
2439 return false;
2440
2441 // The single candidate is called MI.
2442 if (!MI) MI = Sub;
2443
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002444 // We can't use a predicated instruction - it doesn't always write the flags.
2445 if (isPredicated(MI))
2446 return false;
2447
Bill Wendling7de9d522010-08-06 01:32:48 +00002448 switch (MI->getOpcode()) {
2449 default: break;
Cameron Zwarich93eae152011-04-15 20:28:28 +00002450 case ARM::RSBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002451 case ARM::RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002452 case ARM::RSCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002453 case ARM::RSCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002454 case ARM::ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002455 case ARM::ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002456 case ARM::ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002457 case ARM::ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002458 case ARM::SUBrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002459 case ARM::SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002460 case ARM::SBCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002461 case ARM::SBCri:
2462 case ARM::t2RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002463 case ARM::t2ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002464 case ARM::t2ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002465 case ARM::t2ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002466 case ARM::t2ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002467 case ARM::t2SUBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002468 case ARM::t2SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002469 case ARM::t2SBCrr:
Cameron Zwarich0829b302011-04-15 20:45:00 +00002470 case ARM::t2SBCri:
2471 case ARM::ANDrr:
2472 case ARM::ANDri:
2473 case ARM::t2ANDrr:
Cameron Zwarich9c65e4d2011-04-15 21:24:38 +00002474 case ARM::t2ANDri:
2475 case ARM::ORRrr:
2476 case ARM::ORRri:
2477 case ARM::t2ORRrr:
2478 case ARM::t2ORRri:
2479 case ARM::EORrr:
2480 case ARM::EORri:
2481 case ARM::t2EORrr:
2482 case ARM::t2EORri: {
Manman Rendc8ad002012-05-11 01:30:47 +00002483 // Scan forward for the use of CPSR
2484 // When checking against MI: if it's a conditional code requires
Manman Ren34cb93e2012-07-11 22:51:44 +00002485 // checking of V bit, then this is not safe to do.
2486 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2487 // If we are done with the basic block, we need to check whether CPSR is
2488 // live-out.
Manman Renb1b3db62012-06-29 22:06:19 +00002489 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2490 OperandsToUpdate;
Evan Cheng425489d2011-03-23 22:52:04 +00002491 bool isSafe = false;
2492 I = CmpInstr;
Manman Rendc8ad002012-05-11 01:30:47 +00002493 E = CmpInstr->getParent()->end();
Evan Cheng425489d2011-03-23 22:52:04 +00002494 while (!isSafe && ++I != E) {
2495 const MachineInstr &Instr = *I;
2496 for (unsigned IO = 0, EO = Instr.getNumOperands();
2497 !isSafe && IO != EO; ++IO) {
2498 const MachineOperand &MO = Instr.getOperand(IO);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +00002499 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2500 isSafe = true;
2501 break;
2502 }
Evan Cheng425489d2011-03-23 22:52:04 +00002503 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2504 continue;
2505 if (MO.isDef()) {
2506 isSafe = true;
2507 break;
2508 }
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002509 // Condition code is after the operand before CPSR except for VSELs.
2510 ARMCC::CondCodes CC;
2511 bool IsInstrVSel = true;
2512 switch (Instr.getOpcode()) {
2513 default:
2514 IsInstrVSel = false;
2515 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2516 break;
2517 case ARM::VSELEQD:
2518 case ARM::VSELEQS:
2519 CC = ARMCC::EQ;
2520 break;
2521 case ARM::VSELGTD:
2522 case ARM::VSELGTS:
2523 CC = ARMCC::GT;
2524 break;
2525 case ARM::VSELGED:
2526 case ARM::VSELGES:
2527 CC = ARMCC::GE;
2528 break;
2529 case ARM::VSELVSS:
2530 case ARM::VSELVSD:
2531 CC = ARMCC::VS;
2532 break;
2533 }
2534
Manman Renb1b3db62012-06-29 22:06:19 +00002535 if (Sub) {
2536 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2537 if (NewCC == ARMCC::AL)
Manman Rendc8ad002012-05-11 01:30:47 +00002538 return false;
Manman Renb1b3db62012-06-29 22:06:19 +00002539 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2540 // on CMP needs to be updated to be based on SUB.
2541 // Push the condition code operands to OperandsToUpdate.
2542 // If it is safe to remove CmpInstr, the condition code of these
2543 // operands will be modified.
2544 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002545 Sub->getOperand(2).getReg() == SrcReg) {
2546 // VSel doesn't support condition code update.
2547 if (IsInstrVSel)
2548 return false;
2549 OperandsToUpdate.push_back(
2550 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2551 }
2552 } else
Manman Rendc8ad002012-05-11 01:30:47 +00002553 switch (CC) {
2554 default:
Manman Ren88a0d332012-07-11 23:47:00 +00002555 // CPSR can be used multiple times, we should continue.
Manman Rendc8ad002012-05-11 01:30:47 +00002556 break;
2557 case ARMCC::VS:
2558 case ARMCC::VC:
2559 case ARMCC::GE:
2560 case ARMCC::LT:
2561 case ARMCC::GT:
2562 case ARMCC::LE:
2563 return false;
2564 }
Evan Cheng425489d2011-03-23 22:52:04 +00002565 }
2566 }
2567
Manman Ren34cb93e2012-07-11 22:51:44 +00002568 // If CPSR is not killed nor re-defined, we should check whether it is
2569 // live-out. If it is live-out, do not optimize.
2570 if (!isSafe) {
2571 MachineBasicBlock *MBB = CmpInstr->getParent();
2572 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2573 SE = MBB->succ_end(); SI != SE; ++SI)
2574 if ((*SI)->isLiveIn(ARM::CPSR))
2575 return false;
2576 }
Evan Cheng425489d2011-03-23 22:52:04 +00002577
Evan Cheng65536472010-11-17 08:06:50 +00002578 // Toggle the optional operand to CPSR.
2579 MI->getOperand(5).setReg(ARM::CPSR);
2580 MI->getOperand(5).setIsDef(true);
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002581 assert(!isPredicated(MI) && "Can't use flags from predicated instruction");
Bill Wendling7de9d522010-08-06 01:32:48 +00002582 CmpInstr->eraseFromParent();
Manman Rendc8ad002012-05-11 01:30:47 +00002583
2584 // Modify the condition code of operands in OperandsToUpdate.
2585 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2586 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Manman Renb1b3db62012-06-29 22:06:19 +00002587 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2588 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
Bill Wendling7de9d522010-08-06 01:32:48 +00002589 return true;
2590 }
Cameron Zwarich0829b302011-04-15 20:45:00 +00002591 }
Bill Wendling7de9d522010-08-06 01:32:48 +00002592
2593 return false;
2594}
Evan Cheng367a5df2010-09-09 18:18:55 +00002595
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002596bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
2597 MachineInstr *DefMI, unsigned Reg,
2598 MachineRegisterInfo *MRI) const {
2599 // Fold large immediates into add, sub, or, xor.
2600 unsigned DefOpc = DefMI->getOpcode();
2601 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2602 return false;
2603 if (!DefMI->getOperand(1).isImm())
2604 // Could be t2MOVi32imm <ga:xx>
2605 return false;
2606
2607 if (!MRI->hasOneNonDBGUse(Reg))
2608 return false;
2609
Evan Chenga2b48d92012-03-26 23:31:00 +00002610 const MCInstrDesc &DefMCID = DefMI->getDesc();
2611 if (DefMCID.hasOptionalDef()) {
2612 unsigned NumOps = DefMCID.getNumOperands();
2613 const MachineOperand &MO = DefMI->getOperand(NumOps-1);
2614 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2615 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2616 // to delete DefMI.
2617 return false;
2618 }
2619
2620 const MCInstrDesc &UseMCID = UseMI->getDesc();
2621 if (UseMCID.hasOptionalDef()) {
2622 unsigned NumOps = UseMCID.getNumOperands();
2623 if (UseMI->getOperand(NumOps-1).getReg() == ARM::CPSR)
2624 // If the instruction sets the flag, do not attempt this optimization
2625 // since it may change the semantics of the code.
2626 return false;
2627 }
2628
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002629 unsigned UseOpc = UseMI->getOpcode();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002630 unsigned NewUseOpc = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002631 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002632 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002633 bool Commute = false;
2634 switch (UseOpc) {
2635 default: return false;
2636 case ARM::SUBrr:
2637 case ARM::ADDrr:
2638 case ARM::ORRrr:
2639 case ARM::EORrr:
2640 case ARM::t2SUBrr:
2641 case ARM::t2ADDrr:
2642 case ARM::t2ORRrr:
2643 case ARM::t2EORrr: {
2644 Commute = UseMI->getOperand(2).getReg() != Reg;
2645 switch (UseOpc) {
2646 default: break;
2647 case ARM::SUBrr: {
2648 if (Commute)
2649 return false;
2650 ImmVal = -ImmVal;
2651 NewUseOpc = ARM::SUBri;
2652 // Fallthrough
2653 }
2654 case ARM::ADDrr:
2655 case ARM::ORRrr:
2656 case ARM::EORrr: {
2657 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2658 return false;
2659 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2660 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2661 switch (UseOpc) {
2662 default: break;
2663 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
2664 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2665 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2666 }
2667 break;
2668 }
2669 case ARM::t2SUBrr: {
2670 if (Commute)
2671 return false;
2672 ImmVal = -ImmVal;
2673 NewUseOpc = ARM::t2SUBri;
2674 // Fallthrough
2675 }
2676 case ARM::t2ADDrr:
2677 case ARM::t2ORRrr:
2678 case ARM::t2EORrr: {
2679 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2680 return false;
2681 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2682 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2683 switch (UseOpc) {
2684 default: break;
2685 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
2686 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2687 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2688 }
2689 break;
2690 }
2691 }
2692 }
2693 }
2694
2695 unsigned OpIdx = Commute ? 2 : 1;
2696 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
2697 bool isKill = UseMI->getOperand(OpIdx).isKill();
2698 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
2699 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
Evan Cheng7fae11b2011-12-14 02:11:42 +00002700 UseMI, UseMI->getDebugLoc(),
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002701 get(NewUseOpc), NewReg)
2702 .addReg(Reg1, getKillRegState(isKill))
2703 .addImm(SOImmValV1)));
2704 UseMI->setDesc(get(NewUseOpc));
2705 UseMI->getOperand(1).setReg(NewReg);
2706 UseMI->getOperand(1).setIsKill();
2707 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
2708 DefMI->eraseFromParent();
2709 return true;
2710}
2711
Bob Wilsone8a549c2012-09-29 21:43:49 +00002712static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
2713 const MachineInstr *MI) {
2714 switch (MI->getOpcode()) {
2715 default: {
2716 const MCInstrDesc &Desc = MI->getDesc();
2717 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
2718 assert(UOps >= 0 && "bad # UOps");
2719 return UOps;
2720 }
2721
2722 case ARM::LDRrs:
2723 case ARM::LDRBrs:
2724 case ARM::STRrs:
2725 case ARM::STRBrs: {
2726 unsigned ShOpVal = MI->getOperand(3).getImm();
2727 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2728 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2729 if (!isSub &&
2730 (ShImm == 0 ||
2731 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2732 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2733 return 1;
2734 return 2;
2735 }
2736
2737 case ARM::LDRH:
2738 case ARM::STRH: {
2739 if (!MI->getOperand(2).getReg())
2740 return 1;
2741
2742 unsigned ShOpVal = MI->getOperand(3).getImm();
2743 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2744 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2745 if (!isSub &&
2746 (ShImm == 0 ||
2747 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2748 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2749 return 1;
2750 return 2;
2751 }
2752
2753 case ARM::LDRSB:
2754 case ARM::LDRSH:
2755 return (ARM_AM::getAM3Op(MI->getOperand(3).getImm()) == ARM_AM::sub) ? 3:2;
2756
2757 case ARM::LDRSB_POST:
2758 case ARM::LDRSH_POST: {
2759 unsigned Rt = MI->getOperand(0).getReg();
2760 unsigned Rm = MI->getOperand(3).getReg();
2761 return (Rt == Rm) ? 4 : 3;
2762 }
2763
2764 case ARM::LDR_PRE_REG:
2765 case ARM::LDRB_PRE_REG: {
2766 unsigned Rt = MI->getOperand(0).getReg();
2767 unsigned Rm = MI->getOperand(3).getReg();
2768 if (Rt == Rm)
2769 return 3;
2770 unsigned ShOpVal = MI->getOperand(4).getImm();
2771 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2772 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2773 if (!isSub &&
2774 (ShImm == 0 ||
2775 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2776 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2777 return 2;
2778 return 3;
2779 }
2780
2781 case ARM::STR_PRE_REG:
2782 case ARM::STRB_PRE_REG: {
2783 unsigned ShOpVal = MI->getOperand(4).getImm();
2784 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2785 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2786 if (!isSub &&
2787 (ShImm == 0 ||
2788 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2789 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2790 return 2;
2791 return 3;
2792 }
2793
2794 case ARM::LDRH_PRE:
2795 case ARM::STRH_PRE: {
2796 unsigned Rt = MI->getOperand(0).getReg();
2797 unsigned Rm = MI->getOperand(3).getReg();
2798 if (!Rm)
2799 return 2;
2800 if (Rt == Rm)
2801 return 3;
2802 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub)
2803 ? 3 : 2;
2804 }
2805
2806 case ARM::LDR_POST_REG:
2807 case ARM::LDRB_POST_REG:
2808 case ARM::LDRH_POST: {
2809 unsigned Rt = MI->getOperand(0).getReg();
2810 unsigned Rm = MI->getOperand(3).getReg();
2811 return (Rt == Rm) ? 3 : 2;
2812 }
2813
2814 case ARM::LDR_PRE_IMM:
2815 case ARM::LDRB_PRE_IMM:
2816 case ARM::LDR_POST_IMM:
2817 case ARM::LDRB_POST_IMM:
2818 case ARM::STRB_POST_IMM:
2819 case ARM::STRB_POST_REG:
2820 case ARM::STRB_PRE_IMM:
2821 case ARM::STRH_POST:
2822 case ARM::STR_POST_IMM:
2823 case ARM::STR_POST_REG:
2824 case ARM::STR_PRE_IMM:
2825 return 2;
2826
2827 case ARM::LDRSB_PRE:
2828 case ARM::LDRSH_PRE: {
2829 unsigned Rm = MI->getOperand(3).getReg();
2830 if (Rm == 0)
2831 return 3;
2832 unsigned Rt = MI->getOperand(0).getReg();
2833 if (Rt == Rm)
2834 return 4;
2835 unsigned ShOpVal = MI->getOperand(4).getImm();
2836 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2837 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2838 if (!isSub &&
2839 (ShImm == 0 ||
2840 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2841 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2842 return 3;
2843 return 4;
2844 }
2845
2846 case ARM::LDRD: {
2847 unsigned Rt = MI->getOperand(0).getReg();
2848 unsigned Rn = MI->getOperand(2).getReg();
2849 unsigned Rm = MI->getOperand(3).getReg();
2850 if (Rm)
2851 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2852 return (Rt == Rn) ? 3 : 2;
2853 }
2854
2855 case ARM::STRD: {
2856 unsigned Rm = MI->getOperand(3).getReg();
2857 if (Rm)
2858 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2859 return 2;
2860 }
2861
2862 case ARM::LDRD_POST:
2863 case ARM::t2LDRD_POST:
2864 return 3;
2865
2866 case ARM::STRD_POST:
2867 case ARM::t2STRD_POST:
2868 return 4;
2869
2870 case ARM::LDRD_PRE: {
2871 unsigned Rt = MI->getOperand(0).getReg();
2872 unsigned Rn = MI->getOperand(3).getReg();
2873 unsigned Rm = MI->getOperand(4).getReg();
2874 if (Rm)
2875 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2876 return (Rt == Rn) ? 4 : 3;
2877 }
2878
2879 case ARM::t2LDRD_PRE: {
2880 unsigned Rt = MI->getOperand(0).getReg();
2881 unsigned Rn = MI->getOperand(3).getReg();
2882 return (Rt == Rn) ? 4 : 3;
2883 }
2884
2885 case ARM::STRD_PRE: {
2886 unsigned Rm = MI->getOperand(4).getReg();
2887 if (Rm)
2888 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2889 return 3;
2890 }
2891
2892 case ARM::t2STRD_PRE:
2893 return 3;
2894
2895 case ARM::t2LDR_POST:
2896 case ARM::t2LDRB_POST:
2897 case ARM::t2LDRB_PRE:
2898 case ARM::t2LDRSBi12:
2899 case ARM::t2LDRSBi8:
2900 case ARM::t2LDRSBpci:
2901 case ARM::t2LDRSBs:
2902 case ARM::t2LDRH_POST:
2903 case ARM::t2LDRH_PRE:
2904 case ARM::t2LDRSBT:
2905 case ARM::t2LDRSB_POST:
2906 case ARM::t2LDRSB_PRE:
2907 case ARM::t2LDRSH_POST:
2908 case ARM::t2LDRSH_PRE:
2909 case ARM::t2LDRSHi12:
2910 case ARM::t2LDRSHi8:
2911 case ARM::t2LDRSHpci:
2912 case ARM::t2LDRSHs:
2913 return 2;
2914
2915 case ARM::t2LDRDi8: {
2916 unsigned Rt = MI->getOperand(0).getReg();
2917 unsigned Rn = MI->getOperand(2).getReg();
2918 return (Rt == Rn) ? 3 : 2;
2919 }
2920
2921 case ARM::t2STRB_POST:
2922 case ARM::t2STRB_PRE:
2923 case ARM::t2STRBs:
2924 case ARM::t2STRDi8:
2925 case ARM::t2STRH_POST:
2926 case ARM::t2STRH_PRE:
2927 case ARM::t2STRHs:
2928 case ARM::t2STR_POST:
2929 case ARM::t2STR_PRE:
2930 case ARM::t2STRs:
2931 return 2;
2932 }
2933}
2934
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00002935// Return the number of 32-bit words loaded by LDM or stored by STM. If this
2936// can't be easily determined return 0 (missing MachineMemOperand).
2937//
2938// FIXME: The current MachineInstr design does not support relying on machine
2939// mem operands to determine the width of a memory access. Instead, we expect
2940// the target to provide this information based on the instruction opcode and
Robin Morisset039781e2014-08-29 21:53:01 +00002941// operands. However, using MachineMemOperand is the best solution now for
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00002942// two reasons:
2943//
2944// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
2945// operands. This is much more dangerous than using the MachineMemOperand
2946// sizes because CodeGen passes can insert/remove optional machine operands. In
2947// fact, it's totally incorrect for preRA passes and appears to be wrong for
2948// postRA passes as well.
2949//
2950// 2) getNumLDMAddresses is only used by the scheduling machine model and any
2951// machine model that calls this should handle the unknown (zero size) case.
2952//
2953// Long term, we should require a target hook that verifies MachineMemOperand
2954// sizes during MC lowering. That target hook should be local to MC lowering
2955// because we can't ensure that it is aware of other MI forms. Doing this will
2956// ensure that MachineMemOperands are correctly propagated through all passes.
2957unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr *MI) const {
2958 unsigned Size = 0;
2959 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
2960 E = MI->memoperands_end(); I != E; ++I) {
2961 Size += (*I)->getSize();
2962 }
2963 return Size / 4;
2964}
2965
Evan Cheng367a5df2010-09-09 18:18:55 +00002966unsigned
Evan Chengdebf9c52010-11-03 00:45:17 +00002967ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
2968 const MachineInstr *MI) const {
Evan Chengbf407072010-09-10 01:29:16 +00002969 if (!ItinData || ItinData->isEmpty())
Evan Cheng367a5df2010-09-09 18:18:55 +00002970 return 1;
2971
Evan Cheng6cc775f2011-06-28 19:10:37 +00002972 const MCInstrDesc &Desc = MI->getDesc();
Evan Cheng367a5df2010-09-09 18:18:55 +00002973 unsigned Class = Desc.getSchedClass();
Andrew Trickf161e392012-07-02 18:10:42 +00002974 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsone8a549c2012-09-29 21:43:49 +00002975 if (ItinUOps >= 0) {
2976 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
2977 return getNumMicroOpsSwiftLdSt(ItinData, MI);
2978
Andrew Trickf161e392012-07-02 18:10:42 +00002979 return ItinUOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002980 }
Evan Cheng367a5df2010-09-09 18:18:55 +00002981
2982 unsigned Opc = MI->getOpcode();
2983 switch (Opc) {
2984 default:
2985 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002986 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002987 case ARM::VSTMQIA:
Evan Cheng367a5df2010-09-09 18:18:55 +00002988 return 2;
2989
2990 // The number of uOps for load / store multiple are determined by the number
2991 // registers.
Andrew Trickc416ba62010-12-24 04:28:06 +00002992 //
Evan Chengbf407072010-09-10 01:29:16 +00002993 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
2994 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledru35521e22012-07-23 08:51:15 +00002995 // separately by assuming the address is not 64-bit aligned.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002996 //
Evan Chengbf407072010-09-10 01:29:16 +00002997 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002998 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
2999 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3000 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003001 case ARM::VLDMDIA_UPD:
3002 case ARM::VLDMDDB_UPD:
3003 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003004 case ARM::VLDMSIA_UPD:
3005 case ARM::VLDMSDB_UPD:
3006 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003007 case ARM::VSTMDIA_UPD:
3008 case ARM::VSTMDDB_UPD:
3009 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003010 case ARM::VSTMSIA_UPD:
3011 case ARM::VSTMSDB_UPD: {
Evan Cheng367a5df2010-09-09 18:18:55 +00003012 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
3013 return (NumRegs / 2) + (NumRegs % 2) + 1;
3014 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003015
3016 case ARM::LDMIA_RET:
3017 case ARM::LDMIA:
3018 case ARM::LDMDA:
3019 case ARM::LDMDB:
3020 case ARM::LDMIB:
3021 case ARM::LDMIA_UPD:
3022 case ARM::LDMDA_UPD:
3023 case ARM::LDMDB_UPD:
3024 case ARM::LDMIB_UPD:
3025 case ARM::STMIA:
3026 case ARM::STMDA:
3027 case ARM::STMDB:
3028 case ARM::STMIB:
3029 case ARM::STMIA_UPD:
3030 case ARM::STMDA_UPD:
3031 case ARM::STMDB_UPD:
3032 case ARM::STMIB_UPD:
3033 case ARM::tLDMIA:
3034 case ARM::tLDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003035 case ARM::tSTMIA_UPD:
Evan Cheng367a5df2010-09-09 18:18:55 +00003036 case ARM::tPOP_RET:
3037 case ARM::tPOP:
3038 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003039 case ARM::t2LDMIA_RET:
3040 case ARM::t2LDMIA:
3041 case ARM::t2LDMDB:
3042 case ARM::t2LDMIA_UPD:
3043 case ARM::t2LDMDB_UPD:
3044 case ARM::t2STMIA:
3045 case ARM::t2STMDB:
3046 case ARM::t2STMIA_UPD:
3047 case ARM::t2STMDB_UPD: {
Evan Chengbf407072010-09-10 01:29:16 +00003048 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003049 if (Subtarget.isSwift()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003050 int UOps = 1 + NumRegs; // One for address computation, one for each ld / st.
3051 switch (Opc) {
3052 default: break;
3053 case ARM::VLDMDIA_UPD:
3054 case ARM::VLDMDDB_UPD:
3055 case ARM::VLDMSIA_UPD:
3056 case ARM::VLDMSDB_UPD:
3057 case ARM::VSTMDIA_UPD:
3058 case ARM::VSTMDDB_UPD:
3059 case ARM::VSTMSIA_UPD:
3060 case ARM::VSTMSDB_UPD:
3061 case ARM::LDMIA_UPD:
3062 case ARM::LDMDA_UPD:
3063 case ARM::LDMDB_UPD:
3064 case ARM::LDMIB_UPD:
3065 case ARM::STMIA_UPD:
3066 case ARM::STMDA_UPD:
3067 case ARM::STMDB_UPD:
3068 case ARM::STMIB_UPD:
3069 case ARM::tLDMIA_UPD:
3070 case ARM::tSTMIA_UPD:
3071 case ARM::t2LDMIA_UPD:
3072 case ARM::t2LDMDB_UPD:
3073 case ARM::t2STMIA_UPD:
3074 case ARM::t2STMDB_UPD:
3075 ++UOps; // One for base register writeback.
3076 break;
3077 case ARM::LDMIA_RET:
3078 case ARM::tPOP_RET:
3079 case ARM::t2LDMIA_RET:
3080 UOps += 2; // One for base reg wb, one for write to pc.
3081 break;
3082 }
3083 return UOps;
Tim Northover0feb91e2014-04-01 14:10:07 +00003084 } else if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Chengdebf9c52010-11-03 00:45:17 +00003085 if (NumRegs < 4)
3086 return 2;
3087 // 4 registers would be issued: 2, 2.
3088 // 5 registers would be issued: 2, 2, 1.
Andrew Trickf161e392012-07-02 18:10:42 +00003089 int A8UOps = (NumRegs / 2);
Evan Chengdebf9c52010-11-03 00:45:17 +00003090 if (NumRegs % 2)
Andrew Trickf161e392012-07-02 18:10:42 +00003091 ++A8UOps;
3092 return A8UOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003093 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Andrew Trickf161e392012-07-02 18:10:42 +00003094 int A9UOps = (NumRegs / 2);
Evan Chengbf407072010-09-10 01:29:16 +00003095 // If there are odd number of registers or if it's not 64-bit aligned,
3096 // then it takes an extra AGU (Address Generation Unit) cycle.
3097 if ((NumRegs % 2) ||
3098 !MI->hasOneMemOperand() ||
3099 (*MI->memoperands_begin())->getAlignment() < 8)
Andrew Trickf161e392012-07-02 18:10:42 +00003100 ++A9UOps;
3101 return A9UOps;
Evan Chengbf407072010-09-10 01:29:16 +00003102 } else {
3103 // Assume the worst.
3104 return NumRegs;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00003105 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003106 }
3107 }
3108}
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003109
3110int
Evan Cheng412e37b2010-10-07 23:12:15 +00003111ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003112 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003113 unsigned DefClass,
3114 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003115 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003116 if (RegNo <= 0)
3117 // Def is the address writeback.
3118 return ItinData->getOperandCycle(DefClass, DefIdx);
3119
3120 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003121 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003122 // (regno / 2) + (regno % 2) + 1
3123 DefCycle = RegNo / 2 + 1;
3124 if (RegNo % 2)
3125 ++DefCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003126 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003127 DefCycle = RegNo;
3128 bool isSLoad = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003129
Evan Cheng6cc775f2011-06-28 19:10:37 +00003130 switch (DefMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003131 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003132 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003133 case ARM::VLDMSIA_UPD:
3134 case ARM::VLDMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003135 isSLoad = true;
3136 break;
3137 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003138
Evan Cheng412e37b2010-10-07 23:12:15 +00003139 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3140 // then it takes an extra cycle.
3141 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3142 ++DefCycle;
3143 } else {
3144 // Assume the worst.
3145 DefCycle = RegNo + 2;
3146 }
3147
3148 return DefCycle;
3149}
3150
3151int
3152ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003153 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003154 unsigned DefClass,
3155 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003156 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003157 if (RegNo <= 0)
3158 // Def is the address writeback.
3159 return ItinData->getOperandCycle(DefClass, DefIdx);
3160
3161 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003162 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003163 // 4 registers would be issued: 1, 2, 1.
3164 // 5 registers would be issued: 1, 2, 2.
3165 DefCycle = RegNo / 2;
3166 if (DefCycle < 1)
3167 DefCycle = 1;
3168 // Result latency is issue cycle + 2: E2.
3169 DefCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003170 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003171 DefCycle = (RegNo / 2);
3172 // If there are odd number of registers or if it's not 64-bit aligned,
3173 // then it takes an extra AGU (Address Generation Unit) cycle.
3174 if ((RegNo % 2) || DefAlign < 8)
3175 ++DefCycle;
3176 // Result latency is AGU cycles + 2.
3177 DefCycle += 2;
3178 } else {
3179 // Assume the worst.
3180 DefCycle = RegNo + 2;
3181 }
3182
3183 return DefCycle;
3184}
3185
3186int
3187ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003188 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003189 unsigned UseClass,
3190 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003191 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003192 if (RegNo <= 0)
3193 return ItinData->getOperandCycle(UseClass, UseIdx);
3194
3195 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003196 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003197 // (regno / 2) + (regno % 2) + 1
3198 UseCycle = RegNo / 2 + 1;
3199 if (RegNo % 2)
3200 ++UseCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003201 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003202 UseCycle = RegNo;
3203 bool isSStore = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003204
Evan Cheng6cc775f2011-06-28 19:10:37 +00003205 switch (UseMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003206 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003207 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003208 case ARM::VSTMSIA_UPD:
3209 case ARM::VSTMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003210 isSStore = true;
3211 break;
3212 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003213
Evan Cheng412e37b2010-10-07 23:12:15 +00003214 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3215 // then it takes an extra cycle.
3216 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3217 ++UseCycle;
3218 } else {
3219 // Assume the worst.
3220 UseCycle = RegNo + 2;
3221 }
3222
3223 return UseCycle;
3224}
3225
3226int
3227ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003228 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003229 unsigned UseClass,
3230 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003231 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003232 if (RegNo <= 0)
3233 return ItinData->getOperandCycle(UseClass, UseIdx);
3234
3235 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003236 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003237 UseCycle = RegNo / 2;
3238 if (UseCycle < 2)
3239 UseCycle = 2;
3240 // Read in E3.
3241 UseCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003242 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003243 UseCycle = (RegNo / 2);
3244 // If there are odd number of registers or if it's not 64-bit aligned,
3245 // then it takes an extra AGU (Address Generation Unit) cycle.
3246 if ((RegNo % 2) || UseAlign < 8)
3247 ++UseCycle;
3248 } else {
3249 // Assume the worst.
3250 UseCycle = 1;
3251 }
3252 return UseCycle;
3253}
3254
3255int
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003256ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003257 const MCInstrDesc &DefMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003258 unsigned DefIdx, unsigned DefAlign,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003259 const MCInstrDesc &UseMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003260 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003261 unsigned DefClass = DefMCID.getSchedClass();
3262 unsigned UseClass = UseMCID.getSchedClass();
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003263
Evan Cheng6cc775f2011-06-28 19:10:37 +00003264 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003265 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3266
3267 // This may be a def / use of a variable_ops instruction, the operand
3268 // latency might be determinable dynamically. Let the target try to
3269 // figure it out.
Evan Chenge2c211c2010-10-28 02:00:25 +00003270 int DefCycle = -1;
Evan Chengff310732010-10-28 06:47:08 +00003271 bool LdmBypass = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003272 switch (DefMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003273 default:
3274 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3275 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003276
3277 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003278 case ARM::VLDMDIA_UPD:
3279 case ARM::VLDMDDB_UPD:
3280 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003281 case ARM::VLDMSIA_UPD:
3282 case ARM::VLDMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003283 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003284 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003285
3286 case ARM::LDMIA_RET:
3287 case ARM::LDMIA:
3288 case ARM::LDMDA:
3289 case ARM::LDMDB:
3290 case ARM::LDMIB:
3291 case ARM::LDMIA_UPD:
3292 case ARM::LDMDA_UPD:
3293 case ARM::LDMDB_UPD:
3294 case ARM::LDMIB_UPD:
3295 case ARM::tLDMIA:
3296 case ARM::tLDMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003297 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003298 case ARM::t2LDMIA_RET:
3299 case ARM::t2LDMIA:
3300 case ARM::t2LDMDB:
3301 case ARM::t2LDMIA_UPD:
3302 case ARM::t2LDMDB_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003303 LdmBypass = 1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003304 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng412e37b2010-10-07 23:12:15 +00003305 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003306 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003307
3308 if (DefCycle == -1)
3309 // We can't seem to determine the result latency of the def, assume it's 2.
3310 DefCycle = 2;
3311
3312 int UseCycle = -1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003313 switch (UseMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003314 default:
3315 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3316 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003317
3318 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003319 case ARM::VSTMDIA_UPD:
3320 case ARM::VSTMDDB_UPD:
3321 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003322 case ARM::VSTMSIA_UPD:
3323 case ARM::VSTMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003324 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003325 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003326
3327 case ARM::STMIA:
3328 case ARM::STMDA:
3329 case ARM::STMDB:
3330 case ARM::STMIB:
3331 case ARM::STMIA_UPD:
3332 case ARM::STMDA_UPD:
3333 case ARM::STMDB_UPD:
3334 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003335 case ARM::tSTMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003336 case ARM::tPOP_RET:
3337 case ARM::tPOP:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003338 case ARM::t2STMIA:
3339 case ARM::t2STMDB:
3340 case ARM::t2STMIA_UPD:
3341 case ARM::t2STMDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003342 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003343 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003344 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003345
3346 if (UseCycle == -1)
3347 // Assume it's read in the first stage.
3348 UseCycle = 1;
3349
3350 UseCycle = DefCycle - UseCycle + 1;
3351 if (UseCycle > 0) {
3352 if (LdmBypass) {
3353 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3354 // first def operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003355 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003356 UseClass, UseIdx))
3357 --UseCycle;
3358 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003359 UseClass, UseIdx)) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003360 --UseCycle;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003361 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003362 }
3363
3364 return UseCycle;
3365}
3366
Evan Cheng7fae11b2011-12-14 02:11:42 +00003367static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003368 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003369 unsigned &DefIdx, unsigned &Dist) {
3370 Dist = 0;
3371
3372 MachineBasicBlock::const_iterator I = MI; ++I;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003373 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
Evan Cheng7fae11b2011-12-14 02:11:42 +00003374 assert(II->isInsideBundle() && "Empty bundle?");
3375
3376 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003377 while (II->isInsideBundle()) {
3378 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3379 if (Idx != -1)
3380 break;
3381 --II;
3382 ++Dist;
3383 }
3384
3385 assert(Idx != -1 && "Cannot find bundled definition!");
3386 DefIdx = Idx;
3387 return II;
3388}
3389
3390static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003391 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003392 unsigned &UseIdx, unsigned &Dist) {
3393 Dist = 0;
3394
3395 MachineBasicBlock::const_instr_iterator II = MI; ++II;
3396 assert(II->isInsideBundle() && "Empty bundle?");
3397 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3398
3399 // FIXME: This doesn't properly handle multiple uses.
3400 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003401 while (II != E && II->isInsideBundle()) {
3402 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3403 if (Idx != -1)
3404 break;
3405 if (II->getOpcode() != ARM::t2IT)
3406 ++Dist;
3407 ++II;
3408 }
3409
Evan Chengda103bf2011-12-14 20:00:08 +00003410 if (Idx == -1) {
3411 Dist = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003412 return nullptr;
Evan Chengda103bf2011-12-14 20:00:08 +00003413 }
3414
Evan Cheng7fae11b2011-12-14 02:11:42 +00003415 UseIdx = Idx;
3416 return II;
3417}
3418
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003419/// Return the number of cycles to add to (or subtract from) the static
3420/// itinerary based on the def opcode and alignment. The caller will ensure that
3421/// adjusted latency is at least one cycle.
3422static int adjustDefLatency(const ARMSubtarget &Subtarget,
3423 const MachineInstr *DefMI,
3424 const MCInstrDesc *DefMCID, unsigned DefAlign) {
3425 int Adjust = 0;
Tim Northover0feb91e2014-04-01 14:10:07 +00003426 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
Evan Chengff310732010-10-28 06:47:08 +00003427 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3428 // variants are one cycle cheaper.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003429 switch (DefMCID->getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003430 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003431 case ARM::LDRrs:
3432 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003433 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3434 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3435 if (ShImm == 0 ||
3436 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003437 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003438 break;
3439 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003440 case ARM::t2LDRs:
3441 case ARM::t2LDRBs:
3442 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003443 case ARM::t2LDRSHs: {
3444 // Thumb2 mode: lsl only.
3445 unsigned ShAmt = DefMI->getOperand(3).getImm();
3446 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003447 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003448 break;
3449 }
3450 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003451 } else if (Subtarget.isSwift()) {
3452 // FIXME: Properly handle all of the latency adjustments for address
3453 // writeback.
3454 switch (DefMCID->getOpcode()) {
3455 default: break;
3456 case ARM::LDRrs:
3457 case ARM::LDRBrs: {
3458 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3459 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3460 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3461 if (!isSub &&
3462 (ShImm == 0 ||
3463 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3464 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3465 Adjust -= 2;
3466 else if (!isSub &&
3467 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3468 --Adjust;
3469 break;
3470 }
3471 case ARM::t2LDRs:
3472 case ARM::t2LDRBs:
3473 case ARM::t2LDRHs:
3474 case ARM::t2LDRSHs: {
3475 // Thumb2 mode: lsl only.
3476 unsigned ShAmt = DefMI->getOperand(3).getImm();
3477 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3478 Adjust -= 2;
3479 break;
3480 }
3481 }
Evan Chengff310732010-10-28 06:47:08 +00003482 }
3483
Silviu Barangab47bb942012-09-13 15:05:10 +00003484 if (DefAlign < 8 && Subtarget.isLikeA9()) {
Evan Cheng7fae11b2011-12-14 02:11:42 +00003485 switch (DefMCID->getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003486 default: break;
3487 case ARM::VLD1q8:
3488 case ARM::VLD1q16:
3489 case ARM::VLD1q32:
3490 case ARM::VLD1q64:
Jim Grosbach2098cb12011-10-24 21:45:13 +00003491 case ARM::VLD1q8wb_fixed:
3492 case ARM::VLD1q16wb_fixed:
3493 case ARM::VLD1q32wb_fixed:
3494 case ARM::VLD1q64wb_fixed:
3495 case ARM::VLD1q8wb_register:
3496 case ARM::VLD1q16wb_register:
3497 case ARM::VLD1q32wb_register:
3498 case ARM::VLD1q64wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003499 case ARM::VLD2d8:
3500 case ARM::VLD2d16:
3501 case ARM::VLD2d32:
3502 case ARM::VLD2q8:
3503 case ARM::VLD2q16:
3504 case ARM::VLD2q32:
Jim Grosbachd146a022011-12-09 21:28:25 +00003505 case ARM::VLD2d8wb_fixed:
3506 case ARM::VLD2d16wb_fixed:
3507 case ARM::VLD2d32wb_fixed:
3508 case ARM::VLD2q8wb_fixed:
3509 case ARM::VLD2q16wb_fixed:
3510 case ARM::VLD2q32wb_fixed:
3511 case ARM::VLD2d8wb_register:
3512 case ARM::VLD2d16wb_register:
3513 case ARM::VLD2d32wb_register:
3514 case ARM::VLD2q8wb_register:
3515 case ARM::VLD2q16wb_register:
3516 case ARM::VLD2q32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003517 case ARM::VLD3d8:
3518 case ARM::VLD3d16:
3519 case ARM::VLD3d32:
3520 case ARM::VLD1d64T:
3521 case ARM::VLD3d8_UPD:
3522 case ARM::VLD3d16_UPD:
3523 case ARM::VLD3d32_UPD:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00003524 case ARM::VLD1d64Twb_fixed:
3525 case ARM::VLD1d64Twb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003526 case ARM::VLD3q8_UPD:
3527 case ARM::VLD3q16_UPD:
3528 case ARM::VLD3q32_UPD:
3529 case ARM::VLD4d8:
3530 case ARM::VLD4d16:
3531 case ARM::VLD4d32:
3532 case ARM::VLD1d64Q:
3533 case ARM::VLD4d8_UPD:
3534 case ARM::VLD4d16_UPD:
3535 case ARM::VLD4d32_UPD:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00003536 case ARM::VLD1d64Qwb_fixed:
3537 case ARM::VLD1d64Qwb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003538 case ARM::VLD4q8_UPD:
3539 case ARM::VLD4q16_UPD:
3540 case ARM::VLD4q32_UPD:
3541 case ARM::VLD1DUPq8:
3542 case ARM::VLD1DUPq16:
3543 case ARM::VLD1DUPq32:
Jim Grosbacha68c9a82011-11-30 19:35:44 +00003544 case ARM::VLD1DUPq8wb_fixed:
3545 case ARM::VLD1DUPq16wb_fixed:
3546 case ARM::VLD1DUPq32wb_fixed:
3547 case ARM::VLD1DUPq8wb_register:
3548 case ARM::VLD1DUPq16wb_register:
3549 case ARM::VLD1DUPq32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003550 case ARM::VLD2DUPd8:
3551 case ARM::VLD2DUPd16:
3552 case ARM::VLD2DUPd32:
Jim Grosbachc80a2642011-12-21 19:40:55 +00003553 case ARM::VLD2DUPd8wb_fixed:
3554 case ARM::VLD2DUPd16wb_fixed:
3555 case ARM::VLD2DUPd32wb_fixed:
3556 case ARM::VLD2DUPd8wb_register:
3557 case ARM::VLD2DUPd16wb_register:
3558 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003559 case ARM::VLD4DUPd8:
3560 case ARM::VLD4DUPd16:
3561 case ARM::VLD4DUPd32:
3562 case ARM::VLD4DUPd8_UPD:
3563 case ARM::VLD4DUPd16_UPD:
3564 case ARM::VLD4DUPd32_UPD:
3565 case ARM::VLD1LNd8:
3566 case ARM::VLD1LNd16:
3567 case ARM::VLD1LNd32:
3568 case ARM::VLD1LNd8_UPD:
3569 case ARM::VLD1LNd16_UPD:
3570 case ARM::VLD1LNd32_UPD:
3571 case ARM::VLD2LNd8:
3572 case ARM::VLD2LNd16:
3573 case ARM::VLD2LNd32:
3574 case ARM::VLD2LNq16:
3575 case ARM::VLD2LNq32:
3576 case ARM::VLD2LNd8_UPD:
3577 case ARM::VLD2LNd16_UPD:
3578 case ARM::VLD2LNd32_UPD:
3579 case ARM::VLD2LNq16_UPD:
3580 case ARM::VLD2LNq32_UPD:
3581 case ARM::VLD4LNd8:
3582 case ARM::VLD4LNd16:
3583 case ARM::VLD4LNd32:
3584 case ARM::VLD4LNq16:
3585 case ARM::VLD4LNq32:
3586 case ARM::VLD4LNd8_UPD:
3587 case ARM::VLD4LNd16_UPD:
3588 case ARM::VLD4LNd32_UPD:
3589 case ARM::VLD4LNq16_UPD:
3590 case ARM::VLD4LNq32_UPD:
3591 // If the address is not 64-bit aligned, the latencies of these
3592 // instructions increases by one.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003593 ++Adjust;
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003594 break;
3595 }
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003596 }
3597 return Adjust;
3598}
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003599
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003600
3601
3602int
3603ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3604 const MachineInstr *DefMI, unsigned DefIdx,
3605 const MachineInstr *UseMI,
3606 unsigned UseIdx) const {
3607 // No operand latency. The caller may fall back to getInstrLatency.
3608 if (!ItinData || ItinData->isEmpty())
3609 return -1;
3610
3611 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
3612 unsigned Reg = DefMO.getReg();
3613 const MCInstrDesc *DefMCID = &DefMI->getDesc();
3614 const MCInstrDesc *UseMCID = &UseMI->getDesc();
3615
3616 unsigned DefAdj = 0;
3617 if (DefMI->isBundle()) {
3618 DefMI = getBundledDefMI(&getRegisterInfo(), DefMI, Reg, DefIdx, DefAdj);
3619 DefMCID = &DefMI->getDesc();
3620 }
3621 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
3622 DefMI->isRegSequence() || DefMI->isImplicitDef()) {
3623 return 1;
3624 }
3625
3626 unsigned UseAdj = 0;
3627 if (UseMI->isBundle()) {
3628 unsigned NewUseIdx;
3629 const MachineInstr *NewUseMI = getBundledUseMI(&getRegisterInfo(), UseMI,
3630 Reg, NewUseIdx, UseAdj);
Andrew Trick77d0b882012-06-22 02:50:33 +00003631 if (!NewUseMI)
3632 return -1;
3633
3634 UseMI = NewUseMI;
3635 UseIdx = NewUseIdx;
3636 UseMCID = &UseMI->getDesc();
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003637 }
3638
3639 if (Reg == ARM::CPSR) {
3640 if (DefMI->getOpcode() == ARM::FMSTAT) {
3641 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Barangab47bb942012-09-13 15:05:10 +00003642 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003643 }
3644
3645 // CPSR set and branch can be paired in the same cycle.
3646 if (UseMI->isBranch())
3647 return 0;
3648
3649 // Otherwise it takes the instruction latency (generally one).
3650 unsigned Latency = getInstrLatency(ItinData, DefMI);
3651
3652 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3653 // its uses. Instructions which are otherwise scheduled between them may
3654 // incur a code size penalty (not able to use the CPSR setting 16-bit
3655 // instructions).
3656 if (Latency > 0 && Subtarget.isThumb2()) {
3657 const MachineFunction *MF = DefMI->getParent()->getParent();
Bill Wendling698e84f2012-12-30 10:32:01 +00003658 if (MF->getFunction()->getAttributes().
3659 hasAttribute(AttributeSet::FunctionIndex,
3660 Attribute::OptimizeForSize))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003661 --Latency;
3662 }
3663 return Latency;
3664 }
3665
Andrew Trick77d0b882012-06-22 02:50:33 +00003666 if (DefMO.isImplicit() || UseMI->getOperand(UseIdx).isImplicit())
3667 return -1;
3668
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003669 unsigned DefAlign = DefMI->hasOneMemOperand()
3670 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
3671 unsigned UseAlign = UseMI->hasOneMemOperand()
3672 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
3673
3674 // Get the itinerary's latency if possible, and handle variable_ops.
3675 int Latency = getOperandLatency(ItinData, *DefMCID, DefIdx, DefAlign,
3676 *UseMCID, UseIdx, UseAlign);
3677 // Unable to find operand latency. The caller may resort to getInstrLatency.
3678 if (Latency < 0)
3679 return Latency;
3680
3681 // Adjust for IT block position.
3682 int Adj = DefAdj + UseAdj;
3683
3684 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3685 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
3686 if (Adj >= 0 || (int)Latency > -Adj) {
3687 return Latency + Adj;
3688 }
3689 // Return the itinerary latency, which may be zero but not less than zero.
Evan Chengff310732010-10-28 06:47:08 +00003690 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003691}
3692
3693int
3694ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3695 SDNode *DefNode, unsigned DefIdx,
3696 SDNode *UseNode, unsigned UseIdx) const {
3697 if (!DefNode->isMachineOpcode())
3698 return 1;
3699
Evan Cheng6cc775f2011-06-28 19:10:37 +00003700 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trick47ff14b2011-01-21 05:51:33 +00003701
Evan Cheng6cc775f2011-06-28 19:10:37 +00003702 if (isZeroCost(DefMCID.Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +00003703 return 0;
3704
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003705 if (!ItinData || ItinData->isEmpty())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003706 return DefMCID.mayLoad() ? 3 : 1;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003707
Evan Cheng6c1414f2010-10-29 18:09:28 +00003708 if (!UseNode->isMachineOpcode()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003709 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003710 if (Subtarget.isLikeA9() || Subtarget.isSwift())
Evan Cheng6c1414f2010-10-29 18:09:28 +00003711 return Latency <= 2 ? 1 : Latency - 1;
3712 else
3713 return Latency <= 3 ? 1 : Latency - 2;
3714 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003715
Evan Cheng6cc775f2011-06-28 19:10:37 +00003716 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003717 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
3718 unsigned DefAlign = !DefMN->memoperands_empty()
3719 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
3720 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
3721 unsigned UseAlign = !UseMN->memoperands_empty()
3722 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003723 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
3724 UseMCID, UseIdx, UseAlign);
Evan Chengff310732010-10-28 06:47:08 +00003725
3726 if (Latency > 1 &&
Tim Northover0feb91e2014-04-01 14:10:07 +00003727 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
3728 Subtarget.isCortexA7())) {
Evan Chengff310732010-10-28 06:47:08 +00003729 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3730 // variants are one cycle cheaper.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003731 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003732 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003733 case ARM::LDRrs:
3734 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003735 unsigned ShOpVal =
3736 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3737 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3738 if (ShImm == 0 ||
3739 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3740 --Latency;
3741 break;
3742 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003743 case ARM::t2LDRs:
3744 case ARM::t2LDRBs:
3745 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003746 case ARM::t2LDRSHs: {
3747 // Thumb2 mode: lsl only.
3748 unsigned ShAmt =
3749 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3750 if (ShAmt == 0 || ShAmt == 2)
3751 --Latency;
3752 break;
3753 }
3754 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003755 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
3756 // FIXME: Properly handle all of the latency adjustments for address
3757 // writeback.
3758 switch (DefMCID.getOpcode()) {
3759 default: break;
3760 case ARM::LDRrs:
3761 case ARM::LDRBrs: {
3762 unsigned ShOpVal =
3763 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3764 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3765 if (ShImm == 0 ||
3766 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3767 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3768 Latency -= 2;
3769 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3770 --Latency;
3771 break;
3772 }
3773 case ARM::t2LDRs:
3774 case ARM::t2LDRBs:
3775 case ARM::t2LDRHs:
3776 case ARM::t2LDRSHs: {
3777 // Thumb2 mode: lsl 0-3 only.
3778 Latency -= 2;
3779 break;
3780 }
3781 }
Evan Chengff310732010-10-28 06:47:08 +00003782 }
3783
Silviu Barangab47bb942012-09-13 15:05:10 +00003784 if (DefAlign < 8 && Subtarget.isLikeA9())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003785 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003786 default: break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003787 case ARM::VLD1q8:
3788 case ARM::VLD1q16:
3789 case ARM::VLD1q32:
3790 case ARM::VLD1q64:
3791 case ARM::VLD1q8wb_register:
3792 case ARM::VLD1q16wb_register:
3793 case ARM::VLD1q32wb_register:
3794 case ARM::VLD1q64wb_register:
3795 case ARM::VLD1q8wb_fixed:
3796 case ARM::VLD1q16wb_fixed:
3797 case ARM::VLD1q32wb_fixed:
3798 case ARM::VLD1q64wb_fixed:
3799 case ARM::VLD2d8:
3800 case ARM::VLD2d16:
3801 case ARM::VLD2d32:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003802 case ARM::VLD2q8Pseudo:
3803 case ARM::VLD2q16Pseudo:
3804 case ARM::VLD2q32Pseudo:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003805 case ARM::VLD2d8wb_fixed:
3806 case ARM::VLD2d16wb_fixed:
3807 case ARM::VLD2d32wb_fixed:
Jim Grosbachd146a022011-12-09 21:28:25 +00003808 case ARM::VLD2q8PseudoWB_fixed:
3809 case ARM::VLD2q16PseudoWB_fixed:
3810 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003811 case ARM::VLD2d8wb_register:
3812 case ARM::VLD2d16wb_register:
3813 case ARM::VLD2d32wb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00003814 case ARM::VLD2q8PseudoWB_register:
3815 case ARM::VLD2q16PseudoWB_register:
3816 case ARM::VLD2q32PseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003817 case ARM::VLD3d8Pseudo:
3818 case ARM::VLD3d16Pseudo:
3819 case ARM::VLD3d32Pseudo:
3820 case ARM::VLD1d64TPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003821 case ARM::VLD1d64TPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003822 case ARM::VLD3d8Pseudo_UPD:
3823 case ARM::VLD3d16Pseudo_UPD:
3824 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003825 case ARM::VLD3q8Pseudo_UPD:
3826 case ARM::VLD3q16Pseudo_UPD:
3827 case ARM::VLD3q32Pseudo_UPD:
3828 case ARM::VLD3q8oddPseudo:
3829 case ARM::VLD3q16oddPseudo:
3830 case ARM::VLD3q32oddPseudo:
3831 case ARM::VLD3q8oddPseudo_UPD:
3832 case ARM::VLD3q16oddPseudo_UPD:
3833 case ARM::VLD3q32oddPseudo_UPD:
3834 case ARM::VLD4d8Pseudo:
3835 case ARM::VLD4d16Pseudo:
3836 case ARM::VLD4d32Pseudo:
3837 case ARM::VLD1d64QPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003838 case ARM::VLD1d64QPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003839 case ARM::VLD4d8Pseudo_UPD:
3840 case ARM::VLD4d16Pseudo_UPD:
3841 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003842 case ARM::VLD4q8Pseudo_UPD:
3843 case ARM::VLD4q16Pseudo_UPD:
3844 case ARM::VLD4q32Pseudo_UPD:
3845 case ARM::VLD4q8oddPseudo:
3846 case ARM::VLD4q16oddPseudo:
3847 case ARM::VLD4q32oddPseudo:
3848 case ARM::VLD4q8oddPseudo_UPD:
3849 case ARM::VLD4q16oddPseudo_UPD:
3850 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbach13a292c2012-03-06 22:01:44 +00003851 case ARM::VLD1DUPq8:
3852 case ARM::VLD1DUPq16:
3853 case ARM::VLD1DUPq32:
3854 case ARM::VLD1DUPq8wb_fixed:
3855 case ARM::VLD1DUPq16wb_fixed:
3856 case ARM::VLD1DUPq32wb_fixed:
3857 case ARM::VLD1DUPq8wb_register:
3858 case ARM::VLD1DUPq16wb_register:
3859 case ARM::VLD1DUPq32wb_register:
3860 case ARM::VLD2DUPd8:
3861 case ARM::VLD2DUPd16:
3862 case ARM::VLD2DUPd32:
3863 case ARM::VLD2DUPd8wb_fixed:
3864 case ARM::VLD2DUPd16wb_fixed:
3865 case ARM::VLD2DUPd32wb_fixed:
3866 case ARM::VLD2DUPd8wb_register:
3867 case ARM::VLD2DUPd16wb_register:
3868 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003869 case ARM::VLD4DUPd8Pseudo:
3870 case ARM::VLD4DUPd16Pseudo:
3871 case ARM::VLD4DUPd32Pseudo:
3872 case ARM::VLD4DUPd8Pseudo_UPD:
3873 case ARM::VLD4DUPd16Pseudo_UPD:
3874 case ARM::VLD4DUPd32Pseudo_UPD:
3875 case ARM::VLD1LNq8Pseudo:
3876 case ARM::VLD1LNq16Pseudo:
3877 case ARM::VLD1LNq32Pseudo:
3878 case ARM::VLD1LNq8Pseudo_UPD:
3879 case ARM::VLD1LNq16Pseudo_UPD:
3880 case ARM::VLD1LNq32Pseudo_UPD:
3881 case ARM::VLD2LNd8Pseudo:
3882 case ARM::VLD2LNd16Pseudo:
3883 case ARM::VLD2LNd32Pseudo:
3884 case ARM::VLD2LNq16Pseudo:
3885 case ARM::VLD2LNq32Pseudo:
3886 case ARM::VLD2LNd8Pseudo_UPD:
3887 case ARM::VLD2LNd16Pseudo_UPD:
3888 case ARM::VLD2LNd32Pseudo_UPD:
3889 case ARM::VLD2LNq16Pseudo_UPD:
3890 case ARM::VLD2LNq32Pseudo_UPD:
3891 case ARM::VLD4LNd8Pseudo:
3892 case ARM::VLD4LNd16Pseudo:
3893 case ARM::VLD4LNd32Pseudo:
3894 case ARM::VLD4LNq16Pseudo:
3895 case ARM::VLD4LNq32Pseudo:
3896 case ARM::VLD4LNd8Pseudo_UPD:
3897 case ARM::VLD4LNd16Pseudo_UPD:
3898 case ARM::VLD4LNd32Pseudo_UPD:
3899 case ARM::VLD4LNq16Pseudo_UPD:
3900 case ARM::VLD4LNq32Pseudo_UPD:
3901 // If the address is not 64-bit aligned, the latencies of these
3902 // instructions increases by one.
3903 ++Latency;
3904 break;
3905 }
3906
Evan Chengff310732010-10-28 06:47:08 +00003907 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003908}
Evan Cheng63c76082010-10-19 18:58:51 +00003909
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00003910unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr *MI) const {
3911 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3912 MI->isRegSequence() || MI->isImplicitDef())
3913 return 0;
3914
3915 if (MI->isBundle())
3916 return 0;
3917
3918 const MCInstrDesc &MCID = MI->getDesc();
3919
3920 if (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR)) {
3921 // When predicated, CPSR is an additional source operand for CPSR updating
3922 // instructions, this apparently increases their latencies.
3923 return 1;
3924 }
3925 return 0;
3926}
3927
Andrew Trick45446062012-06-05 21:11:27 +00003928unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3929 const MachineInstr *MI,
3930 unsigned *PredCost) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00003931 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3932 MI->isRegSequence() || MI->isImplicitDef())
3933 return 1;
3934
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003935 // An instruction scheduler typically runs on unbundled instructions, however
3936 // other passes may query the latency of a bundled instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003937 if (MI->isBundle()) {
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003938 unsigned Latency = 0;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003939 MachineBasicBlock::const_instr_iterator I = MI;
3940 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3941 while (++I != E && I->isInsideBundle()) {
3942 if (I->getOpcode() != ARM::t2IT)
3943 Latency += getInstrLatency(ItinData, I, PredCost);
3944 }
3945 return Latency;
3946 }
3947
Evan Cheng6cc775f2011-06-28 19:10:37 +00003948 const MCInstrDesc &MCID = MI->getDesc();
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003949 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) {
Evan Chengdebf9c52010-11-03 00:45:17 +00003950 // When predicated, CPSR is an additional source operand for CPSR updating
3951 // instructions, this apparently increases their latencies.
3952 *PredCost = 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003953 }
3954 // Be sure to call getStageLatency for an empty itinerary in case it has a
3955 // valid MinLatency property.
3956 if (!ItinData)
3957 return MI->mayLoad() ? 3 : 1;
3958
3959 unsigned Class = MCID.getSchedClass();
3960
3961 // For instructions with variable uops, use uops as latency.
Andrew Trick21cca972012-07-02 19:12:29 +00003962 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003963 return getNumMicroOps(ItinData, MI);
Andrew Trick21cca972012-07-02 19:12:29 +00003964
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003965 // For the common case, fall back on the itinerary's latency.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003966 unsigned Latency = ItinData->getStageLatency(Class);
3967
3968 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3969 unsigned DefAlign = MI->hasOneMemOperand()
3970 ? (*MI->memoperands_begin())->getAlignment() : 0;
3971 int Adj = adjustDefLatency(Subtarget, MI, &MCID, DefAlign);
3972 if (Adj >= 0 || (int)Latency > -Adj) {
3973 return Latency + Adj;
3974 }
3975 return Latency;
Evan Chengdebf9c52010-11-03 00:45:17 +00003976}
3977
3978int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3979 SDNode *Node) const {
3980 if (!Node->isMachineOpcode())
3981 return 1;
3982
3983 if (!ItinData || ItinData->isEmpty())
3984 return 1;
3985
3986 unsigned Opcode = Node->getMachineOpcode();
3987 switch (Opcode) {
3988 default:
3989 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003990 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003991 case ARM::VSTMQIA:
Evan Chengdebf9c52010-11-03 00:45:17 +00003992 return 2;
Eric Christopherb006fc92010-11-18 19:40:05 +00003993 }
Evan Chengdebf9c52010-11-03 00:45:17 +00003994}
3995
Evan Cheng63c76082010-10-19 18:58:51 +00003996bool ARMBaseInstrInfo::
3997hasHighOperandLatency(const InstrItineraryData *ItinData,
3998 const MachineRegisterInfo *MRI,
3999 const MachineInstr *DefMI, unsigned DefIdx,
4000 const MachineInstr *UseMI, unsigned UseIdx) const {
4001 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
4002 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
4003 if (Subtarget.isCortexA8() &&
4004 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
4005 // CortexA8 VFP instructions are not pipelined.
4006 return true;
4007
4008 // Hoist VFP / NEON instructions with 4 or higher latency.
Andrew Trickde2109e2013-06-15 04:49:57 +00004009 int Latency = computeOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
Andrew Trick3564bdf2012-06-07 19:41:58 +00004010 if (Latency < 0)
4011 Latency = getInstrLatency(ItinData, DefMI);
Evan Cheng63c76082010-10-19 18:58:51 +00004012 if (Latency <= 3)
4013 return false;
4014 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4015 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4016}
Evan Chenge96b8d72010-10-26 02:08:50 +00004017
4018bool ARMBaseInstrInfo::
4019hasLowDefLatency(const InstrItineraryData *ItinData,
4020 const MachineInstr *DefMI, unsigned DefIdx) const {
4021 if (!ItinData || ItinData->isEmpty())
4022 return false;
4023
4024 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
4025 if (DDomain == ARMII::DomainGeneral) {
4026 unsigned DefClass = DefMI->getDesc().getSchedClass();
4027 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4028 return (DefCycle != -1 && DefCycle <= 2);
4029 }
4030 return false;
4031}
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004032
Andrew Trick924123a2011-09-21 02:20:46 +00004033bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr *MI,
4034 StringRef &ErrInfo) const {
4035 if (convertAddSubFlagsOpcode(MI->getOpcode())) {
4036 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4037 return false;
4038 }
4039 return true;
4040}
4041
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004042// LoadStackGuard has so far only been implemented for MachO. Different code
4043// sequence is needed for other targets.
4044void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4045 unsigned LoadImmOpc,
4046 unsigned LoadOpc,
4047 Reloc::Model RM) const {
4048 MachineBasicBlock &MBB = *MI->getParent();
4049 DebugLoc DL = MI->getDebugLoc();
4050 unsigned Reg = MI->getOperand(0).getReg();
4051 const GlobalValue *GV =
4052 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4053 MachineInstrBuilder MIB;
4054
4055 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4056 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4057
4058 if (Subtarget.GVIsIndirectSymbol(GV, RM)) {
4059 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4060 MIB.addReg(Reg, RegState::Kill).addImm(0);
4061 unsigned Flag = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant;
4062 MachineMemOperand *MMO = MBB.getParent()->
4063 getMachineMemOperand(MachinePointerInfo::getGOT(), Flag, 4, 4);
4064 MIB.addMemOperand(MMO);
4065 AddDefaultPred(MIB);
4066 }
4067
4068 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4069 MIB.addReg(Reg, RegState::Kill).addImm(0);
4070 MIB.setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
4071 AddDefaultPred(MIB);
4072}
4073
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004074bool
4075ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4076 unsigned &AddSubOpc,
4077 bool &NegAcc, bool &HasLane) const {
4078 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4079 if (I == MLxEntryMap.end())
4080 return false;
4081
4082 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4083 MulOpc = Entry.MulOpc;
4084 AddSubOpc = Entry.AddSubOpc;
4085 NegAcc = Entry.NegAcc;
4086 HasLane = Entry.HasLane;
4087 return true;
4088}
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004089
4090//===----------------------------------------------------------------------===//
4091// Execution domains.
4092//===----------------------------------------------------------------------===//
4093//
4094// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4095// and some can go down both. The vmov instructions go down the VFP pipeline,
4096// but they can be changed to vorr equivalents that are executed by the NEON
4097// pipeline.
4098//
4099// We use the following execution domain numbering:
4100//
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004101enum ARMExeDomain {
4102 ExeGeneric = 0,
4103 ExeVFP = 1,
4104 ExeNEON = 2
4105};
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004106//
4107// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4108//
4109std::pair<uint16_t, uint16_t>
4110ARMBaseInstrInfo::getExecutionDomain(const MachineInstr *MI) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004111 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4112 // if they are not predicated.
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004113 if (MI->getOpcode() == ARM::VMOVD && !isPredicated(MI))
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004114 return std::make_pair(ExeVFP, (1<<ExeVFP) | (1<<ExeNEON));
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004115
Silviu Barangadc453362013-03-27 12:38:44 +00004116 // CortexA9 is particularly picky about mixing the two and wants these
Tim Northoverf6618152012-08-17 11:32:52 +00004117 // converted.
Silviu Barangadc453362013-03-27 12:38:44 +00004118 if (Subtarget.isCortexA9() && !isPredicated(MI) &&
Tim Northoverf6618152012-08-17 11:32:52 +00004119 (MI->getOpcode() == ARM::VMOVRS ||
Tim Northoverca9f3842012-08-30 10:17:45 +00004120 MI->getOpcode() == ARM::VMOVSR ||
4121 MI->getOpcode() == ARM::VMOVS))
Tim Northoverf6618152012-08-17 11:32:52 +00004122 return std::make_pair(ExeVFP, (1<<ExeVFP) | (1<<ExeNEON));
4123
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004124 // No other instructions can be swizzled, so just determine their domain.
4125 unsigned Domain = MI->getDesc().TSFlags & ARMII::DomainMask;
4126
4127 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004128 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004129
4130 // Certain instructions can go either way on Cortex-A8.
4131 // Treat them as NEON instructions.
4132 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004133 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004134
4135 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004136 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004137
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004138 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004139}
4140
Tim Northover771f1602012-08-29 16:36:07 +00004141static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4142 unsigned SReg, unsigned &Lane) {
4143 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4144 Lane = 0;
4145
4146 if (DReg != ARM::NoRegister)
4147 return DReg;
4148
4149 Lane = 1;
4150 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4151
4152 assert(DReg && "S-register with no D super-register?");
4153 return DReg;
4154}
4155
Andrew Trickd9296ec2012-10-10 05:43:01 +00004156/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloyea052562012-09-18 08:31:15 +00004157/// set ImplicitSReg to a register number that must be marked as implicit-use or
4158/// zero if no register needs to be defined as implicit-use.
4159///
4160/// If the function cannot determine if an SPR should be marked implicit use or
4161/// not, it returns false.
4162///
4163/// This function handles cases where an instruction is being modified from taking
Andrew Trickd9296ec2012-10-10 05:43:01 +00004164/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloyea052562012-09-18 08:31:15 +00004165/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4166/// lane of the DPR).
4167///
4168/// If the other SPR is defined, an implicit-use of it should be added. Else,
4169/// (including the case where the DPR itself is defined), it should not.
Andrew Trickd9296ec2012-10-10 05:43:01 +00004170///
James Molloyea052562012-09-18 08:31:15 +00004171static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
4172 MachineInstr *MI,
4173 unsigned DReg, unsigned Lane,
4174 unsigned &ImplicitSReg) {
4175 // If the DPR is defined or used already, the other SPR lane will be chained
4176 // correctly, so there is nothing to be done.
4177 if (MI->definesRegister(DReg, TRI) || MI->readsRegister(DReg, TRI)) {
4178 ImplicitSReg = 0;
4179 return true;
4180 }
4181
4182 // Otherwise we need to go searching to see if the SPR is set explicitly.
4183 ImplicitSReg = TRI->getSubReg(DReg,
4184 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4185 MachineBasicBlock::LivenessQueryResult LQR =
4186 MI->getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
4187
4188 if (LQR == MachineBasicBlock::LQR_Live)
4189 return true;
4190 else if (LQR == MachineBasicBlock::LQR_Unknown)
4191 return false;
4192
4193 // If the register is known not to be live, there is no need to add an
4194 // implicit-use.
4195 ImplicitSReg = 0;
4196 return true;
4197}
Tim Northover771f1602012-08-29 16:36:07 +00004198
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004199void
4200ARMBaseInstrInfo::setExecutionDomain(MachineInstr *MI, unsigned Domain) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004201 unsigned DstReg, SrcReg, DReg;
4202 unsigned Lane;
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00004203 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Tim Northoverf6618152012-08-17 11:32:52 +00004204 const TargetRegisterInfo *TRI = &getRegisterInfo();
Tim Northoverf6618152012-08-17 11:32:52 +00004205 switch (MI->getOpcode()) {
4206 default:
4207 llvm_unreachable("cannot handle opcode!");
4208 break;
4209 case ARM::VMOVD:
4210 if (Domain != ExeNEON)
4211 break;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004212
Tim Northoverf6618152012-08-17 11:32:52 +00004213 // Zap the predicate operands.
4214 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004215
Tim Northover771f1602012-08-29 16:36:07 +00004216 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4217 DstReg = MI->getOperand(0).getReg();
4218 SrcReg = MI->getOperand(1).getReg();
4219
4220 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4221 MI->RemoveOperand(i-1);
4222
4223 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004224 MI->setDesc(get(ARM::VORRd));
Tim Northover771f1602012-08-29 16:36:07 +00004225 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4226 .addReg(SrcReg)
4227 .addReg(SrcReg));
Tim Northoverf6618152012-08-17 11:32:52 +00004228 break;
4229 case ARM::VMOVRS:
4230 if (Domain != ExeNEON)
4231 break;
4232 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
4233
Tim Northover771f1602012-08-29 16:36:07 +00004234 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004235 DstReg = MI->getOperand(0).getReg();
4236 SrcReg = MI->getOperand(1).getReg();
4237
Tim Northover771f1602012-08-29 16:36:07 +00004238 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4239 MI->RemoveOperand(i-1);
Tim Northoverf6618152012-08-17 11:32:52 +00004240
Tim Northover771f1602012-08-29 16:36:07 +00004241 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northoverf6618152012-08-17 11:32:52 +00004242
Tim Northover771f1602012-08-29 16:36:07 +00004243 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4244 // Note that DSrc has been widened and the other lane may be undef, which
4245 // contaminates the entire register.
Tim Northoverf6618152012-08-17 11:32:52 +00004246 MI->setDesc(get(ARM::VGETLNi32));
Tim Northover771f1602012-08-29 16:36:07 +00004247 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4248 .addReg(DReg, RegState::Undef)
4249 .addImm(Lane));
Tim Northoverf6618152012-08-17 11:32:52 +00004250
Tim Northover771f1602012-08-29 16:36:07 +00004251 // The old source should be an implicit use, otherwise we might think it
4252 // was dead before here.
Tim Northoverf6618152012-08-17 11:32:52 +00004253 MIB.addReg(SrcReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004254 break;
James Molloyea052562012-09-18 08:31:15 +00004255 case ARM::VMOVSR: {
Tim Northoverf6618152012-08-17 11:32:52 +00004256 if (Domain != ExeNEON)
4257 break;
4258 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
4259
Tim Northover771f1602012-08-29 16:36:07 +00004260 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004261 DstReg = MI->getOperand(0).getReg();
4262 SrcReg = MI->getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004263
Tim Northover771f1602012-08-29 16:36:07 +00004264 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
4265
James Molloyea052562012-09-18 08:31:15 +00004266 unsigned ImplicitSReg;
4267 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
4268 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004269
Tim Northoverc8d867d2012-09-05 18:37:53 +00004270 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4271 MI->RemoveOperand(i-1);
4272
Tim Northover771f1602012-08-29 16:36:07 +00004273 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4274 // Again DDst may be undefined at the beginning of this instruction.
Tim Northoverf6618152012-08-17 11:32:52 +00004275 MI->setDesc(get(ARM::VSETLNi32));
Tim Northover726d32c2012-09-01 18:07:29 +00004276 MIB.addReg(DReg, RegState::Define)
4277 .addReg(DReg, getUndefRegState(!MI->readsRegister(DReg, TRI)))
4278 .addReg(SrcReg)
4279 .addImm(Lane);
4280 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004281
Tim Northover726d32c2012-09-01 18:07:29 +00004282 // The narrower destination must be marked as set to keep previous chains
4283 // in place.
Tim Northover771f1602012-08-29 16:36:07 +00004284 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004285 if (ImplicitSReg != 0)
4286 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004287 break;
James Molloyea052562012-09-18 08:31:15 +00004288 }
Tim Northoverca9f3842012-08-30 10:17:45 +00004289 case ARM::VMOVS: {
4290 if (Domain != ExeNEON)
4291 break;
4292
4293 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
4294 DstReg = MI->getOperand(0).getReg();
4295 SrcReg = MI->getOperand(1).getReg();
4296
Tim Northoverca9f3842012-08-30 10:17:45 +00004297 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4298 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4299 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4300
James Molloyea052562012-09-18 08:31:15 +00004301 unsigned ImplicitSReg;
4302 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4303 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004304
Tim Northoverc8d867d2012-09-05 18:37:53 +00004305 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4306 MI->RemoveOperand(i-1);
4307
Tim Northoverca9f3842012-08-30 10:17:45 +00004308 if (DSrc == DDst) {
4309 // Destination can be:
4310 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
4311 MI->setDesc(get(ARM::VDUPLN32d));
Tim Northover726d32c2012-09-01 18:07:29 +00004312 MIB.addReg(DDst, RegState::Define)
4313 .addReg(DDst, getUndefRegState(!MI->readsRegister(DDst, TRI)))
4314 .addImm(SrcLane);
4315 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004316
4317 // Neither the source or the destination are naturally represented any
4318 // more, so add them in manually.
4319 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4320 MIB.addReg(SrcReg, RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004321 if (ImplicitSReg != 0)
4322 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004323 break;
4324 }
4325
4326 // In general there's no single instruction that can perform an S <-> S
4327 // move in NEON space, but a pair of VEXT instructions *can* do the
4328 // job. It turns out that the VEXTs needed will only use DSrc once, with
4329 // the position based purely on the combination of lane-0 and lane-1
4330 // involved. For example
4331 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4332 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4333 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4334 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4335 //
4336 // Pattern of the MachineInstrs is:
4337 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4338 MachineInstrBuilder NewMIB;
4339 NewMIB = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4340 get(ARM::VEXTd32), DDst);
Tim Northover726d32c2012-09-01 18:07:29 +00004341
4342 // On the first instruction, both DSrc and DDst may be <undef> if present.
4343 // Specifically when the original instruction didn't have them as an
4344 // <imp-use>.
4345 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
4346 bool CurUndef = !MI->readsRegister(CurReg, TRI);
4347 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4348
4349 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
4350 CurUndef = !MI->readsRegister(CurReg, TRI);
4351 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4352
Tim Northoverca9f3842012-08-30 10:17:45 +00004353 NewMIB.addImm(1);
4354 AddDefaultPred(NewMIB);
4355
4356 if (SrcLane == DstLane)
4357 NewMIB.addReg(SrcReg, RegState::Implicit);
4358
4359 MI->setDesc(get(ARM::VEXTd32));
4360 MIB.addReg(DDst, RegState::Define);
Tim Northover726d32c2012-09-01 18:07:29 +00004361
4362 // On the second instruction, DDst has definitely been defined above, so
4363 // it is not <undef>. DSrc, if present, can be <undef> as above.
4364 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
4365 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4366 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4367
4368 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
4369 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4370 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4371
Tim Northoverca9f3842012-08-30 10:17:45 +00004372 MIB.addImm(1);
4373 AddDefaultPred(MIB);
4374
4375 if (SrcLane != DstLane)
4376 MIB.addReg(SrcReg, RegState::Implicit);
4377
4378 // As before, the original destination is no longer represented, add it
4379 // implicitly.
4380 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004381 if (ImplicitSReg != 0)
4382 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004383 break;
4384 }
Tim Northoverf6618152012-08-17 11:32:52 +00004385 }
4386
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004387}
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004388
Bob Wilsone8a549c2012-09-29 21:43:49 +00004389//===----------------------------------------------------------------------===//
4390// Partial register updates
4391//===----------------------------------------------------------------------===//
4392//
4393// Swift renames NEON registers with 64-bit granularity. That means any
4394// instruction writing an S-reg implicitly reads the containing D-reg. The
4395// problem is mostly avoided by translating f32 operations to v2f32 operations
4396// on D-registers, but f32 loads are still a problem.
4397//
4398// These instructions can load an f32 into a NEON register:
4399//
4400// VLDRS - Only writes S, partial D update.
4401// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4402// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4403//
4404// FCONSTD can be used as a dependency-breaking instruction.
Bob Wilsone8a549c2012-09-29 21:43:49 +00004405unsigned ARMBaseInstrInfo::
4406getPartialRegUpdateClearance(const MachineInstr *MI,
4407 unsigned OpNum,
4408 const TargetRegisterInfo *TRI) const {
Silviu Barangadc453362013-03-27 12:38:44 +00004409 if (!SwiftPartialUpdateClearance ||
4410 !(Subtarget.isSwift() || Subtarget.isCortexA15()))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004411 return 0;
4412
4413 assert(TRI && "Need TRI instance");
4414
4415 const MachineOperand &MO = MI->getOperand(OpNum);
4416 if (MO.readsReg())
4417 return 0;
4418 unsigned Reg = MO.getReg();
4419 int UseOp = -1;
4420
4421 switch(MI->getOpcode()) {
4422 // Normal instructions writing only an S-register.
4423 case ARM::VLDRS:
4424 case ARM::FCONSTS:
4425 case ARM::VMOVSR:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004426 case ARM::VMOVv8i8:
4427 case ARM::VMOVv4i16:
4428 case ARM::VMOVv2i32:
4429 case ARM::VMOVv2f32:
4430 case ARM::VMOVv1i64:
4431 UseOp = MI->findRegisterUseOperandIdx(Reg, false, TRI);
4432 break;
4433
4434 // Explicitly reads the dependency.
4435 case ARM::VLD1LNd32:
Silviu Barangadc453362013-03-27 12:38:44 +00004436 UseOp = 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004437 break;
4438 default:
4439 return 0;
4440 }
4441
4442 // If this instruction actually reads a value from Reg, there is no unwanted
4443 // dependency.
4444 if (UseOp != -1 && MI->getOperand(UseOp).readsReg())
4445 return 0;
4446
4447 // We must be able to clobber the whole D-reg.
4448 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4449 // Virtual register must be a foo:ssub_0<def,undef> operand.
4450 if (!MO.getSubReg() || MI->readsVirtualRegister(Reg))
4451 return 0;
4452 } else if (ARM::SPRRegClass.contains(Reg)) {
4453 // Physical register: MI must define the full D-reg.
4454 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4455 &ARM::DPRRegClass);
4456 if (!DReg || !MI->definesRegister(DReg, TRI))
4457 return 0;
4458 }
4459
4460 // MI has an unwanted D-register dependency.
4461 // Avoid defs in the previous N instructrions.
4462 return SwiftPartialUpdateClearance;
4463}
4464
4465// Break a partial register dependency after getPartialRegUpdateClearance
4466// returned non-zero.
4467void ARMBaseInstrInfo::
4468breakPartialRegDependency(MachineBasicBlock::iterator MI,
4469 unsigned OpNum,
4470 const TargetRegisterInfo *TRI) const {
4471 assert(MI && OpNum < MI->getDesc().getNumDefs() && "OpNum is not a def");
4472 assert(TRI && "Need TRI instance");
4473
4474 const MachineOperand &MO = MI->getOperand(OpNum);
4475 unsigned Reg = MO.getReg();
4476 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4477 "Can't break virtual register dependencies.");
4478 unsigned DReg = Reg;
4479
4480 // If MI defines an S-reg, find the corresponding D super-register.
4481 if (ARM::SPRRegClass.contains(Reg)) {
4482 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4483 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4484 }
4485
4486 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
4487 assert(MI->definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
4488
4489 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4490 // the full D-register by loading the same value to both lanes. The
4491 // instruction is micro-coded with 2 uops, so don't do this until we can
Robert Wilhelm516be562013-09-14 09:34:24 +00004492 // properly schedule micro-coded instructions. The dispatcher stalls cause
Bob Wilsone8a549c2012-09-29 21:43:49 +00004493 // too big regressions.
4494
4495 // Insert the dependency-breaking FCONSTD before MI.
4496 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
4497 AddDefaultPred(BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4498 get(ARM::FCONSTD), DReg).addImm(96));
4499 MI->addRegisterKilled(DReg, TRI, true);
4500}
4501
Tom Roeder44cb65f2014-06-05 19:29:43 +00004502void ARMBaseInstrInfo::getUnconditionalBranch(
4503 MCInst &Branch, const MCSymbolRefExpr *BranchTarget) const {
4504 if (Subtarget.isThumb())
4505 Branch.setOpcode(ARM::tB);
4506 else if (Subtarget.isThumb2())
4507 Branch.setOpcode(ARM::t2B);
4508 else
4509 Branch.setOpcode(ARM::Bcc);
4510
4511 Branch.addOperand(MCOperand::CreateExpr(BranchTarget));
4512 Branch.addOperand(MCOperand::CreateImm(ARMCC::AL));
4513 Branch.addOperand(MCOperand::CreateReg(0));
4514}
4515
4516void ARMBaseInstrInfo::getTrap(MCInst &MI) const {
4517 if (Subtarget.isThumb())
4518 MI.setOpcode(ARM::tTRAP);
4519 else if (Subtarget.useNaClTrap())
4520 MI.setOpcode(ARM::TRAPNaCl);
4521 else
4522 MI.setOpcode(ARM::TRAP);
4523}
4524
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004525bool ARMBaseInstrInfo::hasNOP() const {
4526 return (Subtarget.getFeatureBits() & ARM::HasV6T2Ops) != 0;
4527}
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004528
4529bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
Arnold Schwaighofere9375922013-06-05 14:59:36 +00004530 if (MI->getNumOperands() < 4)
4531 return true;
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004532 unsigned ShOpVal = MI->getOperand(3).getImm();
4533 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4534 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4535 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4536 ((ShImm == 1 || ShImm == 2) &&
4537 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
4538 return true;
4539
4540 return false;
4541}
Quentin Colombetd358e842014-08-22 18:05:22 +00004542
4543bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
4544 const MachineInstr &MI, unsigned DefIdx,
4545 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
4546 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4547 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
4548
4549 switch (MI.getOpcode()) {
4550 case ARM::VMOVDRR:
4551 // dX = VMOVDRR rY, rZ
4552 // is the same as:
4553 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
4554 // Populate the InputRegs accordingly.
4555 // rY
4556 const MachineOperand *MOReg = &MI.getOperand(1);
4557 InputRegs.push_back(
4558 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0));
4559 // rZ
4560 MOReg = &MI.getOperand(2);
4561 InputRegs.push_back(
4562 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1));
4563 return true;
4564 }
4565 llvm_unreachable("Target dependent opcode missing");
4566}
4567
4568bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
4569 const MachineInstr &MI, unsigned DefIdx,
4570 RegSubRegPairAndIdx &InputReg) const {
4571 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4572 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
4573
4574 switch (MI.getOpcode()) {
4575 case ARM::VMOVRRD:
4576 // rX, rY = VMOVRRD dZ
4577 // is the same as:
4578 // rX = EXTRACT_SUBREG dZ, ssub_0
4579 // rY = EXTRACT_SUBREG dZ, ssub_1
4580 const MachineOperand &MOReg = MI.getOperand(2);
4581 InputReg.Reg = MOReg.getReg();
4582 InputReg.SubReg = MOReg.getSubReg();
4583 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
4584 return true;
4585 }
4586 llvm_unreachable("Target dependent opcode missing");
4587}
4588
4589bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
4590 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
4591 RegSubRegPairAndIdx &InsertedReg) const {
4592 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4593 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
4594
4595 switch (MI.getOpcode()) {
4596 case ARM::VSETLNi32:
4597 // dX = VSETLNi32 dY, rZ, imm
4598 const MachineOperand &MOBaseReg = MI.getOperand(1);
4599 const MachineOperand &MOInsertedReg = MI.getOperand(2);
4600 const MachineOperand &MOIndex = MI.getOperand(3);
4601 BaseReg.Reg = MOBaseReg.getReg();
4602 BaseReg.SubReg = MOBaseReg.getSubReg();
4603
4604 InsertedReg.Reg = MOInsertedReg.getReg();
4605 InsertedReg.SubReg = MOInsertedReg.getSubReg();
4606 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
4607 return true;
4608 }
4609 llvm_unreachable("Target dependent opcode missing");
4610}