blob: 93dc423b83bdc686494950d5741087bbea7f33ad [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- ARMBaseInstrInfo.cpp - ARM Instruction Information ----------------===//
David Goodwinaf7451b2009-07-08 16:09:28 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Base ARM implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
David Goodwinaf7451b2009-07-08 16:09:28 +000014#include "ARM.h"
Amara Emerson52cfb6a2013-10-03 09:31:51 +000015#include "ARMBaseInstrInfo.h"
Craig Topper5fa0caa2012-03-26 00:45:15 +000016#include "ARMBaseRegisterInfo.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000017#include "ARMConstantPoolValue.h"
Amara Emerson52cfb6a2013-10-03 09:31:51 +000018#include "ARMFeatures.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000019#include "ARMHazardRecognizer.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000020#include "ARMMachineFunctionInfo.h"
Evan Chenga20cde32011-07-20 23:34:39 +000021#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/ADT/STLExtras.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000023#include "llvm/CodeGen/LiveVariables.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000024#include "llvm/CodeGen/MachineConstantPool.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/MachineJumpTableInfo.h"
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +000028#include "llvm/CodeGen/MachineMemOperand.h"
Evan Cheng168ced92010-05-22 01:47:14 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chenga20cde32011-07-20 23:34:39 +000030#include "llvm/CodeGen/SelectionDAGNodes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/Constants.h"
32#include "llvm/IR/Function.h"
33#include "llvm/IR/GlobalValue.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000034#include "llvm/MC/MCAsmInfo.h"
Tom Roeder44cb65f2014-06-05 19:29:43 +000035#include "llvm/MC/MCExpr.h"
Jakub Staszak9b07c0a2011-07-10 02:58:07 +000036#include "llvm/Support/BranchProbability.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000037#include "llvm/Support/CommandLine.h"
Anton Korobeynikov14635da2009-11-02 00:10:38 +000038#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000039#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000040#include "llvm/Support/raw_ostream.h"
Evan Cheng1e210d02011-06-28 20:07:07 +000041
David Goodwinaf7451b2009-07-08 16:09:28 +000042using namespace llvm;
43
Chandler Carruthe96dd892014-04-21 22:55:11 +000044#define DEBUG_TYPE "arm-instrinfo"
45
Chandler Carruthd174b722014-04-22 02:03:14 +000046#define GET_INSTRINFO_CTOR_DTOR
47#include "ARMGenInstrInfo.inc"
48
David Goodwinaf7451b2009-07-08 16:09:28 +000049static cl::opt<bool>
50EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
51 cl::desc("Enable ARM 2-addr to 3-addr conv"));
52
Jakob Stoklund Olesencd893392011-08-31 17:00:02 +000053static cl::opt<bool>
Jakob Stoklund Olesen653183f2011-11-15 23:53:18 +000054WidenVMOVS("widen-vmovs", cl::Hidden, cl::init(true),
Jakob Stoklund Olesencd893392011-08-31 17:00:02 +000055 cl::desc("Widen ARM vmovs to vmovd when possible"));
56
Bob Wilsone8a549c2012-09-29 21:43:49 +000057static cl::opt<unsigned>
58SwiftPartialUpdateClearance("swift-partial-update-clearance",
59 cl::Hidden, cl::init(12),
60 cl::desc("Clearance before partial register updates"));
61
Evan Cheng62c7b5b2010-12-05 22:04:16 +000062/// ARM_MLxEntry - Record information about MLA / MLS instructions.
63struct ARM_MLxEntry {
Craig Topper2fbd1302012-05-24 03:59:11 +000064 uint16_t MLxOpc; // MLA / MLS opcode
65 uint16_t MulOpc; // Expanded multiplication opcode
66 uint16_t AddSubOpc; // Expanded add / sub opcode
Evan Cheng62c7b5b2010-12-05 22:04:16 +000067 bool NegAcc; // True if the acc is negated before the add / sub.
68 bool HasLane; // True if instruction has an extra "lane" operand.
69};
70
71static const ARM_MLxEntry ARM_MLxTable[] = {
72 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
73 // fp scalar ops
74 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
75 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
76 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
77 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
Evan Cheng62c7b5b2010-12-05 22:04:16 +000078 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
79 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
80 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
81 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
82
83 // fp SIMD ops
84 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
85 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
86 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
87 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
88 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
89 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
90 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
91 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
92};
93
Anton Korobeynikov14635da2009-11-02 00:10:38 +000094ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
Evan Cheng703a0fb2011-07-01 17:57:27 +000095 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
Anton Korobeynikov14635da2009-11-02 00:10:38 +000096 Subtarget(STI) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +000097 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
98 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
99 assert(false && "Duplicated entries?");
100 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
101 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
102 }
103}
104
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000105// Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
106// currently defaults to no prepass hazard recognizer.
Eric Christopherf047bfd2014-06-13 22:38:52 +0000107ScheduleHazardRecognizer *
108ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
109 const ScheduleDAG *DAG) const {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000110 if (usePreRAHazardRecognizer()) {
Eric Christopherf047bfd2014-06-13 22:38:52 +0000111 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +0000112 static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000113 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
114 }
Eric Christopherf047bfd2014-06-13 22:38:52 +0000115 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000116}
117
118ScheduleHazardRecognizer *ARMBaseInstrInfo::
119CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
120 const ScheduleDAG *DAG) const {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000121 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
Bill Wendlingf95178e2013-06-07 05:54:19 +0000122 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000123 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
David Goodwinaf7451b2009-07-08 16:09:28 +0000124}
125
126MachineInstr *
127ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
128 MachineBasicBlock::iterator &MBBI,
129 LiveVariables *LV) const {
Evan Cheng0e075e22009-07-27 18:44:00 +0000130 // FIXME: Thumb2 support.
131
David Goodwinaf7451b2009-07-08 16:09:28 +0000132 if (!EnableARM3Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +0000133 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000134
135 MachineInstr *MI = MBBI;
136 MachineFunction &MF = *MI->getParent()->getParent();
Bruno Cardoso Lopesc2f87b72010-06-08 22:51:23 +0000137 uint64_t TSFlags = MI->getDesc().TSFlags;
David Goodwinaf7451b2009-07-08 16:09:28 +0000138 bool isPre = false;
139 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000140 default: return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000141 case ARMII::IndexModePre:
142 isPre = true;
143 break;
144 case ARMII::IndexModePost:
145 break;
146 }
147
148 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
149 // operation.
150 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
151 if (MemOpc == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000152 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000153
Craig Topper062a2ba2014-04-25 05:30:21 +0000154 MachineInstr *UpdateMI = nullptr;
155 MachineInstr *MemMI = nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000156 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000157 const MCInstrDesc &MCID = MI->getDesc();
158 unsigned NumOps = MCID.getNumOperands();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000159 bool isLoad = !MI->mayStore();
David Goodwinaf7451b2009-07-08 16:09:28 +0000160 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
161 const MachineOperand &Base = MI->getOperand(2);
162 const MachineOperand &Offset = MI->getOperand(NumOps-3);
163 unsigned WBReg = WB.getReg();
164 unsigned BaseReg = Base.getReg();
165 unsigned OffReg = Offset.getReg();
166 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
167 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
168 switch (AddrMode) {
Craig Toppere55c5562012-02-07 02:50:20 +0000169 default: llvm_unreachable("Unknown indexed op!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000170 case ARMII::AddrMode2: {
171 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
172 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
173 if (OffReg == 0) {
Evan Chenge3a53c42009-07-08 21:03:57 +0000174 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwinaf7451b2009-07-08 16:09:28 +0000175 // Can't encode it in a so_imm operand. This transformation will
176 // add more than 1 instruction. Abandon!
Craig Topper062a2ba2014-04-25 05:30:21 +0000177 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000178 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000179 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Evan Chenge3a53c42009-07-08 21:03:57 +0000180 .addReg(BaseReg).addImm(Amt)
David Goodwinaf7451b2009-07-08 16:09:28 +0000181 .addImm(Pred).addReg(0).addReg(0);
182 } else if (Amt != 0) {
183 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
184 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
185 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Owen Andersonb595ed02011-07-21 18:54:16 +0000186 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000187 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
188 .addImm(Pred).addReg(0).addReg(0);
189 } else
190 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000191 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000192 .addReg(BaseReg).addReg(OffReg)
193 .addImm(Pred).addReg(0).addReg(0);
194 break;
195 }
196 case ARMII::AddrMode3 : {
197 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
198 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
199 if (OffReg == 0)
200 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
201 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000202 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000203 .addReg(BaseReg).addImm(Amt)
204 .addImm(Pred).addReg(0).addReg(0);
205 else
206 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000207 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000208 .addReg(BaseReg).addReg(OffReg)
209 .addImm(Pred).addReg(0).addReg(0);
210 break;
211 }
212 }
213
214 std::vector<MachineInstr*> NewMIs;
215 if (isPre) {
216 if (isLoad)
217 MemMI = BuildMI(MF, MI->getDebugLoc(),
218 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000219 .addReg(WBReg).addImm(0).addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000220 else
221 MemMI = BuildMI(MF, MI->getDebugLoc(),
222 get(MemOpc)).addReg(MI->getOperand(1).getReg())
223 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
224 NewMIs.push_back(MemMI);
225 NewMIs.push_back(UpdateMI);
226 } else {
227 if (isLoad)
228 MemMI = BuildMI(MF, MI->getDebugLoc(),
229 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000230 .addReg(BaseReg).addImm(0).addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000231 else
232 MemMI = BuildMI(MF, MI->getDebugLoc(),
233 get(MemOpc)).addReg(MI->getOperand(1).getReg())
234 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
235 if (WB.isDead())
236 UpdateMI->getOperand(0).setIsDead();
237 NewMIs.push_back(UpdateMI);
238 NewMIs.push_back(MemMI);
239 }
240
241 // Transfer LiveVariables states, kill / dead info.
242 if (LV) {
243 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
244 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000245 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000246 unsigned Reg = MO.getReg();
247
248 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
249 if (MO.isDef()) {
250 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
251 if (MO.isDead())
252 LV->addVirtualRegisterDead(Reg, NewMI);
253 }
254 if (MO.isUse() && MO.isKill()) {
255 for (unsigned j = 0; j < 2; ++j) {
256 // Look at the two new MI's in reverse order.
257 MachineInstr *NewMI = NewMIs[j];
258 if (!NewMI->readsRegister(Reg))
259 continue;
260 LV->addVirtualRegisterKilled(Reg, NewMI);
261 if (VI.removeKill(MI))
262 VI.Kills.push_back(NewMI);
263 break;
264 }
265 }
266 }
267 }
268 }
269
270 MFI->insert(MBBI, NewMIs[1]);
271 MFI->insert(MBBI, NewMIs[0]);
272 return NewMIs[0];
273}
274
275// Branch analysis.
276bool
277ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
278 MachineBasicBlock *&FBB,
279 SmallVectorImpl<MachineOperand> &Cond,
280 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000281 TBB = nullptr;
282 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000283
David Goodwinaf7451b2009-07-08 16:09:28 +0000284 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000285 if (I == MBB.begin())
Lang Hames24864fe2013-07-19 23:52:47 +0000286 return false; // Empty blocks are easy.
Dale Johannesen4244d122010-04-02 01:38:09 +0000287 --I;
Lang Hames24864fe2013-07-19 23:52:47 +0000288
289 // Walk backwards from the end of the basic block until the branch is
290 // analyzed or we give up.
Lang Hames18c98a52013-12-20 20:27:51 +0000291 while (isPredicated(I) || I->isTerminator() || I->isDebugValue()) {
Lang Hames24864fe2013-07-19 23:52:47 +0000292
293 // Flag to be raised on unanalyzeable instructions. This is useful in cases
294 // where we want to clean up on the end of the basic block before we bail
295 // out.
296 bool CantAnalyze = false;
297
298 // Skip over DEBUG values and predicated nonterminators.
299 while (I->isDebugValue() || !I->isTerminator()) {
300 if (I == MBB.begin())
301 return false;
302 --I;
303 }
304
305 if (isIndirectBranchOpcode(I->getOpcode()) ||
306 isJumpTableBranchOpcode(I->getOpcode())) {
307 // Indirect branches and jump tables can't be analyzed, but we still want
308 // to clean up any instructions at the tail of the basic block.
309 CantAnalyze = true;
310 } else if (isUncondBranchOpcode(I->getOpcode())) {
311 TBB = I->getOperand(0).getMBB();
312 } else if (isCondBranchOpcode(I->getOpcode())) {
313 // Bail out if we encounter multiple conditional branches.
314 if (!Cond.empty())
315 return true;
316
317 assert(!FBB && "FBB should have been null.");
318 FBB = TBB;
319 TBB = I->getOperand(0).getMBB();
320 Cond.push_back(I->getOperand(1));
321 Cond.push_back(I->getOperand(2));
322 } else if (I->isReturn()) {
323 // Returns can't be analyzed, but we should run cleanup.
324 CantAnalyze = !isPredicated(I);
325 } else {
326 // We encountered other unrecognized terminator. Bail out immediately.
327 return true;
328 }
329
330 // Cleanup code - to be run for unpredicated unconditional branches and
331 // returns.
332 if (!isPredicated(I) &&
333 (isUncondBranchOpcode(I->getOpcode()) ||
334 isIndirectBranchOpcode(I->getOpcode()) ||
335 isJumpTableBranchOpcode(I->getOpcode()) ||
336 I->isReturn())) {
337 // Forget any previous condition branch information - it no longer applies.
338 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000339 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000340
341 // If we can modify the function, delete everything below this
342 // unconditional branch.
343 if (AllowModify) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000344 MachineBasicBlock::iterator DI = std::next(I);
Lang Hames24864fe2013-07-19 23:52:47 +0000345 while (DI != MBB.end()) {
346 MachineInstr *InstToDelete = DI;
347 ++DI;
348 InstToDelete->eraseFromParent();
349 }
350 }
351 }
352
353 if (CantAnalyze)
354 return true;
355
Dale Johannesen4244d122010-04-02 01:38:09 +0000356 if (I == MBB.begin())
357 return false;
Lang Hames24864fe2013-07-19 23:52:47 +0000358
Dale Johannesen4244d122010-04-02 01:38:09 +0000359 --I;
360 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000361
Lang Hames24864fe2013-07-19 23:52:47 +0000362 // We made it past the terminators without bailing out - we must have
363 // analyzed this branch successfully.
364 return false;
David Goodwinaf7451b2009-07-08 16:09:28 +0000365}
366
367
368unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000369 MachineBasicBlock::iterator I = MBB.end();
370 if (I == MBB.begin()) return 0;
371 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000372 while (I->isDebugValue()) {
373 if (I == MBB.begin())
374 return 0;
375 --I;
376 }
Evan Cheng056c6692009-07-27 18:20:05 +0000377 if (!isUncondBranchOpcode(I->getOpcode()) &&
378 !isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000379 return 0;
380
381 // Remove the branch.
382 I->eraseFromParent();
383
384 I = MBB.end();
385
386 if (I == MBB.begin()) return 1;
387 --I;
Evan Cheng056c6692009-07-27 18:20:05 +0000388 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000389 return 1;
390
391 // Remove the branch.
392 I->eraseFromParent();
393 return 2;
394}
395
396unsigned
397ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000398 MachineBasicBlock *FBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000399 ArrayRef<MachineOperand> Cond,
Stuart Hastings0125b642010-06-17 22:43:56 +0000400 DebugLoc DL) const {
Evan Cheng780748d2009-07-28 05:48:47 +0000401 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
402 int BOpc = !AFI->isThumbFunction()
403 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
404 int BccOpc = !AFI->isThumbFunction()
405 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000406 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
Andrew Trick3f1fdf12011-09-21 02:17:37 +0000407
David Goodwinaf7451b2009-07-08 16:09:28 +0000408 // Shouldn't be a fall through.
409 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
410 assert((Cond.size() == 2 || Cond.size() == 0) &&
411 "ARM branch conditions have two components!");
412
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000413 // For conditional branches, we use addOperand to preserve CPSR flags.
414
Craig Topper062a2ba2014-04-25 05:30:21 +0000415 if (!FBB) {
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000416 if (Cond.empty()) { // Unconditional branch?
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000417 if (isThumb)
418 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).addImm(ARMCC::AL).addReg(0);
419 else
420 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000421 } else
Stuart Hastings0125b642010-06-17 22:43:56 +0000422 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000423 .addImm(Cond[0].getImm()).addOperand(Cond[1]);
David Goodwinaf7451b2009-07-08 16:09:28 +0000424 return 1;
425 }
426
427 // Two-way conditional branch.
Stuart Hastings0125b642010-06-17 22:43:56 +0000428 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000429 .addImm(Cond[0].getImm()).addOperand(Cond[1]);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000430 if (isThumb)
431 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).addImm(ARMCC::AL).addReg(0);
432 else
433 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwinaf7451b2009-07-08 16:09:28 +0000434 return 2;
435}
436
437bool ARMBaseInstrInfo::
438ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
439 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
440 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
441 return false;
442}
443
Evan Cheng7fae11b2011-12-14 02:11:42 +0000444bool ARMBaseInstrInfo::isPredicated(const MachineInstr *MI) const {
445 if (MI->isBundle()) {
446 MachineBasicBlock::const_instr_iterator I = MI;
447 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
448 while (++I != E && I->isInsideBundle()) {
449 int PIdx = I->findFirstPredOperandIdx();
450 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
451 return true;
452 }
453 return false;
454 }
455
456 int PIdx = MI->findFirstPredOperandIdx();
457 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
458}
459
David Goodwinaf7451b2009-07-08 16:09:28 +0000460bool ARMBaseInstrInfo::
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000461PredicateInstruction(MachineInstr *MI, ArrayRef<MachineOperand> Pred) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000462 unsigned Opc = MI->getOpcode();
Evan Cheng056c6692009-07-27 18:20:05 +0000463 if (isUncondBranchOpcode(Opc)) {
464 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +0000465 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
466 .addImm(Pred[0].getImm())
467 .addReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000468 return true;
469 }
470
471 int PIdx = MI->findFirstPredOperandIdx();
472 if (PIdx != -1) {
473 MachineOperand &PMO = MI->getOperand(PIdx);
474 PMO.setImm(Pred[0].getImm());
475 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
476 return true;
477 }
478 return false;
479}
480
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000481bool ARMBaseInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
482 ArrayRef<MachineOperand> Pred2) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000483 if (Pred1.size() > 2 || Pred2.size() > 2)
484 return false;
485
486 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
487 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
488 if (CC1 == CC2)
489 return true;
490
491 switch (CC1) {
492 default:
493 return false;
494 case ARMCC::AL:
495 return true;
496 case ARMCC::HS:
497 return CC2 == ARMCC::HI;
498 case ARMCC::LS:
499 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
500 case ARMCC::GE:
501 return CC2 == ARMCC::GT;
502 case ARMCC::LE:
503 return CC2 == ARMCC::LT;
504 }
505}
506
507bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
508 std::vector<MachineOperand> &Pred) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000509 bool Found = false;
510 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
511 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +0000512 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
513 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000514 Pred.push_back(MO);
515 Found = true;
516 }
517 }
518
519 return Found;
520}
521
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000522static bool isCPSRDefined(const MachineInstr *MI) {
523 for (const auto &MO : MI->operands())
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000524 if (MO.isReg() && MO.getReg() == ARM::CPSR && MO.isDef())
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000525 return true;
526 return false;
527}
528
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000529static bool isEligibleForITBlock(const MachineInstr *MI) {
530 switch (MI->getOpcode()) {
531 default: return true;
532 case ARM::tADC: // ADC (register) T1
533 case ARM::tADDi3: // ADD (immediate) T1
534 case ARM::tADDi8: // ADD (immediate) T2
535 case ARM::tADDrr: // ADD (register) T1
536 case ARM::tAND: // AND (register) T1
537 case ARM::tASRri: // ASR (immediate) T1
538 case ARM::tASRrr: // ASR (register) T1
539 case ARM::tBIC: // BIC (register) T1
540 case ARM::tEOR: // EOR (register) T1
541 case ARM::tLSLri: // LSL (immediate) T1
542 case ARM::tLSLrr: // LSL (register) T1
543 case ARM::tLSRri: // LSR (immediate) T1
544 case ARM::tLSRrr: // LSR (register) T1
545 case ARM::tMUL: // MUL T1
546 case ARM::tMVN: // MVN (register) T1
547 case ARM::tORR: // ORR (register) T1
548 case ARM::tROR: // ROR (register) T1
549 case ARM::tRSB: // RSB (immediate) T1
550 case ARM::tSBC: // SBC (register) T1
551 case ARM::tSUBi3: // SUB (immediate) T1
552 case ARM::tSUBi8: // SUB (immediate) T2
553 case ARM::tSUBrr: // SUB (register) T1
554 return !isCPSRDefined(MI);
555 }
556}
557
Evan Chenga33fc862009-11-21 06:21:52 +0000558/// isPredicable - Return true if the specified instruction can be predicated.
559/// By default, this returns true for every instruction with a
560/// PredicateOperand.
561bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000562 if (!MI->isPredicable())
Evan Chenga33fc862009-11-21 06:21:52 +0000563 return false;
564
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000565 if (!isEligibleForITBlock(MI))
566 return false;
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000567
Joey Goulya5153cb2013-09-09 14:21:49 +0000568 ARMFunctionInfo *AFI =
569 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
570
571 if (AFI->isThumb2Function()) {
Weiming Zhao0da5cc02013-11-13 18:29:49 +0000572 if (getSubtarget().restrictIT())
Joey Goulya5153cb2013-09-09 14:21:49 +0000573 return isV8EligibleForIT(MI);
574 } else { // non-Thumb
575 if ((MI->getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
576 return false;
Evan Chenga33fc862009-11-21 06:21:52 +0000577 }
Joey Goulya5153cb2013-09-09 14:21:49 +0000578
Evan Chenga33fc862009-11-21 06:21:52 +0000579 return true;
580}
David Goodwinaf7451b2009-07-08 16:09:28 +0000581
Benjamin Kramer44a53da2014-04-12 18:45:24 +0000582namespace llvm {
583template <> bool IsCPSRDead<MachineInstr>(MachineInstr *MI) {
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000584 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
585 const MachineOperand &MO = MI->getOperand(i);
586 if (!MO.isReg() || MO.isUndef() || MO.isUse())
587 continue;
588 if (MO.getReg() != ARM::CPSR)
589 continue;
590 if (!MO.isDead())
591 return false;
592 }
593 // all definitions of CPSR are dead
594 return true;
595}
Benjamin Kramer44a53da2014-04-12 18:45:24 +0000596}
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000597
David Goodwinaf7451b2009-07-08 16:09:28 +0000598/// GetInstSize - Return the size of the specified MachineInstr.
599///
600unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
601 const MachineBasicBlock &MBB = *MI->getParent();
602 const MachineFunction *MF = MBB.getParent();
Chris Lattnere9a75a62009-08-22 21:43:10 +0000603 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwinaf7451b2009-07-08 16:09:28 +0000604
Evan Cheng6cc775f2011-06-28 19:10:37 +0000605 const MCInstrDesc &MCID = MI->getDesc();
Owen Anderson651b2302011-07-13 23:22:26 +0000606 if (MCID.getSize())
607 return MCID.getSize();
David Goodwinaf7451b2009-07-08 16:09:28 +0000608
David Blaikie46a9f012012-01-20 21:51:11 +0000609 // If this machine instr is an inline asm, measure it.
610 if (MI->getOpcode() == ARM::INLINEASM)
611 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
David Blaikie46a9f012012-01-20 21:51:11 +0000612 unsigned Opc = MI->getOpcode();
613 switch (Opc) {
Rafael Espindolaafeb01c2014-03-07 04:45:03 +0000614 default:
615 // pseudo-instruction sizes are zero.
David Blaikie46a9f012012-01-20 21:51:11 +0000616 return 0;
617 case TargetOpcode::BUNDLE:
618 return getInstBundleLength(MI);
619 case ARM::MOVi16_ga_pcrel:
620 case ARM::MOVTi16_ga_pcrel:
621 case ARM::t2MOVi16_ga_pcrel:
622 case ARM::t2MOVTi16_ga_pcrel:
623 return 4;
624 case ARM::MOVi32imm:
625 case ARM::t2MOVi32imm:
626 return 8;
627 case ARM::CONSTPOOL_ENTRY:
Tim Northovera603c402015-05-31 19:22:07 +0000628 case ARM::JUMPTABLE_INSTS:
629 case ARM::JUMPTABLE_ADDRS:
630 case ARM::JUMPTABLE_TBB:
631 case ARM::JUMPTABLE_TBH:
David Blaikie46a9f012012-01-20 21:51:11 +0000632 // If this machine instr is a constant pool entry, its size is recorded as
633 // operand #2.
634 return MI->getOperand(2).getImm();
635 case ARM::Int_eh_sjlj_longjmp:
636 return 16;
637 case ARM::tInt_eh_sjlj_longjmp:
638 return 10;
639 case ARM::Int_eh_sjlj_setjmp:
640 case ARM::Int_eh_sjlj_setjmp_nofp:
641 return 20;
642 case ARM::tInt_eh_sjlj_setjmp:
643 case ARM::t2Int_eh_sjlj_setjmp:
644 case ARM::t2Int_eh_sjlj_setjmp_nofp:
645 return 12;
Tim Northover650b0ee52014-11-13 17:58:48 +0000646 case ARM::SPACE:
647 return MI->getOperand(1).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000648 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000649}
650
Evan Cheng7fae11b2011-12-14 02:11:42 +0000651unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr *MI) const {
652 unsigned Size = 0;
653 MachineBasicBlock::const_instr_iterator I = MI;
654 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
655 while (++I != E && I->isInsideBundle()) {
656 assert(!I->isBundle() && "No nested bundle!");
657 Size += GetInstSizeInBytes(&*I);
658 }
659 return Size;
660}
661
Tim Northover5d72c5d2014-10-01 19:21:03 +0000662void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB,
663 MachineBasicBlock::iterator I,
664 unsigned DestReg, bool KillSrc,
665 const ARMSubtarget &Subtarget) const {
666 unsigned Opc = Subtarget.isThumb()
667 ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
668 : ARM::MRS;
669
670 MachineInstrBuilder MIB =
671 BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
672
673 // There is only 1 A/R class MRS instruction, and it always refers to
674 // APSR. However, there are lots of other possibilities on M-class cores.
675 if (Subtarget.isMClass())
676 MIB.addImm(0x800);
677
678 AddDefaultPred(MIB);
679
680 MIB.addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
681}
682
683void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
684 MachineBasicBlock::iterator I,
685 unsigned SrcReg, bool KillSrc,
686 const ARMSubtarget &Subtarget) const {
687 unsigned Opc = Subtarget.isThumb()
688 ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
689 : ARM::MSR;
690
691 MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
692
693 if (Subtarget.isMClass())
694 MIB.addImm(0x800);
695 else
696 MIB.addImm(8);
697
698 MIB.addReg(SrcReg, getKillRegState(KillSrc));
699
700 AddDefaultPred(MIB);
701
702 MIB.addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
703}
704
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000705void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
706 MachineBasicBlock::iterator I, DebugLoc DL,
707 unsigned DestReg, unsigned SrcReg,
708 bool KillSrc) const {
709 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000710 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson70aa8d02010-02-16 17:24:15 +0000711
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000712 if (GPRDest && GPRSrc) {
713 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
Jim Grosbach8815bef2013-10-22 02:29:35 +0000714 .addReg(SrcReg, getKillRegState(KillSrc))));
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000715 return;
David Goodwine5b5d8f2009-08-05 21:02:22 +0000716 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000717
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000718 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000719 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000720
Chad Rosierbe762512011-08-20 00:17:25 +0000721 unsigned Opc = 0;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000722 if (SPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000723 Opc = ARM::VMOVS;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000724 else if (GPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000725 Opc = ARM::VMOVRS;
726 else if (SPRDest && GPRSrc)
727 Opc = ARM::VMOVSR;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000728 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP())
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000729 Opc = ARM::VMOVD;
730 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
Owen Anderson454e1c72011-07-15 18:46:47 +0000731 Opc = ARM::VORRq;
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000732
Chad Rosierbe762512011-08-20 00:17:25 +0000733 if (Opc) {
734 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
Owen Anderson454e1c72011-07-15 18:46:47 +0000735 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosierbe762512011-08-20 00:17:25 +0000736 if (Opc == ARM::VORRq)
737 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosier61f92ef2011-08-20 00:52:40 +0000738 AddDefaultPred(MIB);
Chad Rosierbe762512011-08-20 00:17:25 +0000739 return;
740 }
741
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000742 // Handle register classes that require multiple instructions.
743 unsigned BeginIdx = 0;
744 unsigned SubRegs = 0;
Andrew Trickb57e2252012-08-29 04:41:37 +0000745 int Spacing = 1;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000746
747 // Use VORRq when possible.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000748 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
749 Opc = ARM::VORRq;
750 BeginIdx = ARM::qsub_0;
751 SubRegs = 2;
752 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
753 Opc = ARM::VORRq;
754 BeginIdx = ARM::qsub_0;
755 SubRegs = 4;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000756 // Fall back to VMOVD.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000757 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
758 Opc = ARM::VMOVD;
759 BeginIdx = ARM::dsub_0;
760 SubRegs = 2;
761 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
762 Opc = ARM::VMOVD;
763 BeginIdx = ARM::dsub_0;
764 SubRegs = 3;
765 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
766 Opc = ARM::VMOVD;
767 BeginIdx = ARM::dsub_0;
768 SubRegs = 4;
769 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
Jim Grosbachdba14dd2013-10-22 02:29:37 +0000770 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000771 BeginIdx = ARM::gsub_0;
772 SubRegs = 2;
773 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
774 Opc = ARM::VMOVD;
775 BeginIdx = ARM::dsub_0;
776 SubRegs = 2;
777 Spacing = 2;
778 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
779 Opc = ARM::VMOVD;
780 BeginIdx = ARM::dsub_0;
781 SubRegs = 3;
782 Spacing = 2;
783 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
784 Opc = ARM::VMOVD;
785 BeginIdx = ARM::dsub_0;
786 SubRegs = 4;
787 Spacing = 2;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000788 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) {
789 Opc = ARM::VMOVS;
790 BeginIdx = ARM::ssub_0;
791 SubRegs = 2;
Tim Northover5d72c5d2014-10-01 19:21:03 +0000792 } else if (SrcReg == ARM::CPSR) {
793 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
794 return;
795 } else if (DestReg == ARM::CPSR) {
796 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
797 return;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000798 }
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000799
Andrew Trickb57e2252012-08-29 04:41:37 +0000800 assert(Opc && "Impossible reg-to-reg copy");
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000801
Andrew Trick4cc69492012-08-29 01:58:52 +0000802 const TargetRegisterInfo *TRI = &getRegisterInfo();
803 MachineInstrBuilder Mov;
Andrew Trickbd0073d2012-08-29 01:58:55 +0000804
805 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
806 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000807 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000808 Spacing = -Spacing;
809 }
810#ifndef NDEBUG
811 SmallSet<unsigned, 4> DstRegs;
812#endif
Andrew Trick4cc69492012-08-29 01:58:52 +0000813 for (unsigned i = 0; i != SubRegs; ++i) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000814 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
815 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
Andrew Trick4cc69492012-08-29 01:58:52 +0000816 assert(Dst && Src && "Bad sub-register");
Andrew Trickbd0073d2012-08-29 01:58:55 +0000817#ifndef NDEBUG
Andrew Trickbd0073d2012-08-29 01:58:55 +0000818 assert(!DstRegs.count(Src) && "destructive vector copy");
Andrew Trickb57e2252012-08-29 04:41:37 +0000819 DstRegs.insert(Dst);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000820#endif
Jim Grosbach8815bef2013-10-22 02:29:35 +0000821 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
Andrew Trick4cc69492012-08-29 01:58:52 +0000822 // VORR takes two source operands.
823 if (Opc == ARM::VORRq)
824 Mov.addReg(Src);
825 Mov = AddDefaultPred(Mov);
JF Bastien583db652013-07-12 23:33:03 +0000826 // MOVr can set CC.
827 if (Opc == ARM::MOVr)
828 Mov = AddDefaultCC(Mov);
Andrew Trick4cc69492012-08-29 01:58:52 +0000829 }
830 // Add implicit super-register defs and kills to the last instruction.
831 Mov->addRegisterDefined(DestReg, TRI);
832 if (KillSrc)
833 Mov->addRegisterKilled(SrcReg, TRI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000834}
835
Tim Northover798697d2013-04-21 11:57:07 +0000836const MachineInstrBuilder &
837ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
838 unsigned SubIdx, unsigned State,
839 const TargetRegisterInfo *TRI) const {
Evan Chengddc93c72010-05-07 00:24:52 +0000840 if (!SubIdx)
841 return MIB.addReg(Reg, State);
842
843 if (TargetRegisterInfo::isPhysicalRegister(Reg))
844 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
845 return MIB.addReg(Reg, State, SubIdx);
846}
847
David Goodwinaf7451b2009-07-08 16:09:28 +0000848void ARMBaseInstrInfo::
849storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
850 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000851 const TargetRegisterClass *RC,
852 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000853 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +0000854 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000855 MachineFunction &MF = *MBB.getParent();
856 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000857 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000858
859 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000860 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000861 MachineMemOperand::MOStore,
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000862 MFI.getObjectSize(FI),
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000863 Align);
David Goodwinaf7451b2009-07-08 16:09:28 +0000864
Owen Anderson732f82c2011-08-10 17:21:20 +0000865 switch (RC->getSize()) {
866 case 4:
867 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
868 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
David Goodwinaf7451b2009-07-08 16:09:28 +0000869 .addReg(SrcReg, getKillRegState(isKill))
Jim Grosbach338de3e2010-10-27 23:12:14 +0000870 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000871 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
872 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
Evan Cheng9d768f42010-05-06 01:34:11 +0000873 .addReg(SrcReg, getKillRegState(isKill))
874 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000875 } else
876 llvm_unreachable("Unknown reg class!");
877 break;
878 case 8:
879 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
880 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwinaf7451b2009-07-08 16:09:28 +0000881 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000882 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +0000883 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +0000884 if (Subtarget.hasV5TEOps()) {
885 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD));
886 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
887 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
888 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
889
890 AddDefaultPred(MIB);
891 } else {
892 // Fallback to STM instruction, which has existed since the dawn of
893 // time.
894 MachineInstrBuilder MIB =
895 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STMIA))
896 .addFrameIndex(FI).addMemOperand(MMO));
897 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
898 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
899 }
Owen Anderson732f82c2011-08-10 17:21:20 +0000900 } else
901 llvm_unreachable("Unknown reg class!");
902 break;
903 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +0000904 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +0000905 // Use aligned spills if the stack can be realigned.
906 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000907 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64))
Bob Wilson4c1ca292010-07-06 21:26:18 +0000908 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000909 .addReg(SrcReg, getKillRegState(isKill))
910 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000911 } else {
912 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000913 .addReg(SrcReg, getKillRegState(isKill))
914 .addFrameIndex(FI)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000915 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000916 }
917 } else
918 llvm_unreachable("Unknown reg class!");
919 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000920 case 24:
921 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
922 // Use aligned spills if the stack can be realigned.
923 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
924 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
925 .addFrameIndex(FI).addImm(16)
926 .addReg(SrcReg, getKillRegState(isKill))
927 .addMemOperand(MMO));
928 } else {
929 MachineInstrBuilder MIB =
930 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
931 .addFrameIndex(FI))
932 .addMemOperand(MMO);
933 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
934 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
935 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
936 }
937 } else
938 llvm_unreachable("Unknown reg class!");
939 break;
Owen Anderson732f82c2011-08-10 17:21:20 +0000940 case 32:
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000941 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +0000942 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
943 // FIXME: It's possible to only store part of the QQ register if the
944 // spilled def has a sub-register index.
945 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +0000946 .addFrameIndex(FI).addImm(16)
947 .addReg(SrcReg, getKillRegState(isKill))
948 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000949 } else {
950 MachineInstrBuilder MIB =
951 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000952 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +0000953 .addMemOperand(MMO);
954 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
955 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
956 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
957 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
958 }
959 } else
960 llvm_unreachable("Unknown reg class!");
961 break;
962 case 64:
963 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
964 MachineInstrBuilder MIB =
965 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
966 .addFrameIndex(FI))
967 .addMemOperand(MMO);
968 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
969 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
970 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
971 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
972 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
973 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
974 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
975 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
976 } else
977 llvm_unreachable("Unknown reg class!");
978 break;
979 default:
980 llvm_unreachable("Unknown reg class!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000981 }
982}
983
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000984unsigned
985ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
986 int &FrameIndex) const {
987 switch (MI->getOpcode()) {
988 default: break;
Jim Grosbach338de3e2010-10-27 23:12:14 +0000989 case ARM::STRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000990 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
991 if (MI->getOperand(1).isFI() &&
992 MI->getOperand(2).isReg() &&
993 MI->getOperand(3).isImm() &&
994 MI->getOperand(2).getReg() == 0 &&
995 MI->getOperand(3).getImm() == 0) {
996 FrameIndex = MI->getOperand(1).getIndex();
997 return MI->getOperand(0).getReg();
998 }
999 break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001000 case ARM::STRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001001 case ARM::t2STRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001002 case ARM::tSTRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001003 case ARM::VSTRD:
1004 case ARM::VSTRS:
1005 if (MI->getOperand(1).isFI() &&
1006 MI->getOperand(2).isImm() &&
1007 MI->getOperand(2).getImm() == 0) {
1008 FrameIndex = MI->getOperand(1).getIndex();
1009 return MI->getOperand(0).getReg();
1010 }
1011 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001012 case ARM::VST1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001013 case ARM::VST1d64TPseudo:
1014 case ARM::VST1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001015 if (MI->getOperand(0).isFI() &&
1016 MI->getOperand(2).getSubReg() == 0) {
1017 FrameIndex = MI->getOperand(0).getIndex();
1018 return MI->getOperand(2).getReg();
1019 }
Jakob Stoklund Olesenb929c712010-09-15 21:40:09 +00001020 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001021 case ARM::VSTMQIA:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001022 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001023 MI->getOperand(0).getSubReg() == 0) {
1024 FrameIndex = MI->getOperand(1).getIndex();
1025 return MI->getOperand(0).getReg();
1026 }
1027 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001028 }
1029
1030 return 0;
1031}
1032
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001033unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
1034 int &FrameIndex) const {
1035 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001036 return MI->mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001037}
1038
David Goodwinaf7451b2009-07-08 16:09:28 +00001039void ARMBaseInstrInfo::
1040loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1041 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +00001042 const TargetRegisterClass *RC,
1043 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +00001044 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +00001045 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001046 MachineFunction &MF = *MBB.getParent();
1047 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001048 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001049 MachineMemOperand *MMO =
Chris Lattnere3d864b2010-09-21 04:39:43 +00001050 MF.getMachineMemOperand(
Jay Foad465101b2011-11-15 07:34:52 +00001051 MachinePointerInfo::getFixedStack(FI),
Chris Lattnere3d864b2010-09-21 04:39:43 +00001052 MachineMemOperand::MOLoad,
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001053 MFI.getObjectSize(FI),
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001054 Align);
David Goodwinaf7451b2009-07-08 16:09:28 +00001055
Owen Anderson732f82c2011-08-10 17:21:20 +00001056 switch (RC->getSize()) {
1057 case 4:
1058 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
1059 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1060 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilson37f106e2010-02-16 22:01:59 +00001061
Owen Anderson732f82c2011-08-10 17:21:20 +00001062 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
1063 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001064 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001065 } else
1066 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001067 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001068 case 8:
1069 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
1070 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Evan Cheng9d768f42010-05-06 01:34:11 +00001071 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001072 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001073 MachineInstrBuilder MIB;
1074
1075 if (Subtarget.hasV5TEOps()) {
1076 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1077 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1078 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1079 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
1080
1081 AddDefaultPred(MIB);
1082 } else {
1083 // Fallback to LDM instruction, which has existed since the dawn of
1084 // time.
1085 MIB = AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDMIA))
1086 .addFrameIndex(FI).addMemOperand(MMO));
1087 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1088 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1089 }
1090
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001091 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1092 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001093 } else
1094 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001095 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001096 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001097 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001098 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001099 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
Bob Wilson4c1ca292010-07-06 21:26:18 +00001100 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +00001101 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001102 } else {
1103 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1104 .addFrameIndex(FI)
1105 .addMemOperand(MMO));
1106 }
1107 } else
1108 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001109 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001110 case 24:
1111 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1112 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1113 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1114 .addFrameIndex(FI).addImm(16)
1115 .addMemOperand(MMO));
1116 } else {
1117 MachineInstrBuilder MIB =
1118 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1119 .addFrameIndex(FI)
1120 .addMemOperand(MMO));
1121 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1122 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1123 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1124 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1125 MIB.addReg(DestReg, RegState::ImplicitDefine);
1126 }
1127 } else
1128 llvm_unreachable("Unknown reg class!");
1129 break;
1130 case 32:
1131 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001132 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1133 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +00001134 .addFrameIndex(FI).addImm(16)
1135 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001136 } else {
1137 MachineInstrBuilder MIB =
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001138 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1139 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +00001140 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001141 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1142 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1143 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1144 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001145 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1146 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001147 }
1148 } else
1149 llvm_unreachable("Unknown reg class!");
1150 break;
1151 case 64:
1152 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1153 MachineInstrBuilder MIB =
1154 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1155 .addFrameIndex(FI))
1156 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001157 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1158 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1159 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1160 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1161 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1162 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1163 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1164 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001165 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1166 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001167 } else
1168 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001169 break;
Bob Wilsona92e41a2010-06-18 21:32:42 +00001170 default:
1171 llvm_unreachable("Unknown regclass!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001172 }
1173}
1174
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001175unsigned
1176ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
1177 int &FrameIndex) const {
1178 switch (MI->getOpcode()) {
1179 default: break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001180 case ARM::LDRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001181 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
1182 if (MI->getOperand(1).isFI() &&
1183 MI->getOperand(2).isReg() &&
1184 MI->getOperand(3).isImm() &&
1185 MI->getOperand(2).getReg() == 0 &&
1186 MI->getOperand(3).getImm() == 0) {
1187 FrameIndex = MI->getOperand(1).getIndex();
1188 return MI->getOperand(0).getReg();
1189 }
1190 break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001191 case ARM::LDRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001192 case ARM::t2LDRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001193 case ARM::tLDRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001194 case ARM::VLDRD:
1195 case ARM::VLDRS:
1196 if (MI->getOperand(1).isFI() &&
1197 MI->getOperand(2).isImm() &&
1198 MI->getOperand(2).getImm() == 0) {
1199 FrameIndex = MI->getOperand(1).getIndex();
1200 return MI->getOperand(0).getReg();
1201 }
1202 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001203 case ARM::VLD1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001204 case ARM::VLD1d64TPseudo:
1205 case ARM::VLD1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001206 if (MI->getOperand(1).isFI() &&
1207 MI->getOperand(0).getSubReg() == 0) {
1208 FrameIndex = MI->getOperand(1).getIndex();
1209 return MI->getOperand(0).getReg();
1210 }
1211 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001212 case ARM::VLDMQIA:
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001213 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001214 MI->getOperand(0).getSubReg() == 0) {
1215 FrameIndex = MI->getOperand(1).getIndex();
1216 return MI->getOperand(0).getReg();
1217 }
1218 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001219 }
1220
1221 return 0;
1222}
1223
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001224unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
1225 int &FrameIndex) const {
1226 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001227 return MI->mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001228}
1229
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001230bool
1231ARMBaseInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
1232 MachineFunction &MF = *MI->getParent()->getParent();
1233 Reloc::Model RM = MF.getTarget().getRelocationModel();
1234
1235 if (MI->getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
1236 assert(getSubtarget().getTargetTriple().getObjectFormat() ==
1237 Triple::MachO &&
1238 "LOAD_STACK_GUARD currently supported only for MachO.");
1239 expandLoadStackGuard(MI, RM);
1240 MI->getParent()->erase(MI);
1241 return true;
1242 }
1243
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001244 // This hook gets to expand COPY instructions before they become
1245 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1246 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1247 // changed into a VORR that can go down the NEON pipeline.
Oliver Stannard51b1d462014-08-21 12:50:31 +00001248 if (!WidenVMOVS || !MI->isCopy() || Subtarget.isCortexA15() ||
1249 Subtarget.isFPOnlySP())
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001250 return false;
1251
1252 // Look for a copy between even S-registers. That is where we keep floats
1253 // when using NEON v2f32 instructions for f32 arithmetic.
1254 unsigned DstRegS = MI->getOperand(0).getReg();
1255 unsigned SrcRegS = MI->getOperand(1).getReg();
1256 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1257 return false;
1258
1259 const TargetRegisterInfo *TRI = &getRegisterInfo();
1260 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1261 &ARM::DPRRegClass);
1262 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1263 &ARM::DPRRegClass);
1264 if (!DstRegD || !SrcRegD)
1265 return false;
1266
1267 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1268 // legal if the COPY already defines the full DstRegD, and it isn't a
1269 // sub-register insertion.
1270 if (!MI->definesRegister(DstRegD, TRI) || MI->readsRegister(DstRegD, TRI))
1271 return false;
1272
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001273 // A dead copy shouldn't show up here, but reject it just in case.
1274 if (MI->getOperand(0).isDead())
1275 return false;
1276
1277 // All clear, widen the COPY.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001278 DEBUG(dbgs() << "widening: " << *MI);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001279 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001280
1281 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg
1282 // or some other super-register.
1283 int ImpDefIdx = MI->findRegisterDefOperandIdx(DstRegD);
1284 if (ImpDefIdx != -1)
1285 MI->RemoveOperand(ImpDefIdx);
1286
1287 // Change the opcode and operands.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001288 MI->setDesc(get(ARM::VMOVD));
1289 MI->getOperand(0).setReg(DstRegD);
1290 MI->getOperand(1).setReg(SrcRegD);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001291 AddDefaultPred(MIB);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001292
1293 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1294 // register scavenger and machine verifier, so we need to indicate that we
1295 // are reading an undefined value from SrcRegD, but a proper value from
1296 // SrcRegS.
1297 MI->getOperand(1).setIsUndef();
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001298 MIB.addReg(SrcRegS, RegState::Implicit);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001299
1300 // SrcRegD may actually contain an unrelated value in the ssub_1
1301 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
1302 if (MI->getOperand(1).isKill()) {
1303 MI->getOperand(1).setIsKill(false);
1304 MI->addRegisterKilled(SrcRegS, TRI, true);
1305 }
1306
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001307 DEBUG(dbgs() << "replaced by: " << *MI);
1308 return true;
1309}
1310
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001311/// Create a copy of a const pool value. Update CPI to the new index and return
1312/// the label UID.
1313static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1314 MachineConstantPool *MCP = MF.getConstantPool();
1315 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1316
1317 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1318 assert(MCPE.isMachineConstantPoolEntry() &&
1319 "Expecting a machine constantpool entry!");
1320 ARMConstantPoolValue *ACPV =
1321 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1322
Evan Chengdfce83c2011-01-17 08:03:18 +00001323 unsigned PCLabelId = AFI->createPICLabelUId();
Craig Topper062a2ba2014-04-25 05:30:21 +00001324 ARMConstantPoolValue *NewCPV = nullptr;
Oliver Stannard8f859942014-01-29 16:01:24 +00001325
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001326 // FIXME: The below assumes PIC relocation model and that the function
1327 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1328 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1329 // instructions, so that's probably OK, but is PIC always correct when
1330 // we get here?
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001331 if (ACPV->isGlobalValue())
Bill Wendling7753d662011-10-01 08:00:54 +00001332 NewCPV = ARMConstantPoolConstant::
1333 Create(cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId,
1334 ARMCP::CPValue, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001335 else if (ACPV->isExtSymbol())
Bill Wendlingc214cb02011-10-01 08:58:29 +00001336 NewCPV = ARMConstantPoolSymbol::
1337 Create(MF.getFunction()->getContext(),
1338 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001339 else if (ACPV->isBlockAddress())
Bill Wendling7753d662011-10-01 08:00:54 +00001340 NewCPV = ARMConstantPoolConstant::
1341 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1342 ARMCP::CPBlockAddress, 4);
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001343 else if (ACPV->isLSDA())
Bill Wendling7753d662011-10-01 08:00:54 +00001344 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId,
1345 ARMCP::CPLSDA, 4);
Bill Wendling69bc3de2011-09-29 23:50:42 +00001346 else if (ACPV->isMachineBasicBlock())
Bill Wendling4a4772f2011-10-01 09:30:42 +00001347 NewCPV = ARMConstantPoolMBB::
1348 Create(MF.getFunction()->getContext(),
1349 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001350 else
1351 llvm_unreachable("Unexpected ARM constantpool value type!!");
1352 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1353 return PCLabelId;
1354}
1355
Evan Chengfe864422009-11-08 00:15:23 +00001356void ARMBaseInstrInfo::
1357reMaterialize(MachineBasicBlock &MBB,
1358 MachineBasicBlock::iterator I,
1359 unsigned DestReg, unsigned SubIdx,
Evan Cheng6ad7da92009-11-14 02:55:43 +00001360 const MachineInstr *Orig,
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001361 const TargetRegisterInfo &TRI) const {
Evan Chengfe864422009-11-08 00:15:23 +00001362 unsigned Opcode = Orig->getOpcode();
1363 switch (Opcode) {
1364 default: {
1365 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001366 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfe864422009-11-08 00:15:23 +00001367 MBB.insert(I, MI);
1368 break;
1369 }
1370 case ARM::tLDRpci_pic:
1371 case ARM::t2LDRpci_pic: {
1372 MachineFunction &MF = *MBB.getParent();
Evan Chengfe864422009-11-08 00:15:23 +00001373 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001374 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfe864422009-11-08 00:15:23 +00001375 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1376 DestReg)
1377 .addConstantPoolIndex(CPI).addImm(PCLabelId);
Chris Lattner1d0c2572011-04-29 05:24:29 +00001378 MIB->setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
Evan Chengfe864422009-11-08 00:15:23 +00001379 break;
1380 }
1381 }
Evan Chengfe864422009-11-08 00:15:23 +00001382}
1383
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001384MachineInstr *
1385ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001386 MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001387 switch(Orig->getOpcode()) {
1388 case ARM::tLDRpci_pic:
1389 case ARM::t2LDRpci_pic: {
1390 unsigned CPI = Orig->getOperand(1).getIndex();
1391 unsigned PCLabelId = duplicateCPV(MF, CPI);
1392 Orig->getOperand(1).setIndex(CPI);
1393 Orig->getOperand(2).setImm(PCLabelId);
1394 break;
1395 }
1396 }
1397 return MI;
1398}
1399
Evan Chenge9c46c22010-03-03 01:44:33 +00001400bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
Evan Chengb8b0ad82011-01-20 08:34:58 +00001401 const MachineInstr *MI1,
1402 const MachineRegisterInfo *MRI) const {
Matthias Braunfa3872e2015-05-18 20:27:55 +00001403 unsigned Opcode = MI0->getOpcode();
Evan Cheng028ccbfc2011-01-20 23:55:07 +00001404 if (Opcode == ARM::t2LDRpci ||
Evan Chengbbd50b02009-11-20 02:10:27 +00001405 Opcode == ARM::t2LDRpci_pic ||
1406 Opcode == ARM::tLDRpci ||
Evan Chengb8b0ad82011-01-20 08:34:58 +00001407 Opcode == ARM::tLDRpci_pic ||
Tim Northover72360d22013-12-02 10:35:41 +00001408 Opcode == ARM::LDRLIT_ga_pcrel ||
1409 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1410 Opcode == ARM::tLDRLIT_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001411 Opcode == ARM::MOV_ga_pcrel ||
1412 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001413 Opcode == ARM::t2MOV_ga_pcrel) {
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001414 if (MI1->getOpcode() != Opcode)
1415 return false;
1416 if (MI0->getNumOperands() != MI1->getNumOperands())
1417 return false;
1418
1419 const MachineOperand &MO0 = MI0->getOperand(1);
1420 const MachineOperand &MO1 = MI1->getOperand(1);
1421 if (MO0.getOffset() != MO1.getOffset())
1422 return false;
1423
Tim Northover72360d22013-12-02 10:35:41 +00001424 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1425 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1426 Opcode == ARM::tLDRLIT_ga_pcrel ||
1427 Opcode == ARM::MOV_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001428 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001429 Opcode == ARM::t2MOV_ga_pcrel)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001430 // Ignore the PC labels.
1431 return MO0.getGlobal() == MO1.getGlobal();
1432
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001433 const MachineFunction *MF = MI0->getParent()->getParent();
1434 const MachineConstantPool *MCP = MF->getConstantPool();
1435 int CPI0 = MO0.getIndex();
1436 int CPI1 = MO1.getIndex();
1437 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1438 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
Evan Chengf098bf12011-03-24 06:20:03 +00001439 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1440 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1441 if (isARMCP0 && isARMCP1) {
1442 ARMConstantPoolValue *ACPV0 =
1443 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1444 ARMConstantPoolValue *ACPV1 =
1445 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1446 return ACPV0->hasSameValue(ACPV1);
1447 } else if (!isARMCP0 && !isARMCP1) {
1448 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1449 }
1450 return false;
Evan Chengb8b0ad82011-01-20 08:34:58 +00001451 } else if (Opcode == ARM::PICLDR) {
1452 if (MI1->getOpcode() != Opcode)
1453 return false;
1454 if (MI0->getNumOperands() != MI1->getNumOperands())
1455 return false;
1456
1457 unsigned Addr0 = MI0->getOperand(1).getReg();
1458 unsigned Addr1 = MI1->getOperand(1).getReg();
1459 if (Addr0 != Addr1) {
1460 if (!MRI ||
1461 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1462 !TargetRegisterInfo::isVirtualRegister(Addr1))
1463 return false;
1464
1465 // This assumes SSA form.
1466 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1467 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1468 // Check if the loaded value, e.g. a constantpool of a global address, are
1469 // the same.
1470 if (!produceSameValue(Def0, Def1, MRI))
1471 return false;
1472 }
1473
1474 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1475 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1476 const MachineOperand &MO0 = MI0->getOperand(i);
1477 const MachineOperand &MO1 = MI1->getOperand(i);
1478 if (!MO0.isIdenticalTo(MO1))
1479 return false;
1480 }
1481 return true;
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001482 }
1483
Evan Chenge9c46c22010-03-03 01:44:33 +00001484 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001485}
1486
Bill Wendlingf4707472010-06-23 23:00:16 +00001487/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1488/// determine if two loads are loading from the same base address. It should
1489/// only return true if the base pointers are the same and the only differences
1490/// between the two addresses is the offset. It also returns the offsets by
1491/// reference.
Andrew Tricka7714a02012-11-12 19:40:10 +00001492///
1493/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1494/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001495bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1496 int64_t &Offset1,
1497 int64_t &Offset2) const {
1498 // Don't worry about Thumb: just ARM and Thumb2.
1499 if (Subtarget.isThumb1Only()) return false;
1500
1501 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1502 return false;
1503
1504 switch (Load1->getMachineOpcode()) {
1505 default:
1506 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001507 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001508 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001509 case ARM::LDRD:
1510 case ARM::LDRH:
1511 case ARM::LDRSB:
1512 case ARM::LDRSH:
1513 case ARM::VLDRD:
1514 case ARM::VLDRS:
1515 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001516 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001517 case ARM::t2LDRDi8:
1518 case ARM::t2LDRSHi8:
1519 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001520 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001521 case ARM::t2LDRSHi12:
1522 break;
1523 }
1524
1525 switch (Load2->getMachineOpcode()) {
1526 default:
1527 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001528 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001529 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001530 case ARM::LDRD:
1531 case ARM::LDRH:
1532 case ARM::LDRSB:
1533 case ARM::LDRSH:
1534 case ARM::VLDRD:
1535 case ARM::VLDRS:
1536 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001537 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001538 case ARM::t2LDRSHi8:
1539 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001540 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001541 case ARM::t2LDRSHi12:
1542 break;
1543 }
1544
1545 // Check if base addresses and chain operands match.
1546 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1547 Load1->getOperand(4) != Load2->getOperand(4))
1548 return false;
1549
1550 // Index should be Reg0.
1551 if (Load1->getOperand(3) != Load2->getOperand(3))
1552 return false;
1553
1554 // Determine the offsets.
1555 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1556 isa<ConstantSDNode>(Load2->getOperand(1))) {
1557 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1558 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1559 return true;
1560 }
1561
1562 return false;
1563}
1564
1565/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001566/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Bill Wendlingf4707472010-06-23 23:00:16 +00001567/// be scheduled togther. On some targets if two loads are loading from
1568/// addresses in the same cache line, it's better if they are scheduled
1569/// together. This function takes two integers that represent the load offsets
1570/// from the common base address. It returns true if it decides it's desirable
1571/// to schedule the two loads together. "NumLoads" is the number of loads that
1572/// have already been scheduled after Load1.
Andrew Tricka7714a02012-11-12 19:40:10 +00001573///
1574/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1575/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001576bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1577 int64_t Offset1, int64_t Offset2,
1578 unsigned NumLoads) const {
1579 // Don't worry about Thumb: just ARM and Thumb2.
1580 if (Subtarget.isThumb1Only()) return false;
1581
1582 assert(Offset2 > Offset1);
1583
1584 if ((Offset2 - Offset1) / 8 > 64)
1585 return false;
1586
Renato Golinb184cd92013-08-14 16:35:29 +00001587 // Check if the machine opcodes are different. If they are different
1588 // then we consider them to not be of the same base address,
1589 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1590 // In this case, they are considered to be the same because they are different
1591 // encoding forms of the same basic instruction.
1592 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1593 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1594 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1595 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1596 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
Bill Wendlingf4707472010-06-23 23:00:16 +00001597 return false; // FIXME: overly conservative?
1598
1599 // Four loads in a row should be sufficient.
1600 if (NumLoads >= 3)
1601 return false;
1602
1603 return true;
1604}
1605
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001606bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1607 const MachineBasicBlock *MBB,
1608 const MachineFunction &MF) const {
Jim Grosbachba3ece62010-06-25 18:43:14 +00001609 // Debug info is never a scheduling boundary. It's necessary to be explicit
1610 // due to the special treatment of IT instructions below, otherwise a
1611 // dbg_value followed by an IT will result in the IT instruction being
1612 // considered a scheduling hazard, which is wrong. It should be the actual
1613 // instruction preceding the dbg_value instruction(s), just like it is
1614 // when debug info is not present.
1615 if (MI->isDebugValue())
1616 return false;
1617
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001618 // Terminators and labels can't be scheduled around.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001619 if (MI->isTerminator() || MI->isPosition())
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001620 return true;
1621
1622 // Treat the start of the IT block as a scheduling boundary, but schedule
1623 // t2IT along with all instructions following it.
1624 // FIXME: This is a big hammer. But the alternative is to add all potential
1625 // true and anti dependencies to IT block instructions as implicit operands
1626 // to the t2IT instruction. The added compile time and complexity does not
1627 // seem worth it.
1628 MachineBasicBlock::const_iterator I = MI;
Jim Grosbachba3ece62010-06-25 18:43:14 +00001629 // Make sure to skip any dbg_value instructions
1630 while (++I != MBB->end() && I->isDebugValue())
1631 ;
1632 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001633 return true;
1634
1635 // Don't attempt to schedule around any instruction that defines
1636 // a stack-oriented pointer, as it's unlikely to be profitable. This
1637 // saves compile time, because it doesn't require every single
1638 // stack slot reference to depend on the instruction that does the
1639 // modification.
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001640 // Calls don't actually change the stack pointer, even if they have imp-defs.
Jakob Stoklund Olesen5f37f1c2012-02-22 01:07:19 +00001641 // No ARM calling conventions change the stack pointer. (X86 calling
1642 // conventions sometimes do).
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001643 if (!MI->isCall() && MI->definesRegister(ARM::SP))
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001644 return true;
1645
1646 return false;
1647}
1648
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001649bool ARMBaseInstrInfo::
1650isProfitableToIfCvt(MachineBasicBlock &MBB,
1651 unsigned NumCycles, unsigned ExtraPredCycles,
1652 const BranchProbability &Probability) const {
Cameron Zwarich80018502011-04-13 06:39:16 +00001653 if (!NumCycles)
Evan Cheng02b184d2010-06-25 22:42:03 +00001654 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001655
Peter Collingbourne65295232015-04-23 20:31:30 +00001656 // If we are optimizing for size, see if the branch in the predecessor can be
1657 // lowered to cbn?z by the constant island lowering pass, and return false if
1658 // so. This results in a shorter instruction sequence.
1659 const Function *F = MBB.getParent()->getFunction();
1660 if (F->hasFnAttribute(Attribute::OptimizeForSize) ||
1661 F->hasFnAttribute(Attribute::MinSize)) {
1662 MachineBasicBlock *Pred = *MBB.pred_begin();
1663 if (!Pred->empty()) {
1664 MachineInstr *LastMI = &*Pred->rbegin();
1665 if (LastMI->getOpcode() == ARM::t2Bcc) {
1666 MachineBasicBlock::iterator CmpMI = LastMI;
1667 if (CmpMI != Pred->begin()) {
1668 --CmpMI;
1669 if (CmpMI->getOpcode() == ARM::tCMPi8 ||
1670 CmpMI->getOpcode() == ARM::t2CMPri) {
1671 unsigned Reg = CmpMI->getOperand(0).getReg();
1672 unsigned PredReg = 0;
1673 ARMCC::CondCodes P = getInstrPredicate(CmpMI, PredReg);
1674 if (P == ARMCC::AL && CmpMI->getOperand(1).getImm() == 0 &&
1675 isARMLowRegister(Reg))
1676 return false;
1677 }
1678 }
1679 }
1680 }
1681 }
1682
Owen Anderson88af7d02010-09-28 18:32:13 +00001683 // Attempt to estimate the relative costs of predication versus branching.
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001684 unsigned UnpredCost = Probability.getNumerator() * NumCycles;
1685 UnpredCost /= Probability.getDenominator();
1686 UnpredCost += 1; // The branch itself
1687 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001688
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001689 return (NumCycles + ExtraPredCycles) <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001690}
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001691
Evan Cheng02b184d2010-06-25 22:42:03 +00001692bool ARMBaseInstrInfo::
Evan Chengdebf9c52010-11-03 00:45:17 +00001693isProfitableToIfCvt(MachineBasicBlock &TMBB,
1694 unsigned TCycles, unsigned TExtra,
1695 MachineBasicBlock &FMBB,
1696 unsigned FCycles, unsigned FExtra,
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001697 const BranchProbability &Probability) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00001698 if (!TCycles || !FCycles)
Owen Anderson88af7d02010-09-28 18:32:13 +00001699 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001700
Owen Anderson88af7d02010-09-28 18:32:13 +00001701 // Attempt to estimate the relative costs of predication versus branching.
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001702 unsigned TUnpredCost = Probability.getNumerator() * TCycles;
1703 TUnpredCost /= Probability.getDenominator();
Andrew Trick3f1fdf12011-09-21 02:17:37 +00001704
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001705 uint32_t Comp = Probability.getDenominator() - Probability.getNumerator();
1706 unsigned FUnpredCost = Comp * FCycles;
1707 FUnpredCost /= Probability.getDenominator();
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001708
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001709 unsigned UnpredCost = TUnpredCost + FUnpredCost;
1710 UnpredCost += 1; // The branch itself
1711 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
1712
1713 return (TCycles + FCycles + TExtra + FExtra) <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001714}
1715
Bob Wilsone8a549c2012-09-29 21:43:49 +00001716bool
1717ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1718 MachineBasicBlock &FMBB) const {
1719 // Reduce false anti-dependencies to let Swift's out-of-order execution
1720 // engine do its thing.
1721 return Subtarget.isSwift();
1722}
1723
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001724/// getInstrPredicate - If instruction is predicated, returns its predicate
1725/// condition, otherwise returns AL. It also returns the condition code
1726/// register by reference.
Evan Cheng83e0d482009-09-28 09:14:39 +00001727ARMCC::CondCodes
1728llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001729 int PIdx = MI->findFirstPredOperandIdx();
1730 if (PIdx == -1) {
1731 PredReg = 0;
1732 return ARMCC::AL;
1733 }
1734
1735 PredReg = MI->getOperand(PIdx+1).getReg();
1736 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1737}
1738
1739
Matthias Braunfa3872e2015-05-18 20:27:55 +00001740unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) {
Evan Cheng056c6692009-07-27 18:20:05 +00001741 if (Opc == ARM::B)
1742 return ARM::Bcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001743 if (Opc == ARM::tB)
Evan Cheng056c6692009-07-27 18:20:05 +00001744 return ARM::tBcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001745 if (Opc == ARM::t2B)
1746 return ARM::t2Bcc;
Evan Cheng056c6692009-07-27 18:20:05 +00001747
1748 llvm_unreachable("Unknown unconditional branch opcode!");
Evan Cheng056c6692009-07-27 18:20:05 +00001749}
1750
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001751/// commuteInstruction - Handle commutable instructions.
1752MachineInstr *
1753ARMBaseInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
1754 switch (MI->getOpcode()) {
1755 case ARM::MOVCCr:
1756 case ARM::t2MOVCCr: {
1757 // MOVCC can be commuted by inverting the condition.
1758 unsigned PredReg = 0;
1759 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
1760 // MOVCC AL can't be inverted. Shouldn't happen.
1761 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
Craig Topper062a2ba2014-04-25 05:30:21 +00001762 return nullptr;
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001763 MI = TargetInstrInfo::commuteInstruction(MI, NewMI);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001764 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001765 return nullptr;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001766 // After swapping the MOVCC operands, also invert the condition.
1767 MI->getOperand(MI->findFirstPredOperandIdx())
1768 .setImm(ARMCC::getOppositeCondition(CC));
1769 return MI;
1770 }
1771 }
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001772 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001773}
Evan Cheng780748d2009-07-28 05:48:47 +00001774
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001775/// Identify instructions that can be folded into a MOVCC instruction, and
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001776/// return the defining instruction.
1777static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
1778 const MachineRegisterInfo &MRI,
1779 const TargetInstrInfo *TII) {
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001780 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001781 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001782 if (!MRI.hasOneNonDBGUse(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001783 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001784 MachineInstr *MI = MRI.getVRegDef(Reg);
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001785 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001786 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001787 // MI is folded into the MOVCC by predicating it.
1788 if (!MI->isPredicable())
Craig Topper062a2ba2014-04-25 05:30:21 +00001789 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001790 // Check if MI has any non-dead defs or physreg uses. This also detects
1791 // predicated instructions which will be reading CPSR.
1792 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
1793 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen7b1a2e82012-08-17 20:55:34 +00001794 // Reject frame index operands, PEI can't handle the predicated pseudos.
1795 if (MO.isFI() || MO.isCPI() || MO.isJTI())
Craig Topper062a2ba2014-04-25 05:30:21 +00001796 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001797 if (!MO.isReg())
1798 continue;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001799 // MI can't have any tied operands, that would conflict with predication.
1800 if (MO.isTied())
Craig Topper062a2ba2014-04-25 05:30:21 +00001801 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001802 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Craig Topper062a2ba2014-04-25 05:30:21 +00001803 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001804 if (MO.isDef() && !MO.isDead())
Craig Topper062a2ba2014-04-25 05:30:21 +00001805 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001806 }
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001807 bool DontMoveAcrossStores = true;
Matthias Braun07066cc2015-05-19 21:22:20 +00001808 if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores))
Craig Topper062a2ba2014-04-25 05:30:21 +00001809 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001810 return MI;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001811}
1812
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001813bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr *MI,
1814 SmallVectorImpl<MachineOperand> &Cond,
1815 unsigned &TrueOp, unsigned &FalseOp,
1816 bool &Optimizable) const {
1817 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1818 "Unknown select instruction");
1819 // MOVCC operands:
1820 // 0: Def.
1821 // 1: True use.
1822 // 2: False use.
1823 // 3: Condition code.
1824 // 4: CPSR use.
1825 TrueOp = 1;
1826 FalseOp = 2;
1827 Cond.push_back(MI->getOperand(3));
1828 Cond.push_back(MI->getOperand(4));
1829 // We can always fold a def.
1830 Optimizable = true;
1831 return false;
1832}
1833
Mehdi Amini22e59742015-01-13 07:07:13 +00001834MachineInstr *
1835ARMBaseInstrInfo::optimizeSelect(MachineInstr *MI,
1836 SmallPtrSetImpl<MachineInstr *> &SeenMIs,
1837 bool PreferFalse) const {
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001838 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1839 "Unknown select instruction");
Matthias Braun2f169f92013-10-04 16:52:56 +00001840 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001841 MachineInstr *DefMI = canFoldIntoMOVCC(MI->getOperand(2).getReg(), MRI, this);
1842 bool Invert = !DefMI;
1843 if (!DefMI)
1844 DefMI = canFoldIntoMOVCC(MI->getOperand(1).getReg(), MRI, this);
1845 if (!DefMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001846 return nullptr;
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001847
Matthias Braun2f169f92013-10-04 16:52:56 +00001848 // Find new register class to use.
1849 MachineOperand FalseReg = MI->getOperand(Invert ? 2 : 1);
1850 unsigned DestReg = MI->getOperand(0).getReg();
1851 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
1852 if (!MRI.constrainRegClass(DestReg, PreviousClass))
Craig Topper062a2ba2014-04-25 05:30:21 +00001853 return nullptr;
Matthias Braun2f169f92013-10-04 16:52:56 +00001854
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001855 // Create a new predicated version of DefMI.
1856 // Rfalse is the first use.
1857 MachineInstrBuilder NewMI = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
Matthias Braun2f169f92013-10-04 16:52:56 +00001858 DefMI->getDesc(), DestReg);
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001859
1860 // Copy all the DefMI operands, excluding its (null) predicate.
1861 const MCInstrDesc &DefDesc = DefMI->getDesc();
1862 for (unsigned i = 1, e = DefDesc.getNumOperands();
1863 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
1864 NewMI.addOperand(DefMI->getOperand(i));
1865
1866 unsigned CondCode = MI->getOperand(3).getImm();
1867 if (Invert)
1868 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
1869 else
1870 NewMI.addImm(CondCode);
1871 NewMI.addOperand(MI->getOperand(4));
1872
1873 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
1874 if (NewMI->hasOptionalDef())
1875 AddDefaultCC(NewMI);
1876
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001877 // The output register value when the predicate is false is an implicit
1878 // register operand tied to the first def.
1879 // The tie makes the register allocator ensure the FalseReg is allocated the
1880 // same register as operand 0.
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001881 FalseReg.setImplicit();
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +00001882 NewMI.addOperand(FalseReg);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001883 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
1884
Mehdi Amini22e59742015-01-13 07:07:13 +00001885 // Update SeenMIs set: register newly created MI and erase removed DefMI.
1886 SeenMIs.insert(NewMI);
1887 SeenMIs.erase(DefMI);
1888
Pete Cooper2127b002015-04-30 23:57:47 +00001889 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
1890 // DefMI would be invalid when tranferred inside the loop. Checking for a
1891 // loop is expensive, but at least remove kill flags if they are in different
1892 // BBs.
1893 if (DefMI->getParent() != MI->getParent())
1894 NewMI->clearKillInfo();
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.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00001996 if (!MF.getFunction()->hasFnAttribute(Attribute::MinSize))
Tim Northover93bcc662013-11-08 17:18:07 +00001997 return false;
1998
1999 // If only one register is pushed/popped, LLVM can use an LDR/STR
2000 // instead. We can't modify those so make sure we're dealing with an
2001 // instruction we understand.
2002 bool IsPop = isPopOpcode(MI->getOpcode());
2003 bool IsPush = isPushOpcode(MI->getOpcode());
2004 if (!IsPush && !IsPop)
2005 return false;
2006
2007 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
2008 MI->getOpcode() == ARM::VLDMDIA_UPD;
2009 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
2010 MI->getOpcode() == ARM::tPOP ||
2011 MI->getOpcode() == ARM::tPOP_RET;
2012
2013 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
2014 MI->getOperand(1).getReg() == ARM::SP)) &&
2015 "trying to fold sp update into non-sp-updating push/pop");
2016
2017 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2018 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2019 // if this is violated.
2020 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
2021 return false;
2022
2023 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2024 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2025 int RegListIdx = IsT1PushPop ? 2 : 4;
2026
2027 // Calculate the space we'll need in terms of registers.
2028 unsigned FirstReg = MI->getOperand(RegListIdx).getReg();
2029 unsigned RD0Reg, RegsNeeded;
2030 if (IsVFPPushPop) {
2031 RD0Reg = ARM::D0;
2032 RegsNeeded = NumBytes / 8;
2033 } else {
2034 RD0Reg = ARM::R0;
2035 RegsNeeded = NumBytes / 4;
2036 }
2037
2038 // We're going to have to strip all list operands off before
2039 // re-adding them since the order matters, so save the existing ones
2040 // for later.
2041 SmallVector<MachineOperand, 4> RegList;
2042 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2043 RegList.push_back(MI->getOperand(i));
2044
Tim Northover93bcc662013-11-08 17:18:07 +00002045 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
Tim Northover45479dc2013-12-01 14:16:24 +00002046 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Tim Northover93bcc662013-11-08 17:18:07 +00002047
2048 // Now try to find enough space in the reglist to allocate NumBytes.
2049 for (unsigned CurReg = FirstReg - 1; CurReg >= RD0Reg && RegsNeeded;
Tim Northover45479dc2013-12-01 14:16:24 +00002050 --CurReg) {
Tim Northover93bcc662013-11-08 17:18:07 +00002051 if (!IsPop) {
2052 // Pushing any register is completely harmless, mark the
2053 // register involved as undef since we don't care about it in
2054 // the slightest.
2055 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2056 false, false, true));
Tim Northover45479dc2013-12-01 14:16:24 +00002057 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002058 continue;
2059 }
2060
Tim Northover45479dc2013-12-01 14:16:24 +00002061 // However, we can only pop an extra register if it's not live. For
2062 // registers live within the function we might clobber a return value
2063 // register; the other way a register can be live here is if it's
2064 // callee-saved.
Weiming Zhao01524852014-03-20 23:28:16 +00002065 // TODO: Currently, computeRegisterLiveness() does not report "live" if a
2066 // sub reg is live. When computeRegisterLiveness() works for sub reg, it
2067 // can replace isAnySubRegLive().
Tim Northover45479dc2013-12-01 14:16:24 +00002068 if (isCalleeSavedRegister(CurReg, CSRegs) ||
Weiming Zhao01524852014-03-20 23:28:16 +00002069 isAnySubRegLive(CurReg, TRI, MI)) {
Tim Northover45479dc2013-12-01 14:16:24 +00002070 // VFP pops don't allow holes in the register list, so any skip is fatal
2071 // for our transformation. GPR pops do, so we should just keep looking.
2072 if (IsVFPPushPop)
2073 return false;
2074 else
2075 continue;
2076 }
Tim Northover93bcc662013-11-08 17:18:07 +00002077
2078 // Mark the unimportant registers as <def,dead> in the POP.
Lang Hames1ca11232013-11-22 00:46:32 +00002079 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2080 true));
Tim Northover45479dc2013-12-01 14:16:24 +00002081 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002082 }
2083
2084 if (RegsNeeded > 0)
2085 return false;
2086
2087 // Finally we know we can profitably perform the optimisation so go
2088 // ahead: strip all existing registers off and add them back again
2089 // in the right order.
2090 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2091 MI->RemoveOperand(i);
2092
2093 // Add the complete list back in.
2094 MachineInstrBuilder MIB(MF, &*MI);
2095 for (int i = RegList.size() - 1; i >= 0; --i)
2096 MIB.addOperand(RegList[i]);
2097
2098 return true;
2099}
2100
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002101bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2102 unsigned FrameReg, int &Offset,
2103 const ARMBaseInstrInfo &TII) {
Evan Cheng780748d2009-07-28 05:48:47 +00002104 unsigned Opcode = MI.getOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002105 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng780748d2009-07-28 05:48:47 +00002106 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2107 bool isSub = false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002108
Evan Cheng780748d2009-07-28 05:48:47 +00002109 // Memory operands in inline assembly always use AddrMode2.
2110 if (Opcode == ARM::INLINEASM)
2111 AddrMode = ARMII::AddrMode2;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002112
Evan Cheng780748d2009-07-28 05:48:47 +00002113 if (Opcode == ARM::ADDri) {
2114 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2115 if (Offset == 0) {
2116 // Turn it into a move.
2117 MI.setDesc(TII.get(ARM::MOVr));
2118 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2119 MI.RemoveOperand(FrameRegIdx+1);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002120 Offset = 0;
2121 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002122 } else if (Offset < 0) {
2123 Offset = -Offset;
2124 isSub = true;
2125 MI.setDesc(TII.get(ARM::SUBri));
2126 }
2127
2128 // Common case: small offset, fits into instruction.
2129 if (ARM_AM::getSOImmVal(Offset) != -1) {
2130 // Replace the FrameIndex with sp / fp
2131 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2132 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002133 Offset = 0;
2134 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002135 }
2136
2137 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2138 // as possible.
2139 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2140 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2141
2142 // We will handle these bits from offset, clear them.
2143 Offset &= ~ThisImmVal;
2144
2145 // Get the properly encoded SOImmVal field.
2146 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2147 "Bit extraction didn't work?");
2148 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2149 } else {
2150 unsigned ImmIdx = 0;
2151 int InstrOffs = 0;
2152 unsigned NumBits = 0;
2153 unsigned Scale = 1;
2154 switch (AddrMode) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002155 case ARMII::AddrMode_i12: {
2156 ImmIdx = FrameRegIdx + 1;
2157 InstrOffs = MI.getOperand(ImmIdx).getImm();
2158 NumBits = 12;
2159 break;
2160 }
Evan Cheng780748d2009-07-28 05:48:47 +00002161 case ARMII::AddrMode2: {
2162 ImmIdx = FrameRegIdx+2;
2163 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2164 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2165 InstrOffs *= -1;
2166 NumBits = 12;
2167 break;
2168 }
2169 case ARMII::AddrMode3: {
2170 ImmIdx = FrameRegIdx+2;
2171 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2172 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2173 InstrOffs *= -1;
2174 NumBits = 8;
2175 break;
2176 }
Anton Korobeynikov887d05c2009-08-08 13:35:48 +00002177 case ARMII::AddrMode4:
Jim Grosbach01c1cae2009-11-15 21:45:34 +00002178 case ARMII::AddrMode6:
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002179 // Can't fold any offset even if it's zero.
2180 return false;
Evan Cheng780748d2009-07-28 05:48:47 +00002181 case ARMII::AddrMode5: {
2182 ImmIdx = FrameRegIdx+1;
2183 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2184 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2185 InstrOffs *= -1;
2186 NumBits = 8;
2187 Scale = 4;
2188 break;
2189 }
2190 default:
2191 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng780748d2009-07-28 05:48:47 +00002192 }
2193
2194 Offset += InstrOffs * Scale;
2195 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2196 if (Offset < 0) {
2197 Offset = -Offset;
2198 isSub = true;
2199 }
2200
2201 // Attempt to fold address comp. if opcode has offset bits
2202 if (NumBits > 0) {
2203 // Common case: small offset, fits into instruction.
2204 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2205 int ImmedOffset = Offset / Scale;
2206 unsigned Mask = (1 << NumBits) - 1;
2207 if ((unsigned)Offset <= Mask * Scale) {
2208 // Replace the FrameIndex with sp
2209 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach9d2d1f02010-10-27 01:19:41 +00002210 // FIXME: When addrmode2 goes away, this will simplify (like the
2211 // T2 version), as the LDR.i12 versions don't need the encoding
2212 // tricks for the offset value.
2213 if (isSub) {
2214 if (AddrMode == ARMII::AddrMode_i12)
2215 ImmedOffset = -ImmedOffset;
2216 else
2217 ImmedOffset |= 1 << NumBits;
2218 }
Evan Cheng780748d2009-07-28 05:48:47 +00002219 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002220 Offset = 0;
2221 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002222 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002223
Evan Cheng780748d2009-07-28 05:48:47 +00002224 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2225 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach8bf14832010-10-27 16:50:31 +00002226 if (isSub) {
2227 if (AddrMode == ARMII::AddrMode_i12)
2228 ImmedOffset = -ImmedOffset;
2229 else
2230 ImmedOffset |= 1 << NumBits;
2231 }
Evan Cheng780748d2009-07-28 05:48:47 +00002232 ImmOp.ChangeToImmediate(ImmedOffset);
2233 Offset &= ~(Mask*Scale);
2234 }
2235 }
2236
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002237 Offset = (isSub) ? -Offset : Offset;
2238 return Offset == 0;
Evan Cheng780748d2009-07-28 05:48:47 +00002239}
Bill Wendling7de9d522010-08-06 01:32:48 +00002240
Manman Ren6fa76dc2012-06-29 21:33:59 +00002241/// analyzeCompare - For a comparison instruction, return the source registers
2242/// in SrcReg and SrcReg2 if having two register operands, and the value it
2243/// compares against in CmpValue. Return true if the comparison instruction
2244/// can be analyzed.
Bill Wendling7de9d522010-08-06 01:32:48 +00002245bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002246analyzeCompare(const MachineInstr *MI, unsigned &SrcReg, unsigned &SrcReg2,
2247 int &CmpMask, int &CmpValue) const {
Bill Wendling7de9d522010-08-06 01:32:48 +00002248 switch (MI->getOpcode()) {
2249 default: break;
Bill Wendling79553ba2010-08-11 00:23:00 +00002250 case ARM::CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002251 case ARM::t2CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002252 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002253 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002254 CmpMask = ~0;
Bill Wendling7de9d522010-08-06 01:32:48 +00002255 CmpValue = MI->getOperand(1).getImm();
2256 return true;
Manman Rendc8ad002012-05-11 01:30:47 +00002257 case ARM::CMPrr:
2258 case ARM::t2CMPrr:
2259 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002260 SrcReg2 = MI->getOperand(1).getReg();
Manman Rendc8ad002012-05-11 01:30:47 +00002261 CmpMask = ~0;
2262 CmpValue = 0;
2263 return true;
Gabor Greifadbbb932010-09-21 12:01:15 +00002264 case ARM::TSTri:
2265 case ARM::t2TSTri:
2266 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002267 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002268 CmpMask = MI->getOperand(1).getImm();
2269 CmpValue = 0;
2270 return true;
2271 }
2272
2273 return false;
2274}
2275
Gabor Greifd36e3e82010-09-29 10:12:08 +00002276/// isSuitableForMask - Identify a suitable 'and' instruction that
2277/// operates on the given source register and applies the same mask
2278/// as a 'tst' instruction. Provide a limited look-through for copies.
2279/// When successful, MI will hold the found instruction.
2280static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif1a25ae82010-09-21 13:30:57 +00002281 int CmpMask, bool CommonUse) {
Gabor Greifd36e3e82010-09-29 10:12:08 +00002282 switch (MI->getOpcode()) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002283 case ARM::ANDri:
2284 case ARM::t2ANDri:
Gabor Greifd36e3e82010-09-29 10:12:08 +00002285 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif1a25ae82010-09-21 13:30:57 +00002286 return false;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002287 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greifadbbb932010-09-21 12:01:15 +00002288 return true;
2289 break;
Bill Wendling7de9d522010-08-06 01:32:48 +00002290 }
2291
2292 return false;
2293}
2294
Manman Renb1b3db62012-06-29 22:06:19 +00002295/// getSwappedCondition - assume the flags are set by MI(a,b), return
2296/// the condition code if we modify the instructions such that flags are
2297/// set by MI(b,a).
2298inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2299 switch (CC) {
2300 default: return ARMCC::AL;
2301 case ARMCC::EQ: return ARMCC::EQ;
2302 case ARMCC::NE: return ARMCC::NE;
2303 case ARMCC::HS: return ARMCC::LS;
2304 case ARMCC::LO: return ARMCC::HI;
2305 case ARMCC::HI: return ARMCC::LO;
2306 case ARMCC::LS: return ARMCC::HS;
2307 case ARMCC::GE: return ARMCC::LE;
2308 case ARMCC::LT: return ARMCC::GT;
2309 case ARMCC::GT: return ARMCC::LT;
2310 case ARMCC::LE: return ARMCC::GE;
2311 }
2312}
2313
2314/// isRedundantFlagInstr - check whether the first instruction, whose only
2315/// purpose is to update flags, can be made redundant.
2316/// CMPrr can be made redundant by SUBrr if the operands are the same.
2317/// CMPri can be made redundant by SUBri if the operands are the same.
2318/// This function can be extended later on.
2319inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2320 unsigned SrcReg2, int ImmValue,
2321 MachineInstr *OI) {
2322 if ((CmpI->getOpcode() == ARM::CMPrr ||
2323 CmpI->getOpcode() == ARM::t2CMPrr) &&
2324 (OI->getOpcode() == ARM::SUBrr ||
2325 OI->getOpcode() == ARM::t2SUBrr) &&
2326 ((OI->getOperand(1).getReg() == SrcReg &&
2327 OI->getOperand(2).getReg() == SrcReg2) ||
2328 (OI->getOperand(1).getReg() == SrcReg2 &&
2329 OI->getOperand(2).getReg() == SrcReg)))
2330 return true;
2331
2332 if ((CmpI->getOpcode() == ARM::CMPri ||
2333 CmpI->getOpcode() == ARM::t2CMPri) &&
2334 (OI->getOpcode() == ARM::SUBri ||
2335 OI->getOpcode() == ARM::t2SUBri) &&
2336 OI->getOperand(1).getReg() == SrcReg &&
2337 OI->getOperand(2).getImm() == ImmValue)
2338 return true;
2339 return false;
2340}
2341
Manman Ren6fa76dc2012-06-29 21:33:59 +00002342/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2343/// comparison into one that sets the zero bit in the flags register;
2344/// Remove a redundant Compare instruction if an earlier instruction can set the
2345/// flags in the same way as Compare.
2346/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2347/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2348/// condition code of instructions which use the flags.
Bill Wendling7de9d522010-08-06 01:32:48 +00002349bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002350optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, unsigned SrcReg2,
2351 int CmpMask, int CmpValue,
2352 const MachineRegisterInfo *MRI) const {
Manman Renb1b3db62012-06-29 22:06:19 +00002353 // Get the unique definition of SrcReg.
2354 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2355 if (!MI) return false;
Bill Wendling04123002010-09-10 23:34:19 +00002356
Gabor Greifadbbb932010-09-21 12:01:15 +00002357 // Masked compares sometimes use the same register as the corresponding 'and'.
2358 if (CmpMask != ~0) {
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002359 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(MI)) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002360 MI = nullptr;
Owen Anderson16c6bf42014-03-13 23:12:04 +00002361 for (MachineRegisterInfo::use_instr_iterator
2362 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2363 UI != UE; ++UI) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002364 if (UI->getParent() != CmpInstr->getParent()) continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002365 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002366 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
2367 isPredicated(PotentialAND))
Gabor Greifadbbb932010-09-21 12:01:15 +00002368 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002369 MI = PotentialAND;
Gabor Greifadbbb932010-09-21 12:01:15 +00002370 break;
2371 }
2372 if (!MI) return false;
2373 }
2374 }
2375
Manman Rendc8ad002012-05-11 01:30:47 +00002376 // Get ready to iterate backward from CmpInstr.
2377 MachineBasicBlock::iterator I = CmpInstr, E = MI,
2378 B = CmpInstr->getParent()->begin();
Bill Wendling59ebe442010-10-09 00:03:48 +00002379
2380 // Early exit if CmpInstr is at the beginning of the BB.
2381 if (I == B) return false;
2382
Manman Rendc8ad002012-05-11 01:30:47 +00002383 // There are two possible candidates which can be changed to set CPSR:
2384 // One is MI, the other is a SUB instruction.
2385 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2386 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
Craig Topper062a2ba2014-04-25 05:30:21 +00002387 MachineInstr *Sub = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002388 if (SrcReg2 != 0)
Manman Rendc8ad002012-05-11 01:30:47 +00002389 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00002390 MI = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002391 else if (MI->getParent() != CmpInstr->getParent() || CmpValue != 0) {
Manman Rendc8ad002012-05-11 01:30:47 +00002392 // Conservatively refuse to convert an instruction which isn't in the same
2393 // BB as the comparison.
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002394 // For CMPri w/ CmpValue != 0, a Sub may still be a candidate.
2395 // Thus we cannot return here.
Manman Ren0d5ec282012-05-11 15:36:46 +00002396 if (CmpInstr->getOpcode() == ARM::CMPri ||
Manman Rendc8ad002012-05-11 01:30:47 +00002397 CmpInstr->getOpcode() == ARM::t2CMPri)
Craig Topper062a2ba2014-04-25 05:30:21 +00002398 MI = nullptr;
Manman Rendc8ad002012-05-11 01:30:47 +00002399 else
2400 return false;
2401 }
2402
2403 // Check that CPSR isn't set between the comparison instruction and the one we
2404 // want to change. At the same time, search for Sub.
Manman Renb1b3db62012-06-29 22:06:19 +00002405 const TargetRegisterInfo *TRI = &getRegisterInfo();
Bill Wendling7de9d522010-08-06 01:32:48 +00002406 --I;
2407 for (; I != E; --I) {
2408 const MachineInstr &Instr = *I;
2409
Manman Renb1b3db62012-06-29 22:06:19 +00002410 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2411 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendlingc6627ee2010-11-01 20:41:43 +00002412 // This instruction modifies or uses CPSR after the one we want to
2413 // change. We can't do this transformation.
Manman Renb1b3db62012-06-29 22:06:19 +00002414 return false;
Evan Chengd757c882010-09-21 23:49:07 +00002415
Manman Renb1b3db62012-06-29 22:06:19 +00002416 // Check whether CmpInstr can be made redundant by the current instruction.
2417 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
Manman Rendc8ad002012-05-11 01:30:47 +00002418 Sub = &*I;
2419 break;
2420 }
2421
Evan Chengd757c882010-09-21 23:49:07 +00002422 if (I == B)
2423 // The 'and' is below the comparison instruction.
2424 return false;
Bill Wendling7de9d522010-08-06 01:32:48 +00002425 }
2426
Manman Rendc8ad002012-05-11 01:30:47 +00002427 // Return false if no candidates exist.
2428 if (!MI && !Sub)
2429 return false;
2430
2431 // The single candidate is called MI.
2432 if (!MI) MI = Sub;
2433
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002434 // We can't use a predicated instruction - it doesn't always write the flags.
2435 if (isPredicated(MI))
2436 return false;
2437
Bill Wendling7de9d522010-08-06 01:32:48 +00002438 switch (MI->getOpcode()) {
2439 default: break;
Cameron Zwarich93eae152011-04-15 20:28:28 +00002440 case ARM::RSBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002441 case ARM::RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002442 case ARM::RSCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002443 case ARM::RSCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002444 case ARM::ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002445 case ARM::ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002446 case ARM::ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002447 case ARM::ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002448 case ARM::SUBrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002449 case ARM::SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002450 case ARM::SBCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002451 case ARM::SBCri:
2452 case ARM::t2RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002453 case ARM::t2ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002454 case ARM::t2ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002455 case ARM::t2ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002456 case ARM::t2ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002457 case ARM::t2SUBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002458 case ARM::t2SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002459 case ARM::t2SBCrr:
Cameron Zwarich0829b302011-04-15 20:45:00 +00002460 case ARM::t2SBCri:
2461 case ARM::ANDrr:
2462 case ARM::ANDri:
2463 case ARM::t2ANDrr:
Cameron Zwarich9c65e4d2011-04-15 21:24:38 +00002464 case ARM::t2ANDri:
2465 case ARM::ORRrr:
2466 case ARM::ORRri:
2467 case ARM::t2ORRrr:
2468 case ARM::t2ORRri:
2469 case ARM::EORrr:
2470 case ARM::EORri:
2471 case ARM::t2EORrr:
2472 case ARM::t2EORri: {
Manman Rendc8ad002012-05-11 01:30:47 +00002473 // Scan forward for the use of CPSR
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002474 // When checking against MI: if it's a conditional code that requires
2475 // checking of the V bit or C bit, then this is not safe to do.
Manman Ren34cb93e2012-07-11 22:51:44 +00002476 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2477 // If we are done with the basic block, we need to check whether CPSR is
2478 // live-out.
Manman Renb1b3db62012-06-29 22:06:19 +00002479 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2480 OperandsToUpdate;
Evan Cheng425489d2011-03-23 22:52:04 +00002481 bool isSafe = false;
2482 I = CmpInstr;
Manman Rendc8ad002012-05-11 01:30:47 +00002483 E = CmpInstr->getParent()->end();
Evan Cheng425489d2011-03-23 22:52:04 +00002484 while (!isSafe && ++I != E) {
2485 const MachineInstr &Instr = *I;
2486 for (unsigned IO = 0, EO = Instr.getNumOperands();
2487 !isSafe && IO != EO; ++IO) {
2488 const MachineOperand &MO = Instr.getOperand(IO);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +00002489 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2490 isSafe = true;
2491 break;
2492 }
Evan Cheng425489d2011-03-23 22:52:04 +00002493 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2494 continue;
2495 if (MO.isDef()) {
2496 isSafe = true;
2497 break;
2498 }
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002499 // Condition code is after the operand before CPSR except for VSELs.
2500 ARMCC::CondCodes CC;
2501 bool IsInstrVSel = true;
2502 switch (Instr.getOpcode()) {
2503 default:
2504 IsInstrVSel = false;
2505 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2506 break;
2507 case ARM::VSELEQD:
2508 case ARM::VSELEQS:
2509 CC = ARMCC::EQ;
2510 break;
2511 case ARM::VSELGTD:
2512 case ARM::VSELGTS:
2513 CC = ARMCC::GT;
2514 break;
2515 case ARM::VSELGED:
2516 case ARM::VSELGES:
2517 CC = ARMCC::GE;
2518 break;
2519 case ARM::VSELVSS:
2520 case ARM::VSELVSD:
2521 CC = ARMCC::VS;
2522 break;
2523 }
2524
Manman Renb1b3db62012-06-29 22:06:19 +00002525 if (Sub) {
2526 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2527 if (NewCC == ARMCC::AL)
Manman Rendc8ad002012-05-11 01:30:47 +00002528 return false;
Manman Renb1b3db62012-06-29 22:06:19 +00002529 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2530 // on CMP needs to be updated to be based on SUB.
2531 // Push the condition code operands to OperandsToUpdate.
2532 // If it is safe to remove CmpInstr, the condition code of these
2533 // operands will be modified.
2534 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002535 Sub->getOperand(2).getReg() == SrcReg) {
2536 // VSel doesn't support condition code update.
2537 if (IsInstrVSel)
2538 return false;
2539 OperandsToUpdate.push_back(
2540 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2541 }
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002542 } else {
2543 // No Sub, so this is x = <op> y, z; cmp x, 0.
Manman Rendc8ad002012-05-11 01:30:47 +00002544 switch (CC) {
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002545 case ARMCC::EQ: // Z
2546 case ARMCC::NE: // Z
2547 case ARMCC::MI: // N
2548 case ARMCC::PL: // N
2549 case ARMCC::AL: // none
Manman Ren88a0d332012-07-11 23:47:00 +00002550 // CPSR can be used multiple times, we should continue.
Manman Rendc8ad002012-05-11 01:30:47 +00002551 break;
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002552 case ARMCC::HS: // C
2553 case ARMCC::LO: // C
2554 case ARMCC::VS: // V
2555 case ARMCC::VC: // V
2556 case ARMCC::HI: // C Z
2557 case ARMCC::LS: // C Z
2558 case ARMCC::GE: // N V
2559 case ARMCC::LT: // N V
2560 case ARMCC::GT: // Z N V
2561 case ARMCC::LE: // Z N V
2562 // The instruction uses the V bit or C bit which is not safe.
Manman Rendc8ad002012-05-11 01:30:47 +00002563 return false;
2564 }
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002565 }
Evan Cheng425489d2011-03-23 22:52:04 +00002566 }
2567 }
2568
Manman Ren34cb93e2012-07-11 22:51:44 +00002569 // If CPSR is not killed nor re-defined, we should check whether it is
2570 // live-out. If it is live-out, do not optimize.
2571 if (!isSafe) {
2572 MachineBasicBlock *MBB = CmpInstr->getParent();
2573 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2574 SE = MBB->succ_end(); SI != SE; ++SI)
2575 if ((*SI)->isLiveIn(ARM::CPSR))
2576 return false;
2577 }
Evan Cheng425489d2011-03-23 22:52:04 +00002578
Evan Cheng65536472010-11-17 08:06:50 +00002579 // Toggle the optional operand to CPSR.
2580 MI->getOperand(5).setReg(ARM::CPSR);
2581 MI->getOperand(5).setIsDef(true);
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002582 assert(!isPredicated(MI) && "Can't use flags from predicated instruction");
Bill Wendling7de9d522010-08-06 01:32:48 +00002583 CmpInstr->eraseFromParent();
Manman Rendc8ad002012-05-11 01:30:47 +00002584
2585 // Modify the condition code of operands in OperandsToUpdate.
2586 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2587 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Manman Renb1b3db62012-06-29 22:06:19 +00002588 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2589 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
Bill Wendling7de9d522010-08-06 01:32:48 +00002590 return true;
2591 }
Cameron Zwarich0829b302011-04-15 20:45:00 +00002592 }
Bill Wendling7de9d522010-08-06 01:32:48 +00002593
2594 return false;
2595}
Evan Cheng367a5df2010-09-09 18:18:55 +00002596
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002597bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
2598 MachineInstr *DefMI, unsigned Reg,
2599 MachineRegisterInfo *MRI) const {
2600 // Fold large immediates into add, sub, or, xor.
2601 unsigned DefOpc = DefMI->getOpcode();
2602 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2603 return false;
2604 if (!DefMI->getOperand(1).isImm())
2605 // Could be t2MOVi32imm <ga:xx>
2606 return false;
2607
2608 if (!MRI->hasOneNonDBGUse(Reg))
2609 return false;
2610
Evan Chenga2b48d92012-03-26 23:31:00 +00002611 const MCInstrDesc &DefMCID = DefMI->getDesc();
2612 if (DefMCID.hasOptionalDef()) {
2613 unsigned NumOps = DefMCID.getNumOperands();
2614 const MachineOperand &MO = DefMI->getOperand(NumOps-1);
2615 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2616 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2617 // to delete DefMI.
2618 return false;
2619 }
2620
2621 const MCInstrDesc &UseMCID = UseMI->getDesc();
2622 if (UseMCID.hasOptionalDef()) {
2623 unsigned NumOps = UseMCID.getNumOperands();
2624 if (UseMI->getOperand(NumOps-1).getReg() == ARM::CPSR)
2625 // If the instruction sets the flag, do not attempt this optimization
2626 // since it may change the semantics of the code.
2627 return false;
2628 }
2629
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002630 unsigned UseOpc = UseMI->getOpcode();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002631 unsigned NewUseOpc = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002632 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002633 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002634 bool Commute = false;
2635 switch (UseOpc) {
2636 default: return false;
2637 case ARM::SUBrr:
2638 case ARM::ADDrr:
2639 case ARM::ORRrr:
2640 case ARM::EORrr:
2641 case ARM::t2SUBrr:
2642 case ARM::t2ADDrr:
2643 case ARM::t2ORRrr:
2644 case ARM::t2EORrr: {
2645 Commute = UseMI->getOperand(2).getReg() != Reg;
2646 switch (UseOpc) {
2647 default: break;
2648 case ARM::SUBrr: {
2649 if (Commute)
2650 return false;
2651 ImmVal = -ImmVal;
2652 NewUseOpc = ARM::SUBri;
2653 // Fallthrough
2654 }
2655 case ARM::ADDrr:
2656 case ARM::ORRrr:
2657 case ARM::EORrr: {
2658 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2659 return false;
2660 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2661 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2662 switch (UseOpc) {
2663 default: break;
2664 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
2665 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2666 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2667 }
2668 break;
2669 }
2670 case ARM::t2SUBrr: {
2671 if (Commute)
2672 return false;
2673 ImmVal = -ImmVal;
2674 NewUseOpc = ARM::t2SUBri;
2675 // Fallthrough
2676 }
2677 case ARM::t2ADDrr:
2678 case ARM::t2ORRrr:
2679 case ARM::t2EORrr: {
2680 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2681 return false;
2682 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2683 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2684 switch (UseOpc) {
2685 default: break;
2686 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
2687 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2688 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2689 }
2690 break;
2691 }
2692 }
2693 }
2694 }
2695
2696 unsigned OpIdx = Commute ? 2 : 1;
2697 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
2698 bool isKill = UseMI->getOperand(OpIdx).isKill();
2699 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
2700 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
Evan Cheng7fae11b2011-12-14 02:11:42 +00002701 UseMI, UseMI->getDebugLoc(),
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002702 get(NewUseOpc), NewReg)
2703 .addReg(Reg1, getKillRegState(isKill))
2704 .addImm(SOImmValV1)));
2705 UseMI->setDesc(get(NewUseOpc));
2706 UseMI->getOperand(1).setReg(NewReg);
2707 UseMI->getOperand(1).setIsKill();
2708 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
2709 DefMI->eraseFromParent();
2710 return true;
2711}
2712
Bob Wilsone8a549c2012-09-29 21:43:49 +00002713static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
2714 const MachineInstr *MI) {
2715 switch (MI->getOpcode()) {
2716 default: {
2717 const MCInstrDesc &Desc = MI->getDesc();
2718 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
2719 assert(UOps >= 0 && "bad # UOps");
2720 return UOps;
2721 }
2722
2723 case ARM::LDRrs:
2724 case ARM::LDRBrs:
2725 case ARM::STRrs:
2726 case ARM::STRBrs: {
2727 unsigned ShOpVal = MI->getOperand(3).getImm();
2728 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2729 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2730 if (!isSub &&
2731 (ShImm == 0 ||
2732 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2733 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2734 return 1;
2735 return 2;
2736 }
2737
2738 case ARM::LDRH:
2739 case ARM::STRH: {
2740 if (!MI->getOperand(2).getReg())
2741 return 1;
2742
2743 unsigned ShOpVal = MI->getOperand(3).getImm();
2744 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2745 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2746 if (!isSub &&
2747 (ShImm == 0 ||
2748 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2749 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2750 return 1;
2751 return 2;
2752 }
2753
2754 case ARM::LDRSB:
2755 case ARM::LDRSH:
2756 return (ARM_AM::getAM3Op(MI->getOperand(3).getImm()) == ARM_AM::sub) ? 3:2;
2757
2758 case ARM::LDRSB_POST:
2759 case ARM::LDRSH_POST: {
2760 unsigned Rt = MI->getOperand(0).getReg();
2761 unsigned Rm = MI->getOperand(3).getReg();
2762 return (Rt == Rm) ? 4 : 3;
2763 }
2764
2765 case ARM::LDR_PRE_REG:
2766 case ARM::LDRB_PRE_REG: {
2767 unsigned Rt = MI->getOperand(0).getReg();
2768 unsigned Rm = MI->getOperand(3).getReg();
2769 if (Rt == Rm)
2770 return 3;
2771 unsigned ShOpVal = MI->getOperand(4).getImm();
2772 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2773 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2774 if (!isSub &&
2775 (ShImm == 0 ||
2776 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2777 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2778 return 2;
2779 return 3;
2780 }
2781
2782 case ARM::STR_PRE_REG:
2783 case ARM::STRB_PRE_REG: {
2784 unsigned ShOpVal = MI->getOperand(4).getImm();
2785 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2786 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2787 if (!isSub &&
2788 (ShImm == 0 ||
2789 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2790 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2791 return 2;
2792 return 3;
2793 }
2794
2795 case ARM::LDRH_PRE:
2796 case ARM::STRH_PRE: {
2797 unsigned Rt = MI->getOperand(0).getReg();
2798 unsigned Rm = MI->getOperand(3).getReg();
2799 if (!Rm)
2800 return 2;
2801 if (Rt == Rm)
2802 return 3;
2803 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub)
2804 ? 3 : 2;
2805 }
2806
2807 case ARM::LDR_POST_REG:
2808 case ARM::LDRB_POST_REG:
2809 case ARM::LDRH_POST: {
2810 unsigned Rt = MI->getOperand(0).getReg();
2811 unsigned Rm = MI->getOperand(3).getReg();
2812 return (Rt == Rm) ? 3 : 2;
2813 }
2814
2815 case ARM::LDR_PRE_IMM:
2816 case ARM::LDRB_PRE_IMM:
2817 case ARM::LDR_POST_IMM:
2818 case ARM::LDRB_POST_IMM:
2819 case ARM::STRB_POST_IMM:
2820 case ARM::STRB_POST_REG:
2821 case ARM::STRB_PRE_IMM:
2822 case ARM::STRH_POST:
2823 case ARM::STR_POST_IMM:
2824 case ARM::STR_POST_REG:
2825 case ARM::STR_PRE_IMM:
2826 return 2;
2827
2828 case ARM::LDRSB_PRE:
2829 case ARM::LDRSH_PRE: {
2830 unsigned Rm = MI->getOperand(3).getReg();
2831 if (Rm == 0)
2832 return 3;
2833 unsigned Rt = MI->getOperand(0).getReg();
2834 if (Rt == Rm)
2835 return 4;
2836 unsigned ShOpVal = MI->getOperand(4).getImm();
2837 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2838 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2839 if (!isSub &&
2840 (ShImm == 0 ||
2841 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2842 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2843 return 3;
2844 return 4;
2845 }
2846
2847 case ARM::LDRD: {
2848 unsigned Rt = MI->getOperand(0).getReg();
2849 unsigned Rn = MI->getOperand(2).getReg();
2850 unsigned Rm = MI->getOperand(3).getReg();
2851 if (Rm)
2852 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2853 return (Rt == Rn) ? 3 : 2;
2854 }
2855
2856 case ARM::STRD: {
2857 unsigned Rm = MI->getOperand(3).getReg();
2858 if (Rm)
2859 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2860 return 2;
2861 }
2862
2863 case ARM::LDRD_POST:
2864 case ARM::t2LDRD_POST:
2865 return 3;
2866
2867 case ARM::STRD_POST:
2868 case ARM::t2STRD_POST:
2869 return 4;
2870
2871 case ARM::LDRD_PRE: {
2872 unsigned Rt = MI->getOperand(0).getReg();
2873 unsigned Rn = MI->getOperand(3).getReg();
2874 unsigned Rm = MI->getOperand(4).getReg();
2875 if (Rm)
2876 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2877 return (Rt == Rn) ? 4 : 3;
2878 }
2879
2880 case ARM::t2LDRD_PRE: {
2881 unsigned Rt = MI->getOperand(0).getReg();
2882 unsigned Rn = MI->getOperand(3).getReg();
2883 return (Rt == Rn) ? 4 : 3;
2884 }
2885
2886 case ARM::STRD_PRE: {
2887 unsigned Rm = MI->getOperand(4).getReg();
2888 if (Rm)
2889 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2890 return 3;
2891 }
2892
2893 case ARM::t2STRD_PRE:
2894 return 3;
2895
2896 case ARM::t2LDR_POST:
2897 case ARM::t2LDRB_POST:
2898 case ARM::t2LDRB_PRE:
2899 case ARM::t2LDRSBi12:
2900 case ARM::t2LDRSBi8:
2901 case ARM::t2LDRSBpci:
2902 case ARM::t2LDRSBs:
2903 case ARM::t2LDRH_POST:
2904 case ARM::t2LDRH_PRE:
2905 case ARM::t2LDRSBT:
2906 case ARM::t2LDRSB_POST:
2907 case ARM::t2LDRSB_PRE:
2908 case ARM::t2LDRSH_POST:
2909 case ARM::t2LDRSH_PRE:
2910 case ARM::t2LDRSHi12:
2911 case ARM::t2LDRSHi8:
2912 case ARM::t2LDRSHpci:
2913 case ARM::t2LDRSHs:
2914 return 2;
2915
2916 case ARM::t2LDRDi8: {
2917 unsigned Rt = MI->getOperand(0).getReg();
2918 unsigned Rn = MI->getOperand(2).getReg();
2919 return (Rt == Rn) ? 3 : 2;
2920 }
2921
2922 case ARM::t2STRB_POST:
2923 case ARM::t2STRB_PRE:
2924 case ARM::t2STRBs:
2925 case ARM::t2STRDi8:
2926 case ARM::t2STRH_POST:
2927 case ARM::t2STRH_PRE:
2928 case ARM::t2STRHs:
2929 case ARM::t2STR_POST:
2930 case ARM::t2STR_PRE:
2931 case ARM::t2STRs:
2932 return 2;
2933 }
2934}
2935
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00002936// Return the number of 32-bit words loaded by LDM or stored by STM. If this
2937// can't be easily determined return 0 (missing MachineMemOperand).
2938//
2939// FIXME: The current MachineInstr design does not support relying on machine
2940// mem operands to determine the width of a memory access. Instead, we expect
2941// the target to provide this information based on the instruction opcode and
Robin Morisset039781e2014-08-29 21:53:01 +00002942// operands. However, using MachineMemOperand is the best solution now for
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00002943// two reasons:
2944//
2945// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
2946// operands. This is much more dangerous than using the MachineMemOperand
2947// sizes because CodeGen passes can insert/remove optional machine operands. In
2948// fact, it's totally incorrect for preRA passes and appears to be wrong for
2949// postRA passes as well.
2950//
2951// 2) getNumLDMAddresses is only used by the scheduling machine model and any
2952// machine model that calls this should handle the unknown (zero size) case.
2953//
2954// Long term, we should require a target hook that verifies MachineMemOperand
2955// sizes during MC lowering. That target hook should be local to MC lowering
2956// because we can't ensure that it is aware of other MI forms. Doing this will
2957// ensure that MachineMemOperands are correctly propagated through all passes.
2958unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr *MI) const {
2959 unsigned Size = 0;
2960 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
2961 E = MI->memoperands_end(); I != E; ++I) {
2962 Size += (*I)->getSize();
2963 }
2964 return Size / 4;
2965}
2966
Evan Cheng367a5df2010-09-09 18:18:55 +00002967unsigned
Evan Chengdebf9c52010-11-03 00:45:17 +00002968ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
2969 const MachineInstr *MI) const {
Evan Chengbf407072010-09-10 01:29:16 +00002970 if (!ItinData || ItinData->isEmpty())
Evan Cheng367a5df2010-09-09 18:18:55 +00002971 return 1;
2972
Evan Cheng6cc775f2011-06-28 19:10:37 +00002973 const MCInstrDesc &Desc = MI->getDesc();
Evan Cheng367a5df2010-09-09 18:18:55 +00002974 unsigned Class = Desc.getSchedClass();
Andrew Trickf161e392012-07-02 18:10:42 +00002975 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsone8a549c2012-09-29 21:43:49 +00002976 if (ItinUOps >= 0) {
2977 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
2978 return getNumMicroOpsSwiftLdSt(ItinData, MI);
2979
Andrew Trickf161e392012-07-02 18:10:42 +00002980 return ItinUOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002981 }
Evan Cheng367a5df2010-09-09 18:18:55 +00002982
2983 unsigned Opc = MI->getOpcode();
2984 switch (Opc) {
2985 default:
2986 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002987 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002988 case ARM::VSTMQIA:
Evan Cheng367a5df2010-09-09 18:18:55 +00002989 return 2;
2990
2991 // The number of uOps for load / store multiple are determined by the number
2992 // registers.
Andrew Trickc416ba62010-12-24 04:28:06 +00002993 //
Evan Chengbf407072010-09-10 01:29:16 +00002994 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
2995 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledru35521e22012-07-23 08:51:15 +00002996 // separately by assuming the address is not 64-bit aligned.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002997 //
Evan Chengbf407072010-09-10 01:29:16 +00002998 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002999 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
3000 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3001 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003002 case ARM::VLDMDIA_UPD:
3003 case ARM::VLDMDDB_UPD:
3004 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003005 case ARM::VLDMSIA_UPD:
3006 case ARM::VLDMSDB_UPD:
3007 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003008 case ARM::VSTMDIA_UPD:
3009 case ARM::VSTMDDB_UPD:
3010 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003011 case ARM::VSTMSIA_UPD:
3012 case ARM::VSTMSDB_UPD: {
Evan Cheng367a5df2010-09-09 18:18:55 +00003013 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
3014 return (NumRegs / 2) + (NumRegs % 2) + 1;
3015 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003016
3017 case ARM::LDMIA_RET:
3018 case ARM::LDMIA:
3019 case ARM::LDMDA:
3020 case ARM::LDMDB:
3021 case ARM::LDMIB:
3022 case ARM::LDMIA_UPD:
3023 case ARM::LDMDA_UPD:
3024 case ARM::LDMDB_UPD:
3025 case ARM::LDMIB_UPD:
3026 case ARM::STMIA:
3027 case ARM::STMDA:
3028 case ARM::STMDB:
3029 case ARM::STMIB:
3030 case ARM::STMIA_UPD:
3031 case ARM::STMDA_UPD:
3032 case ARM::STMDB_UPD:
3033 case ARM::STMIB_UPD:
3034 case ARM::tLDMIA:
3035 case ARM::tLDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003036 case ARM::tSTMIA_UPD:
Evan Cheng367a5df2010-09-09 18:18:55 +00003037 case ARM::tPOP_RET:
3038 case ARM::tPOP:
3039 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003040 case ARM::t2LDMIA_RET:
3041 case ARM::t2LDMIA:
3042 case ARM::t2LDMDB:
3043 case ARM::t2LDMIA_UPD:
3044 case ARM::t2LDMDB_UPD:
3045 case ARM::t2STMIA:
3046 case ARM::t2STMDB:
3047 case ARM::t2STMIA_UPD:
3048 case ARM::t2STMDB_UPD: {
Evan Chengbf407072010-09-10 01:29:16 +00003049 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003050 if (Subtarget.isSwift()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003051 int UOps = 1 + NumRegs; // One for address computation, one for each ld / st.
3052 switch (Opc) {
3053 default: break;
3054 case ARM::VLDMDIA_UPD:
3055 case ARM::VLDMDDB_UPD:
3056 case ARM::VLDMSIA_UPD:
3057 case ARM::VLDMSDB_UPD:
3058 case ARM::VSTMDIA_UPD:
3059 case ARM::VSTMDDB_UPD:
3060 case ARM::VSTMSIA_UPD:
3061 case ARM::VSTMSDB_UPD:
3062 case ARM::LDMIA_UPD:
3063 case ARM::LDMDA_UPD:
3064 case ARM::LDMDB_UPD:
3065 case ARM::LDMIB_UPD:
3066 case ARM::STMIA_UPD:
3067 case ARM::STMDA_UPD:
3068 case ARM::STMDB_UPD:
3069 case ARM::STMIB_UPD:
3070 case ARM::tLDMIA_UPD:
3071 case ARM::tSTMIA_UPD:
3072 case ARM::t2LDMIA_UPD:
3073 case ARM::t2LDMDB_UPD:
3074 case ARM::t2STMIA_UPD:
3075 case ARM::t2STMDB_UPD:
3076 ++UOps; // One for base register writeback.
3077 break;
3078 case ARM::LDMIA_RET:
3079 case ARM::tPOP_RET:
3080 case ARM::t2LDMIA_RET:
3081 UOps += 2; // One for base reg wb, one for write to pc.
3082 break;
3083 }
3084 return UOps;
Tim Northover0feb91e2014-04-01 14:10:07 +00003085 } else if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Chengdebf9c52010-11-03 00:45:17 +00003086 if (NumRegs < 4)
3087 return 2;
3088 // 4 registers would be issued: 2, 2.
3089 // 5 registers would be issued: 2, 2, 1.
Andrew Trickf161e392012-07-02 18:10:42 +00003090 int A8UOps = (NumRegs / 2);
Evan Chengdebf9c52010-11-03 00:45:17 +00003091 if (NumRegs % 2)
Andrew Trickf161e392012-07-02 18:10:42 +00003092 ++A8UOps;
3093 return A8UOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003094 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Andrew Trickf161e392012-07-02 18:10:42 +00003095 int A9UOps = (NumRegs / 2);
Evan Chengbf407072010-09-10 01:29:16 +00003096 // If there are odd number of registers or if it's not 64-bit aligned,
3097 // then it takes an extra AGU (Address Generation Unit) cycle.
3098 if ((NumRegs % 2) ||
3099 !MI->hasOneMemOperand() ||
3100 (*MI->memoperands_begin())->getAlignment() < 8)
Andrew Trickf161e392012-07-02 18:10:42 +00003101 ++A9UOps;
3102 return A9UOps;
Evan Chengbf407072010-09-10 01:29:16 +00003103 } else {
3104 // Assume the worst.
3105 return NumRegs;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00003106 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003107 }
3108 }
3109}
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003110
3111int
Evan Cheng412e37b2010-10-07 23:12:15 +00003112ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003113 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003114 unsigned DefClass,
3115 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003116 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003117 if (RegNo <= 0)
3118 // Def is the address writeback.
3119 return ItinData->getOperandCycle(DefClass, DefIdx);
3120
3121 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003122 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003123 // (regno / 2) + (regno % 2) + 1
3124 DefCycle = RegNo / 2 + 1;
3125 if (RegNo % 2)
3126 ++DefCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003127 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003128 DefCycle = RegNo;
3129 bool isSLoad = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003130
Evan Cheng6cc775f2011-06-28 19:10:37 +00003131 switch (DefMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003132 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003133 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003134 case ARM::VLDMSIA_UPD:
3135 case ARM::VLDMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003136 isSLoad = true;
3137 break;
3138 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003139
Evan Cheng412e37b2010-10-07 23:12:15 +00003140 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3141 // then it takes an extra cycle.
3142 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3143 ++DefCycle;
3144 } else {
3145 // Assume the worst.
3146 DefCycle = RegNo + 2;
3147 }
3148
3149 return DefCycle;
3150}
3151
3152int
3153ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003154 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003155 unsigned DefClass,
3156 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003157 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003158 if (RegNo <= 0)
3159 // Def is the address writeback.
3160 return ItinData->getOperandCycle(DefClass, DefIdx);
3161
3162 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003163 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003164 // 4 registers would be issued: 1, 2, 1.
3165 // 5 registers would be issued: 1, 2, 2.
3166 DefCycle = RegNo / 2;
3167 if (DefCycle < 1)
3168 DefCycle = 1;
3169 // Result latency is issue cycle + 2: E2.
3170 DefCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003171 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003172 DefCycle = (RegNo / 2);
3173 // If there are odd number of registers or if it's not 64-bit aligned,
3174 // then it takes an extra AGU (Address Generation Unit) cycle.
3175 if ((RegNo % 2) || DefAlign < 8)
3176 ++DefCycle;
3177 // Result latency is AGU cycles + 2.
3178 DefCycle += 2;
3179 } else {
3180 // Assume the worst.
3181 DefCycle = RegNo + 2;
3182 }
3183
3184 return DefCycle;
3185}
3186
3187int
3188ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003189 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003190 unsigned UseClass,
3191 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003192 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003193 if (RegNo <= 0)
3194 return ItinData->getOperandCycle(UseClass, UseIdx);
3195
3196 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003197 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003198 // (regno / 2) + (regno % 2) + 1
3199 UseCycle = RegNo / 2 + 1;
3200 if (RegNo % 2)
3201 ++UseCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003202 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003203 UseCycle = RegNo;
3204 bool isSStore = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003205
Evan Cheng6cc775f2011-06-28 19:10:37 +00003206 switch (UseMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003207 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003208 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003209 case ARM::VSTMSIA_UPD:
3210 case ARM::VSTMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003211 isSStore = true;
3212 break;
3213 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003214
Evan Cheng412e37b2010-10-07 23:12:15 +00003215 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3216 // then it takes an extra cycle.
3217 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3218 ++UseCycle;
3219 } else {
3220 // Assume the worst.
3221 UseCycle = RegNo + 2;
3222 }
3223
3224 return UseCycle;
3225}
3226
3227int
3228ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003229 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003230 unsigned UseClass,
3231 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003232 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003233 if (RegNo <= 0)
3234 return ItinData->getOperandCycle(UseClass, UseIdx);
3235
3236 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003237 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003238 UseCycle = RegNo / 2;
3239 if (UseCycle < 2)
3240 UseCycle = 2;
3241 // Read in E3.
3242 UseCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003243 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003244 UseCycle = (RegNo / 2);
3245 // If there are odd number of registers or if it's not 64-bit aligned,
3246 // then it takes an extra AGU (Address Generation Unit) cycle.
3247 if ((RegNo % 2) || UseAlign < 8)
3248 ++UseCycle;
3249 } else {
3250 // Assume the worst.
3251 UseCycle = 1;
3252 }
3253 return UseCycle;
3254}
3255
3256int
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003257ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003258 const MCInstrDesc &DefMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003259 unsigned DefIdx, unsigned DefAlign,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003260 const MCInstrDesc &UseMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003261 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003262 unsigned DefClass = DefMCID.getSchedClass();
3263 unsigned UseClass = UseMCID.getSchedClass();
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003264
Evan Cheng6cc775f2011-06-28 19:10:37 +00003265 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003266 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3267
3268 // This may be a def / use of a variable_ops instruction, the operand
3269 // latency might be determinable dynamically. Let the target try to
3270 // figure it out.
Evan Chenge2c211c2010-10-28 02:00:25 +00003271 int DefCycle = -1;
Evan Chengff310732010-10-28 06:47:08 +00003272 bool LdmBypass = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003273 switch (DefMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003274 default:
3275 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3276 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003277
3278 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003279 case ARM::VLDMDIA_UPD:
3280 case ARM::VLDMDDB_UPD:
3281 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003282 case ARM::VLDMSIA_UPD:
3283 case ARM::VLDMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003284 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003285 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003286
3287 case ARM::LDMIA_RET:
3288 case ARM::LDMIA:
3289 case ARM::LDMDA:
3290 case ARM::LDMDB:
3291 case ARM::LDMIB:
3292 case ARM::LDMIA_UPD:
3293 case ARM::LDMDA_UPD:
3294 case ARM::LDMDB_UPD:
3295 case ARM::LDMIB_UPD:
3296 case ARM::tLDMIA:
3297 case ARM::tLDMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003298 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003299 case ARM::t2LDMIA_RET:
3300 case ARM::t2LDMIA:
3301 case ARM::t2LDMDB:
3302 case ARM::t2LDMIA_UPD:
3303 case ARM::t2LDMDB_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003304 LdmBypass = 1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003305 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng412e37b2010-10-07 23:12:15 +00003306 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003307 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003308
3309 if (DefCycle == -1)
3310 // We can't seem to determine the result latency of the def, assume it's 2.
3311 DefCycle = 2;
3312
3313 int UseCycle = -1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003314 switch (UseMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003315 default:
3316 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3317 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003318
3319 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003320 case ARM::VSTMDIA_UPD:
3321 case ARM::VSTMDDB_UPD:
3322 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003323 case ARM::VSTMSIA_UPD:
3324 case ARM::VSTMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003325 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003326 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003327
3328 case ARM::STMIA:
3329 case ARM::STMDA:
3330 case ARM::STMDB:
3331 case ARM::STMIB:
3332 case ARM::STMIA_UPD:
3333 case ARM::STMDA_UPD:
3334 case ARM::STMDB_UPD:
3335 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003336 case ARM::tSTMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003337 case ARM::tPOP_RET:
3338 case ARM::tPOP:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003339 case ARM::t2STMIA:
3340 case ARM::t2STMDB:
3341 case ARM::t2STMIA_UPD:
3342 case ARM::t2STMDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003343 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003344 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003345 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003346
3347 if (UseCycle == -1)
3348 // Assume it's read in the first stage.
3349 UseCycle = 1;
3350
3351 UseCycle = DefCycle - UseCycle + 1;
3352 if (UseCycle > 0) {
3353 if (LdmBypass) {
3354 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3355 // first def operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003356 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003357 UseClass, UseIdx))
3358 --UseCycle;
3359 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003360 UseClass, UseIdx)) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003361 --UseCycle;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003362 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003363 }
3364
3365 return UseCycle;
3366}
3367
Evan Cheng7fae11b2011-12-14 02:11:42 +00003368static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003369 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003370 unsigned &DefIdx, unsigned &Dist) {
3371 Dist = 0;
3372
3373 MachineBasicBlock::const_iterator I = MI; ++I;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003374 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
Evan Cheng7fae11b2011-12-14 02:11:42 +00003375 assert(II->isInsideBundle() && "Empty bundle?");
3376
3377 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003378 while (II->isInsideBundle()) {
3379 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3380 if (Idx != -1)
3381 break;
3382 --II;
3383 ++Dist;
3384 }
3385
3386 assert(Idx != -1 && "Cannot find bundled definition!");
3387 DefIdx = Idx;
3388 return II;
3389}
3390
3391static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003392 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003393 unsigned &UseIdx, unsigned &Dist) {
3394 Dist = 0;
3395
3396 MachineBasicBlock::const_instr_iterator II = MI; ++II;
3397 assert(II->isInsideBundle() && "Empty bundle?");
3398 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3399
3400 // FIXME: This doesn't properly handle multiple uses.
3401 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003402 while (II != E && II->isInsideBundle()) {
3403 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3404 if (Idx != -1)
3405 break;
3406 if (II->getOpcode() != ARM::t2IT)
3407 ++Dist;
3408 ++II;
3409 }
3410
Evan Chengda103bf2011-12-14 20:00:08 +00003411 if (Idx == -1) {
3412 Dist = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003413 return nullptr;
Evan Chengda103bf2011-12-14 20:00:08 +00003414 }
3415
Evan Cheng7fae11b2011-12-14 02:11:42 +00003416 UseIdx = Idx;
3417 return II;
3418}
3419
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003420/// Return the number of cycles to add to (or subtract from) the static
3421/// itinerary based on the def opcode and alignment. The caller will ensure that
3422/// adjusted latency is at least one cycle.
3423static int adjustDefLatency(const ARMSubtarget &Subtarget,
3424 const MachineInstr *DefMI,
3425 const MCInstrDesc *DefMCID, unsigned DefAlign) {
3426 int Adjust = 0;
Tim Northover0feb91e2014-04-01 14:10:07 +00003427 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
Evan Chengff310732010-10-28 06:47:08 +00003428 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3429 // variants are one cycle cheaper.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003430 switch (DefMCID->getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003431 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003432 case ARM::LDRrs:
3433 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003434 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3435 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3436 if (ShImm == 0 ||
3437 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003438 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003439 break;
3440 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003441 case ARM::t2LDRs:
3442 case ARM::t2LDRBs:
3443 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003444 case ARM::t2LDRSHs: {
3445 // Thumb2 mode: lsl only.
3446 unsigned ShAmt = DefMI->getOperand(3).getImm();
3447 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003448 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003449 break;
3450 }
3451 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003452 } else if (Subtarget.isSwift()) {
3453 // FIXME: Properly handle all of the latency adjustments for address
3454 // writeback.
3455 switch (DefMCID->getOpcode()) {
3456 default: break;
3457 case ARM::LDRrs:
3458 case ARM::LDRBrs: {
3459 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3460 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3461 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3462 if (!isSub &&
3463 (ShImm == 0 ||
3464 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3465 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3466 Adjust -= 2;
3467 else if (!isSub &&
3468 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3469 --Adjust;
3470 break;
3471 }
3472 case ARM::t2LDRs:
3473 case ARM::t2LDRBs:
3474 case ARM::t2LDRHs:
3475 case ARM::t2LDRSHs: {
3476 // Thumb2 mode: lsl only.
3477 unsigned ShAmt = DefMI->getOperand(3).getImm();
3478 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3479 Adjust -= 2;
3480 break;
3481 }
3482 }
Evan Chengff310732010-10-28 06:47:08 +00003483 }
3484
Silviu Barangab47bb942012-09-13 15:05:10 +00003485 if (DefAlign < 8 && Subtarget.isLikeA9()) {
Evan Cheng7fae11b2011-12-14 02:11:42 +00003486 switch (DefMCID->getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003487 default: break;
3488 case ARM::VLD1q8:
3489 case ARM::VLD1q16:
3490 case ARM::VLD1q32:
3491 case ARM::VLD1q64:
Jim Grosbach2098cb12011-10-24 21:45:13 +00003492 case ARM::VLD1q8wb_fixed:
3493 case ARM::VLD1q16wb_fixed:
3494 case ARM::VLD1q32wb_fixed:
3495 case ARM::VLD1q64wb_fixed:
3496 case ARM::VLD1q8wb_register:
3497 case ARM::VLD1q16wb_register:
3498 case ARM::VLD1q32wb_register:
3499 case ARM::VLD1q64wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003500 case ARM::VLD2d8:
3501 case ARM::VLD2d16:
3502 case ARM::VLD2d32:
3503 case ARM::VLD2q8:
3504 case ARM::VLD2q16:
3505 case ARM::VLD2q32:
Jim Grosbachd146a022011-12-09 21:28:25 +00003506 case ARM::VLD2d8wb_fixed:
3507 case ARM::VLD2d16wb_fixed:
3508 case ARM::VLD2d32wb_fixed:
3509 case ARM::VLD2q8wb_fixed:
3510 case ARM::VLD2q16wb_fixed:
3511 case ARM::VLD2q32wb_fixed:
3512 case ARM::VLD2d8wb_register:
3513 case ARM::VLD2d16wb_register:
3514 case ARM::VLD2d32wb_register:
3515 case ARM::VLD2q8wb_register:
3516 case ARM::VLD2q16wb_register:
3517 case ARM::VLD2q32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003518 case ARM::VLD3d8:
3519 case ARM::VLD3d16:
3520 case ARM::VLD3d32:
3521 case ARM::VLD1d64T:
3522 case ARM::VLD3d8_UPD:
3523 case ARM::VLD3d16_UPD:
3524 case ARM::VLD3d32_UPD:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00003525 case ARM::VLD1d64Twb_fixed:
3526 case ARM::VLD1d64Twb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003527 case ARM::VLD3q8_UPD:
3528 case ARM::VLD3q16_UPD:
3529 case ARM::VLD3q32_UPD:
3530 case ARM::VLD4d8:
3531 case ARM::VLD4d16:
3532 case ARM::VLD4d32:
3533 case ARM::VLD1d64Q:
3534 case ARM::VLD4d8_UPD:
3535 case ARM::VLD4d16_UPD:
3536 case ARM::VLD4d32_UPD:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00003537 case ARM::VLD1d64Qwb_fixed:
3538 case ARM::VLD1d64Qwb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003539 case ARM::VLD4q8_UPD:
3540 case ARM::VLD4q16_UPD:
3541 case ARM::VLD4q32_UPD:
3542 case ARM::VLD1DUPq8:
3543 case ARM::VLD1DUPq16:
3544 case ARM::VLD1DUPq32:
Jim Grosbacha68c9a82011-11-30 19:35:44 +00003545 case ARM::VLD1DUPq8wb_fixed:
3546 case ARM::VLD1DUPq16wb_fixed:
3547 case ARM::VLD1DUPq32wb_fixed:
3548 case ARM::VLD1DUPq8wb_register:
3549 case ARM::VLD1DUPq16wb_register:
3550 case ARM::VLD1DUPq32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003551 case ARM::VLD2DUPd8:
3552 case ARM::VLD2DUPd16:
3553 case ARM::VLD2DUPd32:
Jim Grosbachc80a2642011-12-21 19:40:55 +00003554 case ARM::VLD2DUPd8wb_fixed:
3555 case ARM::VLD2DUPd16wb_fixed:
3556 case ARM::VLD2DUPd32wb_fixed:
3557 case ARM::VLD2DUPd8wb_register:
3558 case ARM::VLD2DUPd16wb_register:
3559 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003560 case ARM::VLD4DUPd8:
3561 case ARM::VLD4DUPd16:
3562 case ARM::VLD4DUPd32:
3563 case ARM::VLD4DUPd8_UPD:
3564 case ARM::VLD4DUPd16_UPD:
3565 case ARM::VLD4DUPd32_UPD:
3566 case ARM::VLD1LNd8:
3567 case ARM::VLD1LNd16:
3568 case ARM::VLD1LNd32:
3569 case ARM::VLD1LNd8_UPD:
3570 case ARM::VLD1LNd16_UPD:
3571 case ARM::VLD1LNd32_UPD:
3572 case ARM::VLD2LNd8:
3573 case ARM::VLD2LNd16:
3574 case ARM::VLD2LNd32:
3575 case ARM::VLD2LNq16:
3576 case ARM::VLD2LNq32:
3577 case ARM::VLD2LNd8_UPD:
3578 case ARM::VLD2LNd16_UPD:
3579 case ARM::VLD2LNd32_UPD:
3580 case ARM::VLD2LNq16_UPD:
3581 case ARM::VLD2LNq32_UPD:
3582 case ARM::VLD4LNd8:
3583 case ARM::VLD4LNd16:
3584 case ARM::VLD4LNd32:
3585 case ARM::VLD4LNq16:
3586 case ARM::VLD4LNq32:
3587 case ARM::VLD4LNd8_UPD:
3588 case ARM::VLD4LNd16_UPD:
3589 case ARM::VLD4LNd32_UPD:
3590 case ARM::VLD4LNq16_UPD:
3591 case ARM::VLD4LNq32_UPD:
3592 // If the address is not 64-bit aligned, the latencies of these
3593 // instructions increases by one.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003594 ++Adjust;
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003595 break;
3596 }
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003597 }
3598 return Adjust;
3599}
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003600
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003601
3602
3603int
3604ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3605 const MachineInstr *DefMI, unsigned DefIdx,
3606 const MachineInstr *UseMI,
3607 unsigned UseIdx) const {
3608 // No operand latency. The caller may fall back to getInstrLatency.
3609 if (!ItinData || ItinData->isEmpty())
3610 return -1;
3611
3612 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
3613 unsigned Reg = DefMO.getReg();
3614 const MCInstrDesc *DefMCID = &DefMI->getDesc();
3615 const MCInstrDesc *UseMCID = &UseMI->getDesc();
3616
3617 unsigned DefAdj = 0;
3618 if (DefMI->isBundle()) {
3619 DefMI = getBundledDefMI(&getRegisterInfo(), DefMI, Reg, DefIdx, DefAdj);
3620 DefMCID = &DefMI->getDesc();
3621 }
3622 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
3623 DefMI->isRegSequence() || DefMI->isImplicitDef()) {
3624 return 1;
3625 }
3626
3627 unsigned UseAdj = 0;
3628 if (UseMI->isBundle()) {
3629 unsigned NewUseIdx;
3630 const MachineInstr *NewUseMI = getBundledUseMI(&getRegisterInfo(), UseMI,
3631 Reg, NewUseIdx, UseAdj);
Andrew Trick77d0b882012-06-22 02:50:33 +00003632 if (!NewUseMI)
3633 return -1;
3634
3635 UseMI = NewUseMI;
3636 UseIdx = NewUseIdx;
3637 UseMCID = &UseMI->getDesc();
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003638 }
3639
3640 if (Reg == ARM::CPSR) {
3641 if (DefMI->getOpcode() == ARM::FMSTAT) {
3642 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Barangab47bb942012-09-13 15:05:10 +00003643 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003644 }
3645
3646 // CPSR set and branch can be paired in the same cycle.
3647 if (UseMI->isBranch())
3648 return 0;
3649
3650 // Otherwise it takes the instruction latency (generally one).
3651 unsigned Latency = getInstrLatency(ItinData, DefMI);
3652
3653 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3654 // its uses. Instructions which are otherwise scheduled between them may
3655 // incur a code size penalty (not able to use the CPSR setting 16-bit
3656 // instructions).
3657 if (Latency > 0 && Subtarget.isThumb2()) {
3658 const MachineFunction *MF = DefMI->getParent()->getParent();
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00003659 if (MF->getFunction()->hasFnAttribute(Attribute::OptimizeForSize))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003660 --Latency;
3661 }
3662 return Latency;
3663 }
3664
Andrew Trick77d0b882012-06-22 02:50:33 +00003665 if (DefMO.isImplicit() || UseMI->getOperand(UseIdx).isImplicit())
3666 return -1;
3667
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003668 unsigned DefAlign = DefMI->hasOneMemOperand()
3669 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
3670 unsigned UseAlign = UseMI->hasOneMemOperand()
3671 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
3672
3673 // Get the itinerary's latency if possible, and handle variable_ops.
3674 int Latency = getOperandLatency(ItinData, *DefMCID, DefIdx, DefAlign,
3675 *UseMCID, UseIdx, UseAlign);
3676 // Unable to find operand latency. The caller may resort to getInstrLatency.
3677 if (Latency < 0)
3678 return Latency;
3679
3680 // Adjust for IT block position.
3681 int Adj = DefAdj + UseAdj;
3682
3683 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3684 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
3685 if (Adj >= 0 || (int)Latency > -Adj) {
3686 return Latency + Adj;
3687 }
3688 // Return the itinerary latency, which may be zero but not less than zero.
Evan Chengff310732010-10-28 06:47:08 +00003689 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003690}
3691
3692int
3693ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3694 SDNode *DefNode, unsigned DefIdx,
3695 SDNode *UseNode, unsigned UseIdx) const {
3696 if (!DefNode->isMachineOpcode())
3697 return 1;
3698
Evan Cheng6cc775f2011-06-28 19:10:37 +00003699 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trick47ff14b2011-01-21 05:51:33 +00003700
Evan Cheng6cc775f2011-06-28 19:10:37 +00003701 if (isZeroCost(DefMCID.Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +00003702 return 0;
3703
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003704 if (!ItinData || ItinData->isEmpty())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003705 return DefMCID.mayLoad() ? 3 : 1;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003706
Evan Cheng6c1414f2010-10-29 18:09:28 +00003707 if (!UseNode->isMachineOpcode()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003708 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003709 if (Subtarget.isLikeA9() || Subtarget.isSwift())
Evan Cheng6c1414f2010-10-29 18:09:28 +00003710 return Latency <= 2 ? 1 : Latency - 1;
3711 else
3712 return Latency <= 3 ? 1 : Latency - 2;
3713 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003714
Evan Cheng6cc775f2011-06-28 19:10:37 +00003715 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003716 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
3717 unsigned DefAlign = !DefMN->memoperands_empty()
3718 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
3719 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
3720 unsigned UseAlign = !UseMN->memoperands_empty()
3721 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003722 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
3723 UseMCID, UseIdx, UseAlign);
Evan Chengff310732010-10-28 06:47:08 +00003724
3725 if (Latency > 1 &&
Tim Northover0feb91e2014-04-01 14:10:07 +00003726 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
3727 Subtarget.isCortexA7())) {
Evan Chengff310732010-10-28 06:47:08 +00003728 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3729 // variants are one cycle cheaper.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003730 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003731 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003732 case ARM::LDRrs:
3733 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003734 unsigned ShOpVal =
3735 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3736 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3737 if (ShImm == 0 ||
3738 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3739 --Latency;
3740 break;
3741 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003742 case ARM::t2LDRs:
3743 case ARM::t2LDRBs:
3744 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003745 case ARM::t2LDRSHs: {
3746 // Thumb2 mode: lsl only.
3747 unsigned ShAmt =
3748 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3749 if (ShAmt == 0 || ShAmt == 2)
3750 --Latency;
3751 break;
3752 }
3753 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003754 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
3755 // FIXME: Properly handle all of the latency adjustments for address
3756 // writeback.
3757 switch (DefMCID.getOpcode()) {
3758 default: break;
3759 case ARM::LDRrs:
3760 case ARM::LDRBrs: {
3761 unsigned ShOpVal =
3762 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3763 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3764 if (ShImm == 0 ||
3765 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3766 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3767 Latency -= 2;
3768 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3769 --Latency;
3770 break;
3771 }
3772 case ARM::t2LDRs:
3773 case ARM::t2LDRBs:
3774 case ARM::t2LDRHs:
3775 case ARM::t2LDRSHs: {
3776 // Thumb2 mode: lsl 0-3 only.
3777 Latency -= 2;
3778 break;
3779 }
3780 }
Evan Chengff310732010-10-28 06:47:08 +00003781 }
3782
Silviu Barangab47bb942012-09-13 15:05:10 +00003783 if (DefAlign < 8 && Subtarget.isLikeA9())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003784 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003785 default: break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003786 case ARM::VLD1q8:
3787 case ARM::VLD1q16:
3788 case ARM::VLD1q32:
3789 case ARM::VLD1q64:
3790 case ARM::VLD1q8wb_register:
3791 case ARM::VLD1q16wb_register:
3792 case ARM::VLD1q32wb_register:
3793 case ARM::VLD1q64wb_register:
3794 case ARM::VLD1q8wb_fixed:
3795 case ARM::VLD1q16wb_fixed:
3796 case ARM::VLD1q32wb_fixed:
3797 case ARM::VLD1q64wb_fixed:
3798 case ARM::VLD2d8:
3799 case ARM::VLD2d16:
3800 case ARM::VLD2d32:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003801 case ARM::VLD2q8Pseudo:
3802 case ARM::VLD2q16Pseudo:
3803 case ARM::VLD2q32Pseudo:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003804 case ARM::VLD2d8wb_fixed:
3805 case ARM::VLD2d16wb_fixed:
3806 case ARM::VLD2d32wb_fixed:
Jim Grosbachd146a022011-12-09 21:28:25 +00003807 case ARM::VLD2q8PseudoWB_fixed:
3808 case ARM::VLD2q16PseudoWB_fixed:
3809 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003810 case ARM::VLD2d8wb_register:
3811 case ARM::VLD2d16wb_register:
3812 case ARM::VLD2d32wb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00003813 case ARM::VLD2q8PseudoWB_register:
3814 case ARM::VLD2q16PseudoWB_register:
3815 case ARM::VLD2q32PseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003816 case ARM::VLD3d8Pseudo:
3817 case ARM::VLD3d16Pseudo:
3818 case ARM::VLD3d32Pseudo:
3819 case ARM::VLD1d64TPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003820 case ARM::VLD1d64TPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003821 case ARM::VLD3d8Pseudo_UPD:
3822 case ARM::VLD3d16Pseudo_UPD:
3823 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003824 case ARM::VLD3q8Pseudo_UPD:
3825 case ARM::VLD3q16Pseudo_UPD:
3826 case ARM::VLD3q32Pseudo_UPD:
3827 case ARM::VLD3q8oddPseudo:
3828 case ARM::VLD3q16oddPseudo:
3829 case ARM::VLD3q32oddPseudo:
3830 case ARM::VLD3q8oddPseudo_UPD:
3831 case ARM::VLD3q16oddPseudo_UPD:
3832 case ARM::VLD3q32oddPseudo_UPD:
3833 case ARM::VLD4d8Pseudo:
3834 case ARM::VLD4d16Pseudo:
3835 case ARM::VLD4d32Pseudo:
3836 case ARM::VLD1d64QPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003837 case ARM::VLD1d64QPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003838 case ARM::VLD4d8Pseudo_UPD:
3839 case ARM::VLD4d16Pseudo_UPD:
3840 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003841 case ARM::VLD4q8Pseudo_UPD:
3842 case ARM::VLD4q16Pseudo_UPD:
3843 case ARM::VLD4q32Pseudo_UPD:
3844 case ARM::VLD4q8oddPseudo:
3845 case ARM::VLD4q16oddPseudo:
3846 case ARM::VLD4q32oddPseudo:
3847 case ARM::VLD4q8oddPseudo_UPD:
3848 case ARM::VLD4q16oddPseudo_UPD:
3849 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbach13a292c2012-03-06 22:01:44 +00003850 case ARM::VLD1DUPq8:
3851 case ARM::VLD1DUPq16:
3852 case ARM::VLD1DUPq32:
3853 case ARM::VLD1DUPq8wb_fixed:
3854 case ARM::VLD1DUPq16wb_fixed:
3855 case ARM::VLD1DUPq32wb_fixed:
3856 case ARM::VLD1DUPq8wb_register:
3857 case ARM::VLD1DUPq16wb_register:
3858 case ARM::VLD1DUPq32wb_register:
3859 case ARM::VLD2DUPd8:
3860 case ARM::VLD2DUPd16:
3861 case ARM::VLD2DUPd32:
3862 case ARM::VLD2DUPd8wb_fixed:
3863 case ARM::VLD2DUPd16wb_fixed:
3864 case ARM::VLD2DUPd32wb_fixed:
3865 case ARM::VLD2DUPd8wb_register:
3866 case ARM::VLD2DUPd16wb_register:
3867 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003868 case ARM::VLD4DUPd8Pseudo:
3869 case ARM::VLD4DUPd16Pseudo:
3870 case ARM::VLD4DUPd32Pseudo:
3871 case ARM::VLD4DUPd8Pseudo_UPD:
3872 case ARM::VLD4DUPd16Pseudo_UPD:
3873 case ARM::VLD4DUPd32Pseudo_UPD:
3874 case ARM::VLD1LNq8Pseudo:
3875 case ARM::VLD1LNq16Pseudo:
3876 case ARM::VLD1LNq32Pseudo:
3877 case ARM::VLD1LNq8Pseudo_UPD:
3878 case ARM::VLD1LNq16Pseudo_UPD:
3879 case ARM::VLD1LNq32Pseudo_UPD:
3880 case ARM::VLD2LNd8Pseudo:
3881 case ARM::VLD2LNd16Pseudo:
3882 case ARM::VLD2LNd32Pseudo:
3883 case ARM::VLD2LNq16Pseudo:
3884 case ARM::VLD2LNq32Pseudo:
3885 case ARM::VLD2LNd8Pseudo_UPD:
3886 case ARM::VLD2LNd16Pseudo_UPD:
3887 case ARM::VLD2LNd32Pseudo_UPD:
3888 case ARM::VLD2LNq16Pseudo_UPD:
3889 case ARM::VLD2LNq32Pseudo_UPD:
3890 case ARM::VLD4LNd8Pseudo:
3891 case ARM::VLD4LNd16Pseudo:
3892 case ARM::VLD4LNd32Pseudo:
3893 case ARM::VLD4LNq16Pseudo:
3894 case ARM::VLD4LNq32Pseudo:
3895 case ARM::VLD4LNd8Pseudo_UPD:
3896 case ARM::VLD4LNd16Pseudo_UPD:
3897 case ARM::VLD4LNd32Pseudo_UPD:
3898 case ARM::VLD4LNq16Pseudo_UPD:
3899 case ARM::VLD4LNq32Pseudo_UPD:
3900 // If the address is not 64-bit aligned, the latencies of these
3901 // instructions increases by one.
3902 ++Latency;
3903 break;
3904 }
3905
Evan Chengff310732010-10-28 06:47:08 +00003906 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003907}
Evan Cheng63c76082010-10-19 18:58:51 +00003908
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00003909unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr *MI) const {
3910 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3911 MI->isRegSequence() || MI->isImplicitDef())
3912 return 0;
3913
3914 if (MI->isBundle())
3915 return 0;
3916
3917 const MCInstrDesc &MCID = MI->getDesc();
3918
3919 if (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR)) {
3920 // When predicated, CPSR is an additional source operand for CPSR updating
3921 // instructions, this apparently increases their latencies.
3922 return 1;
3923 }
3924 return 0;
3925}
3926
Andrew Trick45446062012-06-05 21:11:27 +00003927unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3928 const MachineInstr *MI,
3929 unsigned *PredCost) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00003930 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3931 MI->isRegSequence() || MI->isImplicitDef())
3932 return 1;
3933
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003934 // An instruction scheduler typically runs on unbundled instructions, however
3935 // other passes may query the latency of a bundled instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003936 if (MI->isBundle()) {
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003937 unsigned Latency = 0;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003938 MachineBasicBlock::const_instr_iterator I = MI;
3939 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3940 while (++I != E && I->isInsideBundle()) {
3941 if (I->getOpcode() != ARM::t2IT)
3942 Latency += getInstrLatency(ItinData, I, PredCost);
3943 }
3944 return Latency;
3945 }
3946
Evan Cheng6cc775f2011-06-28 19:10:37 +00003947 const MCInstrDesc &MCID = MI->getDesc();
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003948 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) {
Evan Chengdebf9c52010-11-03 00:45:17 +00003949 // When predicated, CPSR is an additional source operand for CPSR updating
3950 // instructions, this apparently increases their latencies.
3951 *PredCost = 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003952 }
3953 // Be sure to call getStageLatency for an empty itinerary in case it has a
3954 // valid MinLatency property.
3955 if (!ItinData)
3956 return MI->mayLoad() ? 3 : 1;
3957
3958 unsigned Class = MCID.getSchedClass();
3959
3960 // For instructions with variable uops, use uops as latency.
Andrew Trick21cca972012-07-02 19:12:29 +00003961 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003962 return getNumMicroOps(ItinData, MI);
Andrew Trick21cca972012-07-02 19:12:29 +00003963
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003964 // For the common case, fall back on the itinerary's latency.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003965 unsigned Latency = ItinData->getStageLatency(Class);
3966
3967 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3968 unsigned DefAlign = MI->hasOneMemOperand()
3969 ? (*MI->memoperands_begin())->getAlignment() : 0;
3970 int Adj = adjustDefLatency(Subtarget, MI, &MCID, DefAlign);
3971 if (Adj >= 0 || (int)Latency > -Adj) {
3972 return Latency + Adj;
3973 }
3974 return Latency;
Evan Chengdebf9c52010-11-03 00:45:17 +00003975}
3976
3977int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3978 SDNode *Node) const {
3979 if (!Node->isMachineOpcode())
3980 return 1;
3981
3982 if (!ItinData || ItinData->isEmpty())
3983 return 1;
3984
3985 unsigned Opcode = Node->getMachineOpcode();
3986 switch (Opcode) {
3987 default:
3988 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003989 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003990 case ARM::VSTMQIA:
Evan Chengdebf9c52010-11-03 00:45:17 +00003991 return 2;
Eric Christopherb006fc92010-11-18 19:40:05 +00003992 }
Evan Chengdebf9c52010-11-03 00:45:17 +00003993}
3994
Evan Cheng63c76082010-10-19 18:58:51 +00003995bool ARMBaseInstrInfo::
3996hasHighOperandLatency(const InstrItineraryData *ItinData,
3997 const MachineRegisterInfo *MRI,
3998 const MachineInstr *DefMI, unsigned DefIdx,
3999 const MachineInstr *UseMI, unsigned UseIdx) const {
4000 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
4001 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
4002 if (Subtarget.isCortexA8() &&
4003 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
4004 // CortexA8 VFP instructions are not pipelined.
4005 return true;
4006
4007 // Hoist VFP / NEON instructions with 4 or higher latency.
Andrew Trickde2109e2013-06-15 04:49:57 +00004008 int Latency = computeOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
Andrew Trick3564bdf2012-06-07 19:41:58 +00004009 if (Latency < 0)
4010 Latency = getInstrLatency(ItinData, DefMI);
Evan Cheng63c76082010-10-19 18:58:51 +00004011 if (Latency <= 3)
4012 return false;
4013 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4014 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4015}
Evan Chenge96b8d72010-10-26 02:08:50 +00004016
4017bool ARMBaseInstrInfo::
4018hasLowDefLatency(const InstrItineraryData *ItinData,
4019 const MachineInstr *DefMI, unsigned DefIdx) const {
4020 if (!ItinData || ItinData->isEmpty())
4021 return false;
4022
4023 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
4024 if (DDomain == ARMII::DomainGeneral) {
4025 unsigned DefClass = DefMI->getDesc().getSchedClass();
4026 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4027 return (DefCycle != -1 && DefCycle <= 2);
4028 }
4029 return false;
4030}
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004031
Andrew Trick924123a2011-09-21 02:20:46 +00004032bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr *MI,
4033 StringRef &ErrInfo) const {
4034 if (convertAddSubFlagsOpcode(MI->getOpcode())) {
4035 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4036 return false;
4037 }
4038 return true;
4039}
4040
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004041// LoadStackGuard has so far only been implemented for MachO. Different code
4042// sequence is needed for other targets.
4043void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4044 unsigned LoadImmOpc,
4045 unsigned LoadOpc,
4046 Reloc::Model RM) const {
4047 MachineBasicBlock &MBB = *MI->getParent();
4048 DebugLoc DL = MI->getDebugLoc();
4049 unsigned Reg = MI->getOperand(0).getReg();
4050 const GlobalValue *GV =
4051 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4052 MachineInstrBuilder MIB;
4053
4054 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4055 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4056
4057 if (Subtarget.GVIsIndirectSymbol(GV, RM)) {
4058 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4059 MIB.addReg(Reg, RegState::Kill).addImm(0);
4060 unsigned Flag = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant;
4061 MachineMemOperand *MMO = MBB.getParent()->
4062 getMachineMemOperand(MachinePointerInfo::getGOT(), Flag, 4, 4);
4063 MIB.addMemOperand(MMO);
4064 AddDefaultPred(MIB);
4065 }
4066
4067 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4068 MIB.addReg(Reg, RegState::Kill).addImm(0);
4069 MIB.setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
4070 AddDefaultPred(MIB);
4071}
4072
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004073bool
4074ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4075 unsigned &AddSubOpc,
4076 bool &NegAcc, bool &HasLane) const {
4077 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4078 if (I == MLxEntryMap.end())
4079 return false;
4080
4081 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4082 MulOpc = Entry.MulOpc;
4083 AddSubOpc = Entry.AddSubOpc;
4084 NegAcc = Entry.NegAcc;
4085 HasLane = Entry.HasLane;
4086 return true;
4087}
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004088
4089//===----------------------------------------------------------------------===//
4090// Execution domains.
4091//===----------------------------------------------------------------------===//
4092//
4093// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4094// and some can go down both. The vmov instructions go down the VFP pipeline,
4095// but they can be changed to vorr equivalents that are executed by the NEON
4096// pipeline.
4097//
4098// We use the following execution domain numbering:
4099//
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004100enum ARMExeDomain {
4101 ExeGeneric = 0,
4102 ExeVFP = 1,
4103 ExeNEON = 2
4104};
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004105//
4106// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4107//
4108std::pair<uint16_t, uint16_t>
4109ARMBaseInstrInfo::getExecutionDomain(const MachineInstr *MI) const {
Eric Christopher7e70aba2015-03-07 00:12:22 +00004110 // If we don't have access to NEON instructions then we won't be able
4111 // to swizzle anything to the NEON domain. Check to make sure.
4112 if (Subtarget.hasNEON()) {
4113 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4114 // if they are not predicated.
4115 if (MI->getOpcode() == ARM::VMOVD && !isPredicated(MI))
4116 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004117
Eric Christopher7e70aba2015-03-07 00:12:22 +00004118 // CortexA9 is particularly picky about mixing the two and wants these
4119 // converted.
4120 if (Subtarget.isCortexA9() && !isPredicated(MI) &&
4121 (MI->getOpcode() == ARM::VMOVRS || MI->getOpcode() == ARM::VMOVSR ||
4122 MI->getOpcode() == ARM::VMOVS))
4123 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4124 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004125 // No other instructions can be swizzled, so just determine their domain.
4126 unsigned Domain = MI->getDesc().TSFlags & ARMII::DomainMask;
4127
4128 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004129 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004130
4131 // Certain instructions can go either way on Cortex-A8.
4132 // Treat them as NEON instructions.
4133 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004134 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004135
4136 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004137 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004138
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004139 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004140}
4141
Tim Northover771f1602012-08-29 16:36:07 +00004142static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4143 unsigned SReg, unsigned &Lane) {
4144 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4145 Lane = 0;
4146
4147 if (DReg != ARM::NoRegister)
4148 return DReg;
4149
4150 Lane = 1;
4151 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4152
4153 assert(DReg && "S-register with no D super-register?");
4154 return DReg;
4155}
4156
Andrew Trickd9296ec2012-10-10 05:43:01 +00004157/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloyea052562012-09-18 08:31:15 +00004158/// set ImplicitSReg to a register number that must be marked as implicit-use or
4159/// zero if no register needs to be defined as implicit-use.
4160///
4161/// If the function cannot determine if an SPR should be marked implicit use or
4162/// not, it returns false.
4163///
4164/// This function handles cases where an instruction is being modified from taking
Andrew Trickd9296ec2012-10-10 05:43:01 +00004165/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloyea052562012-09-18 08:31:15 +00004166/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4167/// lane of the DPR).
4168///
4169/// If the other SPR is defined, an implicit-use of it should be added. Else,
4170/// (including the case where the DPR itself is defined), it should not.
Andrew Trickd9296ec2012-10-10 05:43:01 +00004171///
James Molloyea052562012-09-18 08:31:15 +00004172static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
4173 MachineInstr *MI,
4174 unsigned DReg, unsigned Lane,
4175 unsigned &ImplicitSReg) {
4176 // If the DPR is defined or used already, the other SPR lane will be chained
4177 // correctly, so there is nothing to be done.
4178 if (MI->definesRegister(DReg, TRI) || MI->readsRegister(DReg, TRI)) {
4179 ImplicitSReg = 0;
4180 return true;
4181 }
4182
4183 // Otherwise we need to go searching to see if the SPR is set explicitly.
4184 ImplicitSReg = TRI->getSubReg(DReg,
4185 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4186 MachineBasicBlock::LivenessQueryResult LQR =
4187 MI->getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
4188
4189 if (LQR == MachineBasicBlock::LQR_Live)
4190 return true;
4191 else if (LQR == MachineBasicBlock::LQR_Unknown)
4192 return false;
4193
4194 // If the register is known not to be live, there is no need to add an
4195 // implicit-use.
4196 ImplicitSReg = 0;
4197 return true;
4198}
Tim Northover771f1602012-08-29 16:36:07 +00004199
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004200void
4201ARMBaseInstrInfo::setExecutionDomain(MachineInstr *MI, unsigned Domain) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004202 unsigned DstReg, SrcReg, DReg;
4203 unsigned Lane;
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00004204 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Tim Northoverf6618152012-08-17 11:32:52 +00004205 const TargetRegisterInfo *TRI = &getRegisterInfo();
Tim Northoverf6618152012-08-17 11:32:52 +00004206 switch (MI->getOpcode()) {
4207 default:
4208 llvm_unreachable("cannot handle opcode!");
4209 break;
4210 case ARM::VMOVD:
4211 if (Domain != ExeNEON)
4212 break;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004213
Tim Northoverf6618152012-08-17 11:32:52 +00004214 // Zap the predicate operands.
4215 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004216
Eric Christopher7e70aba2015-03-07 00:12:22 +00004217 // Make sure we've got NEON instructions.
4218 assert(Subtarget.hasNEON() && "VORRd requires NEON");
4219
Tim Northover771f1602012-08-29 16:36:07 +00004220 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4221 DstReg = MI->getOperand(0).getReg();
4222 SrcReg = MI->getOperand(1).getReg();
4223
4224 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4225 MI->RemoveOperand(i-1);
4226
4227 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004228 MI->setDesc(get(ARM::VORRd));
Tim Northover771f1602012-08-29 16:36:07 +00004229 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4230 .addReg(SrcReg)
4231 .addReg(SrcReg));
Tim Northoverf6618152012-08-17 11:32:52 +00004232 break;
4233 case ARM::VMOVRS:
4234 if (Domain != ExeNEON)
4235 break;
4236 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
4237
Tim Northover771f1602012-08-29 16:36:07 +00004238 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004239 DstReg = MI->getOperand(0).getReg();
4240 SrcReg = MI->getOperand(1).getReg();
4241
Tim Northover771f1602012-08-29 16:36:07 +00004242 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4243 MI->RemoveOperand(i-1);
Tim Northoverf6618152012-08-17 11:32:52 +00004244
Tim Northover771f1602012-08-29 16:36:07 +00004245 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northoverf6618152012-08-17 11:32:52 +00004246
Tim Northover771f1602012-08-29 16:36:07 +00004247 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4248 // Note that DSrc has been widened and the other lane may be undef, which
4249 // contaminates the entire register.
Tim Northoverf6618152012-08-17 11:32:52 +00004250 MI->setDesc(get(ARM::VGETLNi32));
Tim Northover771f1602012-08-29 16:36:07 +00004251 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4252 .addReg(DReg, RegState::Undef)
4253 .addImm(Lane));
Tim Northoverf6618152012-08-17 11:32:52 +00004254
Tim Northover771f1602012-08-29 16:36:07 +00004255 // The old source should be an implicit use, otherwise we might think it
4256 // was dead before here.
Tim Northoverf6618152012-08-17 11:32:52 +00004257 MIB.addReg(SrcReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004258 break;
James Molloyea052562012-09-18 08:31:15 +00004259 case ARM::VMOVSR: {
Tim Northoverf6618152012-08-17 11:32:52 +00004260 if (Domain != ExeNEON)
4261 break;
4262 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
4263
Tim Northover771f1602012-08-29 16:36:07 +00004264 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004265 DstReg = MI->getOperand(0).getReg();
4266 SrcReg = MI->getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004267
Tim Northover771f1602012-08-29 16:36:07 +00004268 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
4269
James Molloyea052562012-09-18 08:31:15 +00004270 unsigned ImplicitSReg;
4271 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
4272 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004273
Tim Northoverc8d867d2012-09-05 18:37:53 +00004274 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4275 MI->RemoveOperand(i-1);
4276
Tim Northover771f1602012-08-29 16:36:07 +00004277 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4278 // Again DDst may be undefined at the beginning of this instruction.
Tim Northoverf6618152012-08-17 11:32:52 +00004279 MI->setDesc(get(ARM::VSETLNi32));
Tim Northover726d32c2012-09-01 18:07:29 +00004280 MIB.addReg(DReg, RegState::Define)
4281 .addReg(DReg, getUndefRegState(!MI->readsRegister(DReg, TRI)))
4282 .addReg(SrcReg)
4283 .addImm(Lane);
4284 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004285
Tim Northover726d32c2012-09-01 18:07:29 +00004286 // The narrower destination must be marked as set to keep previous chains
4287 // in place.
Tim Northover771f1602012-08-29 16:36:07 +00004288 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004289 if (ImplicitSReg != 0)
4290 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004291 break;
James Molloyea052562012-09-18 08:31:15 +00004292 }
Tim Northoverca9f3842012-08-30 10:17:45 +00004293 case ARM::VMOVS: {
4294 if (Domain != ExeNEON)
4295 break;
4296
4297 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
4298 DstReg = MI->getOperand(0).getReg();
4299 SrcReg = MI->getOperand(1).getReg();
4300
Tim Northoverca9f3842012-08-30 10:17:45 +00004301 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4302 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4303 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4304
James Molloyea052562012-09-18 08:31:15 +00004305 unsigned ImplicitSReg;
4306 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4307 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004308
Tim Northoverc8d867d2012-09-05 18:37:53 +00004309 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4310 MI->RemoveOperand(i-1);
4311
Tim Northoverca9f3842012-08-30 10:17:45 +00004312 if (DSrc == DDst) {
4313 // Destination can be:
4314 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
4315 MI->setDesc(get(ARM::VDUPLN32d));
Tim Northover726d32c2012-09-01 18:07:29 +00004316 MIB.addReg(DDst, RegState::Define)
4317 .addReg(DDst, getUndefRegState(!MI->readsRegister(DDst, TRI)))
4318 .addImm(SrcLane);
4319 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004320
4321 // Neither the source or the destination are naturally represented any
4322 // more, so add them in manually.
4323 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4324 MIB.addReg(SrcReg, RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004325 if (ImplicitSReg != 0)
4326 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004327 break;
4328 }
4329
4330 // In general there's no single instruction that can perform an S <-> S
4331 // move in NEON space, but a pair of VEXT instructions *can* do the
4332 // job. It turns out that the VEXTs needed will only use DSrc once, with
4333 // the position based purely on the combination of lane-0 and lane-1
4334 // involved. For example
4335 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4336 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4337 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4338 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4339 //
4340 // Pattern of the MachineInstrs is:
4341 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4342 MachineInstrBuilder NewMIB;
4343 NewMIB = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4344 get(ARM::VEXTd32), DDst);
Tim Northover726d32c2012-09-01 18:07:29 +00004345
4346 // On the first instruction, both DSrc and DDst may be <undef> if present.
4347 // Specifically when the original instruction didn't have them as an
4348 // <imp-use>.
4349 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
4350 bool CurUndef = !MI->readsRegister(CurReg, TRI);
4351 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4352
4353 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
4354 CurUndef = !MI->readsRegister(CurReg, TRI);
4355 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4356
Tim Northoverca9f3842012-08-30 10:17:45 +00004357 NewMIB.addImm(1);
4358 AddDefaultPred(NewMIB);
4359
4360 if (SrcLane == DstLane)
4361 NewMIB.addReg(SrcReg, RegState::Implicit);
4362
4363 MI->setDesc(get(ARM::VEXTd32));
4364 MIB.addReg(DDst, RegState::Define);
Tim Northover726d32c2012-09-01 18:07:29 +00004365
4366 // On the second instruction, DDst has definitely been defined above, so
4367 // it is not <undef>. DSrc, if present, can be <undef> as above.
4368 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
4369 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4370 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4371
4372 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
4373 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4374 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4375
Tim Northoverca9f3842012-08-30 10:17:45 +00004376 MIB.addImm(1);
4377 AddDefaultPred(MIB);
4378
4379 if (SrcLane != DstLane)
4380 MIB.addReg(SrcReg, RegState::Implicit);
4381
4382 // As before, the original destination is no longer represented, add it
4383 // implicitly.
4384 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004385 if (ImplicitSReg != 0)
4386 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004387 break;
4388 }
Tim Northoverf6618152012-08-17 11:32:52 +00004389 }
4390
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004391}
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004392
Bob Wilsone8a549c2012-09-29 21:43:49 +00004393//===----------------------------------------------------------------------===//
4394// Partial register updates
4395//===----------------------------------------------------------------------===//
4396//
4397// Swift renames NEON registers with 64-bit granularity. That means any
4398// instruction writing an S-reg implicitly reads the containing D-reg. The
4399// problem is mostly avoided by translating f32 operations to v2f32 operations
4400// on D-registers, but f32 loads are still a problem.
4401//
4402// These instructions can load an f32 into a NEON register:
4403//
4404// VLDRS - Only writes S, partial D update.
4405// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4406// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4407//
4408// FCONSTD can be used as a dependency-breaking instruction.
Bob Wilsone8a549c2012-09-29 21:43:49 +00004409unsigned ARMBaseInstrInfo::
4410getPartialRegUpdateClearance(const MachineInstr *MI,
4411 unsigned OpNum,
4412 const TargetRegisterInfo *TRI) const {
Silviu Barangadc453362013-03-27 12:38:44 +00004413 if (!SwiftPartialUpdateClearance ||
4414 !(Subtarget.isSwift() || Subtarget.isCortexA15()))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004415 return 0;
4416
4417 assert(TRI && "Need TRI instance");
4418
4419 const MachineOperand &MO = MI->getOperand(OpNum);
4420 if (MO.readsReg())
4421 return 0;
4422 unsigned Reg = MO.getReg();
4423 int UseOp = -1;
4424
4425 switch(MI->getOpcode()) {
4426 // Normal instructions writing only an S-register.
4427 case ARM::VLDRS:
4428 case ARM::FCONSTS:
4429 case ARM::VMOVSR:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004430 case ARM::VMOVv8i8:
4431 case ARM::VMOVv4i16:
4432 case ARM::VMOVv2i32:
4433 case ARM::VMOVv2f32:
4434 case ARM::VMOVv1i64:
4435 UseOp = MI->findRegisterUseOperandIdx(Reg, false, TRI);
4436 break;
4437
4438 // Explicitly reads the dependency.
4439 case ARM::VLD1LNd32:
Silviu Barangadc453362013-03-27 12:38:44 +00004440 UseOp = 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004441 break;
4442 default:
4443 return 0;
4444 }
4445
4446 // If this instruction actually reads a value from Reg, there is no unwanted
4447 // dependency.
4448 if (UseOp != -1 && MI->getOperand(UseOp).readsReg())
4449 return 0;
4450
4451 // We must be able to clobber the whole D-reg.
4452 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4453 // Virtual register must be a foo:ssub_0<def,undef> operand.
4454 if (!MO.getSubReg() || MI->readsVirtualRegister(Reg))
4455 return 0;
4456 } else if (ARM::SPRRegClass.contains(Reg)) {
4457 // Physical register: MI must define the full D-reg.
4458 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4459 &ARM::DPRRegClass);
4460 if (!DReg || !MI->definesRegister(DReg, TRI))
4461 return 0;
4462 }
4463
4464 // MI has an unwanted D-register dependency.
4465 // Avoid defs in the previous N instructrions.
4466 return SwiftPartialUpdateClearance;
4467}
4468
4469// Break a partial register dependency after getPartialRegUpdateClearance
4470// returned non-zero.
4471void ARMBaseInstrInfo::
4472breakPartialRegDependency(MachineBasicBlock::iterator MI,
4473 unsigned OpNum,
4474 const TargetRegisterInfo *TRI) const {
4475 assert(MI && OpNum < MI->getDesc().getNumDefs() && "OpNum is not a def");
4476 assert(TRI && "Need TRI instance");
4477
4478 const MachineOperand &MO = MI->getOperand(OpNum);
4479 unsigned Reg = MO.getReg();
4480 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4481 "Can't break virtual register dependencies.");
4482 unsigned DReg = Reg;
4483
4484 // If MI defines an S-reg, find the corresponding D super-register.
4485 if (ARM::SPRRegClass.contains(Reg)) {
4486 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4487 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4488 }
4489
4490 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
4491 assert(MI->definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
4492
4493 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4494 // the full D-register by loading the same value to both lanes. The
4495 // instruction is micro-coded with 2 uops, so don't do this until we can
Robert Wilhelm516be562013-09-14 09:34:24 +00004496 // properly schedule micro-coded instructions. The dispatcher stalls cause
Bob Wilsone8a549c2012-09-29 21:43:49 +00004497 // too big regressions.
4498
4499 // Insert the dependency-breaking FCONSTD before MI.
4500 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
4501 AddDefaultPred(BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4502 get(ARM::FCONSTD), DReg).addImm(96));
4503 MI->addRegisterKilled(DReg, TRI, true);
4504}
4505
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004506bool ARMBaseInstrInfo::hasNOP() const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +00004507 return Subtarget.getFeatureBits()[ARM::HasV6KOps];
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004508}
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004509
4510bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
Arnold Schwaighofere9375922013-06-05 14:59:36 +00004511 if (MI->getNumOperands() < 4)
4512 return true;
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004513 unsigned ShOpVal = MI->getOperand(3).getImm();
4514 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4515 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4516 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4517 ((ShImm == 1 || ShImm == 2) &&
4518 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
4519 return true;
4520
4521 return false;
4522}
Quentin Colombetd358e842014-08-22 18:05:22 +00004523
4524bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
4525 const MachineInstr &MI, unsigned DefIdx,
4526 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
4527 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4528 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
4529
4530 switch (MI.getOpcode()) {
4531 case ARM::VMOVDRR:
4532 // dX = VMOVDRR rY, rZ
4533 // is the same as:
4534 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
4535 // Populate the InputRegs accordingly.
4536 // rY
4537 const MachineOperand *MOReg = &MI.getOperand(1);
4538 InputRegs.push_back(
4539 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0));
4540 // rZ
4541 MOReg = &MI.getOperand(2);
4542 InputRegs.push_back(
4543 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1));
4544 return true;
4545 }
4546 llvm_unreachable("Target dependent opcode missing");
4547}
4548
4549bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
4550 const MachineInstr &MI, unsigned DefIdx,
4551 RegSubRegPairAndIdx &InputReg) const {
4552 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4553 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
4554
4555 switch (MI.getOpcode()) {
4556 case ARM::VMOVRRD:
4557 // rX, rY = VMOVRRD dZ
4558 // is the same as:
4559 // rX = EXTRACT_SUBREG dZ, ssub_0
4560 // rY = EXTRACT_SUBREG dZ, ssub_1
4561 const MachineOperand &MOReg = MI.getOperand(2);
4562 InputReg.Reg = MOReg.getReg();
4563 InputReg.SubReg = MOReg.getSubReg();
4564 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
4565 return true;
4566 }
4567 llvm_unreachable("Target dependent opcode missing");
4568}
4569
4570bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
4571 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
4572 RegSubRegPairAndIdx &InsertedReg) const {
4573 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4574 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
4575
4576 switch (MI.getOpcode()) {
4577 case ARM::VSETLNi32:
4578 // dX = VSETLNi32 dY, rZ, imm
4579 const MachineOperand &MOBaseReg = MI.getOperand(1);
4580 const MachineOperand &MOInsertedReg = MI.getOperand(2);
4581 const MachineOperand &MOIndex = MI.getOperand(3);
4582 BaseReg.Reg = MOBaseReg.getReg();
4583 BaseReg.SubReg = MOBaseReg.getSubReg();
4584
4585 InsertedReg.Reg = MOInsertedReg.getReg();
4586 InsertedReg.SubReg = MOInsertedReg.getSubReg();
4587 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
4588 return true;
4589 }
4590 llvm_unreachable("Target dependent opcode missing");
4591}